virtio_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  289. .type = VIRTIO_SCSI_T_TMF,
  290. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  291. .lun[0] = 1,
  292. .lun[1] = sc->device->id,
  293. .lun[2] = (sc->device->lun >> 8) | 0x40,
  294. .lun[3] = sc->device->lun & 0xff,
  295. };
  296. return virtscsi_tmf(vscsi, cmd);
  297. }
  298. static int virtscsi_abort(struct scsi_cmnd *sc)
  299. {
  300. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  301. struct virtio_scsi_cmd *cmd;
  302. scmd_printk(KERN_INFO, sc, "abort\n");
  303. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  304. if (!cmd)
  305. return FAILED;
  306. memset(cmd, 0, sizeof(*cmd));
  307. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  308. .type = VIRTIO_SCSI_T_TMF,
  309. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  310. .lun[0] = 1,
  311. .lun[1] = sc->device->id,
  312. .lun[2] = (sc->device->lun >> 8) | 0x40,
  313. .lun[3] = sc->device->lun & 0xff,
  314. .tag = (unsigned long)sc,
  315. };
  316. return virtscsi_tmf(vscsi, cmd);
  317. }
  318. static struct scsi_host_template virtscsi_host_template = {
  319. .module = THIS_MODULE,
  320. .name = "Virtio SCSI HBA",
  321. .proc_name = "virtio_scsi",
  322. .queuecommand = virtscsi_queuecommand,
  323. .this_id = -1,
  324. .eh_abort_handler = virtscsi_abort,
  325. .eh_device_reset_handler = virtscsi_device_reset,
  326. .can_queue = 1024,
  327. .dma_boundary = UINT_MAX,
  328. .use_clustering = ENABLE_CLUSTERING,
  329. };
  330. #define virtscsi_config_get(vdev, fld) \
  331. ({ \
  332. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  333. vdev->config->get(vdev, \
  334. offsetof(struct virtio_scsi_config, fld), \
  335. &__val, sizeof(__val)); \
  336. __val; \
  337. })
  338. #define virtscsi_config_set(vdev, fld, val) \
  339. (void)({ \
  340. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  341. vdev->config->set(vdev, \
  342. offsetof(struct virtio_scsi_config, fld), \
  343. &__val, sizeof(__val)); \
  344. })
  345. static int virtscsi_init(struct virtio_device *vdev,
  346. struct virtio_scsi *vscsi)
  347. {
  348. int err;
  349. struct virtqueue *vqs[3];
  350. vq_callback_t *callbacks[] = {
  351. virtscsi_ctrl_done,
  352. virtscsi_event_done,
  353. virtscsi_req_done
  354. };
  355. const char *names[] = {
  356. "control",
  357. "event",
  358. "request"
  359. };
  360. /* Discover virtqueues and write information to configuration. */
  361. err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
  362. if (err)
  363. return err;
  364. vscsi->ctrl_vq = vqs[0];
  365. vscsi->event_vq = vqs[1];
  366. vscsi->req_vq = vqs[2];
  367. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  368. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  369. return 0;
  370. }
  371. static int __devinit virtscsi_probe(struct virtio_device *vdev)
  372. {
  373. struct Scsi_Host *shost;
  374. struct virtio_scsi *vscsi;
  375. int err;
  376. u32 sg_elems;
  377. u32 cmd_per_lun;
  378. /* We need to know how many segments before we allocate.
  379. * We need an extra sg elements at head and tail.
  380. */
  381. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  382. /* Allocate memory and link the structs together. */
  383. shost = scsi_host_alloc(&virtscsi_host_template,
  384. sizeof(*vscsi) + sizeof(vscsi->sg[0]) * (sg_elems + 2));
  385. if (!shost)
  386. return -ENOMEM;
  387. shost->sg_tablesize = sg_elems;
  388. vscsi = shost_priv(shost);
  389. vscsi->vdev = vdev;
  390. vdev->priv = shost;
  391. /* Random initializations. */
  392. spin_lock_init(&vscsi->vq_lock);
  393. sg_init_table(vscsi->sg, sg_elems + 2);
  394. err = virtscsi_init(vdev, vscsi);
  395. if (err)
  396. goto virtscsi_init_failed;
  397. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  398. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  399. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  400. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1;
  401. shost->max_id = virtscsi_config_get(vdev, max_target) + 1;
  402. shost->max_channel = 0;
  403. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  404. err = scsi_add_host(shost, &vdev->dev);
  405. if (err)
  406. goto scsi_add_host_failed;
  407. scsi_scan_host(shost);
  408. return 0;
  409. scsi_add_host_failed:
  410. vdev->config->del_vqs(vdev);
  411. virtscsi_init_failed:
  412. scsi_host_put(shost);
  413. return err;
  414. }
  415. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  416. {
  417. /* Stop all the virtqueues. */
  418. vdev->config->reset(vdev);
  419. vdev->config->del_vqs(vdev);
  420. }
  421. static void __devexit virtscsi_remove(struct virtio_device *vdev)
  422. {
  423. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  424. scsi_remove_host(shost);
  425. virtscsi_remove_vqs(vdev);
  426. scsi_host_put(shost);
  427. }
  428. #ifdef CONFIG_PM
  429. static int virtscsi_freeze(struct virtio_device *vdev)
  430. {
  431. virtscsi_remove_vqs(vdev);
  432. return 0;
  433. }
  434. static int virtscsi_restore(struct virtio_device *vdev)
  435. {
  436. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  437. struct virtio_scsi *vscsi = shost_priv(sh);
  438. return virtscsi_init(vdev, vscsi);
  439. }
  440. #endif
  441. static struct virtio_device_id id_table[] = {
  442. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  443. { 0 },
  444. };
  445. static struct virtio_driver virtio_scsi_driver = {
  446. .driver.name = KBUILD_MODNAME,
  447. .driver.owner = THIS_MODULE,
  448. .id_table = id_table,
  449. .probe = virtscsi_probe,
  450. #ifdef CONFIG_PM
  451. .freeze = virtscsi_freeze,
  452. .restore = virtscsi_restore,
  453. #endif
  454. .remove = __devexit_p(virtscsi_remove),
  455. };
  456. static int __init init(void)
  457. {
  458. int ret = -ENOMEM;
  459. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  460. if (!virtscsi_cmd_cache) {
  461. printk(KERN_ERR "kmem_cache_create() for "
  462. "virtscsi_cmd_cache failed\n");
  463. goto error;
  464. }
  465. virtscsi_cmd_pool =
  466. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  467. virtscsi_cmd_cache);
  468. if (!virtscsi_cmd_pool) {
  469. printk(KERN_ERR "mempool_create() for"
  470. "virtscsi_cmd_pool failed\n");
  471. goto error;
  472. }
  473. ret = register_virtio_driver(&virtio_scsi_driver);
  474. if (ret < 0)
  475. goto error;
  476. return 0;
  477. error:
  478. if (virtscsi_cmd_pool) {
  479. mempool_destroy(virtscsi_cmd_pool);
  480. virtscsi_cmd_pool = NULL;
  481. }
  482. if (virtscsi_cmd_cache) {
  483. kmem_cache_destroy(virtscsi_cmd_cache);
  484. virtscsi_cmd_cache = NULL;
  485. }
  486. return ret;
  487. }
  488. static void __exit fini(void)
  489. {
  490. unregister_virtio_driver(&virtio_scsi_driver);
  491. mempool_destroy(virtscsi_cmd_pool);
  492. kmem_cache_destroy(virtscsi_cmd_cache);
  493. }
  494. module_init(init);
  495. module_exit(fini);
  496. MODULE_DEVICE_TABLE(virtio, id_table);
  497. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  498. MODULE_LICENSE("GPL");