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 struct BusInfo scsi_bus_info
= {
17 .size
= sizeof(SCSIBus
),
18 .get_dev_path
= scsibus_get_dev_path
,
19 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
20 .props
= (Property
[]) {
21 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
22 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
23 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
24 DEFINE_PROP_END_OF_LIST(),
27 static int next_scsi_bus
;
29 static int scsi_device_init(SCSIDevice
*s
)
31 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
38 static void scsi_device_destroy(SCSIDevice
*s
)
40 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
46 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
47 uint8_t *buf
, void *hba_private
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
51 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
57 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
59 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
60 if (sc
->unit_attention_reported
) {
61 sc
->unit_attention_reported(s
);
65 /* Create a scsi bus, and attach devices to it. */
66 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
68 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
69 bus
->busnr
= next_scsi_bus
++;
71 bus
->qbus
.allow_hotplug
= 1;
74 static void scsi_dma_restart_bh(void *opaque
)
76 SCSIDevice
*s
= opaque
;
77 SCSIRequest
*req
, *next
;
79 qemu_bh_delete(s
->bh
);
82 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
86 switch (req
->cmd
.mode
) {
87 case SCSI_XFER_FROM_DEV
:
88 case SCSI_XFER_TO_DEV
:
89 scsi_req_continue(req
);
93 scsi_req_dequeue(req
);
94 scsi_req_enqueue(req
);
102 void scsi_req_retry(SCSIRequest
*req
)
104 /* No need to save a reference, because scsi_dma_restart_bh just
105 * looks at the request list. */
109 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
111 SCSIDevice
*s
= opaque
;
117 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
118 qemu_bh_schedule(s
->bh
);
122 static int scsi_qdev_init(DeviceState
*qdev
)
124 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
125 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
129 if (dev
->channel
> bus
->info
->max_channel
) {
130 error_report("bad scsi channel id: %d", dev
->channel
);
133 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
134 error_report("bad scsi device id: %d", dev
->id
);
137 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
138 error_report("bad scsi device lun: %d", dev
->lun
);
144 if (dev
->lun
== -1) {
148 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
149 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
150 if (d
&& d
->lun
== dev
->lun
) {
151 error_report("no free target");
155 } else if (dev
->lun
== -1) {
158 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
159 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
160 if (d
&& d
->lun
== lun
) {
161 error_report("no free lun");
166 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
168 if (d
->lun
== dev
->lun
&& dev
!= d
) {
173 QTAILQ_INIT(&dev
->requests
);
174 rc
= scsi_device_init(dev
);
176 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
184 static int scsi_qdev_exit(DeviceState
*qdev
)
186 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
189 qemu_del_vm_change_state_handler(dev
->vmsentry
);
191 scsi_device_destroy(dev
);
195 /* handle legacy '-drive if=scsi,...' cmd line args */
196 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
197 int unit
, bool removable
, int bootindex
)
202 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
203 dev
= qdev_create(&bus
->qbus
, driver
);
204 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
205 if (bootindex
>= 0) {
206 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
208 if (qdev_prop_exists(dev
, "removable")) {
209 qdev_prop_set_bit(dev
, "removable", removable
);
211 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
215 if (qdev_init(dev
) < 0)
217 return SCSI_DEVICE(dev
);
220 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
227 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
228 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
232 qemu_opts_loc_restore(dinfo
->opts
);
233 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
242 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
244 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
245 scsi_req_complete(req
, CHECK_CONDITION
);
249 static const struct SCSIReqOps reqops_invalid_field
= {
250 .size
= sizeof(SCSIRequest
),
251 .send_command
= scsi_invalid_field
254 /* SCSIReqOps implementation for invalid commands. */
256 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
258 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
259 scsi_req_complete(req
, CHECK_CONDITION
);
263 static const struct SCSIReqOps reqops_invalid_opcode
= {
264 .size
= sizeof(SCSIRequest
),
265 .send_command
= scsi_invalid_command
268 /* SCSIReqOps implementation for unit attention conditions. */
270 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
272 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
273 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
274 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
275 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
277 scsi_req_complete(req
, CHECK_CONDITION
);
281 static const struct SCSIReqOps reqops_unit_attention
= {
282 .size
= sizeof(SCSIRequest
),
283 .send_command
= scsi_unit_attention
286 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
289 typedef struct SCSITargetReq SCSITargetReq
;
291 struct SCSITargetReq
{
297 static void store_lun(uint8_t *outbuf
, int lun
)
303 outbuf
[1] = (lun
& 255);
304 outbuf
[0] = (lun
>> 8) | 0x40;
307 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
314 if (r
->req
.cmd
.xfer
< 16) {
317 if (r
->req
.cmd
.buf
[2] > 2) {
320 channel
= r
->req
.dev
->channel
;
324 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
325 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
327 if (dev
->channel
== channel
&& dev
->id
== id
) {
337 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
338 if (len
> sizeof(r
->buf
)) {
339 /* TODO: > 256 LUNs? */
343 memset(r
->buf
, 0, len
);
344 stl_be_p(&r
->buf
, n
);
345 i
= found_lun0
? 8 : 16;
346 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
347 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
349 if (dev
->channel
== channel
&& dev
->id
== id
) {
350 store_lun(&r
->buf
[i
], dev
->lun
);
359 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
361 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
362 if (r
->req
.cmd
.buf
[1] & 0x2) {
363 /* Command support data - optional, not implemented */
367 if (r
->req
.cmd
.buf
[1] & 0x1) {
368 /* Vital product data */
369 uint8_t page_code
= r
->req
.cmd
.buf
[2];
370 r
->buf
[r
->len
++] = page_code
; /* this page */
371 r
->buf
[r
->len
++] = 0x00;
374 case 0x00: /* Supported page codes, mandatory */
378 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
379 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
386 assert(r
->len
< sizeof(r
->buf
));
387 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
391 /* Standard INQUIRY data */
392 if (r
->req
.cmd
.buf
[2] != 0) {
397 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
398 memset(r
->buf
, 0, r
->len
);
399 if (r
->req
.lun
!= 0) {
400 r
->buf
[0] = TYPE_NO_LUN
;
402 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
403 r
->buf
[2] = 5; /* Version */
404 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
405 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
406 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
407 memcpy(&r
->buf
[8], "QEMU ", 8);
408 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
409 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
414 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
416 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
420 if (!scsi_target_emulate_report_luns(r
)) {
421 goto illegal_request
;
425 if (!scsi_target_emulate_inquiry(r
)) {
426 goto illegal_request
;
430 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
431 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
432 (req
->cmd
.buf
[1] & 1) == 0);
433 if (r
->req
.dev
->sense_is_ua
) {
434 scsi_device_unit_attention_reported(req
->dev
);
435 r
->req
.dev
->sense_len
= 0;
436 r
->req
.dev
->sense_is_ua
= false;
440 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
441 scsi_req_complete(req
, CHECK_CONDITION
);
444 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
445 scsi_req_complete(req
, CHECK_CONDITION
);
450 scsi_req_complete(req
, GOOD
);
455 static void scsi_target_read_data(SCSIRequest
*req
)
457 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
463 scsi_req_data(&r
->req
, n
);
465 scsi_req_complete(&r
->req
, GOOD
);
469 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
471 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
476 static const struct SCSIReqOps reqops_target_command
= {
477 .size
= sizeof(SCSITargetReq
),
478 .send_command
= scsi_target_send_command
,
479 .read_data
= scsi_target_read_data
,
480 .get_buf
= scsi_target_get_buf
,
484 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
485 uint32_t tag
, uint32_t lun
, void *hba_private
)
489 req
= g_malloc0(reqops
->size
);
491 req
->bus
= scsi_bus_from_device(d
);
495 req
->hba_private
= hba_private
;
499 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
503 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
504 uint8_t *buf
, void *hba_private
)
506 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
510 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
511 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
512 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
514 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
517 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
521 if (cmd
.xfer
> INT32_MAX
) {
522 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
523 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
524 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
525 (buf
[0] != INQUIRY
&&
526 buf
[0] != REPORT_LUNS
&&
527 buf
[0] != GET_CONFIGURATION
&&
528 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
531 * If we already have a pending unit attention condition,
532 * report this one before triggering another one.
534 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
535 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
537 } else if (lun
!= d
->lun
||
538 buf
[0] == REPORT_LUNS
||
539 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
540 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
543 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
548 req
->resid
= req
->cmd
.xfer
;
552 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
554 case TEST_UNIT_READY
:
555 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
558 trace_scsi_report_luns(d
->id
, lun
, tag
);
561 trace_scsi_request_sense(d
->id
, lun
, tag
);
570 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
572 return req
->ops
->get_buf(req
);
575 static void scsi_clear_unit_attention(SCSIRequest
*req
)
578 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
579 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
584 * If an INQUIRY command enters the enabled command state,
585 * the device server shall [not] clear any unit attention condition;
586 * See also MMC-6, paragraphs 6.5 and 6.6.2.
588 if (req
->cmd
.buf
[0] == INQUIRY
||
589 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
590 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
594 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
595 ua
= &req
->dev
->unit_attention
;
597 ua
= &req
->bus
->unit_attention
;
601 * If a REPORT LUNS command enters the enabled command state, [...]
602 * the device server shall clear any pending unit attention condition
603 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
605 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
606 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
607 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
611 *ua
= SENSE_CODE(NO_SENSE
);
614 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
619 if (!req
->sense_len
) {
623 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
626 * FIXME: clearing unit attention conditions upon autosense should be done
627 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
630 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
631 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
632 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
634 if (req
->dev
->sense_is_ua
) {
635 scsi_device_unit_attention_reported(req
->dev
);
636 req
->dev
->sense_len
= 0;
637 req
->dev
->sense_is_ua
= false;
642 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
644 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
647 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
649 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
650 sense
.key
, sense
.asc
, sense
.ascq
);
651 memset(req
->sense
, 0, 18);
652 req
->sense
[0] = 0x70;
653 req
->sense
[2] = sense
.key
;
655 req
->sense
[12] = sense
.asc
;
656 req
->sense
[13] = sense
.ascq
;
660 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
662 assert(!req
->enqueued
);
664 if (req
->bus
->info
->get_sg_list
) {
665 req
->sg
= req
->bus
->info
->get_sg_list(req
);
669 req
->enqueued
= true;
670 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
673 int32_t scsi_req_enqueue(SCSIRequest
*req
)
678 scsi_req_enqueue_internal(req
);
680 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
685 static void scsi_req_dequeue(SCSIRequest
*req
)
687 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
690 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
691 req
->enqueued
= false;
696 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
698 /* MMC-6, paragraph 6.7. */
701 if ((data_type
& 3) == 0) {
702 /* Each descriptor is as in Table 295 - Nominal performance. */
703 return 16 * num_desc
+ 8;
705 /* Each descriptor is as in Table 296 - Exceptions. */
706 return 6 * num_desc
+ 8;
711 return 8 * num_desc
+ 8;
713 return 2048 * num_desc
+ 8;
715 return 16 * num_desc
+ 8;
721 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
723 switch (buf
[0] >> 5) {
730 cmd
->xfer
= lduw_be_p(&buf
[7]);
734 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
738 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
746 case TEST_UNIT_READY
:
750 case WRITE_FILEMARKS
:
751 case WRITE_FILEMARKS_16
:
756 case ALLOW_MEDIUM_REMOVAL
:
759 case SYNCHRONIZE_CACHE
:
760 case SYNCHRONIZE_CACHE_16
:
762 case LOCK_UNLOCK_CACHE
:
773 case ALLOW_OVERWRITE
:
780 cmd
->xfer
= dev
->blocksize
;
782 case READ_CAPACITY_10
:
785 case READ_BLOCK_LIMITS
:
788 case SEND_VOLUME_TAG
:
789 /* GPCMD_SET_STREAMING from multimedia commands. */
790 if (dev
->type
== TYPE_ROM
) {
791 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
793 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
797 /* length 0 means 256 blocks */
798 if (cmd
->xfer
== 0) {
802 case WRITE_VERIFY_10
:
804 case WRITE_VERIFY_12
:
806 case WRITE_VERIFY_16
:
807 cmd
->xfer
*= dev
->blocksize
;
811 /* length 0 means 256 blocks */
812 if (cmd
->xfer
== 0) {
816 case RECOVER_BUFFERED_DATA
:
819 cmd
->xfer
*= dev
->blocksize
;
822 /* MMC mandates the parameter list to be 12-bytes long. Parameters
823 * for block devices are restricted to the header right now. */
824 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
827 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
831 case RECEIVE_DIAGNOSTIC
:
832 case SEND_DIAGNOSTIC
:
833 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
839 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
841 case PERSISTENT_RESERVE_OUT
:
842 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
845 if (dev
->type
== TYPE_ROM
) {
846 /* MMC command GET PERFORMANCE. */
847 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
848 buf
[10], buf
[1] & 0x1f);
851 case MECHANISM_STATUS
:
852 case READ_DVD_STRUCTURE
:
853 case SEND_DVD_STRUCTURE
:
854 case MAINTENANCE_OUT
:
856 if (dev
->type
== TYPE_ROM
) {
857 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
858 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
865 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
868 /* stream commands */
875 case RECOVER_BUFFERED_DATA
:
878 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
879 if (buf
[1] & 0x01) { /* fixed */
880 cmd
->xfer
*= dev
->blocksize
;
884 case READ_REVERSE_16
:
888 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
889 if (buf
[1] & 0x01) { /* fixed */
890 cmd
->xfer
*= dev
->blocksize
;
899 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
902 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
905 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
907 /* generic commands */
909 return scsi_req_length(cmd
, dev
, buf
);
914 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
917 cmd
->mode
= SCSI_XFER_NONE
;
920 switch (cmd
->buf
[0]) {
923 case WRITE_VERIFY_10
:
925 case WRITE_VERIFY_12
:
927 case WRITE_VERIFY_16
:
931 case CHANGE_DEFINITION
:
935 case SEND_DIAGNOSTIC
:
938 case REASSIGN_BLOCKS
:
948 case SEARCH_EQUAL_12
:
951 case SEND_VOLUME_TAG
:
953 case SEND_DVD_STRUCTURE
:
954 case PERSISTENT_RESERVE_OUT
:
955 case MAINTENANCE_OUT
:
956 case ATA_PASSTHROUGH
:
957 cmd
->mode
= SCSI_XFER_TO_DEV
;
960 cmd
->mode
= SCSI_XFER_FROM_DEV
;
965 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
967 uint8_t *buf
= cmd
->buf
;
970 switch (buf
[0] >> 5) {
972 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
977 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
980 lba
= ldq_be_p(&buf
[2]);
989 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
993 if (dev
->type
== TYPE_TAPE
) {
994 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
996 rc
= scsi_req_length(cmd
, dev
, buf
);
1001 memcpy(cmd
->buf
, buf
, cmd
->len
);
1002 scsi_cmd_xfer_mode(cmd
);
1003 cmd
->lba
= scsi_cmd_lba(cmd
);
1008 * Predefined sense codes
1011 /* No sense data available */
1012 const struct SCSISense sense_code_NO_SENSE
= {
1013 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1016 /* LUN not ready, Manual intervention required */
1017 const struct SCSISense sense_code_LUN_NOT_READY
= {
1018 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1021 /* LUN not ready, Medium not present */
1022 const struct SCSISense sense_code_NO_MEDIUM
= {
1023 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1026 /* LUN not ready, medium removal prevented */
1027 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1028 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
1031 /* Hardware error, internal target failure */
1032 const struct SCSISense sense_code_TARGET_FAILURE
= {
1033 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1036 /* Illegal request, invalid command operation code */
1037 const struct SCSISense sense_code_INVALID_OPCODE
= {
1038 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1041 /* Illegal request, LBA out of range */
1042 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1043 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1046 /* Illegal request, Invalid field in CDB */
1047 const struct SCSISense sense_code_INVALID_FIELD
= {
1048 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1051 /* Illegal request, LUN not supported */
1052 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1053 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1056 /* Illegal request, Saving parameters not supported */
1057 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1058 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1061 /* Illegal request, Incompatible medium installed */
1062 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1063 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1066 /* Illegal request, medium removal prevented */
1067 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1068 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
1071 /* Command aborted, I/O process terminated */
1072 const struct SCSISense sense_code_IO_ERROR
= {
1073 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1076 /* Command aborted, I_T Nexus loss occurred */
1077 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1078 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1081 /* Command aborted, Logical Unit failure */
1082 const struct SCSISense sense_code_LUN_FAILURE
= {
1083 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1086 /* Unit attention, Power on, reset or bus device reset occurred */
1087 const struct SCSISense sense_code_RESET
= {
1088 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1091 /* Unit attention, No medium */
1092 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1093 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1096 /* Unit attention, Medium may have changed */
1097 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1098 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1101 /* Unit attention, Reported LUNs data has changed */
1102 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1103 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1106 /* Unit attention, Device internal reset */
1107 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1108 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1114 * Convert between fixed and descriptor sense buffers
1116 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1117 uint8_t *buf
, int len
, bool fixed
)
1121 if (!fixed
&& len
< 8) {
1126 sense
.key
= NO_SENSE
;
1130 fixed_in
= (in_buf
[0] & 2) == 0;
1132 if (fixed
== fixed_in
) {
1133 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1134 return MIN(len
, in_len
);
1138 sense
.key
= in_buf
[2];
1139 sense
.asc
= in_buf
[12];
1140 sense
.ascq
= in_buf
[13];
1142 sense
.key
= in_buf
[1];
1143 sense
.asc
= in_buf
[2];
1144 sense
.ascq
= in_buf
[3];
1148 memset(buf
, 0, len
);
1150 /* Return fixed format sense buffer */
1154 buf
[12] = sense
.asc
;
1155 buf
[13] = sense
.ascq
;
1156 return MIN(len
, 18);
1158 /* Return descriptor format sense buffer */
1162 buf
[3] = sense
.ascq
;
1167 static const char *scsi_command_name(uint8_t cmd
)
1169 static const char *names
[] = {
1170 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1171 [ REWIND
] = "REWIND",
1172 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1173 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1174 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1175 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1176 [ READ_6
] = "READ_6",
1177 [ WRITE_6
] = "WRITE_6",
1178 [ SET_CAPACITY
] = "SET_CAPACITY",
1179 [ READ_REVERSE
] = "READ_REVERSE",
1180 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1181 [ SPACE
] = "SPACE",
1182 [ INQUIRY
] = "INQUIRY",
1183 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1184 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1185 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1186 [ MODE_SELECT
] = "MODE_SELECT",
1187 [ RESERVE
] = "RESERVE",
1188 [ RELEASE
] = "RELEASE",
1190 [ ERASE
] = "ERASE",
1191 [ MODE_SENSE
] = "MODE_SENSE",
1192 [ START_STOP
] = "START_STOP",
1193 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1194 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1195 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1196 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1197 [ READ_10
] = "READ_10",
1198 [ WRITE_10
] = "WRITE_10",
1199 [ SEEK_10
] = "SEEK_10",
1200 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1201 [ VERIFY_10
] = "VERIFY_10",
1202 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1203 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1204 [ SEARCH_LOW
] = "SEARCH_LOW",
1205 [ SET_LIMITS
] = "SET_LIMITS",
1206 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1207 /* READ_POSITION and PRE_FETCH use the same operation code */
1208 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1209 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1210 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1211 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1212 [ COMPARE
] = "COMPARE",
1213 [ COPY_VERIFY
] = "COPY_VERIFY",
1214 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1215 [ READ_BUFFER
] = "READ_BUFFER",
1216 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1217 [ READ_LONG_10
] = "READ_LONG_10",
1218 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1219 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1220 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1221 [ UNMAP
] = "UNMAP",
1222 [ READ_TOC
] = "READ_TOC",
1223 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1224 [ SANITIZE
] = "SANITIZE",
1225 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1226 [ LOG_SELECT
] = "LOG_SELECT",
1227 [ LOG_SENSE
] = "LOG_SENSE",
1228 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1229 [ RESERVE_10
] = "RESERVE_10",
1230 [ RELEASE_10
] = "RELEASE_10",
1231 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1232 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1233 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1234 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1235 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1236 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1237 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1238 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1239 [ READ_16
] = "READ_16",
1240 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1241 [ WRITE_16
] = "WRITE_16",
1242 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1243 [ VERIFY_16
] = "VERIFY_16",
1244 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1245 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1246 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1247 [ LOCATE_16
] = "LOCATE_16",
1248 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1249 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1250 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1251 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1252 [ REPORT_LUNS
] = "REPORT_LUNS",
1253 [ BLANK
] = "BLANK",
1254 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1255 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1256 [ READ_12
] = "READ_12",
1257 [ WRITE_12
] = "WRITE_12",
1258 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1259 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1260 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1261 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1262 [ VERIFY_12
] = "VERIFY_12",
1263 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1264 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1265 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1266 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1267 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1268 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1269 [ READ_CD
] = "READ_CD",
1270 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1271 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1272 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1273 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1274 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1275 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1276 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1277 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1278 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1281 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1286 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1292 void scsi_req_unref(SCSIRequest
*req
)
1294 assert(req
->refcount
> 0);
1295 if (--req
->refcount
== 0) {
1296 if (req
->ops
->free_req
) {
1297 req
->ops
->free_req(req
);
1303 /* Tell the device that we finished processing this chunk of I/O. It
1304 will start the next chunk or complete the command. */
1305 void scsi_req_continue(SCSIRequest
*req
)
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
);
1405 scsi_req_unref(req
);
1408 void scsi_req_cancel(SCSIRequest
*req
)
1410 if (!req
->enqueued
) {
1414 scsi_req_dequeue(req
);
1415 req
->io_canceled
= true;
1416 if (req
->ops
->cancel_io
) {
1417 req
->ops
->cancel_io(req
);
1419 if (req
->bus
->info
->cancel
) {
1420 req
->bus
->info
->cancel(req
);
1422 scsi_req_unref(req
);
1425 void scsi_req_abort(SCSIRequest
*req
, int status
)
1427 if (!req
->enqueued
) {
1431 scsi_req_dequeue(req
);
1432 req
->io_canceled
= true;
1433 if (req
->ops
->cancel_io
) {
1434 req
->ops
->cancel_io(req
);
1436 scsi_req_complete(req
, status
);
1437 scsi_req_unref(req
);
1440 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1444 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1445 req
= QTAILQ_FIRST(&sdev
->requests
);
1446 scsi_req_cancel(req
);
1448 sdev
->unit_attention
= sense
;
1451 static char *scsibus_get_dev_path(DeviceState
*dev
)
1453 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1454 DeviceState
*hba
= dev
->parent_bus
->parent
;
1458 if (hba
&& hba
->parent_bus
&& hba
->parent_bus
->info
->get_dev_path
) {
1459 id
= hba
->parent_bus
->info
->get_dev_path(hba
);
1462 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1464 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1470 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1472 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1475 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1476 qdev_fw_name(dev
), d
->id
, d
->lun
);
1478 return strdup(path
);
1481 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1484 SCSIDevice
*target_dev
= NULL
;
1486 QTAILQ_FOREACH_REVERSE(qdev
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1487 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1489 if (dev
->channel
== channel
&& dev
->id
== id
) {
1490 if (dev
->lun
== lun
) {
1499 /* SCSI request list. For simplicity, pv points to the whole device */
1501 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1504 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1507 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1508 assert(!req
->io_canceled
);
1509 assert(req
->status
== -1);
1511 assert(req
->enqueued
);
1513 qemu_put_sbyte(f
, 1);
1514 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1515 qemu_put_be32s(f
, &req
->tag
);
1516 qemu_put_be32s(f
, &req
->lun
);
1517 if (bus
->info
->save_request
) {
1518 bus
->info
->save_request(f
, req
);
1520 if (req
->ops
->save_request
) {
1521 req
->ops
->save_request(f
, req
);
1524 qemu_put_sbyte(f
, 0);
1527 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1530 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1532 while (qemu_get_sbyte(f
)) {
1533 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1538 qemu_get_buffer(f
, buf
, sizeof(buf
));
1539 qemu_get_be32s(f
, &tag
);
1540 qemu_get_be32s(f
, &lun
);
1541 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1542 if (bus
->info
->load_request
) {
1543 req
->hba_private
= bus
->info
->load_request(f
, req
);
1545 if (req
->ops
->load_request
) {
1546 req
->ops
->load_request(f
, req
);
1549 /* Just restart it later. */
1551 scsi_req_enqueue_internal(req
);
1553 /* At this point, the request will be kept alive by the reference
1554 * added by scsi_req_enqueue_internal, so we can release our reference.
1555 * The HBA of course will add its own reference in the load_request
1556 * callback if it needs to hold on the SCSIRequest.
1558 scsi_req_unref(req
);
1564 const VMStateInfo vmstate_info_scsi_requests
= {
1565 .name
= "scsi-requests",
1566 .get
= get_scsi_requests
,
1567 .put
= put_scsi_requests
,
1570 const VMStateDescription vmstate_scsi_device
= {
1571 .name
= "SCSIDevice",
1573 .minimum_version_id
= 1,
1574 .minimum_version_id_old
= 1,
1575 .fields
= (VMStateField
[]) {
1576 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1577 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1578 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1579 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1580 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1581 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1585 .field_exists
= NULL
,
1586 .size
= 0, /* ouch */
1587 .info
= &vmstate_info_scsi_requests
,
1588 .flags
= VMS_SINGLE
,
1591 VMSTATE_END_OF_LIST()
1595 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1597 DeviceClass
*k
= DEVICE_CLASS(klass
);
1598 k
->bus_info
= &scsi_bus_info
;
1599 k
->init
= scsi_qdev_init
;
1600 k
->unplug
= qdev_simple_unplug_cb
;
1601 k
->exit
= scsi_qdev_exit
;
1604 static TypeInfo scsi_device_type_info
= {
1605 .name
= TYPE_SCSI_DEVICE
,
1606 .parent
= TYPE_DEVICE
,
1607 .instance_size
= sizeof(SCSIDevice
),
1609 .class_size
= sizeof(SCSIDeviceClass
),
1610 .class_init
= scsi_device_class_init
,
1613 static void scsi_register_types(void)
1615 type_register_static(&scsi_device_type_info
);
1618 type_init(scsi_register_types
)