2 #include "qemu-error.h"
8 static struct BusInfo scsi_bus_info
= {
10 .size
= sizeof(SCSIBus
),
11 .props
= (Property
[]) {
12 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
13 DEFINE_PROP_END_OF_LIST(),
16 static int next_scsi_bus
;
18 /* Create a scsi bus, and attach devices to it. */
19 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
20 scsi_completionfn complete
)
22 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
23 bus
->busnr
= next_scsi_bus
++;
26 bus
->complete
= complete
;
27 bus
->qbus
.allow_hotplug
= 1;
30 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
32 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
33 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
34 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
38 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
39 if (bus
->devs
[dev
->id
] == NULL
)
43 if (dev
->id
>= bus
->ndev
) {
44 error_report("bad scsi device id: %d", dev
->id
);
48 if (bus
->devs
[dev
->id
]) {
49 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
51 bus
->devs
[dev
->id
] = dev
;
54 QTAILQ_INIT(&dev
->requests
);
55 rc
= dev
->info
->init(dev
);
57 bus
->devs
[dev
->id
] = NULL
;
64 static int scsi_qdev_exit(DeviceState
*qdev
)
66 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
67 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
69 assert(bus
->devs
[dev
->id
] != NULL
);
70 if (bus
->devs
[dev
->id
]->info
->destroy
) {
71 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
73 bus
->devs
[dev
->id
] = NULL
;
77 void scsi_qdev_register(SCSIDeviceInfo
*info
)
79 info
->qdev
.bus_info
= &scsi_bus_info
;
80 info
->qdev
.init
= scsi_qdev_init
;
81 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
82 info
->qdev
.exit
= scsi_qdev_exit
;
83 qdev_register(&info
->qdev
);
86 /* handle legacy '-drive if=scsi,...' cmd line args */
87 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
, int unit
)
92 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
93 dev
= qdev_create(&bus
->qbus
, driver
);
94 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
95 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
99 if (qdev_init(dev
) < 0)
101 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
104 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
111 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
112 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
116 qemu_opts_loc_restore(dinfo
->opts
);
117 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
)) {
126 void scsi_dev_clear_sense(SCSIDevice
*dev
)
128 memset(&dev
->sense
, 0, sizeof(dev
->sense
));
131 void scsi_dev_set_sense(SCSIDevice
*dev
, uint8_t key
)
133 dev
->sense
.key
= key
;
136 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
140 req
= qemu_mallocz(size
);
141 req
->bus
= scsi_bus_from_device(d
);
146 req
->enqueued
= true;
147 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
151 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
155 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
156 if (req
->tag
== tag
) {
163 static void scsi_req_dequeue(SCSIRequest
*req
)
166 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
167 req
->enqueued
= false;
171 void scsi_req_free(SCSIRequest
*req
)
173 scsi_req_dequeue(req
);
177 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
179 switch (cmd
[0] >> 5) {
181 req
->cmd
.xfer
= cmd
[4];
183 /* length 0 means 256 blocks */
184 if (req
->cmd
.xfer
== 0)
189 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
193 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
197 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
205 case TEST_UNIT_READY
:
209 case WRITE_FILEMARKS
:
212 case ALLOW_MEDIUM_REMOVAL
:
215 case SYNCHRONIZE_CACHE
:
216 case LOCK_UNLOCK_CACHE
:
233 case READ_BLOCK_LIMITS
:
239 case SEND_VOLUME_TAG
:
249 case WRITE_VERIFY_12
:
251 case WRITE_VERIFY_16
:
252 req
->cmd
.xfer
*= req
->dev
->blocksize
;
257 case RECOVER_BUFFERED_DATA
:
260 req
->cmd
.xfer
*= req
->dev
->blocksize
;
263 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
265 case MAINTENANCE_OUT
:
267 if (req
->dev
->type
== TYPE_ROM
) {
268 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
269 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
276 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
279 /* stream commands */
282 case RECOVER_BUFFERED_DATA
:
285 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
286 if (cmd
[1] & 0x01) /* fixed */
287 req
->cmd
.xfer
*= req
->dev
->blocksize
;
294 /* generic commands */
296 return scsi_req_length(req
, cmd
);
301 static void scsi_req_xfer_mode(SCSIRequest
*req
)
303 switch (req
->cmd
.buf
[0]) {
308 case WRITE_VERIFY_12
:
310 case WRITE_VERIFY_16
:
314 case CHANGE_DEFINITION
:
318 case SEND_DIAGNOSTIC
:
321 case REASSIGN_BLOCKS
:
330 case SEARCH_EQUAL_12
:
334 case SEND_VOLUME_TAG
:
336 case PERSISTENT_RESERVE_OUT
:
337 case MAINTENANCE_OUT
:
338 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
342 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
344 req
->cmd
.mode
= SCSI_XFER_NONE
;
350 static uint64_t scsi_req_lba(SCSIRequest
*req
)
352 uint8_t *buf
= req
->cmd
.buf
;
355 switch (buf
[0] >> 5) {
357 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
358 (((uint64_t) buf
[1] & 0x1f) << 16);
362 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
363 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
366 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
367 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
368 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
369 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
372 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
373 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
382 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
386 if (req
->dev
->type
== TYPE_TAPE
) {
387 rc
= scsi_req_stream_length(req
, buf
);
389 rc
= scsi_req_length(req
, buf
);
394 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
395 scsi_req_xfer_mode(req
);
396 req
->cmd
.lba
= scsi_req_lba(req
);
400 static const char *scsi_command_name(uint8_t cmd
)
402 static const char *names
[] = {
403 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
404 [ REZERO_UNIT
] = "REZERO_UNIT",
405 /* REWIND and REZERO_UNIT use the same operation code */
406 [ REQUEST_SENSE
] = "REQUEST_SENSE",
407 [ FORMAT_UNIT
] = "FORMAT_UNIT",
408 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
409 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
410 [ READ_6
] = "READ_6",
411 [ WRITE_6
] = "WRITE_6",
412 [ SEEK_6
] = "SEEK_6",
413 [ READ_REVERSE
] = "READ_REVERSE",
414 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
416 [ INQUIRY
] = "INQUIRY",
417 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
418 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
419 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
420 [ MODE_SELECT
] = "MODE_SELECT",
421 [ RESERVE
] = "RESERVE",
422 [ RELEASE
] = "RELEASE",
425 [ MODE_SENSE
] = "MODE_SENSE",
426 [ START_STOP
] = "START_STOP",
427 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
428 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
429 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
431 [ SET_WINDOW
] = "SET_WINDOW",
432 [ READ_CAPACITY
] = "READ_CAPACITY",
433 [ READ_10
] = "READ_10",
434 [ WRITE_10
] = "WRITE_10",
435 [ SEEK_10
] = "SEEK_10",
436 [ WRITE_VERIFY
] = "WRITE_VERIFY",
437 [ VERIFY
] = "VERIFY",
438 [ SEARCH_HIGH
] = "SEARCH_HIGH",
439 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
440 [ SEARCH_LOW
] = "SEARCH_LOW",
441 [ SET_LIMITS
] = "SET_LIMITS",
442 [ PRE_FETCH
] = "PRE_FETCH",
443 /* READ_POSITION and PRE_FETCH use the same operation code */
444 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
445 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
446 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
447 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
448 [ COMPARE
] = "COMPARE",
449 [ COPY_VERIFY
] = "COPY_VERIFY",
450 [ WRITE_BUFFER
] = "WRITE_BUFFER",
451 [ READ_BUFFER
] = "READ_BUFFER",
452 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
453 [ READ_LONG
] = "READ_LONG",
454 [ WRITE_LONG
] = "WRITE_LONG",
455 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
456 [ WRITE_SAME
] = "WRITE_SAME",
457 [ READ_TOC
] = "READ_TOC",
458 [ LOG_SELECT
] = "LOG_SELECT",
459 [ LOG_SENSE
] = "LOG_SENSE",
460 [ MODE_SELECT_10
] = "MODE_SELECT_10",
461 [ RESERVE_10
] = "RESERVE_10",
462 [ RELEASE_10
] = "RELEASE_10",
463 [ MODE_SENSE_10
] = "MODE_SENSE_10",
464 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
465 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
466 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
467 [ READ_12
] = "READ_12",
468 [ WRITE_12
] = "WRITE_12",
469 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
470 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
471 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
472 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
473 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
474 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
475 [ WRITE_LONG_2
] = "WRITE_LONG_2",
477 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
478 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
479 [ READ_16
] = "READ_16",
480 [ WRITE_16
] = "WRITE_16",
481 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
482 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
483 [ REPORT_LUNS
] = "REPORT_LUNS",
484 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
485 [ SET_CD_SPEED
] = "SET_CD_SPEED",
489 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
494 void scsi_req_print(SCSIRequest
*req
)
499 fprintf(fp
, "[%s id=%d] %s",
500 req
->dev
->qdev
.parent_bus
->name
,
502 scsi_command_name(req
->cmd
.buf
[0]));
503 for (i
= 1; i
< req
->cmd
.len
; i
++) {
504 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
506 switch (req
->cmd
.mode
) {
508 fprintf(fp
, " - none\n");
510 case SCSI_XFER_FROM_DEV
:
511 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
513 case SCSI_XFER_TO_DEV
:
514 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
517 fprintf(fp
, " - Oops\n");
522 void scsi_req_complete(SCSIRequest
*req
)
524 assert(req
->status
!= -1);
525 scsi_req_dequeue(req
);
526 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,