virtio_scsi.c 20 KB

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