2 * Copyright (c) 2012, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Driver for VirtIO SCSI devices. */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sglist.h>
39 #include <sys/sysctl.h>
41 #include <sys/mutex.h>
42 #include <sys/callout.h>
43 #include <sys/queue.h>
46 #include <machine/stdarg.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_debug.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
62 #include <dev/virtio/virtio.h>
63 #include <dev/virtio/virtqueue.h>
64 #include <dev/virtio/scsi/virtio_scsi.h>
65 #include <dev/virtio/scsi/virtio_scsivar.h>
67 #include "virtio_if.h"
69 static int vtscsi_modevent(module_t, int, void *);
71 static int vtscsi_probe(device_t);
72 static int vtscsi_attach(device_t);
73 static int vtscsi_detach(device_t);
74 static int vtscsi_suspend(device_t);
75 static int vtscsi_resume(device_t);
77 static void vtscsi_negotiate_features(struct vtscsi_softc *);
78 static void vtscsi_read_config(struct vtscsi_softc *,
79 struct virtio_scsi_config *);
80 static int vtscsi_maximum_segments(struct vtscsi_softc *, int);
81 static int vtscsi_alloc_virtqueues(struct vtscsi_softc *);
82 static void vtscsi_write_device_config(struct vtscsi_softc *);
83 static int vtscsi_reinit(struct vtscsi_softc *);
85 static int vtscsi_alloc_cam(struct vtscsi_softc *);
86 static int vtscsi_register_cam(struct vtscsi_softc *);
87 static void vtscsi_free_cam(struct vtscsi_softc *);
88 static void vtscsi_cam_async(void *, uint32_t, struct cam_path *, void *);
89 static int vtscsi_register_async(struct vtscsi_softc *);
90 static void vtscsi_deregister_async(struct vtscsi_softc *);
91 static void vtscsi_cam_action(struct cam_sim *, union ccb *);
92 static void vtscsi_cam_poll(struct cam_sim *);
94 static void vtscsi_cam_scsi_io(struct vtscsi_softc *, struct cam_sim *,
96 static void vtscsi_cam_get_tran_settings(struct vtscsi_softc *,
98 static void vtscsi_cam_reset_bus(struct vtscsi_softc *, union ccb *);
99 static void vtscsi_cam_reset_dev(struct vtscsi_softc *, union ccb *);
100 static void vtscsi_cam_abort(struct vtscsi_softc *, union ccb *);
101 static void vtscsi_cam_path_inquiry(struct vtscsi_softc *,
102 struct cam_sim *, union ccb *);
104 static int vtscsi_sg_append_scsi_buf(struct vtscsi_softc *,
105 struct sglist *, struct ccb_scsiio *);
106 static int vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *,
107 struct vtscsi_request *, int *, int *);
108 static int vtscsi_execute_scsi_cmd(struct vtscsi_softc *,
109 struct vtscsi_request *);
110 static int vtscsi_start_scsi_cmd(struct vtscsi_softc *, union ccb *);
111 static void vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *,
112 struct vtscsi_request *);
113 static int vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *,
114 struct vtscsi_request *);
115 static void vtscsi_timedout_scsi_cmd(void *);
116 static cam_status vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *);
117 static cam_status vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *,
118 struct ccb_scsiio *, struct virtio_scsi_cmd_resp *);
119 static void vtscsi_complete_scsi_cmd(struct vtscsi_softc *,
120 struct vtscsi_request *);
122 static void vtscsi_poll_ctrl_req(struct vtscsi_softc *,
123 struct vtscsi_request *);
124 static int vtscsi_execute_ctrl_req(struct vtscsi_softc *,
125 struct vtscsi_request *, struct sglist *, int, int, int);
126 static void vtscsi_complete_abort_task_cmd(struct vtscsi_softc *c,
127 struct vtscsi_request *);
128 static int vtscsi_execute_abort_task_cmd(struct vtscsi_softc *,
129 struct vtscsi_request *);
130 static int vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *,
131 struct vtscsi_request *);
133 static void vtscsi_get_request_lun(uint8_t [], target_id_t *, lun_id_t *);
134 static void vtscsi_set_request_lun(struct ccb_hdr *, uint8_t []);
135 static void vtscsi_init_scsi_cmd_req(struct ccb_scsiio *,
136 struct virtio_scsi_cmd_req *);
137 static void vtscsi_init_ctrl_tmf_req(struct ccb_hdr *, uint32_t,
138 uintptr_t, struct virtio_scsi_ctrl_tmf_req *);
140 static void vtscsi_freeze_simq(struct vtscsi_softc *, int);
141 static int vtscsi_thaw_simq(struct vtscsi_softc *, int);
143 static void vtscsi_announce(struct vtscsi_softc *, uint32_t, target_id_t,
145 static void vtscsi_execute_rescan(struct vtscsi_softc *, target_id_t,
147 static void vtscsi_execute_rescan_bus(struct vtscsi_softc *);
149 static void vtscsi_handle_event(struct vtscsi_softc *,
150 struct virtio_scsi_event *);
151 static int vtscsi_enqueue_event_buf(struct vtscsi_softc *,
152 struct virtio_scsi_event *);
153 static int vtscsi_init_event_vq(struct vtscsi_softc *);
154 static void vtscsi_reinit_event_vq(struct vtscsi_softc *);
155 static void vtscsi_drain_event_vq(struct vtscsi_softc *);
157 static void vtscsi_complete_vqs_locked(struct vtscsi_softc *);
158 static void vtscsi_complete_vqs(struct vtscsi_softc *);
159 static void vtscsi_drain_vqs(struct vtscsi_softc *);
160 static void vtscsi_cancel_request(struct vtscsi_softc *,
161 struct vtscsi_request *);
162 static void vtscsi_drain_vq(struct vtscsi_softc *, struct virtqueue *);
163 static void vtscsi_stop(struct vtscsi_softc *);
164 static int vtscsi_reset_bus(struct vtscsi_softc *);
166 static void vtscsi_init_request(struct vtscsi_softc *,
167 struct vtscsi_request *);
168 static int vtscsi_alloc_requests(struct vtscsi_softc *);
169 static void vtscsi_free_requests(struct vtscsi_softc *);
170 static void vtscsi_enqueue_request(struct vtscsi_softc *,
171 struct vtscsi_request *);
172 static struct vtscsi_request * vtscsi_dequeue_request(struct vtscsi_softc *);
174 static void vtscsi_complete_request(struct vtscsi_request *);
175 static void vtscsi_complete_vq(struct vtscsi_softc *, struct virtqueue *);
177 static void vtscsi_control_vq_intr(void *);
178 static void vtscsi_event_vq_intr(void *);
179 static void vtscsi_request_vq_intr(void *);
180 static void vtscsi_disable_vqs_intr(struct vtscsi_softc *);
181 static void vtscsi_enable_vqs_intr(struct vtscsi_softc *);
183 static void vtscsi_get_tunables(struct vtscsi_softc *);
184 static void vtscsi_add_sysctl(struct vtscsi_softc *);
186 static void vtscsi_printf_req(struct vtscsi_request *, const char *,
189 /* Global tunables. */
191 * The current QEMU VirtIO SCSI implementation does not cancel in-flight
192 * IO during virtio_stop(). So in-flight requests still complete after the
193 * device reset. We would have to wait for all the in-flight IO to complete,
194 * which defeats the typical purpose of a bus reset. We could simulate the
195 * bus reset with either I_T_NEXUS_RESET of all the targets, or with
196 * LOGICAL_UNIT_RESET of all the LUNs (assuming there is space in the
197 * control virtqueue). But this isn't very useful if things really go off
198 * the rails, so default to disabled for now.
200 static int vtscsi_bus_reset_disable = 1;
201 TUNABLE_INT("hw.vtscsi.bus_reset_disable", &vtscsi_bus_reset_disable);
203 static struct virtio_feature_desc vtscsi_feature_desc[] = {
204 { VIRTIO_SCSI_F_INOUT, "InOut" },
205 { VIRTIO_SCSI_F_HOTPLUG, "Hotplug" },
210 static device_method_t vtscsi_methods[] = {
211 /* Device methods. */
212 DEVMETHOD(device_probe, vtscsi_probe),
213 DEVMETHOD(device_attach, vtscsi_attach),
214 DEVMETHOD(device_detach, vtscsi_detach),
215 DEVMETHOD(device_suspend, vtscsi_suspend),
216 DEVMETHOD(device_resume, vtscsi_resume),
221 static driver_t vtscsi_driver = {
224 sizeof(struct vtscsi_softc)
226 static devclass_t vtscsi_devclass;
228 DRIVER_MODULE(virtio_scsi, virtio_pci, vtscsi_driver, vtscsi_devclass,
230 MODULE_VERSION(virtio_scsi, 1);
231 MODULE_DEPEND(virtio_scsi, virtio, 1, 1, 1);
232 MODULE_DEPEND(virtio_scsi, cam, 1, 1, 1);
235 vtscsi_modevent(module_t mod, int type, void *unused)
255 vtscsi_probe(device_t dev)
258 if (virtio_get_device_type(dev) != VIRTIO_ID_SCSI)
261 device_set_desc(dev, "VirtIO SCSI Adapter");
263 return (BUS_PROBE_DEFAULT);
267 vtscsi_attach(device_t dev)
269 struct vtscsi_softc *sc;
270 struct virtio_scsi_config scsicfg;
273 sc = device_get_softc(dev);
274 sc->vtscsi_dev = dev;
276 VTSCSI_LOCK_INIT(sc, device_get_nameunit(dev));
277 TAILQ_INIT(&sc->vtscsi_req_free);
279 vtscsi_get_tunables(sc);
280 vtscsi_add_sysctl(sc);
282 virtio_set_feature_desc(dev, vtscsi_feature_desc);
283 vtscsi_negotiate_features(sc);
285 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
286 sc->vtscsi_flags |= VTSCSI_FLAG_INDIRECT;
287 if (virtio_with_feature(dev, VIRTIO_SCSI_F_INOUT))
288 sc->vtscsi_flags |= VTSCSI_FLAG_BIDIRECTIONAL;
289 if (virtio_with_feature(dev, VIRTIO_SCSI_F_HOTPLUG))
290 sc->vtscsi_flags |= VTSCSI_FLAG_HOTPLUG;
292 vtscsi_read_config(sc, &scsicfg);
294 sc->vtscsi_max_channel = scsicfg.max_channel;
295 sc->vtscsi_max_target = scsicfg.max_target;
296 sc->vtscsi_max_lun = scsicfg.max_lun;
297 sc->vtscsi_event_buf_size = scsicfg.event_info_size;
299 vtscsi_write_device_config(sc);
301 sc->vtscsi_max_nsegs = vtscsi_maximum_segments(sc, scsicfg.seg_max);
302 sc->vtscsi_sglist = sglist_alloc(sc->vtscsi_max_nsegs, M_NOWAIT);
303 if (sc->vtscsi_sglist == NULL) {
305 device_printf(dev, "cannot allocate sglist\n");
309 error = vtscsi_alloc_virtqueues(sc);
311 device_printf(dev, "cannot allocate virtqueues\n");
315 error = vtscsi_init_event_vq(sc);
317 device_printf(dev, "cannot populate the eventvq\n");
321 error = vtscsi_alloc_requests(sc);
323 device_printf(dev, "cannot allocate requests\n");
327 error = vtscsi_alloc_cam(sc);
329 device_printf(dev, "cannot allocate CAM structures\n");
333 error = virtio_setup_intr(dev, INTR_TYPE_CAM);
335 device_printf(dev, "cannot setup virtqueue interrupts\n");
339 vtscsi_enable_vqs_intr(sc);
342 * Register with CAM after interrupts are enabled so we will get
343 * notified of the probe responses.
345 error = vtscsi_register_cam(sc);
347 device_printf(dev, "cannot register with CAM\n");
359 vtscsi_detach(device_t dev)
361 struct vtscsi_softc *sc;
363 sc = device_get_softc(dev);
366 sc->vtscsi_flags |= VTSCSI_FLAG_DETACH;
367 if (device_is_attached(dev))
371 vtscsi_complete_vqs(sc);
372 vtscsi_drain_vqs(sc);
375 vtscsi_free_requests(sc);
377 if (sc->vtscsi_sglist != NULL) {
378 sglist_free(sc->vtscsi_sglist);
379 sc->vtscsi_sglist = NULL;
382 VTSCSI_LOCK_DESTROY(sc);
388 vtscsi_suspend(device_t dev)
395 vtscsi_resume(device_t dev)
402 vtscsi_negotiate_features(struct vtscsi_softc *sc)
407 dev = sc->vtscsi_dev;
408 features = virtio_negotiate_features(dev, VTSCSI_FEATURES);
409 sc->vtscsi_features = features;
412 #define VTSCSI_GET_CONFIG(_dev, _field, _cfg) \
413 virtio_read_device_config(_dev, \
414 offsetof(struct virtio_scsi_config, _field), \
415 &(_cfg)->_field, sizeof((_cfg)->_field)) \
418 vtscsi_read_config(struct vtscsi_softc *sc,
419 struct virtio_scsi_config *scsicfg)
423 dev = sc->vtscsi_dev;
425 bzero(scsicfg, sizeof(struct virtio_scsi_config));
427 VTSCSI_GET_CONFIG(dev, num_queues, scsicfg);
428 VTSCSI_GET_CONFIG(dev, seg_max, scsicfg);
429 VTSCSI_GET_CONFIG(dev, max_sectors, scsicfg);
430 VTSCSI_GET_CONFIG(dev, cmd_per_lun, scsicfg);
431 VTSCSI_GET_CONFIG(dev, event_info_size, scsicfg);
432 VTSCSI_GET_CONFIG(dev, sense_size, scsicfg);
433 VTSCSI_GET_CONFIG(dev, cdb_size, scsicfg);
434 VTSCSI_GET_CONFIG(dev, max_channel, scsicfg);
435 VTSCSI_GET_CONFIG(dev, max_target, scsicfg);
436 VTSCSI_GET_CONFIG(dev, max_lun, scsicfg);
439 #undef VTSCSI_GET_CONFIG
442 vtscsi_maximum_segments(struct vtscsi_softc *sc, int seg_max)
446 nsegs = VTSCSI_MIN_SEGMENTS;
449 nsegs += MIN(seg_max, MAXPHYS / PAGE_SIZE + 1);
450 if (sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT)
451 nsegs = MIN(nsegs, VIRTIO_MAX_INDIRECT);
459 vtscsi_alloc_virtqueues(struct vtscsi_softc *sc)
462 struct vq_alloc_info vq_info[3];
465 dev = sc->vtscsi_dev;
468 VQ_ALLOC_INFO_INIT(&vq_info[0], 0, vtscsi_control_vq_intr, sc,
469 &sc->vtscsi_control_vq, "%s control", device_get_nameunit(dev));
471 VQ_ALLOC_INFO_INIT(&vq_info[1], 0, vtscsi_event_vq_intr, sc,
472 &sc->vtscsi_event_vq, "%s event", device_get_nameunit(dev));
474 VQ_ALLOC_INFO_INIT(&vq_info[2], sc->vtscsi_max_nsegs,
475 vtscsi_request_vq_intr, sc, &sc->vtscsi_request_vq,
476 "%s request", device_get_nameunit(dev));
478 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
482 vtscsi_write_device_config(struct vtscsi_softc *sc)
485 virtio_write_dev_config_4(sc->vtscsi_dev,
486 offsetof(struct virtio_scsi_config, sense_size),
487 VIRTIO_SCSI_SENSE_SIZE);
490 * This is the size in the virtio_scsi_cmd_req structure. Note
491 * this value (32) is larger than the maximum CAM CDB size (16).
493 virtio_write_dev_config_4(sc->vtscsi_dev,
494 offsetof(struct virtio_scsi_config, cdb_size),
495 VIRTIO_SCSI_CDB_SIZE);
499 vtscsi_reinit(struct vtscsi_softc *sc)
504 dev = sc->vtscsi_dev;
506 error = virtio_reinit(dev, sc->vtscsi_features);
508 vtscsi_write_device_config(sc);
509 vtscsi_reinit_event_vq(sc);
510 virtio_reinit_complete(dev);
512 vtscsi_enable_vqs_intr(sc);
515 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d\n", error);
521 vtscsi_alloc_cam(struct vtscsi_softc *sc)
524 struct cam_devq *devq;
527 dev = sc->vtscsi_dev;
528 openings = sc->vtscsi_nrequests - VTSCSI_RESERVED_REQUESTS;
530 devq = cam_simq_alloc(openings);
532 device_printf(dev, "cannot allocate SIM queue\n");
536 sc->vtscsi_sim = cam_sim_alloc(vtscsi_cam_action, vtscsi_cam_poll,
537 "vtscsi", sc, device_get_unit(dev), VTSCSI_MTX(sc), 1,
539 if (sc->vtscsi_sim == NULL) {
541 device_printf(dev, "cannot allocate SIM\n");
549 vtscsi_register_cam(struct vtscsi_softc *sc)
552 int registered, error;
554 dev = sc->vtscsi_dev;
559 if (xpt_bus_register(sc->vtscsi_sim, dev, 0) != CAM_SUCCESS) {
561 device_printf(dev, "cannot register XPT bus\n");
567 if (xpt_create_path(&sc->vtscsi_path, NULL,
568 cam_sim_path(sc->vtscsi_sim), CAM_TARGET_WILDCARD,
569 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
571 device_printf(dev, "cannot create bus path\n");
575 if (vtscsi_register_async(sc) != CAM_REQ_CMP) {
577 device_printf(dev, "cannot register async callback\n");
586 if (sc->vtscsi_path != NULL) {
587 xpt_free_path(sc->vtscsi_path);
588 sc->vtscsi_path = NULL;
592 xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim));
600 vtscsi_free_cam(struct vtscsi_softc *sc)
605 if (sc->vtscsi_path != NULL) {
606 vtscsi_deregister_async(sc);
608 xpt_free_path(sc->vtscsi_path);
609 sc->vtscsi_path = NULL;
611 xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim));
614 if (sc->vtscsi_sim != NULL) {
615 cam_sim_free(sc->vtscsi_sim, 1);
616 sc->vtscsi_sim = NULL;
623 vtscsi_cam_async(void *cb_arg, uint32_t code, struct cam_path *path, void *arg)
626 struct vtscsi_softc *sc;
629 sc = cam_sim_softc(sim);
631 vtscsi_dprintf(sc, VTSCSI_TRACE, "code=%u\n", code);
634 * TODO Once QEMU supports event reporting, we should
635 * (un)subscribe to events here.
638 case AC_FOUND_DEVICE:
646 vtscsi_register_async(struct vtscsi_softc *sc)
648 struct ccb_setasync csa;
650 xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5);
651 csa.ccb_h.func_code = XPT_SASYNC_CB;
652 csa.event_enable = AC_LOST_DEVICE | AC_FOUND_DEVICE;
653 csa.callback = vtscsi_cam_async;
654 csa.callback_arg = sc->vtscsi_sim;
656 xpt_action((union ccb *) &csa);
658 return (csa.ccb_h.status);
662 vtscsi_deregister_async(struct vtscsi_softc *sc)
664 struct ccb_setasync csa;
666 xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5);
667 csa.ccb_h.func_code = XPT_SASYNC_CB;
668 csa.event_enable = 0;
669 csa.callback = vtscsi_cam_async;
670 csa.callback_arg = sc->vtscsi_sim;
672 xpt_action((union ccb *) &csa);
676 vtscsi_cam_action(struct cam_sim *sim, union ccb *ccb)
678 struct vtscsi_softc *sc;
679 struct ccb_hdr *ccbh;
681 sc = cam_sim_softc(sim);
684 VTSCSI_LOCK_OWNED(sc);
686 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) {
688 * The VTSCSI_MTX is briefly dropped between setting
689 * VTSCSI_FLAG_DETACH and deregistering with CAM, so
690 * drop any CCBs that come in during that window.
692 ccbh->status = CAM_NO_HBA;
697 switch (ccbh->func_code) {
699 vtscsi_cam_scsi_io(sc, sim, ccb);
702 case XPT_SET_TRAN_SETTINGS:
703 ccbh->status = CAM_FUNC_NOTAVAIL;
707 case XPT_GET_TRAN_SETTINGS:
708 vtscsi_cam_get_tran_settings(sc, ccb);
712 vtscsi_cam_reset_bus(sc, ccb);
716 vtscsi_cam_reset_dev(sc, ccb);
720 vtscsi_cam_abort(sc, ccb);
723 case XPT_CALC_GEOMETRY:
724 cam_calc_geometry(&ccb->ccg, 1);
729 vtscsi_cam_path_inquiry(sc, sim, ccb);
733 vtscsi_dprintf(sc, VTSCSI_ERROR,
734 "invalid ccb=%p func=%#x\n", ccb, ccbh->func_code);
736 ccbh->status = CAM_REQ_INVALID;
743 vtscsi_cam_poll(struct cam_sim *sim)
745 struct vtscsi_softc *sc;
747 sc = cam_sim_softc(sim);
749 vtscsi_complete_vqs_locked(sc);
753 vtscsi_cam_scsi_io(struct vtscsi_softc *sc, struct cam_sim *sim,
756 struct ccb_hdr *ccbh;
757 struct ccb_scsiio *csio;
763 if (csio->cdb_len > VIRTIO_SCSI_CDB_SIZE) {
765 ccbh->status = CAM_REQ_INVALID;
769 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_BOTH &&
770 (sc->vtscsi_flags & VTSCSI_FLAG_BIDIRECTIONAL) == 0) {
772 ccbh->status = CAM_REQ_INVALID;
776 error = vtscsi_start_scsi_cmd(sc, ccb);
780 vtscsi_dprintf(sc, VTSCSI_ERROR,
781 "error=%d ccb=%p status=%#x\n", error, ccb, ccbh->status);
787 vtscsi_cam_get_tran_settings(struct vtscsi_softc *sc, union ccb *ccb)
789 struct ccb_trans_settings *cts;
790 struct ccb_trans_settings_scsi *scsi;
793 scsi = &cts->proto_specific.scsi;
795 cts->protocol = PROTO_SCSI;
796 cts->protocol_version = SCSI_REV_SPC3;
797 cts->transport = XPORT_SAS;
798 cts->transport_version = 0;
800 scsi->valid = CTS_SCSI_VALID_TQ;
801 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
803 ccb->ccb_h.status = CAM_REQ_CMP;
808 vtscsi_cam_reset_bus(struct vtscsi_softc *sc, union ccb *ccb)
812 error = vtscsi_reset_bus(sc);
814 ccb->ccb_h.status = CAM_REQ_CMP;
816 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
818 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d ccb=%p status=%#x\n",
819 error, ccb, ccb->ccb_h.status);
825 vtscsi_cam_reset_dev(struct vtscsi_softc *sc, union ccb *ccb)
827 struct ccb_hdr *ccbh;
828 struct vtscsi_request *req;
833 req = vtscsi_dequeue_request(sc);
836 vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
842 error = vtscsi_execute_reset_dev_cmd(sc, req);
846 vtscsi_enqueue_request(sc, req);
849 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n",
853 ccbh->status = CAM_RESRC_UNAVAIL;
855 ccbh->status = CAM_REQ_CMP_ERR;
861 vtscsi_cam_abort(struct vtscsi_softc *sc, union ccb *ccb)
863 struct vtscsi_request *req;
864 struct ccb_hdr *ccbh;
869 req = vtscsi_dequeue_request(sc);
872 vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
878 error = vtscsi_execute_abort_task_cmd(sc, req);
882 vtscsi_enqueue_request(sc, req);
885 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n",
889 ccbh->status = CAM_RESRC_UNAVAIL;
891 ccbh->status = CAM_REQ_CMP_ERR;
897 vtscsi_cam_path_inquiry(struct vtscsi_softc *sc, struct cam_sim *sim,
901 struct ccb_pathinq *cpi;
903 dev = sc->vtscsi_dev;
906 vtscsi_dprintf(sc, VTSCSI_TRACE, "sim=%p ccb=%p\n", sim, ccb);
908 cpi->version_num = 1;
909 cpi->hba_inquiry = PI_TAG_ABLE;
910 cpi->target_sprt = 0;
911 cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED;
912 if (vtscsi_bus_reset_disable != 0)
913 cpi->hba_misc |= PIM_NOBUSRESET;
914 cpi->hba_eng_cnt = 0;
916 cpi->max_target = sc->vtscsi_max_target;
917 cpi->max_lun = sc->vtscsi_max_lun;
918 cpi->initiator_id = VTSCSI_INITIATOR_ID;
920 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
921 strncpy(cpi->hba_vid, "VirtIO", HBA_IDLEN);
922 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
924 cpi->unit_number = cam_sim_unit(sim);
925 cpi->bus_id = cam_sim_bus(sim);
927 cpi->base_transfer_speed = 300000;
929 cpi->protocol = PROTO_SCSI;
930 cpi->protocol_version = SCSI_REV_SPC3;
931 cpi->transport = XPORT_SAS;
932 cpi->transport_version = 0;
934 cpi->maxio = (sc->vtscsi_max_nsegs - VTSCSI_MIN_SEGMENTS - 1) *
937 cpi->hba_vendor = virtio_get_vendor(dev);
938 cpi->hba_device = virtio_get_device(dev);
939 cpi->hba_subvendor = virtio_get_subvendor(dev);
940 cpi->hba_subdevice = virtio_get_subdevice(dev);
942 ccb->ccb_h.status = CAM_REQ_CMP;
947 vtscsi_sg_append_scsi_buf(struct vtscsi_softc *sc, struct sglist *sg,
948 struct ccb_scsiio *csio)
950 struct ccb_hdr *ccbh;
951 struct bus_dma_segment *dseg;
957 switch ((ccbh->flags & CAM_DATA_MASK)) {
959 error = sglist_append(sg, csio->data_ptr, csio->dxfer_len);
962 error = sglist_append_phys(sg,
963 (vm_paddr_t)(vm_offset_t) csio->data_ptr, csio->dxfer_len);
966 for (i = 0; i < csio->sglist_cnt && error == 0; i++) {
967 dseg = &((struct bus_dma_segment *)csio->data_ptr)[i];
968 error = sglist_append(sg,
969 (void *)(vm_offset_t) dseg->ds_addr, dseg->ds_len);
972 case CAM_DATA_SG_PADDR:
973 for (i = 0; i < csio->sglist_cnt && error == 0; i++) {
974 dseg = &((struct bus_dma_segment *)csio->data_ptr)[i];
975 error = sglist_append_phys(sg,
976 (vm_paddr_t) dseg->ds_addr, dseg->ds_len);
980 error = sglist_append_bio(sg, (struct bio *) csio->data_ptr);
991 vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *sc, struct vtscsi_request *req,
992 int *readable, int *writable)
995 struct ccb_hdr *ccbh;
996 struct ccb_scsiio *csio;
997 struct virtio_scsi_cmd_req *cmd_req;
998 struct virtio_scsi_cmd_resp *cmd_resp;
1001 sg = sc->vtscsi_sglist;
1002 csio = &req->vsr_ccb->csio;
1003 ccbh = &csio->ccb_h;
1004 cmd_req = &req->vsr_cmd_req;
1005 cmd_resp = &req->vsr_cmd_resp;
1009 sglist_append(sg, cmd_req, sizeof(struct virtio_scsi_cmd_req));
1010 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1011 error = vtscsi_sg_append_scsi_buf(sc, sg, csio);
1012 /* At least one segment must be left for the response. */
1013 if (error || sg->sg_nseg == sg->sg_maxseg)
1017 *readable = sg->sg_nseg;
1019 sglist_append(sg, cmd_resp, sizeof(struct virtio_scsi_cmd_resp));
1020 if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1021 error = vtscsi_sg_append_scsi_buf(sc, sg, csio);
1026 *writable = sg->sg_nseg - *readable;
1028 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p readable=%d "
1029 "writable=%d\n", req, ccbh, *readable, *writable);
1035 * This should never happen unless maxio was incorrectly set.
1037 vtscsi_set_ccb_status(ccbh, CAM_REQ_TOO_BIG, 0);
1039 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p "
1040 "nseg=%d maxseg=%d\n",
1041 error, req, ccbh, sg->sg_nseg, sg->sg_maxseg);
1047 vtscsi_execute_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req)
1050 struct virtqueue *vq;
1051 struct ccb_scsiio *csio;
1052 struct ccb_hdr *ccbh;
1053 struct virtio_scsi_cmd_req *cmd_req;
1054 struct virtio_scsi_cmd_resp *cmd_resp;
1055 int readable, writable, error;
1057 sg = sc->vtscsi_sglist;
1058 vq = sc->vtscsi_request_vq;
1059 csio = &req->vsr_ccb->csio;
1060 ccbh = &csio->ccb_h;
1061 cmd_req = &req->vsr_cmd_req;
1062 cmd_resp = &req->vsr_cmd_resp;
1064 vtscsi_init_scsi_cmd_req(csio, cmd_req);
1066 error = vtscsi_fill_scsi_cmd_sglist(sc, req, &readable, &writable);
1070 req->vsr_complete = vtscsi_complete_scsi_cmd;
1071 cmd_resp->response = -1;
1073 error = virtqueue_enqueue(vq, req, sg, readable, writable);
1075 vtscsi_dprintf(sc, VTSCSI_ERROR,
1076 "enqueue error=%d req=%p ccb=%p\n", error, req, ccbh);
1078 ccbh->status = CAM_REQUEUE_REQ;
1079 vtscsi_freeze_simq(sc, VTSCSI_REQUEST_VQ);
1083 ccbh->status |= CAM_SIM_QUEUED;
1084 ccbh->ccbh_vtscsi_req = req;
1086 virtqueue_notify(vq);
1088 if (ccbh->timeout != CAM_TIME_INFINITY) {
1089 req->vsr_flags |= VTSCSI_REQ_FLAG_TIMEOUT_SET;
1090 callout_reset_sbt(&req->vsr_callout, SBT_1MS * ccbh->timeout,
1091 0, vtscsi_timedout_scsi_cmd, req, 0);
1094 vtscsi_dprintf_req(req, VTSCSI_TRACE, "enqueued req=%p ccb=%p\n",
1101 vtscsi_start_scsi_cmd(struct vtscsi_softc *sc, union ccb *ccb)
1103 struct vtscsi_request *req;
1106 req = vtscsi_dequeue_request(sc);
1108 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1109 vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
1115 error = vtscsi_execute_scsi_cmd(sc, req);
1117 vtscsi_enqueue_request(sc, req);
1123 vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *sc,
1124 struct vtscsi_request *req)
1126 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1127 struct vtscsi_request *to_req;
1130 tmf_resp = &req->vsr_tmf_resp;
1131 response = tmf_resp->response;
1132 to_req = req->vsr_timedout_req;
1134 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p to_req=%p response=%d\n",
1135 req, to_req, response);
1137 vtscsi_enqueue_request(sc, req);
1140 * The timedout request could have completed between when the
1141 * abort task was sent and when the host processed it.
1143 if (to_req->vsr_state != VTSCSI_REQ_STATE_TIMEDOUT)
1146 /* The timedout request was successfully aborted. */
1147 if (response == VIRTIO_SCSI_S_FUNCTION_COMPLETE)
1150 /* Don't bother if the device is going away. */
1151 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH)
1154 /* The timedout request will be aborted by the reset. */
1155 if (sc->vtscsi_flags & VTSCSI_FLAG_RESET)
1158 vtscsi_reset_bus(sc);
1162 vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *sc,
1163 struct vtscsi_request *to_req)
1166 struct ccb_hdr *to_ccbh;
1167 struct vtscsi_request *req;
1168 struct virtio_scsi_ctrl_tmf_req *tmf_req;
1169 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1172 sg = sc->vtscsi_sglist;
1173 to_ccbh = &to_req->vsr_ccb->ccb_h;
1175 req = vtscsi_dequeue_request(sc);
1181 tmf_req = &req->vsr_tmf_req;
1182 tmf_resp = &req->vsr_tmf_resp;
1184 vtscsi_init_ctrl_tmf_req(to_ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK,
1185 (uintptr_t) to_ccbh, tmf_req);
1188 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
1189 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
1191 req->vsr_timedout_req = to_req;
1192 req->vsr_complete = vtscsi_complete_abort_timedout_scsi_cmd;
1193 tmf_resp->response = -1;
1195 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
1196 VTSCSI_EXECUTE_ASYNC);
1200 vtscsi_enqueue_request(sc, req);
1203 vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p "
1204 "timedout req=%p ccb=%p\n", error, req, to_req, to_ccbh);
1210 vtscsi_timedout_scsi_cmd(void *xreq)
1212 struct vtscsi_softc *sc;
1213 struct vtscsi_request *to_req;
1216 sc = to_req->vsr_softc;
1218 vtscsi_dprintf(sc, VTSCSI_INFO, "timedout req=%p ccb=%p state=%#x\n",
1219 to_req, to_req->vsr_ccb, to_req->vsr_state);
1221 /* Don't bother if the device is going away. */
1222 if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH)
1226 * Bail if the request is not in use. We likely raced when
1227 * stopping the callout handler or it has already been aborted.
1229 if (to_req->vsr_state != VTSCSI_REQ_STATE_INUSE ||
1230 (to_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) == 0)
1234 * Complete the request queue in case the timedout request is
1235 * actually just pending.
1237 vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
1238 if (to_req->vsr_state == VTSCSI_REQ_STATE_FREE)
1241 sc->vtscsi_stats.scsi_cmd_timeouts++;
1242 to_req->vsr_state = VTSCSI_REQ_STATE_TIMEDOUT;
1244 if (vtscsi_abort_timedout_scsi_cmd(sc, to_req) == 0)
1247 vtscsi_dprintf(sc, VTSCSI_ERROR, "resetting bus\n");
1248 vtscsi_reset_bus(sc);
1252 vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *cmd_resp)
1256 switch (cmd_resp->response) {
1257 case VIRTIO_SCSI_S_OK:
1258 status = CAM_REQ_CMP;
1260 case VIRTIO_SCSI_S_OVERRUN:
1261 status = CAM_DATA_RUN_ERR;
1263 case VIRTIO_SCSI_S_ABORTED:
1264 status = CAM_REQ_ABORTED;
1266 case VIRTIO_SCSI_S_BAD_TARGET:
1267 status = CAM_SEL_TIMEOUT;
1269 case VIRTIO_SCSI_S_RESET:
1270 status = CAM_SCSI_BUS_RESET;
1272 case VIRTIO_SCSI_S_BUSY:
1273 status = CAM_SCSI_BUSY;
1275 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
1276 case VIRTIO_SCSI_S_TARGET_FAILURE:
1277 case VIRTIO_SCSI_S_NEXUS_FAILURE:
1278 status = CAM_SCSI_IT_NEXUS_LOST;
1280 default: /* VIRTIO_SCSI_S_FAILURE */
1281 status = CAM_REQ_CMP_ERR;
1289 vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *sc,
1290 struct ccb_scsiio *csio, struct virtio_scsi_cmd_resp *cmd_resp)
1294 csio->scsi_status = cmd_resp->status;
1295 csio->resid = cmd_resp->resid;
1297 if (csio->scsi_status == SCSI_STATUS_OK)
1298 status = CAM_REQ_CMP;
1300 status = CAM_SCSI_STATUS_ERROR;
1302 if (cmd_resp->sense_len > 0) {
1303 status |= CAM_AUTOSNS_VALID;
1305 if (cmd_resp->sense_len < csio->sense_len)
1306 csio->sense_resid = csio->sense_len -
1307 cmd_resp->sense_len;
1309 csio->sense_resid = 0;
1311 bzero(&csio->sense_data, sizeof(csio->sense_data));
1312 memcpy(cmd_resp->sense, &csio->sense_data,
1313 csio->sense_len - csio->sense_resid);
1316 vtscsi_dprintf(sc, status == CAM_REQ_CMP ? VTSCSI_TRACE : VTSCSI_ERROR,
1317 "ccb=%p scsi_status=%#x resid=%u sense_resid=%u\n",
1318 csio, csio->scsi_status, csio->resid, csio->sense_resid);
1324 vtscsi_complete_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req)
1326 struct ccb_hdr *ccbh;
1327 struct ccb_scsiio *csio;
1328 struct virtio_scsi_cmd_resp *cmd_resp;
1331 csio = &req->vsr_ccb->csio;
1332 ccbh = &csio->ccb_h;
1333 cmd_resp = &req->vsr_cmd_resp;
1335 KASSERT(ccbh->ccbh_vtscsi_req == req,
1336 ("ccb %p req mismatch %p/%p", ccbh, ccbh->ccbh_vtscsi_req, req));
1338 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET)
1339 callout_stop(&req->vsr_callout);
1341 status = vtscsi_scsi_cmd_cam_status(cmd_resp);
1342 if (status == CAM_REQ_ABORTED) {
1343 if (req->vsr_state == VTSCSI_REQ_STATE_TIMEDOUT)
1344 status = CAM_CMD_TIMEOUT;
1345 } else if (status == CAM_REQ_CMP)
1346 status = vtscsi_complete_scsi_cmd_response(sc, csio, cmd_resp);
1348 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1349 status |= CAM_DEV_QFRZN;
1350 xpt_freeze_devq(ccbh->path, 1);
1353 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0)
1354 status |= CAM_RELEASE_SIMQ;
1356 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p status=%#x\n",
1359 ccbh->status = status;
1360 xpt_done(req->vsr_ccb);
1361 vtscsi_enqueue_request(sc, req);
1365 vtscsi_poll_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req)
1368 /* XXX We probably shouldn't poll forever. */
1369 req->vsr_flags |= VTSCSI_REQ_FLAG_POLLED;
1371 vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
1372 while ((req->vsr_flags & VTSCSI_REQ_FLAG_COMPLETE) == 0);
1374 req->vsr_flags &= ~VTSCSI_REQ_FLAG_POLLED;
1378 vtscsi_execute_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req,
1379 struct sglist *sg, int readable, int writable, int flag)
1381 struct virtqueue *vq;
1384 vq = sc->vtscsi_control_vq;
1386 MPASS(flag == VTSCSI_EXECUTE_POLL || req->vsr_complete != NULL);
1388 error = virtqueue_enqueue(vq, req, sg, readable, writable);
1391 * Return EAGAIN when the virtqueue does not have enough
1392 * descriptors available.
1394 if (error == ENOSPC || error == EMSGSIZE)
1400 virtqueue_notify(vq);
1401 if (flag == VTSCSI_EXECUTE_POLL)
1402 vtscsi_poll_ctrl_req(sc, req);
1408 vtscsi_complete_abort_task_cmd(struct vtscsi_softc *sc,
1409 struct vtscsi_request *req)
1412 struct ccb_hdr *ccbh;
1413 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1417 tmf_resp = &req->vsr_tmf_resp;
1419 switch (tmf_resp->response) {
1420 case VIRTIO_SCSI_S_FUNCTION_COMPLETE:
1421 ccbh->status = CAM_REQ_CMP;
1423 case VIRTIO_SCSI_S_FUNCTION_REJECTED:
1424 ccbh->status = CAM_UA_ABORT;
1427 ccbh->status = CAM_REQ_CMP_ERR;
1432 vtscsi_enqueue_request(sc, req);
1436 vtscsi_execute_abort_task_cmd(struct vtscsi_softc *sc,
1437 struct vtscsi_request *req)
1440 struct ccb_abort *cab;
1441 struct ccb_hdr *ccbh;
1442 struct ccb_hdr *abort_ccbh;
1443 struct vtscsi_request *abort_req;
1444 struct virtio_scsi_ctrl_tmf_req *tmf_req;
1445 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1448 sg = sc->vtscsi_sglist;
1449 cab = &req->vsr_ccb->cab;
1451 tmf_req = &req->vsr_tmf_req;
1452 tmf_resp = &req->vsr_tmf_resp;
1454 /* CCB header and request that's to be aborted. */
1455 abort_ccbh = &cab->abort_ccb->ccb_h;
1456 abort_req = abort_ccbh->ccbh_vtscsi_req;
1458 if (abort_ccbh->func_code != XPT_SCSI_IO || abort_req == NULL) {
1463 /* Only attempt to abort requests that could be in-flight. */
1464 if (abort_req->vsr_state != VTSCSI_REQ_STATE_INUSE) {
1469 abort_req->vsr_state = VTSCSI_REQ_STATE_ABORTED;
1470 if (abort_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET)
1471 callout_stop(&abort_req->vsr_callout);
1473 vtscsi_init_ctrl_tmf_req(ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK,
1474 (uintptr_t) abort_ccbh, tmf_req);
1477 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
1478 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
1480 req->vsr_complete = vtscsi_complete_abort_task_cmd;
1481 tmf_resp->response = -1;
1483 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
1484 VTSCSI_EXECUTE_ASYNC);
1487 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p abort_ccb=%p "
1488 "abort_req=%p\n", error, req, abort_ccbh, abort_req);
1494 vtscsi_complete_reset_dev_cmd(struct vtscsi_softc *sc,
1495 struct vtscsi_request *req)
1498 struct ccb_hdr *ccbh;
1499 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1503 tmf_resp = &req->vsr_tmf_resp;
1505 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p response=%d\n",
1506 req, ccb, tmf_resp->response);
1508 if (tmf_resp->response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) {
1509 ccbh->status = CAM_REQ_CMP;
1510 vtscsi_announce(sc, AC_SENT_BDR, ccbh->target_id,
1513 ccbh->status = CAM_REQ_CMP_ERR;
1516 vtscsi_enqueue_request(sc, req);
1520 vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *sc,
1521 struct vtscsi_request *req)
1524 struct ccb_resetdev *crd;
1525 struct ccb_hdr *ccbh;
1526 struct virtio_scsi_ctrl_tmf_req *tmf_req;
1527 struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
1531 sg = sc->vtscsi_sglist;
1532 crd = &req->vsr_ccb->crd;
1534 tmf_req = &req->vsr_tmf_req;
1535 tmf_resp = &req->vsr_tmf_resp;
1537 if (ccbh->target_lun == CAM_LUN_WILDCARD)
1538 subtype = VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET;
1540 subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET;
1542 vtscsi_init_ctrl_tmf_req(ccbh, subtype, 0, tmf_req);
1545 sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
1546 sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
1548 req->vsr_complete = vtscsi_complete_reset_dev_cmd;
1549 tmf_resp->response = -1;
1551 error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
1552 VTSCSI_EXECUTE_ASYNC);
1554 vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p ccb=%p\n",
1561 vtscsi_get_request_lun(uint8_t lun[], target_id_t *target_id, lun_id_t *lun_id)
1564 *target_id = lun[1];
1565 *lun_id = (lun[2] << 8) | lun[3];
1569 vtscsi_set_request_lun(struct ccb_hdr *ccbh, uint8_t lun[])
1573 lun[1] = ccbh->target_id;
1574 lun[2] = 0x40 | ((ccbh->target_lun >> 8) & 0x3F);
1575 lun[3] = ccbh->target_lun & 0xFF;
1579 vtscsi_init_scsi_cmd_req(struct ccb_scsiio *csio,
1580 struct virtio_scsi_cmd_req *cmd_req)
1584 switch (csio->tag_action) {
1585 case MSG_HEAD_OF_Q_TAG:
1586 attr = VIRTIO_SCSI_S_HEAD;
1588 case MSG_ORDERED_Q_TAG:
1589 attr = VIRTIO_SCSI_S_ORDERED;
1592 attr = VIRTIO_SCSI_S_ACA;
1594 default: /* MSG_SIMPLE_Q_TAG */
1595 attr = VIRTIO_SCSI_S_SIMPLE;
1599 vtscsi_set_request_lun(&csio->ccb_h, cmd_req->lun);
1600 cmd_req->tag = (uintptr_t) csio;
1601 cmd_req->task_attr = attr;
1603 memcpy(cmd_req->cdb,
1604 csio->ccb_h.flags & CAM_CDB_POINTER ?
1605 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
1610 vtscsi_init_ctrl_tmf_req(struct ccb_hdr *ccbh, uint32_t subtype,
1611 uintptr_t tag, struct virtio_scsi_ctrl_tmf_req *tmf_req)
1614 vtscsi_set_request_lun(ccbh, tmf_req->lun);
1616 tmf_req->type = VIRTIO_SCSI_T_TMF;
1617 tmf_req->subtype = subtype;
1622 vtscsi_freeze_simq(struct vtscsi_softc *sc, int reason)
1626 frozen = sc->vtscsi_frozen;
1628 if (reason & VTSCSI_REQUEST &&
1629 (sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) == 0)
1630 sc->vtscsi_frozen |= VTSCSI_FROZEN_NO_REQUESTS;
1632 if (reason & VTSCSI_REQUEST_VQ &&
1633 (sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) == 0)
1634 sc->vtscsi_frozen |= VTSCSI_FROZEN_REQUEST_VQ_FULL;
1636 /* Freeze the SIMQ if transitioned to frozen. */
1637 if (frozen == 0 && sc->vtscsi_frozen != 0) {
1638 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ frozen\n");
1639 xpt_freeze_simq(sc->vtscsi_sim, 1);
1644 vtscsi_thaw_simq(struct vtscsi_softc *sc, int reason)
1648 if (sc->vtscsi_frozen == 0 || reason == 0)
1651 if (reason & VTSCSI_REQUEST &&
1652 sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS)
1653 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_NO_REQUESTS;
1655 if (reason & VTSCSI_REQUEST_VQ &&
1656 sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL)
1657 sc->vtscsi_frozen &= ~VTSCSI_FROZEN_REQUEST_VQ_FULL;
1659 thawed = sc->vtscsi_frozen == 0;
1661 vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ thawed\n");
1667 vtscsi_announce(struct vtscsi_softc *sc, uint32_t ac_code,
1668 target_id_t target_id, lun_id_t lun_id)
1670 struct cam_path *path;
1672 /* Use the wildcard path from our softc for bus announcements. */
1673 if (target_id == CAM_TARGET_WILDCARD && lun_id == CAM_LUN_WILDCARD) {
1674 xpt_async(ac_code, sc->vtscsi_path, NULL);
1678 if (xpt_create_path(&path, NULL, cam_sim_path(sc->vtscsi_sim),
1679 target_id, lun_id) != CAM_REQ_CMP) {
1680 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot create path\n");
1684 xpt_async(ac_code, path, NULL);
1685 xpt_free_path(path);
1689 vtscsi_execute_rescan(struct vtscsi_softc *sc, target_id_t target_id,
1695 ccb = xpt_alloc_ccb_nowait();
1697 vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n");
1701 status = xpt_create_path(&ccb->ccb_h.path, NULL,
1702 cam_sim_path(sc->vtscsi_sim), target_id, lun_id);
1703 if (status != CAM_REQ_CMP) {
1712 vtscsi_execute_rescan_bus(struct vtscsi_softc *sc)
1715 vtscsi_execute_rescan(sc, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1719 vtscsi_transport_reset_event(struct vtscsi_softc *sc,
1720 struct virtio_scsi_event *event)
1722 target_id_t target_id;
1725 vtscsi_get_request_lun(event->lun, &target_id, &lun_id);
1727 switch (event->reason) {
1728 case VIRTIO_SCSI_EVT_RESET_RESCAN:
1729 case VIRTIO_SCSI_EVT_RESET_REMOVED:
1730 vtscsi_execute_rescan(sc, target_id, lun_id);
1733 device_printf(sc->vtscsi_dev,
1734 "unhandled transport event reason: %d\n", event->reason);
1740 vtscsi_handle_event(struct vtscsi_softc *sc, struct virtio_scsi_event *event)
1744 if ((event->event & VIRTIO_SCSI_T_EVENTS_MISSED) == 0) {
1745 switch (event->event) {
1746 case VIRTIO_SCSI_T_TRANSPORT_RESET:
1747 vtscsi_transport_reset_event(sc, event);
1750 device_printf(sc->vtscsi_dev,
1751 "unhandled event: %d\n", event->event);
1755 vtscsi_execute_rescan_bus(sc);
1758 * This should always be successful since the buffer
1759 * was just dequeued.
1761 error = vtscsi_enqueue_event_buf(sc, event);
1763 ("cannot requeue event buffer: %d", error));
1767 vtscsi_enqueue_event_buf(struct vtscsi_softc *sc,
1768 struct virtio_scsi_event *event)
1771 struct virtqueue *vq;
1774 sg = sc->vtscsi_sglist;
1775 vq = sc->vtscsi_event_vq;
1776 size = sc->vtscsi_event_buf_size;
1781 error = sglist_append(sg, event, size);
1785 error = virtqueue_enqueue(vq, event, sg, 0, sg->sg_nseg);
1789 virtqueue_notify(vq);
1795 vtscsi_init_event_vq(struct vtscsi_softc *sc)
1797 struct virtio_scsi_event *event;
1801 * The first release of QEMU with VirtIO SCSI support would crash
1802 * when attempting to notify the event virtqueue. This was fixed
1803 * when hotplug support was added.
1805 if (sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG)
1806 size = sc->vtscsi_event_buf_size;
1810 if (size < sizeof(struct virtio_scsi_event))
1813 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) {
1814 event = &sc->vtscsi_event_bufs[i];
1816 error = vtscsi_enqueue_event_buf(sc, event);
1822 * Even just one buffer is enough. Missed events are
1823 * denoted with the VIRTIO_SCSI_T_EVENTS_MISSED flag.
1832 vtscsi_reinit_event_vq(struct vtscsi_softc *sc)
1834 struct virtio_scsi_event *event;
1837 if ((sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) == 0 ||
1838 sc->vtscsi_event_buf_size < sizeof(struct virtio_scsi_event))
1841 for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) {
1842 event = &sc->vtscsi_event_bufs[i];
1844 error = vtscsi_enqueue_event_buf(sc, event);
1849 KASSERT(i > 0, ("cannot reinit event vq: %d", error));
1853 vtscsi_drain_event_vq(struct vtscsi_softc *sc)
1855 struct virtqueue *vq;
1858 vq = sc->vtscsi_event_vq;
1861 while (virtqueue_drain(vq, &last) != NULL)
1864 KASSERT(virtqueue_empty(vq), ("eventvq not empty"));
1868 vtscsi_complete_vqs_locked(struct vtscsi_softc *sc)
1871 VTSCSI_LOCK_OWNED(sc);
1873 if (sc->vtscsi_request_vq != NULL)
1874 vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
1875 if (sc->vtscsi_control_vq != NULL)
1876 vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
1880 vtscsi_complete_vqs(struct vtscsi_softc *sc)
1884 vtscsi_complete_vqs_locked(sc);
1889 vtscsi_cancel_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
1896 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p\n", req, ccb);
1899 * The callout must be drained when detaching since the request is
1900 * about to be freed. The VTSCSI_MTX must not be held for this in
1901 * case the callout is pending because there is a deadlock potential.
1902 * Otherwise, the virtqueue is being drained because of a bus reset
1903 * so we only need to attempt to stop the callouts.
1905 detach = (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) != 0;
1907 VTSCSI_LOCK_NOTOWNED(sc);
1909 VTSCSI_LOCK_OWNED(sc);
1911 if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) {
1913 callout_drain(&req->vsr_callout);
1915 callout_stop(&req->vsr_callout);
1921 ccb->ccb_h.status = CAM_NO_HBA;
1923 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1929 vtscsi_enqueue_request(sc, req);
1933 vtscsi_drain_vq(struct vtscsi_softc *sc, struct virtqueue *vq)
1935 struct vtscsi_request *req;
1940 vtscsi_dprintf(sc, VTSCSI_TRACE, "vq=%p\n", vq);
1942 while ((req = virtqueue_drain(vq, &last)) != NULL)
1943 vtscsi_cancel_request(sc, req);
1945 KASSERT(virtqueue_empty(vq), ("virtqueue not empty"));
1949 vtscsi_drain_vqs(struct vtscsi_softc *sc)
1952 if (sc->vtscsi_control_vq != NULL)
1953 vtscsi_drain_vq(sc, sc->vtscsi_control_vq);
1954 if (sc->vtscsi_request_vq != NULL)
1955 vtscsi_drain_vq(sc, sc->vtscsi_request_vq);
1956 if (sc->vtscsi_event_vq != NULL)
1957 vtscsi_drain_event_vq(sc);
1961 vtscsi_stop(struct vtscsi_softc *sc)
1964 vtscsi_disable_vqs_intr(sc);
1965 virtio_stop(sc->vtscsi_dev);
1969 vtscsi_reset_bus(struct vtscsi_softc *sc)
1973 VTSCSI_LOCK_OWNED(sc);
1975 if (vtscsi_bus_reset_disable != 0) {
1976 device_printf(sc->vtscsi_dev, "bus reset disabled\n");
1980 sc->vtscsi_flags |= VTSCSI_FLAG_RESET;
1983 * vtscsi_stop() will cause the in-flight requests to be canceled.
1984 * Those requests are then completed here so CAM will retry them
1985 * after the reset is complete.
1988 vtscsi_complete_vqs_locked(sc);
1990 /* Rid the virtqueues of any remaining requests. */
1991 vtscsi_drain_vqs(sc);
1994 * Any resource shortage that froze the SIMQ cannot persist across
1995 * a bus reset so ensure it gets thawed here.
1997 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0)
1998 xpt_release_simq(sc->vtscsi_sim, 0);
2000 error = vtscsi_reinit(sc);
2002 device_printf(sc->vtscsi_dev,
2003 "reinitialization failed, stopping device...\n");
2006 vtscsi_announce(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
2009 sc->vtscsi_flags &= ~VTSCSI_FLAG_RESET;
2015 vtscsi_init_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
2019 int req_nsegs, resp_nsegs;
2021 req_nsegs = sglist_count(&req->vsr_ureq, sizeof(req->vsr_ureq));
2022 resp_nsegs = sglist_count(&req->vsr_uresp, sizeof(req->vsr_uresp));
2024 KASSERT(req_nsegs == 1, ("request crossed page boundary"));
2025 KASSERT(resp_nsegs == 1, ("response crossed page boundary"));
2028 req->vsr_softc = sc;
2029 callout_init_mtx(&req->vsr_callout, VTSCSI_MTX(sc), 0);
2033 vtscsi_alloc_requests(struct vtscsi_softc *sc)
2035 struct vtscsi_request *req;
2039 * Commands destined for either the request or control queues come
2040 * from the same SIM queue. Use the size of the request virtqueue
2041 * as it (should) be much more frequently used. Some additional
2042 * requests are allocated for internal (TMF) use.
2044 nreqs = virtqueue_size(sc->vtscsi_request_vq);
2045 if ((sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT) == 0)
2046 nreqs /= VTSCSI_MIN_SEGMENTS;
2047 nreqs += VTSCSI_RESERVED_REQUESTS;
2049 for (i = 0; i < nreqs; i++) {
2050 req = malloc(sizeof(struct vtscsi_request), M_DEVBUF,
2055 vtscsi_init_request(sc, req);
2057 sc->vtscsi_nrequests++;
2058 vtscsi_enqueue_request(sc, req);
2065 vtscsi_free_requests(struct vtscsi_softc *sc)
2067 struct vtscsi_request *req;
2069 while ((req = vtscsi_dequeue_request(sc)) != NULL) {
2070 KASSERT(callout_active(&req->vsr_callout) == 0,
2071 ("request callout still active"));
2073 sc->vtscsi_nrequests--;
2074 free(req, M_DEVBUF);
2077 KASSERT(sc->vtscsi_nrequests == 0, ("leaked requests: %d",
2078 sc->vtscsi_nrequests));
2082 vtscsi_enqueue_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
2085 KASSERT(req->vsr_softc == sc,
2086 ("non-matching request vsr_softc %p/%p", req->vsr_softc, sc));
2088 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req);
2090 /* A request is available so the SIMQ could be released. */
2091 if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST) != 0)
2092 xpt_release_simq(sc->vtscsi_sim, 1);
2094 req->vsr_ccb = NULL;
2095 req->vsr_complete = NULL;
2096 req->vsr_ptr0 = NULL;
2097 req->vsr_state = VTSCSI_REQ_STATE_FREE;
2100 bzero(&req->vsr_ureq, sizeof(req->vsr_ureq));
2101 bzero(&req->vsr_uresp, sizeof(req->vsr_uresp));
2104 * We insert at the tail of the queue in order to make it
2105 * very unlikely a request will be reused if we race with
2106 * stopping its callout handler.
2108 TAILQ_INSERT_TAIL(&sc->vtscsi_req_free, req, vsr_link);
2111 static struct vtscsi_request *
2112 vtscsi_dequeue_request(struct vtscsi_softc *sc)
2114 struct vtscsi_request *req;
2116 req = TAILQ_FIRST(&sc->vtscsi_req_free);
2118 req->vsr_state = VTSCSI_REQ_STATE_INUSE;
2119 TAILQ_REMOVE(&sc->vtscsi_req_free, req, vsr_link);
2121 sc->vtscsi_stats.dequeue_no_requests++;
2123 vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req);
2129 vtscsi_complete_request(struct vtscsi_request *req)
2132 if (req->vsr_flags & VTSCSI_REQ_FLAG_POLLED)
2133 req->vsr_flags |= VTSCSI_REQ_FLAG_COMPLETE;
2135 if (req->vsr_complete != NULL)
2136 req->vsr_complete(req->vsr_softc, req);
2140 vtscsi_complete_vq(struct vtscsi_softc *sc, struct virtqueue *vq)
2142 struct vtscsi_request *req;
2144 VTSCSI_LOCK_OWNED(sc);
2146 while ((req = virtqueue_dequeue(vq, NULL)) != NULL)
2147 vtscsi_complete_request(req);
2151 vtscsi_control_vq_intr(void *xsc)
2153 struct vtscsi_softc *sc;
2154 struct virtqueue *vq;
2157 vq = sc->vtscsi_control_vq;
2162 vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
2164 if (virtqueue_enable_intr(vq) != 0) {
2165 virtqueue_disable_intr(vq);
2174 vtscsi_event_vq_intr(void *xsc)
2176 struct vtscsi_softc *sc;
2177 struct virtqueue *vq;
2178 struct virtio_scsi_event *event;
2181 vq = sc->vtscsi_event_vq;
2186 while ((event = virtqueue_dequeue(vq, NULL)) != NULL)
2187 vtscsi_handle_event(sc, event);
2189 if (virtqueue_enable_intr(vq) != 0) {
2190 virtqueue_disable_intr(vq);
2199 vtscsi_request_vq_intr(void *xsc)
2201 struct vtscsi_softc *sc;
2202 struct virtqueue *vq;
2205 vq = sc->vtscsi_request_vq;
2210 vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
2212 if (virtqueue_enable_intr(vq) != 0) {
2213 virtqueue_disable_intr(vq);
2222 vtscsi_disable_vqs_intr(struct vtscsi_softc *sc)
2225 virtqueue_disable_intr(sc->vtscsi_control_vq);
2226 virtqueue_disable_intr(sc->vtscsi_event_vq);
2227 virtqueue_disable_intr(sc->vtscsi_request_vq);
2231 vtscsi_enable_vqs_intr(struct vtscsi_softc *sc)
2234 virtqueue_enable_intr(sc->vtscsi_control_vq);
2235 virtqueue_enable_intr(sc->vtscsi_event_vq);
2236 virtqueue_enable_intr(sc->vtscsi_request_vq);
2240 vtscsi_get_tunables(struct vtscsi_softc *sc)
2244 TUNABLE_INT_FETCH("hw.vtscsi.debug_level", &sc->vtscsi_debug);
2246 snprintf(tmpstr, sizeof(tmpstr), "dev.vtscsi.%d.debug_level",
2247 device_get_unit(sc->vtscsi_dev));
2248 TUNABLE_INT_FETCH(tmpstr, &sc->vtscsi_debug);
2252 vtscsi_add_sysctl(struct vtscsi_softc *sc)
2255 struct vtscsi_statistics *stats;
2256 struct sysctl_ctx_list *ctx;
2257 struct sysctl_oid *tree;
2258 struct sysctl_oid_list *child;
2260 dev = sc->vtscsi_dev;
2261 stats = &sc->vtscsi_stats;
2262 ctx = device_get_sysctl_ctx(dev);
2263 tree = device_get_sysctl_tree(dev);
2264 child = SYSCTL_CHILDREN(tree);
2266 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug_level",
2267 CTLFLAG_RW, &sc->vtscsi_debug, 0,
2270 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "scsi_cmd_timeouts",
2271 CTLFLAG_RD, &stats->scsi_cmd_timeouts,
2272 "SCSI command timeouts");
2273 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dequeue_no_requests",
2274 CTLFLAG_RD, &stats->dequeue_no_requests,
2275 "No available requests to dequeue");
2279 vtscsi_printf_req(struct vtscsi_request *req, const char *func,
2280 const char *fmt, ...)
2282 struct vtscsi_softc *sc;
2292 sc = req->vsr_softc;
2296 sbuf_new(&sb, str, sizeof(str), 0);
2299 sbuf_printf(&sb, "(noperiph:%s%d:%u): ",
2300 cam_sim_name(sc->vtscsi_sim), cam_sim_unit(sc->vtscsi_sim),
2301 cam_sim_bus(sc->vtscsi_sim));
2303 xpt_path_string(ccb->ccb_h.path, path_str, sizeof(path_str));
2304 sbuf_cat(&sb, path_str);
2305 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2306 scsi_command_string(&ccb->csio, &sb);
2307 sbuf_printf(&sb, "length %d ", ccb->csio.dxfer_len);
2311 sbuf_vprintf(&sb, fmt, ap);
2315 printf("%s: %s: %s", device_get_nameunit(sc->vtscsi_dev), func,