xref: /openbmc/qemu/hw/scsi/scsi-bus.c (revision d6032e06)
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     case TEST_UNIT_READY:
473         break;
474     default:
475         scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
476         scsi_req_complete(req, CHECK_CONDITION);
477         return 0;
478     illegal_request:
479         scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
480         scsi_req_complete(req, CHECK_CONDITION);
481         return 0;
482     }
483 
484     if (!r->len) {
485         scsi_req_complete(req, GOOD);
486     }
487     return r->len;
488 }
489 
490 static void scsi_target_read_data(SCSIRequest *req)
491 {
492     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
493     uint32_t n;
494 
495     n = r->len;
496     if (n > 0) {
497         r->len = 0;
498         scsi_req_data(&r->req, n);
499     } else {
500         scsi_req_complete(&r->req, GOOD);
501     }
502 }
503 
504 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
505 {
506     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
507 
508     return r->buf;
509 }
510 
511 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
512 {
513     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
514 
515     r->buf = g_malloc(len);
516     r->buf_len = len;
517 
518     return r->buf;
519 }
520 
521 static void scsi_target_free_buf(SCSIRequest *req)
522 {
523     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
524 
525     g_free(r->buf);
526 }
527 
528 static const struct SCSIReqOps reqops_target_command = {
529     .size         = sizeof(SCSITargetReq),
530     .send_command = scsi_target_send_command,
531     .read_data    = scsi_target_read_data,
532     .get_buf      = scsi_target_get_buf,
533     .free_req     = scsi_target_free_buf,
534 };
535 
536 
537 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
538                             uint32_t tag, uint32_t lun, void *hba_private)
539 {
540     SCSIRequest *req;
541     SCSIBus *bus = scsi_bus_from_device(d);
542     BusState *qbus = BUS(bus);
543 
544     req = g_malloc0(reqops->size);
545     req->refcount = 1;
546     req->bus = bus;
547     req->dev = d;
548     req->tag = tag;
549     req->lun = lun;
550     req->hba_private = hba_private;
551     req->status = -1;
552     req->sense_len = 0;
553     req->ops = reqops;
554     object_ref(OBJECT(d));
555     object_ref(OBJECT(qbus->parent));
556     trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
557     return req;
558 }
559 
560 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
561                           uint8_t *buf, void *hba_private)
562 {
563     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
564     SCSIRequest *req;
565     SCSICommand cmd;
566 
567     if (scsi_req_parse(&cmd, d, buf) != 0) {
568         trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
569         req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
570     } else {
571         trace_scsi_req_parsed(d->id, lun, tag, buf[0],
572                               cmd.mode, cmd.xfer);
573         if (cmd.lba != -1) {
574             trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
575                                       cmd.lba);
576         }
577 
578         if (cmd.xfer > INT32_MAX) {
579             req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
580         } else if ((d->unit_attention.key == UNIT_ATTENTION ||
581                    bus->unit_attention.key == UNIT_ATTENTION) &&
582                   (buf[0] != INQUIRY &&
583                    buf[0] != REPORT_LUNS &&
584                    buf[0] != GET_CONFIGURATION &&
585                    buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
586 
587                    /*
588                     * If we already have a pending unit attention condition,
589                     * report this one before triggering another one.
590                     */
591                    !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
592             req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
593                                  hba_private);
594         } else if (lun != d->lun ||
595                    buf[0] == REPORT_LUNS ||
596                    (buf[0] == REQUEST_SENSE && d->sense_len)) {
597             req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
598                                  hba_private);
599         } else {
600             req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
601         }
602     }
603 
604     req->cmd = cmd;
605     req->resid = req->cmd.xfer;
606 
607     switch (buf[0]) {
608     case INQUIRY:
609         trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
610         break;
611     case TEST_UNIT_READY:
612         trace_scsi_test_unit_ready(d->id, lun, tag);
613         break;
614     case REPORT_LUNS:
615         trace_scsi_report_luns(d->id, lun, tag);
616         break;
617     case REQUEST_SENSE:
618         trace_scsi_request_sense(d->id, lun, tag);
619         break;
620     default:
621         break;
622     }
623 
624     return req;
625 }
626 
627 uint8_t *scsi_req_get_buf(SCSIRequest *req)
628 {
629     return req->ops->get_buf(req);
630 }
631 
632 static void scsi_clear_unit_attention(SCSIRequest *req)
633 {
634     SCSISense *ua;
635     if (req->dev->unit_attention.key != UNIT_ATTENTION &&
636         req->bus->unit_attention.key != UNIT_ATTENTION) {
637         return;
638     }
639 
640     /*
641      * If an INQUIRY command enters the enabled command state,
642      * the device server shall [not] clear any unit attention condition;
643      * See also MMC-6, paragraphs 6.5 and 6.6.2.
644      */
645     if (req->cmd.buf[0] == INQUIRY ||
646         req->cmd.buf[0] == GET_CONFIGURATION ||
647         req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
648         return;
649     }
650 
651     if (req->dev->unit_attention.key == UNIT_ATTENTION) {
652         ua = &req->dev->unit_attention;
653     } else {
654         ua = &req->bus->unit_attention;
655     }
656 
657     /*
658      * If a REPORT LUNS command enters the enabled command state, [...]
659      * the device server shall clear any pending unit attention condition
660      * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
661      */
662     if (req->cmd.buf[0] == REPORT_LUNS &&
663         !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
664           ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
665         return;
666     }
667 
668     *ua = SENSE_CODE(NO_SENSE);
669 }
670 
671 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
672 {
673     int ret;
674 
675     assert(len >= 14);
676     if (!req->sense_len) {
677         return 0;
678     }
679 
680     ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
681 
682     /*
683      * FIXME: clearing unit attention conditions upon autosense should be done
684      * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
685      * (SAM-5, 5.14).
686      *
687      * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
688      * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
689      * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
690      */
691     if (req->dev->sense_is_ua) {
692         scsi_device_unit_attention_reported(req->dev);
693         req->dev->sense_len = 0;
694         req->dev->sense_is_ua = false;
695     }
696     return ret;
697 }
698 
699 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
700 {
701     return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
702 }
703 
704 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
705 {
706     trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
707                                sense.key, sense.asc, sense.ascq);
708     memset(req->sense, 0, 18);
709     req->sense[0] = 0x70;
710     req->sense[2] = sense.key;
711     req->sense[7] = 10;
712     req->sense[12] = sense.asc;
713     req->sense[13] = sense.ascq;
714     req->sense_len = 18;
715 }
716 
717 static void scsi_req_enqueue_internal(SCSIRequest *req)
718 {
719     assert(!req->enqueued);
720     scsi_req_ref(req);
721     if (req->bus->info->get_sg_list) {
722         req->sg = req->bus->info->get_sg_list(req);
723     } else {
724         req->sg = NULL;
725     }
726     req->enqueued = true;
727     QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
728 }
729 
730 int32_t scsi_req_enqueue(SCSIRequest *req)
731 {
732     int32_t rc;
733 
734     assert(!req->retry);
735     scsi_req_enqueue_internal(req);
736     scsi_req_ref(req);
737     rc = req->ops->send_command(req, req->cmd.buf);
738     scsi_req_unref(req);
739     return rc;
740 }
741 
742 static void scsi_req_dequeue(SCSIRequest *req)
743 {
744     trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
745     req->retry = false;
746     if (req->enqueued) {
747         QTAILQ_REMOVE(&req->dev->requests, req, next);
748         req->enqueued = false;
749         scsi_req_unref(req);
750     }
751 }
752 
753 static int scsi_get_performance_length(int num_desc, int type, int data_type)
754 {
755     /* MMC-6, paragraph 6.7.  */
756     switch (type) {
757     case 0:
758         if ((data_type & 3) == 0) {
759             /* Each descriptor is as in Table 295 - Nominal performance.  */
760             return 16 * num_desc + 8;
761         } else {
762             /* Each descriptor is as in Table 296 - Exceptions.  */
763             return 6 * num_desc + 8;
764         }
765     case 1:
766     case 4:
767     case 5:
768         return 8 * num_desc + 8;
769     case 2:
770         return 2048 * num_desc + 8;
771     case 3:
772         return 16 * num_desc + 8;
773     default:
774         return 8;
775     }
776 }
777 
778 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
779 {
780     int byte_block = (buf[2] >> 2) & 0x1;
781     int type = (buf[2] >> 4) & 0x1;
782     int xfer_unit;
783 
784     if (byte_block) {
785         if (type) {
786             xfer_unit = dev->blocksize;
787         } else {
788             xfer_unit = 512;
789         }
790     } else {
791         xfer_unit = 1;
792     }
793 
794     return xfer_unit;
795 }
796 
797 static int ata_passthrough_12_xfer_size(SCSIDevice *dev, uint8_t *buf)
798 {
799     int length = buf[2] & 0x3;
800     int xfer;
801     int unit = ata_passthrough_xfer_unit(dev, buf);
802 
803     switch (length) {
804     case 0:
805     case 3: /* USB-specific.  */
806     default:
807         xfer = 0;
808         break;
809     case 1:
810         xfer = buf[3];
811         break;
812     case 2:
813         xfer = buf[4];
814         break;
815     }
816 
817     return xfer * unit;
818 }
819 
820 static int ata_passthrough_16_xfer_size(SCSIDevice *dev, uint8_t *buf)
821 {
822     int extend = buf[1] & 0x1;
823     int length = buf[2] & 0x3;
824     int xfer;
825     int unit = ata_passthrough_xfer_unit(dev, buf);
826 
827     switch (length) {
828     case 0:
829     case 3: /* USB-specific.  */
830     default:
831         xfer = 0;
832         break;
833     case 1:
834         xfer = buf[4];
835         xfer |= (extend ? buf[3] << 8 : 0);
836         break;
837     case 2:
838         xfer = buf[6];
839         xfer |= (extend ? buf[5] << 8 : 0);
840         break;
841     }
842 
843     return xfer * unit;
844 }
845 
846 uint32_t scsi_data_cdb_length(uint8_t *buf)
847 {
848     if ((buf[0] >> 5) == 0 && buf[4] == 0) {
849         return 256;
850     } else {
851         return scsi_cdb_length(buf);
852     }
853 }
854 
855 uint32_t scsi_cdb_length(uint8_t *buf)
856 {
857     switch (buf[0] >> 5) {
858     case 0:
859         return buf[4];
860         break;
861     case 1:
862     case 2:
863         return lduw_be_p(&buf[7]);
864         break;
865     case 4:
866         return ldl_be_p(&buf[10]) & 0xffffffffULL;
867         break;
868     case 5:
869         return ldl_be_p(&buf[6]) & 0xffffffffULL;
870         break;
871     default:
872         return -1;
873     }
874 }
875 
876 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
877 {
878     cmd->xfer = scsi_cdb_length(buf);
879     switch (buf[0]) {
880     case TEST_UNIT_READY:
881     case REWIND:
882     case START_STOP:
883     case SET_CAPACITY:
884     case WRITE_FILEMARKS:
885     case WRITE_FILEMARKS_16:
886     case SPACE:
887     case RESERVE:
888     case RELEASE:
889     case ERASE:
890     case ALLOW_MEDIUM_REMOVAL:
891     case SEEK_10:
892     case SYNCHRONIZE_CACHE:
893     case SYNCHRONIZE_CACHE_16:
894     case LOCATE_16:
895     case LOCK_UNLOCK_CACHE:
896     case SET_CD_SPEED:
897     case SET_LIMITS:
898     case WRITE_LONG_10:
899     case UPDATE_BLOCK:
900     case RESERVE_TRACK:
901     case SET_READ_AHEAD:
902     case PRE_FETCH:
903     case PRE_FETCH_16:
904     case ALLOW_OVERWRITE:
905         cmd->xfer = 0;
906         break;
907     case VERIFY_10:
908     case VERIFY_12:
909     case VERIFY_16:
910         if ((buf[1] & 2) == 0) {
911             cmd->xfer = 0;
912         } else if ((buf[1] & 4) == 1) {
913             cmd->xfer = 1;
914         }
915         cmd->xfer *= dev->blocksize;
916         break;
917     case MODE_SENSE:
918         break;
919     case WRITE_SAME_10:
920     case WRITE_SAME_16:
921         cmd->xfer = dev->blocksize;
922         break;
923     case READ_CAPACITY_10:
924         cmd->xfer = 8;
925         break;
926     case READ_BLOCK_LIMITS:
927         cmd->xfer = 6;
928         break;
929     case SEND_VOLUME_TAG:
930         /* GPCMD_SET_STREAMING from multimedia commands.  */
931         if (dev->type == TYPE_ROM) {
932             cmd->xfer = buf[10] | (buf[9] << 8);
933         } else {
934             cmd->xfer = buf[9] | (buf[8] << 8);
935         }
936         break;
937     case WRITE_6:
938         /* length 0 means 256 blocks */
939         if (cmd->xfer == 0) {
940             cmd->xfer = 256;
941         }
942     case WRITE_10:
943     case WRITE_VERIFY_10:
944     case WRITE_12:
945     case WRITE_VERIFY_12:
946     case WRITE_16:
947     case WRITE_VERIFY_16:
948         cmd->xfer *= dev->blocksize;
949         break;
950     case READ_6:
951     case READ_REVERSE:
952         /* length 0 means 256 blocks */
953         if (cmd->xfer == 0) {
954             cmd->xfer = 256;
955         }
956     case READ_10:
957     case RECOVER_BUFFERED_DATA:
958     case READ_12:
959     case READ_16:
960         cmd->xfer *= dev->blocksize;
961         break;
962     case FORMAT_UNIT:
963         /* MMC mandates the parameter list to be 12-bytes long.  Parameters
964          * for block devices are restricted to the header right now.  */
965         if (dev->type == TYPE_ROM && (buf[1] & 16)) {
966             cmd->xfer = 12;
967         } else {
968             cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
969         }
970         break;
971     case INQUIRY:
972     case RECEIVE_DIAGNOSTIC:
973     case SEND_DIAGNOSTIC:
974         cmd->xfer = buf[4] | (buf[3] << 8);
975         break;
976     case READ_CD:
977     case READ_BUFFER:
978     case WRITE_BUFFER:
979     case SEND_CUE_SHEET:
980         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
981         break;
982     case PERSISTENT_RESERVE_OUT:
983         cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
984         break;
985     case ERASE_12:
986         if (dev->type == TYPE_ROM) {
987             /* MMC command GET PERFORMANCE.  */
988             cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
989                                                     buf[10], buf[1] & 0x1f);
990         }
991         break;
992     case MECHANISM_STATUS:
993     case READ_DVD_STRUCTURE:
994     case SEND_DVD_STRUCTURE:
995     case MAINTENANCE_OUT:
996     case MAINTENANCE_IN:
997         if (dev->type == TYPE_ROM) {
998             /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
999             cmd->xfer = buf[9] | (buf[8] << 8);
1000         }
1001         break;
1002     case ATA_PASSTHROUGH_12:
1003         if (dev->type == TYPE_ROM) {
1004             /* BLANK command of MMC */
1005             cmd->xfer = 0;
1006         } else {
1007             cmd->xfer = ata_passthrough_12_xfer_size(dev, buf);
1008         }
1009         break;
1010     case ATA_PASSTHROUGH_16:
1011         cmd->xfer = ata_passthrough_16_xfer_size(dev, buf);
1012         break;
1013     }
1014     return 0;
1015 }
1016 
1017 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1018 {
1019     switch (buf[0]) {
1020     /* stream commands */
1021     case ERASE_12:
1022     case ERASE_16:
1023         cmd->xfer = 0;
1024         break;
1025     case READ_6:
1026     case READ_REVERSE:
1027     case RECOVER_BUFFERED_DATA:
1028     case WRITE_6:
1029         cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1030         if (buf[1] & 0x01) { /* fixed */
1031             cmd->xfer *= dev->blocksize;
1032         }
1033         break;
1034     case READ_16:
1035     case READ_REVERSE_16:
1036     case VERIFY_16:
1037     case WRITE_16:
1038         cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1039         if (buf[1] & 0x01) { /* fixed */
1040             cmd->xfer *= dev->blocksize;
1041         }
1042         break;
1043     case REWIND:
1044     case LOAD_UNLOAD:
1045         cmd->xfer = 0;
1046         break;
1047     case SPACE_16:
1048         cmd->xfer = buf[13] | (buf[12] << 8);
1049         break;
1050     case READ_POSITION:
1051         switch (buf[1] & 0x1f) /* operation code */ {
1052         case SHORT_FORM_BLOCK_ID:
1053         case SHORT_FORM_VENDOR_SPECIFIC:
1054             cmd->xfer = 20;
1055             break;
1056         case LONG_FORM:
1057             cmd->xfer = 32;
1058             break;
1059         case EXTENDED_FORM:
1060             cmd->xfer = buf[8] | (buf[7] << 8);
1061             break;
1062         default:
1063             return -1;
1064         }
1065 
1066         break;
1067     case FORMAT_UNIT:
1068         cmd->xfer = buf[4] | (buf[3] << 8);
1069         break;
1070     /* generic commands */
1071     default:
1072         return scsi_req_length(cmd, dev, buf);
1073     }
1074     return 0;
1075 }
1076 
1077 static int scsi_req_medium_changer_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1078 {
1079     switch (buf[0]) {
1080     /* medium changer commands */
1081     case EXCHANGE_MEDIUM:
1082     case INITIALIZE_ELEMENT_STATUS:
1083     case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1084     case MOVE_MEDIUM:
1085     case POSITION_TO_ELEMENT:
1086         cmd->xfer = 0;
1087         break;
1088     case READ_ELEMENT_STATUS:
1089         cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1090         break;
1091 
1092     /* generic commands */
1093     default:
1094         return scsi_req_length(cmd, dev, buf);
1095     }
1096     return 0;
1097 }
1098 
1099 
1100 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1101 {
1102     if (!cmd->xfer) {
1103         cmd->mode = SCSI_XFER_NONE;
1104         return;
1105     }
1106     switch (cmd->buf[0]) {
1107     case WRITE_6:
1108     case WRITE_10:
1109     case WRITE_VERIFY_10:
1110     case WRITE_12:
1111     case WRITE_VERIFY_12:
1112     case WRITE_16:
1113     case WRITE_VERIFY_16:
1114     case VERIFY_10:
1115     case VERIFY_12:
1116     case VERIFY_16:
1117     case COPY:
1118     case COPY_VERIFY:
1119     case COMPARE:
1120     case CHANGE_DEFINITION:
1121     case LOG_SELECT:
1122     case MODE_SELECT:
1123     case MODE_SELECT_10:
1124     case SEND_DIAGNOSTIC:
1125     case WRITE_BUFFER:
1126     case FORMAT_UNIT:
1127     case REASSIGN_BLOCKS:
1128     case SEARCH_EQUAL:
1129     case SEARCH_HIGH:
1130     case SEARCH_LOW:
1131     case UPDATE_BLOCK:
1132     case WRITE_LONG_10:
1133     case WRITE_SAME_10:
1134     case WRITE_SAME_16:
1135     case UNMAP:
1136     case SEARCH_HIGH_12:
1137     case SEARCH_EQUAL_12:
1138     case SEARCH_LOW_12:
1139     case MEDIUM_SCAN:
1140     case SEND_VOLUME_TAG:
1141     case SEND_CUE_SHEET:
1142     case SEND_DVD_STRUCTURE:
1143     case PERSISTENT_RESERVE_OUT:
1144     case MAINTENANCE_OUT:
1145         cmd->mode = SCSI_XFER_TO_DEV;
1146         break;
1147     case ATA_PASSTHROUGH_12:
1148     case ATA_PASSTHROUGH_16:
1149         /* T_DIR */
1150         cmd->mode = (cmd->buf[2] & 0x8) ?
1151                    SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1152         break;
1153     default:
1154         cmd->mode = SCSI_XFER_FROM_DEV;
1155         break;
1156     }
1157 }
1158 
1159 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
1160 {
1161     uint8_t *buf = cmd->buf;
1162     uint64_t lba;
1163 
1164     switch (buf[0] >> 5) {
1165     case 0:
1166         lba = ldl_be_p(&buf[0]) & 0x1fffff;
1167         break;
1168     case 1:
1169     case 2:
1170     case 5:
1171         lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
1172         break;
1173     case 4:
1174         lba = ldq_be_p(&buf[2]);
1175         break;
1176     default:
1177         lba = -1;
1178 
1179     }
1180     return lba;
1181 }
1182 
1183 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1184 {
1185     int rc;
1186 
1187     switch (buf[0] >> 5) {
1188     case 0:
1189         cmd->len = 6;
1190         break;
1191     case 1:
1192     case 2:
1193         cmd->len = 10;
1194         break;
1195     case 4:
1196         cmd->len = 16;
1197         break;
1198     case 5:
1199         cmd->len = 12;
1200         break;
1201     default:
1202         return -1;
1203     }
1204 
1205     switch (dev->type) {
1206     case TYPE_TAPE:
1207         rc = scsi_req_stream_length(cmd, dev, buf);
1208         break;
1209     case TYPE_MEDIUM_CHANGER:
1210         rc = scsi_req_medium_changer_length(cmd, dev, buf);
1211         break;
1212     default:
1213         rc = scsi_req_length(cmd, dev, buf);
1214         break;
1215     }
1216 
1217     if (rc != 0)
1218         return rc;
1219 
1220     memcpy(cmd->buf, buf, cmd->len);
1221     scsi_cmd_xfer_mode(cmd);
1222     cmd->lba = scsi_cmd_lba(cmd);
1223     return 0;
1224 }
1225 
1226 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1227 {
1228     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1229 
1230     scsi_device_set_ua(dev, sense);
1231     if (bus->info->change) {
1232         bus->info->change(bus, dev, sense);
1233     }
1234 }
1235 
1236 /*
1237  * Predefined sense codes
1238  */
1239 
1240 /* No sense data available */
1241 const struct SCSISense sense_code_NO_SENSE = {
1242     .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
1243 };
1244 
1245 /* LUN not ready, Manual intervention required */
1246 const struct SCSISense sense_code_LUN_NOT_READY = {
1247     .key = NOT_READY, .asc = 0x04, .ascq = 0x03
1248 };
1249 
1250 /* LUN not ready, Medium not present */
1251 const struct SCSISense sense_code_NO_MEDIUM = {
1252     .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
1253 };
1254 
1255 /* LUN not ready, medium removal prevented */
1256 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
1257     .key = NOT_READY, .asc = 0x53, .ascq = 0x02
1258 };
1259 
1260 /* Hardware error, internal target failure */
1261 const struct SCSISense sense_code_TARGET_FAILURE = {
1262     .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
1263 };
1264 
1265 /* Illegal request, invalid command operation code */
1266 const struct SCSISense sense_code_INVALID_OPCODE = {
1267     .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
1268 };
1269 
1270 /* Illegal request, LBA out of range */
1271 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
1272     .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
1273 };
1274 
1275 /* Illegal request, Invalid field in CDB */
1276 const struct SCSISense sense_code_INVALID_FIELD = {
1277     .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
1278 };
1279 
1280 /* Illegal request, Invalid field in parameter list */
1281 const struct SCSISense sense_code_INVALID_PARAM = {
1282     .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
1283 };
1284 
1285 /* Illegal request, Parameter list length error */
1286 const struct SCSISense sense_code_INVALID_PARAM_LEN = {
1287     .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
1288 };
1289 
1290 /* Illegal request, LUN not supported */
1291 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
1292     .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
1293 };
1294 
1295 /* Illegal request, Saving parameters not supported */
1296 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
1297     .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1298 };
1299 
1300 /* Illegal request, Incompatible medium installed */
1301 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1302     .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1303 };
1304 
1305 /* Illegal request, medium removal prevented */
1306 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1307     .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
1308 };
1309 
1310 /* Illegal request, Invalid Transfer Tag */
1311 const struct SCSISense sense_code_INVALID_TAG = {
1312     .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
1313 };
1314 
1315 /* Command aborted, I/O process terminated */
1316 const struct SCSISense sense_code_IO_ERROR = {
1317     .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1318 };
1319 
1320 /* Command aborted, I_T Nexus loss occurred */
1321 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1322     .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1323 };
1324 
1325 /* Command aborted, Logical Unit failure */
1326 const struct SCSISense sense_code_LUN_FAILURE = {
1327     .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1328 };
1329 
1330 /* Command aborted, Overlapped Commands Attempted */
1331 const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
1332     .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
1333 };
1334 
1335 /* Unit attention, Capacity data has changed */
1336 const struct SCSISense sense_code_CAPACITY_CHANGED = {
1337     .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
1338 };
1339 
1340 /* Unit attention, Power on, reset or bus device reset occurred */
1341 const struct SCSISense sense_code_RESET = {
1342     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1343 };
1344 
1345 /* Unit attention, No medium */
1346 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1347     .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1348 };
1349 
1350 /* Unit attention, Medium may have changed */
1351 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1352     .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1353 };
1354 
1355 /* Unit attention, Reported LUNs data has changed */
1356 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1357     .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1358 };
1359 
1360 /* Unit attention, Device internal reset */
1361 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1362     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1363 };
1364 
1365 /* Data Protection, Write Protected */
1366 const struct SCSISense sense_code_WRITE_PROTECTED = {
1367     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
1368 };
1369 
1370 /*
1371  * scsi_build_sense
1372  *
1373  * Convert between fixed and descriptor sense buffers
1374  */
1375 int scsi_build_sense(uint8_t *in_buf, int in_len,
1376                      uint8_t *buf, int len, bool fixed)
1377 {
1378     bool fixed_in;
1379     SCSISense sense;
1380     if (!fixed && len < 8) {
1381         return 0;
1382     }
1383 
1384     if (in_len == 0) {
1385         sense.key = NO_SENSE;
1386         sense.asc = 0;
1387         sense.ascq = 0;
1388     } else {
1389         fixed_in = (in_buf[0] & 2) == 0;
1390 
1391         if (fixed == fixed_in) {
1392             memcpy(buf, in_buf, MIN(len, in_len));
1393             return MIN(len, in_len);
1394         }
1395 
1396         if (fixed_in) {
1397             sense.key = in_buf[2];
1398             sense.asc = in_buf[12];
1399             sense.ascq = in_buf[13];
1400         } else {
1401             sense.key = in_buf[1];
1402             sense.asc = in_buf[2];
1403             sense.ascq = in_buf[3];
1404         }
1405     }
1406 
1407     memset(buf, 0, len);
1408     if (fixed) {
1409         /* Return fixed format sense buffer */
1410         buf[0] = 0x70;
1411         buf[2] = sense.key;
1412         buf[7] = 10;
1413         buf[12] = sense.asc;
1414         buf[13] = sense.ascq;
1415         return MIN(len, SCSI_SENSE_LEN);
1416     } else {
1417         /* Return descriptor format sense buffer */
1418         buf[0] = 0x72;
1419         buf[1] = sense.key;
1420         buf[2] = sense.asc;
1421         buf[3] = sense.ascq;
1422         return 8;
1423     }
1424 }
1425 
1426 static const char *scsi_command_name(uint8_t cmd)
1427 {
1428     static const char *names[] = {
1429         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
1430         [ REWIND                   ] = "REWIND",
1431         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
1432         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
1433         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
1434         [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1435         /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1436         [ READ_6                   ] = "READ_6",
1437         [ WRITE_6                  ] = "WRITE_6",
1438         [ SET_CAPACITY             ] = "SET_CAPACITY",
1439         [ READ_REVERSE             ] = "READ_REVERSE",
1440         [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
1441         [ SPACE                    ] = "SPACE",
1442         [ INQUIRY                  ] = "INQUIRY",
1443         [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
1444         [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
1445         [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
1446         [ MODE_SELECT              ] = "MODE_SELECT",
1447         [ RESERVE                  ] = "RESERVE",
1448         [ RELEASE                  ] = "RELEASE",
1449         [ COPY                     ] = "COPY",
1450         [ ERASE                    ] = "ERASE",
1451         [ MODE_SENSE               ] = "MODE_SENSE",
1452         [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
1453         /* LOAD_UNLOAD and START_STOP use the same operation code */
1454         [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
1455         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
1456         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
1457         [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
1458         [ READ_10                  ] = "READ_10",
1459         [ WRITE_10                 ] = "WRITE_10",
1460         [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
1461         /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1462         [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
1463         [ VERIFY_10                ] = "VERIFY_10",
1464         [ SEARCH_HIGH              ] = "SEARCH_HIGH",
1465         [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
1466         [ SEARCH_LOW               ] = "SEARCH_LOW",
1467         [ SET_LIMITS               ] = "SET_LIMITS",
1468         [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
1469         /* READ_POSITION and PRE_FETCH use the same operation code */
1470         [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
1471         [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
1472         [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1473         /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1474         [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
1475         [ COMPARE                  ] = "COMPARE",
1476         [ COPY_VERIFY              ] = "COPY_VERIFY",
1477         [ WRITE_BUFFER             ] = "WRITE_BUFFER",
1478         [ READ_BUFFER              ] = "READ_BUFFER",
1479         [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
1480         [ READ_LONG_10             ] = "READ_LONG_10",
1481         [ WRITE_LONG_10            ] = "WRITE_LONG_10",
1482         [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
1483         [ WRITE_SAME_10            ] = "WRITE_SAME_10",
1484         [ UNMAP                    ] = "UNMAP",
1485         [ READ_TOC                 ] = "READ_TOC",
1486         [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
1487         [ SANITIZE                 ] = "SANITIZE",
1488         [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
1489         [ LOG_SELECT               ] = "LOG_SELECT",
1490         [ LOG_SENSE                ] = "LOG_SENSE",
1491         [ MODE_SELECT_10           ] = "MODE_SELECT_10",
1492         [ RESERVE_10               ] = "RESERVE_10",
1493         [ RELEASE_10               ] = "RELEASE_10",
1494         [ MODE_SENSE_10            ] = "MODE_SENSE_10",
1495         [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
1496         [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
1497         [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
1498         [ EXTENDED_COPY            ] = "EXTENDED_COPY",
1499         [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
1500         [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
1501         [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
1502         [ READ_16                  ] = "READ_16",
1503         [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
1504         [ WRITE_16                 ] = "WRITE_16",
1505         [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
1506         [ VERIFY_16                ] = "VERIFY_16",
1507         [ PRE_FETCH_16             ] = "PRE_FETCH_16",
1508         [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1509         /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1510         [ LOCATE_16                ] = "LOCATE_16",
1511         [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
1512         /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1513         [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
1514         [ WRITE_LONG_16            ] = "WRITE_LONG_16",
1515         [ REPORT_LUNS              ] = "REPORT_LUNS",
1516         [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
1517         [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
1518         [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
1519         [ READ_12                  ] = "READ_12",
1520         [ WRITE_12                 ] = "WRITE_12",
1521         [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
1522         /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1523         [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
1524         [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
1525         [ VERIFY_12                ] = "VERIFY_12",
1526         [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
1527         [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
1528         [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
1529         [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
1530         [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
1531         /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1532         [ READ_CD                  ] = "READ_CD",
1533         [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
1534         [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
1535         [ RESERVE_TRACK            ] = "RESERVE_TRACK",
1536         [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
1537         [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
1538         [ SET_CD_SPEED             ] = "SET_CD_SPEED",
1539         [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
1540         [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
1541         [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
1542     };
1543 
1544     if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1545         return "*UNKNOWN*";
1546     return names[cmd];
1547 }
1548 
1549 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1550 {
1551     assert(req->refcount > 0);
1552     req->refcount++;
1553     return req;
1554 }
1555 
1556 void scsi_req_unref(SCSIRequest *req)
1557 {
1558     assert(req->refcount > 0);
1559     if (--req->refcount == 0) {
1560         BusState *qbus = req->dev->qdev.parent_bus;
1561         SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1562 
1563         if (bus->info->free_request && req->hba_private) {
1564             bus->info->free_request(bus, req->hba_private);
1565         }
1566         if (req->ops->free_req) {
1567             req->ops->free_req(req);
1568         }
1569         object_unref(OBJECT(req->dev));
1570         object_unref(OBJECT(qbus->parent));
1571         g_free(req);
1572     }
1573 }
1574 
1575 /* Tell the device that we finished processing this chunk of I/O.  It
1576    will start the next chunk or complete the command.  */
1577 void scsi_req_continue(SCSIRequest *req)
1578 {
1579     if (req->io_canceled) {
1580         trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1581         return;
1582     }
1583     trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1584     if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1585         req->ops->write_data(req);
1586     } else {
1587         req->ops->read_data(req);
1588     }
1589 }
1590 
1591 /* Called by the devices when data is ready for the HBA.  The HBA should
1592    start a DMA operation to read or fill the device's data buffer.
1593    Once it completes, calling scsi_req_continue will restart I/O.  */
1594 void scsi_req_data(SCSIRequest *req, int len)
1595 {
1596     uint8_t *buf;
1597     if (req->io_canceled) {
1598         trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1599         return;
1600     }
1601     trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1602     assert(req->cmd.mode != SCSI_XFER_NONE);
1603     if (!req->sg) {
1604         req->resid -= len;
1605         req->bus->info->transfer_data(req, len);
1606         return;
1607     }
1608 
1609     /* If the device calls scsi_req_data and the HBA specified a
1610      * scatter/gather list, the transfer has to happen in a single
1611      * step.  */
1612     assert(!req->dma_started);
1613     req->dma_started = true;
1614 
1615     buf = scsi_req_get_buf(req);
1616     if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1617         req->resid = dma_buf_read(buf, len, req->sg);
1618     } else {
1619         req->resid = dma_buf_write(buf, len, req->sg);
1620     }
1621     scsi_req_continue(req);
1622 }
1623 
1624 void scsi_req_print(SCSIRequest *req)
1625 {
1626     FILE *fp = stderr;
1627     int i;
1628 
1629     fprintf(fp, "[%s id=%d] %s",
1630             req->dev->qdev.parent_bus->name,
1631             req->dev->id,
1632             scsi_command_name(req->cmd.buf[0]));
1633     for (i = 1; i < req->cmd.len; i++) {
1634         fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1635     }
1636     switch (req->cmd.mode) {
1637     case SCSI_XFER_NONE:
1638         fprintf(fp, " - none\n");
1639         break;
1640     case SCSI_XFER_FROM_DEV:
1641         fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1642         break;
1643     case SCSI_XFER_TO_DEV:
1644         fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1645         break;
1646     default:
1647         fprintf(fp, " - Oops\n");
1648         break;
1649     }
1650 }
1651 
1652 void scsi_req_complete(SCSIRequest *req, int status)
1653 {
1654     assert(req->status == -1);
1655     req->status = status;
1656 
1657     assert(req->sense_len <= sizeof(req->sense));
1658     if (status == GOOD) {
1659         req->sense_len = 0;
1660     }
1661 
1662     if (req->sense_len) {
1663         memcpy(req->dev->sense, req->sense, req->sense_len);
1664         req->dev->sense_len = req->sense_len;
1665         req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1666     } else {
1667         req->dev->sense_len = 0;
1668         req->dev->sense_is_ua = false;
1669     }
1670 
1671     /*
1672      * Unit attention state is now stored in the device's sense buffer
1673      * if the HBA didn't do autosense.  Clear the pending unit attention
1674      * flags.
1675      */
1676     scsi_clear_unit_attention(req);
1677 
1678     scsi_req_ref(req);
1679     scsi_req_dequeue(req);
1680     req->bus->info->complete(req, req->status, req->resid);
1681     scsi_req_unref(req);
1682 }
1683 
1684 void scsi_req_cancel(SCSIRequest *req)
1685 {
1686     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1687     if (!req->enqueued) {
1688         return;
1689     }
1690     scsi_req_ref(req);
1691     scsi_req_dequeue(req);
1692     req->io_canceled = true;
1693     if (req->ops->cancel_io) {
1694         req->ops->cancel_io(req);
1695     }
1696     if (req->bus->info->cancel) {
1697         req->bus->info->cancel(req);
1698     }
1699     scsi_req_unref(req);
1700 }
1701 
1702 void scsi_req_abort(SCSIRequest *req, int status)
1703 {
1704     if (!req->enqueued) {
1705         return;
1706     }
1707     scsi_req_ref(req);
1708     scsi_req_dequeue(req);
1709     req->io_canceled = true;
1710     if (req->ops->cancel_io) {
1711         req->ops->cancel_io(req);
1712     }
1713     scsi_req_complete(req, status);
1714     scsi_req_unref(req);
1715 }
1716 
1717 static int scsi_ua_precedence(SCSISense sense)
1718 {
1719     if (sense.key != UNIT_ATTENTION) {
1720         return INT_MAX;
1721     }
1722     if (sense.asc == 0x29 && sense.ascq == 0x04) {
1723         /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1724         return 1;
1725     } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1726         /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1727         return 2;
1728     } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1729         /* These two go with "all others". */
1730         ;
1731     } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1732         /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1733          * POWER ON OCCURRED = 1
1734          * SCSI BUS RESET OCCURRED = 2
1735          * BUS DEVICE RESET FUNCTION OCCURRED = 3
1736          * I_T NEXUS LOSS OCCURRED = 7
1737          */
1738         return sense.ascq;
1739     } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1740         /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION  */
1741         return 8;
1742     }
1743     return (sense.asc << 8) | sense.ascq;
1744 }
1745 
1746 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1747 {
1748     int prec1, prec2;
1749     if (sense.key != UNIT_ATTENTION) {
1750         return;
1751     }
1752     trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1753                              sense.asc, sense.ascq);
1754 
1755     /*
1756      * Override a pre-existing unit attention condition, except for a more
1757      * important reset condition.
1758     */
1759     prec1 = scsi_ua_precedence(sdev->unit_attention);
1760     prec2 = scsi_ua_precedence(sense);
1761     if (prec2 < prec1) {
1762         sdev->unit_attention = sense;
1763     }
1764 }
1765 
1766 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1767 {
1768     SCSIRequest *req;
1769 
1770     while (!QTAILQ_EMPTY(&sdev->requests)) {
1771         req = QTAILQ_FIRST(&sdev->requests);
1772         scsi_req_cancel(req);
1773     }
1774 
1775     scsi_device_set_ua(sdev, sense);
1776 }
1777 
1778 static char *scsibus_get_dev_path(DeviceState *dev)
1779 {
1780     SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1781     DeviceState *hba = dev->parent_bus->parent;
1782     char *id;
1783     char *path;
1784 
1785     id = qdev_get_dev_path(hba);
1786     if (id) {
1787         path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1788     } else {
1789         path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1790     }
1791     g_free(id);
1792     return path;
1793 }
1794 
1795 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1796 {
1797     SCSIDevice *d = SCSI_DEVICE(dev);
1798     return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1799                            qdev_fw_name(dev), d->id, d->lun);
1800 }
1801 
1802 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1803 {
1804     BusChild *kid;
1805     SCSIDevice *target_dev = NULL;
1806 
1807     QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) {
1808         DeviceState *qdev = kid->child;
1809         SCSIDevice *dev = SCSI_DEVICE(qdev);
1810 
1811         if (dev->channel == channel && dev->id == id) {
1812             if (dev->lun == lun) {
1813                 return dev;
1814             }
1815             target_dev = dev;
1816         }
1817     }
1818     return target_dev;
1819 }
1820 
1821 /* SCSI request list.  For simplicity, pv points to the whole device */
1822 
1823 static void put_scsi_requests(QEMUFile *f, void *pv, size_t size)
1824 {
1825     SCSIDevice *s = pv;
1826     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1827     SCSIRequest *req;
1828 
1829     QTAILQ_FOREACH(req, &s->requests, next) {
1830         assert(!req->io_canceled);
1831         assert(req->status == -1);
1832         assert(req->enqueued);
1833 
1834         qemu_put_sbyte(f, req->retry ? 1 : 2);
1835         qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1836         qemu_put_be32s(f, &req->tag);
1837         qemu_put_be32s(f, &req->lun);
1838         if (bus->info->save_request) {
1839             bus->info->save_request(f, req);
1840         }
1841         if (req->ops->save_request) {
1842             req->ops->save_request(f, req);
1843         }
1844     }
1845     qemu_put_sbyte(f, 0);
1846 }
1847 
1848 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size)
1849 {
1850     SCSIDevice *s = pv;
1851     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1852     int8_t sbyte;
1853 
1854     while ((sbyte = qemu_get_sbyte(f)) > 0) {
1855         uint8_t buf[SCSI_CMD_BUF_SIZE];
1856         uint32_t tag;
1857         uint32_t lun;
1858         SCSIRequest *req;
1859 
1860         qemu_get_buffer(f, buf, sizeof(buf));
1861         qemu_get_be32s(f, &tag);
1862         qemu_get_be32s(f, &lun);
1863         req = scsi_req_new(s, tag, lun, buf, NULL);
1864         req->retry = (sbyte == 1);
1865         if (bus->info->load_request) {
1866             req->hba_private = bus->info->load_request(f, req);
1867         }
1868         if (req->ops->load_request) {
1869             req->ops->load_request(f, req);
1870         }
1871 
1872         /* Just restart it later.  */
1873         scsi_req_enqueue_internal(req);
1874 
1875         /* At this point, the request will be kept alive by the reference
1876          * added by scsi_req_enqueue_internal, so we can release our reference.
1877          * The HBA of course will add its own reference in the load_request
1878          * callback if it needs to hold on the SCSIRequest.
1879          */
1880         scsi_req_unref(req);
1881     }
1882 
1883     return 0;
1884 }
1885 
1886 static int scsi_qdev_unplug(DeviceState *qdev)
1887 {
1888     SCSIDevice *dev = SCSI_DEVICE(qdev);
1889     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1890 
1891     if (bus->info->hot_unplug) {
1892         bus->info->hot_unplug(bus, dev);
1893     }
1894     return qdev_simple_unplug_cb(qdev);
1895 }
1896 
1897 static const VMStateInfo vmstate_info_scsi_requests = {
1898     .name = "scsi-requests",
1899     .get  = get_scsi_requests,
1900     .put  = put_scsi_requests,
1901 };
1902 
1903 const VMStateDescription vmstate_scsi_device = {
1904     .name = "SCSIDevice",
1905     .version_id = 1,
1906     .minimum_version_id = 1,
1907     .minimum_version_id_old = 1,
1908     .fields = (VMStateField[]) {
1909         VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1910         VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1911         VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1912         VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1913         VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE),
1914         VMSTATE_UINT32(sense_len, SCSIDevice),
1915         {
1916             .name         = "requests",
1917             .version_id   = 0,
1918             .field_exists = NULL,
1919             .size         = 0,   /* ouch */
1920             .info         = &vmstate_info_scsi_requests,
1921             .flags        = VMS_SINGLE,
1922             .offset       = 0,
1923         },
1924         VMSTATE_END_OF_LIST()
1925     }
1926 };
1927 
1928 static void scsi_device_class_init(ObjectClass *klass, void *data)
1929 {
1930     DeviceClass *k = DEVICE_CLASS(klass);
1931     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1932     k->bus_type = TYPE_SCSI_BUS;
1933     k->init     = scsi_qdev_init;
1934     k->unplug   = scsi_qdev_unplug;
1935     k->exit     = scsi_qdev_exit;
1936     k->props    = scsi_props;
1937 }
1938 
1939 static const TypeInfo scsi_device_type_info = {
1940     .name = TYPE_SCSI_DEVICE,
1941     .parent = TYPE_DEVICE,
1942     .instance_size = sizeof(SCSIDevice),
1943     .abstract = true,
1944     .class_size = sizeof(SCSIDeviceClass),
1945     .class_init = scsi_device_class_init,
1946 };
1947 
1948 static void scsi_register_types(void)
1949 {
1950     type_register_static(&scsi_bus_info);
1951     type_register_static(&scsi_device_type_info);
1952 }
1953 
1954 type_init(scsi_register_types)
1955