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 licenced under the LGPL.
14 #include "qemu-common.h"
16 #include "scsi-disk.h"
20 SCSIDevice
*scsi_generic_init(BlockDriverState
*bdrv
, int tcq
,
21 scsi_completionfn completion
, void *opaque
)
31 #define DPRINTF(fmt, ...) \
32 do { printf("scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) do {} while(0)
37 #define BADF(fmt, ...) \
38 do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
41 #include <sys/types.h>
45 #include <scsi/scsi.h>
48 #define REPORT_DENSITY_SUPPORT 0x44
49 #define LOAD_UNLOAD 0xa6
50 #define SET_CD_SPEED 0xbb
53 #define SCSI_CMD_BUF_SIZE 16
54 #define SCSI_SENSE_BUF_SIZE 96
56 #define SG_ERR_DRIVER_TIMEOUT 0x06
57 #define SG_ERR_DRIVER_SENSE 0x08
60 #define MAX_UINT ((unsigned int)-1)
63 typedef struct SCSIRequest
{
64 BlockDriverAIOCB
*aiocb
;
65 struct SCSIRequest
*next
;
68 uint8_t cmd
[SCSI_CMD_BUF_SIZE
];
73 sg_io_hdr_t io_header
;
76 struct SCSIDeviceState
78 SCSIRequest
*requests
;
79 BlockDriverState
*bdrv
;
83 scsi_completionfn completion
;
86 uint8_t sensebuf
[SCSI_SENSE_BUF_SIZE
];
90 /* Global pool of SCSIRequest structures. */
91 static SCSIRequest
*free_requests
= NULL
;
93 static SCSIRequest
*scsi_new_request(SCSIDeviceState
*s
, uint32_t tag
)
99 free_requests
= r
->next
;
101 r
= qemu_malloc(sizeof(SCSIRequest
));
107 memset(r
->cmd
, 0, sizeof(r
->cmd
));
108 memset(&r
->io_header
, 0, sizeof(r
->io_header
));
115 r
->next
= s
->requests
;
120 static void scsi_remove_request(SCSIRequest
*r
)
123 SCSIDeviceState
*s
= r
->dev
;
125 if (s
->requests
== r
) {
126 s
->requests
= r
->next
;
129 while (last
&& last
->next
!= r
)
132 last
->next
= r
->next
;
134 BADF("Orphaned request\n");
137 r
->next
= free_requests
;
141 static SCSIRequest
*scsi_find_request(SCSIDeviceState
*s
, uint32_t tag
)
146 while (r
&& r
->tag
!= tag
)
152 /* Helper function for command completion. */
153 static void scsi_command_complete(void *opaque
, int ret
)
155 SCSIRequest
*r
= (SCSIRequest
*)opaque
;
156 SCSIDeviceState
*s
= r
->dev
;
160 s
->driver_status
= r
->io_header
.driver_status
;
161 if (s
->driver_status
& SG_ERR_DRIVER_SENSE
)
162 s
->senselen
= r
->io_header
.sb_len_wr
;
167 if (s
->driver_status
& SG_ERR_DRIVER_TIMEOUT
) {
169 BADF("Driver Timeout\n");
170 } else if (r
->io_header
.status
)
171 status
= r
->io_header
.status
;
172 else if (s
->driver_status
& SG_ERR_DRIVER_SENSE
)
173 status
= CHECK_CONDITION
<< 1;
177 DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
180 scsi_remove_request(r
);
181 s
->completion(s
->opaque
, SCSI_REASON_DONE
, tag
, status
);
184 /* Cancel a pending data transfer. */
185 static void scsi_cancel_io(SCSIDevice
*d
, uint32_t tag
)
187 DPRINTF("scsi_cancel_io 0x%x\n", tag
);
188 SCSIDeviceState
*s
= d
->state
;
190 DPRINTF("Cancel tag=0x%x\n", tag
);
191 r
= scsi_find_request(s
, tag
);
194 bdrv_aio_cancel(r
->aiocb
);
196 scsi_remove_request(r
);
200 static int execute_command(BlockDriverState
*bdrv
,
201 SCSIRequest
*r
, int direction
,
202 BlockDriverCompletionFunc
*complete
)
204 r
->io_header
.interface_id
= 'S';
205 r
->io_header
.dxfer_direction
= direction
;
206 r
->io_header
.dxferp
= r
->buf
;
207 r
->io_header
.dxfer_len
= r
->buflen
;
208 r
->io_header
.cmdp
= r
->cmd
;
209 r
->io_header
.cmd_len
= r
->cmdlen
;
210 r
->io_header
.mx_sb_len
= sizeof(r
->dev
->sensebuf
);
211 r
->io_header
.sbp
= r
->dev
->sensebuf
;
212 r
->io_header
.timeout
= MAX_UINT
;
213 r
->io_header
.usr_ptr
= r
;
214 r
->io_header
.flags
|= SG_FLAG_DIRECT_IO
;
216 r
->aiocb
= bdrv_aio_ioctl(bdrv
, SG_IO
, &r
->io_header
, complete
, r
);
217 if (r
->aiocb
== NULL
) {
218 BADF("execute_command: read failed !\n");
225 static void scsi_read_complete(void * opaque
, int ret
)
227 SCSIRequest
*r
= (SCSIRequest
*)opaque
;
228 SCSIDeviceState
*s
= r
->dev
;
232 DPRINTF("IO error\n");
233 scsi_command_complete(r
, ret
);
236 len
= r
->io_header
.dxfer_len
- r
->io_header
.resid
;
237 DPRINTF("Data ready tag=0x%x len=%d\n", r
->tag
, len
);
240 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, len
);
242 scsi_command_complete(r
, 0);
245 /* Read more data from scsi device into buffer. */
246 static void scsi_read_data(SCSIDevice
*d
, uint32_t tag
)
248 SCSIDeviceState
*s
= d
->state
;
252 DPRINTF("scsi_read_data 0x%x\n", tag
);
253 r
= scsi_find_request(s
, tag
);
255 BADF("Bad read tag 0x%x\n", tag
);
256 /* ??? This is the wrong error. */
257 scsi_command_complete(r
, -EINVAL
);
262 scsi_command_complete(r
, 0);
266 if (r
->cmd
[0] == REQUEST_SENSE
&& s
->driver_status
& SG_ERR_DRIVER_SENSE
)
268 s
->senselen
= MIN(r
->len
, s
->senselen
);
269 memcpy(r
->buf
, s
->sensebuf
, s
->senselen
);
270 r
->io_header
.driver_status
= 0;
271 r
->io_header
.status
= 0;
272 r
->io_header
.dxfer_len
= s
->senselen
;
274 DPRINTF("Data ready tag=0x%x len=%d\n", r
->tag
, s
->senselen
);
275 DPRINTF("Sense: %d %d %d %d %d %d %d %d\n",
276 r
->buf
[0], r
->buf
[1], r
->buf
[2], r
->buf
[3],
277 r
->buf
[4], r
->buf
[5], r
->buf
[6], r
->buf
[7]);
278 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, s
->senselen
);
282 ret
= execute_command(s
->bdrv
, r
, SG_DXFER_FROM_DEV
, scsi_read_complete
);
284 scsi_command_complete(r
, -EINVAL
);
289 static void scsi_write_complete(void * opaque
, int ret
)
291 SCSIRequest
*r
= (SCSIRequest
*)opaque
;
293 DPRINTF("scsi_write_complete() ret = %d\n", ret
);
295 DPRINTF("IO error\n");
296 scsi_command_complete(r
, ret
);
300 if (r
->cmd
[0] == MODE_SELECT
&& r
->cmd
[4] == 12 &&
301 r
->dev
->type
== TYPE_TAPE
) {
302 r
->dev
->blocksize
= (r
->buf
[9] << 16) | (r
->buf
[10] << 8) | r
->buf
[11];
303 DPRINTF("block size %d\n", r
->dev
->blocksize
);
306 scsi_command_complete(r
, ret
);
309 /* Write data to a scsi device. Returns nonzero on failure.
310 The transfer may complete asynchronously. */
311 static int scsi_write_data(SCSIDevice
*d
, uint32_t tag
)
313 SCSIDeviceState
*s
= d
->state
;
317 DPRINTF("scsi_write_data 0x%x\n", tag
);
318 r
= scsi_find_request(s
, tag
);
320 BADF("Bad write tag 0x%x\n", tag
);
321 /* ??? This is the wrong error. */
322 scsi_command_complete(r
, -EINVAL
);
328 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, r
->len
);
332 ret
= execute_command(s
->bdrv
, r
, SG_DXFER_TO_DEV
, scsi_write_complete
);
334 scsi_command_complete(r
, -EINVAL
);
341 /* Return a pointer to the data buffer. */
342 static uint8_t *scsi_get_buf(SCSIDevice
*d
, uint32_t tag
)
344 SCSIDeviceState
*s
= d
->state
;
346 r
= scsi_find_request(s
, tag
);
348 BADF("Bad buffer tag 0x%x\n", tag
);
354 static int scsi_length(uint8_t *cmd
, int blocksize
, int *cmdlen
, uint32_t *len
)
356 switch (cmd
[0] >> 5) {
360 /* length 0 means 256 blocks */
366 *len
= cmd
[8] | (cmd
[7] << 8);
370 *len
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
374 *len
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
382 case TEST_UNIT_READY
:
386 case WRITE_FILEMARKS
:
389 case ALLOW_MEDIUM_REMOVAL
:
392 case SYNCHRONIZE_CACHE
:
393 case LOCK_UNLOCK_CACHE
:
410 case READ_BLOCK_LIMITS
:
416 case SEND_VOLUME_TAG
:
423 cmd
[1] &= ~0x08; /* disable FUA */
427 case WRITE_VERIFY_12
:
431 cmd
[1] &= ~0x08; /* disable FUA */
434 case RECOVER_BUFFERED_DATA
:
439 *len
= cmd
[4] | (cmd
[3] << 8);
445 static int scsi_stream_length(uint8_t *cmd
, int blocksize
, int *cmdlen
, uint32_t *len
)
448 /* stream commands */
451 case RECOVER_BUFFERED_DATA
:
454 *len
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
455 if (cmd
[1] & 0x01) /* fixed */
462 cmd
[1] = 0x01; /* force IMMED, otherwise qemu waits end of command */
464 /* generic commands */
466 return scsi_length(cmd
, blocksize
, cmdlen
, len
);
471 static int is_write(int command
)
477 case CHANGE_DEFINITION
:
481 case SEND_DIAGNOSTIC
:
484 case REASSIGN_BLOCKS
:
496 case SEARCH_EQUAL_12
:
499 case WRITE_VERIFY_12
:
502 case SEND_VOLUME_TAG
:
509 /* Execute a scsi command. Returns the length of the data expected by the
510 command. This will be Positive for data transfers from the device
511 (eg. disk reads), negative for transfers to the device (eg. disk writes),
512 and zero if the command does not transfer any data. */
514 static int32_t scsi_send_command(SCSIDevice
*d
, uint32_t tag
,
515 uint8_t *cmd
, int lun
)
517 SCSIDeviceState
*s
= d
->state
;
523 if (s
->type
== TYPE_TAPE
) {
524 if (scsi_stream_length(cmd
, s
->blocksize
, &cmdlen
, &len
) == -1) {
525 BADF("Unsupported command length, command %x\n", cmd
[0]);
529 if (scsi_length(cmd
, s
->blocksize
, &cmdlen
, &len
) == -1) {
530 BADF("Unsupported command length, command %x\n", cmd
[0]);
535 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x len %d\n", lun
, tag
,
538 if (cmd
[0] != REQUEST_SENSE
&&
539 (lun
!= s
->lun
|| (cmd
[1] >> 5) != s
->lun
)) {
540 DPRINTF("Unimplemented LUN %d\n", lun
? lun
: cmd
[1] >> 5);
542 s
->sensebuf
[0] = 0x70;
543 s
->sensebuf
[1] = 0x00;
544 s
->sensebuf
[2] = ILLEGAL_REQUEST
;
545 s
->sensebuf
[3] = 0x00;
546 s
->sensebuf
[4] = 0x00;
547 s
->sensebuf
[5] = 0x00;
548 s
->sensebuf
[6] = 0x00;
550 s
->driver_status
= SG_ERR_DRIVER_SENSE
;
551 s
->completion(s
->opaque
, SCSI_REASON_DONE
, tag
, CHECK_CONDITION
<< 1);
555 r
= scsi_find_request(s
, tag
);
557 BADF("Tag 0x%x already in use %p\n", tag
, r
);
558 scsi_cancel_io(d
, tag
);
560 r
= scsi_new_request(s
, tag
);
562 memcpy(r
->cmd
, cmd
, cmdlen
);
570 ret
= execute_command(s
->bdrv
, r
, SG_DXFER_NONE
, scsi_command_complete
);
572 scsi_command_complete(r
, -EINVAL
);
578 if (r
->buflen
!= len
) {
581 r
->buf
= qemu_malloc(len
);
585 memset(r
->buf
, 0, r
->buflen
);
587 if (is_write(cmd
[0])) {
595 static int get_blocksize(BlockDriverState
*bdrv
)
600 sg_io_hdr_t io_header
;
603 memset(cmd
, 0, sizeof(cmd
));
604 memset(buf
, 0, sizeof(buf
));
605 cmd
[0] = READ_CAPACITY
;
607 memset(&io_header
, 0, sizeof(io_header
));
608 io_header
.interface_id
= 'S';
609 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
610 io_header
.dxfer_len
= sizeof(buf
);
611 io_header
.dxferp
= buf
;
612 io_header
.cmdp
= cmd
;
613 io_header
.cmd_len
= sizeof(cmd
);
614 io_header
.mx_sb_len
= sizeof(sensebuf
);
615 io_header
.sbp
= sensebuf
;
616 io_header
.timeout
= 6000; /* XXX */
618 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
622 return (buf
[4] << 24) | (buf
[5] << 16) | (buf
[6] << 8) | buf
[7];
625 static int get_stream_blocksize(BlockDriverState
*bdrv
)
630 sg_io_hdr_t io_header
;
633 memset(cmd
, 0, sizeof(cmd
));
634 memset(buf
, 0, sizeof(buf
));
636 cmd
[4] = sizeof(buf
);
638 memset(&io_header
, 0, sizeof(io_header
));
639 io_header
.interface_id
= 'S';
640 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
641 io_header
.dxfer_len
= sizeof(buf
);
642 io_header
.dxferp
= buf
;
643 io_header
.cmdp
= cmd
;
644 io_header
.cmd_len
= sizeof(cmd
);
645 io_header
.mx_sb_len
= sizeof(sensebuf
);
646 io_header
.sbp
= sensebuf
;
647 io_header
.timeout
= 6000; /* XXX */
649 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
653 return (buf
[9] << 16) | (buf
[10] << 8) | buf
[11];
656 static void scsi_destroy(SCSIDevice
*d
)
660 r
= d
->state
->requests
;
678 SCSIDevice
*scsi_generic_init(BlockDriverState
*bdrv
, int tcq
,
679 scsi_completionfn completion
, void *opaque
)
684 struct sg_scsi_id scsiid
;
686 /* check we are really using a /dev/sg* file */
688 if (!bdrv_is_sg(bdrv
))
691 /* check we are using a driver managing SG_IO (version 3 and after */
693 if (bdrv_ioctl(bdrv
, SG_GET_VERSION_NUM
, &sg_version
) < 0 ||
697 /* get LUN of the /dev/sg? */
699 if (bdrv_ioctl(bdrv
, SG_GET_SCSI_ID
, &scsiid
))
702 /* define device state */
704 s
= (SCSIDeviceState
*)qemu_mallocz(sizeof(SCSIDeviceState
));
707 s
->completion
= completion
;
710 DPRINTF("LUN %d\n", s
->lun
);
711 s
->type
= scsiid
.scsi_type
;
712 DPRINTF("device type %d\n", s
->type
);
713 if (s
->type
== TYPE_TAPE
) {
714 s
->blocksize
= get_stream_blocksize(s
->bdrv
);
715 if (s
->blocksize
== -1)
718 s
->blocksize
= get_blocksize(s
->bdrv
);
719 /* removable media returns 0 if not present */
720 if (s
->blocksize
<= 0) {
721 if (s
->type
== TYPE_ROM
|| s
->type
== TYPE_WORM
)
727 DPRINTF("block size %d\n", s
->blocksize
);
728 s
->driver_status
= 0;
729 memset(s
->sensebuf
, 0, sizeof(s
->sensebuf
));
731 /* define function to manage device */
733 d
= (SCSIDevice
*)qemu_mallocz(sizeof(SCSIDevice
));
735 d
->destroy
= scsi_destroy
;
736 d
->send_command
= scsi_send_command
;
737 d
->read_data
= scsi_read_data
;
738 d
->write_data
= scsi_write_data
;
739 d
->cancel_io
= scsi_cancel_io
;
740 d
->get_buf
= scsi_get_buf
;
744 #endif /* __linux__ */