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-common.h"
15 #include "qemu-error.h"
24 #define DPRINTF(fmt, ...) \
25 do { printf("scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
27 #define DPRINTF(fmt, ...) do {} while(0)
30 #define BADF(fmt, ...) \
31 do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
34 #include <sys/types.h>
38 #include "scsi-defs.h"
40 #define SCSI_SENSE_BUF_SIZE 96
42 #define SG_ERR_DRIVER_TIMEOUT 0x06
43 #define SG_ERR_DRIVER_SENSE 0x08
46 #define MAX_UINT ((unsigned int)-1)
49 typedef struct SCSIGenericState SCSIGenericState
;
51 typedef struct SCSIGenericReq
{
56 sg_io_hdr_t io_header
;
59 struct SCSIGenericState
65 static void scsi_free_request(SCSIRequest
*req
)
67 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
72 /* Helper function for command completion. */
73 static void scsi_command_complete(void *opaque
, int ret
)
76 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
79 if (r
->io_header
.driver_status
& SG_ERR_DRIVER_SENSE
)
80 r
->req
.sense_len
= r
->io_header
.sb_len_wr
;
85 status
= TASK_SET_FULL
;
88 status
= CHECK_CONDITION
;
89 scsi_req_build_sense(&r
->req
, SENSE_CODE(TARGET_FAILURE
));
92 status
= CHECK_CONDITION
;
93 scsi_req_build_sense(&r
->req
, SENSE_CODE(IO_ERROR
));
97 if (r
->io_header
.driver_status
& SG_ERR_DRIVER_TIMEOUT
) {
99 BADF("Driver Timeout\n");
100 } else if (r
->io_header
.status
) {
101 status
= r
->io_header
.status
;
102 } else if (r
->io_header
.driver_status
& SG_ERR_DRIVER_SENSE
) {
103 status
= CHECK_CONDITION
;
108 DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
109 r
, r
->req
.tag
, status
);
111 scsi_req_complete(&r
->req
, status
);
114 /* Cancel a pending data transfer. */
115 static void scsi_cancel_io(SCSIRequest
*req
)
117 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
119 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
121 bdrv_aio_cancel(r
->req
.aiocb
);
126 static int execute_command(BlockDriverState
*bdrv
,
127 SCSIGenericReq
*r
, int direction
,
128 BlockDriverCompletionFunc
*complete
)
130 r
->io_header
.interface_id
= 'S';
131 r
->io_header
.dxfer_direction
= direction
;
132 r
->io_header
.dxferp
= r
->buf
;
133 r
->io_header
.dxfer_len
= r
->buflen
;
134 r
->io_header
.cmdp
= r
->req
.cmd
.buf
;
135 r
->io_header
.cmd_len
= r
->req
.cmd
.len
;
136 r
->io_header
.mx_sb_len
= sizeof(r
->req
.sense
);
137 r
->io_header
.sbp
= r
->req
.sense
;
138 r
->io_header
.timeout
= MAX_UINT
;
139 r
->io_header
.usr_ptr
= r
;
140 r
->io_header
.flags
|= SG_FLAG_DIRECT_IO
;
142 r
->req
.aiocb
= bdrv_aio_ioctl(bdrv
, SG_IO
, &r
->io_header
, complete
, r
);
143 if (r
->req
.aiocb
== NULL
) {
144 BADF("execute_command: read failed !\n");
151 static void scsi_read_complete(void * opaque
, int ret
)
153 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
158 DPRINTF("IO error ret %d\n", ret
);
159 scsi_command_complete(r
, ret
);
162 len
= r
->io_header
.dxfer_len
- r
->io_header
.resid
;
163 DPRINTF("Data ready tag=0x%x len=%d\n", r
->req
.tag
, len
);
167 scsi_command_complete(r
, 0);
169 scsi_req_data(&r
->req
, len
);
173 /* Read more data from scsi device into buffer. */
174 static void scsi_read_data(SCSIRequest
*req
)
176 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
177 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, r
->req
.dev
);
180 DPRINTF("scsi_read_data 0x%x\n", req
->tag
);
182 scsi_command_complete(r
, 0);
186 ret
= execute_command(s
->bs
, r
, SG_DXFER_FROM_DEV
, scsi_read_complete
);
188 scsi_command_complete(r
, ret
);
193 static void scsi_write_complete(void * opaque
, int ret
)
195 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
196 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, r
->req
.dev
);
198 DPRINTF("scsi_write_complete() ret = %d\n", ret
);
201 DPRINTF("IO error\n");
202 scsi_command_complete(r
, ret
);
206 if (r
->req
.cmd
.buf
[0] == MODE_SELECT
&& r
->req
.cmd
.buf
[4] == 12 &&
207 s
->qdev
.type
== TYPE_TAPE
) {
208 s
->qdev
.blocksize
= (r
->buf
[9] << 16) | (r
->buf
[10] << 8) | r
->buf
[11];
209 DPRINTF("block size %d\n", s
->qdev
.blocksize
);
212 scsi_command_complete(r
, ret
);
215 /* Write data to a scsi device. Returns nonzero on failure.
216 The transfer may complete asynchronously. */
217 static void scsi_write_data(SCSIRequest
*req
)
219 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, req
->dev
);
220 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
223 DPRINTF("scsi_write_data 0x%x\n", req
->tag
);
226 scsi_req_data(&r
->req
, r
->len
);
230 ret
= execute_command(s
->bs
, r
, SG_DXFER_TO_DEV
, scsi_write_complete
);
232 scsi_command_complete(r
, ret
);
236 /* Return a pointer to the data buffer. */
237 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
239 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
244 static void scsi_req_fixup(SCSIRequest
*req
)
246 switch(req
->cmd
.buf
[0]) {
249 if (req
->dev
->type
== TYPE_TAPE
) {
250 /* force IMMED, otherwise qemu waits end of command */
251 req
->cmd
.buf
[1] = 0x01;
257 /* Execute a scsi command. Returns the length of the data expected by the
258 command. This will be Positive for data transfers from the device
259 (eg. disk reads), negative for transfers to the device (eg. disk writes),
260 and zero if the command does not transfer any data. */
262 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *cmd
)
264 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, req
->dev
);
265 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
268 scsi_req_fixup(&r
->req
);
270 DPRINTF("Command: lun=%d tag=0x%x len %zd data=0x%02x", lun
, tag
,
271 r
->req
.cmd
.xfer
, cmd
[0]);
276 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
277 printf(" 0x%02x", cmd
[i
]);
283 if (r
->req
.cmd
.xfer
== 0) {
288 ret
= execute_command(s
->bs
, r
, SG_DXFER_NONE
, scsi_command_complete
);
290 scsi_command_complete(r
, ret
);
296 if (r
->buflen
!= r
->req
.cmd
.xfer
) {
299 r
->buf
= g_malloc(r
->req
.cmd
.xfer
);
300 r
->buflen
= r
->req
.cmd
.xfer
;
303 memset(r
->buf
, 0, r
->buflen
);
304 r
->len
= r
->req
.cmd
.xfer
;
305 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
307 return -r
->req
.cmd
.xfer
;
309 return r
->req
.cmd
.xfer
;
313 static int get_blocksize(BlockDriverState
*bdrv
)
318 sg_io_hdr_t io_header
;
321 memset(cmd
, 0, sizeof(cmd
));
322 memset(buf
, 0, sizeof(buf
));
323 cmd
[0] = READ_CAPACITY_10
;
325 memset(&io_header
, 0, sizeof(io_header
));
326 io_header
.interface_id
= 'S';
327 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
328 io_header
.dxfer_len
= sizeof(buf
);
329 io_header
.dxferp
= buf
;
330 io_header
.cmdp
= cmd
;
331 io_header
.cmd_len
= sizeof(cmd
);
332 io_header
.mx_sb_len
= sizeof(sensebuf
);
333 io_header
.sbp
= sensebuf
;
334 io_header
.timeout
= 6000; /* XXX */
336 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
340 return (buf
[4] << 24) | (buf
[5] << 16) | (buf
[6] << 8) | buf
[7];
343 static int get_stream_blocksize(BlockDriverState
*bdrv
)
348 sg_io_hdr_t io_header
;
351 memset(cmd
, 0, sizeof(cmd
));
352 memset(buf
, 0, sizeof(buf
));
354 cmd
[4] = sizeof(buf
);
356 memset(&io_header
, 0, sizeof(io_header
));
357 io_header
.interface_id
= 'S';
358 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
359 io_header
.dxfer_len
= sizeof(buf
);
360 io_header
.dxferp
= buf
;
361 io_header
.cmdp
= cmd
;
362 io_header
.cmd_len
= sizeof(cmd
);
363 io_header
.mx_sb_len
= sizeof(sensebuf
);
364 io_header
.sbp
= sensebuf
;
365 io_header
.timeout
= 6000; /* XXX */
367 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
371 return (buf
[9] << 16) | (buf
[10] << 8) | buf
[11];
374 static void scsi_generic_reset(DeviceState
*dev
)
376 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
.qdev
, dev
);
378 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
381 static void scsi_destroy(SCSIDevice
*d
)
383 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, d
);
385 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
386 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
389 static int scsi_generic_initfn(SCSIDevice
*dev
)
391 SCSIGenericState
*s
= DO_UPCAST(SCSIGenericState
, qdev
, dev
);
393 struct sg_scsi_id scsiid
;
395 if (!s
->qdev
.conf
.bs
) {
396 error_report("scsi-generic: drive property not set");
399 s
->bs
= s
->qdev
.conf
.bs
;
401 /* check we are really using a /dev/sg* file */
402 if (!bdrv_is_sg(s
->bs
)) {
403 error_report("scsi-generic: not /dev/sg*");
407 if (bdrv_get_on_error(s
->bs
, 0) != BLOCK_ERR_STOP_ENOSPC
) {
408 error_report("Device doesn't support drive option werror");
411 if (bdrv_get_on_error(s
->bs
, 1) != BLOCK_ERR_REPORT
) {
412 error_report("Device doesn't support drive option rerror");
416 /* check we are using a driver managing SG_IO (version 3 and after */
417 if (bdrv_ioctl(s
->bs
, SG_GET_VERSION_NUM
, &sg_version
) < 0 ||
418 sg_version
< 30000) {
419 error_report("scsi-generic: scsi generic interface too old");
423 /* get LUN of the /dev/sg? */
424 if (bdrv_ioctl(s
->bs
, SG_GET_SCSI_ID
, &scsiid
)) {
425 error_report("scsi-generic: SG_GET_SCSI_ID ioctl failed");
429 /* define device state */
430 s
->qdev
.type
= scsiid
.scsi_type
;
431 DPRINTF("device type %d\n", s
->qdev
.type
);
432 if (s
->qdev
.type
== TYPE_TAPE
) {
433 s
->qdev
.blocksize
= get_stream_blocksize(s
->bs
);
434 if (s
->qdev
.blocksize
== -1)
435 s
->qdev
.blocksize
= 0;
437 s
->qdev
.blocksize
= get_blocksize(s
->bs
);
438 /* removable media returns 0 if not present */
439 if (s
->qdev
.blocksize
<= 0) {
440 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
)
441 s
->qdev
.blocksize
= 2048;
443 s
->qdev
.blocksize
= 512;
446 DPRINTF("block size %d\n", s
->qdev
.blocksize
);
450 static SCSIReqOps scsi_generic_req_ops
= {
451 .size
= sizeof(SCSIGenericReq
),
452 .free_req
= scsi_free_request
,
453 .send_command
= scsi_send_command
,
454 .read_data
= scsi_read_data
,
455 .write_data
= scsi_write_data
,
456 .cancel_io
= scsi_cancel_io
,
457 .get_buf
= scsi_get_buf
,
460 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
465 req
= scsi_req_alloc(&scsi_generic_req_ops
, d
, tag
, lun
, hba_private
);
469 static SCSIDeviceInfo scsi_generic_info
= {
470 .qdev
.name
= "scsi-generic",
471 .qdev
.desc
= "pass through generic scsi device (/dev/sg*)",
472 .qdev
.size
= sizeof(SCSIGenericState
),
473 .qdev
.reset
= scsi_generic_reset
,
474 .init
= scsi_generic_initfn
,
475 .destroy
= scsi_destroy
,
476 .alloc_req
= scsi_new_request
,
477 .qdev
.props
= (Property
[]) {
478 DEFINE_BLOCK_PROPERTIES(SCSIGenericState
, qdev
.conf
),
479 DEFINE_PROP_END_OF_LIST(),
483 static void scsi_generic_register_devices(void)
485 scsi_qdev_register(&scsi_generic_info
);
487 device_init(scsi_generic_register_devices
)
489 #endif /* __linux__ */