vmw_pvscsi.c 43 KB

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