virtio_scsi.c 29 KB

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