vmw_pvscsi.c 43 KB

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