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