virtio_scsi.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * Virtio SCSI HBA driver
  3. *
  4. * Copyright IBM Corp. 2010
  5. * Copyright Red Hat, Inc. 2011
  6. *
  7. * Authors:
  8. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  9. * Paolo Bonzini <pbonzini@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/mempool.h>
  19. #include <linux/virtio.h>
  20. #include <linux/virtio_ids.h>
  21. #include <linux/virtio_config.h>
  22. #include <linux/virtio_scsi.h>
  23. #include <linux/cpu.h>
  24. #include <linux/blkdev.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_tcq.h>
  29. #include <linux/seqlock.h>
  30. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  31. #define VIRTIO_SCSI_EVENT_LEN 8
  32. #define VIRTIO_SCSI_VQ_BASE 2
  33. /* Command queue element */
  34. struct virtio_scsi_cmd {
  35. struct scsi_cmnd *sc;
  36. struct completion *comp;
  37. union {
  38. struct virtio_scsi_cmd_req cmd;
  39. struct virtio_scsi_cmd_req_pi cmd_pi;
  40. struct virtio_scsi_ctrl_tmf_req tmf;
  41. struct virtio_scsi_ctrl_an_req an;
  42. } req;
  43. union {
  44. struct virtio_scsi_cmd_resp cmd;
  45. struct virtio_scsi_ctrl_tmf_resp tmf;
  46. struct virtio_scsi_ctrl_an_resp an;
  47. struct virtio_scsi_event evt;
  48. } resp;
  49. } ____cacheline_aligned_in_smp;
  50. struct virtio_scsi_event_node {
  51. struct virtio_scsi *vscsi;
  52. struct virtio_scsi_event event;
  53. struct work_struct work;
  54. };
  55. struct virtio_scsi_vq {
  56. /* Protects vq */
  57. spinlock_t vq_lock;
  58. struct virtqueue *vq;
  59. };
  60. /*
  61. * Per-target queue state.
  62. *
  63. * This struct holds the data needed by the queue steering policy. When a
  64. * target is sent multiple requests, we need to drive them to the same queue so
  65. * that FIFO processing order is kept. However, if a target was idle, we can
  66. * choose a queue arbitrarily. In this case the queue is chosen according to
  67. * the current VCPU, so the driver expects the number of request queues to be
  68. * equal to the number of VCPUs. This makes it easy and fast to select the
  69. * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
  70. * (each virtqueue's affinity is set to the CPU that "owns" the queue).
  71. *
  72. * tgt_seq is held to serialize reading and writing req_vq.
  73. *
  74. * Decrements of reqs are never concurrent with writes of req_vq: before the
  75. * decrement reqs will be != 0; after the decrement the virtqueue completion
  76. * routine will not use the req_vq so it can be changed by a new request.
  77. * Thus they can happen outside the tgt_seq, provided of course we make reqs
  78. * an atomic_t.
  79. */
  80. struct virtio_scsi_target_state {
  81. seqcount_t tgt_seq;
  82. /* Count of outstanding requests. */
  83. atomic_t reqs;
  84. /* Currently active virtqueue for requests sent to this target. */
  85. struct virtio_scsi_vq *req_vq;
  86. };
  87. /* Driver instance state */
  88. struct virtio_scsi {
  89. struct virtio_device *vdev;
  90. /* Get some buffers ready for event vq */
  91. struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
  92. u32 num_queues;
  93. /* If the affinity hint is set for virtqueues */
  94. bool affinity_hint_set;
  95. struct hlist_node node;
  96. struct hlist_node node_dead;
  97. /* Protected by event_vq lock */
  98. bool stop_events;
  99. struct virtio_scsi_vq ctrl_vq;
  100. struct virtio_scsi_vq event_vq;
  101. struct virtio_scsi_vq req_vqs[];
  102. };
  103. static enum cpuhp_state virtioscsi_online;
  104. static struct kmem_cache *virtscsi_cmd_cache;
  105. static mempool_t *virtscsi_cmd_pool;
  106. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  107. {
  108. return vdev->priv;
  109. }
  110. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  111. {
  112. if (!resid)
  113. return;
  114. if (!scsi_bidi_cmnd(sc)) {
  115. scsi_set_resid(sc, resid);
  116. return;
  117. }
  118. scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
  119. scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
  120. }
  121. /**
  122. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  123. *
  124. * Called with vq_lock held.
  125. */
  126. static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
  127. {
  128. struct virtio_scsi_cmd *cmd = buf;
  129. struct scsi_cmnd *sc = cmd->sc;
  130. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  131. struct virtio_scsi_target_state *tgt =
  132. scsi_target(sc->device)->hostdata;
  133. dev_dbg(&sc->device->sdev_gendev,
  134. "cmd %p response %u status %#02x sense_len %u\n",
  135. sc, resp->response, resp->status, resp->sense_len);
  136. sc->result = resp->status;
  137. virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
  138. switch (resp->response) {
  139. case VIRTIO_SCSI_S_OK:
  140. set_host_byte(sc, DID_OK);
  141. break;
  142. case VIRTIO_SCSI_S_OVERRUN:
  143. set_host_byte(sc, DID_ERROR);
  144. break;
  145. case VIRTIO_SCSI_S_ABORTED:
  146. set_host_byte(sc, DID_ABORT);
  147. break;
  148. case VIRTIO_SCSI_S_BAD_TARGET:
  149. set_host_byte(sc, DID_BAD_TARGET);
  150. break;
  151. case VIRTIO_SCSI_S_RESET:
  152. set_host_byte(sc, DID_RESET);
  153. break;
  154. case VIRTIO_SCSI_S_BUSY:
  155. set_host_byte(sc, DID_BUS_BUSY);
  156. break;
  157. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  158. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  159. break;
  160. case VIRTIO_SCSI_S_TARGET_FAILURE:
  161. set_host_byte(sc, DID_TARGET_FAILURE);
  162. break;
  163. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  164. set_host_byte(sc, DID_NEXUS_FAILURE);
  165. break;
  166. default:
  167. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  168. resp->response);
  169. /* fall through */
  170. case VIRTIO_SCSI_S_FAILURE:
  171. set_host_byte(sc, DID_ERROR);
  172. break;
  173. }
  174. WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
  175. VIRTIO_SCSI_SENSE_SIZE);
  176. if (sc->sense_buffer) {
  177. memcpy(sc->sense_buffer, resp->sense,
  178. min_t(u32,
  179. virtio32_to_cpu(vscsi->vdev, resp->sense_len),
  180. VIRTIO_SCSI_SENSE_SIZE));
  181. if (resp->sense_len)
  182. set_driver_byte(sc, DRIVER_SENSE);
  183. }
  184. sc->scsi_done(sc);
  185. atomic_dec(&tgt->reqs);
  186. }
  187. static void virtscsi_vq_done(struct virtio_scsi *vscsi,
  188. struct virtio_scsi_vq *virtscsi_vq,
  189. void (*fn)(struct virtio_scsi *vscsi, void *buf))
  190. {
  191. void *buf;
  192. unsigned int len;
  193. unsigned long flags;
  194. struct virtqueue *vq = virtscsi_vq->vq;
  195. spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
  196. do {
  197. virtqueue_disable_cb(vq);
  198. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  199. fn(vscsi, buf);
  200. if (unlikely(virtqueue_is_broken(vq)))
  201. break;
  202. } while (!virtqueue_enable_cb(vq));
  203. spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
  204. }
  205. static void virtscsi_req_done(struct virtqueue *vq)
  206. {
  207. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  208. struct virtio_scsi *vscsi = shost_priv(sh);
  209. int index = vq->index - VIRTIO_SCSI_VQ_BASE;
  210. struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
  211. virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
  212. };
  213. static void virtscsi_poll_requests(struct virtio_scsi *vscsi)
  214. {
  215. int i, num_vqs;
  216. num_vqs = vscsi->num_queues;
  217. for (i = 0; i < num_vqs; i++)
  218. virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
  219. virtscsi_complete_cmd);
  220. }
  221. static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
  222. {
  223. struct virtio_scsi_cmd *cmd = buf;
  224. if (cmd->comp)
  225. complete(cmd->comp);
  226. }
  227. static void virtscsi_ctrl_done(struct virtqueue *vq)
  228. {
  229. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  230. struct virtio_scsi *vscsi = shost_priv(sh);
  231. virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
  232. };
  233. static void virtscsi_handle_event(struct work_struct *work);
  234. static int virtscsi_kick_event(struct virtio_scsi *vscsi,
  235. struct virtio_scsi_event_node *event_node)
  236. {
  237. int err;
  238. struct scatterlist sg;
  239. unsigned long flags;
  240. INIT_WORK(&event_node->work, virtscsi_handle_event);
  241. sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
  242. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  243. err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
  244. GFP_ATOMIC);
  245. if (!err)
  246. virtqueue_kick(vscsi->event_vq.vq);
  247. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  248. return err;
  249. }
  250. static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
  251. {
  252. int i;
  253. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
  254. vscsi->event_list[i].vscsi = vscsi;
  255. virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
  256. }
  257. return 0;
  258. }
  259. static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
  260. {
  261. int i;
  262. /* Stop scheduling work before calling cancel_work_sync. */
  263. spin_lock_irq(&vscsi->event_vq.vq_lock);
  264. vscsi->stop_events = true;
  265. spin_unlock_irq(&vscsi->event_vq.vq_lock);
  266. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
  267. cancel_work_sync(&vscsi->event_list[i].work);
  268. }
  269. static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
  270. struct virtio_scsi_event *event)
  271. {
  272. struct scsi_device *sdev;
  273. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  274. unsigned int target = event->lun[1];
  275. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  276. switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
  277. case VIRTIO_SCSI_EVT_RESET_RESCAN:
  278. scsi_add_device(shost, 0, target, lun);
  279. break;
  280. case VIRTIO_SCSI_EVT_RESET_REMOVED:
  281. sdev = scsi_device_lookup(shost, 0, target, lun);
  282. if (sdev) {
  283. scsi_remove_device(sdev);
  284. scsi_device_put(sdev);
  285. } else {
  286. pr_err("SCSI device %d 0 %d %d not found\n",
  287. shost->host_no, target, lun);
  288. }
  289. break;
  290. default:
  291. pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
  292. }
  293. }
  294. static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
  295. struct virtio_scsi_event *event)
  296. {
  297. struct scsi_device *sdev;
  298. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  299. unsigned int target = event->lun[1];
  300. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  301. u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
  302. u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
  303. sdev = scsi_device_lookup(shost, 0, target, lun);
  304. if (!sdev) {
  305. pr_err("SCSI device %d 0 %d %d not found\n",
  306. shost->host_no, target, lun);
  307. return;
  308. }
  309. /* Handle "Parameters changed", "Mode parameters changed", and
  310. "Capacity data has changed". */
  311. if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
  312. scsi_rescan_device(&sdev->sdev_gendev);
  313. scsi_device_put(sdev);
  314. }
  315. static void virtscsi_handle_event(struct work_struct *work)
  316. {
  317. struct virtio_scsi_event_node *event_node =
  318. container_of(work, struct virtio_scsi_event_node, work);
  319. struct virtio_scsi *vscsi = event_node->vscsi;
  320. struct virtio_scsi_event *event = &event_node->event;
  321. if (event->event &
  322. cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
  323. event->event &= ~cpu_to_virtio32(vscsi->vdev,
  324. VIRTIO_SCSI_T_EVENTS_MISSED);
  325. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  326. }
  327. switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
  328. case VIRTIO_SCSI_T_NO_EVENT:
  329. break;
  330. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  331. virtscsi_handle_transport_reset(vscsi, event);
  332. break;
  333. case VIRTIO_SCSI_T_PARAM_CHANGE:
  334. virtscsi_handle_param_change(vscsi, event);
  335. break;
  336. default:
  337. pr_err("Unsupport virtio scsi event %x\n", event->event);
  338. }
  339. virtscsi_kick_event(vscsi, event_node);
  340. }
  341. static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
  342. {
  343. struct virtio_scsi_event_node *event_node = buf;
  344. if (!vscsi->stop_events)
  345. queue_work(system_freezable_wq, &event_node->work);
  346. }
  347. static void virtscsi_event_done(struct virtqueue *vq)
  348. {
  349. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  350. struct virtio_scsi *vscsi = shost_priv(sh);
  351. virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
  352. };
  353. /**
  354. * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
  355. * @vq : the struct virtqueue we're talking about
  356. * @cmd : command structure
  357. * @req_size : size of the request buffer
  358. * @resp_size : size of the response buffer
  359. */
  360. static int virtscsi_add_cmd(struct virtqueue *vq,
  361. struct virtio_scsi_cmd *cmd,
  362. size_t req_size, size_t resp_size)
  363. {
  364. struct scsi_cmnd *sc = cmd->sc;
  365. struct scatterlist *sgs[6], req, resp;
  366. struct sg_table *out, *in;
  367. unsigned out_num = 0, in_num = 0;
  368. out = in = NULL;
  369. if (sc && sc->sc_data_direction != DMA_NONE) {
  370. if (sc->sc_data_direction != DMA_FROM_DEVICE)
  371. out = &scsi_out(sc)->table;
  372. if (sc->sc_data_direction != DMA_TO_DEVICE)
  373. in = &scsi_in(sc)->table;
  374. }
  375. /* Request header. */
  376. sg_init_one(&req, &cmd->req, req_size);
  377. sgs[out_num++] = &req;
  378. /* Data-out buffer. */
  379. if (out) {
  380. /* Place WRITE protection SGLs before Data OUT payload */
  381. if (scsi_prot_sg_count(sc))
  382. sgs[out_num++] = scsi_prot_sglist(sc);
  383. sgs[out_num++] = out->sgl;
  384. }
  385. /* Response header. */
  386. sg_init_one(&resp, &cmd->resp, resp_size);
  387. sgs[out_num + in_num++] = &resp;
  388. /* Data-in buffer */
  389. if (in) {
  390. /* Place READ protection SGLs before Data IN payload */
  391. if (scsi_prot_sg_count(sc))
  392. sgs[out_num + in_num++] = scsi_prot_sglist(sc);
  393. sgs[out_num + in_num++] = in->sgl;
  394. }
  395. return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
  396. }
  397. static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
  398. struct virtio_scsi_cmd *cmd,
  399. size_t req_size, size_t resp_size)
  400. {
  401. unsigned long flags;
  402. int err;
  403. bool needs_kick = false;
  404. spin_lock_irqsave(&vq->vq_lock, flags);
  405. err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
  406. if (!err)
  407. needs_kick = virtqueue_kick_prepare(vq->vq);
  408. spin_unlock_irqrestore(&vq->vq_lock, flags);
  409. if (needs_kick)
  410. virtqueue_notify(vq->vq);
  411. return err;
  412. }
  413. static void virtio_scsi_init_hdr(struct virtio_device *vdev,
  414. struct virtio_scsi_cmd_req *cmd,
  415. struct scsi_cmnd *sc)
  416. {
  417. cmd->lun[0] = 1;
  418. cmd->lun[1] = sc->device->id;
  419. cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
  420. cmd->lun[3] = sc->device->lun & 0xff;
  421. cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
  422. cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
  423. cmd->prio = 0;
  424. cmd->crn = 0;
  425. }
  426. #ifdef CONFIG_BLK_DEV_INTEGRITY
  427. static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
  428. struct virtio_scsi_cmd_req_pi *cmd_pi,
  429. struct scsi_cmnd *sc)
  430. {
  431. struct request *rq = sc->request;
  432. struct blk_integrity *bi;
  433. virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
  434. if (!rq || !scsi_prot_sg_count(sc))
  435. return;
  436. bi = blk_get_integrity(rq->rq_disk);
  437. if (sc->sc_data_direction == DMA_TO_DEVICE)
  438. cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
  439. blk_rq_sectors(rq) *
  440. bi->tuple_size);
  441. else if (sc->sc_data_direction == DMA_FROM_DEVICE)
  442. cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
  443. blk_rq_sectors(rq) *
  444. bi->tuple_size);
  445. }
  446. #endif
  447. static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
  448. struct virtio_scsi_vq *req_vq,
  449. struct scsi_cmnd *sc)
  450. {
  451. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  452. struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
  453. unsigned long flags;
  454. int req_size;
  455. int ret;
  456. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  457. /* TODO: check feature bit and fail if unsupported? */
  458. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  459. dev_dbg(&sc->device->sdev_gendev,
  460. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  461. memset(cmd, 0, sizeof(*cmd));
  462. cmd->sc = sc;
  463. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  464. #ifdef CONFIG_BLK_DEV_INTEGRITY
  465. if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
  466. virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
  467. memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
  468. req_size = sizeof(cmd->req.cmd_pi);
  469. } else
  470. #endif
  471. {
  472. virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
  473. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  474. req_size = sizeof(cmd->req.cmd);
  475. }
  476. ret = virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd));
  477. if (ret == -EIO) {
  478. cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
  479. spin_lock_irqsave(&req_vq->vq_lock, flags);
  480. virtscsi_complete_cmd(vscsi, cmd);
  481. spin_unlock_irqrestore(&req_vq->vq_lock, flags);
  482. } else if (ret != 0) {
  483. return SCSI_MLQUEUE_HOST_BUSY;
  484. }
  485. return 0;
  486. }
  487. static int virtscsi_queuecommand_single(struct Scsi_Host *sh,
  488. struct scsi_cmnd *sc)
  489. {
  490. struct virtio_scsi *vscsi = shost_priv(sh);
  491. struct virtio_scsi_target_state *tgt =
  492. scsi_target(sc->device)->hostdata;
  493. atomic_inc(&tgt->reqs);
  494. return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc);
  495. }
  496. static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
  497. struct scsi_cmnd *sc)
  498. {
  499. u32 tag = blk_mq_unique_tag(sc->request);
  500. u16 hwq = blk_mq_unique_tag_to_hwq(tag);
  501. return &vscsi->req_vqs[hwq];
  502. }
  503. static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi,
  504. struct virtio_scsi_target_state *tgt)
  505. {
  506. struct virtio_scsi_vq *vq;
  507. unsigned long flags;
  508. u32 queue_num;
  509. local_irq_save(flags);
  510. if (atomic_inc_return(&tgt->reqs) > 1) {
  511. unsigned long seq;
  512. do {
  513. seq = read_seqcount_begin(&tgt->tgt_seq);
  514. vq = tgt->req_vq;
  515. } while (read_seqcount_retry(&tgt->tgt_seq, seq));
  516. } else {
  517. /* no writes can be concurrent because of atomic_t */
  518. write_seqcount_begin(&tgt->tgt_seq);
  519. /* keep previous req_vq if a reader just arrived */
  520. if (unlikely(atomic_read(&tgt->reqs) > 1)) {
  521. vq = tgt->req_vq;
  522. goto unlock;
  523. }
  524. queue_num = smp_processor_id();
  525. while (unlikely(queue_num >= vscsi->num_queues))
  526. queue_num -= vscsi->num_queues;
  527. tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
  528. unlock:
  529. write_seqcount_end(&tgt->tgt_seq);
  530. }
  531. local_irq_restore(flags);
  532. return vq;
  533. }
  534. static int virtscsi_queuecommand_multi(struct Scsi_Host *sh,
  535. struct scsi_cmnd *sc)
  536. {
  537. struct virtio_scsi *vscsi = shost_priv(sh);
  538. struct virtio_scsi_target_state *tgt =
  539. scsi_target(sc->device)->hostdata;
  540. struct virtio_scsi_vq *req_vq;
  541. if (shost_use_blk_mq(sh))
  542. req_vq = virtscsi_pick_vq_mq(vscsi, sc);
  543. else
  544. req_vq = virtscsi_pick_vq(vscsi, tgt);
  545. return virtscsi_queuecommand(vscsi, req_vq, sc);
  546. }
  547. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  548. {
  549. DECLARE_COMPLETION_ONSTACK(comp);
  550. int ret = FAILED;
  551. cmd->comp = &comp;
  552. if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
  553. sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
  554. goto out;
  555. wait_for_completion(&comp);
  556. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  557. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  558. ret = SUCCESS;
  559. /*
  560. * The spec guarantees that all requests related to the TMF have
  561. * been completed, but the callback might not have run yet if
  562. * we're using independent interrupts (e.g. MSI). Poll the
  563. * virtqueues once.
  564. *
  565. * In the abort case, sc->scsi_done will do nothing, because
  566. * the block layer must have detected a timeout and as a result
  567. * REQ_ATOM_COMPLETE has been set.
  568. */
  569. virtscsi_poll_requests(vscsi);
  570. out:
  571. mempool_free(cmd, virtscsi_cmd_pool);
  572. return ret;
  573. }
  574. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  575. {
  576. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  577. struct virtio_scsi_cmd *cmd;
  578. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  579. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  580. if (!cmd)
  581. return FAILED;
  582. memset(cmd, 0, sizeof(*cmd));
  583. cmd->sc = sc;
  584. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  585. .type = VIRTIO_SCSI_T_TMF,
  586. .subtype = cpu_to_virtio32(vscsi->vdev,
  587. VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
  588. .lun[0] = 1,
  589. .lun[1] = sc->device->id,
  590. .lun[2] = (sc->device->lun >> 8) | 0x40,
  591. .lun[3] = sc->device->lun & 0xff,
  592. };
  593. return virtscsi_tmf(vscsi, cmd);
  594. }
  595. /**
  596. * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
  597. * @sdev: Virtscsi target whose queue depth to change
  598. * @qdepth: New queue depth
  599. */
  600. static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  601. {
  602. struct Scsi_Host *shost = sdev->host;
  603. int max_depth = shost->cmd_per_lun;
  604. return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
  605. }
  606. static int virtscsi_abort(struct scsi_cmnd *sc)
  607. {
  608. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  609. struct virtio_scsi_cmd *cmd;
  610. scmd_printk(KERN_INFO, sc, "abort\n");
  611. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  612. if (!cmd)
  613. return FAILED;
  614. memset(cmd, 0, sizeof(*cmd));
  615. cmd->sc = sc;
  616. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  617. .type = VIRTIO_SCSI_T_TMF,
  618. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  619. .lun[0] = 1,
  620. .lun[1] = sc->device->id,
  621. .lun[2] = (sc->device->lun >> 8) | 0x40,
  622. .lun[3] = sc->device->lun & 0xff,
  623. .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
  624. };
  625. return virtscsi_tmf(vscsi, cmd);
  626. }
  627. static int virtscsi_target_alloc(struct scsi_target *starget)
  628. {
  629. struct Scsi_Host *sh = dev_to_shost(starget->dev.parent);
  630. struct virtio_scsi *vscsi = shost_priv(sh);
  631. struct virtio_scsi_target_state *tgt =
  632. kmalloc(sizeof(*tgt), GFP_KERNEL);
  633. if (!tgt)
  634. return -ENOMEM;
  635. seqcount_init(&tgt->tgt_seq);
  636. atomic_set(&tgt->reqs, 0);
  637. tgt->req_vq = &vscsi->req_vqs[0];
  638. starget->hostdata = tgt;
  639. return 0;
  640. }
  641. static void virtscsi_target_destroy(struct scsi_target *starget)
  642. {
  643. struct virtio_scsi_target_state *tgt = starget->hostdata;
  644. kfree(tgt);
  645. }
  646. static struct scsi_host_template virtscsi_host_template_single = {
  647. .module = THIS_MODULE,
  648. .name = "Virtio SCSI HBA",
  649. .proc_name = "virtio_scsi",
  650. .this_id = -1,
  651. .cmd_size = sizeof(struct virtio_scsi_cmd),
  652. .queuecommand = virtscsi_queuecommand_single,
  653. .change_queue_depth = virtscsi_change_queue_depth,
  654. .eh_abort_handler = virtscsi_abort,
  655. .eh_device_reset_handler = virtscsi_device_reset,
  656. .can_queue = 1024,
  657. .dma_boundary = UINT_MAX,
  658. .use_clustering = ENABLE_CLUSTERING,
  659. .target_alloc = virtscsi_target_alloc,
  660. .target_destroy = virtscsi_target_destroy,
  661. .track_queue_depth = 1,
  662. };
  663. static struct scsi_host_template virtscsi_host_template_multi = {
  664. .module = THIS_MODULE,
  665. .name = "Virtio SCSI HBA",
  666. .proc_name = "virtio_scsi",
  667. .this_id = -1,
  668. .cmd_size = sizeof(struct virtio_scsi_cmd),
  669. .queuecommand = virtscsi_queuecommand_multi,
  670. .change_queue_depth = virtscsi_change_queue_depth,
  671. .eh_abort_handler = virtscsi_abort,
  672. .eh_device_reset_handler = virtscsi_device_reset,
  673. .can_queue = 1024,
  674. .dma_boundary = UINT_MAX,
  675. .use_clustering = ENABLE_CLUSTERING,
  676. .target_alloc = virtscsi_target_alloc,
  677. .target_destroy = virtscsi_target_destroy,
  678. .track_queue_depth = 1,
  679. };
  680. #define virtscsi_config_get(vdev, fld) \
  681. ({ \
  682. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  683. virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
  684. __val; \
  685. })
  686. #define virtscsi_config_set(vdev, fld, val) \
  687. do { \
  688. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  689. virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
  690. } while(0)
  691. static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  692. {
  693. int i;
  694. int cpu;
  695. /* In multiqueue mode, when the number of cpu is equal
  696. * to the number of request queues, we let the qeueues
  697. * to be private to one cpu by setting the affinity hint
  698. * to eliminate the contention.
  699. */
  700. if ((vscsi->num_queues == 1 ||
  701. vscsi->num_queues != num_online_cpus()) && affinity) {
  702. if (vscsi->affinity_hint_set)
  703. affinity = false;
  704. else
  705. return;
  706. }
  707. if (affinity) {
  708. i = 0;
  709. for_each_online_cpu(cpu) {
  710. virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu);
  711. i++;
  712. }
  713. vscsi->affinity_hint_set = true;
  714. } else {
  715. for (i = 0; i < vscsi->num_queues; i++) {
  716. if (!vscsi->req_vqs[i].vq)
  717. continue;
  718. virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
  719. }
  720. vscsi->affinity_hint_set = false;
  721. }
  722. }
  723. static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  724. {
  725. get_online_cpus();
  726. __virtscsi_set_affinity(vscsi, affinity);
  727. put_online_cpus();
  728. }
  729. static int virtscsi_cpu_online(unsigned int cpu, struct hlist_node *node)
  730. {
  731. struct virtio_scsi *vscsi = hlist_entry_safe(node, struct virtio_scsi,
  732. node);
  733. __virtscsi_set_affinity(vscsi, true);
  734. return 0;
  735. }
  736. static int virtscsi_cpu_notif_add(struct virtio_scsi *vi)
  737. {
  738. int ret;
  739. ret = cpuhp_state_add_instance(virtioscsi_online, &vi->node);
  740. if (ret)
  741. return ret;
  742. ret = cpuhp_state_add_instance(CPUHP_VIRT_SCSI_DEAD, &vi->node_dead);
  743. if (ret)
  744. cpuhp_state_remove_instance(virtioscsi_online, &vi->node);
  745. return ret;
  746. }
  747. static void virtscsi_cpu_notif_remove(struct virtio_scsi *vi)
  748. {
  749. cpuhp_state_remove_instance_nocalls(virtioscsi_online, &vi->node);
  750. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_SCSI_DEAD,
  751. &vi->node_dead);
  752. }
  753. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  754. struct virtqueue *vq)
  755. {
  756. spin_lock_init(&virtscsi_vq->vq_lock);
  757. virtscsi_vq->vq = vq;
  758. }
  759. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  760. {
  761. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  762. struct virtio_scsi *vscsi = shost_priv(sh);
  763. virtscsi_set_affinity(vscsi, false);
  764. /* Stop all the virtqueues. */
  765. vdev->config->reset(vdev);
  766. vdev->config->del_vqs(vdev);
  767. }
  768. static int virtscsi_init(struct virtio_device *vdev,
  769. struct virtio_scsi *vscsi)
  770. {
  771. int err;
  772. u32 i;
  773. u32 num_vqs;
  774. vq_callback_t **callbacks;
  775. const char **names;
  776. struct virtqueue **vqs;
  777. num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
  778. vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL);
  779. callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL);
  780. names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL);
  781. if (!callbacks || !vqs || !names) {
  782. err = -ENOMEM;
  783. goto out;
  784. }
  785. callbacks[0] = virtscsi_ctrl_done;
  786. callbacks[1] = virtscsi_event_done;
  787. names[0] = "control";
  788. names[1] = "event";
  789. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
  790. callbacks[i] = virtscsi_req_done;
  791. names[i] = "request";
  792. }
  793. /* Discover virtqueues and write information to configuration. */
  794. err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names,
  795. NULL);
  796. if (err)
  797. goto out;
  798. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  799. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  800. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
  801. virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
  802. vqs[i]);
  803. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  804. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  805. err = 0;
  806. out:
  807. kfree(names);
  808. kfree(callbacks);
  809. kfree(vqs);
  810. if (err)
  811. virtscsi_remove_vqs(vdev);
  812. return err;
  813. }
  814. static int virtscsi_probe(struct virtio_device *vdev)
  815. {
  816. struct Scsi_Host *shost;
  817. struct virtio_scsi *vscsi;
  818. int err;
  819. u32 sg_elems, num_targets;
  820. u32 cmd_per_lun;
  821. u32 num_queues;
  822. struct scsi_host_template *hostt;
  823. if (!vdev->config->get) {
  824. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  825. __func__);
  826. return -EINVAL;
  827. }
  828. /* We need to know how many queues before we allocate. */
  829. num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
  830. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  831. if (num_queues == 1)
  832. hostt = &virtscsi_host_template_single;
  833. else
  834. hostt = &virtscsi_host_template_multi;
  835. shost = scsi_host_alloc(hostt,
  836. sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
  837. if (!shost)
  838. return -ENOMEM;
  839. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  840. shost->sg_tablesize = sg_elems;
  841. vscsi = shost_priv(shost);
  842. vscsi->vdev = vdev;
  843. vscsi->num_queues = num_queues;
  844. vdev->priv = shost;
  845. err = virtscsi_init(vdev, vscsi);
  846. if (err)
  847. goto virtscsi_init_failed;
  848. err = virtscsi_cpu_notif_add(vscsi);
  849. if (err)
  850. goto scsi_add_host_failed;
  851. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  852. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  853. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  854. /* LUNs > 256 are reported with format 1, so they go in the range
  855. * 16640-32767.
  856. */
  857. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  858. shost->max_id = num_targets;
  859. shost->max_channel = 0;
  860. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  861. shost->nr_hw_queues = num_queues;
  862. #ifdef CONFIG_BLK_DEV_INTEGRITY
  863. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
  864. int host_prot;
  865. host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
  866. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
  867. SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
  868. scsi_host_set_prot(shost, host_prot);
  869. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  870. }
  871. #endif
  872. err = scsi_add_host(shost, &vdev->dev);
  873. if (err)
  874. goto scsi_add_host_failed;
  875. virtio_device_ready(vdev);
  876. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  877. virtscsi_kick_event_all(vscsi);
  878. scsi_scan_host(shost);
  879. return 0;
  880. scsi_add_host_failed:
  881. vdev->config->del_vqs(vdev);
  882. virtscsi_init_failed:
  883. scsi_host_put(shost);
  884. return err;
  885. }
  886. static void virtscsi_remove(struct virtio_device *vdev)
  887. {
  888. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  889. struct virtio_scsi *vscsi = shost_priv(shost);
  890. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  891. virtscsi_cancel_event_work(vscsi);
  892. scsi_remove_host(shost);
  893. virtscsi_cpu_notif_remove(vscsi);
  894. virtscsi_remove_vqs(vdev);
  895. scsi_host_put(shost);
  896. }
  897. #ifdef CONFIG_PM_SLEEP
  898. static int virtscsi_freeze(struct virtio_device *vdev)
  899. {
  900. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  901. struct virtio_scsi *vscsi = shost_priv(sh);
  902. virtscsi_cpu_notif_remove(vscsi);
  903. virtscsi_remove_vqs(vdev);
  904. return 0;
  905. }
  906. static int virtscsi_restore(struct virtio_device *vdev)
  907. {
  908. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  909. struct virtio_scsi *vscsi = shost_priv(sh);
  910. int err;
  911. err = virtscsi_init(vdev, vscsi);
  912. if (err)
  913. return err;
  914. err = virtscsi_cpu_notif_add(vscsi);
  915. if (err) {
  916. vdev->config->del_vqs(vdev);
  917. return err;
  918. }
  919. virtio_device_ready(vdev);
  920. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  921. virtscsi_kick_event_all(vscsi);
  922. return err;
  923. }
  924. #endif
  925. static struct virtio_device_id id_table[] = {
  926. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  927. { 0 },
  928. };
  929. static unsigned int features[] = {
  930. VIRTIO_SCSI_F_HOTPLUG,
  931. VIRTIO_SCSI_F_CHANGE,
  932. #ifdef CONFIG_BLK_DEV_INTEGRITY
  933. VIRTIO_SCSI_F_T10_PI,
  934. #endif
  935. };
  936. static struct virtio_driver virtio_scsi_driver = {
  937. .feature_table = features,
  938. .feature_table_size = ARRAY_SIZE(features),
  939. .driver.name = KBUILD_MODNAME,
  940. .driver.owner = THIS_MODULE,
  941. .id_table = id_table,
  942. .probe = virtscsi_probe,
  943. #ifdef CONFIG_PM_SLEEP
  944. .freeze = virtscsi_freeze,
  945. .restore = virtscsi_restore,
  946. #endif
  947. .remove = virtscsi_remove,
  948. };
  949. static int __init init(void)
  950. {
  951. int ret = -ENOMEM;
  952. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  953. if (!virtscsi_cmd_cache) {
  954. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  955. goto error;
  956. }
  957. virtscsi_cmd_pool =
  958. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  959. virtscsi_cmd_cache);
  960. if (!virtscsi_cmd_pool) {
  961. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  962. goto error;
  963. }
  964. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  965. "scsi/virtio:online",
  966. virtscsi_cpu_online, NULL);
  967. if (ret < 0)
  968. goto error;
  969. virtioscsi_online = ret;
  970. ret = cpuhp_setup_state_multi(CPUHP_VIRT_SCSI_DEAD, "scsi/virtio:dead",
  971. NULL, virtscsi_cpu_online);
  972. if (ret)
  973. goto error;
  974. ret = register_virtio_driver(&virtio_scsi_driver);
  975. if (ret < 0)
  976. goto error;
  977. return 0;
  978. error:
  979. if (virtscsi_cmd_pool) {
  980. mempool_destroy(virtscsi_cmd_pool);
  981. virtscsi_cmd_pool = NULL;
  982. }
  983. if (virtscsi_cmd_cache) {
  984. kmem_cache_destroy(virtscsi_cmd_cache);
  985. virtscsi_cmd_cache = NULL;
  986. }
  987. if (virtioscsi_online)
  988. cpuhp_remove_multi_state(virtioscsi_online);
  989. cpuhp_remove_multi_state(CPUHP_VIRT_SCSI_DEAD);
  990. return ret;
  991. }
  992. static void __exit fini(void)
  993. {
  994. unregister_virtio_driver(&virtio_scsi_driver);
  995. cpuhp_remove_multi_state(virtioscsi_online);
  996. cpuhp_remove_multi_state(CPUHP_VIRT_SCSI_DEAD);
  997. mempool_destroy(virtscsi_cmd_pool);
  998. kmem_cache_destroy(virtscsi_cmd_cache);
  999. }
  1000. module_init(init);
  1001. module_exit(fini);
  1002. MODULE_DEVICE_TABLE(virtio, id_table);
  1003. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  1004. MODULE_LICENSE("GPL");