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
));
175 void scsi_req_retry(SCSIRequest
*req
)
177 /* No need to save a reference, because scsi_dma_restart_bh just
178 * looks at the request list. */
182 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
184 SCSIDevice
*s
= opaque
;
190 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
191 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
192 qemu_bh_schedule(s
->bh
);
196 static bool scsi_bus_is_address_free(SCSIBus
*bus
,
197 int channel
, int target
, int lun
,
202 RCU_READ_LOCK_GUARD();
203 d
= do_scsi_device_find(bus
, channel
, target
, lun
, true);
204 if (d
&& d
->lun
== lun
) {
216 static bool scsi_bus_check_address(BusState
*qbus
, DeviceState
*qdev
, Error
**errp
)
218 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
219 SCSIBus
*bus
= SCSI_BUS(qbus
);
221 if (dev
->channel
> bus
->info
->max_channel
) {
222 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
225 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
226 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
229 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
230 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
234 if (dev
->id
!= -1 && dev
->lun
!= -1) {
236 if (!scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, dev
->lun
, &d
)) {
237 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
245 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
247 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
248 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
250 Error
*local_err
= NULL
;
254 if (dev
->lun
== -1) {
258 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, ++id
, dev
->lun
, NULL
);
259 } while (!is_free
&& id
< bus
->info
->max_target
);
261 error_setg(errp
, "no free target");
265 } else if (dev
->lun
== -1) {
268 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, ++lun
, NULL
);
269 } while (!is_free
&& lun
< bus
->info
->max_lun
);
271 error_setg(errp
, "no free lun");
277 QTAILQ_INIT(&dev
->requests
);
278 scsi_device_realize(dev
, &local_err
);
280 error_propagate(errp
, local_err
);
283 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
284 scsi_dma_restart_cb
, dev
);
287 static void scsi_qdev_unrealize(DeviceState
*qdev
)
289 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
292 qemu_del_vm_change_state_handler(dev
->vmsentry
);
295 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
297 scsi_device_unrealize(dev
);
299 blockdev_mark_auto_del(dev
->conf
.blk
);
302 /* handle legacy '-drive if=scsi,...' cmd line args */
303 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
304 int unit
, bool removable
, int bootindex
,
306 BlockdevOnError rerror
,
307 BlockdevOnError werror
,
308 const char *serial
, Error
**errp
)
315 if (blk_is_sg(blk
)) {
316 driver
= "scsi-generic";
318 dinfo
= blk_legacy_dinfo(blk
);
319 if (dinfo
&& dinfo
->media_cd
) {
325 dev
= qdev_new(driver
);
326 name
= g_strdup_printf("legacy[%d]", unit
);
327 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
));
330 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
331 if (bootindex
>= 0) {
332 object_property_set_int(OBJECT(dev
), "bootindex", bootindex
,
335 if (object_property_find(OBJECT(dev
), "removable")) {
336 qdev_prop_set_bit(dev
, "removable", removable
);
338 if (serial
&& object_property_find(OBJECT(dev
), "serial")) {
339 qdev_prop_set_string(dev
, "serial", serial
);
341 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, errp
)) {
342 object_unparent(OBJECT(dev
));
345 if (!object_property_set_bool(OBJECT(dev
), "share-rw", share_rw
, errp
)) {
346 object_unparent(OBJECT(dev
));
350 qdev_prop_set_enum(dev
, "rerror", rerror
);
351 qdev_prop_set_enum(dev
, "werror", werror
);
353 if (!qdev_realize_and_unref(dev
, &bus
->qbus
, errp
)) {
354 object_unparent(OBJECT(dev
));
357 return SCSI_DEVICE(dev
);
360 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
367 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
368 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
372 qemu_opts_loc_restore(dinfo
->opts
);
373 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
374 unit
, false, -1, false,
375 BLOCKDEV_ON_ERROR_AUTO
,
376 BLOCKDEV_ON_ERROR_AUTO
,
382 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
384 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
385 scsi_req_complete(req
, CHECK_CONDITION
);
389 static const struct SCSIReqOps reqops_invalid_field
= {
390 .size
= sizeof(SCSIRequest
),
391 .send_command
= scsi_invalid_field
394 /* SCSIReqOps implementation for invalid commands. */
396 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
398 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
399 scsi_req_complete(req
, CHECK_CONDITION
);
403 static const struct SCSIReqOps reqops_invalid_opcode
= {
404 .size
= sizeof(SCSIRequest
),
405 .send_command
= scsi_invalid_command
408 /* SCSIReqOps implementation for unit attention conditions. */
410 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
412 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
413 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
414 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
415 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
417 scsi_req_complete(req
, CHECK_CONDITION
);
421 static const struct SCSIReqOps reqops_unit_attention
= {
422 .size
= sizeof(SCSIRequest
),
423 .send_command
= scsi_unit_attention
426 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
429 typedef struct SCSITargetReq SCSITargetReq
;
431 struct SCSITargetReq
{
438 static void store_lun(uint8_t *outbuf
, int lun
)
441 /* Simple logical unit addressing method*/
445 /* Flat space addressing method */
446 outbuf
[0] = 0x40 | (lun
>> 8);
447 outbuf
[1] = (lun
& 255);
451 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
455 uint8_t tmp
[8] = {0};
459 if (r
->req
.cmd
.xfer
< 16) {
462 if (r
->req
.cmd
.buf
[2] > 2) {
466 /* reserve space for 63 LUNs*/
467 buf
= g_byte_array_sized_new(512);
469 channel
= r
->req
.dev
->channel
;
472 /* add size (will be updated later to correct value */
473 g_byte_array_append(buf
, tmp
, 8);
477 g_byte_array_append(buf
, tmp
, 8);
480 WITH_RCU_READ_LOCK_GUARD() {
481 QTAILQ_FOREACH_RCU(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
482 DeviceState
*qdev
= kid
->child
;
483 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
485 if (dev
->channel
== channel
&& dev
->id
== id
&& dev
->lun
!= 0) {
486 store_lun(tmp
, dev
->lun
);
487 g_byte_array_append(buf
, tmp
, 8);
494 r
->buf
= g_byte_array_free(buf
, FALSE
);
495 r
->len
= MIN(len
, r
->req
.cmd
.xfer
& ~7);
497 /* store the LUN list length */
498 stl_be_p(&r
->buf
[0], len
- 8);
502 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
504 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
506 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
508 if (r
->req
.cmd
.buf
[1] & 0x2) {
509 /* Command support data - optional, not implemented */
513 if (r
->req
.cmd
.buf
[1] & 0x1) {
514 /* Vital product data */
515 uint8_t page_code
= r
->req
.cmd
.buf
[2];
516 r
->buf
[r
->len
++] = page_code
; /* this page */
517 r
->buf
[r
->len
++] = 0x00;
520 case 0x00: /* Supported page codes, mandatory */
524 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
525 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
532 assert(r
->len
< r
->buf_len
);
533 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
537 /* Standard INQUIRY data */
538 if (r
->req
.cmd
.buf
[2] != 0) {
543 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
544 memset(r
->buf
, 0, r
->len
);
545 if (r
->req
.lun
!= 0) {
546 r
->buf
[0] = TYPE_NO_LUN
;
548 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
549 r
->buf
[2] = 5; /* Version */
550 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
551 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
552 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
553 memcpy(&r
->buf
[8], "QEMU ", 8);
554 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
555 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
560 static size_t scsi_sense_len(SCSIRequest
*req
)
562 if (req
->dev
->type
== TYPE_SCANNER
)
563 return SCSI_SENSE_LEN_SCANNER
;
565 return SCSI_SENSE_LEN
;
568 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
570 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
571 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
574 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
575 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
576 scsi_req_complete(req
, CHECK_CONDITION
);
581 if (!scsi_target_emulate_report_luns(r
)) {
582 goto illegal_request
;
586 if (!scsi_target_emulate_inquiry(r
)) {
587 goto illegal_request
;
591 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
593 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
595 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
598 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
599 MIN(req
->cmd
.xfer
, r
->buf_len
),
602 if (r
->req
.dev
->sense_is_ua
) {
603 scsi_device_unit_attention_reported(req
->dev
);
604 r
->req
.dev
->sense_len
= 0;
605 r
->req
.dev
->sense_is_ua
= false;
608 case TEST_UNIT_READY
:
611 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
612 scsi_req_complete(req
, CHECK_CONDITION
);
615 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
616 scsi_req_complete(req
, CHECK_CONDITION
);
621 scsi_req_complete(req
, GOOD
);
626 static void scsi_target_read_data(SCSIRequest
*req
)
628 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
634 scsi_req_data(&r
->req
, n
);
636 scsi_req_complete(&r
->req
, GOOD
);
640 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
642 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
647 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
649 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
651 r
->buf
= g_malloc(len
);
657 static void scsi_target_free_buf(SCSIRequest
*req
)
659 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
664 static const struct SCSIReqOps reqops_target_command
= {
665 .size
= sizeof(SCSITargetReq
),
666 .send_command
= scsi_target_send_command
,
667 .read_data
= scsi_target_read_data
,
668 .get_buf
= scsi_target_get_buf
,
669 .free_req
= scsi_target_free_buf
,
673 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
674 uint32_t tag
, uint32_t lun
, void *hba_private
)
677 SCSIBus
*bus
= scsi_bus_from_device(d
);
678 BusState
*qbus
= BUS(bus
);
679 const int memset_off
= offsetof(SCSIRequest
, sense
)
680 + sizeof(req
->sense
);
682 req
= g_malloc(reqops
->size
);
683 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
689 req
->hba_private
= hba_private
;
692 object_ref(OBJECT(d
));
693 object_ref(OBJECT(qbus
->parent
));
694 notifier_list_init(&req
->cancel_notifiers
);
695 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
699 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
700 uint8_t *buf
, void *hba_private
)
702 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
703 const SCSIReqOps
*ops
;
704 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
706 SCSICommand cmd
= { .len
= 0 };
709 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
710 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
711 (buf
[0] != INQUIRY
&&
712 buf
[0] != REPORT_LUNS
&&
713 buf
[0] != GET_CONFIGURATION
&&
714 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
717 * If we already have a pending unit attention condition,
718 * report this one before triggering another one.
720 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
721 ops
= &reqops_unit_attention
;
722 } else if (lun
!= d
->lun
||
723 buf
[0] == REPORT_LUNS
||
724 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
725 ops
= &reqops_target_command
;
730 if (ops
!= NULL
|| !sc
->parse_cdb
) {
731 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
733 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
737 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
738 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
740 assert(cmd
.len
!= 0);
741 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
744 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
748 if (cmd
.xfer
> INT32_MAX
) {
749 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
751 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
753 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
758 req
->resid
= req
->cmd
.xfer
;
762 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
764 case TEST_UNIT_READY
:
765 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
768 trace_scsi_report_luns(d
->id
, lun
, tag
);
771 trace_scsi_request_sense(d
->id
, lun
, tag
);
780 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
782 return req
->ops
->get_buf(req
);
785 static void scsi_clear_unit_attention(SCSIRequest
*req
)
788 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
789 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
794 * If an INQUIRY command enters the enabled command state,
795 * the device server shall [not] clear any unit attention condition;
796 * See also MMC-6, paragraphs 6.5 and 6.6.2.
798 if (req
->cmd
.buf
[0] == INQUIRY
||
799 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
800 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
804 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
805 ua
= &req
->dev
->unit_attention
;
807 ua
= &req
->bus
->unit_attention
;
811 * If a REPORT LUNS command enters the enabled command state, [...]
812 * the device server shall clear any pending unit attention condition
813 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
815 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
816 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
817 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
821 *ua
= SENSE_CODE(NO_SENSE
);
824 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
829 if (!req
->sense_len
) {
833 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
836 * FIXME: clearing unit attention conditions upon autosense should be done
837 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
840 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
841 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
842 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
844 if (req
->dev
->sense_is_ua
) {
845 scsi_device_unit_attention_reported(req
->dev
);
846 req
->dev
->sense_len
= 0;
847 req
->dev
->sense_is_ua
= false;
852 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
854 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
857 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
859 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
860 sense
.key
, sense
.asc
, sense
.ascq
);
861 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
864 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
866 assert(!req
->enqueued
);
868 if (req
->bus
->info
->get_sg_list
) {
869 req
->sg
= req
->bus
->info
->get_sg_list(req
);
873 req
->enqueued
= true;
874 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
877 int32_t scsi_req_enqueue(SCSIRequest
*req
)
882 scsi_req_enqueue_internal(req
);
884 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
889 static void scsi_req_dequeue(SCSIRequest
*req
)
891 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
894 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
895 req
->enqueued
= false;
900 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
902 /* MMC-6, paragraph 6.7. */
905 if ((data_type
& 3) == 0) {
906 /* Each descriptor is as in Table 295 - Nominal performance. */
907 return 16 * num_desc
+ 8;
909 /* Each descriptor is as in Table 296 - Exceptions. */
910 return 6 * num_desc
+ 8;
915 return 8 * num_desc
+ 8;
917 return 2048 * num_desc
+ 8;
919 return 16 * num_desc
+ 8;
925 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
927 int byte_block
= (buf
[2] >> 2) & 0x1;
928 int type
= (buf
[2] >> 4) & 0x1;
933 xfer_unit
= dev
->blocksize
;
944 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
946 int length
= buf
[2] & 0x3;
948 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
952 case 3: /* USB-specific. */
967 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
969 int extend
= buf
[1] & 0x1;
970 int length
= buf
[2] & 0x3;
972 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
976 case 3: /* USB-specific. */
982 xfer
|= (extend
? buf
[3] << 8 : 0);
986 xfer
|= (extend
? buf
[5] << 8 : 0);
993 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
995 cmd
->xfer
= scsi_cdb_xfer(buf
);
997 case TEST_UNIT_READY
:
1001 case WRITE_FILEMARKS
:
1002 case WRITE_FILEMARKS_16
:
1007 case ALLOW_MEDIUM_REMOVAL
:
1009 case SYNCHRONIZE_CACHE
:
1010 case SYNCHRONIZE_CACHE_16
:
1012 case LOCK_UNLOCK_CACHE
:
1018 case SET_READ_AHEAD
:
1021 case ALLOW_OVERWRITE
:
1027 if ((buf
[1] & 2) == 0) {
1029 } else if ((buf
[1] & 4) != 0) {
1032 cmd
->xfer
*= dev
->blocksize
;
1038 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
1040 case READ_CAPACITY_10
:
1043 case READ_BLOCK_LIMITS
:
1046 case SEND_VOLUME_TAG
:
1047 /* GPCMD_SET_STREAMING from multimedia commands. */
1048 if (dev
->type
== TYPE_ROM
) {
1049 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1051 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1055 /* length 0 means 256 blocks */
1056 if (cmd
->xfer
== 0) {
1061 case WRITE_VERIFY_10
:
1063 case WRITE_VERIFY_12
:
1065 case WRITE_VERIFY_16
:
1066 cmd
->xfer
*= dev
->blocksize
;
1070 /* length 0 means 256 blocks */
1071 if (cmd
->xfer
== 0) {
1078 cmd
->xfer
*= dev
->blocksize
;
1081 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1082 * for block devices are restricted to the header right now. */
1083 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1086 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1090 case RECEIVE_DIAGNOSTIC
:
1091 case SEND_DIAGNOSTIC
:
1092 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1097 case SEND_CUE_SHEET
:
1098 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1100 case PERSISTENT_RESERVE_OUT
:
1101 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1104 if (dev
->type
== TYPE_ROM
) {
1105 /* MMC command GET PERFORMANCE. */
1106 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1107 buf
[10], buf
[1] & 0x1f);
1110 case MECHANISM_STATUS
:
1111 case READ_DVD_STRUCTURE
:
1112 case SEND_DVD_STRUCTURE
:
1113 case MAINTENANCE_OUT
:
1114 case MAINTENANCE_IN
:
1115 if (dev
->type
== TYPE_ROM
) {
1116 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1117 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1120 case ATA_PASSTHROUGH_12
:
1121 if (dev
->type
== TYPE_ROM
) {
1122 /* BLANK command of MMC */
1125 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1128 case ATA_PASSTHROUGH_16
:
1129 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1135 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1138 /* stream commands */
1145 case RECOVER_BUFFERED_DATA
:
1147 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1148 if (buf
[1] & 0x01) { /* fixed */
1149 cmd
->xfer
*= dev
->blocksize
;
1153 case READ_REVERSE_16
:
1156 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1157 if (buf
[1] & 0x01) { /* fixed */
1158 cmd
->xfer
*= dev
->blocksize
;
1166 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1169 switch (buf
[1] & 0x1f) /* operation code */ {
1170 case SHORT_FORM_BLOCK_ID
:
1171 case SHORT_FORM_VENDOR_SPECIFIC
:
1178 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1186 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1188 /* generic commands */
1190 return scsi_req_xfer(cmd
, dev
, buf
);
1195 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1198 /* medium changer commands */
1199 case EXCHANGE_MEDIUM
:
1200 case INITIALIZE_ELEMENT_STATUS
:
1201 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1203 case POSITION_TO_ELEMENT
:
1206 case READ_ELEMENT_STATUS
:
1207 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1210 /* generic commands */
1212 return scsi_req_xfer(cmd
, dev
, buf
);
1217 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1220 /* Scanner commands */
1221 case OBJECT_POSITION
:
1231 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1234 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1235 return scsi_req_xfer(cmd
, dev
, buf
);
1241 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1244 cmd
->mode
= SCSI_XFER_NONE
;
1247 switch (cmd
->buf
[0]) {
1250 case WRITE_VERIFY_10
:
1252 case WRITE_VERIFY_12
:
1254 case WRITE_VERIFY_16
:
1261 case CHANGE_DEFINITION
:
1264 case MODE_SELECT_10
:
1265 case SEND_DIAGNOSTIC
:
1268 case REASSIGN_BLOCKS
:
1277 case SEARCH_HIGH_12
:
1278 case SEARCH_EQUAL_12
:
1281 case SEND_VOLUME_TAG
:
1282 case SEND_CUE_SHEET
:
1283 case SEND_DVD_STRUCTURE
:
1284 case PERSISTENT_RESERVE_OUT
:
1285 case MAINTENANCE_OUT
:
1288 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1289 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1291 cmd
->mode
= SCSI_XFER_TO_DEV
;
1293 case ATA_PASSTHROUGH_12
:
1294 case ATA_PASSTHROUGH_16
:
1296 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1297 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1300 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1305 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1311 len
= scsi_cdb_length(buf
);
1317 switch (dev
->type
) {
1319 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1321 case TYPE_MEDIUM_CHANGER
:
1322 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1325 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1328 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1335 memcpy(cmd
->buf
, buf
, cmd
->len
);
1336 scsi_cmd_xfer_mode(cmd
);
1337 cmd
->lba
= scsi_cmd_lba(cmd
);
1341 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1343 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1345 scsi_device_set_ua(dev
, sense
);
1346 if (bus
->info
->change
) {
1347 bus
->info
->change(bus
, dev
, sense
);
1351 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1353 assert(req
->refcount
> 0);
1358 void scsi_req_unref(SCSIRequest
*req
)
1360 assert(req
->refcount
> 0);
1361 if (--req
->refcount
== 0) {
1362 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1363 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1365 if (bus
->info
->free_request
&& req
->hba_private
) {
1366 bus
->info
->free_request(bus
, req
->hba_private
);
1368 if (req
->ops
->free_req
) {
1369 req
->ops
->free_req(req
);
1371 object_unref(OBJECT(req
->dev
));
1372 object_unref(OBJECT(qbus
->parent
));
1377 /* Tell the device that we finished processing this chunk of I/O. It
1378 will start the next chunk or complete the command. */
1379 void scsi_req_continue(SCSIRequest
*req
)
1381 if (req
->io_canceled
) {
1382 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1385 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1386 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1387 req
->ops
->write_data(req
);
1389 req
->ops
->read_data(req
);
1393 /* Called by the devices when data is ready for the HBA. The HBA should
1394 start a DMA operation to read or fill the device's data buffer.
1395 Once it completes, calling scsi_req_continue will restart I/O. */
1396 void scsi_req_data(SCSIRequest
*req
, int len
)
1399 if (req
->io_canceled
) {
1400 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1403 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1404 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1407 req
->bus
->info
->transfer_data(req
, len
);
1411 /* If the device calls scsi_req_data and the HBA specified a
1412 * scatter/gather list, the transfer has to happen in a single
1414 assert(!req
->dma_started
);
1415 req
->dma_started
= true;
1417 buf
= scsi_req_get_buf(req
);
1418 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1419 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1421 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1423 scsi_req_continue(req
);
1426 void scsi_req_print(SCSIRequest
*req
)
1431 fprintf(fp
, "[%s id=%d] %s",
1432 req
->dev
->qdev
.parent_bus
->name
,
1434 scsi_command_name(req
->cmd
.buf
[0]));
1435 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1436 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1438 switch (req
->cmd
.mode
) {
1439 case SCSI_XFER_NONE
:
1440 fprintf(fp
, " - none\n");
1442 case SCSI_XFER_FROM_DEV
:
1443 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1445 case SCSI_XFER_TO_DEV
:
1446 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1449 fprintf(fp
, " - Oops\n");
1454 void scsi_req_complete(SCSIRequest
*req
, int status
)
1456 assert(req
->status
== -1);
1457 req
->status
= status
;
1459 assert(req
->sense_len
<= sizeof(req
->sense
));
1460 if (status
== GOOD
) {
1464 if (req
->sense_len
) {
1465 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1466 req
->dev
->sense_len
= req
->sense_len
;
1467 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1469 req
->dev
->sense_len
= 0;
1470 req
->dev
->sense_is_ua
= false;
1474 * Unit attention state is now stored in the device's sense buffer
1475 * if the HBA didn't do autosense. Clear the pending unit attention
1478 scsi_clear_unit_attention(req
);
1481 scsi_req_dequeue(req
);
1482 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
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 /* Called by the devices when the request is canceled. */
1490 void scsi_req_cancel_complete(SCSIRequest
*req
)
1492 assert(req
->io_canceled
);
1493 if (req
->bus
->info
->cancel
) {
1494 req
->bus
->info
->cancel(req
);
1496 notifier_list_notify(&req
->cancel_notifiers
, req
);
1497 scsi_req_unref(req
);
1500 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1501 * notifier list, the bus will be notified the requests cancellation is
1504 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1506 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1508 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1510 if (req
->io_canceled
) {
1511 /* A blk_aio_cancel_async is pending; when it finishes,
1512 * scsi_req_cancel_complete will be called and will
1513 * call the notifier we just added. Just wait for that.
1518 /* Dropped in scsi_req_cancel_complete. */
1520 scsi_req_dequeue(req
);
1521 req
->io_canceled
= true;
1523 blk_aio_cancel_async(req
->aiocb
);
1525 scsi_req_cancel_complete(req
);
1529 void scsi_req_cancel(SCSIRequest
*req
)
1531 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1532 if (!req
->enqueued
) {
1535 assert(!req
->io_canceled
);
1536 /* Dropped in scsi_req_cancel_complete. */
1538 scsi_req_dequeue(req
);
1539 req
->io_canceled
= true;
1541 blk_aio_cancel(req
->aiocb
);
1543 scsi_req_cancel_complete(req
);
1547 static int scsi_ua_precedence(SCSISense sense
)
1549 if (sense
.key
!= UNIT_ATTENTION
) {
1552 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1553 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1555 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1556 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1558 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1559 /* These two go with "all others". */
1561 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1562 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1563 * POWER ON OCCURRED = 1
1564 * SCSI BUS RESET OCCURRED = 2
1565 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1566 * I_T NEXUS LOSS OCCURRED = 7
1569 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1570 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1573 return (sense
.asc
<< 8) | sense
.ascq
;
1576 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1579 if (sense
.key
!= UNIT_ATTENTION
) {
1582 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1583 sense
.asc
, sense
.ascq
);
1586 * Override a pre-existing unit attention condition, except for a more
1587 * important reset condition.
1589 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1590 prec2
= scsi_ua_precedence(sense
);
1591 if (prec2
< prec1
) {
1592 sdev
->unit_attention
= sense
;
1596 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1600 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1601 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1602 req
= QTAILQ_FIRST(&sdev
->requests
);
1603 scsi_req_cancel_async(req
, NULL
);
1605 blk_drain(sdev
->conf
.blk
);
1606 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1607 scsi_device_set_ua(sdev
, sense
);
1610 static char *scsibus_get_dev_path(DeviceState
*dev
)
1612 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1613 DeviceState
*hba
= dev
->parent_bus
->parent
;
1617 id
= qdev_get_dev_path(hba
);
1619 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1621 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1627 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1629 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1630 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1631 qdev_fw_name(dev
), d
->id
, d
->lun
);
1634 /* SCSI request list. For simplicity, pv points to the whole device */
1636 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1637 const VMStateField
*field
, QJSON
*vmdesc
)
1640 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1643 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1644 assert(!req
->io_canceled
);
1645 assert(req
->status
== -1);
1646 assert(req
->enqueued
);
1648 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1649 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1650 qemu_put_be32s(f
, &req
->tag
);
1651 qemu_put_be32s(f
, &req
->lun
);
1652 if (bus
->info
->save_request
) {
1653 bus
->info
->save_request(f
, req
);
1655 if (req
->ops
->save_request
) {
1656 req
->ops
->save_request(f
, req
);
1659 qemu_put_sbyte(f
, 0);
1664 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1665 const VMStateField
*field
)
1668 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1671 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1672 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1677 qemu_get_buffer(f
, buf
, sizeof(buf
));
1678 qemu_get_be32s(f
, &tag
);
1679 qemu_get_be32s(f
, &lun
);
1680 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1681 req
->retry
= (sbyte
== 1);
1682 if (bus
->info
->load_request
) {
1683 req
->hba_private
= bus
->info
->load_request(f
, req
);
1685 if (req
->ops
->load_request
) {
1686 req
->ops
->load_request(f
, req
);
1689 /* Just restart it later. */
1690 scsi_req_enqueue_internal(req
);
1692 /* At this point, the request will be kept alive by the reference
1693 * added by scsi_req_enqueue_internal, so we can release our reference.
1694 * The HBA of course will add its own reference in the load_request
1695 * callback if it needs to hold on the SCSIRequest.
1697 scsi_req_unref(req
);
1703 static const VMStateInfo vmstate_info_scsi_requests
= {
1704 .name
= "scsi-requests",
1705 .get
= get_scsi_requests
,
1706 .put
= put_scsi_requests
,
1709 static bool scsi_sense_state_needed(void *opaque
)
1711 SCSIDevice
*s
= opaque
;
1713 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1716 static const VMStateDescription vmstate_scsi_sense_state
= {
1717 .name
= "SCSIDevice/sense",
1719 .minimum_version_id
= 1,
1720 .needed
= scsi_sense_state_needed
,
1721 .fields
= (VMStateField
[]) {
1722 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1723 SCSI_SENSE_BUF_SIZE_OLD
,
1724 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1725 VMSTATE_END_OF_LIST()
1729 const VMStateDescription vmstate_scsi_device
= {
1730 .name
= "SCSIDevice",
1732 .minimum_version_id
= 1,
1733 .fields
= (VMStateField
[]) {
1734 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1735 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1736 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1737 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1738 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1739 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1743 .field_exists
= NULL
,
1744 .size
= 0, /* ouch */
1745 .info
= &vmstate_info_scsi_requests
,
1746 .flags
= VMS_SINGLE
,
1749 VMSTATE_END_OF_LIST()
1751 .subsections
= (const VMStateDescription
*[]) {
1752 &vmstate_scsi_sense_state
,
1757 static Property scsi_props
[] = {
1758 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
1759 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
1760 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
1761 DEFINE_PROP_END_OF_LIST(),
1764 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1766 DeviceClass
*k
= DEVICE_CLASS(klass
);
1767 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1768 k
->bus_type
= TYPE_SCSI_BUS
;
1769 k
->realize
= scsi_qdev_realize
;
1770 k
->unrealize
= scsi_qdev_unrealize
;
1771 device_class_set_props(k
, scsi_props
);
1774 static void scsi_dev_instance_init(Object
*obj
)
1776 DeviceState
*dev
= DEVICE(obj
);
1777 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1779 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1784 static const TypeInfo scsi_device_type_info
= {
1785 .name
= TYPE_SCSI_DEVICE
,
1786 .parent
= TYPE_DEVICE
,
1787 .instance_size
= sizeof(SCSIDevice
),
1789 .class_size
= sizeof(SCSIDeviceClass
),
1790 .class_init
= scsi_device_class_init
,
1791 .instance_init
= scsi_dev_instance_init
,
1794 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
1796 BusClass
*k
= BUS_CLASS(klass
);
1797 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1799 k
->get_dev_path
= scsibus_get_dev_path
;
1800 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
1801 k
->check_address
= scsi_bus_check_address
;
1802 hc
->unplug
= qdev_simple_device_unplug_cb
;
1805 static const TypeInfo scsi_bus_info
= {
1806 .name
= TYPE_SCSI_BUS
,
1808 .instance_size
= sizeof(SCSIBus
),
1809 .class_init
= scsi_bus_class_init
,
1810 .interfaces
= (InterfaceInfo
[]) {
1811 { TYPE_HOTPLUG_HANDLER
},
1816 static void scsi_register_types(void)
1818 type_register_static(&scsi_bus_info
);
1819 type_register_static(&scsi_device_type_info
);
1822 type_init(scsi_register_types
)