xref: /openbmc/qemu/hw/scsi/virtio-scsi.c (revision 3110409f)
1 /*
2  * Virtio SCSI HBA
3  *
4  * Copyright IBM, Corp. 2010
5  * Copyright Red Hat, Inc. 2011
6  *
7  * Authors:
8  *   Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
9  *   Paolo Bonzini      <pbonzini@redhat.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2 or later.
12  * See the COPYING file in the top-level directory.
13  *
14  */
15 
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "standard-headers/linux/virtio_ids.h"
19 #include "hw/virtio/virtio-scsi.h"
20 #include "migration/qemu-file-types.h"
21 #include "qemu/defer-call.h"
22 #include "qemu/error-report.h"
23 #include "qemu/iov.h"
24 #include "qemu/module.h"
25 #include "sysemu/block-backend.h"
26 #include "sysemu/dma.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/scsi/scsi.h"
29 #include "scsi/constants.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
32 #include "trace.h"
33 
34 typedef struct VirtIOSCSIReq {
35     /*
36      * Note:
37      * - fields up to resp_iov are initialized by virtio_scsi_init_req;
38      * - fields starting at vring are zeroed by virtio_scsi_init_req.
39      */
40     VirtQueueElement elem;
41 
42     VirtIOSCSI *dev;
43     VirtQueue *vq;
44     QEMUSGList qsgl;
45     QEMUIOVector resp_iov;
46 
47     /* Used for two-stage request submission and TMFs deferred to BH */
48     QTAILQ_ENTRY(VirtIOSCSIReq) next;
49 
50     /* Used for cancellation of request during TMFs */
51     int remaining;
52 
53     SCSIRequest *sreq;
54     size_t resp_size;
55     enum SCSIXferMode mode;
56     union {
57         VirtIOSCSICmdResp     cmd;
58         VirtIOSCSICtrlTMFResp tmf;
59         VirtIOSCSICtrlANResp  an;
60         VirtIOSCSIEvent       event;
61     } resp;
62     union {
63         VirtIOSCSICmdReq      cmd;
64         VirtIOSCSICtrlTMFReq  tmf;
65         VirtIOSCSICtrlANReq   an;
66     } req;
67 } VirtIOSCSIReq;
68 
69 static inline int virtio_scsi_get_lun(uint8_t *lun)
70 {
71     return ((lun[2] << 8) | lun[3]) & 0x3FFF;
72 }
73 
74 static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *lun)
75 {
76     if (lun[0] != 1) {
77         return NULL;
78     }
79     if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
80         return NULL;
81     }
82     return scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
83 }
84 
85 static void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req)
86 {
87     VirtIODevice *vdev = VIRTIO_DEVICE(s);
88     const size_t zero_skip =
89         offsetof(VirtIOSCSIReq, resp_iov) + sizeof(req->resp_iov);
90 
91     req->vq = vq;
92     req->dev = s;
93     qemu_sglist_init(&req->qsgl, DEVICE(s), 8, vdev->dma_as);
94     qemu_iovec_init(&req->resp_iov, 1);
95     memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip);
96 }
97 
98 static void virtio_scsi_free_req(VirtIOSCSIReq *req)
99 {
100     qemu_iovec_destroy(&req->resp_iov);
101     qemu_sglist_destroy(&req->qsgl);
102     g_free(req);
103 }
104 
105 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
106 {
107     VirtIOSCSI *s = req->dev;
108     VirtQueue *vq = req->vq;
109     VirtIODevice *vdev = VIRTIO_DEVICE(s);
110 
111     qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
112     virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
113     if (s->dataplane_started && !s->dataplane_fenced) {
114         virtio_notify_irqfd(vdev, vq);
115     } else {
116         virtio_notify(vdev, vq);
117     }
118 
119     if (req->sreq) {
120         req->sreq->hba_private = NULL;
121         scsi_req_unref(req->sreq);
122     }
123     virtio_scsi_free_req(req);
124 }
125 
126 static void virtio_scsi_complete_req_bh(void *opaque)
127 {
128     VirtIOSCSIReq *req = opaque;
129 
130     virtio_scsi_complete_req(req);
131 }
132 
133 /*
134  * Called from virtio_scsi_do_one_tmf_bh() in main loop thread. The main loop
135  * thread cannot touch the virtqueue since that could race with an IOThread.
136  */
137 static void virtio_scsi_complete_req_from_main_loop(VirtIOSCSIReq *req)
138 {
139     VirtIOSCSI *s = req->dev;
140 
141     if (!s->ctx || s->ctx == qemu_get_aio_context()) {
142         /* No need to schedule a BH when there is no IOThread */
143         virtio_scsi_complete_req(req);
144     } else {
145         /* Run request completion in the IOThread */
146         aio_wait_bh_oneshot(s->ctx, virtio_scsi_complete_req_bh, req);
147     }
148 }
149 
150 static void virtio_scsi_bad_req(VirtIOSCSIReq *req)
151 {
152     virtio_error(VIRTIO_DEVICE(req->dev), "wrong size for virtio-scsi headers");
153     virtqueue_detach_element(req->vq, &req->elem, 0);
154     virtio_scsi_free_req(req);
155 }
156 
157 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
158                               hwaddr *addr, int num, size_t skip)
159 {
160     QEMUSGList *qsgl = &req->qsgl;
161     size_t copied = 0;
162 
163     while (num) {
164         if (skip >= iov->iov_len) {
165             skip -= iov->iov_len;
166         } else {
167             qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
168             copied += iov->iov_len - skip;
169             skip = 0;
170         }
171         iov++;
172         addr++;
173         num--;
174     }
175 
176     assert(skip == 0);
177     return copied;
178 }
179 
180 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
181                                  unsigned req_size, unsigned resp_size)
182 {
183     VirtIODevice *vdev = (VirtIODevice *) req->dev;
184     size_t in_size, out_size;
185 
186     if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
187                    &req->req, req_size) < req_size) {
188         return -EINVAL;
189     }
190 
191     if (qemu_iovec_concat_iov(&req->resp_iov,
192                               req->elem.in_sg, req->elem.in_num, 0,
193                               resp_size) < resp_size) {
194         return -EINVAL;
195     }
196 
197     req->resp_size = resp_size;
198 
199     /* Old BIOSes left some padding by mistake after the req_size/resp_size.
200      * As a workaround, always consider the first buffer as the virtio-scsi
201      * request/response, making the payload start at the second element
202      * of the iovec.
203      *
204      * The actual length of the response header, stored in req->resp_size,
205      * does not change.
206      *
207      * TODO: always disable this workaround for virtio 1.0 devices.
208      */
209     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) {
210         if (req->elem.out_num) {
211             req_size = req->elem.out_sg[0].iov_len;
212         }
213         if (req->elem.in_num) {
214             resp_size = req->elem.in_sg[0].iov_len;
215         }
216     }
217 
218     out_size = qemu_sgl_concat(req, req->elem.out_sg,
219                                &req->elem.out_addr[0], req->elem.out_num,
220                                req_size);
221     in_size = qemu_sgl_concat(req, req->elem.in_sg,
222                               &req->elem.in_addr[0], req->elem.in_num,
223                               resp_size);
224 
225     if (out_size && in_size) {
226         return -ENOTSUP;
227     }
228 
229     if (out_size) {
230         req->mode = SCSI_XFER_TO_DEV;
231     } else if (in_size) {
232         req->mode = SCSI_XFER_FROM_DEV;
233     }
234 
235     return 0;
236 }
237 
238 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
239 {
240     VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
241     VirtIOSCSIReq *req;
242 
243     req = virtqueue_pop(vq, sizeof(VirtIOSCSIReq) + vs->cdb_size);
244     if (!req) {
245         return NULL;
246     }
247     virtio_scsi_init_req(s, vq, req);
248     return req;
249 }
250 
251 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
252 {
253     VirtIOSCSIReq *req = sreq->hba_private;
254     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev);
255     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
256     uint32_t n = virtio_get_queue_index(req->vq) - VIRTIO_SCSI_VQ_NUM_FIXED;
257 
258     assert(n < vs->conf.num_queues);
259     qemu_put_be32s(f, &n);
260     qemu_put_virtqueue_element(vdev, f, &req->elem);
261 }
262 
263 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
264 {
265     SCSIBus *bus = sreq->bus;
266     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
267     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
268     VirtIODevice *vdev = VIRTIO_DEVICE(s);
269     VirtIOSCSIReq *req;
270     uint32_t n;
271 
272     qemu_get_be32s(f, &n);
273     assert(n < vs->conf.num_queues);
274     req = qemu_get_virtqueue_element(vdev, f,
275                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
276     virtio_scsi_init_req(s, vs->cmd_vqs[n], req);
277 
278     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
279                               sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
280         error_report("invalid SCSI request migration data");
281         exit(1);
282     }
283 
284     scsi_req_ref(sreq);
285     req->sreq = sreq;
286     if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
287         assert(req->sreq->cmd.mode == req->mode);
288     }
289     return req;
290 }
291 
292 typedef struct {
293     Notifier        notifier;
294     VirtIOSCSIReq  *tmf_req;
295 } VirtIOSCSICancelNotifier;
296 
297 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data)
298 {
299     VirtIOSCSICancelNotifier *n = container_of(notifier,
300                                                VirtIOSCSICancelNotifier,
301                                                notifier);
302 
303     if (--n->tmf_req->remaining == 0) {
304         VirtIOSCSIReq *req = n->tmf_req;
305 
306         trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun),
307                                    req->req.tmf.tag, req->resp.tmf.response);
308         virtio_scsi_complete_req(req);
309     }
310     g_free(n);
311 }
312 
313 static inline void virtio_scsi_ctx_check(VirtIOSCSI *s, SCSIDevice *d)
314 {
315     if (s->dataplane_started && d && blk_is_available(d->conf.blk)) {
316         assert(blk_get_aio_context(d->conf.blk) == s->ctx);
317     }
318 }
319 
320 static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq *req)
321 {
322     VirtIOSCSI *s = req->dev;
323     SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun);
324     BusChild *kid;
325     int target;
326 
327     switch (req->req.tmf.subtype) {
328     case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
329         if (!d) {
330             req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
331             goto out;
332         }
333         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
334             req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
335             goto out;
336         }
337         qatomic_inc(&s->resetting);
338         device_cold_reset(&d->qdev);
339         qatomic_dec(&s->resetting);
340         break;
341 
342     case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
343         target = req->req.tmf.lun[1];
344         qatomic_inc(&s->resetting);
345 
346         rcu_read_lock();
347         QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
348             SCSIDevice *d1 = SCSI_DEVICE(kid->child);
349             if (d1->channel == 0 && d1->id == target) {
350                 device_cold_reset(&d1->qdev);
351             }
352         }
353         rcu_read_unlock();
354 
355         qatomic_dec(&s->resetting);
356         break;
357 
358     default:
359         g_assert_not_reached();
360     }
361 
362 out:
363     object_unref(OBJECT(d));
364     virtio_scsi_complete_req_from_main_loop(req);
365 }
366 
367 /* Some TMFs must be processed from the main loop thread */
368 static void virtio_scsi_do_tmf_bh(void *opaque)
369 {
370     VirtIOSCSI *s = opaque;
371     QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
372     VirtIOSCSIReq *req;
373     VirtIOSCSIReq *tmp;
374 
375     GLOBAL_STATE_CODE();
376 
377     WITH_QEMU_LOCK_GUARD(&s->tmf_bh_lock) {
378         QTAILQ_FOREACH_SAFE(req, &s->tmf_bh_list, next, tmp) {
379             QTAILQ_REMOVE(&s->tmf_bh_list, req, next);
380             QTAILQ_INSERT_TAIL(&reqs, req, next);
381         }
382 
383         qemu_bh_delete(s->tmf_bh);
384         s->tmf_bh = NULL;
385     }
386 
387     QTAILQ_FOREACH_SAFE(req, &reqs, next, tmp) {
388         QTAILQ_REMOVE(&reqs, req, next);
389         virtio_scsi_do_one_tmf_bh(req);
390     }
391 }
392 
393 static void virtio_scsi_reset_tmf_bh(VirtIOSCSI *s)
394 {
395     VirtIOSCSIReq *req;
396     VirtIOSCSIReq *tmp;
397 
398     GLOBAL_STATE_CODE();
399 
400     /* Called after ioeventfd has been stopped, so tmf_bh_lock is not needed */
401     if (s->tmf_bh) {
402         qemu_bh_delete(s->tmf_bh);
403         s->tmf_bh = NULL;
404     }
405 
406     QTAILQ_FOREACH_SAFE(req, &s->tmf_bh_list, next, tmp) {
407         QTAILQ_REMOVE(&s->tmf_bh_list, req, next);
408 
409         /* SAM-6 6.3.2 Hard reset */
410         req->resp.tmf.response = VIRTIO_SCSI_S_TARGET_FAILURE;
411         virtio_scsi_complete_req(req);
412     }
413 }
414 
415 static void virtio_scsi_defer_tmf_to_bh(VirtIOSCSIReq *req)
416 {
417     VirtIOSCSI *s = req->dev;
418 
419     WITH_QEMU_LOCK_GUARD(&s->tmf_bh_lock) {
420         QTAILQ_INSERT_TAIL(&s->tmf_bh_list, req, next);
421 
422         if (!s->tmf_bh) {
423             s->tmf_bh = qemu_bh_new(virtio_scsi_do_tmf_bh, s);
424             qemu_bh_schedule(s->tmf_bh);
425         }
426     }
427 }
428 
429 /* Return 0 if the request is ready to be completed and return to guest;
430  * -EINPROGRESS if the request is submitted and will be completed later, in the
431  *  case of async cancellation. */
432 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
433 {
434     SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun);
435     SCSIRequest *r, *next;
436     int ret = 0;
437 
438     virtio_scsi_ctx_check(s, d);
439     /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE".  */
440     req->resp.tmf.response = VIRTIO_SCSI_S_OK;
441 
442     /*
443      * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s()
444      * to avoid compiler errors.
445      */
446     req->req.tmf.subtype =
447         virtio_tswap32(VIRTIO_DEVICE(s), req->req.tmf.subtype);
448 
449     trace_virtio_scsi_tmf_req(virtio_scsi_get_lun(req->req.tmf.lun),
450                               req->req.tmf.tag, req->req.tmf.subtype);
451 
452     switch (req->req.tmf.subtype) {
453     case VIRTIO_SCSI_T_TMF_ABORT_TASK:
454     case VIRTIO_SCSI_T_TMF_QUERY_TASK:
455         if (!d) {
456             goto fail;
457         }
458         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
459             goto incorrect_lun;
460         }
461         QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
462             VirtIOSCSIReq *cmd_req = r->hba_private;
463             if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) {
464                 break;
465             }
466         }
467         if (r) {
468             /*
469              * Assert that the request has not been completed yet, we
470              * check for it in the loop above.
471              */
472             assert(r->hba_private);
473             if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
474                 /* "If the specified command is present in the task set, then
475                  * return a service response set to FUNCTION SUCCEEDED".
476                  */
477                 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
478             } else {
479                 VirtIOSCSICancelNotifier *notifier;
480 
481                 req->remaining = 1;
482                 notifier = g_new(VirtIOSCSICancelNotifier, 1);
483                 notifier->tmf_req = req;
484                 notifier->notifier.notify = virtio_scsi_cancel_notify;
485                 scsi_req_cancel_async(r, &notifier->notifier);
486                 ret = -EINPROGRESS;
487             }
488         }
489         break;
490 
491     case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
492     case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
493         virtio_scsi_defer_tmf_to_bh(req);
494         ret = -EINPROGRESS;
495         break;
496 
497     case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
498     case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
499     case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
500         if (!d) {
501             goto fail;
502         }
503         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
504             goto incorrect_lun;
505         }
506 
507         /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
508          * This way, if the bus starts calling back to the notifiers
509          * even before we finish the loop, virtio_scsi_cancel_notify
510          * will not complete the TMF too early.
511          */
512         req->remaining = 1;
513         QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
514             if (r->hba_private) {
515                 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
516                     /* "If there is any command present in the task set, then
517                      * return a service response set to FUNCTION SUCCEEDED".
518                      */
519                     req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
520                     break;
521                 } else {
522                     VirtIOSCSICancelNotifier *notifier;
523 
524                     req->remaining++;
525                     notifier = g_new(VirtIOSCSICancelNotifier, 1);
526                     notifier->notifier.notify = virtio_scsi_cancel_notify;
527                     notifier->tmf_req = req;
528                     scsi_req_cancel_async(r, &notifier->notifier);
529                 }
530             }
531         }
532         if (--req->remaining > 0) {
533             ret = -EINPROGRESS;
534         }
535         break;
536 
537     case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
538     default:
539         req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
540         break;
541     }
542 
543     object_unref(OBJECT(d));
544     return ret;
545 
546 incorrect_lun:
547     req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
548     object_unref(OBJECT(d));
549     return ret;
550 
551 fail:
552     req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
553     object_unref(OBJECT(d));
554     return ret;
555 }
556 
557 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
558 {
559     VirtIODevice *vdev = (VirtIODevice *)s;
560     uint32_t type;
561     int r = 0;
562 
563     if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
564                 &type, sizeof(type)) < sizeof(type)) {
565         virtio_scsi_bad_req(req);
566         return;
567     }
568 
569     virtio_tswap32s(vdev, &type);
570     if (type == VIRTIO_SCSI_T_TMF) {
571         if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
572                     sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
573             virtio_scsi_bad_req(req);
574             return;
575         } else {
576             r = virtio_scsi_do_tmf(s, req);
577         }
578 
579     } else if (type == VIRTIO_SCSI_T_AN_QUERY ||
580                type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
581         if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
582                     sizeof(VirtIOSCSICtrlANResp)) < 0) {
583             virtio_scsi_bad_req(req);
584             return;
585         } else {
586             req->req.an.event_requested =
587                 virtio_tswap32(VIRTIO_DEVICE(s), req->req.an.event_requested);
588             trace_virtio_scsi_an_req(virtio_scsi_get_lun(req->req.an.lun),
589                                      req->req.an.event_requested);
590             req->resp.an.event_actual = 0;
591             req->resp.an.response = VIRTIO_SCSI_S_OK;
592         }
593     }
594     if (r == 0) {
595         if (type == VIRTIO_SCSI_T_TMF)
596             trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun),
597                                        req->req.tmf.tag,
598                                        req->resp.tmf.response);
599         else if (type == VIRTIO_SCSI_T_AN_QUERY ||
600                  type == VIRTIO_SCSI_T_AN_SUBSCRIBE)
601             trace_virtio_scsi_an_resp(virtio_scsi_get_lun(req->req.an.lun),
602                                       req->resp.an.response);
603         virtio_scsi_complete_req(req);
604     } else {
605         assert(r == -EINPROGRESS);
606     }
607 }
608 
609 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq)
610 {
611     VirtIOSCSIReq *req;
612 
613     while ((req = virtio_scsi_pop_req(s, vq))) {
614         virtio_scsi_handle_ctrl_req(s, req);
615     }
616 }
617 
618 /*
619  * If dataplane is configured but not yet started, do so now and return true on
620  * success.
621  *
622  * Dataplane is started by the core virtio code but virtqueue handler functions
623  * can also be invoked when a guest kicks before DRIVER_OK, so this helper
624  * function helps us deal with manually starting ioeventfd in that case.
625  */
626 static bool virtio_scsi_defer_to_dataplane(VirtIOSCSI *s)
627 {
628     if (!s->ctx || s->dataplane_started) {
629         return false;
630     }
631 
632     virtio_device_start_ioeventfd(&s->parent_obj.parent_obj);
633     return !s->dataplane_fenced;
634 }
635 
636 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
637 {
638     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
639 
640     if (virtio_scsi_defer_to_dataplane(s)) {
641         return;
642     }
643 
644     virtio_scsi_handle_ctrl_vq(s, vq);
645 }
646 
647 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
648 {
649     trace_virtio_scsi_cmd_resp(virtio_scsi_get_lun(req->req.cmd.lun),
650                                req->req.cmd.tag,
651                                req->resp.cmd.response,
652                                req->resp.cmd.status);
653     /* Sense data is not in req->resp and is copied separately
654      * in virtio_scsi_command_complete.
655      */
656     req->resp_size = sizeof(VirtIOSCSICmdResp);
657     virtio_scsi_complete_req(req);
658 }
659 
660 static void virtio_scsi_command_failed(SCSIRequest *r)
661 {
662     VirtIOSCSIReq *req = r->hba_private;
663 
664     if (r->io_canceled) {
665         return;
666     }
667 
668     req->resp.cmd.status = GOOD;
669     switch (r->host_status) {
670     case SCSI_HOST_NO_LUN:
671         req->resp.cmd.response = VIRTIO_SCSI_S_INCORRECT_LUN;
672         break;
673     case SCSI_HOST_BUSY:
674         req->resp.cmd.response = VIRTIO_SCSI_S_BUSY;
675         break;
676     case SCSI_HOST_TIME_OUT:
677     case SCSI_HOST_ABORTED:
678         req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
679         break;
680     case SCSI_HOST_BAD_RESPONSE:
681         req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
682         break;
683     case SCSI_HOST_RESET:
684         req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
685         break;
686     case SCSI_HOST_TRANSPORT_DISRUPTED:
687         req->resp.cmd.response = VIRTIO_SCSI_S_TRANSPORT_FAILURE;
688         break;
689     case SCSI_HOST_TARGET_FAILURE:
690         req->resp.cmd.response = VIRTIO_SCSI_S_TARGET_FAILURE;
691         break;
692     case SCSI_HOST_RESERVATION_ERROR:
693         req->resp.cmd.response = VIRTIO_SCSI_S_NEXUS_FAILURE;
694         break;
695     case SCSI_HOST_ALLOCATION_FAILURE:
696     case SCSI_HOST_MEDIUM_ERROR:
697     case SCSI_HOST_ERROR:
698     default:
699         req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
700         break;
701     }
702     virtio_scsi_complete_cmd_req(req);
703 }
704 
705 static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid)
706 {
707     VirtIOSCSIReq *req = r->hba_private;
708     uint8_t sense[SCSI_SENSE_BUF_SIZE];
709     uint32_t sense_len;
710     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
711 
712     if (r->io_canceled) {
713         return;
714     }
715 
716     req->resp.cmd.response = VIRTIO_SCSI_S_OK;
717     req->resp.cmd.status = r->status;
718     if (req->resp.cmd.status == GOOD) {
719         req->resp.cmd.resid = virtio_tswap32(vdev, resid);
720     } else {
721         req->resp.cmd.resid = 0;
722         sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
723         sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
724         qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
725                             sense, sense_len);
726         req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
727     }
728     virtio_scsi_complete_cmd_req(req);
729 }
730 
731 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
732                                  uint8_t *buf, size_t buf_len,
733                                  void *hba_private)
734 {
735     VirtIOSCSIReq *req = hba_private;
736 
737     if (cmd->len == 0) {
738         cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE);
739         memcpy(cmd->buf, buf, cmd->len);
740     }
741 
742     /* Extract the direction and mode directly from the request, for
743      * host device passthrough.
744      */
745     cmd->xfer = req->qsgl.size;
746     cmd->mode = req->mode;
747     return 0;
748 }
749 
750 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
751 {
752     VirtIOSCSIReq *req = r->hba_private;
753 
754     return &req->qsgl;
755 }
756 
757 static void virtio_scsi_request_cancelled(SCSIRequest *r)
758 {
759     VirtIOSCSIReq *req = r->hba_private;
760 
761     if (!req) {
762         return;
763     }
764     if (qatomic_read(&req->dev->resetting)) {
765         req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
766     } else {
767         req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
768     }
769     virtio_scsi_complete_cmd_req(req);
770 }
771 
772 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
773 {
774     req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
775     virtio_scsi_complete_cmd_req(req);
776 }
777 
778 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
779 {
780     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
781     SCSIDevice *d;
782     int rc;
783 
784     rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
785                                sizeof(VirtIOSCSICmdResp) + vs->sense_size);
786     if (rc < 0) {
787         if (rc == -ENOTSUP) {
788             virtio_scsi_fail_cmd_req(req);
789             return -ENOTSUP;
790         } else {
791             virtio_scsi_bad_req(req);
792             return -EINVAL;
793         }
794     }
795     trace_virtio_scsi_cmd_req(virtio_scsi_get_lun(req->req.cmd.lun),
796                               req->req.cmd.tag, req->req.cmd.cdb[0]);
797 
798     d = virtio_scsi_device_get(s, req->req.cmd.lun);
799     if (!d) {
800         req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
801         virtio_scsi_complete_cmd_req(req);
802         return -ENOENT;
803     }
804     virtio_scsi_ctx_check(s, d);
805     req->sreq = scsi_req_new(d, req->req.cmd.tag,
806                              virtio_scsi_get_lun(req->req.cmd.lun),
807                              req->req.cmd.cdb, vs->cdb_size, req);
808 
809     if (req->sreq->cmd.mode != SCSI_XFER_NONE
810         && (req->sreq->cmd.mode != req->mode ||
811             req->sreq->cmd.xfer > req->qsgl.size)) {
812         req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
813         virtio_scsi_complete_cmd_req(req);
814         object_unref(OBJECT(d));
815         return -ENOBUFS;
816     }
817     scsi_req_ref(req->sreq);
818     defer_call_begin();
819     object_unref(OBJECT(d));
820     return 0;
821 }
822 
823 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
824 {
825     SCSIRequest *sreq = req->sreq;
826     if (scsi_req_enqueue(sreq)) {
827         scsi_req_continue(sreq);
828     }
829     defer_call_end();
830     scsi_req_unref(sreq);
831 }
832 
833 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq)
834 {
835     VirtIOSCSIReq *req, *next;
836     int ret = 0;
837     bool suppress_notifications = virtio_queue_get_notification(vq);
838 
839     QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
840 
841     do {
842         if (suppress_notifications) {
843             virtio_queue_set_notification(vq, 0);
844         }
845 
846         while ((req = virtio_scsi_pop_req(s, vq))) {
847             ret = virtio_scsi_handle_cmd_req_prepare(s, req);
848             if (!ret) {
849                 QTAILQ_INSERT_TAIL(&reqs, req, next);
850             } else if (ret == -EINVAL) {
851                 /* The device is broken and shouldn't process any request */
852                 while (!QTAILQ_EMPTY(&reqs)) {
853                     req = QTAILQ_FIRST(&reqs);
854                     QTAILQ_REMOVE(&reqs, req, next);
855                     defer_call_end();
856                     scsi_req_unref(req->sreq);
857                     virtqueue_detach_element(req->vq, &req->elem, 0);
858                     virtio_scsi_free_req(req);
859                 }
860             }
861         }
862 
863         if (suppress_notifications) {
864             virtio_queue_set_notification(vq, 1);
865         }
866     } while (ret != -EINVAL && !virtio_queue_empty(vq));
867 
868     QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
869         virtio_scsi_handle_cmd_req_submit(s, req);
870     }
871 }
872 
873 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
874 {
875     /* use non-QOM casts in the data path */
876     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
877 
878     if (virtio_scsi_defer_to_dataplane(s)) {
879         return;
880     }
881 
882     virtio_scsi_handle_cmd_vq(s, vq);
883 }
884 
885 static void virtio_scsi_get_config(VirtIODevice *vdev,
886                                    uint8_t *config)
887 {
888     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
889     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
890 
891     virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
892     virtio_stl_p(vdev, &scsiconf->seg_max,
893                  s->conf.seg_max_adjust ? s->conf.virtqueue_size - 2 : 128 - 2);
894     virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
895     virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
896     virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
897     virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
898     virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
899     virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
900     virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
901     virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
902 }
903 
904 static void virtio_scsi_set_config(VirtIODevice *vdev,
905                                    const uint8_t *config)
906 {
907     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
908     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
909 
910     if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
911         (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
912         virtio_error(vdev,
913                      "bad data written to virtio-scsi configuration space");
914         return;
915     }
916 
917     vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
918     vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
919 }
920 
921 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev,
922                                          uint64_t requested_features,
923                                          Error **errp)
924 {
925     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
926 
927     /* Firstly sync all virtio-scsi possible supported features */
928     requested_features |= s->host_features;
929     return requested_features;
930 }
931 
932 static void virtio_scsi_reset(VirtIODevice *vdev)
933 {
934     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
935     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
936 
937     assert(!s->dataplane_started);
938 
939     virtio_scsi_reset_tmf_bh(s);
940 
941     qatomic_inc(&s->resetting);
942     bus_cold_reset(BUS(&s->bus));
943     qatomic_dec(&s->resetting);
944 
945     vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
946     vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
947     s->events_dropped = false;
948 }
949 
950 typedef struct {
951     uint32_t event;
952     uint32_t reason;
953     union {
954         /* Used by messages specific to a device */
955         struct {
956             uint32_t id;
957             uint32_t lun;
958         } address;
959     };
960 } VirtIOSCSIEventInfo;
961 
962 static void virtio_scsi_push_event(VirtIOSCSI *s,
963                                    const VirtIOSCSIEventInfo *info)
964 {
965     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
966     VirtIOSCSIReq *req;
967     VirtIOSCSIEvent *evt;
968     VirtIODevice *vdev = VIRTIO_DEVICE(s);
969     uint32_t event = info->event;
970     uint32_t reason = info->reason;
971 
972     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
973         return;
974     }
975 
976     req = virtio_scsi_pop_req(s, vs->event_vq);
977     if (!req) {
978         s->events_dropped = true;
979         return;
980     }
981 
982     if (s->events_dropped) {
983         event |= VIRTIO_SCSI_T_EVENTS_MISSED;
984         s->events_dropped = false;
985     }
986 
987     if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
988         virtio_scsi_bad_req(req);
989         return;
990     }
991 
992     evt = &req->resp.event;
993     memset(evt, 0, sizeof(VirtIOSCSIEvent));
994     evt->event = virtio_tswap32(vdev, event);
995     evt->reason = virtio_tswap32(vdev, reason);
996     if (event != VIRTIO_SCSI_T_EVENTS_MISSED) {
997         evt->lun[0] = 1;
998         evt->lun[1] = info->address.id;
999 
1000         /* Linux wants us to keep the same encoding we use for REPORT LUNS.  */
1001         if (info->address.lun >= 256) {
1002             evt->lun[2] = (info->address.lun >> 8) | 0x40;
1003         }
1004         evt->lun[3] = info->address.lun & 0xFF;
1005     }
1006     trace_virtio_scsi_event(virtio_scsi_get_lun(evt->lun), event, reason);
1007 
1008     virtio_scsi_complete_req(req);
1009 }
1010 
1011 static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq)
1012 {
1013     if (s->events_dropped) {
1014         VirtIOSCSIEventInfo info = {
1015             .event = VIRTIO_SCSI_T_NO_EVENT,
1016         };
1017         virtio_scsi_push_event(s, &info);
1018     }
1019 }
1020 
1021 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
1022 {
1023     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1024 
1025     if (virtio_scsi_defer_to_dataplane(s)) {
1026         return;
1027     }
1028 
1029     virtio_scsi_handle_event_vq(s, vq);
1030 }
1031 
1032 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
1033 {
1034     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1035     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1036 
1037     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) &&
1038         dev->type != TYPE_ROM) {
1039         VirtIOSCSIEventInfo info = {
1040             .event   = VIRTIO_SCSI_T_PARAM_CHANGE,
1041             .reason  = sense.asc | (sense.ascq << 8),
1042             .address = {
1043                 .id  = dev->id,
1044                 .lun = dev->lun,
1045             },
1046         };
1047 
1048         virtio_scsi_push_event(s, &info);
1049     }
1050 }
1051 
1052 static void virtio_scsi_pre_hotplug(HotplugHandler *hotplug_dev,
1053                                     DeviceState *dev, Error **errp)
1054 {
1055     SCSIDevice *sd = SCSI_DEVICE(dev);
1056     sd->hba_supports_iothread = true;
1057 }
1058 
1059 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
1060                                 Error **errp)
1061 {
1062     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
1063     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1064     SCSIDevice *sd = SCSI_DEVICE(dev);
1065     int ret;
1066 
1067     if (s->ctx && !s->dataplane_fenced) {
1068         if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
1069             return;
1070         }
1071         ret = blk_set_aio_context(sd->conf.blk, s->ctx, errp);
1072         if (ret < 0) {
1073             return;
1074         }
1075     }
1076 
1077     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
1078         VirtIOSCSIEventInfo info = {
1079             .event   = VIRTIO_SCSI_T_TRANSPORT_RESET,
1080             .reason  = VIRTIO_SCSI_EVT_RESET_RESCAN,
1081             .address = {
1082                 .id  = sd->id,
1083                 .lun = sd->lun,
1084             },
1085         };
1086 
1087         virtio_scsi_push_event(s, &info);
1088         scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
1089     }
1090 }
1091 
1092 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
1093                                   Error **errp)
1094 {
1095     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
1096     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1097     SCSIDevice *sd = SCSI_DEVICE(dev);
1098     VirtIOSCSIEventInfo info = {
1099         .event   = VIRTIO_SCSI_T_TRANSPORT_RESET,
1100         .reason  = VIRTIO_SCSI_EVT_RESET_REMOVED,
1101         .address = {
1102             .id  = sd->id,
1103             .lun = sd->lun,
1104         },
1105     };
1106 
1107     qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
1108 
1109     if (s->ctx) {
1110         /* If other users keep the BlockBackend in the iothread, that's ok */
1111         blk_set_aio_context(sd->conf.blk, qemu_get_aio_context(), NULL);
1112     }
1113 
1114     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
1115         virtio_scsi_push_event(s, &info);
1116         scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
1117     }
1118 }
1119 
1120 /* Suspend virtqueue ioeventfd processing during drain */
1121 static void virtio_scsi_drained_begin(SCSIBus *bus)
1122 {
1123     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1124     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1125     uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED +
1126                             s->parent_obj.conf.num_queues;
1127 
1128     /*
1129      * Drain is called when stopping dataplane but the host notifier has
1130      * already been detached. Detaching multiple times is a no-op if nothing
1131      * else is using the monitoring same file descriptor, but avoid it just in
1132      * case.
1133      *
1134      * Also, don't detach if dataplane has not even been started yet because
1135      * the host notifier isn't attached.
1136      */
1137     if (s->dataplane_stopping || !s->dataplane_started) {
1138         return;
1139     }
1140 
1141     for (uint32_t i = 0; i < total_queues; i++) {
1142         VirtQueue *vq = virtio_get_queue(vdev, i);
1143         virtio_queue_aio_detach_host_notifier(vq, s->ctx);
1144     }
1145 }
1146 
1147 /* Resume virtqueue ioeventfd processing after drain */
1148 static void virtio_scsi_drained_end(SCSIBus *bus)
1149 {
1150     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1151     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
1152     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1153     uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED +
1154                             s->parent_obj.conf.num_queues;
1155 
1156     /*
1157      * Drain is called when stopping dataplane. Keep the host notifier detached
1158      * so it's not left dangling after dataplane is stopped.
1159      *
1160      * Also, don't attach if dataplane has not even been started yet. We're not
1161      * ready.
1162      */
1163     if (s->dataplane_stopping || !s->dataplane_started) {
1164         return;
1165     }
1166 
1167     for (uint32_t i = 0; i < total_queues; i++) {
1168         VirtQueue *vq = virtio_get_queue(vdev, i);
1169         if (vq == vs->event_vq) {
1170             virtio_queue_aio_attach_host_notifier_no_poll(vq, s->ctx);
1171         } else {
1172             virtio_queue_aio_attach_host_notifier(vq, s->ctx);
1173         }
1174     }
1175 }
1176 
1177 static struct SCSIBusInfo virtio_scsi_scsi_info = {
1178     .tcq = true,
1179     .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
1180     .max_target = VIRTIO_SCSI_MAX_TARGET,
1181     .max_lun = VIRTIO_SCSI_MAX_LUN,
1182 
1183     .complete = virtio_scsi_command_complete,
1184     .fail = virtio_scsi_command_failed,
1185     .cancel = virtio_scsi_request_cancelled,
1186     .change = virtio_scsi_change,
1187     .parse_cdb = virtio_scsi_parse_cdb,
1188     .get_sg_list = virtio_scsi_get_sg_list,
1189     .save_request = virtio_scsi_save_request,
1190     .load_request = virtio_scsi_load_request,
1191     .drained_begin = virtio_scsi_drained_begin,
1192     .drained_end = virtio_scsi_drained_end,
1193 };
1194 
1195 void virtio_scsi_common_realize(DeviceState *dev,
1196                                 VirtIOHandleOutput ctrl,
1197                                 VirtIOHandleOutput evt,
1198                                 VirtIOHandleOutput cmd,
1199                                 Error **errp)
1200 {
1201     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1202     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
1203     int i;
1204 
1205     virtio_init(vdev, VIRTIO_ID_SCSI, sizeof(VirtIOSCSIConfig));
1206 
1207     if (s->conf.num_queues == VIRTIO_SCSI_AUTO_NUM_QUEUES) {
1208         s->conf.num_queues = 1;
1209     }
1210     if (s->conf.num_queues == 0 ||
1211             s->conf.num_queues > VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED) {
1212         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1213                          "must be a positive integer less than %d.",
1214                    s->conf.num_queues,
1215                    VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED);
1216         virtio_cleanup(vdev);
1217         return;
1218     }
1219     if (s->conf.virtqueue_size <= 2) {
1220         error_setg(errp, "invalid virtqueue_size property (= %" PRIu32 "), "
1221                    "must be > 2", s->conf.virtqueue_size);
1222         return;
1223     }
1224     s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues);
1225     s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
1226     s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
1227 
1228     s->ctrl_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, ctrl);
1229     s->event_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, evt);
1230     for (i = 0; i < s->conf.num_queues; i++) {
1231         s->cmd_vqs[i] = virtio_add_queue(vdev, s->conf.virtqueue_size, cmd);
1232     }
1233 }
1234 
1235 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
1236 {
1237     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1238     VirtIOSCSI *s = VIRTIO_SCSI(dev);
1239     Error *err = NULL;
1240 
1241     QTAILQ_INIT(&s->tmf_bh_list);
1242     qemu_mutex_init(&s->tmf_bh_lock);
1243 
1244     virtio_scsi_common_realize(dev,
1245                                virtio_scsi_handle_ctrl,
1246                                virtio_scsi_handle_event,
1247                                virtio_scsi_handle_cmd,
1248                                &err);
1249     if (err != NULL) {
1250         error_propagate(errp, err);
1251         return;
1252     }
1253 
1254     scsi_bus_init_named(&s->bus, sizeof(s->bus), dev,
1255                        &virtio_scsi_scsi_info, vdev->bus_name);
1256     /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
1257     qbus_set_hotplug_handler(BUS(&s->bus), OBJECT(dev));
1258 
1259     virtio_scsi_dataplane_setup(s, errp);
1260 }
1261 
1262 void virtio_scsi_common_unrealize(DeviceState *dev)
1263 {
1264     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1265     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
1266     int i;
1267 
1268     virtio_delete_queue(vs->ctrl_vq);
1269     virtio_delete_queue(vs->event_vq);
1270     for (i = 0; i < vs->conf.num_queues; i++) {
1271         virtio_delete_queue(vs->cmd_vqs[i]);
1272     }
1273     g_free(vs->cmd_vqs);
1274     virtio_cleanup(vdev);
1275 }
1276 
1277 static void virtio_scsi_device_unrealize(DeviceState *dev)
1278 {
1279     VirtIOSCSI *s = VIRTIO_SCSI(dev);
1280 
1281     virtio_scsi_reset_tmf_bh(s);
1282 
1283     qbus_set_hotplug_handler(BUS(&s->bus), NULL);
1284     virtio_scsi_common_unrealize(dev);
1285     qemu_mutex_destroy(&s->tmf_bh_lock);
1286 }
1287 
1288 static Property virtio_scsi_properties[] = {
1289     DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues,
1290                        VIRTIO_SCSI_AUTO_NUM_QUEUES),
1291     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI,
1292                                          parent_obj.conf.virtqueue_size, 256),
1293     DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI,
1294                       parent_obj.conf.seg_max_adjust, true),
1295     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors,
1296                                                   0xFFFF),
1297     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun,
1298                                                   128),
1299     DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features,
1300                                            VIRTIO_SCSI_F_HOTPLUG, true),
1301     DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features,
1302                                                 VIRTIO_SCSI_F_CHANGE, true),
1303     DEFINE_PROP_LINK("iothread", VirtIOSCSI, parent_obj.conf.iothread,
1304                      TYPE_IOTHREAD, IOThread *),
1305     DEFINE_PROP_END_OF_LIST(),
1306 };
1307 
1308 static const VMStateDescription vmstate_virtio_scsi = {
1309     .name = "virtio-scsi",
1310     .minimum_version_id = 1,
1311     .version_id = 1,
1312     .fields = (const VMStateField[]) {
1313         VMSTATE_VIRTIO_DEVICE,
1314         VMSTATE_END_OF_LIST()
1315     },
1316 };
1317 
1318 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data)
1319 {
1320     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1321     DeviceClass *dc = DEVICE_CLASS(klass);
1322 
1323     vdc->get_config = virtio_scsi_get_config;
1324     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1325 }
1326 
1327 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
1328 {
1329     DeviceClass *dc = DEVICE_CLASS(klass);
1330     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1331     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1332 
1333     device_class_set_props(dc, virtio_scsi_properties);
1334     dc->vmsd = &vmstate_virtio_scsi;
1335     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1336     vdc->realize = virtio_scsi_device_realize;
1337     vdc->unrealize = virtio_scsi_device_unrealize;
1338     vdc->set_config = virtio_scsi_set_config;
1339     vdc->get_features = virtio_scsi_get_features;
1340     vdc->reset = virtio_scsi_reset;
1341     vdc->start_ioeventfd = virtio_scsi_dataplane_start;
1342     vdc->stop_ioeventfd = virtio_scsi_dataplane_stop;
1343     hc->pre_plug = virtio_scsi_pre_hotplug;
1344     hc->plug = virtio_scsi_hotplug;
1345     hc->unplug = virtio_scsi_hotunplug;
1346 }
1347 
1348 static const TypeInfo virtio_scsi_common_info = {
1349     .name = TYPE_VIRTIO_SCSI_COMMON,
1350     .parent = TYPE_VIRTIO_DEVICE,
1351     .instance_size = sizeof(VirtIOSCSICommon),
1352     .abstract = true,
1353     .class_init = virtio_scsi_common_class_init,
1354 };
1355 
1356 static const TypeInfo virtio_scsi_info = {
1357     .name = TYPE_VIRTIO_SCSI,
1358     .parent = TYPE_VIRTIO_SCSI_COMMON,
1359     .instance_size = sizeof(VirtIOSCSI),
1360     .class_init = virtio_scsi_class_init,
1361     .interfaces = (InterfaceInfo[]) {
1362         { TYPE_HOTPLUG_HANDLER },
1363         { }
1364     }
1365 };
1366 
1367 static void virtio_register_types(void)
1368 {
1369     type_register_static(&virtio_scsi_common_info);
1370     type_register_static(&virtio_scsi_info);
1371 }
1372 
1373 type_init(virtio_register_types)
1374