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/qdev-properties.h"
7 #include "hw/scsi/scsi.h"
8 #include "migration/qemu-file-types.h"
9 #include "migration/vmstate.h"
10 #include "scsi/constants.h"
11 #include "sysemu/block-backend.h"
12 #include "sysemu/blockdev.h"
13 #include "sysemu/sysemu.h"
14 #include "sysemu/runstate.h"
16 #include "sysemu/dma.h"
17 #include "qemu/cutils.h"
19 static char *scsibus_get_dev_path(DeviceState
*dev
);
20 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
21 static void scsi_req_dequeue(SCSIRequest
*req
);
22 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
23 static void scsi_target_free_buf(SCSIRequest
*req
);
25 static Property scsi_props
[] = {
26 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
27 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
28 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
29 DEFINE_PROP_END_OF_LIST(),
32 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
34 BusClass
*k
= BUS_CLASS(klass
);
35 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
37 k
->get_dev_path
= scsibus_get_dev_path
;
38 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
39 hc
->unplug
= qdev_simple_device_unplug_cb
;
42 static const TypeInfo scsi_bus_info
= {
43 .name
= TYPE_SCSI_BUS
,
45 .instance_size
= sizeof(SCSIBus
),
46 .class_init
= scsi_bus_class_init
,
47 .interfaces
= (InterfaceInfo
[]) {
48 { TYPE_HOTPLUG_HANDLER
},
52 static int next_scsi_bus
;
54 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
56 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
62 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
65 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
68 assert(cmd
->len
== 0);
69 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
70 if (bus
->info
->parse_cdb
) {
71 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
76 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
77 uint8_t *buf
, void *hba_private
)
79 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
81 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
87 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
89 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
90 if (sc
->unit_attention_reported
) {
91 sc
->unit_attention_reported(s
);
95 /* Create a scsi bus, and attach devices to it. */
96 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
97 const SCSIBusInfo
*info
, const char *bus_name
)
99 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
100 bus
->busnr
= next_scsi_bus
++;
102 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
105 static void scsi_dma_restart_bh(void *opaque
)
107 SCSIDevice
*s
= opaque
;
108 SCSIRequest
*req
, *next
;
110 qemu_bh_delete(s
->bh
);
113 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
114 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
118 switch (req
->cmd
.mode
) {
119 case SCSI_XFER_FROM_DEV
:
120 case SCSI_XFER_TO_DEV
:
121 scsi_req_continue(req
);
124 scsi_req_dequeue(req
);
125 scsi_req_enqueue(req
);
131 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
134 void scsi_req_retry(SCSIRequest
*req
)
136 /* No need to save a reference, because scsi_dma_restart_bh just
137 * looks at the request list. */
141 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
143 SCSIDevice
*s
= opaque
;
149 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
150 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
151 qemu_bh_schedule(s
->bh
);
155 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
157 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
158 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
160 Error
*local_err
= NULL
;
162 if (dev
->channel
> bus
->info
->max_channel
) {
163 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
166 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
167 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
170 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
171 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
177 if (dev
->lun
== -1) {
181 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
182 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
183 if (d
&& d
->lun
== dev
->lun
) {
184 error_setg(errp
, "no free target");
188 } else if (dev
->lun
== -1) {
191 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
192 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
193 if (d
&& d
->lun
== lun
) {
194 error_setg(errp
, "no free lun");
199 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
201 if (d
->lun
== dev
->lun
&& dev
!= d
) {
202 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
207 QTAILQ_INIT(&dev
->requests
);
208 scsi_device_realize(dev
, &local_err
);
210 error_propagate(errp
, local_err
);
213 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
214 scsi_dma_restart_cb
, dev
);
217 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
219 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
222 qemu_del_vm_change_state_handler(dev
->vmsentry
);
225 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
226 blockdev_mark_auto_del(dev
->conf
.blk
);
229 /* handle legacy '-drive if=scsi,...' cmd line args */
230 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
231 int unit
, bool removable
, int bootindex
,
233 BlockdevOnError rerror
,
234 BlockdevOnError werror
,
235 const char *serial
, Error
**errp
)
242 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
243 dev
= qdev_create(&bus
->qbus
, driver
);
244 name
= g_strdup_printf("legacy[%d]", unit
);
245 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
248 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
249 if (bootindex
>= 0) {
250 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
253 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
254 qdev_prop_set_bit(dev
, "removable", removable
);
256 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
257 qdev_prop_set_string(dev
, "serial", serial
);
259 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
261 error_propagate(errp
, err
);
262 object_unparent(OBJECT(dev
));
265 object_property_set_bool(OBJECT(dev
), share_rw
, "share-rw", &err
);
267 error_propagate(errp
, err
);
268 object_unparent(OBJECT(dev
));
272 qdev_prop_set_enum(dev
, "rerror", rerror
);
273 qdev_prop_set_enum(dev
, "werror", werror
);
275 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
277 error_propagate(errp
, err
);
278 object_unparent(OBJECT(dev
));
281 return SCSI_DEVICE(dev
);
284 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
291 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
292 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
296 qemu_opts_loc_restore(dinfo
->opts
);
297 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
298 unit
, false, -1, false,
299 BLOCKDEV_ON_ERROR_AUTO
,
300 BLOCKDEV_ON_ERROR_AUTO
,
306 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
308 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
309 scsi_req_complete(req
, CHECK_CONDITION
);
313 static const struct SCSIReqOps reqops_invalid_field
= {
314 .size
= sizeof(SCSIRequest
),
315 .send_command
= scsi_invalid_field
318 /* SCSIReqOps implementation for invalid commands. */
320 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
322 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
323 scsi_req_complete(req
, CHECK_CONDITION
);
327 static const struct SCSIReqOps reqops_invalid_opcode
= {
328 .size
= sizeof(SCSIRequest
),
329 .send_command
= scsi_invalid_command
332 /* SCSIReqOps implementation for unit attention conditions. */
334 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
336 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
337 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
338 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
339 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
341 scsi_req_complete(req
, CHECK_CONDITION
);
345 static const struct SCSIReqOps reqops_unit_attention
= {
346 .size
= sizeof(SCSIRequest
),
347 .send_command
= scsi_unit_attention
350 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
353 typedef struct SCSITargetReq SCSITargetReq
;
355 struct SCSITargetReq
{
362 static void store_lun(uint8_t *outbuf
, int lun
)
368 outbuf
[1] = (lun
& 255);
369 outbuf
[0] = (lun
>> 8) | 0x40;
372 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
379 if (r
->req
.cmd
.xfer
< 16) {
382 if (r
->req
.cmd
.buf
[2] > 2) {
385 channel
= r
->req
.dev
->channel
;
389 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
390 DeviceState
*qdev
= kid
->child
;
391 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
393 if (dev
->channel
== channel
&& dev
->id
== id
) {
404 scsi_target_alloc_buf(&r
->req
, n
+ 8);
406 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
407 memset(r
->buf
, 0, len
);
408 stl_be_p(&r
->buf
[0], n
);
409 i
= found_lun0
? 8 : 16;
410 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
411 DeviceState
*qdev
= kid
->child
;
412 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
414 if (dev
->channel
== channel
&& dev
->id
== id
) {
415 store_lun(&r
->buf
[i
], dev
->lun
);
424 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
426 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
428 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
430 if (r
->req
.cmd
.buf
[1] & 0x2) {
431 /* Command support data - optional, not implemented */
435 if (r
->req
.cmd
.buf
[1] & 0x1) {
436 /* Vital product data */
437 uint8_t page_code
= r
->req
.cmd
.buf
[2];
438 r
->buf
[r
->len
++] = page_code
; /* this page */
439 r
->buf
[r
->len
++] = 0x00;
442 case 0x00: /* Supported page codes, mandatory */
446 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
447 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
454 assert(r
->len
< r
->buf_len
);
455 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
459 /* Standard INQUIRY data */
460 if (r
->req
.cmd
.buf
[2] != 0) {
465 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
466 memset(r
->buf
, 0, r
->len
);
467 if (r
->req
.lun
!= 0) {
468 r
->buf
[0] = TYPE_NO_LUN
;
470 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
471 r
->buf
[2] = 5; /* Version */
472 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
473 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
474 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
475 memcpy(&r
->buf
[8], "QEMU ", 8);
476 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
477 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
482 static size_t scsi_sense_len(SCSIRequest
*req
)
484 if (req
->dev
->type
== TYPE_SCANNER
)
485 return SCSI_SENSE_LEN_SCANNER
;
487 return SCSI_SENSE_LEN
;
490 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
492 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
493 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
496 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
497 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
498 scsi_req_complete(req
, CHECK_CONDITION
);
503 if (!scsi_target_emulate_report_luns(r
)) {
504 goto illegal_request
;
508 if (!scsi_target_emulate_inquiry(r
)) {
509 goto illegal_request
;
513 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
515 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
517 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
520 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
521 MIN(req
->cmd
.xfer
, r
->buf_len
),
524 if (r
->req
.dev
->sense_is_ua
) {
525 scsi_device_unit_attention_reported(req
->dev
);
526 r
->req
.dev
->sense_len
= 0;
527 r
->req
.dev
->sense_is_ua
= false;
530 case TEST_UNIT_READY
:
533 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
534 scsi_req_complete(req
, CHECK_CONDITION
);
537 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
538 scsi_req_complete(req
, CHECK_CONDITION
);
543 scsi_req_complete(req
, GOOD
);
548 static void scsi_target_read_data(SCSIRequest
*req
)
550 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
556 scsi_req_data(&r
->req
, n
);
558 scsi_req_complete(&r
->req
, GOOD
);
562 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
564 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
569 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
571 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
573 r
->buf
= g_malloc(len
);
579 static void scsi_target_free_buf(SCSIRequest
*req
)
581 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
586 static const struct SCSIReqOps reqops_target_command
= {
587 .size
= sizeof(SCSITargetReq
),
588 .send_command
= scsi_target_send_command
,
589 .read_data
= scsi_target_read_data
,
590 .get_buf
= scsi_target_get_buf
,
591 .free_req
= scsi_target_free_buf
,
595 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
596 uint32_t tag
, uint32_t lun
, void *hba_private
)
599 SCSIBus
*bus
= scsi_bus_from_device(d
);
600 BusState
*qbus
= BUS(bus
);
601 const int memset_off
= offsetof(SCSIRequest
, sense
)
602 + sizeof(req
->sense
);
604 req
= g_malloc(reqops
->size
);
605 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
611 req
->hba_private
= hba_private
;
614 object_ref(OBJECT(d
));
615 object_ref(OBJECT(qbus
->parent
));
616 notifier_list_init(&req
->cancel_notifiers
);
617 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
621 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
622 uint8_t *buf
, void *hba_private
)
624 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
625 const SCSIReqOps
*ops
;
626 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
628 SCSICommand cmd
= { .len
= 0 };
631 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
632 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
633 (buf
[0] != INQUIRY
&&
634 buf
[0] != REPORT_LUNS
&&
635 buf
[0] != GET_CONFIGURATION
&&
636 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
639 * If we already have a pending unit attention condition,
640 * report this one before triggering another one.
642 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
643 ops
= &reqops_unit_attention
;
644 } else if (lun
!= d
->lun
||
645 buf
[0] == REPORT_LUNS
||
646 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
647 ops
= &reqops_target_command
;
652 if (ops
!= NULL
|| !sc
->parse_cdb
) {
653 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
655 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
659 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
660 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
662 assert(cmd
.len
!= 0);
663 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
666 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
670 if (cmd
.xfer
> INT32_MAX
) {
671 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
673 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
675 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
680 req
->resid
= req
->cmd
.xfer
;
684 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
686 case TEST_UNIT_READY
:
687 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
690 trace_scsi_report_luns(d
->id
, lun
, tag
);
693 trace_scsi_request_sense(d
->id
, lun
, tag
);
702 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
704 return req
->ops
->get_buf(req
);
707 static void scsi_clear_unit_attention(SCSIRequest
*req
)
710 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
711 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
716 * If an INQUIRY command enters the enabled command state,
717 * the device server shall [not] clear any unit attention condition;
718 * See also MMC-6, paragraphs 6.5 and 6.6.2.
720 if (req
->cmd
.buf
[0] == INQUIRY
||
721 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
722 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
726 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
727 ua
= &req
->dev
->unit_attention
;
729 ua
= &req
->bus
->unit_attention
;
733 * If a REPORT LUNS command enters the enabled command state, [...]
734 * the device server shall clear any pending unit attention condition
735 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
737 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
738 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
739 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
743 *ua
= SENSE_CODE(NO_SENSE
);
746 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
751 if (!req
->sense_len
) {
755 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
758 * FIXME: clearing unit attention conditions upon autosense should be done
759 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
762 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
763 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
764 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
766 if (req
->dev
->sense_is_ua
) {
767 scsi_device_unit_attention_reported(req
->dev
);
768 req
->dev
->sense_len
= 0;
769 req
->dev
->sense_is_ua
= false;
774 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
776 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
779 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
781 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
782 sense
.key
, sense
.asc
, sense
.ascq
);
783 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
786 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
788 assert(!req
->enqueued
);
790 if (req
->bus
->info
->get_sg_list
) {
791 req
->sg
= req
->bus
->info
->get_sg_list(req
);
795 req
->enqueued
= true;
796 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
799 int32_t scsi_req_enqueue(SCSIRequest
*req
)
804 scsi_req_enqueue_internal(req
);
806 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
811 static void scsi_req_dequeue(SCSIRequest
*req
)
813 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
816 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
817 req
->enqueued
= false;
822 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
824 /* MMC-6, paragraph 6.7. */
827 if ((data_type
& 3) == 0) {
828 /* Each descriptor is as in Table 295 - Nominal performance. */
829 return 16 * num_desc
+ 8;
831 /* Each descriptor is as in Table 296 - Exceptions. */
832 return 6 * num_desc
+ 8;
837 return 8 * num_desc
+ 8;
839 return 2048 * num_desc
+ 8;
841 return 16 * num_desc
+ 8;
847 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
849 int byte_block
= (buf
[2] >> 2) & 0x1;
850 int type
= (buf
[2] >> 4) & 0x1;
855 xfer_unit
= dev
->blocksize
;
866 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
868 int length
= buf
[2] & 0x3;
870 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
874 case 3: /* USB-specific. */
889 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
891 int extend
= buf
[1] & 0x1;
892 int length
= buf
[2] & 0x3;
894 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
898 case 3: /* USB-specific. */
904 xfer
|= (extend
? buf
[3] << 8 : 0);
908 xfer
|= (extend
? buf
[5] << 8 : 0);
915 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
917 cmd
->xfer
= scsi_cdb_xfer(buf
);
919 case TEST_UNIT_READY
:
923 case WRITE_FILEMARKS
:
924 case WRITE_FILEMARKS_16
:
929 case ALLOW_MEDIUM_REMOVAL
:
931 case SYNCHRONIZE_CACHE
:
932 case SYNCHRONIZE_CACHE_16
:
934 case LOCK_UNLOCK_CACHE
:
943 case ALLOW_OVERWRITE
:
949 if ((buf
[1] & 2) == 0) {
951 } else if ((buf
[1] & 4) != 0) {
954 cmd
->xfer
*= dev
->blocksize
;
960 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
962 case READ_CAPACITY_10
:
965 case READ_BLOCK_LIMITS
:
968 case SEND_VOLUME_TAG
:
969 /* GPCMD_SET_STREAMING from multimedia commands. */
970 if (dev
->type
== TYPE_ROM
) {
971 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
973 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
977 /* length 0 means 256 blocks */
978 if (cmd
->xfer
== 0) {
983 case WRITE_VERIFY_10
:
985 case WRITE_VERIFY_12
:
987 case WRITE_VERIFY_16
:
988 cmd
->xfer
*= dev
->blocksize
;
992 /* length 0 means 256 blocks */
993 if (cmd
->xfer
== 0) {
1000 cmd
->xfer
*= dev
->blocksize
;
1003 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1004 * for block devices are restricted to the header right now. */
1005 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1008 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1012 case RECEIVE_DIAGNOSTIC
:
1013 case SEND_DIAGNOSTIC
:
1014 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1019 case SEND_CUE_SHEET
:
1020 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1022 case PERSISTENT_RESERVE_OUT
:
1023 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1026 if (dev
->type
== TYPE_ROM
) {
1027 /* MMC command GET PERFORMANCE. */
1028 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1029 buf
[10], buf
[1] & 0x1f);
1032 case MECHANISM_STATUS
:
1033 case READ_DVD_STRUCTURE
:
1034 case SEND_DVD_STRUCTURE
:
1035 case MAINTENANCE_OUT
:
1036 case MAINTENANCE_IN
:
1037 if (dev
->type
== TYPE_ROM
) {
1038 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1039 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1042 case ATA_PASSTHROUGH_12
:
1043 if (dev
->type
== TYPE_ROM
) {
1044 /* BLANK command of MMC */
1047 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1050 case ATA_PASSTHROUGH_16
:
1051 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1057 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1060 /* stream commands */
1067 case RECOVER_BUFFERED_DATA
:
1069 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1070 if (buf
[1] & 0x01) { /* fixed */
1071 cmd
->xfer
*= dev
->blocksize
;
1075 case READ_REVERSE_16
:
1078 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1079 if (buf
[1] & 0x01) { /* fixed */
1080 cmd
->xfer
*= dev
->blocksize
;
1088 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1091 switch (buf
[1] & 0x1f) /* operation code */ {
1092 case SHORT_FORM_BLOCK_ID
:
1093 case SHORT_FORM_VENDOR_SPECIFIC
:
1100 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1108 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1110 /* generic commands */
1112 return scsi_req_xfer(cmd
, dev
, buf
);
1117 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1120 /* medium changer commands */
1121 case EXCHANGE_MEDIUM
:
1122 case INITIALIZE_ELEMENT_STATUS
:
1123 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1125 case POSITION_TO_ELEMENT
:
1128 case READ_ELEMENT_STATUS
:
1129 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1132 /* generic commands */
1134 return scsi_req_xfer(cmd
, dev
, buf
);
1139 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1142 /* Scanner commands */
1143 case OBJECT_POSITION
:
1153 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1156 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1157 return scsi_req_xfer(cmd
, dev
, buf
);
1163 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1166 cmd
->mode
= SCSI_XFER_NONE
;
1169 switch (cmd
->buf
[0]) {
1172 case WRITE_VERIFY_10
:
1174 case WRITE_VERIFY_12
:
1176 case WRITE_VERIFY_16
:
1183 case CHANGE_DEFINITION
:
1186 case MODE_SELECT_10
:
1187 case SEND_DIAGNOSTIC
:
1190 case REASSIGN_BLOCKS
:
1199 case SEARCH_HIGH_12
:
1200 case SEARCH_EQUAL_12
:
1203 case SEND_VOLUME_TAG
:
1204 case SEND_CUE_SHEET
:
1205 case SEND_DVD_STRUCTURE
:
1206 case PERSISTENT_RESERVE_OUT
:
1207 case MAINTENANCE_OUT
:
1210 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1211 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1213 cmd
->mode
= SCSI_XFER_TO_DEV
;
1215 case ATA_PASSTHROUGH_12
:
1216 case ATA_PASSTHROUGH_16
:
1218 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1219 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1222 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1227 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1233 len
= scsi_cdb_length(buf
);
1239 switch (dev
->type
) {
1241 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1243 case TYPE_MEDIUM_CHANGER
:
1244 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1247 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1250 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1257 memcpy(cmd
->buf
, buf
, cmd
->len
);
1258 scsi_cmd_xfer_mode(cmd
);
1259 cmd
->lba
= scsi_cmd_lba(cmd
);
1263 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1265 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1267 scsi_device_set_ua(dev
, sense
);
1268 if (bus
->info
->change
) {
1269 bus
->info
->change(bus
, dev
, sense
);
1273 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1275 assert(req
->refcount
> 0);
1280 void scsi_req_unref(SCSIRequest
*req
)
1282 assert(req
->refcount
> 0);
1283 if (--req
->refcount
== 0) {
1284 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1285 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1287 if (bus
->info
->free_request
&& req
->hba_private
) {
1288 bus
->info
->free_request(bus
, req
->hba_private
);
1290 if (req
->ops
->free_req
) {
1291 req
->ops
->free_req(req
);
1293 object_unref(OBJECT(req
->dev
));
1294 object_unref(OBJECT(qbus
->parent
));
1299 /* Tell the device that we finished processing this chunk of I/O. It
1300 will start the next chunk or complete the command. */
1301 void scsi_req_continue(SCSIRequest
*req
)
1303 if (req
->io_canceled
) {
1304 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1307 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1308 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1309 req
->ops
->write_data(req
);
1311 req
->ops
->read_data(req
);
1315 /* Called by the devices when data is ready for the HBA. The HBA should
1316 start a DMA operation to read or fill the device's data buffer.
1317 Once it completes, calling scsi_req_continue will restart I/O. */
1318 void scsi_req_data(SCSIRequest
*req
, int len
)
1321 if (req
->io_canceled
) {
1322 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1325 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1326 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1329 req
->bus
->info
->transfer_data(req
, len
);
1333 /* If the device calls scsi_req_data and the HBA specified a
1334 * scatter/gather list, the transfer has to happen in a single
1336 assert(!req
->dma_started
);
1337 req
->dma_started
= true;
1339 buf
= scsi_req_get_buf(req
);
1340 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1341 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1343 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1345 scsi_req_continue(req
);
1348 void scsi_req_print(SCSIRequest
*req
)
1353 fprintf(fp
, "[%s id=%d] %s",
1354 req
->dev
->qdev
.parent_bus
->name
,
1356 scsi_command_name(req
->cmd
.buf
[0]));
1357 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1358 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1360 switch (req
->cmd
.mode
) {
1361 case SCSI_XFER_NONE
:
1362 fprintf(fp
, " - none\n");
1364 case SCSI_XFER_FROM_DEV
:
1365 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1367 case SCSI_XFER_TO_DEV
:
1368 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1371 fprintf(fp
, " - Oops\n");
1376 void scsi_req_complete(SCSIRequest
*req
, int status
)
1378 assert(req
->status
== -1);
1379 req
->status
= status
;
1381 assert(req
->sense_len
<= sizeof(req
->sense
));
1382 if (status
== GOOD
) {
1386 if (req
->sense_len
) {
1387 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1388 req
->dev
->sense_len
= req
->sense_len
;
1389 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1391 req
->dev
->sense_len
= 0;
1392 req
->dev
->sense_is_ua
= false;
1396 * Unit attention state is now stored in the device's sense buffer
1397 * if the HBA didn't do autosense. Clear the pending unit attention
1400 scsi_clear_unit_attention(req
);
1403 scsi_req_dequeue(req
);
1404 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1406 /* Cancelled requests might end up being completed instead of cancelled */
1407 notifier_list_notify(&req
->cancel_notifiers
, req
);
1408 scsi_req_unref(req
);
1411 /* Called by the devices when the request is canceled. */
1412 void scsi_req_cancel_complete(SCSIRequest
*req
)
1414 assert(req
->io_canceled
);
1415 if (req
->bus
->info
->cancel
) {
1416 req
->bus
->info
->cancel(req
);
1418 notifier_list_notify(&req
->cancel_notifiers
, req
);
1419 scsi_req_unref(req
);
1422 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1423 * notifier list, the bus will be notified the requests cancellation is
1426 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1428 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1430 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1432 if (req
->io_canceled
) {
1433 /* A blk_aio_cancel_async is pending; when it finishes,
1434 * scsi_req_cancel_complete will be called and will
1435 * call the notifier we just added. Just wait for that.
1440 /* Dropped in scsi_req_cancel_complete. */
1442 scsi_req_dequeue(req
);
1443 req
->io_canceled
= true;
1445 blk_aio_cancel_async(req
->aiocb
);
1447 scsi_req_cancel_complete(req
);
1451 void scsi_req_cancel(SCSIRequest
*req
)
1453 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1454 if (!req
->enqueued
) {
1457 assert(!req
->io_canceled
);
1458 /* Dropped in scsi_req_cancel_complete. */
1460 scsi_req_dequeue(req
);
1461 req
->io_canceled
= true;
1463 blk_aio_cancel(req
->aiocb
);
1465 scsi_req_cancel_complete(req
);
1469 static int scsi_ua_precedence(SCSISense sense
)
1471 if (sense
.key
!= UNIT_ATTENTION
) {
1474 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1475 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1477 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1478 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1480 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1481 /* These two go with "all others". */
1483 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1484 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1485 * POWER ON OCCURRED = 1
1486 * SCSI BUS RESET OCCURRED = 2
1487 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1488 * I_T NEXUS LOSS OCCURRED = 7
1491 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1492 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1495 return (sense
.asc
<< 8) | sense
.ascq
;
1498 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1501 if (sense
.key
!= UNIT_ATTENTION
) {
1504 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1505 sense
.asc
, sense
.ascq
);
1508 * Override a pre-existing unit attention condition, except for a more
1509 * important reset condition.
1511 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1512 prec2
= scsi_ua_precedence(sense
);
1513 if (prec2
< prec1
) {
1514 sdev
->unit_attention
= sense
;
1518 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1522 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1523 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1524 req
= QTAILQ_FIRST(&sdev
->requests
);
1525 scsi_req_cancel_async(req
, NULL
);
1527 blk_drain(sdev
->conf
.blk
);
1528 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1529 scsi_device_set_ua(sdev
, sense
);
1532 static char *scsibus_get_dev_path(DeviceState
*dev
)
1534 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1535 DeviceState
*hba
= dev
->parent_bus
->parent
;
1539 id
= qdev_get_dev_path(hba
);
1541 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1543 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1549 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1551 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1552 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1553 qdev_fw_name(dev
), d
->id
, d
->lun
);
1556 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1559 SCSIDevice
*target_dev
= NULL
;
1561 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, sibling
) {
1562 DeviceState
*qdev
= kid
->child
;
1563 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1565 if (dev
->channel
== channel
&& dev
->id
== id
) {
1566 if (dev
->lun
== lun
) {
1575 /* SCSI request list. For simplicity, pv points to the whole device */
1577 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1578 const VMStateField
*field
, QJSON
*vmdesc
)
1581 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1584 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1585 assert(!req
->io_canceled
);
1586 assert(req
->status
== -1);
1587 assert(req
->enqueued
);
1589 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1590 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1591 qemu_put_be32s(f
, &req
->tag
);
1592 qemu_put_be32s(f
, &req
->lun
);
1593 if (bus
->info
->save_request
) {
1594 bus
->info
->save_request(f
, req
);
1596 if (req
->ops
->save_request
) {
1597 req
->ops
->save_request(f
, req
);
1600 qemu_put_sbyte(f
, 0);
1605 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1606 const VMStateField
*field
)
1609 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1612 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1613 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1618 qemu_get_buffer(f
, buf
, sizeof(buf
));
1619 qemu_get_be32s(f
, &tag
);
1620 qemu_get_be32s(f
, &lun
);
1621 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1622 req
->retry
= (sbyte
== 1);
1623 if (bus
->info
->load_request
) {
1624 req
->hba_private
= bus
->info
->load_request(f
, req
);
1626 if (req
->ops
->load_request
) {
1627 req
->ops
->load_request(f
, req
);
1630 /* Just restart it later. */
1631 scsi_req_enqueue_internal(req
);
1633 /* At this point, the request will be kept alive by the reference
1634 * added by scsi_req_enqueue_internal, so we can release our reference.
1635 * The HBA of course will add its own reference in the load_request
1636 * callback if it needs to hold on the SCSIRequest.
1638 scsi_req_unref(req
);
1644 static const VMStateInfo vmstate_info_scsi_requests
= {
1645 .name
= "scsi-requests",
1646 .get
= get_scsi_requests
,
1647 .put
= put_scsi_requests
,
1650 static bool scsi_sense_state_needed(void *opaque
)
1652 SCSIDevice
*s
= opaque
;
1654 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1657 static const VMStateDescription vmstate_scsi_sense_state
= {
1658 .name
= "SCSIDevice/sense",
1660 .minimum_version_id
= 1,
1661 .needed
= scsi_sense_state_needed
,
1662 .fields
= (VMStateField
[]) {
1663 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1664 SCSI_SENSE_BUF_SIZE_OLD
,
1665 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1666 VMSTATE_END_OF_LIST()
1670 const VMStateDescription vmstate_scsi_device
= {
1671 .name
= "SCSIDevice",
1673 .minimum_version_id
= 1,
1674 .fields
= (VMStateField
[]) {
1675 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1676 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1677 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1678 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1679 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1680 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1684 .field_exists
= NULL
,
1685 .size
= 0, /* ouch */
1686 .info
= &vmstate_info_scsi_requests
,
1687 .flags
= VMS_SINGLE
,
1690 VMSTATE_END_OF_LIST()
1692 .subsections
= (const VMStateDescription
*[]) {
1693 &vmstate_scsi_sense_state
,
1698 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1700 DeviceClass
*k
= DEVICE_CLASS(klass
);
1701 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1702 k
->bus_type
= TYPE_SCSI_BUS
;
1703 k
->realize
= scsi_qdev_realize
;
1704 k
->unrealize
= scsi_qdev_unrealize
;
1705 k
->props
= scsi_props
;
1708 static void scsi_dev_instance_init(Object
*obj
)
1710 DeviceState
*dev
= DEVICE(obj
);
1711 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1713 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1718 static const TypeInfo scsi_device_type_info
= {
1719 .name
= TYPE_SCSI_DEVICE
,
1720 .parent
= TYPE_DEVICE
,
1721 .instance_size
= sizeof(SCSIDevice
),
1723 .class_size
= sizeof(SCSIDeviceClass
),
1724 .class_init
= scsi_device_class_init
,
1725 .instance_init
= scsi_dev_instance_init
,
1728 static void scsi_register_types(void)
1730 type_register_static(&scsi_bus_info
);
1731 type_register_static(&scsi_device_type_info
);
1734 type_init(scsi_register_types
)