1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qemu/error-report.h"
4 #include "qemu/module.h"
5 #include "qemu/option.h"
6 #include "qemu/hw-version.h"
7 #include "hw/qdev-properties.h"
8 #include "hw/scsi/scsi.h"
9 #include "migration/qemu-file-types.h"
10 #include "migration/vmstate.h"
11 #include "scsi/constants.h"
12 #include "sysemu/block-backend.h"
13 #include "sysemu/blockdev.h"
14 #include "sysemu/sysemu.h"
15 #include "sysemu/runstate.h"
17 #include "sysemu/dma.h"
18 #include "qemu/cutils.h"
20 static char *scsibus_get_dev_path(DeviceState
*dev
);
21 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
22 static void scsi_req_dequeue(SCSIRequest
*req
);
23 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
24 static void scsi_target_free_buf(SCSIRequest
*req
);
26 static int next_scsi_bus
;
28 static SCSIDevice
*do_scsi_device_find(SCSIBus
*bus
,
29 int channel
, int id
, int lun
,
30 bool include_unrealized
)
33 SCSIDevice
*retval
= NULL
;
35 QTAILQ_FOREACH_RCU(kid
, &bus
->qbus
.children
, sibling
) {
36 DeviceState
*qdev
= kid
->child
;
37 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
39 if (dev
->channel
== channel
&& dev
->id
== id
) {
40 if (dev
->lun
== lun
) {
46 * If we don't find exact match (channel/bus/lun),
47 * we will return the first device which matches channel/bus
57 * This function might run on the IO thread and we might race against
58 * main thread hot-plugging the device.
59 * We assume that as soon as .realized is set to true we can let
60 * the user access the device.
63 if (retval
&& !include_unrealized
&&
64 !qatomic_load_acquire(&retval
->qdev
.realized
)) {
71 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
73 RCU_READ_LOCK_GUARD();
74 return do_scsi_device_find(bus
, channel
, id
, lun
, false);
77 SCSIDevice
*scsi_device_get(SCSIBus
*bus
, int channel
, int id
, int lun
)
80 RCU_READ_LOCK_GUARD();
81 d
= do_scsi_device_find(bus
, channel
, id
, lun
, false);
88 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
90 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
96 static void scsi_device_unrealize(SCSIDevice
*s
)
98 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
104 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
107 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
110 assert(cmd
->len
== 0);
111 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
112 if (bus
->info
->parse_cdb
) {
113 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
118 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
119 uint8_t *buf
, void *hba_private
)
121 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
123 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
129 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
131 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
132 if (sc
->unit_attention_reported
) {
133 sc
->unit_attention_reported(s
);
137 /* Create a scsi bus, and attach devices to it. */
138 void scsi_bus_init_named(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
139 const SCSIBusInfo
*info
, const char *bus_name
)
141 qbus_init(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
142 bus
->busnr
= next_scsi_bus
++;
144 qbus_set_bus_hotplug_handler(BUS(bus
));
147 static void scsi_dma_restart_bh(void *opaque
)
149 SCSIDevice
*s
= opaque
;
150 SCSIRequest
*req
, *next
;
152 qemu_bh_delete(s
->bh
);
155 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
156 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
160 switch (req
->cmd
.mode
) {
161 case SCSI_XFER_FROM_DEV
:
162 case SCSI_XFER_TO_DEV
:
163 scsi_req_continue(req
);
166 scsi_req_dequeue(req
);
167 scsi_req_enqueue(req
);
173 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
174 /* Drop the reference that was acquired in scsi_dma_restart_cb */
175 object_unref(OBJECT(s
));
178 void scsi_req_retry(SCSIRequest
*req
)
180 /* No need to save a reference, because scsi_dma_restart_bh just
181 * looks at the request list. */
185 static void scsi_dma_restart_cb(void *opaque
, bool running
, RunState state
)
187 SCSIDevice
*s
= opaque
;
193 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
194 /* The reference is dropped in scsi_dma_restart_bh.*/
195 object_ref(OBJECT(s
));
196 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
197 qemu_bh_schedule(s
->bh
);
201 static bool scsi_bus_is_address_free(SCSIBus
*bus
,
202 int channel
, int target
, int lun
,
207 RCU_READ_LOCK_GUARD();
208 d
= do_scsi_device_find(bus
, channel
, target
, lun
, true);
209 if (d
&& d
->lun
== lun
) {
221 static bool scsi_bus_check_address(BusState
*qbus
, DeviceState
*qdev
, Error
**errp
)
223 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
224 SCSIBus
*bus
= SCSI_BUS(qbus
);
226 if (dev
->channel
> bus
->info
->max_channel
) {
227 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
230 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
231 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
234 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
235 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
239 if (dev
->id
!= -1 && dev
->lun
!= -1) {
241 if (!scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, dev
->lun
, &d
)) {
242 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
250 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
252 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
253 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
255 Error
*local_err
= NULL
;
259 if (dev
->lun
== -1) {
263 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, ++id
, dev
->lun
, NULL
);
264 } while (!is_free
&& id
< bus
->info
->max_target
);
266 error_setg(errp
, "no free target");
270 } else if (dev
->lun
== -1) {
273 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, ++lun
, NULL
);
274 } while (!is_free
&& lun
< bus
->info
->max_lun
);
276 error_setg(errp
, "no free lun");
282 QTAILQ_INIT(&dev
->requests
);
283 scsi_device_realize(dev
, &local_err
);
285 error_propagate(errp
, local_err
);
288 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
289 scsi_dma_restart_cb
, dev
);
292 static void scsi_qdev_unrealize(DeviceState
*qdev
)
294 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
297 qemu_del_vm_change_state_handler(dev
->vmsentry
);
300 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
302 scsi_device_unrealize(dev
);
304 blockdev_mark_auto_del(dev
->conf
.blk
);
307 /* handle legacy '-drive if=scsi,...' cmd line args */
308 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
309 int unit
, bool removable
, int bootindex
,
311 BlockdevOnError rerror
,
312 BlockdevOnError werror
,
313 const char *serial
, Error
**errp
)
320 if (blk_is_sg(blk
)) {
321 driver
= "scsi-generic";
323 dinfo
= blk_legacy_dinfo(blk
);
324 if (dinfo
&& dinfo
->media_cd
) {
330 dev
= qdev_new(driver
);
331 name
= g_strdup_printf("legacy[%d]", unit
);
332 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
));
335 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
336 if (bootindex
>= 0) {
337 object_property_set_int(OBJECT(dev
), "bootindex", bootindex
,
340 if (object_property_find(OBJECT(dev
), "removable")) {
341 qdev_prop_set_bit(dev
, "removable", removable
);
343 if (serial
&& object_property_find(OBJECT(dev
), "serial")) {
344 qdev_prop_set_string(dev
, "serial", serial
);
346 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, errp
)) {
347 object_unparent(OBJECT(dev
));
350 if (!object_property_set_bool(OBJECT(dev
), "share-rw", share_rw
, errp
)) {
351 object_unparent(OBJECT(dev
));
355 qdev_prop_set_enum(dev
, "rerror", rerror
);
356 qdev_prop_set_enum(dev
, "werror", werror
);
358 if (!qdev_realize_and_unref(dev
, &bus
->qbus
, errp
)) {
359 object_unparent(OBJECT(dev
));
362 return SCSI_DEVICE(dev
);
365 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
372 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
373 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
377 qemu_opts_loc_restore(dinfo
->opts
);
378 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
379 unit
, false, -1, false,
380 BLOCKDEV_ON_ERROR_AUTO
,
381 BLOCKDEV_ON_ERROR_AUTO
,
387 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
389 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
390 scsi_req_complete(req
, CHECK_CONDITION
);
394 static const struct SCSIReqOps reqops_invalid_field
= {
395 .size
= sizeof(SCSIRequest
),
396 .send_command
= scsi_invalid_field
399 /* SCSIReqOps implementation for invalid commands. */
401 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
403 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
404 scsi_req_complete(req
, CHECK_CONDITION
);
408 static const struct SCSIReqOps reqops_invalid_opcode
= {
409 .size
= sizeof(SCSIRequest
),
410 .send_command
= scsi_invalid_command
413 /* SCSIReqOps implementation for unit attention conditions. */
415 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
417 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
418 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
419 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
420 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
422 scsi_req_complete(req
, CHECK_CONDITION
);
426 static const struct SCSIReqOps reqops_unit_attention
= {
427 .size
= sizeof(SCSIRequest
),
428 .send_command
= scsi_unit_attention
431 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
434 typedef struct SCSITargetReq SCSITargetReq
;
436 struct SCSITargetReq
{
443 static void store_lun(uint8_t *outbuf
, int lun
)
446 /* Simple logical unit addressing method*/
450 /* Flat space addressing method */
451 outbuf
[0] = 0x40 | (lun
>> 8);
452 outbuf
[1] = (lun
& 255);
456 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
460 uint8_t tmp
[8] = {0};
464 if (r
->req
.cmd
.xfer
< 16) {
467 if (r
->req
.cmd
.buf
[2] > 2) {
471 /* reserve space for 63 LUNs*/
472 buf
= g_byte_array_sized_new(512);
474 channel
= r
->req
.dev
->channel
;
477 /* add size (will be updated later to correct value */
478 g_byte_array_append(buf
, tmp
, 8);
482 g_byte_array_append(buf
, tmp
, 8);
485 WITH_RCU_READ_LOCK_GUARD() {
486 QTAILQ_FOREACH_RCU(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
487 DeviceState
*qdev
= kid
->child
;
488 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
490 if (dev
->channel
== channel
&& dev
->id
== id
&& dev
->lun
!= 0) {
491 store_lun(tmp
, dev
->lun
);
492 g_byte_array_append(buf
, tmp
, 8);
499 r
->buf
= g_byte_array_free(buf
, FALSE
);
500 r
->len
= MIN(len
, r
->req
.cmd
.xfer
& ~7);
502 /* store the LUN list length */
503 stl_be_p(&r
->buf
[0], len
- 8);
507 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
509 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
511 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
513 if (r
->req
.cmd
.buf
[1] & 0x2) {
514 /* Command support data - optional, not implemented */
518 if (r
->req
.cmd
.buf
[1] & 0x1) {
519 /* Vital product data */
520 uint8_t page_code
= r
->req
.cmd
.buf
[2];
521 r
->buf
[r
->len
++] = page_code
; /* this page */
522 r
->buf
[r
->len
++] = 0x00;
525 case 0x00: /* Supported page codes, mandatory */
529 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
530 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
537 assert(r
->len
< r
->buf_len
);
538 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
542 /* Standard INQUIRY data */
543 if (r
->req
.cmd
.buf
[2] != 0) {
548 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
549 memset(r
->buf
, 0, r
->len
);
550 if (r
->req
.lun
!= 0) {
551 r
->buf
[0] = TYPE_NO_LUN
;
553 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
554 r
->buf
[2] = 5; /* Version */
555 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
556 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
557 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
558 memcpy(&r
->buf
[8], "QEMU ", 8);
559 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
560 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
565 static size_t scsi_sense_len(SCSIRequest
*req
)
567 if (req
->dev
->type
== TYPE_SCANNER
)
568 return SCSI_SENSE_LEN_SCANNER
;
570 return SCSI_SENSE_LEN
;
573 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
575 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
576 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
579 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
580 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
581 scsi_req_complete(req
, CHECK_CONDITION
);
586 if (!scsi_target_emulate_report_luns(r
)) {
587 goto illegal_request
;
591 if (!scsi_target_emulate_inquiry(r
)) {
592 goto illegal_request
;
596 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
598 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
600 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
603 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
604 MIN(req
->cmd
.xfer
, r
->buf_len
),
607 if (r
->req
.dev
->sense_is_ua
) {
608 scsi_device_unit_attention_reported(req
->dev
);
609 r
->req
.dev
->sense_len
= 0;
610 r
->req
.dev
->sense_is_ua
= false;
613 case TEST_UNIT_READY
:
616 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
617 scsi_req_complete(req
, CHECK_CONDITION
);
620 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
621 scsi_req_complete(req
, CHECK_CONDITION
);
626 scsi_req_complete(req
, GOOD
);
631 static void scsi_target_read_data(SCSIRequest
*req
)
633 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
639 scsi_req_data(&r
->req
, n
);
641 scsi_req_complete(&r
->req
, GOOD
);
645 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
647 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
652 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
654 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
656 r
->buf
= g_malloc(len
);
662 static void scsi_target_free_buf(SCSIRequest
*req
)
664 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
669 static const struct SCSIReqOps reqops_target_command
= {
670 .size
= sizeof(SCSITargetReq
),
671 .send_command
= scsi_target_send_command
,
672 .read_data
= scsi_target_read_data
,
673 .get_buf
= scsi_target_get_buf
,
674 .free_req
= scsi_target_free_buf
,
678 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
679 uint32_t tag
, uint32_t lun
, void *hba_private
)
682 SCSIBus
*bus
= scsi_bus_from_device(d
);
683 BusState
*qbus
= BUS(bus
);
684 const int memset_off
= offsetof(SCSIRequest
, sense
)
685 + sizeof(req
->sense
);
687 req
= g_malloc(reqops
->size
);
688 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
694 req
->hba_private
= hba_private
;
696 req
->host_status
= -1;
698 object_ref(OBJECT(d
));
699 object_ref(OBJECT(qbus
->parent
));
700 notifier_list_init(&req
->cancel_notifiers
);
701 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
705 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
706 uint8_t *buf
, void *hba_private
)
708 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
709 const SCSIReqOps
*ops
;
710 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
712 SCSICommand cmd
= { .len
= 0 };
715 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
716 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
717 (buf
[0] != INQUIRY
&&
718 buf
[0] != REPORT_LUNS
&&
719 buf
[0] != GET_CONFIGURATION
&&
720 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
723 * If we already have a pending unit attention condition,
724 * report this one before triggering another one.
726 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
727 ops
= &reqops_unit_attention
;
728 } else if (lun
!= d
->lun
||
729 buf
[0] == REPORT_LUNS
||
730 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
731 ops
= &reqops_target_command
;
736 if (ops
!= NULL
|| !sc
->parse_cdb
) {
737 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
739 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
743 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
744 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
746 assert(cmd
.len
!= 0);
747 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
750 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
754 if (cmd
.xfer
> INT32_MAX
) {
755 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
757 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
759 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
764 req
->residual
= req
->cmd
.xfer
;
768 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
770 case TEST_UNIT_READY
:
771 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
774 trace_scsi_report_luns(d
->id
, lun
, tag
);
777 trace_scsi_request_sense(d
->id
, lun
, tag
);
786 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
788 return req
->ops
->get_buf(req
);
791 static void scsi_clear_unit_attention(SCSIRequest
*req
)
794 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
795 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
800 * If an INQUIRY command enters the enabled command state,
801 * the device server shall [not] clear any unit attention condition;
802 * See also MMC-6, paragraphs 6.5 and 6.6.2.
804 if (req
->cmd
.buf
[0] == INQUIRY
||
805 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
806 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
810 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
811 ua
= &req
->dev
->unit_attention
;
813 ua
= &req
->bus
->unit_attention
;
817 * If a REPORT LUNS command enters the enabled command state, [...]
818 * the device server shall clear any pending unit attention condition
819 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
821 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
822 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
823 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
827 *ua
= SENSE_CODE(NO_SENSE
);
830 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
835 if (!req
->sense_len
) {
839 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
842 * FIXME: clearing unit attention conditions upon autosense should be done
843 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
846 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
847 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
848 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
850 if (req
->dev
->sense_is_ua
) {
851 scsi_device_unit_attention_reported(req
->dev
);
852 req
->dev
->sense_len
= 0;
853 req
->dev
->sense_is_ua
= false;
858 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
860 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
863 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
865 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
866 sense
.key
, sense
.asc
, sense
.ascq
);
867 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
870 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
872 assert(!req
->enqueued
);
874 if (req
->bus
->info
->get_sg_list
) {
875 req
->sg
= req
->bus
->info
->get_sg_list(req
);
879 req
->enqueued
= true;
880 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
883 int32_t scsi_req_enqueue(SCSIRequest
*req
)
888 scsi_req_enqueue_internal(req
);
890 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
895 static void scsi_req_dequeue(SCSIRequest
*req
)
897 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
900 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
901 req
->enqueued
= false;
906 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
908 /* MMC-6, paragraph 6.7. */
911 if ((data_type
& 3) == 0) {
912 /* Each descriptor is as in Table 295 - Nominal performance. */
913 return 16 * num_desc
+ 8;
915 /* Each descriptor is as in Table 296 - Exceptions. */
916 return 6 * num_desc
+ 8;
921 return 8 * num_desc
+ 8;
923 return 2048 * num_desc
+ 8;
925 return 16 * num_desc
+ 8;
931 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
933 int byte_block
= (buf
[2] >> 2) & 0x1;
934 int type
= (buf
[2] >> 4) & 0x1;
939 xfer_unit
= dev
->blocksize
;
950 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
952 int length
= buf
[2] & 0x3;
954 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
958 case 3: /* USB-specific. */
973 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
975 int extend
= buf
[1] & 0x1;
976 int length
= buf
[2] & 0x3;
978 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
982 case 3: /* USB-specific. */
988 xfer
|= (extend
? buf
[3] << 8 : 0);
992 xfer
|= (extend
? buf
[5] << 8 : 0);
999 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1001 cmd
->xfer
= scsi_cdb_xfer(buf
);
1003 case TEST_UNIT_READY
:
1007 case WRITE_FILEMARKS
:
1008 case WRITE_FILEMARKS_16
:
1013 case ALLOW_MEDIUM_REMOVAL
:
1015 case SYNCHRONIZE_CACHE
:
1016 case SYNCHRONIZE_CACHE_16
:
1018 case LOCK_UNLOCK_CACHE
:
1024 case SET_READ_AHEAD
:
1027 case ALLOW_OVERWRITE
:
1033 if ((buf
[1] & 2) == 0) {
1035 } else if ((buf
[1] & 4) != 0) {
1038 cmd
->xfer
*= dev
->blocksize
;
1044 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
1046 case READ_CAPACITY_10
:
1049 case READ_BLOCK_LIMITS
:
1052 case SEND_VOLUME_TAG
:
1053 /* GPCMD_SET_STREAMING from multimedia commands. */
1054 if (dev
->type
== TYPE_ROM
) {
1055 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1057 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1061 /* length 0 means 256 blocks */
1062 if (cmd
->xfer
== 0) {
1067 case WRITE_VERIFY_10
:
1069 case WRITE_VERIFY_12
:
1071 case WRITE_VERIFY_16
:
1072 cmd
->xfer
*= dev
->blocksize
;
1076 /* length 0 means 256 blocks */
1077 if (cmd
->xfer
== 0) {
1084 cmd
->xfer
*= dev
->blocksize
;
1087 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1088 * for block devices are restricted to the header right now. */
1089 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1092 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1096 case RECEIVE_DIAGNOSTIC
:
1097 case SEND_DIAGNOSTIC
:
1098 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1103 case SEND_CUE_SHEET
:
1104 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1106 case PERSISTENT_RESERVE_OUT
:
1107 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1110 if (dev
->type
== TYPE_ROM
) {
1111 /* MMC command GET PERFORMANCE. */
1112 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1113 buf
[10], buf
[1] & 0x1f);
1116 case MECHANISM_STATUS
:
1117 case READ_DVD_STRUCTURE
:
1118 case SEND_DVD_STRUCTURE
:
1119 case MAINTENANCE_OUT
:
1120 case MAINTENANCE_IN
:
1121 if (dev
->type
== TYPE_ROM
) {
1122 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1123 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1126 case ATA_PASSTHROUGH_12
:
1127 if (dev
->type
== TYPE_ROM
) {
1128 /* BLANK command of MMC */
1131 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1134 case ATA_PASSTHROUGH_16
:
1135 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1141 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1144 /* stream commands */
1151 case RECOVER_BUFFERED_DATA
:
1153 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1154 if (buf
[1] & 0x01) { /* fixed */
1155 cmd
->xfer
*= dev
->blocksize
;
1159 case READ_REVERSE_16
:
1162 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1163 if (buf
[1] & 0x01) { /* fixed */
1164 cmd
->xfer
*= dev
->blocksize
;
1172 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1175 switch (buf
[1] & 0x1f) /* operation code */ {
1176 case SHORT_FORM_BLOCK_ID
:
1177 case SHORT_FORM_VENDOR_SPECIFIC
:
1184 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1192 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1194 /* generic commands */
1196 return scsi_req_xfer(cmd
, dev
, buf
);
1201 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1204 /* medium changer commands */
1205 case EXCHANGE_MEDIUM
:
1206 case INITIALIZE_ELEMENT_STATUS
:
1207 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1209 case POSITION_TO_ELEMENT
:
1212 case READ_ELEMENT_STATUS
:
1213 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1216 /* generic commands */
1218 return scsi_req_xfer(cmd
, dev
, buf
);
1223 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1226 /* Scanner commands */
1227 case OBJECT_POSITION
:
1237 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1240 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1241 return scsi_req_xfer(cmd
, dev
, buf
);
1247 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1250 cmd
->mode
= SCSI_XFER_NONE
;
1253 switch (cmd
->buf
[0]) {
1256 case WRITE_VERIFY_10
:
1258 case WRITE_VERIFY_12
:
1260 case WRITE_VERIFY_16
:
1267 case CHANGE_DEFINITION
:
1270 case MODE_SELECT_10
:
1271 case SEND_DIAGNOSTIC
:
1274 case REASSIGN_BLOCKS
:
1283 case SEARCH_HIGH_12
:
1284 case SEARCH_EQUAL_12
:
1287 case SEND_VOLUME_TAG
:
1288 case SEND_CUE_SHEET
:
1289 case SEND_DVD_STRUCTURE
:
1290 case PERSISTENT_RESERVE_OUT
:
1291 case MAINTENANCE_OUT
:
1294 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1295 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1297 cmd
->mode
= SCSI_XFER_TO_DEV
;
1299 case ATA_PASSTHROUGH_12
:
1300 case ATA_PASSTHROUGH_16
:
1302 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1303 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1306 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1311 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1317 len
= scsi_cdb_length(buf
);
1323 switch (dev
->type
) {
1325 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1327 case TYPE_MEDIUM_CHANGER
:
1328 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1331 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1334 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1341 memcpy(cmd
->buf
, buf
, cmd
->len
);
1342 scsi_cmd_xfer_mode(cmd
);
1343 cmd
->lba
= scsi_cmd_lba(cmd
);
1347 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1349 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1351 scsi_device_set_ua(dev
, sense
);
1352 if (bus
->info
->change
) {
1353 bus
->info
->change(bus
, dev
, sense
);
1357 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1359 assert(req
->refcount
> 0);
1364 void scsi_req_unref(SCSIRequest
*req
)
1366 assert(req
->refcount
> 0);
1367 if (--req
->refcount
== 0) {
1368 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1369 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1371 if (bus
->info
->free_request
&& req
->hba_private
) {
1372 bus
->info
->free_request(bus
, req
->hba_private
);
1374 if (req
->ops
->free_req
) {
1375 req
->ops
->free_req(req
);
1377 object_unref(OBJECT(req
->dev
));
1378 object_unref(OBJECT(qbus
->parent
));
1383 /* Tell the device that we finished processing this chunk of I/O. It
1384 will start the next chunk or complete the command. */
1385 void scsi_req_continue(SCSIRequest
*req
)
1387 if (req
->io_canceled
) {
1388 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1391 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1392 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1393 req
->ops
->write_data(req
);
1395 req
->ops
->read_data(req
);
1399 /* Called by the devices when data is ready for the HBA. The HBA should
1400 start a DMA operation to read or fill the device's data buffer.
1401 Once it completes, calling scsi_req_continue will restart I/O. */
1402 void scsi_req_data(SCSIRequest
*req
, int len
)
1405 if (req
->io_canceled
) {
1406 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1409 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1410 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1412 req
->residual
-= len
;
1413 req
->bus
->info
->transfer_data(req
, len
);
1417 /* If the device calls scsi_req_data and the HBA specified a
1418 * scatter/gather list, the transfer has to happen in a single
1420 assert(!req
->dma_started
);
1421 req
->dma_started
= true;
1423 buf
= scsi_req_get_buf(req
);
1424 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1425 dma_buf_read(buf
, len
, &req
->residual
, req
->sg
,
1426 MEMTXATTRS_UNSPECIFIED
);
1428 dma_buf_write(buf
, len
, &req
->residual
, req
->sg
,
1429 MEMTXATTRS_UNSPECIFIED
);
1431 scsi_req_continue(req
);
1434 void scsi_req_print(SCSIRequest
*req
)
1439 fprintf(fp
, "[%s id=%d] %s",
1440 req
->dev
->qdev
.parent_bus
->name
,
1442 scsi_command_name(req
->cmd
.buf
[0]));
1443 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1444 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1446 switch (req
->cmd
.mode
) {
1447 case SCSI_XFER_NONE
:
1448 fprintf(fp
, " - none\n");
1450 case SCSI_XFER_FROM_DEV
:
1451 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1453 case SCSI_XFER_TO_DEV
:
1454 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1457 fprintf(fp
, " - Oops\n");
1462 void scsi_req_complete_failed(SCSIRequest
*req
, int host_status
)
1467 assert(req
->status
== -1 && req
->host_status
== -1);
1468 assert(req
->ops
!= &reqops_unit_attention
);
1470 if (!req
->bus
->info
->fail
) {
1471 status
= scsi_sense_from_host_status(req
->host_status
, &sense
);
1472 if (status
== CHECK_CONDITION
) {
1473 scsi_req_build_sense(req
, sense
);
1475 scsi_req_complete(req
, status
);
1479 req
->host_status
= host_status
;
1481 scsi_req_dequeue(req
);
1482 req
->bus
->info
->fail(req
);
1484 /* Cancelled requests might end up being completed instead of cancelled */
1485 notifier_list_notify(&req
->cancel_notifiers
, req
);
1486 scsi_req_unref(req
);
1489 void scsi_req_complete(SCSIRequest
*req
, int status
)
1491 assert(req
->status
== -1 && req
->host_status
== -1);
1492 req
->status
= status
;
1493 req
->host_status
= SCSI_HOST_OK
;
1495 assert(req
->sense_len
<= sizeof(req
->sense
));
1496 if (status
== GOOD
) {
1500 if (req
->sense_len
) {
1501 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1502 req
->dev
->sense_len
= req
->sense_len
;
1503 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1505 req
->dev
->sense_len
= 0;
1506 req
->dev
->sense_is_ua
= false;
1510 * Unit attention state is now stored in the device's sense buffer
1511 * if the HBA didn't do autosense. Clear the pending unit attention
1514 scsi_clear_unit_attention(req
);
1517 scsi_req_dequeue(req
);
1518 req
->bus
->info
->complete(req
, req
->residual
);
1520 /* Cancelled requests might end up being completed instead of cancelled */
1521 notifier_list_notify(&req
->cancel_notifiers
, req
);
1522 scsi_req_unref(req
);
1525 /* Called by the devices when the request is canceled. */
1526 void scsi_req_cancel_complete(SCSIRequest
*req
)
1528 assert(req
->io_canceled
);
1529 if (req
->bus
->info
->cancel
) {
1530 req
->bus
->info
->cancel(req
);
1532 notifier_list_notify(&req
->cancel_notifiers
, req
);
1533 scsi_req_unref(req
);
1536 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1537 * notifier list, the bus will be notified the requests cancellation is
1540 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1542 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1544 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1546 if (req
->io_canceled
) {
1547 /* A blk_aio_cancel_async is pending; when it finishes,
1548 * scsi_req_cancel_complete will be called and will
1549 * call the notifier we just added. Just wait for that.
1554 /* Dropped in scsi_req_cancel_complete. */
1556 scsi_req_dequeue(req
);
1557 req
->io_canceled
= true;
1559 blk_aio_cancel_async(req
->aiocb
);
1561 scsi_req_cancel_complete(req
);
1565 void scsi_req_cancel(SCSIRequest
*req
)
1567 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1568 if (!req
->enqueued
) {
1571 assert(!req
->io_canceled
);
1572 /* Dropped in scsi_req_cancel_complete. */
1574 scsi_req_dequeue(req
);
1575 req
->io_canceled
= true;
1577 blk_aio_cancel(req
->aiocb
);
1579 scsi_req_cancel_complete(req
);
1583 static int scsi_ua_precedence(SCSISense sense
)
1585 if (sense
.key
!= UNIT_ATTENTION
) {
1588 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1589 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1591 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1592 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1594 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1595 /* These two go with "all others". */
1597 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1598 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1599 * POWER ON OCCURRED = 1
1600 * SCSI BUS RESET OCCURRED = 2
1601 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1602 * I_T NEXUS LOSS OCCURRED = 7
1605 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1606 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1609 return (sense
.asc
<< 8) | sense
.ascq
;
1612 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1615 if (sense
.key
!= UNIT_ATTENTION
) {
1618 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1619 sense
.asc
, sense
.ascq
);
1622 * Override a pre-existing unit attention condition, except for a more
1623 * important reset condition.
1625 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1626 prec2
= scsi_ua_precedence(sense
);
1627 if (prec2
< prec1
) {
1628 sdev
->unit_attention
= sense
;
1632 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1636 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1637 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1638 req
= QTAILQ_FIRST(&sdev
->requests
);
1639 scsi_req_cancel_async(req
, NULL
);
1641 blk_drain(sdev
->conf
.blk
);
1642 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1643 scsi_device_set_ua(sdev
, sense
);
1646 static char *scsibus_get_dev_path(DeviceState
*dev
)
1648 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1649 DeviceState
*hba
= dev
->parent_bus
->parent
;
1653 id
= qdev_get_dev_path(hba
);
1655 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1657 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1663 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1665 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1666 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1667 qdev_fw_name(dev
), d
->id
, d
->lun
);
1670 /* SCSI request list. For simplicity, pv points to the whole device */
1672 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1673 const VMStateField
*field
, JSONWriter
*vmdesc
)
1676 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1679 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1680 assert(!req
->io_canceled
);
1681 assert(req
->status
== -1 && req
->host_status
== -1);
1682 assert(req
->enqueued
);
1684 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1685 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1686 qemu_put_be32s(f
, &req
->tag
);
1687 qemu_put_be32s(f
, &req
->lun
);
1688 if (bus
->info
->save_request
) {
1689 bus
->info
->save_request(f
, req
);
1691 if (req
->ops
->save_request
) {
1692 req
->ops
->save_request(f
, req
);
1695 qemu_put_sbyte(f
, 0);
1700 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1701 const VMStateField
*field
)
1704 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1707 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1708 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1713 qemu_get_buffer(f
, buf
, sizeof(buf
));
1714 qemu_get_be32s(f
, &tag
);
1715 qemu_get_be32s(f
, &lun
);
1716 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1717 req
->retry
= (sbyte
== 1);
1718 if (bus
->info
->load_request
) {
1719 req
->hba_private
= bus
->info
->load_request(f
, req
);
1721 if (req
->ops
->load_request
) {
1722 req
->ops
->load_request(f
, req
);
1725 /* Just restart it later. */
1726 scsi_req_enqueue_internal(req
);
1728 /* At this point, the request will be kept alive by the reference
1729 * added by scsi_req_enqueue_internal, so we can release our reference.
1730 * The HBA of course will add its own reference in the load_request
1731 * callback if it needs to hold on the SCSIRequest.
1733 scsi_req_unref(req
);
1739 static const VMStateInfo vmstate_info_scsi_requests
= {
1740 .name
= "scsi-requests",
1741 .get
= get_scsi_requests
,
1742 .put
= put_scsi_requests
,
1745 static bool scsi_sense_state_needed(void *opaque
)
1747 SCSIDevice
*s
= opaque
;
1749 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1752 static const VMStateDescription vmstate_scsi_sense_state
= {
1753 .name
= "SCSIDevice/sense",
1755 .minimum_version_id
= 1,
1756 .needed
= scsi_sense_state_needed
,
1757 .fields
= (VMStateField
[]) {
1758 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1759 SCSI_SENSE_BUF_SIZE_OLD
,
1760 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1761 VMSTATE_END_OF_LIST()
1765 const VMStateDescription vmstate_scsi_device
= {
1766 .name
= "SCSIDevice",
1768 .minimum_version_id
= 1,
1769 .fields
= (VMStateField
[]) {
1770 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1771 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1772 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1773 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1774 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1775 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1779 .field_exists
= NULL
,
1780 .size
= 0, /* ouch */
1781 .info
= &vmstate_info_scsi_requests
,
1782 .flags
= VMS_SINGLE
,
1785 VMSTATE_END_OF_LIST()
1787 .subsections
= (const VMStateDescription
*[]) {
1788 &vmstate_scsi_sense_state
,
1793 static Property scsi_props
[] = {
1794 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
1795 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
1796 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
1797 DEFINE_PROP_END_OF_LIST(),
1800 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1802 DeviceClass
*k
= DEVICE_CLASS(klass
);
1803 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1804 k
->bus_type
= TYPE_SCSI_BUS
;
1805 k
->realize
= scsi_qdev_realize
;
1806 k
->unrealize
= scsi_qdev_unrealize
;
1807 device_class_set_props(k
, scsi_props
);
1810 static void scsi_dev_instance_init(Object
*obj
)
1812 DeviceState
*dev
= DEVICE(obj
);
1813 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1815 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1820 static const TypeInfo scsi_device_type_info
= {
1821 .name
= TYPE_SCSI_DEVICE
,
1822 .parent
= TYPE_DEVICE
,
1823 .instance_size
= sizeof(SCSIDevice
),
1825 .class_size
= sizeof(SCSIDeviceClass
),
1826 .class_init
= scsi_device_class_init
,
1827 .instance_init
= scsi_dev_instance_init
,
1830 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
1832 BusClass
*k
= BUS_CLASS(klass
);
1833 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1835 k
->get_dev_path
= scsibus_get_dev_path
;
1836 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
1837 k
->check_address
= scsi_bus_check_address
;
1838 hc
->unplug
= qdev_simple_device_unplug_cb
;
1841 static const TypeInfo scsi_bus_info
= {
1842 .name
= TYPE_SCSI_BUS
,
1844 .instance_size
= sizeof(SCSIBus
),
1845 .class_init
= scsi_bus_class_init
,
1846 .interfaces
= (InterfaceInfo
[]) {
1847 { TYPE_HOTPLUG_HANDLER
},
1852 static void scsi_register_types(void)
1854 type_register_static(&scsi_bus_info
);
1855 type_register_static(&scsi_device_type_info
);
1858 type_init(scsi_register_types
)