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
, req
->cmd
.xfer
,
296 (req
->cmd
.buf
[1] & 1) == 0);
299 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
300 scsi_req_complete(req
, CHECK_CONDITION
);
303 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
304 scsi_req_complete(req
, CHECK_CONDITION
);
309 scsi_req_complete(req
, GOOD
);
314 static void scsi_target_read_data(SCSIRequest
*req
)
316 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
322 scsi_req_data(&r
->req
, n
);
324 scsi_req_complete(&r
->req
, GOOD
);
328 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
330 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
335 struct SCSIReqOps reqops_target_command
= {
336 .size
= sizeof(SCSITargetReq
),
337 .send_command
= scsi_target_send_command
,
338 .read_data
= scsi_target_read_data
,
339 .get_buf
= scsi_target_get_buf
,
343 SCSIRequest
*scsi_req_alloc(SCSIReqOps
*reqops
, SCSIDevice
*d
, uint32_t tag
,
344 uint32_t lun
, void *hba_private
)
348 req
= qemu_mallocz(reqops
->size
);
350 req
->bus
= scsi_bus_from_device(d
);
354 req
->hba_private
= hba_private
;
358 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
362 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
363 uint8_t *buf
, void *hba_private
)
365 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
369 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
370 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
371 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
373 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
375 if (req
->cmd
.lba
!= -1) {
376 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
380 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
381 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
382 (buf
[0] != INQUIRY
&&
383 buf
[0] != REPORT_LUNS
&&
384 buf
[0] != GET_CONFIGURATION
&&
385 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
)) {
386 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
388 } else if (lun
!= d
->lun
||
389 buf
[0] == REPORT_LUNS
||
390 buf
[0] == REQUEST_SENSE
) {
391 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
394 req
= d
->info
->alloc_req(d
, tag
, lun
, hba_private
);
401 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
403 case TEST_UNIT_READY
:
404 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
407 trace_scsi_report_luns(d
->id
, lun
, tag
);
410 trace_scsi_request_sense(d
->id
, lun
, tag
);
419 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
421 return req
->ops
->get_buf(req
);
424 static void scsi_clear_unit_attention(SCSIRequest
*req
)
427 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
428 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
433 * If an INQUIRY command enters the enabled command state,
434 * the device server shall [not] clear any unit attention condition;
435 * See also MMC-6, paragraphs 6.5 and 6.6.2.
437 if (req
->cmd
.buf
[0] == INQUIRY
||
438 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
439 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
443 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
444 ua
= &req
->dev
->unit_attention
;
446 ua
= &req
->bus
->unit_attention
;
450 * If a REPORT LUNS command enters the enabled command state, [...]
451 * the device server shall clear any pending unit attention condition
452 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
454 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
455 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
456 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
460 *ua
= SENSE_CODE(NO_SENSE
);
463 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
468 if (!req
->sense_len
) {
472 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
475 * FIXME: clearing unit attention conditions upon autosense should be done
476 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
479 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
480 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
481 * In the latter case, scsi_req_complete clears unit attention conditions
482 * after moving them to the device's sense buffer.
484 scsi_clear_unit_attention(req
);
488 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
490 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
493 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
495 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
496 sense
.key
, sense
.asc
, sense
.ascq
);
497 memset(req
->sense
, 0, 18);
498 req
->sense
[0] = 0xf0;
499 req
->sense
[2] = sense
.key
;
500 req
->sense
[12] = sense
.asc
;
501 req
->sense
[13] = sense
.ascq
;
505 int32_t scsi_req_enqueue(SCSIRequest
*req
)
509 assert(!req
->enqueued
);
511 req
->enqueued
= true;
512 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
515 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
520 static void scsi_req_dequeue(SCSIRequest
*req
)
522 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
524 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
525 req
->enqueued
= false;
530 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
532 switch (buf
[0] >> 5) {
536 /* length 0 means 256 blocks */
537 if (cmd
->xfer
== 0) {
543 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
547 cmd
->xfer
= buf
[13] | (buf
[12] << 8) | (buf
[11] << 16) | (buf
[10] << 24);
551 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16) | (buf
[6] << 24);
559 case TEST_UNIT_READY
:
563 case WRITE_FILEMARKS
:
568 case ALLOW_MEDIUM_REMOVAL
:
571 case SYNCHRONIZE_CACHE
:
572 case LOCK_UNLOCK_CACHE
:
586 case READ_CAPACITY_10
:
589 case READ_BLOCK_LIMITS
:
595 case SEND_VOLUME_TAG
:
602 case WRITE_VERIFY_10
:
605 case WRITE_VERIFY_12
:
607 case WRITE_VERIFY_16
:
608 cmd
->xfer
*= dev
->blocksize
;
613 case RECOVER_BUFFERED_DATA
:
616 cmd
->xfer
*= dev
->blocksize
;
619 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
621 case MAINTENANCE_OUT
:
623 if (dev
->type
== TYPE_ROM
) {
624 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
625 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
632 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
635 /* stream commands */
638 case RECOVER_BUFFERED_DATA
:
641 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
642 if (buf
[1] & 0x01) { /* fixed */
643 cmd
->xfer
*= dev
->blocksize
;
651 /* generic commands */
653 return scsi_req_length(cmd
, dev
, buf
);
658 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
660 switch (cmd
->buf
[0]) {
663 case WRITE_VERIFY_10
:
665 case WRITE_VERIFY_12
:
667 case WRITE_VERIFY_16
:
671 case CHANGE_DEFINITION
:
675 case SEND_DIAGNOSTIC
:
678 case REASSIGN_BLOCKS
:
686 case SEARCH_EQUAL_12
:
689 case SEND_VOLUME_TAG
:
690 case PERSISTENT_RESERVE_OUT
:
691 case MAINTENANCE_OUT
:
692 cmd
->mode
= SCSI_XFER_TO_DEV
;
696 cmd
->mode
= SCSI_XFER_FROM_DEV
;
698 cmd
->mode
= SCSI_XFER_NONE
;
704 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
706 uint8_t *buf
= cmd
->buf
;
709 switch (buf
[0] >> 5) {
711 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
712 (((uint64_t) buf
[1] & 0x1f) << 16);
716 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
717 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
720 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
721 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
722 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
723 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
726 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
727 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
736 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
740 if (dev
->type
== TYPE_TAPE
) {
741 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
743 rc
= scsi_req_length(cmd
, dev
, buf
);
748 memcpy(cmd
->buf
, buf
, cmd
->len
);
749 scsi_cmd_xfer_mode(cmd
);
750 cmd
->lba
= scsi_cmd_lba(cmd
);
755 * Predefined sense codes
758 /* No sense data available */
759 const struct SCSISense sense_code_NO_SENSE
= {
760 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
763 /* LUN not ready, Manual intervention required */
764 const struct SCSISense sense_code_LUN_NOT_READY
= {
765 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
768 /* LUN not ready, Medium not present */
769 const struct SCSISense sense_code_NO_MEDIUM
= {
770 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
773 /* Hardware error, internal target failure */
774 const struct SCSISense sense_code_TARGET_FAILURE
= {
775 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
778 /* Illegal request, invalid command operation code */
779 const struct SCSISense sense_code_INVALID_OPCODE
= {
780 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
783 /* Illegal request, LBA out of range */
784 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
785 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
788 /* Illegal request, Invalid field in CDB */
789 const struct SCSISense sense_code_INVALID_FIELD
= {
790 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
793 /* Illegal request, LUN not supported */
794 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
795 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
798 /* Illegal request, Saving parameters not supported */
799 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
800 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
803 /* Illegal request, Incompatible medium installed */
804 const struct SCSISense sense_code_INCOMPATIBLE_MEDIUM
= {
805 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
808 /* Command aborted, I/O process terminated */
809 const struct SCSISense sense_code_IO_ERROR
= {
810 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
813 /* Command aborted, I_T Nexus loss occurred */
814 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
815 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
818 /* Command aborted, Logical Unit failure */
819 const struct SCSISense sense_code_LUN_FAILURE
= {
820 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
823 /* Unit attention, Power on, reset or bus device reset occurred */
824 const struct SCSISense sense_code_RESET
= {
825 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
828 /* Unit attention, Medium may have changed */
829 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
830 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
833 /* Unit attention, Reported LUNs data has changed */
834 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
835 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
838 /* Unit attention, Device internal reset */
839 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
840 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
846 * Convert between fixed and descriptor sense buffers
848 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
849 uint8_t *buf
, int len
, bool fixed
)
853 if (!fixed
&& len
< 8) {
858 sense
.key
= NO_SENSE
;
862 fixed_in
= (in_buf
[0] & 2) == 0;
864 if (fixed
== fixed_in
) {
865 memcpy(buf
, in_buf
, MIN(len
, in_len
));
866 return MIN(len
, in_len
);
870 sense
.key
= in_buf
[2];
871 sense
.asc
= in_buf
[12];
872 sense
.ascq
= in_buf
[13];
874 sense
.key
= in_buf
[1];
875 sense
.asc
= in_buf
[2];
876 sense
.ascq
= in_buf
[3];
882 /* Return fixed format sense buffer */
887 buf
[13] = sense
.ascq
;
890 /* Return descriptor format sense buffer */
899 static const char *scsi_command_name(uint8_t cmd
)
901 static const char *names
[] = {
902 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
903 [ REWIND
] = "REWIND",
904 [ REQUEST_SENSE
] = "REQUEST_SENSE",
905 [ FORMAT_UNIT
] = "FORMAT_UNIT",
906 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
907 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
908 [ READ_6
] = "READ_6",
909 [ WRITE_6
] = "WRITE_6",
910 [ SEEK_6
] = "SEEK_6",
911 [ READ_REVERSE
] = "READ_REVERSE",
912 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
914 [ INQUIRY
] = "INQUIRY",
915 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
916 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
917 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
918 [ MODE_SELECT
] = "MODE_SELECT",
919 [ RESERVE
] = "RESERVE",
920 [ RELEASE
] = "RELEASE",
923 [ MODE_SENSE
] = "MODE_SENSE",
924 [ START_STOP
] = "START_STOP",
925 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
926 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
927 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
928 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
929 [ READ_10
] = "READ_10",
930 [ WRITE_10
] = "WRITE_10",
931 [ SEEK_10
] = "SEEK_10",
932 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
933 [ VERIFY_10
] = "VERIFY_10",
934 [ SEARCH_HIGH
] = "SEARCH_HIGH",
935 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
936 [ SEARCH_LOW
] = "SEARCH_LOW",
937 [ SET_LIMITS
] = "SET_LIMITS",
938 [ PRE_FETCH
] = "PRE_FETCH",
939 /* READ_POSITION and PRE_FETCH use the same operation code */
940 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
941 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
942 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
943 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
944 [ COMPARE
] = "COMPARE",
945 [ COPY_VERIFY
] = "COPY_VERIFY",
946 [ WRITE_BUFFER
] = "WRITE_BUFFER",
947 [ READ_BUFFER
] = "READ_BUFFER",
948 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
949 [ READ_LONG_10
] = "READ_LONG_10",
950 [ WRITE_LONG_10
] = "WRITE_LONG_10",
951 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
952 [ WRITE_SAME_10
] = "WRITE_SAME_10",
954 [ READ_TOC
] = "READ_TOC",
955 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
956 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
957 [ LOG_SELECT
] = "LOG_SELECT",
958 [ LOG_SENSE
] = "LOG_SENSE",
959 [ MODE_SELECT_10
] = "MODE_SELECT_10",
960 [ RESERVE_10
] = "RESERVE_10",
961 [ RELEASE_10
] = "RELEASE_10",
962 [ MODE_SENSE_10
] = "MODE_SENSE_10",
963 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
964 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
965 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
966 [ EXTENDED_COPY
] = "EXTENDED_COPY",
967 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
968 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
969 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
970 [ READ_16
] = "READ_16",
971 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
972 [ WRITE_16
] = "WRITE_16",
973 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
974 [ VERIFY_16
] = "VERIFY_16",
975 [ SYNCHRONIZE_CACHE_16
] = "SYNCHRONIZE_CACHE_16",
976 [ LOCATE_16
] = "LOCATE_16",
977 [ WRITE_SAME_16
] = "WRITE_SAME_16",
978 [ ERASE_16
] = "ERASE_16",
979 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
980 [ WRITE_LONG_16
] = "WRITE_LONG_16",
981 [ REPORT_LUNS
] = "REPORT_LUNS",
983 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
984 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
985 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
986 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
987 [ READ_12
] = "READ_12",
988 [ WRITE_12
] = "WRITE_12",
989 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
990 [ VERIFY_12
] = "VERIFY_12",
991 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
992 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
993 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
994 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
995 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
996 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
997 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1000 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1005 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1011 void scsi_req_unref(SCSIRequest
*req
)
1013 if (--req
->refcount
== 0) {
1014 if (req
->ops
->free_req
) {
1015 req
->ops
->free_req(req
);
1021 /* Tell the device that we finished processing this chunk of I/O. It
1022 will start the next chunk or complete the command. */
1023 void scsi_req_continue(SCSIRequest
*req
)
1025 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1026 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1027 req
->ops
->write_data(req
);
1029 req
->ops
->read_data(req
);
1033 /* Called by the devices when data is ready for the HBA. The HBA should
1034 start a DMA operation to read or fill the device's data buffer.
1035 Once it completes, calling scsi_req_continue will restart I/O. */
1036 void scsi_req_data(SCSIRequest
*req
, int len
)
1038 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1039 req
->bus
->ops
->transfer_data(req
, len
);
1042 void scsi_req_print(SCSIRequest
*req
)
1047 fprintf(fp
, "[%s id=%d] %s",
1048 req
->dev
->qdev
.parent_bus
->name
,
1050 scsi_command_name(req
->cmd
.buf
[0]));
1051 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1052 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1054 switch (req
->cmd
.mode
) {
1055 case SCSI_XFER_NONE
:
1056 fprintf(fp
, " - none\n");
1058 case SCSI_XFER_FROM_DEV
:
1059 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1061 case SCSI_XFER_TO_DEV
:
1062 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1065 fprintf(fp
, " - Oops\n");
1070 void scsi_req_complete(SCSIRequest
*req
, int status
)
1072 assert(req
->status
== -1);
1073 req
->status
= status
;
1075 assert(req
->sense_len
< sizeof(req
->sense
));
1076 if (status
== GOOD
) {
1080 if (req
->sense_len
) {
1081 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1083 req
->dev
->sense_len
= req
->sense_len
;
1086 * Unit attention state is now stored in the device's sense buffer
1087 * if the HBA didn't do autosense. Clear the pending unit attention
1090 scsi_clear_unit_attention(req
);
1093 scsi_req_dequeue(req
);
1094 req
->bus
->ops
->complete(req
, req
->status
);
1095 scsi_req_unref(req
);
1098 void scsi_req_cancel(SCSIRequest
*req
)
1100 if (req
->ops
->cancel_io
) {
1101 req
->ops
->cancel_io(req
);
1104 scsi_req_dequeue(req
);
1105 if (req
->bus
->ops
->cancel
) {
1106 req
->bus
->ops
->cancel(req
);
1108 scsi_req_unref(req
);
1111 void scsi_req_abort(SCSIRequest
*req
, int status
)
1113 if (req
->ops
->cancel_io
) {
1114 req
->ops
->cancel_io(req
);
1116 scsi_req_complete(req
, status
);
1119 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1123 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1124 req
= QTAILQ_FIRST(&sdev
->requests
);
1125 scsi_req_cancel(req
);
1127 sdev
->unit_attention
= sense
;
1130 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1132 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1133 SCSIBus
*bus
= scsi_bus_from_device(d
);
1137 for (i
= 0; i
< bus
->ndev
; i
++) {
1138 if (bus
->devs
[i
] == d
) {
1143 assert(i
!= bus
->ndev
);
1145 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);
1147 return strdup(path
);