vmw_pvscsi.c 43 KB

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