1 #include "qemu/osdep.h"
3 #include "qapi/error.h"
4 #include "qemu/error-report.h"
5 #include "qemu/module.h"
6 #include "qemu/option.h"
7 #include "hw/scsi/scsi.h"
8 #include "scsi/constants.h"
10 #include "sysemu/block-backend.h"
11 #include "sysemu/blockdev.h"
13 #include "sysemu/dma.h"
14 #include "qemu/cutils.h"
16 static char *scsibus_get_dev_path(DeviceState
*dev
);
17 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
18 static void scsi_req_dequeue(SCSIRequest
*req
);
19 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
20 static void scsi_target_free_buf(SCSIRequest
*req
);
22 static Property scsi_props
[] = {
23 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
24 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
25 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
26 DEFINE_PROP_END_OF_LIST(),
29 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
31 BusClass
*k
= BUS_CLASS(klass
);
32 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
34 k
->get_dev_path
= scsibus_get_dev_path
;
35 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
36 hc
->unplug
= qdev_simple_device_unplug_cb
;
39 static const TypeInfo scsi_bus_info
= {
40 .name
= TYPE_SCSI_BUS
,
42 .instance_size
= sizeof(SCSIBus
),
43 .class_init
= scsi_bus_class_init
,
44 .interfaces
= (InterfaceInfo
[]) {
45 { TYPE_HOTPLUG_HANDLER
},
49 static int next_scsi_bus
;
51 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
53 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
59 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
62 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
65 assert(cmd
->len
== 0);
66 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
67 if (bus
->info
->parse_cdb
) {
68 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
73 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
74 uint8_t *buf
, void *hba_private
)
76 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
78 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
84 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
86 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
87 if (sc
->unit_attention_reported
) {
88 sc
->unit_attention_reported(s
);
92 /* Create a scsi bus, and attach devices to it. */
93 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
94 const SCSIBusInfo
*info
, const char *bus_name
)
96 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
97 bus
->busnr
= next_scsi_bus
++;
99 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
102 static void scsi_dma_restart_bh(void *opaque
)
104 SCSIDevice
*s
= opaque
;
105 SCSIRequest
*req
, *next
;
107 qemu_bh_delete(s
->bh
);
110 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
111 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
115 switch (req
->cmd
.mode
) {
116 case SCSI_XFER_FROM_DEV
:
117 case SCSI_XFER_TO_DEV
:
118 scsi_req_continue(req
);
121 scsi_req_dequeue(req
);
122 scsi_req_enqueue(req
);
128 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
131 void scsi_req_retry(SCSIRequest
*req
)
133 /* No need to save a reference, because scsi_dma_restart_bh just
134 * looks at the request list. */
138 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
140 SCSIDevice
*s
= opaque
;
146 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
147 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
148 qemu_bh_schedule(s
->bh
);
152 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
154 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
155 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
157 Error
*local_err
= NULL
;
159 if (dev
->channel
> bus
->info
->max_channel
) {
160 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
163 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
164 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
167 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
168 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
174 if (dev
->lun
== -1) {
178 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
179 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
180 if (d
&& d
->lun
== dev
->lun
) {
181 error_setg(errp
, "no free target");
185 } else if (dev
->lun
== -1) {
188 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
189 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
190 if (d
&& d
->lun
== lun
) {
191 error_setg(errp
, "no free lun");
196 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
198 if (d
->lun
== dev
->lun
&& dev
!= d
) {
199 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
204 QTAILQ_INIT(&dev
->requests
);
205 scsi_device_realize(dev
, &local_err
);
207 error_propagate(errp
, local_err
);
210 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
214 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
216 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
219 qemu_del_vm_change_state_handler(dev
->vmsentry
);
222 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
223 blockdev_mark_auto_del(dev
->conf
.blk
);
226 /* handle legacy '-drive if=scsi,...' cmd line args */
227 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
228 int unit
, bool removable
, int bootindex
,
230 BlockdevOnError rerror
,
231 BlockdevOnError werror
,
232 const char *serial
, Error
**errp
)
239 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
240 dev
= qdev_create(&bus
->qbus
, driver
);
241 name
= g_strdup_printf("legacy[%d]", unit
);
242 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
245 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
246 if (bootindex
>= 0) {
247 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
250 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
251 qdev_prop_set_bit(dev
, "removable", removable
);
253 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
254 qdev_prop_set_string(dev
, "serial", serial
);
256 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
258 error_propagate(errp
, err
);
259 object_unparent(OBJECT(dev
));
262 object_property_set_bool(OBJECT(dev
), share_rw
, "share-rw", &err
);
264 error_propagate(errp
, err
);
265 object_unparent(OBJECT(dev
));
269 qdev_prop_set_enum(dev
, "rerror", rerror
);
270 qdev_prop_set_enum(dev
, "werror", werror
);
272 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
274 error_propagate(errp
, err
);
275 object_unparent(OBJECT(dev
));
278 return SCSI_DEVICE(dev
);
281 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
288 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
289 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
293 qemu_opts_loc_restore(dinfo
->opts
);
294 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
295 unit
, false, -1, false,
296 BLOCKDEV_ON_ERROR_AUTO
,
297 BLOCKDEV_ON_ERROR_AUTO
,
303 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
305 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
306 scsi_req_complete(req
, CHECK_CONDITION
);
310 static const struct SCSIReqOps reqops_invalid_field
= {
311 .size
= sizeof(SCSIRequest
),
312 .send_command
= scsi_invalid_field
315 /* SCSIReqOps implementation for invalid commands. */
317 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
319 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
320 scsi_req_complete(req
, CHECK_CONDITION
);
324 static const struct SCSIReqOps reqops_invalid_opcode
= {
325 .size
= sizeof(SCSIRequest
),
326 .send_command
= scsi_invalid_command
329 /* SCSIReqOps implementation for unit attention conditions. */
331 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
333 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
334 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
335 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
336 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
338 scsi_req_complete(req
, CHECK_CONDITION
);
342 static const struct SCSIReqOps reqops_unit_attention
= {
343 .size
= sizeof(SCSIRequest
),
344 .send_command
= scsi_unit_attention
347 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
350 typedef struct SCSITargetReq SCSITargetReq
;
352 struct SCSITargetReq
{
359 static void store_lun(uint8_t *outbuf
, int lun
)
365 outbuf
[1] = (lun
& 255);
366 outbuf
[0] = (lun
>> 8) | 0x40;
369 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
376 if (r
->req
.cmd
.xfer
< 16) {
379 if (r
->req
.cmd
.buf
[2] > 2) {
382 channel
= r
->req
.dev
->channel
;
386 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
387 DeviceState
*qdev
= kid
->child
;
388 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
390 if (dev
->channel
== channel
&& dev
->id
== id
) {
401 scsi_target_alloc_buf(&r
->req
, n
+ 8);
403 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
404 memset(r
->buf
, 0, len
);
405 stl_be_p(&r
->buf
[0], n
);
406 i
= found_lun0
? 8 : 16;
407 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
408 DeviceState
*qdev
= kid
->child
;
409 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
411 if (dev
->channel
== channel
&& dev
->id
== id
) {
412 store_lun(&r
->buf
[i
], dev
->lun
);
421 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
423 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
425 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
427 if (r
->req
.cmd
.buf
[1] & 0x2) {
428 /* Command support data - optional, not implemented */
432 if (r
->req
.cmd
.buf
[1] & 0x1) {
433 /* Vital product data */
434 uint8_t page_code
= r
->req
.cmd
.buf
[2];
435 r
->buf
[r
->len
++] = page_code
; /* this page */
436 r
->buf
[r
->len
++] = 0x00;
439 case 0x00: /* Supported page codes, mandatory */
443 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
444 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
451 assert(r
->len
< r
->buf_len
);
452 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
456 /* Standard INQUIRY data */
457 if (r
->req
.cmd
.buf
[2] != 0) {
462 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
463 memset(r
->buf
, 0, r
->len
);
464 if (r
->req
.lun
!= 0) {
465 r
->buf
[0] = TYPE_NO_LUN
;
467 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
468 r
->buf
[2] = 5; /* Version */
469 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
470 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
471 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
472 memcpy(&r
->buf
[8], "QEMU ", 8);
473 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
474 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
479 static size_t scsi_sense_len(SCSIRequest
*req
)
481 if (req
->dev
->type
== TYPE_SCANNER
)
482 return SCSI_SENSE_LEN_SCANNER
;
484 return SCSI_SENSE_LEN
;
487 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
489 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
490 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
493 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
494 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
495 scsi_req_complete(req
, CHECK_CONDITION
);
500 if (!scsi_target_emulate_report_luns(r
)) {
501 goto illegal_request
;
505 if (!scsi_target_emulate_inquiry(r
)) {
506 goto illegal_request
;
510 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
512 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
514 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
517 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
518 MIN(req
->cmd
.xfer
, r
->buf_len
),
521 if (r
->req
.dev
->sense_is_ua
) {
522 scsi_device_unit_attention_reported(req
->dev
);
523 r
->req
.dev
->sense_len
= 0;
524 r
->req
.dev
->sense_is_ua
= false;
527 case TEST_UNIT_READY
:
530 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
531 scsi_req_complete(req
, CHECK_CONDITION
);
534 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
535 scsi_req_complete(req
, CHECK_CONDITION
);
540 scsi_req_complete(req
, GOOD
);
545 static void scsi_target_read_data(SCSIRequest
*req
)
547 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
553 scsi_req_data(&r
->req
, n
);
555 scsi_req_complete(&r
->req
, GOOD
);
559 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
561 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
566 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
568 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
570 r
->buf
= g_malloc(len
);
576 static void scsi_target_free_buf(SCSIRequest
*req
)
578 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
583 static const struct SCSIReqOps reqops_target_command
= {
584 .size
= sizeof(SCSITargetReq
),
585 .send_command
= scsi_target_send_command
,
586 .read_data
= scsi_target_read_data
,
587 .get_buf
= scsi_target_get_buf
,
588 .free_req
= scsi_target_free_buf
,
592 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
593 uint32_t tag
, uint32_t lun
, void *hba_private
)
596 SCSIBus
*bus
= scsi_bus_from_device(d
);
597 BusState
*qbus
= BUS(bus
);
598 const int memset_off
= offsetof(SCSIRequest
, sense
)
599 + sizeof(req
->sense
);
601 req
= g_malloc(reqops
->size
);
602 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
608 req
->hba_private
= hba_private
;
611 object_ref(OBJECT(d
));
612 object_ref(OBJECT(qbus
->parent
));
613 notifier_list_init(&req
->cancel_notifiers
);
614 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
618 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
619 uint8_t *buf
, void *hba_private
)
621 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
622 const SCSIReqOps
*ops
;
623 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
625 SCSICommand cmd
= { .len
= 0 };
628 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
629 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
630 (buf
[0] != INQUIRY
&&
631 buf
[0] != REPORT_LUNS
&&
632 buf
[0] != GET_CONFIGURATION
&&
633 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
636 * If we already have a pending unit attention condition,
637 * report this one before triggering another one.
639 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
640 ops
= &reqops_unit_attention
;
641 } else if (lun
!= d
->lun
||
642 buf
[0] == REPORT_LUNS
||
643 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
644 ops
= &reqops_target_command
;
649 if (ops
!= NULL
|| !sc
->parse_cdb
) {
650 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
652 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
656 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
657 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
659 assert(cmd
.len
!= 0);
660 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
663 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
667 if (cmd
.xfer
> INT32_MAX
) {
668 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
670 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
672 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
677 req
->resid
= req
->cmd
.xfer
;
681 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
683 case TEST_UNIT_READY
:
684 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
687 trace_scsi_report_luns(d
->id
, lun
, tag
);
690 trace_scsi_request_sense(d
->id
, lun
, tag
);
699 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
701 return req
->ops
->get_buf(req
);
704 static void scsi_clear_unit_attention(SCSIRequest
*req
)
707 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
708 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
713 * If an INQUIRY command enters the enabled command state,
714 * the device server shall [not] clear any unit attention condition;
715 * See also MMC-6, paragraphs 6.5 and 6.6.2.
717 if (req
->cmd
.buf
[0] == INQUIRY
||
718 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
719 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
723 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
724 ua
= &req
->dev
->unit_attention
;
726 ua
= &req
->bus
->unit_attention
;
730 * If a REPORT LUNS command enters the enabled command state, [...]
731 * the device server shall clear any pending unit attention condition
732 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
734 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
735 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
736 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
740 *ua
= SENSE_CODE(NO_SENSE
);
743 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
748 if (!req
->sense_len
) {
752 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
755 * FIXME: clearing unit attention conditions upon autosense should be done
756 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
759 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
760 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
761 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
763 if (req
->dev
->sense_is_ua
) {
764 scsi_device_unit_attention_reported(req
->dev
);
765 req
->dev
->sense_len
= 0;
766 req
->dev
->sense_is_ua
= false;
771 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
773 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
776 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
778 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
779 sense
.key
, sense
.asc
, sense
.ascq
);
780 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
783 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
785 assert(!req
->enqueued
);
787 if (req
->bus
->info
->get_sg_list
) {
788 req
->sg
= req
->bus
->info
->get_sg_list(req
);
792 req
->enqueued
= true;
793 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
796 int32_t scsi_req_enqueue(SCSIRequest
*req
)
801 scsi_req_enqueue_internal(req
);
803 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
808 static void scsi_req_dequeue(SCSIRequest
*req
)
810 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
813 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
814 req
->enqueued
= false;
819 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
821 /* MMC-6, paragraph 6.7. */
824 if ((data_type
& 3) == 0) {
825 /* Each descriptor is as in Table 295 - Nominal performance. */
826 return 16 * num_desc
+ 8;
828 /* Each descriptor is as in Table 296 - Exceptions. */
829 return 6 * num_desc
+ 8;
834 return 8 * num_desc
+ 8;
836 return 2048 * num_desc
+ 8;
838 return 16 * num_desc
+ 8;
844 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
846 int byte_block
= (buf
[2] >> 2) & 0x1;
847 int type
= (buf
[2] >> 4) & 0x1;
852 xfer_unit
= dev
->blocksize
;
863 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
865 int length
= buf
[2] & 0x3;
867 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
871 case 3: /* USB-specific. */
886 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
888 int extend
= buf
[1] & 0x1;
889 int length
= buf
[2] & 0x3;
891 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
895 case 3: /* USB-specific. */
901 xfer
|= (extend
? buf
[3] << 8 : 0);
905 xfer
|= (extend
? buf
[5] << 8 : 0);
912 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
914 cmd
->xfer
= scsi_cdb_xfer(buf
);
916 case TEST_UNIT_READY
:
920 case WRITE_FILEMARKS
:
921 case WRITE_FILEMARKS_16
:
926 case ALLOW_MEDIUM_REMOVAL
:
928 case SYNCHRONIZE_CACHE
:
929 case SYNCHRONIZE_CACHE_16
:
931 case LOCK_UNLOCK_CACHE
:
940 case ALLOW_OVERWRITE
:
946 if ((buf
[1] & 2) == 0) {
948 } else if ((buf
[1] & 4) != 0) {
951 cmd
->xfer
*= dev
->blocksize
;
957 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
959 case READ_CAPACITY_10
:
962 case READ_BLOCK_LIMITS
:
965 case SEND_VOLUME_TAG
:
966 /* GPCMD_SET_STREAMING from multimedia commands. */
967 if (dev
->type
== TYPE_ROM
) {
968 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
970 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
974 /* length 0 means 256 blocks */
975 if (cmd
->xfer
== 0) {
980 case WRITE_VERIFY_10
:
982 case WRITE_VERIFY_12
:
984 case WRITE_VERIFY_16
:
985 cmd
->xfer
*= dev
->blocksize
;
989 /* length 0 means 256 blocks */
990 if (cmd
->xfer
== 0) {
997 cmd
->xfer
*= dev
->blocksize
;
1000 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1001 * for block devices are restricted to the header right now. */
1002 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1005 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1009 case RECEIVE_DIAGNOSTIC
:
1010 case SEND_DIAGNOSTIC
:
1011 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1016 case SEND_CUE_SHEET
:
1017 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1019 case PERSISTENT_RESERVE_OUT
:
1020 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1023 if (dev
->type
== TYPE_ROM
) {
1024 /* MMC command GET PERFORMANCE. */
1025 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1026 buf
[10], buf
[1] & 0x1f);
1029 case MECHANISM_STATUS
:
1030 case READ_DVD_STRUCTURE
:
1031 case SEND_DVD_STRUCTURE
:
1032 case MAINTENANCE_OUT
:
1033 case MAINTENANCE_IN
:
1034 if (dev
->type
== TYPE_ROM
) {
1035 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1036 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1039 case ATA_PASSTHROUGH_12
:
1040 if (dev
->type
== TYPE_ROM
) {
1041 /* BLANK command of MMC */
1044 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1047 case ATA_PASSTHROUGH_16
:
1048 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1054 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1057 /* stream commands */
1064 case RECOVER_BUFFERED_DATA
:
1066 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1067 if (buf
[1] & 0x01) { /* fixed */
1068 cmd
->xfer
*= dev
->blocksize
;
1072 case READ_REVERSE_16
:
1075 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1076 if (buf
[1] & 0x01) { /* fixed */
1077 cmd
->xfer
*= dev
->blocksize
;
1085 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1088 switch (buf
[1] & 0x1f) /* operation code */ {
1089 case SHORT_FORM_BLOCK_ID
:
1090 case SHORT_FORM_VENDOR_SPECIFIC
:
1097 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1105 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1107 /* generic commands */
1109 return scsi_req_xfer(cmd
, dev
, buf
);
1114 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1117 /* medium changer commands */
1118 case EXCHANGE_MEDIUM
:
1119 case INITIALIZE_ELEMENT_STATUS
:
1120 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1122 case POSITION_TO_ELEMENT
:
1125 case READ_ELEMENT_STATUS
:
1126 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1129 /* generic commands */
1131 return scsi_req_xfer(cmd
, dev
, buf
);
1136 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1139 /* Scanner commands */
1140 case OBJECT_POSITION
:
1150 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1153 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1154 return scsi_req_xfer(cmd
, dev
, buf
);
1160 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1163 cmd
->mode
= SCSI_XFER_NONE
;
1166 switch (cmd
->buf
[0]) {
1169 case WRITE_VERIFY_10
:
1171 case WRITE_VERIFY_12
:
1173 case WRITE_VERIFY_16
:
1180 case CHANGE_DEFINITION
:
1183 case MODE_SELECT_10
:
1184 case SEND_DIAGNOSTIC
:
1187 case REASSIGN_BLOCKS
:
1196 case SEARCH_HIGH_12
:
1197 case SEARCH_EQUAL_12
:
1200 case SEND_VOLUME_TAG
:
1201 case SEND_CUE_SHEET
:
1202 case SEND_DVD_STRUCTURE
:
1203 case PERSISTENT_RESERVE_OUT
:
1204 case MAINTENANCE_OUT
:
1207 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1208 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1210 cmd
->mode
= SCSI_XFER_TO_DEV
;
1212 case ATA_PASSTHROUGH_12
:
1213 case ATA_PASSTHROUGH_16
:
1215 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1216 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1219 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1224 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1230 len
= scsi_cdb_length(buf
);
1236 switch (dev
->type
) {
1238 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1240 case TYPE_MEDIUM_CHANGER
:
1241 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1244 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1247 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1254 memcpy(cmd
->buf
, buf
, cmd
->len
);
1255 scsi_cmd_xfer_mode(cmd
);
1256 cmd
->lba
= scsi_cmd_lba(cmd
);
1260 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1262 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1264 scsi_device_set_ua(dev
, sense
);
1265 if (bus
->info
->change
) {
1266 bus
->info
->change(bus
, dev
, sense
);
1270 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1272 assert(req
->refcount
> 0);
1277 void scsi_req_unref(SCSIRequest
*req
)
1279 assert(req
->refcount
> 0);
1280 if (--req
->refcount
== 0) {
1281 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1282 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1284 if (bus
->info
->free_request
&& req
->hba_private
) {
1285 bus
->info
->free_request(bus
, req
->hba_private
);
1287 if (req
->ops
->free_req
) {
1288 req
->ops
->free_req(req
);
1290 object_unref(OBJECT(req
->dev
));
1291 object_unref(OBJECT(qbus
->parent
));
1296 /* Tell the device that we finished processing this chunk of I/O. It
1297 will start the next chunk or complete the command. */
1298 void scsi_req_continue(SCSIRequest
*req
)
1300 if (req
->io_canceled
) {
1301 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1304 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1305 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1306 req
->ops
->write_data(req
);
1308 req
->ops
->read_data(req
);
1312 /* Called by the devices when data is ready for the HBA. The HBA should
1313 start a DMA operation to read or fill the device's data buffer.
1314 Once it completes, calling scsi_req_continue will restart I/O. */
1315 void scsi_req_data(SCSIRequest
*req
, int len
)
1318 if (req
->io_canceled
) {
1319 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1322 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1323 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1326 req
->bus
->info
->transfer_data(req
, len
);
1330 /* If the device calls scsi_req_data and the HBA specified a
1331 * scatter/gather list, the transfer has to happen in a single
1333 assert(!req
->dma_started
);
1334 req
->dma_started
= true;
1336 buf
= scsi_req_get_buf(req
);
1337 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1338 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1340 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1342 scsi_req_continue(req
);
1345 void scsi_req_print(SCSIRequest
*req
)
1350 fprintf(fp
, "[%s id=%d] %s",
1351 req
->dev
->qdev
.parent_bus
->name
,
1353 scsi_command_name(req
->cmd
.buf
[0]));
1354 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1355 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1357 switch (req
->cmd
.mode
) {
1358 case SCSI_XFER_NONE
:
1359 fprintf(fp
, " - none\n");
1361 case SCSI_XFER_FROM_DEV
:
1362 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1364 case SCSI_XFER_TO_DEV
:
1365 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1368 fprintf(fp
, " - Oops\n");
1373 void scsi_req_complete(SCSIRequest
*req
, int status
)
1375 assert(req
->status
== -1);
1376 req
->status
= status
;
1378 assert(req
->sense_len
<= sizeof(req
->sense
));
1379 if (status
== GOOD
) {
1383 if (req
->sense_len
) {
1384 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1385 req
->dev
->sense_len
= req
->sense_len
;
1386 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1388 req
->dev
->sense_len
= 0;
1389 req
->dev
->sense_is_ua
= false;
1393 * Unit attention state is now stored in the device's sense buffer
1394 * if the HBA didn't do autosense. Clear the pending unit attention
1397 scsi_clear_unit_attention(req
);
1400 scsi_req_dequeue(req
);
1401 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1403 /* Cancelled requests might end up being completed instead of cancelled */
1404 notifier_list_notify(&req
->cancel_notifiers
, req
);
1405 scsi_req_unref(req
);
1408 /* Called by the devices when the request is canceled. */
1409 void scsi_req_cancel_complete(SCSIRequest
*req
)
1411 assert(req
->io_canceled
);
1412 if (req
->bus
->info
->cancel
) {
1413 req
->bus
->info
->cancel(req
);
1415 notifier_list_notify(&req
->cancel_notifiers
, req
);
1416 scsi_req_unref(req
);
1419 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1420 * notifier list, the bus will be notified the requests cancellation is
1423 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1425 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1427 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1429 if (req
->io_canceled
) {
1430 /* A blk_aio_cancel_async is pending; when it finishes,
1431 * scsi_req_cancel_complete will be called and will
1432 * call the notifier we just added. Just wait for that.
1437 /* Dropped in scsi_req_cancel_complete. */
1439 scsi_req_dequeue(req
);
1440 req
->io_canceled
= true;
1442 blk_aio_cancel_async(req
->aiocb
);
1444 scsi_req_cancel_complete(req
);
1448 void scsi_req_cancel(SCSIRequest
*req
)
1450 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1451 if (!req
->enqueued
) {
1454 assert(!req
->io_canceled
);
1455 /* Dropped in scsi_req_cancel_complete. */
1457 scsi_req_dequeue(req
);
1458 req
->io_canceled
= true;
1460 blk_aio_cancel(req
->aiocb
);
1462 scsi_req_cancel_complete(req
);
1466 static int scsi_ua_precedence(SCSISense sense
)
1468 if (sense
.key
!= UNIT_ATTENTION
) {
1471 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1472 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1474 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1475 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1477 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1478 /* These two go with "all others". */
1480 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1481 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1482 * POWER ON OCCURRED = 1
1483 * SCSI BUS RESET OCCURRED = 2
1484 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1485 * I_T NEXUS LOSS OCCURRED = 7
1488 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1489 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1492 return (sense
.asc
<< 8) | sense
.ascq
;
1495 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1498 if (sense
.key
!= UNIT_ATTENTION
) {
1501 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1502 sense
.asc
, sense
.ascq
);
1505 * Override a pre-existing unit attention condition, except for a more
1506 * important reset condition.
1508 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1509 prec2
= scsi_ua_precedence(sense
);
1510 if (prec2
< prec1
) {
1511 sdev
->unit_attention
= sense
;
1515 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1519 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1520 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1521 req
= QTAILQ_FIRST(&sdev
->requests
);
1522 scsi_req_cancel_async(req
, NULL
);
1524 blk_drain(sdev
->conf
.blk
);
1525 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1526 scsi_device_set_ua(sdev
, sense
);
1529 static char *scsibus_get_dev_path(DeviceState
*dev
)
1531 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1532 DeviceState
*hba
= dev
->parent_bus
->parent
;
1536 id
= qdev_get_dev_path(hba
);
1538 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1540 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1546 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1548 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1549 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1550 qdev_fw_name(dev
), d
->id
, d
->lun
);
1553 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1556 SCSIDevice
*target_dev
= NULL
;
1558 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, sibling
) {
1559 DeviceState
*qdev
= kid
->child
;
1560 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1562 if (dev
->channel
== channel
&& dev
->id
== id
) {
1563 if (dev
->lun
== lun
) {
1572 /* SCSI request list. For simplicity, pv points to the whole device */
1574 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1575 const VMStateField
*field
, QJSON
*vmdesc
)
1578 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1581 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1582 assert(!req
->io_canceled
);
1583 assert(req
->status
== -1);
1584 assert(req
->enqueued
);
1586 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1587 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1588 qemu_put_be32s(f
, &req
->tag
);
1589 qemu_put_be32s(f
, &req
->lun
);
1590 if (bus
->info
->save_request
) {
1591 bus
->info
->save_request(f
, req
);
1593 if (req
->ops
->save_request
) {
1594 req
->ops
->save_request(f
, req
);
1597 qemu_put_sbyte(f
, 0);
1602 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1603 const VMStateField
*field
)
1606 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1609 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1610 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1615 qemu_get_buffer(f
, buf
, sizeof(buf
));
1616 qemu_get_be32s(f
, &tag
);
1617 qemu_get_be32s(f
, &lun
);
1618 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1619 req
->retry
= (sbyte
== 1);
1620 if (bus
->info
->load_request
) {
1621 req
->hba_private
= bus
->info
->load_request(f
, req
);
1623 if (req
->ops
->load_request
) {
1624 req
->ops
->load_request(f
, req
);
1627 /* Just restart it later. */
1628 scsi_req_enqueue_internal(req
);
1630 /* At this point, the request will be kept alive by the reference
1631 * added by scsi_req_enqueue_internal, so we can release our reference.
1632 * The HBA of course will add its own reference in the load_request
1633 * callback if it needs to hold on the SCSIRequest.
1635 scsi_req_unref(req
);
1641 static const VMStateInfo vmstate_info_scsi_requests
= {
1642 .name
= "scsi-requests",
1643 .get
= get_scsi_requests
,
1644 .put
= put_scsi_requests
,
1647 static bool scsi_sense_state_needed(void *opaque
)
1649 SCSIDevice
*s
= opaque
;
1651 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1654 static const VMStateDescription vmstate_scsi_sense_state
= {
1655 .name
= "SCSIDevice/sense",
1657 .minimum_version_id
= 1,
1658 .needed
= scsi_sense_state_needed
,
1659 .fields
= (VMStateField
[]) {
1660 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1661 SCSI_SENSE_BUF_SIZE_OLD
,
1662 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1663 VMSTATE_END_OF_LIST()
1667 const VMStateDescription vmstate_scsi_device
= {
1668 .name
= "SCSIDevice",
1670 .minimum_version_id
= 1,
1671 .fields
= (VMStateField
[]) {
1672 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1673 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1674 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1675 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1676 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1677 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1681 .field_exists
= NULL
,
1682 .size
= 0, /* ouch */
1683 .info
= &vmstate_info_scsi_requests
,
1684 .flags
= VMS_SINGLE
,
1687 VMSTATE_END_OF_LIST()
1689 .subsections
= (const VMStateDescription
*[]) {
1690 &vmstate_scsi_sense_state
,
1695 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1697 DeviceClass
*k
= DEVICE_CLASS(klass
);
1698 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1699 k
->bus_type
= TYPE_SCSI_BUS
;
1700 k
->realize
= scsi_qdev_realize
;
1701 k
->unrealize
= scsi_qdev_unrealize
;
1702 k
->props
= scsi_props
;
1705 static void scsi_dev_instance_init(Object
*obj
)
1707 DeviceState
*dev
= DEVICE(obj
);
1708 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1710 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1715 static const TypeInfo scsi_device_type_info
= {
1716 .name
= TYPE_SCSI_DEVICE
,
1717 .parent
= TYPE_DEVICE
,
1718 .instance_size
= sizeof(SCSIDevice
),
1720 .class_size
= sizeof(SCSIDeviceClass
),
1721 .class_init
= scsi_device_class_init
,
1722 .instance_init
= scsi_dev_instance_init
,
1725 static void scsi_register_types(void)
1727 type_register_static(&scsi_bus_info
);
1728 type_register_static(&scsi_device_type_info
);
1731 type_init(scsi_register_types
)