2 #include "qemu/error-report.h"
3 #include "hw/scsi/scsi.h"
4 #include "block/scsi.h"
6 #include "sysemu/blockdev.h"
8 #include "sysemu/dma.h"
10 static char *scsibus_get_dev_path(DeviceState
*dev
);
11 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
12 static void scsi_req_dequeue(SCSIRequest
*req
);
13 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
14 static void scsi_target_free_buf(SCSIRequest
*req
);
16 static Property scsi_props
[] = {
17 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
18 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
19 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
20 DEFINE_PROP_END_OF_LIST(),
23 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
25 BusClass
*k
= BUS_CLASS(klass
);
27 k
->get_dev_path
= scsibus_get_dev_path
;
28 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
31 static const TypeInfo scsi_bus_info
= {
32 .name
= TYPE_SCSI_BUS
,
34 .instance_size
= sizeof(SCSIBus
),
35 .class_init
= scsi_bus_class_init
,
37 static int next_scsi_bus
;
39 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
41 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
47 static void scsi_device_unrealize(SCSIDevice
*s
, Error
**errp
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
51 sc
->unrealize(s
, errp
);
55 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
58 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
61 assert(cmd
->len
== 0);
62 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
63 if (bus
->info
->parse_cdb
) {
64 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
69 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
70 uint8_t *buf
, void *hba_private
)
72 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
74 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
80 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
82 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
83 if (sc
->unit_attention_reported
) {
84 sc
->unit_attention_reported(s
);
88 /* Create a scsi bus, and attach devices to it. */
89 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
90 const SCSIBusInfo
*info
, const char *bus_name
)
92 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
93 bus
->busnr
= next_scsi_bus
++;
95 bus
->qbus
.allow_hotplug
= 1;
98 static void scsi_dma_restart_bh(void *opaque
)
100 SCSIDevice
*s
= opaque
;
101 SCSIRequest
*req
, *next
;
103 qemu_bh_delete(s
->bh
);
106 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
110 switch (req
->cmd
.mode
) {
111 case SCSI_XFER_FROM_DEV
:
112 case SCSI_XFER_TO_DEV
:
113 scsi_req_continue(req
);
116 scsi_req_dequeue(req
);
117 scsi_req_enqueue(req
);
125 void scsi_req_retry(SCSIRequest
*req
)
127 /* No need to save a reference, because scsi_dma_restart_bh just
128 * looks at the request list. */
132 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
134 SCSIDevice
*s
= opaque
;
140 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
141 qemu_bh_schedule(s
->bh
);
145 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
147 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
148 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
150 Error
*local_err
= NULL
;
152 if (dev
->channel
> bus
->info
->max_channel
) {
153 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
156 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
157 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
160 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
161 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
167 if (dev
->lun
== -1) {
171 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
172 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
173 if (d
&& d
->lun
== dev
->lun
) {
174 error_setg(errp
, "no free target");
178 } else if (dev
->lun
== -1) {
181 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
182 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
183 if (d
&& d
->lun
== lun
) {
184 error_setg(errp
, "no free lun");
189 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
191 if (d
->lun
== dev
->lun
&& dev
!= d
) {
192 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
197 QTAILQ_INIT(&dev
->requests
);
198 scsi_device_realize(dev
, &local_err
);
200 error_propagate(errp
, local_err
);
203 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
206 if (bus
->info
->hotplug
) {
207 bus
->info
->hotplug(bus
, dev
);
211 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
213 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
216 qemu_del_vm_change_state_handler(dev
->vmsentry
);
218 scsi_device_unrealize(dev
, errp
);
221 /* handle legacy '-drive if=scsi,...' cmd line args */
222 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
223 int unit
, bool removable
, int bootindex
,
224 const char *serial
, Error
**errp
)
230 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
231 dev
= qdev_create(&bus
->qbus
, driver
);
232 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
233 if (bootindex
>= 0) {
234 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
237 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
238 qdev_prop_set_bit(dev
, "removable", removable
);
240 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
241 qdev_prop_set_string(dev
, "serial", serial
);
243 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
244 error_setg(errp
, "Setting drive property failed");
245 object_unparent(OBJECT(dev
));
248 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
250 error_propagate(errp
, err
);
251 object_unparent(OBJECT(dev
));
254 return SCSI_DEVICE(dev
);
257 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
265 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
266 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
270 qemu_opts_loc_restore(dinfo
->opts
);
271 scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1, NULL
,
274 error_report("%s", error_get_pretty(err
));
275 error_propagate(errp
, err
);
282 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
284 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
285 scsi_req_complete(req
, CHECK_CONDITION
);
289 static const struct SCSIReqOps reqops_invalid_field
= {
290 .size
= sizeof(SCSIRequest
),
291 .send_command
= scsi_invalid_field
294 /* SCSIReqOps implementation for invalid commands. */
296 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
298 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
299 scsi_req_complete(req
, CHECK_CONDITION
);
303 static const struct SCSIReqOps reqops_invalid_opcode
= {
304 .size
= sizeof(SCSIRequest
),
305 .send_command
= scsi_invalid_command
308 /* SCSIReqOps implementation for unit attention conditions. */
310 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
312 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
313 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
314 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
315 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
317 scsi_req_complete(req
, CHECK_CONDITION
);
321 static const struct SCSIReqOps reqops_unit_attention
= {
322 .size
= sizeof(SCSIRequest
),
323 .send_command
= scsi_unit_attention
326 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
329 typedef struct SCSITargetReq SCSITargetReq
;
331 struct SCSITargetReq
{
338 static void store_lun(uint8_t *outbuf
, int lun
)
344 outbuf
[1] = (lun
& 255);
345 outbuf
[0] = (lun
>> 8) | 0x40;
348 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
355 if (r
->req
.cmd
.xfer
< 16) {
358 if (r
->req
.cmd
.buf
[2] > 2) {
361 channel
= r
->req
.dev
->channel
;
365 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
366 DeviceState
*qdev
= kid
->child
;
367 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
369 if (dev
->channel
== channel
&& dev
->id
== id
) {
380 scsi_target_alloc_buf(&r
->req
, n
+ 8);
382 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
383 memset(r
->buf
, 0, len
);
384 stl_be_p(&r
->buf
[0], n
);
385 i
= found_lun0
? 8 : 16;
386 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
387 DeviceState
*qdev
= kid
->child
;
388 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
390 if (dev
->channel
== channel
&& dev
->id
== id
) {
391 store_lun(&r
->buf
[i
], dev
->lun
);
400 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
402 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
404 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
406 if (r
->req
.cmd
.buf
[1] & 0x2) {
407 /* Command support data - optional, not implemented */
411 if (r
->req
.cmd
.buf
[1] & 0x1) {
412 /* Vital product data */
413 uint8_t page_code
= r
->req
.cmd
.buf
[2];
414 r
->buf
[r
->len
++] = page_code
; /* this page */
415 r
->buf
[r
->len
++] = 0x00;
418 case 0x00: /* Supported page codes, mandatory */
422 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
423 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
430 assert(r
->len
< r
->buf_len
);
431 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
435 /* Standard INQUIRY data */
436 if (r
->req
.cmd
.buf
[2] != 0) {
441 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
442 memset(r
->buf
, 0, r
->len
);
443 if (r
->req
.lun
!= 0) {
444 r
->buf
[0] = TYPE_NO_LUN
;
446 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
447 r
->buf
[2] = 5; /* Version */
448 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
449 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
450 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
451 memcpy(&r
->buf
[8], "QEMU ", 8);
452 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
453 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
458 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
460 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
464 if (!scsi_target_emulate_report_luns(r
)) {
465 goto illegal_request
;
469 if (!scsi_target_emulate_inquiry(r
)) {
470 goto illegal_request
;
474 scsi_target_alloc_buf(&r
->req
, SCSI_SENSE_LEN
);
475 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
476 MIN(req
->cmd
.xfer
, r
->buf_len
),
477 (req
->cmd
.buf
[1] & 1) == 0);
478 if (r
->req
.dev
->sense_is_ua
) {
479 scsi_device_unit_attention_reported(req
->dev
);
480 r
->req
.dev
->sense_len
= 0;
481 r
->req
.dev
->sense_is_ua
= false;
484 case TEST_UNIT_READY
:
487 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
488 scsi_req_complete(req
, CHECK_CONDITION
);
491 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
492 scsi_req_complete(req
, CHECK_CONDITION
);
497 scsi_req_complete(req
, GOOD
);
502 static void scsi_target_read_data(SCSIRequest
*req
)
504 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
510 scsi_req_data(&r
->req
, n
);
512 scsi_req_complete(&r
->req
, GOOD
);
516 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
518 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
523 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
525 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
527 r
->buf
= g_malloc(len
);
533 static void scsi_target_free_buf(SCSIRequest
*req
)
535 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
540 static const struct SCSIReqOps reqops_target_command
= {
541 .size
= sizeof(SCSITargetReq
),
542 .send_command
= scsi_target_send_command
,
543 .read_data
= scsi_target_read_data
,
544 .get_buf
= scsi_target_get_buf
,
545 .free_req
= scsi_target_free_buf
,
549 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
550 uint32_t tag
, uint32_t lun
, void *hba_private
)
553 SCSIBus
*bus
= scsi_bus_from_device(d
);
554 BusState
*qbus
= BUS(bus
);
555 const int memset_off
= offsetof(SCSIRequest
, sense
)
556 + sizeof(req
->sense
);
558 req
= g_slice_alloc(reqops
->size
);
559 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
565 req
->hba_private
= hba_private
;
568 object_ref(OBJECT(d
));
569 object_ref(OBJECT(qbus
->parent
));
570 notifier_list_init(&req
->cancel_notifiers
);
571 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
575 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
576 uint8_t *buf
, void *hba_private
)
578 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
579 const SCSIReqOps
*ops
;
580 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
582 SCSICommand cmd
= { .len
= 0 };
585 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
586 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
587 (buf
[0] != INQUIRY
&&
588 buf
[0] != REPORT_LUNS
&&
589 buf
[0] != GET_CONFIGURATION
&&
590 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
593 * If we already have a pending unit attention condition,
594 * report this one before triggering another one.
596 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
597 ops
= &reqops_unit_attention
;
598 } else if (lun
!= d
->lun
||
599 buf
[0] == REPORT_LUNS
||
600 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
601 ops
= &reqops_target_command
;
606 if (ops
!= NULL
|| !sc
->parse_cdb
) {
607 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
609 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
613 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
614 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
616 assert(cmd
.len
!= 0);
617 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
620 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
624 if (cmd
.xfer
> INT32_MAX
) {
625 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
627 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
629 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
634 req
->resid
= req
->cmd
.xfer
;
638 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
640 case TEST_UNIT_READY
:
641 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
644 trace_scsi_report_luns(d
->id
, lun
, tag
);
647 trace_scsi_request_sense(d
->id
, lun
, tag
);
656 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
658 return req
->ops
->get_buf(req
);
661 static void scsi_clear_unit_attention(SCSIRequest
*req
)
664 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
665 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
670 * If an INQUIRY command enters the enabled command state,
671 * the device server shall [not] clear any unit attention condition;
672 * See also MMC-6, paragraphs 6.5 and 6.6.2.
674 if (req
->cmd
.buf
[0] == INQUIRY
||
675 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
676 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
680 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
681 ua
= &req
->dev
->unit_attention
;
683 ua
= &req
->bus
->unit_attention
;
687 * If a REPORT LUNS command enters the enabled command state, [...]
688 * the device server shall clear any pending unit attention condition
689 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
691 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
692 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
693 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
697 *ua
= SENSE_CODE(NO_SENSE
);
700 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
705 if (!req
->sense_len
) {
709 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
712 * FIXME: clearing unit attention conditions upon autosense should be done
713 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
716 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
717 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
718 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
720 if (req
->dev
->sense_is_ua
) {
721 scsi_device_unit_attention_reported(req
->dev
);
722 req
->dev
->sense_len
= 0;
723 req
->dev
->sense_is_ua
= false;
728 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
730 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
733 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
735 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
736 sense
.key
, sense
.asc
, sense
.ascq
);
737 memset(req
->sense
, 0, 18);
738 req
->sense
[0] = 0x70;
739 req
->sense
[2] = sense
.key
;
741 req
->sense
[12] = sense
.asc
;
742 req
->sense
[13] = sense
.ascq
;
746 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
748 assert(!req
->enqueued
);
750 if (req
->bus
->info
->get_sg_list
) {
751 req
->sg
= req
->bus
->info
->get_sg_list(req
);
755 req
->enqueued
= true;
756 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
759 int32_t scsi_req_enqueue(SCSIRequest
*req
)
764 scsi_req_enqueue_internal(req
);
766 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
771 static void scsi_req_dequeue(SCSIRequest
*req
)
773 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
776 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
777 req
->enqueued
= false;
782 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
784 /* MMC-6, paragraph 6.7. */
787 if ((data_type
& 3) == 0) {
788 /* Each descriptor is as in Table 295 - Nominal performance. */
789 return 16 * num_desc
+ 8;
791 /* Each descriptor is as in Table 296 - Exceptions. */
792 return 6 * num_desc
+ 8;
797 return 8 * num_desc
+ 8;
799 return 2048 * num_desc
+ 8;
801 return 16 * num_desc
+ 8;
807 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
809 int byte_block
= (buf
[2] >> 2) & 0x1;
810 int type
= (buf
[2] >> 4) & 0x1;
815 xfer_unit
= dev
->blocksize
;
826 static int ata_passthrough_12_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
828 int length
= buf
[2] & 0x3;
830 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
834 case 3: /* USB-specific. */
849 static int ata_passthrough_16_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
851 int extend
= buf
[1] & 0x1;
852 int length
= buf
[2] & 0x3;
854 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
858 case 3: /* USB-specific. */
864 xfer
|= (extend
? buf
[3] << 8 : 0);
868 xfer
|= (extend
? buf
[5] << 8 : 0);
875 uint32_t scsi_data_cdb_length(uint8_t *buf
)
877 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
880 return scsi_cdb_length(buf
);
884 uint32_t scsi_cdb_length(uint8_t *buf
)
886 switch (buf
[0] >> 5) {
892 return lduw_be_p(&buf
[7]);
895 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
898 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
905 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
907 cmd
->xfer
= scsi_cdb_length(buf
);
909 case TEST_UNIT_READY
:
913 case WRITE_FILEMARKS
:
914 case WRITE_FILEMARKS_16
:
919 case ALLOW_MEDIUM_REMOVAL
:
921 case SYNCHRONIZE_CACHE
:
922 case SYNCHRONIZE_CACHE_16
:
924 case LOCK_UNLOCK_CACHE
:
933 case ALLOW_OVERWRITE
:
939 if ((buf
[1] & 2) == 0) {
941 } else if ((buf
[1] & 4) != 0) {
944 cmd
->xfer
*= dev
->blocksize
;
950 cmd
->xfer
= dev
->blocksize
;
952 case READ_CAPACITY_10
:
955 case READ_BLOCK_LIMITS
:
958 case SEND_VOLUME_TAG
:
959 /* GPCMD_SET_STREAMING from multimedia commands. */
960 if (dev
->type
== TYPE_ROM
) {
961 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
963 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
967 /* length 0 means 256 blocks */
968 if (cmd
->xfer
== 0) {
973 case WRITE_VERIFY_10
:
975 case WRITE_VERIFY_12
:
977 case WRITE_VERIFY_16
:
978 cmd
->xfer
*= dev
->blocksize
;
982 /* length 0 means 256 blocks */
983 if (cmd
->xfer
== 0) {
988 case RECOVER_BUFFERED_DATA
:
991 cmd
->xfer
*= dev
->blocksize
;
994 /* MMC mandates the parameter list to be 12-bytes long. Parameters
995 * for block devices are restricted to the header right now. */
996 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
999 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1003 case RECEIVE_DIAGNOSTIC
:
1004 case SEND_DIAGNOSTIC
:
1005 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1010 case SEND_CUE_SHEET
:
1011 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1013 case PERSISTENT_RESERVE_OUT
:
1014 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1017 if (dev
->type
== TYPE_ROM
) {
1018 /* MMC command GET PERFORMANCE. */
1019 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1020 buf
[10], buf
[1] & 0x1f);
1023 case MECHANISM_STATUS
:
1024 case READ_DVD_STRUCTURE
:
1025 case SEND_DVD_STRUCTURE
:
1026 case MAINTENANCE_OUT
:
1027 case MAINTENANCE_IN
:
1028 if (dev
->type
== TYPE_ROM
) {
1029 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1030 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1033 case ATA_PASSTHROUGH_12
:
1034 if (dev
->type
== TYPE_ROM
) {
1035 /* BLANK command of MMC */
1038 cmd
->xfer
= ata_passthrough_12_xfer_size(dev
, buf
);
1041 case ATA_PASSTHROUGH_16
:
1042 cmd
->xfer
= ata_passthrough_16_xfer_size(dev
, buf
);
1048 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1051 /* stream commands */
1058 case RECOVER_BUFFERED_DATA
:
1060 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1061 if (buf
[1] & 0x01) { /* fixed */
1062 cmd
->xfer
*= dev
->blocksize
;
1066 case READ_REVERSE_16
:
1069 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1070 if (buf
[1] & 0x01) { /* fixed */
1071 cmd
->xfer
*= dev
->blocksize
;
1079 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1082 switch (buf
[1] & 0x1f) /* operation code */ {
1083 case SHORT_FORM_BLOCK_ID
:
1084 case SHORT_FORM_VENDOR_SPECIFIC
:
1091 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1099 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1101 /* generic commands */
1103 return scsi_req_length(cmd
, dev
, buf
);
1108 static int scsi_req_medium_changer_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1111 /* medium changer commands */
1112 case EXCHANGE_MEDIUM
:
1113 case INITIALIZE_ELEMENT_STATUS
:
1114 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1116 case POSITION_TO_ELEMENT
:
1119 case READ_ELEMENT_STATUS
:
1120 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1123 /* generic commands */
1125 return scsi_req_length(cmd
, dev
, buf
);
1131 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1134 cmd
->mode
= SCSI_XFER_NONE
;
1137 switch (cmd
->buf
[0]) {
1140 case WRITE_VERIFY_10
:
1142 case WRITE_VERIFY_12
:
1144 case WRITE_VERIFY_16
:
1151 case CHANGE_DEFINITION
:
1154 case MODE_SELECT_10
:
1155 case SEND_DIAGNOSTIC
:
1158 case REASSIGN_BLOCKS
:
1167 case SEARCH_HIGH_12
:
1168 case SEARCH_EQUAL_12
:
1171 case SEND_VOLUME_TAG
:
1172 case SEND_CUE_SHEET
:
1173 case SEND_DVD_STRUCTURE
:
1174 case PERSISTENT_RESERVE_OUT
:
1175 case MAINTENANCE_OUT
:
1176 cmd
->mode
= SCSI_XFER_TO_DEV
;
1178 case ATA_PASSTHROUGH_12
:
1179 case ATA_PASSTHROUGH_16
:
1181 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1182 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1185 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1190 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1192 uint8_t *buf
= cmd
->buf
;
1195 switch (buf
[0] >> 5) {
1197 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1202 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1205 lba
= ldq_be_p(&buf
[2]);
1214 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1219 switch (buf
[0] >> 5) {
1237 switch (dev
->type
) {
1239 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1241 case TYPE_MEDIUM_CHANGER
:
1242 rc
= scsi_req_medium_changer_length(cmd
, dev
, buf
);
1245 rc
= scsi_req_length(cmd
, dev
, buf
);
1252 memcpy(cmd
->buf
, buf
, cmd
->len
);
1253 scsi_cmd_xfer_mode(cmd
);
1254 cmd
->lba
= scsi_cmd_lba(cmd
);
1258 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1260 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1262 scsi_device_set_ua(dev
, sense
);
1263 if (bus
->info
->change
) {
1264 bus
->info
->change(bus
, dev
, sense
);
1269 * Predefined sense codes
1272 /* No sense data available */
1273 const struct SCSISense sense_code_NO_SENSE
= {
1274 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1277 /* LUN not ready, Manual intervention required */
1278 const struct SCSISense sense_code_LUN_NOT_READY
= {
1279 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1282 /* LUN not ready, Medium not present */
1283 const struct SCSISense sense_code_NO_MEDIUM
= {
1284 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1287 /* LUN not ready, medium removal prevented */
1288 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1289 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1292 /* Hardware error, internal target failure */
1293 const struct SCSISense sense_code_TARGET_FAILURE
= {
1294 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1297 /* Illegal request, invalid command operation code */
1298 const struct SCSISense sense_code_INVALID_OPCODE
= {
1299 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1302 /* Illegal request, LBA out of range */
1303 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1304 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1307 /* Illegal request, Invalid field in CDB */
1308 const struct SCSISense sense_code_INVALID_FIELD
= {
1309 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1312 /* Illegal request, Invalid field in parameter list */
1313 const struct SCSISense sense_code_INVALID_PARAM
= {
1314 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1317 /* Illegal request, Parameter list length error */
1318 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1319 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1322 /* Illegal request, LUN not supported */
1323 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1324 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1327 /* Illegal request, Saving parameters not supported */
1328 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1329 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1332 /* Illegal request, Incompatible medium installed */
1333 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1334 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1337 /* Illegal request, medium removal prevented */
1338 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1339 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1342 /* Illegal request, Invalid Transfer Tag */
1343 const struct SCSISense sense_code_INVALID_TAG
= {
1344 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1347 /* Command aborted, I/O process terminated */
1348 const struct SCSISense sense_code_IO_ERROR
= {
1349 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1352 /* Command aborted, I_T Nexus loss occurred */
1353 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1354 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1357 /* Command aborted, Logical Unit failure */
1358 const struct SCSISense sense_code_LUN_FAILURE
= {
1359 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1362 /* Command aborted, Overlapped Commands Attempted */
1363 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1364 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1367 /* Unit attention, Capacity data has changed */
1368 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1369 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1372 /* Unit attention, Power on, reset or bus device reset occurred */
1373 const struct SCSISense sense_code_RESET
= {
1374 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1377 /* Unit attention, No medium */
1378 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1379 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1382 /* Unit attention, Medium may have changed */
1383 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1384 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1387 /* Unit attention, Reported LUNs data has changed */
1388 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1389 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1392 /* Unit attention, Device internal reset */
1393 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1394 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1397 /* Data Protection, Write Protected */
1398 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1399 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1402 /* Data Protection, Space Allocation Failed Write Protect */
1403 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1404 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1410 * Convert between fixed and descriptor sense buffers
1412 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1413 uint8_t *buf
, int len
, bool fixed
)
1417 if (!fixed
&& len
< 8) {
1422 sense
.key
= NO_SENSE
;
1426 fixed_in
= (in_buf
[0] & 2) == 0;
1428 if (fixed
== fixed_in
) {
1429 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1430 return MIN(len
, in_len
);
1434 sense
.key
= in_buf
[2];
1435 sense
.asc
= in_buf
[12];
1436 sense
.ascq
= in_buf
[13];
1438 sense
.key
= in_buf
[1];
1439 sense
.asc
= in_buf
[2];
1440 sense
.ascq
= in_buf
[3];
1444 memset(buf
, 0, len
);
1446 /* Return fixed format sense buffer */
1450 buf
[12] = sense
.asc
;
1451 buf
[13] = sense
.ascq
;
1452 return MIN(len
, SCSI_SENSE_LEN
);
1454 /* Return descriptor format sense buffer */
1458 buf
[3] = sense
.ascq
;
1463 const char *scsi_command_name(uint8_t cmd
)
1465 static const char *names
[] = {
1466 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1467 [ REWIND
] = "REWIND",
1468 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1469 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1470 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1471 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1472 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1473 [ READ_6
] = "READ_6",
1474 [ WRITE_6
] = "WRITE_6",
1475 [ SET_CAPACITY
] = "SET_CAPACITY",
1476 [ READ_REVERSE
] = "READ_REVERSE",
1477 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1478 [ SPACE
] = "SPACE",
1479 [ INQUIRY
] = "INQUIRY",
1480 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1481 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1482 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1483 [ MODE_SELECT
] = "MODE_SELECT",
1484 [ RESERVE
] = "RESERVE",
1485 [ RELEASE
] = "RELEASE",
1487 [ ERASE
] = "ERASE",
1488 [ MODE_SENSE
] = "MODE_SENSE",
1489 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1490 /* LOAD_UNLOAD and START_STOP use the same operation code */
1491 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1492 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1493 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1494 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1495 [ READ_10
] = "READ_10",
1496 [ WRITE_10
] = "WRITE_10",
1497 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1498 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1499 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1500 [ VERIFY_10
] = "VERIFY_10",
1501 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1502 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1503 [ SEARCH_LOW
] = "SEARCH_LOW",
1504 [ SET_LIMITS
] = "SET_LIMITS",
1505 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1506 /* READ_POSITION and PRE_FETCH use the same operation code */
1507 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1508 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1509 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1510 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1511 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1512 [ COMPARE
] = "COMPARE",
1513 [ COPY_VERIFY
] = "COPY_VERIFY",
1514 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1515 [ READ_BUFFER
] = "READ_BUFFER",
1516 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1517 [ READ_LONG_10
] = "READ_LONG_10",
1518 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1519 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1520 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1521 [ UNMAP
] = "UNMAP",
1522 [ READ_TOC
] = "READ_TOC",
1523 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1524 [ SANITIZE
] = "SANITIZE",
1525 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1526 [ LOG_SELECT
] = "LOG_SELECT",
1527 [ LOG_SENSE
] = "LOG_SENSE",
1528 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1529 [ RESERVE_10
] = "RESERVE_10",
1530 [ RELEASE_10
] = "RELEASE_10",
1531 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1532 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1533 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1534 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1535 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1536 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1537 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1538 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1539 [ READ_16
] = "READ_16",
1540 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1541 [ WRITE_16
] = "WRITE_16",
1542 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1543 [ VERIFY_16
] = "VERIFY_16",
1544 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1545 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1546 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1547 [ LOCATE_16
] = "LOCATE_16",
1548 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1549 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1550 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1551 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1552 [ REPORT_LUNS
] = "REPORT_LUNS",
1553 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1554 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1555 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1556 [ READ_12
] = "READ_12",
1557 [ WRITE_12
] = "WRITE_12",
1558 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1559 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1560 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1561 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1562 [ VERIFY_12
] = "VERIFY_12",
1563 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1564 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1565 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1566 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1567 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1568 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1569 [ READ_CD
] = "READ_CD",
1570 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1571 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1572 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1573 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1574 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1575 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1576 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1577 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1578 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1579 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1580 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1583 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1588 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1590 assert(req
->refcount
> 0);
1595 void scsi_req_unref(SCSIRequest
*req
)
1597 assert(req
->refcount
> 0);
1598 if (--req
->refcount
== 0) {
1599 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1600 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1602 if (bus
->info
->free_request
&& req
->hba_private
) {
1603 bus
->info
->free_request(bus
, req
->hba_private
);
1605 if (req
->ops
->free_req
) {
1606 req
->ops
->free_req(req
);
1608 object_unref(OBJECT(req
->dev
));
1609 object_unref(OBJECT(qbus
->parent
));
1610 g_slice_free1(req
->ops
->size
, req
);
1614 /* Tell the device that we finished processing this chunk of I/O. It
1615 will start the next chunk or complete the command. */
1616 void scsi_req_continue(SCSIRequest
*req
)
1618 if (req
->io_canceled
) {
1619 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1622 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1623 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1624 req
->ops
->write_data(req
);
1626 req
->ops
->read_data(req
);
1630 /* Called by the devices when data is ready for the HBA. The HBA should
1631 start a DMA operation to read or fill the device's data buffer.
1632 Once it completes, calling scsi_req_continue will restart I/O. */
1633 void scsi_req_data(SCSIRequest
*req
, int len
)
1636 if (req
->io_canceled
) {
1637 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1640 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1641 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1644 req
->bus
->info
->transfer_data(req
, len
);
1648 /* If the device calls scsi_req_data and the HBA specified a
1649 * scatter/gather list, the transfer has to happen in a single
1651 assert(!req
->dma_started
);
1652 req
->dma_started
= true;
1654 buf
= scsi_req_get_buf(req
);
1655 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1656 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1658 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1660 scsi_req_continue(req
);
1663 void scsi_req_print(SCSIRequest
*req
)
1668 fprintf(fp
, "[%s id=%d] %s",
1669 req
->dev
->qdev
.parent_bus
->name
,
1671 scsi_command_name(req
->cmd
.buf
[0]));
1672 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1673 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1675 switch (req
->cmd
.mode
) {
1676 case SCSI_XFER_NONE
:
1677 fprintf(fp
, " - none\n");
1679 case SCSI_XFER_FROM_DEV
:
1680 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1682 case SCSI_XFER_TO_DEV
:
1683 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1686 fprintf(fp
, " - Oops\n");
1691 void scsi_req_complete(SCSIRequest
*req
, int status
)
1693 assert(req
->status
== -1);
1694 req
->status
= status
;
1696 assert(req
->sense_len
<= sizeof(req
->sense
));
1697 if (status
== GOOD
) {
1701 if (req
->sense_len
) {
1702 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1703 req
->dev
->sense_len
= req
->sense_len
;
1704 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1706 req
->dev
->sense_len
= 0;
1707 req
->dev
->sense_is_ua
= false;
1711 * Unit attention state is now stored in the device's sense buffer
1712 * if the HBA didn't do autosense. Clear the pending unit attention
1715 scsi_clear_unit_attention(req
);
1718 scsi_req_dequeue(req
);
1719 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1721 /* Cancelled requests might end up being completed instead of cancelled */
1722 notifier_list_notify(&req
->cancel_notifiers
, req
);
1723 scsi_req_unref(req
);
1726 /* Called by the devices when the request is canceled. */
1727 void scsi_req_cancel_complete(SCSIRequest
*req
)
1729 assert(req
->io_canceled
);
1730 if (req
->bus
->info
->cancel
) {
1731 req
->bus
->info
->cancel(req
);
1733 notifier_list_notify(&req
->cancel_notifiers
, req
);
1734 scsi_req_unref(req
);
1737 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1738 * notifier list, the bus will be notified the requests cancellation is
1741 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1743 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1745 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1747 if (req
->io_canceled
) {
1751 scsi_req_dequeue(req
);
1752 req
->io_canceled
= true;
1754 bdrv_aio_cancel_async(req
->aiocb
);
1758 void scsi_req_cancel(SCSIRequest
*req
)
1760 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1761 if (!req
->enqueued
) {
1765 scsi_req_dequeue(req
);
1766 req
->io_canceled
= true;
1768 bdrv_aio_cancel(req
->aiocb
);
1772 static int scsi_ua_precedence(SCSISense sense
)
1774 if (sense
.key
!= UNIT_ATTENTION
) {
1777 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1778 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1780 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1781 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1783 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1784 /* These two go with "all others". */
1786 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1787 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1788 * POWER ON OCCURRED = 1
1789 * SCSI BUS RESET OCCURRED = 2
1790 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1791 * I_T NEXUS LOSS OCCURRED = 7
1794 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1795 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1798 return (sense
.asc
<< 8) | sense
.ascq
;
1801 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1804 if (sense
.key
!= UNIT_ATTENTION
) {
1807 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1808 sense
.asc
, sense
.ascq
);
1811 * Override a pre-existing unit attention condition, except for a more
1812 * important reset condition.
1814 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1815 prec2
= scsi_ua_precedence(sense
);
1816 if (prec2
< prec1
) {
1817 sdev
->unit_attention
= sense
;
1821 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1825 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1826 req
= QTAILQ_FIRST(&sdev
->requests
);
1827 scsi_req_cancel(req
);
1830 scsi_device_set_ua(sdev
, sense
);
1833 static char *scsibus_get_dev_path(DeviceState
*dev
)
1835 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1836 DeviceState
*hba
= dev
->parent_bus
->parent
;
1840 id
= qdev_get_dev_path(hba
);
1842 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1844 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1850 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1852 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1853 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1854 qdev_fw_name(dev
), d
->id
, d
->lun
);
1857 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1860 SCSIDevice
*target_dev
= NULL
;
1862 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1863 DeviceState
*qdev
= kid
->child
;
1864 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1866 if (dev
->channel
== channel
&& dev
->id
== id
) {
1867 if (dev
->lun
== lun
) {
1876 /* SCSI request list. For simplicity, pv points to the whole device */
1878 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1881 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1884 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1885 assert(!req
->io_canceled
);
1886 assert(req
->status
== -1);
1887 assert(req
->enqueued
);
1889 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1890 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1891 qemu_put_be32s(f
, &req
->tag
);
1892 qemu_put_be32s(f
, &req
->lun
);
1893 if (bus
->info
->save_request
) {
1894 bus
->info
->save_request(f
, req
);
1896 if (req
->ops
->save_request
) {
1897 req
->ops
->save_request(f
, req
);
1900 qemu_put_sbyte(f
, 0);
1903 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1906 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1909 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1910 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1915 qemu_get_buffer(f
, buf
, sizeof(buf
));
1916 qemu_get_be32s(f
, &tag
);
1917 qemu_get_be32s(f
, &lun
);
1918 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1919 req
->retry
= (sbyte
== 1);
1920 if (bus
->info
->load_request
) {
1921 req
->hba_private
= bus
->info
->load_request(f
, req
);
1923 if (req
->ops
->load_request
) {
1924 req
->ops
->load_request(f
, req
);
1927 /* Just restart it later. */
1928 scsi_req_enqueue_internal(req
);
1930 /* At this point, the request will be kept alive by the reference
1931 * added by scsi_req_enqueue_internal, so we can release our reference.
1932 * The HBA of course will add its own reference in the load_request
1933 * callback if it needs to hold on the SCSIRequest.
1935 scsi_req_unref(req
);
1941 static int scsi_qdev_unplug(DeviceState
*qdev
)
1943 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1944 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1946 if (bus
->info
->hot_unplug
) {
1947 bus
->info
->hot_unplug(bus
, dev
);
1949 return qdev_simple_unplug_cb(qdev
);
1952 static const VMStateInfo vmstate_info_scsi_requests
= {
1953 .name
= "scsi-requests",
1954 .get
= get_scsi_requests
,
1955 .put
= put_scsi_requests
,
1958 static bool scsi_sense_state_needed(void *opaque
)
1960 SCSIDevice
*s
= opaque
;
1962 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1965 static const VMStateDescription vmstate_scsi_sense_state
= {
1966 .name
= "SCSIDevice/sense",
1968 .minimum_version_id
= 1,
1969 .fields
= (VMStateField
[]) {
1970 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1971 SCSI_SENSE_BUF_SIZE_OLD
,
1972 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1973 VMSTATE_END_OF_LIST()
1977 const VMStateDescription vmstate_scsi_device
= {
1978 .name
= "SCSIDevice",
1980 .minimum_version_id
= 1,
1981 .fields
= (VMStateField
[]) {
1982 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1983 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1984 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1985 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1986 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1987 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1991 .field_exists
= NULL
,
1992 .size
= 0, /* ouch */
1993 .info
= &vmstate_info_scsi_requests
,
1994 .flags
= VMS_SINGLE
,
1997 VMSTATE_END_OF_LIST()
1999 .subsections
= (VMStateSubsection
[]) {
2001 .vmsd
= &vmstate_scsi_sense_state
,
2002 .needed
= scsi_sense_state_needed
,
2009 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
2011 DeviceClass
*k
= DEVICE_CLASS(klass
);
2012 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
2013 k
->bus_type
= TYPE_SCSI_BUS
;
2014 k
->realize
= scsi_qdev_realize
;
2015 k
->unplug
= scsi_qdev_unplug
;
2016 k
->unrealize
= scsi_qdev_unrealize
;
2017 k
->props
= scsi_props
;
2020 static void scsi_dev_instance_init(Object
*obj
)
2022 DeviceState
*dev
= DEVICE(obj
);
2023 SCSIDevice
*s
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
2025 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
2030 static const TypeInfo scsi_device_type_info
= {
2031 .name
= TYPE_SCSI_DEVICE
,
2032 .parent
= TYPE_DEVICE
,
2033 .instance_size
= sizeof(SCSIDevice
),
2035 .class_size
= sizeof(SCSIDeviceClass
),
2036 .class_init
= scsi_device_class_init
,
2037 .instance_init
= scsi_dev_instance_init
,
2040 static void scsi_register_types(void)
2042 type_register_static(&scsi_bus_info
);
2043 type_register_static(&scsi_device_type_info
);
2046 type_init(scsi_register_types
)