2 #include "qemu-error.h"
7 static struct BusInfo scsi_bus_info
= {
9 .size
= sizeof(SCSIBus
),
10 .props
= (Property
[]) {
11 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
12 DEFINE_PROP_END_OF_LIST(),
15 static int next_scsi_bus
;
17 /* Create a scsi bus, and attach devices to it. */
18 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
19 scsi_completionfn complete
)
21 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
22 bus
->busnr
= next_scsi_bus
++;
25 bus
->complete
= complete
;
26 bus
->qbus
.allow_hotplug
= 1;
29 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
31 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
32 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
33 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
37 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
38 if (bus
->devs
[dev
->id
] == NULL
)
42 if (dev
->id
>= bus
->ndev
) {
43 error_report("bad scsi device id: %d", dev
->id
);
47 if (bus
->devs
[dev
->id
]) {
48 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
50 bus
->devs
[dev
->id
] = dev
;
53 QTAILQ_INIT(&dev
->requests
);
54 rc
= dev
->info
->init(dev
);
56 bus
->devs
[dev
->id
] = NULL
;
63 static int scsi_qdev_exit(DeviceState
*qdev
)
65 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
66 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
68 assert(bus
->devs
[dev
->id
] != NULL
);
69 if (bus
->devs
[dev
->id
]->info
->destroy
) {
70 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
72 bus
->devs
[dev
->id
] = NULL
;
76 void scsi_qdev_register(SCSIDeviceInfo
*info
)
78 info
->qdev
.bus_info
= &scsi_bus_info
;
79 info
->qdev
.init
= scsi_qdev_init
;
80 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
81 info
->qdev
.exit
= scsi_qdev_exit
;
82 qdev_register(&info
->qdev
);
85 /* handle legacy '-drive if=scsi,...' cmd line args */
86 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
, int unit
)
91 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
92 dev
= qdev_create(&bus
->qbus
, driver
);
93 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
94 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
98 if (qdev_init(dev
) < 0)
100 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
103 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
110 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
111 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
115 qemu_opts_loc_restore(dinfo
->opts
);
116 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
)) {
125 void scsi_dev_clear_sense(SCSIDevice
*dev
)
127 memset(&dev
->sense
, 0, sizeof(dev
->sense
));
130 void scsi_dev_set_sense(SCSIDevice
*dev
, uint8_t key
)
132 dev
->sense
.key
= key
;
135 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
139 req
= qemu_mallocz(size
);
140 req
->bus
= scsi_bus_from_device(d
);
145 req
->enqueued
= true;
146 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
150 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
154 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
155 if (req
->tag
== tag
) {
162 static void scsi_req_dequeue(SCSIRequest
*req
)
165 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
166 req
->enqueued
= false;
170 void scsi_req_free(SCSIRequest
*req
)
172 scsi_req_dequeue(req
);
176 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
178 switch (cmd
[0] >> 5) {
180 req
->cmd
.xfer
= cmd
[4];
182 /* length 0 means 256 blocks */
183 if (req
->cmd
.xfer
== 0)
188 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
192 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
196 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
204 case TEST_UNIT_READY
:
208 case WRITE_FILEMARKS
:
211 case ALLOW_MEDIUM_REMOVAL
:
214 case SYNCHRONIZE_CACHE
:
215 case LOCK_UNLOCK_CACHE
:
232 case READ_BLOCK_LIMITS
:
238 case SEND_VOLUME_TAG
:
248 case WRITE_VERIFY_12
:
250 case WRITE_VERIFY_16
:
251 req
->cmd
.xfer
*= req
->dev
->blocksize
;
256 case RECOVER_BUFFERED_DATA
:
259 req
->cmd
.xfer
*= req
->dev
->blocksize
;
262 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
264 case MAINTENANCE_OUT
:
266 if (req
->dev
->type
== TYPE_ROM
) {
267 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
268 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
275 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
278 /* stream commands */
281 case RECOVER_BUFFERED_DATA
:
284 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
285 if (cmd
[1] & 0x01) /* fixed */
286 req
->cmd
.xfer
*= req
->dev
->blocksize
;
293 /* generic commands */
295 return scsi_req_length(req
, cmd
);
300 static void scsi_req_xfer_mode(SCSIRequest
*req
)
302 switch (req
->cmd
.buf
[0]) {
307 case WRITE_VERIFY_12
:
309 case WRITE_VERIFY_16
:
313 case CHANGE_DEFINITION
:
317 case SEND_DIAGNOSTIC
:
320 case REASSIGN_BLOCKS
:
329 case SEARCH_EQUAL_12
:
333 case SEND_VOLUME_TAG
:
335 case PERSISTENT_RESERVE_OUT
:
336 case MAINTENANCE_OUT
:
337 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
341 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
343 req
->cmd
.mode
= SCSI_XFER_NONE
;
349 static uint64_t scsi_req_lba(SCSIRequest
*req
)
351 uint8_t *buf
= req
->cmd
.buf
;
354 switch (buf
[0] >> 5) {
356 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
357 (((uint64_t) buf
[1] & 0x1f) << 16);
361 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
362 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
365 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
366 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
367 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
368 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
371 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
372 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
381 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
385 if (req
->dev
->type
== TYPE_TAPE
) {
386 rc
= scsi_req_stream_length(req
, buf
);
388 rc
= scsi_req_length(req
, buf
);
393 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
394 scsi_req_xfer_mode(req
);
395 req
->cmd
.lba
= scsi_req_lba(req
);
399 static const char *scsi_command_name(uint8_t cmd
)
401 static const char *names
[] = {
402 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
403 [ REZERO_UNIT
] = "REZERO_UNIT",
404 /* REWIND and REZERO_UNIT use the same operation code */
405 [ REQUEST_SENSE
] = "REQUEST_SENSE",
406 [ FORMAT_UNIT
] = "FORMAT_UNIT",
407 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
408 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
409 [ READ_6
] = "READ_6",
410 [ WRITE_6
] = "WRITE_6",
411 [ SEEK_6
] = "SEEK_6",
412 [ READ_REVERSE
] = "READ_REVERSE",
413 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
415 [ INQUIRY
] = "INQUIRY",
416 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
417 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
418 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
419 [ MODE_SELECT
] = "MODE_SELECT",
420 [ RESERVE
] = "RESERVE",
421 [ RELEASE
] = "RELEASE",
424 [ MODE_SENSE
] = "MODE_SENSE",
425 [ START_STOP
] = "START_STOP",
426 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
427 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
428 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
430 [ SET_WINDOW
] = "SET_WINDOW",
431 [ READ_CAPACITY
] = "READ_CAPACITY",
432 [ READ_10
] = "READ_10",
433 [ WRITE_10
] = "WRITE_10",
434 [ SEEK_10
] = "SEEK_10",
435 [ WRITE_VERIFY
] = "WRITE_VERIFY",
436 [ VERIFY
] = "VERIFY",
437 [ SEARCH_HIGH
] = "SEARCH_HIGH",
438 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
439 [ SEARCH_LOW
] = "SEARCH_LOW",
440 [ SET_LIMITS
] = "SET_LIMITS",
441 [ PRE_FETCH
] = "PRE_FETCH",
442 /* READ_POSITION and PRE_FETCH use the same operation code */
443 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
444 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
445 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
446 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
447 [ COMPARE
] = "COMPARE",
448 [ COPY_VERIFY
] = "COPY_VERIFY",
449 [ WRITE_BUFFER
] = "WRITE_BUFFER",
450 [ READ_BUFFER
] = "READ_BUFFER",
451 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
452 [ READ_LONG
] = "READ_LONG",
453 [ WRITE_LONG
] = "WRITE_LONG",
454 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
455 [ WRITE_SAME
] = "WRITE_SAME",
456 [ READ_TOC
] = "READ_TOC",
457 [ LOG_SELECT
] = "LOG_SELECT",
458 [ LOG_SENSE
] = "LOG_SENSE",
459 [ MODE_SELECT_10
] = "MODE_SELECT_10",
460 [ RESERVE_10
] = "RESERVE_10",
461 [ RELEASE_10
] = "RELEASE_10",
462 [ MODE_SENSE_10
] = "MODE_SENSE_10",
463 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
464 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
465 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
466 [ READ_12
] = "READ_12",
467 [ WRITE_12
] = "WRITE_12",
468 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
469 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
470 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
471 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
472 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
473 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
474 [ WRITE_LONG_2
] = "WRITE_LONG_2",
476 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
477 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
478 [ READ_16
] = "READ_16",
479 [ WRITE_16
] = "WRITE_16",
480 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
481 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
482 [ REPORT_LUNS
] = "REPORT_LUNS",
483 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
484 [ SET_CD_SPEED
] = "SET_CD_SPEED",
488 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
493 void scsi_req_print(SCSIRequest
*req
)
498 fprintf(fp
, "[%s id=%d] %s",
499 req
->dev
->qdev
.parent_bus
->name
,
501 scsi_command_name(req
->cmd
.buf
[0]));
502 for (i
= 1; i
< req
->cmd
.len
; i
++) {
503 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
505 switch (req
->cmd
.mode
) {
507 fprintf(fp
, " - none\n");
509 case SCSI_XFER_FROM_DEV
:
510 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
512 case SCSI_XFER_TO_DEV
:
513 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
516 fprintf(fp
, " - Oops\n");
521 void scsi_req_complete(SCSIRequest
*req
)
523 assert(req
->status
!= -1);
524 scsi_req_dequeue(req
);
525 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,