virtio_scsi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio SCSI HBA driver
  4. *
  5. * Copyright IBM Corp. 2010
  6. * Copyright Red Hat, Inc. 2011
  7. *
  8. * Authors:
  9. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  10. * Paolo Bonzini <pbonzini@redhat.com>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/mempool.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/virtio.h>
  18. #include <linux/virtio_ids.h>
  19. #include <linux/virtio_config.h>
  20. #include <linux/virtio_scsi.h>
  21. #include <linux/cpu.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/blk-integrity.h>
  24. #include <scsi/scsi_host.h>
  25. #include <scsi/scsi_device.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_tcq.h>
  28. #include <scsi/scsi_devinfo.h>
  29. #include <linux/seqlock.h>
  30. #include <linux/dma-mapping.h>
  31. #include "sd.h"
  32. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  33. #define VIRTIO_SCSI_EVENT_LEN 8
  34. #define VIRTIO_SCSI_VQ_BASE 2
  35. static unsigned int virtscsi_poll_queues;
  36. module_param(virtscsi_poll_queues, uint, 0644);
  37. MODULE_PARM_DESC(virtscsi_poll_queues,
  38. "The number of dedicated virtqueues for polling I/O");
  39. /* Command queue element */
  40. struct virtio_scsi_cmd {
  41. struct scsi_cmnd *sc;
  42. struct completion *comp;
  43. union {
  44. struct virtio_scsi_cmd_req cmd;
  45. struct virtio_scsi_cmd_req_pi cmd_pi;
  46. struct virtio_scsi_ctrl_tmf_req tmf;
  47. struct virtio_scsi_ctrl_an_req an;
  48. } req;
  49. union {
  50. struct virtio_scsi_cmd_resp cmd;
  51. struct virtio_scsi_ctrl_tmf_resp tmf;
  52. struct virtio_scsi_ctrl_an_resp an;
  53. struct virtio_scsi_event evt;
  54. } resp;
  55. } ____cacheline_aligned_in_smp;
  56. struct virtio_scsi_event_node {
  57. struct virtio_scsi *vscsi;
  58. struct virtio_scsi_event *event;
  59. struct work_struct work;
  60. };
  61. struct virtio_scsi_vq {
  62. /* Protects vq */
  63. spinlock_t vq_lock;
  64. struct virtqueue *vq;
  65. };
  66. /* Driver instance state */
  67. struct virtio_scsi {
  68. struct virtio_device *vdev;
  69. /* Get some buffers ready for event vq */
  70. struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
  71. u32 num_queues;
  72. int io_queues[HCTX_MAX_TYPES];
  73. struct hlist_node node;
  74. /* Protected by event_vq lock */
  75. bool stop_events;
  76. struct virtio_scsi_vq ctrl_vq;
  77. struct virtio_scsi_vq event_vq;
  78. __dma_from_device_group_begin();
  79. struct virtio_scsi_event events[VIRTIO_SCSI_EVENT_LEN];
  80. __dma_from_device_group_end();
  81. struct virtio_scsi_vq req_vqs[];
  82. };
  83. static struct kmem_cache *virtscsi_cmd_cache;
  84. static mempool_t *virtscsi_cmd_pool;
  85. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  86. {
  87. return vdev->priv;
  88. }
  89. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  90. {
  91. if (resid)
  92. scsi_set_resid(sc, min(resid, scsi_bufflen(sc)));
  93. }
  94. /*
  95. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  96. *
  97. * Called with vq_lock held.
  98. */
  99. static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
  100. {
  101. struct virtio_scsi_cmd *cmd = buf;
  102. struct scsi_cmnd *sc = cmd->sc;
  103. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  104. dev_dbg(&sc->device->sdev_gendev,
  105. "cmd %p response %u status %#02x sense_len %u\n",
  106. sc, resp->response, resp->status, resp->sense_len);
  107. sc->result = resp->status;
  108. virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
  109. switch (resp->response) {
  110. case VIRTIO_SCSI_S_OK:
  111. set_host_byte(sc, DID_OK);
  112. break;
  113. case VIRTIO_SCSI_S_OVERRUN:
  114. set_host_byte(sc, DID_ERROR);
  115. break;
  116. case VIRTIO_SCSI_S_ABORTED:
  117. set_host_byte(sc, DID_ABORT);
  118. break;
  119. case VIRTIO_SCSI_S_BAD_TARGET:
  120. set_host_byte(sc, DID_BAD_TARGET);
  121. break;
  122. case VIRTIO_SCSI_S_RESET:
  123. set_host_byte(sc, DID_RESET);
  124. break;
  125. case VIRTIO_SCSI_S_BUSY:
  126. set_host_byte(sc, DID_BUS_BUSY);
  127. break;
  128. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  129. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  130. break;
  131. case VIRTIO_SCSI_S_TARGET_FAILURE:
  132. set_host_byte(sc, DID_BAD_TARGET);
  133. break;
  134. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  135. set_status_byte(sc, SAM_STAT_RESERVATION_CONFLICT);
  136. break;
  137. default:
  138. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  139. resp->response);
  140. fallthrough;
  141. case VIRTIO_SCSI_S_FAILURE:
  142. set_host_byte(sc, DID_ERROR);
  143. break;
  144. }
  145. WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
  146. VIRTIO_SCSI_SENSE_SIZE);
  147. if (resp->sense_len) {
  148. memcpy(sc->sense_buffer, resp->sense,
  149. min_t(u32,
  150. virtio32_to_cpu(vscsi->vdev, resp->sense_len),
  151. VIRTIO_SCSI_SENSE_SIZE));
  152. }
  153. scsi_done(sc);
  154. }
  155. static void virtscsi_vq_done(struct virtio_scsi *vscsi,
  156. struct virtio_scsi_vq *virtscsi_vq,
  157. void (*fn)(struct virtio_scsi *vscsi, void *buf))
  158. {
  159. void *buf;
  160. unsigned int len;
  161. unsigned long flags;
  162. struct virtqueue *vq = virtscsi_vq->vq;
  163. spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
  164. do {
  165. virtqueue_disable_cb(vq);
  166. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  167. fn(vscsi, buf);
  168. } while (!virtqueue_enable_cb(vq));
  169. spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
  170. }
  171. static void virtscsi_req_done(struct virtqueue *vq)
  172. {
  173. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  174. struct virtio_scsi *vscsi = shost_priv(sh);
  175. int index = vq->index - VIRTIO_SCSI_VQ_BASE;
  176. struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
  177. virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
  178. };
  179. static void virtscsi_poll_requests(struct virtio_scsi *vscsi)
  180. {
  181. int i, num_vqs;
  182. num_vqs = vscsi->num_queues;
  183. for (i = 0; i < num_vqs; i++)
  184. virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
  185. virtscsi_complete_cmd);
  186. }
  187. static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
  188. {
  189. struct virtio_scsi_cmd *cmd = buf;
  190. if (cmd->comp)
  191. complete(cmd->comp);
  192. }
  193. static void virtscsi_ctrl_done(struct virtqueue *vq)
  194. {
  195. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  196. struct virtio_scsi *vscsi = shost_priv(sh);
  197. virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
  198. };
  199. static void virtscsi_handle_event(struct work_struct *work);
  200. static int virtscsi_kick_event(struct virtio_scsi *vscsi,
  201. struct virtio_scsi_event_node *event_node)
  202. {
  203. int err;
  204. struct scatterlist sg;
  205. unsigned long flags;
  206. INIT_WORK(&event_node->work, virtscsi_handle_event);
  207. sg_init_one(&sg, event_node->event, sizeof(struct virtio_scsi_event));
  208. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  209. err = virtqueue_add_inbuf_cache_clean(vscsi->event_vq.vq, &sg, 1, event_node,
  210. GFP_ATOMIC);
  211. if (!err)
  212. virtqueue_kick(vscsi->event_vq.vq);
  213. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  214. return err;
  215. }
  216. static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
  217. {
  218. int i;
  219. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
  220. vscsi->event_list[i].vscsi = vscsi;
  221. vscsi->event_list[i].event = &vscsi->events[i];
  222. virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
  223. }
  224. return 0;
  225. }
  226. static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
  227. {
  228. int i;
  229. /* Stop scheduling work before calling cancel_work_sync. */
  230. spin_lock_irq(&vscsi->event_vq.vq_lock);
  231. vscsi->stop_events = true;
  232. spin_unlock_irq(&vscsi->event_vq.vq_lock);
  233. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
  234. cancel_work_sync(&vscsi->event_list[i].work);
  235. }
  236. static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
  237. struct virtio_scsi_event *event)
  238. {
  239. struct scsi_device *sdev;
  240. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  241. unsigned int target = event->lun[1];
  242. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  243. switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
  244. case VIRTIO_SCSI_EVT_RESET_RESCAN:
  245. if (lun == 0) {
  246. scsi_scan_target(&shost->shost_gendev, 0, target,
  247. SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
  248. } else {
  249. scsi_add_device(shost, 0, target, lun);
  250. }
  251. break;
  252. case VIRTIO_SCSI_EVT_RESET_REMOVED:
  253. sdev = scsi_device_lookup(shost, 0, target, lun);
  254. if (sdev) {
  255. scsi_remove_device(sdev);
  256. scsi_device_put(sdev);
  257. } else {
  258. pr_err("SCSI device %d 0 %d %d not found\n",
  259. shost->host_no, target, lun);
  260. }
  261. break;
  262. default:
  263. pr_info("Unsupported virtio scsi event reason %x\n", event->reason);
  264. }
  265. }
  266. static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
  267. struct virtio_scsi_event *event)
  268. {
  269. struct scsi_device *sdev;
  270. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  271. unsigned int target = event->lun[1];
  272. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  273. u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
  274. u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
  275. sdev = scsi_device_lookup(shost, 0, target, lun);
  276. if (!sdev) {
  277. pr_err("SCSI device %d 0 %d %d not found\n",
  278. shost->host_no, target, lun);
  279. return;
  280. }
  281. /* Handle "Parameters changed", "Mode parameters changed", and
  282. "Capacity data has changed". */
  283. if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
  284. scsi_rescan_device(sdev);
  285. scsi_device_put(sdev);
  286. }
  287. static int virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
  288. {
  289. struct scsi_device *sdev;
  290. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  291. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  292. int result, inquiry_len, inq_result_len = 256;
  293. char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
  294. if (!inq_result)
  295. return -ENOMEM;
  296. shost_for_each_device(sdev, shost) {
  297. inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
  298. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  299. scsi_cmd[0] = INQUIRY;
  300. scsi_cmd[4] = (unsigned char) inquiry_len;
  301. memset(inq_result, 0, inq_result_len);
  302. result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN,
  303. inq_result, inquiry_len,
  304. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  305. if (result == 0 && inq_result[0] >> 5) {
  306. /* PQ indicates the LUN is not attached */
  307. scsi_remove_device(sdev);
  308. } else if (result > 0 && host_byte(result) == DID_BAD_TARGET) {
  309. /*
  310. * If all LUNs of a virtio-scsi device are unplugged
  311. * it will respond with BAD TARGET on any INQUIRY
  312. * command.
  313. * Remove the device in this case as well.
  314. */
  315. scsi_remove_device(sdev);
  316. }
  317. }
  318. kfree(inq_result);
  319. return 0;
  320. }
  321. static void virtscsi_handle_event(struct work_struct *work)
  322. {
  323. struct virtio_scsi_event_node *event_node =
  324. container_of(work, struct virtio_scsi_event_node, work);
  325. struct virtio_scsi *vscsi = event_node->vscsi;
  326. struct virtio_scsi_event *event = event_node->event;
  327. if (event->event &
  328. cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
  329. int ret;
  330. event->event &= ~cpu_to_virtio32(vscsi->vdev,
  331. VIRTIO_SCSI_T_EVENTS_MISSED);
  332. ret = virtscsi_rescan_hotunplug(vscsi);
  333. if (ret)
  334. return;
  335. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  336. }
  337. switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
  338. case VIRTIO_SCSI_T_NO_EVENT:
  339. break;
  340. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  341. virtscsi_handle_transport_reset(vscsi, event);
  342. break;
  343. case VIRTIO_SCSI_T_PARAM_CHANGE:
  344. virtscsi_handle_param_change(vscsi, event);
  345. break;
  346. default:
  347. pr_err("Unsupported virtio scsi event %x\n", event->event);
  348. }
  349. virtscsi_kick_event(vscsi, event_node);
  350. }
  351. static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
  352. {
  353. struct virtio_scsi_event_node *event_node = buf;
  354. if (!vscsi->stop_events)
  355. queue_work(system_freezable_wq, &event_node->work);
  356. }
  357. static void virtscsi_event_done(struct virtqueue *vq)
  358. {
  359. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  360. struct virtio_scsi *vscsi = shost_priv(sh);
  361. virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
  362. };
  363. static int __virtscsi_add_cmd(struct virtqueue *vq,
  364. struct virtio_scsi_cmd *cmd,
  365. size_t req_size, size_t resp_size)
  366. {
  367. struct scsi_cmnd *sc = cmd->sc;
  368. struct scatterlist *sgs[6], req, resp;
  369. struct sg_table *out, *in;
  370. unsigned out_num = 0, in_num = 0;
  371. out = in = NULL;
  372. if (sc && sc->sc_data_direction != DMA_NONE) {
  373. if (sc->sc_data_direction != DMA_FROM_DEVICE)
  374. out = &sc->sdb.table;
  375. if (sc->sc_data_direction != DMA_TO_DEVICE)
  376. in = &sc->sdb.table;
  377. }
  378. /* Request header. */
  379. sg_init_one(&req, &cmd->req, req_size);
  380. sgs[out_num++] = &req;
  381. /* Data-out buffer. */
  382. if (out) {
  383. /* Place WRITE protection SGLs before Data OUT payload */
  384. if (scsi_prot_sg_count(sc))
  385. sgs[out_num++] = scsi_prot_sglist(sc);
  386. sgs[out_num++] = out->sgl;
  387. }
  388. /* Response header. */
  389. sg_init_one(&resp, &cmd->resp, resp_size);
  390. sgs[out_num + in_num++] = &resp;
  391. /* Data-in buffer */
  392. if (in) {
  393. /* Place READ protection SGLs before Data IN payload */
  394. if (scsi_prot_sg_count(sc))
  395. sgs[out_num + in_num++] = scsi_prot_sglist(sc);
  396. sgs[out_num + in_num++] = in->sgl;
  397. }
  398. return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
  399. }
  400. static void virtscsi_kick_vq(struct virtio_scsi_vq *vq)
  401. {
  402. bool needs_kick;
  403. unsigned long flags;
  404. spin_lock_irqsave(&vq->vq_lock, flags);
  405. needs_kick = virtqueue_kick_prepare(vq->vq);
  406. spin_unlock_irqrestore(&vq->vq_lock, flags);
  407. if (needs_kick)
  408. virtqueue_notify(vq->vq);
  409. }
  410. /**
  411. * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue, optionally kick it
  412. * @vq : the struct virtqueue we're talking about
  413. * @cmd : command structure
  414. * @req_size : size of the request buffer
  415. * @resp_size : size of the response buffer
  416. * @kick : whether to kick the virtqueue immediately
  417. */
  418. static int virtscsi_add_cmd(struct virtio_scsi_vq *vq,
  419. struct virtio_scsi_cmd *cmd,
  420. size_t req_size, size_t resp_size,
  421. bool kick)
  422. {
  423. unsigned long flags;
  424. int err;
  425. bool needs_kick = false;
  426. spin_lock_irqsave(&vq->vq_lock, flags);
  427. err = __virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
  428. if (!err && kick)
  429. needs_kick = virtqueue_kick_prepare(vq->vq);
  430. spin_unlock_irqrestore(&vq->vq_lock, flags);
  431. if (needs_kick)
  432. virtqueue_notify(vq->vq);
  433. return err;
  434. }
  435. static void virtio_scsi_init_hdr(struct virtio_device *vdev,
  436. struct virtio_scsi_cmd_req *cmd,
  437. struct scsi_cmnd *sc)
  438. {
  439. cmd->lun[0] = 1;
  440. cmd->lun[1] = sc->device->id;
  441. cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
  442. cmd->lun[3] = sc->device->lun & 0xff;
  443. cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
  444. cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
  445. cmd->prio = 0;
  446. cmd->crn = 0;
  447. }
  448. #ifdef CONFIG_BLK_DEV_INTEGRITY
  449. static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
  450. struct virtio_scsi_cmd_req_pi *cmd_pi,
  451. struct scsi_cmnd *sc)
  452. {
  453. struct request *rq = scsi_cmd_to_rq(sc);
  454. struct blk_integrity *bi;
  455. virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
  456. if (!rq || !scsi_prot_sg_count(sc))
  457. return;
  458. bi = blk_get_integrity(rq->q->disk);
  459. if (sc->sc_data_direction == DMA_TO_DEVICE)
  460. cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
  461. bio_integrity_bytes(bi,
  462. blk_rq_sectors(rq)));
  463. else if (sc->sc_data_direction == DMA_FROM_DEVICE)
  464. cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
  465. bio_integrity_bytes(bi,
  466. blk_rq_sectors(rq)));
  467. }
  468. #endif
  469. static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
  470. struct scsi_cmnd *sc)
  471. {
  472. u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(sc));
  473. u16 hwq = blk_mq_unique_tag_to_hwq(tag);
  474. return &vscsi->req_vqs[hwq];
  475. }
  476. static enum scsi_qc_status virtscsi_queuecommand(struct Scsi_Host *shost,
  477. struct scsi_cmnd *sc)
  478. {
  479. struct virtio_scsi *vscsi = shost_priv(shost);
  480. struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
  481. struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
  482. bool kick;
  483. unsigned long flags;
  484. int req_size;
  485. int ret;
  486. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  487. /* TODO: check feature bit and fail if unsupported? */
  488. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  489. dev_dbg(&sc->device->sdev_gendev,
  490. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  491. cmd->sc = sc;
  492. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  493. #ifdef CONFIG_BLK_DEV_INTEGRITY
  494. if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
  495. virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
  496. memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
  497. req_size = sizeof(cmd->req.cmd_pi);
  498. } else
  499. #endif
  500. {
  501. virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
  502. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  503. req_size = sizeof(cmd->req.cmd);
  504. }
  505. kick = (sc->flags & SCMD_LAST) != 0;
  506. ret = virtscsi_add_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd), kick);
  507. if (ret == -EIO) {
  508. cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
  509. spin_lock_irqsave(&req_vq->vq_lock, flags);
  510. virtscsi_complete_cmd(vscsi, cmd);
  511. spin_unlock_irqrestore(&req_vq->vq_lock, flags);
  512. } else if (ret != 0) {
  513. return SCSI_MLQUEUE_HOST_BUSY;
  514. }
  515. return 0;
  516. }
  517. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  518. {
  519. DECLARE_COMPLETION_ONSTACK(comp);
  520. int ret = FAILED;
  521. cmd->comp = &comp;
  522. if (virtscsi_add_cmd(&vscsi->ctrl_vq, cmd,
  523. sizeof cmd->req.tmf, sizeof cmd->resp.tmf, true) < 0)
  524. goto out;
  525. wait_for_completion(&comp);
  526. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  527. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  528. ret = SUCCESS;
  529. /*
  530. * The spec guarantees that all requests related to the TMF have
  531. * been completed, but the callback might not have run yet if
  532. * we're using independent interrupts (e.g. MSI). Poll the
  533. * virtqueues once.
  534. *
  535. * In the abort case, scsi_done() will do nothing, because the
  536. * command timed out and hence SCMD_STATE_COMPLETE has been set.
  537. */
  538. virtscsi_poll_requests(vscsi);
  539. out:
  540. mempool_free(cmd, virtscsi_cmd_pool);
  541. return ret;
  542. }
  543. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  544. {
  545. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  546. struct virtio_scsi_cmd *cmd;
  547. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  548. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  549. if (!cmd)
  550. return FAILED;
  551. memset(cmd, 0, sizeof(*cmd));
  552. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  553. .type = VIRTIO_SCSI_T_TMF,
  554. .subtype = cpu_to_virtio32(vscsi->vdev,
  555. VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
  556. .lun[0] = 1,
  557. .lun[1] = sc->device->id,
  558. .lun[2] = (sc->device->lun >> 8) | 0x40,
  559. .lun[3] = sc->device->lun & 0xff,
  560. };
  561. return virtscsi_tmf(vscsi, cmd);
  562. }
  563. static int virtscsi_device_alloc(struct scsi_device *sdevice)
  564. {
  565. /*
  566. * Passed through SCSI targets (e.g. with qemu's 'scsi-block')
  567. * may have transfer limits which come from the host SCSI
  568. * controller or something on the host side other than the
  569. * target itself.
  570. *
  571. * To make this work properly, the hypervisor can adjust the
  572. * target's VPD information to advertise these limits. But
  573. * for that to work, the guest has to look at the VPD pages,
  574. * which we won't do by default if it is an SPC-2 device, even
  575. * if it does actually support it.
  576. *
  577. * So, set the blist to always try to read the VPD pages.
  578. */
  579. sdevice->sdev_bflags = BLIST_TRY_VPD_PAGES;
  580. return 0;
  581. }
  582. /**
  583. * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
  584. * @sdev: Virtscsi target whose queue depth to change
  585. * @qdepth: New queue depth
  586. */
  587. static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  588. {
  589. struct Scsi_Host *shost = sdev->host;
  590. int max_depth = shost->cmd_per_lun;
  591. return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
  592. }
  593. static int virtscsi_abort(struct scsi_cmnd *sc)
  594. {
  595. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  596. struct virtio_scsi_cmd *cmd;
  597. scmd_printk(KERN_INFO, sc, "abort\n");
  598. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  599. if (!cmd)
  600. return FAILED;
  601. memset(cmd, 0, sizeof(*cmd));
  602. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  603. .type = VIRTIO_SCSI_T_TMF,
  604. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  605. .lun[0] = 1,
  606. .lun[1] = sc->device->id,
  607. .lun[2] = (sc->device->lun >> 8) | 0x40,
  608. .lun[3] = sc->device->lun & 0xff,
  609. .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
  610. };
  611. return virtscsi_tmf(vscsi, cmd);
  612. }
  613. static void virtscsi_map_queues(struct Scsi_Host *shost)
  614. {
  615. struct virtio_scsi *vscsi = shost_priv(shost);
  616. int i, qoff;
  617. for (i = 0, qoff = 0; i < shost->nr_maps; i++) {
  618. struct blk_mq_queue_map *map = &shost->tag_set.map[i];
  619. map->nr_queues = vscsi->io_queues[i];
  620. map->queue_offset = qoff;
  621. qoff += map->nr_queues;
  622. if (map->nr_queues == 0)
  623. continue;
  624. /*
  625. * Regular queues have interrupts and hence CPU affinity is
  626. * defined by the core virtio code, but polling queues have
  627. * no interrupts so we let the block layer assign CPU affinity.
  628. */
  629. if (i == HCTX_TYPE_POLL)
  630. blk_mq_map_queues(map);
  631. else
  632. blk_mq_map_hw_queues(map, &vscsi->vdev->dev, 2);
  633. }
  634. }
  635. static int virtscsi_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
  636. {
  637. struct virtio_scsi *vscsi = shost_priv(shost);
  638. struct virtio_scsi_vq *virtscsi_vq = &vscsi->req_vqs[queue_num];
  639. unsigned long flags;
  640. unsigned int len;
  641. int found = 0;
  642. void *buf;
  643. spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
  644. while ((buf = virtqueue_get_buf(virtscsi_vq->vq, &len)) != NULL) {
  645. virtscsi_complete_cmd(vscsi, buf);
  646. found++;
  647. }
  648. spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
  649. return found;
  650. }
  651. static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq)
  652. {
  653. struct virtio_scsi *vscsi = shost_priv(shost);
  654. virtscsi_kick_vq(&vscsi->req_vqs[hwq]);
  655. }
  656. /*
  657. * The host guarantees to respond to each command, although I/O
  658. * latencies might be higher than on bare metal. Reset the timer
  659. * unconditionally to give the host a chance to perform EH.
  660. */
  661. static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
  662. {
  663. return SCSI_EH_RESET_TIMER;
  664. }
  665. static const struct scsi_host_template virtscsi_host_template = {
  666. .module = THIS_MODULE,
  667. .name = "Virtio SCSI HBA",
  668. .proc_name = "virtio_scsi",
  669. .this_id = -1,
  670. .cmd_size = sizeof(struct virtio_scsi_cmd),
  671. .queuecommand = virtscsi_queuecommand,
  672. .mq_poll = virtscsi_mq_poll,
  673. .commit_rqs = virtscsi_commit_rqs,
  674. .change_queue_depth = virtscsi_change_queue_depth,
  675. .eh_abort_handler = virtscsi_abort,
  676. .eh_device_reset_handler = virtscsi_device_reset,
  677. .eh_timed_out = virtscsi_eh_timed_out,
  678. .sdev_init = virtscsi_device_alloc,
  679. .dma_boundary = UINT_MAX,
  680. .map_queues = virtscsi_map_queues,
  681. .track_queue_depth = 1,
  682. };
  683. #define virtscsi_config_get(vdev, fld) \
  684. ({ \
  685. __virtio_native_type(struct virtio_scsi_config, fld) __val; \
  686. virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
  687. __val; \
  688. })
  689. #define virtscsi_config_set(vdev, fld, val) \
  690. do { \
  691. __virtio_native_type(struct virtio_scsi_config, fld) __val = (val); \
  692. virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
  693. } while(0)
  694. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  695. struct virtqueue *vq)
  696. {
  697. spin_lock_init(&virtscsi_vq->vq_lock);
  698. virtscsi_vq->vq = vq;
  699. }
  700. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  701. {
  702. /* Stop all the virtqueues. */
  703. virtio_reset_device(vdev);
  704. vdev->config->del_vqs(vdev);
  705. }
  706. static int virtscsi_init(struct virtio_device *vdev,
  707. struct virtio_scsi *vscsi)
  708. {
  709. int err;
  710. u32 i;
  711. u32 num_vqs, num_poll_vqs, num_req_vqs;
  712. struct virtqueue_info *vqs_info;
  713. struct virtqueue **vqs;
  714. struct irq_affinity desc = { .pre_vectors = 2 };
  715. num_req_vqs = vscsi->num_queues;
  716. num_vqs = num_req_vqs + VIRTIO_SCSI_VQ_BASE;
  717. vqs = kmalloc_objs(struct virtqueue *, num_vqs);
  718. vqs_info = kzalloc_objs(*vqs_info, num_vqs);
  719. if (!vqs || !vqs_info) {
  720. err = -ENOMEM;
  721. goto out;
  722. }
  723. num_poll_vqs = min_t(unsigned int, virtscsi_poll_queues,
  724. num_req_vqs - 1);
  725. vscsi->io_queues[HCTX_TYPE_DEFAULT] = num_req_vqs - num_poll_vqs;
  726. vscsi->io_queues[HCTX_TYPE_READ] = 0;
  727. vscsi->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
  728. dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
  729. vscsi->io_queues[HCTX_TYPE_DEFAULT],
  730. vscsi->io_queues[HCTX_TYPE_READ],
  731. vscsi->io_queues[HCTX_TYPE_POLL]);
  732. vqs_info[0].callback = virtscsi_ctrl_done;
  733. vqs_info[0].name = "control";
  734. vqs_info[1].callback = virtscsi_event_done;
  735. vqs_info[1].name = "event";
  736. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs - num_poll_vqs; i++) {
  737. vqs_info[i].callback = virtscsi_req_done;
  738. vqs_info[i].name = "request";
  739. }
  740. for (; i < num_vqs; i++)
  741. vqs_info[i].name = "request_poll";
  742. /* Discover virtqueues and write information to configuration. */
  743. err = virtio_find_vqs(vdev, num_vqs, vqs, vqs_info, &desc);
  744. if (err)
  745. goto out;
  746. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  747. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  748. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
  749. virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
  750. vqs[i]);
  751. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  752. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  753. err = 0;
  754. out:
  755. kfree(vqs_info);
  756. kfree(vqs);
  757. if (err)
  758. virtscsi_remove_vqs(vdev);
  759. return err;
  760. }
  761. static int virtscsi_probe(struct virtio_device *vdev)
  762. {
  763. struct Scsi_Host *shost;
  764. struct virtio_scsi *vscsi;
  765. int err;
  766. u32 sg_elems, num_targets;
  767. u32 cmd_per_lun;
  768. u32 num_queues;
  769. if (!vdev->config->get) {
  770. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  771. __func__);
  772. return -EINVAL;
  773. }
  774. /* We need to know how many queues before we allocate. */
  775. num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
  776. num_queues = min_t(unsigned int, nr_cpu_ids, num_queues);
  777. num_queues = blk_mq_num_possible_queues(num_queues);
  778. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  779. shost = scsi_host_alloc(&virtscsi_host_template,
  780. struct_size(vscsi, req_vqs, num_queues));
  781. if (!shost)
  782. return -ENOMEM;
  783. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  784. shost->sg_tablesize = sg_elems;
  785. shost->nr_maps = 1;
  786. vscsi = shost_priv(shost);
  787. vscsi->vdev = vdev;
  788. vscsi->num_queues = num_queues;
  789. vdev->priv = shost;
  790. err = virtscsi_init(vdev, vscsi);
  791. if (err)
  792. goto virtscsi_init_failed;
  793. if (vscsi->io_queues[HCTX_TYPE_POLL])
  794. shost->nr_maps = HCTX_TYPE_POLL + 1;
  795. shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
  796. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  797. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  798. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  799. /* LUNs > 256 are reported with format 1, so they go in the range
  800. * 16640-32767.
  801. */
  802. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  803. shost->max_id = num_targets;
  804. shost->max_channel = 0;
  805. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  806. shost->nr_hw_queues = num_queues;
  807. #ifdef CONFIG_BLK_DEV_INTEGRITY
  808. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
  809. int host_prot;
  810. host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
  811. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
  812. SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
  813. scsi_host_set_prot(shost, host_prot);
  814. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  815. }
  816. #endif
  817. err = scsi_add_host(shost, &vdev->dev);
  818. if (err)
  819. goto scsi_add_host_failed;
  820. virtio_device_ready(vdev);
  821. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  822. virtscsi_kick_event_all(vscsi);
  823. scsi_scan_host(shost);
  824. return 0;
  825. scsi_add_host_failed:
  826. vdev->config->del_vqs(vdev);
  827. virtscsi_init_failed:
  828. scsi_host_put(shost);
  829. return err;
  830. }
  831. static void virtscsi_remove(struct virtio_device *vdev)
  832. {
  833. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  834. struct virtio_scsi *vscsi = shost_priv(shost);
  835. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  836. virtscsi_cancel_event_work(vscsi);
  837. scsi_remove_host(shost);
  838. virtscsi_remove_vqs(vdev);
  839. scsi_host_put(shost);
  840. }
  841. #ifdef CONFIG_PM_SLEEP
  842. static int virtscsi_freeze(struct virtio_device *vdev)
  843. {
  844. virtscsi_remove_vqs(vdev);
  845. return 0;
  846. }
  847. static int virtscsi_restore(struct virtio_device *vdev)
  848. {
  849. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  850. struct virtio_scsi *vscsi = shost_priv(sh);
  851. int err;
  852. err = virtscsi_init(vdev, vscsi);
  853. if (err)
  854. return err;
  855. virtio_device_ready(vdev);
  856. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  857. virtscsi_kick_event_all(vscsi);
  858. return err;
  859. }
  860. #endif
  861. static struct virtio_device_id id_table[] = {
  862. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  863. { 0 },
  864. };
  865. static unsigned int features[] = {
  866. VIRTIO_SCSI_F_HOTPLUG,
  867. VIRTIO_SCSI_F_CHANGE,
  868. #ifdef CONFIG_BLK_DEV_INTEGRITY
  869. VIRTIO_SCSI_F_T10_PI,
  870. #endif
  871. };
  872. static struct virtio_driver virtio_scsi_driver = {
  873. .feature_table = features,
  874. .feature_table_size = ARRAY_SIZE(features),
  875. .driver.name = KBUILD_MODNAME,
  876. .id_table = id_table,
  877. .probe = virtscsi_probe,
  878. #ifdef CONFIG_PM_SLEEP
  879. .freeze = virtscsi_freeze,
  880. .restore = virtscsi_restore,
  881. #endif
  882. .remove = virtscsi_remove,
  883. };
  884. static int __init virtio_scsi_init(void)
  885. {
  886. int ret = -ENOMEM;
  887. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  888. if (!virtscsi_cmd_cache) {
  889. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  890. goto error;
  891. }
  892. virtscsi_cmd_pool =
  893. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  894. virtscsi_cmd_cache);
  895. if (!virtscsi_cmd_pool) {
  896. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  897. goto error;
  898. }
  899. ret = register_virtio_driver(&virtio_scsi_driver);
  900. if (ret < 0)
  901. goto error;
  902. return 0;
  903. error:
  904. mempool_destroy(virtscsi_cmd_pool);
  905. virtscsi_cmd_pool = NULL;
  906. kmem_cache_destroy(virtscsi_cmd_cache);
  907. virtscsi_cmd_cache = NULL;
  908. return ret;
  909. }
  910. static void __exit virtio_scsi_fini(void)
  911. {
  912. unregister_virtio_driver(&virtio_scsi_driver);
  913. mempool_destroy(virtscsi_cmd_pool);
  914. kmem_cache_destroy(virtscsi_cmd_cache);
  915. }
  916. module_init(virtio_scsi_init);
  917. module_exit(virtio_scsi_fini);
  918. MODULE_DEVICE_TABLE(virtio, id_table);
  919. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  920. MODULE_LICENSE("GPL");