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
, int 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
;
696 object_ref(OBJECT(d
));
697 object_ref(OBJECT(qbus
->parent
));
698 notifier_list_init(&req
->cancel_notifiers
);
699 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
703 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
704 uint8_t *buf
, void *hba_private
)
706 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
707 const SCSIReqOps
*ops
;
708 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
710 SCSICommand cmd
= { .len
= 0 };
713 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
714 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
715 (buf
[0] != INQUIRY
&&
716 buf
[0] != REPORT_LUNS
&&
717 buf
[0] != GET_CONFIGURATION
&&
718 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
721 * If we already have a pending unit attention condition,
722 * report this one before triggering another one.
724 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
725 ops
= &reqops_unit_attention
;
726 } else if (lun
!= d
->lun
||
727 buf
[0] == REPORT_LUNS
||
728 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
729 ops
= &reqops_target_command
;
734 if (ops
!= NULL
|| !sc
->parse_cdb
) {
735 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
737 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
741 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
742 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
744 assert(cmd
.len
!= 0);
745 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
748 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
752 if (cmd
.xfer
> INT32_MAX
) {
753 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
755 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
757 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
762 req
->resid
= req
->cmd
.xfer
;
766 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
768 case TEST_UNIT_READY
:
769 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
772 trace_scsi_report_luns(d
->id
, lun
, tag
);
775 trace_scsi_request_sense(d
->id
, lun
, tag
);
784 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
786 return req
->ops
->get_buf(req
);
789 static void scsi_clear_unit_attention(SCSIRequest
*req
)
792 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
793 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
798 * If an INQUIRY command enters the enabled command state,
799 * the device server shall [not] clear any unit attention condition;
800 * See also MMC-6, paragraphs 6.5 and 6.6.2.
802 if (req
->cmd
.buf
[0] == INQUIRY
||
803 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
804 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
808 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
809 ua
= &req
->dev
->unit_attention
;
811 ua
= &req
->bus
->unit_attention
;
815 * If a REPORT LUNS command enters the enabled command state, [...]
816 * the device server shall clear any pending unit attention condition
817 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
819 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
820 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
821 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
825 *ua
= SENSE_CODE(NO_SENSE
);
828 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
833 if (!req
->sense_len
) {
837 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
840 * FIXME: clearing unit attention conditions upon autosense should be done
841 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
844 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
845 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
846 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
848 if (req
->dev
->sense_is_ua
) {
849 scsi_device_unit_attention_reported(req
->dev
);
850 req
->dev
->sense_len
= 0;
851 req
->dev
->sense_is_ua
= false;
856 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
858 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
861 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
863 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
864 sense
.key
, sense
.asc
, sense
.ascq
);
865 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
868 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
870 assert(!req
->enqueued
);
872 if (req
->bus
->info
->get_sg_list
) {
873 req
->sg
= req
->bus
->info
->get_sg_list(req
);
877 req
->enqueued
= true;
878 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
881 int32_t scsi_req_enqueue(SCSIRequest
*req
)
886 scsi_req_enqueue_internal(req
);
888 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
893 static void scsi_req_dequeue(SCSIRequest
*req
)
895 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
898 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
899 req
->enqueued
= false;
904 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
906 /* MMC-6, paragraph 6.7. */
909 if ((data_type
& 3) == 0) {
910 /* Each descriptor is as in Table 295 - Nominal performance. */
911 return 16 * num_desc
+ 8;
913 /* Each descriptor is as in Table 296 - Exceptions. */
914 return 6 * num_desc
+ 8;
919 return 8 * num_desc
+ 8;
921 return 2048 * num_desc
+ 8;
923 return 16 * num_desc
+ 8;
929 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
931 int byte_block
= (buf
[2] >> 2) & 0x1;
932 int type
= (buf
[2] >> 4) & 0x1;
937 xfer_unit
= dev
->blocksize
;
948 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
950 int length
= buf
[2] & 0x3;
952 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
956 case 3: /* USB-specific. */
971 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
973 int extend
= buf
[1] & 0x1;
974 int length
= buf
[2] & 0x3;
976 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
980 case 3: /* USB-specific. */
986 xfer
|= (extend
? buf
[3] << 8 : 0);
990 xfer
|= (extend
? buf
[5] << 8 : 0);
997 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
999 cmd
->xfer
= scsi_cdb_xfer(buf
);
1001 case TEST_UNIT_READY
:
1005 case WRITE_FILEMARKS
:
1006 case WRITE_FILEMARKS_16
:
1011 case ALLOW_MEDIUM_REMOVAL
:
1013 case SYNCHRONIZE_CACHE
:
1014 case SYNCHRONIZE_CACHE_16
:
1016 case LOCK_UNLOCK_CACHE
:
1022 case SET_READ_AHEAD
:
1025 case ALLOW_OVERWRITE
:
1031 if ((buf
[1] & 2) == 0) {
1033 } else if ((buf
[1] & 4) != 0) {
1036 cmd
->xfer
*= dev
->blocksize
;
1042 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
1044 case READ_CAPACITY_10
:
1047 case READ_BLOCK_LIMITS
:
1050 case SEND_VOLUME_TAG
:
1051 /* GPCMD_SET_STREAMING from multimedia commands. */
1052 if (dev
->type
== TYPE_ROM
) {
1053 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1055 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1059 /* length 0 means 256 blocks */
1060 if (cmd
->xfer
== 0) {
1065 case WRITE_VERIFY_10
:
1067 case WRITE_VERIFY_12
:
1069 case WRITE_VERIFY_16
:
1070 cmd
->xfer
*= dev
->blocksize
;
1074 /* length 0 means 256 blocks */
1075 if (cmd
->xfer
== 0) {
1082 cmd
->xfer
*= dev
->blocksize
;
1085 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1086 * for block devices are restricted to the header right now. */
1087 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1090 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1094 case RECEIVE_DIAGNOSTIC
:
1095 case SEND_DIAGNOSTIC
:
1096 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1101 case SEND_CUE_SHEET
:
1102 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1104 case PERSISTENT_RESERVE_OUT
:
1105 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1108 if (dev
->type
== TYPE_ROM
) {
1109 /* MMC command GET PERFORMANCE. */
1110 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1111 buf
[10], buf
[1] & 0x1f);
1114 case MECHANISM_STATUS
:
1115 case READ_DVD_STRUCTURE
:
1116 case SEND_DVD_STRUCTURE
:
1117 case MAINTENANCE_OUT
:
1118 case MAINTENANCE_IN
:
1119 if (dev
->type
== TYPE_ROM
) {
1120 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1121 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1124 case ATA_PASSTHROUGH_12
:
1125 if (dev
->type
== TYPE_ROM
) {
1126 /* BLANK command of MMC */
1129 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1132 case ATA_PASSTHROUGH_16
:
1133 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1139 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1142 /* stream commands */
1149 case RECOVER_BUFFERED_DATA
:
1151 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1152 if (buf
[1] & 0x01) { /* fixed */
1153 cmd
->xfer
*= dev
->blocksize
;
1157 case READ_REVERSE_16
:
1160 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1161 if (buf
[1] & 0x01) { /* fixed */
1162 cmd
->xfer
*= dev
->blocksize
;
1170 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1173 switch (buf
[1] & 0x1f) /* operation code */ {
1174 case SHORT_FORM_BLOCK_ID
:
1175 case SHORT_FORM_VENDOR_SPECIFIC
:
1182 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1190 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1192 /* generic commands */
1194 return scsi_req_xfer(cmd
, dev
, buf
);
1199 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1202 /* medium changer commands */
1203 case EXCHANGE_MEDIUM
:
1204 case INITIALIZE_ELEMENT_STATUS
:
1205 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1207 case POSITION_TO_ELEMENT
:
1210 case READ_ELEMENT_STATUS
:
1211 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1214 /* generic commands */
1216 return scsi_req_xfer(cmd
, dev
, buf
);
1221 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1224 /* Scanner commands */
1225 case OBJECT_POSITION
:
1235 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1238 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1239 return scsi_req_xfer(cmd
, dev
, buf
);
1245 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1248 cmd
->mode
= SCSI_XFER_NONE
;
1251 switch (cmd
->buf
[0]) {
1254 case WRITE_VERIFY_10
:
1256 case WRITE_VERIFY_12
:
1258 case WRITE_VERIFY_16
:
1265 case CHANGE_DEFINITION
:
1268 case MODE_SELECT_10
:
1269 case SEND_DIAGNOSTIC
:
1272 case REASSIGN_BLOCKS
:
1281 case SEARCH_HIGH_12
:
1282 case SEARCH_EQUAL_12
:
1285 case SEND_VOLUME_TAG
:
1286 case SEND_CUE_SHEET
:
1287 case SEND_DVD_STRUCTURE
:
1288 case PERSISTENT_RESERVE_OUT
:
1289 case MAINTENANCE_OUT
:
1292 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1293 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1295 cmd
->mode
= SCSI_XFER_TO_DEV
;
1297 case ATA_PASSTHROUGH_12
:
1298 case ATA_PASSTHROUGH_16
:
1300 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1301 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1304 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1309 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1315 len
= scsi_cdb_length(buf
);
1321 switch (dev
->type
) {
1323 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1325 case TYPE_MEDIUM_CHANGER
:
1326 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1329 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1332 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1339 memcpy(cmd
->buf
, buf
, cmd
->len
);
1340 scsi_cmd_xfer_mode(cmd
);
1341 cmd
->lba
= scsi_cmd_lba(cmd
);
1345 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1347 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1349 scsi_device_set_ua(dev
, sense
);
1350 if (bus
->info
->change
) {
1351 bus
->info
->change(bus
, dev
, sense
);
1355 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1357 assert(req
->refcount
> 0);
1362 void scsi_req_unref(SCSIRequest
*req
)
1364 assert(req
->refcount
> 0);
1365 if (--req
->refcount
== 0) {
1366 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1367 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1369 if (bus
->info
->free_request
&& req
->hba_private
) {
1370 bus
->info
->free_request(bus
, req
->hba_private
);
1372 if (req
->ops
->free_req
) {
1373 req
->ops
->free_req(req
);
1375 object_unref(OBJECT(req
->dev
));
1376 object_unref(OBJECT(qbus
->parent
));
1381 /* Tell the device that we finished processing this chunk of I/O. It
1382 will start the next chunk or complete the command. */
1383 void scsi_req_continue(SCSIRequest
*req
)
1385 if (req
->io_canceled
) {
1386 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1389 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1390 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1391 req
->ops
->write_data(req
);
1393 req
->ops
->read_data(req
);
1397 /* Called by the devices when data is ready for the HBA. The HBA should
1398 start a DMA operation to read or fill the device's data buffer.
1399 Once it completes, calling scsi_req_continue will restart I/O. */
1400 void scsi_req_data(SCSIRequest
*req
, int len
)
1403 if (req
->io_canceled
) {
1404 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1407 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1408 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1411 req
->bus
->info
->transfer_data(req
, len
);
1415 /* If the device calls scsi_req_data and the HBA specified a
1416 * scatter/gather list, the transfer has to happen in a single
1418 assert(!req
->dma_started
);
1419 req
->dma_started
= true;
1421 buf
= scsi_req_get_buf(req
);
1422 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1423 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1425 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1427 scsi_req_continue(req
);
1430 void scsi_req_print(SCSIRequest
*req
)
1435 fprintf(fp
, "[%s id=%d] %s",
1436 req
->dev
->qdev
.parent_bus
->name
,
1438 scsi_command_name(req
->cmd
.buf
[0]));
1439 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1440 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1442 switch (req
->cmd
.mode
) {
1443 case SCSI_XFER_NONE
:
1444 fprintf(fp
, " - none\n");
1446 case SCSI_XFER_FROM_DEV
:
1447 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1449 case SCSI_XFER_TO_DEV
:
1450 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1453 fprintf(fp
, " - Oops\n");
1458 void scsi_req_complete(SCSIRequest
*req
, int status
)
1460 assert(req
->status
== -1);
1461 req
->status
= status
;
1463 assert(req
->sense_len
<= sizeof(req
->sense
));
1464 if (status
== GOOD
) {
1468 if (req
->sense_len
) {
1469 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1470 req
->dev
->sense_len
= req
->sense_len
;
1471 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1473 req
->dev
->sense_len
= 0;
1474 req
->dev
->sense_is_ua
= false;
1478 * Unit attention state is now stored in the device's sense buffer
1479 * if the HBA didn't do autosense. Clear the pending unit attention
1482 scsi_clear_unit_attention(req
);
1485 scsi_req_dequeue(req
);
1486 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1488 /* Cancelled requests might end up being completed instead of cancelled */
1489 notifier_list_notify(&req
->cancel_notifiers
, req
);
1490 scsi_req_unref(req
);
1493 /* Called by the devices when the request is canceled. */
1494 void scsi_req_cancel_complete(SCSIRequest
*req
)
1496 assert(req
->io_canceled
);
1497 if (req
->bus
->info
->cancel
) {
1498 req
->bus
->info
->cancel(req
);
1500 notifier_list_notify(&req
->cancel_notifiers
, req
);
1501 scsi_req_unref(req
);
1504 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1505 * notifier list, the bus will be notified the requests cancellation is
1508 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1510 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1512 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1514 if (req
->io_canceled
) {
1515 /* A blk_aio_cancel_async is pending; when it finishes,
1516 * scsi_req_cancel_complete will be called and will
1517 * call the notifier we just added. Just wait for that.
1522 /* Dropped in scsi_req_cancel_complete. */
1524 scsi_req_dequeue(req
);
1525 req
->io_canceled
= true;
1527 blk_aio_cancel_async(req
->aiocb
);
1529 scsi_req_cancel_complete(req
);
1533 void scsi_req_cancel(SCSIRequest
*req
)
1535 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1536 if (!req
->enqueued
) {
1539 assert(!req
->io_canceled
);
1540 /* Dropped in scsi_req_cancel_complete. */
1542 scsi_req_dequeue(req
);
1543 req
->io_canceled
= true;
1545 blk_aio_cancel(req
->aiocb
);
1547 scsi_req_cancel_complete(req
);
1551 static int scsi_ua_precedence(SCSISense sense
)
1553 if (sense
.key
!= UNIT_ATTENTION
) {
1556 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1557 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1559 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1560 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1562 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1563 /* These two go with "all others". */
1565 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1566 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1567 * POWER ON OCCURRED = 1
1568 * SCSI BUS RESET OCCURRED = 2
1569 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1570 * I_T NEXUS LOSS OCCURRED = 7
1573 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1574 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1577 return (sense
.asc
<< 8) | sense
.ascq
;
1580 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1583 if (sense
.key
!= UNIT_ATTENTION
) {
1586 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1587 sense
.asc
, sense
.ascq
);
1590 * Override a pre-existing unit attention condition, except for a more
1591 * important reset condition.
1593 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1594 prec2
= scsi_ua_precedence(sense
);
1595 if (prec2
< prec1
) {
1596 sdev
->unit_attention
= sense
;
1600 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1604 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1605 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1606 req
= QTAILQ_FIRST(&sdev
->requests
);
1607 scsi_req_cancel_async(req
, NULL
);
1609 blk_drain(sdev
->conf
.blk
);
1610 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1611 scsi_device_set_ua(sdev
, sense
);
1614 static char *scsibus_get_dev_path(DeviceState
*dev
)
1616 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1617 DeviceState
*hba
= dev
->parent_bus
->parent
;
1621 id
= qdev_get_dev_path(hba
);
1623 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1625 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1631 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1633 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1634 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1635 qdev_fw_name(dev
), d
->id
, d
->lun
);
1638 /* SCSI request list. For simplicity, pv points to the whole device */
1640 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1641 const VMStateField
*field
, JSONWriter
*vmdesc
)
1644 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1647 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1648 assert(!req
->io_canceled
);
1649 assert(req
->status
== -1);
1650 assert(req
->enqueued
);
1652 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1653 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1654 qemu_put_be32s(f
, &req
->tag
);
1655 qemu_put_be32s(f
, &req
->lun
);
1656 if (bus
->info
->save_request
) {
1657 bus
->info
->save_request(f
, req
);
1659 if (req
->ops
->save_request
) {
1660 req
->ops
->save_request(f
, req
);
1663 qemu_put_sbyte(f
, 0);
1668 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1669 const VMStateField
*field
)
1672 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1675 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1676 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1681 qemu_get_buffer(f
, buf
, sizeof(buf
));
1682 qemu_get_be32s(f
, &tag
);
1683 qemu_get_be32s(f
, &lun
);
1684 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1685 req
->retry
= (sbyte
== 1);
1686 if (bus
->info
->load_request
) {
1687 req
->hba_private
= bus
->info
->load_request(f
, req
);
1689 if (req
->ops
->load_request
) {
1690 req
->ops
->load_request(f
, req
);
1693 /* Just restart it later. */
1694 scsi_req_enqueue_internal(req
);
1696 /* At this point, the request will be kept alive by the reference
1697 * added by scsi_req_enqueue_internal, so we can release our reference.
1698 * The HBA of course will add its own reference in the load_request
1699 * callback if it needs to hold on the SCSIRequest.
1701 scsi_req_unref(req
);
1707 static const VMStateInfo vmstate_info_scsi_requests
= {
1708 .name
= "scsi-requests",
1709 .get
= get_scsi_requests
,
1710 .put
= put_scsi_requests
,
1713 static bool scsi_sense_state_needed(void *opaque
)
1715 SCSIDevice
*s
= opaque
;
1717 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1720 static const VMStateDescription vmstate_scsi_sense_state
= {
1721 .name
= "SCSIDevice/sense",
1723 .minimum_version_id
= 1,
1724 .needed
= scsi_sense_state_needed
,
1725 .fields
= (VMStateField
[]) {
1726 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1727 SCSI_SENSE_BUF_SIZE_OLD
,
1728 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1729 VMSTATE_END_OF_LIST()
1733 const VMStateDescription vmstate_scsi_device
= {
1734 .name
= "SCSIDevice",
1736 .minimum_version_id
= 1,
1737 .fields
= (VMStateField
[]) {
1738 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1739 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1740 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1741 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1742 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1743 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1747 .field_exists
= NULL
,
1748 .size
= 0, /* ouch */
1749 .info
= &vmstate_info_scsi_requests
,
1750 .flags
= VMS_SINGLE
,
1753 VMSTATE_END_OF_LIST()
1755 .subsections
= (const VMStateDescription
*[]) {
1756 &vmstate_scsi_sense_state
,
1761 static Property scsi_props
[] = {
1762 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
1763 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
1764 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
1765 DEFINE_PROP_END_OF_LIST(),
1768 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1770 DeviceClass
*k
= DEVICE_CLASS(klass
);
1771 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1772 k
->bus_type
= TYPE_SCSI_BUS
;
1773 k
->realize
= scsi_qdev_realize
;
1774 k
->unrealize
= scsi_qdev_unrealize
;
1775 device_class_set_props(k
, scsi_props
);
1778 static void scsi_dev_instance_init(Object
*obj
)
1780 DeviceState
*dev
= DEVICE(obj
);
1781 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1783 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1788 static const TypeInfo scsi_device_type_info
= {
1789 .name
= TYPE_SCSI_DEVICE
,
1790 .parent
= TYPE_DEVICE
,
1791 .instance_size
= sizeof(SCSIDevice
),
1793 .class_size
= sizeof(SCSIDeviceClass
),
1794 .class_init
= scsi_device_class_init
,
1795 .instance_init
= scsi_dev_instance_init
,
1798 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
1800 BusClass
*k
= BUS_CLASS(klass
);
1801 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1803 k
->get_dev_path
= scsibus_get_dev_path
;
1804 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
1805 k
->check_address
= scsi_bus_check_address
;
1806 hc
->unplug
= qdev_simple_device_unplug_cb
;
1809 static const TypeInfo scsi_bus_info
= {
1810 .name
= TYPE_SCSI_BUS
,
1812 .instance_size
= sizeof(SCSIBus
),
1813 .class_init
= scsi_bus_class_init
,
1814 .interfaces
= (InterfaceInfo
[]) {
1815 { TYPE_HOTPLUG_HANDLER
},
1820 static void scsi_register_types(void)
1822 type_register_static(&scsi_bus_info
);
1823 type_register_static(&scsi_device_type_info
);
1826 type_init(scsi_register_types
)