virtio_scsi.c 29 KB

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