]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/pci_virtio_scsi.c
MFV: Merge xz 5.2.8
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / pci_virtio_scsi.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 Jakub Klama <jceel@FreeBSD.org>.
5  * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <sys/time.h>
39 #include <sys/queue.h>
40 #include <sys/sbuf.h>
41
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdbool.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <assert.h>
50 #include <pthread.h>
51 #include <pthread_np.h>
52
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>
61 #include <camlib.h>
62
63 #include "bhyverun.h"
64 #include "config.h"
65 #include "debug.h"
66 #include "pci_emul.h"
67 #include "virtio.h"
68 #include "iov.h"
69
70 #define VTSCSI_RINGSZ           64
71 #define VTSCSI_REQUESTQ         1
72 #define VTSCSI_THR_PER_Q        16
73 #define VTSCSI_MAXQ             (VTSCSI_REQUESTQ + 2)
74 #define VTSCSI_MAXSEG           64
75
76 #define VTSCSI_IN_HEADER_LEN(_sc)       \
77         (sizeof(struct pci_vtscsi_req_cmd_rd) + _sc->vss_config.cdb_size)
78
79 #define VTSCSI_OUT_HEADER_LEN(_sc)      \
80         (sizeof(struct pci_vtscsi_req_cmd_wr) + _sc->vss_config.sense_size)
81
82 #define VIRTIO_SCSI_MAX_CHANNEL 0
83 #define VIRTIO_SCSI_MAX_TARGET  0
84 #define VIRTIO_SCSI_MAX_LUN     16383
85
86 #define VIRTIO_SCSI_F_INOUT     (1 << 0)
87 #define VIRTIO_SCSI_F_HOTPLUG   (1 << 1)
88 #define VIRTIO_SCSI_F_CHANGE    (1 << 2)
89
90 static int pci_vtscsi_debug = 0;
91 #define WPRINTF(msg, params...) PRINTLN("virtio-scsi: " msg, ##params)
92 #define DPRINTF(msg, params...) if (pci_vtscsi_debug) WPRINTF(msg, ##params)
93
94 struct pci_vtscsi_config {
95         uint32_t num_queues;
96         uint32_t seg_max;
97         uint32_t max_sectors;
98         uint32_t cmd_per_lun;
99         uint32_t event_info_size;
100         uint32_t sense_size;
101         uint32_t cdb_size;
102         uint16_t max_channel;
103         uint16_t max_target;
104         uint32_t max_lun;
105 } __attribute__((packed));
106
107 struct pci_vtscsi_queue {
108         struct pci_vtscsi_softc *         vsq_sc;
109         struct vqueue_info *              vsq_vq;
110         pthread_mutex_t                   vsq_mtx;
111         pthread_mutex_t                   vsq_qmtx;
112         pthread_cond_t                    vsq_cv;
113         STAILQ_HEAD(, pci_vtscsi_request) vsq_requests;
114         LIST_HEAD(, pci_vtscsi_worker)    vsq_workers;
115 };
116
117 struct pci_vtscsi_worker {
118         struct pci_vtscsi_queue *     vsw_queue;
119         pthread_t                     vsw_thread;
120         bool                          vsw_exiting;
121         LIST_ENTRY(pci_vtscsi_worker) vsw_link;
122 };
123
124 struct pci_vtscsi_request {
125         struct pci_vtscsi_queue * vsr_queue;
126         struct iovec              vsr_iov_in[VTSCSI_MAXSEG];
127         int                       vsr_niov_in;
128         struct iovec              vsr_iov_out[VTSCSI_MAXSEG];
129         int                       vsr_niov_out;
130         uint32_t                  vsr_idx;
131         STAILQ_ENTRY(pci_vtscsi_request) vsr_link;
132 };
133
134 /*
135  * Per-device softc
136  */
137 struct pci_vtscsi_softc {
138         struct virtio_softc      vss_vs;
139         struct vqueue_info       vss_vq[VTSCSI_MAXQ];
140         struct pci_vtscsi_queue  vss_queues[VTSCSI_REQUESTQ];
141         pthread_mutex_t          vss_mtx;
142         int                      vss_iid;
143         int                      vss_ctl_fd;
144         uint32_t                 vss_features;
145         struct pci_vtscsi_config vss_config;
146 };
147
148 #define VIRTIO_SCSI_T_TMF                       0
149 #define VIRTIO_SCSI_T_TMF_ABORT_TASK            0
150 #define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET        1
151 #define VIRTIO_SCSI_T_TMF_CLEAR_ACA             2
152 #define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET        3
153 #define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET       4
154 #define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET    5
155 #define VIRTIO_SCSI_T_TMF_QUERY_TASK            6
156 #define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET        7
157
158 /* command-specific response values */
159 #define VIRTIO_SCSI_S_FUNCTION_COMPLETE         0
160 #define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED        10
161 #define VIRTIO_SCSI_S_FUNCTION_REJECTED         11
162
163 struct pci_vtscsi_ctrl_tmf {
164         uint32_t type;
165         uint32_t subtype;
166         uint8_t lun[8];
167         uint64_t id;
168         uint8_t response;
169 } __attribute__((packed));
170
171 #define VIRTIO_SCSI_T_AN_QUERY                  1
172 #define VIRTIO_SCSI_EVT_ASYNC_OPERATIONAL_CHANGE 2
173 #define VIRTIO_SCSI_EVT_ASYNC_POWER_MGMT        4
174 #define VIRTIO_SCSI_EVT_ASYNC_EXTERNAL_REQUEST  8
175 #define VIRTIO_SCSI_EVT_ASYNC_MEDIA_CHANGE      16
176 #define VIRTIO_SCSI_EVT_ASYNC_MULTI_HOST        32
177 #define VIRTIO_SCSI_EVT_ASYNC_DEVICE_BUSY       64
178
179 struct pci_vtscsi_ctrl_an {
180         uint32_t type;
181         uint8_t lun[8];
182         uint32_t event_requested;
183         uint32_t event_actual;
184         uint8_t response;
185 } __attribute__((packed));
186
187 /* command-specific response values */
188 #define VIRTIO_SCSI_S_OK                        0
189 #define VIRTIO_SCSI_S_OVERRUN                   1
190 #define VIRTIO_SCSI_S_ABORTED                   2
191 #define VIRTIO_SCSI_S_BAD_TARGET                3
192 #define VIRTIO_SCSI_S_RESET                     4
193 #define VIRTIO_SCSI_S_BUSY                      5
194 #define VIRTIO_SCSI_S_TRANSPORT_FAILURE         6
195 #define VIRTIO_SCSI_S_TARGET_FAILURE            7
196 #define VIRTIO_SCSI_S_NEXUS_FAILURE             8
197 #define VIRTIO_SCSI_S_FAILURE                   9
198 #define VIRTIO_SCSI_S_INCORRECT_LUN             12
199
200 /* task_attr */
201 #define VIRTIO_SCSI_S_SIMPLE                    0
202 #define VIRTIO_SCSI_S_ORDERED                   1
203 #define VIRTIO_SCSI_S_HEAD                      2
204 #define VIRTIO_SCSI_S_ACA                       3
205
206 struct pci_vtscsi_event {
207         uint32_t event;
208         uint8_t lun[8];
209         uint32_t reason;
210 } __attribute__((packed));
211
212 struct pci_vtscsi_req_cmd_rd {
213         uint8_t lun[8];
214         uint64_t id;
215         uint8_t task_attr;
216         uint8_t prio;
217         uint8_t crn;
218         uint8_t cdb[];
219 } __attribute__((packed));
220
221 struct pci_vtscsi_req_cmd_wr {
222         uint32_t sense_len;
223         uint32_t residual;
224         uint16_t status_qualifier;
225         uint8_t status;
226         uint8_t response;
227         uint8_t sense[];
228 } __attribute__((packed));
229
230 static void *pci_vtscsi_proc(void *);
231 static void pci_vtscsi_reset(void *);
232 static void pci_vtscsi_neg_features(void *, uint64_t);
233 static int pci_vtscsi_cfgread(void *, int, int, uint32_t *);
234 static int pci_vtscsi_cfgwrite(void *, int, int, uint32_t);
235 static inline int pci_vtscsi_get_lun(uint8_t *);
236 static int pci_vtscsi_control_handle(struct pci_vtscsi_softc *, void *, size_t);
237 static int pci_vtscsi_tmf_handle(struct pci_vtscsi_softc *,
238     struct pci_vtscsi_ctrl_tmf *);
239 static int pci_vtscsi_an_handle(struct pci_vtscsi_softc *,
240     struct pci_vtscsi_ctrl_an *);
241 static int pci_vtscsi_request_handle(struct pci_vtscsi_queue *, struct iovec *,
242     int, struct iovec *, int);
243 static void pci_vtscsi_controlq_notify(void *, struct vqueue_info *);
244 static void pci_vtscsi_eventq_notify(void *, struct vqueue_info *);
245 static void pci_vtscsi_requestq_notify(void *, struct vqueue_info *);
246 static int  pci_vtscsi_init_queue(struct pci_vtscsi_softc *,
247     struct pci_vtscsi_queue *, int);
248 static int pci_vtscsi_init(struct vmctx *, struct pci_devinst *, nvlist_t *);
249
250 static struct virtio_consts vtscsi_vi_consts = {
251         .vc_name =      "vtscsi",
252         .vc_nvq =       VTSCSI_MAXQ,
253         .vc_cfgsize =   sizeof(struct pci_vtscsi_config),
254         .vc_reset =     pci_vtscsi_reset,
255         .vc_cfgread =   pci_vtscsi_cfgread,
256         .vc_cfgwrite =  pci_vtscsi_cfgwrite,
257         .vc_apply_features = pci_vtscsi_neg_features,
258         .vc_hv_caps =   0,
259 };
260
261 static void *
262 pci_vtscsi_proc(void *arg)
263 {
264         struct pci_vtscsi_worker *worker = (struct pci_vtscsi_worker *)arg;
265         struct pci_vtscsi_queue *q = worker->vsw_queue;
266         struct pci_vtscsi_request *req;
267         int iolen;
268
269         for (;;) {
270                 pthread_mutex_lock(&q->vsq_mtx);
271
272                 while (STAILQ_EMPTY(&q->vsq_requests)
273                     && !worker->vsw_exiting)
274                         pthread_cond_wait(&q->vsq_cv, &q->vsq_mtx);
275
276                 if (worker->vsw_exiting)
277                         break;
278
279                 req = STAILQ_FIRST(&q->vsq_requests);
280                 STAILQ_REMOVE_HEAD(&q->vsq_requests, vsr_link);
281
282                 pthread_mutex_unlock(&q->vsq_mtx);
283                 iolen = pci_vtscsi_request_handle(q, req->vsr_iov_in,
284                     req->vsr_niov_in, req->vsr_iov_out, req->vsr_niov_out);
285
286                 pthread_mutex_lock(&q->vsq_qmtx);
287                 vq_relchain(q->vsq_vq, req->vsr_idx, iolen);
288                 vq_endchains(q->vsq_vq, 0);
289                 pthread_mutex_unlock(&q->vsq_qmtx);
290
291                 DPRINTF("request <idx=%d> completed", req->vsr_idx);
292                 free(req);
293         }
294
295         pthread_mutex_unlock(&q->vsq_mtx);
296         return (NULL);
297 }
298
299 static void
300 pci_vtscsi_reset(void *vsc)
301 {
302         struct pci_vtscsi_softc *sc;
303
304         sc = vsc;
305
306         DPRINTF("device reset requested");
307         vi_reset_dev(&sc->vss_vs);
308
309         /* initialize config structure */
310         sc->vss_config = (struct pci_vtscsi_config){
311                 .num_queues = VTSCSI_REQUESTQ,
312                 /* Leave room for the request and the response. */
313                 .seg_max = VTSCSI_MAXSEG - 2,
314                 .max_sectors = 2,
315                 .cmd_per_lun = 1,
316                 .event_info_size = sizeof(struct pci_vtscsi_event),
317                 .sense_size = 96,
318                 .cdb_size = 32,
319                 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
320                 .max_target = VIRTIO_SCSI_MAX_TARGET,
321                 .max_lun = VIRTIO_SCSI_MAX_LUN
322         };
323 }
324
325 static void
326 pci_vtscsi_neg_features(void *vsc, uint64_t negotiated_features)
327 {
328         struct pci_vtscsi_softc *sc = vsc;
329
330         sc->vss_features = negotiated_features;
331 }
332
333 static int
334 pci_vtscsi_cfgread(void *vsc, int offset, int size, uint32_t *retval)
335 {
336         struct pci_vtscsi_softc *sc = vsc;
337         void *ptr;
338
339         ptr = (uint8_t *)&sc->vss_config + offset;
340         memcpy(retval, ptr, size);
341         return (0);
342 }
343
344 static int
345 pci_vtscsi_cfgwrite(void *vsc __unused, int offset __unused, int size __unused,
346     uint32_t val __unused)
347 {
348         return (0);
349 }
350
351 static inline int
352 pci_vtscsi_get_lun(uint8_t *lun)
353 {
354
355         return (((lun[2] << 8) | lun[3]) & 0x3fff);
356 }
357
358 static int
359 pci_vtscsi_control_handle(struct pci_vtscsi_softc *sc, void *buf,
360     size_t bufsize)
361 {
362         struct pci_vtscsi_ctrl_tmf *tmf;
363         struct pci_vtscsi_ctrl_an *an;
364         uint32_t type;
365
366         if (bufsize < sizeof(uint32_t)) {
367                 WPRINTF("ignoring truncated control request");
368                 return (0);
369         }
370
371         type = *(uint32_t *)buf;
372
373         if (type == VIRTIO_SCSI_T_TMF) {
374                 if (bufsize != sizeof(*tmf)) {
375                         WPRINTF("ignoring tmf request with size %zu", bufsize);
376                         return (0);
377                 }
378                 tmf = (struct pci_vtscsi_ctrl_tmf *)buf;
379                 return (pci_vtscsi_tmf_handle(sc, tmf));
380         }
381
382         if (type == VIRTIO_SCSI_T_AN_QUERY) {
383                 if (bufsize != sizeof(*an)) {
384                         WPRINTF("ignoring AN request with size %zu", bufsize);
385                         return (0);
386                 }
387                 an = (struct pci_vtscsi_ctrl_an *)buf;
388                 return (pci_vtscsi_an_handle(sc, an));
389         }
390
391         return (0);
392 }
393
394 static int
395 pci_vtscsi_tmf_handle(struct pci_vtscsi_softc *sc,
396     struct pci_vtscsi_ctrl_tmf *tmf)
397 {
398         union ctl_io *io;
399         int err;
400
401         io = ctl_scsi_alloc_io(sc->vss_iid);
402         ctl_scsi_zero_io(io);
403
404         io->io_hdr.io_type = CTL_IO_TASK;
405         io->io_hdr.nexus.initid = sc->vss_iid;
406         io->io_hdr.nexus.targ_lun = pci_vtscsi_get_lun(tmf->lun);
407         io->taskio.tag_type = CTL_TAG_SIMPLE;
408         io->taskio.tag_num = (uint32_t)tmf->id;
409
410         switch (tmf->subtype) {
411         case VIRTIO_SCSI_T_TMF_ABORT_TASK:
412                 io->taskio.task_action = CTL_TASK_ABORT_TASK;
413                 break;
414
415         case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
416                 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
417                 break;
418
419         case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
420                 io->taskio.task_action = CTL_TASK_CLEAR_ACA;
421                 break;
422
423         case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
424                 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
425                 break;
426
427         case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
428                 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
429                 break;
430
431         case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
432                 io->taskio.task_action = CTL_TASK_LUN_RESET;
433                 break;
434
435         case VIRTIO_SCSI_T_TMF_QUERY_TASK:
436                 io->taskio.task_action = CTL_TASK_QUERY_TASK;
437                 break;
438
439         case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
440                 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
441                 break;
442         }
443
444         if (pci_vtscsi_debug) {
445                 struct sbuf *sb = sbuf_new_auto();
446                 ctl_io_sbuf(io, sb);
447                 sbuf_finish(sb);
448                 DPRINTF("%s", sbuf_data(sb));
449                 sbuf_delete(sb);
450         }
451
452         err = ioctl(sc->vss_ctl_fd, CTL_IO, io);
453         if (err != 0)
454                 WPRINTF("CTL_IO: err=%d (%s)", errno, strerror(errno));
455
456         tmf->response = io->taskio.task_status;
457         ctl_scsi_free_io(io);
458         return (1);
459 }
460
461 static int
462 pci_vtscsi_an_handle(struct pci_vtscsi_softc *sc __unused,
463     struct pci_vtscsi_ctrl_an *an __unused)
464 {
465         return (0);
466 }
467
468 static int
469 pci_vtscsi_request_handle(struct pci_vtscsi_queue *q, struct iovec *iov_in,
470     int niov_in, struct iovec *iov_out, int niov_out)
471 {
472         struct pci_vtscsi_softc *sc = q->vsq_sc;
473         struct pci_vtscsi_req_cmd_rd *cmd_rd = NULL;
474         struct pci_vtscsi_req_cmd_wr *cmd_wr;
475         struct iovec data_iov_in[VTSCSI_MAXSEG], data_iov_out[VTSCSI_MAXSEG];
476         union ctl_io *io;
477         int data_niov_in, data_niov_out;
478         void *ext_data_ptr = NULL;
479         uint32_t ext_data_len = 0, ext_sg_entries = 0;
480         int err, nxferred;
481
482         if (count_iov(iov_out, niov_out) < VTSCSI_OUT_HEADER_LEN(sc)) {
483                 WPRINTF("ignoring request with insufficient output");
484                 return (0);
485         }
486         if (count_iov(iov_in, niov_in) < VTSCSI_IN_HEADER_LEN(sc)) {
487                 WPRINTF("ignoring request with incomplete header");
488                 return (0);
489         }
490
491         seek_iov(iov_in, niov_in, data_iov_in, &data_niov_in,
492             VTSCSI_IN_HEADER_LEN(sc));
493         seek_iov(iov_out, niov_out, data_iov_out, &data_niov_out,
494             VTSCSI_OUT_HEADER_LEN(sc));
495
496         truncate_iov(iov_in, &niov_in, VTSCSI_IN_HEADER_LEN(sc));
497         truncate_iov(iov_out, &niov_out, VTSCSI_OUT_HEADER_LEN(sc));
498         iov_to_buf(iov_in, niov_in, (void **)&cmd_rd);
499
500         cmd_wr = calloc(1, VTSCSI_OUT_HEADER_LEN(sc));
501         io = ctl_scsi_alloc_io(sc->vss_iid);
502         ctl_scsi_zero_io(io);
503
504         io->io_hdr.nexus.initid = sc->vss_iid;
505         io->io_hdr.nexus.targ_lun = pci_vtscsi_get_lun(cmd_rd->lun);
506
507         io->io_hdr.io_type = CTL_IO_SCSI;
508
509         if (data_niov_in > 0) {
510                 ext_data_ptr = (void *)data_iov_in;
511                 ext_sg_entries = data_niov_in;
512                 ext_data_len = count_iov(data_iov_in, data_niov_in);
513                 io->io_hdr.flags |= CTL_FLAG_DATA_OUT;
514         } else if (data_niov_out > 0) {
515                 ext_data_ptr = (void *)data_iov_out;
516                 ext_sg_entries = data_niov_out;
517                 ext_data_len = count_iov(data_iov_out, data_niov_out);
518                 io->io_hdr.flags |= CTL_FLAG_DATA_IN;
519         }
520
521         io->scsiio.sense_len = sc->vss_config.sense_size;
522         io->scsiio.tag_num = (uint32_t)cmd_rd->id;
523         switch (cmd_rd->task_attr) {
524         case VIRTIO_SCSI_S_ORDERED:
525                 io->scsiio.tag_type = CTL_TAG_ORDERED;
526                 break;
527         case VIRTIO_SCSI_S_HEAD:
528                 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
529                 break;
530         case VIRTIO_SCSI_S_ACA:
531                 io->scsiio.tag_type = CTL_TAG_ACA;
532                 break;
533         case VIRTIO_SCSI_S_SIMPLE:
534         default:
535                 io->scsiio.tag_type = CTL_TAG_SIMPLE;
536                 break;
537         }
538         io->scsiio.ext_sg_entries = ext_sg_entries;
539         io->scsiio.ext_data_ptr = ext_data_ptr;
540         io->scsiio.ext_data_len = ext_data_len;
541         io->scsiio.ext_data_filled = 0;
542         io->scsiio.cdb_len = sc->vss_config.cdb_size;
543         memcpy(io->scsiio.cdb, cmd_rd->cdb, sc->vss_config.cdb_size);
544
545         if (pci_vtscsi_debug) {
546                 struct sbuf *sb = sbuf_new_auto();
547                 ctl_io_sbuf(io, sb);
548                 sbuf_finish(sb);
549                 DPRINTF("%s", sbuf_data(sb));
550                 sbuf_delete(sb);
551         }
552
553         err = ioctl(sc->vss_ctl_fd, CTL_IO, io);
554         if (err != 0) {
555                 WPRINTF("CTL_IO: err=%d (%s)", errno, strerror(errno));
556                 cmd_wr->response = VIRTIO_SCSI_S_FAILURE;
557         } else {
558                 cmd_wr->sense_len = MIN(io->scsiio.sense_len,
559                     sc->vss_config.sense_size);
560                 cmd_wr->residual = io->scsiio.residual;
561                 cmd_wr->status = io->scsiio.scsi_status;
562                 cmd_wr->response = VIRTIO_SCSI_S_OK;
563                 memcpy(&cmd_wr->sense, &io->scsiio.sense_data,
564                     cmd_wr->sense_len);
565         }
566
567         buf_to_iov(cmd_wr, VTSCSI_OUT_HEADER_LEN(sc), iov_out, niov_out, 0);
568         nxferred = VTSCSI_OUT_HEADER_LEN(sc) + io->scsiio.ext_data_filled;
569         free(cmd_rd);
570         free(cmd_wr);
571         ctl_scsi_free_io(io);
572         return (nxferred);
573 }
574
575 static void
576 pci_vtscsi_controlq_notify(void *vsc, struct vqueue_info *vq)
577 {
578         struct pci_vtscsi_softc *sc;
579         struct iovec iov[VTSCSI_MAXSEG];
580         struct vi_req req;
581         void *buf = NULL;
582         size_t bufsize;
583         int iolen, n;
584
585         sc = vsc;
586
587         while (vq_has_descs(vq)) {
588                 n = vq_getchain(vq, iov, VTSCSI_MAXSEG, &req);
589                 assert(n >= 1 && n <= VTSCSI_MAXSEG);
590
591                 bufsize = iov_to_buf(iov, n, &buf);
592                 iolen = pci_vtscsi_control_handle(sc, buf, bufsize);
593                 buf_to_iov((uint8_t *)buf + bufsize - iolen, iolen, iov, n,
594                     bufsize - iolen);
595
596                 /*
597                  * Release this chain and handle more
598                  */
599                 vq_relchain(vq, req.idx, iolen);
600         }
601         vq_endchains(vq, 1);    /* Generate interrupt if appropriate. */
602         free(buf);
603 }
604
605 static void
606 pci_vtscsi_eventq_notify(void *vsc __unused, struct vqueue_info *vq)
607 {
608         vq_kick_disable(vq);
609 }
610
611 static void
612 pci_vtscsi_requestq_notify(void *vsc, struct vqueue_info *vq)
613 {
614         struct pci_vtscsi_softc *sc;
615         struct pci_vtscsi_queue *q;
616         struct pci_vtscsi_request *req;
617         struct iovec iov[VTSCSI_MAXSEG];
618         struct vi_req vireq;
619         int n;
620
621         sc = vsc;
622         q = &sc->vss_queues[vq->vq_num - 2];
623
624         while (vq_has_descs(vq)) {
625                 n = vq_getchain(vq, iov, VTSCSI_MAXSEG, &vireq);
626                 assert(n >= 1 && n <= VTSCSI_MAXSEG);
627
628                 req = calloc(1, sizeof(struct pci_vtscsi_request));
629                 req->vsr_idx = vireq.idx;
630                 req->vsr_queue = q;
631                 req->vsr_niov_in = vireq.readable;
632                 req->vsr_niov_out = vireq.writable;
633                 memcpy(req->vsr_iov_in, iov,
634                     req->vsr_niov_in * sizeof(struct iovec));
635                 memcpy(req->vsr_iov_out, iov + vireq.readable,
636                     req->vsr_niov_out * sizeof(struct iovec));
637
638                 pthread_mutex_lock(&q->vsq_mtx);
639                 STAILQ_INSERT_TAIL(&q->vsq_requests, req, vsr_link);
640                 pthread_cond_signal(&q->vsq_cv);
641                 pthread_mutex_unlock(&q->vsq_mtx);
642
643                 DPRINTF("request <idx=%d> enqueued", vireq.idx);
644         }
645 }
646
647 static int
648 pci_vtscsi_init_queue(struct pci_vtscsi_softc *sc,
649     struct pci_vtscsi_queue *queue, int num)
650 {
651         struct pci_vtscsi_worker *worker;
652         char tname[MAXCOMLEN + 1];
653         int i;
654
655         queue->vsq_sc = sc;
656         queue->vsq_vq = &sc->vss_vq[num + 2];
657
658         pthread_mutex_init(&queue->vsq_mtx, NULL);
659         pthread_mutex_init(&queue->vsq_qmtx, NULL);
660         pthread_cond_init(&queue->vsq_cv, NULL);
661         STAILQ_INIT(&queue->vsq_requests);
662         LIST_INIT(&queue->vsq_workers);
663
664         for (i = 0; i < VTSCSI_THR_PER_Q; i++) {
665                 worker = calloc(1, sizeof(struct pci_vtscsi_worker));
666                 worker->vsw_queue = queue;
667
668                 pthread_create(&worker->vsw_thread, NULL, &pci_vtscsi_proc,
669                     (void *)worker);
670
671                 snprintf(tname, sizeof(tname), "vtscsi:%d-%d", num, i);
672                 pthread_set_name_np(worker->vsw_thread, tname);
673                 LIST_INSERT_HEAD(&queue->vsq_workers, worker, vsw_link);
674         }
675
676         return (0);
677 }
678
679 static int
680 pci_vtscsi_legacy_config(nvlist_t *nvl, const char *opts)
681 {
682         char *cp, *devname;
683
684         if (opts == NULL)
685                 return (0);
686
687         cp = strchr(opts, ',');
688         if (cp == NULL) {
689                 set_config_value_node(nvl, "dev", opts);
690                 return (0);
691         }
692         devname = strndup(opts, cp - opts);
693         set_config_value_node(nvl, "dev", devname);
694         free(devname);
695         return (pci_parse_legacy_config(nvl, cp + 1));
696 }
697
698 static int
699 pci_vtscsi_init(struct vmctx *ctx __unused, struct pci_devinst *pi,
700     nvlist_t *nvl)
701 {
702         struct pci_vtscsi_softc *sc;
703         const char *devname, *value;
704         int i;
705
706         sc = calloc(1, sizeof(struct pci_vtscsi_softc));
707         value = get_config_value_node(nvl, "iid");
708         if (value != NULL)
709                 sc->vss_iid = strtoul(value, NULL, 10);
710
711         devname = get_config_value_node(nvl, "dev");
712         if (devname == NULL)
713                 devname = "/dev/cam/ctl";
714         sc->vss_ctl_fd = open(devname, O_RDWR);
715         if (sc->vss_ctl_fd < 0) {
716                 WPRINTF("cannot open %s: %s", devname, strerror(errno));
717                 free(sc);
718                 return (1);
719         }
720
721         pthread_mutex_init(&sc->vss_mtx, NULL);
722
723         vi_softc_linkup(&sc->vss_vs, &vtscsi_vi_consts, sc, pi, sc->vss_vq);
724         sc->vss_vs.vs_mtx = &sc->vss_mtx;
725
726         /* controlq */
727         sc->vss_vq[0].vq_qsize = VTSCSI_RINGSZ;
728         sc->vss_vq[0].vq_notify = pci_vtscsi_controlq_notify;
729
730         /* eventq */
731         sc->vss_vq[1].vq_qsize = VTSCSI_RINGSZ;
732         sc->vss_vq[1].vq_notify = pci_vtscsi_eventq_notify;
733
734         /* request queues */
735         for (i = 2; i < VTSCSI_MAXQ; i++) {
736                 sc->vss_vq[i].vq_qsize = VTSCSI_RINGSZ;
737                 sc->vss_vq[i].vq_notify = pci_vtscsi_requestq_notify;
738                 pci_vtscsi_init_queue(sc, &sc->vss_queues[i - 2], i - 2);
739         }
740
741         /* initialize config space */
742         pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_SCSI);
743         pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
744         pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
745         pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_SCSI);
746         pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
747
748         if (vi_intr_init(&sc->vss_vs, 1, fbsdrun_virtio_msix()))
749                 return (1);
750         vi_set_io_bar(&sc->vss_vs, 0);
751
752         return (0);
753 }
754
755
756 static const struct pci_devemu pci_de_vscsi = {
757         .pe_emu =       "virtio-scsi",
758         .pe_init =      pci_vtscsi_init,
759         .pe_legacy_config = pci_vtscsi_legacy_config,
760         .pe_barwrite =  vi_pci_write,
761         .pe_barread =   vi_pci_read
762 };
763 PCI_EMUL_SET(pci_de_vscsi);