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 "hw/qdev-properties.h"
7 #include "hw/scsi/scsi.h"
8 #include "migration/qemu-file-types.h"
9 #include "migration/vmstate.h"
10 #include "scsi/constants.h"
11 #include "sysemu/block-backend.h"
12 #include "sysemu/blockdev.h"
13 #include "sysemu/sysemu.h"
14 #include "sysemu/runstate.h"
16 #include "sysemu/dma.h"
17 #include "qemu/cutils.h"
19 static char *scsibus_get_dev_path(DeviceState
*dev
);
20 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
21 static void scsi_req_dequeue(SCSIRequest
*req
);
22 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
23 static void scsi_target_free_buf(SCSIRequest
*req
);
25 static int next_scsi_bus
;
27 static SCSIDevice
*do_scsi_device_find(SCSIBus
*bus
,
28 int channel
, int id
, int lun
,
29 bool include_unrealized
)
32 SCSIDevice
*retval
= NULL
;
34 QTAILQ_FOREACH_RCU(kid
, &bus
->qbus
.children
, sibling
) {
35 DeviceState
*qdev
= kid
->child
;
36 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
38 if (dev
->channel
== channel
&& dev
->id
== id
) {
39 if (dev
->lun
== lun
) {
45 * If we don't find exact match (channel/bus/lun),
46 * we will return the first device which matches channel/bus
56 * This function might run on the IO thread and we might race against
57 * main thread hot-plugging the device.
58 * We assume that as soon as .realized is set to true we can let
59 * the user access the device.
62 if (retval
&& !include_unrealized
&&
63 !qatomic_load_acquire(&retval
->qdev
.realized
)) {
70 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
72 RCU_READ_LOCK_GUARD();
73 return do_scsi_device_find(bus
, channel
, id
, lun
, false);
76 SCSIDevice
*scsi_device_get(SCSIBus
*bus
, int channel
, int id
, int lun
)
79 RCU_READ_LOCK_GUARD();
80 d
= do_scsi_device_find(bus
, channel
, id
, lun
, false);
87 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
89 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
95 static void scsi_device_unrealize(SCSIDevice
*s
)
97 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
103 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
106 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
109 assert(cmd
->len
== 0);
110 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
111 if (bus
->info
->parse_cdb
) {
112 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
117 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
118 uint8_t *buf
, void *hba_private
)
120 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
122 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
128 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
130 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
131 if (sc
->unit_attention_reported
) {
132 sc
->unit_attention_reported(s
);
136 /* Create a scsi bus, and attach devices to it. */
137 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
138 const SCSIBusInfo
*info
, const char *bus_name
)
140 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
141 bus
->busnr
= next_scsi_bus
++;
143 qbus_set_bus_hotplug_handler(BUS(bus
));
146 static void scsi_dma_restart_bh(void *opaque
)
148 SCSIDevice
*s
= opaque
;
149 SCSIRequest
*req
, *next
;
151 qemu_bh_delete(s
->bh
);
154 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
155 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
159 switch (req
->cmd
.mode
) {
160 case SCSI_XFER_FROM_DEV
:
161 case SCSI_XFER_TO_DEV
:
162 scsi_req_continue(req
);
165 scsi_req_dequeue(req
);
166 scsi_req_enqueue(req
);
172 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
173 /* Drop the reference that was acquired in scsi_dma_restart_cb */
174 object_unref(OBJECT(s
));
177 void scsi_req_retry(SCSIRequest
*req
)
179 /* No need to save a reference, because scsi_dma_restart_bh just
180 * looks at the request list. */
184 static void scsi_dma_restart_cb(void *opaque
, bool running
, RunState state
)
186 SCSIDevice
*s
= opaque
;
192 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
193 /* The reference is dropped in scsi_dma_restart_bh.*/
194 object_ref(OBJECT(s
));
195 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
196 qemu_bh_schedule(s
->bh
);
200 static bool scsi_bus_is_address_free(SCSIBus
*bus
,
201 int channel
, int target
, int lun
,
206 RCU_READ_LOCK_GUARD();
207 d
= do_scsi_device_find(bus
, channel
, target
, lun
, true);
208 if (d
&& d
->lun
== lun
) {
220 static bool scsi_bus_check_address(BusState
*qbus
, DeviceState
*qdev
, Error
**errp
)
222 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
223 SCSIBus
*bus
= SCSI_BUS(qbus
);
225 if (dev
->channel
> bus
->info
->max_channel
) {
226 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
229 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
230 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
233 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
234 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
238 if (dev
->id
!= -1 && dev
->lun
!= -1) {
240 if (!scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, dev
->lun
, &d
)) {
241 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
249 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
251 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
252 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
254 Error
*local_err
= NULL
;
258 if (dev
->lun
== -1) {
262 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, ++id
, dev
->lun
, NULL
);
263 } while (!is_free
&& id
< bus
->info
->max_target
);
265 error_setg(errp
, "no free target");
269 } else if (dev
->lun
== -1) {
272 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, ++lun
, NULL
);
273 } while (!is_free
&& lun
< bus
->info
->max_lun
);
275 error_setg(errp
, "no free lun");
281 QTAILQ_INIT(&dev
->requests
);
282 scsi_device_realize(dev
, &local_err
);
284 error_propagate(errp
, local_err
);
287 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
288 scsi_dma_restart_cb
, dev
);
291 static void scsi_qdev_unrealize(DeviceState
*qdev
)
293 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
296 qemu_del_vm_change_state_handler(dev
->vmsentry
);
299 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
301 scsi_device_unrealize(dev
);
303 blockdev_mark_auto_del(dev
->conf
.blk
);
306 /* handle legacy '-drive if=scsi,...' cmd line args */
307 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
308 int unit
, bool removable
, int bootindex
,
310 BlockdevOnError rerror
,
311 BlockdevOnError werror
,
312 const char *serial
, Error
**errp
)
319 if (blk_is_sg(blk
)) {
320 driver
= "scsi-generic";
322 dinfo
= blk_legacy_dinfo(blk
);
323 if (dinfo
&& dinfo
->media_cd
) {
329 dev
= qdev_new(driver
);
330 name
= g_strdup_printf("legacy[%d]", unit
);
331 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
));
334 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
335 if (bootindex
>= 0) {
336 object_property_set_int(OBJECT(dev
), "bootindex", bootindex
,
339 if (object_property_find(OBJECT(dev
), "removable")) {
340 qdev_prop_set_bit(dev
, "removable", removable
);
342 if (serial
&& object_property_find(OBJECT(dev
), "serial")) {
343 qdev_prop_set_string(dev
, "serial", serial
);
345 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, errp
)) {
346 object_unparent(OBJECT(dev
));
349 if (!object_property_set_bool(OBJECT(dev
), "share-rw", share_rw
, errp
)) {
350 object_unparent(OBJECT(dev
));
354 qdev_prop_set_enum(dev
, "rerror", rerror
);
355 qdev_prop_set_enum(dev
, "werror", werror
);
357 if (!qdev_realize_and_unref(dev
, &bus
->qbus
, errp
)) {
358 object_unparent(OBJECT(dev
));
361 return SCSI_DEVICE(dev
);
364 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
371 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
372 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
376 qemu_opts_loc_restore(dinfo
->opts
);
377 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
378 unit
, false, -1, false,
379 BLOCKDEV_ON_ERROR_AUTO
,
380 BLOCKDEV_ON_ERROR_AUTO
,
386 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
388 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
389 scsi_req_complete(req
, CHECK_CONDITION
);
393 static const struct SCSIReqOps reqops_invalid_field
= {
394 .size
= sizeof(SCSIRequest
),
395 .send_command
= scsi_invalid_field
398 /* SCSIReqOps implementation for invalid commands. */
400 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
402 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
403 scsi_req_complete(req
, CHECK_CONDITION
);
407 static const struct SCSIReqOps reqops_invalid_opcode
= {
408 .size
= sizeof(SCSIRequest
),
409 .send_command
= scsi_invalid_command
412 /* SCSIReqOps implementation for unit attention conditions. */
414 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
416 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
417 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
418 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
419 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
421 scsi_req_complete(req
, CHECK_CONDITION
);
425 static const struct SCSIReqOps reqops_unit_attention
= {
426 .size
= sizeof(SCSIRequest
),
427 .send_command
= scsi_unit_attention
430 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
433 typedef struct SCSITargetReq SCSITargetReq
;
435 struct SCSITargetReq
{
442 static void store_lun(uint8_t *outbuf
, int lun
)
445 /* Simple logical unit addressing method*/
449 /* Flat space addressing method */
450 outbuf
[0] = 0x40 | (lun
>> 8);
451 outbuf
[1] = (lun
& 255);
455 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
459 uint8_t tmp
[8] = {0};
463 if (r
->req
.cmd
.xfer
< 16) {
466 if (r
->req
.cmd
.buf
[2] > 2) {
470 /* reserve space for 63 LUNs*/
471 buf
= g_byte_array_sized_new(512);
473 channel
= r
->req
.dev
->channel
;
476 /* add size (will be updated later to correct value */
477 g_byte_array_append(buf
, tmp
, 8);
481 g_byte_array_append(buf
, tmp
, 8);
484 WITH_RCU_READ_LOCK_GUARD() {
485 QTAILQ_FOREACH_RCU(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
486 DeviceState
*qdev
= kid
->child
;
487 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
489 if (dev
->channel
== channel
&& dev
->id
== id
&& dev
->lun
!= 0) {
490 store_lun(tmp
, dev
->lun
);
491 g_byte_array_append(buf
, tmp
, 8);
498 r
->buf
= g_byte_array_free(buf
, FALSE
);
499 r
->len
= MIN(len
, r
->req
.cmd
.xfer
& ~7);
501 /* store the LUN list length */
502 stl_be_p(&r
->buf
[0], len
- 8);
506 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
508 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
510 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
512 if (r
->req
.cmd
.buf
[1] & 0x2) {
513 /* Command support data - optional, not implemented */
517 if (r
->req
.cmd
.buf
[1] & 0x1) {
518 /* Vital product data */
519 uint8_t page_code
= r
->req
.cmd
.buf
[2];
520 r
->buf
[r
->len
++] = page_code
; /* this page */
521 r
->buf
[r
->len
++] = 0x00;
524 case 0x00: /* Supported page codes, mandatory */
528 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
529 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
536 assert(r
->len
< r
->buf_len
);
537 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
541 /* Standard INQUIRY data */
542 if (r
->req
.cmd
.buf
[2] != 0) {
547 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
548 memset(r
->buf
, 0, r
->len
);
549 if (r
->req
.lun
!= 0) {
550 r
->buf
[0] = TYPE_NO_LUN
;
552 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
553 r
->buf
[2] = 5; /* Version */
554 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
555 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
556 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
557 memcpy(&r
->buf
[8], "QEMU ", 8);
558 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
559 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
564 static size_t scsi_sense_len(SCSIRequest
*req
)
566 if (req
->dev
->type
== TYPE_SCANNER
)
567 return SCSI_SENSE_LEN_SCANNER
;
569 return SCSI_SENSE_LEN
;
572 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
574 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
575 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
578 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
579 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
580 scsi_req_complete(req
, CHECK_CONDITION
);
585 if (!scsi_target_emulate_report_luns(r
)) {
586 goto illegal_request
;
590 if (!scsi_target_emulate_inquiry(r
)) {
591 goto illegal_request
;
595 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
597 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
599 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
602 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
603 MIN(req
->cmd
.xfer
, r
->buf_len
),
606 if (r
->req
.dev
->sense_is_ua
) {
607 scsi_device_unit_attention_reported(req
->dev
);
608 r
->req
.dev
->sense_len
= 0;
609 r
->req
.dev
->sense_is_ua
= false;
612 case TEST_UNIT_READY
:
615 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
616 scsi_req_complete(req
, CHECK_CONDITION
);
619 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
620 scsi_req_complete(req
, CHECK_CONDITION
);
625 scsi_req_complete(req
, GOOD
);
630 static void scsi_target_read_data(SCSIRequest
*req
)
632 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
638 scsi_req_data(&r
->req
, n
);
640 scsi_req_complete(&r
->req
, GOOD
);
644 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
646 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
651 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
653 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
655 r
->buf
= g_malloc(len
);
661 static void scsi_target_free_buf(SCSIRequest
*req
)
663 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
668 static const struct SCSIReqOps reqops_target_command
= {
669 .size
= sizeof(SCSITargetReq
),
670 .send_command
= scsi_target_send_command
,
671 .read_data
= scsi_target_read_data
,
672 .get_buf
= scsi_target_get_buf
,
673 .free_req
= scsi_target_free_buf
,
677 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
678 uint32_t tag
, uint32_t lun
, void *hba_private
)
681 SCSIBus
*bus
= scsi_bus_from_device(d
);
682 BusState
*qbus
= BUS(bus
);
683 const int memset_off
= offsetof(SCSIRequest
, sense
)
684 + sizeof(req
->sense
);
686 req
= g_malloc(reqops
->size
);
687 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
693 req
->hba_private
= hba_private
;
695 req
->host_status
= -1;
697 object_ref(OBJECT(d
));
698 object_ref(OBJECT(qbus
->parent
));
699 notifier_list_init(&req
->cancel_notifiers
);
700 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
704 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
705 uint8_t *buf
, void *hba_private
)
707 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
708 const SCSIReqOps
*ops
;
709 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
711 SCSICommand cmd
= { .len
= 0 };
714 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
715 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
716 (buf
[0] != INQUIRY
&&
717 buf
[0] != REPORT_LUNS
&&
718 buf
[0] != GET_CONFIGURATION
&&
719 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
722 * If we already have a pending unit attention condition,
723 * report this one before triggering another one.
725 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
726 ops
= &reqops_unit_attention
;
727 } else if (lun
!= d
->lun
||
728 buf
[0] == REPORT_LUNS
||
729 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
730 ops
= &reqops_target_command
;
735 if (ops
!= NULL
|| !sc
->parse_cdb
) {
736 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
738 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
742 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
743 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
745 assert(cmd
.len
!= 0);
746 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
749 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
753 if (cmd
.xfer
> INT32_MAX
) {
754 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
756 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
758 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
763 req
->resid
= req
->cmd
.xfer
;
767 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
769 case TEST_UNIT_READY
:
770 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
773 trace_scsi_report_luns(d
->id
, lun
, tag
);
776 trace_scsi_request_sense(d
->id
, lun
, tag
);
785 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
787 return req
->ops
->get_buf(req
);
790 static void scsi_clear_unit_attention(SCSIRequest
*req
)
793 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
794 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
799 * If an INQUIRY command enters the enabled command state,
800 * the device server shall [not] clear any unit attention condition;
801 * See also MMC-6, paragraphs 6.5 and 6.6.2.
803 if (req
->cmd
.buf
[0] == INQUIRY
||
804 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
805 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
809 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
810 ua
= &req
->dev
->unit_attention
;
812 ua
= &req
->bus
->unit_attention
;
816 * If a REPORT LUNS command enters the enabled command state, [...]
817 * the device server shall clear any pending unit attention condition
818 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
820 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
821 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
822 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
826 *ua
= SENSE_CODE(NO_SENSE
);
829 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
834 if (!req
->sense_len
) {
838 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
841 * FIXME: clearing unit attention conditions upon autosense should be done
842 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
845 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
846 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
847 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
849 if (req
->dev
->sense_is_ua
) {
850 scsi_device_unit_attention_reported(req
->dev
);
851 req
->dev
->sense_len
= 0;
852 req
->dev
->sense_is_ua
= false;
857 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
859 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
862 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
864 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
865 sense
.key
, sense
.asc
, sense
.ascq
);
866 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
869 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
871 assert(!req
->enqueued
);
873 if (req
->bus
->info
->get_sg_list
) {
874 req
->sg
= req
->bus
->info
->get_sg_list(req
);
878 req
->enqueued
= true;
879 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
882 int32_t scsi_req_enqueue(SCSIRequest
*req
)
887 scsi_req_enqueue_internal(req
);
889 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
894 static void scsi_req_dequeue(SCSIRequest
*req
)
896 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
899 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
900 req
->enqueued
= false;
905 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
907 /* MMC-6, paragraph 6.7. */
910 if ((data_type
& 3) == 0) {
911 /* Each descriptor is as in Table 295 - Nominal performance. */
912 return 16 * num_desc
+ 8;
914 /* Each descriptor is as in Table 296 - Exceptions. */
915 return 6 * num_desc
+ 8;
920 return 8 * num_desc
+ 8;
922 return 2048 * num_desc
+ 8;
924 return 16 * num_desc
+ 8;
930 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
932 int byte_block
= (buf
[2] >> 2) & 0x1;
933 int type
= (buf
[2] >> 4) & 0x1;
938 xfer_unit
= dev
->blocksize
;
949 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
951 int length
= buf
[2] & 0x3;
953 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
957 case 3: /* USB-specific. */
972 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
974 int extend
= buf
[1] & 0x1;
975 int length
= buf
[2] & 0x3;
977 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
981 case 3: /* USB-specific. */
987 xfer
|= (extend
? buf
[3] << 8 : 0);
991 xfer
|= (extend
? buf
[5] << 8 : 0);
998 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1000 cmd
->xfer
= scsi_cdb_xfer(buf
);
1002 case TEST_UNIT_READY
:
1006 case WRITE_FILEMARKS
:
1007 case WRITE_FILEMARKS_16
:
1012 case ALLOW_MEDIUM_REMOVAL
:
1014 case SYNCHRONIZE_CACHE
:
1015 case SYNCHRONIZE_CACHE_16
:
1017 case LOCK_UNLOCK_CACHE
:
1023 case SET_READ_AHEAD
:
1026 case ALLOW_OVERWRITE
:
1032 if ((buf
[1] & 2) == 0) {
1034 } else if ((buf
[1] & 4) != 0) {
1037 cmd
->xfer
*= dev
->blocksize
;
1043 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
1045 case READ_CAPACITY_10
:
1048 case READ_BLOCK_LIMITS
:
1051 case SEND_VOLUME_TAG
:
1052 /* GPCMD_SET_STREAMING from multimedia commands. */
1053 if (dev
->type
== TYPE_ROM
) {
1054 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1056 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1060 /* length 0 means 256 blocks */
1061 if (cmd
->xfer
== 0) {
1066 case WRITE_VERIFY_10
:
1068 case WRITE_VERIFY_12
:
1070 case WRITE_VERIFY_16
:
1071 cmd
->xfer
*= dev
->blocksize
;
1075 /* length 0 means 256 blocks */
1076 if (cmd
->xfer
== 0) {
1083 cmd
->xfer
*= dev
->blocksize
;
1086 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1087 * for block devices are restricted to the header right now. */
1088 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1091 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1095 case RECEIVE_DIAGNOSTIC
:
1096 case SEND_DIAGNOSTIC
:
1097 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1102 case SEND_CUE_SHEET
:
1103 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1105 case PERSISTENT_RESERVE_OUT
:
1106 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1109 if (dev
->type
== TYPE_ROM
) {
1110 /* MMC command GET PERFORMANCE. */
1111 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1112 buf
[10], buf
[1] & 0x1f);
1115 case MECHANISM_STATUS
:
1116 case READ_DVD_STRUCTURE
:
1117 case SEND_DVD_STRUCTURE
:
1118 case MAINTENANCE_OUT
:
1119 case MAINTENANCE_IN
:
1120 if (dev
->type
== TYPE_ROM
) {
1121 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1122 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1125 case ATA_PASSTHROUGH_12
:
1126 if (dev
->type
== TYPE_ROM
) {
1127 /* BLANK command of MMC */
1130 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1133 case ATA_PASSTHROUGH_16
:
1134 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1140 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1143 /* stream commands */
1150 case RECOVER_BUFFERED_DATA
:
1152 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1153 if (buf
[1] & 0x01) { /* fixed */
1154 cmd
->xfer
*= dev
->blocksize
;
1158 case READ_REVERSE_16
:
1161 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1162 if (buf
[1] & 0x01) { /* fixed */
1163 cmd
->xfer
*= dev
->blocksize
;
1171 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1174 switch (buf
[1] & 0x1f) /* operation code */ {
1175 case SHORT_FORM_BLOCK_ID
:
1176 case SHORT_FORM_VENDOR_SPECIFIC
:
1183 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1191 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1193 /* generic commands */
1195 return scsi_req_xfer(cmd
, dev
, buf
);
1200 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1203 /* medium changer commands */
1204 case EXCHANGE_MEDIUM
:
1205 case INITIALIZE_ELEMENT_STATUS
:
1206 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1208 case POSITION_TO_ELEMENT
:
1211 case READ_ELEMENT_STATUS
:
1212 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1215 /* generic commands */
1217 return scsi_req_xfer(cmd
, dev
, buf
);
1222 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1225 /* Scanner commands */
1226 case OBJECT_POSITION
:
1236 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1239 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1240 return scsi_req_xfer(cmd
, dev
, buf
);
1246 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1249 cmd
->mode
= SCSI_XFER_NONE
;
1252 switch (cmd
->buf
[0]) {
1255 case WRITE_VERIFY_10
:
1257 case WRITE_VERIFY_12
:
1259 case WRITE_VERIFY_16
:
1266 case CHANGE_DEFINITION
:
1269 case MODE_SELECT_10
:
1270 case SEND_DIAGNOSTIC
:
1273 case REASSIGN_BLOCKS
:
1282 case SEARCH_HIGH_12
:
1283 case SEARCH_EQUAL_12
:
1286 case SEND_VOLUME_TAG
:
1287 case SEND_CUE_SHEET
:
1288 case SEND_DVD_STRUCTURE
:
1289 case PERSISTENT_RESERVE_OUT
:
1290 case MAINTENANCE_OUT
:
1293 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1294 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1296 cmd
->mode
= SCSI_XFER_TO_DEV
;
1298 case ATA_PASSTHROUGH_12
:
1299 case ATA_PASSTHROUGH_16
:
1301 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1302 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1305 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1310 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1316 len
= scsi_cdb_length(buf
);
1322 switch (dev
->type
) {
1324 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1326 case TYPE_MEDIUM_CHANGER
:
1327 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1330 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1333 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1340 memcpy(cmd
->buf
, buf
, cmd
->len
);
1341 scsi_cmd_xfer_mode(cmd
);
1342 cmd
->lba
= scsi_cmd_lba(cmd
);
1346 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1348 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1350 scsi_device_set_ua(dev
, sense
);
1351 if (bus
->info
->change
) {
1352 bus
->info
->change(bus
, dev
, sense
);
1356 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1358 assert(req
->refcount
> 0);
1363 void scsi_req_unref(SCSIRequest
*req
)
1365 assert(req
->refcount
> 0);
1366 if (--req
->refcount
== 0) {
1367 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1368 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1370 if (bus
->info
->free_request
&& req
->hba_private
) {
1371 bus
->info
->free_request(bus
, req
->hba_private
);
1373 if (req
->ops
->free_req
) {
1374 req
->ops
->free_req(req
);
1376 object_unref(OBJECT(req
->dev
));
1377 object_unref(OBJECT(qbus
->parent
));
1382 /* Tell the device that we finished processing this chunk of I/O. It
1383 will start the next chunk or complete the command. */
1384 void scsi_req_continue(SCSIRequest
*req
)
1386 if (req
->io_canceled
) {
1387 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1390 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1391 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1392 req
->ops
->write_data(req
);
1394 req
->ops
->read_data(req
);
1398 /* Called by the devices when data is ready for the HBA. The HBA should
1399 start a DMA operation to read or fill the device's data buffer.
1400 Once it completes, calling scsi_req_continue will restart I/O. */
1401 void scsi_req_data(SCSIRequest
*req
, int len
)
1404 if (req
->io_canceled
) {
1405 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1408 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1409 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1412 req
->bus
->info
->transfer_data(req
, len
);
1416 /* If the device calls scsi_req_data and the HBA specified a
1417 * scatter/gather list, the transfer has to happen in a single
1419 assert(!req
->dma_started
);
1420 req
->dma_started
= true;
1422 buf
= scsi_req_get_buf(req
);
1423 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1424 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1426 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1428 scsi_req_continue(req
);
1431 void scsi_req_print(SCSIRequest
*req
)
1436 fprintf(fp
, "[%s id=%d] %s",
1437 req
->dev
->qdev
.parent_bus
->name
,
1439 scsi_command_name(req
->cmd
.buf
[0]));
1440 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1441 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1443 switch (req
->cmd
.mode
) {
1444 case SCSI_XFER_NONE
:
1445 fprintf(fp
, " - none\n");
1447 case SCSI_XFER_FROM_DEV
:
1448 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1450 case SCSI_XFER_TO_DEV
:
1451 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1454 fprintf(fp
, " - Oops\n");
1459 void scsi_req_complete_failed(SCSIRequest
*req
, int host_status
)
1464 assert(req
->status
== -1 && req
->host_status
== -1);
1465 assert(req
->ops
!= &reqops_unit_attention
);
1467 if (!req
->bus
->info
->fail
) {
1468 status
= scsi_sense_from_host_status(req
->host_status
, &sense
);
1469 if (status
== CHECK_CONDITION
) {
1470 scsi_req_build_sense(req
, sense
);
1472 scsi_req_complete(req
, status
);
1476 req
->host_status
= host_status
;
1478 scsi_req_dequeue(req
);
1479 req
->bus
->info
->fail(req
);
1481 /* Cancelled requests might end up being completed instead of cancelled */
1482 notifier_list_notify(&req
->cancel_notifiers
, req
);
1483 scsi_req_unref(req
);
1486 void scsi_req_complete(SCSIRequest
*req
, int status
)
1488 assert(req
->status
== -1 && req
->host_status
== -1);
1489 req
->status
= status
;
1490 req
->host_status
= SCSI_HOST_OK
;
1492 assert(req
->sense_len
<= sizeof(req
->sense
));
1493 if (status
== GOOD
) {
1497 if (req
->sense_len
) {
1498 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1499 req
->dev
->sense_len
= req
->sense_len
;
1500 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1502 req
->dev
->sense_len
= 0;
1503 req
->dev
->sense_is_ua
= false;
1507 * Unit attention state is now stored in the device's sense buffer
1508 * if the HBA didn't do autosense. Clear the pending unit attention
1511 scsi_clear_unit_attention(req
);
1514 scsi_req_dequeue(req
);
1515 req
->bus
->info
->complete(req
, req
->resid
);
1517 /* Cancelled requests might end up being completed instead of cancelled */
1518 notifier_list_notify(&req
->cancel_notifiers
, req
);
1519 scsi_req_unref(req
);
1522 /* Called by the devices when the request is canceled. */
1523 void scsi_req_cancel_complete(SCSIRequest
*req
)
1525 assert(req
->io_canceled
);
1526 if (req
->bus
->info
->cancel
) {
1527 req
->bus
->info
->cancel(req
);
1529 notifier_list_notify(&req
->cancel_notifiers
, req
);
1530 scsi_req_unref(req
);
1533 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1534 * notifier list, the bus will be notified the requests cancellation is
1537 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1539 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1541 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1543 if (req
->io_canceled
) {
1544 /* A blk_aio_cancel_async is pending; when it finishes,
1545 * scsi_req_cancel_complete will be called and will
1546 * call the notifier we just added. Just wait for that.
1551 /* Dropped in scsi_req_cancel_complete. */
1553 scsi_req_dequeue(req
);
1554 req
->io_canceled
= true;
1556 blk_aio_cancel_async(req
->aiocb
);
1558 scsi_req_cancel_complete(req
);
1562 void scsi_req_cancel(SCSIRequest
*req
)
1564 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1565 if (!req
->enqueued
) {
1568 assert(!req
->io_canceled
);
1569 /* Dropped in scsi_req_cancel_complete. */
1571 scsi_req_dequeue(req
);
1572 req
->io_canceled
= true;
1574 blk_aio_cancel(req
->aiocb
);
1576 scsi_req_cancel_complete(req
);
1580 static int scsi_ua_precedence(SCSISense sense
)
1582 if (sense
.key
!= UNIT_ATTENTION
) {
1585 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1586 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1588 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1589 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1591 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1592 /* These two go with "all others". */
1594 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1595 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1596 * POWER ON OCCURRED = 1
1597 * SCSI BUS RESET OCCURRED = 2
1598 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1599 * I_T NEXUS LOSS OCCURRED = 7
1602 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1603 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1606 return (sense
.asc
<< 8) | sense
.ascq
;
1609 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1612 if (sense
.key
!= UNIT_ATTENTION
) {
1615 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1616 sense
.asc
, sense
.ascq
);
1619 * Override a pre-existing unit attention condition, except for a more
1620 * important reset condition.
1622 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1623 prec2
= scsi_ua_precedence(sense
);
1624 if (prec2
< prec1
) {
1625 sdev
->unit_attention
= sense
;
1629 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1633 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1634 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1635 req
= QTAILQ_FIRST(&sdev
->requests
);
1636 scsi_req_cancel_async(req
, NULL
);
1638 blk_drain(sdev
->conf
.blk
);
1639 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1640 scsi_device_set_ua(sdev
, sense
);
1643 static char *scsibus_get_dev_path(DeviceState
*dev
)
1645 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1646 DeviceState
*hba
= dev
->parent_bus
->parent
;
1650 id
= qdev_get_dev_path(hba
);
1652 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1654 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1660 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1662 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1663 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1664 qdev_fw_name(dev
), d
->id
, d
->lun
);
1667 /* SCSI request list. For simplicity, pv points to the whole device */
1669 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1670 const VMStateField
*field
, JSONWriter
*vmdesc
)
1673 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1676 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1677 assert(!req
->io_canceled
);
1678 assert(req
->status
== -1 && req
->host_status
== -1);
1679 assert(req
->enqueued
);
1681 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1682 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1683 qemu_put_be32s(f
, &req
->tag
);
1684 qemu_put_be32s(f
, &req
->lun
);
1685 if (bus
->info
->save_request
) {
1686 bus
->info
->save_request(f
, req
);
1688 if (req
->ops
->save_request
) {
1689 req
->ops
->save_request(f
, req
);
1692 qemu_put_sbyte(f
, 0);
1697 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1698 const VMStateField
*field
)
1701 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1704 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1705 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1710 qemu_get_buffer(f
, buf
, sizeof(buf
));
1711 qemu_get_be32s(f
, &tag
);
1712 qemu_get_be32s(f
, &lun
);
1713 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1714 req
->retry
= (sbyte
== 1);
1715 if (bus
->info
->load_request
) {
1716 req
->hba_private
= bus
->info
->load_request(f
, req
);
1718 if (req
->ops
->load_request
) {
1719 req
->ops
->load_request(f
, req
);
1722 /* Just restart it later. */
1723 scsi_req_enqueue_internal(req
);
1725 /* At this point, the request will be kept alive by the reference
1726 * added by scsi_req_enqueue_internal, so we can release our reference.
1727 * The HBA of course will add its own reference in the load_request
1728 * callback if it needs to hold on the SCSIRequest.
1730 scsi_req_unref(req
);
1736 static const VMStateInfo vmstate_info_scsi_requests
= {
1737 .name
= "scsi-requests",
1738 .get
= get_scsi_requests
,
1739 .put
= put_scsi_requests
,
1742 static bool scsi_sense_state_needed(void *opaque
)
1744 SCSIDevice
*s
= opaque
;
1746 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1749 static const VMStateDescription vmstate_scsi_sense_state
= {
1750 .name
= "SCSIDevice/sense",
1752 .minimum_version_id
= 1,
1753 .needed
= scsi_sense_state_needed
,
1754 .fields
= (VMStateField
[]) {
1755 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1756 SCSI_SENSE_BUF_SIZE_OLD
,
1757 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1758 VMSTATE_END_OF_LIST()
1762 const VMStateDescription vmstate_scsi_device
= {
1763 .name
= "SCSIDevice",
1765 .minimum_version_id
= 1,
1766 .fields
= (VMStateField
[]) {
1767 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1768 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1769 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1770 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1771 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1772 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1776 .field_exists
= NULL
,
1777 .size
= 0, /* ouch */
1778 .info
= &vmstate_info_scsi_requests
,
1779 .flags
= VMS_SINGLE
,
1782 VMSTATE_END_OF_LIST()
1784 .subsections
= (const VMStateDescription
*[]) {
1785 &vmstate_scsi_sense_state
,
1790 static Property scsi_props
[] = {
1791 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
1792 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
1793 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
1794 DEFINE_PROP_END_OF_LIST(),
1797 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1799 DeviceClass
*k
= DEVICE_CLASS(klass
);
1800 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1801 k
->bus_type
= TYPE_SCSI_BUS
;
1802 k
->realize
= scsi_qdev_realize
;
1803 k
->unrealize
= scsi_qdev_unrealize
;
1804 device_class_set_props(k
, scsi_props
);
1807 static void scsi_dev_instance_init(Object
*obj
)
1809 DeviceState
*dev
= DEVICE(obj
);
1810 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1812 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1817 static const TypeInfo scsi_device_type_info
= {
1818 .name
= TYPE_SCSI_DEVICE
,
1819 .parent
= TYPE_DEVICE
,
1820 .instance_size
= sizeof(SCSIDevice
),
1822 .class_size
= sizeof(SCSIDeviceClass
),
1823 .class_init
= scsi_device_class_init
,
1824 .instance_init
= scsi_dev_instance_init
,
1827 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
1829 BusClass
*k
= BUS_CLASS(klass
);
1830 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1832 k
->get_dev_path
= scsibus_get_dev_path
;
1833 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
1834 k
->check_address
= scsi_bus_check_address
;
1835 hc
->unplug
= qdev_simple_device_unplug_cb
;
1838 static const TypeInfo scsi_bus_info
= {
1839 .name
= TYPE_SCSI_BUS
,
1841 .instance_size
= sizeof(SCSIBus
),
1842 .class_init
= scsi_bus_class_init
,
1843 .interfaces
= (InterfaceInfo
[]) {
1844 { TYPE_HOTPLUG_HANDLER
},
1849 static void scsi_register_types(void)
1851 type_register_static(&scsi_bus_info
);
1852 type_register_static(&scsi_device_type_info
);
1855 type_init(scsi_register_types
)