virtio_scsi.c 30 KB

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