xref: /openbmc/qemu/hw/scsi/scsi-bus.c (revision 5d7a40b5b280cd82f24a9b4e5f4557e200111030)
1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qemu/error-report.h"
4 #include "qemu/module.h"
5 #include "qemu/option.h"
6 #include "qemu/hw-version.h"
7 #include "hw/qdev-properties.h"
8 #include "hw/scsi/scsi.h"
9 #include "migration/qemu-file-types.h"
10 #include "migration/vmstate.h"
11 #include "scsi/constants.h"
12 #include "system/block-backend.h"
13 #include "system/blockdev.h"
14 #include "system/system.h"
15 #include "system/runstate.h"
16 #include "trace.h"
17 #include "system/dma.h"
18 #include "qemu/cutils.h"
19 
20 static char *scsibus_get_dev_path(DeviceState *dev);
21 static char *scsibus_get_fw_dev_path(DeviceState *dev);
22 static void scsi_req_dequeue(SCSIRequest *req);
23 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
24 static void scsi_target_free_buf(SCSIRequest *req);
25 static void scsi_clear_reported_luns_changed(SCSIRequest *req);
26 
27 static int next_scsi_bus;
28 
29 static SCSIDevice *do_scsi_device_find(SCSIBus *bus,
30                                        int channel, int id, int lun,
31                                        bool include_unrealized)
32 {
33     BusChild *kid;
34     SCSIDevice *retval = NULL;
35 
36     QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) {
37         DeviceState *qdev = kid->child;
38         SCSIDevice *dev = SCSI_DEVICE(qdev);
39 
40         if (dev->channel == channel && dev->id == id) {
41             if (dev->lun == lun) {
42                 retval = dev;
43                 break;
44             }
45 
46             /*
47              * If we don't find exact match (channel/bus/lun),
48              * we will return the first device which matches channel/bus
49              */
50 
51             if (!retval) {
52                 retval = dev;
53             }
54         }
55     }
56 
57     /*
58      * This function might run on the IO thread and we might race against
59      * main thread hot-plugging the device.
60      * We assume that as soon as .realized is set to true we can let
61      * the user access the device.
62      */
63 
64     if (retval && !include_unrealized && !qdev_is_realized(&retval->qdev)) {
65         retval = NULL;
66     }
67 
68     return retval;
69 }
70 
71 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
72 {
73     RCU_READ_LOCK_GUARD();
74     return do_scsi_device_find(bus, channel, id, lun, false);
75 }
76 
77 SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
78 {
79     SCSIDevice *d;
80     RCU_READ_LOCK_GUARD();
81     d = do_scsi_device_find(bus, channel, id, lun, false);
82     if (d) {
83         object_ref(d);
84     }
85     return d;
86 }
87 
88 /*
89  * Invoke @fn() for each enqueued request in device @s. Must be called from the
90  * main loop thread while the guest is stopped. This is only suitable for
91  * vmstate ->put(), use scsi_device_for_each_req_async() for other cases.
92  */
93 static void scsi_device_for_each_req_sync(SCSIDevice *s,
94                                           void (*fn)(SCSIRequest *, void *),
95                                           void *opaque)
96 {
97     SCSIRequest *req;
98     SCSIRequest *next_req;
99 
100     assert(!runstate_is_running());
101     assert(qemu_in_main_thread());
102 
103     /*
104      * Locking is not necessary because the guest is stopped and no other
105      * threads can be accessing the requests list, but take the lock for
106      * consistency.
107      */
108     WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
109         QTAILQ_FOREACH_SAFE(req, &s->requests, next, next_req) {
110             fn(req, opaque);
111         }
112     }
113 }
114 
115 typedef struct {
116     SCSIDevice *s;
117     void (*fn)(SCSIRequest *, void *);
118     void *fn_opaque;
119 } SCSIDeviceForEachReqAsyncData;
120 
121 static void scsi_device_for_each_req_async_bh(void *opaque)
122 {
123     g_autofree SCSIDeviceForEachReqAsyncData *data = opaque;
124     SCSIDevice *s = data->s;
125     g_autoptr(GList) reqs = NULL;
126 
127     /*
128      * Build a list of requests in this AioContext so fn() can be invoked later
129      * outside requests_lock.
130      */
131     WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
132         AioContext *ctx = qemu_get_current_aio_context();
133         SCSIRequest *req;
134         SCSIRequest *next;
135 
136         QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
137             if (req->ctx == ctx) {
138                 scsi_req_ref(req); /* dropped after calling fn() */
139                 reqs = g_list_prepend(reqs, req);
140             }
141         }
142     }
143 
144     /* Call fn() on each request */
145     for (GList *elem = g_list_first(reqs); elem; elem = g_list_next(elem)) {
146         data->fn(elem->data, data->fn_opaque);
147         scsi_req_unref(elem->data);
148     }
149 
150     /* Drop the reference taken by scsi_device_for_each_req_async() */
151     object_unref(OBJECT(s));
152 
153     /* Paired with blk_inc_in_flight() in scsi_device_for_each_req_async() */
154     blk_dec_in_flight(s->conf.blk);
155 }
156 
157 static void scsi_device_for_each_req_async_do_ctx(gpointer key, gpointer value,
158                                                   gpointer user_data)
159 {
160     AioContext *ctx = key;
161     SCSIDeviceForEachReqAsyncData *params = user_data;
162     SCSIDeviceForEachReqAsyncData *data;
163 
164     data = g_new(SCSIDeviceForEachReqAsyncData, 1);
165     data->s = params->s;
166     data->fn = params->fn;
167     data->fn_opaque = params->fn_opaque;
168 
169     /*
170      * Hold a reference to the SCSIDevice until
171      * scsi_device_for_each_req_async_bh() finishes.
172      */
173     object_ref(OBJECT(data->s));
174 
175     /* Paired with scsi_device_for_each_req_async_bh() */
176     blk_inc_in_flight(data->s->conf.blk);
177 
178     aio_bh_schedule_oneshot(ctx, scsi_device_for_each_req_async_bh, data);
179 }
180 
181 /*
182  * Schedule @fn() to be invoked for each enqueued request in device @s. @fn()
183  * must be thread-safe because it runs concurrently in each AioContext that is
184  * executing a request.
185  *
186  * Keeps the BlockBackend's in-flight counter incremented until everything is
187  * done, so draining it will settle all scheduled @fn() calls.
188  */
189 static void scsi_device_for_each_req_async(SCSIDevice *s,
190                                            void (*fn)(SCSIRequest *, void *),
191                                            void *opaque)
192 {
193     assert(qemu_in_main_thread());
194 
195     /* The set of AioContexts where the requests are being processed */
196     g_autoptr(GHashTable) aio_contexts = g_hash_table_new(NULL, NULL);
197     WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
198         SCSIRequest *req;
199         QTAILQ_FOREACH(req, &s->requests, next) {
200             g_hash_table_add(aio_contexts, req->ctx);
201         }
202     }
203 
204     /* Schedule a BH for each AioContext */
205     SCSIDeviceForEachReqAsyncData params = {
206         .s = s,
207         .fn = fn,
208         .fn_opaque = opaque,
209     };
210     g_hash_table_foreach(
211             aio_contexts,
212             scsi_device_for_each_req_async_do_ctx,
213             &params
214     );
215 }
216 
217 static void scsi_device_realize(SCSIDevice *s, Error **errp)
218 {
219     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
220     if (sc->realize) {
221         sc->realize(s, errp);
222     }
223 }
224 
225 static void scsi_device_unrealize(SCSIDevice *s)
226 {
227     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
228     if (sc->unrealize) {
229         sc->unrealize(s);
230     }
231 }
232 
233 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
234                        size_t buf_len, void *hba_private)
235 {
236     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
237     int rc;
238 
239     assert(cmd->len == 0);
240     rc = scsi_req_parse_cdb(dev, cmd, buf, buf_len);
241     if (bus->info->parse_cdb) {
242         rc = bus->info->parse_cdb(dev, cmd, buf, buf_len, hba_private);
243     }
244     return rc;
245 }
246 
247 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
248                                           uint8_t *buf, void *hba_private)
249 {
250     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
251     if (sc->alloc_req) {
252         return sc->alloc_req(s, tag, lun, buf, hba_private);
253     }
254 
255     return NULL;
256 }
257 
258 void scsi_device_unit_attention_reported(SCSIDevice *s)
259 {
260     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
261     if (sc->unit_attention_reported) {
262         sc->unit_attention_reported(s);
263     }
264 }
265 
266 /* Create a scsi bus, and attach devices to it.  */
267 void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
268                          const SCSIBusInfo *info, const char *bus_name)
269 {
270     qbus_init(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
271     bus->busnr = next_scsi_bus++;
272     bus->info = info;
273     qbus_set_bus_hotplug_handler(BUS(bus));
274 }
275 
276 void scsi_req_retry(SCSIRequest *req)
277 {
278     req->retry = true;
279 }
280 
281 /* Called in the AioContext that is executing the request */
282 static void scsi_dma_restart_req(SCSIRequest *req, void *opaque)
283 {
284     scsi_req_ref(req);
285     if (req->retry) {
286         req->retry = false;
287         switch (req->cmd.mode) {
288             case SCSI_XFER_FROM_DEV:
289             case SCSI_XFER_TO_DEV:
290                 scsi_req_continue(req);
291                 break;
292             case SCSI_XFER_NONE:
293                 scsi_req_dequeue(req);
294                 scsi_req_enqueue(req);
295                 break;
296         }
297     }
298     scsi_req_unref(req);
299 }
300 
301 static void scsi_dma_restart_cb(void *opaque, bool running, RunState state)
302 {
303     SCSIDevice *s = opaque;
304 
305     assert(qemu_in_main_thread());
306 
307     if (!running) {
308         return;
309     }
310 
311     scsi_device_for_each_req_async(s, scsi_dma_restart_req, NULL);
312 }
313 
314 static bool scsi_bus_is_address_free(SCSIBus *bus,
315                                      int channel, int target, int lun,
316                                      SCSIDevice **p_dev)
317 {
318     SCSIDevice *d;
319 
320     RCU_READ_LOCK_GUARD();
321     d = do_scsi_device_find(bus, channel, target, lun, true);
322     if (d && d->lun == lun) {
323         if (p_dev) {
324             *p_dev = d;
325         }
326         return false;
327     }
328     if (p_dev) {
329         *p_dev = NULL;
330     }
331     return true;
332 }
333 
334 static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Error **errp)
335 {
336     SCSIDevice *dev = SCSI_DEVICE(qdev);
337     SCSIBus *bus = SCSI_BUS(qbus);
338 
339     if (dev->channel > bus->info->max_channel) {
340         error_setg(errp, "bad scsi channel id: %d", dev->channel);
341         return false;
342     }
343     if (dev->id != -1 && dev->id > bus->info->max_target) {
344         error_setg(errp, "bad scsi device id: %d", dev->id);
345         return false;
346     }
347     if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
348         error_setg(errp, "bad scsi device lun: %d", dev->lun);
349         return false;
350     }
351 
352     if (dev->id != -1 && dev->lun != -1) {
353         SCSIDevice *d;
354         if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun, &d)) {
355             error_setg(errp, "lun already used by '%s'", d->qdev.id);
356             return false;
357         }
358     }
359 
360     return true;
361 }
362 
363 static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
364 {
365     SCSIDevice *dev = SCSI_DEVICE(qdev);
366     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
367     bool is_free;
368     Error *local_err = NULL;
369 
370     if (dev->id == -1) {
371         int id = -1;
372         if (dev->lun == -1) {
373             dev->lun = 0;
374         }
375         do {
376             is_free = scsi_bus_is_address_free(bus, dev->channel, ++id, dev->lun, NULL);
377         } while (!is_free && id < bus->info->max_target);
378         if (!is_free) {
379             error_setg(errp, "no free target");
380             return;
381         }
382         dev->id = id;
383     } else if (dev->lun == -1) {
384         int lun = -1;
385         do {
386             is_free = scsi_bus_is_address_free(bus, dev->channel, dev->id, ++lun, NULL);
387         } while (!is_free && lun < bus->info->max_lun);
388         if (!is_free) {
389             error_setg(errp, "no free lun");
390             return;
391         }
392         dev->lun = lun;
393     }
394 
395     qemu_mutex_init(&dev->requests_lock);
396     QTAILQ_INIT(&dev->requests);
397     scsi_device_realize(dev, &local_err);
398     if (local_err) {
399         error_propagate(errp, local_err);
400         return;
401     }
402     dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
403             scsi_dma_restart_cb, NULL, dev);
404 }
405 
406 static void scsi_qdev_unrealize(DeviceState *qdev)
407 {
408     SCSIDevice *dev = SCSI_DEVICE(qdev);
409 
410     if (dev->vmsentry) {
411         qemu_del_vm_change_state_handler(dev->vmsentry);
412     }
413 
414     scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
415 
416     qemu_mutex_destroy(&dev->requests_lock);
417 
418     scsi_device_unrealize(dev);
419 
420     blockdev_mark_auto_del(dev->conf.blk);
421 }
422 
423 /* handle legacy '-drive if=scsi,...' cmd line args */
424 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
425                                       int unit, bool removable, BlockConf *conf,
426                                       const char *serial, Error **errp)
427 {
428     const char *driver;
429     char *name;
430     DeviceState *dev;
431     SCSIDevice *s;
432     DriveInfo *dinfo;
433     Error *local_err = NULL;
434 
435     if (blk_is_sg(blk)) {
436         driver = "scsi-generic";
437     } else {
438         dinfo = blk_legacy_dinfo(blk);
439         if (dinfo && dinfo->media_cd) {
440             driver = "scsi-cd";
441         } else {
442             driver = "scsi-hd";
443         }
444     }
445     dev = qdev_new(driver);
446     name = g_strdup_printf("legacy[%d]", unit);
447     object_property_add_child(OBJECT(bus), name, OBJECT(dev));
448     g_free(name);
449 
450     s = SCSI_DEVICE(dev);
451     s->conf = *conf;
452 
453     check_boot_index(conf->bootindex, &local_err);
454     if (local_err) {
455         object_unparent(OBJECT(dev));
456         error_propagate(errp, local_err);
457         return NULL;
458     }
459     add_boot_device_path(conf->bootindex, dev, NULL);
460 
461     qdev_prop_set_uint32(dev, "scsi-id", unit);
462     if (object_property_find(OBJECT(dev), "removable")) {
463         qdev_prop_set_bit(dev, "removable", removable);
464     }
465     if (serial && object_property_find(OBJECT(dev), "serial")) {
466         qdev_prop_set_string(dev, "serial", serial);
467     }
468     if (!qdev_prop_set_drive_err(dev, "drive", blk, errp)) {
469         object_unparent(OBJECT(dev));
470         return NULL;
471     }
472 
473     if (!qdev_realize_and_unref(dev, &bus->qbus, errp)) {
474         object_unparent(OBJECT(dev));
475         return NULL;
476     }
477     return s;
478 }
479 
480 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
481 {
482     Location loc;
483     DriveInfo *dinfo;
484     int unit;
485     BlockConf conf = {
486         .bootindex = -1,
487         .share_rw = false,
488         .rerror = BLOCKDEV_ON_ERROR_AUTO,
489         .werror = BLOCKDEV_ON_ERROR_AUTO,
490     };
491 
492     loc_push_none(&loc);
493     for (unit = 0; unit <= bus->info->max_target; unit++) {
494         dinfo = drive_get(IF_SCSI, bus->busnr, unit);
495         if (dinfo == NULL) {
496             continue;
497         }
498         qemu_opts_loc_restore(dinfo->opts);
499         scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
500                                   unit, false, &conf, NULL, &error_fatal);
501     }
502     loc_pop(&loc);
503 }
504 
505 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
506 {
507     scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
508     scsi_req_complete(req, CHECK_CONDITION);
509     return 0;
510 }
511 
512 static const struct SCSIReqOps reqops_invalid_field = {
513     .size         = sizeof(SCSIRequest),
514     .send_command = scsi_invalid_field
515 };
516 
517 /* SCSIReqOps implementation for invalid commands.  */
518 
519 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
520 {
521     scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
522     scsi_req_complete(req, CHECK_CONDITION);
523     return 0;
524 }
525 
526 static const struct SCSIReqOps reqops_invalid_opcode = {
527     .size         = sizeof(SCSIRequest),
528     .send_command = scsi_invalid_command
529 };
530 
531 /* SCSIReqOps implementation for unit attention conditions.  */
532 
533 static void scsi_fetch_unit_attention_sense(SCSIRequest *req)
534 {
535     SCSISense *ua = NULL;
536 
537     if (req->dev->unit_attention.key == UNIT_ATTENTION) {
538         ua = &req->dev->unit_attention;
539     } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
540         ua = &req->bus->unit_attention;
541     }
542 
543     /*
544      * Fetch the unit attention sense immediately so that another
545      * scsi_req_new does not use reqops_unit_attention.
546      */
547     if (ua) {
548         scsi_req_build_sense(req, *ua);
549         *ua = SENSE_CODE(NO_SENSE);
550     }
551 }
552 
553 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
554 {
555     scsi_req_complete(req, CHECK_CONDITION);
556     return 0;
557 }
558 
559 static const struct SCSIReqOps reqops_unit_attention = {
560     .size         = sizeof(SCSIRequest),
561     .init_req     = scsi_fetch_unit_attention_sense,
562     .send_command = scsi_unit_attention
563 };
564 
565 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
566    an invalid LUN.  */
567 
568 typedef struct SCSITargetReq SCSITargetReq;
569 
570 struct SCSITargetReq {
571     SCSIRequest req;
572     int len;
573     uint8_t *buf;
574     int buf_len;
575 };
576 
577 static void store_lun(uint8_t *outbuf, int lun)
578 {
579     if (lun < 256) {
580         /* Simple logical unit addressing method*/
581         outbuf[0] = 0;
582         outbuf[1] = lun;
583     } else {
584         /* Flat space addressing method */
585         outbuf[0] = 0x40 | (lun >> 8);
586         outbuf[1] = (lun & 255);
587     }
588 }
589 
590 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
591 {
592     BusChild *kid;
593     int channel, id;
594     uint8_t tmp[8] = {0};
595     int len = 0;
596     GByteArray *buf;
597 
598     if (r->req.cmd.xfer < 16) {
599         return false;
600     }
601     if (r->req.cmd.buf[2] > 2) {
602         return false;
603     }
604 
605     /* reserve space for 63 LUNs*/
606     buf = g_byte_array_sized_new(512);
607 
608     channel = r->req.dev->channel;
609     id = r->req.dev->id;
610 
611     /* add size (will be updated later to correct value */
612     g_byte_array_append(buf, tmp, 8);
613     len += 8;
614 
615     /* add LUN0 */
616     g_byte_array_append(buf, tmp, 8);
617     len += 8;
618 
619     WITH_RCU_READ_LOCK_GUARD() {
620         QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
621             DeviceState *qdev = kid->child;
622             SCSIDevice *dev = SCSI_DEVICE(qdev);
623 
624             if (dev->channel == channel && dev->id == id && dev->lun != 0 &&
625                 qdev_is_realized(&dev->qdev)) {
626                 store_lun(tmp, dev->lun);
627                 g_byte_array_append(buf, tmp, 8);
628                 len += 8;
629             }
630         }
631     }
632 
633     r->buf_len = len;
634     r->buf = g_byte_array_free(buf, FALSE);
635     r->len = MIN(len, r->req.cmd.xfer & ~7);
636 
637     /* store the LUN list length */
638     stl_be_p(&r->buf[0], len - 8);
639 
640     /*
641      * If a REPORT LUNS command enters the enabled command state, [...]
642      * the device server shall clear any pending unit attention condition
643      * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
644      */
645     scsi_clear_reported_luns_changed(&r->req);
646 
647     return true;
648 }
649 
650 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
651 {
652     assert(r->req.dev->lun != r->req.lun);
653 
654     scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
655 
656     if (r->req.cmd.buf[1] & 0x2) {
657         /* Command support data - optional, not implemented */
658         return false;
659     }
660 
661     if (r->req.cmd.buf[1] & 0x1) {
662         /* Vital product data */
663         uint8_t page_code = r->req.cmd.buf[2];
664         r->buf[r->len++] = page_code ; /* this page */
665         r->buf[r->len++] = 0x00;
666 
667         switch (page_code) {
668         case 0x00: /* Supported page codes, mandatory */
669         {
670             int pages;
671             pages = r->len++;
672             r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
673             r->buf[pages] = r->len - pages - 1; /* number of pages */
674             break;
675         }
676         default:
677             return false;
678         }
679         /* done with EVPD */
680         assert(r->len < r->buf_len);
681         r->len = MIN(r->req.cmd.xfer, r->len);
682         return true;
683     }
684 
685     /* Standard INQUIRY data */
686     if (r->req.cmd.buf[2] != 0) {
687         return false;
688     }
689 
690     /* PAGE CODE == 0 */
691     r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
692     memset(r->buf, 0, r->len);
693     if (r->req.lun != 0) {
694         r->buf[0] = TYPE_NO_LUN;
695     } else {
696         r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
697         r->buf[2] = 5; /* Version */
698         r->buf[3] = 2 | 0x10; /* HiSup, response data format */
699         r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
700         r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ.  */
701         memcpy(&r->buf[8], "QEMU    ", 8);
702         memcpy(&r->buf[16], "QEMU TARGET     ", 16);
703         pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
704     }
705     return true;
706 }
707 
708 static size_t scsi_sense_len(SCSIRequest *req)
709 {
710     if (req->dev->type == TYPE_SCANNER)
711         return SCSI_SENSE_LEN_SCANNER;
712     else
713         return SCSI_SENSE_LEN;
714 }
715 
716 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
717 {
718     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
719     int fixed_sense = (req->cmd.buf[1] & 1) == 0;
720 
721     if (req->lun != 0 &&
722         buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
723         scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
724         scsi_req_complete(req, CHECK_CONDITION);
725         return 0;
726     }
727     switch (buf[0]) {
728     case REPORT_LUNS:
729         if (!scsi_target_emulate_report_luns(r)) {
730             goto illegal_request;
731         }
732         break;
733     case INQUIRY:
734         if (!scsi_target_emulate_inquiry(r)) {
735             goto illegal_request;
736         }
737         break;
738     case REQUEST_SENSE:
739         scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
740         if (req->lun != 0) {
741             const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
742 
743             r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
744                                           sense, fixed_sense);
745         } else {
746             r->len = scsi_device_get_sense(r->req.dev, r->buf,
747                                            MIN(req->cmd.xfer, r->buf_len),
748                                            fixed_sense);
749         }
750         if (r->req.dev->sense_is_ua) {
751             scsi_device_unit_attention_reported(req->dev);
752             r->req.dev->sense_len = 0;
753             r->req.dev->sense_is_ua = false;
754         }
755         break;
756     case TEST_UNIT_READY:
757         break;
758     default:
759         scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
760         scsi_req_complete(req, CHECK_CONDITION);
761         return 0;
762     illegal_request:
763         scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
764         scsi_req_complete(req, CHECK_CONDITION);
765         return 0;
766     }
767 
768     if (!r->len) {
769         scsi_req_complete(req, GOOD);
770     }
771     return r->len;
772 }
773 
774 static void scsi_target_read_data(SCSIRequest *req)
775 {
776     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
777     uint32_t n;
778 
779     n = r->len;
780     if (n > 0) {
781         r->len = 0;
782         scsi_req_data(&r->req, n);
783     } else {
784         scsi_req_complete(&r->req, GOOD);
785     }
786 }
787 
788 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
789 {
790     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
791 
792     return r->buf;
793 }
794 
795 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
796 {
797     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
798 
799     r->buf = g_malloc(len);
800     r->buf_len = len;
801 
802     return r->buf;
803 }
804 
805 static void scsi_target_free_buf(SCSIRequest *req)
806 {
807     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
808 
809     g_free(r->buf);
810 }
811 
812 static const struct SCSIReqOps reqops_target_command = {
813     .size         = sizeof(SCSITargetReq),
814     .send_command = scsi_target_send_command,
815     .read_data    = scsi_target_read_data,
816     .get_buf      = scsi_target_get_buf,
817     .free_req     = scsi_target_free_buf,
818 };
819 
820 
821 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
822                             uint32_t tag, uint32_t lun, void *hba_private)
823 {
824     SCSIRequest *req;
825     SCSIBus *bus = scsi_bus_from_device(d);
826     const int memset_off = offsetof(SCSIRequest, sense)
827                            + sizeof(req->sense);
828 
829     req = g_malloc(reqops->size);
830     memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
831     req->refcount = 1;
832     req->bus = bus;
833     req->dev = d;
834     req->tag = tag;
835     req->lun = lun;
836     req->hba_private = hba_private;
837     req->status = -1;
838     req->host_status = -1;
839     req->ops = reqops;
840     notifier_list_init(&req->cancel_notifiers);
841 
842     if (reqops->init_req) {
843         reqops->init_req(req);
844     }
845 
846     trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
847     return req;
848 }
849 
850 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
851                           uint8_t *buf, size_t buf_len, void *hba_private)
852 {
853     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
854     const SCSIReqOps *ops;
855     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
856     SCSIRequest *req;
857     SCSICommand cmd = { .len = 0 };
858     int ret;
859 
860     if (buf_len == 0) {
861         trace_scsi_req_parse_bad(d->id, lun, tag, 0);
862         goto invalid_opcode;
863     }
864 
865     if ((d->unit_attention.key == UNIT_ATTENTION ||
866          bus->unit_attention.key == UNIT_ATTENTION) &&
867         (buf[0] != INQUIRY &&
868          buf[0] != REPORT_LUNS &&
869          buf[0] != GET_CONFIGURATION &&
870          buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
871 
872          /*
873           * If we already have a pending unit attention condition,
874           * report this one before triggering another one.
875           */
876          !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
877         ops = &reqops_unit_attention;
878     } else if (lun != d->lun ||
879                buf[0] == REPORT_LUNS ||
880                (buf[0] == REQUEST_SENSE && d->sense_len)) {
881         ops = &reqops_target_command;
882     } else {
883         ops = NULL;
884     }
885 
886     if (ops != NULL || !sc->parse_cdb) {
887         ret = scsi_req_parse_cdb(d, &cmd, buf, buf_len);
888     } else {
889         ret = sc->parse_cdb(d, &cmd, buf, buf_len, hba_private);
890     }
891 
892     if (ret != 0) {
893         trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
894 invalid_opcode:
895         req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
896     } else {
897         assert(cmd.len != 0);
898         trace_scsi_req_parsed(d->id, lun, tag, buf[0],
899                               cmd.mode, cmd.xfer);
900         if (cmd.lba != -1) {
901             trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
902                                       cmd.lba);
903         }
904 
905         if (cmd.xfer > INT32_MAX) {
906             req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
907         } else if (ops) {
908             req = scsi_req_alloc(ops, d, tag, lun, hba_private);
909         } else {
910             req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
911         }
912     }
913 
914     req->ctx = qemu_get_current_aio_context();
915     req->cmd = cmd;
916     req->residual = req->cmd.xfer;
917 
918     switch (buf[0]) {
919     case INQUIRY:
920         trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
921         break;
922     case TEST_UNIT_READY:
923         trace_scsi_test_unit_ready(d->id, lun, tag);
924         break;
925     case REPORT_LUNS:
926         trace_scsi_report_luns(d->id, lun, tag);
927         break;
928     case REQUEST_SENSE:
929         trace_scsi_request_sense(d->id, lun, tag);
930         break;
931     default:
932         break;
933     }
934 
935     return req;
936 }
937 
938 uint8_t *scsi_req_get_buf(SCSIRequest *req)
939 {
940     return req->ops->get_buf(req);
941 }
942 
943 static void scsi_clear_reported_luns_changed(SCSIRequest *req)
944 {
945     SCSISense *ua;
946 
947     if (req->dev->unit_attention.key == UNIT_ATTENTION) {
948         ua = &req->dev->unit_attention;
949     } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
950         ua = &req->bus->unit_attention;
951     } else {
952         return;
953     }
954 
955     if (ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
956         ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq) {
957         *ua = SENSE_CODE(NO_SENSE);
958     }
959 }
960 
961 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
962 {
963     int ret;
964 
965     assert(len >= 14);
966     if (!req->sense_len) {
967         return 0;
968     }
969 
970     ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
971 
972     /*
973      * FIXME: clearing unit attention conditions upon autosense should be done
974      * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
975      * (SAM-5, 5.14).
976      *
977      * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
978      * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
979      * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
980      */
981     if (req->dev->sense_is_ua) {
982         scsi_device_unit_attention_reported(req->dev);
983         req->dev->sense_len = 0;
984         req->dev->sense_is_ua = false;
985     }
986     return ret;
987 }
988 
989 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
990 {
991     return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
992 }
993 
994 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
995 {
996     trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
997                                sense.key, sense.asc, sense.ascq);
998     req->sense_len = scsi_build_sense(req->sense, sense);
999 }
1000 
1001 static void scsi_req_enqueue_internal(SCSIRequest *req)
1002 {
1003     assert(!req->enqueued);
1004     scsi_req_ref(req);
1005     if (req->bus->info->get_sg_list) {
1006         req->sg = req->bus->info->get_sg_list(req);
1007     } else {
1008         req->sg = NULL;
1009     }
1010     req->enqueued = true;
1011 
1012     WITH_QEMU_LOCK_GUARD(&req->dev->requests_lock) {
1013         QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
1014     }
1015 }
1016 
1017 int32_t scsi_req_enqueue(SCSIRequest *req)
1018 {
1019     int32_t rc;
1020 
1021     assert(!req->retry);
1022     scsi_req_enqueue_internal(req);
1023     scsi_req_ref(req);
1024     rc = req->ops->send_command(req, req->cmd.buf);
1025     scsi_req_unref(req);
1026     return rc;
1027 }
1028 
1029 static void scsi_req_dequeue(SCSIRequest *req)
1030 {
1031     trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
1032     req->retry = false;
1033     if (req->enqueued) {
1034         WITH_QEMU_LOCK_GUARD(&req->dev->requests_lock) {
1035             QTAILQ_REMOVE(&req->dev->requests, req, next);
1036         }
1037         req->enqueued = false;
1038         scsi_req_unref(req);
1039     }
1040 }
1041 
1042 static int scsi_get_performance_length(int num_desc, int type, int data_type)
1043 {
1044     /* MMC-6, paragraph 6.7.  */
1045     switch (type) {
1046     case 0:
1047         if ((data_type & 3) == 0) {
1048             /* Each descriptor is as in Table 295 - Nominal performance.  */
1049             return 16 * num_desc + 8;
1050         } else {
1051             /* Each descriptor is as in Table 296 - Exceptions.  */
1052             return 6 * num_desc + 8;
1053         }
1054     case 1:
1055     case 4:
1056     case 5:
1057         return 8 * num_desc + 8;
1058     case 2:
1059         return 2048 * num_desc + 8;
1060     case 3:
1061         return 16 * num_desc + 8;
1062     default:
1063         return 8;
1064     }
1065 }
1066 
1067 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
1068 {
1069     int byte_block = (buf[2] >> 2) & 0x1;
1070     int type = (buf[2] >> 4) & 0x1;
1071     int xfer_unit;
1072 
1073     if (byte_block) {
1074         if (type) {
1075             xfer_unit = dev->blocksize;
1076         } else {
1077             xfer_unit = 512;
1078         }
1079     } else {
1080         xfer_unit = 1;
1081     }
1082 
1083     return xfer_unit;
1084 }
1085 
1086 static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
1087 {
1088     int length = buf[2] & 0x3;
1089     int xfer;
1090     int unit = ata_passthrough_xfer_unit(dev, buf);
1091 
1092     switch (length) {
1093     case 0:
1094     case 3: /* USB-specific.  */
1095     default:
1096         xfer = 0;
1097         break;
1098     case 1:
1099         xfer = buf[3];
1100         break;
1101     case 2:
1102         xfer = buf[4];
1103         break;
1104     }
1105 
1106     return xfer * unit;
1107 }
1108 
1109 static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
1110 {
1111     int extend = buf[1] & 0x1;
1112     int length = buf[2] & 0x3;
1113     int xfer;
1114     int unit = ata_passthrough_xfer_unit(dev, buf);
1115 
1116     switch (length) {
1117     case 0:
1118     case 3: /* USB-specific.  */
1119     default:
1120         xfer = 0;
1121         break;
1122     case 1:
1123         xfer = buf[4];
1124         xfer |= (extend ? buf[3] << 8 : 0);
1125         break;
1126     case 2:
1127         xfer = buf[6];
1128         xfer |= (extend ? buf[5] << 8 : 0);
1129         break;
1130     }
1131 
1132     return xfer * unit;
1133 }
1134 
1135 static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1136 {
1137     cmd->xfer = scsi_cdb_xfer(buf);
1138     switch (buf[0]) {
1139     case TEST_UNIT_READY:
1140     case REWIND:
1141     case START_STOP:
1142     case SET_CAPACITY:
1143     case WRITE_FILEMARKS:
1144     case WRITE_FILEMARKS_16:
1145     case SPACE:
1146     case RESERVE:
1147     case RELEASE:
1148     case ERASE:
1149     case ALLOW_MEDIUM_REMOVAL:
1150     case SEEK_10:
1151     case SYNCHRONIZE_CACHE:
1152     case SYNCHRONIZE_CACHE_16:
1153     case LOCATE_16:
1154     case LOCK_UNLOCK_CACHE:
1155     case SET_CD_SPEED:
1156     case SET_LIMITS:
1157     case WRITE_LONG_10:
1158     case UPDATE_BLOCK:
1159     case RESERVE_TRACK:
1160     case SET_READ_AHEAD:
1161     case PRE_FETCH:
1162     case PRE_FETCH_16:
1163     case ALLOW_OVERWRITE:
1164         cmd->xfer = 0;
1165         break;
1166     case VERIFY_10:
1167     case VERIFY_12:
1168     case VERIFY_16:
1169         if ((buf[1] & 2) == 0) {
1170             cmd->xfer = 0;
1171         } else if ((buf[1] & 4) != 0) {
1172             cmd->xfer = 1;
1173         }
1174         cmd->xfer *= dev->blocksize;
1175         break;
1176     case MODE_SENSE:
1177         break;
1178     case WRITE_SAME_10:
1179     case WRITE_SAME_16:
1180         cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
1181         break;
1182     case READ_CAPACITY_10:
1183         cmd->xfer = 8;
1184         break;
1185     case READ_BLOCK_LIMITS:
1186         cmd->xfer = 6;
1187         break;
1188     case SEND_VOLUME_TAG:
1189         /* GPCMD_SET_STREAMING from multimedia commands.  */
1190         if (dev->type == TYPE_ROM) {
1191             cmd->xfer = buf[10] | (buf[9] << 8);
1192         } else {
1193             cmd->xfer = buf[9] | (buf[8] << 8);
1194         }
1195         break;
1196     case WRITE_6:
1197         /* length 0 means 256 blocks */
1198         if (cmd->xfer == 0) {
1199             cmd->xfer = 256;
1200         }
1201         /* fall through */
1202     case WRITE_10:
1203     case WRITE_VERIFY_10:
1204     case WRITE_12:
1205     case WRITE_VERIFY_12:
1206     case WRITE_16:
1207     case WRITE_VERIFY_16:
1208         cmd->xfer *= dev->blocksize;
1209         break;
1210     case READ_6:
1211     case READ_REVERSE:
1212         /* length 0 means 256 blocks */
1213         if (cmd->xfer == 0) {
1214             cmd->xfer = 256;
1215         }
1216         /* fall through */
1217     case READ_10:
1218     case READ_12:
1219     case READ_16:
1220         cmd->xfer *= dev->blocksize;
1221         break;
1222     case FORMAT_UNIT:
1223         /* MMC mandates the parameter list to be 12-bytes long.  Parameters
1224          * for block devices are restricted to the header right now.  */
1225         if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1226             cmd->xfer = 12;
1227         } else {
1228             cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1229         }
1230         break;
1231     case INQUIRY:
1232     case RECEIVE_DIAGNOSTIC:
1233     case SEND_DIAGNOSTIC:
1234         cmd->xfer = buf[4] | (buf[3] << 8);
1235         break;
1236     case READ_CD:
1237     case READ_BUFFER:
1238     case WRITE_BUFFER:
1239     case SEND_CUE_SHEET:
1240         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1241         break;
1242     case PERSISTENT_RESERVE_OUT:
1243         cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1244         break;
1245     case ERASE_12:
1246         if (dev->type == TYPE_ROM) {
1247             /* MMC command GET PERFORMANCE.  */
1248             cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1249                                                     buf[10], buf[1] & 0x1f);
1250         }
1251         break;
1252     case MECHANISM_STATUS:
1253     case READ_DVD_STRUCTURE:
1254     case SEND_DVD_STRUCTURE:
1255     case MAINTENANCE_OUT:
1256     case MAINTENANCE_IN:
1257         if (dev->type == TYPE_ROM) {
1258             /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1259             cmd->xfer = buf[9] | (buf[8] << 8);
1260         }
1261         break;
1262     case ATA_PASSTHROUGH_12:
1263         if (dev->type == TYPE_ROM) {
1264             /* BLANK command of MMC */
1265             cmd->xfer = 0;
1266         } else {
1267             cmd->xfer = ata_passthrough_12_xfer(dev, buf);
1268         }
1269         break;
1270     case ATA_PASSTHROUGH_16:
1271         cmd->xfer = ata_passthrough_16_xfer(dev, buf);
1272         break;
1273     }
1274     return 0;
1275 }
1276 
1277 static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1278 {
1279     switch (buf[0]) {
1280     /* stream commands */
1281     case ERASE_12:
1282     case ERASE_16:
1283         cmd->xfer = 0;
1284         break;
1285     case READ_6:
1286     case READ_REVERSE:
1287     case RECOVER_BUFFERED_DATA:
1288     case WRITE_6:
1289         cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1290         if (buf[1] & 0x01) { /* fixed */
1291             cmd->xfer *= dev->blocksize;
1292         }
1293         break;
1294     case READ_16:
1295     case READ_REVERSE_16:
1296     case VERIFY_16:
1297     case WRITE_16:
1298         cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1299         if (buf[1] & 0x01) { /* fixed */
1300             cmd->xfer *= dev->blocksize;
1301         }
1302         break;
1303     case REWIND:
1304     case LOAD_UNLOAD:
1305         cmd->xfer = 0;
1306         break;
1307     case SPACE_16:
1308         cmd->xfer = buf[13] | (buf[12] << 8);
1309         break;
1310     case READ_POSITION:
1311         switch (buf[1] & 0x1f) /* operation code */ {
1312         case SHORT_FORM_BLOCK_ID:
1313         case SHORT_FORM_VENDOR_SPECIFIC:
1314             cmd->xfer = 20;
1315             break;
1316         case LONG_FORM:
1317             cmd->xfer = 32;
1318             break;
1319         case EXTENDED_FORM:
1320             cmd->xfer = buf[8] | (buf[7] << 8);
1321             break;
1322         default:
1323             return -1;
1324         }
1325 
1326         break;
1327     case FORMAT_UNIT:
1328         cmd->xfer = buf[4] | (buf[3] << 8);
1329         break;
1330     /* generic commands */
1331     default:
1332         return scsi_req_xfer(cmd, dev, buf);
1333     }
1334     return 0;
1335 }
1336 
1337 static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1338 {
1339     switch (buf[0]) {
1340     /* medium changer commands */
1341     case EXCHANGE_MEDIUM:
1342     case INITIALIZE_ELEMENT_STATUS:
1343     case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1344     case MOVE_MEDIUM:
1345     case POSITION_TO_ELEMENT:
1346         cmd->xfer = 0;
1347         break;
1348     case READ_ELEMENT_STATUS:
1349         cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1350         break;
1351 
1352     /* generic commands */
1353     default:
1354         return scsi_req_xfer(cmd, dev, buf);
1355     }
1356     return 0;
1357 }
1358 
1359 static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1360 {
1361     switch (buf[0]) {
1362     /* Scanner commands */
1363     case OBJECT_POSITION:
1364         cmd->xfer = 0;
1365         break;
1366     case SCAN:
1367         cmd->xfer = buf[4];
1368         break;
1369     case READ_10:
1370     case SEND:
1371     case GET_WINDOW:
1372     case SET_WINDOW:
1373         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1374         break;
1375     default:
1376         /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1377         return scsi_req_xfer(cmd, dev, buf);
1378     }
1379 
1380     return 0;
1381 }
1382 
1383 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1384 {
1385     if (!cmd->xfer) {
1386         cmd->mode = SCSI_XFER_NONE;
1387         return;
1388     }
1389     switch (cmd->buf[0]) {
1390     case WRITE_6:
1391     case WRITE_10:
1392     case WRITE_VERIFY_10:
1393     case WRITE_12:
1394     case WRITE_VERIFY_12:
1395     case WRITE_16:
1396     case WRITE_VERIFY_16:
1397     case VERIFY_10:
1398     case VERIFY_12:
1399     case VERIFY_16:
1400     case COPY:
1401     case COPY_VERIFY:
1402     case COMPARE:
1403     case CHANGE_DEFINITION:
1404     case LOG_SELECT:
1405     case MODE_SELECT:
1406     case MODE_SELECT_10:
1407     case SEND_DIAGNOSTIC:
1408     case WRITE_BUFFER:
1409     case FORMAT_UNIT:
1410     case REASSIGN_BLOCKS:
1411     case SEARCH_EQUAL:
1412     case SEARCH_HIGH:
1413     case SEARCH_LOW:
1414     case UPDATE_BLOCK:
1415     case WRITE_LONG_10:
1416     case WRITE_SAME_10:
1417     case WRITE_SAME_16:
1418     case UNMAP:
1419     case SEARCH_HIGH_12:
1420     case SEARCH_EQUAL_12:
1421     case SEARCH_LOW_12:
1422     case MEDIUM_SCAN:
1423     case SEND_VOLUME_TAG:
1424     case SEND_CUE_SHEET:
1425     case SEND_DVD_STRUCTURE:
1426     case PERSISTENT_RESERVE_OUT:
1427     case MAINTENANCE_OUT:
1428     case SET_WINDOW:
1429     case SCAN:
1430         /* SCAN conflicts with START_STOP.  START_STOP has cmd->xfer set to 0 for
1431          * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1432          */
1433         cmd->mode = SCSI_XFER_TO_DEV;
1434         break;
1435     case ATA_PASSTHROUGH_12:
1436     case ATA_PASSTHROUGH_16:
1437         /* T_DIR */
1438         cmd->mode = (cmd->buf[2] & 0x8) ?
1439                    SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1440         break;
1441     default:
1442         cmd->mode = SCSI_XFER_FROM_DEV;
1443         break;
1444     }
1445 }
1446 
1447 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
1448                        size_t buf_len)
1449 {
1450     int rc;
1451     int len;
1452 
1453     cmd->lba = -1;
1454     len = scsi_cdb_length(buf);
1455     if (len < 0 || len > buf_len) {
1456         return -1;
1457     }
1458 
1459     cmd->len = len;
1460     switch (dev->type) {
1461     case TYPE_TAPE:
1462         rc = scsi_req_stream_xfer(cmd, dev, buf);
1463         break;
1464     case TYPE_MEDIUM_CHANGER:
1465         rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
1466         break;
1467     case TYPE_SCANNER:
1468         rc = scsi_req_scanner_length(cmd, dev, buf);
1469         break;
1470     default:
1471         rc = scsi_req_xfer(cmd, dev, buf);
1472         break;
1473     }
1474 
1475     if (rc != 0)
1476         return rc;
1477 
1478     memcpy(cmd->buf, buf, cmd->len);
1479     scsi_cmd_xfer_mode(cmd);
1480     cmd->lba = scsi_cmd_lba(cmd);
1481     return 0;
1482 }
1483 
1484 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1485 {
1486     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1487 
1488     scsi_device_set_ua(dev, sense);
1489     if (bus->info->change) {
1490         bus->info->change(bus, dev, sense);
1491     }
1492 }
1493 
1494 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1495 {
1496     assert(qatomic_read(&req->refcount) > 0);
1497     qatomic_inc(&req->refcount);
1498     return req;
1499 }
1500 
1501 void scsi_req_unref(SCSIRequest *req)
1502 {
1503     assert(qatomic_read(&req->refcount) > 0);
1504     if (qatomic_fetch_dec(&req->refcount) == 1) {
1505         BusState *qbus = req->dev->qdev.parent_bus;
1506         SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1507 
1508         if (bus->info->free_request && req->hba_private) {
1509             bus->info->free_request(bus, req->hba_private);
1510         }
1511         if (req->ops->free_req) {
1512             req->ops->free_req(req);
1513         }
1514         g_free(req);
1515     }
1516 }
1517 
1518 /* Tell the device that we finished processing this chunk of I/O.  It
1519    will start the next chunk or complete the command.  */
1520 void scsi_req_continue(SCSIRequest *req)
1521 {
1522     if (req->io_canceled) {
1523         trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1524         return;
1525     }
1526     trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1527     if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1528         req->ops->write_data(req);
1529     } else {
1530         req->ops->read_data(req);
1531     }
1532 }
1533 
1534 /* Called by the devices when data is ready for the HBA.  The HBA should
1535    start a DMA operation to read or fill the device's data buffer.
1536    Once it completes, calling scsi_req_continue will restart I/O.  */
1537 void scsi_req_data(SCSIRequest *req, int len)
1538 {
1539     uint8_t *buf;
1540     if (req->io_canceled) {
1541         trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1542         return;
1543     }
1544     trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1545     assert(req->cmd.mode != SCSI_XFER_NONE);
1546     if (!req->sg) {
1547         req->residual -= len;
1548         req->bus->info->transfer_data(req, len);
1549         return;
1550     }
1551 
1552     /* If the device calls scsi_req_data and the HBA specified a
1553      * scatter/gather list, the transfer has to happen in a single
1554      * step.  */
1555     assert(!req->dma_started);
1556     req->dma_started = true;
1557 
1558     buf = scsi_req_get_buf(req);
1559     if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1560         dma_buf_read(buf, len, &req->residual, req->sg,
1561                      MEMTXATTRS_UNSPECIFIED);
1562     } else {
1563         dma_buf_write(buf, len, &req->residual, req->sg,
1564                       MEMTXATTRS_UNSPECIFIED);
1565     }
1566     scsi_req_continue(req);
1567 }
1568 
1569 void scsi_req_print(SCSIRequest *req)
1570 {
1571     FILE *fp = stderr;
1572     int i;
1573 
1574     fprintf(fp, "[%s id=%d] %s",
1575             req->dev->qdev.parent_bus->name,
1576             req->dev->id,
1577             scsi_command_name(req->cmd.buf[0]));
1578     for (i = 1; i < req->cmd.len; i++) {
1579         fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1580     }
1581     switch (req->cmd.mode) {
1582     case SCSI_XFER_NONE:
1583         fprintf(fp, " - none\n");
1584         break;
1585     case SCSI_XFER_FROM_DEV:
1586         fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1587         break;
1588     case SCSI_XFER_TO_DEV:
1589         fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1590         break;
1591     default:
1592         fprintf(fp, " - Oops\n");
1593         break;
1594     }
1595 }
1596 
1597 void scsi_req_complete_failed(SCSIRequest *req, int host_status)
1598 {
1599     SCSISense sense;
1600     int status;
1601 
1602     assert(req->status == -1 && req->host_status == -1);
1603     assert(req->ops != &reqops_unit_attention);
1604 
1605     if (!req->bus->info->fail) {
1606         status = scsi_sense_from_host_status(req->host_status, &sense);
1607         if (status == CHECK_CONDITION) {
1608             scsi_req_build_sense(req, sense);
1609         }
1610         scsi_req_complete(req, status);
1611         return;
1612     }
1613 
1614     req->host_status = host_status;
1615     scsi_req_ref(req);
1616     scsi_req_dequeue(req);
1617     req->bus->info->fail(req);
1618 
1619     /* Cancelled requests might end up being completed instead of cancelled */
1620     notifier_list_notify(&req->cancel_notifiers, req);
1621     scsi_req_unref(req);
1622 }
1623 
1624 void scsi_req_complete(SCSIRequest *req, int status)
1625 {
1626     assert(req->status == -1 && req->host_status == -1);
1627     req->status = status;
1628     req->host_status = SCSI_HOST_OK;
1629 
1630     assert(req->sense_len <= sizeof(req->sense));
1631     if (status == GOOD) {
1632         req->sense_len = 0;
1633     }
1634 
1635     if (req->sense_len) {
1636         memcpy(req->dev->sense, req->sense, req->sense_len);
1637         req->dev->sense_len = req->sense_len;
1638         req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1639     } else {
1640         req->dev->sense_len = 0;
1641         req->dev->sense_is_ua = false;
1642     }
1643 
1644     scsi_req_ref(req);
1645     scsi_req_dequeue(req);
1646     req->bus->info->complete(req, req->residual);
1647 
1648     /* Cancelled requests might end up being completed instead of cancelled */
1649     notifier_list_notify(&req->cancel_notifiers, req);
1650     scsi_req_unref(req);
1651 }
1652 
1653 /* Called by the devices when the request is canceled. */
1654 void scsi_req_cancel_complete(SCSIRequest *req)
1655 {
1656     assert(req->io_canceled);
1657     if (req->bus->info->cancel) {
1658         req->bus->info->cancel(req);
1659     }
1660     notifier_list_notify(&req->cancel_notifiers, req);
1661     scsi_req_unref(req);
1662 }
1663 
1664 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1665  * notifier list, the bus will be notified the requests cancellation is
1666  * completed.
1667  * */
1668 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1669 {
1670     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1671     if (notifier) {
1672         notifier_list_add(&req->cancel_notifiers, notifier);
1673     }
1674     if (req->io_canceled) {
1675         /* A blk_aio_cancel_async is pending; when it finishes,
1676          * scsi_req_cancel_complete will be called and will
1677          * call the notifier we just added.  Just wait for that.
1678          */
1679         assert(req->aiocb);
1680         return;
1681     }
1682     /* Dropped in scsi_req_cancel_complete.  */
1683     scsi_req_ref(req);
1684     scsi_req_dequeue(req);
1685     req->io_canceled = true;
1686     if (req->aiocb) {
1687         blk_aio_cancel_async(req->aiocb);
1688     } else {
1689         scsi_req_cancel_complete(req);
1690     }
1691 }
1692 
1693 void scsi_req_cancel(SCSIRequest *req)
1694 {
1695     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1696     if (!req->enqueued) {
1697         return;
1698     }
1699     assert(!req->io_canceled);
1700     /* Dropped in scsi_req_cancel_complete.  */
1701     scsi_req_ref(req);
1702     scsi_req_dequeue(req);
1703     req->io_canceled = true;
1704     if (req->aiocb) {
1705         blk_aio_cancel(req->aiocb);
1706     } else {
1707         scsi_req_cancel_complete(req);
1708     }
1709 }
1710 
1711 static int scsi_ua_precedence(SCSISense sense)
1712 {
1713     if (sense.key != UNIT_ATTENTION) {
1714         return INT_MAX;
1715     }
1716     if (sense.asc == 0x29 && sense.ascq == 0x04) {
1717         /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1718         return 1;
1719     } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1720         /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1721         return 2;
1722     } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1723         /* These two go with "all others". */
1724         ;
1725     } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1726         /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1727          * POWER ON OCCURRED = 1
1728          * SCSI BUS RESET OCCURRED = 2
1729          * BUS DEVICE RESET FUNCTION OCCURRED = 3
1730          * I_T NEXUS LOSS OCCURRED = 7
1731          */
1732         return sense.ascq;
1733     } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1734         /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION  */
1735         return 8;
1736     }
1737     return (sense.asc << 8) | sense.ascq;
1738 }
1739 
1740 void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense)
1741 {
1742     int prec1, prec2;
1743     if (sense.key != UNIT_ATTENTION) {
1744         return;
1745     }
1746 
1747     /*
1748      * Override a pre-existing unit attention condition, except for a more
1749      * important reset condition.
1750      */
1751     prec1 = scsi_ua_precedence(bus->unit_attention);
1752     prec2 = scsi_ua_precedence(sense);
1753     if (prec2 < prec1) {
1754         bus->unit_attention = sense;
1755     }
1756 }
1757 
1758 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1759 {
1760     int prec1, prec2;
1761     if (sense.key != UNIT_ATTENTION) {
1762         return;
1763     }
1764     trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1765                              sense.asc, sense.ascq);
1766 
1767     /*
1768      * Override a pre-existing unit attention condition, except for a more
1769      * important reset condition.
1770     */
1771     prec1 = scsi_ua_precedence(sdev->unit_attention);
1772     prec2 = scsi_ua_precedence(sense);
1773     if (prec2 < prec1) {
1774         sdev->unit_attention = sense;
1775     }
1776 }
1777 
1778 static void scsi_device_purge_one_req(SCSIRequest *req, void *opaque)
1779 {
1780     scsi_req_cancel_async(req, NULL);
1781 }
1782 
1783 /**
1784  * Cancel all requests, and block until they are deleted.
1785  */
1786 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1787 {
1788     scsi_device_for_each_req_async(sdev, scsi_device_purge_one_req, NULL);
1789 
1790     /*
1791      * Await all the scsi_device_purge_one_req() calls scheduled by
1792      * scsi_device_for_each_req_async(), and all I/O requests that were
1793      * cancelled this way, but may still take a bit of time to settle.
1794      */
1795     blk_drain(sdev->conf.blk);
1796 
1797     scsi_device_set_ua(sdev, sense);
1798 }
1799 
1800 void scsi_device_drained_begin(SCSIDevice *sdev)
1801 {
1802     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1803     if (!bus) {
1804         return;
1805     }
1806 
1807     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1808     assert(bus->drain_count < INT_MAX);
1809 
1810     /*
1811      * Multiple BlockBackends can be on a SCSIBus and each may begin/end
1812      * draining at any time. Keep a counter so HBAs only see begin/end once.
1813      */
1814     if (bus->drain_count++ == 0) {
1815         trace_scsi_bus_drained_begin(bus, sdev);
1816         if (bus->info->drained_begin) {
1817             bus->info->drained_begin(bus);
1818         }
1819     }
1820 }
1821 
1822 void scsi_device_drained_end(SCSIDevice *sdev)
1823 {
1824     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1825     if (!bus) {
1826         return;
1827     }
1828 
1829     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1830     assert(bus->drain_count > 0);
1831 
1832     if (bus->drain_count-- == 1) {
1833         trace_scsi_bus_drained_end(bus, sdev);
1834         if (bus->info->drained_end) {
1835             bus->info->drained_end(bus);
1836         }
1837     }
1838 }
1839 
1840 static char *scsibus_get_dev_path(DeviceState *dev)
1841 {
1842     SCSIDevice *d = SCSI_DEVICE(dev);
1843     DeviceState *hba = dev->parent_bus->parent;
1844     char *id;
1845     char *path;
1846 
1847     id = qdev_get_dev_path(hba);
1848     if (id) {
1849         path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1850     } else {
1851         path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1852     }
1853     g_free(id);
1854     return path;
1855 }
1856 
1857 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1858 {
1859     SCSIDevice *d = SCSI_DEVICE(dev);
1860     return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1861                            qdev_fw_name(dev), d->id, d->lun);
1862 }
1863 
1864 /* SCSI request list.  For simplicity, pv points to the whole device */
1865 
1866 static void put_scsi_req(SCSIRequest *req, void *opaque)
1867 {
1868     QEMUFile *f = opaque;
1869 
1870     assert(!req->io_canceled);
1871     assert(req->status == -1 && req->host_status == -1);
1872     assert(req->enqueued);
1873 
1874     qemu_put_sbyte(f, req->retry ? 1 : 2);
1875     qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1876     qemu_put_be32s(f, &req->tag);
1877     qemu_put_be32s(f, &req->lun);
1878     if (req->bus->info->save_request) {
1879         req->bus->info->save_request(f, req);
1880     }
1881     if (req->ops->save_request) {
1882         req->ops->save_request(f, req);
1883     }
1884 }
1885 
1886 static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
1887                              const VMStateField *field, JSONWriter *vmdesc)
1888 {
1889     SCSIDevice *s = pv;
1890 
1891     scsi_device_for_each_req_sync(s, put_scsi_req, f);
1892     qemu_put_sbyte(f, 0);
1893     return 0;
1894 }
1895 
1896 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
1897                              const VMStateField *field)
1898 {
1899     SCSIDevice *s = pv;
1900     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1901     int8_t sbyte;
1902 
1903     while ((sbyte = qemu_get_sbyte(f)) > 0) {
1904         uint8_t buf[SCSI_CMD_BUF_SIZE];
1905         uint32_t tag;
1906         uint32_t lun;
1907         SCSIRequest *req;
1908 
1909         qemu_get_buffer(f, buf, sizeof(buf));
1910         qemu_get_be32s(f, &tag);
1911         qemu_get_be32s(f, &lun);
1912         /*
1913          * A too-short CDB would have been rejected by scsi_req_new, so just use
1914          * SCSI_CMD_BUF_SIZE as the CDB length.
1915          */
1916         req = scsi_req_new(s, tag, lun, buf, sizeof(buf), NULL);
1917         req->retry = (sbyte == 1);
1918         if (bus->info->load_request) {
1919             req->hba_private = bus->info->load_request(f, req);
1920         }
1921         if (req->ops->load_request) {
1922             req->ops->load_request(f, req);
1923         }
1924 
1925         /* Just restart it later.  */
1926         scsi_req_enqueue_internal(req);
1927 
1928         /* At this point, the request will be kept alive by the reference
1929          * added by scsi_req_enqueue_internal, so we can release our reference.
1930          * The HBA of course will add its own reference in the load_request
1931          * callback if it needs to hold on the SCSIRequest.
1932          */
1933         scsi_req_unref(req);
1934     }
1935 
1936     return 0;
1937 }
1938 
1939 static const VMStateInfo vmstate_info_scsi_requests = {
1940     .name = "scsi-requests",
1941     .get  = get_scsi_requests,
1942     .put  = put_scsi_requests,
1943 };
1944 
1945 static bool scsi_sense_state_needed(void *opaque)
1946 {
1947     SCSIDevice *s = opaque;
1948 
1949     return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1950 }
1951 
1952 static const VMStateDescription vmstate_scsi_sense_state = {
1953     .name = "SCSIDevice/sense",
1954     .version_id = 1,
1955     .minimum_version_id = 1,
1956     .needed = scsi_sense_state_needed,
1957     .fields = (const VMStateField[]) {
1958         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1959                                 SCSI_SENSE_BUF_SIZE_OLD,
1960                                 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1961         VMSTATE_END_OF_LIST()
1962     }
1963 };
1964 
1965 const VMStateDescription vmstate_scsi_device = {
1966     .name = "SCSIDevice",
1967     .version_id = 1,
1968     .minimum_version_id = 1,
1969     .fields = (const VMStateField[]) {
1970         VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1971         VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1972         VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1973         VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1974         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
1975         VMSTATE_UINT32(sense_len, SCSIDevice),
1976         {
1977             .name         = "requests",
1978             .version_id   = 0,
1979             .field_exists = NULL,
1980             .size         = 0,   /* ouch */
1981             .info         = &vmstate_info_scsi_requests,
1982             .flags        = VMS_SINGLE,
1983             .offset       = 0,
1984         },
1985         VMSTATE_END_OF_LIST()
1986     },
1987     .subsections = (const VMStateDescription * const []) {
1988         &vmstate_scsi_sense_state,
1989         NULL
1990     }
1991 };
1992 
1993 static const Property scsi_props[] = {
1994     DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
1995     DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
1996     DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
1997 };
1998 
1999 static void scsi_device_class_init(ObjectClass *klass, const void *data)
2000 {
2001     DeviceClass *k = DEVICE_CLASS(klass);
2002     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
2003     k->bus_type  = TYPE_SCSI_BUS;
2004     k->realize   = scsi_qdev_realize;
2005     k->unrealize = scsi_qdev_unrealize;
2006     device_class_set_props(k, scsi_props);
2007 }
2008 
2009 static void scsi_dev_instance_init(Object *obj)
2010 {
2011     SCSIDevice *s = SCSI_DEVICE(obj);
2012 
2013     device_add_bootindex_property(obj, &s->conf.bootindex,
2014                                   "bootindex", NULL,
2015                                   &s->qdev);
2016 }
2017 
2018 static const TypeInfo scsi_device_type_info = {
2019     .name = TYPE_SCSI_DEVICE,
2020     .parent = TYPE_DEVICE,
2021     .instance_size = sizeof(SCSIDevice),
2022     .abstract = true,
2023     .class_size = sizeof(SCSIDeviceClass),
2024     .class_init = scsi_device_class_init,
2025     .instance_init = scsi_dev_instance_init,
2026 };
2027 
2028 static void scsi_bus_class_init(ObjectClass *klass, const void *data)
2029 {
2030     BusClass *k = BUS_CLASS(klass);
2031     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
2032 
2033     k->get_dev_path = scsibus_get_dev_path;
2034     k->get_fw_dev_path = scsibus_get_fw_dev_path;
2035     k->check_address = scsi_bus_check_address;
2036     hc->unplug = qdev_simple_device_unplug_cb;
2037 }
2038 
2039 static const TypeInfo scsi_bus_info = {
2040     .name = TYPE_SCSI_BUS,
2041     .parent = TYPE_BUS,
2042     .instance_size = sizeof(SCSIBus),
2043     .class_init = scsi_bus_class_init,
2044     .interfaces = (const InterfaceInfo[]) {
2045         { TYPE_HOTPLUG_HANDLER },
2046         { }
2047     }
2048 };
2049 
2050 static void scsi_register_types(void)
2051 {
2052     type_register_static(&scsi_bus_info);
2053     type_register_static(&scsi_device_type_info);
2054 }
2055 
2056 type_init(scsi_register_types)
2057