1 #include "qemu/osdep.h"
3 #include "qapi/error.h"
4 #include "qemu/error-report.h"
5 #include "hw/scsi/scsi.h"
6 #include "block/scsi.h"
8 #include "sysemu/block-backend.h"
9 #include "sysemu/blockdev.h"
11 #include "sysemu/dma.h"
12 #include "qemu/cutils.h"
14 static char *scsibus_get_dev_path(DeviceState
*dev
);
15 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
16 static void scsi_req_dequeue(SCSIRequest
*req
);
17 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
18 static void scsi_target_free_buf(SCSIRequest
*req
);
20 static Property scsi_props
[] = {
21 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
22 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
23 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
24 DEFINE_PROP_END_OF_LIST(),
27 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
29 BusClass
*k
= BUS_CLASS(klass
);
30 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
32 k
->get_dev_path
= scsibus_get_dev_path
;
33 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
34 hc
->unplug
= qdev_simple_device_unplug_cb
;
37 static const TypeInfo scsi_bus_info
= {
38 .name
= TYPE_SCSI_BUS
,
40 .instance_size
= sizeof(SCSIBus
),
41 .class_init
= scsi_bus_class_init
,
42 .interfaces
= (InterfaceInfo
[]) {
43 { TYPE_HOTPLUG_HANDLER
},
47 static int next_scsi_bus
;
49 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
51 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
57 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
60 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
63 assert(cmd
->len
== 0);
64 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
65 if (bus
->info
->parse_cdb
) {
66 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
71 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
72 uint8_t *buf
, void *hba_private
)
74 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
76 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
82 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
84 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
85 if (sc
->unit_attention_reported
) {
86 sc
->unit_attention_reported(s
);
90 /* Create a scsi bus, and attach devices to it. */
91 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
92 const SCSIBusInfo
*info
, const char *bus_name
)
94 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
95 bus
->busnr
= next_scsi_bus
++;
97 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
100 static void scsi_dma_restart_bh(void *opaque
)
102 SCSIDevice
*s
= opaque
;
103 SCSIRequest
*req
, *next
;
105 qemu_bh_delete(s
->bh
);
108 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
109 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
113 switch (req
->cmd
.mode
) {
114 case SCSI_XFER_FROM_DEV
:
115 case SCSI_XFER_TO_DEV
:
116 scsi_req_continue(req
);
119 scsi_req_dequeue(req
);
120 scsi_req_enqueue(req
);
126 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
129 void scsi_req_retry(SCSIRequest
*req
)
131 /* No need to save a reference, because scsi_dma_restart_bh just
132 * looks at the request list. */
136 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
138 SCSIDevice
*s
= opaque
;
144 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
145 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
146 qemu_bh_schedule(s
->bh
);
150 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
152 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
153 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
155 Error
*local_err
= NULL
;
157 if (dev
->channel
> bus
->info
->max_channel
) {
158 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
161 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
162 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
165 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
166 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
172 if (dev
->lun
== -1) {
176 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
177 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
178 if (d
&& d
->lun
== dev
->lun
) {
179 error_setg(errp
, "no free target");
183 } else if (dev
->lun
== -1) {
186 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
187 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
188 if (d
&& d
->lun
== lun
) {
189 error_setg(errp
, "no free lun");
194 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
196 if (d
->lun
== dev
->lun
&& dev
!= d
) {
197 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
202 QTAILQ_INIT(&dev
->requests
);
203 scsi_device_realize(dev
, &local_err
);
205 error_propagate(errp
, local_err
);
208 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
212 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
214 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
217 qemu_del_vm_change_state_handler(dev
->vmsentry
);
220 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
221 blockdev_mark_auto_del(dev
->conf
.blk
);
224 /* handle legacy '-drive if=scsi,...' cmd line args */
225 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
226 int unit
, bool removable
, int bootindex
,
227 const char *serial
, Error
**errp
)
234 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
235 dev
= qdev_create(&bus
->qbus
, driver
);
236 name
= g_strdup_printf("legacy[%d]", unit
);
237 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
240 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
241 if (bootindex
>= 0) {
242 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
245 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
246 qdev_prop_set_bit(dev
, "removable", removable
);
248 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
249 qdev_prop_set_string(dev
, "serial", serial
);
251 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
253 error_propagate(errp
, err
);
254 object_unparent(OBJECT(dev
));
257 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
259 error_propagate(errp
, err
);
260 object_unparent(OBJECT(dev
));
263 return SCSI_DEVICE(dev
);
266 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
274 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
275 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
279 qemu_opts_loc_restore(dinfo
->opts
);
280 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
281 unit
, false, -1, NULL
, &err
);
283 error_propagate(errp
, err
);
290 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
292 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
293 scsi_req_complete(req
, CHECK_CONDITION
);
297 static const struct SCSIReqOps reqops_invalid_field
= {
298 .size
= sizeof(SCSIRequest
),
299 .send_command
= scsi_invalid_field
302 /* SCSIReqOps implementation for invalid commands. */
304 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
306 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
307 scsi_req_complete(req
, CHECK_CONDITION
);
311 static const struct SCSIReqOps reqops_invalid_opcode
= {
312 .size
= sizeof(SCSIRequest
),
313 .send_command
= scsi_invalid_command
316 /* SCSIReqOps implementation for unit attention conditions. */
318 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
320 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
321 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
322 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
323 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
325 scsi_req_complete(req
, CHECK_CONDITION
);
329 static const struct SCSIReqOps reqops_unit_attention
= {
330 .size
= sizeof(SCSIRequest
),
331 .send_command
= scsi_unit_attention
334 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
337 typedef struct SCSITargetReq SCSITargetReq
;
339 struct SCSITargetReq
{
346 static void store_lun(uint8_t *outbuf
, int lun
)
352 outbuf
[1] = (lun
& 255);
353 outbuf
[0] = (lun
>> 8) | 0x40;
356 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
363 if (r
->req
.cmd
.xfer
< 16) {
366 if (r
->req
.cmd
.buf
[2] > 2) {
369 channel
= r
->req
.dev
->channel
;
373 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
374 DeviceState
*qdev
= kid
->child
;
375 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
377 if (dev
->channel
== channel
&& dev
->id
== id
) {
388 scsi_target_alloc_buf(&r
->req
, n
+ 8);
390 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
391 memset(r
->buf
, 0, len
);
392 stl_be_p(&r
->buf
[0], n
);
393 i
= found_lun0
? 8 : 16;
394 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
395 DeviceState
*qdev
= kid
->child
;
396 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
398 if (dev
->channel
== channel
&& dev
->id
== id
) {
399 store_lun(&r
->buf
[i
], dev
->lun
);
408 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
410 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
412 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
414 if (r
->req
.cmd
.buf
[1] & 0x2) {
415 /* Command support data - optional, not implemented */
419 if (r
->req
.cmd
.buf
[1] & 0x1) {
420 /* Vital product data */
421 uint8_t page_code
= r
->req
.cmd
.buf
[2];
422 r
->buf
[r
->len
++] = page_code
; /* this page */
423 r
->buf
[r
->len
++] = 0x00;
426 case 0x00: /* Supported page codes, mandatory */
430 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
431 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
438 assert(r
->len
< r
->buf_len
);
439 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
443 /* Standard INQUIRY data */
444 if (r
->req
.cmd
.buf
[2] != 0) {
449 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
450 memset(r
->buf
, 0, r
->len
);
451 if (r
->req
.lun
!= 0) {
452 r
->buf
[0] = TYPE_NO_LUN
;
454 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
455 r
->buf
[2] = 5; /* Version */
456 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
457 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
458 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
459 memcpy(&r
->buf
[8], "QEMU ", 8);
460 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
461 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
466 static size_t scsi_sense_len(SCSIRequest
*req
)
468 if (req
->dev
->type
== TYPE_SCANNER
)
469 return SCSI_SENSE_LEN_SCANNER
;
471 return SCSI_SENSE_LEN
;
474 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
476 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
480 if (!scsi_target_emulate_report_luns(r
)) {
481 goto illegal_request
;
485 if (!scsi_target_emulate_inquiry(r
)) {
486 goto illegal_request
;
490 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
491 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
492 MIN(req
->cmd
.xfer
, r
->buf_len
),
493 (req
->cmd
.buf
[1] & 1) == 0);
494 if (r
->req
.dev
->sense_is_ua
) {
495 scsi_device_unit_attention_reported(req
->dev
);
496 r
->req
.dev
->sense_len
= 0;
497 r
->req
.dev
->sense_is_ua
= false;
500 case TEST_UNIT_READY
:
503 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
504 scsi_req_complete(req
, CHECK_CONDITION
);
507 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
508 scsi_req_complete(req
, CHECK_CONDITION
);
513 scsi_req_complete(req
, GOOD
);
518 static void scsi_target_read_data(SCSIRequest
*req
)
520 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
526 scsi_req_data(&r
->req
, n
);
528 scsi_req_complete(&r
->req
, GOOD
);
532 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
534 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
539 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
541 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
543 r
->buf
= g_malloc(len
);
549 static void scsi_target_free_buf(SCSIRequest
*req
)
551 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
556 static const struct SCSIReqOps reqops_target_command
= {
557 .size
= sizeof(SCSITargetReq
),
558 .send_command
= scsi_target_send_command
,
559 .read_data
= scsi_target_read_data
,
560 .get_buf
= scsi_target_get_buf
,
561 .free_req
= scsi_target_free_buf
,
565 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
566 uint32_t tag
, uint32_t lun
, void *hba_private
)
569 SCSIBus
*bus
= scsi_bus_from_device(d
);
570 BusState
*qbus
= BUS(bus
);
571 const int memset_off
= offsetof(SCSIRequest
, sense
)
572 + sizeof(req
->sense
);
574 req
= g_malloc(reqops
->size
);
575 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
581 req
->hba_private
= hba_private
;
584 object_ref(OBJECT(d
));
585 object_ref(OBJECT(qbus
->parent
));
586 notifier_list_init(&req
->cancel_notifiers
);
587 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
591 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
592 uint8_t *buf
, void *hba_private
)
594 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
595 const SCSIReqOps
*ops
;
596 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
598 SCSICommand cmd
= { .len
= 0 };
601 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
602 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
603 (buf
[0] != INQUIRY
&&
604 buf
[0] != REPORT_LUNS
&&
605 buf
[0] != GET_CONFIGURATION
&&
606 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
609 * If we already have a pending unit attention condition,
610 * report this one before triggering another one.
612 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
613 ops
= &reqops_unit_attention
;
614 } else if (lun
!= d
->lun
||
615 buf
[0] == REPORT_LUNS
||
616 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
617 ops
= &reqops_target_command
;
622 if (ops
!= NULL
|| !sc
->parse_cdb
) {
623 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
625 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
629 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
630 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
632 assert(cmd
.len
!= 0);
633 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
636 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
640 if (cmd
.xfer
> INT32_MAX
) {
641 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
643 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
645 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
650 req
->resid
= req
->cmd
.xfer
;
654 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
656 case TEST_UNIT_READY
:
657 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
660 trace_scsi_report_luns(d
->id
, lun
, tag
);
663 trace_scsi_request_sense(d
->id
, lun
, tag
);
672 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
674 return req
->ops
->get_buf(req
);
677 static void scsi_clear_unit_attention(SCSIRequest
*req
)
680 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
681 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
686 * If an INQUIRY command enters the enabled command state,
687 * the device server shall [not] clear any unit attention condition;
688 * See also MMC-6, paragraphs 6.5 and 6.6.2.
690 if (req
->cmd
.buf
[0] == INQUIRY
||
691 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
692 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
696 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
697 ua
= &req
->dev
->unit_attention
;
699 ua
= &req
->bus
->unit_attention
;
703 * If a REPORT LUNS command enters the enabled command state, [...]
704 * the device server shall clear any pending unit attention condition
705 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
707 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
708 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
709 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
713 *ua
= SENSE_CODE(NO_SENSE
);
716 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
721 if (!req
->sense_len
) {
725 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
728 * FIXME: clearing unit attention conditions upon autosense should be done
729 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
732 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
733 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
734 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
736 if (req
->dev
->sense_is_ua
) {
737 scsi_device_unit_attention_reported(req
->dev
);
738 req
->dev
->sense_len
= 0;
739 req
->dev
->sense_is_ua
= false;
744 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
746 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
749 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
751 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
752 sense
.key
, sense
.asc
, sense
.ascq
);
753 memset(req
->sense
, 0, 18);
754 req
->sense
[0] = 0x70;
755 req
->sense
[2] = sense
.key
;
757 req
->sense
[12] = sense
.asc
;
758 req
->sense
[13] = sense
.ascq
;
762 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
764 assert(!req
->enqueued
);
766 if (req
->bus
->info
->get_sg_list
) {
767 req
->sg
= req
->bus
->info
->get_sg_list(req
);
771 req
->enqueued
= true;
772 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
775 int32_t scsi_req_enqueue(SCSIRequest
*req
)
780 scsi_req_enqueue_internal(req
);
782 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
787 static void scsi_req_dequeue(SCSIRequest
*req
)
789 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
792 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
793 req
->enqueued
= false;
798 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
800 /* MMC-6, paragraph 6.7. */
803 if ((data_type
& 3) == 0) {
804 /* Each descriptor is as in Table 295 - Nominal performance. */
805 return 16 * num_desc
+ 8;
807 /* Each descriptor is as in Table 296 - Exceptions. */
808 return 6 * num_desc
+ 8;
813 return 8 * num_desc
+ 8;
815 return 2048 * num_desc
+ 8;
817 return 16 * num_desc
+ 8;
823 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
825 int byte_block
= (buf
[2] >> 2) & 0x1;
826 int type
= (buf
[2] >> 4) & 0x1;
831 xfer_unit
= dev
->blocksize
;
842 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
844 int length
= buf
[2] & 0x3;
846 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
850 case 3: /* USB-specific. */
865 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
867 int extend
= buf
[1] & 0x1;
868 int length
= buf
[2] & 0x3;
870 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
874 case 3: /* USB-specific. */
880 xfer
|= (extend
? buf
[3] << 8 : 0);
884 xfer
|= (extend
? buf
[5] << 8 : 0);
891 uint32_t scsi_data_cdb_xfer(uint8_t *buf
)
893 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
896 return scsi_cdb_xfer(buf
);
900 uint32_t scsi_cdb_xfer(uint8_t *buf
)
902 switch (buf
[0] >> 5) {
908 return lduw_be_p(&buf
[7]);
911 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
914 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
921 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
923 cmd
->xfer
= scsi_cdb_xfer(buf
);
925 case TEST_UNIT_READY
:
929 case WRITE_FILEMARKS
:
930 case WRITE_FILEMARKS_16
:
935 case ALLOW_MEDIUM_REMOVAL
:
937 case SYNCHRONIZE_CACHE
:
938 case SYNCHRONIZE_CACHE_16
:
940 case LOCK_UNLOCK_CACHE
:
949 case ALLOW_OVERWRITE
:
955 if ((buf
[1] & 2) == 0) {
957 } else if ((buf
[1] & 4) != 0) {
960 cmd
->xfer
*= dev
->blocksize
;
966 cmd
->xfer
= dev
->blocksize
;
968 case READ_CAPACITY_10
:
971 case READ_BLOCK_LIMITS
:
974 case SEND_VOLUME_TAG
:
975 /* GPCMD_SET_STREAMING from multimedia commands. */
976 if (dev
->type
== TYPE_ROM
) {
977 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
979 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
983 /* length 0 means 256 blocks */
984 if (cmd
->xfer
== 0) {
989 case WRITE_VERIFY_10
:
991 case WRITE_VERIFY_12
:
993 case WRITE_VERIFY_16
:
994 cmd
->xfer
*= dev
->blocksize
;
998 /* length 0 means 256 blocks */
999 if (cmd
->xfer
== 0) {
1006 cmd
->xfer
*= dev
->blocksize
;
1009 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1010 * for block devices are restricted to the header right now. */
1011 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1014 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1018 case RECEIVE_DIAGNOSTIC
:
1019 case SEND_DIAGNOSTIC
:
1020 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1025 case SEND_CUE_SHEET
:
1026 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1028 case PERSISTENT_RESERVE_OUT
:
1029 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1032 if (dev
->type
== TYPE_ROM
) {
1033 /* MMC command GET PERFORMANCE. */
1034 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1035 buf
[10], buf
[1] & 0x1f);
1038 case MECHANISM_STATUS
:
1039 case READ_DVD_STRUCTURE
:
1040 case SEND_DVD_STRUCTURE
:
1041 case MAINTENANCE_OUT
:
1042 case MAINTENANCE_IN
:
1043 if (dev
->type
== TYPE_ROM
) {
1044 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1045 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1048 case ATA_PASSTHROUGH_12
:
1049 if (dev
->type
== TYPE_ROM
) {
1050 /* BLANK command of MMC */
1053 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1056 case ATA_PASSTHROUGH_16
:
1057 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1063 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1066 /* stream commands */
1073 case RECOVER_BUFFERED_DATA
:
1075 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1076 if (buf
[1] & 0x01) { /* fixed */
1077 cmd
->xfer
*= dev
->blocksize
;
1081 case READ_REVERSE_16
:
1084 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1085 if (buf
[1] & 0x01) { /* fixed */
1086 cmd
->xfer
*= dev
->blocksize
;
1094 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1097 switch (buf
[1] & 0x1f) /* operation code */ {
1098 case SHORT_FORM_BLOCK_ID
:
1099 case SHORT_FORM_VENDOR_SPECIFIC
:
1106 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1114 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1116 /* generic commands */
1118 return scsi_req_xfer(cmd
, dev
, buf
);
1123 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1126 /* medium changer commands */
1127 case EXCHANGE_MEDIUM
:
1128 case INITIALIZE_ELEMENT_STATUS
:
1129 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1131 case POSITION_TO_ELEMENT
:
1134 case READ_ELEMENT_STATUS
:
1135 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1138 /* generic commands */
1140 return scsi_req_xfer(cmd
, dev
, buf
);
1145 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1148 /* Scanner commands */
1149 case OBJECT_POSITION
:
1159 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1162 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1163 return scsi_req_xfer(cmd
, dev
, buf
);
1169 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1172 cmd
->mode
= SCSI_XFER_NONE
;
1175 switch (cmd
->buf
[0]) {
1178 case WRITE_VERIFY_10
:
1180 case WRITE_VERIFY_12
:
1182 case WRITE_VERIFY_16
:
1189 case CHANGE_DEFINITION
:
1192 case MODE_SELECT_10
:
1193 case SEND_DIAGNOSTIC
:
1196 case REASSIGN_BLOCKS
:
1205 case SEARCH_HIGH_12
:
1206 case SEARCH_EQUAL_12
:
1209 case SEND_VOLUME_TAG
:
1210 case SEND_CUE_SHEET
:
1211 case SEND_DVD_STRUCTURE
:
1212 case PERSISTENT_RESERVE_OUT
:
1213 case MAINTENANCE_OUT
:
1216 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1217 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1219 cmd
->mode
= SCSI_XFER_TO_DEV
;
1221 case ATA_PASSTHROUGH_12
:
1222 case ATA_PASSTHROUGH_16
:
1224 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1225 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1228 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1233 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1235 uint8_t *buf
= cmd
->buf
;
1238 switch (buf
[0] >> 5) {
1240 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1245 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1248 lba
= ldq_be_p(&buf
[2]);
1257 int scsi_cdb_length(uint8_t *buf
) {
1260 switch (buf
[0] >> 5) {
1280 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1286 len
= scsi_cdb_length(buf
);
1292 switch (dev
->type
) {
1294 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1296 case TYPE_MEDIUM_CHANGER
:
1297 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1300 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1303 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1310 memcpy(cmd
->buf
, buf
, cmd
->len
);
1311 scsi_cmd_xfer_mode(cmd
);
1312 cmd
->lba
= scsi_cmd_lba(cmd
);
1316 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1318 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1320 scsi_device_set_ua(dev
, sense
);
1321 if (bus
->info
->change
) {
1322 bus
->info
->change(bus
, dev
, sense
);
1327 * Predefined sense codes
1330 /* No sense data available */
1331 const struct SCSISense sense_code_NO_SENSE
= {
1332 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1335 /* LUN not ready, Manual intervention required */
1336 const struct SCSISense sense_code_LUN_NOT_READY
= {
1337 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1340 /* LUN not ready, Medium not present */
1341 const struct SCSISense sense_code_NO_MEDIUM
= {
1342 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1345 /* LUN not ready, medium removal prevented */
1346 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1347 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1350 /* Hardware error, internal target failure */
1351 const struct SCSISense sense_code_TARGET_FAILURE
= {
1352 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1355 /* Illegal request, invalid command operation code */
1356 const struct SCSISense sense_code_INVALID_OPCODE
= {
1357 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1360 /* Illegal request, LBA out of range */
1361 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1362 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1365 /* Illegal request, Invalid field in CDB */
1366 const struct SCSISense sense_code_INVALID_FIELD
= {
1367 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1370 /* Illegal request, Invalid field in parameter list */
1371 const struct SCSISense sense_code_INVALID_PARAM
= {
1372 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1375 /* Illegal request, Parameter list length error */
1376 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1377 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1380 /* Illegal request, LUN not supported */
1381 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1382 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1385 /* Illegal request, Saving parameters not supported */
1386 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1387 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1390 /* Illegal request, Incompatible medium installed */
1391 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1392 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1395 /* Illegal request, medium removal prevented */
1396 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1397 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1400 /* Illegal request, Invalid Transfer Tag */
1401 const struct SCSISense sense_code_INVALID_TAG
= {
1402 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1405 /* Command aborted, I/O process terminated */
1406 const struct SCSISense sense_code_IO_ERROR
= {
1407 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1410 /* Command aborted, I_T Nexus loss occurred */
1411 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1412 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1415 /* Command aborted, Logical Unit failure */
1416 const struct SCSISense sense_code_LUN_FAILURE
= {
1417 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1420 /* Command aborted, Overlapped Commands Attempted */
1421 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1422 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1425 /* Unit attention, Capacity data has changed */
1426 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1427 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1430 /* Unit attention, Power on, reset or bus device reset occurred */
1431 const struct SCSISense sense_code_RESET
= {
1432 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1435 /* Unit attention, No medium */
1436 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1437 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1440 /* Unit attention, Medium may have changed */
1441 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1442 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1445 /* Unit attention, Reported LUNs data has changed */
1446 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1447 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1450 /* Unit attention, Device internal reset */
1451 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1452 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1455 /* Data Protection, Write Protected */
1456 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1457 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1460 /* Data Protection, Space Allocation Failed Write Protect */
1461 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1462 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1468 * Convert between fixed and descriptor sense buffers
1470 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1471 uint8_t *buf
, int len
, bool fixed
)
1475 if (!fixed
&& len
< 8) {
1480 sense
.key
= NO_SENSE
;
1484 fixed_in
= (in_buf
[0] & 2) == 0;
1486 if (fixed
== fixed_in
) {
1487 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1488 return MIN(len
, in_len
);
1492 sense
.key
= in_buf
[2];
1493 sense
.asc
= in_buf
[12];
1494 sense
.ascq
= in_buf
[13];
1496 sense
.key
= in_buf
[1];
1497 sense
.asc
= in_buf
[2];
1498 sense
.ascq
= in_buf
[3];
1502 memset(buf
, 0, len
);
1504 /* Return fixed format sense buffer */
1508 buf
[12] = sense
.asc
;
1509 buf
[13] = sense
.ascq
;
1510 return MIN(len
, SCSI_SENSE_LEN
);
1512 /* Return descriptor format sense buffer */
1516 buf
[3] = sense
.ascq
;
1521 const char *scsi_command_name(uint8_t cmd
)
1523 static const char *names
[] = {
1524 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1525 [ REWIND
] = "REWIND",
1526 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1527 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1528 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1529 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1530 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1531 [ READ_6
] = "READ_6",
1532 [ WRITE_6
] = "WRITE_6",
1533 [ SET_CAPACITY
] = "SET_CAPACITY",
1534 [ READ_REVERSE
] = "READ_REVERSE",
1535 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1536 [ SPACE
] = "SPACE",
1537 [ INQUIRY
] = "INQUIRY",
1538 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1539 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1540 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1541 [ MODE_SELECT
] = "MODE_SELECT",
1542 [ RESERVE
] = "RESERVE",
1543 [ RELEASE
] = "RELEASE",
1545 [ ERASE
] = "ERASE",
1546 [ MODE_SENSE
] = "MODE_SENSE",
1547 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1548 /* LOAD_UNLOAD and START_STOP use the same operation code */
1549 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1550 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1551 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1552 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1553 [ READ_10
] = "READ_10",
1554 [ WRITE_10
] = "WRITE_10",
1555 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1556 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1557 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1558 [ VERIFY_10
] = "VERIFY_10",
1559 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1560 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1561 [ SEARCH_LOW
] = "SEARCH_LOW",
1562 [ SET_LIMITS
] = "SET_LIMITS",
1563 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1564 /* READ_POSITION and PRE_FETCH use the same operation code */
1565 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1566 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1567 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1568 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1569 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1570 [ COMPARE
] = "COMPARE",
1571 [ COPY_VERIFY
] = "COPY_VERIFY",
1572 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1573 [ READ_BUFFER
] = "READ_BUFFER",
1574 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1575 [ READ_LONG_10
] = "READ_LONG_10",
1576 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1577 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1578 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1579 [ UNMAP
] = "UNMAP",
1580 [ READ_TOC
] = "READ_TOC",
1581 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1582 [ SANITIZE
] = "SANITIZE",
1583 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1584 [ LOG_SELECT
] = "LOG_SELECT",
1585 [ LOG_SENSE
] = "LOG_SENSE",
1586 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1587 [ RESERVE_10
] = "RESERVE_10",
1588 [ RELEASE_10
] = "RELEASE_10",
1589 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1590 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1591 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1592 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1593 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1594 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1595 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1596 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1597 [ READ_16
] = "READ_16",
1598 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1599 [ WRITE_16
] = "WRITE_16",
1600 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1601 [ VERIFY_16
] = "VERIFY_16",
1602 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1603 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1604 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1605 [ LOCATE_16
] = "LOCATE_16",
1606 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1607 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1608 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1609 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1610 [ REPORT_LUNS
] = "REPORT_LUNS",
1611 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1612 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1613 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1614 [ READ_12
] = "READ_12",
1615 [ WRITE_12
] = "WRITE_12",
1616 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1617 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1618 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1619 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1620 [ VERIFY_12
] = "VERIFY_12",
1621 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1622 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1623 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1624 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1625 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1626 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1627 [ READ_CD
] = "READ_CD",
1628 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1629 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1630 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1631 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1632 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1633 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1634 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1635 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1636 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1637 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1638 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1641 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1646 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1648 assert(req
->refcount
> 0);
1653 void scsi_req_unref(SCSIRequest
*req
)
1655 assert(req
->refcount
> 0);
1656 if (--req
->refcount
== 0) {
1657 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1658 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1660 if (bus
->info
->free_request
&& req
->hba_private
) {
1661 bus
->info
->free_request(bus
, req
->hba_private
);
1663 if (req
->ops
->free_req
) {
1664 req
->ops
->free_req(req
);
1666 object_unref(OBJECT(req
->dev
));
1667 object_unref(OBJECT(qbus
->parent
));
1672 /* Tell the device that we finished processing this chunk of I/O. It
1673 will start the next chunk or complete the command. */
1674 void scsi_req_continue(SCSIRequest
*req
)
1676 if (req
->io_canceled
) {
1677 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1680 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1681 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1682 req
->ops
->write_data(req
);
1684 req
->ops
->read_data(req
);
1688 /* Called by the devices when data is ready for the HBA. The HBA should
1689 start a DMA operation to read or fill the device's data buffer.
1690 Once it completes, calling scsi_req_continue will restart I/O. */
1691 void scsi_req_data(SCSIRequest
*req
, int len
)
1694 if (req
->io_canceled
) {
1695 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1698 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1699 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1702 req
->bus
->info
->transfer_data(req
, len
);
1706 /* If the device calls scsi_req_data and the HBA specified a
1707 * scatter/gather list, the transfer has to happen in a single
1709 assert(!req
->dma_started
);
1710 req
->dma_started
= true;
1712 buf
= scsi_req_get_buf(req
);
1713 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1714 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1716 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1718 scsi_req_continue(req
);
1721 void scsi_req_print(SCSIRequest
*req
)
1726 fprintf(fp
, "[%s id=%d] %s",
1727 req
->dev
->qdev
.parent_bus
->name
,
1729 scsi_command_name(req
->cmd
.buf
[0]));
1730 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1731 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1733 switch (req
->cmd
.mode
) {
1734 case SCSI_XFER_NONE
:
1735 fprintf(fp
, " - none\n");
1737 case SCSI_XFER_FROM_DEV
:
1738 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1740 case SCSI_XFER_TO_DEV
:
1741 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1744 fprintf(fp
, " - Oops\n");
1749 void scsi_req_complete(SCSIRequest
*req
, int status
)
1751 assert(req
->status
== -1);
1752 req
->status
= status
;
1754 assert(req
->sense_len
<= sizeof(req
->sense
));
1755 if (status
== GOOD
) {
1759 if (req
->sense_len
) {
1760 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1761 req
->dev
->sense_len
= req
->sense_len
;
1762 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1764 req
->dev
->sense_len
= 0;
1765 req
->dev
->sense_is_ua
= false;
1769 * Unit attention state is now stored in the device's sense buffer
1770 * if the HBA didn't do autosense. Clear the pending unit attention
1773 scsi_clear_unit_attention(req
);
1776 scsi_req_dequeue(req
);
1777 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1779 /* Cancelled requests might end up being completed instead of cancelled */
1780 notifier_list_notify(&req
->cancel_notifiers
, req
);
1781 scsi_req_unref(req
);
1784 /* Called by the devices when the request is canceled. */
1785 void scsi_req_cancel_complete(SCSIRequest
*req
)
1787 assert(req
->io_canceled
);
1788 if (req
->bus
->info
->cancel
) {
1789 req
->bus
->info
->cancel(req
);
1791 notifier_list_notify(&req
->cancel_notifiers
, req
);
1792 scsi_req_unref(req
);
1795 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1796 * notifier list, the bus will be notified the requests cancellation is
1799 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1801 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1803 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1805 if (req
->io_canceled
) {
1806 /* A blk_aio_cancel_async is pending; when it finishes,
1807 * scsi_req_cancel_complete will be called and will
1808 * call the notifier we just added. Just wait for that.
1813 /* Dropped in scsi_req_cancel_complete. */
1815 scsi_req_dequeue(req
);
1816 req
->io_canceled
= true;
1818 blk_aio_cancel_async(req
->aiocb
);
1820 scsi_req_cancel_complete(req
);
1824 void scsi_req_cancel(SCSIRequest
*req
)
1826 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1827 if (!req
->enqueued
) {
1830 assert(!req
->io_canceled
);
1831 /* Dropped in scsi_req_cancel_complete. */
1833 scsi_req_dequeue(req
);
1834 req
->io_canceled
= true;
1836 blk_aio_cancel(req
->aiocb
);
1838 scsi_req_cancel_complete(req
);
1842 static int scsi_ua_precedence(SCSISense sense
)
1844 if (sense
.key
!= UNIT_ATTENTION
) {
1847 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1848 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1850 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1851 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1853 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1854 /* These two go with "all others". */
1856 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1857 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1858 * POWER ON OCCURRED = 1
1859 * SCSI BUS RESET OCCURRED = 2
1860 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1861 * I_T NEXUS LOSS OCCURRED = 7
1864 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1865 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1868 return (sense
.asc
<< 8) | sense
.ascq
;
1871 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1874 if (sense
.key
!= UNIT_ATTENTION
) {
1877 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1878 sense
.asc
, sense
.ascq
);
1881 * Override a pre-existing unit attention condition, except for a more
1882 * important reset condition.
1884 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1885 prec2
= scsi_ua_precedence(sense
);
1886 if (prec2
< prec1
) {
1887 sdev
->unit_attention
= sense
;
1891 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1895 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1896 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1897 req
= QTAILQ_FIRST(&sdev
->requests
);
1898 scsi_req_cancel_async(req
, NULL
);
1900 blk_drain(sdev
->conf
.blk
);
1901 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1902 scsi_device_set_ua(sdev
, sense
);
1905 static char *scsibus_get_dev_path(DeviceState
*dev
)
1907 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1908 DeviceState
*hba
= dev
->parent_bus
->parent
;
1912 id
= qdev_get_dev_path(hba
);
1914 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1916 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1922 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1924 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1925 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1926 qdev_fw_name(dev
), d
->id
, d
->lun
);
1929 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1932 SCSIDevice
*target_dev
= NULL
;
1934 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1935 DeviceState
*qdev
= kid
->child
;
1936 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1938 if (dev
->channel
== channel
&& dev
->id
== id
) {
1939 if (dev
->lun
== lun
) {
1948 /* SCSI request list. For simplicity, pv points to the whole device */
1950 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1951 VMStateField
*field
, QJSON
*vmdesc
)
1954 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1957 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1958 assert(!req
->io_canceled
);
1959 assert(req
->status
== -1);
1960 assert(req
->enqueued
);
1962 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1963 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1964 qemu_put_be32s(f
, &req
->tag
);
1965 qemu_put_be32s(f
, &req
->lun
);
1966 if (bus
->info
->save_request
) {
1967 bus
->info
->save_request(f
, req
);
1969 if (req
->ops
->save_request
) {
1970 req
->ops
->save_request(f
, req
);
1973 qemu_put_sbyte(f
, 0);
1978 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1979 VMStateField
*field
)
1982 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1985 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1986 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1991 qemu_get_buffer(f
, buf
, sizeof(buf
));
1992 qemu_get_be32s(f
, &tag
);
1993 qemu_get_be32s(f
, &lun
);
1994 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1995 req
->retry
= (sbyte
== 1);
1996 if (bus
->info
->load_request
) {
1997 req
->hba_private
= bus
->info
->load_request(f
, req
);
1999 if (req
->ops
->load_request
) {
2000 req
->ops
->load_request(f
, req
);
2003 /* Just restart it later. */
2004 scsi_req_enqueue_internal(req
);
2006 /* At this point, the request will be kept alive by the reference
2007 * added by scsi_req_enqueue_internal, so we can release our reference.
2008 * The HBA of course will add its own reference in the load_request
2009 * callback if it needs to hold on the SCSIRequest.
2011 scsi_req_unref(req
);
2017 static const VMStateInfo vmstate_info_scsi_requests
= {
2018 .name
= "scsi-requests",
2019 .get
= get_scsi_requests
,
2020 .put
= put_scsi_requests
,
2023 static bool scsi_sense_state_needed(void *opaque
)
2025 SCSIDevice
*s
= opaque
;
2027 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
2030 static const VMStateDescription vmstate_scsi_sense_state
= {
2031 .name
= "SCSIDevice/sense",
2033 .minimum_version_id
= 1,
2034 .needed
= scsi_sense_state_needed
,
2035 .fields
= (VMStateField
[]) {
2036 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
2037 SCSI_SENSE_BUF_SIZE_OLD
,
2038 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
2039 VMSTATE_END_OF_LIST()
2043 const VMStateDescription vmstate_scsi_device
= {
2044 .name
= "SCSIDevice",
2046 .minimum_version_id
= 1,
2047 .fields
= (VMStateField
[]) {
2048 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
2049 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
2050 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
2051 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
2052 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
2053 VMSTATE_UINT32(sense_len
, SCSIDevice
),
2057 .field_exists
= NULL
,
2058 .size
= 0, /* ouch */
2059 .info
= &vmstate_info_scsi_requests
,
2060 .flags
= VMS_SINGLE
,
2063 VMSTATE_END_OF_LIST()
2065 .subsections
= (const VMStateDescription
*[]) {
2066 &vmstate_scsi_sense_state
,
2071 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
2073 DeviceClass
*k
= DEVICE_CLASS(klass
);
2074 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
2075 k
->bus_type
= TYPE_SCSI_BUS
;
2076 k
->realize
= scsi_qdev_realize
;
2077 k
->unrealize
= scsi_qdev_unrealize
;
2078 k
->props
= scsi_props
;
2081 static void scsi_dev_instance_init(Object
*obj
)
2083 DeviceState
*dev
= DEVICE(obj
);
2084 SCSIDevice
*s
= SCSI_DEVICE(dev
);
2086 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
2091 static const TypeInfo scsi_device_type_info
= {
2092 .name
= TYPE_SCSI_DEVICE
,
2093 .parent
= TYPE_DEVICE
,
2094 .instance_size
= sizeof(SCSIDevice
),
2096 .class_size
= sizeof(SCSIDeviceClass
),
2097 .class_init
= scsi_device_class_init
,
2098 .instance_init
= scsi_dev_instance_init
,
2101 static void scsi_register_types(void)
2103 type_register_static(&scsi_bus_info
);
2104 type_register_static(&scsi_device_type_info
);
2107 type_init(scsi_register_types
)