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