2 #include "qemu-error.h"
10 static char *scsibus_get_dev_path(DeviceState
*dev
);
11 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
12 static int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
);
13 static void scsi_req_dequeue(SCSIRequest
*req
);
15 static Property scsi_props
[] = {
16 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
17 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
18 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
19 DEFINE_PROP_END_OF_LIST(),
22 static struct BusInfo scsi_bus_info
= {
24 .size
= sizeof(SCSIBus
),
25 .get_dev_path
= scsibus_get_dev_path
,
26 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
29 static int next_scsi_bus
;
31 static int scsi_device_init(SCSIDevice
*s
)
33 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
40 static void scsi_device_destroy(SCSIDevice
*s
)
42 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
48 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
49 uint8_t *buf
, void *hba_private
)
51 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
53 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
59 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
61 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
62 if (sc
->unit_attention_reported
) {
63 sc
->unit_attention_reported(s
);
67 /* Create a scsi bus, and attach devices to it. */
68 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
70 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
71 bus
->busnr
= next_scsi_bus
++;
73 bus
->qbus
.allow_hotplug
= 1;
76 static void scsi_dma_restart_bh(void *opaque
)
78 SCSIDevice
*s
= opaque
;
79 SCSIRequest
*req
, *next
;
81 qemu_bh_delete(s
->bh
);
84 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
88 switch (req
->cmd
.mode
) {
89 case SCSI_XFER_FROM_DEV
:
90 case SCSI_XFER_TO_DEV
:
91 scsi_req_continue(req
);
95 scsi_req_dequeue(req
);
96 scsi_req_enqueue(req
);
104 void scsi_req_retry(SCSIRequest
*req
)
106 /* No need to save a reference, because scsi_dma_restart_bh just
107 * looks at the request list. */
111 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
113 SCSIDevice
*s
= opaque
;
119 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
120 qemu_bh_schedule(s
->bh
);
124 static int scsi_qdev_init(DeviceState
*qdev
)
126 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
127 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
131 if (dev
->channel
> bus
->info
->max_channel
) {
132 error_report("bad scsi channel id: %d", dev
->channel
);
135 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
136 error_report("bad scsi device id: %d", dev
->id
);
139 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
140 error_report("bad scsi device lun: %d", dev
->lun
);
146 if (dev
->lun
== -1) {
150 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
151 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
152 if (d
&& d
->lun
== dev
->lun
) {
153 error_report("no free target");
157 } else if (dev
->lun
== -1) {
160 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
161 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
162 if (d
&& d
->lun
== lun
) {
163 error_report("no free lun");
168 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
170 if (d
->lun
== dev
->lun
&& dev
!= d
) {
175 QTAILQ_INIT(&dev
->requests
);
176 rc
= scsi_device_init(dev
);
178 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
186 static int scsi_qdev_exit(DeviceState
*qdev
)
188 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
191 qemu_del_vm_change_state_handler(dev
->vmsentry
);
193 scsi_device_destroy(dev
);
197 /* handle legacy '-drive if=scsi,...' cmd line args */
198 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
199 int unit
, bool removable
, int bootindex
)
204 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
205 dev
= qdev_create(&bus
->qbus
, driver
);
206 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
207 if (bootindex
>= 0) {
208 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
210 if (qdev_prop_exists(dev
, "removable")) {
211 qdev_prop_set_bit(dev
, "removable", removable
);
213 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
217 if (qdev_init(dev
) < 0)
219 return SCSI_DEVICE(dev
);
222 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
229 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
230 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
234 qemu_opts_loc_restore(dinfo
->opts
);
235 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
244 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
246 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
247 scsi_req_complete(req
, CHECK_CONDITION
);
251 static const struct SCSIReqOps reqops_invalid_field
= {
252 .size
= sizeof(SCSIRequest
),
253 .send_command
= scsi_invalid_field
256 /* SCSIReqOps implementation for invalid commands. */
258 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
260 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
261 scsi_req_complete(req
, CHECK_CONDITION
);
265 static const struct SCSIReqOps reqops_invalid_opcode
= {
266 .size
= sizeof(SCSIRequest
),
267 .send_command
= scsi_invalid_command
270 /* SCSIReqOps implementation for unit attention conditions. */
272 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
274 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
275 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
276 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
277 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
279 scsi_req_complete(req
, CHECK_CONDITION
);
283 static const struct SCSIReqOps reqops_unit_attention
= {
284 .size
= sizeof(SCSIRequest
),
285 .send_command
= scsi_unit_attention
288 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
291 typedef struct SCSITargetReq SCSITargetReq
;
293 struct SCSITargetReq
{
299 static void store_lun(uint8_t *outbuf
, int lun
)
305 outbuf
[1] = (lun
& 255);
306 outbuf
[0] = (lun
>> 8) | 0x40;
309 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
316 if (r
->req
.cmd
.xfer
< 16) {
319 if (r
->req
.cmd
.buf
[2] > 2) {
322 channel
= r
->req
.dev
->channel
;
326 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
327 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
329 if (dev
->channel
== channel
&& dev
->id
== id
) {
339 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
340 if (len
> sizeof(r
->buf
)) {
341 /* TODO: > 256 LUNs? */
345 memset(r
->buf
, 0, len
);
346 stl_be_p(&r
->buf
, n
);
347 i
= found_lun0
? 8 : 16;
348 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
349 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
351 if (dev
->channel
== channel
&& dev
->id
== id
) {
352 store_lun(&r
->buf
[i
], dev
->lun
);
361 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
363 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
364 if (r
->req
.cmd
.buf
[1] & 0x2) {
365 /* Command support data - optional, not implemented */
369 if (r
->req
.cmd
.buf
[1] & 0x1) {
370 /* Vital product data */
371 uint8_t page_code
= r
->req
.cmd
.buf
[2];
372 r
->buf
[r
->len
++] = page_code
; /* this page */
373 r
->buf
[r
->len
++] = 0x00;
376 case 0x00: /* Supported page codes, mandatory */
380 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
381 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
388 assert(r
->len
< sizeof(r
->buf
));
389 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
393 /* Standard INQUIRY data */
394 if (r
->req
.cmd
.buf
[2] != 0) {
399 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
400 memset(r
->buf
, 0, r
->len
);
401 if (r
->req
.lun
!= 0) {
402 r
->buf
[0] = TYPE_NO_LUN
;
404 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
405 r
->buf
[2] = 5; /* Version */
406 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
407 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
408 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
409 memcpy(&r
->buf
[8], "QEMU ", 8);
410 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
411 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
416 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
418 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
422 if (!scsi_target_emulate_report_luns(r
)) {
423 goto illegal_request
;
427 if (!scsi_target_emulate_inquiry(r
)) {
428 goto illegal_request
;
432 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
433 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
434 (req
->cmd
.buf
[1] & 1) == 0);
435 if (r
->req
.dev
->sense_is_ua
) {
436 scsi_device_unit_attention_reported(req
->dev
);
437 r
->req
.dev
->sense_len
= 0;
438 r
->req
.dev
->sense_is_ua
= false;
442 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
443 scsi_req_complete(req
, CHECK_CONDITION
);
446 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
447 scsi_req_complete(req
, CHECK_CONDITION
);
452 scsi_req_complete(req
, GOOD
);
457 static void scsi_target_read_data(SCSIRequest
*req
)
459 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
465 scsi_req_data(&r
->req
, n
);
467 scsi_req_complete(&r
->req
, GOOD
);
471 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
473 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
478 static const struct SCSIReqOps reqops_target_command
= {
479 .size
= sizeof(SCSITargetReq
),
480 .send_command
= scsi_target_send_command
,
481 .read_data
= scsi_target_read_data
,
482 .get_buf
= scsi_target_get_buf
,
486 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
487 uint32_t tag
, uint32_t lun
, void *hba_private
)
491 req
= g_malloc0(reqops
->size
);
493 req
->bus
= scsi_bus_from_device(d
);
497 req
->hba_private
= hba_private
;
501 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
505 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
506 uint8_t *buf
, void *hba_private
)
508 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
512 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
513 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
514 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
516 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
519 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
523 if (cmd
.xfer
> INT32_MAX
) {
524 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
525 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
526 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
527 (buf
[0] != INQUIRY
&&
528 buf
[0] != REPORT_LUNS
&&
529 buf
[0] != GET_CONFIGURATION
&&
530 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
533 * If we already have a pending unit attention condition,
534 * report this one before triggering another one.
536 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
537 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
539 } else if (lun
!= d
->lun
||
540 buf
[0] == REPORT_LUNS
||
541 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
542 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
545 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
550 req
->resid
= req
->cmd
.xfer
;
554 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
556 case TEST_UNIT_READY
:
557 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
560 trace_scsi_report_luns(d
->id
, lun
, tag
);
563 trace_scsi_request_sense(d
->id
, lun
, tag
);
572 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
574 return req
->ops
->get_buf(req
);
577 static void scsi_clear_unit_attention(SCSIRequest
*req
)
580 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
581 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
586 * If an INQUIRY command enters the enabled command state,
587 * the device server shall [not] clear any unit attention condition;
588 * See also MMC-6, paragraphs 6.5 and 6.6.2.
590 if (req
->cmd
.buf
[0] == INQUIRY
||
591 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
592 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
596 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
597 ua
= &req
->dev
->unit_attention
;
599 ua
= &req
->bus
->unit_attention
;
603 * If a REPORT LUNS command enters the enabled command state, [...]
604 * the device server shall clear any pending unit attention condition
605 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
607 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
608 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
609 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
613 *ua
= SENSE_CODE(NO_SENSE
);
616 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
621 if (!req
->sense_len
) {
625 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
628 * FIXME: clearing unit attention conditions upon autosense should be done
629 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
632 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
633 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
634 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
636 if (req
->dev
->sense_is_ua
) {
637 scsi_device_unit_attention_reported(req
->dev
);
638 req
->dev
->sense_len
= 0;
639 req
->dev
->sense_is_ua
= false;
644 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
646 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
649 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
651 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
652 sense
.key
, sense
.asc
, sense
.ascq
);
653 memset(req
->sense
, 0, 18);
654 req
->sense
[0] = 0x70;
655 req
->sense
[2] = sense
.key
;
657 req
->sense
[12] = sense
.asc
;
658 req
->sense
[13] = sense
.ascq
;
662 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
664 assert(!req
->enqueued
);
666 if (req
->bus
->info
->get_sg_list
) {
667 req
->sg
= req
->bus
->info
->get_sg_list(req
);
671 req
->enqueued
= true;
672 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
675 int32_t scsi_req_enqueue(SCSIRequest
*req
)
680 scsi_req_enqueue_internal(req
);
682 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
687 static void scsi_req_dequeue(SCSIRequest
*req
)
689 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
692 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
693 req
->enqueued
= false;
698 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
700 /* MMC-6, paragraph 6.7. */
703 if ((data_type
& 3) == 0) {
704 /* Each descriptor is as in Table 295 - Nominal performance. */
705 return 16 * num_desc
+ 8;
707 /* Each descriptor is as in Table 296 - Exceptions. */
708 return 6 * num_desc
+ 8;
713 return 8 * num_desc
+ 8;
715 return 2048 * num_desc
+ 8;
717 return 16 * num_desc
+ 8;
723 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
725 switch (buf
[0] >> 5) {
732 cmd
->xfer
= lduw_be_p(&buf
[7]);
736 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
740 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
748 case TEST_UNIT_READY
:
752 case WRITE_FILEMARKS
:
753 case WRITE_FILEMARKS_16
:
758 case ALLOW_MEDIUM_REMOVAL
:
761 case SYNCHRONIZE_CACHE
:
762 case SYNCHRONIZE_CACHE_16
:
764 case LOCK_UNLOCK_CACHE
:
775 case ALLOW_OVERWRITE
:
782 cmd
->xfer
= dev
->blocksize
;
784 case READ_CAPACITY_10
:
787 case READ_BLOCK_LIMITS
:
790 case SEND_VOLUME_TAG
:
791 /* GPCMD_SET_STREAMING from multimedia commands. */
792 if (dev
->type
== TYPE_ROM
) {
793 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
795 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
799 /* length 0 means 256 blocks */
800 if (cmd
->xfer
== 0) {
804 case WRITE_VERIFY_10
:
806 case WRITE_VERIFY_12
:
808 case WRITE_VERIFY_16
:
809 cmd
->xfer
*= dev
->blocksize
;
813 /* length 0 means 256 blocks */
814 if (cmd
->xfer
== 0) {
818 case RECOVER_BUFFERED_DATA
:
821 cmd
->xfer
*= dev
->blocksize
;
824 /* MMC mandates the parameter list to be 12-bytes long. Parameters
825 * for block devices are restricted to the header right now. */
826 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
829 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
833 case RECEIVE_DIAGNOSTIC
:
834 case SEND_DIAGNOSTIC
:
835 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
841 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
843 case PERSISTENT_RESERVE_OUT
:
844 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
847 if (dev
->type
== TYPE_ROM
) {
848 /* MMC command GET PERFORMANCE. */
849 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
850 buf
[10], buf
[1] & 0x1f);
853 case MECHANISM_STATUS
:
854 case READ_DVD_STRUCTURE
:
855 case SEND_DVD_STRUCTURE
:
856 case MAINTENANCE_OUT
:
858 if (dev
->type
== TYPE_ROM
) {
859 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
860 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
867 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
870 /* stream commands */
877 case RECOVER_BUFFERED_DATA
:
880 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
881 if (buf
[1] & 0x01) { /* fixed */
882 cmd
->xfer
*= dev
->blocksize
;
886 case READ_REVERSE_16
:
890 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
891 if (buf
[1] & 0x01) { /* fixed */
892 cmd
->xfer
*= dev
->blocksize
;
901 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
904 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
907 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
909 /* generic commands */
911 return scsi_req_length(cmd
, dev
, buf
);
916 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
919 cmd
->mode
= SCSI_XFER_NONE
;
922 switch (cmd
->buf
[0]) {
925 case WRITE_VERIFY_10
:
927 case WRITE_VERIFY_12
:
929 case WRITE_VERIFY_16
:
933 case CHANGE_DEFINITION
:
937 case SEND_DIAGNOSTIC
:
940 case REASSIGN_BLOCKS
:
950 case SEARCH_EQUAL_12
:
953 case SEND_VOLUME_TAG
:
955 case SEND_DVD_STRUCTURE
:
956 case PERSISTENT_RESERVE_OUT
:
957 case MAINTENANCE_OUT
:
958 case ATA_PASSTHROUGH
:
959 cmd
->mode
= SCSI_XFER_TO_DEV
;
962 cmd
->mode
= SCSI_XFER_FROM_DEV
;
967 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
969 uint8_t *buf
= cmd
->buf
;
972 switch (buf
[0] >> 5) {
974 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
979 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
982 lba
= ldq_be_p(&buf
[2]);
991 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
995 if (dev
->type
== TYPE_TAPE
) {
996 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
998 rc
= scsi_req_length(cmd
, dev
, buf
);
1003 memcpy(cmd
->buf
, buf
, cmd
->len
);
1004 scsi_cmd_xfer_mode(cmd
);
1005 cmd
->lba
= scsi_cmd_lba(cmd
);
1010 * Predefined sense codes
1013 /* No sense data available */
1014 const struct SCSISense sense_code_NO_SENSE
= {
1015 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1018 /* LUN not ready, Manual intervention required */
1019 const struct SCSISense sense_code_LUN_NOT_READY
= {
1020 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1023 /* LUN not ready, Medium not present */
1024 const struct SCSISense sense_code_NO_MEDIUM
= {
1025 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1028 /* LUN not ready, medium removal prevented */
1029 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1030 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
1033 /* Hardware error, internal target failure */
1034 const struct SCSISense sense_code_TARGET_FAILURE
= {
1035 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1038 /* Illegal request, invalid command operation code */
1039 const struct SCSISense sense_code_INVALID_OPCODE
= {
1040 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1043 /* Illegal request, LBA out of range */
1044 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1045 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1048 /* Illegal request, Invalid field in CDB */
1049 const struct SCSISense sense_code_INVALID_FIELD
= {
1050 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1053 /* Illegal request, LUN not supported */
1054 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1055 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1058 /* Illegal request, Saving parameters not supported */
1059 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1060 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1063 /* Illegal request, Incompatible medium installed */
1064 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1065 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1068 /* Illegal request, medium removal prevented */
1069 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1070 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
1073 /* Command aborted, I/O process terminated */
1074 const struct SCSISense sense_code_IO_ERROR
= {
1075 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1078 /* Command aborted, I_T Nexus loss occurred */
1079 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1080 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1083 /* Command aborted, Logical Unit failure */
1084 const struct SCSISense sense_code_LUN_FAILURE
= {
1085 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1088 /* Unit attention, Power on, reset or bus device reset occurred */
1089 const struct SCSISense sense_code_RESET
= {
1090 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1093 /* Unit attention, No medium */
1094 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1095 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1098 /* Unit attention, Medium may have changed */
1099 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1100 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1103 /* Unit attention, Reported LUNs data has changed */
1104 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1105 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1108 /* Unit attention, Device internal reset */
1109 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1110 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1116 * Convert between fixed and descriptor sense buffers
1118 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1119 uint8_t *buf
, int len
, bool fixed
)
1123 if (!fixed
&& len
< 8) {
1128 sense
.key
= NO_SENSE
;
1132 fixed_in
= (in_buf
[0] & 2) == 0;
1134 if (fixed
== fixed_in
) {
1135 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1136 return MIN(len
, in_len
);
1140 sense
.key
= in_buf
[2];
1141 sense
.asc
= in_buf
[12];
1142 sense
.ascq
= in_buf
[13];
1144 sense
.key
= in_buf
[1];
1145 sense
.asc
= in_buf
[2];
1146 sense
.ascq
= in_buf
[3];
1150 memset(buf
, 0, len
);
1152 /* Return fixed format sense buffer */
1156 buf
[12] = sense
.asc
;
1157 buf
[13] = sense
.ascq
;
1158 return MIN(len
, 18);
1160 /* Return descriptor format sense buffer */
1164 buf
[3] = sense
.ascq
;
1169 static const char *scsi_command_name(uint8_t cmd
)
1171 static const char *names
[] = {
1172 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1173 [ REWIND
] = "REWIND",
1174 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1175 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1176 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1177 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1178 [ READ_6
] = "READ_6",
1179 [ WRITE_6
] = "WRITE_6",
1180 [ SET_CAPACITY
] = "SET_CAPACITY",
1181 [ READ_REVERSE
] = "READ_REVERSE",
1182 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1183 [ SPACE
] = "SPACE",
1184 [ INQUIRY
] = "INQUIRY",
1185 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1186 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1187 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1188 [ MODE_SELECT
] = "MODE_SELECT",
1189 [ RESERVE
] = "RESERVE",
1190 [ RELEASE
] = "RELEASE",
1192 [ ERASE
] = "ERASE",
1193 [ MODE_SENSE
] = "MODE_SENSE",
1194 [ START_STOP
] = "START_STOP",
1195 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1196 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1197 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1198 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1199 [ READ_10
] = "READ_10",
1200 [ WRITE_10
] = "WRITE_10",
1201 [ SEEK_10
] = "SEEK_10",
1202 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1203 [ VERIFY_10
] = "VERIFY_10",
1204 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1205 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1206 [ SEARCH_LOW
] = "SEARCH_LOW",
1207 [ SET_LIMITS
] = "SET_LIMITS",
1208 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1209 /* READ_POSITION and PRE_FETCH use the same operation code */
1210 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1211 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1212 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1213 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1214 [ COMPARE
] = "COMPARE",
1215 [ COPY_VERIFY
] = "COPY_VERIFY",
1216 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1217 [ READ_BUFFER
] = "READ_BUFFER",
1218 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1219 [ READ_LONG_10
] = "READ_LONG_10",
1220 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1221 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1222 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1223 [ UNMAP
] = "UNMAP",
1224 [ READ_TOC
] = "READ_TOC",
1225 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1226 [ SANITIZE
] = "SANITIZE",
1227 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1228 [ LOG_SELECT
] = "LOG_SELECT",
1229 [ LOG_SENSE
] = "LOG_SENSE",
1230 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1231 [ RESERVE_10
] = "RESERVE_10",
1232 [ RELEASE_10
] = "RELEASE_10",
1233 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1234 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1235 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1236 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1237 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1238 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1239 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1240 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1241 [ READ_16
] = "READ_16",
1242 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1243 [ WRITE_16
] = "WRITE_16",
1244 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1245 [ VERIFY_16
] = "VERIFY_16",
1246 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1247 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1248 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1249 [ LOCATE_16
] = "LOCATE_16",
1250 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1251 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1252 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1253 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1254 [ REPORT_LUNS
] = "REPORT_LUNS",
1255 [ BLANK
] = "BLANK",
1256 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1257 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1258 [ READ_12
] = "READ_12",
1259 [ WRITE_12
] = "WRITE_12",
1260 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1261 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1262 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1263 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1264 [ VERIFY_12
] = "VERIFY_12",
1265 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1266 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1267 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1268 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1269 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1270 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1271 [ READ_CD
] = "READ_CD",
1272 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1273 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1274 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1275 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1276 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1277 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1278 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1279 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1280 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1283 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1288 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1294 void scsi_req_unref(SCSIRequest
*req
)
1296 assert(req
->refcount
> 0);
1297 if (--req
->refcount
== 0) {
1298 if (req
->ops
->free_req
) {
1299 req
->ops
->free_req(req
);
1305 /* Tell the device that we finished processing this chunk of I/O. It
1306 will start the next chunk or complete the command. */
1307 void scsi_req_continue(SCSIRequest
*req
)
1309 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1310 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1311 req
->ops
->write_data(req
);
1313 req
->ops
->read_data(req
);
1317 /* Called by the devices when data is ready for the HBA. The HBA should
1318 start a DMA operation to read or fill the device's data buffer.
1319 Once it completes, calling scsi_req_continue will restart I/O. */
1320 void scsi_req_data(SCSIRequest
*req
, int len
)
1323 if (req
->io_canceled
) {
1324 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1327 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1328 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1331 req
->bus
->info
->transfer_data(req
, len
);
1335 /* If the device calls scsi_req_data and the HBA specified a
1336 * scatter/gather list, the transfer has to happen in a single
1338 assert(!req
->dma_started
);
1339 req
->dma_started
= true;
1341 buf
= scsi_req_get_buf(req
);
1342 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1343 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1345 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1347 scsi_req_continue(req
);
1350 void scsi_req_print(SCSIRequest
*req
)
1355 fprintf(fp
, "[%s id=%d] %s",
1356 req
->dev
->qdev
.parent_bus
->name
,
1358 scsi_command_name(req
->cmd
.buf
[0]));
1359 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1360 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1362 switch (req
->cmd
.mode
) {
1363 case SCSI_XFER_NONE
:
1364 fprintf(fp
, " - none\n");
1366 case SCSI_XFER_FROM_DEV
:
1367 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1369 case SCSI_XFER_TO_DEV
:
1370 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1373 fprintf(fp
, " - Oops\n");
1378 void scsi_req_complete(SCSIRequest
*req
, int status
)
1380 assert(req
->status
== -1);
1381 req
->status
= status
;
1383 assert(req
->sense_len
< sizeof(req
->sense
));
1384 if (status
== GOOD
) {
1388 if (req
->sense_len
) {
1389 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1390 req
->dev
->sense_len
= req
->sense_len
;
1391 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1393 req
->dev
->sense_len
= 0;
1394 req
->dev
->sense_is_ua
= false;
1398 * Unit attention state is now stored in the device's sense buffer
1399 * if the HBA didn't do autosense. Clear the pending unit attention
1402 scsi_clear_unit_attention(req
);
1405 scsi_req_dequeue(req
);
1406 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1407 scsi_req_unref(req
);
1410 void scsi_req_cancel(SCSIRequest
*req
)
1412 if (!req
->enqueued
) {
1416 scsi_req_dequeue(req
);
1417 req
->io_canceled
= true;
1418 if (req
->ops
->cancel_io
) {
1419 req
->ops
->cancel_io(req
);
1421 if (req
->bus
->info
->cancel
) {
1422 req
->bus
->info
->cancel(req
);
1424 scsi_req_unref(req
);
1427 void scsi_req_abort(SCSIRequest
*req
, int status
)
1429 if (!req
->enqueued
) {
1433 scsi_req_dequeue(req
);
1434 req
->io_canceled
= true;
1435 if (req
->ops
->cancel_io
) {
1436 req
->ops
->cancel_io(req
);
1438 scsi_req_complete(req
, status
);
1439 scsi_req_unref(req
);
1442 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1446 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1447 req
= QTAILQ_FIRST(&sdev
->requests
);
1448 scsi_req_cancel(req
);
1450 sdev
->unit_attention
= sense
;
1453 static char *scsibus_get_dev_path(DeviceState
*dev
)
1455 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1456 DeviceState
*hba
= dev
->parent_bus
->parent
;
1460 if (hba
&& hba
->parent_bus
&& hba
->parent_bus
->info
->get_dev_path
) {
1461 id
= hba
->parent_bus
->info
->get_dev_path(hba
);
1464 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1466 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1472 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1474 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1477 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1478 qdev_fw_name(dev
), d
->id
, d
->lun
);
1480 return strdup(path
);
1483 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1486 SCSIDevice
*target_dev
= NULL
;
1488 QTAILQ_FOREACH_REVERSE(qdev
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1489 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1491 if (dev
->channel
== channel
&& dev
->id
== id
) {
1492 if (dev
->lun
== lun
) {
1501 /* SCSI request list. For simplicity, pv points to the whole device */
1503 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1506 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1509 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1510 assert(!req
->io_canceled
);
1511 assert(req
->status
== -1);
1512 assert(req
->enqueued
);
1514 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1515 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1516 qemu_put_be32s(f
, &req
->tag
);
1517 qemu_put_be32s(f
, &req
->lun
);
1518 if (bus
->info
->save_request
) {
1519 bus
->info
->save_request(f
, req
);
1521 if (req
->ops
->save_request
) {
1522 req
->ops
->save_request(f
, req
);
1525 qemu_put_sbyte(f
, 0);
1528 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1531 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1534 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1535 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1540 qemu_get_buffer(f
, buf
, sizeof(buf
));
1541 qemu_get_be32s(f
, &tag
);
1542 qemu_get_be32s(f
, &lun
);
1543 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1544 req
->retry
= (sbyte
== 1);
1545 if (bus
->info
->load_request
) {
1546 req
->hba_private
= bus
->info
->load_request(f
, req
);
1548 if (req
->ops
->load_request
) {
1549 req
->ops
->load_request(f
, req
);
1552 /* Just restart it later. */
1553 scsi_req_enqueue_internal(req
);
1555 /* At this point, the request will be kept alive by the reference
1556 * added by scsi_req_enqueue_internal, so we can release our reference.
1557 * The HBA of course will add its own reference in the load_request
1558 * callback if it needs to hold on the SCSIRequest.
1560 scsi_req_unref(req
);
1566 static const VMStateInfo vmstate_info_scsi_requests
= {
1567 .name
= "scsi-requests",
1568 .get
= get_scsi_requests
,
1569 .put
= put_scsi_requests
,
1572 const VMStateDescription vmstate_scsi_device
= {
1573 .name
= "SCSIDevice",
1575 .minimum_version_id
= 1,
1576 .minimum_version_id_old
= 1,
1577 .fields
= (VMStateField
[]) {
1578 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1579 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1580 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1581 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1582 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1583 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1587 .field_exists
= NULL
,
1588 .size
= 0, /* ouch */
1589 .info
= &vmstate_info_scsi_requests
,
1590 .flags
= VMS_SINGLE
,
1593 VMSTATE_END_OF_LIST()
1597 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1599 DeviceClass
*k
= DEVICE_CLASS(klass
);
1600 k
->bus_info
= &scsi_bus_info
;
1601 k
->init
= scsi_qdev_init
;
1602 k
->unplug
= qdev_simple_unplug_cb
;
1603 k
->exit
= scsi_qdev_exit
;
1606 static TypeInfo scsi_device_type_info
= {
1607 .name
= TYPE_SCSI_DEVICE
,
1608 .parent
= TYPE_DEVICE
,
1609 .instance_size
= sizeof(SCSIDevice
),
1611 .class_size
= sizeof(SCSIDeviceClass
),
1612 .class_init
= scsi_device_class_init
,
1615 static void scsi_register_types(void)
1617 type_register_static(&scsi_device_type_info
);
1620 type_init(scsi_register_types
)