virtio_scsi.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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(struct virtio_scsi *vscsi,
  471. struct virtio_scsi_target_state *tgt)
  472. {
  473. struct virtio_scsi_vq *vq;
  474. unsigned long flags;
  475. u32 queue_num;
  476. local_irq_save(flags);
  477. if (atomic_inc_return(&tgt->reqs) > 1) {
  478. unsigned long seq;
  479. do {
  480. seq = read_seqcount_begin(&tgt->tgt_seq);
  481. vq = tgt->req_vq;
  482. } while (read_seqcount_retry(&tgt->tgt_seq, seq));
  483. } else {
  484. /* no writes can be concurrent because of atomic_t */
  485. write_seqcount_begin(&tgt->tgt_seq);
  486. /* keep previous req_vq if a reader just arrived */
  487. if (unlikely(atomic_read(&tgt->reqs) > 1)) {
  488. vq = tgt->req_vq;
  489. goto unlock;
  490. }
  491. queue_num = smp_processor_id();
  492. while (unlikely(queue_num >= vscsi->num_queues))
  493. queue_num -= vscsi->num_queues;
  494. tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
  495. unlock:
  496. write_seqcount_end(&tgt->tgt_seq);
  497. }
  498. local_irq_restore(flags);
  499. return vq;
  500. }
  501. static int virtscsi_queuecommand_multi(struct Scsi_Host *sh,
  502. struct scsi_cmnd *sc)
  503. {
  504. struct virtio_scsi *vscsi = shost_priv(sh);
  505. struct virtio_scsi_target_state *tgt =
  506. scsi_target(sc->device)->hostdata;
  507. struct virtio_scsi_vq *req_vq = virtscsi_pick_vq(vscsi, tgt);
  508. return virtscsi_queuecommand(vscsi, req_vq, sc);
  509. }
  510. static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
  511. {
  512. DECLARE_COMPLETION_ONSTACK(comp);
  513. int ret = FAILED;
  514. cmd->comp = &comp;
  515. if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
  516. sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
  517. goto out;
  518. wait_for_completion(&comp);
  519. if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
  520. cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
  521. ret = SUCCESS;
  522. /*
  523. * The spec guarantees that all requests related to the TMF have
  524. * been completed, but the callback might not have run yet if
  525. * we're using independent interrupts (e.g. MSI). Poll the
  526. * virtqueues once.
  527. *
  528. * In the abort case, sc->scsi_done will do nothing, because
  529. * the block layer must have detected a timeout and as a result
  530. * REQ_ATOM_COMPLETE has been set.
  531. */
  532. virtscsi_poll_requests(vscsi);
  533. out:
  534. mempool_free(cmd, virtscsi_cmd_pool);
  535. return ret;
  536. }
  537. static int virtscsi_device_reset(struct scsi_cmnd *sc)
  538. {
  539. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  540. struct virtio_scsi_cmd *cmd;
  541. sdev_printk(KERN_INFO, sc->device, "device reset\n");
  542. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  543. if (!cmd)
  544. return FAILED;
  545. memset(cmd, 0, sizeof(*cmd));
  546. cmd->sc = sc;
  547. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  548. .type = VIRTIO_SCSI_T_TMF,
  549. .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
  550. .lun[0] = 1,
  551. .lun[1] = sc->device->id,
  552. .lun[2] = (sc->device->lun >> 8) | 0x40,
  553. .lun[3] = sc->device->lun & 0xff,
  554. };
  555. return virtscsi_tmf(vscsi, cmd);
  556. }
  557. /**
  558. * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
  559. * @sdev: Virtscsi target whose queue depth to change
  560. * @qdepth: New queue depth
  561. * @reason: Reason for the queue depth change.
  562. */
  563. static int virtscsi_change_queue_depth(struct scsi_device *sdev,
  564. int qdepth,
  565. int reason)
  566. {
  567. struct Scsi_Host *shost = sdev->host;
  568. int max_depth = shost->cmd_per_lun;
  569. switch (reason) {
  570. case SCSI_QDEPTH_QFULL: /* Drop qdepth in response to BUSY state */
  571. scsi_track_queue_full(sdev, qdepth);
  572. break;
  573. case SCSI_QDEPTH_RAMP_UP: /* Raise qdepth after BUSY state resolved */
  574. case SCSI_QDEPTH_DEFAULT: /* Manual change via sysfs */
  575. scsi_adjust_queue_depth(sdev,
  576. scsi_get_tag_type(sdev),
  577. min(max_depth, qdepth));
  578. break;
  579. default:
  580. return -EOPNOTSUPP;
  581. }
  582. return sdev->queue_depth;
  583. }
  584. static int virtscsi_abort(struct scsi_cmnd *sc)
  585. {
  586. struct virtio_scsi *vscsi = shost_priv(sc->device->host);
  587. struct virtio_scsi_cmd *cmd;
  588. scmd_printk(KERN_INFO, sc, "abort\n");
  589. cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
  590. if (!cmd)
  591. return FAILED;
  592. memset(cmd, 0, sizeof(*cmd));
  593. cmd->sc = sc;
  594. cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
  595. .type = VIRTIO_SCSI_T_TMF,
  596. .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
  597. .lun[0] = 1,
  598. .lun[1] = sc->device->id,
  599. .lun[2] = (sc->device->lun >> 8) | 0x40,
  600. .lun[3] = sc->device->lun & 0xff,
  601. .tag = (unsigned long)sc,
  602. };
  603. return virtscsi_tmf(vscsi, cmd);
  604. }
  605. static int virtscsi_target_alloc(struct scsi_target *starget)
  606. {
  607. struct Scsi_Host *sh = dev_to_shost(starget->dev.parent);
  608. struct virtio_scsi *vscsi = shost_priv(sh);
  609. struct virtio_scsi_target_state *tgt =
  610. kmalloc(sizeof(*tgt), GFP_KERNEL);
  611. if (!tgt)
  612. return -ENOMEM;
  613. seqcount_init(&tgt->tgt_seq);
  614. atomic_set(&tgt->reqs, 0);
  615. tgt->req_vq = &vscsi->req_vqs[0];
  616. starget->hostdata = tgt;
  617. return 0;
  618. }
  619. static void virtscsi_target_destroy(struct scsi_target *starget)
  620. {
  621. struct virtio_scsi_target_state *tgt = starget->hostdata;
  622. kfree(tgt);
  623. }
  624. static struct scsi_host_template virtscsi_host_template_single = {
  625. .module = THIS_MODULE,
  626. .name = "Virtio SCSI HBA",
  627. .proc_name = "virtio_scsi",
  628. .this_id = -1,
  629. .cmd_size = sizeof(struct virtio_scsi_cmd),
  630. .queuecommand = virtscsi_queuecommand_single,
  631. .change_queue_depth = virtscsi_change_queue_depth,
  632. .eh_abort_handler = virtscsi_abort,
  633. .eh_device_reset_handler = virtscsi_device_reset,
  634. .can_queue = 1024,
  635. .dma_boundary = UINT_MAX,
  636. .use_clustering = ENABLE_CLUSTERING,
  637. .target_alloc = virtscsi_target_alloc,
  638. .target_destroy = virtscsi_target_destroy,
  639. };
  640. static struct scsi_host_template virtscsi_host_template_multi = {
  641. .module = THIS_MODULE,
  642. .name = "Virtio SCSI HBA",
  643. .proc_name = "virtio_scsi",
  644. .this_id = -1,
  645. .cmd_size = sizeof(struct virtio_scsi_cmd),
  646. .queuecommand = virtscsi_queuecommand_multi,
  647. .change_queue_depth = virtscsi_change_queue_depth,
  648. .eh_abort_handler = virtscsi_abort,
  649. .eh_device_reset_handler = virtscsi_device_reset,
  650. .can_queue = 1024,
  651. .dma_boundary = UINT_MAX,
  652. .use_clustering = ENABLE_CLUSTERING,
  653. .target_alloc = virtscsi_target_alloc,
  654. .target_destroy = virtscsi_target_destroy,
  655. };
  656. #define virtscsi_config_get(vdev, fld) \
  657. ({ \
  658. typeof(((struct virtio_scsi_config *)0)->fld) __val; \
  659. virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
  660. __val; \
  661. })
  662. #define virtscsi_config_set(vdev, fld, val) \
  663. do { \
  664. typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
  665. virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
  666. } while(0)
  667. static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  668. {
  669. int i;
  670. int cpu;
  671. /* In multiqueue mode, when the number of cpu is equal
  672. * to the number of request queues, we let the qeueues
  673. * to be private to one cpu by setting the affinity hint
  674. * to eliminate the contention.
  675. */
  676. if ((vscsi->num_queues == 1 ||
  677. vscsi->num_queues != num_online_cpus()) && affinity) {
  678. if (vscsi->affinity_hint_set)
  679. affinity = false;
  680. else
  681. return;
  682. }
  683. if (affinity) {
  684. i = 0;
  685. for_each_online_cpu(cpu) {
  686. virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu);
  687. i++;
  688. }
  689. vscsi->affinity_hint_set = true;
  690. } else {
  691. for (i = 0; i < vscsi->num_queues; i++) {
  692. if (!vscsi->req_vqs[i].vq)
  693. continue;
  694. virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
  695. }
  696. vscsi->affinity_hint_set = false;
  697. }
  698. }
  699. static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
  700. {
  701. get_online_cpus();
  702. __virtscsi_set_affinity(vscsi, affinity);
  703. put_online_cpus();
  704. }
  705. static int virtscsi_cpu_callback(struct notifier_block *nfb,
  706. unsigned long action, void *hcpu)
  707. {
  708. struct virtio_scsi *vscsi = container_of(nfb, struct virtio_scsi, nb);
  709. switch(action) {
  710. case CPU_ONLINE:
  711. case CPU_ONLINE_FROZEN:
  712. case CPU_DEAD:
  713. case CPU_DEAD_FROZEN:
  714. __virtscsi_set_affinity(vscsi, true);
  715. break;
  716. default:
  717. break;
  718. }
  719. return NOTIFY_OK;
  720. }
  721. static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
  722. struct virtqueue *vq)
  723. {
  724. spin_lock_init(&virtscsi_vq->vq_lock);
  725. virtscsi_vq->vq = vq;
  726. }
  727. static void virtscsi_scan(struct virtio_device *vdev)
  728. {
  729. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  730. struct virtio_scsi *vscsi = shost_priv(shost);
  731. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  732. virtscsi_kick_event_all(vscsi);
  733. scsi_scan_host(shost);
  734. }
  735. static void virtscsi_remove_vqs(struct virtio_device *vdev)
  736. {
  737. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  738. struct virtio_scsi *vscsi = shost_priv(sh);
  739. virtscsi_set_affinity(vscsi, false);
  740. /* Stop all the virtqueues. */
  741. vdev->config->reset(vdev);
  742. vdev->config->del_vqs(vdev);
  743. }
  744. static int virtscsi_init(struct virtio_device *vdev,
  745. struct virtio_scsi *vscsi)
  746. {
  747. int err;
  748. u32 i;
  749. u32 num_vqs;
  750. vq_callback_t **callbacks;
  751. const char **names;
  752. struct virtqueue **vqs;
  753. num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
  754. vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL);
  755. callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL);
  756. names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL);
  757. if (!callbacks || !vqs || !names) {
  758. err = -ENOMEM;
  759. goto out;
  760. }
  761. callbacks[0] = virtscsi_ctrl_done;
  762. callbacks[1] = virtscsi_event_done;
  763. names[0] = "control";
  764. names[1] = "event";
  765. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
  766. callbacks[i] = virtscsi_req_done;
  767. names[i] = "request";
  768. }
  769. /* Discover virtqueues and write information to configuration. */
  770. err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
  771. if (err)
  772. goto out;
  773. virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
  774. virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
  775. for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
  776. virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
  777. vqs[i]);
  778. virtscsi_set_affinity(vscsi, true);
  779. virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
  780. virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
  781. err = 0;
  782. out:
  783. kfree(names);
  784. kfree(callbacks);
  785. kfree(vqs);
  786. if (err)
  787. virtscsi_remove_vqs(vdev);
  788. return err;
  789. }
  790. static int virtscsi_probe(struct virtio_device *vdev)
  791. {
  792. struct Scsi_Host *shost;
  793. struct virtio_scsi *vscsi;
  794. int err, host_prot;
  795. u32 sg_elems, num_targets;
  796. u32 cmd_per_lun;
  797. u32 num_queues;
  798. struct scsi_host_template *hostt;
  799. /* We need to know how many queues before we allocate. */
  800. num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
  801. num_targets = virtscsi_config_get(vdev, max_target) + 1;
  802. if (num_queues == 1)
  803. hostt = &virtscsi_host_template_single;
  804. else
  805. hostt = &virtscsi_host_template_multi;
  806. shost = scsi_host_alloc(hostt,
  807. sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
  808. if (!shost)
  809. return -ENOMEM;
  810. sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
  811. shost->sg_tablesize = sg_elems;
  812. vscsi = shost_priv(shost);
  813. vscsi->vdev = vdev;
  814. vscsi->num_queues = num_queues;
  815. vdev->priv = shost;
  816. err = virtscsi_init(vdev, vscsi);
  817. if (err)
  818. goto virtscsi_init_failed;
  819. vscsi->nb.notifier_call = &virtscsi_cpu_callback;
  820. err = register_hotcpu_notifier(&vscsi->nb);
  821. if (err) {
  822. pr_err("registering cpu notifier failed\n");
  823. goto scsi_add_host_failed;
  824. }
  825. cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
  826. shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
  827. shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
  828. /* LUNs > 256 are reported with format 1, so they go in the range
  829. * 16640-32767.
  830. */
  831. shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
  832. shost->max_id = num_targets;
  833. shost->max_channel = 0;
  834. shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
  835. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
  836. host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
  837. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
  838. SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
  839. scsi_host_set_prot(shost, host_prot);
  840. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  841. }
  842. err = scsi_add_host(shost, &vdev->dev);
  843. if (err)
  844. goto scsi_add_host_failed;
  845. /*
  846. * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
  847. * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
  848. */
  849. return 0;
  850. scsi_add_host_failed:
  851. vdev->config->del_vqs(vdev);
  852. virtscsi_init_failed:
  853. scsi_host_put(shost);
  854. return err;
  855. }
  856. static void virtscsi_remove(struct virtio_device *vdev)
  857. {
  858. struct Scsi_Host *shost = virtio_scsi_host(vdev);
  859. struct virtio_scsi *vscsi = shost_priv(shost);
  860. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  861. virtscsi_cancel_event_work(vscsi);
  862. scsi_remove_host(shost);
  863. unregister_hotcpu_notifier(&vscsi->nb);
  864. virtscsi_remove_vqs(vdev);
  865. scsi_host_put(shost);
  866. }
  867. #ifdef CONFIG_PM_SLEEP
  868. static int virtscsi_freeze(struct virtio_device *vdev)
  869. {
  870. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  871. struct virtio_scsi *vscsi = shost_priv(sh);
  872. unregister_hotcpu_notifier(&vscsi->nb);
  873. virtscsi_remove_vqs(vdev);
  874. return 0;
  875. }
  876. static int virtscsi_restore(struct virtio_device *vdev)
  877. {
  878. struct Scsi_Host *sh = virtio_scsi_host(vdev);
  879. struct virtio_scsi *vscsi = shost_priv(sh);
  880. int err;
  881. err = virtscsi_init(vdev, vscsi);
  882. if (err)
  883. return err;
  884. err = register_hotcpu_notifier(&vscsi->nb);
  885. if (err) {
  886. vdev->config->del_vqs(vdev);
  887. return err;
  888. }
  889. virtio_device_ready(vdev);
  890. if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
  891. virtscsi_kick_event_all(vscsi);
  892. return err;
  893. }
  894. #endif
  895. static struct virtio_device_id id_table[] = {
  896. { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
  897. { 0 },
  898. };
  899. static unsigned int features[] = {
  900. VIRTIO_SCSI_F_HOTPLUG,
  901. VIRTIO_SCSI_F_CHANGE,
  902. VIRTIO_SCSI_F_T10_PI,
  903. };
  904. static struct virtio_driver virtio_scsi_driver = {
  905. .feature_table = features,
  906. .feature_table_size = ARRAY_SIZE(features),
  907. .driver.name = KBUILD_MODNAME,
  908. .driver.owner = THIS_MODULE,
  909. .id_table = id_table,
  910. .probe = virtscsi_probe,
  911. .scan = virtscsi_scan,
  912. #ifdef CONFIG_PM_SLEEP
  913. .freeze = virtscsi_freeze,
  914. .restore = virtscsi_restore,
  915. #endif
  916. .remove = virtscsi_remove,
  917. };
  918. static int __init init(void)
  919. {
  920. int ret = -ENOMEM;
  921. virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
  922. if (!virtscsi_cmd_cache) {
  923. pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
  924. goto error;
  925. }
  926. virtscsi_cmd_pool =
  927. mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
  928. virtscsi_cmd_cache);
  929. if (!virtscsi_cmd_pool) {
  930. pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
  931. goto error;
  932. }
  933. ret = register_virtio_driver(&virtio_scsi_driver);
  934. if (ret < 0)
  935. goto error;
  936. return 0;
  937. error:
  938. if (virtscsi_cmd_pool) {
  939. mempool_destroy(virtscsi_cmd_pool);
  940. virtscsi_cmd_pool = NULL;
  941. }
  942. if (virtscsi_cmd_cache) {
  943. kmem_cache_destroy(virtscsi_cmd_cache);
  944. virtscsi_cmd_cache = NULL;
  945. }
  946. return ret;
  947. }
  948. static void __exit fini(void)
  949. {
  950. unregister_virtio_driver(&virtio_scsi_driver);
  951. mempool_destroy(virtscsi_cmd_pool);
  952. kmem_cache_destroy(virtscsi_cmd_cache);
  953. }
  954. module_init(init);
  955. module_exit(fini);
  956. MODULE_DEVICE_TABLE(virtio, id_table);
  957. MODULE_DESCRIPTION("Virtio SCSI HBA driver");
  958. MODULE_LICENSE("GPL");