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
,
28 static int next_scsi_bus
;
30 static int scsi_device_init(SCSIDevice
*s
)
32 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
39 static void scsi_device_destroy(SCSIDevice
*s
)
41 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
47 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
48 uint8_t *buf
, void *hba_private
)
50 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
52 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
58 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
60 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
61 if (sc
->unit_attention_reported
) {
62 sc
->unit_attention_reported(s
);
66 /* Create a scsi bus, and attach devices to it. */
67 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
69 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
70 bus
->busnr
= next_scsi_bus
++;
72 bus
->qbus
.allow_hotplug
= 1;
75 static void scsi_dma_restart_bh(void *opaque
)
77 SCSIDevice
*s
= opaque
;
78 SCSIRequest
*req
, *next
;
80 qemu_bh_delete(s
->bh
);
83 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
87 switch (req
->cmd
.mode
) {
88 case SCSI_XFER_FROM_DEV
:
89 case SCSI_XFER_TO_DEV
:
90 scsi_req_continue(req
);
94 scsi_req_dequeue(req
);
95 scsi_req_enqueue(req
);
103 void scsi_req_retry(SCSIRequest
*req
)
105 /* No need to save a reference, because scsi_dma_restart_bh just
106 * looks at the request list. */
110 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
112 SCSIDevice
*s
= opaque
;
118 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
119 qemu_bh_schedule(s
->bh
);
123 static int scsi_qdev_init(DeviceState
*qdev
)
125 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
126 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
130 if (dev
->channel
> bus
->info
->max_channel
) {
131 error_report("bad scsi channel id: %d", dev
->channel
);
134 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
135 error_report("bad scsi device id: %d", dev
->id
);
138 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
139 error_report("bad scsi device lun: %d", dev
->lun
);
145 if (dev
->lun
== -1) {
149 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
150 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
151 if (d
&& d
->lun
== dev
->lun
) {
152 error_report("no free target");
156 } else if (dev
->lun
== -1) {
159 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
160 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
161 if (d
&& d
->lun
== lun
) {
162 error_report("no free lun");
167 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
169 if (d
->lun
== dev
->lun
&& dev
!= d
) {
174 QTAILQ_INIT(&dev
->requests
);
175 rc
= scsi_device_init(dev
);
177 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
185 static int scsi_qdev_exit(DeviceState
*qdev
)
187 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
190 qemu_del_vm_change_state_handler(dev
->vmsentry
);
192 scsi_device_destroy(dev
);
196 /* handle legacy '-drive if=scsi,...' cmd line args */
197 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
198 int unit
, bool removable
, int bootindex
)
203 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
204 dev
= qdev_create(&bus
->qbus
, driver
);
205 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
206 if (bootindex
>= 0) {
207 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
209 if (qdev_prop_exists(dev
, "removable")) {
210 qdev_prop_set_bit(dev
, "removable", removable
);
212 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
216 if (qdev_init(dev
) < 0)
218 return SCSI_DEVICE(dev
);
221 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
228 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
229 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
233 qemu_opts_loc_restore(dinfo
->opts
);
234 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
243 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
245 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
246 scsi_req_complete(req
, CHECK_CONDITION
);
250 static const struct SCSIReqOps reqops_invalid_field
= {
251 .size
= sizeof(SCSIRequest
),
252 .send_command
= scsi_invalid_field
255 /* SCSIReqOps implementation for invalid commands. */
257 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
259 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
260 scsi_req_complete(req
, CHECK_CONDITION
);
264 static const struct SCSIReqOps reqops_invalid_opcode
= {
265 .size
= sizeof(SCSIRequest
),
266 .send_command
= scsi_invalid_command
269 /* SCSIReqOps implementation for unit attention conditions. */
271 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
273 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
274 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
275 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
276 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
278 scsi_req_complete(req
, CHECK_CONDITION
);
282 static const struct SCSIReqOps reqops_unit_attention
= {
283 .size
= sizeof(SCSIRequest
),
284 .send_command
= scsi_unit_attention
287 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
290 typedef struct SCSITargetReq SCSITargetReq
;
292 struct SCSITargetReq
{
298 static void store_lun(uint8_t *outbuf
, int lun
)
304 outbuf
[1] = (lun
& 255);
305 outbuf
[0] = (lun
>> 8) | 0x40;
308 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
315 if (r
->req
.cmd
.xfer
< 16) {
318 if (r
->req
.cmd
.buf
[2] > 2) {
321 channel
= r
->req
.dev
->channel
;
325 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
326 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
328 if (dev
->channel
== channel
&& dev
->id
== id
) {
338 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
339 if (len
> sizeof(r
->buf
)) {
340 /* TODO: > 256 LUNs? */
344 memset(r
->buf
, 0, len
);
345 stl_be_p(&r
->buf
, n
);
346 i
= found_lun0
? 8 : 16;
347 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
348 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
350 if (dev
->channel
== channel
&& dev
->id
== id
) {
351 store_lun(&r
->buf
[i
], dev
->lun
);
360 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
362 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
363 if (r
->req
.cmd
.buf
[1] & 0x2) {
364 /* Command support data - optional, not implemented */
368 if (r
->req
.cmd
.buf
[1] & 0x1) {
369 /* Vital product data */
370 uint8_t page_code
= r
->req
.cmd
.buf
[2];
371 r
->buf
[r
->len
++] = page_code
; /* this page */
372 r
->buf
[r
->len
++] = 0x00;
375 case 0x00: /* Supported page codes, mandatory */
379 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
380 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
387 assert(r
->len
< sizeof(r
->buf
));
388 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
392 /* Standard INQUIRY data */
393 if (r
->req
.cmd
.buf
[2] != 0) {
398 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
399 memset(r
->buf
, 0, r
->len
);
400 if (r
->req
.lun
!= 0) {
401 r
->buf
[0] = TYPE_NO_LUN
;
403 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
404 r
->buf
[2] = 5; /* Version */
405 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
406 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
407 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
408 memcpy(&r
->buf
[8], "QEMU ", 8);
409 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
410 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
415 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
417 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
421 if (!scsi_target_emulate_report_luns(r
)) {
422 goto illegal_request
;
426 if (!scsi_target_emulate_inquiry(r
)) {
427 goto illegal_request
;
431 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
432 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
433 (req
->cmd
.buf
[1] & 1) == 0);
434 if (r
->req
.dev
->sense_is_ua
) {
435 scsi_device_unit_attention_reported(req
->dev
);
436 r
->req
.dev
->sense_len
= 0;
437 r
->req
.dev
->sense_is_ua
= false;
441 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
442 scsi_req_complete(req
, CHECK_CONDITION
);
445 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
446 scsi_req_complete(req
, CHECK_CONDITION
);
451 scsi_req_complete(req
, GOOD
);
456 static void scsi_target_read_data(SCSIRequest
*req
)
458 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
464 scsi_req_data(&r
->req
, n
);
466 scsi_req_complete(&r
->req
, GOOD
);
470 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
472 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
477 static const struct SCSIReqOps reqops_target_command
= {
478 .size
= sizeof(SCSITargetReq
),
479 .send_command
= scsi_target_send_command
,
480 .read_data
= scsi_target_read_data
,
481 .get_buf
= scsi_target_get_buf
,
485 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
486 uint32_t tag
, uint32_t lun
, void *hba_private
)
490 req
= g_malloc0(reqops
->size
);
492 req
->bus
= scsi_bus_from_device(d
);
496 req
->hba_private
= hba_private
;
500 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
504 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
505 uint8_t *buf
, void *hba_private
)
507 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
511 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
512 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
513 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
515 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
518 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
522 if (cmd
.xfer
> INT32_MAX
) {
523 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
524 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
525 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
526 (buf
[0] != INQUIRY
&&
527 buf
[0] != REPORT_LUNS
&&
528 buf
[0] != GET_CONFIGURATION
&&
529 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
532 * If we already have a pending unit attention condition,
533 * report this one before triggering another one.
535 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
536 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
538 } else if (lun
!= d
->lun
||
539 buf
[0] == REPORT_LUNS
||
540 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
541 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
544 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
549 req
->resid
= req
->cmd
.xfer
;
553 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
555 case TEST_UNIT_READY
:
556 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
559 trace_scsi_report_luns(d
->id
, lun
, tag
);
562 trace_scsi_request_sense(d
->id
, lun
, tag
);
571 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
573 return req
->ops
->get_buf(req
);
576 static void scsi_clear_unit_attention(SCSIRequest
*req
)
579 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
580 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
585 * If an INQUIRY command enters the enabled command state,
586 * the device server shall [not] clear any unit attention condition;
587 * See also MMC-6, paragraphs 6.5 and 6.6.2.
589 if (req
->cmd
.buf
[0] == INQUIRY
||
590 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
591 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
595 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
596 ua
= &req
->dev
->unit_attention
;
598 ua
= &req
->bus
->unit_attention
;
602 * If a REPORT LUNS command enters the enabled command state, [...]
603 * the device server shall clear any pending unit attention condition
604 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
606 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
607 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
608 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
612 *ua
= SENSE_CODE(NO_SENSE
);
615 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
620 if (!req
->sense_len
) {
624 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
627 * FIXME: clearing unit attention conditions upon autosense should be done
628 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
631 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
632 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
633 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
635 if (req
->dev
->sense_is_ua
) {
636 scsi_device_unit_attention_reported(req
->dev
);
637 req
->dev
->sense_len
= 0;
638 req
->dev
->sense_is_ua
= false;
643 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
645 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
648 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
650 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
651 sense
.key
, sense
.asc
, sense
.ascq
);
652 memset(req
->sense
, 0, 18);
653 req
->sense
[0] = 0x70;
654 req
->sense
[2] = sense
.key
;
656 req
->sense
[12] = sense
.asc
;
657 req
->sense
[13] = sense
.ascq
;
661 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
663 assert(!req
->enqueued
);
665 if (req
->bus
->info
->get_sg_list
) {
666 req
->sg
= req
->bus
->info
->get_sg_list(req
);
670 req
->enqueued
= true;
671 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
674 int32_t scsi_req_enqueue(SCSIRequest
*req
)
679 scsi_req_enqueue_internal(req
);
681 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
686 static void scsi_req_dequeue(SCSIRequest
*req
)
688 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
691 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
692 req
->enqueued
= false;
697 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
699 /* MMC-6, paragraph 6.7. */
702 if ((data_type
& 3) == 0) {
703 /* Each descriptor is as in Table 295 - Nominal performance. */
704 return 16 * num_desc
+ 8;
706 /* Each descriptor is as in Table 296 - Exceptions. */
707 return 6 * num_desc
+ 8;
712 return 8 * num_desc
+ 8;
714 return 2048 * num_desc
+ 8;
716 return 16 * num_desc
+ 8;
722 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
724 switch (buf
[0] >> 5) {
731 cmd
->xfer
= lduw_be_p(&buf
[7]);
735 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
739 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
747 case TEST_UNIT_READY
:
751 case WRITE_FILEMARKS
:
752 case WRITE_FILEMARKS_16
:
757 case ALLOW_MEDIUM_REMOVAL
:
760 case SYNCHRONIZE_CACHE
:
761 case SYNCHRONIZE_CACHE_16
:
763 case LOCK_UNLOCK_CACHE
:
774 case ALLOW_OVERWRITE
:
781 cmd
->xfer
= dev
->blocksize
;
783 case READ_CAPACITY_10
:
786 case READ_BLOCK_LIMITS
:
789 case SEND_VOLUME_TAG
:
790 /* GPCMD_SET_STREAMING from multimedia commands. */
791 if (dev
->type
== TYPE_ROM
) {
792 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
794 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
798 /* length 0 means 256 blocks */
799 if (cmd
->xfer
== 0) {
803 case WRITE_VERIFY_10
:
805 case WRITE_VERIFY_12
:
807 case WRITE_VERIFY_16
:
808 cmd
->xfer
*= dev
->blocksize
;
812 /* length 0 means 256 blocks */
813 if (cmd
->xfer
== 0) {
817 case RECOVER_BUFFERED_DATA
:
820 cmd
->xfer
*= dev
->blocksize
;
823 /* MMC mandates the parameter list to be 12-bytes long. Parameters
824 * for block devices are restricted to the header right now. */
825 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
828 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
832 case RECEIVE_DIAGNOSTIC
:
833 case SEND_DIAGNOSTIC
:
834 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
840 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
842 case PERSISTENT_RESERVE_OUT
:
843 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
846 if (dev
->type
== TYPE_ROM
) {
847 /* MMC command GET PERFORMANCE. */
848 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
849 buf
[10], buf
[1] & 0x1f);
852 case MECHANISM_STATUS
:
853 case READ_DVD_STRUCTURE
:
854 case SEND_DVD_STRUCTURE
:
855 case MAINTENANCE_OUT
:
857 if (dev
->type
== TYPE_ROM
) {
858 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
859 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
866 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
869 /* stream commands */
876 case RECOVER_BUFFERED_DATA
:
879 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
880 if (buf
[1] & 0x01) { /* fixed */
881 cmd
->xfer
*= dev
->blocksize
;
885 case READ_REVERSE_16
:
889 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
890 if (buf
[1] & 0x01) { /* fixed */
891 cmd
->xfer
*= dev
->blocksize
;
900 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
903 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
906 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
908 /* generic commands */
910 return scsi_req_length(cmd
, dev
, buf
);
915 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
918 cmd
->mode
= SCSI_XFER_NONE
;
921 switch (cmd
->buf
[0]) {
924 case WRITE_VERIFY_10
:
926 case WRITE_VERIFY_12
:
928 case WRITE_VERIFY_16
:
932 case CHANGE_DEFINITION
:
936 case SEND_DIAGNOSTIC
:
939 case REASSIGN_BLOCKS
:
949 case SEARCH_EQUAL_12
:
952 case SEND_VOLUME_TAG
:
954 case SEND_DVD_STRUCTURE
:
955 case PERSISTENT_RESERVE_OUT
:
956 case MAINTENANCE_OUT
:
957 case ATA_PASSTHROUGH
:
958 cmd
->mode
= SCSI_XFER_TO_DEV
;
961 cmd
->mode
= SCSI_XFER_FROM_DEV
;
966 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
968 uint8_t *buf
= cmd
->buf
;
971 switch (buf
[0] >> 5) {
973 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
978 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
981 lba
= ldq_be_p(&buf
[2]);
990 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
994 if (dev
->type
== TYPE_TAPE
) {
995 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
997 rc
= scsi_req_length(cmd
, dev
, buf
);
1002 memcpy(cmd
->buf
, buf
, cmd
->len
);
1003 scsi_cmd_xfer_mode(cmd
);
1004 cmd
->lba
= scsi_cmd_lba(cmd
);
1009 * Predefined sense codes
1012 /* No sense data available */
1013 const struct SCSISense sense_code_NO_SENSE
= {
1014 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1017 /* LUN not ready, Manual intervention required */
1018 const struct SCSISense sense_code_LUN_NOT_READY
= {
1019 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1022 /* LUN not ready, Medium not present */
1023 const struct SCSISense sense_code_NO_MEDIUM
= {
1024 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1027 /* LUN not ready, medium removal prevented */
1028 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1029 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
1032 /* Hardware error, internal target failure */
1033 const struct SCSISense sense_code_TARGET_FAILURE
= {
1034 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1037 /* Illegal request, invalid command operation code */
1038 const struct SCSISense sense_code_INVALID_OPCODE
= {
1039 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1042 /* Illegal request, LBA out of range */
1043 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1044 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1047 /* Illegal request, Invalid field in CDB */
1048 const struct SCSISense sense_code_INVALID_FIELD
= {
1049 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1052 /* Illegal request, LUN not supported */
1053 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1054 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1057 /* Illegal request, Saving parameters not supported */
1058 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1059 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1062 /* Illegal request, Incompatible medium installed */
1063 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1064 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1067 /* Illegal request, medium removal prevented */
1068 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1069 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
1072 /* Command aborted, I/O process terminated */
1073 const struct SCSISense sense_code_IO_ERROR
= {
1074 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1077 /* Command aborted, I_T Nexus loss occurred */
1078 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1079 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1082 /* Command aborted, Logical Unit failure */
1083 const struct SCSISense sense_code_LUN_FAILURE
= {
1084 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1087 /* Unit attention, Power on, reset or bus device reset occurred */
1088 const struct SCSISense sense_code_RESET
= {
1089 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1092 /* Unit attention, No medium */
1093 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1094 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1097 /* Unit attention, Medium may have changed */
1098 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1099 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1102 /* Unit attention, Reported LUNs data has changed */
1103 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1104 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1107 /* Unit attention, Device internal reset */
1108 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1109 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1115 * Convert between fixed and descriptor sense buffers
1117 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1118 uint8_t *buf
, int len
, bool fixed
)
1122 if (!fixed
&& len
< 8) {
1127 sense
.key
= NO_SENSE
;
1131 fixed_in
= (in_buf
[0] & 2) == 0;
1133 if (fixed
== fixed_in
) {
1134 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1135 return MIN(len
, in_len
);
1139 sense
.key
= in_buf
[2];
1140 sense
.asc
= in_buf
[12];
1141 sense
.ascq
= in_buf
[13];
1143 sense
.key
= in_buf
[1];
1144 sense
.asc
= in_buf
[2];
1145 sense
.ascq
= in_buf
[3];
1149 memset(buf
, 0, len
);
1151 /* Return fixed format sense buffer */
1155 buf
[12] = sense
.asc
;
1156 buf
[13] = sense
.ascq
;
1157 return MIN(len
, 18);
1159 /* Return descriptor format sense buffer */
1163 buf
[3] = sense
.ascq
;
1168 static const char *scsi_command_name(uint8_t cmd
)
1170 static const char *names
[] = {
1171 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1172 [ REWIND
] = "REWIND",
1173 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1174 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1175 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1176 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1177 [ READ_6
] = "READ_6",
1178 [ WRITE_6
] = "WRITE_6",
1179 [ SET_CAPACITY
] = "SET_CAPACITY",
1180 [ READ_REVERSE
] = "READ_REVERSE",
1181 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1182 [ SPACE
] = "SPACE",
1183 [ INQUIRY
] = "INQUIRY",
1184 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1185 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1186 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1187 [ MODE_SELECT
] = "MODE_SELECT",
1188 [ RESERVE
] = "RESERVE",
1189 [ RELEASE
] = "RELEASE",
1191 [ ERASE
] = "ERASE",
1192 [ MODE_SENSE
] = "MODE_SENSE",
1193 [ START_STOP
] = "START_STOP",
1194 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1195 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1196 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1197 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1198 [ READ_10
] = "READ_10",
1199 [ WRITE_10
] = "WRITE_10",
1200 [ SEEK_10
] = "SEEK_10",
1201 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1202 [ VERIFY_10
] = "VERIFY_10",
1203 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1204 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1205 [ SEARCH_LOW
] = "SEARCH_LOW",
1206 [ SET_LIMITS
] = "SET_LIMITS",
1207 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1208 /* READ_POSITION and PRE_FETCH use the same operation code */
1209 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1210 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1211 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1212 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1213 [ COMPARE
] = "COMPARE",
1214 [ COPY_VERIFY
] = "COPY_VERIFY",
1215 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1216 [ READ_BUFFER
] = "READ_BUFFER",
1217 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1218 [ READ_LONG_10
] = "READ_LONG_10",
1219 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1220 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1221 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1222 [ UNMAP
] = "UNMAP",
1223 [ READ_TOC
] = "READ_TOC",
1224 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1225 [ SANITIZE
] = "SANITIZE",
1226 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1227 [ LOG_SELECT
] = "LOG_SELECT",
1228 [ LOG_SENSE
] = "LOG_SENSE",
1229 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1230 [ RESERVE_10
] = "RESERVE_10",
1231 [ RELEASE_10
] = "RELEASE_10",
1232 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1233 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1234 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1235 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1236 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1237 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1238 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1239 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1240 [ READ_16
] = "READ_16",
1241 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1242 [ WRITE_16
] = "WRITE_16",
1243 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1244 [ VERIFY_16
] = "VERIFY_16",
1245 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1246 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1247 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1248 [ LOCATE_16
] = "LOCATE_16",
1249 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1250 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1251 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1252 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1253 [ REPORT_LUNS
] = "REPORT_LUNS",
1254 [ BLANK
] = "BLANK",
1255 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1256 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1257 [ READ_12
] = "READ_12",
1258 [ WRITE_12
] = "WRITE_12",
1259 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1260 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1261 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1262 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1263 [ VERIFY_12
] = "VERIFY_12",
1264 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1265 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1266 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1267 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1268 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1269 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1270 [ READ_CD
] = "READ_CD",
1271 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1272 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1273 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1274 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1275 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1276 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1277 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1278 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1279 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1282 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1287 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1293 void scsi_req_unref(SCSIRequest
*req
)
1295 assert(req
->refcount
> 0);
1296 if (--req
->refcount
== 0) {
1297 if (req
->ops
->free_req
) {
1298 req
->ops
->free_req(req
);
1304 /* Tell the device that we finished processing this chunk of I/O. It
1305 will start the next chunk or complete the command. */
1306 void scsi_req_continue(SCSIRequest
*req
)
1308 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1309 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1310 req
->ops
->write_data(req
);
1312 req
->ops
->read_data(req
);
1316 /* Called by the devices when data is ready for the HBA. The HBA should
1317 start a DMA operation to read or fill the device's data buffer.
1318 Once it completes, calling scsi_req_continue will restart I/O. */
1319 void scsi_req_data(SCSIRequest
*req
, int len
)
1322 if (req
->io_canceled
) {
1323 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1326 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1327 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1330 req
->bus
->info
->transfer_data(req
, len
);
1334 /* If the device calls scsi_req_data and the HBA specified a
1335 * scatter/gather list, the transfer has to happen in a single
1337 assert(!req
->dma_started
);
1338 req
->dma_started
= true;
1340 buf
= scsi_req_get_buf(req
);
1341 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1342 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1344 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1346 scsi_req_continue(req
);
1349 void scsi_req_print(SCSIRequest
*req
)
1354 fprintf(fp
, "[%s id=%d] %s",
1355 req
->dev
->qdev
.parent_bus
->name
,
1357 scsi_command_name(req
->cmd
.buf
[0]));
1358 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1359 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1361 switch (req
->cmd
.mode
) {
1362 case SCSI_XFER_NONE
:
1363 fprintf(fp
, " - none\n");
1365 case SCSI_XFER_FROM_DEV
:
1366 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1368 case SCSI_XFER_TO_DEV
:
1369 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1372 fprintf(fp
, " - Oops\n");
1377 void scsi_req_complete(SCSIRequest
*req
, int status
)
1379 assert(req
->status
== -1);
1380 req
->status
= status
;
1382 assert(req
->sense_len
< sizeof(req
->sense
));
1383 if (status
== GOOD
) {
1387 if (req
->sense_len
) {
1388 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1389 req
->dev
->sense_len
= req
->sense_len
;
1390 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1392 req
->dev
->sense_len
= 0;
1393 req
->dev
->sense_is_ua
= false;
1397 * Unit attention state is now stored in the device's sense buffer
1398 * if the HBA didn't do autosense. Clear the pending unit attention
1401 scsi_clear_unit_attention(req
);
1404 scsi_req_dequeue(req
);
1405 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1406 scsi_req_unref(req
);
1409 void scsi_req_cancel(SCSIRequest
*req
)
1411 if (!req
->enqueued
) {
1415 scsi_req_dequeue(req
);
1416 req
->io_canceled
= true;
1417 if (req
->ops
->cancel_io
) {
1418 req
->ops
->cancel_io(req
);
1420 if (req
->bus
->info
->cancel
) {
1421 req
->bus
->info
->cancel(req
);
1423 scsi_req_unref(req
);
1426 void scsi_req_abort(SCSIRequest
*req
, int status
)
1428 if (!req
->enqueued
) {
1432 scsi_req_dequeue(req
);
1433 req
->io_canceled
= true;
1434 if (req
->ops
->cancel_io
) {
1435 req
->ops
->cancel_io(req
);
1437 scsi_req_complete(req
, status
);
1438 scsi_req_unref(req
);
1441 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1445 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1446 req
= QTAILQ_FIRST(&sdev
->requests
);
1447 scsi_req_cancel(req
);
1449 sdev
->unit_attention
= sense
;
1452 static char *scsibus_get_dev_path(DeviceState
*dev
)
1454 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1455 DeviceState
*hba
= dev
->parent_bus
->parent
;
1459 id
= qdev_get_dev_path(hba
);
1461 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1463 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1469 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1471 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1474 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1475 qdev_fw_name(dev
), d
->id
, d
->lun
);
1477 return strdup(path
);
1480 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1483 SCSIDevice
*target_dev
= NULL
;
1485 QTAILQ_FOREACH_REVERSE(qdev
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1486 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1488 if (dev
->channel
== channel
&& dev
->id
== id
) {
1489 if (dev
->lun
== lun
) {
1498 /* SCSI request list. For simplicity, pv points to the whole device */
1500 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1503 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1506 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1507 assert(!req
->io_canceled
);
1508 assert(req
->status
== -1);
1509 assert(req
->enqueued
);
1511 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1512 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1513 qemu_put_be32s(f
, &req
->tag
);
1514 qemu_put_be32s(f
, &req
->lun
);
1515 if (bus
->info
->save_request
) {
1516 bus
->info
->save_request(f
, req
);
1518 if (req
->ops
->save_request
) {
1519 req
->ops
->save_request(f
, req
);
1522 qemu_put_sbyte(f
, 0);
1525 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1528 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1531 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1532 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1537 qemu_get_buffer(f
, buf
, sizeof(buf
));
1538 qemu_get_be32s(f
, &tag
);
1539 qemu_get_be32s(f
, &lun
);
1540 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1541 req
->retry
= (sbyte
== 1);
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. */
1550 scsi_req_enqueue_internal(req
);
1552 /* At this point, the request will be kept alive by the reference
1553 * added by scsi_req_enqueue_internal, so we can release our reference.
1554 * The HBA of course will add its own reference in the load_request
1555 * callback if it needs to hold on the SCSIRequest.
1557 scsi_req_unref(req
);
1563 static const VMStateInfo vmstate_info_scsi_requests
= {
1564 .name
= "scsi-requests",
1565 .get
= get_scsi_requests
,
1566 .put
= put_scsi_requests
,
1569 const VMStateDescription vmstate_scsi_device
= {
1570 .name
= "SCSIDevice",
1572 .minimum_version_id
= 1,
1573 .minimum_version_id_old
= 1,
1574 .fields
= (VMStateField
[]) {
1575 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1576 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1577 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1578 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1579 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1580 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1584 .field_exists
= NULL
,
1585 .size
= 0, /* ouch */
1586 .info
= &vmstate_info_scsi_requests
,
1587 .flags
= VMS_SINGLE
,
1590 VMSTATE_END_OF_LIST()
1594 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1596 DeviceClass
*k
= DEVICE_CLASS(klass
);
1597 k
->bus_info
= &scsi_bus_info
;
1598 k
->init
= scsi_qdev_init
;
1599 k
->unplug
= qdev_simple_unplug_cb
;
1600 k
->exit
= scsi_qdev_exit
;
1601 k
->props
= scsi_props
;
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
)