virtio_scsi.c 31 KB

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