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 "qapi/error.h"
16 #include "qemu/ctype.h"
17 #include "qemu/error-report.h"
18 #include "qemu/module.h"
19 #include "hw/scsi/scsi.h"
20 #include "migration/qemu-file-types.h"
21 #include "hw/qdev-properties.h"
22 #include "hw/qdev-properties-system.h"
23 #include "hw/scsi/emulation.h"
24 #include "sysemu/block-backend.h"
30 #include "scsi/constants.h"
33 #define MAX_UINT ((unsigned int)-1)
36 typedef struct SCSIGenericReq
{
41 sg_io_hdr_t io_header
;
44 static void scsi_generic_save_request(QEMUFile
*f
, SCSIRequest
*req
)
46 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
48 qemu_put_sbe32s(f
, &r
->buflen
);
49 if (r
->buflen
&& r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
51 qemu_put_buffer(f
, r
->buf
, r
->req
.cmd
.xfer
);
55 static void scsi_generic_load_request(QEMUFile
*f
, SCSIRequest
*req
)
57 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
59 qemu_get_sbe32s(f
, &r
->buflen
);
60 if (r
->buflen
&& r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
62 qemu_get_buffer(f
, r
->buf
, r
->req
.cmd
.xfer
);
66 static void scsi_free_request(SCSIRequest
*req
)
68 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
73 /* Helper function for command completion. */
74 static void scsi_command_complete_noio(SCSIGenericReq
*r
, int ret
)
78 sg_io_hdr_t
*io_hdr
= &r
->io_header
;
80 assert(r
->req
.aiocb
== NULL
);
82 if (r
->req
.io_canceled
) {
83 scsi_req_cancel_complete(&r
->req
);
87 status
= scsi_sense_from_errno(-ret
, &sense
);
88 if (status
== CHECK_CONDITION
) {
89 scsi_req_build_sense(&r
->req
, sense
);
91 } else if (io_hdr
->host_status
!= SCSI_HOST_OK
) {
92 scsi_req_complete_failed(&r
->req
, io_hdr
->host_status
);
94 } else if (io_hdr
->driver_status
& SG_ERR_DRIVER_TIMEOUT
) {
97 status
= io_hdr
->status
;
98 if (io_hdr
->driver_status
& SG_ERR_DRIVER_SENSE
) {
99 r
->req
.sense_len
= io_hdr
->sb_len_wr
;
102 trace_scsi_generic_command_complete_noio(r
, r
->req
.tag
, status
);
104 scsi_req_complete(&r
->req
, status
);
106 scsi_req_unref(&r
->req
);
109 static void scsi_command_complete(void *opaque
, int ret
)
111 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
112 SCSIDevice
*s
= r
->req
.dev
;
114 assert(r
->req
.aiocb
!= NULL
);
117 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
118 scsi_command_complete_noio(r
, ret
);
119 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
122 static int execute_command(BlockBackend
*blk
,
123 SCSIGenericReq
*r
, int direction
,
124 BlockCompletionFunc
*complete
)
126 SCSIDevice
*s
= r
->req
.dev
;
128 r
->io_header
.interface_id
= 'S';
129 r
->io_header
.dxfer_direction
= direction
;
130 r
->io_header
.dxferp
= r
->buf
;
131 r
->io_header
.dxfer_len
= r
->buflen
;
132 r
->io_header
.cmdp
= r
->req
.cmd
.buf
;
133 r
->io_header
.cmd_len
= r
->req
.cmd
.len
;
134 r
->io_header
.mx_sb_len
= sizeof(r
->req
.sense
);
135 r
->io_header
.sbp
= r
->req
.sense
;
136 r
->io_header
.timeout
= s
->io_timeout
* 1000;
137 r
->io_header
.usr_ptr
= r
;
138 r
->io_header
.flags
|= SG_FLAG_DIRECT_IO
;
140 trace_scsi_generic_aio_sgio_command(r
->req
.tag
, r
->req
.cmd
.buf
[0],
141 r
->io_header
.timeout
);
142 r
->req
.aiocb
= blk_aio_ioctl(blk
, SG_IO
, &r
->io_header
, complete
, r
);
143 if (r
->req
.aiocb
== NULL
) {
150 static uint64_t calculate_max_transfer(SCSIDevice
*s
)
152 uint64_t max_transfer
= blk_get_max_hw_transfer(s
->conf
.blk
);
153 uint32_t max_iov
= blk_get_max_hw_iov(s
->conf
.blk
);
155 assert(max_transfer
);
156 max_transfer
= MIN_NON_ZERO(max_transfer
,
157 max_iov
* qemu_real_host_page_size());
159 return max_transfer
/ s
->blocksize
;
162 static int scsi_handle_inquiry_reply(SCSIGenericReq
*r
, SCSIDevice
*s
, int len
)
164 uint8_t page
, page_idx
;
167 * EVPD set to zero returns the standard INQUIRY data.
169 * Check if scsi_version is unset (-1) to avoid re-defining it
170 * each time an INQUIRY with standard data is received.
171 * scsi_version is initialized with -1 in scsi_generic_reset
172 * and scsi_disk_reset, making sure that we'll set the
173 * scsi_version after a reset. If the version field of the
174 * INQUIRY response somehow changes after a guest reboot,
175 * we'll be able to keep track of it.
177 * On SCSI-2 and older, first 3 bits of byte 2 is the
178 * ANSI-approved version, while on later versions the
179 * whole byte 2 contains the version. Check if we're dealing
180 * with a newer version and, in that case, assign the
183 if (s
->scsi_version
== -1 && !(r
->req
.cmd
.buf
[1] & 0x01)) {
184 s
->scsi_version
= r
->buf
[2] & 0x07;
185 if (s
->scsi_version
> 2) {
186 s
->scsi_version
= r
->buf
[2];
190 if ((s
->type
== TYPE_DISK
|| s
->type
== TYPE_ZBC
) &&
191 (r
->req
.cmd
.buf
[1] & 0x01)) {
192 page
= r
->req
.cmd
.buf
[2];
194 uint64_t max_transfer
= calculate_max_transfer(s
);
195 stl_be_p(&r
->buf
[8], max_transfer
);
196 /* Also take care of the opt xfer len. */
197 stl_be_p(&r
->buf
[12],
198 MIN_NON_ZERO(max_transfer
, ldl_be_p(&r
->buf
[12])));
199 } else if (s
->needs_vpd_bl_emulation
&& page
== 0x00 && r
->buflen
>= 4) {
201 * Now we're capable of supplying the VPD Block Limits
202 * response if the hardware can't. Add it in the INQUIRY
203 * Supported VPD pages response in case we are using the
204 * emulation for this device.
206 * This way, the guest kernel will be aware of the support
207 * and will use it to proper setup the SCSI device.
209 * VPD page numbers must be sorted, so insert 0xb0 at the
210 * right place with an in-place insert. When the while loop
211 * begins the device response is at r[0] to r[page_idx - 1].
213 page_idx
= lduw_be_p(r
->buf
+ 2) + 4;
214 page_idx
= MIN(page_idx
, r
->buflen
);
215 while (page_idx
> 4 && r
->buf
[page_idx
- 1] >= 0xb0) {
216 if (page_idx
< r
->buflen
) {
217 r
->buf
[page_idx
] = r
->buf
[page_idx
- 1];
221 if (page_idx
< r
->buflen
) {
222 r
->buf
[page_idx
] = 0xb0;
224 stw_be_p(r
->buf
+ 2, lduw_be_p(r
->buf
+ 2) + 1);
226 if (len
< r
->buflen
) {
234 static int scsi_generic_emulate_block_limits(SCSIGenericReq
*r
, SCSIDevice
*s
)
239 SCSIBlockLimits bl
= {
240 .max_io_sectors
= calculate_max_transfer(s
),
243 memset(r
->buf
, 0, r
->buflen
);
245 stb_p(buf
+ 1, 0xb0);
246 len
= scsi_emulate_block_limits(buf
+ 4, &bl
);
247 assert(len
<= sizeof(buf
) - 4);
248 stw_be_p(buf
+ 2, len
);
250 memcpy(r
->buf
, buf
, MIN(r
->buflen
, len
+ 4));
252 r
->io_header
.sb_len_wr
= 0;
255 * We have valid contents in the reply buffer but the
256 * io_header can report a sense error coming from
257 * the hardware in scsi_command_complete_noio. Clean
258 * up the io_header to avoid reporting it.
260 r
->io_header
.driver_status
= 0;
261 r
->io_header
.status
= 0;
266 static void scsi_read_complete(void * opaque
, int ret
)
268 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
269 SCSIDevice
*s
= r
->req
.dev
;
272 assert(r
->req
.aiocb
!= NULL
);
275 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
277 if (ret
|| r
->req
.io_canceled
) {
278 scsi_command_complete_noio(r
, ret
);
282 len
= r
->io_header
.dxfer_len
- r
->io_header
.resid
;
283 trace_scsi_generic_read_complete(r
->req
.tag
, len
);
287 if (r
->io_header
.driver_status
& SG_ERR_DRIVER_SENSE
) {
289 scsi_parse_sense_buf(r
->req
.sense
, r
->io_header
.sb_len_wr
);
292 * Check if this is a VPD Block Limits request that
293 * resulted in sense error but would need emulation.
294 * In this case, emulate a valid VPD response.
296 if (sense
.key
== ILLEGAL_REQUEST
&&
297 s
->needs_vpd_bl_emulation
&&
298 r
->req
.cmd
.buf
[0] == INQUIRY
&&
299 (r
->req
.cmd
.buf
[1] & 0x01) &&
300 r
->req
.cmd
.buf
[2] == 0xb0) {
301 len
= scsi_generic_emulate_block_limits(r
, s
);
303 * It's okay to jup to req_complete: no need to
304 * let scsi_handle_inquiry_reply handle an
305 * INQUIRY VPD BL request we created manually.
313 if (r
->io_header
.host_status
!= SCSI_HOST_OK
||
314 (r
->io_header
.driver_status
& SG_ERR_DRIVER_TIMEOUT
) ||
315 r
->io_header
.status
!= GOOD
||
317 scsi_command_complete_noio(r
, 0);
321 /* Snoop READ CAPACITY output to set the blocksize. */
322 if (r
->req
.cmd
.buf
[0] == READ_CAPACITY_10
&&
323 (ldl_be_p(&r
->buf
[0]) != 0xffffffffU
|| s
->max_lba
== 0)) {
324 s
->blocksize
= ldl_be_p(&r
->buf
[4]);
325 s
->max_lba
= ldl_be_p(&r
->buf
[0]) & 0xffffffffULL
;
326 } else if (r
->req
.cmd
.buf
[0] == SERVICE_ACTION_IN_16
&&
327 (r
->req
.cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
328 s
->blocksize
= ldl_be_p(&r
->buf
[8]);
329 s
->max_lba
= ldq_be_p(&r
->buf
[0]);
333 * Patch MODE SENSE device specific parameters if the BDS is opened
336 if ((s
->type
== TYPE_DISK
|| s
->type
== TYPE_TAPE
|| s
->type
== TYPE_ZBC
) &&
337 !blk_is_writable(s
->conf
.blk
) &&
338 (r
->req
.cmd
.buf
[0] == MODE_SENSE
||
339 r
->req
.cmd
.buf
[0] == MODE_SENSE_10
) &&
340 (r
->req
.cmd
.buf
[1] & 0x8) == 0) {
341 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
347 if (r
->req
.cmd
.buf
[0] == INQUIRY
) {
348 len
= scsi_handle_inquiry_reply(r
, s
, len
);
352 scsi_req_data(&r
->req
, len
);
353 scsi_req_unref(&r
->req
);
356 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
359 /* Read more data from scsi device into buffer. */
360 static void scsi_read_data(SCSIRequest
*req
)
362 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
363 SCSIDevice
*s
= r
->req
.dev
;
366 trace_scsi_generic_read_data(req
->tag
);
368 /* The request is used as the AIO opaque value, so add a ref. */
369 scsi_req_ref(&r
->req
);
371 scsi_command_complete_noio(r
, 0);
375 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_FROM_DEV
,
378 scsi_command_complete_noio(r
, ret
);
382 static void scsi_write_complete(void * opaque
, int ret
)
384 SCSIGenericReq
*r
= (SCSIGenericReq
*)opaque
;
385 SCSIDevice
*s
= r
->req
.dev
;
387 trace_scsi_generic_write_complete(ret
);
389 assert(r
->req
.aiocb
!= NULL
);
392 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
394 if (ret
|| r
->req
.io_canceled
) {
395 scsi_command_complete_noio(r
, ret
);
399 if (r
->req
.cmd
.buf
[0] == MODE_SELECT
&& r
->req
.cmd
.buf
[4] == 12 &&
400 s
->type
== TYPE_TAPE
) {
401 s
->blocksize
= (r
->buf
[9] << 16) | (r
->buf
[10] << 8) | r
->buf
[11];
402 trace_scsi_generic_write_complete_blocksize(s
->blocksize
);
405 scsi_command_complete_noio(r
, ret
);
408 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
411 /* Write data to a scsi device. Returns nonzero on failure.
412 The transfer may complete asynchronously. */
413 static void scsi_write_data(SCSIRequest
*req
)
415 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
416 SCSIDevice
*s
= r
->req
.dev
;
419 trace_scsi_generic_write_data(req
->tag
);
422 scsi_req_data(&r
->req
, r
->len
);
426 /* The request is used as the AIO opaque value, so add a ref. */
427 scsi_req_ref(&r
->req
);
428 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_TO_DEV
, scsi_write_complete
);
430 scsi_command_complete_noio(r
, ret
);
434 /* Return a pointer to the data buffer. */
435 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
437 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
442 static void scsi_generic_command_dump(uint8_t *cmd
, int len
)
445 char *line_buffer
, *p
;
447 line_buffer
= g_malloc(len
* 5 + 1);
449 for (i
= 0, p
= line_buffer
; i
< len
; i
++) {
450 p
+= sprintf(p
, " 0x%02x", cmd
[i
]);
452 trace_scsi_generic_send_command(line_buffer
);
457 /* Execute a scsi command. Returns the length of the data expected by the
458 command. This will be Positive for data transfers from the device
459 (eg. disk reads), negative for transfers to the device (eg. disk writes),
460 and zero if the command does not transfer any data. */
462 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *cmd
)
464 SCSIGenericReq
*r
= DO_UPCAST(SCSIGenericReq
, req
, req
);
465 SCSIDevice
*s
= r
->req
.dev
;
468 if (trace_event_get_state_backends(TRACE_SCSI_GENERIC_SEND_COMMAND
)) {
469 scsi_generic_command_dump(cmd
, r
->req
.cmd
.len
);
472 if (r
->req
.cmd
.xfer
== 0) {
476 /* The request is used as the AIO opaque value, so add a ref. */
477 scsi_req_ref(&r
->req
);
478 ret
= execute_command(s
->conf
.blk
, r
, SG_DXFER_NONE
,
479 scsi_command_complete
);
481 scsi_command_complete_noio(r
, ret
);
487 if (r
->buflen
!= r
->req
.cmd
.xfer
) {
489 r
->buf
= g_malloc(r
->req
.cmd
.xfer
);
490 r
->buflen
= r
->req
.cmd
.xfer
;
493 memset(r
->buf
, 0, r
->buflen
);
494 r
->len
= r
->req
.cmd
.xfer
;
495 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
497 return -r
->req
.cmd
.xfer
;
499 return r
->req
.cmd
.xfer
;
503 static int read_naa_id(const uint8_t *p
, uint64_t *p_wwn
)
507 if ((p
[1] & 0xF) == 3) {
508 /* NAA designator type */
512 *p_wwn
= ldq_be_p(p
+ 4);
516 if ((p
[1] & 0xF) == 8) {
517 /* SCSI name string designator type */
518 if (p
[3] < 20 || memcmp(&p
[4], "naa.", 4)) {
521 if (p
[3] > 20 && p
[24] != ',') {
525 for (i
= 8; i
< 24; i
++) {
526 char c
= qemu_toupper(p
[i
]);
527 c
-= (c
>= '0' && c
<= '9' ? '0' : 'A' - 10);
528 *p_wwn
= (*p_wwn
<< 4) | c
;
536 int scsi_SG_IO_FROM_DEV(BlockBackend
*blk
, uint8_t *cmd
, uint8_t cmd_size
,
537 uint8_t *buf
, uint8_t buf_size
, uint32_t timeout
)
539 sg_io_hdr_t io_header
;
543 memset(&io_header
, 0, sizeof(io_header
));
544 io_header
.interface_id
= 'S';
545 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
546 io_header
.dxfer_len
= buf_size
;
547 io_header
.dxferp
= buf
;
548 io_header
.cmdp
= cmd
;
549 io_header
.cmd_len
= cmd_size
;
550 io_header
.mx_sb_len
= sizeof(sensebuf
);
551 io_header
.sbp
= sensebuf
;
552 io_header
.timeout
= timeout
* 1000;
554 trace_scsi_generic_ioctl_sgio_command(cmd
[0], io_header
.timeout
);
555 ret
= blk_ioctl(blk
, SG_IO
, &io_header
);
556 if (ret
< 0 || io_header
.status
||
557 io_header
.driver_status
|| io_header
.host_status
) {
558 trace_scsi_generic_ioctl_sgio_done(cmd
[0], ret
, io_header
.status
,
559 io_header
.host_status
);
566 * Executes an INQUIRY request with EVPD set to retrieve the
567 * available VPD pages of the device. If the device does
568 * not support the Block Limits page (page 0xb0), set
569 * the needs_vpd_bl_emulation flag for future use.
571 static void scsi_generic_set_vpd_bl_emulation(SCSIDevice
*s
)
578 memset(cmd
, 0, sizeof(cmd
));
579 memset(buf
, 0, sizeof(buf
));
583 cmd
[4] = sizeof(buf
);
585 ret
= scsi_SG_IO_FROM_DEV(s
->conf
.blk
, cmd
, sizeof(cmd
),
586 buf
, sizeof(buf
), s
->io_timeout
);
589 * Do not assume anything if we can't retrieve the
590 * INQUIRY response to assert the VPD Block Limits
593 s
->needs_vpd_bl_emulation
= false;
598 for (i
= 4; i
< MIN(sizeof(buf
), page_len
+ 4); i
++) {
599 if (buf
[i
] == 0xb0) {
600 s
->needs_vpd_bl_emulation
= false;
604 s
->needs_vpd_bl_emulation
= true;
607 static void scsi_generic_read_device_identification(SCSIDevice
*s
)
614 memset(cmd
, 0, sizeof(cmd
));
615 memset(buf
, 0, sizeof(buf
));
619 cmd
[4] = sizeof(buf
);
621 ret
= scsi_SG_IO_FROM_DEV(s
->conf
.blk
, cmd
, sizeof(cmd
),
622 buf
, sizeof(buf
), s
->io_timeout
);
627 len
= MIN((buf
[2] << 8) | buf
[3], sizeof(buf
) - 4);
628 for (i
= 0; i
+ 3 <= len
; ) {
629 const uint8_t *p
= &buf
[i
+ 4];
632 if (i
+ (p
[3] + 4) > len
) {
636 if ((p
[1] & 0x10) == 0) {
637 /* Associated with the logical unit */
638 if (read_naa_id(p
, &wwn
) == 0) {
641 } else if ((p
[1] & 0x10) == 0x10) {
642 /* Associated with the target port */
643 if (read_naa_id(p
, &wwn
) == 0) {
652 void scsi_generic_read_device_inquiry(SCSIDevice
*s
)
654 scsi_generic_read_device_identification(s
);
655 if (s
->type
== TYPE_DISK
|| s
->type
== TYPE_ZBC
) {
656 scsi_generic_set_vpd_bl_emulation(s
);
658 s
->needs_vpd_bl_emulation
= false;
662 static int get_stream_blocksize(BlockBackend
*blk
)
668 memset(cmd
, 0, sizeof(cmd
));
669 memset(buf
, 0, sizeof(buf
));
671 cmd
[4] = sizeof(buf
);
673 ret
= scsi_SG_IO_FROM_DEV(blk
, cmd
, sizeof(cmd
), buf
, sizeof(buf
), 6);
678 return (buf
[9] << 16) | (buf
[10] << 8) | buf
[11];
681 static void scsi_generic_reset(DeviceState
*dev
)
683 SCSIDevice
*s
= SCSI_DEVICE(dev
);
685 s
->scsi_version
= s
->default_scsi_version
;
686 scsi_device_purge_requests(s
, SENSE_CODE(RESET
));
689 static void scsi_generic_realize(SCSIDevice
*s
, Error
**errp
)
693 struct sg_scsi_id scsiid
;
696 error_setg(errp
, "drive property not set");
700 if (blk_get_on_error(s
->conf
.blk
, 0) != BLOCKDEV_ON_ERROR_ENOSPC
&&
701 blk_get_on_error(s
->conf
.blk
, 0) != BLOCKDEV_ON_ERROR_REPORT
) {
702 error_setg(errp
, "Device doesn't support drive option werror");
705 if (blk_get_on_error(s
->conf
.blk
, 1) != BLOCKDEV_ON_ERROR_REPORT
) {
706 error_setg(errp
, "Device doesn't support drive option rerror");
710 /* check we are using a driver managing SG_IO (version 3 and after */
711 rc
= blk_ioctl(s
->conf
.blk
, SG_GET_VERSION_NUM
, &sg_version
);
713 error_setg_errno(errp
, -rc
, "cannot get SG_IO version number");
715 error_append_hint(errp
, "Is this a SCSI device?\n");
719 if (sg_version
< 30000) {
720 error_setg(errp
, "scsi generic interface too old");
724 /* get LUN of the /dev/sg? */
725 if (blk_ioctl(s
->conf
.blk
, SG_GET_SCSI_ID
, &scsiid
)) {
726 error_setg(errp
, "SG_GET_SCSI_ID ioctl failed");
729 if (!blkconf_apply_backend_options(&s
->conf
,
730 !blk_supports_write_perm(s
->conf
.blk
),
735 /* define device state */
736 s
->type
= scsiid
.scsi_type
;
737 trace_scsi_generic_realize_type(s
->type
);
741 s
->blocksize
= get_stream_blocksize(s
->conf
.blk
);
742 if (s
->blocksize
== -1) {
747 /* Make a guess for block devices, we'll fix it when the guest sends.
748 * READ CAPACITY. If they don't, they likely would assume these sizes
749 * anyway. (TODO: they could also send MODE SENSE).
760 trace_scsi_generic_realize_blocksize(s
->blocksize
);
762 /* Only used by scsi-block, but initialize it nevertheless to be clean. */
763 s
->default_scsi_version
= -1;
764 s
->io_timeout
= DEFAULT_IO_TIMEOUT
;
765 scsi_generic_read_device_inquiry(s
);
768 const SCSIReqOps scsi_generic_req_ops
= {
769 .size
= sizeof(SCSIGenericReq
),
770 .free_req
= scsi_free_request
,
771 .send_command
= scsi_send_command
,
772 .read_data
= scsi_read_data
,
773 .write_data
= scsi_write_data
,
774 .get_buf
= scsi_get_buf
,
775 .load_request
= scsi_generic_load_request
,
776 .save_request
= scsi_generic_save_request
,
779 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
780 uint8_t *buf
, void *hba_private
)
782 return scsi_req_alloc(&scsi_generic_req_ops
, d
, tag
, lun
, hba_private
);
785 static Property scsi_generic_properties
[] = {
786 DEFINE_PROP_DRIVE("drive", SCSIDevice
, conf
.blk
),
787 DEFINE_PROP_BOOL("share-rw", SCSIDevice
, conf
.share_rw
, false),
788 DEFINE_PROP_UINT32("io_timeout", SCSIDevice
, io_timeout
,
790 DEFINE_PROP_END_OF_LIST(),
793 static int scsi_generic_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
,
794 uint8_t *buf
, size_t buf_len
,
797 return scsi_bus_parse_cdb(dev
, cmd
, buf
, buf_len
, hba_private
);
800 static void scsi_generic_class_initfn(ObjectClass
*klass
, void *data
)
802 DeviceClass
*dc
= DEVICE_CLASS(klass
);
803 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
805 sc
->realize
= scsi_generic_realize
;
806 sc
->alloc_req
= scsi_new_request
;
807 sc
->parse_cdb
= scsi_generic_parse_cdb
;
808 dc
->fw_name
= "disk";
809 dc
->desc
= "pass through generic scsi device (/dev/sg*)";
810 dc
->reset
= scsi_generic_reset
;
811 device_class_set_props(dc
, scsi_generic_properties
);
812 dc
->vmsd
= &vmstate_scsi_device
;
815 static const TypeInfo scsi_generic_info
= {
816 .name
= "scsi-generic",
817 .parent
= TYPE_SCSI_DEVICE
,
818 .instance_size
= sizeof(SCSIDevice
),
819 .class_init
= scsi_generic_class_initfn
,
822 static void scsi_generic_register_types(void)
824 type_register_static(&scsi_generic_info
);
827 type_init(scsi_generic_register_types
)
829 #endif /* __linux__ */