1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qemu/error-report.h"
4 #include "qemu/module.h"
5 #include "qemu/option.h"
6 #include "hw/scsi/scsi.h"
7 #include "migration/qemu-file-types.h"
8 #include "migration/vmstate.h"
9 #include "scsi/constants.h"
11 #include "sysemu/block-backend.h"
12 #include "sysemu/blockdev.h"
14 #include "sysemu/dma.h"
15 #include "qemu/cutils.h"
17 static char *scsibus_get_dev_path(DeviceState
*dev
);
18 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
19 static void scsi_req_dequeue(SCSIRequest
*req
);
20 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
21 static void scsi_target_free_buf(SCSIRequest
*req
);
23 static Property scsi_props
[] = {
24 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
25 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
26 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
27 DEFINE_PROP_END_OF_LIST(),
30 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
32 BusClass
*k
= BUS_CLASS(klass
);
33 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
35 k
->get_dev_path
= scsibus_get_dev_path
;
36 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
37 hc
->unplug
= qdev_simple_device_unplug_cb
;
40 static const TypeInfo scsi_bus_info
= {
41 .name
= TYPE_SCSI_BUS
,
43 .instance_size
= sizeof(SCSIBus
),
44 .class_init
= scsi_bus_class_init
,
45 .interfaces
= (InterfaceInfo
[]) {
46 { TYPE_HOTPLUG_HANDLER
},
50 static int next_scsi_bus
;
52 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
54 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
60 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
63 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
66 assert(cmd
->len
== 0);
67 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
68 if (bus
->info
->parse_cdb
) {
69 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
74 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
75 uint8_t *buf
, void *hba_private
)
77 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
79 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
85 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
87 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
88 if (sc
->unit_attention_reported
) {
89 sc
->unit_attention_reported(s
);
93 /* Create a scsi bus, and attach devices to it. */
94 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
95 const SCSIBusInfo
*info
, const char *bus_name
)
97 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
98 bus
->busnr
= next_scsi_bus
++;
100 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
103 static void scsi_dma_restart_bh(void *opaque
)
105 SCSIDevice
*s
= opaque
;
106 SCSIRequest
*req
, *next
;
108 qemu_bh_delete(s
->bh
);
111 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
112 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
116 switch (req
->cmd
.mode
) {
117 case SCSI_XFER_FROM_DEV
:
118 case SCSI_XFER_TO_DEV
:
119 scsi_req_continue(req
);
122 scsi_req_dequeue(req
);
123 scsi_req_enqueue(req
);
129 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
132 void scsi_req_retry(SCSIRequest
*req
)
134 /* No need to save a reference, because scsi_dma_restart_bh just
135 * looks at the request list. */
139 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
141 SCSIDevice
*s
= opaque
;
147 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
148 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
149 qemu_bh_schedule(s
->bh
);
153 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
155 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
156 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
158 Error
*local_err
= NULL
;
160 if (dev
->channel
> bus
->info
->max_channel
) {
161 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
164 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
165 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
168 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
169 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
175 if (dev
->lun
== -1) {
179 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
180 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
181 if (d
&& d
->lun
== dev
->lun
) {
182 error_setg(errp
, "no free target");
186 } else if (dev
->lun
== -1) {
189 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
190 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
191 if (d
&& d
->lun
== lun
) {
192 error_setg(errp
, "no free lun");
197 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
199 if (d
->lun
== dev
->lun
&& dev
!= d
) {
200 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
205 QTAILQ_INIT(&dev
->requests
);
206 scsi_device_realize(dev
, &local_err
);
208 error_propagate(errp
, local_err
);
211 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
212 scsi_dma_restart_cb
, dev
);
215 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
217 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
220 qemu_del_vm_change_state_handler(dev
->vmsentry
);
223 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
224 blockdev_mark_auto_del(dev
->conf
.blk
);
227 /* handle legacy '-drive if=scsi,...' cmd line args */
228 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
229 int unit
, bool removable
, int bootindex
,
231 BlockdevOnError rerror
,
232 BlockdevOnError werror
,
233 const char *serial
, Error
**errp
)
240 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
241 dev
= qdev_create(&bus
->qbus
, driver
);
242 name
= g_strdup_printf("legacy[%d]", unit
);
243 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
246 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
247 if (bootindex
>= 0) {
248 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
251 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
252 qdev_prop_set_bit(dev
, "removable", removable
);
254 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
255 qdev_prop_set_string(dev
, "serial", serial
);
257 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
259 error_propagate(errp
, err
);
260 object_unparent(OBJECT(dev
));
263 object_property_set_bool(OBJECT(dev
), share_rw
, "share-rw", &err
);
265 error_propagate(errp
, err
);
266 object_unparent(OBJECT(dev
));
270 qdev_prop_set_enum(dev
, "rerror", rerror
);
271 qdev_prop_set_enum(dev
, "werror", werror
);
273 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
275 error_propagate(errp
, err
);
276 object_unparent(OBJECT(dev
));
279 return SCSI_DEVICE(dev
);
282 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
289 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
290 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
294 qemu_opts_loc_restore(dinfo
->opts
);
295 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
296 unit
, false, -1, false,
297 BLOCKDEV_ON_ERROR_AUTO
,
298 BLOCKDEV_ON_ERROR_AUTO
,
304 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
306 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
307 scsi_req_complete(req
, CHECK_CONDITION
);
311 static const struct SCSIReqOps reqops_invalid_field
= {
312 .size
= sizeof(SCSIRequest
),
313 .send_command
= scsi_invalid_field
316 /* SCSIReqOps implementation for invalid commands. */
318 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
320 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
321 scsi_req_complete(req
, CHECK_CONDITION
);
325 static const struct SCSIReqOps reqops_invalid_opcode
= {
326 .size
= sizeof(SCSIRequest
),
327 .send_command
= scsi_invalid_command
330 /* SCSIReqOps implementation for unit attention conditions. */
332 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
334 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
335 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
336 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
337 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
339 scsi_req_complete(req
, CHECK_CONDITION
);
343 static const struct SCSIReqOps reqops_unit_attention
= {
344 .size
= sizeof(SCSIRequest
),
345 .send_command
= scsi_unit_attention
348 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
351 typedef struct SCSITargetReq SCSITargetReq
;
353 struct SCSITargetReq
{
360 static void store_lun(uint8_t *outbuf
, int lun
)
366 outbuf
[1] = (lun
& 255);
367 outbuf
[0] = (lun
>> 8) | 0x40;
370 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
377 if (r
->req
.cmd
.xfer
< 16) {
380 if (r
->req
.cmd
.buf
[2] > 2) {
383 channel
= r
->req
.dev
->channel
;
387 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
388 DeviceState
*qdev
= kid
->child
;
389 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
391 if (dev
->channel
== channel
&& dev
->id
== id
) {
402 scsi_target_alloc_buf(&r
->req
, n
+ 8);
404 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
405 memset(r
->buf
, 0, len
);
406 stl_be_p(&r
->buf
[0], n
);
407 i
= found_lun0
? 8 : 16;
408 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
409 DeviceState
*qdev
= kid
->child
;
410 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
412 if (dev
->channel
== channel
&& dev
->id
== id
) {
413 store_lun(&r
->buf
[i
], dev
->lun
);
422 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
424 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
426 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
428 if (r
->req
.cmd
.buf
[1] & 0x2) {
429 /* Command support data - optional, not implemented */
433 if (r
->req
.cmd
.buf
[1] & 0x1) {
434 /* Vital product data */
435 uint8_t page_code
= r
->req
.cmd
.buf
[2];
436 r
->buf
[r
->len
++] = page_code
; /* this page */
437 r
->buf
[r
->len
++] = 0x00;
440 case 0x00: /* Supported page codes, mandatory */
444 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
445 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
452 assert(r
->len
< r
->buf_len
);
453 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
457 /* Standard INQUIRY data */
458 if (r
->req
.cmd
.buf
[2] != 0) {
463 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
464 memset(r
->buf
, 0, r
->len
);
465 if (r
->req
.lun
!= 0) {
466 r
->buf
[0] = TYPE_NO_LUN
;
468 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
469 r
->buf
[2] = 5; /* Version */
470 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
471 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
472 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
473 memcpy(&r
->buf
[8], "QEMU ", 8);
474 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
475 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
480 static size_t scsi_sense_len(SCSIRequest
*req
)
482 if (req
->dev
->type
== TYPE_SCANNER
)
483 return SCSI_SENSE_LEN_SCANNER
;
485 return SCSI_SENSE_LEN
;
488 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
490 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
491 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
494 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
495 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
496 scsi_req_complete(req
, CHECK_CONDITION
);
501 if (!scsi_target_emulate_report_luns(r
)) {
502 goto illegal_request
;
506 if (!scsi_target_emulate_inquiry(r
)) {
507 goto illegal_request
;
511 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
513 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
515 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
518 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
519 MIN(req
->cmd
.xfer
, r
->buf_len
),
522 if (r
->req
.dev
->sense_is_ua
) {
523 scsi_device_unit_attention_reported(req
->dev
);
524 r
->req
.dev
->sense_len
= 0;
525 r
->req
.dev
->sense_is_ua
= false;
528 case TEST_UNIT_READY
:
531 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
532 scsi_req_complete(req
, CHECK_CONDITION
);
535 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
536 scsi_req_complete(req
, CHECK_CONDITION
);
541 scsi_req_complete(req
, GOOD
);
546 static void scsi_target_read_data(SCSIRequest
*req
)
548 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
554 scsi_req_data(&r
->req
, n
);
556 scsi_req_complete(&r
->req
, GOOD
);
560 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
562 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
567 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
569 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
571 r
->buf
= g_malloc(len
);
577 static void scsi_target_free_buf(SCSIRequest
*req
)
579 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
584 static const struct SCSIReqOps reqops_target_command
= {
585 .size
= sizeof(SCSITargetReq
),
586 .send_command
= scsi_target_send_command
,
587 .read_data
= scsi_target_read_data
,
588 .get_buf
= scsi_target_get_buf
,
589 .free_req
= scsi_target_free_buf
,
593 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
594 uint32_t tag
, uint32_t lun
, void *hba_private
)
597 SCSIBus
*bus
= scsi_bus_from_device(d
);
598 BusState
*qbus
= BUS(bus
);
599 const int memset_off
= offsetof(SCSIRequest
, sense
)
600 + sizeof(req
->sense
);
602 req
= g_malloc(reqops
->size
);
603 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
609 req
->hba_private
= hba_private
;
612 object_ref(OBJECT(d
));
613 object_ref(OBJECT(qbus
->parent
));
614 notifier_list_init(&req
->cancel_notifiers
);
615 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
619 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
620 uint8_t *buf
, void *hba_private
)
622 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
623 const SCSIReqOps
*ops
;
624 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
626 SCSICommand cmd
= { .len
= 0 };
629 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
630 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
631 (buf
[0] != INQUIRY
&&
632 buf
[0] != REPORT_LUNS
&&
633 buf
[0] != GET_CONFIGURATION
&&
634 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
637 * If we already have a pending unit attention condition,
638 * report this one before triggering another one.
640 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
641 ops
= &reqops_unit_attention
;
642 } else if (lun
!= d
->lun
||
643 buf
[0] == REPORT_LUNS
||
644 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
645 ops
= &reqops_target_command
;
650 if (ops
!= NULL
|| !sc
->parse_cdb
) {
651 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
653 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
657 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
658 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
660 assert(cmd
.len
!= 0);
661 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
664 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
668 if (cmd
.xfer
> INT32_MAX
) {
669 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
671 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
673 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
678 req
->resid
= req
->cmd
.xfer
;
682 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
684 case TEST_UNIT_READY
:
685 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
688 trace_scsi_report_luns(d
->id
, lun
, tag
);
691 trace_scsi_request_sense(d
->id
, lun
, tag
);
700 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
702 return req
->ops
->get_buf(req
);
705 static void scsi_clear_unit_attention(SCSIRequest
*req
)
708 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
709 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
714 * If an INQUIRY command enters the enabled command state,
715 * the device server shall [not] clear any unit attention condition;
716 * See also MMC-6, paragraphs 6.5 and 6.6.2.
718 if (req
->cmd
.buf
[0] == INQUIRY
||
719 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
720 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
724 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
725 ua
= &req
->dev
->unit_attention
;
727 ua
= &req
->bus
->unit_attention
;
731 * If a REPORT LUNS command enters the enabled command state, [...]
732 * the device server shall clear any pending unit attention condition
733 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
735 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
736 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
737 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
741 *ua
= SENSE_CODE(NO_SENSE
);
744 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
749 if (!req
->sense_len
) {
753 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
756 * FIXME: clearing unit attention conditions upon autosense should be done
757 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
760 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
761 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
762 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
764 if (req
->dev
->sense_is_ua
) {
765 scsi_device_unit_attention_reported(req
->dev
);
766 req
->dev
->sense_len
= 0;
767 req
->dev
->sense_is_ua
= false;
772 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
774 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
777 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
779 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
780 sense
.key
, sense
.asc
, sense
.ascq
);
781 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
784 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
786 assert(!req
->enqueued
);
788 if (req
->bus
->info
->get_sg_list
) {
789 req
->sg
= req
->bus
->info
->get_sg_list(req
);
793 req
->enqueued
= true;
794 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
797 int32_t scsi_req_enqueue(SCSIRequest
*req
)
802 scsi_req_enqueue_internal(req
);
804 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
809 static void scsi_req_dequeue(SCSIRequest
*req
)
811 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
814 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
815 req
->enqueued
= false;
820 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
822 /* MMC-6, paragraph 6.7. */
825 if ((data_type
& 3) == 0) {
826 /* Each descriptor is as in Table 295 - Nominal performance. */
827 return 16 * num_desc
+ 8;
829 /* Each descriptor is as in Table 296 - Exceptions. */
830 return 6 * num_desc
+ 8;
835 return 8 * num_desc
+ 8;
837 return 2048 * num_desc
+ 8;
839 return 16 * num_desc
+ 8;
845 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
847 int byte_block
= (buf
[2] >> 2) & 0x1;
848 int type
= (buf
[2] >> 4) & 0x1;
853 xfer_unit
= dev
->blocksize
;
864 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
866 int length
= buf
[2] & 0x3;
868 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
872 case 3: /* USB-specific. */
887 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
889 int extend
= buf
[1] & 0x1;
890 int length
= buf
[2] & 0x3;
892 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
896 case 3: /* USB-specific. */
902 xfer
|= (extend
? buf
[3] << 8 : 0);
906 xfer
|= (extend
? buf
[5] << 8 : 0);
913 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
915 cmd
->xfer
= scsi_cdb_xfer(buf
);
917 case TEST_UNIT_READY
:
921 case WRITE_FILEMARKS
:
922 case WRITE_FILEMARKS_16
:
927 case ALLOW_MEDIUM_REMOVAL
:
929 case SYNCHRONIZE_CACHE
:
930 case SYNCHRONIZE_CACHE_16
:
932 case LOCK_UNLOCK_CACHE
:
941 case ALLOW_OVERWRITE
:
947 if ((buf
[1] & 2) == 0) {
949 } else if ((buf
[1] & 4) != 0) {
952 cmd
->xfer
*= dev
->blocksize
;
958 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
960 case READ_CAPACITY_10
:
963 case READ_BLOCK_LIMITS
:
966 case SEND_VOLUME_TAG
:
967 /* GPCMD_SET_STREAMING from multimedia commands. */
968 if (dev
->type
== TYPE_ROM
) {
969 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
971 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
975 /* length 0 means 256 blocks */
976 if (cmd
->xfer
== 0) {
981 case WRITE_VERIFY_10
:
983 case WRITE_VERIFY_12
:
985 case WRITE_VERIFY_16
:
986 cmd
->xfer
*= dev
->blocksize
;
990 /* length 0 means 256 blocks */
991 if (cmd
->xfer
== 0) {
998 cmd
->xfer
*= dev
->blocksize
;
1001 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1002 * for block devices are restricted to the header right now. */
1003 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1006 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1010 case RECEIVE_DIAGNOSTIC
:
1011 case SEND_DIAGNOSTIC
:
1012 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1017 case SEND_CUE_SHEET
:
1018 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1020 case PERSISTENT_RESERVE_OUT
:
1021 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1024 if (dev
->type
== TYPE_ROM
) {
1025 /* MMC command GET PERFORMANCE. */
1026 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1027 buf
[10], buf
[1] & 0x1f);
1030 case MECHANISM_STATUS
:
1031 case READ_DVD_STRUCTURE
:
1032 case SEND_DVD_STRUCTURE
:
1033 case MAINTENANCE_OUT
:
1034 case MAINTENANCE_IN
:
1035 if (dev
->type
== TYPE_ROM
) {
1036 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1037 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1040 case ATA_PASSTHROUGH_12
:
1041 if (dev
->type
== TYPE_ROM
) {
1042 /* BLANK command of MMC */
1045 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1048 case ATA_PASSTHROUGH_16
:
1049 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1055 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1058 /* stream commands */
1065 case RECOVER_BUFFERED_DATA
:
1067 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1068 if (buf
[1] & 0x01) { /* fixed */
1069 cmd
->xfer
*= dev
->blocksize
;
1073 case READ_REVERSE_16
:
1076 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1077 if (buf
[1] & 0x01) { /* fixed */
1078 cmd
->xfer
*= dev
->blocksize
;
1086 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1089 switch (buf
[1] & 0x1f) /* operation code */ {
1090 case SHORT_FORM_BLOCK_ID
:
1091 case SHORT_FORM_VENDOR_SPECIFIC
:
1098 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1106 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1108 /* generic commands */
1110 return scsi_req_xfer(cmd
, dev
, buf
);
1115 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1118 /* medium changer commands */
1119 case EXCHANGE_MEDIUM
:
1120 case INITIALIZE_ELEMENT_STATUS
:
1121 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1123 case POSITION_TO_ELEMENT
:
1126 case READ_ELEMENT_STATUS
:
1127 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1130 /* generic commands */
1132 return scsi_req_xfer(cmd
, dev
, buf
);
1137 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1140 /* Scanner commands */
1141 case OBJECT_POSITION
:
1151 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1154 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1155 return scsi_req_xfer(cmd
, dev
, buf
);
1161 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1164 cmd
->mode
= SCSI_XFER_NONE
;
1167 switch (cmd
->buf
[0]) {
1170 case WRITE_VERIFY_10
:
1172 case WRITE_VERIFY_12
:
1174 case WRITE_VERIFY_16
:
1181 case CHANGE_DEFINITION
:
1184 case MODE_SELECT_10
:
1185 case SEND_DIAGNOSTIC
:
1188 case REASSIGN_BLOCKS
:
1197 case SEARCH_HIGH_12
:
1198 case SEARCH_EQUAL_12
:
1201 case SEND_VOLUME_TAG
:
1202 case SEND_CUE_SHEET
:
1203 case SEND_DVD_STRUCTURE
:
1204 case PERSISTENT_RESERVE_OUT
:
1205 case MAINTENANCE_OUT
:
1208 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1209 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1211 cmd
->mode
= SCSI_XFER_TO_DEV
;
1213 case ATA_PASSTHROUGH_12
:
1214 case ATA_PASSTHROUGH_16
:
1216 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1217 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1220 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1225 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1231 len
= scsi_cdb_length(buf
);
1237 switch (dev
->type
) {
1239 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1241 case TYPE_MEDIUM_CHANGER
:
1242 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1245 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1248 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1255 memcpy(cmd
->buf
, buf
, cmd
->len
);
1256 scsi_cmd_xfer_mode(cmd
);
1257 cmd
->lba
= scsi_cmd_lba(cmd
);
1261 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1263 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1265 scsi_device_set_ua(dev
, sense
);
1266 if (bus
->info
->change
) {
1267 bus
->info
->change(bus
, dev
, sense
);
1271 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1273 assert(req
->refcount
> 0);
1278 void scsi_req_unref(SCSIRequest
*req
)
1280 assert(req
->refcount
> 0);
1281 if (--req
->refcount
== 0) {
1282 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1283 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1285 if (bus
->info
->free_request
&& req
->hba_private
) {
1286 bus
->info
->free_request(bus
, req
->hba_private
);
1288 if (req
->ops
->free_req
) {
1289 req
->ops
->free_req(req
);
1291 object_unref(OBJECT(req
->dev
));
1292 object_unref(OBJECT(qbus
->parent
));
1297 /* Tell the device that we finished processing this chunk of I/O. It
1298 will start the next chunk or complete the command. */
1299 void scsi_req_continue(SCSIRequest
*req
)
1301 if (req
->io_canceled
) {
1302 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1305 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1306 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1307 req
->ops
->write_data(req
);
1309 req
->ops
->read_data(req
);
1313 /* Called by the devices when data is ready for the HBA. The HBA should
1314 start a DMA operation to read or fill the device's data buffer.
1315 Once it completes, calling scsi_req_continue will restart I/O. */
1316 void scsi_req_data(SCSIRequest
*req
, int len
)
1319 if (req
->io_canceled
) {
1320 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1323 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1324 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1327 req
->bus
->info
->transfer_data(req
, len
);
1331 /* If the device calls scsi_req_data and the HBA specified a
1332 * scatter/gather list, the transfer has to happen in a single
1334 assert(!req
->dma_started
);
1335 req
->dma_started
= true;
1337 buf
= scsi_req_get_buf(req
);
1338 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1339 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1341 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1343 scsi_req_continue(req
);
1346 void scsi_req_print(SCSIRequest
*req
)
1351 fprintf(fp
, "[%s id=%d] %s",
1352 req
->dev
->qdev
.parent_bus
->name
,
1354 scsi_command_name(req
->cmd
.buf
[0]));
1355 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1356 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1358 switch (req
->cmd
.mode
) {
1359 case SCSI_XFER_NONE
:
1360 fprintf(fp
, " - none\n");
1362 case SCSI_XFER_FROM_DEV
:
1363 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1365 case SCSI_XFER_TO_DEV
:
1366 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1369 fprintf(fp
, " - Oops\n");
1374 void scsi_req_complete(SCSIRequest
*req
, int status
)
1376 assert(req
->status
== -1);
1377 req
->status
= status
;
1379 assert(req
->sense_len
<= sizeof(req
->sense
));
1380 if (status
== GOOD
) {
1384 if (req
->sense_len
) {
1385 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1386 req
->dev
->sense_len
= req
->sense_len
;
1387 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1389 req
->dev
->sense_len
= 0;
1390 req
->dev
->sense_is_ua
= false;
1394 * Unit attention state is now stored in the device's sense buffer
1395 * if the HBA didn't do autosense. Clear the pending unit attention
1398 scsi_clear_unit_attention(req
);
1401 scsi_req_dequeue(req
);
1402 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1404 /* Cancelled requests might end up being completed instead of cancelled */
1405 notifier_list_notify(&req
->cancel_notifiers
, req
);
1406 scsi_req_unref(req
);
1409 /* Called by the devices when the request is canceled. */
1410 void scsi_req_cancel_complete(SCSIRequest
*req
)
1412 assert(req
->io_canceled
);
1413 if (req
->bus
->info
->cancel
) {
1414 req
->bus
->info
->cancel(req
);
1416 notifier_list_notify(&req
->cancel_notifiers
, req
);
1417 scsi_req_unref(req
);
1420 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1421 * notifier list, the bus will be notified the requests cancellation is
1424 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1426 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1428 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1430 if (req
->io_canceled
) {
1431 /* A blk_aio_cancel_async is pending; when it finishes,
1432 * scsi_req_cancel_complete will be called and will
1433 * call the notifier we just added. Just wait for that.
1438 /* Dropped in scsi_req_cancel_complete. */
1440 scsi_req_dequeue(req
);
1441 req
->io_canceled
= true;
1443 blk_aio_cancel_async(req
->aiocb
);
1445 scsi_req_cancel_complete(req
);
1449 void scsi_req_cancel(SCSIRequest
*req
)
1451 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1452 if (!req
->enqueued
) {
1455 assert(!req
->io_canceled
);
1456 /* Dropped in scsi_req_cancel_complete. */
1458 scsi_req_dequeue(req
);
1459 req
->io_canceled
= true;
1461 blk_aio_cancel(req
->aiocb
);
1463 scsi_req_cancel_complete(req
);
1467 static int scsi_ua_precedence(SCSISense sense
)
1469 if (sense
.key
!= UNIT_ATTENTION
) {
1472 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1473 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1475 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1476 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1478 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1479 /* These two go with "all others". */
1481 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1482 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1483 * POWER ON OCCURRED = 1
1484 * SCSI BUS RESET OCCURRED = 2
1485 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1486 * I_T NEXUS LOSS OCCURRED = 7
1489 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1490 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1493 return (sense
.asc
<< 8) | sense
.ascq
;
1496 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1499 if (sense
.key
!= UNIT_ATTENTION
) {
1502 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1503 sense
.asc
, sense
.ascq
);
1506 * Override a pre-existing unit attention condition, except for a more
1507 * important reset condition.
1509 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1510 prec2
= scsi_ua_precedence(sense
);
1511 if (prec2
< prec1
) {
1512 sdev
->unit_attention
= sense
;
1516 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1520 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1521 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1522 req
= QTAILQ_FIRST(&sdev
->requests
);
1523 scsi_req_cancel_async(req
, NULL
);
1525 blk_drain(sdev
->conf
.blk
);
1526 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1527 scsi_device_set_ua(sdev
, sense
);
1530 static char *scsibus_get_dev_path(DeviceState
*dev
)
1532 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1533 DeviceState
*hba
= dev
->parent_bus
->parent
;
1537 id
= qdev_get_dev_path(hba
);
1539 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1541 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1547 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1549 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1550 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1551 qdev_fw_name(dev
), d
->id
, d
->lun
);
1554 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1557 SCSIDevice
*target_dev
= NULL
;
1559 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, sibling
) {
1560 DeviceState
*qdev
= kid
->child
;
1561 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1563 if (dev
->channel
== channel
&& dev
->id
== id
) {
1564 if (dev
->lun
== lun
) {
1573 /* SCSI request list. For simplicity, pv points to the whole device */
1575 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1576 const VMStateField
*field
, QJSON
*vmdesc
)
1579 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1582 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1583 assert(!req
->io_canceled
);
1584 assert(req
->status
== -1);
1585 assert(req
->enqueued
);
1587 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1588 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1589 qemu_put_be32s(f
, &req
->tag
);
1590 qemu_put_be32s(f
, &req
->lun
);
1591 if (bus
->info
->save_request
) {
1592 bus
->info
->save_request(f
, req
);
1594 if (req
->ops
->save_request
) {
1595 req
->ops
->save_request(f
, req
);
1598 qemu_put_sbyte(f
, 0);
1603 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1604 const VMStateField
*field
)
1607 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1610 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1611 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1616 qemu_get_buffer(f
, buf
, sizeof(buf
));
1617 qemu_get_be32s(f
, &tag
);
1618 qemu_get_be32s(f
, &lun
);
1619 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1620 req
->retry
= (sbyte
== 1);
1621 if (bus
->info
->load_request
) {
1622 req
->hba_private
= bus
->info
->load_request(f
, req
);
1624 if (req
->ops
->load_request
) {
1625 req
->ops
->load_request(f
, req
);
1628 /* Just restart it later. */
1629 scsi_req_enqueue_internal(req
);
1631 /* At this point, the request will be kept alive by the reference
1632 * added by scsi_req_enqueue_internal, so we can release our reference.
1633 * The HBA of course will add its own reference in the load_request
1634 * callback if it needs to hold on the SCSIRequest.
1636 scsi_req_unref(req
);
1642 static const VMStateInfo vmstate_info_scsi_requests
= {
1643 .name
= "scsi-requests",
1644 .get
= get_scsi_requests
,
1645 .put
= put_scsi_requests
,
1648 static bool scsi_sense_state_needed(void *opaque
)
1650 SCSIDevice
*s
= opaque
;
1652 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1655 static const VMStateDescription vmstate_scsi_sense_state
= {
1656 .name
= "SCSIDevice/sense",
1658 .minimum_version_id
= 1,
1659 .needed
= scsi_sense_state_needed
,
1660 .fields
= (VMStateField
[]) {
1661 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1662 SCSI_SENSE_BUF_SIZE_OLD
,
1663 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1664 VMSTATE_END_OF_LIST()
1668 const VMStateDescription vmstate_scsi_device
= {
1669 .name
= "SCSIDevice",
1671 .minimum_version_id
= 1,
1672 .fields
= (VMStateField
[]) {
1673 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1674 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1675 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1676 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1677 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1678 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1682 .field_exists
= NULL
,
1683 .size
= 0, /* ouch */
1684 .info
= &vmstate_info_scsi_requests
,
1685 .flags
= VMS_SINGLE
,
1688 VMSTATE_END_OF_LIST()
1690 .subsections
= (const VMStateDescription
*[]) {
1691 &vmstate_scsi_sense_state
,
1696 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1698 DeviceClass
*k
= DEVICE_CLASS(klass
);
1699 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1700 k
->bus_type
= TYPE_SCSI_BUS
;
1701 k
->realize
= scsi_qdev_realize
;
1702 k
->unrealize
= scsi_qdev_unrealize
;
1703 k
->props
= scsi_props
;
1706 static void scsi_dev_instance_init(Object
*obj
)
1708 DeviceState
*dev
= DEVICE(obj
);
1709 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1711 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1716 static const TypeInfo scsi_device_type_info
= {
1717 .name
= TYPE_SCSI_DEVICE
,
1718 .parent
= TYPE_DEVICE
,
1719 .instance_size
= sizeof(SCSIDevice
),
1721 .class_size
= sizeof(SCSIDeviceClass
),
1722 .class_init
= scsi_device_class_init
,
1723 .instance_init
= scsi_dev_instance_init
,
1726 static void scsi_register_types(void)
1728 type_register_static(&scsi_bus_info
);
1729 type_register_static(&scsi_device_type_info
);
1732 type_init(scsi_register_types
)