perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / scsi / virtio_scsi.c
blob13f1b3b9923a8becb47b3dc59a77a8f5780816fb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Virtio SCSI HBA driver
5 * Copyright IBM Corp. 2010
6 * Copyright Red Hat, Inc. 2011
8 * Authors:
9 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/mempool.h>
18 #include <linux/interrupt.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 <scsi/scsi_devinfo.h>
30 #include <linux/seqlock.h>
31 #include <linux/blk-mq-virtio.h>
33 #define VIRTIO_SCSI_MEMPOOL_SZ 64
34 #define VIRTIO_SCSI_EVENT_LEN 8
35 #define VIRTIO_SCSI_VQ_BASE 2
37 /* Command queue element */
38 struct virtio_scsi_cmd {
39 struct scsi_cmnd *sc;
40 struct completion *comp;
41 union {
42 struct virtio_scsi_cmd_req cmd;
43 struct virtio_scsi_cmd_req_pi cmd_pi;
44 struct virtio_scsi_ctrl_tmf_req tmf;
45 struct virtio_scsi_ctrl_an_req an;
46 } req;
47 union {
48 struct virtio_scsi_cmd_resp cmd;
49 struct virtio_scsi_ctrl_tmf_resp tmf;
50 struct virtio_scsi_ctrl_an_resp an;
51 struct virtio_scsi_event evt;
52 } resp;
53 } ____cacheline_aligned_in_smp;
55 struct virtio_scsi_event_node {
56 struct virtio_scsi *vscsi;
57 struct virtio_scsi_event event;
58 struct work_struct work;
61 struct virtio_scsi_vq {
62 /* Protects vq */
63 spinlock_t vq_lock;
65 struct virtqueue *vq;
68 /* Driver instance state */
69 struct virtio_scsi {
70 struct virtio_device *vdev;
72 /* Get some buffers ready for event vq */
73 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
75 u32 num_queues;
77 /* If the affinity hint is set for virtqueues */
78 bool affinity_hint_set;
80 struct hlist_node node;
82 /* Protected by event_vq lock */
83 bool stop_events;
85 struct virtio_scsi_vq ctrl_vq;
86 struct virtio_scsi_vq event_vq;
87 struct virtio_scsi_vq req_vqs[];
90 static struct kmem_cache *virtscsi_cmd_cache;
91 static mempool_t *virtscsi_cmd_pool;
93 static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
95 return vdev->priv;
98 static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
100 if (resid)
101 scsi_set_resid(sc, resid);
105 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
107 * Called with vq_lock held.
109 static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
111 struct virtio_scsi_cmd *cmd = buf;
112 struct scsi_cmnd *sc = cmd->sc;
113 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
115 dev_dbg(&sc->device->sdev_gendev,
116 "cmd %p response %u status %#02x sense_len %u\n",
117 sc, resp->response, resp->status, resp->sense_len);
119 sc->result = resp->status;
120 virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
121 switch (resp->response) {
122 case VIRTIO_SCSI_S_OK:
123 set_host_byte(sc, DID_OK);
124 break;
125 case VIRTIO_SCSI_S_OVERRUN:
126 set_host_byte(sc, DID_ERROR);
127 break;
128 case VIRTIO_SCSI_S_ABORTED:
129 set_host_byte(sc, DID_ABORT);
130 break;
131 case VIRTIO_SCSI_S_BAD_TARGET:
132 set_host_byte(sc, DID_BAD_TARGET);
133 break;
134 case VIRTIO_SCSI_S_RESET:
135 set_host_byte(sc, DID_RESET);
136 break;
137 case VIRTIO_SCSI_S_BUSY:
138 set_host_byte(sc, DID_BUS_BUSY);
139 break;
140 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
141 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
142 break;
143 case VIRTIO_SCSI_S_TARGET_FAILURE:
144 set_host_byte(sc, DID_TARGET_FAILURE);
145 break;
146 case VIRTIO_SCSI_S_NEXUS_FAILURE:
147 set_host_byte(sc, DID_NEXUS_FAILURE);
148 break;
149 default:
150 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
151 resp->response);
152 /* fall through */
153 case VIRTIO_SCSI_S_FAILURE:
154 set_host_byte(sc, DID_ERROR);
155 break;
158 WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
159 VIRTIO_SCSI_SENSE_SIZE);
160 if (sc->sense_buffer) {
161 memcpy(sc->sense_buffer, resp->sense,
162 min_t(u32,
163 virtio32_to_cpu(vscsi->vdev, resp->sense_len),
164 VIRTIO_SCSI_SENSE_SIZE));
165 if (resp->sense_len)
166 set_driver_byte(sc, DRIVER_SENSE);
169 sc->scsi_done(sc);
172 static void virtscsi_vq_done(struct virtio_scsi *vscsi,
173 struct virtio_scsi_vq *virtscsi_vq,
174 void (*fn)(struct virtio_scsi *vscsi, void *buf))
176 void *buf;
177 unsigned int len;
178 unsigned long flags;
179 struct virtqueue *vq = virtscsi_vq->vq;
181 spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
182 do {
183 virtqueue_disable_cb(vq);
184 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
185 fn(vscsi, buf);
187 if (unlikely(virtqueue_is_broken(vq)))
188 break;
189 } while (!virtqueue_enable_cb(vq));
190 spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
193 static void virtscsi_req_done(struct virtqueue *vq)
195 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
196 struct virtio_scsi *vscsi = shost_priv(sh);
197 int index = vq->index - VIRTIO_SCSI_VQ_BASE;
198 struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
200 virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
203 static void virtscsi_poll_requests(struct virtio_scsi *vscsi)
205 int i, num_vqs;
207 num_vqs = vscsi->num_queues;
208 for (i = 0; i < num_vqs; i++)
209 virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
210 virtscsi_complete_cmd);
213 static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
215 struct virtio_scsi_cmd *cmd = buf;
217 if (cmd->comp)
218 complete(cmd->comp);
221 static void virtscsi_ctrl_done(struct virtqueue *vq)
223 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
224 struct virtio_scsi *vscsi = shost_priv(sh);
226 virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
229 static void virtscsi_handle_event(struct work_struct *work);
231 static int virtscsi_kick_event(struct virtio_scsi *vscsi,
232 struct virtio_scsi_event_node *event_node)
234 int err;
235 struct scatterlist sg;
236 unsigned long flags;
238 INIT_WORK(&event_node->work, virtscsi_handle_event);
239 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
241 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
243 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
244 GFP_ATOMIC);
245 if (!err)
246 virtqueue_kick(vscsi->event_vq.vq);
248 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
250 return err;
253 static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
255 int i;
257 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
258 vscsi->event_list[i].vscsi = vscsi;
259 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
262 return 0;
265 static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
267 int i;
269 /* Stop scheduling work before calling cancel_work_sync. */
270 spin_lock_irq(&vscsi->event_vq.vq_lock);
271 vscsi->stop_events = true;
272 spin_unlock_irq(&vscsi->event_vq.vq_lock);
274 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
275 cancel_work_sync(&vscsi->event_list[i].work);
278 static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
279 struct virtio_scsi_event *event)
281 struct scsi_device *sdev;
282 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
283 unsigned int target = event->lun[1];
284 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
286 switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
287 case VIRTIO_SCSI_EVT_RESET_RESCAN:
288 scsi_add_device(shost, 0, target, lun);
289 break;
290 case VIRTIO_SCSI_EVT_RESET_REMOVED:
291 sdev = scsi_device_lookup(shost, 0, target, lun);
292 if (sdev) {
293 scsi_remove_device(sdev);
294 scsi_device_put(sdev);
295 } else {
296 pr_err("SCSI device %d 0 %d %d not found\n",
297 shost->host_no, target, lun);
299 break;
300 default:
301 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
305 static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
306 struct virtio_scsi_event *event)
308 struct scsi_device *sdev;
309 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
310 unsigned int target = event->lun[1];
311 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
312 u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
313 u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
315 sdev = scsi_device_lookup(shost, 0, target, lun);
316 if (!sdev) {
317 pr_err("SCSI device %d 0 %d %d not found\n",
318 shost->host_no, target, lun);
319 return;
322 /* Handle "Parameters changed", "Mode parameters changed", and
323 "Capacity data has changed". */
324 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
325 scsi_rescan_device(&sdev->sdev_gendev);
327 scsi_device_put(sdev);
330 static void virtscsi_handle_event(struct work_struct *work)
332 struct virtio_scsi_event_node *event_node =
333 container_of(work, struct virtio_scsi_event_node, work);
334 struct virtio_scsi *vscsi = event_node->vscsi;
335 struct virtio_scsi_event *event = &event_node->event;
337 if (event->event &
338 cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
339 event->event &= ~cpu_to_virtio32(vscsi->vdev,
340 VIRTIO_SCSI_T_EVENTS_MISSED);
341 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
344 switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
345 case VIRTIO_SCSI_T_NO_EVENT:
346 break;
347 case VIRTIO_SCSI_T_TRANSPORT_RESET:
348 virtscsi_handle_transport_reset(vscsi, event);
349 break;
350 case VIRTIO_SCSI_T_PARAM_CHANGE:
351 virtscsi_handle_param_change(vscsi, event);
352 break;
353 default:
354 pr_err("Unsupport virtio scsi event %x\n", event->event);
356 virtscsi_kick_event(vscsi, event_node);
359 static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
361 struct virtio_scsi_event_node *event_node = buf;
363 if (!vscsi->stop_events)
364 queue_work(system_freezable_wq, &event_node->work);
367 static void virtscsi_event_done(struct virtqueue *vq)
369 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
370 struct virtio_scsi *vscsi = shost_priv(sh);
372 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
376 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
377 * @vq : the struct virtqueue we're talking about
378 * @cmd : command structure
379 * @req_size : size of the request buffer
380 * @resp_size : size of the response buffer
382 static int virtscsi_add_cmd(struct virtqueue *vq,
383 struct virtio_scsi_cmd *cmd,
384 size_t req_size, size_t resp_size)
386 struct scsi_cmnd *sc = cmd->sc;
387 struct scatterlist *sgs[6], req, resp;
388 struct sg_table *out, *in;
389 unsigned out_num = 0, in_num = 0;
391 out = in = NULL;
393 if (sc && sc->sc_data_direction != DMA_NONE) {
394 if (sc->sc_data_direction != DMA_FROM_DEVICE)
395 out = &sc->sdb.table;
396 if (sc->sc_data_direction != DMA_TO_DEVICE)
397 in = &sc->sdb.table;
400 /* Request header. */
401 sg_init_one(&req, &cmd->req, req_size);
402 sgs[out_num++] = &req;
404 /* Data-out buffer. */
405 if (out) {
406 /* Place WRITE protection SGLs before Data OUT payload */
407 if (scsi_prot_sg_count(sc))
408 sgs[out_num++] = scsi_prot_sglist(sc);
409 sgs[out_num++] = out->sgl;
412 /* Response header. */
413 sg_init_one(&resp, &cmd->resp, resp_size);
414 sgs[out_num + in_num++] = &resp;
416 /* Data-in buffer */
417 if (in) {
418 /* Place READ protection SGLs before Data IN payload */
419 if (scsi_prot_sg_count(sc))
420 sgs[out_num + in_num++] = scsi_prot_sglist(sc);
421 sgs[out_num + in_num++] = in->sgl;
424 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
427 static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
428 struct virtio_scsi_cmd *cmd,
429 size_t req_size, size_t resp_size)
431 unsigned long flags;
432 int err;
433 bool needs_kick = false;
435 spin_lock_irqsave(&vq->vq_lock, flags);
436 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
437 if (!err)
438 needs_kick = virtqueue_kick_prepare(vq->vq);
440 spin_unlock_irqrestore(&vq->vq_lock, flags);
442 if (needs_kick)
443 virtqueue_notify(vq->vq);
444 return err;
447 static void virtio_scsi_init_hdr(struct virtio_device *vdev,
448 struct virtio_scsi_cmd_req *cmd,
449 struct scsi_cmnd *sc)
451 cmd->lun[0] = 1;
452 cmd->lun[1] = sc->device->id;
453 cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
454 cmd->lun[3] = sc->device->lun & 0xff;
455 cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
456 cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
457 cmd->prio = 0;
458 cmd->crn = 0;
461 #ifdef CONFIG_BLK_DEV_INTEGRITY
462 static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
463 struct virtio_scsi_cmd_req_pi *cmd_pi,
464 struct scsi_cmnd *sc)
466 struct request *rq = sc->request;
467 struct blk_integrity *bi;
469 virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
471 if (!rq || !scsi_prot_sg_count(sc))
472 return;
474 bi = blk_get_integrity(rq->rq_disk);
476 if (sc->sc_data_direction == DMA_TO_DEVICE)
477 cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
478 bio_integrity_bytes(bi,
479 blk_rq_sectors(rq)));
480 else if (sc->sc_data_direction == DMA_FROM_DEVICE)
481 cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
482 bio_integrity_bytes(bi,
483 blk_rq_sectors(rq)));
485 #endif
487 static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
488 struct scsi_cmnd *sc)
490 u32 tag = blk_mq_unique_tag(sc->request);
491 u16 hwq = blk_mq_unique_tag_to_hwq(tag);
493 return &vscsi->req_vqs[hwq];
496 static int virtscsi_queuecommand(struct Scsi_Host *shost,
497 struct scsi_cmnd *sc)
499 struct virtio_scsi *vscsi = shost_priv(shost);
500 struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
501 struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
502 unsigned long flags;
503 int req_size;
504 int ret;
506 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
508 /* TODO: check feature bit and fail if unsupported? */
509 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
511 dev_dbg(&sc->device->sdev_gendev,
512 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
514 cmd->sc = sc;
516 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
518 #ifdef CONFIG_BLK_DEV_INTEGRITY
519 if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
520 virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
521 memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
522 req_size = sizeof(cmd->req.cmd_pi);
523 } else
524 #endif
526 virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
527 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
528 req_size = sizeof(cmd->req.cmd);
531 ret = virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd));
532 if (ret == -EIO) {
533 cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
534 spin_lock_irqsave(&req_vq->vq_lock, flags);
535 virtscsi_complete_cmd(vscsi, cmd);
536 spin_unlock_irqrestore(&req_vq->vq_lock, flags);
537 } else if (ret != 0) {
538 return SCSI_MLQUEUE_HOST_BUSY;
540 return 0;
543 static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
545 DECLARE_COMPLETION_ONSTACK(comp);
546 int ret = FAILED;
548 cmd->comp = &comp;
549 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
550 sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
551 goto out;
553 wait_for_completion(&comp);
554 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
555 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
556 ret = SUCCESS;
559 * The spec guarantees that all requests related to the TMF have
560 * been completed, but the callback might not have run yet if
561 * we're using independent interrupts (e.g. MSI). Poll the
562 * virtqueues once.
564 * In the abort case, sc->scsi_done will do nothing, because
565 * the block layer must have detected a timeout and as a result
566 * REQ_ATOM_COMPLETE has been set.
568 virtscsi_poll_requests(vscsi);
570 out:
571 mempool_free(cmd, virtscsi_cmd_pool);
572 return ret;
575 static int virtscsi_device_reset(struct scsi_cmnd *sc)
577 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
578 struct virtio_scsi_cmd *cmd;
580 sdev_printk(KERN_INFO, sc->device, "device reset\n");
581 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
582 if (!cmd)
583 return FAILED;
585 memset(cmd, 0, sizeof(*cmd));
586 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
587 .type = VIRTIO_SCSI_T_TMF,
588 .subtype = cpu_to_virtio32(vscsi->vdev,
589 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
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,
595 return virtscsi_tmf(vscsi, cmd);
598 static int virtscsi_device_alloc(struct scsi_device *sdevice)
601 * Passed through SCSI targets (e.g. with qemu's 'scsi-block')
602 * may have transfer limits which come from the host SCSI
603 * controller or something on the host side other than the
604 * target itself.
606 * To make this work properly, the hypervisor can adjust the
607 * target's VPD information to advertise these limits. But
608 * for that to work, the guest has to look at the VPD pages,
609 * which we won't do by default if it is an SPC-2 device, even
610 * if it does actually support it.
612 * So, set the blist to always try to read the VPD pages.
614 sdevice->sdev_bflags = BLIST_TRY_VPD_PAGES;
616 return 0;
621 * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
622 * @sdev: Virtscsi target whose queue depth to change
623 * @qdepth: New queue depth
625 static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
627 struct Scsi_Host *shost = sdev->host;
628 int max_depth = shost->cmd_per_lun;
630 return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
633 static int virtscsi_abort(struct scsi_cmnd *sc)
635 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
636 struct virtio_scsi_cmd *cmd;
638 scmd_printk(KERN_INFO, sc, "abort\n");
639 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
640 if (!cmd)
641 return FAILED;
643 memset(cmd, 0, sizeof(*cmd));
644 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
645 .type = VIRTIO_SCSI_T_TMF,
646 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
647 .lun[0] = 1,
648 .lun[1] = sc->device->id,
649 .lun[2] = (sc->device->lun >> 8) | 0x40,
650 .lun[3] = sc->device->lun & 0xff,
651 .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
653 return virtscsi_tmf(vscsi, cmd);
656 static int virtscsi_map_queues(struct Scsi_Host *shost)
658 struct virtio_scsi *vscsi = shost_priv(shost);
659 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
661 return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
665 * The host guarantees to respond to each command, although I/O
666 * latencies might be higher than on bare metal. Reset the timer
667 * unconditionally to give the host a chance to perform EH.
669 static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
671 return BLK_EH_RESET_TIMER;
674 static struct scsi_host_template virtscsi_host_template = {
675 .module = THIS_MODULE,
676 .name = "Virtio SCSI HBA",
677 .proc_name = "virtio_scsi",
678 .this_id = -1,
679 .cmd_size = sizeof(struct virtio_scsi_cmd),
680 .queuecommand = virtscsi_queuecommand,
681 .change_queue_depth = virtscsi_change_queue_depth,
682 .eh_abort_handler = virtscsi_abort,
683 .eh_device_reset_handler = virtscsi_device_reset,
684 .eh_timed_out = virtscsi_eh_timed_out,
685 .slave_alloc = virtscsi_device_alloc,
687 .dma_boundary = UINT_MAX,
688 .map_queues = virtscsi_map_queues,
689 .track_queue_depth = 1,
690 .force_blk_mq = 1,
693 #define virtscsi_config_get(vdev, fld) \
694 ({ \
695 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
696 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
697 __val; \
700 #define virtscsi_config_set(vdev, fld, val) \
701 do { \
702 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
703 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
704 } while(0)
706 static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
707 struct virtqueue *vq)
709 spin_lock_init(&virtscsi_vq->vq_lock);
710 virtscsi_vq->vq = vq;
713 static void virtscsi_remove_vqs(struct virtio_device *vdev)
715 /* Stop all the virtqueues. */
716 vdev->config->reset(vdev);
717 vdev->config->del_vqs(vdev);
720 static int virtscsi_init(struct virtio_device *vdev,
721 struct virtio_scsi *vscsi)
723 int err;
724 u32 i;
725 u32 num_vqs;
726 vq_callback_t **callbacks;
727 const char **names;
728 struct virtqueue **vqs;
729 struct irq_affinity desc = { .pre_vectors = 2 };
731 num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
732 vqs = kmalloc_array(num_vqs, sizeof(struct virtqueue *), GFP_KERNEL);
733 callbacks = kmalloc_array(num_vqs, sizeof(vq_callback_t *),
734 GFP_KERNEL);
735 names = kmalloc_array(num_vqs, sizeof(char *), GFP_KERNEL);
737 if (!callbacks || !vqs || !names) {
738 err = -ENOMEM;
739 goto out;
742 callbacks[0] = virtscsi_ctrl_done;
743 callbacks[1] = virtscsi_event_done;
744 names[0] = "control";
745 names[1] = "event";
746 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
747 callbacks[i] = virtscsi_req_done;
748 names[i] = "request";
751 /* Discover virtqueues and write information to configuration. */
752 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
753 if (err)
754 goto out;
756 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
757 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
758 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
759 virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
760 vqs[i]);
762 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
763 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
765 err = 0;
767 out:
768 kfree(names);
769 kfree(callbacks);
770 kfree(vqs);
771 if (err)
772 virtscsi_remove_vqs(vdev);
773 return err;
776 static int virtscsi_probe(struct virtio_device *vdev)
778 struct Scsi_Host *shost;
779 struct virtio_scsi *vscsi;
780 int err;
781 u32 sg_elems, num_targets;
782 u32 cmd_per_lun;
783 u32 num_queues;
785 if (!vdev->config->get) {
786 dev_err(&vdev->dev, "%s failure: config access disabled\n",
787 __func__);
788 return -EINVAL;
791 /* We need to know how many queues before we allocate. */
792 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
793 num_queues = min_t(unsigned int, nr_cpu_ids, num_queues);
795 num_targets = virtscsi_config_get(vdev, max_target) + 1;
797 shost = scsi_host_alloc(&virtscsi_host_template,
798 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
799 if (!shost)
800 return -ENOMEM;
802 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
803 shost->sg_tablesize = sg_elems;
804 vscsi = shost_priv(shost);
805 vscsi->vdev = vdev;
806 vscsi->num_queues = num_queues;
807 vdev->priv = shost;
809 err = virtscsi_init(vdev, vscsi);
810 if (err)
811 goto virtscsi_init_failed;
813 shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
815 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
816 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
817 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
819 /* LUNs > 256 are reported with format 1, so they go in the range
820 * 16640-32767.
822 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
823 shost->max_id = num_targets;
824 shost->max_channel = 0;
825 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
826 shost->nr_hw_queues = num_queues;
828 #ifdef CONFIG_BLK_DEV_INTEGRITY
829 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
830 int host_prot;
832 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
833 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
834 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
836 scsi_host_set_prot(shost, host_prot);
837 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
839 #endif
841 err = scsi_add_host(shost, &vdev->dev);
842 if (err)
843 goto scsi_add_host_failed;
845 virtio_device_ready(vdev);
847 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
848 virtscsi_kick_event_all(vscsi);
850 scsi_scan_host(shost);
851 return 0;
853 scsi_add_host_failed:
854 vdev->config->del_vqs(vdev);
855 virtscsi_init_failed:
856 scsi_host_put(shost);
857 return err;
860 static void virtscsi_remove(struct virtio_device *vdev)
862 struct Scsi_Host *shost = virtio_scsi_host(vdev);
863 struct virtio_scsi *vscsi = shost_priv(shost);
865 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
866 virtscsi_cancel_event_work(vscsi);
868 scsi_remove_host(shost);
869 virtscsi_remove_vqs(vdev);
870 scsi_host_put(shost);
873 #ifdef CONFIG_PM_SLEEP
874 static int virtscsi_freeze(struct virtio_device *vdev)
876 virtscsi_remove_vqs(vdev);
877 return 0;
880 static int virtscsi_restore(struct virtio_device *vdev)
882 struct Scsi_Host *sh = virtio_scsi_host(vdev);
883 struct virtio_scsi *vscsi = shost_priv(sh);
884 int err;
886 err = virtscsi_init(vdev, vscsi);
887 if (err)
888 return err;
890 virtio_device_ready(vdev);
892 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
893 virtscsi_kick_event_all(vscsi);
895 return err;
897 #endif
899 static struct virtio_device_id id_table[] = {
900 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
901 { 0 },
904 static unsigned int features[] = {
905 VIRTIO_SCSI_F_HOTPLUG,
906 VIRTIO_SCSI_F_CHANGE,
907 #ifdef CONFIG_BLK_DEV_INTEGRITY
908 VIRTIO_SCSI_F_T10_PI,
909 #endif
912 static struct virtio_driver virtio_scsi_driver = {
913 .feature_table = features,
914 .feature_table_size = ARRAY_SIZE(features),
915 .driver.name = KBUILD_MODNAME,
916 .driver.owner = THIS_MODULE,
917 .id_table = id_table,
918 .probe = virtscsi_probe,
919 #ifdef CONFIG_PM_SLEEP
920 .freeze = virtscsi_freeze,
921 .restore = virtscsi_restore,
922 #endif
923 .remove = virtscsi_remove,
926 static int __init init(void)
928 int ret = -ENOMEM;
930 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
931 if (!virtscsi_cmd_cache) {
932 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
933 goto error;
937 virtscsi_cmd_pool =
938 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
939 virtscsi_cmd_cache);
940 if (!virtscsi_cmd_pool) {
941 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
942 goto error;
944 ret = register_virtio_driver(&virtio_scsi_driver);
945 if (ret < 0)
946 goto error;
948 return 0;
950 error:
951 if (virtscsi_cmd_pool) {
952 mempool_destroy(virtscsi_cmd_pool);
953 virtscsi_cmd_pool = NULL;
955 if (virtscsi_cmd_cache) {
956 kmem_cache_destroy(virtscsi_cmd_cache);
957 virtscsi_cmd_cache = NULL;
959 return ret;
962 static void __exit fini(void)
964 unregister_virtio_driver(&virtio_scsi_driver);
965 mempool_destroy(virtscsi_cmd_pool);
966 kmem_cache_destroy(virtscsi_cmd_cache);
968 module_init(init);
969 module_exit(fini);
971 MODULE_DEVICE_TABLE(virtio, id_table);
972 MODULE_DESCRIPTION("Virtio SCSI HBA driver");
973 MODULE_LICENSE("GPL");