vmw_pvscsi.c 38 KB

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