virtio_scsi.c 27 KB

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