virtio_scsi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 ret;
  188. struct scatterlist sg;
  189. unsigned long flags;
  190. sg_set_buf(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
  191. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  192. ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC);
  193. if (ret >= 0)
  194. virtqueue_kick(vscsi->event_vq.vq);
  195. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  196. return ret;
  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_event(struct work_struct *work)
  239. {
  240. struct virtio_scsi_event_node *event_node =
  241. container_of(work, struct virtio_scsi_event_node, work);
  242. struct virtio_scsi *vscsi = event_node->vscsi;
  243. struct virtio_scsi_event *event = &event_node->event;
  244. if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
  245. event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
  246. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  247. }
  248. switch (event->event) {
  249. case VIRTIO_SCSI_T_NO_EVENT:
  250. break;
  251. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  252. virtscsi_handle_transport_reset(vscsi, event);
  253. break;
  254. default:
  255. pr_err("Unsupport virtio scsi event %x\n", event->event);
  256. }
  257. virtscsi_kick_event(vscsi, event_node);
  258. }
  259. static void virtscsi_complete_event(void *buf)
  260. {
  261. struct virtio_scsi_event_node *event_node = buf;
  262. INIT_WORK(&event_node->work, virtscsi_handle_event);
  263. schedule_work(&event_node->work);
  264. }
  265. static void virtscsi_event_done(struct virtqueue *vq)
  266. {
  267. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  268. struct virtio_scsi *vscsi = shost_priv(sh);
  269. unsigned long flags;
  270. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  271. virtscsi_vq_done(vq, virtscsi_complete_event);
  272. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  273. };
  274. static void virtscsi_map_sgl(struct scatterlist *sg, unsigned int *p_idx,
  275. struct scsi_data_buffer *sdb)
  276. {
  277. struct sg_table *table = &sdb->table;
  278. struct scatterlist *sg_elem;
  279. unsigned int idx = *p_idx;
  280. int i;
  281. for_each_sg(table->sgl, sg_elem, table->nents, i)
  282. sg_set_buf(&sg[idx++], sg_virt(sg_elem), sg_elem->length);
  283. *p_idx = idx;
  284. }
  285. /**
  286. * virtscsi_map_cmd - map a scsi_cmd to a virtqueue scatterlist
  287. * @vscsi : virtio_scsi state
  288. * @cmd : command structure
  289. * @out_num : number of read-only elements
  290. * @in_num : number of write-only elements
  291. * @req_size : size of the request buffer
  292. * @resp_size : size of the response buffer
  293. *
  294. * Called with tgt_lock held.
  295. */
  296. static void virtscsi_map_cmd(struct virtio_scsi_target_state *tgt,
  297. struct virtio_scsi_cmd *cmd,
  298. unsigned *out_num, unsigned *in_num,
  299. size_t req_size, size_t resp_size)
  300. {
  301. struct scsi_cmnd *sc = cmd->sc;
  302. struct scatterlist *sg = tgt->sg;
  303. unsigned int idx = 0;
  304. /* Request header. */
  305. sg_set_buf(&sg[idx++], &cmd->req, req_size);
  306. /* Data-out buffer. */
  307. if (sc && sc->sc_data_direction != DMA_FROM_DEVICE)
  308. virtscsi_map_sgl(sg, &idx, scsi_out(sc));
  309. *out_num = idx;
  310. /* Response header. */
  311. sg_set_buf(&sg[idx++], &cmd->resp, resp_size);
  312. /* Data-in buffer */
  313. if (sc && sc->sc_data_direction != DMA_TO_DEVICE)
  314. virtscsi_map_sgl(sg, &idx, scsi_in(sc));
  315. *in_num = idx - *out_num;
  316. }
  317. static int virtscsi_kick_cmd(struct virtio_scsi_target_state *tgt,
  318. struct virtio_scsi_vq *vq,
  319. struct virtio_scsi_cmd *cmd,
  320. size_t req_size, size_t resp_size, gfp_t gfp)
  321. {
  322. unsigned int out_num, in_num;
  323. unsigned long flags;
  324. int ret;
  325. spin_lock_irqsave(&tgt->tgt_lock, flags);
  326. virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size);
  327. spin_lock(&vq->vq_lock);
  328. ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp);
  329. spin_unlock(&tgt->tgt_lock);
  330. if (ret >= 0)
  331. ret = virtqueue_kick_prepare(vq->vq);
  332. spin_unlock_irqrestore(&vq->vq_lock, flags);
  333. if (ret > 0)
  334. virtqueue_notify(vq->vq);
  335. return ret;
  336. }
  337. static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
  338. {
  339. struct virtio_scsi *vscsi = shost_priv(sh);
  340. struct virtio_scsi_target_state *tgt = vscsi->tgt[sc->device->id];
  341. struct virtio_scsi_cmd *cmd;
  342. int ret;
  343. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  344. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  345. /* TODO: check feature bit and fail if unsupported? */
  346. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  347. dev_dbg(&sc->device->sdev_gendev,
  348. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  349. ret = SCSI_MLQUEUE_HOST_BUSY;
  350. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
  351. if (!cmd)
  352. goto out;
  353. memset(cmd, 0, sizeof(*cmd));
  354. cmd->sc = sc;
  355. cmd->req.cmd = (struct virtio_scsi_cmd_req){
  356. .lun[0] = 1,
  357. .lun[1] = sc->device->id,
  358. .lun[2] = (sc->device->lun >> 8) | 0x40,
  359. .lun[3] = sc->device->lun & 0xff,
  360. .tag = (unsigned long)sc,
  361. .task_attr = VIRTIO_SCSI_S_SIMPLE,
  362. .prio = 0,
  363. .crn = 0,
  364. };
  365. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  366. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  367. if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd,
  368. sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
  369. GFP_ATOMIC) >= 0)
  370. ret = 0;
  371. out:
  372. return ret;
  373. }
  374. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  375. {
  376. DECLARE_COMPLETION_ONSTACK(comp);
  377. struct virtio_scsi_target_state *tgt = vscsi->tgt[cmd->sc->device->id];
  378. int ret = FAILED;
  379. cmd->comp = &comp;
  380. if (virtscsi_kick_cmd(tgt, &vscsi->ctrl_vq, cmd,
  381. sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
  382. GFP_NOIO) < 0)
  383. goto out;
  384. wait_for_completion(&comp);
  385. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  386. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  387. ret = SUCCESS;
  388. out:
  389. mempool_free(cmd, virtscsi_cmd_pool);
  390. return ret;
  391. }
  392. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  393. {
  394. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  395. struct virtio_scsi_cmd *cmd;
  396. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  397. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  398. if (!cmd)
  399. return FAILED;
  400. memset(cmd, 0, sizeof(*cmd));
  401. cmd->sc = sc;
  402. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  403. .type = VIRTIO_SCSI_T_TMF,
  404. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  405. .lun[0] = 1,
  406. .lun[1] = sc->device->id,
  407. .lun[2] = (sc->device->lun >> 8) | 0x40,
  408. .lun[3] = sc->device->lun & 0xff,
  409. };
  410. return virtscsi_tmf(vscsi, cmd);
  411. }
  412. static int virtscsi_abort(struct scsi_cmnd *sc)
  413. {
  414. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  415. struct virtio_scsi_cmd *cmd;
  416. scmd_printk(KERN_INFO, sc, "abort\n");
  417. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  418. if (!cmd)
  419. return FAILED;
  420. memset(cmd, 0, sizeof(*cmd));
  421. cmd->sc = sc;
  422. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  423. .type = VIRTIO_SCSI_T_TMF,
  424. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  425. .lun[0] = 1,
  426. .lun[1] = sc->device->id,
  427. .lun[2] = (sc->device->lun >> 8) | 0x40,
  428. .lun[3] = sc->device->lun & 0xff,
  429. .tag = (unsigned long)sc,
  430. };
  431. return virtscsi_tmf(vscsi, cmd);
  432. }
  433. static struct scsi_host_template virtscsi_host_template = {
  434. .module = THIS_MODULE,
  435. .name = "Virtio SCSI HBA",
  436. .proc_name = "virtio_scsi",
  437. .queuecommand = virtscsi_queuecommand,
  438. .this_id = -1,
  439. .eh_abort_handler = virtscsi_abort,
  440. .eh_device_reset_handler = virtscsi_device_reset,
  441. .can_queue = 1024,
  442. .dma_boundary = UINT_MAX,
  443. .use_clustering = ENABLE_CLUSTERING,
  444. };
  445. #define virtscsi_config_get(vdev, fld) \
  446. ({ \
  447. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  448. vdev->config->get(vdev, \
  449. offsetof(struct virtio_scsi_config, fld), \
  450. &__val, sizeof(__val)); \
  451. __val; \
  452. })
  453. #define virtscsi_config_set(vdev, fld, val) \
  454. (void)({ \
  455. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  456. vdev->config->set(vdev, \
  457. offsetof(struct virtio_scsi_config, fld), \
  458. &__val, sizeof(__val)); \
  459. })
  460. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  461. struct virtqueue *vq)
  462. {
  463. spin_lock_init(&virtscsi_vq->vq_lock);
  464. virtscsi_vq->vq = vq;
  465. }
  466. static struct virtio_scsi_target_state *virtscsi_alloc_tgt(
  467. struct virtio_device *vdev, int sg_elems)
  468. {
  469. struct virtio_scsi_target_state *tgt;
  470. gfp_t gfp_mask = GFP_KERNEL;
  471. /* We need extra sg elements at head and tail. */
  472. tgt = kmalloc(sizeof(*tgt) + sizeof(tgt->sg[0]) * (sg_elems + 2),
  473. gfp_mask);
  474. if (!tgt)
  475. return NULL;
  476. spin_lock_init(&tgt->tgt_lock);
  477. sg_init_table(tgt->sg, sg_elems + 2);
  478. return tgt;
  479. }
  480. static void virtscsi_scan(struct virtio_device *vdev)
  481. {
  482. struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
  483. scsi_scan_host(shost);
  484. }
  485. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  486. {
  487. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  488. struct virtio_scsi *vscsi = shost_priv(sh);
  489. u32 i, num_targets;
  490. /* Stop all the virtqueues. */
  491. vdev->config->reset(vdev);
  492. num_targets = sh->max_id;
  493. for (i = 0; i < num_targets; i++) {
  494. kfree(vscsi->tgt[i]);
  495. vscsi->tgt[i] = NULL;
  496. }
  497. vdev->config->del_vqs(vdev);
  498. }
  499. static int virtscsi_init(struct virtio_device *vdev,
  500. struct virtio_scsi *vscsi, int num_targets)
  501. {
  502. int err;
  503. struct virtqueue *vqs[3];
  504. u32 i, sg_elems;
  505. vq_callback_t *callbacks[] = {
  506. virtscsi_ctrl_done,
  507. virtscsi_event_done,
  508. virtscsi_req_done
  509. };
  510. const char *names[] = {
  511. "control",
  512. "event",
  513. "request"
  514. };
  515. /* Discover virtqueues and write information to configuration. */
  516. err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
  517. if (err)
  518. return err;
  519. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  520. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  521. virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
  522. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  523. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  524. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  525. virtscsi_kick_event_all(vscsi);
  526. /* We need to know how many segments before we allocate. */
  527. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  528. for (i = 0; i < num_targets; i++) {
  529. vscsi->tgt[i] = virtscsi_alloc_tgt(vdev, sg_elems);
  530. if (!vscsi->tgt[i]) {
  531. err = -ENOMEM;
  532. goto out;
  533. }
  534. }
  535. err = 0;
  536. out:
  537. if (err)
  538. virtscsi_remove_vqs(vdev);
  539. return err;
  540. }
  541. static int __devinit virtscsi_probe(struct virtio_device *vdev)
  542. {
  543. struct Scsi_Host *shost;
  544. struct virtio_scsi *vscsi;
  545. int err;
  546. u32 sg_elems, num_targets;
  547. u32 cmd_per_lun;
  548. /* Allocate memory and link the structs together. */
  549. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  550. shost = scsi_host_alloc(&virtscsi_host_template,
  551. sizeof(*vscsi)
  552. + num_targets * sizeof(struct virtio_scsi_target_state));
  553. if (!shost)
  554. return -ENOMEM;
  555. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  556. shost->sg_tablesize = sg_elems;
  557. vscsi = shost_priv(shost);
  558. vscsi->vdev = vdev;
  559. vdev->priv = shost;
  560. err = virtscsi_init(vdev, vscsi, num_targets);
  561. if (err)
  562. goto virtscsi_init_failed;
  563. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  564. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  565. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  566. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1;
  567. shost->max_id = num_targets;
  568. shost->max_channel = 0;
  569. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  570. err = scsi_add_host(shost, &vdev->dev);
  571. if (err)
  572. goto scsi_add_host_failed;
  573. /*
  574. * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
  575. * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
  576. */
  577. return 0;
  578. scsi_add_host_failed:
  579. vdev->config->del_vqs(vdev);
  580. virtscsi_init_failed:
  581. scsi_host_put(shost);
  582. return err;
  583. }
  584. static void __devexit virtscsi_remove(struct virtio_device *vdev)
  585. {
  586. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  587. struct virtio_scsi *vscsi = shost_priv(shost);
  588. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  589. virtscsi_cancel_event_work(vscsi);
  590. scsi_remove_host(shost);
  591. virtscsi_remove_vqs(vdev);
  592. scsi_host_put(shost);
  593. }
  594. #ifdef CONFIG_PM
  595. static int virtscsi_freeze(struct virtio_device *vdev)
  596. {
  597. virtscsi_remove_vqs(vdev);
  598. return 0;
  599. }
  600. static int virtscsi_restore(struct virtio_device *vdev)
  601. {
  602. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  603. struct virtio_scsi *vscsi = shost_priv(sh);
  604. return virtscsi_init(vdev, vscsi, sh->max_id);
  605. }
  606. #endif
  607. static struct virtio_device_id id_table[] = {
  608. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  609. { 0 },
  610. };
  611. static unsigned int features[] = {
  612. VIRTIO_SCSI_F_HOTPLUG
  613. };
  614. static struct virtio_driver virtio_scsi_driver = {
  615. .feature_table = features,
  616. .feature_table_size = ARRAY_SIZE(features),
  617. .driver.name = KBUILD_MODNAME,
  618. .driver.owner = THIS_MODULE,
  619. .id_table = id_table,
  620. .probe = virtscsi_probe,
  621. .scan = virtscsi_scan,
  622. #ifdef CONFIG_PM
  623. .freeze = virtscsi_freeze,
  624. .restore = virtscsi_restore,
  625. #endif
  626. .remove = __devexit_p(virtscsi_remove),
  627. };
  628. static int __init init(void)
  629. {
  630. int ret = -ENOMEM;
  631. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  632. if (!virtscsi_cmd_cache) {
  633. printk(KERN_ERR "kmem_cache_create() for "
  634. "virtscsi_cmd_cache failed\n");
  635. goto error;
  636. }
  637. virtscsi_cmd_pool =
  638. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  639. virtscsi_cmd_cache);
  640. if (!virtscsi_cmd_pool) {
  641. printk(KERN_ERR "mempool_create() for"
  642. "virtscsi_cmd_pool failed\n");
  643. goto error;
  644. }
  645. ret = register_virtio_driver(&virtio_scsi_driver);
  646. if (ret < 0)
  647. goto error;
  648. return 0;
  649. error:
  650. if (virtscsi_cmd_pool) {
  651. mempool_destroy(virtscsi_cmd_pool);
  652. virtscsi_cmd_pool = NULL;
  653. }
  654. if (virtscsi_cmd_cache) {
  655. kmem_cache_destroy(virtscsi_cmd_cache);
  656. virtscsi_cmd_cache = NULL;
  657. }
  658. return ret;
  659. }
  660. static void __exit fini(void)
  661. {
  662. unregister_virtio_driver(&virtio_scsi_driver);
  663. mempool_destroy(virtscsi_cmd_pool);
  664. kmem_cache_destroy(virtscsi_cmd_cache);
  665. }
  666. module_init(init);
  667. module_exit(fini);
  668. MODULE_DEVICE_TABLE(virtio, id_table);
  669. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  670. MODULE_LICENSE("GPL");