vmw_pvscsi.c 42 KB

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