virtio_scsi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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 <linux/cpu.h>
  24. #include <linux/blkdev.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #define VIRTIO_SCSI_MEMPOOL_SZ 64
  29. #define VIRTIO_SCSI_EVENT_LEN 8
  30. #define VIRTIO_SCSI_VQ_BASE 2
  31. /* Command queue element */
  32. struct virtio_scsi_cmd {
  33. struct scsi_cmnd *sc;
  34. struct completion *comp;
  35. union {
  36. struct virtio_scsi_cmd_req cmd;
  37. struct virtio_scsi_cmd_req_pi cmd_pi;
  38. struct virtio_scsi_ctrl_tmf_req tmf;
  39. struct virtio_scsi_ctrl_an_req an;
  40. } req;
  41. union {
  42. struct virtio_scsi_cmd_resp cmd;
  43. struct virtio_scsi_ctrl_tmf_resp tmf;
  44. struct virtio_scsi_ctrl_an_resp an;
  45. struct virtio_scsi_event evt;
  46. } resp;
  47. } ____cacheline_aligned_in_smp;
  48. struct virtio_scsi_event_node {
  49. struct virtio_scsi *vscsi;
  50. struct virtio_scsi_event event;
  51. struct work_struct work;
  52. };
  53. struct virtio_scsi_vq {
  54. /* Protects vq */
  55. spinlock_t vq_lock;
  56. struct virtqueue *vq;
  57. };
  58. /*
  59. * Per-target queue state.
  60. *
  61. * This struct holds the data needed by the queue steering policy. When a
  62. * target is sent multiple requests, we need to drive them to the same queue so
  63. * that FIFO processing order is kept. However, if a target was idle, we can
  64. * choose a queue arbitrarily. In this case the queue is chosen according to
  65. * the current VCPU, so the driver expects the number of request queues to be
  66. * equal to the number of VCPUs. This makes it easy and fast to select the
  67. * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
  68. * (each virtqueue's affinity is set to the CPU that "owns" the queue).
  69. *
  70. * tgt_lock is held to serialize reading and writing req_vq. Reading req_vq
  71. * could be done locklessly, but we do not do it yet.
  72. *
  73. * Decrements of reqs are never concurrent with writes of req_vq: before the
  74. * decrement reqs will be != 0; after the decrement the virtqueue completion
  75. * routine will not use the req_vq so it can be changed by a new request.
  76. * Thus they can happen outside the tgt_lock, provided of course we make reqs
  77. * an atomic_t.
  78. */
  79. struct virtio_scsi_target_state {
  80. /* This spinlock never held at the same time as vq_lock. */
  81. spinlock_t tgt_lock;
  82. /* Count of outstanding requests. */
  83. atomic_t reqs;
  84. /* Currently active virtqueue for requests sent to this target. */
  85. struct virtio_scsi_vq *req_vq;
  86. };
  87. /* Driver instance state */
  88. struct virtio_scsi {
  89. struct virtio_device *vdev;
  90. /* Get some buffers ready for event vq */
  91. struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
  92. u32 num_queues;
  93. /* If the affinity hint is set for virtqueues */
  94. bool affinity_hint_set;
  95. /* CPU hotplug notifier */
  96. struct notifier_block nb;
  97. struct virtio_scsi_vq ctrl_vq;
  98. struct virtio_scsi_vq event_vq;
  99. struct virtio_scsi_vq req_vqs[];
  100. };
  101. static struct kmem_cache *virtscsi_cmd_cache;
  102. static mempool_t *virtscsi_cmd_pool;
  103. static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
  104. {
  105. return vdev->priv;
  106. }
  107. static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
  108. {
  109. if (!resid)
  110. return;
  111. if (!scsi_bidi_cmnd(sc)) {
  112. scsi_set_resid(sc, resid);
  113. return;
  114. }
  115. scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
  116. scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
  117. }
  118. /**
  119. * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
  120. *
  121. * Called with vq_lock held.
  122. */
  123. static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
  124. {
  125. struct virtio_scsi_cmd *cmd = buf;
  126. struct scsi_cmnd *sc = cmd->sc;
  127. struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
  128. struct virtio_scsi_target_state *tgt =
  129. scsi_target(sc->device)->hostdata;
  130. dev_dbg(&sc->device->sdev_gendev,
  131. "cmd %p response %u status %#02x sense_len %u\n",
  132. sc, resp->response, resp->status, resp->sense_len);
  133. sc->result = resp->status;
  134. virtscsi_compute_resid(sc, resp->resid);
  135. switch (resp->response) {
  136. case VIRTIO_SCSI_S_OK:
  137. set_host_byte(sc, DID_OK);
  138. break;
  139. case VIRTIO_SCSI_S_OVERRUN:
  140. set_host_byte(sc, DID_ERROR);
  141. break;
  142. case VIRTIO_SCSI_S_ABORTED:
  143. set_host_byte(sc, DID_ABORT);
  144. break;
  145. case VIRTIO_SCSI_S_BAD_TARGET:
  146. set_host_byte(sc, DID_BAD_TARGET);
  147. break;
  148. case VIRTIO_SCSI_S_RESET:
  149. set_host_byte(sc, DID_RESET);
  150. break;
  151. case VIRTIO_SCSI_S_BUSY:
  152. set_host_byte(sc, DID_BUS_BUSY);
  153. break;
  154. case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
  155. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  156. break;
  157. case VIRTIO_SCSI_S_TARGET_FAILURE:
  158. set_host_byte(sc, DID_TARGET_FAILURE);
  159. break;
  160. case VIRTIO_SCSI_S_NEXUS_FAILURE:
  161. set_host_byte(sc, DID_NEXUS_FAILURE);
  162. break;
  163. default:
  164. scmd_printk(KERN_WARNING, sc, "Unknown response %d",
  165. resp->response);
  166. /* fall through */
  167. case VIRTIO_SCSI_S_FAILURE:
  168. set_host_byte(sc, DID_ERROR);
  169. break;
  170. }
  171. WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
  172. if (sc->sense_buffer) {
  173. memcpy(sc->sense_buffer, resp->sense,
  174. min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
  175. if (resp->sense_len)
  176. set_driver_byte(sc, DRIVER_SENSE);
  177. }
  178. sc->scsi_done(sc);
  179. atomic_dec(&tgt->reqs);
  180. }
  181. static void virtscsi_vq_done(struct virtio_scsi *vscsi,
  182. struct virtio_scsi_vq *virtscsi_vq,
  183. void (*fn)(struct virtio_scsi *vscsi, void *buf))
  184. {
  185. void *buf;
  186. unsigned int len;
  187. unsigned long flags;
  188. struct virtqueue *vq = virtscsi_vq->vq;
  189. spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
  190. do {
  191. virtqueue_disable_cb(vq);
  192. while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
  193. fn(vscsi, buf);
  194. if (unlikely(virtqueue_is_broken(vq)))
  195. break;
  196. } while (!virtqueue_enable_cb(vq));
  197. spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
  198. }
  199. static void virtscsi_req_done(struct virtqueue *vq)
  200. {
  201. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  202. struct virtio_scsi *vscsi = shost_priv(sh);
  203. int index = vq->index - VIRTIO_SCSI_VQ_BASE;
  204. struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
  205. virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
  206. };
  207. static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
  208. {
  209. struct virtio_scsi_cmd *cmd = buf;
  210. if (cmd->comp)
  211. complete_all(cmd->comp);
  212. }
  213. static void virtscsi_ctrl_done(struct virtqueue *vq)
  214. {
  215. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  216. struct virtio_scsi *vscsi = shost_priv(sh);
  217. virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
  218. };
  219. static int virtscsi_kick_event(struct virtio_scsi *vscsi,
  220. struct virtio_scsi_event_node *event_node)
  221. {
  222. int err;
  223. struct scatterlist sg;
  224. unsigned long flags;
  225. sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
  226. spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
  227. err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
  228. GFP_ATOMIC);
  229. if (!err)
  230. virtqueue_kick(vscsi->event_vq.vq);
  231. spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
  232. return err;
  233. }
  234. static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
  235. {
  236. int i;
  237. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
  238. vscsi->event_list[i].vscsi = vscsi;
  239. virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
  240. }
  241. return 0;
  242. }
  243. static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
  244. {
  245. int i;
  246. for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
  247. cancel_work_sync(&vscsi->event_list[i].work);
  248. }
  249. static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
  250. struct virtio_scsi_event *event)
  251. {
  252. struct scsi_device *sdev;
  253. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  254. unsigned int target = event->lun[1];
  255. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  256. switch (event->reason) {
  257. case VIRTIO_SCSI_EVT_RESET_RESCAN:
  258. scsi_add_device(shost, 0, target, lun);
  259. break;
  260. case VIRTIO_SCSI_EVT_RESET_REMOVED:
  261. sdev = scsi_device_lookup(shost, 0, target, lun);
  262. if (sdev) {
  263. scsi_remove_device(sdev);
  264. scsi_device_put(sdev);
  265. } else {
  266. pr_err("SCSI device %d 0 %d %d not found\n",
  267. shost->host_no, target, lun);
  268. }
  269. break;
  270. default:
  271. pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
  272. }
  273. }
  274. static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
  275. struct virtio_scsi_event *event)
  276. {
  277. struct scsi_device *sdev;
  278. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  279. unsigned int target = event->lun[1];
  280. unsigned int lun = (event->lun[2] << 8) | event->lun[3];
  281. u8 asc = event->reason & 255;
  282. u8 ascq = event->reason >> 8;
  283. sdev = scsi_device_lookup(shost, 0, target, lun);
  284. if (!sdev) {
  285. pr_err("SCSI device %d 0 %d %d not found\n",
  286. shost->host_no, target, lun);
  287. return;
  288. }
  289. /* Handle "Parameters changed", "Mode parameters changed", and
  290. "Capacity data has changed". */
  291. if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
  292. scsi_rescan_device(&sdev->sdev_gendev);
  293. scsi_device_put(sdev);
  294. }
  295. static void virtscsi_handle_event(struct work_struct *work)
  296. {
  297. struct virtio_scsi_event_node *event_node =
  298. container_of(work, struct virtio_scsi_event_node, work);
  299. struct virtio_scsi *vscsi = event_node->vscsi;
  300. struct virtio_scsi_event *event = &event_node->event;
  301. if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
  302. event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
  303. scsi_scan_host(virtio_scsi_host(vscsi->vdev));
  304. }
  305. switch (event->event) {
  306. case VIRTIO_SCSI_T_NO_EVENT:
  307. break;
  308. case VIRTIO_SCSI_T_TRANSPORT_RESET:
  309. virtscsi_handle_transport_reset(vscsi, event);
  310. break;
  311. case VIRTIO_SCSI_T_PARAM_CHANGE:
  312. virtscsi_handle_param_change(vscsi, event);
  313. break;
  314. default:
  315. pr_err("Unsupport virtio scsi event %x\n", event->event);
  316. }
  317. virtscsi_kick_event(vscsi, event_node);
  318. }
  319. static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
  320. {
  321. struct virtio_scsi_event_node *event_node = buf;
  322. INIT_WORK(&event_node->work, virtscsi_handle_event);
  323. schedule_work(&event_node->work);
  324. }
  325. static void virtscsi_event_done(struct virtqueue *vq)
  326. {
  327. struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
  328. struct virtio_scsi *vscsi = shost_priv(sh);
  329. virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
  330. };
  331. /**
  332. * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
  333. * @vq : the struct virtqueue we're talking about
  334. * @cmd : command structure
  335. * @req_size : size of the request buffer
  336. * @resp_size : size of the response buffer
  337. */
  338. static int virtscsi_add_cmd(struct virtqueue *vq,
  339. struct virtio_scsi_cmd *cmd,
  340. size_t req_size, size_t resp_size)
  341. {
  342. struct scsi_cmnd *sc = cmd->sc;
  343. struct scatterlist *sgs[6], req, resp;
  344. struct sg_table *out, *in;
  345. unsigned out_num = 0, in_num = 0;
  346. out = in = NULL;
  347. if (sc && sc->sc_data_direction != DMA_NONE) {
  348. if (sc->sc_data_direction != DMA_FROM_DEVICE)
  349. out = &scsi_out(sc)->table;
  350. if (sc->sc_data_direction != DMA_TO_DEVICE)
  351. in = &scsi_in(sc)->table;
  352. }
  353. /* Request header. */
  354. sg_init_one(&req, &cmd->req, req_size);
  355. sgs[out_num++] = &req;
  356. /* Data-out buffer. */
  357. if (out) {
  358. /* Place WRITE protection SGLs before Data OUT payload */
  359. if (scsi_prot_sg_count(sc))
  360. sgs[out_num++] = scsi_prot_sglist(sc);
  361. sgs[out_num++] = out->sgl;
  362. }
  363. /* Response header. */
  364. sg_init_one(&resp, &cmd->resp, resp_size);
  365. sgs[out_num + in_num++] = &resp;
  366. /* Data-in buffer */
  367. if (in) {
  368. /* Place READ protection SGLs before Data IN payload */
  369. if (scsi_prot_sg_count(sc))
  370. sgs[out_num + in_num++] = scsi_prot_sglist(sc);
  371. sgs[out_num + in_num++] = in->sgl;
  372. }
  373. return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
  374. }
  375. static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
  376. struct virtio_scsi_cmd *cmd,
  377. size_t req_size, size_t resp_size)
  378. {
  379. unsigned long flags;
  380. int err;
  381. bool needs_kick = false;
  382. spin_lock_irqsave(&vq->vq_lock, flags);
  383. err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
  384. if (!err)
  385. needs_kick = virtqueue_kick_prepare(vq->vq);
  386. spin_unlock_irqrestore(&vq->vq_lock, flags);
  387. if (needs_kick)
  388. virtqueue_notify(vq->vq);
  389. return err;
  390. }
  391. static void virtio_scsi_init_hdr(struct virtio_scsi_cmd_req *cmd,
  392. struct scsi_cmnd *sc)
  393. {
  394. cmd->lun[0] = 1;
  395. cmd->lun[1] = sc->device->id;
  396. cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
  397. cmd->lun[3] = sc->device->lun & 0xff;
  398. cmd->tag = (unsigned long)sc;
  399. cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
  400. cmd->prio = 0;
  401. cmd->crn = 0;
  402. }
  403. static void virtio_scsi_init_hdr_pi(struct virtio_scsi_cmd_req_pi *cmd_pi,
  404. struct scsi_cmnd *sc)
  405. {
  406. struct request *rq = sc->request;
  407. struct blk_integrity *bi;
  408. virtio_scsi_init_hdr((struct virtio_scsi_cmd_req *)cmd_pi, sc);
  409. if (!rq || !scsi_prot_sg_count(sc))
  410. return;
  411. bi = blk_get_integrity(rq->rq_disk);
  412. if (sc->sc_data_direction == DMA_TO_DEVICE)
  413. cmd_pi->pi_bytesout = blk_rq_sectors(rq) * bi->tuple_size;
  414. else if (sc->sc_data_direction == DMA_FROM_DEVICE)
  415. cmd_pi->pi_bytesin = blk_rq_sectors(rq) * bi->tuple_size;
  416. }
  417. static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
  418. struct virtio_scsi_vq *req_vq,
  419. struct scsi_cmnd *sc)
  420. {
  421. struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
  422. struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
  423. int req_size;
  424. BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
  425. /* TODO: check feature bit and fail if unsupported? */
  426. BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
  427. dev_dbg(&sc->device->sdev_gendev,
  428. "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
  429. memset(cmd, 0, sizeof(*cmd));
  430. cmd->sc = sc;
  431. BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
  432. if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
  433. virtio_scsi_init_hdr_pi(&cmd->req.cmd_pi, sc);
  434. memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
  435. req_size = sizeof(cmd->req.cmd_pi);
  436. } else {
  437. virtio_scsi_init_hdr(&cmd->req.cmd, sc);
  438. memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
  439. req_size = sizeof(cmd->req.cmd);
  440. }
  441. if (virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd)) != 0)
  442. return SCSI_MLQUEUE_HOST_BUSY;
  443. return 0;
  444. }
  445. static int virtscsi_queuecommand_single(struct Scsi_Host *sh,
  446. struct scsi_cmnd *sc)
  447. {
  448. struct virtio_scsi *vscsi = shost_priv(sh);
  449. struct virtio_scsi_target_state *tgt =
  450. scsi_target(sc->device)->hostdata;
  451. atomic_inc(&tgt->reqs);
  452. return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc);
  453. }
  454. static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi,
  455. struct virtio_scsi_target_state *tgt)
  456. {
  457. struct virtio_scsi_vq *vq;
  458. unsigned long flags;
  459. u32 queue_num;
  460. spin_lock_irqsave(&tgt->tgt_lock, flags);
  461. if (atomic_inc_return(&tgt->reqs) > 1)
  462. vq = tgt->req_vq;
  463. else {
  464. queue_num = smp_processor_id();
  465. while (unlikely(queue_num >= vscsi->num_queues))
  466. queue_num -= vscsi->num_queues;
  467. tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
  468. }
  469. spin_unlock_irqrestore(&tgt->tgt_lock, flags);
  470. return vq;
  471. }
  472. static int virtscsi_queuecommand_multi(struct Scsi_Host *sh,
  473. struct scsi_cmnd *sc)
  474. {
  475. struct virtio_scsi *vscsi = shost_priv(sh);
  476. struct virtio_scsi_target_state *tgt =
  477. scsi_target(sc->device)->hostdata;
  478. struct virtio_scsi_vq *req_vq = virtscsi_pick_vq(vscsi, tgt);
  479. return virtscsi_queuecommand(vscsi, req_vq, sc);
  480. }
  481. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  482. {
  483. DECLARE_COMPLETION_ONSTACK(comp);
  484. int ret = FAILED;
  485. cmd->comp = &comp;
  486. if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
  487. sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
  488. goto out;
  489. wait_for_completion(&comp);
  490. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  491. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  492. ret = SUCCESS;
  493. out:
  494. mempool_free(cmd, virtscsi_cmd_pool);
  495. return ret;
  496. }
  497. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  498. {
  499. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  500. struct virtio_scsi_cmd *cmd;
  501. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  502. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  503. if (!cmd)
  504. return FAILED;
  505. memset(cmd, 0, sizeof(*cmd));
  506. cmd->sc = sc;
  507. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  508. .type = VIRTIO_SCSI_T_TMF,
  509. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  510. .lun[0] = 1,
  511. .lun[1] = sc->device->id,
  512. .lun[2] = (sc->device->lun >> 8) | 0x40,
  513. .lun[3] = sc->device->lun & 0xff,
  514. };
  515. return virtscsi_tmf(vscsi, cmd);
  516. }
  517. static int virtscsi_abort(struct scsi_cmnd *sc)
  518. {
  519. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  520. struct virtio_scsi_cmd *cmd;
  521. scmd_printk(KERN_INFO, sc, "abort\n");
  522. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  523. if (!cmd)
  524. return FAILED;
  525. memset(cmd, 0, sizeof(*cmd));
  526. cmd->sc = sc;
  527. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  528. .type = VIRTIO_SCSI_T_TMF,
  529. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  530. .lun[0] = 1,
  531. .lun[1] = sc->device->id,
  532. .lun[2] = (sc->device->lun >> 8) | 0x40,
  533. .lun[3] = sc->device->lun & 0xff,
  534. .tag = (unsigned long)sc,
  535. };
  536. return virtscsi_tmf(vscsi, cmd);
  537. }
  538. static int virtscsi_target_alloc(struct scsi_target *starget)
  539. {
  540. struct virtio_scsi_target_state *tgt =
  541. kmalloc(sizeof(*tgt), GFP_KERNEL);
  542. if (!tgt)
  543. return -ENOMEM;
  544. spin_lock_init(&tgt->tgt_lock);
  545. atomic_set(&tgt->reqs, 0);
  546. tgt->req_vq = NULL;
  547. starget->hostdata = tgt;
  548. return 0;
  549. }
  550. static void virtscsi_target_destroy(struct scsi_target *starget)
  551. {
  552. struct virtio_scsi_target_state *tgt = starget->hostdata;
  553. kfree(tgt);
  554. }
  555. static struct scsi_host_template virtscsi_host_template_single = {
  556. .module = THIS_MODULE,
  557. .name = "Virtio SCSI HBA",
  558. .proc_name = "virtio_scsi",
  559. .this_id = -1,
  560. .cmd_size = sizeof(struct virtio_scsi_cmd),
  561. .queuecommand = virtscsi_queuecommand_single,
  562. .eh_abort_handler = virtscsi_abort,
  563. .eh_device_reset_handler = virtscsi_device_reset,
  564. .can_queue = 1024,
  565. .dma_boundary = UINT_MAX,
  566. .use_clustering = ENABLE_CLUSTERING,
  567. .target_alloc = virtscsi_target_alloc,
  568. .target_destroy = virtscsi_target_destroy,
  569. };
  570. static struct scsi_host_template virtscsi_host_template_multi = {
  571. .module = THIS_MODULE,
  572. .name = "Virtio SCSI HBA",
  573. .proc_name = "virtio_scsi",
  574. .this_id = -1,
  575. .cmd_size = sizeof(struct virtio_scsi_cmd),
  576. .queuecommand = virtscsi_queuecommand_multi,
  577. .eh_abort_handler = virtscsi_abort,
  578. .eh_device_reset_handler = virtscsi_device_reset,
  579. .can_queue = 1024,
  580. .dma_boundary = UINT_MAX,
  581. .use_clustering = ENABLE_CLUSTERING,
  582. .target_alloc = virtscsi_target_alloc,
  583. .target_destroy = virtscsi_target_destroy,
  584. };
  585. #define virtscsi_config_get(vdev, fld) \
  586. ({ \
  587. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  588. virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
  589. __val; \
  590. })
  591. #define virtscsi_config_set(vdev, fld, val) \
  592. do { \
  593. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  594. virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
  595. } while(0)
  596. static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  597. {
  598. int i;
  599. int cpu;
  600. /* In multiqueue mode, when the number of cpu is equal
  601. * to the number of request queues, we let the qeueues
  602. * to be private to one cpu by setting the affinity hint
  603. * to eliminate the contention.
  604. */
  605. if ((vscsi->num_queues == 1 ||
  606. vscsi->num_queues != num_online_cpus()) && affinity) {
  607. if (vscsi->affinity_hint_set)
  608. affinity = false;
  609. else
  610. return;
  611. }
  612. if (affinity) {
  613. i = 0;
  614. for_each_online_cpu(cpu) {
  615. virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu);
  616. i++;
  617. }
  618. vscsi->affinity_hint_set = true;
  619. } else {
  620. for (i = 0; i < vscsi->num_queues; i++) {
  621. if (!vscsi->req_vqs[i].vq)
  622. continue;
  623. virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
  624. }
  625. vscsi->affinity_hint_set = false;
  626. }
  627. }
  628. static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  629. {
  630. get_online_cpus();
  631. __virtscsi_set_affinity(vscsi, affinity);
  632. put_online_cpus();
  633. }
  634. static int virtscsi_cpu_callback(struct notifier_block *nfb,
  635. unsigned long action, void *hcpu)
  636. {
  637. struct virtio_scsi *vscsi = container_of(nfb, struct virtio_scsi, nb);
  638. switch(action) {
  639. case CPU_ONLINE:
  640. case CPU_ONLINE_FROZEN:
  641. case CPU_DEAD:
  642. case CPU_DEAD_FROZEN:
  643. __virtscsi_set_affinity(vscsi, true);
  644. break;
  645. default:
  646. break;
  647. }
  648. return NOTIFY_OK;
  649. }
  650. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  651. struct virtqueue *vq)
  652. {
  653. spin_lock_init(&virtscsi_vq->vq_lock);
  654. virtscsi_vq->vq = vq;
  655. }
  656. static void virtscsi_scan(struct virtio_device *vdev)
  657. {
  658. struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
  659. scsi_scan_host(shost);
  660. }
  661. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  662. {
  663. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  664. struct virtio_scsi *vscsi = shost_priv(sh);
  665. virtscsi_set_affinity(vscsi, false);
  666. /* Stop all the virtqueues. */
  667. vdev->config->reset(vdev);
  668. vdev->config->del_vqs(vdev);
  669. }
  670. static int virtscsi_init(struct virtio_device *vdev,
  671. struct virtio_scsi *vscsi)
  672. {
  673. int err;
  674. u32 i;
  675. u32 num_vqs;
  676. vq_callback_t **callbacks;
  677. const char **names;
  678. struct virtqueue **vqs;
  679. num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
  680. vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL);
  681. callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL);
  682. names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL);
  683. if (!callbacks || !vqs || !names) {
  684. err = -ENOMEM;
  685. goto out;
  686. }
  687. callbacks[0] = virtscsi_ctrl_done;
  688. callbacks[1] = virtscsi_event_done;
  689. names[0] = "control";
  690. names[1] = "event";
  691. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
  692. callbacks[i] = virtscsi_req_done;
  693. names[i] = "request";
  694. }
  695. /* Discover virtqueues and write information to configuration. */
  696. err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
  697. if (err)
  698. goto out;
  699. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  700. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  701. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
  702. virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
  703. vqs[i]);
  704. virtscsi_set_affinity(vscsi, true);
  705. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  706. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  707. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  708. virtscsi_kick_event_all(vscsi);
  709. err = 0;
  710. out:
  711. kfree(names);
  712. kfree(callbacks);
  713. kfree(vqs);
  714. if (err)
  715. virtscsi_remove_vqs(vdev);
  716. return err;
  717. }
  718. static int virtscsi_probe(struct virtio_device *vdev)
  719. {
  720. struct Scsi_Host *shost;
  721. struct virtio_scsi *vscsi;
  722. int err, host_prot;
  723. u32 sg_elems, num_targets;
  724. u32 cmd_per_lun;
  725. u32 num_queues;
  726. struct scsi_host_template *hostt;
  727. /* We need to know how many queues before we allocate. */
  728. num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
  729. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  730. if (num_queues == 1)
  731. hostt = &virtscsi_host_template_single;
  732. else
  733. hostt = &virtscsi_host_template_multi;
  734. shost = scsi_host_alloc(hostt,
  735. sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
  736. if (!shost)
  737. return -ENOMEM;
  738. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  739. shost->sg_tablesize = sg_elems;
  740. vscsi = shost_priv(shost);
  741. vscsi->vdev = vdev;
  742. vscsi->num_queues = num_queues;
  743. vdev->priv = shost;
  744. err = virtscsi_init(vdev, vscsi);
  745. if (err)
  746. goto virtscsi_init_failed;
  747. vscsi->nb.notifier_call = &virtscsi_cpu_callback;
  748. err = register_hotcpu_notifier(&vscsi->nb);
  749. if (err) {
  750. pr_err("registering cpu notifier failed\n");
  751. goto scsi_add_host_failed;
  752. }
  753. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  754. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  755. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  756. /* LUNs > 256 are reported with format 1, so they go in the range
  757. * 16640-32767.
  758. */
  759. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  760. shost->max_id = num_targets;
  761. shost->max_channel = 0;
  762. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  763. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
  764. host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
  765. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
  766. SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
  767. scsi_host_set_prot(shost, host_prot);
  768. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  769. }
  770. err = scsi_add_host(shost, &vdev->dev);
  771. if (err)
  772. goto scsi_add_host_failed;
  773. /*
  774. * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
  775. * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
  776. */
  777. return 0;
  778. scsi_add_host_failed:
  779. vdev->config->del_vqs(vdev);
  780. virtscsi_init_failed:
  781. scsi_host_put(shost);
  782. return err;
  783. }
  784. static void virtscsi_remove(struct virtio_device *vdev)
  785. {
  786. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  787. struct virtio_scsi *vscsi = shost_priv(shost);
  788. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  789. virtscsi_cancel_event_work(vscsi);
  790. scsi_remove_host(shost);
  791. unregister_hotcpu_notifier(&vscsi->nb);
  792. virtscsi_remove_vqs(vdev);
  793. scsi_host_put(shost);
  794. }
  795. #ifdef CONFIG_PM_SLEEP
  796. static int virtscsi_freeze(struct virtio_device *vdev)
  797. {
  798. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  799. struct virtio_scsi *vscsi = shost_priv(sh);
  800. unregister_hotcpu_notifier(&vscsi->nb);
  801. virtscsi_remove_vqs(vdev);
  802. return 0;
  803. }
  804. static int virtscsi_restore(struct virtio_device *vdev)
  805. {
  806. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  807. struct virtio_scsi *vscsi = shost_priv(sh);
  808. int err;
  809. err = virtscsi_init(vdev, vscsi);
  810. if (err)
  811. return err;
  812. err = register_hotcpu_notifier(&vscsi->nb);
  813. if (err)
  814. vdev->config->del_vqs(vdev);
  815. return err;
  816. }
  817. #endif
  818. static struct virtio_device_id id_table[] = {
  819. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  820. { 0 },
  821. };
  822. static unsigned int features[] = {
  823. VIRTIO_SCSI_F_HOTPLUG,
  824. VIRTIO_SCSI_F_CHANGE,
  825. VIRTIO_SCSI_F_T10_PI,
  826. };
  827. static struct virtio_driver virtio_scsi_driver = {
  828. .feature_table = features,
  829. .feature_table_size = ARRAY_SIZE(features),
  830. .driver.name = KBUILD_MODNAME,
  831. .driver.owner = THIS_MODULE,
  832. .id_table = id_table,
  833. .probe = virtscsi_probe,
  834. .scan = virtscsi_scan,
  835. #ifdef CONFIG_PM_SLEEP
  836. .freeze = virtscsi_freeze,
  837. .restore = virtscsi_restore,
  838. #endif
  839. .remove = virtscsi_remove,
  840. };
  841. static int __init init(void)
  842. {
  843. int ret = -ENOMEM;
  844. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  845. if (!virtscsi_cmd_cache) {
  846. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  847. goto error;
  848. }
  849. virtscsi_cmd_pool =
  850. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  851. virtscsi_cmd_cache);
  852. if (!virtscsi_cmd_pool) {
  853. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  854. goto error;
  855. }
  856. ret = register_virtio_driver(&virtio_scsi_driver);
  857. if (ret < 0)
  858. goto error;
  859. return 0;
  860. error:
  861. if (virtscsi_cmd_pool) {
  862. mempool_destroy(virtscsi_cmd_pool);
  863. virtscsi_cmd_pool = NULL;
  864. }
  865. if (virtscsi_cmd_cache) {
  866. kmem_cache_destroy(virtscsi_cmd_cache);
  867. virtscsi_cmd_cache = NULL;
  868. }
  869. return ret;
  870. }
  871. static void __exit fini(void)
  872. {
  873. unregister_virtio_driver(&virtio_scsi_driver);
  874. mempool_destroy(virtscsi_cmd_pool);
  875. kmem_cache_destroy(virtscsi_cmd_cache);
  876. }
  877. module_init(init);
  878. module_exit(fini);
  879. MODULE_DEVICE_TABLE(virtio, id_table);
  880. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  881. MODULE_LICENSE("GPL");