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 /* FIXME callers should check for failure, but don't */
87 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, DriveInfo
*dinfo
, int unit
)
92 driver
= bdrv_is_sg(dinfo
->bdrv
) ? "scsi-generic" : "scsi-disk";
93 dev
= qdev_create(&bus
->qbus
, driver
);
94 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
95 qdev_prop_set_drive(dev
, "drive", dinfo
);
96 if (qdev_init(dev
) < 0)
98 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
101 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
106 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
107 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
111 scsi_bus_legacy_add_drive(bus
, dinfo
, unit
);
115 void scsi_dev_clear_sense(SCSIDevice
*dev
)
117 memset(&dev
->sense
, 0, sizeof(dev
->sense
));
120 void scsi_dev_set_sense(SCSIDevice
*dev
, uint8_t key
)
122 dev
->sense
.key
= key
;
125 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
129 req
= qemu_mallocz(size
);
130 req
->bus
= scsi_bus_from_device(d
);
135 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
139 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
143 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
144 if (req
->tag
== tag
) {
151 void scsi_req_free(SCSIRequest
*req
)
153 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
157 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
159 switch (cmd
[0] >> 5) {
161 req
->cmd
.xfer
= cmd
[4];
163 /* length 0 means 256 blocks */
164 if (req
->cmd
.xfer
== 0)
169 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
173 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
177 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
185 case TEST_UNIT_READY
:
189 case WRITE_FILEMARKS
:
192 case ALLOW_MEDIUM_REMOVAL
:
195 case SYNCHRONIZE_CACHE
:
196 case LOCK_UNLOCK_CACHE
:
213 case READ_BLOCK_LIMITS
:
219 case SEND_VOLUME_TAG
:
229 case WRITE_VERIFY_12
:
231 case WRITE_VERIFY_16
:
232 req
->cmd
.xfer
*= req
->dev
->blocksize
;
237 case RECOVER_BUFFERED_DATA
:
240 req
->cmd
.xfer
*= req
->dev
->blocksize
;
243 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
245 case MAINTENANCE_OUT
:
247 if (req
->dev
->type
== TYPE_ROM
) {
248 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
249 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
256 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
259 /* stream commands */
262 case RECOVER_BUFFERED_DATA
:
265 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
266 if (cmd
[1] & 0x01) /* fixed */
267 req
->cmd
.xfer
*= req
->dev
->blocksize
;
274 /* generic commands */
276 return scsi_req_length(req
, cmd
);
281 static void scsi_req_xfer_mode(SCSIRequest
*req
)
283 switch (req
->cmd
.buf
[0]) {
288 case WRITE_VERIFY_12
:
290 case WRITE_VERIFY_16
:
294 case CHANGE_DEFINITION
:
298 case SEND_DIAGNOSTIC
:
301 case REASSIGN_BLOCKS
:
310 case SEARCH_EQUAL_12
:
314 case SEND_VOLUME_TAG
:
316 case PERSISTENT_RESERVE_OUT
:
317 case MAINTENANCE_OUT
:
318 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
322 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
324 req
->cmd
.mode
= SCSI_XFER_NONE
;
330 static uint64_t scsi_req_lba(SCSIRequest
*req
)
332 uint8_t *buf
= req
->cmd
.buf
;
335 switch (buf
[0] >> 5) {
337 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
338 (((uint64_t) buf
[1] & 0x1f) << 16);
342 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
343 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
346 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
347 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
348 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
349 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
352 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
353 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
362 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
366 if (req
->dev
->type
== TYPE_TAPE
) {
367 rc
= scsi_req_stream_length(req
, buf
);
369 rc
= scsi_req_length(req
, buf
);
374 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
375 scsi_req_xfer_mode(req
);
376 req
->cmd
.lba
= scsi_req_lba(req
);
380 static const char *scsi_command_name(uint8_t cmd
)
382 static const char *names
[] = {
383 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
384 [ REZERO_UNIT
] = "REZERO_UNIT",
385 /* REWIND and REZERO_UNIT use the same operation code */
386 [ REQUEST_SENSE
] = "REQUEST_SENSE",
387 [ FORMAT_UNIT
] = "FORMAT_UNIT",
388 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
389 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
390 [ READ_6
] = "READ_6",
391 [ WRITE_6
] = "WRITE_6",
392 [ SEEK_6
] = "SEEK_6",
393 [ READ_REVERSE
] = "READ_REVERSE",
394 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
396 [ INQUIRY
] = "INQUIRY",
397 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
398 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
399 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
400 [ MODE_SELECT
] = "MODE_SELECT",
401 [ RESERVE
] = "RESERVE",
402 [ RELEASE
] = "RELEASE",
405 [ MODE_SENSE
] = "MODE_SENSE",
406 [ START_STOP
] = "START_STOP",
407 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
408 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
409 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
411 [ SET_WINDOW
] = "SET_WINDOW",
412 [ READ_CAPACITY
] = "READ_CAPACITY",
413 [ READ_10
] = "READ_10",
414 [ WRITE_10
] = "WRITE_10",
415 [ SEEK_10
] = "SEEK_10",
416 [ WRITE_VERIFY
] = "WRITE_VERIFY",
417 [ VERIFY
] = "VERIFY",
418 [ SEARCH_HIGH
] = "SEARCH_HIGH",
419 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
420 [ SEARCH_LOW
] = "SEARCH_LOW",
421 [ SET_LIMITS
] = "SET_LIMITS",
422 [ PRE_FETCH
] = "PRE_FETCH",
423 /* READ_POSITION and PRE_FETCH use the same operation code */
424 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
425 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
426 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
427 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
428 [ COMPARE
] = "COMPARE",
429 [ COPY_VERIFY
] = "COPY_VERIFY",
430 [ WRITE_BUFFER
] = "WRITE_BUFFER",
431 [ READ_BUFFER
] = "READ_BUFFER",
432 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
433 [ READ_LONG
] = "READ_LONG",
434 [ WRITE_LONG
] = "WRITE_LONG",
435 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
436 [ WRITE_SAME
] = "WRITE_SAME",
437 [ READ_TOC
] = "READ_TOC",
438 [ LOG_SELECT
] = "LOG_SELECT",
439 [ LOG_SENSE
] = "LOG_SENSE",
440 [ MODE_SELECT_10
] = "MODE_SELECT_10",
441 [ RESERVE_10
] = "RESERVE_10",
442 [ RELEASE_10
] = "RELEASE_10",
443 [ MODE_SENSE_10
] = "MODE_SENSE_10",
444 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
445 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
446 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
447 [ READ_12
] = "READ_12",
448 [ WRITE_12
] = "WRITE_12",
449 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
450 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
451 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
452 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
453 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
454 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
455 [ WRITE_LONG_2
] = "WRITE_LONG_2",
457 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
458 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
459 [ READ_16
] = "READ_16",
460 [ WRITE_16
] = "WRITE_16",
461 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
462 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
463 [ REPORT_LUNS
] = "REPORT_LUNS",
464 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
465 [ SET_CD_SPEED
] = "SET_CD_SPEED",
469 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
474 void scsi_req_print(SCSIRequest
*req
)
479 fprintf(fp
, "[%s id=%d] %s",
480 req
->dev
->qdev
.parent_bus
->name
,
482 scsi_command_name(req
->cmd
.buf
[0]));
483 for (i
= 1; i
< req
->cmd
.len
; i
++) {
484 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
486 switch (req
->cmd
.mode
) {
488 fprintf(fp
, " - none\n");
490 case SCSI_XFER_FROM_DEV
:
491 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
493 case SCSI_XFER_TO_DEV
:
494 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
497 fprintf(fp
, " - Oops\n");
502 void scsi_req_complete(SCSIRequest
*req
)
504 assert(req
->status
!= -1);
505 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,