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