2 #include "qemu-error.h"
8 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
10 static struct BusInfo scsi_bus_info
= {
12 .size
= sizeof(SCSIBus
),
13 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
14 .props
= (Property
[]) {
15 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
16 DEFINE_PROP_END_OF_LIST(),
19 static int next_scsi_bus
;
21 /* Create a scsi bus, and attach devices to it. */
22 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
23 scsi_completionfn complete
)
25 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
26 bus
->busnr
= next_scsi_bus
++;
29 bus
->complete
= complete
;
30 bus
->qbus
.allow_hotplug
= 1;
33 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
35 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
36 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
37 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
41 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
42 if (bus
->devs
[dev
->id
] == NULL
)
46 if (dev
->id
>= bus
->ndev
) {
47 error_report("bad scsi device id: %d", dev
->id
);
51 if (bus
->devs
[dev
->id
]) {
52 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
54 bus
->devs
[dev
->id
] = dev
;
57 QTAILQ_INIT(&dev
->requests
);
58 rc
= dev
->info
->init(dev
);
60 bus
->devs
[dev
->id
] = NULL
;
67 static int scsi_qdev_exit(DeviceState
*qdev
)
69 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
70 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
72 assert(bus
->devs
[dev
->id
] != NULL
);
73 if (bus
->devs
[dev
->id
]->info
->destroy
) {
74 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
76 bus
->devs
[dev
->id
] = NULL
;
80 void scsi_qdev_register(SCSIDeviceInfo
*info
)
82 info
->qdev
.bus_info
= &scsi_bus_info
;
83 info
->qdev
.init
= scsi_qdev_init
;
84 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
85 info
->qdev
.exit
= scsi_qdev_exit
;
86 qdev_register(&info
->qdev
);
89 /* handle legacy '-drive if=scsi,...' cmd line args */
90 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
91 int unit
, bool removable
)
96 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
97 dev
= qdev_create(&bus
->qbus
, driver
);
98 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
99 if (qdev_prop_exists(dev
, "removable")) {
100 qdev_prop_set_bit(dev
, "removable", removable
);
102 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
106 if (qdev_init(dev
) < 0)
108 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
111 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
118 for (unit
= 0; unit
< bus
->ndev
; unit
++) {
119 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
123 qemu_opts_loc_restore(dinfo
->opts
);
124 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false)) {
133 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
137 req
= qemu_mallocz(size
);
138 req
->bus
= scsi_bus_from_device(d
);
143 req
->enqueued
= true;
144 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
148 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
152 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
153 if (req
->tag
== tag
) {
160 static void scsi_req_dequeue(SCSIRequest
*req
)
163 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
164 req
->enqueued
= false;
168 void scsi_req_free(SCSIRequest
*req
)
170 scsi_req_dequeue(req
);
174 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
176 switch (cmd
[0] >> 5) {
178 req
->cmd
.xfer
= cmd
[4];
180 /* length 0 means 256 blocks */
181 if (req
->cmd
.xfer
== 0)
186 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
190 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
194 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
202 case TEST_UNIT_READY
:
206 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
:
328 case SEARCH_EQUAL_12
:
332 case SEND_VOLUME_TAG
:
334 case PERSISTENT_RESERVE_OUT
:
335 case MAINTENANCE_OUT
:
336 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
340 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
342 req
->cmd
.mode
= SCSI_XFER_NONE
;
348 static uint64_t scsi_req_lba(SCSIRequest
*req
)
350 uint8_t *buf
= req
->cmd
.buf
;
353 switch (buf
[0] >> 5) {
355 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
356 (((uint64_t) buf
[1] & 0x1f) << 16);
360 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
361 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
364 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
365 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
366 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
367 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
370 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
371 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
380 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
384 if (req
->dev
->type
== TYPE_TAPE
) {
385 rc
= scsi_req_stream_length(req
, buf
);
387 rc
= scsi_req_length(req
, buf
);
392 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
393 scsi_req_xfer_mode(req
);
394 req
->cmd
.lba
= scsi_req_lba(req
);
398 static const char *scsi_command_name(uint8_t cmd
)
400 static const char *names
[] = {
401 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
402 [ REZERO_UNIT
] = "REZERO_UNIT",
403 /* REWIND and REZERO_UNIT use the same operation code */
404 [ REQUEST_SENSE
] = "REQUEST_SENSE",
405 [ FORMAT_UNIT
] = "FORMAT_UNIT",
406 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
407 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
408 [ READ_6
] = "READ_6",
409 [ WRITE_6
] = "WRITE_6",
410 [ SEEK_6
] = "SEEK_6",
411 [ READ_REVERSE
] = "READ_REVERSE",
412 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
414 [ INQUIRY
] = "INQUIRY",
415 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
416 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
417 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
418 [ MODE_SELECT
] = "MODE_SELECT",
419 [ RESERVE
] = "RESERVE",
420 [ RELEASE
] = "RELEASE",
423 [ MODE_SENSE
] = "MODE_SENSE",
424 [ START_STOP
] = "START_STOP",
425 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
426 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
427 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
429 [ SET_WINDOW
] = "SET_WINDOW",
430 [ READ_CAPACITY
] = "READ_CAPACITY",
431 [ READ_10
] = "READ_10",
432 [ WRITE_10
] = "WRITE_10",
433 [ SEEK_10
] = "SEEK_10",
434 [ WRITE_VERIFY
] = "WRITE_VERIFY",
435 [ VERIFY
] = "VERIFY",
436 [ SEARCH_HIGH
] = "SEARCH_HIGH",
437 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
438 [ SEARCH_LOW
] = "SEARCH_LOW",
439 [ SET_LIMITS
] = "SET_LIMITS",
440 [ PRE_FETCH
] = "PRE_FETCH",
441 /* READ_POSITION and PRE_FETCH use the same operation code */
442 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
443 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
444 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
445 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
446 [ COMPARE
] = "COMPARE",
447 [ COPY_VERIFY
] = "COPY_VERIFY",
448 [ WRITE_BUFFER
] = "WRITE_BUFFER",
449 [ READ_BUFFER
] = "READ_BUFFER",
450 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
451 [ READ_LONG
] = "READ_LONG",
452 [ WRITE_LONG
] = "WRITE_LONG",
453 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
454 [ WRITE_SAME
] = "WRITE_SAME",
455 [ READ_TOC
] = "READ_TOC",
456 [ LOG_SELECT
] = "LOG_SELECT",
457 [ LOG_SENSE
] = "LOG_SENSE",
458 [ MODE_SELECT_10
] = "MODE_SELECT_10",
459 [ RESERVE_10
] = "RESERVE_10",
460 [ RELEASE_10
] = "RELEASE_10",
461 [ MODE_SENSE_10
] = "MODE_SENSE_10",
462 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
463 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
464 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
465 [ READ_12
] = "READ_12",
466 [ WRITE_12
] = "WRITE_12",
467 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
468 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
469 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
470 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
471 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
472 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
473 [ WRITE_LONG_2
] = "WRITE_LONG_2",
475 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
476 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
477 [ READ_16
] = "READ_16",
478 [ WRITE_16
] = "WRITE_16",
479 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
480 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
481 [ REPORT_LUNS
] = "REPORT_LUNS",
482 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
483 [ SET_CD_SPEED
] = "SET_CD_SPEED",
487 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
492 void scsi_req_print(SCSIRequest
*req
)
497 fprintf(fp
, "[%s id=%d] %s",
498 req
->dev
->qdev
.parent_bus
->name
,
500 scsi_command_name(req
->cmd
.buf
[0]));
501 for (i
= 1; i
< req
->cmd
.len
; i
++) {
502 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
504 switch (req
->cmd
.mode
) {
506 fprintf(fp
, " - none\n");
508 case SCSI_XFER_FROM_DEV
:
509 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
511 case SCSI_XFER_TO_DEV
:
512 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
515 fprintf(fp
, " - Oops\n");
520 void scsi_req_complete(SCSIRequest
*req
)
522 assert(req
->status
!= -1);
523 scsi_req_dequeue(req
);
524 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,
529 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
531 SCSIDevice
*d
= (SCSIDevice
*)dev
;
532 SCSIBus
*bus
= scsi_bus_from_device(d
);
536 for (i
= 0; i
< bus
->ndev
; i
++) {
537 if (bus
->devs
[i
] == d
) {
542 assert(i
!= bus
->ndev
);
544 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);