2 * Virtio SCSI HBA driver
4 * Copyright IBM Corp. 2010
5 * Copyright Red Hat, Inc. 2011
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/mempool.h>
21 #include <linux/virtio.h>
22 #include <linux/virtio_ids.h>
23 #include <linux/virtio_config.h>
24 #include <linux/virtio_scsi.h>
25 #include <linux/cpu.h>
26 #include <linux/blkdev.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_cmnd.h>
31 #define VIRTIO_SCSI_MEMPOOL_SZ 64
32 #define VIRTIO_SCSI_EVENT_LEN 8
33 #define VIRTIO_SCSI_VQ_BASE 2
35 /* Command queue element */
36 struct virtio_scsi_cmd
{
38 struct completion
*comp
;
40 struct virtio_scsi_cmd_req cmd
;
41 struct virtio_scsi_cmd_req_pi cmd_pi
;
42 struct virtio_scsi_ctrl_tmf_req tmf
;
43 struct virtio_scsi_ctrl_an_req an
;
46 struct virtio_scsi_cmd_resp cmd
;
47 struct virtio_scsi_ctrl_tmf_resp tmf
;
48 struct virtio_scsi_ctrl_an_resp an
;
49 struct virtio_scsi_event evt
;
51 } ____cacheline_aligned_in_smp
;
53 struct virtio_scsi_event_node
{
54 struct virtio_scsi
*vscsi
;
55 struct virtio_scsi_event event
;
56 struct work_struct work
;
59 struct virtio_scsi_vq
{
67 * Per-target queue state.
69 * This struct holds the data needed by the queue steering policy. When a
70 * target is sent multiple requests, we need to drive them to the same queue so
71 * that FIFO processing order is kept. However, if a target was idle, we can
72 * choose a queue arbitrarily. In this case the queue is chosen according to
73 * the current VCPU, so the driver expects the number of request queues to be
74 * equal to the number of VCPUs. This makes it easy and fast to select the
75 * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
76 * (each virtqueue's affinity is set to the CPU that "owns" the queue).
78 * tgt_lock is held to serialize reading and writing req_vq. Reading req_vq
79 * could be done locklessly, but we do not do it yet.
81 * Decrements of reqs are never concurrent with writes of req_vq: before the
82 * decrement reqs will be != 0; after the decrement the virtqueue completion
83 * routine will not use the req_vq so it can be changed by a new request.
84 * Thus they can happen outside the tgt_lock, provided of course we make reqs
87 struct virtio_scsi_target_state
{
88 /* This spinlock never held at the same time as vq_lock. */
91 /* Count of outstanding requests. */
94 /* Currently active virtqueue for requests sent to this target. */
95 struct virtio_scsi_vq
*req_vq
;
98 /* Driver instance state */
100 struct virtio_device
*vdev
;
102 /* Get some buffers ready for event vq */
103 struct virtio_scsi_event_node event_list
[VIRTIO_SCSI_EVENT_LEN
];
107 /* If the affinity hint is set for virtqueues */
108 bool affinity_hint_set
;
110 /* CPU hotplug notifier */
111 struct notifier_block nb
;
113 struct virtio_scsi_vq ctrl_vq
;
114 struct virtio_scsi_vq event_vq
;
115 struct virtio_scsi_vq req_vqs
[];
118 static struct kmem_cache
*virtscsi_cmd_cache
;
119 static mempool_t
*virtscsi_cmd_pool
;
121 static inline struct Scsi_Host
*virtio_scsi_host(struct virtio_device
*vdev
)
126 static void virtscsi_compute_resid(struct scsi_cmnd
*sc
, u32 resid
)
131 if (!scsi_bidi_cmnd(sc
)) {
132 scsi_set_resid(sc
, resid
);
136 scsi_in(sc
)->resid
= min(resid
, scsi_in(sc
)->length
);
137 scsi_out(sc
)->resid
= resid
- scsi_in(sc
)->resid
;
141 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
143 * Called with vq_lock held.
145 static void virtscsi_complete_cmd(struct virtio_scsi
*vscsi
, void *buf
)
147 struct virtio_scsi_cmd
*cmd
= buf
;
148 struct scsi_cmnd
*sc
= cmd
->sc
;
149 struct virtio_scsi_cmd_resp
*resp
= &cmd
->resp
.cmd
;
150 struct virtio_scsi_target_state
*tgt
=
151 scsi_target(sc
->device
)->hostdata
;
153 dev_dbg(&sc
->device
->sdev_gendev
,
154 "cmd %p response %u status %#02x sense_len %u\n",
155 sc
, resp
->response
, resp
->status
, resp
->sense_len
);
157 sc
->result
= resp
->status
;
158 virtscsi_compute_resid(sc
, resp
->resid
);
159 switch (resp
->response
) {
160 case VIRTIO_SCSI_S_OK
:
161 set_host_byte(sc
, DID_OK
);
163 case VIRTIO_SCSI_S_OVERRUN
:
164 set_host_byte(sc
, DID_ERROR
);
166 case VIRTIO_SCSI_S_ABORTED
:
167 set_host_byte(sc
, DID_ABORT
);
169 case VIRTIO_SCSI_S_BAD_TARGET
:
170 set_host_byte(sc
, DID_BAD_TARGET
);
172 case VIRTIO_SCSI_S_RESET
:
173 set_host_byte(sc
, DID_RESET
);
175 case VIRTIO_SCSI_S_BUSY
:
176 set_host_byte(sc
, DID_BUS_BUSY
);
178 case VIRTIO_SCSI_S_TRANSPORT_FAILURE
:
179 set_host_byte(sc
, DID_TRANSPORT_DISRUPTED
);
181 case VIRTIO_SCSI_S_TARGET_FAILURE
:
182 set_host_byte(sc
, DID_TARGET_FAILURE
);
184 case VIRTIO_SCSI_S_NEXUS_FAILURE
:
185 set_host_byte(sc
, DID_NEXUS_FAILURE
);
188 scmd_printk(KERN_WARNING
, sc
, "Unknown response %d",
191 case VIRTIO_SCSI_S_FAILURE
:
192 set_host_byte(sc
, DID_ERROR
);
196 WARN_ON(resp
->sense_len
> VIRTIO_SCSI_SENSE_SIZE
);
197 if (sc
->sense_buffer
) {
198 memcpy(sc
->sense_buffer
, resp
->sense
,
199 min_t(u32
, resp
->sense_len
, VIRTIO_SCSI_SENSE_SIZE
));
201 set_driver_byte(sc
, DRIVER_SENSE
);
206 atomic_dec(&tgt
->reqs
);
209 static void virtscsi_vq_done(struct virtio_scsi
*vscsi
,
210 struct virtio_scsi_vq
*virtscsi_vq
,
211 void (*fn
)(struct virtio_scsi
*vscsi
, void *buf
))
216 struct virtqueue
*vq
= virtscsi_vq
->vq
;
218 spin_lock_irqsave(&virtscsi_vq
->vq_lock
, flags
);
220 virtqueue_disable_cb(vq
);
221 while ((buf
= virtqueue_get_buf(vq
, &len
)) != NULL
)
224 if (unlikely(virtqueue_is_broken(vq
)))
226 } while (!virtqueue_enable_cb(vq
));
227 spin_unlock_irqrestore(&virtscsi_vq
->vq_lock
, flags
);
230 static void virtscsi_req_done(struct virtqueue
*vq
)
232 struct Scsi_Host
*sh
= virtio_scsi_host(vq
->vdev
);
233 struct virtio_scsi
*vscsi
= shost_priv(sh
);
234 int index
= vq
->index
- VIRTIO_SCSI_VQ_BASE
;
235 struct virtio_scsi_vq
*req_vq
= &vscsi
->req_vqs
[index
];
237 virtscsi_vq_done(vscsi
, req_vq
, virtscsi_complete_cmd
);
240 static void virtscsi_complete_free(struct virtio_scsi
*vscsi
, void *buf
)
242 struct virtio_scsi_cmd
*cmd
= buf
;
245 complete_all(cmd
->comp
);
248 static void virtscsi_ctrl_done(struct virtqueue
*vq
)
250 struct Scsi_Host
*sh
= virtio_scsi_host(vq
->vdev
);
251 struct virtio_scsi
*vscsi
= shost_priv(sh
);
253 virtscsi_vq_done(vscsi
, &vscsi
->ctrl_vq
, virtscsi_complete_free
);
256 static int virtscsi_kick_event(struct virtio_scsi
*vscsi
,
257 struct virtio_scsi_event_node
*event_node
)
260 struct scatterlist sg
;
263 sg_init_one(&sg
, &event_node
->event
, sizeof(struct virtio_scsi_event
));
265 spin_lock_irqsave(&vscsi
->event_vq
.vq_lock
, flags
);
267 err
= virtqueue_add_inbuf(vscsi
->event_vq
.vq
, &sg
, 1, event_node
,
270 virtqueue_kick(vscsi
->event_vq
.vq
);
272 spin_unlock_irqrestore(&vscsi
->event_vq
.vq_lock
, flags
);
277 static int virtscsi_kick_event_all(struct virtio_scsi
*vscsi
)
281 for (i
= 0; i
< VIRTIO_SCSI_EVENT_LEN
; i
++) {
282 vscsi
->event_list
[i
].vscsi
= vscsi
;
283 virtscsi_kick_event(vscsi
, &vscsi
->event_list
[i
]);
289 static void virtscsi_cancel_event_work(struct virtio_scsi
*vscsi
)
293 for (i
= 0; i
< VIRTIO_SCSI_EVENT_LEN
; i
++)
294 cancel_work_sync(&vscsi
->event_list
[i
].work
);
297 static void virtscsi_handle_transport_reset(struct virtio_scsi
*vscsi
,
298 struct virtio_scsi_event
*event
)
300 struct scsi_device
*sdev
;
301 struct Scsi_Host
*shost
= virtio_scsi_host(vscsi
->vdev
);
302 unsigned int target
= event
->lun
[1];
303 unsigned int lun
= (event
->lun
[2] << 8) | event
->lun
[3];
305 switch (event
->reason
) {
306 case VIRTIO_SCSI_EVT_RESET_RESCAN
:
307 scsi_add_device(shost
, 0, target
, lun
);
309 case VIRTIO_SCSI_EVT_RESET_REMOVED
:
310 sdev
= scsi_device_lookup(shost
, 0, target
, lun
);
312 scsi_remove_device(sdev
);
313 scsi_device_put(sdev
);
315 pr_err("SCSI device %d 0 %d %d not found\n",
316 shost
->host_no
, target
, lun
);
320 pr_info("Unsupport virtio scsi event reason %x\n", event
->reason
);
324 static void virtscsi_handle_param_change(struct virtio_scsi
*vscsi
,
325 struct virtio_scsi_event
*event
)
327 struct scsi_device
*sdev
;
328 struct Scsi_Host
*shost
= virtio_scsi_host(vscsi
->vdev
);
329 unsigned int target
= event
->lun
[1];
330 unsigned int lun
= (event
->lun
[2] << 8) | event
->lun
[3];
331 u8 asc
= event
->reason
& 255;
332 u8 ascq
= event
->reason
>> 8;
334 sdev
= scsi_device_lookup(shost
, 0, target
, lun
);
336 pr_err("SCSI device %d 0 %d %d not found\n",
337 shost
->host_no
, target
, lun
);
341 /* Handle "Parameters changed", "Mode parameters changed", and
342 "Capacity data has changed". */
343 if (asc
== 0x2a && (ascq
== 0x00 || ascq
== 0x01 || ascq
== 0x09))
344 scsi_rescan_device(&sdev
->sdev_gendev
);
346 scsi_device_put(sdev
);
349 static void virtscsi_handle_event(struct work_struct
*work
)
351 struct virtio_scsi_event_node
*event_node
=
352 container_of(work
, struct virtio_scsi_event_node
, work
);
353 struct virtio_scsi
*vscsi
= event_node
->vscsi
;
354 struct virtio_scsi_event
*event
= &event_node
->event
;
356 if (event
->event
& VIRTIO_SCSI_T_EVENTS_MISSED
) {
357 event
->event
&= ~VIRTIO_SCSI_T_EVENTS_MISSED
;
358 scsi_scan_host(virtio_scsi_host(vscsi
->vdev
));
361 switch (event
->event
) {
362 case VIRTIO_SCSI_T_NO_EVENT
:
364 case VIRTIO_SCSI_T_TRANSPORT_RESET
:
365 virtscsi_handle_transport_reset(vscsi
, event
);
367 case VIRTIO_SCSI_T_PARAM_CHANGE
:
368 virtscsi_handle_param_change(vscsi
, event
);
371 pr_err("Unsupport virtio scsi event %x\n", event
->event
);
373 virtscsi_kick_event(vscsi
, event_node
);
376 static void virtscsi_complete_event(struct virtio_scsi
*vscsi
, void *buf
)
378 struct virtio_scsi_event_node
*event_node
= buf
;
380 INIT_WORK(&event_node
->work
, virtscsi_handle_event
);
381 schedule_work(&event_node
->work
);
384 static void virtscsi_event_done(struct virtqueue
*vq
)
386 struct Scsi_Host
*sh
= virtio_scsi_host(vq
->vdev
);
387 struct virtio_scsi
*vscsi
= shost_priv(sh
);
389 virtscsi_vq_done(vscsi
, &vscsi
->event_vq
, virtscsi_complete_event
);
393 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
394 * @vq : the struct virtqueue we're talking about
395 * @cmd : command structure
396 * @req_size : size of the request buffer
397 * @resp_size : size of the response buffer
399 static int virtscsi_add_cmd(struct virtqueue
*vq
,
400 struct virtio_scsi_cmd
*cmd
,
401 size_t req_size
, size_t resp_size
)
403 struct scsi_cmnd
*sc
= cmd
->sc
;
404 struct scatterlist
*sgs
[6], req
, resp
;
405 struct sg_table
*out
, *in
;
406 unsigned out_num
= 0, in_num
= 0;
410 if (sc
&& sc
->sc_data_direction
!= DMA_NONE
) {
411 if (sc
->sc_data_direction
!= DMA_FROM_DEVICE
)
412 out
= &scsi_out(sc
)->table
;
413 if (sc
->sc_data_direction
!= DMA_TO_DEVICE
)
414 in
= &scsi_in(sc
)->table
;
417 /* Request header. */
418 sg_init_one(&req
, &cmd
->req
, req_size
);
419 sgs
[out_num
++] = &req
;
421 /* Data-out buffer. */
423 /* Place WRITE protection SGLs before Data OUT payload */
424 if (scsi_prot_sg_count(sc
))
425 sgs
[out_num
++] = scsi_prot_sglist(sc
);
426 sgs
[out_num
++] = out
->sgl
;
429 /* Response header. */
430 sg_init_one(&resp
, &cmd
->resp
, resp_size
);
431 sgs
[out_num
+ in_num
++] = &resp
;
435 /* Place READ protection SGLs before Data IN payload */
436 if (scsi_prot_sg_count(sc
))
437 sgs
[out_num
+ in_num
++] = scsi_prot_sglist(sc
);
438 sgs
[out_num
+ in_num
++] = in
->sgl
;
441 return virtqueue_add_sgs(vq
, sgs
, out_num
, in_num
, cmd
, GFP_ATOMIC
);
444 static int virtscsi_kick_cmd(struct virtio_scsi_vq
*vq
,
445 struct virtio_scsi_cmd
*cmd
,
446 size_t req_size
, size_t resp_size
)
450 bool needs_kick
= false;
452 spin_lock_irqsave(&vq
->vq_lock
, flags
);
453 err
= virtscsi_add_cmd(vq
->vq
, cmd
, req_size
, resp_size
);
455 needs_kick
= virtqueue_kick_prepare(vq
->vq
);
457 spin_unlock_irqrestore(&vq
->vq_lock
, flags
);
460 virtqueue_notify(vq
->vq
);
464 static void virtio_scsi_init_hdr(struct virtio_scsi_cmd_req
*cmd
,
465 struct scsi_cmnd
*sc
)
468 cmd
->lun
[1] = sc
->device
->id
;
469 cmd
->lun
[2] = (sc
->device
->lun
>> 8) | 0x40;
470 cmd
->lun
[3] = sc
->device
->lun
& 0xff;
471 cmd
->tag
= (unsigned long)sc
;
472 cmd
->task_attr
= VIRTIO_SCSI_S_SIMPLE
;
477 static void virtio_scsi_init_hdr_pi(struct virtio_scsi_cmd_req_pi
*cmd_pi
,
478 struct scsi_cmnd
*sc
)
480 struct request
*rq
= sc
->request
;
481 struct blk_integrity
*bi
;
483 virtio_scsi_init_hdr((struct virtio_scsi_cmd_req
*)cmd_pi
, sc
);
485 if (!rq
|| !scsi_prot_sg_count(sc
))
488 bi
= blk_get_integrity(rq
->rq_disk
);
490 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
491 cmd_pi
->pi_bytesout
= blk_rq_sectors(rq
) * bi
->tuple_size
;
492 else if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
493 cmd_pi
->pi_bytesin
= blk_rq_sectors(rq
) * bi
->tuple_size
;
496 static int virtscsi_queuecommand(struct virtio_scsi
*vscsi
,
497 struct virtio_scsi_vq
*req_vq
,
498 struct scsi_cmnd
*sc
)
500 struct Scsi_Host
*shost
= virtio_scsi_host(vscsi
->vdev
);
501 struct virtio_scsi_cmd
*cmd
= scsi_cmd_priv(sc
);
504 BUG_ON(scsi_sg_count(sc
) > shost
->sg_tablesize
);
506 /* TODO: check feature bit and fail if unsupported? */
507 BUG_ON(sc
->sc_data_direction
== DMA_BIDIRECTIONAL
);
509 dev_dbg(&sc
->device
->sdev_gendev
,
510 "cmd %p CDB: %#02x\n", sc
, sc
->cmnd
[0]);
512 memset(cmd
, 0, sizeof(*cmd
));
515 BUG_ON(sc
->cmd_len
> VIRTIO_SCSI_CDB_SIZE
);
517 if (virtio_has_feature(vscsi
->vdev
, VIRTIO_SCSI_F_T10_PI
)) {
518 virtio_scsi_init_hdr_pi(&cmd
->req
.cmd_pi
, sc
);
519 memcpy(cmd
->req
.cmd_pi
.cdb
, sc
->cmnd
, sc
->cmd_len
);
520 req_size
= sizeof(cmd
->req
.cmd_pi
);
522 virtio_scsi_init_hdr(&cmd
->req
.cmd
, sc
);
523 memcpy(cmd
->req
.cmd
.cdb
, sc
->cmnd
, sc
->cmd_len
);
524 req_size
= sizeof(cmd
->req
.cmd
);
527 if (virtscsi_kick_cmd(req_vq
, cmd
, req_size
, sizeof(cmd
->resp
.cmd
)) != 0)
528 return SCSI_MLQUEUE_HOST_BUSY
;
532 static int virtscsi_queuecommand_single(struct Scsi_Host
*sh
,
533 struct scsi_cmnd
*sc
)
535 struct virtio_scsi
*vscsi
= shost_priv(sh
);
536 struct virtio_scsi_target_state
*tgt
=
537 scsi_target(sc
->device
)->hostdata
;
539 atomic_inc(&tgt
->reqs
);
540 return virtscsi_queuecommand(vscsi
, &vscsi
->req_vqs
[0], sc
);
543 static struct virtio_scsi_vq
*virtscsi_pick_vq(struct virtio_scsi
*vscsi
,
544 struct virtio_scsi_target_state
*tgt
)
546 struct virtio_scsi_vq
*vq
;
550 spin_lock_irqsave(&tgt
->tgt_lock
, flags
);
552 if (atomic_inc_return(&tgt
->reqs
) > 1)
555 queue_num
= smp_processor_id();
556 while (unlikely(queue_num
>= vscsi
->num_queues
))
557 queue_num
-= vscsi
->num_queues
;
559 tgt
->req_vq
= vq
= &vscsi
->req_vqs
[queue_num
];
562 spin_unlock_irqrestore(&tgt
->tgt_lock
, flags
);
566 static int virtscsi_queuecommand_multi(struct Scsi_Host
*sh
,
567 struct scsi_cmnd
*sc
)
569 struct virtio_scsi
*vscsi
= shost_priv(sh
);
570 struct virtio_scsi_target_state
*tgt
=
571 scsi_target(sc
->device
)->hostdata
;
572 struct virtio_scsi_vq
*req_vq
= virtscsi_pick_vq(vscsi
, tgt
);
574 return virtscsi_queuecommand(vscsi
, req_vq
, sc
);
577 static int virtscsi_tmf(struct virtio_scsi
*vscsi
, struct virtio_scsi_cmd
*cmd
)
579 DECLARE_COMPLETION_ONSTACK(comp
);
583 if (virtscsi_kick_cmd(&vscsi
->ctrl_vq
, cmd
,
584 sizeof cmd
->req
.tmf
, sizeof cmd
->resp
.tmf
) < 0)
587 wait_for_completion(&comp
);
588 if (cmd
->resp
.tmf
.response
== VIRTIO_SCSI_S_OK
||
589 cmd
->resp
.tmf
.response
== VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
)
593 mempool_free(cmd
, virtscsi_cmd_pool
);
597 static int virtscsi_device_reset(struct scsi_cmnd
*sc
)
599 struct virtio_scsi
*vscsi
= shost_priv(sc
->device
->host
);
600 struct virtio_scsi_cmd
*cmd
;
602 sdev_printk(KERN_INFO
, sc
->device
, "device reset\n");
603 cmd
= mempool_alloc(virtscsi_cmd_pool
, GFP_NOIO
);
607 memset(cmd
, 0, sizeof(*cmd
));
609 cmd
->req
.tmf
= (struct virtio_scsi_ctrl_tmf_req
){
610 .type
= VIRTIO_SCSI_T_TMF
,
611 .subtype
= VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
,
613 .lun
[1] = sc
->device
->id
,
614 .lun
[2] = (sc
->device
->lun
>> 8) | 0x40,
615 .lun
[3] = sc
->device
->lun
& 0xff,
617 return virtscsi_tmf(vscsi
, cmd
);
620 static int virtscsi_abort(struct scsi_cmnd
*sc
)
622 struct virtio_scsi
*vscsi
= shost_priv(sc
->device
->host
);
623 struct virtio_scsi_cmd
*cmd
;
625 scmd_printk(KERN_INFO
, sc
, "abort\n");
626 cmd
= mempool_alloc(virtscsi_cmd_pool
, GFP_NOIO
);
630 memset(cmd
, 0, sizeof(*cmd
));
632 cmd
->req
.tmf
= (struct virtio_scsi_ctrl_tmf_req
){
633 .type
= VIRTIO_SCSI_T_TMF
,
634 .subtype
= VIRTIO_SCSI_T_TMF_ABORT_TASK
,
636 .lun
[1] = sc
->device
->id
,
637 .lun
[2] = (sc
->device
->lun
>> 8) | 0x40,
638 .lun
[3] = sc
->device
->lun
& 0xff,
639 .tag
= (unsigned long)sc
,
641 return virtscsi_tmf(vscsi
, cmd
);
644 static int virtscsi_target_alloc(struct scsi_target
*starget
)
646 struct virtio_scsi_target_state
*tgt
=
647 kmalloc(sizeof(*tgt
), GFP_KERNEL
);
651 spin_lock_init(&tgt
->tgt_lock
);
652 atomic_set(&tgt
->reqs
, 0);
655 starget
->hostdata
= tgt
;
659 static void virtscsi_target_destroy(struct scsi_target
*starget
)
661 struct virtio_scsi_target_state
*tgt
= starget
->hostdata
;
665 static struct scsi_host_template virtscsi_host_template_single
= {
666 .module
= THIS_MODULE
,
667 .name
= "Virtio SCSI HBA",
668 .proc_name
= "virtio_scsi",
670 .cmd_size
= sizeof(struct virtio_scsi_cmd
),
671 .queuecommand
= virtscsi_queuecommand_single
,
672 .eh_abort_handler
= virtscsi_abort
,
673 .eh_device_reset_handler
= virtscsi_device_reset
,
676 .dma_boundary
= UINT_MAX
,
677 .use_clustering
= ENABLE_CLUSTERING
,
678 .target_alloc
= virtscsi_target_alloc
,
679 .target_destroy
= virtscsi_target_destroy
,
682 static struct scsi_host_template virtscsi_host_template_multi
= {
683 .module
= THIS_MODULE
,
684 .name
= "Virtio SCSI HBA",
685 .proc_name
= "virtio_scsi",
687 .cmd_size
= sizeof(struct virtio_scsi_cmd
),
688 .queuecommand
= virtscsi_queuecommand_multi
,
689 .eh_abort_handler
= virtscsi_abort
,
690 .eh_device_reset_handler
= virtscsi_device_reset
,
693 .dma_boundary
= UINT_MAX
,
694 .use_clustering
= ENABLE_CLUSTERING
,
695 .target_alloc
= virtscsi_target_alloc
,
696 .target_destroy
= virtscsi_target_destroy
,
699 #define virtscsi_config_get(vdev, fld) \
701 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
702 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
706 #define virtscsi_config_set(vdev, fld, val) \
708 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
709 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
712 static void __virtscsi_set_affinity(struct virtio_scsi
*vscsi
, bool affinity
)
717 /* In multiqueue mode, when the number of cpu is equal
718 * to the number of request queues, we let the qeueues
719 * to be private to one cpu by setting the affinity hint
720 * to eliminate the contention.
722 if ((vscsi
->num_queues
== 1 ||
723 vscsi
->num_queues
!= num_online_cpus()) && affinity
) {
724 if (vscsi
->affinity_hint_set
)
732 for_each_online_cpu(cpu
) {
733 virtqueue_set_affinity(vscsi
->req_vqs
[i
].vq
, cpu
);
737 vscsi
->affinity_hint_set
= true;
739 for (i
= 0; i
< vscsi
->num_queues
; i
++) {
740 if (!vscsi
->req_vqs
[i
].vq
)
743 virtqueue_set_affinity(vscsi
->req_vqs
[i
].vq
, -1);
746 vscsi
->affinity_hint_set
= false;
750 static void virtscsi_set_affinity(struct virtio_scsi
*vscsi
, bool affinity
)
753 __virtscsi_set_affinity(vscsi
, affinity
);
757 static int virtscsi_cpu_callback(struct notifier_block
*nfb
,
758 unsigned long action
, void *hcpu
)
760 struct virtio_scsi
*vscsi
= container_of(nfb
, struct virtio_scsi
, nb
);
763 case CPU_ONLINE_FROZEN
:
765 case CPU_DEAD_FROZEN
:
766 __virtscsi_set_affinity(vscsi
, true);
774 static void virtscsi_init_vq(struct virtio_scsi_vq
*virtscsi_vq
,
775 struct virtqueue
*vq
)
777 spin_lock_init(&virtscsi_vq
->vq_lock
);
778 virtscsi_vq
->vq
= vq
;
781 static void virtscsi_scan(struct virtio_device
*vdev
)
783 struct Scsi_Host
*shost
= (struct Scsi_Host
*)vdev
->priv
;
785 scsi_scan_host(shost
);
788 static void virtscsi_remove_vqs(struct virtio_device
*vdev
)
790 struct Scsi_Host
*sh
= virtio_scsi_host(vdev
);
791 struct virtio_scsi
*vscsi
= shost_priv(sh
);
793 virtscsi_set_affinity(vscsi
, false);
795 /* Stop all the virtqueues. */
796 vdev
->config
->reset(vdev
);
798 vdev
->config
->del_vqs(vdev
);
801 static int virtscsi_init(struct virtio_device
*vdev
,
802 struct virtio_scsi
*vscsi
)
807 vq_callback_t
**callbacks
;
809 struct virtqueue
**vqs
;
811 num_vqs
= vscsi
->num_queues
+ VIRTIO_SCSI_VQ_BASE
;
812 vqs
= kmalloc(num_vqs
* sizeof(struct virtqueue
*), GFP_KERNEL
);
813 callbacks
= kmalloc(num_vqs
* sizeof(vq_callback_t
*), GFP_KERNEL
);
814 names
= kmalloc(num_vqs
* sizeof(char *), GFP_KERNEL
);
816 if (!callbacks
|| !vqs
|| !names
) {
821 callbacks
[0] = virtscsi_ctrl_done
;
822 callbacks
[1] = virtscsi_event_done
;
823 names
[0] = "control";
825 for (i
= VIRTIO_SCSI_VQ_BASE
; i
< num_vqs
; i
++) {
826 callbacks
[i
] = virtscsi_req_done
;
827 names
[i
] = "request";
830 /* Discover virtqueues and write information to configuration. */
831 err
= vdev
->config
->find_vqs(vdev
, num_vqs
, vqs
, callbacks
, names
);
835 virtscsi_init_vq(&vscsi
->ctrl_vq
, vqs
[0]);
836 virtscsi_init_vq(&vscsi
->event_vq
, vqs
[1]);
837 for (i
= VIRTIO_SCSI_VQ_BASE
; i
< num_vqs
; i
++)
838 virtscsi_init_vq(&vscsi
->req_vqs
[i
- VIRTIO_SCSI_VQ_BASE
],
841 virtscsi_set_affinity(vscsi
, true);
843 virtscsi_config_set(vdev
, cdb_size
, VIRTIO_SCSI_CDB_SIZE
);
844 virtscsi_config_set(vdev
, sense_size
, VIRTIO_SCSI_SENSE_SIZE
);
846 if (virtio_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
))
847 virtscsi_kick_event_all(vscsi
);
856 virtscsi_remove_vqs(vdev
);
860 static int virtscsi_probe(struct virtio_device
*vdev
)
862 struct Scsi_Host
*shost
;
863 struct virtio_scsi
*vscsi
;
865 u32 sg_elems
, num_targets
;
868 struct scsi_host_template
*hostt
;
870 /* We need to know how many queues before we allocate. */
871 num_queues
= virtscsi_config_get(vdev
, num_queues
) ? : 1;
873 num_targets
= virtscsi_config_get(vdev
, max_target
) + 1;
876 hostt
= &virtscsi_host_template_single
;
878 hostt
= &virtscsi_host_template_multi
;
880 shost
= scsi_host_alloc(hostt
,
881 sizeof(*vscsi
) + sizeof(vscsi
->req_vqs
[0]) * num_queues
);
885 sg_elems
= virtscsi_config_get(vdev
, seg_max
) ?: 1;
886 shost
->sg_tablesize
= sg_elems
;
887 vscsi
= shost_priv(shost
);
889 vscsi
->num_queues
= num_queues
;
892 err
= virtscsi_init(vdev
, vscsi
);
894 goto virtscsi_init_failed
;
896 vscsi
->nb
.notifier_call
= &virtscsi_cpu_callback
;
897 err
= register_hotcpu_notifier(&vscsi
->nb
);
899 pr_err("registering cpu notifier failed\n");
900 goto scsi_add_host_failed
;
903 cmd_per_lun
= virtscsi_config_get(vdev
, cmd_per_lun
) ?: 1;
904 shost
->cmd_per_lun
= min_t(u32
, cmd_per_lun
, shost
->can_queue
);
905 shost
->max_sectors
= virtscsi_config_get(vdev
, max_sectors
) ?: 0xFFFF;
907 /* LUNs > 256 are reported with format 1, so they go in the range
910 shost
->max_lun
= virtscsi_config_get(vdev
, max_lun
) + 1 + 0x4000;
911 shost
->max_id
= num_targets
;
912 shost
->max_channel
= 0;
913 shost
->max_cmd_len
= VIRTIO_SCSI_CDB_SIZE
;
915 if (virtio_has_feature(vdev
, VIRTIO_SCSI_F_T10_PI
)) {
916 host_prot
= SHOST_DIF_TYPE1_PROTECTION
| SHOST_DIF_TYPE2_PROTECTION
|
917 SHOST_DIF_TYPE3_PROTECTION
| SHOST_DIX_TYPE1_PROTECTION
|
918 SHOST_DIX_TYPE2_PROTECTION
| SHOST_DIX_TYPE3_PROTECTION
;
920 scsi_host_set_prot(shost
, host_prot
);
921 scsi_host_set_guard(shost
, SHOST_DIX_GUARD_CRC
);
924 err
= scsi_add_host(shost
, &vdev
->dev
);
926 goto scsi_add_host_failed
;
928 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
929 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
933 scsi_add_host_failed
:
934 vdev
->config
->del_vqs(vdev
);
935 virtscsi_init_failed
:
936 scsi_host_put(shost
);
940 static void virtscsi_remove(struct virtio_device
*vdev
)
942 struct Scsi_Host
*shost
= virtio_scsi_host(vdev
);
943 struct virtio_scsi
*vscsi
= shost_priv(shost
);
945 if (virtio_has_feature(vdev
, VIRTIO_SCSI_F_HOTPLUG
))
946 virtscsi_cancel_event_work(vscsi
);
948 scsi_remove_host(shost
);
950 unregister_hotcpu_notifier(&vscsi
->nb
);
952 virtscsi_remove_vqs(vdev
);
953 scsi_host_put(shost
);
956 #ifdef CONFIG_PM_SLEEP
957 static int virtscsi_freeze(struct virtio_device
*vdev
)
959 struct Scsi_Host
*sh
= virtio_scsi_host(vdev
);
960 struct virtio_scsi
*vscsi
= shost_priv(sh
);
962 unregister_hotcpu_notifier(&vscsi
->nb
);
963 virtscsi_remove_vqs(vdev
);
967 static int virtscsi_restore(struct virtio_device
*vdev
)
969 struct Scsi_Host
*sh
= virtio_scsi_host(vdev
);
970 struct virtio_scsi
*vscsi
= shost_priv(sh
);
973 err
= virtscsi_init(vdev
, vscsi
);
977 err
= register_hotcpu_notifier(&vscsi
->nb
);
979 vdev
->config
->del_vqs(vdev
);
985 static struct virtio_device_id id_table
[] = {
986 { VIRTIO_ID_SCSI
, VIRTIO_DEV_ANY_ID
},
990 static unsigned int features
[] = {
991 VIRTIO_SCSI_F_HOTPLUG
,
992 VIRTIO_SCSI_F_CHANGE
,
993 VIRTIO_SCSI_F_T10_PI
,
996 static struct virtio_driver virtio_scsi_driver
= {
997 .feature_table
= features
,
998 .feature_table_size
= ARRAY_SIZE(features
),
999 .driver
.name
= KBUILD_MODNAME
,
1000 .driver
.owner
= THIS_MODULE
,
1001 .id_table
= id_table
,
1002 .probe
= virtscsi_probe
,
1003 .scan
= virtscsi_scan
,
1004 #ifdef CONFIG_PM_SLEEP
1005 .freeze
= virtscsi_freeze
,
1006 .restore
= virtscsi_restore
,
1008 .remove
= virtscsi_remove
,
1011 static int __init
init(void)
1015 virtscsi_cmd_cache
= KMEM_CACHE(virtio_scsi_cmd
, 0);
1016 if (!virtscsi_cmd_cache
) {
1017 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
1023 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ
,
1024 virtscsi_cmd_cache
);
1025 if (!virtscsi_cmd_pool
) {
1026 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
1029 ret
= register_virtio_driver(&virtio_scsi_driver
);
1036 if (virtscsi_cmd_pool
) {
1037 mempool_destroy(virtscsi_cmd_pool
);
1038 virtscsi_cmd_pool
= NULL
;
1040 if (virtscsi_cmd_cache
) {
1041 kmem_cache_destroy(virtscsi_cmd_cache
);
1042 virtscsi_cmd_cache
= NULL
;
1047 static void __exit
fini(void)
1049 unregister_virtio_driver(&virtio_scsi_driver
);
1050 mempool_destroy(virtscsi_cmd_pool
);
1051 kmem_cache_destroy(virtscsi_cmd_cache
);
1056 MODULE_DEVICE_TABLE(virtio
, id_table
);
1057 MODULE_DESCRIPTION("Virtio SCSI HBA driver");
1058 MODULE_LICENSE("GPL");