virtio_scsi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 <scsi/scsi_host.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  27. #define VIRTIO_SCSI_EVENT_LEN 8
  28. /* Command queue element */
  29. struct virtio_scsi_cmd {
  30. struct scsi_cmnd *sc;
  31. struct completion *comp;
  32. union {
  33. struct virtio_scsi_cmd_req cmd;
  34. struct virtio_scsi_ctrl_tmf_req tmf;
  35. struct virtio_scsi_ctrl_an_req an;
  36. } req;
  37. union {
  38. struct virtio_scsi_cmd_resp cmd;
  39. struct virtio_scsi_ctrl_tmf_resp tmf;
  40. struct virtio_scsi_ctrl_an_resp an;
  41. struct virtio_scsi_event evt;
  42. } resp;
  43. } ____cacheline_aligned_in_smp;
  44. struct virtio_scsi_event_node {
  45. struct virtio_scsi *vscsi;
  46. struct virtio_scsi_event event;
  47. struct work_struct work;
  48. };
  49. struct virtio_scsi_vq {
  50. /* Protects vq */
  51. spinlock_t vq_lock;
  52. struct virtqueue *vq;
  53. };
  54. /* Per-target queue state */
  55. struct virtio_scsi_target_state {
  56. /* Never held at the same time as vq_lock. */
  57. spinlock_t tgt_lock;
  58. };
  59. /* Driver instance state */
  60. struct virtio_scsi {
  61. struct virtio_device *vdev;
  62. struct virtio_scsi_vq ctrl_vq;
  63. struct virtio_scsi_vq event_vq;
  64. struct virtio_scsi_vq req_vq;
  65. /* Get some buffers ready for event vq */
  66. struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
  67. };
  68. static struct kmem_cache *virtscsi_cmd_cache;
  69. static mempool_t *virtscsi_cmd_pool;
  70. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  71. {
  72. return vdev->priv;
  73. }
  74. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  75. {
  76. if (!resid)
  77. return;
  78. if (!scsi_bidi_cmnd(sc)) {
  79. scsi_set_resid(sc, resid);
  80. return;
  81. }
  82. scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
  83. scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
  84. }
  85. /**
  86. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  87. *
  88. * Called with vq_lock held.
  89. */
  90. static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
  91. {
  92. struct virtio_scsi_cmd *cmd = buf;
  93. struct scsi_cmnd *sc = cmd->sc;
  94. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  95. dev_dbg(&sc->device->sdev_gendev,
  96. "cmd %p response %u status %#02x sense_len %u\n",
  97. sc, resp->response, resp->status, resp->sense_len);
  98. sc->result = resp->status;
  99. virtscsi_compute_resid(sc, resp->resid);
  100. switch (resp->response) {
  101. case VIRTIO_SCSI_S_OK:
  102. set_host_byte(sc, DID_OK);
  103. break;
  104. case VIRTIO_SCSI_S_OVERRUN:
  105. set_host_byte(sc, DID_ERROR);
  106. break;
  107. case VIRTIO_SCSI_S_ABORTED:
  108. set_host_byte(sc, DID_ABORT);
  109. break;
  110. case VIRTIO_SCSI_S_BAD_TARGET:
  111. set_host_byte(sc, DID_BAD_TARGET);
  112. break;
  113. case VIRTIO_SCSI_S_RESET:
  114. set_host_byte(sc, DID_RESET);
  115. break;
  116. case VIRTIO_SCSI_S_BUSY:
  117. set_host_byte(sc, DID_BUS_BUSY);
  118. break;
  119. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  120. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  121. break;
  122. case VIRTIO_SCSI_S_TARGET_FAILURE:
  123. set_host_byte(sc, DID_TARGET_FAILURE);
  124. break;
  125. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  126. set_host_byte(sc, DID_NEXUS_FAILURE);
  127. break;
  128. default:
  129. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  130. resp->response);
  131. /* fall through */
  132. case VIRTIO_SCSI_S_FAILURE:
  133. set_host_byte(sc, DID_ERROR);
  134. break;
  135. }
  136. WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
  137. if (sc->sense_buffer) {
  138. memcpy(sc->sense_buffer, resp->sense,
  139. min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
  140. if (resp->sense_len)
  141. set_driver_byte(sc, DRIVER_SENSE);
  142. }
  143. mempool_free(cmd, virtscsi_cmd_pool);
  144. sc->scsi_done(sc);
  145. }
  146. static void virtscsi_vq_done(struct virtio_scsi *vscsi, struct virtqueue *vq,
  147. void (*fn)(struct virtio_scsi *vscsi, void *buf))
  148. {
  149. void *buf;
  150. unsigned int len;
  151. do {
  152. virtqueue_disable_cb(vq);
  153. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  154. fn(vscsi, buf);
  155. } while (!virtqueue_enable_cb(vq));
  156. }
  157. static void virtscsi_req_done(struct virtqueue *vq)
  158. {
  159. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  160. struct virtio_scsi *vscsi = shost_priv(sh);
  161. unsigned long flags;
  162. spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags);
  163. virtscsi_vq_done(vscsi, vq, virtscsi_complete_cmd);
  164. spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags);
  165. };
  166. static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
  167. {
  168. struct virtio_scsi_cmd *cmd = buf;
  169. if (cmd->comp)
  170. complete_all(cmd->comp);
  171. else
  172. mempool_free(cmd, virtscsi_cmd_pool);
  173. }
  174. static void virtscsi_ctrl_done(struct virtqueue *vq)
  175. {
  176. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  177. struct virtio_scsi *vscsi = shost_priv(sh);
  178. unsigned long flags;
  179. spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
  180. virtscsi_vq_done(vscsi, vq, virtscsi_complete_free);
  181. spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
  182. };
  183. static int virtscsi_kick_event(struct virtio_scsi *vscsi,
  184. struct virtio_scsi_event_node *event_node)
  185. {
  186. int err;
  187. struct scatterlist sg;
  188. unsigned long flags;
  189. sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
  190. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  191. err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
  192. GFP_ATOMIC);
  193. if (!err)
  194. virtqueue_kick(vscsi->event_vq.vq);
  195. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  196. return err;
  197. }
  198. static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
  199. {
  200. int i;
  201. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
  202. vscsi->event_list[i].vscsi = vscsi;
  203. virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
  204. }
  205. return 0;
  206. }
  207. static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
  208. {
  209. int i;
  210. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
  211. cancel_work_sync(&vscsi->event_list[i].work);
  212. }
  213. static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
  214. struct virtio_scsi_event *event)
  215. {
  216. struct scsi_device *sdev;
  217. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  218. unsigned int target = event->lun[1];
  219. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  220. switch (event->reason) {
  221. case VIRTIO_SCSI_EVT_RESET_RESCAN:
  222. scsi_add_device(shost, 0, target, lun);
  223. break;
  224. case VIRTIO_SCSI_EVT_RESET_REMOVED:
  225. sdev = scsi_device_lookup(shost, 0, target, lun);
  226. if (sdev) {
  227. scsi_remove_device(sdev);
  228. scsi_device_put(sdev);
  229. } else {
  230. pr_err("SCSI device %d 0 %d %d not found\n",
  231. shost->host_no, target, lun);
  232. }
  233. break;
  234. default:
  235. pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
  236. }
  237. }
  238. static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
  239. struct virtio_scsi_event *event)
  240. {
  241. struct scsi_device *sdev;
  242. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  243. unsigned int target = event->lun[1];
  244. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  245. u8 asc = event->reason & 255;
  246. u8 ascq = event->reason >> 8;
  247. sdev = scsi_device_lookup(shost, 0, target, lun);
  248. if (!sdev) {
  249. pr_err("SCSI device %d 0 %d %d not found\n",
  250. shost->host_no, target, lun);
  251. return;
  252. }
  253. /* Handle "Parameters changed", "Mode parameters changed", and
  254. "Capacity data has changed". */
  255. if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
  256. scsi_rescan_device(&sdev->sdev_gendev);
  257. scsi_device_put(sdev);
  258. }
  259. static void virtscsi_handle_event(struct work_struct *work)
  260. {
  261. struct virtio_scsi_event_node *event_node =
  262. container_of(work, struct virtio_scsi_event_node, work);
  263. struct virtio_scsi *vscsi = event_node->vscsi;
  264. struct virtio_scsi_event *event = &event_node->event;
  265. if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
  266. event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
  267. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  268. }
  269. switch (event->event) {
  270. case VIRTIO_SCSI_T_NO_EVENT:
  271. break;
  272. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  273. virtscsi_handle_transport_reset(vscsi, event);
  274. break;
  275. case VIRTIO_SCSI_T_PARAM_CHANGE:
  276. virtscsi_handle_param_change(vscsi, event);
  277. break;
  278. default:
  279. pr_err("Unsupport virtio scsi event %x\n", event->event);
  280. }
  281. virtscsi_kick_event(vscsi, event_node);
  282. }
  283. static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
  284. {
  285. struct virtio_scsi_event_node *event_node = buf;
  286. INIT_WORK(&event_node->work, virtscsi_handle_event);
  287. schedule_work(&event_node->work);
  288. }
  289. static void virtscsi_event_done(struct virtqueue *vq)
  290. {
  291. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  292. struct virtio_scsi *vscsi = shost_priv(sh);
  293. unsigned long flags;
  294. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  295. virtscsi_vq_done(vscsi, vq, virtscsi_complete_event);
  296. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  297. };
  298. /**
  299. * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
  300. * @vq : the struct virtqueue we're talking about
  301. * @cmd : command structure
  302. * @req_size : size of the request buffer
  303. * @resp_size : size of the response buffer
  304. * @gfp : flags to use for memory allocations
  305. */
  306. static int virtscsi_add_cmd(struct virtqueue *vq,
  307. struct virtio_scsi_cmd *cmd,
  308. size_t req_size, size_t resp_size, gfp_t gfp)
  309. {
  310. struct scsi_cmnd *sc = cmd->sc;
  311. struct scatterlist *sgs[4], req, resp;
  312. struct sg_table *out, *in;
  313. unsigned out_num = 0, in_num = 0;
  314. out = in = NULL;
  315. if (sc && sc->sc_data_direction != DMA_NONE) {
  316. if (sc->sc_data_direction != DMA_FROM_DEVICE)
  317. out = &scsi_out(sc)->table;
  318. if (sc->sc_data_direction != DMA_TO_DEVICE)
  319. in = &scsi_in(sc)->table;
  320. }
  321. /* Request header. */
  322. sg_init_one(&req, &cmd->req, req_size);
  323. sgs[out_num++] = &req;
  324. /* Data-out buffer. */
  325. if (out)
  326. sgs[out_num++] = out->sgl;
  327. /* Response header. */
  328. sg_init_one(&resp, &cmd->resp, resp_size);
  329. sgs[out_num + in_num++] = &resp;
  330. /* Data-in buffer */
  331. if (in)
  332. sgs[out_num + in_num++] = in->sgl;
  333. return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
  334. }
  335. static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
  336. struct virtio_scsi_cmd *cmd,
  337. size_t req_size, size_t resp_size, gfp_t gfp)
  338. {
  339. unsigned long flags;
  340. int err;
  341. bool needs_kick = false;
  342. spin_lock_irqsave(&vq->vq_lock, flags);
  343. err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
  344. if (!err)
  345. needs_kick = virtqueue_kick_prepare(vq->vq);
  346. spin_unlock_irqrestore(&vq->vq_lock, flags);
  347. if (needs_kick)
  348. virtqueue_notify(vq->vq);
  349. return err;
  350. }
  351. static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
  352. {
  353. struct virtio_scsi *vscsi = shost_priv(sh);
  354. struct virtio_scsi_cmd *cmd;
  355. int ret;
  356. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  357. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  358. /* TODO: check feature bit and fail if unsupported? */
  359. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  360. dev_dbg(&sc->device->sdev_gendev,
  361. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  362. ret = SCSI_MLQUEUE_HOST_BUSY;
  363. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
  364. if (!cmd)
  365. goto out;
  366. memset(cmd, 0, sizeof(*cmd));
  367. cmd->sc = sc;
  368. cmd->req.cmd = (struct virtio_scsi_cmd_req){
  369. .lun[0] = 1,
  370. .lun[1] = sc->device->id,
  371. .lun[2] = (sc->device->lun >> 8) | 0x40,
  372. .lun[3] = sc->device->lun & 0xff,
  373. .tag = (unsigned long)sc,
  374. .task_attr = VIRTIO_SCSI_S_SIMPLE,
  375. .prio = 0,
  376. .crn = 0,
  377. };
  378. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  379. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  380. if (virtscsi_kick_cmd(&vscsi->req_vq, cmd,
  381. sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
  382. GFP_ATOMIC) == 0)
  383. ret = 0;
  384. else
  385. mempool_free(cmd, virtscsi_cmd_pool);
  386. out:
  387. return ret;
  388. }
  389. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  390. {
  391. DECLARE_COMPLETION_ONSTACK(comp);
  392. int ret = FAILED;
  393. cmd->comp = &comp;
  394. if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
  395. sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
  396. GFP_NOIO) < 0)
  397. goto out;
  398. wait_for_completion(&comp);
  399. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  400. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  401. ret = SUCCESS;
  402. out:
  403. mempool_free(cmd, virtscsi_cmd_pool);
  404. return ret;
  405. }
  406. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  407. {
  408. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  409. struct virtio_scsi_cmd *cmd;
  410. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  411. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  412. if (!cmd)
  413. return FAILED;
  414. memset(cmd, 0, sizeof(*cmd));
  415. cmd->sc = sc;
  416. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  417. .type = VIRTIO_SCSI_T_TMF,
  418. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  419. .lun[0] = 1,
  420. .lun[1] = sc->device->id,
  421. .lun[2] = (sc->device->lun >> 8) | 0x40,
  422. .lun[3] = sc->device->lun & 0xff,
  423. };
  424. return virtscsi_tmf(vscsi, cmd);
  425. }
  426. static int virtscsi_abort(struct scsi_cmnd *sc)
  427. {
  428. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  429. struct virtio_scsi_cmd *cmd;
  430. scmd_printk(KERN_INFO, sc, "abort\n");
  431. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  432. if (!cmd)
  433. return FAILED;
  434. memset(cmd, 0, sizeof(*cmd));
  435. cmd->sc = sc;
  436. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  437. .type = VIRTIO_SCSI_T_TMF,
  438. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  439. .lun[0] = 1,
  440. .lun[1] = sc->device->id,
  441. .lun[2] = (sc->device->lun >> 8) | 0x40,
  442. .lun[3] = sc->device->lun & 0xff,
  443. .tag = (unsigned long)sc,
  444. };
  445. return virtscsi_tmf(vscsi, cmd);
  446. }
  447. static int virtscsi_target_alloc(struct scsi_target *starget)
  448. {
  449. struct virtio_scsi_target_state *tgt =
  450. kmalloc(sizeof(*tgt), GFP_KERNEL);
  451. if (!tgt)
  452. return -ENOMEM;
  453. spin_lock_init(&tgt->tgt_lock);
  454. starget->hostdata = tgt;
  455. return 0;
  456. }
  457. static void virtscsi_target_destroy(struct scsi_target *starget)
  458. {
  459. struct virtio_scsi_target_state *tgt = starget->hostdata;
  460. kfree(tgt);
  461. }
  462. static struct scsi_host_template virtscsi_host_template = {
  463. .module = THIS_MODULE,
  464. .name = "Virtio SCSI HBA",
  465. .proc_name = "virtio_scsi",
  466. .queuecommand = virtscsi_queuecommand,
  467. .this_id = -1,
  468. .eh_abort_handler = virtscsi_abort,
  469. .eh_device_reset_handler = virtscsi_device_reset,
  470. .can_queue = 1024,
  471. .dma_boundary = UINT_MAX,
  472. .use_clustering = ENABLE_CLUSTERING,
  473. .target_alloc = virtscsi_target_alloc,
  474. .target_destroy = virtscsi_target_destroy,
  475. };
  476. #define virtscsi_config_get(vdev, fld) \
  477. ({ \
  478. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  479. vdev->config->get(vdev, \
  480. offsetof(struct virtio_scsi_config, fld), \
  481. &__val, sizeof(__val)); \
  482. __val; \
  483. })
  484. #define virtscsi_config_set(vdev, fld, val) \
  485. (void)({ \
  486. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  487. vdev->config->set(vdev, \
  488. offsetof(struct virtio_scsi_config, fld), \
  489. &__val, sizeof(__val)); \
  490. })
  491. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  492. struct virtqueue *vq)
  493. {
  494. spin_lock_init(&virtscsi_vq->vq_lock);
  495. virtscsi_vq->vq = vq;
  496. }
  497. static void virtscsi_scan(struct virtio_device *vdev)
  498. {
  499. struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
  500. scsi_scan_host(shost);
  501. }
  502. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  503. {
  504. /* Stop all the virtqueues. */
  505. vdev->config->reset(vdev);
  506. vdev->config->del_vqs(vdev);
  507. }
  508. static int virtscsi_init(struct virtio_device *vdev,
  509. struct virtio_scsi *vscsi)
  510. {
  511. int err;
  512. struct virtqueue *vqs[3];
  513. vq_callback_t *callbacks[] = {
  514. virtscsi_ctrl_done,
  515. virtscsi_event_done,
  516. virtscsi_req_done
  517. };
  518. const char *names[] = {
  519. "control",
  520. "event",
  521. "request"
  522. };
  523. /* Discover virtqueues and write information to configuration. */
  524. err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
  525. if (err)
  526. return err;
  527. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  528. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  529. virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
  530. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  531. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  532. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  533. virtscsi_kick_event_all(vscsi);
  534. return err;
  535. }
  536. static int virtscsi_probe(struct virtio_device *vdev)
  537. {
  538. struct Scsi_Host *shost;
  539. struct virtio_scsi *vscsi;
  540. int err;
  541. u32 sg_elems, num_targets;
  542. u32 cmd_per_lun;
  543. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  544. shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi));
  545. if (!shost)
  546. return -ENOMEM;
  547. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  548. shost->sg_tablesize = sg_elems;
  549. vscsi = shost_priv(shost);
  550. vscsi->vdev = vdev;
  551. vdev->priv = shost;
  552. err = virtscsi_init(vdev, vscsi);
  553. if (err)
  554. goto virtscsi_init_failed;
  555. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  556. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  557. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  558. /* LUNs > 256 are reported with format 1, so they go in the range
  559. * 16640-32767.
  560. */
  561. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  562. shost->max_id = num_targets;
  563. shost->max_channel = 0;
  564. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  565. err = scsi_add_host(shost, &vdev->dev);
  566. if (err)
  567. goto scsi_add_host_failed;
  568. /*
  569. * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
  570. * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
  571. */
  572. return 0;
  573. scsi_add_host_failed:
  574. vdev->config->del_vqs(vdev);
  575. virtscsi_init_failed:
  576. scsi_host_put(shost);
  577. return err;
  578. }
  579. static void virtscsi_remove(struct virtio_device *vdev)
  580. {
  581. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  582. struct virtio_scsi *vscsi = shost_priv(shost);
  583. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  584. virtscsi_cancel_event_work(vscsi);
  585. scsi_remove_host(shost);
  586. virtscsi_remove_vqs(vdev);
  587. scsi_host_put(shost);
  588. }
  589. #ifdef CONFIG_PM
  590. static int virtscsi_freeze(struct virtio_device *vdev)
  591. {
  592. virtscsi_remove_vqs(vdev);
  593. return 0;
  594. }
  595. static int virtscsi_restore(struct virtio_device *vdev)
  596. {
  597. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  598. struct virtio_scsi *vscsi = shost_priv(sh);
  599. return virtscsi_init(vdev, vscsi);
  600. }
  601. #endif
  602. static struct virtio_device_id id_table[] = {
  603. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  604. { 0 },
  605. };
  606. static unsigned int features[] = {
  607. VIRTIO_SCSI_F_HOTPLUG,
  608. VIRTIO_SCSI_F_CHANGE,
  609. };
  610. static struct virtio_driver virtio_scsi_driver = {
  611. .feature_table = features,
  612. .feature_table_size = ARRAY_SIZE(features),
  613. .driver.name = KBUILD_MODNAME,
  614. .driver.owner = THIS_MODULE,
  615. .id_table = id_table,
  616. .probe = virtscsi_probe,
  617. .scan = virtscsi_scan,
  618. #ifdef CONFIG_PM
  619. .freeze = virtscsi_freeze,
  620. .restore = virtscsi_restore,
  621. #endif
  622. .remove = virtscsi_remove,
  623. };
  624. static int __init init(void)
  625. {
  626. int ret = -ENOMEM;
  627. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  628. if (!virtscsi_cmd_cache) {
  629. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  630. goto error;
  631. }
  632. virtscsi_cmd_pool =
  633. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  634. virtscsi_cmd_cache);
  635. if (!virtscsi_cmd_pool) {
  636. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  637. goto error;
  638. }
  639. ret = register_virtio_driver(&virtio_scsi_driver);
  640. if (ret < 0)
  641. goto error;
  642. return 0;
  643. error:
  644. if (virtscsi_cmd_pool) {
  645. mempool_destroy(virtscsi_cmd_pool);
  646. virtscsi_cmd_pool = NULL;
  647. }
  648. if (virtscsi_cmd_cache) {
  649. kmem_cache_destroy(virtscsi_cmd_cache);
  650. virtscsi_cmd_cache = NULL;
  651. }
  652. return ret;
  653. }
  654. static void __exit fini(void)
  655. {
  656. unregister_virtio_driver(&virtio_scsi_driver);
  657. mempool_destroy(virtscsi_cmd_pool);
  658. kmem_cache_destroy(virtscsi_cmd_cache);
  659. }
  660. module_init(init);
  661. module_exit(fini);
  662. MODULE_DEVICE_TABLE(virtio, id_table);
  663. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  664. MODULE_LICENSE("GPL");