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 void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
24 BusClass
*k
= BUS_CLASS(klass
);
26 k
->get_dev_path
= scsibus_get_dev_path
;
27 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
30 static const TypeInfo scsi_bus_info
= {
31 .name
= TYPE_SCSI_BUS
,
33 .instance_size
= sizeof(SCSIBus
),
34 .class_init
= scsi_bus_class_init
,
36 static int next_scsi_bus
;
38 static int scsi_device_init(SCSIDevice
*s
)
40 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
47 static void scsi_device_destroy(SCSIDevice
*s
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
55 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
56 uint8_t *buf
, void *hba_private
)
58 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
60 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
66 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
68 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
69 if (sc
->unit_attention_reported
) {
70 sc
->unit_attention_reported(s
);
74 /* Create a scsi bus, and attach devices to it. */
75 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
77 qbus_create_inplace(&bus
->qbus
, TYPE_SCSI_BUS
, host
, NULL
);
78 bus
->busnr
= next_scsi_bus
++;
80 bus
->qbus
.allow_hotplug
= 1;
83 static void scsi_dma_restart_bh(void *opaque
)
85 SCSIDevice
*s
= opaque
;
86 SCSIRequest
*req
, *next
;
88 qemu_bh_delete(s
->bh
);
91 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
95 switch (req
->cmd
.mode
) {
96 case SCSI_XFER_FROM_DEV
:
97 case SCSI_XFER_TO_DEV
:
98 scsi_req_continue(req
);
102 scsi_req_dequeue(req
);
103 scsi_req_enqueue(req
);
111 void scsi_req_retry(SCSIRequest
*req
)
113 /* No need to save a reference, because scsi_dma_restart_bh just
114 * looks at the request list. */
118 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
120 SCSIDevice
*s
= opaque
;
126 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
127 qemu_bh_schedule(s
->bh
);
131 static int scsi_qdev_init(DeviceState
*qdev
)
133 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
134 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
138 if (dev
->channel
> bus
->info
->max_channel
) {
139 error_report("bad scsi channel id: %d", dev
->channel
);
142 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
143 error_report("bad scsi device id: %d", dev
->id
);
146 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
147 error_report("bad scsi device lun: %d", dev
->lun
);
153 if (dev
->lun
== -1) {
157 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
158 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
159 if (d
&& d
->lun
== dev
->lun
) {
160 error_report("no free target");
164 } else if (dev
->lun
== -1) {
167 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
168 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
169 if (d
&& d
->lun
== lun
) {
170 error_report("no free lun");
175 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
177 if (d
->lun
== dev
->lun
&& dev
!= d
) {
182 QTAILQ_INIT(&dev
->requests
);
183 rc
= scsi_device_init(dev
);
185 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
193 static int scsi_qdev_exit(DeviceState
*qdev
)
195 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
198 qemu_del_vm_change_state_handler(dev
->vmsentry
);
200 scsi_device_destroy(dev
);
204 /* handle legacy '-drive if=scsi,...' cmd line args */
205 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
206 int unit
, bool removable
, int bootindex
)
211 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
212 dev
= qdev_create(&bus
->qbus
, driver
);
213 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
214 if (bootindex
>= 0) {
215 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
217 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
218 qdev_prop_set_bit(dev
, "removable", removable
);
220 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
224 if (qdev_init(dev
) < 0)
226 return SCSI_DEVICE(dev
);
229 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
236 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
237 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
241 qemu_opts_loc_restore(dinfo
->opts
);
242 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
251 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
253 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
254 scsi_req_complete(req
, CHECK_CONDITION
);
258 static const struct SCSIReqOps reqops_invalid_field
= {
259 .size
= sizeof(SCSIRequest
),
260 .send_command
= scsi_invalid_field
263 /* SCSIReqOps implementation for invalid commands. */
265 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
267 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
268 scsi_req_complete(req
, CHECK_CONDITION
);
272 static const struct SCSIReqOps reqops_invalid_opcode
= {
273 .size
= sizeof(SCSIRequest
),
274 .send_command
= scsi_invalid_command
277 /* SCSIReqOps implementation for unit attention conditions. */
279 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
281 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
282 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
283 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
284 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
286 scsi_req_complete(req
, CHECK_CONDITION
);
290 static const struct SCSIReqOps reqops_unit_attention
= {
291 .size
= sizeof(SCSIRequest
),
292 .send_command
= scsi_unit_attention
295 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
298 typedef struct SCSITargetReq SCSITargetReq
;
300 struct SCSITargetReq
{
306 static void store_lun(uint8_t *outbuf
, int lun
)
312 outbuf
[1] = (lun
& 255);
313 outbuf
[0] = (lun
>> 8) | 0x40;
316 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
323 if (r
->req
.cmd
.xfer
< 16) {
326 if (r
->req
.cmd
.buf
[2] > 2) {
329 channel
= r
->req
.dev
->channel
;
333 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
334 DeviceState
*qdev
= kid
->child
;
335 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
337 if (dev
->channel
== channel
&& dev
->id
== id
) {
347 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
348 if (len
> sizeof(r
->buf
)) {
349 /* TODO: > 256 LUNs? */
353 memset(r
->buf
, 0, len
);
354 stl_be_p(&r
->buf
, n
);
355 i
= found_lun0
? 8 : 16;
356 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
357 DeviceState
*qdev
= kid
->child
;
358 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
360 if (dev
->channel
== channel
&& dev
->id
== id
) {
361 store_lun(&r
->buf
[i
], dev
->lun
);
370 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
372 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
373 if (r
->req
.cmd
.buf
[1] & 0x2) {
374 /* Command support data - optional, not implemented */
378 if (r
->req
.cmd
.buf
[1] & 0x1) {
379 /* Vital product data */
380 uint8_t page_code
= r
->req
.cmd
.buf
[2];
381 r
->buf
[r
->len
++] = page_code
; /* this page */
382 r
->buf
[r
->len
++] = 0x00;
385 case 0x00: /* Supported page codes, mandatory */
389 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
390 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
397 assert(r
->len
< sizeof(r
->buf
));
398 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
402 /* Standard INQUIRY data */
403 if (r
->req
.cmd
.buf
[2] != 0) {
408 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
409 memset(r
->buf
, 0, r
->len
);
410 if (r
->req
.lun
!= 0) {
411 r
->buf
[0] = TYPE_NO_LUN
;
413 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
414 r
->buf
[2] = 5; /* Version */
415 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
416 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
417 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
418 memcpy(&r
->buf
[8], "QEMU ", 8);
419 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
420 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
425 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
427 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
431 if (!scsi_target_emulate_report_luns(r
)) {
432 goto illegal_request
;
436 if (!scsi_target_emulate_inquiry(r
)) {
437 goto illegal_request
;
441 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
442 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
443 (req
->cmd
.buf
[1] & 1) == 0);
444 if (r
->req
.dev
->sense_is_ua
) {
445 scsi_device_unit_attention_reported(req
->dev
);
446 r
->req
.dev
->sense_len
= 0;
447 r
->req
.dev
->sense_is_ua
= false;
451 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
452 scsi_req_complete(req
, CHECK_CONDITION
);
455 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
456 scsi_req_complete(req
, CHECK_CONDITION
);
461 scsi_req_complete(req
, GOOD
);
466 static void scsi_target_read_data(SCSIRequest
*req
)
468 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
474 scsi_req_data(&r
->req
, n
);
476 scsi_req_complete(&r
->req
, GOOD
);
480 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
482 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
487 static const struct SCSIReqOps reqops_target_command
= {
488 .size
= sizeof(SCSITargetReq
),
489 .send_command
= scsi_target_send_command
,
490 .read_data
= scsi_target_read_data
,
491 .get_buf
= scsi_target_get_buf
,
495 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
496 uint32_t tag
, uint32_t lun
, void *hba_private
)
500 req
= g_malloc0(reqops
->size
);
502 req
->bus
= scsi_bus_from_device(d
);
506 req
->hba_private
= hba_private
;
510 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
514 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
515 uint8_t *buf
, void *hba_private
)
517 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
521 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
522 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
523 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
525 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
528 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
532 if (cmd
.xfer
> INT32_MAX
) {
533 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
534 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
535 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
536 (buf
[0] != INQUIRY
&&
537 buf
[0] != REPORT_LUNS
&&
538 buf
[0] != GET_CONFIGURATION
&&
539 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
542 * If we already have a pending unit attention condition,
543 * report this one before triggering another one.
545 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
546 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
548 } else if (lun
!= d
->lun
||
549 buf
[0] == REPORT_LUNS
||
550 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
551 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
554 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
559 req
->resid
= req
->cmd
.xfer
;
563 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
565 case TEST_UNIT_READY
:
566 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
569 trace_scsi_report_luns(d
->id
, lun
, tag
);
572 trace_scsi_request_sense(d
->id
, lun
, tag
);
581 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
583 return req
->ops
->get_buf(req
);
586 static void scsi_clear_unit_attention(SCSIRequest
*req
)
589 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
590 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
595 * If an INQUIRY command enters the enabled command state,
596 * the device server shall [not] clear any unit attention condition;
597 * See also MMC-6, paragraphs 6.5 and 6.6.2.
599 if (req
->cmd
.buf
[0] == INQUIRY
||
600 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
601 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
605 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
606 ua
= &req
->dev
->unit_attention
;
608 ua
= &req
->bus
->unit_attention
;
612 * If a REPORT LUNS command enters the enabled command state, [...]
613 * the device server shall clear any pending unit attention condition
614 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
616 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
617 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
618 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
622 *ua
= SENSE_CODE(NO_SENSE
);
625 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
630 if (!req
->sense_len
) {
634 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
637 * FIXME: clearing unit attention conditions upon autosense should be done
638 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
641 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
642 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
643 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
645 if (req
->dev
->sense_is_ua
) {
646 scsi_device_unit_attention_reported(req
->dev
);
647 req
->dev
->sense_len
= 0;
648 req
->dev
->sense_is_ua
= false;
653 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
655 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
658 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
660 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
661 sense
.key
, sense
.asc
, sense
.ascq
);
662 memset(req
->sense
, 0, 18);
663 req
->sense
[0] = 0x70;
664 req
->sense
[2] = sense
.key
;
666 req
->sense
[12] = sense
.asc
;
667 req
->sense
[13] = sense
.ascq
;
671 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
673 assert(!req
->enqueued
);
675 if (req
->bus
->info
->get_sg_list
) {
676 req
->sg
= req
->bus
->info
->get_sg_list(req
);
680 req
->enqueued
= true;
681 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
684 int32_t scsi_req_enqueue(SCSIRequest
*req
)
689 scsi_req_enqueue_internal(req
);
691 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
696 static void scsi_req_dequeue(SCSIRequest
*req
)
698 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
701 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
702 req
->enqueued
= false;
707 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
709 /* MMC-6, paragraph 6.7. */
712 if ((data_type
& 3) == 0) {
713 /* Each descriptor is as in Table 295 - Nominal performance. */
714 return 16 * num_desc
+ 8;
716 /* Each descriptor is as in Table 296 - Exceptions. */
717 return 6 * num_desc
+ 8;
722 return 8 * num_desc
+ 8;
724 return 2048 * num_desc
+ 8;
726 return 16 * num_desc
+ 8;
732 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
734 switch (buf
[0] >> 5) {
741 cmd
->xfer
= lduw_be_p(&buf
[7]);
745 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
749 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
757 case TEST_UNIT_READY
:
761 case WRITE_FILEMARKS
:
762 case WRITE_FILEMARKS_16
:
767 case ALLOW_MEDIUM_REMOVAL
:
770 case SYNCHRONIZE_CACHE
:
771 case SYNCHRONIZE_CACHE_16
:
773 case LOCK_UNLOCK_CACHE
:
784 case ALLOW_OVERWRITE
:
791 cmd
->xfer
= dev
->blocksize
;
793 case READ_CAPACITY_10
:
796 case READ_BLOCK_LIMITS
:
799 case SEND_VOLUME_TAG
:
800 /* GPCMD_SET_STREAMING from multimedia commands. */
801 if (dev
->type
== TYPE_ROM
) {
802 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
804 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
808 /* length 0 means 256 blocks */
809 if (cmd
->xfer
== 0) {
813 case WRITE_VERIFY_10
:
815 case WRITE_VERIFY_12
:
817 case WRITE_VERIFY_16
:
818 cmd
->xfer
*= dev
->blocksize
;
822 /* length 0 means 256 blocks */
823 if (cmd
->xfer
== 0) {
827 case RECOVER_BUFFERED_DATA
:
830 cmd
->xfer
*= dev
->blocksize
;
833 /* MMC mandates the parameter list to be 12-bytes long. Parameters
834 * for block devices are restricted to the header right now. */
835 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
838 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
842 case RECEIVE_DIAGNOSTIC
:
843 case SEND_DIAGNOSTIC
:
844 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
850 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
852 case PERSISTENT_RESERVE_OUT
:
853 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
856 if (dev
->type
== TYPE_ROM
) {
857 /* MMC command GET PERFORMANCE. */
858 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
859 buf
[10], buf
[1] & 0x1f);
862 case MECHANISM_STATUS
:
863 case READ_DVD_STRUCTURE
:
864 case SEND_DVD_STRUCTURE
:
865 case MAINTENANCE_OUT
:
867 if (dev
->type
== TYPE_ROM
) {
868 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
869 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
876 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
879 /* stream commands */
886 case RECOVER_BUFFERED_DATA
:
889 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
890 if (buf
[1] & 0x01) { /* fixed */
891 cmd
->xfer
*= dev
->blocksize
;
895 case READ_REVERSE_16
:
899 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
900 if (buf
[1] & 0x01) { /* fixed */
901 cmd
->xfer
*= dev
->blocksize
;
910 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
913 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
916 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
918 /* generic commands */
920 return scsi_req_length(cmd
, dev
, buf
);
925 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
928 cmd
->mode
= SCSI_XFER_NONE
;
931 switch (cmd
->buf
[0]) {
934 case WRITE_VERIFY_10
:
936 case WRITE_VERIFY_12
:
938 case WRITE_VERIFY_16
:
942 case CHANGE_DEFINITION
:
946 case SEND_DIAGNOSTIC
:
949 case REASSIGN_BLOCKS
:
959 case SEARCH_EQUAL_12
:
962 case SEND_VOLUME_TAG
:
964 case SEND_DVD_STRUCTURE
:
965 case PERSISTENT_RESERVE_OUT
:
966 case MAINTENANCE_OUT
:
967 case ATA_PASSTHROUGH
:
968 cmd
->mode
= SCSI_XFER_TO_DEV
;
971 cmd
->mode
= SCSI_XFER_FROM_DEV
;
976 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
978 uint8_t *buf
= cmd
->buf
;
981 switch (buf
[0] >> 5) {
983 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
988 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
991 lba
= ldq_be_p(&buf
[2]);
1000 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1004 if (dev
->type
== TYPE_TAPE
) {
1005 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1007 rc
= scsi_req_length(cmd
, dev
, buf
);
1012 memcpy(cmd
->buf
, buf
, cmd
->len
);
1013 scsi_cmd_xfer_mode(cmd
);
1014 cmd
->lba
= scsi_cmd_lba(cmd
);
1019 * Predefined sense codes
1022 /* No sense data available */
1023 const struct SCSISense sense_code_NO_SENSE
= {
1024 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1027 /* LUN not ready, Manual intervention required */
1028 const struct SCSISense sense_code_LUN_NOT_READY
= {
1029 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1032 /* LUN not ready, Medium not present */
1033 const struct SCSISense sense_code_NO_MEDIUM
= {
1034 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1037 /* LUN not ready, medium removal prevented */
1038 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1039 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
1042 /* Hardware error, internal target failure */
1043 const struct SCSISense sense_code_TARGET_FAILURE
= {
1044 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1047 /* Illegal request, invalid command operation code */
1048 const struct SCSISense sense_code_INVALID_OPCODE
= {
1049 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1052 /* Illegal request, LBA out of range */
1053 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1054 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1057 /* Illegal request, Invalid field in CDB */
1058 const struct SCSISense sense_code_INVALID_FIELD
= {
1059 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1062 /* Illegal request, LUN not supported */
1063 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1064 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1067 /* Illegal request, Saving parameters not supported */
1068 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1069 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1072 /* Illegal request, Incompatible medium installed */
1073 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1074 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1077 /* Illegal request, medium removal prevented */
1078 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1079 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
1082 /* Command aborted, I/O process terminated */
1083 const struct SCSISense sense_code_IO_ERROR
= {
1084 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1087 /* Command aborted, I_T Nexus loss occurred */
1088 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1089 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1092 /* Command aborted, Logical Unit failure */
1093 const struct SCSISense sense_code_LUN_FAILURE
= {
1094 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1097 /* Unit attention, Power on, reset or bus device reset occurred */
1098 const struct SCSISense sense_code_RESET
= {
1099 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1102 /* Unit attention, No medium */
1103 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1104 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1107 /* Unit attention, Medium may have changed */
1108 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1109 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1112 /* Unit attention, Reported LUNs data has changed */
1113 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1114 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1117 /* Unit attention, Device internal reset */
1118 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1119 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1125 * Convert between fixed and descriptor sense buffers
1127 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1128 uint8_t *buf
, int len
, bool fixed
)
1132 if (!fixed
&& len
< 8) {
1137 sense
.key
= NO_SENSE
;
1141 fixed_in
= (in_buf
[0] & 2) == 0;
1143 if (fixed
== fixed_in
) {
1144 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1145 return MIN(len
, in_len
);
1149 sense
.key
= in_buf
[2];
1150 sense
.asc
= in_buf
[12];
1151 sense
.ascq
= in_buf
[13];
1153 sense
.key
= in_buf
[1];
1154 sense
.asc
= in_buf
[2];
1155 sense
.ascq
= in_buf
[3];
1159 memset(buf
, 0, len
);
1161 /* Return fixed format sense buffer */
1165 buf
[12] = sense
.asc
;
1166 buf
[13] = sense
.ascq
;
1167 return MIN(len
, 18);
1169 /* Return descriptor format sense buffer */
1173 buf
[3] = sense
.ascq
;
1178 static const char *scsi_command_name(uint8_t cmd
)
1180 static const char *names
[] = {
1181 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1182 [ REWIND
] = "REWIND",
1183 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1184 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1185 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1186 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1187 [ READ_6
] = "READ_6",
1188 [ WRITE_6
] = "WRITE_6",
1189 [ SET_CAPACITY
] = "SET_CAPACITY",
1190 [ READ_REVERSE
] = "READ_REVERSE",
1191 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1192 [ SPACE
] = "SPACE",
1193 [ INQUIRY
] = "INQUIRY",
1194 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1195 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1196 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1197 [ MODE_SELECT
] = "MODE_SELECT",
1198 [ RESERVE
] = "RESERVE",
1199 [ RELEASE
] = "RELEASE",
1201 [ ERASE
] = "ERASE",
1202 [ MODE_SENSE
] = "MODE_SENSE",
1203 [ START_STOP
] = "START_STOP",
1204 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1205 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1206 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1207 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1208 [ READ_10
] = "READ_10",
1209 [ WRITE_10
] = "WRITE_10",
1210 [ SEEK_10
] = "SEEK_10",
1211 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1212 [ VERIFY_10
] = "VERIFY_10",
1213 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1214 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1215 [ SEARCH_LOW
] = "SEARCH_LOW",
1216 [ SET_LIMITS
] = "SET_LIMITS",
1217 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1218 /* READ_POSITION and PRE_FETCH use the same operation code */
1219 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1220 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1221 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1222 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1223 [ COMPARE
] = "COMPARE",
1224 [ COPY_VERIFY
] = "COPY_VERIFY",
1225 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1226 [ READ_BUFFER
] = "READ_BUFFER",
1227 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1228 [ READ_LONG_10
] = "READ_LONG_10",
1229 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1230 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1231 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1232 [ UNMAP
] = "UNMAP",
1233 [ READ_TOC
] = "READ_TOC",
1234 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1235 [ SANITIZE
] = "SANITIZE",
1236 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1237 [ LOG_SELECT
] = "LOG_SELECT",
1238 [ LOG_SENSE
] = "LOG_SENSE",
1239 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1240 [ RESERVE_10
] = "RESERVE_10",
1241 [ RELEASE_10
] = "RELEASE_10",
1242 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1243 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1244 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1245 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1246 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1247 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1248 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1249 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1250 [ READ_16
] = "READ_16",
1251 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1252 [ WRITE_16
] = "WRITE_16",
1253 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1254 [ VERIFY_16
] = "VERIFY_16",
1255 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1256 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1257 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1258 [ LOCATE_16
] = "LOCATE_16",
1259 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1260 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1261 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1262 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1263 [ REPORT_LUNS
] = "REPORT_LUNS",
1264 [ BLANK
] = "BLANK",
1265 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1266 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1267 [ READ_12
] = "READ_12",
1268 [ WRITE_12
] = "WRITE_12",
1269 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1270 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1271 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1272 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1273 [ VERIFY_12
] = "VERIFY_12",
1274 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1275 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1276 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1277 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1278 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1279 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1280 [ READ_CD
] = "READ_CD",
1281 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1282 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1283 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1284 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1285 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1286 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1287 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1288 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1289 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1292 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1297 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1303 void scsi_req_unref(SCSIRequest
*req
)
1305 assert(req
->refcount
> 0);
1306 if (--req
->refcount
== 0) {
1307 if (req
->ops
->free_req
) {
1308 req
->ops
->free_req(req
);
1314 /* Tell the device that we finished processing this chunk of I/O. It
1315 will start the next chunk or complete the command. */
1316 void scsi_req_continue(SCSIRequest
*req
)
1318 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1319 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1320 req
->ops
->write_data(req
);
1322 req
->ops
->read_data(req
);
1326 /* Called by the devices when data is ready for the HBA. The HBA should
1327 start a DMA operation to read or fill the device's data buffer.
1328 Once it completes, calling scsi_req_continue will restart I/O. */
1329 void scsi_req_data(SCSIRequest
*req
, int len
)
1332 if (req
->io_canceled
) {
1333 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1336 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1337 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1340 req
->bus
->info
->transfer_data(req
, len
);
1344 /* If the device calls scsi_req_data and the HBA specified a
1345 * scatter/gather list, the transfer has to happen in a single
1347 assert(!req
->dma_started
);
1348 req
->dma_started
= true;
1350 buf
= scsi_req_get_buf(req
);
1351 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1352 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1354 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1356 scsi_req_continue(req
);
1359 void scsi_req_print(SCSIRequest
*req
)
1364 fprintf(fp
, "[%s id=%d] %s",
1365 req
->dev
->qdev
.parent_bus
->name
,
1367 scsi_command_name(req
->cmd
.buf
[0]));
1368 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1369 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1371 switch (req
->cmd
.mode
) {
1372 case SCSI_XFER_NONE
:
1373 fprintf(fp
, " - none\n");
1375 case SCSI_XFER_FROM_DEV
:
1376 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1378 case SCSI_XFER_TO_DEV
:
1379 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1382 fprintf(fp
, " - Oops\n");
1387 void scsi_req_complete(SCSIRequest
*req
, int status
)
1389 assert(req
->status
== -1);
1390 req
->status
= status
;
1392 assert(req
->sense_len
< sizeof(req
->sense
));
1393 if (status
== GOOD
) {
1397 if (req
->sense_len
) {
1398 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1399 req
->dev
->sense_len
= req
->sense_len
;
1400 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1402 req
->dev
->sense_len
= 0;
1403 req
->dev
->sense_is_ua
= false;
1407 * Unit attention state is now stored in the device's sense buffer
1408 * if the HBA didn't do autosense. Clear the pending unit attention
1411 scsi_clear_unit_attention(req
);
1414 scsi_req_dequeue(req
);
1415 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1416 scsi_req_unref(req
);
1419 void scsi_req_cancel(SCSIRequest
*req
)
1421 if (!req
->enqueued
) {
1425 scsi_req_dequeue(req
);
1426 req
->io_canceled
= true;
1427 if (req
->ops
->cancel_io
) {
1428 req
->ops
->cancel_io(req
);
1430 if (req
->bus
->info
->cancel
) {
1431 req
->bus
->info
->cancel(req
);
1433 scsi_req_unref(req
);
1436 void scsi_req_abort(SCSIRequest
*req
, int status
)
1438 if (!req
->enqueued
) {
1442 scsi_req_dequeue(req
);
1443 req
->io_canceled
= true;
1444 if (req
->ops
->cancel_io
) {
1445 req
->ops
->cancel_io(req
);
1447 scsi_req_complete(req
, status
);
1448 scsi_req_unref(req
);
1451 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1455 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1456 req
= QTAILQ_FIRST(&sdev
->requests
);
1457 scsi_req_cancel(req
);
1459 sdev
->unit_attention
= sense
;
1462 static char *scsibus_get_dev_path(DeviceState
*dev
)
1464 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1465 DeviceState
*hba
= dev
->parent_bus
->parent
;
1469 id
= qdev_get_dev_path(hba
);
1471 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1473 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1479 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1481 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1484 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1485 qdev_fw_name(dev
), d
->id
, d
->lun
);
1487 return strdup(path
);
1490 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1493 SCSIDevice
*target_dev
= NULL
;
1495 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1496 DeviceState
*qdev
= kid
->child
;
1497 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1499 if (dev
->channel
== channel
&& dev
->id
== id
) {
1500 if (dev
->lun
== lun
) {
1509 /* SCSI request list. For simplicity, pv points to the whole device */
1511 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1514 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1517 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1518 assert(!req
->io_canceled
);
1519 assert(req
->status
== -1);
1520 assert(req
->enqueued
);
1522 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1523 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1524 qemu_put_be32s(f
, &req
->tag
);
1525 qemu_put_be32s(f
, &req
->lun
);
1526 if (bus
->info
->save_request
) {
1527 bus
->info
->save_request(f
, req
);
1529 if (req
->ops
->save_request
) {
1530 req
->ops
->save_request(f
, req
);
1533 qemu_put_sbyte(f
, 0);
1536 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1539 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1542 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1543 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1548 qemu_get_buffer(f
, buf
, sizeof(buf
));
1549 qemu_get_be32s(f
, &tag
);
1550 qemu_get_be32s(f
, &lun
);
1551 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1552 req
->retry
= (sbyte
== 1);
1553 if (bus
->info
->load_request
) {
1554 req
->hba_private
= bus
->info
->load_request(f
, req
);
1556 if (req
->ops
->load_request
) {
1557 req
->ops
->load_request(f
, req
);
1560 /* Just restart it later. */
1561 scsi_req_enqueue_internal(req
);
1563 /* At this point, the request will be kept alive by the reference
1564 * added by scsi_req_enqueue_internal, so we can release our reference.
1565 * The HBA of course will add its own reference in the load_request
1566 * callback if it needs to hold on the SCSIRequest.
1568 scsi_req_unref(req
);
1574 static const VMStateInfo vmstate_info_scsi_requests
= {
1575 .name
= "scsi-requests",
1576 .get
= get_scsi_requests
,
1577 .put
= put_scsi_requests
,
1580 const VMStateDescription vmstate_scsi_device
= {
1581 .name
= "SCSIDevice",
1583 .minimum_version_id
= 1,
1584 .minimum_version_id_old
= 1,
1585 .fields
= (VMStateField
[]) {
1586 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1587 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1588 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1589 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1590 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1591 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1595 .field_exists
= NULL
,
1596 .size
= 0, /* ouch */
1597 .info
= &vmstate_info_scsi_requests
,
1598 .flags
= VMS_SINGLE
,
1601 VMSTATE_END_OF_LIST()
1605 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1607 DeviceClass
*k
= DEVICE_CLASS(klass
);
1608 k
->bus_type
= TYPE_SCSI_BUS
;
1609 k
->init
= scsi_qdev_init
;
1610 k
->unplug
= qdev_simple_unplug_cb
;
1611 k
->exit
= scsi_qdev_exit
;
1612 k
->props
= scsi_props
;
1615 static TypeInfo scsi_device_type_info
= {
1616 .name
= TYPE_SCSI_DEVICE
,
1617 .parent
= TYPE_DEVICE
,
1618 .instance_size
= sizeof(SCSIDevice
),
1620 .class_size
= sizeof(SCSIDeviceClass
),
1621 .class_init
= scsi_device_class_init
,
1624 static void scsi_register_types(void)
1626 type_register_static(&scsi_bus_info
);
1627 type_register_static(&scsi_device_type_info
);
1630 type_init(scsi_register_types
)