virtio_scsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 <scsi/scsi_host.h>
  25. #include <scsi/scsi_device.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  28. #define VIRTIO_SCSI_EVENT_LEN 8
  29. #define VIRTIO_SCSI_VQ_BASE 2
  30. /* Command queue element */
  31. struct virtio_scsi_cmd {
  32. struct scsi_cmnd *sc;
  33. struct completion *comp;
  34. union {
  35. struct virtio_scsi_cmd_req cmd;
  36. struct virtio_scsi_ctrl_tmf_req tmf;
  37. struct virtio_scsi_ctrl_an_req an;
  38. } req;
  39. union {
  40. struct virtio_scsi_cmd_resp cmd;
  41. struct virtio_scsi_ctrl_tmf_resp tmf;
  42. struct virtio_scsi_ctrl_an_resp an;
  43. struct virtio_scsi_event evt;
  44. } resp;
  45. } ____cacheline_aligned_in_smp;
  46. struct virtio_scsi_event_node {
  47. struct virtio_scsi *vscsi;
  48. struct virtio_scsi_event event;
  49. struct work_struct work;
  50. };
  51. struct virtio_scsi_vq {
  52. /* Protects vq */
  53. spinlock_t vq_lock;
  54. struct virtqueue *vq;
  55. };
  56. /*
  57. * Per-target queue state.
  58. *
  59. * This struct holds the data needed by the queue steering policy. When a
  60. * target is sent multiple requests, we need to drive them to the same queue so
  61. * that FIFO processing order is kept. However, if a target was idle, we can
  62. * choose a queue arbitrarily. In this case the queue is chosen according to
  63. * the current VCPU, so the driver expects the number of request queues to be
  64. * equal to the number of VCPUs. This makes it easy and fast to select the
  65. * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
  66. * (each virtqueue's affinity is set to the CPU that "owns" the queue).
  67. *
  68. * An interesting effect of this policy is that only writes to req_vq need to
  69. * take the tgt_lock. Read can be done outside the lock because:
  70. *
  71. * - writes of req_vq only occur when atomic_inc_return(&tgt->reqs) returns 1.
  72. * In that case, no other CPU is reading req_vq: even if they were in
  73. * virtscsi_queuecommand_multi, they would be spinning on tgt_lock.
  74. *
  75. * - reads of req_vq only occur when the target is not idle (reqs != 0).
  76. * A CPU that enters virtscsi_queuecommand_multi will not modify req_vq.
  77. *
  78. * Similarly, decrements of reqs are never concurrent with writes of req_vq.
  79. * Thus they can happen outside the tgt_lock, provided of course we make reqs
  80. * an atomic_t.
  81. */
  82. struct virtio_scsi_target_state {
  83. /* This spinlock never held at the same time as vq_lock. */
  84. spinlock_t tgt_lock;
  85. /* Count of outstanding requests. */
  86. atomic_t reqs;
  87. /* Currently active virtqueue for requests sent to this target. */
  88. struct virtio_scsi_vq *req_vq;
  89. };
  90. /* Driver instance state */
  91. struct virtio_scsi {
  92. struct virtio_device *vdev;
  93. /* Get some buffers ready for event vq */
  94. struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
  95. u32 num_queues;
  96. /* If the affinity hint is set for virtqueues */
  97. bool affinity_hint_set;
  98. /* CPU hotplug notifier */
  99. struct notifier_block nb;
  100. struct virtio_scsi_vq ctrl_vq;
  101. struct virtio_scsi_vq event_vq;
  102. struct virtio_scsi_vq req_vqs[];
  103. };
  104. static struct kmem_cache *virtscsi_cmd_cache;
  105. static mempool_t *virtscsi_cmd_pool;
  106. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  107. {
  108. return vdev->priv;
  109. }
  110. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  111. {
  112. if (!resid)
  113. return;
  114. if (!scsi_bidi_cmnd(sc)) {
  115. scsi_set_resid(sc, resid);
  116. return;
  117. }
  118. scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
  119. scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
  120. }
  121. /**
  122. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  123. *
  124. * Called with vq_lock held.
  125. */
  126. static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
  127. {
  128. struct virtio_scsi_cmd *cmd = buf;
  129. struct scsi_cmnd *sc = cmd->sc;
  130. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  131. struct virtio_scsi_target_state *tgt =
  132. scsi_target(sc->device)->hostdata;
  133. dev_dbg(&sc->device->sdev_gendev,
  134. "cmd %p response %u status %#02x sense_len %u\n",
  135. sc, resp->response, resp->status, resp->sense_len);
  136. sc->result = resp->status;
  137. virtscsi_compute_resid(sc, resp->resid);
  138. switch (resp->response) {
  139. case VIRTIO_SCSI_S_OK:
  140. set_host_byte(sc, DID_OK);
  141. break;
  142. case VIRTIO_SCSI_S_OVERRUN:
  143. set_host_byte(sc, DID_ERROR);
  144. break;
  145. case VIRTIO_SCSI_S_ABORTED:
  146. set_host_byte(sc, DID_ABORT);
  147. break;
  148. case VIRTIO_SCSI_S_BAD_TARGET:
  149. set_host_byte(sc, DID_BAD_TARGET);
  150. break;
  151. case VIRTIO_SCSI_S_RESET:
  152. set_host_byte(sc, DID_RESET);
  153. break;
  154. case VIRTIO_SCSI_S_BUSY:
  155. set_host_byte(sc, DID_BUS_BUSY);
  156. break;
  157. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  158. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  159. break;
  160. case VIRTIO_SCSI_S_TARGET_FAILURE:
  161. set_host_byte(sc, DID_TARGET_FAILURE);
  162. break;
  163. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  164. set_host_byte(sc, DID_NEXUS_FAILURE);
  165. break;
  166. default:
  167. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  168. resp->response);
  169. /* fall through */
  170. case VIRTIO_SCSI_S_FAILURE:
  171. set_host_byte(sc, DID_ERROR);
  172. break;
  173. }
  174. WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
  175. if (sc->sense_buffer) {
  176. memcpy(sc->sense_buffer, resp->sense,
  177. min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
  178. if (resp->sense_len)
  179. set_driver_byte(sc, DRIVER_SENSE);
  180. }
  181. mempool_free(cmd, virtscsi_cmd_pool);
  182. sc->scsi_done(sc);
  183. atomic_dec(&tgt->reqs);
  184. }
  185. static void virtscsi_vq_done(struct virtio_scsi *vscsi,
  186. struct virtio_scsi_vq *virtscsi_vq,
  187. void (*fn)(struct virtio_scsi *vscsi, void *buf))
  188. {
  189. void *buf;
  190. unsigned int len;
  191. unsigned long flags;
  192. struct virtqueue *vq = virtscsi_vq->vq;
  193. spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
  194. do {
  195. virtqueue_disable_cb(vq);
  196. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  197. fn(vscsi, buf);
  198. if (unlikely(virtqueue_is_broken(vq)))
  199. break;
  200. } while (!virtqueue_enable_cb(vq));
  201. spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
  202. }
  203. static void virtscsi_req_done(struct virtqueue *vq)
  204. {
  205. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  206. struct virtio_scsi *vscsi = shost_priv(sh);
  207. int index = vq->index - VIRTIO_SCSI_VQ_BASE;
  208. struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
  209. /*
  210. * Read req_vq before decrementing the reqs field in
  211. * virtscsi_complete_cmd.
  212. *
  213. * With barriers:
  214. *
  215. * CPU #0 virtscsi_queuecommand_multi (CPU #1)
  216. * ------------------------------------------------------------
  217. * lock vq_lock
  218. * read req_vq
  219. * read reqs (reqs = 1)
  220. * write reqs (reqs = 0)
  221. * increment reqs (reqs = 1)
  222. * write req_vq
  223. *
  224. * Possible reordering without barriers:
  225. *
  226. * CPU #0 virtscsi_queuecommand_multi (CPU #1)
  227. * ------------------------------------------------------------
  228. * lock vq_lock
  229. * read reqs (reqs = 1)
  230. * write reqs (reqs = 0)
  231. * increment reqs (reqs = 1)
  232. * write req_vq
  233. * read (wrong) req_vq
  234. *
  235. * We do not need a full smp_rmb, because req_vq is required to get
  236. * to tgt->reqs: tgt is &vscsi->tgt[sc->device->id], where sc is stored
  237. * in the virtqueue as the user token.
  238. */
  239. smp_read_barrier_depends();
  240. virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
  241. };
  242. static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
  243. {
  244. struct virtio_scsi_cmd *cmd = buf;
  245. if (cmd->comp)
  246. complete_all(cmd->comp);
  247. else
  248. mempool_free(cmd, virtscsi_cmd_pool);
  249. }
  250. static void virtscsi_ctrl_done(struct virtqueue *vq)
  251. {
  252. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  253. struct virtio_scsi *vscsi = shost_priv(sh);
  254. virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
  255. };
  256. static int virtscsi_kick_event(struct virtio_scsi *vscsi,
  257. struct virtio_scsi_event_node *event_node)
  258. {
  259. int err;
  260. struct scatterlist sg;
  261. unsigned long flags;
  262. sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
  263. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  264. err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
  265. GFP_ATOMIC);
  266. if (!err)
  267. virtqueue_kick(vscsi->event_vq.vq);
  268. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  269. return err;
  270. }
  271. static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
  272. {
  273. int i;
  274. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
  275. vscsi->event_list[i].vscsi = vscsi;
  276. virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
  277. }
  278. return 0;
  279. }
  280. static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
  281. {
  282. int i;
  283. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
  284. cancel_work_sync(&vscsi->event_list[i].work);
  285. }
  286. static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
  287. struct virtio_scsi_event *event)
  288. {
  289. struct scsi_device *sdev;
  290. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  291. unsigned int target = event->lun[1];
  292. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  293. switch (event->reason) {
  294. case VIRTIO_SCSI_EVT_RESET_RESCAN:
  295. scsi_add_device(shost, 0, target, lun);
  296. break;
  297. case VIRTIO_SCSI_EVT_RESET_REMOVED:
  298. sdev = scsi_device_lookup(shost, 0, target, lun);
  299. if (sdev) {
  300. scsi_remove_device(sdev);
  301. scsi_device_put(sdev);
  302. } else {
  303. pr_err("SCSI device %d 0 %d %d not found\n",
  304. shost->host_no, target, lun);
  305. }
  306. break;
  307. default:
  308. pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
  309. }
  310. }
  311. static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
  312. struct virtio_scsi_event *event)
  313. {
  314. struct scsi_device *sdev;
  315. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  316. unsigned int target = event->lun[1];
  317. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  318. u8 asc = event->reason & 255;
  319. u8 ascq = event->reason >> 8;
  320. sdev = scsi_device_lookup(shost, 0, target, lun);
  321. if (!sdev) {
  322. pr_err("SCSI device %d 0 %d %d not found\n",
  323. shost->host_no, target, lun);
  324. return;
  325. }
  326. /* Handle "Parameters changed", "Mode parameters changed", and
  327. "Capacity data has changed". */
  328. if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
  329. scsi_rescan_device(&sdev->sdev_gendev);
  330. scsi_device_put(sdev);
  331. }
  332. static void virtscsi_handle_event(struct work_struct *work)
  333. {
  334. struct virtio_scsi_event_node *event_node =
  335. container_of(work, struct virtio_scsi_event_node, work);
  336. struct virtio_scsi *vscsi = event_node->vscsi;
  337. struct virtio_scsi_event *event = &event_node->event;
  338. if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
  339. event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
  340. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  341. }
  342. switch (event->event) {
  343. case VIRTIO_SCSI_T_NO_EVENT:
  344. break;
  345. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  346. virtscsi_handle_transport_reset(vscsi, event);
  347. break;
  348. case VIRTIO_SCSI_T_PARAM_CHANGE:
  349. virtscsi_handle_param_change(vscsi, event);
  350. break;
  351. default:
  352. pr_err("Unsupport virtio scsi event %x\n", event->event);
  353. }
  354. virtscsi_kick_event(vscsi, event_node);
  355. }
  356. static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
  357. {
  358. struct virtio_scsi_event_node *event_node = buf;
  359. INIT_WORK(&event_node->work, virtscsi_handle_event);
  360. schedule_work(&event_node->work);
  361. }
  362. static void virtscsi_event_done(struct virtqueue *vq)
  363. {
  364. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  365. struct virtio_scsi *vscsi = shost_priv(sh);
  366. virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
  367. };
  368. /**
  369. * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
  370. * @vq : the struct virtqueue we're talking about
  371. * @cmd : command structure
  372. * @req_size : size of the request buffer
  373. * @resp_size : size of the response buffer
  374. * @gfp : flags to use for memory allocations
  375. */
  376. static int virtscsi_add_cmd(struct virtqueue *vq,
  377. struct virtio_scsi_cmd *cmd,
  378. size_t req_size, size_t resp_size, gfp_t gfp)
  379. {
  380. struct scsi_cmnd *sc = cmd->sc;
  381. struct scatterlist *sgs[4], req, resp;
  382. struct sg_table *out, *in;
  383. unsigned out_num = 0, in_num = 0;
  384. out = in = NULL;
  385. if (sc && sc->sc_data_direction != DMA_NONE) {
  386. if (sc->sc_data_direction != DMA_FROM_DEVICE)
  387. out = &scsi_out(sc)->table;
  388. if (sc->sc_data_direction != DMA_TO_DEVICE)
  389. in = &scsi_in(sc)->table;
  390. }
  391. /* Request header. */
  392. sg_init_one(&req, &cmd->req, req_size);
  393. sgs[out_num++] = &req;
  394. /* Data-out buffer. */
  395. if (out)
  396. sgs[out_num++] = out->sgl;
  397. /* Response header. */
  398. sg_init_one(&resp, &cmd->resp, resp_size);
  399. sgs[out_num + in_num++] = &resp;
  400. /* Data-in buffer */
  401. if (in)
  402. sgs[out_num + in_num++] = in->sgl;
  403. return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
  404. }
  405. static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
  406. struct virtio_scsi_cmd *cmd,
  407. size_t req_size, size_t resp_size, gfp_t gfp)
  408. {
  409. unsigned long flags;
  410. int err;
  411. bool needs_kick = false;
  412. spin_lock_irqsave(&vq->vq_lock, flags);
  413. err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
  414. if (!err)
  415. needs_kick = virtqueue_kick_prepare(vq->vq);
  416. spin_unlock_irqrestore(&vq->vq_lock, flags);
  417. if (needs_kick)
  418. virtqueue_notify(vq->vq);
  419. return err;
  420. }
  421. static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
  422. struct virtio_scsi_vq *req_vq,
  423. struct scsi_cmnd *sc)
  424. {
  425. struct virtio_scsi_cmd *cmd;
  426. int ret;
  427. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  428. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  429. /* TODO: check feature bit and fail if unsupported? */
  430. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  431. dev_dbg(&sc->device->sdev_gendev,
  432. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  433. ret = SCSI_MLQUEUE_HOST_BUSY;
  434. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
  435. if (!cmd)
  436. goto out;
  437. memset(cmd, 0, sizeof(*cmd));
  438. cmd->sc = sc;
  439. cmd->req.cmd = (struct virtio_scsi_cmd_req){
  440. .lun[0] = 1,
  441. .lun[1] = sc->device->id,
  442. .lun[2] = (sc->device->lun >> 8) | 0x40,
  443. .lun[3] = sc->device->lun & 0xff,
  444. .tag = (unsigned long)sc,
  445. .task_attr = VIRTIO_SCSI_S_SIMPLE,
  446. .prio = 0,
  447. .crn = 0,
  448. };
  449. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  450. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  451. if (virtscsi_kick_cmd(req_vq, cmd,
  452. sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
  453. GFP_ATOMIC) == 0)
  454. ret = 0;
  455. else
  456. mempool_free(cmd, virtscsi_cmd_pool);
  457. out:
  458. return ret;
  459. }
  460. static int virtscsi_queuecommand_single(struct Scsi_Host *sh,
  461. struct scsi_cmnd *sc)
  462. {
  463. struct virtio_scsi *vscsi = shost_priv(sh);
  464. struct virtio_scsi_target_state *tgt =
  465. scsi_target(sc->device)->hostdata;
  466. atomic_inc(&tgt->reqs);
  467. return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc);
  468. }
  469. static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi,
  470. struct virtio_scsi_target_state *tgt)
  471. {
  472. struct virtio_scsi_vq *vq;
  473. unsigned long flags;
  474. u32 queue_num;
  475. spin_lock_irqsave(&tgt->tgt_lock, flags);
  476. /*
  477. * The memory barrier after atomic_inc_return matches
  478. * the smp_read_barrier_depends() in virtscsi_req_done.
  479. */
  480. if (atomic_inc_return(&tgt->reqs) > 1)
  481. vq = ACCESS_ONCE(tgt->req_vq);
  482. else {
  483. queue_num = smp_processor_id();
  484. while (unlikely(queue_num >= vscsi->num_queues))
  485. queue_num -= vscsi->num_queues;
  486. tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
  487. }
  488. spin_unlock_irqrestore(&tgt->tgt_lock, flags);
  489. return vq;
  490. }
  491. static int virtscsi_queuecommand_multi(struct Scsi_Host *sh,
  492. struct scsi_cmnd *sc)
  493. {
  494. struct virtio_scsi *vscsi = shost_priv(sh);
  495. struct virtio_scsi_target_state *tgt =
  496. scsi_target(sc->device)->hostdata;
  497. struct virtio_scsi_vq *req_vq = virtscsi_pick_vq(vscsi, tgt);
  498. return virtscsi_queuecommand(vscsi, req_vq, sc);
  499. }
  500. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  501. {
  502. DECLARE_COMPLETION_ONSTACK(comp);
  503. int ret = FAILED;
  504. cmd->comp = &comp;
  505. if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
  506. sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
  507. GFP_NOIO) < 0)
  508. goto out;
  509. wait_for_completion(&comp);
  510. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  511. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  512. ret = SUCCESS;
  513. out:
  514. mempool_free(cmd, virtscsi_cmd_pool);
  515. return ret;
  516. }
  517. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  518. {
  519. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  520. struct virtio_scsi_cmd *cmd;
  521. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  522. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  523. if (!cmd)
  524. return FAILED;
  525. memset(cmd, 0, sizeof(*cmd));
  526. cmd->sc = sc;
  527. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  528. .type = VIRTIO_SCSI_T_TMF,
  529. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  530. .lun[0] = 1,
  531. .lun[1] = sc->device->id,
  532. .lun[2] = (sc->device->lun >> 8) | 0x40,
  533. .lun[3] = sc->device->lun & 0xff,
  534. };
  535. return virtscsi_tmf(vscsi, cmd);
  536. }
  537. static int virtscsi_abort(struct scsi_cmnd *sc)
  538. {
  539. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  540. struct virtio_scsi_cmd *cmd;
  541. scmd_printk(KERN_INFO, sc, "abort\n");
  542. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  543. if (!cmd)
  544. return FAILED;
  545. memset(cmd, 0, sizeof(*cmd));
  546. cmd->sc = sc;
  547. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  548. .type = VIRTIO_SCSI_T_TMF,
  549. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  550. .lun[0] = 1,
  551. .lun[1] = sc->device->id,
  552. .lun[2] = (sc->device->lun >> 8) | 0x40,
  553. .lun[3] = sc->device->lun & 0xff,
  554. .tag = (unsigned long)sc,
  555. };
  556. return virtscsi_tmf(vscsi, cmd);
  557. }
  558. static int virtscsi_target_alloc(struct scsi_target *starget)
  559. {
  560. struct virtio_scsi_target_state *tgt =
  561. kmalloc(sizeof(*tgt), GFP_KERNEL);
  562. if (!tgt)
  563. return -ENOMEM;
  564. spin_lock_init(&tgt->tgt_lock);
  565. atomic_set(&tgt->reqs, 0);
  566. tgt->req_vq = NULL;
  567. starget->hostdata = tgt;
  568. return 0;
  569. }
  570. static void virtscsi_target_destroy(struct scsi_target *starget)
  571. {
  572. struct virtio_scsi_target_state *tgt = starget->hostdata;
  573. kfree(tgt);
  574. }
  575. static struct scsi_host_template virtscsi_host_template_single = {
  576. .module = THIS_MODULE,
  577. .name = "Virtio SCSI HBA",
  578. .proc_name = "virtio_scsi",
  579. .this_id = -1,
  580. .queuecommand = virtscsi_queuecommand_single,
  581. .eh_abort_handler = virtscsi_abort,
  582. .eh_device_reset_handler = virtscsi_device_reset,
  583. .can_queue = 1024,
  584. .dma_boundary = UINT_MAX,
  585. .use_clustering = ENABLE_CLUSTERING,
  586. .target_alloc = virtscsi_target_alloc,
  587. .target_destroy = virtscsi_target_destroy,
  588. };
  589. static struct scsi_host_template virtscsi_host_template_multi = {
  590. .module = THIS_MODULE,
  591. .name = "Virtio SCSI HBA",
  592. .proc_name = "virtio_scsi",
  593. .this_id = -1,
  594. .queuecommand = virtscsi_queuecommand_multi,
  595. .eh_abort_handler = virtscsi_abort,
  596. .eh_device_reset_handler = virtscsi_device_reset,
  597. .can_queue = 1024,
  598. .dma_boundary = UINT_MAX,
  599. .use_clustering = ENABLE_CLUSTERING,
  600. .target_alloc = virtscsi_target_alloc,
  601. .target_destroy = virtscsi_target_destroy,
  602. };
  603. #define virtscsi_config_get(vdev, fld) \
  604. ({ \
  605. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  606. virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
  607. __val; \
  608. })
  609. #define virtscsi_config_set(vdev, fld, val) \
  610. do { \
  611. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  612. virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
  613. } while(0)
  614. static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  615. {
  616. int i;
  617. int cpu;
  618. /* In multiqueue mode, when the number of cpu is equal
  619. * to the number of request queues, we let the qeueues
  620. * to be private to one cpu by setting the affinity hint
  621. * to eliminate the contention.
  622. */
  623. if ((vscsi->num_queues == 1 ||
  624. vscsi->num_queues != num_online_cpus()) && affinity) {
  625. if (vscsi->affinity_hint_set)
  626. affinity = false;
  627. else
  628. return;
  629. }
  630. if (affinity) {
  631. i = 0;
  632. for_each_online_cpu(cpu) {
  633. virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu);
  634. i++;
  635. }
  636. vscsi->affinity_hint_set = true;
  637. } else {
  638. for (i = 0; i < vscsi->num_queues; i++)
  639. virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
  640. vscsi->affinity_hint_set = false;
  641. }
  642. }
  643. static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  644. {
  645. get_online_cpus();
  646. __virtscsi_set_affinity(vscsi, affinity);
  647. put_online_cpus();
  648. }
  649. static int virtscsi_cpu_callback(struct notifier_block *nfb,
  650. unsigned long action, void *hcpu)
  651. {
  652. struct virtio_scsi *vscsi = container_of(nfb, struct virtio_scsi, nb);
  653. switch(action) {
  654. case CPU_ONLINE:
  655. case CPU_ONLINE_FROZEN:
  656. case CPU_DEAD:
  657. case CPU_DEAD_FROZEN:
  658. __virtscsi_set_affinity(vscsi, true);
  659. break;
  660. default:
  661. break;
  662. }
  663. return NOTIFY_OK;
  664. }
  665. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  666. struct virtqueue *vq)
  667. {
  668. spin_lock_init(&virtscsi_vq->vq_lock);
  669. virtscsi_vq->vq = vq;
  670. }
  671. static void virtscsi_scan(struct virtio_device *vdev)
  672. {
  673. struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
  674. scsi_scan_host(shost);
  675. }
  676. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  677. {
  678. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  679. struct virtio_scsi *vscsi = shost_priv(sh);
  680. virtscsi_set_affinity(vscsi, false);
  681. /* Stop all the virtqueues. */
  682. vdev->config->reset(vdev);
  683. vdev->config->del_vqs(vdev);
  684. }
  685. static int virtscsi_init(struct virtio_device *vdev,
  686. struct virtio_scsi *vscsi)
  687. {
  688. int err;
  689. u32 i;
  690. u32 num_vqs;
  691. vq_callback_t **callbacks;
  692. const char **names;
  693. struct virtqueue **vqs;
  694. num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
  695. vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL);
  696. callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL);
  697. names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL);
  698. if (!callbacks || !vqs || !names) {
  699. err = -ENOMEM;
  700. goto out;
  701. }
  702. callbacks[0] = virtscsi_ctrl_done;
  703. callbacks[1] = virtscsi_event_done;
  704. names[0] = "control";
  705. names[1] = "event";
  706. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
  707. callbacks[i] = virtscsi_req_done;
  708. names[i] = "request";
  709. }
  710. /* Discover virtqueues and write information to configuration. */
  711. err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
  712. if (err)
  713. goto out;
  714. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  715. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  716. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
  717. virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
  718. vqs[i]);
  719. virtscsi_set_affinity(vscsi, true);
  720. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  721. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  722. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  723. virtscsi_kick_event_all(vscsi);
  724. err = 0;
  725. out:
  726. kfree(names);
  727. kfree(callbacks);
  728. kfree(vqs);
  729. if (err)
  730. virtscsi_remove_vqs(vdev);
  731. return err;
  732. }
  733. static int virtscsi_probe(struct virtio_device *vdev)
  734. {
  735. struct Scsi_Host *shost;
  736. struct virtio_scsi *vscsi;
  737. int err;
  738. u32 sg_elems, num_targets;
  739. u32 cmd_per_lun;
  740. u32 num_queues;
  741. struct scsi_host_template *hostt;
  742. /* We need to know how many queues before we allocate. */
  743. num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
  744. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  745. if (num_queues == 1)
  746. hostt = &virtscsi_host_template_single;
  747. else
  748. hostt = &virtscsi_host_template_multi;
  749. shost = scsi_host_alloc(hostt,
  750. sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
  751. if (!shost)
  752. return -ENOMEM;
  753. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  754. shost->sg_tablesize = sg_elems;
  755. vscsi = shost_priv(shost);
  756. vscsi->vdev = vdev;
  757. vscsi->num_queues = num_queues;
  758. vdev->priv = shost;
  759. err = virtscsi_init(vdev, vscsi);
  760. if (err)
  761. goto virtscsi_init_failed;
  762. vscsi->nb.notifier_call = &virtscsi_cpu_callback;
  763. err = register_hotcpu_notifier(&vscsi->nb);
  764. if (err) {
  765. pr_err("registering cpu notifier failed\n");
  766. goto scsi_add_host_failed;
  767. }
  768. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  769. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  770. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  771. /* LUNs > 256 are reported with format 1, so they go in the range
  772. * 16640-32767.
  773. */
  774. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  775. shost->max_id = num_targets;
  776. shost->max_channel = 0;
  777. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  778. err = scsi_add_host(shost, &vdev->dev);
  779. if (err)
  780. goto scsi_add_host_failed;
  781. /*
  782. * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
  783. * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
  784. */
  785. return 0;
  786. scsi_add_host_failed:
  787. vdev->config->del_vqs(vdev);
  788. virtscsi_init_failed:
  789. scsi_host_put(shost);
  790. return err;
  791. }
  792. static void virtscsi_remove(struct virtio_device *vdev)
  793. {
  794. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  795. struct virtio_scsi *vscsi = shost_priv(shost);
  796. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  797. virtscsi_cancel_event_work(vscsi);
  798. scsi_remove_host(shost);
  799. unregister_hotcpu_notifier(&vscsi->nb);
  800. virtscsi_remove_vqs(vdev);
  801. scsi_host_put(shost);
  802. }
  803. #ifdef CONFIG_PM_SLEEP
  804. static int virtscsi_freeze(struct virtio_device *vdev)
  805. {
  806. virtscsi_remove_vqs(vdev);
  807. return 0;
  808. }
  809. static int virtscsi_restore(struct virtio_device *vdev)
  810. {
  811. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  812. struct virtio_scsi *vscsi = shost_priv(sh);
  813. return virtscsi_init(vdev, vscsi);
  814. }
  815. #endif
  816. static struct virtio_device_id id_table[] = {
  817. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  818. { 0 },
  819. };
  820. static unsigned int features[] = {
  821. VIRTIO_SCSI_F_HOTPLUG,
  822. VIRTIO_SCSI_F_CHANGE,
  823. };
  824. static struct virtio_driver virtio_scsi_driver = {
  825. .feature_table = features,
  826. .feature_table_size = ARRAY_SIZE(features),
  827. .driver.name = KBUILD_MODNAME,
  828. .driver.owner = THIS_MODULE,
  829. .id_table = id_table,
  830. .probe = virtscsi_probe,
  831. .scan = virtscsi_scan,
  832. #ifdef CONFIG_PM_SLEEP
  833. .freeze = virtscsi_freeze,
  834. .restore = virtscsi_restore,
  835. #endif
  836. .remove = virtscsi_remove,
  837. };
  838. static int __init init(void)
  839. {
  840. int ret = -ENOMEM;
  841. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  842. if (!virtscsi_cmd_cache) {
  843. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  844. goto error;
  845. }
  846. virtscsi_cmd_pool =
  847. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  848. virtscsi_cmd_cache);
  849. if (!virtscsi_cmd_pool) {
  850. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  851. goto error;
  852. }
  853. ret = register_virtio_driver(&virtio_scsi_driver);
  854. if (ret < 0)
  855. goto error;
  856. return 0;
  857. error:
  858. if (virtscsi_cmd_pool) {
  859. mempool_destroy(virtscsi_cmd_pool);
  860. virtscsi_cmd_pool = NULL;
  861. }
  862. if (virtscsi_cmd_cache) {
  863. kmem_cache_destroy(virtscsi_cmd_cache);
  864. virtscsi_cmd_cache = NULL;
  865. }
  866. return ret;
  867. }
  868. static void __exit fini(void)
  869. {
  870. unregister_virtio_driver(&virtio_scsi_driver);
  871. mempool_destroy(virtscsi_cmd_pool);
  872. kmem_cache_destroy(virtscsi_cmd_cache);
  873. }
  874. module_init(init);
  875. module_exit(fini);
  876. MODULE_DEVICE_TABLE(virtio, id_table);
  877. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  878. MODULE_LICENSE("GPL");