vmw_pvscsi.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * Linux driver for VMware's para-virtualized SCSI HBA.
  3. *
  4. * Copyright (C) 2008-2014, VMware, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more
  14. * details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Maintained by: Jim Gill <jgill@vmware.com>
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/pci.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include "vmw_pvscsi.h"
  35. #define PVSCSI_LINUX_DRIVER_DESC "VMware PVSCSI driver"
  36. MODULE_DESCRIPTION(PVSCSI_LINUX_DRIVER_DESC);
  37. MODULE_AUTHOR("VMware, Inc.");
  38. MODULE_LICENSE("GPL");
  39. MODULE_VERSION(PVSCSI_DRIVER_VERSION_STRING);
  40. #define PVSCSI_DEFAULT_NUM_PAGES_PER_RING 8
  41. #define PVSCSI_DEFAULT_NUM_PAGES_MSG_RING 1
  42. #define PVSCSI_DEFAULT_QUEUE_DEPTH 254
  43. #define SGL_SIZE PAGE_SIZE
  44. struct pvscsi_sg_list {
  45. struct PVSCSISGElement sge[PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT];
  46. };
  47. struct pvscsi_ctx {
  48. /*
  49. * The index of the context in cmd_map serves as the context ID for a
  50. * 1-to-1 mapping completions back to requests.
  51. */
  52. struct scsi_cmnd *cmd;
  53. struct pvscsi_sg_list *sgl;
  54. struct list_head list;
  55. dma_addr_t dataPA;
  56. dma_addr_t sensePA;
  57. dma_addr_t sglPA;
  58. struct completion *abort_cmp;
  59. };
  60. struct pvscsi_adapter {
  61. char *mmioBase;
  62. u8 rev;
  63. bool use_msg;
  64. bool use_req_threshold;
  65. spinlock_t hw_lock;
  66. struct workqueue_struct *workqueue;
  67. struct work_struct work;
  68. struct PVSCSIRingReqDesc *req_ring;
  69. unsigned req_pages;
  70. unsigned req_depth;
  71. dma_addr_t reqRingPA;
  72. struct PVSCSIRingCmpDesc *cmp_ring;
  73. unsigned cmp_pages;
  74. dma_addr_t cmpRingPA;
  75. struct PVSCSIRingMsgDesc *msg_ring;
  76. unsigned msg_pages;
  77. dma_addr_t msgRingPA;
  78. struct PVSCSIRingsState *rings_state;
  79. dma_addr_t ringStatePA;
  80. struct pci_dev *dev;
  81. struct Scsi_Host *host;
  82. struct list_head cmd_pool;
  83. struct pvscsi_ctx *cmd_map;
  84. };
  85. /* Command line parameters */
  86. static int pvscsi_ring_pages;
  87. static int pvscsi_msg_ring_pages = PVSCSI_DEFAULT_NUM_PAGES_MSG_RING;
  88. static int pvscsi_cmd_per_lun = PVSCSI_DEFAULT_QUEUE_DEPTH;
  89. static bool pvscsi_disable_msi;
  90. static bool pvscsi_disable_msix;
  91. static bool pvscsi_use_msg = true;
  92. static bool pvscsi_use_req_threshold = true;
  93. #define PVSCSI_RW (S_IRUSR | S_IWUSR)
  94. module_param_named(ring_pages, pvscsi_ring_pages, int, PVSCSI_RW);
  95. MODULE_PARM_DESC(ring_pages, "Number of pages per req/cmp ring - (default="
  96. __stringify(PVSCSI_DEFAULT_NUM_PAGES_PER_RING)
  97. "[up to 16 targets],"
  98. __stringify(PVSCSI_SETUP_RINGS_MAX_NUM_PAGES)
  99. "[for 16+ targets])");
  100. module_param_named(msg_ring_pages, pvscsi_msg_ring_pages, int, PVSCSI_RW);
  101. MODULE_PARM_DESC(msg_ring_pages, "Number of pages for the msg ring - (default="
  102. __stringify(PVSCSI_DEFAULT_NUM_PAGES_MSG_RING) ")");
  103. module_param_named(cmd_per_lun, pvscsi_cmd_per_lun, int, PVSCSI_RW);
  104. MODULE_PARM_DESC(cmd_per_lun, "Maximum commands per lun - (default="
  105. __stringify(PVSCSI_DEFAULT_QUEUE_DEPTH) ")");
  106. module_param_named(disable_msi, pvscsi_disable_msi, bool, PVSCSI_RW);
  107. MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
  108. module_param_named(disable_msix, pvscsi_disable_msix, bool, PVSCSI_RW);
  109. MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
  110. module_param_named(use_msg, pvscsi_use_msg, bool, PVSCSI_RW);
  111. MODULE_PARM_DESC(use_msg, "Use msg ring when available - (default=1)");
  112. module_param_named(use_req_threshold, pvscsi_use_req_threshold,
  113. bool, PVSCSI_RW);
  114. MODULE_PARM_DESC(use_req_threshold, "Use driver-based request coalescing if configured - (default=1)");
  115. static const struct pci_device_id pvscsi_pci_tbl[] = {
  116. { PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_PVSCSI) },
  117. { 0 }
  118. };
  119. MODULE_DEVICE_TABLE(pci, pvscsi_pci_tbl);
  120. static struct device *
  121. pvscsi_dev(const struct pvscsi_adapter *adapter)
  122. {
  123. return &(adapter->dev->dev);
  124. }
  125. static struct pvscsi_ctx *
  126. pvscsi_find_context(const struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
  127. {
  128. struct pvscsi_ctx *ctx, *end;
  129. end = &adapter->cmd_map[adapter->req_depth];
  130. for (ctx = adapter->cmd_map; ctx < end; ctx++)
  131. if (ctx->cmd == cmd)
  132. return ctx;
  133. return NULL;
  134. }
  135. static struct pvscsi_ctx *
  136. pvscsi_acquire_context(struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
  137. {
  138. struct pvscsi_ctx *ctx;
  139. if (list_empty(&adapter->cmd_pool))
  140. return NULL;
  141. ctx = list_first_entry(&adapter->cmd_pool, struct pvscsi_ctx, list);
  142. ctx->cmd = cmd;
  143. list_del(&ctx->list);
  144. return ctx;
  145. }
  146. static void pvscsi_release_context(struct pvscsi_adapter *adapter,
  147. struct pvscsi_ctx *ctx)
  148. {
  149. ctx->cmd = NULL;
  150. ctx->abort_cmp = NULL;
  151. list_add(&ctx->list, &adapter->cmd_pool);
  152. }
  153. /*
  154. * Map a pvscsi_ctx struct to a context ID field value; we map to a simple
  155. * non-zero integer. ctx always points to an entry in cmd_map array, hence
  156. * the return value is always >=1.
  157. */
  158. static u64 pvscsi_map_context(const struct pvscsi_adapter *adapter,
  159. const struct pvscsi_ctx *ctx)
  160. {
  161. return ctx - adapter->cmd_map + 1;
  162. }
  163. static struct pvscsi_ctx *
  164. pvscsi_get_context(const struct pvscsi_adapter *adapter, u64 context)
  165. {
  166. return &adapter->cmd_map[context - 1];
  167. }
  168. static void pvscsi_reg_write(const struct pvscsi_adapter *adapter,
  169. u32 offset, u32 val)
  170. {
  171. writel(val, adapter->mmioBase + offset);
  172. }
  173. static u32 pvscsi_reg_read(const struct pvscsi_adapter *adapter, u32 offset)
  174. {
  175. return readl(adapter->mmioBase + offset);
  176. }
  177. static u32 pvscsi_read_intr_status(const struct pvscsi_adapter *adapter)
  178. {
  179. return pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_INTR_STATUS);
  180. }
  181. static void pvscsi_write_intr_status(const struct pvscsi_adapter *adapter,
  182. u32 val)
  183. {
  184. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_STATUS, val);
  185. }
  186. static void pvscsi_unmask_intr(const struct pvscsi_adapter *adapter)
  187. {
  188. u32 intr_bits;
  189. intr_bits = PVSCSI_INTR_CMPL_MASK;
  190. if (adapter->use_msg)
  191. intr_bits |= PVSCSI_INTR_MSG_MASK;
  192. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, intr_bits);
  193. }
  194. static void pvscsi_mask_intr(const struct pvscsi_adapter *adapter)
  195. {
  196. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, 0);
  197. }
  198. static void pvscsi_write_cmd_desc(const struct pvscsi_adapter *adapter,
  199. u32 cmd, const void *desc, size_t len)
  200. {
  201. const u32 *ptr = desc;
  202. size_t i;
  203. len /= sizeof(*ptr);
  204. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND, cmd);
  205. for (i = 0; i < len; i++)
  206. pvscsi_reg_write(adapter,
  207. PVSCSI_REG_OFFSET_COMMAND_DATA, ptr[i]);
  208. }
  209. static void pvscsi_abort_cmd(const struct pvscsi_adapter *adapter,
  210. const struct pvscsi_ctx *ctx)
  211. {
  212. struct PVSCSICmdDescAbortCmd cmd = { 0 };
  213. cmd.target = ctx->cmd->device->id;
  214. cmd.context = pvscsi_map_context(adapter, ctx);
  215. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ABORT_CMD, &cmd, sizeof(cmd));
  216. }
  217. static void pvscsi_kick_rw_io(const struct pvscsi_adapter *adapter)
  218. {
  219. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_RW_IO, 0);
  220. }
  221. static void pvscsi_process_request_ring(const struct pvscsi_adapter *adapter)
  222. {
  223. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_NON_RW_IO, 0);
  224. }
  225. static int scsi_is_rw(unsigned char op)
  226. {
  227. return op == READ_6 || op == WRITE_6 ||
  228. op == READ_10 || op == WRITE_10 ||
  229. op == READ_12 || op == WRITE_12 ||
  230. op == READ_16 || op == WRITE_16;
  231. }
  232. static void pvscsi_kick_io(const struct pvscsi_adapter *adapter,
  233. unsigned char op)
  234. {
  235. if (scsi_is_rw(op)) {
  236. struct PVSCSIRingsState *s = adapter->rings_state;
  237. if (!adapter->use_req_threshold ||
  238. s->reqProdIdx - s->reqConsIdx >= s->reqCallThreshold)
  239. pvscsi_kick_rw_io(adapter);
  240. } else {
  241. pvscsi_process_request_ring(adapter);
  242. }
  243. }
  244. static void ll_adapter_reset(const struct pvscsi_adapter *adapter)
  245. {
  246. dev_dbg(pvscsi_dev(adapter), "Adapter Reset on %p\n", adapter);
  247. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ADAPTER_RESET, NULL, 0);
  248. }
  249. static void ll_bus_reset(const struct pvscsi_adapter *adapter)
  250. {
  251. dev_dbg(pvscsi_dev(adapter), "Resetting bus on %p\n", adapter);
  252. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_BUS, NULL, 0);
  253. }
  254. static void ll_device_reset(const struct pvscsi_adapter *adapter, u32 target)
  255. {
  256. struct PVSCSICmdDescResetDevice cmd = { 0 };
  257. dev_dbg(pvscsi_dev(adapter), "Resetting device: target=%u\n", target);
  258. cmd.target = target;
  259. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_DEVICE,
  260. &cmd, sizeof(cmd));
  261. }
  262. static void pvscsi_create_sg(struct pvscsi_ctx *ctx,
  263. struct scatterlist *sg, unsigned count)
  264. {
  265. unsigned i;
  266. struct PVSCSISGElement *sge;
  267. BUG_ON(count > PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT);
  268. sge = &ctx->sgl->sge[0];
  269. for (i = 0; i < count; i++, sg++) {
  270. sge[i].addr = sg_dma_address(sg);
  271. sge[i].length = sg_dma_len(sg);
  272. sge[i].flags = 0;
  273. }
  274. }
  275. /*
  276. * Map all data buffers for a command into PCI space and
  277. * setup the scatter/gather list if needed.
  278. */
  279. static int pvscsi_map_buffers(struct pvscsi_adapter *adapter,
  280. struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd,
  281. struct PVSCSIRingReqDesc *e)
  282. {
  283. unsigned count;
  284. unsigned bufflen = scsi_bufflen(cmd);
  285. struct scatterlist *sg;
  286. e->dataLen = bufflen;
  287. e->dataAddr = 0;
  288. if (bufflen == 0)
  289. return 0;
  290. sg = scsi_sglist(cmd);
  291. count = scsi_sg_count(cmd);
  292. if (count != 0) {
  293. int segs = scsi_dma_map(cmd);
  294. if (segs == -ENOMEM) {
  295. scmd_printk(KERN_ERR, cmd,
  296. "vmw_pvscsi: Failed to map cmd sglist for DMA.\n");
  297. return -ENOMEM;
  298. } else if (segs > 1) {
  299. pvscsi_create_sg(ctx, sg, segs);
  300. e->flags |= PVSCSI_FLAG_CMD_WITH_SG_LIST;
  301. ctx->sglPA = pci_map_single(adapter->dev, ctx->sgl,
  302. SGL_SIZE, PCI_DMA_TODEVICE);
  303. if (pci_dma_mapping_error(adapter->dev, ctx->sglPA)) {
  304. scmd_printk(KERN_ERR, cmd,
  305. "vmw_pvscsi: Failed to map ctx sglist for DMA.\n");
  306. scsi_dma_unmap(cmd);
  307. ctx->sglPA = 0;
  308. return -ENOMEM;
  309. }
  310. e->dataAddr = ctx->sglPA;
  311. } else
  312. e->dataAddr = sg_dma_address(sg);
  313. } else {
  314. /*
  315. * In case there is no S/G list, scsi_sglist points
  316. * directly to the buffer.
  317. */
  318. ctx->dataPA = pci_map_single(adapter->dev, sg, bufflen,
  319. cmd->sc_data_direction);
  320. if (pci_dma_mapping_error(adapter->dev, ctx->dataPA)) {
  321. scmd_printk(KERN_ERR, cmd,
  322. "vmw_pvscsi: Failed to map direct data buffer for DMA.\n");
  323. return -ENOMEM;
  324. }
  325. e->dataAddr = ctx->dataPA;
  326. }
  327. return 0;
  328. }
  329. static void pvscsi_unmap_buffers(const struct pvscsi_adapter *adapter,
  330. struct pvscsi_ctx *ctx)
  331. {
  332. struct scsi_cmnd *cmd;
  333. unsigned bufflen;
  334. cmd = ctx->cmd;
  335. bufflen = scsi_bufflen(cmd);
  336. if (bufflen != 0) {
  337. unsigned count = scsi_sg_count(cmd);
  338. if (count != 0) {
  339. scsi_dma_unmap(cmd);
  340. if (ctx->sglPA) {
  341. pci_unmap_single(adapter->dev, ctx->sglPA,
  342. SGL_SIZE, PCI_DMA_TODEVICE);
  343. ctx->sglPA = 0;
  344. }
  345. } else
  346. pci_unmap_single(adapter->dev, ctx->dataPA, bufflen,
  347. cmd->sc_data_direction);
  348. }
  349. if (cmd->sense_buffer)
  350. pci_unmap_single(adapter->dev, ctx->sensePA,
  351. SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
  352. }
  353. static int pvscsi_allocate_rings(struct pvscsi_adapter *adapter)
  354. {
  355. adapter->rings_state = pci_alloc_consistent(adapter->dev, PAGE_SIZE,
  356. &adapter->ringStatePA);
  357. if (!adapter->rings_state)
  358. return -ENOMEM;
  359. adapter->req_pages = min(PVSCSI_MAX_NUM_PAGES_REQ_RING,
  360. pvscsi_ring_pages);
  361. adapter->req_depth = adapter->req_pages
  362. * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
  363. adapter->req_ring = pci_alloc_consistent(adapter->dev,
  364. adapter->req_pages * PAGE_SIZE,
  365. &adapter->reqRingPA);
  366. if (!adapter->req_ring)
  367. return -ENOMEM;
  368. adapter->cmp_pages = min(PVSCSI_MAX_NUM_PAGES_CMP_RING,
  369. pvscsi_ring_pages);
  370. adapter->cmp_ring = pci_alloc_consistent(adapter->dev,
  371. adapter->cmp_pages * PAGE_SIZE,
  372. &adapter->cmpRingPA);
  373. if (!adapter->cmp_ring)
  374. return -ENOMEM;
  375. BUG_ON(!IS_ALIGNED(adapter->ringStatePA, PAGE_SIZE));
  376. BUG_ON(!IS_ALIGNED(adapter->reqRingPA, PAGE_SIZE));
  377. BUG_ON(!IS_ALIGNED(adapter->cmpRingPA, PAGE_SIZE));
  378. if (!adapter->use_msg)
  379. return 0;
  380. adapter->msg_pages = min(PVSCSI_MAX_NUM_PAGES_MSG_RING,
  381. pvscsi_msg_ring_pages);
  382. adapter->msg_ring = pci_alloc_consistent(adapter->dev,
  383. adapter->msg_pages * PAGE_SIZE,
  384. &adapter->msgRingPA);
  385. if (!adapter->msg_ring)
  386. return -ENOMEM;
  387. BUG_ON(!IS_ALIGNED(adapter->msgRingPA, PAGE_SIZE));
  388. return 0;
  389. }
  390. static void pvscsi_setup_all_rings(const struct pvscsi_adapter *adapter)
  391. {
  392. struct PVSCSICmdDescSetupRings cmd = { 0 };
  393. dma_addr_t base;
  394. unsigned i;
  395. cmd.ringsStatePPN = adapter->ringStatePA >> PAGE_SHIFT;
  396. cmd.reqRingNumPages = adapter->req_pages;
  397. cmd.cmpRingNumPages = adapter->cmp_pages;
  398. base = adapter->reqRingPA;
  399. for (i = 0; i < adapter->req_pages; i++) {
  400. cmd.reqRingPPNs[i] = base >> PAGE_SHIFT;
  401. base += PAGE_SIZE;
  402. }
  403. base = adapter->cmpRingPA;
  404. for (i = 0; i < adapter->cmp_pages; i++) {
  405. cmd.cmpRingPPNs[i] = base >> PAGE_SHIFT;
  406. base += PAGE_SIZE;
  407. }
  408. memset(adapter->rings_state, 0, PAGE_SIZE);
  409. memset(adapter->req_ring, 0, adapter->req_pages * PAGE_SIZE);
  410. memset(adapter->cmp_ring, 0, adapter->cmp_pages * PAGE_SIZE);
  411. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_RINGS,
  412. &cmd, sizeof(cmd));
  413. if (adapter->use_msg) {
  414. struct PVSCSICmdDescSetupMsgRing cmd_msg = { 0 };
  415. cmd_msg.numPages = adapter->msg_pages;
  416. base = adapter->msgRingPA;
  417. for (i = 0; i < adapter->msg_pages; i++) {
  418. cmd_msg.ringPPNs[i] = base >> PAGE_SHIFT;
  419. base += PAGE_SIZE;
  420. }
  421. memset(adapter->msg_ring, 0, adapter->msg_pages * PAGE_SIZE);
  422. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_MSG_RING,
  423. &cmd_msg, sizeof(cmd_msg));
  424. }
  425. }
  426. static int pvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  427. {
  428. if (!sdev->tagged_supported)
  429. qdepth = 1;
  430. return scsi_change_queue_depth(sdev, qdepth);
  431. }
  432. /*
  433. * Pull a completion descriptor off and pass the completion back
  434. * to the SCSI mid layer.
  435. */
  436. static void pvscsi_complete_request(struct pvscsi_adapter *adapter,
  437. const struct PVSCSIRingCmpDesc *e)
  438. {
  439. struct pvscsi_ctx *ctx;
  440. struct scsi_cmnd *cmd;
  441. struct completion *abort_cmp;
  442. u32 btstat = e->hostStatus;
  443. u32 sdstat = e->scsiStatus;
  444. ctx = pvscsi_get_context(adapter, e->context);
  445. cmd = ctx->cmd;
  446. abort_cmp = ctx->abort_cmp;
  447. pvscsi_unmap_buffers(adapter, ctx);
  448. pvscsi_release_context(adapter, ctx);
  449. if (abort_cmp) {
  450. /*
  451. * The command was requested to be aborted. Just signal that
  452. * the request completed and swallow the actual cmd completion
  453. * here. The abort handler will post a completion for this
  454. * command indicating that it got successfully aborted.
  455. */
  456. complete(abort_cmp);
  457. return;
  458. }
  459. cmd->result = 0;
  460. if (sdstat != SAM_STAT_GOOD &&
  461. (btstat == BTSTAT_SUCCESS ||
  462. btstat == BTSTAT_LINKED_COMMAND_COMPLETED ||
  463. btstat == BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG)) {
  464. if (sdstat == SAM_STAT_COMMAND_TERMINATED) {
  465. cmd->result = (DID_RESET << 16);
  466. } else {
  467. cmd->result = (DID_OK << 16) | sdstat;
  468. if (sdstat == SAM_STAT_CHECK_CONDITION &&
  469. cmd->sense_buffer)
  470. cmd->result |= (DRIVER_SENSE << 24);
  471. }
  472. } else
  473. switch (btstat) {
  474. case BTSTAT_SUCCESS:
  475. case BTSTAT_LINKED_COMMAND_COMPLETED:
  476. case BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG:
  477. /* If everything went fine, let's move on.. */
  478. cmd->result = (DID_OK << 16);
  479. break;
  480. case BTSTAT_DATARUN:
  481. case BTSTAT_DATA_UNDERRUN:
  482. /* Report residual data in underruns */
  483. scsi_set_resid(cmd, scsi_bufflen(cmd) - e->dataLen);
  484. cmd->result = (DID_ERROR << 16);
  485. break;
  486. case BTSTAT_SELTIMEO:
  487. /* Our emulation returns this for non-connected devs */
  488. cmd->result = (DID_BAD_TARGET << 16);
  489. break;
  490. case BTSTAT_LUNMISMATCH:
  491. case BTSTAT_TAGREJECT:
  492. case BTSTAT_BADMSG:
  493. cmd->result = (DRIVER_INVALID << 24);
  494. /* fall through */
  495. case BTSTAT_HAHARDWARE:
  496. case BTSTAT_INVPHASE:
  497. case BTSTAT_HATIMEOUT:
  498. case BTSTAT_NORESPONSE:
  499. case BTSTAT_DISCONNECT:
  500. case BTSTAT_HASOFTWARE:
  501. case BTSTAT_BUSFREE:
  502. case BTSTAT_SENSFAILED:
  503. cmd->result |= (DID_ERROR << 16);
  504. break;
  505. case BTSTAT_SENTRST:
  506. case BTSTAT_RECVRST:
  507. case BTSTAT_BUSRESET:
  508. cmd->result = (DID_RESET << 16);
  509. break;
  510. case BTSTAT_ABORTQUEUE:
  511. cmd->result = (DID_BUS_BUSY << 16);
  512. break;
  513. case BTSTAT_SCSIPARITY:
  514. cmd->result = (DID_PARITY << 16);
  515. break;
  516. default:
  517. cmd->result = (DID_ERROR << 16);
  518. scmd_printk(KERN_DEBUG, cmd,
  519. "Unknown completion status: 0x%x\n",
  520. btstat);
  521. }
  522. dev_dbg(&cmd->device->sdev_gendev,
  523. "cmd=%p %x ctx=%p result=0x%x status=0x%x,%x\n",
  524. cmd, cmd->cmnd[0], ctx, cmd->result, btstat, sdstat);
  525. cmd->scsi_done(cmd);
  526. }
  527. /*
  528. * barrier usage : Since the PVSCSI device is emulated, there could be cases
  529. * where we may want to serialize some accesses between the driver and the
  530. * emulation layer. We use compiler barriers instead of the more expensive
  531. * memory barriers because PVSCSI is only supported on X86 which has strong
  532. * memory access ordering.
  533. */
  534. static void pvscsi_process_completion_ring(struct pvscsi_adapter *adapter)
  535. {
  536. struct PVSCSIRingsState *s = adapter->rings_state;
  537. struct PVSCSIRingCmpDesc *ring = adapter->cmp_ring;
  538. u32 cmp_entries = s->cmpNumEntriesLog2;
  539. while (s->cmpConsIdx != s->cmpProdIdx) {
  540. struct PVSCSIRingCmpDesc *e = ring + (s->cmpConsIdx &
  541. MASK(cmp_entries));
  542. /*
  543. * This barrier() ensures that *e is not dereferenced while
  544. * the device emulation still writes data into the slot.
  545. * Since the device emulation advances s->cmpProdIdx only after
  546. * updating the slot we want to check it first.
  547. */
  548. barrier();
  549. pvscsi_complete_request(adapter, e);
  550. /*
  551. * This barrier() ensures that compiler doesn't reorder write
  552. * to s->cmpConsIdx before the read of (*e) inside
  553. * pvscsi_complete_request. Otherwise, device emulation may
  554. * overwrite *e before we had a chance to read it.
  555. */
  556. barrier();
  557. s->cmpConsIdx++;
  558. }
  559. }
  560. /*
  561. * Translate a Linux SCSI request into a request ring entry.
  562. */
  563. static int pvscsi_queue_ring(struct pvscsi_adapter *adapter,
  564. struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd)
  565. {
  566. struct PVSCSIRingsState *s;
  567. struct PVSCSIRingReqDesc *e;
  568. struct scsi_device *sdev;
  569. u32 req_entries;
  570. s = adapter->rings_state;
  571. sdev = cmd->device;
  572. req_entries = s->reqNumEntriesLog2;
  573. /*
  574. * If this condition holds, we might have room on the request ring, but
  575. * we might not have room on the completion ring for the response.
  576. * However, we have already ruled out this possibility - we would not
  577. * have successfully allocated a context if it were true, since we only
  578. * have one context per request entry. Check for it anyway, since it
  579. * would be a serious bug.
  580. */
  581. if (s->reqProdIdx - s->cmpConsIdx >= 1 << req_entries) {
  582. scmd_printk(KERN_ERR, cmd, "vmw_pvscsi: "
  583. "ring full: reqProdIdx=%d cmpConsIdx=%d\n",
  584. s->reqProdIdx, s->cmpConsIdx);
  585. return -1;
  586. }
  587. e = adapter->req_ring + (s->reqProdIdx & MASK(req_entries));
  588. e->bus = sdev->channel;
  589. e->target = sdev->id;
  590. memset(e->lun, 0, sizeof(e->lun));
  591. e->lun[1] = sdev->lun;
  592. if (cmd->sense_buffer) {
  593. ctx->sensePA = pci_map_single(adapter->dev, cmd->sense_buffer,
  594. SCSI_SENSE_BUFFERSIZE,
  595. PCI_DMA_FROMDEVICE);
  596. if (pci_dma_mapping_error(adapter->dev, ctx->sensePA)) {
  597. scmd_printk(KERN_ERR, cmd,
  598. "vmw_pvscsi: Failed to map sense buffer for DMA.\n");
  599. ctx->sensePA = 0;
  600. return -ENOMEM;
  601. }
  602. e->senseAddr = ctx->sensePA;
  603. e->senseLen = SCSI_SENSE_BUFFERSIZE;
  604. } else {
  605. e->senseLen = 0;
  606. e->senseAddr = 0;
  607. }
  608. e->cdbLen = cmd->cmd_len;
  609. e->vcpuHint = smp_processor_id();
  610. memcpy(e->cdb, cmd->cmnd, e->cdbLen);
  611. e->tag = SIMPLE_QUEUE_TAG;
  612. if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  613. e->flags = PVSCSI_FLAG_CMD_DIR_TOHOST;
  614. else if (cmd->sc_data_direction == DMA_TO_DEVICE)
  615. e->flags = PVSCSI_FLAG_CMD_DIR_TODEVICE;
  616. else if (cmd->sc_data_direction == DMA_NONE)
  617. e->flags = PVSCSI_FLAG_CMD_DIR_NONE;
  618. else
  619. e->flags = 0;
  620. if (pvscsi_map_buffers(adapter, ctx, cmd, e) != 0) {
  621. if (cmd->sense_buffer) {
  622. pci_unmap_single(adapter->dev, ctx->sensePA,
  623. SCSI_SENSE_BUFFERSIZE,
  624. PCI_DMA_FROMDEVICE);
  625. ctx->sensePA = 0;
  626. }
  627. return -ENOMEM;
  628. }
  629. e->context = pvscsi_map_context(adapter, ctx);
  630. barrier();
  631. s->reqProdIdx++;
  632. return 0;
  633. }
  634. static int pvscsi_queue_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  635. {
  636. struct Scsi_Host *host = cmd->device->host;
  637. struct pvscsi_adapter *adapter = shost_priv(host);
  638. struct pvscsi_ctx *ctx;
  639. unsigned long flags;
  640. unsigned char op;
  641. spin_lock_irqsave(&adapter->hw_lock, flags);
  642. ctx = pvscsi_acquire_context(adapter, cmd);
  643. if (!ctx || pvscsi_queue_ring(adapter, ctx, cmd) != 0) {
  644. if (ctx)
  645. pvscsi_release_context(adapter, ctx);
  646. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  647. return SCSI_MLQUEUE_HOST_BUSY;
  648. }
  649. cmd->scsi_done = done;
  650. op = cmd->cmnd[0];
  651. dev_dbg(&cmd->device->sdev_gendev,
  652. "queued cmd %p, ctx %p, op=%x\n", cmd, ctx, op);
  653. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  654. pvscsi_kick_io(adapter, op);
  655. return 0;
  656. }
  657. static DEF_SCSI_QCMD(pvscsi_queue)
  658. static int pvscsi_abort(struct scsi_cmnd *cmd)
  659. {
  660. struct pvscsi_adapter *adapter = shost_priv(cmd->device->host);
  661. struct pvscsi_ctx *ctx;
  662. unsigned long flags;
  663. int result = SUCCESS;
  664. DECLARE_COMPLETION_ONSTACK(abort_cmp);
  665. int done;
  666. scmd_printk(KERN_DEBUG, cmd, "task abort on host %u, %p\n",
  667. adapter->host->host_no, cmd);
  668. spin_lock_irqsave(&adapter->hw_lock, flags);
  669. /*
  670. * Poll the completion ring first - we might be trying to abort
  671. * a command that is waiting to be dispatched in the completion ring.
  672. */
  673. pvscsi_process_completion_ring(adapter);
  674. /*
  675. * If there is no context for the command, it either already succeeded
  676. * or else was never properly issued. Not our problem.
  677. */
  678. ctx = pvscsi_find_context(adapter, cmd);
  679. if (!ctx) {
  680. scmd_printk(KERN_DEBUG, cmd, "Failed to abort cmd %p\n", cmd);
  681. goto out;
  682. }
  683. /*
  684. * Mark that the command has been requested to be aborted and issue
  685. * the abort.
  686. */
  687. ctx->abort_cmp = &abort_cmp;
  688. pvscsi_abort_cmd(adapter, ctx);
  689. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  690. /* Wait for 2 secs for the completion. */
  691. done = wait_for_completion_timeout(&abort_cmp, msecs_to_jiffies(2000));
  692. spin_lock_irqsave(&adapter->hw_lock, flags);
  693. if (!done) {
  694. /*
  695. * Failed to abort the command, unmark the fact that it
  696. * was requested to be aborted.
  697. */
  698. ctx->abort_cmp = NULL;
  699. result = FAILED;
  700. scmd_printk(KERN_DEBUG, cmd,
  701. "Failed to get completion for aborted cmd %p\n",
  702. cmd);
  703. goto out;
  704. }
  705. /*
  706. * Successfully aborted the command.
  707. */
  708. cmd->result = (DID_ABORT << 16);
  709. cmd->scsi_done(cmd);
  710. out:
  711. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  712. return result;
  713. }
  714. /*
  715. * Abort all outstanding requests. This is only safe to use if the completion
  716. * ring will never be walked again or the device has been reset, because it
  717. * destroys the 1-1 mapping between context field passed to emulation and our
  718. * request structure.
  719. */
  720. static void pvscsi_reset_all(struct pvscsi_adapter *adapter)
  721. {
  722. unsigned i;
  723. for (i = 0; i < adapter->req_depth; i++) {
  724. struct pvscsi_ctx *ctx = &adapter->cmd_map[i];
  725. struct scsi_cmnd *cmd = ctx->cmd;
  726. if (cmd) {
  727. scmd_printk(KERN_ERR, cmd,
  728. "Forced reset on cmd %p\n", cmd);
  729. pvscsi_unmap_buffers(adapter, ctx);
  730. pvscsi_release_context(adapter, ctx);
  731. cmd->result = (DID_RESET << 16);
  732. cmd->scsi_done(cmd);
  733. }
  734. }
  735. }
  736. static int pvscsi_host_reset(struct scsi_cmnd *cmd)
  737. {
  738. struct Scsi_Host *host = cmd->device->host;
  739. struct pvscsi_adapter *adapter = shost_priv(host);
  740. unsigned long flags;
  741. bool use_msg;
  742. scmd_printk(KERN_INFO, cmd, "SCSI Host reset\n");
  743. spin_lock_irqsave(&adapter->hw_lock, flags);
  744. use_msg = adapter->use_msg;
  745. if (use_msg) {
  746. adapter->use_msg = 0;
  747. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  748. /*
  749. * Now that we know that the ISR won't add more work on the
  750. * workqueue we can safely flush any outstanding work.
  751. */
  752. flush_workqueue(adapter->workqueue);
  753. spin_lock_irqsave(&adapter->hw_lock, flags);
  754. }
  755. /*
  756. * We're going to tear down the entire ring structure and set it back
  757. * up, so stalling new requests until all completions are flushed and
  758. * the rings are back in place.
  759. */
  760. pvscsi_process_request_ring(adapter);
  761. ll_adapter_reset(adapter);
  762. /*
  763. * Now process any completions. Note we do this AFTER adapter reset,
  764. * which is strange, but stops races where completions get posted
  765. * between processing the ring and issuing the reset. The backend will
  766. * not touch the ring memory after reset, so the immediately pre-reset
  767. * completion ring state is still valid.
  768. */
  769. pvscsi_process_completion_ring(adapter);
  770. pvscsi_reset_all(adapter);
  771. adapter->use_msg = use_msg;
  772. pvscsi_setup_all_rings(adapter);
  773. pvscsi_unmask_intr(adapter);
  774. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  775. return SUCCESS;
  776. }
  777. static int pvscsi_bus_reset(struct scsi_cmnd *cmd)
  778. {
  779. struct Scsi_Host *host = cmd->device->host;
  780. struct pvscsi_adapter *adapter = shost_priv(host);
  781. unsigned long flags;
  782. scmd_printk(KERN_INFO, cmd, "SCSI Bus reset\n");
  783. /*
  784. * We don't want to queue new requests for this bus after
  785. * flushing all pending requests to emulation, since new
  786. * requests could then sneak in during this bus reset phase,
  787. * so take the lock now.
  788. */
  789. spin_lock_irqsave(&adapter->hw_lock, flags);
  790. pvscsi_process_request_ring(adapter);
  791. ll_bus_reset(adapter);
  792. pvscsi_process_completion_ring(adapter);
  793. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  794. return SUCCESS;
  795. }
  796. static int pvscsi_device_reset(struct scsi_cmnd *cmd)
  797. {
  798. struct Scsi_Host *host = cmd->device->host;
  799. struct pvscsi_adapter *adapter = shost_priv(host);
  800. unsigned long flags;
  801. scmd_printk(KERN_INFO, cmd, "SCSI device reset on scsi%u:%u\n",
  802. host->host_no, cmd->device->id);
  803. /*
  804. * We don't want to queue new requests for this device after flushing
  805. * all pending requests to emulation, since new requests could then
  806. * sneak in during this device reset phase, so take the lock now.
  807. */
  808. spin_lock_irqsave(&adapter->hw_lock, flags);
  809. pvscsi_process_request_ring(adapter);
  810. ll_device_reset(adapter, cmd->device->id);
  811. pvscsi_process_completion_ring(adapter);
  812. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  813. return SUCCESS;
  814. }
  815. static struct scsi_host_template pvscsi_template;
  816. static const char *pvscsi_info(struct Scsi_Host *host)
  817. {
  818. struct pvscsi_adapter *adapter = shost_priv(host);
  819. static char buf[256];
  820. sprintf(buf, "VMware PVSCSI storage adapter rev %d, req/cmp/msg rings: "
  821. "%u/%u/%u pages, cmd_per_lun=%u", adapter->rev,
  822. adapter->req_pages, adapter->cmp_pages, adapter->msg_pages,
  823. pvscsi_template.cmd_per_lun);
  824. return buf;
  825. }
  826. static struct scsi_host_template pvscsi_template = {
  827. .module = THIS_MODULE,
  828. .name = "VMware PVSCSI Host Adapter",
  829. .proc_name = "vmw_pvscsi",
  830. .info = pvscsi_info,
  831. .queuecommand = pvscsi_queue,
  832. .this_id = -1,
  833. .sg_tablesize = PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT,
  834. .dma_boundary = UINT_MAX,
  835. .max_sectors = 0xffff,
  836. .use_clustering = ENABLE_CLUSTERING,
  837. .change_queue_depth = pvscsi_change_queue_depth,
  838. .eh_abort_handler = pvscsi_abort,
  839. .eh_device_reset_handler = pvscsi_device_reset,
  840. .eh_bus_reset_handler = pvscsi_bus_reset,
  841. .eh_host_reset_handler = pvscsi_host_reset,
  842. };
  843. static void pvscsi_process_msg(const struct pvscsi_adapter *adapter,
  844. const struct PVSCSIRingMsgDesc *e)
  845. {
  846. struct PVSCSIRingsState *s = adapter->rings_state;
  847. struct Scsi_Host *host = adapter->host;
  848. struct scsi_device *sdev;
  849. printk(KERN_INFO "vmw_pvscsi: msg type: 0x%x - MSG RING: %u/%u (%u) \n",
  850. e->type, s->msgProdIdx, s->msgConsIdx, s->msgNumEntriesLog2);
  851. BUILD_BUG_ON(PVSCSI_MSG_LAST != 2);
  852. if (e->type == PVSCSI_MSG_DEV_ADDED) {
  853. struct PVSCSIMsgDescDevStatusChanged *desc;
  854. desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
  855. printk(KERN_INFO
  856. "vmw_pvscsi: msg: device added at scsi%u:%u:%u\n",
  857. desc->bus, desc->target, desc->lun[1]);
  858. if (!scsi_host_get(host))
  859. return;
  860. sdev = scsi_device_lookup(host, desc->bus, desc->target,
  861. desc->lun[1]);
  862. if (sdev) {
  863. printk(KERN_INFO "vmw_pvscsi: device already exists\n");
  864. scsi_device_put(sdev);
  865. } else
  866. scsi_add_device(adapter->host, desc->bus,
  867. desc->target, desc->lun[1]);
  868. scsi_host_put(host);
  869. } else if (e->type == PVSCSI_MSG_DEV_REMOVED) {
  870. struct PVSCSIMsgDescDevStatusChanged *desc;
  871. desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
  872. printk(KERN_INFO
  873. "vmw_pvscsi: msg: device removed at scsi%u:%u:%u\n",
  874. desc->bus, desc->target, desc->lun[1]);
  875. if (!scsi_host_get(host))
  876. return;
  877. sdev = scsi_device_lookup(host, desc->bus, desc->target,
  878. desc->lun[1]);
  879. if (sdev) {
  880. scsi_remove_device(sdev);
  881. scsi_device_put(sdev);
  882. } else
  883. printk(KERN_INFO
  884. "vmw_pvscsi: failed to lookup scsi%u:%u:%u\n",
  885. desc->bus, desc->target, desc->lun[1]);
  886. scsi_host_put(host);
  887. }
  888. }
  889. static int pvscsi_msg_pending(const struct pvscsi_adapter *adapter)
  890. {
  891. struct PVSCSIRingsState *s = adapter->rings_state;
  892. return s->msgProdIdx != s->msgConsIdx;
  893. }
  894. static void pvscsi_process_msg_ring(const struct pvscsi_adapter *adapter)
  895. {
  896. struct PVSCSIRingsState *s = adapter->rings_state;
  897. struct PVSCSIRingMsgDesc *ring = adapter->msg_ring;
  898. u32 msg_entries = s->msgNumEntriesLog2;
  899. while (pvscsi_msg_pending(adapter)) {
  900. struct PVSCSIRingMsgDesc *e = ring + (s->msgConsIdx &
  901. MASK(msg_entries));
  902. barrier();
  903. pvscsi_process_msg(adapter, e);
  904. barrier();
  905. s->msgConsIdx++;
  906. }
  907. }
  908. static void pvscsi_msg_workqueue_handler(struct work_struct *data)
  909. {
  910. struct pvscsi_adapter *adapter;
  911. adapter = container_of(data, struct pvscsi_adapter, work);
  912. pvscsi_process_msg_ring(adapter);
  913. }
  914. static int pvscsi_setup_msg_workqueue(struct pvscsi_adapter *adapter)
  915. {
  916. char name[32];
  917. if (!pvscsi_use_msg)
  918. return 0;
  919. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
  920. PVSCSI_CMD_SETUP_MSG_RING);
  921. if (pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS) == -1)
  922. return 0;
  923. snprintf(name, sizeof(name),
  924. "vmw_pvscsi_wq_%u", adapter->host->host_no);
  925. adapter->workqueue = create_singlethread_workqueue(name);
  926. if (!adapter->workqueue) {
  927. printk(KERN_ERR "vmw_pvscsi: failed to create work queue\n");
  928. return 0;
  929. }
  930. INIT_WORK(&adapter->work, pvscsi_msg_workqueue_handler);
  931. return 1;
  932. }
  933. static bool pvscsi_setup_req_threshold(struct pvscsi_adapter *adapter,
  934. bool enable)
  935. {
  936. u32 val;
  937. if (!pvscsi_use_req_threshold)
  938. return false;
  939. pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
  940. PVSCSI_CMD_SETUP_REQCALLTHRESHOLD);
  941. val = pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS);
  942. if (val == -1) {
  943. printk(KERN_INFO "vmw_pvscsi: device does not support req_threshold\n");
  944. return false;
  945. } else {
  946. struct PVSCSICmdDescSetupReqCall cmd_msg = { 0 };
  947. cmd_msg.enable = enable;
  948. printk(KERN_INFO
  949. "vmw_pvscsi: %sabling reqCallThreshold\n",
  950. enable ? "en" : "dis");
  951. pvscsi_write_cmd_desc(adapter,
  952. PVSCSI_CMD_SETUP_REQCALLTHRESHOLD,
  953. &cmd_msg, sizeof(cmd_msg));
  954. return pvscsi_reg_read(adapter,
  955. PVSCSI_REG_OFFSET_COMMAND_STATUS) != 0;
  956. }
  957. }
  958. static irqreturn_t pvscsi_isr(int irq, void *devp)
  959. {
  960. struct pvscsi_adapter *adapter = devp;
  961. unsigned long flags;
  962. spin_lock_irqsave(&adapter->hw_lock, flags);
  963. pvscsi_process_completion_ring(adapter);
  964. if (adapter->use_msg && pvscsi_msg_pending(adapter))
  965. queue_work(adapter->workqueue, &adapter->work);
  966. spin_unlock_irqrestore(&adapter->hw_lock, flags);
  967. return IRQ_HANDLED;
  968. }
  969. static irqreturn_t pvscsi_shared_isr(int irq, void *devp)
  970. {
  971. struct pvscsi_adapter *adapter = devp;
  972. u32 val = pvscsi_read_intr_status(adapter);
  973. if (!(val & PVSCSI_INTR_ALL_SUPPORTED))
  974. return IRQ_NONE;
  975. pvscsi_write_intr_status(devp, val);
  976. return pvscsi_isr(irq, devp);
  977. }
  978. static void pvscsi_free_sgls(const struct pvscsi_adapter *adapter)
  979. {
  980. struct pvscsi_ctx *ctx = adapter->cmd_map;
  981. unsigned i;
  982. for (i = 0; i < adapter->req_depth; ++i, ++ctx)
  983. free_pages((unsigned long)ctx->sgl, get_order(SGL_SIZE));
  984. }
  985. static void pvscsi_shutdown_intr(struct pvscsi_adapter *adapter)
  986. {
  987. free_irq(pci_irq_vector(adapter->dev, 0), adapter);
  988. pci_free_irq_vectors(adapter->dev);
  989. }
  990. static void pvscsi_release_resources(struct pvscsi_adapter *adapter)
  991. {
  992. if (adapter->workqueue)
  993. destroy_workqueue(adapter->workqueue);
  994. if (adapter->mmioBase)
  995. pci_iounmap(adapter->dev, adapter->mmioBase);
  996. pci_release_regions(adapter->dev);
  997. if (adapter->cmd_map) {
  998. pvscsi_free_sgls(adapter);
  999. kfree(adapter->cmd_map);
  1000. }
  1001. if (adapter->rings_state)
  1002. pci_free_consistent(adapter->dev, PAGE_SIZE,
  1003. adapter->rings_state, adapter->ringStatePA);
  1004. if (adapter->req_ring)
  1005. pci_free_consistent(adapter->dev,
  1006. adapter->req_pages * PAGE_SIZE,
  1007. adapter->req_ring, adapter->reqRingPA);
  1008. if (adapter->cmp_ring)
  1009. pci_free_consistent(adapter->dev,
  1010. adapter->cmp_pages * PAGE_SIZE,
  1011. adapter->cmp_ring, adapter->cmpRingPA);
  1012. if (adapter->msg_ring)
  1013. pci_free_consistent(adapter->dev,
  1014. adapter->msg_pages * PAGE_SIZE,
  1015. adapter->msg_ring, adapter->msgRingPA);
  1016. }
  1017. /*
  1018. * Allocate scatter gather lists.
  1019. *
  1020. * These are statically allocated. Trying to be clever was not worth it.
  1021. *
  1022. * Dynamic allocation can fail, and we can't go deep into the memory
  1023. * allocator, since we're a SCSI driver, and trying too hard to allocate
  1024. * memory might generate disk I/O. We also don't want to fail disk I/O
  1025. * in that case because we can't get an allocation - the I/O could be
  1026. * trying to swap out data to free memory. Since that is pathological,
  1027. * just use a statically allocated scatter list.
  1028. *
  1029. */
  1030. static int pvscsi_allocate_sg(struct pvscsi_adapter *adapter)
  1031. {
  1032. struct pvscsi_ctx *ctx;
  1033. int i;
  1034. ctx = adapter->cmd_map;
  1035. BUILD_BUG_ON(sizeof(struct pvscsi_sg_list) > SGL_SIZE);
  1036. for (i = 0; i < adapter->req_depth; ++i, ++ctx) {
  1037. ctx->sgl = (void *)__get_free_pages(GFP_KERNEL,
  1038. get_order(SGL_SIZE));
  1039. ctx->sglPA = 0;
  1040. BUG_ON(!IS_ALIGNED(((unsigned long)ctx->sgl), PAGE_SIZE));
  1041. if (!ctx->sgl) {
  1042. for (; i >= 0; --i, --ctx) {
  1043. free_pages((unsigned long)ctx->sgl,
  1044. get_order(SGL_SIZE));
  1045. ctx->sgl = NULL;
  1046. }
  1047. return -ENOMEM;
  1048. }
  1049. }
  1050. return 0;
  1051. }
  1052. /*
  1053. * Query the device, fetch the config info and return the
  1054. * maximum number of targets on the adapter. In case of
  1055. * failure due to any reason return default i.e. 16.
  1056. */
  1057. static u32 pvscsi_get_max_targets(struct pvscsi_adapter *adapter)
  1058. {
  1059. struct PVSCSICmdDescConfigCmd cmd;
  1060. struct PVSCSIConfigPageHeader *header;
  1061. struct device *dev;
  1062. dma_addr_t configPagePA;
  1063. void *config_page;
  1064. u32 numPhys = 16;
  1065. dev = pvscsi_dev(adapter);
  1066. config_page = pci_alloc_consistent(adapter->dev, PAGE_SIZE,
  1067. &configPagePA);
  1068. if (!config_page) {
  1069. dev_warn(dev, "vmw_pvscsi: failed to allocate memory for config page\n");
  1070. goto exit;
  1071. }
  1072. BUG_ON(configPagePA & ~PAGE_MASK);
  1073. /* Fetch config info from the device. */
  1074. cmd.configPageAddress = ((u64)PVSCSI_CONFIG_CONTROLLER_ADDRESS) << 32;
  1075. cmd.configPageNum = PVSCSI_CONFIG_PAGE_CONTROLLER;
  1076. cmd.cmpAddr = configPagePA;
  1077. cmd._pad = 0;
  1078. /*
  1079. * Mark the completion page header with error values. If the device
  1080. * completes the command successfully, it sets the status values to
  1081. * indicate success.
  1082. */
  1083. header = config_page;
  1084. memset(header, 0, sizeof *header);
  1085. header->hostStatus = BTSTAT_INVPARAM;
  1086. header->scsiStatus = SDSTAT_CHECK;
  1087. pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_CONFIG, &cmd, sizeof cmd);
  1088. if (header->hostStatus == BTSTAT_SUCCESS &&
  1089. header->scsiStatus == SDSTAT_GOOD) {
  1090. struct PVSCSIConfigPageController *config;
  1091. config = config_page;
  1092. numPhys = config->numPhys;
  1093. } else
  1094. dev_warn(dev, "vmw_pvscsi: PVSCSI_CMD_CONFIG failed. hostStatus = 0x%x, scsiStatus = 0x%x\n",
  1095. header->hostStatus, header->scsiStatus);
  1096. pci_free_consistent(adapter->dev, PAGE_SIZE, config_page, configPagePA);
  1097. exit:
  1098. return numPhys;
  1099. }
  1100. static int pvscsi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1101. {
  1102. unsigned int irq_flag = PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY;
  1103. struct pvscsi_adapter *adapter;
  1104. struct pvscsi_adapter adapter_temp;
  1105. struct Scsi_Host *host = NULL;
  1106. unsigned int i;
  1107. int error;
  1108. u32 max_id;
  1109. error = -ENODEV;
  1110. if (pci_enable_device(pdev))
  1111. return error;
  1112. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0 &&
  1113. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
  1114. printk(KERN_INFO "vmw_pvscsi: using 64bit dma\n");
  1115. } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) == 0 &&
  1116. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) == 0) {
  1117. printk(KERN_INFO "vmw_pvscsi: using 32bit dma\n");
  1118. } else {
  1119. printk(KERN_ERR "vmw_pvscsi: failed to set DMA mask\n");
  1120. goto out_disable_device;
  1121. }
  1122. /*
  1123. * Let's use a temp pvscsi_adapter struct until we find the number of
  1124. * targets on the adapter, after that we will switch to the real
  1125. * allocated struct.
  1126. */
  1127. adapter = &adapter_temp;
  1128. memset(adapter, 0, sizeof(*adapter));
  1129. adapter->dev = pdev;
  1130. adapter->rev = pdev->revision;
  1131. if (pci_request_regions(pdev, "vmw_pvscsi")) {
  1132. printk(KERN_ERR "vmw_pvscsi: pci memory selection failed\n");
  1133. goto out_disable_device;
  1134. }
  1135. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1136. if ((pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO))
  1137. continue;
  1138. if (pci_resource_len(pdev, i) < PVSCSI_MEM_SPACE_SIZE)
  1139. continue;
  1140. break;
  1141. }
  1142. if (i == DEVICE_COUNT_RESOURCE) {
  1143. printk(KERN_ERR
  1144. "vmw_pvscsi: adapter has no suitable MMIO region\n");
  1145. goto out_release_resources_and_disable;
  1146. }
  1147. adapter->mmioBase = pci_iomap(pdev, i, PVSCSI_MEM_SPACE_SIZE);
  1148. if (!adapter->mmioBase) {
  1149. printk(KERN_ERR
  1150. "vmw_pvscsi: can't iomap for BAR %d memsize %lu\n",
  1151. i, PVSCSI_MEM_SPACE_SIZE);
  1152. goto out_release_resources_and_disable;
  1153. }
  1154. pci_set_master(pdev);
  1155. /*
  1156. * Ask the device for max number of targets before deciding the
  1157. * default pvscsi_ring_pages value.
  1158. */
  1159. max_id = pvscsi_get_max_targets(adapter);
  1160. printk(KERN_INFO "vmw_pvscsi: max_id: %u\n", max_id);
  1161. if (pvscsi_ring_pages == 0)
  1162. /*
  1163. * Set the right default value. Up to 16 it is 8, above it is
  1164. * max.
  1165. */
  1166. pvscsi_ring_pages = (max_id > 16) ?
  1167. PVSCSI_SETUP_RINGS_MAX_NUM_PAGES :
  1168. PVSCSI_DEFAULT_NUM_PAGES_PER_RING;
  1169. printk(KERN_INFO
  1170. "vmw_pvscsi: setting ring_pages to %d\n",
  1171. pvscsi_ring_pages);
  1172. pvscsi_template.can_queue =
  1173. min(PVSCSI_MAX_NUM_PAGES_REQ_RING, pvscsi_ring_pages) *
  1174. PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
  1175. pvscsi_template.cmd_per_lun =
  1176. min(pvscsi_template.can_queue, pvscsi_cmd_per_lun);
  1177. host = scsi_host_alloc(&pvscsi_template, sizeof(struct pvscsi_adapter));
  1178. if (!host) {
  1179. printk(KERN_ERR "vmw_pvscsi: failed to allocate host\n");
  1180. goto out_release_resources_and_disable;
  1181. }
  1182. /*
  1183. * Let's use the real pvscsi_adapter struct here onwards.
  1184. */
  1185. adapter = shost_priv(host);
  1186. memset(adapter, 0, sizeof(*adapter));
  1187. adapter->dev = pdev;
  1188. adapter->host = host;
  1189. /*
  1190. * Copy back what we already have to the allocated adapter struct.
  1191. */
  1192. adapter->rev = adapter_temp.rev;
  1193. adapter->mmioBase = adapter_temp.mmioBase;
  1194. spin_lock_init(&adapter->hw_lock);
  1195. host->max_channel = 0;
  1196. host->max_lun = 1;
  1197. host->max_cmd_len = 16;
  1198. host->max_id = max_id;
  1199. pci_set_drvdata(pdev, host);
  1200. ll_adapter_reset(adapter);
  1201. adapter->use_msg = pvscsi_setup_msg_workqueue(adapter);
  1202. error = pvscsi_allocate_rings(adapter);
  1203. if (error) {
  1204. printk(KERN_ERR "vmw_pvscsi: unable to allocate ring memory\n");
  1205. goto out_release_resources;
  1206. }
  1207. /*
  1208. * From this point on we should reset the adapter if anything goes
  1209. * wrong.
  1210. */
  1211. pvscsi_setup_all_rings(adapter);
  1212. adapter->cmd_map = kcalloc(adapter->req_depth,
  1213. sizeof(struct pvscsi_ctx), GFP_KERNEL);
  1214. if (!adapter->cmd_map) {
  1215. printk(KERN_ERR "vmw_pvscsi: failed to allocate memory.\n");
  1216. error = -ENOMEM;
  1217. goto out_reset_adapter;
  1218. }
  1219. INIT_LIST_HEAD(&adapter->cmd_pool);
  1220. for (i = 0; i < adapter->req_depth; i++) {
  1221. struct pvscsi_ctx *ctx = adapter->cmd_map + i;
  1222. list_add(&ctx->list, &adapter->cmd_pool);
  1223. }
  1224. error = pvscsi_allocate_sg(adapter);
  1225. if (error) {
  1226. printk(KERN_ERR "vmw_pvscsi: unable to allocate s/g table\n");
  1227. goto out_reset_adapter;
  1228. }
  1229. if (pvscsi_disable_msix)
  1230. irq_flag &= ~PCI_IRQ_MSIX;
  1231. if (pvscsi_disable_msi)
  1232. irq_flag &= ~PCI_IRQ_MSI;
  1233. error = pci_alloc_irq_vectors(adapter->dev, 1, 1, irq_flag);
  1234. if (error < 0)
  1235. goto out_reset_adapter;
  1236. adapter->use_req_threshold = pvscsi_setup_req_threshold(adapter, true);
  1237. printk(KERN_DEBUG "vmw_pvscsi: driver-based request coalescing %sabled\n",
  1238. adapter->use_req_threshold ? "en" : "dis");
  1239. if (adapter->dev->msix_enabled || adapter->dev->msi_enabled) {
  1240. printk(KERN_INFO "vmw_pvscsi: using MSI%s\n",
  1241. adapter->dev->msix_enabled ? "-X" : "");
  1242. error = request_irq(pci_irq_vector(pdev, 0), pvscsi_isr,
  1243. 0, "vmw_pvscsi", adapter);
  1244. } else {
  1245. printk(KERN_INFO "vmw_pvscsi: using INTx\n");
  1246. error = request_irq(pci_irq_vector(pdev, 0), pvscsi_shared_isr,
  1247. IRQF_SHARED, "vmw_pvscsi", adapter);
  1248. }
  1249. if (error) {
  1250. printk(KERN_ERR
  1251. "vmw_pvscsi: unable to request IRQ: %d\n", error);
  1252. goto out_reset_adapter;
  1253. }
  1254. error = scsi_add_host(host, &pdev->dev);
  1255. if (error) {
  1256. printk(KERN_ERR
  1257. "vmw_pvscsi: scsi_add_host failed: %d\n", error);
  1258. goto out_reset_adapter;
  1259. }
  1260. dev_info(&pdev->dev, "VMware PVSCSI rev %d host #%u\n",
  1261. adapter->rev, host->host_no);
  1262. pvscsi_unmask_intr(adapter);
  1263. scsi_scan_host(host);
  1264. return 0;
  1265. out_reset_adapter:
  1266. ll_adapter_reset(adapter);
  1267. out_release_resources:
  1268. pvscsi_shutdown_intr(adapter);
  1269. pvscsi_release_resources(adapter);
  1270. scsi_host_put(host);
  1271. out_disable_device:
  1272. pci_disable_device(pdev);
  1273. return error;
  1274. out_release_resources_and_disable:
  1275. pvscsi_shutdown_intr(adapter);
  1276. pvscsi_release_resources(adapter);
  1277. goto out_disable_device;
  1278. }
  1279. static void __pvscsi_shutdown(struct pvscsi_adapter *adapter)
  1280. {
  1281. pvscsi_mask_intr(adapter);
  1282. if (adapter->workqueue)
  1283. flush_workqueue(adapter->workqueue);
  1284. pvscsi_shutdown_intr(adapter);
  1285. pvscsi_process_request_ring(adapter);
  1286. pvscsi_process_completion_ring(adapter);
  1287. ll_adapter_reset(adapter);
  1288. }
  1289. static void pvscsi_shutdown(struct pci_dev *dev)
  1290. {
  1291. struct Scsi_Host *host = pci_get_drvdata(dev);
  1292. struct pvscsi_adapter *adapter = shost_priv(host);
  1293. __pvscsi_shutdown(adapter);
  1294. }
  1295. static void pvscsi_remove(struct pci_dev *pdev)
  1296. {
  1297. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1298. struct pvscsi_adapter *adapter = shost_priv(host);
  1299. scsi_remove_host(host);
  1300. __pvscsi_shutdown(adapter);
  1301. pvscsi_release_resources(adapter);
  1302. scsi_host_put(host);
  1303. pci_disable_device(pdev);
  1304. }
  1305. static struct pci_driver pvscsi_pci_driver = {
  1306. .name = "vmw_pvscsi",
  1307. .id_table = pvscsi_pci_tbl,
  1308. .probe = pvscsi_probe,
  1309. .remove = pvscsi_remove,
  1310. .shutdown = pvscsi_shutdown,
  1311. };
  1312. static int __init pvscsi_init(void)
  1313. {
  1314. pr_info("%s - version %s\n",
  1315. PVSCSI_LINUX_DRIVER_DESC, PVSCSI_DRIVER_VERSION_STRING);
  1316. return pci_register_driver(&pvscsi_pci_driver);
  1317. }
  1318. static void __exit pvscsi_exit(void)
  1319. {
  1320. pci_unregister_driver(&pvscsi_pci_driver);
  1321. }
  1322. module_init(pvscsi_init);
  1323. module_exit(pvscsi_exit);