virtio_scsi.c 20 KB

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