2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Jakub Klama <jceel@FreeBSD.org>.
5 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/types.h>
39 #include <sys/queue.h>
51 #include <pthread_np.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_message.h>
55 #include <cam/ctl/ctl.h>
56 #include <cam/ctl/ctl_io.h>
57 #include <cam/ctl/ctl_backend.h>
58 #include <cam/ctl/ctl_ioctl.h>
59 #include <cam/ctl/ctl_util.h>
60 #include <cam/ctl/ctl_scsi_all.h>
68 #define VTSCSI_RINGSZ 64
69 #define VTSCSI_REQUESTQ 1
70 #define VTSCSI_THR_PER_Q 16
71 #define VTSCSI_MAXQ (VTSCSI_REQUESTQ + 2)
72 #define VTSCSI_MAXSEG 64
74 #define VTSCSI_IN_HEADER_LEN(_sc) \
75 (sizeof(struct pci_vtscsi_req_cmd_rd) + _sc->vss_config.cdb_size)
77 #define VTSCSI_OUT_HEADER_LEN(_sc) \
78 (sizeof(struct pci_vtscsi_req_cmd_wr) + _sc->vss_config.sense_size)
80 #define VIRTIO_SCSI_MAX_CHANNEL 0
81 #define VIRTIO_SCSI_MAX_TARGET 0
82 #define VIRTIO_SCSI_MAX_LUN 16383
84 #define VIRTIO_SCSI_F_INOUT (1 << 0)
85 #define VIRTIO_SCSI_F_HOTPLUG (1 << 1)
86 #define VIRTIO_SCSI_F_CHANGE (1 << 2)
88 static int pci_vtscsi_debug = 0;
89 #define DPRINTF(params) if (pci_vtscsi_debug) printf params
90 #define WPRINTF(params) printf params
92 struct pci_vtscsi_config {
97 uint32_t event_info_size;
100 uint16_t max_channel;
103 } __attribute__((packed));
105 struct pci_vtscsi_queue {
106 struct pci_vtscsi_softc * vsq_sc;
107 struct vqueue_info * vsq_vq;
108 pthread_mutex_t vsq_mtx;
109 pthread_mutex_t vsq_qmtx;
110 pthread_cond_t vsq_cv;
111 STAILQ_HEAD(, pci_vtscsi_request) vsq_requests;
112 LIST_HEAD(, pci_vtscsi_worker) vsq_workers;
115 struct pci_vtscsi_worker {
116 struct pci_vtscsi_queue * vsw_queue;
117 pthread_t vsw_thread;
119 LIST_ENTRY(pci_vtscsi_worker) vsw_link;
122 struct pci_vtscsi_request {
123 struct pci_vtscsi_queue * vsr_queue;
124 struct iovec vsr_iov_in[VTSCSI_MAXSEG];
126 struct iovec vsr_iov_out[VTSCSI_MAXSEG];
129 STAILQ_ENTRY(pci_vtscsi_request) vsr_link;
135 struct pci_vtscsi_softc {
136 struct virtio_softc vss_vs;
137 struct vqueue_info vss_vq[VTSCSI_MAXQ];
138 struct pci_vtscsi_queue vss_queues[VTSCSI_REQUESTQ];
139 pthread_mutex_t vss_mtx;
142 uint32_t vss_features;
143 struct pci_vtscsi_config vss_config;
146 #define VIRTIO_SCSI_T_TMF 0
147 #define VIRTIO_SCSI_T_TMF_ABORT_TASK 0
148 #define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1
149 #define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2
150 #define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3
151 #define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4
152 #define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5
153 #define VIRTIO_SCSI_T_TMF_QUERY_TASK 6
154 #define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7
156 /* command-specific response values */
157 #define VIRTIO_SCSI_S_FUNCTION_COMPLETE 0
158 #define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10
159 #define VIRTIO_SCSI_S_FUNCTION_REJECTED 11
161 struct pci_vtscsi_ctrl_tmf {
167 } __attribute__((packed));
169 #define VIRTIO_SCSI_T_AN_QUERY 1
170 #define VIRTIO_SCSI_EVT_ASYNC_OPERATIONAL_CHANGE 2
171 #define VIRTIO_SCSI_EVT_ASYNC_POWER_MGMT 4
172 #define VIRTIO_SCSI_EVT_ASYNC_EXTERNAL_REQUEST 8
173 #define VIRTIO_SCSI_EVT_ASYNC_MEDIA_CHANGE 16
174 #define VIRTIO_SCSI_EVT_ASYNC_MULTI_HOST 32
175 #define VIRTIO_SCSI_EVT_ASYNC_DEVICE_BUSY 64
177 struct pci_vtscsi_ctrl_an {
180 uint32_t event_requested;
181 uint32_t event_actual;
183 } __attribute__((packed));
185 /* command-specific response values */
186 #define VIRTIO_SCSI_S_OK 0
187 #define VIRTIO_SCSI_S_OVERRUN 1
188 #define VIRTIO_SCSI_S_ABORTED 2
189 #define VIRTIO_SCSI_S_BAD_TARGET 3
190 #define VIRTIO_SCSI_S_RESET 4
191 #define VIRTIO_SCSI_S_BUSY 5
192 #define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6
193 #define VIRTIO_SCSI_S_TARGET_FAILURE 7
194 #define VIRTIO_SCSI_S_NEXUS_FAILURE 8
195 #define VIRTIO_SCSI_S_FAILURE 9
196 #define VIRTIO_SCSI_S_INCORRECT_LUN 12
199 #define VIRTIO_SCSI_S_SIMPLE 0
200 #define VIRTIO_SCSI_S_ORDERED 1
201 #define VIRTIO_SCSI_S_HEAD 2
202 #define VIRTIO_SCSI_S_ACA 3
204 struct pci_vtscsi_event {
208 } __attribute__((packed));
210 struct pci_vtscsi_req_cmd_rd {
217 } __attribute__((packed));
219 struct pci_vtscsi_req_cmd_wr {
222 uint16_t status_qualifier;
226 } __attribute__((packed));
228 static void *pci_vtscsi_proc(void *);
229 static void pci_vtscsi_reset(void *);
230 static void pci_vtscsi_neg_features(void *, uint64_t);
231 static int pci_vtscsi_cfgread(void *, int, int, uint32_t *);
232 static int pci_vtscsi_cfgwrite(void *, int, int, uint32_t);
233 static inline int pci_vtscsi_get_lun(uint8_t *);
234 static int pci_vtscsi_control_handle(struct pci_vtscsi_softc *, void *, size_t);
235 static int pci_vtscsi_tmf_handle(struct pci_vtscsi_softc *,
236 struct pci_vtscsi_ctrl_tmf *);
237 static int pci_vtscsi_an_handle(struct pci_vtscsi_softc *,
238 struct pci_vtscsi_ctrl_an *);
239 static int pci_vtscsi_request_handle(struct pci_vtscsi_queue *, struct iovec *,
240 int, struct iovec *, int);
241 static void pci_vtscsi_controlq_notify(void *, struct vqueue_info *);
242 static void pci_vtscsi_eventq_notify(void *, struct vqueue_info *);
243 static void pci_vtscsi_requestq_notify(void *, struct vqueue_info *);
244 static int pci_vtscsi_init_queue(struct pci_vtscsi_softc *,
245 struct pci_vtscsi_queue *, int);
246 static int pci_vtscsi_init(struct vmctx *, struct pci_devinst *, char *);
248 static struct virtio_consts vtscsi_vi_consts = {
249 "vtscsi", /* our name */
250 VTSCSI_MAXQ, /* we support 2+n virtqueues */
251 sizeof(struct pci_vtscsi_config), /* config reg size */
252 pci_vtscsi_reset, /* reset */
253 NULL, /* device-wide qnotify */
254 pci_vtscsi_cfgread, /* read virtio config */
255 pci_vtscsi_cfgwrite, /* write virtio config */
256 pci_vtscsi_neg_features, /* apply negotiated features */
257 0, /* our capabilities */
261 pci_vtscsi_proc(void *arg)
263 struct pci_vtscsi_worker *worker = (struct pci_vtscsi_worker *)arg;
264 struct pci_vtscsi_queue *q = worker->vsw_queue;
265 struct pci_vtscsi_request *req;
269 pthread_mutex_lock(&q->vsq_mtx);
271 while (STAILQ_EMPTY(&q->vsq_requests)
272 && !worker->vsw_exiting)
273 pthread_cond_wait(&q->vsq_cv, &q->vsq_mtx);
275 if (worker->vsw_exiting)
278 req = STAILQ_FIRST(&q->vsq_requests);
279 STAILQ_REMOVE_HEAD(&q->vsq_requests, vsr_link);
281 pthread_mutex_unlock(&q->vsq_mtx);
282 iolen = pci_vtscsi_request_handle(q, req->vsr_iov_in,
283 req->vsr_niov_in, req->vsr_iov_out, req->vsr_niov_out);
285 pthread_mutex_lock(&q->vsq_qmtx);
286 vq_relchain(q->vsq_vq, req->vsr_idx, iolen);
287 vq_endchains(q->vsq_vq, 0);
288 pthread_mutex_unlock(&q->vsq_qmtx);
290 DPRINTF(("virtio-scsi: request <idx=%d> completed\n",
295 pthread_mutex_unlock(&q->vsq_mtx);
300 pci_vtscsi_reset(void *vsc)
302 struct pci_vtscsi_softc *sc;
306 DPRINTF(("vtscsi: device reset requested\n"));
307 vi_reset_dev(&sc->vss_vs);
309 /* initialize config structure */
310 sc->vss_config = (struct pci_vtscsi_config){
311 .num_queues = VTSCSI_REQUESTQ,
312 .seg_max = VTSCSI_MAXSEG,
315 .event_info_size = sizeof(struct pci_vtscsi_event),
318 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
319 .max_target = VIRTIO_SCSI_MAX_TARGET,
320 .max_lun = VIRTIO_SCSI_MAX_LUN
325 pci_vtscsi_neg_features(void *vsc, uint64_t negotiated_features)
327 struct pci_vtscsi_softc *sc = vsc;
329 sc->vss_features = negotiated_features;
333 pci_vtscsi_cfgread(void *vsc, int offset, int size, uint32_t *retval)
335 struct pci_vtscsi_softc *sc = vsc;
338 ptr = (uint8_t *)&sc->vss_config + offset;
339 memcpy(retval, ptr, size);
344 pci_vtscsi_cfgwrite(void *vsc, int offset, int size, uint32_t val)
351 pci_vtscsi_get_lun(uint8_t *lun)
354 return (((lun[2] << 8) | lun[3]) & 0x3fff);
358 pci_vtscsi_control_handle(struct pci_vtscsi_softc *sc, void *buf,
361 struct pci_vtscsi_ctrl_tmf *tmf;
362 struct pci_vtscsi_ctrl_an *an;
365 type = *(uint32_t *)buf;
367 if (type == VIRTIO_SCSI_T_TMF) {
368 tmf = (struct pci_vtscsi_ctrl_tmf *)buf;
369 return (pci_vtscsi_tmf_handle(sc, tmf));
372 if (type == VIRTIO_SCSI_T_AN_QUERY) {
373 an = (struct pci_vtscsi_ctrl_an *)buf;
374 return (pci_vtscsi_an_handle(sc, an));
381 pci_vtscsi_tmf_handle(struct pci_vtscsi_softc *sc,
382 struct pci_vtscsi_ctrl_tmf *tmf)
387 io = ctl_scsi_alloc_io(sc->vss_iid);
388 ctl_scsi_zero_io(io);
390 io->io_hdr.io_type = CTL_IO_TASK;
391 io->io_hdr.nexus.initid = sc->vss_iid;
392 io->io_hdr.nexus.targ_lun = pci_vtscsi_get_lun(tmf->lun);
393 io->taskio.tag_type = CTL_TAG_SIMPLE;
394 io->taskio.tag_num = (uint32_t)tmf->id;
396 switch (tmf->subtype) {
397 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
398 io->taskio.task_action = CTL_TASK_ABORT_TASK;
401 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
402 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
405 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
406 io->taskio.task_action = CTL_TASK_CLEAR_ACA;
409 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
410 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
413 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
414 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
417 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
418 io->taskio.task_action = CTL_TASK_LUN_RESET;
421 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
422 io->taskio.task_action = CTL_TASK_QUERY_TASK;
425 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
426 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
430 if (pci_vtscsi_debug) {
431 struct sbuf *sb = sbuf_new_auto();
434 DPRINTF(("pci_virtio_scsi: %s", sbuf_data(sb)));
438 err = ioctl(sc->vss_ctl_fd, CTL_IO, io);
440 WPRINTF(("CTL_IO: err=%d (%s)\n", errno, strerror(errno)));
442 tmf->response = io->taskio.task_status;
443 ctl_scsi_free_io(io);
448 pci_vtscsi_an_handle(struct pci_vtscsi_softc *sc,
449 struct pci_vtscsi_ctrl_an *an)
456 pci_vtscsi_request_handle(struct pci_vtscsi_queue *q, struct iovec *iov_in,
457 int niov_in, struct iovec *iov_out, int niov_out)
459 struct pci_vtscsi_softc *sc = q->vsq_sc;
460 struct pci_vtscsi_req_cmd_rd *cmd_rd = NULL;
461 struct pci_vtscsi_req_cmd_wr *cmd_wr;
462 struct iovec data_iov_in[VTSCSI_MAXSEG], data_iov_out[VTSCSI_MAXSEG];
464 int data_niov_in, data_niov_out;
465 void *ext_data_ptr = NULL;
466 uint32_t ext_data_len = 0, ext_sg_entries = 0;
469 seek_iov(iov_in, niov_in, data_iov_in, &data_niov_in,
470 VTSCSI_IN_HEADER_LEN(sc));
471 seek_iov(iov_out, niov_out, data_iov_out, &data_niov_out,
472 VTSCSI_OUT_HEADER_LEN(sc));
474 truncate_iov(iov_in, &niov_in, VTSCSI_IN_HEADER_LEN(sc));
475 truncate_iov(iov_out, &niov_out, VTSCSI_OUT_HEADER_LEN(sc));
476 iov_to_buf(iov_in, niov_in, (void **)&cmd_rd);
478 cmd_wr = malloc(VTSCSI_OUT_HEADER_LEN(sc));
479 io = ctl_scsi_alloc_io(sc->vss_iid);
480 ctl_scsi_zero_io(io);
482 io->io_hdr.nexus.initid = sc->vss_iid;
483 io->io_hdr.nexus.targ_lun = pci_vtscsi_get_lun(cmd_rd->lun);
485 io->io_hdr.io_type = CTL_IO_SCSI;
487 if (data_niov_in > 0) {
488 ext_data_ptr = (void *)data_iov_in;
489 ext_sg_entries = data_niov_in;
490 ext_data_len = count_iov(data_iov_in, data_niov_in);
491 io->io_hdr.flags |= CTL_FLAG_DATA_OUT;
492 } else if (data_niov_out > 0) {
493 ext_data_ptr = (void *)data_iov_out;
494 ext_sg_entries = data_niov_out;
495 ext_data_len = count_iov(data_iov_out, data_niov_out);
496 io->io_hdr.flags |= CTL_FLAG_DATA_IN;
499 io->scsiio.sense_len = sc->vss_config.sense_size;
500 io->scsiio.tag_num = (uint32_t)cmd_rd->id;
501 switch (cmd_rd->task_attr) {
502 case VIRTIO_SCSI_S_ORDERED:
503 io->scsiio.tag_type = CTL_TAG_ORDERED;
505 case VIRTIO_SCSI_S_HEAD:
506 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
508 case VIRTIO_SCSI_S_ACA:
509 io->scsiio.tag_type = CTL_TAG_ACA;
511 case VIRTIO_SCSI_S_SIMPLE:
513 io->scsiio.tag_type = CTL_TAG_SIMPLE;
516 io->scsiio.ext_sg_entries = ext_sg_entries;
517 io->scsiio.ext_data_ptr = ext_data_ptr;
518 io->scsiio.ext_data_len = ext_data_len;
519 io->scsiio.ext_data_filled = 0;
520 io->scsiio.cdb_len = sc->vss_config.cdb_size;
521 memcpy(io->scsiio.cdb, cmd_rd->cdb, sc->vss_config.cdb_size);
523 if (pci_vtscsi_debug) {
524 struct sbuf *sb = sbuf_new_auto();
527 DPRINTF(("pci_virtio_scsi: %s", sbuf_data(sb)));
531 err = ioctl(sc->vss_ctl_fd, CTL_IO, io);
533 WPRINTF(("CTL_IO: err=%d (%s)\n", errno, strerror(errno)));
534 cmd_wr->response = VIRTIO_SCSI_S_FAILURE;
536 cmd_wr->sense_len = MIN(io->scsiio.sense_len,
537 sc->vss_config.sense_size);
538 cmd_wr->residual = io->scsiio.residual;
539 cmd_wr->status = io->scsiio.scsi_status;
540 cmd_wr->response = VIRTIO_SCSI_S_OK;
541 memcpy(&cmd_wr->sense, &io->scsiio.sense_data,
545 buf_to_iov(cmd_wr, VTSCSI_OUT_HEADER_LEN(sc), iov_out, niov_out, 0);
548 ctl_scsi_free_io(io);
549 return (VTSCSI_OUT_HEADER_LEN(sc) + io->scsiio.ext_data_filled);
553 pci_vtscsi_controlq_notify(void *vsc, struct vqueue_info *vq)
555 struct pci_vtscsi_softc *sc;
556 struct iovec iov[VTSCSI_MAXSEG];
564 while (vq_has_descs(vq)) {
565 n = vq_getchain(vq, &idx, iov, VTSCSI_MAXSEG, NULL);
566 bufsize = iov_to_buf(iov, n, &buf);
567 iolen = pci_vtscsi_control_handle(sc, buf, bufsize);
568 buf_to_iov(buf + bufsize - iolen, iolen, iov, n,
572 * Release this chain and handle more
574 vq_relchain(vq, idx, iolen);
576 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
581 pci_vtscsi_eventq_notify(void *vsc, struct vqueue_info *vq)
584 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
588 pci_vtscsi_requestq_notify(void *vsc, struct vqueue_info *vq)
590 struct pci_vtscsi_softc *sc;
591 struct pci_vtscsi_queue *q;
592 struct pci_vtscsi_request *req;
593 struct iovec iov[VTSCSI_MAXSEG];
594 uint16_t flags[VTSCSI_MAXSEG];
599 q = &sc->vss_queues[vq->vq_num - 2];
601 while (vq_has_descs(vq)) {
603 n = vq_getchain(vq, &idx, iov, VTSCSI_MAXSEG, flags);
605 /* Count readable descriptors */
606 for (i = 0; i < n; i++) {
607 if (flags[i] & VRING_DESC_F_WRITE)
613 req = calloc(1, sizeof(struct pci_vtscsi_request));
616 req->vsr_niov_in = readable;
617 req->vsr_niov_out = n - readable;
618 memcpy(req->vsr_iov_in, iov,
619 req->vsr_niov_in * sizeof(struct iovec));
620 memcpy(req->vsr_iov_out, iov + readable,
621 req->vsr_niov_out * sizeof(struct iovec));
623 pthread_mutex_lock(&q->vsq_mtx);
624 STAILQ_INSERT_TAIL(&q->vsq_requests, req, vsr_link);
625 pthread_cond_signal(&q->vsq_cv);
626 pthread_mutex_unlock(&q->vsq_mtx);
628 DPRINTF(("virtio-scsi: request <idx=%d> enqueued\n", idx));
633 pci_vtscsi_init_queue(struct pci_vtscsi_softc *sc,
634 struct pci_vtscsi_queue *queue, int num)
636 struct pci_vtscsi_worker *worker;
641 queue->vsq_vq = &sc->vss_vq[num + 2];
643 pthread_mutex_init(&queue->vsq_mtx, NULL);
644 pthread_mutex_init(&queue->vsq_qmtx, NULL);
645 pthread_cond_init(&queue->vsq_cv, NULL);
646 STAILQ_INIT(&queue->vsq_requests);
647 LIST_INIT(&queue->vsq_workers);
649 for (i = 0; i < VTSCSI_THR_PER_Q; i++) {
650 worker = calloc(1, sizeof(struct pci_vtscsi_worker));
651 worker->vsw_queue = queue;
653 pthread_create(&worker->vsw_thread, NULL, &pci_vtscsi_proc,
656 sprintf(threadname, "virtio-scsi:%d-%d", num, i);
657 pthread_set_name_np(worker->vsw_thread, threadname);
658 LIST_INSERT_HEAD(&queue->vsq_workers, worker, vsw_link);
665 pci_vtscsi_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
667 struct pci_vtscsi_softc *sc;
672 sc = calloc(1, sizeof(struct pci_vtscsi_softc));
673 devname = "/dev/cam/ctl";
674 while ((opt = strsep(&opts, ",")) != NULL) {
675 optname = strsep(&opt, "=");
676 if (opt == NULL && optidx == 0) {
679 } else if (strcmp(optname, "dev") == 0 && opt != NULL) {
681 } else if (strcmp(optname, "iid") == 0 && opt != NULL) {
682 sc->vss_iid = strtoul(opt, NULL, 10);
684 fprintf(stderr, "Invalid option %s\n", optname);
691 sc->vss_ctl_fd = open(devname, O_RDWR);
692 if (sc->vss_ctl_fd < 0) {
693 WPRINTF(("cannot open %s: %s\n", devname, strerror(errno)));
698 vi_softc_linkup(&sc->vss_vs, &vtscsi_vi_consts, sc, pi, sc->vss_vq);
699 sc->vss_vs.vs_mtx = &sc->vss_mtx;
702 sc->vss_vq[0].vq_qsize = VTSCSI_RINGSZ;
703 sc->vss_vq[0].vq_notify = pci_vtscsi_controlq_notify;
706 sc->vss_vq[1].vq_qsize = VTSCSI_RINGSZ;
707 sc->vss_vq[1].vq_notify = pci_vtscsi_eventq_notify;
710 for (i = 2; i < VTSCSI_MAXQ; i++) {
711 sc->vss_vq[i].vq_qsize = VTSCSI_RINGSZ;
712 sc->vss_vq[i].vq_notify = pci_vtscsi_requestq_notify;
713 pci_vtscsi_init_queue(sc, &sc->vss_queues[i - 2], i - 2);
716 /* initialize config space */
717 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_SCSI);
718 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
719 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
720 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_SCSI);
721 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
723 if (vi_intr_init(&sc->vss_vs, 1, fbsdrun_virtio_msix()))
725 vi_set_io_bar(&sc->vss_vs, 0);
731 struct pci_devemu pci_de_vscsi = {
732 .pe_emu = "virtio-scsi",
733 .pe_init = pci_vtscsi_init,
734 .pe_barwrite = vi_pci_write,
735 .pe_barread = vi_pci_read
737 PCI_EMUL_SET(pci_de_vscsi);