virtio_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/mempool.h>
  18. #include <linux/virtio.h>
  19. #include <linux/virtio_ids.h>
  20. #include <linux/virtio_config.h>
  21. #include <linux/virtio_scsi.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi_device.h>
  24. #include <scsi/scsi_cmnd.h>
  25. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  26. /* Command queue element */
  27. struct virtio_scsi_cmd {
  28. struct scsi_cmnd *sc;
  29. struct completion *comp;
  30. union {
  31. struct virtio_scsi_cmd_req cmd;
  32. struct virtio_scsi_ctrl_tmf_req tmf;
  33. struct virtio_scsi_ctrl_an_req an;
  34. } req;
  35. union {
  36. struct virtio_scsi_cmd_resp cmd;
  37. struct virtio_scsi_ctrl_tmf_resp tmf;
  38. struct virtio_scsi_ctrl_an_resp an;
  39. struct virtio_scsi_event evt;
  40. } resp;
  41. } ____cacheline_aligned_in_smp;
  42. /* Driver instance state */
  43. struct virtio_scsi {
  44. /* Protects ctrl_vq, req_vq and sg[] */
  45. spinlock_t vq_lock;
  46. struct virtio_device *vdev;
  47. struct virtqueue *ctrl_vq;
  48. struct virtqueue *event_vq;
  49. struct virtqueue *req_vq;
  50. /* For sglist construction when adding commands to the virtqueue. */
  51. struct scatterlist sg[];
  52. };
  53. static struct kmem_cache *virtscsi_cmd_cache;
  54. static mempool_t *virtscsi_cmd_pool;
  55. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  56. {
  57. return vdev->priv;
  58. }
  59. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  60. {
  61. if (!resid)
  62. return;
  63. if (!scsi_bidi_cmnd(sc)) {
  64. scsi_set_resid(sc, resid);
  65. return;
  66. }
  67. scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
  68. scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
  69. }
  70. /**
  71. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  72. *
  73. * Called with vq_lock held.
  74. */
  75. static void virtscsi_complete_cmd(void *buf)
  76. {
  77. struct virtio_scsi_cmd *cmd = buf;
  78. struct scsi_cmnd *sc = cmd->sc;
  79. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  80. dev_dbg(&sc->device->sdev_gendev,
  81. "cmd %p response %u status %#02x sense_len %u\n",
  82. sc, resp->response, resp->status, resp->sense_len);
  83. sc->result = resp->status;
  84. virtscsi_compute_resid(sc, resp->resid);
  85. switch (resp->response) {
  86. case VIRTIO_SCSI_S_OK:
  87. set_host_byte(sc, DID_OK);
  88. break;
  89. case VIRTIO_SCSI_S_OVERRUN:
  90. set_host_byte(sc, DID_ERROR);
  91. break;
  92. case VIRTIO_SCSI_S_ABORTED:
  93. set_host_byte(sc, DID_ABORT);
  94. break;
  95. case VIRTIO_SCSI_S_BAD_TARGET:
  96. set_host_byte(sc, DID_BAD_TARGET);
  97. break;
  98. case VIRTIO_SCSI_S_RESET:
  99. set_host_byte(sc, DID_RESET);
  100. break;
  101. case VIRTIO_SCSI_S_BUSY:
  102. set_host_byte(sc, DID_BUS_BUSY);
  103. break;
  104. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  105. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  106. break;
  107. case VIRTIO_SCSI_S_TARGET_FAILURE:
  108. set_host_byte(sc, DID_TARGET_FAILURE);
  109. break;
  110. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  111. set_host_byte(sc, DID_NEXUS_FAILURE);
  112. break;
  113. default:
  114. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  115. resp->response);
  116. /* fall through */
  117. case VIRTIO_SCSI_S_FAILURE:
  118. set_host_byte(sc, DID_ERROR);
  119. break;
  120. }
  121. WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
  122. if (sc->sense_buffer) {
  123. memcpy(sc->sense_buffer, resp->sense,
  124. min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
  125. if (resp->sense_len)
  126. set_driver_byte(sc, DRIVER_SENSE);
  127. }
  128. mempool_free(cmd, virtscsi_cmd_pool);
  129. sc->scsi_done(sc);
  130. }
  131. static void virtscsi_vq_done(struct virtqueue *vq, void (*fn)(void *buf))
  132. {
  133. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  134. struct virtio_scsi *vscsi = shost_priv(sh);
  135. void *buf;
  136. unsigned long flags;
  137. unsigned int len;
  138. spin_lock_irqsave(&vscsi->vq_lock, flags);
  139. do {
  140. virtqueue_disable_cb(vq);
  141. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  142. fn(buf);
  143. } while (!virtqueue_enable_cb(vq));
  144. spin_unlock_irqrestore(&vscsi->vq_lock, flags);
  145. }
  146. static void virtscsi_req_done(struct virtqueue *vq)
  147. {
  148. virtscsi_vq_done(vq, virtscsi_complete_cmd);
  149. };
  150. static void virtscsi_complete_free(void *buf)
  151. {
  152. struct virtio_scsi_cmd *cmd = buf;
  153. if (cmd->comp)
  154. complete_all(cmd->comp);
  155. else
  156. mempool_free(cmd, virtscsi_cmd_pool);
  157. }
  158. static void virtscsi_ctrl_done(struct virtqueue *vq)
  159. {
  160. virtscsi_vq_done(vq, virtscsi_complete_free);
  161. };
  162. static void virtscsi_event_done(struct virtqueue *vq)
  163. {
  164. virtscsi_vq_done(vq, virtscsi_complete_free);
  165. };
  166. static void virtscsi_map_sgl(struct scatterlist *sg, unsigned int *p_idx,
  167. struct scsi_data_buffer *sdb)
  168. {
  169. struct sg_table *table = &sdb->table;
  170. struct scatterlist *sg_elem;
  171. unsigned int idx = *p_idx;
  172. int i;
  173. for_each_sg(table->sgl, sg_elem, table->nents, i)
  174. sg[idx++] = *sg_elem;
  175. *p_idx = idx;
  176. }
  177. /**
  178. * virtscsi_map_cmd - map a scsi_cmd to a virtqueue scatterlist
  179. * @vscsi : virtio_scsi state
  180. * @cmd : command structure
  181. * @out_num : number of read-only elements
  182. * @in_num : number of write-only elements
  183. * @req_size : size of the request buffer
  184. * @resp_size : size of the response buffer
  185. *
  186. * Called with vq_lock held.
  187. */
  188. static void virtscsi_map_cmd(struct virtio_scsi *vscsi,
  189. struct virtio_scsi_cmd *cmd,
  190. unsigned *out_num, unsigned *in_num,
  191. size_t req_size, size_t resp_size)
  192. {
  193. struct scsi_cmnd *sc = cmd->sc;
  194. struct scatterlist *sg = vscsi->sg;
  195. unsigned int idx = 0;
  196. if (sc) {
  197. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  198. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  199. /* TODO: check feature bit and fail if unsupported? */
  200. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  201. }
  202. /* Request header. */
  203. sg_set_buf(&sg[idx++], &cmd->req, req_size);
  204. /* Data-out buffer. */
  205. if (sc && sc->sc_data_direction != DMA_FROM_DEVICE)
  206. virtscsi_map_sgl(sg, &idx, scsi_out(sc));
  207. *out_num = idx;
  208. /* Response header. */
  209. sg_set_buf(&sg[idx++], &cmd->resp, resp_size);
  210. /* Data-in buffer */
  211. if (sc && sc->sc_data_direction != DMA_TO_DEVICE)
  212. virtscsi_map_sgl(sg, &idx, scsi_in(sc));
  213. *in_num = idx - *out_num;
  214. }
  215. static int virtscsi_kick_cmd(struct virtio_scsi *vscsi, struct virtqueue *vq,
  216. struct virtio_scsi_cmd *cmd,
  217. size_t req_size, size_t resp_size, gfp_t gfp)
  218. {
  219. unsigned int out_num, in_num;
  220. unsigned long flags;
  221. int ret;
  222. spin_lock_irqsave(&vscsi->vq_lock, flags);
  223. virtscsi_map_cmd(vscsi, cmd, &out_num, &in_num, req_size, resp_size);
  224. ret = virtqueue_add_buf(vq, vscsi->sg, out_num, in_num, cmd, gfp);
  225. if (ret >= 0)
  226. virtqueue_kick(vq);
  227. spin_unlock_irqrestore(&vscsi->vq_lock, flags);
  228. return ret;
  229. }
  230. static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
  231. {
  232. struct virtio_scsi *vscsi = shost_priv(sh);
  233. struct virtio_scsi_cmd *cmd;
  234. int ret;
  235. dev_dbg(&sc->device->sdev_gendev,
  236. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  237. ret = SCSI_MLQUEUE_HOST_BUSY;
  238. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
  239. if (!cmd)
  240. goto out;
  241. memset(cmd, 0, sizeof(*cmd));
  242. cmd->sc = sc;
  243. cmd->req.cmd = (struct virtio_scsi_cmd_req){
  244. .lun[0] = 1,
  245. .lun[1] = sc->device->id,
  246. .lun[2] = (sc->device->lun >> 8) | 0x40,
  247. .lun[3] = sc->device->lun & 0xff,
  248. .tag = (unsigned long)sc,
  249. .task_attr = VIRTIO_SCSI_S_SIMPLE,
  250. .prio = 0,
  251. .crn = 0,
  252. };
  253. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  254. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  255. if (virtscsi_kick_cmd(vscsi, vscsi->req_vq, cmd,
  256. sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
  257. GFP_ATOMIC) >= 0)
  258. ret = 0;
  259. out:
  260. return ret;
  261. }
  262. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  263. {
  264. DECLARE_COMPLETION_ONSTACK(comp);
  265. int ret = FAILED;
  266. cmd->comp = &comp;
  267. if (virtscsi_kick_cmd(vscsi, vscsi->ctrl_vq, cmd,
  268. sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
  269. GFP_NOIO) < 0)
  270. goto out;
  271. wait_for_completion(&comp);
  272. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  273. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  274. ret = SUCCESS;
  275. out:
  276. mempool_free(cmd, virtscsi_cmd_pool);
  277. return ret;
  278. }
  279. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  280. {
  281. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  282. struct virtio_scsi_cmd *cmd;
  283. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  284. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  285. if (!cmd)
  286. return FAILED;
  287. memset(cmd, 0, sizeof(*cmd));
  288. cmd->sc = sc;
  289. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  290. .type = VIRTIO_SCSI_T_TMF,
  291. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  292. .lun[0] = 1,
  293. .lun[1] = sc->device->id,
  294. .lun[2] = (sc->device->lun >> 8) | 0x40,
  295. .lun[3] = sc->device->lun & 0xff,
  296. };
  297. return virtscsi_tmf(vscsi, cmd);
  298. }
  299. static int virtscsi_abort(struct scsi_cmnd *sc)
  300. {
  301. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  302. struct virtio_scsi_cmd *cmd;
  303. scmd_printk(KERN_INFO, sc, "abort\n");
  304. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  305. if (!cmd)
  306. return FAILED;
  307. memset(cmd, 0, sizeof(*cmd));
  308. cmd->sc = sc;
  309. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  310. .type = VIRTIO_SCSI_T_TMF,
  311. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  312. .lun[0] = 1,
  313. .lun[1] = sc->device->id,
  314. .lun[2] = (sc->device->lun >> 8) | 0x40,
  315. .lun[3] = sc->device->lun & 0xff,
  316. .tag = (unsigned long)sc,
  317. };
  318. return virtscsi_tmf(vscsi, cmd);
  319. }
  320. static struct scsi_host_template virtscsi_host_template = {
  321. .module = THIS_MODULE,
  322. .name = "Virtio SCSI HBA",
  323. .proc_name = "virtio_scsi",
  324. .queuecommand = virtscsi_queuecommand,
  325. .this_id = -1,
  326. .eh_abort_handler = virtscsi_abort,
  327. .eh_device_reset_handler = virtscsi_device_reset,
  328. .can_queue = 1024,
  329. .dma_boundary = UINT_MAX,
  330. .use_clustering = ENABLE_CLUSTERING,
  331. };
  332. #define virtscsi_config_get(vdev, fld) \
  333. ({ \
  334. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  335. vdev->config->get(vdev, \
  336. offsetof(struct virtio_scsi_config, fld), \
  337. &__val, sizeof(__val)); \
  338. __val; \
  339. })
  340. #define virtscsi_config_set(vdev, fld, val) \
  341. (void)({ \
  342. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  343. vdev->config->set(vdev, \
  344. offsetof(struct virtio_scsi_config, fld), \
  345. &__val, sizeof(__val)); \
  346. })
  347. static int virtscsi_init(struct virtio_device *vdev,
  348. struct virtio_scsi *vscsi)
  349. {
  350. int err;
  351. struct virtqueue *vqs[3];
  352. vq_callback_t *callbacks[] = {
  353. virtscsi_ctrl_done,
  354. virtscsi_event_done,
  355. virtscsi_req_done
  356. };
  357. const char *names[] = {
  358. "control",
  359. "event",
  360. "request"
  361. };
  362. /* Discover virtqueues and write information to configuration. */
  363. err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
  364. if (err)
  365. return err;
  366. vscsi->ctrl_vq = vqs[0];
  367. vscsi->event_vq = vqs[1];
  368. vscsi->req_vq = vqs[2];
  369. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  370. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  371. return 0;
  372. }
  373. static int __devinit virtscsi_probe(struct virtio_device *vdev)
  374. {
  375. struct Scsi_Host *shost;
  376. struct virtio_scsi *vscsi;
  377. int err;
  378. u32 sg_elems;
  379. u32 cmd_per_lun;
  380. /* We need to know how many segments before we allocate.
  381. * We need an extra sg elements at head and tail.
  382. */
  383. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  384. /* Allocate memory and link the structs together. */
  385. shost = scsi_host_alloc(&virtscsi_host_template,
  386. sizeof(*vscsi) + sizeof(vscsi->sg[0]) * (sg_elems + 2));
  387. if (!shost)
  388. return -ENOMEM;
  389. shost->sg_tablesize = sg_elems;
  390. vscsi = shost_priv(shost);
  391. vscsi->vdev = vdev;
  392. vdev->priv = shost;
  393. /* Random initializations. */
  394. spin_lock_init(&vscsi->vq_lock);
  395. sg_init_table(vscsi->sg, sg_elems + 2);
  396. err = virtscsi_init(vdev, vscsi);
  397. if (err)
  398. goto virtscsi_init_failed;
  399. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  400. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  401. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  402. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1;
  403. shost->max_id = virtscsi_config_get(vdev, max_target) + 1;
  404. shost->max_channel = 0;
  405. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  406. err = scsi_add_host(shost, &vdev->dev);
  407. if (err)
  408. goto scsi_add_host_failed;
  409. scsi_scan_host(shost);
  410. return 0;
  411. scsi_add_host_failed:
  412. vdev->config->del_vqs(vdev);
  413. virtscsi_init_failed:
  414. scsi_host_put(shost);
  415. return err;
  416. }
  417. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  418. {
  419. /* Stop all the virtqueues. */
  420. vdev->config->reset(vdev);
  421. vdev->config->del_vqs(vdev);
  422. }
  423. static void __devexit virtscsi_remove(struct virtio_device *vdev)
  424. {
  425. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  426. scsi_remove_host(shost);
  427. virtscsi_remove_vqs(vdev);
  428. scsi_host_put(shost);
  429. }
  430. #ifdef CONFIG_PM
  431. static int virtscsi_freeze(struct virtio_device *vdev)
  432. {
  433. virtscsi_remove_vqs(vdev);
  434. return 0;
  435. }
  436. static int virtscsi_restore(struct virtio_device *vdev)
  437. {
  438. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  439. struct virtio_scsi *vscsi = shost_priv(sh);
  440. return virtscsi_init(vdev, vscsi);
  441. }
  442. #endif
  443. static struct virtio_device_id id_table[] = {
  444. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  445. { 0 },
  446. };
  447. static struct virtio_driver virtio_scsi_driver = {
  448. .driver.name = KBUILD_MODNAME,
  449. .driver.owner = THIS_MODULE,
  450. .id_table = id_table,
  451. .probe = virtscsi_probe,
  452. #ifdef CONFIG_PM
  453. .freeze = virtscsi_freeze,
  454. .restore = virtscsi_restore,
  455. #endif
  456. .remove = __devexit_p(virtscsi_remove),
  457. };
  458. static int __init init(void)
  459. {
  460. int ret = -ENOMEM;
  461. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  462. if (!virtscsi_cmd_cache) {
  463. printk(KERN_ERR "kmem_cache_create() for "
  464. "virtscsi_cmd_cache failed\n");
  465. goto error;
  466. }
  467. virtscsi_cmd_pool =
  468. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  469. virtscsi_cmd_cache);
  470. if (!virtscsi_cmd_pool) {
  471. printk(KERN_ERR "mempool_create() for"
  472. "virtscsi_cmd_pool failed\n");
  473. goto error;
  474. }
  475. ret = register_virtio_driver(&virtio_scsi_driver);
  476. if (ret < 0)
  477. goto error;
  478. return 0;
  479. error:
  480. if (virtscsi_cmd_pool) {
  481. mempool_destroy(virtscsi_cmd_pool);
  482. virtscsi_cmd_pool = NULL;
  483. }
  484. if (virtscsi_cmd_cache) {
  485. kmem_cache_destroy(virtscsi_cmd_cache);
  486. virtscsi_cmd_cache = NULL;
  487. }
  488. return ret;
  489. }
  490. static void __exit fini(void)
  491. {
  492. unregister_virtio_driver(&virtio_scsi_driver);
  493. mempool_destroy(virtscsi_cmd_pool);
  494. kmem_cache_destroy(virtscsi_cmd_cache);
  495. }
  496. module_init(init);
  497. module_exit(fini);
  498. MODULE_DEVICE_TABLE(virtio, id_table);
  499. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  500. MODULE_LICENSE("GPL");