2 #include "qemu-error.h"
9 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
10 static int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
);
11 static int scsi_build_sense(uint8_t *in_buf
, int in_len
,
12 uint8_t *buf
, int len
, bool fixed
);
14 static struct BusInfo scsi_bus_info
= {
16 .size
= sizeof(SCSIBus
),
17 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
18 .props
= (Property
[]) {
19 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
20 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, 0),
21 DEFINE_PROP_END_OF_LIST(),
24 static int next_scsi_bus
;
26 /* Create a scsi bus, and attach devices to it. */
27 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
28 const SCSIBusOps
*ops
)
30 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
31 bus
->busnr
= next_scsi_bus
++;
35 bus
->qbus
.allow_hotplug
= 1;
38 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
40 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
41 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
42 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
46 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
47 if (bus
->devs
[dev
->id
] == NULL
)
51 if (dev
->id
>= bus
->ndev
) {
52 error_report("bad scsi device id: %d", dev
->id
);
56 if (bus
->devs
[dev
->id
]) {
57 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
59 bus
->devs
[dev
->id
] = dev
;
62 QTAILQ_INIT(&dev
->requests
);
63 rc
= dev
->info
->init(dev
);
65 bus
->devs
[dev
->id
] = NULL
;
72 static int scsi_qdev_exit(DeviceState
*qdev
)
74 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
75 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
77 assert(bus
->devs
[dev
->id
] != NULL
);
78 if (bus
->devs
[dev
->id
]->info
->destroy
) {
79 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
81 bus
->devs
[dev
->id
] = NULL
;
85 void scsi_qdev_register(SCSIDeviceInfo
*info
)
87 info
->qdev
.bus_info
= &scsi_bus_info
;
88 info
->qdev
.init
= scsi_qdev_init
;
89 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
90 info
->qdev
.exit
= scsi_qdev_exit
;
91 qdev_register(&info
->qdev
);
94 /* handle legacy '-drive if=scsi,...' cmd line args */
95 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
96 int unit
, bool removable
)
101 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
102 dev
= qdev_create(&bus
->qbus
, driver
);
103 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
104 if (qdev_prop_exists(dev
, "removable")) {
105 qdev_prop_set_bit(dev
, "removable", removable
);
107 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
111 if (qdev_init(dev
) < 0)
113 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
116 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
123 for (unit
= 0; unit
< bus
->ndev
; unit
++) {
124 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
128 qemu_opts_loc_restore(dinfo
->opts
);
129 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false)) {
138 /* SCSIReqOps implementation for invalid commands. */
140 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
142 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
143 scsi_req_complete(req
, CHECK_CONDITION
);
147 struct SCSIReqOps reqops_invalid_opcode
= {
148 .size
= sizeof(SCSIRequest
),
149 .send_command
= scsi_invalid_command
152 /* SCSIReqOps implementation for unit attention conditions. */
154 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
156 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
157 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
158 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
159 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
161 scsi_req_complete(req
, CHECK_CONDITION
);
165 struct SCSIReqOps reqops_unit_attention
= {
166 .size
= sizeof(SCSIRequest
),
167 .send_command
= scsi_unit_attention
170 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
173 typedef struct SCSITargetReq SCSITargetReq
;
175 struct SCSITargetReq
{
181 static void store_lun(uint8_t *outbuf
, int lun
)
187 outbuf
[1] = (lun
& 255);
188 outbuf
[0] = (lun
>> 8) | 0x40;
191 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
194 if (r
->req
.cmd
.xfer
< 16) {
197 if (r
->req
.cmd
.buf
[2] > 2) {
200 len
= MIN(sizeof r
->buf
, r
->req
.cmd
.xfer
);
201 memset(r
->buf
, 0, len
);
202 if (r
->req
.dev
->lun
!= 0) {
205 store_lun(&r
->buf
[16], r
->req
.dev
->lun
);
213 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
215 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
216 if (r
->req
.cmd
.buf
[1] & 0x2) {
217 /* Command support data - optional, not implemented */
221 if (r
->req
.cmd
.buf
[1] & 0x1) {
222 /* Vital product data */
223 uint8_t page_code
= r
->req
.cmd
.buf
[2];
224 if (r
->req
.cmd
.xfer
< 4) {
228 r
->buf
[r
->len
++] = page_code
; /* this page */
229 r
->buf
[r
->len
++] = 0x00;
232 case 0x00: /* Supported page codes, mandatory */
236 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
237 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
244 assert(r
->len
< sizeof(r
->buf
));
245 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
249 /* Standard INQUIRY data */
250 if (r
->req
.cmd
.buf
[2] != 0) {
255 if (r
->req
.cmd
.xfer
< 5) {
259 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
260 memset(r
->buf
, 0, r
->len
);
261 if (r
->req
.lun
!= 0) {
262 r
->buf
[0] = TYPE_NO_LUN
;
264 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
265 r
->buf
[2] = 5; /* Version */
266 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
267 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
268 r
->buf
[7] = 0x10 | (r
->req
.bus
->tcq
? 0x02 : 0); /* Sync, TCQ. */
269 memcpy(&r
->buf
[8], "QEMU ", 8);
270 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
271 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
276 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
278 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
282 if (!scsi_target_emulate_report_luns(r
)) {
283 goto illegal_request
;
287 if (!scsi_target_emulate_inquiry(r
)) {
288 goto illegal_request
;
292 if (req
->cmd
.xfer
< 4) {
293 goto illegal_request
;
295 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
296 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
297 (req
->cmd
.buf
[1] & 1) == 0);
300 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
301 scsi_req_complete(req
, CHECK_CONDITION
);
304 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
305 scsi_req_complete(req
, CHECK_CONDITION
);
310 scsi_req_complete(req
, GOOD
);
315 static void scsi_target_read_data(SCSIRequest
*req
)
317 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
323 scsi_req_data(&r
->req
, n
);
325 scsi_req_complete(&r
->req
, GOOD
);
329 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
331 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
336 struct SCSIReqOps reqops_target_command
= {
337 .size
= sizeof(SCSITargetReq
),
338 .send_command
= scsi_target_send_command
,
339 .read_data
= scsi_target_read_data
,
340 .get_buf
= scsi_target_get_buf
,
344 SCSIRequest
*scsi_req_alloc(SCSIReqOps
*reqops
, SCSIDevice
*d
, uint32_t tag
,
345 uint32_t lun
, void *hba_private
)
349 req
= g_malloc0(reqops
->size
);
351 req
->bus
= scsi_bus_from_device(d
);
355 req
->hba_private
= hba_private
;
359 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
363 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
364 uint8_t *buf
, void *hba_private
)
366 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
370 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
371 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
372 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
374 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
377 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
381 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
382 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
383 (buf
[0] != INQUIRY
&&
384 buf
[0] != REPORT_LUNS
&&
385 buf
[0] != GET_CONFIGURATION
&&
386 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
)) {
387 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
389 } else if (lun
!= d
->lun
||
390 buf
[0] == REPORT_LUNS
||
391 buf
[0] == REQUEST_SENSE
) {
392 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
395 req
= d
->info
->alloc_req(d
, tag
, lun
, hba_private
);
402 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
404 case TEST_UNIT_READY
:
405 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
408 trace_scsi_report_luns(d
->id
, lun
, tag
);
411 trace_scsi_request_sense(d
->id
, lun
, tag
);
420 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
422 return req
->ops
->get_buf(req
);
425 static void scsi_clear_unit_attention(SCSIRequest
*req
)
428 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
429 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
434 * If an INQUIRY command enters the enabled command state,
435 * the device server shall [not] clear any unit attention condition;
436 * See also MMC-6, paragraphs 6.5 and 6.6.2.
438 if (req
->cmd
.buf
[0] == INQUIRY
||
439 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
440 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
444 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
445 ua
= &req
->dev
->unit_attention
;
447 ua
= &req
->bus
->unit_attention
;
451 * If a REPORT LUNS command enters the enabled command state, [...]
452 * the device server shall clear any pending unit attention condition
453 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
455 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
456 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
457 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
461 *ua
= SENSE_CODE(NO_SENSE
);
464 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
469 if (!req
->sense_len
) {
473 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
476 * FIXME: clearing unit attention conditions upon autosense should be done
477 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
480 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
481 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
482 * In the latter case, scsi_req_complete clears unit attention conditions
483 * after moving them to the device's sense buffer.
485 scsi_clear_unit_attention(req
);
489 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
491 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
494 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
496 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
497 sense
.key
, sense
.asc
, sense
.ascq
);
498 memset(req
->sense
, 0, 18);
499 req
->sense
[0] = 0xf0;
500 req
->sense
[2] = sense
.key
;
502 req
->sense
[12] = sense
.asc
;
503 req
->sense
[13] = sense
.ascq
;
507 int32_t scsi_req_enqueue(SCSIRequest
*req
)
511 assert(!req
->enqueued
);
513 req
->enqueued
= true;
514 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
517 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
522 static void scsi_req_dequeue(SCSIRequest
*req
)
524 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
526 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
527 req
->enqueued
= false;
532 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
534 switch (buf
[0] >> 5) {
538 /* length 0 means 256 blocks */
539 if (cmd
->xfer
== 0) {
545 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
549 cmd
->xfer
= buf
[13] | (buf
[12] << 8) | (buf
[11] << 16) | (buf
[10] << 24);
553 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16) | (buf
[6] << 24);
561 case TEST_UNIT_READY
:
565 case WRITE_FILEMARKS
:
570 case ALLOW_MEDIUM_REMOVAL
:
573 case SYNCHRONIZE_CACHE
:
574 case LOCK_UNLOCK_CACHE
:
588 case READ_CAPACITY_10
:
591 case READ_BLOCK_LIMITS
:
597 case SEND_VOLUME_TAG
:
604 case WRITE_VERIFY_10
:
607 case WRITE_VERIFY_12
:
609 case WRITE_VERIFY_16
:
610 cmd
->xfer
*= dev
->blocksize
;
615 case RECOVER_BUFFERED_DATA
:
618 cmd
->xfer
*= dev
->blocksize
;
621 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
623 case MAINTENANCE_OUT
:
625 if (dev
->type
== TYPE_ROM
) {
626 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
627 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
634 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
637 /* stream commands */
640 case RECOVER_BUFFERED_DATA
:
643 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
644 if (buf
[1] & 0x01) { /* fixed */
645 cmd
->xfer
*= dev
->blocksize
;
653 /* generic commands */
655 return scsi_req_length(cmd
, dev
, buf
);
660 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
662 switch (cmd
->buf
[0]) {
665 case WRITE_VERIFY_10
:
667 case WRITE_VERIFY_12
:
669 case WRITE_VERIFY_16
:
673 case CHANGE_DEFINITION
:
677 case SEND_DIAGNOSTIC
:
680 case REASSIGN_BLOCKS
:
688 case SEARCH_EQUAL_12
:
691 case SEND_VOLUME_TAG
:
692 case PERSISTENT_RESERVE_OUT
:
693 case MAINTENANCE_OUT
:
694 cmd
->mode
= SCSI_XFER_TO_DEV
;
698 cmd
->mode
= SCSI_XFER_FROM_DEV
;
700 cmd
->mode
= SCSI_XFER_NONE
;
706 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
708 uint8_t *buf
= cmd
->buf
;
711 switch (buf
[0] >> 5) {
713 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
714 (((uint64_t) buf
[1] & 0x1f) << 16);
718 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
719 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
722 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
723 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
724 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
725 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
728 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
729 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
738 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
742 if (dev
->type
== TYPE_TAPE
) {
743 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
745 rc
= scsi_req_length(cmd
, dev
, buf
);
750 memcpy(cmd
->buf
, buf
, cmd
->len
);
751 scsi_cmd_xfer_mode(cmd
);
752 cmd
->lba
= scsi_cmd_lba(cmd
);
757 * Predefined sense codes
760 /* No sense data available */
761 const struct SCSISense sense_code_NO_SENSE
= {
762 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
765 /* LUN not ready, Manual intervention required */
766 const struct SCSISense sense_code_LUN_NOT_READY
= {
767 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
770 /* LUN not ready, Medium not present */
771 const struct SCSISense sense_code_NO_MEDIUM
= {
772 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
775 /* LUN not ready, medium removal prevented */
776 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
777 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
780 /* Hardware error, internal target failure */
781 const struct SCSISense sense_code_TARGET_FAILURE
= {
782 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
785 /* Illegal request, invalid command operation code */
786 const struct SCSISense sense_code_INVALID_OPCODE
= {
787 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
790 /* Illegal request, LBA out of range */
791 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
792 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
795 /* Illegal request, Invalid field in CDB */
796 const struct SCSISense sense_code_INVALID_FIELD
= {
797 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
800 /* Illegal request, LUN not supported */
801 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
802 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
805 /* Illegal request, Saving parameters not supported */
806 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
807 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
810 /* Illegal request, Incompatible medium installed */
811 const struct SCSISense sense_code_INCOMPATIBLE_MEDIUM
= {
812 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
815 /* Illegal request, medium removal prevented */
816 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
817 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
820 /* Command aborted, I/O process terminated */
821 const struct SCSISense sense_code_IO_ERROR
= {
822 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
825 /* Command aborted, I_T Nexus loss occurred */
826 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
827 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
830 /* Command aborted, Logical Unit failure */
831 const struct SCSISense sense_code_LUN_FAILURE
= {
832 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
835 /* Unit attention, Power on, reset or bus device reset occurred */
836 const struct SCSISense sense_code_RESET
= {
837 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
840 /* Unit attention, Medium may have changed */
841 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
842 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
845 /* Unit attention, Reported LUNs data has changed */
846 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
847 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
850 /* Unit attention, Device internal reset */
851 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
852 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
858 * Convert between fixed and descriptor sense buffers
860 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
861 uint8_t *buf
, int len
, bool fixed
)
865 if (!fixed
&& len
< 8) {
870 sense
.key
= NO_SENSE
;
874 fixed_in
= (in_buf
[0] & 2) == 0;
876 if (fixed
== fixed_in
) {
877 memcpy(buf
, in_buf
, MIN(len
, in_len
));
878 return MIN(len
, in_len
);
882 sense
.key
= in_buf
[2];
883 sense
.asc
= in_buf
[12];
884 sense
.ascq
= in_buf
[13];
886 sense
.key
= in_buf
[1];
887 sense
.asc
= in_buf
[2];
888 sense
.ascq
= in_buf
[3];
894 /* Return fixed format sense buffer */
899 buf
[13] = sense
.ascq
;
902 /* Return descriptor format sense buffer */
911 static const char *scsi_command_name(uint8_t cmd
)
913 static const char *names
[] = {
914 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
915 [ REWIND
] = "REWIND",
916 [ REQUEST_SENSE
] = "REQUEST_SENSE",
917 [ FORMAT_UNIT
] = "FORMAT_UNIT",
918 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
919 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
920 [ READ_6
] = "READ_6",
921 [ WRITE_6
] = "WRITE_6",
922 [ SEEK_6
] = "SEEK_6",
923 [ READ_REVERSE
] = "READ_REVERSE",
924 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
926 [ INQUIRY
] = "INQUIRY",
927 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
928 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
929 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
930 [ MODE_SELECT
] = "MODE_SELECT",
931 [ RESERVE
] = "RESERVE",
932 [ RELEASE
] = "RELEASE",
935 [ MODE_SENSE
] = "MODE_SENSE",
936 [ START_STOP
] = "START_STOP",
937 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
938 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
939 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
940 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
941 [ READ_10
] = "READ_10",
942 [ WRITE_10
] = "WRITE_10",
943 [ SEEK_10
] = "SEEK_10",
944 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
945 [ VERIFY_10
] = "VERIFY_10",
946 [ SEARCH_HIGH
] = "SEARCH_HIGH",
947 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
948 [ SEARCH_LOW
] = "SEARCH_LOW",
949 [ SET_LIMITS
] = "SET_LIMITS",
950 [ PRE_FETCH
] = "PRE_FETCH",
951 /* READ_POSITION and PRE_FETCH use the same operation code */
952 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
953 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
954 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
955 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
956 [ COMPARE
] = "COMPARE",
957 [ COPY_VERIFY
] = "COPY_VERIFY",
958 [ WRITE_BUFFER
] = "WRITE_BUFFER",
959 [ READ_BUFFER
] = "READ_BUFFER",
960 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
961 [ READ_LONG_10
] = "READ_LONG_10",
962 [ WRITE_LONG_10
] = "WRITE_LONG_10",
963 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
964 [ WRITE_SAME_10
] = "WRITE_SAME_10",
966 [ READ_TOC
] = "READ_TOC",
967 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
968 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
969 [ LOG_SELECT
] = "LOG_SELECT",
970 [ LOG_SENSE
] = "LOG_SENSE",
971 [ MODE_SELECT_10
] = "MODE_SELECT_10",
972 [ RESERVE_10
] = "RESERVE_10",
973 [ RELEASE_10
] = "RELEASE_10",
974 [ MODE_SENSE_10
] = "MODE_SENSE_10",
975 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
976 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
977 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
978 [ EXTENDED_COPY
] = "EXTENDED_COPY",
979 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
980 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
981 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
982 [ READ_16
] = "READ_16",
983 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
984 [ WRITE_16
] = "WRITE_16",
985 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
986 [ VERIFY_16
] = "VERIFY_16",
987 [ SYNCHRONIZE_CACHE_16
] = "SYNCHRONIZE_CACHE_16",
988 [ LOCATE_16
] = "LOCATE_16",
989 [ WRITE_SAME_16
] = "WRITE_SAME_16",
990 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
991 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
992 [ WRITE_LONG_16
] = "WRITE_LONG_16",
993 [ REPORT_LUNS
] = "REPORT_LUNS",
995 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
996 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
997 [ READ_12
] = "READ_12",
998 [ WRITE_12
] = "WRITE_12",
999 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1000 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1001 [ VERIFY_12
] = "VERIFY_12",
1002 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1003 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1004 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1005 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1006 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
1007 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1008 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1011 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1016 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1022 void scsi_req_unref(SCSIRequest
*req
)
1024 if (--req
->refcount
== 0) {
1025 if (req
->ops
->free_req
) {
1026 req
->ops
->free_req(req
);
1032 /* Tell the device that we finished processing this chunk of I/O. It
1033 will start the next chunk or complete the command. */
1034 void scsi_req_continue(SCSIRequest
*req
)
1036 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1037 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1038 req
->ops
->write_data(req
);
1040 req
->ops
->read_data(req
);
1044 /* Called by the devices when data is ready for the HBA. The HBA should
1045 start a DMA operation to read or fill the device's data buffer.
1046 Once it completes, calling scsi_req_continue will restart I/O. */
1047 void scsi_req_data(SCSIRequest
*req
, int len
)
1049 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1050 req
->bus
->ops
->transfer_data(req
, len
);
1053 void scsi_req_print(SCSIRequest
*req
)
1058 fprintf(fp
, "[%s id=%d] %s",
1059 req
->dev
->qdev
.parent_bus
->name
,
1061 scsi_command_name(req
->cmd
.buf
[0]));
1062 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1063 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1065 switch (req
->cmd
.mode
) {
1066 case SCSI_XFER_NONE
:
1067 fprintf(fp
, " - none\n");
1069 case SCSI_XFER_FROM_DEV
:
1070 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1072 case SCSI_XFER_TO_DEV
:
1073 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1076 fprintf(fp
, " - Oops\n");
1081 void scsi_req_complete(SCSIRequest
*req
, int status
)
1083 assert(req
->status
== -1);
1084 req
->status
= status
;
1086 assert(req
->sense_len
< sizeof(req
->sense
));
1087 if (status
== GOOD
) {
1091 if (req
->sense_len
) {
1092 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1094 req
->dev
->sense_len
= req
->sense_len
;
1097 * Unit attention state is now stored in the device's sense buffer
1098 * if the HBA didn't do autosense. Clear the pending unit attention
1101 scsi_clear_unit_attention(req
);
1104 scsi_req_dequeue(req
);
1105 req
->bus
->ops
->complete(req
, req
->status
);
1106 scsi_req_unref(req
);
1109 void scsi_req_cancel(SCSIRequest
*req
)
1111 if (req
->ops
->cancel_io
) {
1112 req
->ops
->cancel_io(req
);
1115 scsi_req_dequeue(req
);
1116 if (req
->bus
->ops
->cancel
) {
1117 req
->bus
->ops
->cancel(req
);
1119 scsi_req_unref(req
);
1122 void scsi_req_abort(SCSIRequest
*req
, int status
)
1124 if (req
->ops
->cancel_io
) {
1125 req
->ops
->cancel_io(req
);
1127 scsi_req_complete(req
, status
);
1130 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1134 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1135 req
= QTAILQ_FIRST(&sdev
->requests
);
1136 scsi_req_cancel(req
);
1138 sdev
->unit_attention
= sense
;
1141 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1143 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1144 SCSIBus
*bus
= scsi_bus_from_device(d
);
1148 for (i
= 0; i
< bus
->ndev
; i
++) {
1149 if (bus
->devs
[i
] == d
) {
1154 assert(i
!= bus
->ndev
);
1156 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);
1158 return strdup(path
);