2 * Generic SCSI Device support
4 * Copyright (c) 2007 Bull S.A.S.
5 * Based on code by Paul Brook
6 * Based on code by Fabrice Bellard
8 * Written by Laurent Vivier <Laurent.Vivier@bull.net>
10 * This code is licensed under the LGPL.
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qemu/error-report.h"
17 #include "hw/scsi/scsi.h"
18 #include "sysemu/block-backend.h"
19 #include "sysemu/blockdev.h"
26 #define DPRINTF(fmt, ...) \
27 do { printf("scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
29 #define DPRINTF(fmt, ...) do {} while(0)
32 #define BADF(fmt, ...) \
33 do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
36 #include "block/scsi.h"
38 #define SG_ERR_DRIVER_TIMEOUT 0x06
39 #define SG_ERR_DRIVER_SENSE 0x08
41 #define SG_ERR_DID_OK 0x00
42 #define SG_ERR_DID_NO_CONNECT 0x01
43 #define SG_ERR_DID_BUS_BUSY 0x02
44 #define SG_ERR_DID_TIME_OUT 0x03
47 #define MAX_UINT ((unsigned int)-1)
50 typedef struct SCSIGenericReq
{
55 sg_io_hdr_t io_header
;
58 static void scsi_generic_save_request(QEMUFile
*f
, SCSIRequest
*req
)
60 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
62 qemu_put_sbe32s(f
, &r
->buflen
);
63 if (r
->buflen
&& r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
65 qemu_put_buffer(f
, r
->buf
, r
->req
.cmd
.xfer
);
69 static void scsi_generic_load_request(QEMUFile
*f
, SCSIRequest
*req
)
71 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
73 qemu_get_sbe32s(f
, &r
->buflen
);
74 if (r
->buflen
&& r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
76 qemu_get_buffer(f
, r
->buf
, r
->req
.cmd
.xfer
);
80 static void scsi_free_request(SCSIRequest
*req
)
82 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
87 /* Helper function for command completion. */
88 static void scsi_command_complete_noio(SCSIGenericReq
*r
, int ret
)
92 assert(r
->req
.aiocb
== NULL
);
94 if (r
->req
.io_canceled
) {
95 scsi_req_cancel_complete(&r
->req
);
98 if (r
->io_header
.driver_status
& SG_ERR_DRIVER_SENSE
) {
99 r
->req
.sense_len
= r
->io_header
.sb_len_wr
;
105 status
= TASK_SET_FULL
;
108 status
= CHECK_CONDITION
;
109 scsi_req_build_sense(&r
->req
, SENSE_CODE(TARGET_FAILURE
));
112 status
= CHECK_CONDITION
;
113 scsi_req_build_sense(&r
->req
, SENSE_CODE(IO_ERROR
));
117 if (r
->io_header
.host_status
== SG_ERR_DID_NO_CONNECT
||
118 r
->io_header
.host_status
== SG_ERR_DID_BUS_BUSY
||
119 r
->io_header
.host_status
== SG_ERR_DID_TIME_OUT
||
120 (r
->io_header
.driver_status
& SG_ERR_DRIVER_TIMEOUT
)) {
122 BADF("Driver Timeout\n");
123 } else if (r
->io_header
.host_status
) {
124 status
= CHECK_CONDITION
;
125 scsi_req_build_sense(&r
->req
, SENSE_CODE(I_T_NEXUS_LOSS
));
126 } else if (r
->io_header
.status
) {
127 status
= r
->io_header
.status
;
128 } else if (r
->io_header
.driver_status
& SG_ERR_DRIVER_SENSE
) {
129 status
= CHECK_CONDITION
;
134 DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
135 r
, r
->req
.tag
, status
);
137 scsi_req_complete(&r
->req
, status
);
139 scsi_req_unref(&r
->req
);
142 static void scsi_command_complete(void *opaque
, int ret
)
144 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
146 assert(r
->req
.aiocb
!= NULL
);
148 scsi_command_complete_noio(r
, ret
);
151 static int execute_command(BlockBackend
*blk
,
152 SCSIGenericReq
*r
, int direction
,
153 BlockCompletionFunc
*complete
)
155 r
->io_header
.interface_id
= 'S';
156 r
->io_header
.dxfer_direction
= direction
;
157 r
->io_header
.dxferp
= r
->buf
;
158 r
->io_header
.dxfer_len
= r
->buflen
;
159 r
->io_header
.cmdp
= r
->req
.cmd
.buf
;
160 r
->io_header
.cmd_len
= r
->req
.cmd
.len
;
161 r
->io_header
.mx_sb_len
= sizeof(r
->req
.sense
);
162 r
->io_header
.sbp
= r
->req
.sense
;
163 r
->io_header
.timeout
= MAX_UINT
;
164 r
->io_header
.usr_ptr
= r
;
165 r
->io_header
.flags
|= SG_FLAG_DIRECT_IO
;
167 r
->req
.aiocb
= blk_aio_ioctl(blk
, SG_IO
, &r
->io_header
, complete
, r
);
168 if (r
->req
.aiocb
== NULL
) {
175 static void scsi_read_complete(void * opaque
, int ret
)
177 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
178 SCSIDevice
*s
= r
->req
.dev
;
181 assert(r
->req
.aiocb
!= NULL
);
184 if (ret
|| r
->req
.io_canceled
) {
185 scsi_command_complete_noio(r
, ret
);
189 len
= r
->io_header
.dxfer_len
- r
->io_header
.resid
;
190 DPRINTF("Data ready tag=0x%x len=%d\n", r
->req
.tag
, len
);
194 scsi_command_complete_noio(r
, 0);
198 /* Snoop READ CAPACITY output to set the blocksize. */
199 if (r
->req
.cmd
.buf
[0] == READ_CAPACITY_10
&&
200 (ldl_be_p(&r
->buf
[0]) != 0xffffffffU
|| s
->max_lba
== 0)) {
201 s
->blocksize
= ldl_be_p(&r
->buf
[4]);
202 s
->max_lba
= ldl_be_p(&r
->buf
[0]) & 0xffffffffULL
;
203 } else if (r
->req
.cmd
.buf
[0] == SERVICE_ACTION_IN_16
&&
204 (r
->req
.cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
205 s
->blocksize
= ldl_be_p(&r
->buf
[8]);
206 s
->max_lba
= ldq_be_p(&r
->buf
[0]);
208 blk_set_guest_block_size(s
->conf
.blk
, s
->blocksize
);
210 /* Patch MODE SENSE device specific parameters if the BDS is opened
213 if ((s
->type
== TYPE_DISK
|| s
->type
== TYPE_TAPE
) &&
214 blk_is_read_only(s
->conf
.blk
) &&
215 (r
->req
.cmd
.buf
[0] == MODE_SENSE
||
216 r
->req
.cmd
.buf
[0] == MODE_SENSE_10
) &&
217 (r
->req
.cmd
.buf
[1] & 0x8) == 0) {
218 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
224 scsi_req_data(&r
->req
, len
);
225 scsi_req_unref(&r
->req
);
228 /* Read more data from scsi device into buffer. */
229 static void scsi_read_data(SCSIRequest
*req
)
231 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
232 SCSIDevice
*s
= r
->req
.dev
;
235 DPRINTF("scsi_read_data 0x%x\n", req
->tag
);
237 /* The request is used as the AIO opaque value, so add a ref. */
238 scsi_req_ref(&r
->req
);
240 scsi_command_complete_noio(r
, 0);
244 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_FROM_DEV
,
247 scsi_command_complete_noio(r
, ret
);
251 static void scsi_write_complete(void * opaque
, int ret
)
253 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
254 SCSIDevice
*s
= r
->req
.dev
;
256 DPRINTF("scsi_write_complete() ret = %d\n", ret
);
258 assert(r
->req
.aiocb
!= NULL
);
261 if (ret
|| r
->req
.io_canceled
) {
262 scsi_command_complete_noio(r
, ret
);
266 if (r
->req
.cmd
.buf
[0] == MODE_SELECT
&& r
->req
.cmd
.buf
[4] == 12 &&
267 s
->type
== TYPE_TAPE
) {
268 s
->blocksize
= (r
->buf
[9] << 16) | (r
->buf
[10] << 8) | r
->buf
[11];
269 DPRINTF("block size %d\n", s
->blocksize
);
272 scsi_command_complete_noio(r
, ret
);
275 /* Write data to a scsi device. Returns nonzero on failure.
276 The transfer may complete asynchronously. */
277 static void scsi_write_data(SCSIRequest
*req
)
279 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
280 SCSIDevice
*s
= r
->req
.dev
;
283 DPRINTF("scsi_write_data 0x%x\n", req
->tag
);
286 scsi_req_data(&r
->req
, r
->len
);
290 /* The request is used as the AIO opaque value, so add a ref. */
291 scsi_req_ref(&r
->req
);
292 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_TO_DEV
, scsi_write_complete
);
294 scsi_command_complete_noio(r
, ret
);
298 /* Return a pointer to the data buffer. */
299 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
301 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
306 /* Execute a scsi command. Returns the length of the data expected by the
307 command. This will be Positive for data transfers from the device
308 (eg. disk reads), negative for transfers to the device (eg. disk writes),
309 and zero if the command does not transfer any data. */
311 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *cmd
)
313 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
314 SCSIDevice
*s
= r
->req
.dev
;
320 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
321 printf(" 0x%02x", cmd
[i
]);
327 if (r
->req
.cmd
.xfer
== 0) {
331 /* The request is used as the AIO opaque value, so add a ref. */
332 scsi_req_ref(&r
->req
);
333 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_NONE
,
334 scsi_command_complete
);
336 scsi_command_complete_noio(r
, ret
);
342 if (r
->buflen
!= r
->req
.cmd
.xfer
) {
344 r
->buf
= g_malloc(r
->req
.cmd
.xfer
);
345 r
->buflen
= r
->req
.cmd
.xfer
;
348 memset(r
->buf
, 0, r
->buflen
);
349 r
->len
= r
->req
.cmd
.xfer
;
350 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
352 return -r
->req
.cmd
.xfer
;
354 return r
->req
.cmd
.xfer
;
358 static int read_naa_id(const uint8_t *p
, uint64_t *p_wwn
)
362 if ((p
[1] & 0xF) == 3) {
363 /* NAA designator type */
367 *p_wwn
= ldq_be_p(p
+ 4);
371 if ((p
[1] & 0xF) == 8) {
372 /* SCSI name string designator type */
373 if (p
[3] < 20 || memcmp(&p
[4], "naa.", 4)) {
376 if (p
[3] > 20 && p
[24] != ',') {
380 for (i
= 8; i
< 24; i
++) {
381 char c
= toupper(p
[i
]);
382 c
-= (c
>= '0' && c
<= '9' ? '0' : 'A' - 10);
383 *p_wwn
= (*p_wwn
<< 4) | c
;
391 void scsi_generic_read_device_identification(SCSIDevice
*s
)
396 sg_io_hdr_t io_header
;
400 memset(cmd
, 0, sizeof(cmd
));
401 memset(buf
, 0, sizeof(buf
));
405 cmd
[4] = sizeof(buf
);
407 memset(&io_header
, 0, sizeof(io_header
));
408 io_header
.interface_id
= 'S';
409 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
410 io_header
.dxfer_len
= sizeof(buf
);
411 io_header
.dxferp
= buf
;
412 io_header
.cmdp
= cmd
;
413 io_header
.cmd_len
= sizeof(cmd
);
414 io_header
.mx_sb_len
= sizeof(sensebuf
);
415 io_header
.sbp
= sensebuf
;
416 io_header
.timeout
= 6000; /* XXX */
418 ret
= blk_ioctl(s
->conf
.blk
, SG_IO
, &io_header
);
419 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
423 len
= MIN((buf
[2] << 8) | buf
[3], sizeof(buf
) - 4);
424 for (i
= 0; i
+ 3 <= len
; ) {
425 const uint8_t *p
= &buf
[i
+ 4];
428 if (i
+ (p
[3] + 4) > len
) {
432 if ((p
[1] & 0x10) == 0) {
433 /* Associated with the logical unit */
434 if (read_naa_id(p
, &wwn
) == 0) {
437 } else if ((p
[1] & 0x10) == 0x10) {
438 /* Associated with the target port */
439 if (read_naa_id(p
, &wwn
) == 0) {
448 static int get_stream_blocksize(BlockBackend
*blk
)
453 sg_io_hdr_t io_header
;
456 memset(cmd
, 0, sizeof(cmd
));
457 memset(buf
, 0, sizeof(buf
));
459 cmd
[4] = sizeof(buf
);
461 memset(&io_header
, 0, sizeof(io_header
));
462 io_header
.interface_id
= 'S';
463 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
464 io_header
.dxfer_len
= sizeof(buf
);
465 io_header
.dxferp
= buf
;
466 io_header
.cmdp
= cmd
;
467 io_header
.cmd_len
= sizeof(cmd
);
468 io_header
.mx_sb_len
= sizeof(sensebuf
);
469 io_header
.sbp
= sensebuf
;
470 io_header
.timeout
= 6000; /* XXX */
472 ret
= blk_ioctl(blk
, SG_IO
, &io_header
);
473 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
476 return (buf
[9] << 16) | (buf
[10] << 8) | buf
[11];
479 static void scsi_generic_reset(DeviceState
*dev
)
481 SCSIDevice
*s
= SCSI_DEVICE(dev
);
483 scsi_device_purge_requests(s
, SENSE_CODE(RESET
));
486 static void scsi_generic_realize(SCSIDevice
*s
, Error
**errp
)
490 struct sg_scsi_id scsiid
;
493 error_setg(errp
, "drive property not set");
497 if (blk_get_on_error(s
->conf
.blk
, 0) != BLOCKDEV_ON_ERROR_ENOSPC
) {
498 error_setg(errp
, "Device doesn't support drive option werror");
501 if (blk_get_on_error(s
->conf
.blk
, 1) != BLOCKDEV_ON_ERROR_REPORT
) {
502 error_setg(errp
, "Device doesn't support drive option rerror");
506 /* check we are using a driver managing SG_IO (version 3 and after */
507 rc
= blk_ioctl(s
->conf
.blk
, SG_GET_VERSION_NUM
, &sg_version
);
509 error_setg(errp
, "cannot get SG_IO version number: %s. "
510 "Is this a SCSI device?",
514 if (sg_version
< 30000) {
515 error_setg(errp
, "scsi generic interface too old");
519 /* get LUN of the /dev/sg? */
520 if (blk_ioctl(s
->conf
.blk
, SG_GET_SCSI_ID
, &scsiid
)) {
521 error_setg(errp
, "SG_GET_SCSI_ID ioctl failed");
525 /* define device state */
526 s
->type
= scsiid
.scsi_type
;
527 DPRINTF("device type %d\n", s
->type
);
531 s
->blocksize
= get_stream_blocksize(s
->conf
.blk
);
532 if (s
->blocksize
== -1) {
537 /* Make a guess for block devices, we'll fix it when the guest sends.
538 * READ CAPACITY. If they don't, they likely would assume these sizes
539 * anyway. (TODO: they could also send MODE SENSE).
550 DPRINTF("block size %d\n", s
->blocksize
);
552 scsi_generic_read_device_identification(s
);
555 const SCSIReqOps scsi_generic_req_ops
= {
556 .size
= sizeof(SCSIGenericReq
),
557 .free_req
= scsi_free_request
,
558 .send_command
= scsi_send_command
,
559 .read_data
= scsi_read_data
,
560 .write_data
= scsi_write_data
,
561 .get_buf
= scsi_get_buf
,
562 .load_request
= scsi_generic_load_request
,
563 .save_request
= scsi_generic_save_request
,
566 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
567 uint8_t *buf
, void *hba_private
)
571 req
= scsi_req_alloc(&scsi_generic_req_ops
, d
, tag
, lun
, hba_private
);
575 static Property scsi_generic_properties
[] = {
576 DEFINE_PROP_DRIVE("drive", SCSIDevice
, conf
.blk
),
577 DEFINE_PROP_END_OF_LIST(),
580 static int scsi_generic_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
581 uint8_t *buf
, void *hba_private
)
583 return scsi_bus_parse_cdb(dev
, cmd
, buf
, hba_private
);
586 static void scsi_generic_class_initfn(ObjectClass
*klass
, void *data
)
588 DeviceClass
*dc
= DEVICE_CLASS(klass
);
589 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
591 sc
->realize
= scsi_generic_realize
;
592 sc
->alloc_req
= scsi_new_request
;
593 sc
->parse_cdb
= scsi_generic_parse_cdb
;
594 dc
->fw_name
= "disk";
595 dc
->desc
= "pass through generic scsi device (/dev/sg*)";
596 dc
->reset
= scsi_generic_reset
;
597 dc
->props
= scsi_generic_properties
;
598 dc
->vmsd
= &vmstate_scsi_device
;
601 static const TypeInfo scsi_generic_info
= {
602 .name
= "scsi-generic",
603 .parent
= TYPE_SCSI_DEVICE
,
604 .instance_size
= sizeof(SCSIDevice
),
605 .class_init
= scsi_generic_class_initfn
,
608 static void scsi_generic_register_types(void)
610 type_register_static(&scsi_generic_info
);
613 type_init(scsi_generic_register_types
)
615 #endif /* __linux__ */