2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
9 * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case
10 * when the allocation length of CDB is smaller
12 * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the
13 * MODE SENSE response.
15 * This code is licensed under the LGPL.
17 * Note that this file only handles the SCSI architecture model and device
18 * commands. Emulation of interface/link layer protocols is handled by
19 * the host adapter emulator.
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
31 #include "qemu-common.h"
32 #include "qemu/error-report.h"
34 #include "hw/scsi-defs.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/blockdev.h"
37 #include "hw/block-common.h"
38 #include "sysemu/dma.h"
44 #define SCSI_DMA_BUF_SIZE 131072
45 #define SCSI_MAX_INQUIRY_LEN 256
46 #define SCSI_MAX_MODE_LEN 256
48 #define DEFAULT_DISCARD_GRANULARITY 4096
50 typedef struct SCSIDiskState SCSIDiskState
;
52 typedef struct SCSIDiskReq
{
54 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
56 uint32_t sector_count
;
64 #define SCSI_DISK_F_REMOVABLE 0
65 #define SCSI_DISK_F_DPOFUA 1
84 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
86 static void scsi_free_request(SCSIRequest
*req
)
88 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
90 qemu_vfree(r
->iov
.iov_base
);
93 /* Helper function for command completion with sense. */
94 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
96 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
97 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
98 scsi_req_build_sense(&r
->req
, sense
);
99 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
102 /* Cancel a pending data transfer. */
103 static void scsi_cancel_io(SCSIRequest
*req
)
105 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
107 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
109 bdrv_aio_cancel(r
->req
.aiocb
);
111 /* This reference was left in by scsi_*_data. We take ownership of
112 * it the moment scsi_req_cancel is called, independent of whether
113 * bdrv_aio_cancel completes the request or not. */
114 scsi_req_unref(&r
->req
);
119 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
121 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
123 if (!r
->iov
.iov_base
) {
125 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
127 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
128 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
129 return r
->qiov
.size
/ 512;
132 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
134 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
136 qemu_put_be64s(f
, &r
->sector
);
137 qemu_put_be32s(f
, &r
->sector_count
);
138 qemu_put_be32s(f
, &r
->buflen
);
140 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
141 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
142 } else if (!req
->retry
) {
143 uint32_t len
= r
->iov
.iov_len
;
144 qemu_put_be32s(f
, &len
);
145 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
150 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
152 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
154 qemu_get_be64s(f
, &r
->sector
);
155 qemu_get_be32s(f
, &r
->sector_count
);
156 qemu_get_be32s(f
, &r
->buflen
);
158 scsi_init_iovec(r
, r
->buflen
);
159 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
160 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
161 } else if (!r
->req
.retry
) {
163 qemu_get_be32s(f
, &len
);
164 r
->iov
.iov_len
= len
;
165 assert(r
->iov
.iov_len
<= r
->buflen
);
166 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
170 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
173 static void scsi_aio_complete(void *opaque
, int ret
)
175 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
176 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
178 assert(r
->req
.aiocb
!= NULL
);
180 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
181 if (r
->req
.io_canceled
) {
186 if (scsi_handle_rw_error(r
, -ret
)) {
191 scsi_req_complete(&r
->req
, GOOD
);
194 if (!r
->req
.io_canceled
) {
195 scsi_req_unref(&r
->req
);
199 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
201 switch (cmd
->buf
[0]) {
208 return (cmd
->buf
[1] & 8) != 0;
213 case WRITE_VERIFY_10
:
214 case WRITE_VERIFY_12
:
215 case WRITE_VERIFY_16
:
225 static void scsi_write_do_fua(SCSIDiskReq
*r
)
227 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
229 if (r
->req
.io_canceled
) {
233 if (scsi_is_cmd_fua(&r
->req
.cmd
)) {
234 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
235 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
239 scsi_req_complete(&r
->req
, GOOD
);
242 if (!r
->req
.io_canceled
) {
243 scsi_req_unref(&r
->req
);
247 static void scsi_dma_complete(void *opaque
, int ret
)
249 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
250 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
252 assert(r
->req
.aiocb
!= NULL
);
254 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
255 if (r
->req
.io_canceled
) {
260 if (scsi_handle_rw_error(r
, -ret
)) {
265 r
->sector
+= r
->sector_count
;
267 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
268 scsi_write_do_fua(r
);
271 scsi_req_complete(&r
->req
, GOOD
);
275 if (!r
->req
.io_canceled
) {
276 scsi_req_unref(&r
->req
);
280 static void scsi_read_complete(void * opaque
, int ret
)
282 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
283 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
286 assert(r
->req
.aiocb
!= NULL
);
288 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
289 if (r
->req
.io_canceled
) {
294 if (scsi_handle_rw_error(r
, -ret
)) {
299 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
301 n
= r
->qiov
.size
/ 512;
303 r
->sector_count
-= n
;
304 scsi_req_data(&r
->req
, r
->qiov
.size
);
307 if (!r
->req
.io_canceled
) {
308 scsi_req_unref(&r
->req
);
312 /* Actually issue a read to the block device. */
313 static void scsi_do_read(void *opaque
, int ret
)
315 SCSIDiskReq
*r
= opaque
;
316 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
319 if (r
->req
.aiocb
!= NULL
) {
321 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
323 if (r
->req
.io_canceled
) {
328 if (scsi_handle_rw_error(r
, -ret
)) {
333 /* The request is used as the AIO opaque value, so add a ref. */
334 scsi_req_ref(&r
->req
);
337 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_READ
);
338 r
->req
.resid
-= r
->req
.sg
->size
;
339 r
->req
.aiocb
= dma_bdrv_read(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
340 scsi_dma_complete
, r
);
342 n
= scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
343 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
344 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
345 scsi_read_complete
, r
);
349 if (!r
->req
.io_canceled
) {
350 scsi_req_unref(&r
->req
);
354 /* Read more data from scsi device into buffer. */
355 static void scsi_read_data(SCSIRequest
*req
)
357 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
358 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
361 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
362 if (r
->sector_count
== 0) {
363 /* This also clears the sense buffer for REQUEST SENSE. */
364 scsi_req_complete(&r
->req
, GOOD
);
368 /* No data transfer may already be in progress */
369 assert(r
->req
.aiocb
== NULL
);
371 /* The request is used as the AIO opaque value, so add a ref. */
372 scsi_req_ref(&r
->req
);
373 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
374 DPRINTF("Data transfer direction invalid\n");
375 scsi_read_complete(r
, -EINVAL
);
380 scsi_read_complete(r
, -ENOMEDIUM
);
386 if (first
&& scsi_is_cmd_fua(&r
->req
.cmd
)) {
387 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
388 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_do_read
, r
);
395 * scsi_handle_rw_error has two return values. 0 means that the error
396 * must be ignored, 1 means that the error has been processed and the
397 * caller should not do anything else for this request. Note that
398 * scsi_handle_rw_error always manages its reference counts, independent
399 * of the return value.
401 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
403 bool is_read
= (r
->req
.cmd
.xfer
== SCSI_XFER_FROM_DEV
);
404 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
405 BlockErrorAction action
= bdrv_get_error_action(s
->qdev
.conf
.bs
, is_read
, error
);
407 if (action
== BDRV_ACTION_REPORT
) {
410 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
413 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
416 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
419 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
423 bdrv_error_action(s
->qdev
.conf
.bs
, action
, is_read
, error
);
424 if (action
== BDRV_ACTION_STOP
) {
425 scsi_req_retry(&r
->req
);
427 return action
!= BDRV_ACTION_IGNORE
;
430 static void scsi_write_complete(void * opaque
, int ret
)
432 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
433 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
436 if (r
->req
.aiocb
!= NULL
) {
438 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
440 if (r
->req
.io_canceled
) {
445 if (scsi_handle_rw_error(r
, -ret
)) {
450 n
= r
->qiov
.size
/ 512;
452 r
->sector_count
-= n
;
453 if (r
->sector_count
== 0) {
454 scsi_write_do_fua(r
);
457 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
458 DPRINTF("Write complete tag=0x%x more=%zd\n", r
->req
.tag
, r
->qiov
.size
);
459 scsi_req_data(&r
->req
, r
->qiov
.size
);
463 if (!r
->req
.io_canceled
) {
464 scsi_req_unref(&r
->req
);
468 static void scsi_write_data(SCSIRequest
*req
)
470 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
471 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
474 /* No data transfer may already be in progress */
475 assert(r
->req
.aiocb
== NULL
);
477 /* The request is used as the AIO opaque value, so add a ref. */
478 scsi_req_ref(&r
->req
);
479 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
480 DPRINTF("Data transfer direction invalid\n");
481 scsi_write_complete(r
, -EINVAL
);
485 if (!r
->req
.sg
&& !r
->qiov
.size
) {
486 /* Called for the first time. Ask the driver to send us more data. */
488 scsi_write_complete(r
, 0);
492 scsi_write_complete(r
, -ENOMEDIUM
);
496 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
497 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
499 scsi_dma_complete(r
, 0);
501 scsi_write_complete(r
, 0);
507 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_WRITE
);
508 r
->req
.resid
-= r
->req
.sg
->size
;
509 r
->req
.aiocb
= dma_bdrv_write(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
510 scsi_dma_complete
, r
);
512 n
= r
->qiov
.size
/ 512;
513 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
514 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
515 scsi_write_complete
, r
);
519 /* Return a pointer to the data buffer. */
520 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
522 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
524 return (uint8_t *)r
->iov
.iov_base
;
527 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
529 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
533 if (req
->cmd
.buf
[1] & 0x1) {
534 /* Vital product data */
535 uint8_t page_code
= req
->cmd
.buf
[2];
537 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
538 outbuf
[buflen
++] = page_code
; // this page
539 outbuf
[buflen
++] = 0x00;
540 outbuf
[buflen
++] = 0x00;
544 case 0x00: /* Supported page codes, mandatory */
546 DPRINTF("Inquiry EVPD[Supported pages] "
547 "buffer size %zd\n", req
->cmd
.xfer
);
548 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
550 outbuf
[buflen
++] = 0x80; // unit serial number
552 outbuf
[buflen
++] = 0x83; // device identification
553 if (s
->qdev
.type
== TYPE_DISK
) {
554 outbuf
[buflen
++] = 0xb0; // block limits
555 outbuf
[buflen
++] = 0xb2; // thin provisioning
559 case 0x80: /* Device serial number, optional */
564 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
568 l
= strlen(s
->serial
);
573 DPRINTF("Inquiry EVPD[Serial number] "
574 "buffer size %zd\n", req
->cmd
.xfer
);
575 memcpy(outbuf
+buflen
, s
->serial
, l
);
580 case 0x83: /* Device identification page, mandatory */
582 const char *str
= s
->serial
?: bdrv_get_device_name(s
->qdev
.conf
.bs
);
583 int max_len
= s
->serial
? 20 : 255 - 8;
584 int id_len
= strlen(str
);
586 if (id_len
> max_len
) {
589 DPRINTF("Inquiry EVPD[Device identification] "
590 "buffer size %zd\n", req
->cmd
.xfer
);
592 outbuf
[buflen
++] = 0x2; // ASCII
593 outbuf
[buflen
++] = 0; // not officially assigned
594 outbuf
[buflen
++] = 0; // reserved
595 outbuf
[buflen
++] = id_len
; // length of data following
596 memcpy(outbuf
+buflen
, str
, id_len
);
600 outbuf
[buflen
++] = 0x1; // Binary
601 outbuf
[buflen
++] = 0x3; // NAA
602 outbuf
[buflen
++] = 0; // reserved
603 outbuf
[buflen
++] = 8;
604 stq_be_p(&outbuf
[buflen
], s
->wwn
);
609 case 0xb0: /* block limits */
611 unsigned int unmap_sectors
=
612 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
613 unsigned int min_io_size
=
614 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
615 unsigned int opt_io_size
=
616 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
618 if (s
->qdev
.type
== TYPE_ROM
) {
619 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
623 /* required VPD size with unmap support */
625 memset(outbuf
+ 4, 0, buflen
- 4);
627 /* optimal transfer length granularity */
628 outbuf
[6] = (min_io_size
>> 8) & 0xff;
629 outbuf
[7] = min_io_size
& 0xff;
631 /* optimal transfer length */
632 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
633 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
634 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
635 outbuf
[15] = opt_io_size
& 0xff;
637 /* optimal unmap granularity */
638 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
639 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
640 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
641 outbuf
[31] = unmap_sectors
& 0xff;
644 case 0xb2: /* thin provisioning */
648 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
649 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
657 assert(buflen
- start
<= 255);
658 outbuf
[start
- 1] = buflen
- start
;
662 /* Standard INQUIRY data */
663 if (req
->cmd
.buf
[2] != 0) {
668 buflen
= req
->cmd
.xfer
;
669 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
670 buflen
= SCSI_MAX_INQUIRY_LEN
;
673 outbuf
[0] = s
->qdev
.type
& 0x1f;
674 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
676 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
677 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
679 memset(&outbuf
[32], 0, 4);
680 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
682 * We claim conformance to SPC-3, which is required for guests
683 * to ask for modern features like READ CAPACITY(16) or the
684 * block characteristics VPD page by default. Not all of SPC-3
685 * is actually implemented, but we're good enough.
688 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
691 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
693 /* If the allocation length of CDB is too small,
694 the additional length is not adjusted */
698 /* Sync data transfer and TCQ. */
699 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
703 static inline bool media_is_dvd(SCSIDiskState
*s
)
706 if (s
->qdev
.type
!= TYPE_ROM
) {
709 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
712 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
713 return nb_sectors
> CD_MAX_SECTORS
;
716 static inline bool media_is_cd(SCSIDiskState
*s
)
719 if (s
->qdev
.type
!= TYPE_ROM
) {
722 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
725 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
726 return nb_sectors
<= CD_MAX_SECTORS
;
729 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
732 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
734 if (s
->qdev
.type
!= TYPE_ROM
) {
738 /* Types 1/2 are only defined for Blu-Ray. */
740 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
744 memset(outbuf
, 0, 34);
746 outbuf
[2] = 0xe; /* last session complete, disc finalized */
747 outbuf
[3] = 1; /* first track on disc */
748 outbuf
[4] = 1; /* # of sessions */
749 outbuf
[5] = 1; /* first track of last session */
750 outbuf
[6] = 1; /* last track of last session */
751 outbuf
[7] = 0x20; /* unrestricted use */
752 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
753 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
754 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
755 /* 24-31: disc bar code */
756 /* 32: disc application code */
757 /* 33: number of OPC tables */
762 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
765 static const int rds_caps_size
[5] = {
772 uint8_t media
= r
->req
.cmd
.buf
[1];
773 uint8_t layer
= r
->req
.cmd
.buf
[6];
774 uint8_t format
= r
->req
.cmd
.buf
[7];
777 if (s
->qdev
.type
!= TYPE_ROM
) {
781 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
785 if (format
!= 0xff) {
786 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
787 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
790 if (media_is_cd(s
)) {
791 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
794 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
797 size
= rds_caps_size
[format
];
798 memset(outbuf
, 0, size
);
803 /* Physical format information */
808 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
810 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
811 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
812 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
813 outbuf
[7] = 0; /* default densities */
815 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
816 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
820 case 0x01: /* DVD copyright information, all zeros */
823 case 0x03: /* BCA information - invalid field for no BCA info */
826 case 0x04: /* DVD disc manufacturing information, all zeros */
829 case 0xff: { /* List capabilities */
832 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
833 if (!rds_caps_size
[i
]) {
837 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
838 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
848 /* Size of buffer, not including 2 byte size field */
849 stw_be_p(outbuf
, size
- 2);
856 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
858 uint8_t event_code
, media_status
;
862 media_status
= MS_TRAY_OPEN
;
863 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
864 media_status
= MS_MEDIA_PRESENT
;
867 /* Event notification descriptor */
868 event_code
= MEC_NO_CHANGE
;
869 if (media_status
!= MS_TRAY_OPEN
) {
870 if (s
->media_event
) {
871 event_code
= MEC_NEW_MEDIA
;
872 s
->media_event
= false;
873 } else if (s
->eject_request
) {
874 event_code
= MEC_EJECT_REQUESTED
;
875 s
->eject_request
= false;
879 outbuf
[0] = event_code
;
880 outbuf
[1] = media_status
;
882 /* These fields are reserved, just clear them. */
888 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
892 uint8_t *buf
= r
->req
.cmd
.buf
;
893 uint8_t notification_class_request
= buf
[4];
894 if (s
->qdev
.type
!= TYPE_ROM
) {
897 if ((buf
[1] & 1) == 0) {
903 outbuf
[0] = outbuf
[1] = 0;
904 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
905 if (notification_class_request
& (1 << GESN_MEDIA
)) {
906 outbuf
[2] = GESN_MEDIA
;
907 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
911 stw_be_p(outbuf
, size
- 4);
915 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
919 if (s
->qdev
.type
!= TYPE_ROM
) {
922 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
923 memset(outbuf
, 0, 40);
924 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
925 stw_be_p(&outbuf
[6], current
);
926 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
927 outbuf
[10] = 0x03; /* persistent, current */
928 outbuf
[11] = 8; /* two profiles */
929 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
930 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
931 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
932 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
933 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
934 stw_be_p(&outbuf
[20], 1);
935 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
937 stl_be_p(&outbuf
[24], 1); /* SCSI */
938 outbuf
[28] = 1; /* DBE = 1, mandatory */
939 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
940 stw_be_p(&outbuf
[32], 3);
941 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
943 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
944 /* TODO: Random readable, CD read, DVD read, drive serial number,
949 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
951 if (s
->qdev
.type
!= TYPE_ROM
) {
954 memset(outbuf
, 0, 8);
955 outbuf
[5] = 1; /* CD-ROM */
959 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
962 static const int mode_sense_valid
[0x3f] = {
963 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
964 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
965 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
966 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
967 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
968 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
971 uint8_t *p
= *p_outbuf
+ 2;
974 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
979 * If Changeable Values are requested, a mask denoting those mode parameters
980 * that are changeable shall be returned. As we currently don't support
981 * parameter changes via MODE_SELECT all bits are returned set to zero.
982 * The buffer was already menset to zero by the caller of this function.
984 * The offsets here are off by two compared to the descriptions in the
985 * SCSI specs, because those include a 2-byte header. This is unfortunate,
986 * but it is done so that offsets are consistent within our implementation
987 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
988 * 2-byte and 4-byte headers.
991 case MODE_PAGE_HD_GEOMETRY
:
993 if (page_control
== 1) { /* Changeable Values */
996 /* if a geometry hint is available, use it */
997 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
998 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
999 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
1000 p
[3] = s
->qdev
.conf
.heads
& 0xff;
1001 /* Write precomp start cylinder, disabled */
1002 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1003 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1004 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
1005 /* Reduced current start cylinder, disabled */
1006 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1007 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1008 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1009 /* Device step rate [ns], 200ns */
1012 /* Landing zone cylinder */
1016 /* Medium rotation rate [rpm], 5400 rpm */
1017 p
[18] = (5400 >> 8) & 0xff;
1018 p
[19] = 5400 & 0xff;
1021 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1023 if (page_control
== 1) { /* Changeable Values */
1026 /* Transfer rate [kbit/s], 5Mbit/s */
1029 /* if a geometry hint is available, use it */
1030 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1031 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1032 p
[4] = s
->qdev
.blocksize
>> 8;
1033 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1034 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1035 /* Write precomp start cylinder, disabled */
1036 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1037 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1038 /* Reduced current start cylinder, disabled */
1039 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1040 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1041 /* Device step rate [100us], 100us */
1044 /* Device step pulse width [us], 1us */
1046 /* Device head settle delay [100us], 100us */
1049 /* Motor on delay [0.1s], 0.1s */
1051 /* Motor off delay [0.1s], 0.1s */
1053 /* Medium rotation rate [rpm], 5400 rpm */
1054 p
[26] = (5400 >> 8) & 0xff;
1055 p
[27] = 5400 & 0xff;
1058 case MODE_PAGE_CACHING
:
1060 if (page_control
== 1 || /* Changeable Values */
1061 bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1066 case MODE_PAGE_R_W_ERROR
:
1068 if (page_control
== 1) { /* Changeable Values */
1071 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1072 if (s
->qdev
.type
== TYPE_ROM
) {
1073 p
[1] = 0x20; /* Read Retry Count */
1077 case MODE_PAGE_AUDIO_CTL
:
1081 case MODE_PAGE_CAPABILITIES
:
1083 if (page_control
== 1) { /* Changeable Values */
1087 p
[0] = 0x3b; /* CD-R & CD-RW read */
1088 p
[1] = 0; /* Writing not supported */
1089 p
[2] = 0x7f; /* Audio, composite, digital out,
1090 mode 2 form 1&2, multi session */
1091 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1092 RW corrected, C2 errors, ISRC,
1094 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1095 /* Locking supported, jumper present, eject, tray */
1096 p
[5] = 0; /* no volume & mute control, no
1098 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1099 p
[7] = (50 * 176) & 0xff;
1100 p
[8] = 2 >> 8; /* Two volume levels */
1102 p
[10] = 2048 >> 8; /* 2M buffer */
1103 p
[11] = 2048 & 0xff;
1104 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1105 p
[13] = (16 * 176) & 0xff;
1106 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1107 p
[17] = (16 * 176) & 0xff;
1108 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1109 p
[19] = (16 * 176) & 0xff;
1116 assert(length
< 256);
1117 (*p_outbuf
)[0] = page
;
1118 (*p_outbuf
)[1] = length
;
1119 *p_outbuf
+= length
+ 2;
1123 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1125 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1126 uint64_t nb_sectors
;
1128 int page
, buflen
, ret
, page_control
;
1130 uint8_t dev_specific_param
;
1132 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1133 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1134 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1135 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1136 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
1137 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1140 if (s
->qdev
.type
== TYPE_DISK
) {
1141 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1142 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1143 dev_specific_param
|= 0x80; /* Readonly. */
1146 /* MMC prescribes that CD/DVD drives have no block descriptors,
1147 * and defines no device-specific parameter. */
1148 dev_specific_param
= 0x00;
1152 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1153 p
[1] = 0; /* Default media type. */
1154 p
[2] = dev_specific_param
;
1155 p
[3] = 0; /* Block descriptor length. */
1157 } else { /* MODE_SENSE_10 */
1158 p
[2] = 0; /* Default media type. */
1159 p
[3] = dev_specific_param
;
1160 p
[6] = p
[7] = 0; /* Block descriptor length. */
1164 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1165 if (!dbd
&& nb_sectors
) {
1166 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1167 outbuf
[3] = 8; /* Block descriptor length */
1168 } else { /* MODE_SENSE_10 */
1169 outbuf
[7] = 8; /* Block descriptor length */
1171 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1172 if (nb_sectors
> 0xffffff) {
1175 p
[0] = 0; /* media density code */
1176 p
[1] = (nb_sectors
>> 16) & 0xff;
1177 p
[2] = (nb_sectors
>> 8) & 0xff;
1178 p
[3] = nb_sectors
& 0xff;
1179 p
[4] = 0; /* reserved */
1180 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1181 p
[6] = s
->qdev
.blocksize
>> 8;
1186 if (page_control
== 3) {
1188 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1193 for (page
= 0; page
<= 0x3e; page
++) {
1194 mode_sense_page(s
, page
, &p
, page_control
);
1197 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1203 buflen
= p
- outbuf
;
1205 * The mode data length field specifies the length in bytes of the
1206 * following data that is available to be transferred. The mode data
1207 * length does not include itself.
1209 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1210 outbuf
[0] = buflen
- 1;
1211 } else { /* MODE_SENSE_10 */
1212 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1213 outbuf
[1] = (buflen
- 2) & 0xff;
1218 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1220 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1221 int start_track
, format
, msf
, toclen
;
1222 uint64_t nb_sectors
;
1224 msf
= req
->cmd
.buf
[1] & 2;
1225 format
= req
->cmd
.buf
[2] & 0xf;
1226 start_track
= req
->cmd
.buf
[6];
1227 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1228 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1229 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1232 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1235 /* multi session : only a single session defined */
1237 memset(outbuf
, 0, 12);
1243 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1251 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1253 SCSIRequest
*req
= &r
->req
;
1254 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1255 bool start
= req
->cmd
.buf
[4] & 1;
1256 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1257 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1260 /* eject/load only happens for power condition == 0 */
1264 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1265 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1266 scsi_check_condition(r
,
1267 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1268 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1269 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1273 if (s
->tray_open
!= !start
) {
1274 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1275 s
->tray_open
= !start
;
1281 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1283 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1284 int buflen
= r
->iov
.iov_len
;
1287 DPRINTF("Read buf_len=%d\n", buflen
);
1290 scsi_req_data(&r
->req
, buflen
);
1294 /* This also clears the sense buffer for REQUEST SENSE. */
1295 scsi_req_complete(&r
->req
, GOOD
);
1298 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1299 uint8_t *inbuf
, int inlen
)
1301 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1302 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1304 int len
, expected_len
, changeable_len
, i
;
1306 /* The input buffer does not include the page header, so it is
1309 expected_len
= inlen
+ 2;
1310 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1315 memset(mode_current
, 0, inlen
+ 2);
1316 len
= mode_sense_page(s
, page
, &p
, 0);
1317 if (len
< 0 || len
!= expected_len
) {
1321 p
= mode_changeable
;
1322 memset(mode_changeable
, 0, inlen
+ 2);
1323 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1324 assert(changeable_len
== len
);
1326 /* Check that unchangeable bits are the same as what MODE SENSE
1329 for (i
= 2; i
< len
; i
++) {
1330 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1337 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1340 case MODE_PAGE_CACHING
:
1341 bdrv_set_enable_write_cache(s
->qdev
.conf
.bs
, (p
[0] & 4) != 0);
1349 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1351 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1354 int page
, subpage
, page_len
;
1356 /* Parse both possible formats for the mode page headers. */
1360 goto invalid_param_len
;
1363 page_len
= lduw_be_p(&p
[2]);
1368 goto invalid_param_len
;
1379 if (page_len
> len
) {
1380 goto invalid_param_len
;
1384 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1388 scsi_disk_apply_mode_select(s
, page
, p
);
1397 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1401 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1405 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1407 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1409 int cmd
= r
->req
.cmd
.buf
[0];
1410 int len
= r
->req
.cmd
.xfer
;
1411 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1415 /* We only support PF=1, SP=0. */
1416 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1420 if (len
< hdr_len
) {
1421 goto invalid_param_len
;
1424 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1428 goto invalid_param_len
;
1430 if (bd_len
!= 0 && bd_len
!= 8) {
1437 /* Ensure no change is made if there is an error! */
1438 for (pass
= 0; pass
< 2; pass
++) {
1439 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1444 if (!bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1445 /* The request is used as the AIO opaque value, so add a ref. */
1446 scsi_req_ref(&r
->req
);
1447 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1448 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
1452 scsi_req_complete(&r
->req
, GOOD
);
1456 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1460 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1464 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1467 static inline bool check_lba_range(SCSIDiskState
*s
,
1468 uint64_t sector_num
, uint32_t nb_sectors
)
1471 * The first line tests that no overflow happens when computing the last
1472 * sector. The second line tests that the last accessed sector is in
1475 * Careful, the computations should not underflow for nb_sectors == 0,
1476 * and a 0-block read to the first LBA beyond the end of device is
1479 return (sector_num
<= sector_num
+ nb_sectors
&&
1480 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1483 typedef struct UnmapCBData
{
1489 static void scsi_unmap_complete(void *opaque
, int ret
)
1491 UnmapCBData
*data
= opaque
;
1492 SCSIDiskReq
*r
= data
->r
;
1493 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1494 uint64_t sector_num
;
1495 uint32_t nb_sectors
;
1497 r
->req
.aiocb
= NULL
;
1498 if (r
->req
.io_canceled
) {
1503 if (scsi_handle_rw_error(r
, -ret
)) {
1508 if (data
->count
> 0) {
1509 sector_num
= ldq_be_p(&data
->inbuf
[0]);
1510 nb_sectors
= ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
;
1511 if (!check_lba_range(s
, sector_num
, nb_sectors
)) {
1512 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1516 r
->req
.aiocb
= bdrv_aio_discard(s
->qdev
.conf
.bs
,
1517 sector_num
* (s
->qdev
.blocksize
/ 512),
1518 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1519 scsi_unmap_complete
, data
);
1525 scsi_req_complete(&r
->req
, GOOD
);
1528 if (!r
->req
.io_canceled
) {
1529 scsi_req_unref(&r
->req
);
1534 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1537 int len
= r
->req
.cmd
.xfer
;
1541 goto invalid_param_len
;
1543 if (len
< lduw_be_p(&p
[0]) + 2) {
1544 goto invalid_param_len
;
1546 if (len
< lduw_be_p(&p
[2]) + 8) {
1547 goto invalid_param_len
;
1549 if (lduw_be_p(&p
[2]) & 15) {
1550 goto invalid_param_len
;
1553 data
= g_new0(UnmapCBData
, 1);
1555 data
->inbuf
= &p
[8];
1556 data
->count
= lduw_be_p(&p
[2]) >> 4;
1558 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1559 scsi_req_ref(&r
->req
);
1560 scsi_unmap_complete(data
, 0);
1564 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1567 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1569 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1571 if (r
->iov
.iov_len
) {
1572 int buflen
= r
->iov
.iov_len
;
1573 DPRINTF("Write buf_len=%d\n", buflen
);
1575 scsi_req_data(&r
->req
, buflen
);
1579 switch (req
->cmd
.buf
[0]) {
1581 case MODE_SELECT_10
:
1582 /* This also clears the sense buffer for REQUEST SENSE. */
1583 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1587 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1595 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1597 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1598 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1599 uint64_t nb_sectors
;
1603 switch (req
->cmd
.buf
[0]) {
1612 case ALLOW_MEDIUM_REMOVAL
:
1613 case GET_CONFIGURATION
:
1614 case GET_EVENT_STATUS_NOTIFICATION
:
1615 case MECHANISM_STATUS
:
1620 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1621 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1628 * FIXME: we shouldn't return anything bigger than 4k, but the code
1629 * requires the buffer to be as big as req->cmd.xfer in several
1630 * places. So, do not allow CDBs with a very large ALLOCATION
1631 * LENGTH. The real fix would be to modify scsi_read_data and
1632 * dma_buf_read, so that they return data beyond the buflen
1635 if (req
->cmd
.xfer
> 65536) {
1636 goto illegal_request
;
1638 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1640 if (!r
->iov
.iov_base
) {
1641 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1644 buflen
= req
->cmd
.xfer
;
1645 outbuf
= r
->iov
.iov_base
;
1646 memset(outbuf
, 0, r
->buflen
);
1647 switch (req
->cmd
.buf
[0]) {
1648 case TEST_UNIT_READY
:
1649 assert(!s
->tray_open
&& bdrv_is_inserted(s
->qdev
.conf
.bs
));
1652 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1654 goto illegal_request
;
1659 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1661 goto illegal_request
;
1665 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1667 goto illegal_request
;
1671 if (req
->cmd
.buf
[1] & 1) {
1672 goto illegal_request
;
1676 if (req
->cmd
.buf
[1] & 3) {
1677 goto illegal_request
;
1681 if (req
->cmd
.buf
[1] & 1) {
1682 goto illegal_request
;
1686 if (req
->cmd
.buf
[1] & 3) {
1687 goto illegal_request
;
1691 if (scsi_disk_emulate_start_stop(r
) < 0) {
1695 case ALLOW_MEDIUM_REMOVAL
:
1696 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1697 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1699 case READ_CAPACITY_10
:
1700 /* The normal LEN field for this command is zero. */
1701 memset(outbuf
, 0, 8);
1702 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1704 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1707 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1708 goto illegal_request
;
1710 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1711 /* Returned value is the address of the last sector. */
1713 /* Remember the new size for read/write sanity checking. */
1714 s
->qdev
.max_lba
= nb_sectors
;
1715 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1716 if (nb_sectors
> UINT32_MAX
) {
1717 nb_sectors
= UINT32_MAX
;
1719 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1720 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1721 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1722 outbuf
[3] = nb_sectors
& 0xff;
1725 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1729 /* Just return "NO SENSE". */
1730 buflen
= scsi_build_sense(NULL
, 0, outbuf
, r
->buflen
,
1731 (req
->cmd
.buf
[1] & 1) == 0);
1733 goto illegal_request
;
1736 case MECHANISM_STATUS
:
1737 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1739 goto illegal_request
;
1742 case GET_CONFIGURATION
:
1743 buflen
= scsi_get_configuration(s
, outbuf
);
1745 goto illegal_request
;
1748 case GET_EVENT_STATUS_NOTIFICATION
:
1749 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1751 goto illegal_request
;
1754 case READ_DISC_INFORMATION
:
1755 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
1757 goto illegal_request
;
1760 case READ_DVD_STRUCTURE
:
1761 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1763 goto illegal_request
;
1766 case SERVICE_ACTION_IN_16
:
1767 /* Service Action In subcommands. */
1768 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1769 DPRINTF("SAI READ CAPACITY(16)\n");
1770 memset(outbuf
, 0, req
->cmd
.xfer
);
1771 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1773 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1776 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1777 goto illegal_request
;
1779 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1780 /* Returned value is the address of the last sector. */
1782 /* Remember the new size for read/write sanity checking. */
1783 s
->qdev
.max_lba
= nb_sectors
;
1784 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1785 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1786 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1787 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1788 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1789 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1790 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1791 outbuf
[7] = nb_sectors
& 0xff;
1794 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1797 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1799 /* set TPE bit if the format supports discard */
1800 if (s
->qdev
.conf
.discard_granularity
) {
1804 /* Protection, exponent and lowest lba field left blank. */
1807 DPRINTF("Unsupported Service Action In\n");
1808 goto illegal_request
;
1809 case SYNCHRONIZE_CACHE
:
1810 /* The request is used as the AIO opaque value, so add a ref. */
1811 scsi_req_ref(&r
->req
);
1812 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1813 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
1816 DPRINTF("Seek(10) (sector %" PRId64
")\n", r
->req
.cmd
.lba
);
1817 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1822 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1824 case MODE_SELECT_10
:
1825 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1828 DPRINTF("Unmap (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1832 nb_sectors
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
1833 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1834 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1837 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1842 * We only support WRITE SAME with the unmap bit set for now.
1844 if (!(req
->cmd
.buf
[1] & 0x8)) {
1845 goto illegal_request
;
1848 /* The request is used as the AIO opaque value, so add a ref. */
1849 scsi_req_ref(&r
->req
);
1850 r
->req
.aiocb
= bdrv_aio_discard(s
->qdev
.conf
.bs
,
1851 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1852 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1853 scsi_aio_complete
, r
);
1856 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1857 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1860 assert(!r
->req
.aiocb
);
1861 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
1862 if (r
->iov
.iov_len
== 0) {
1863 scsi_req_complete(&r
->req
, GOOD
);
1865 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1866 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
1867 return -r
->iov
.iov_len
;
1869 return r
->iov
.iov_len
;
1873 if (r
->req
.status
== -1) {
1874 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1879 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1883 /* Execute a scsi command. Returns the length of the data expected by the
1884 command. This will be Positive for data transfers from the device
1885 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1886 and zero if the command does not transfer any data. */
1888 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
1890 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1891 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1897 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1898 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1902 len
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
1908 DPRINTF("Read (sector %" PRId64
", count %u)\n", r
->req
.cmd
.lba
, len
);
1909 if (r
->req
.cmd
.buf
[1] & 0xe0) {
1910 goto illegal_request
;
1912 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
1915 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1916 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1922 case WRITE_VERIFY_10
:
1923 case WRITE_VERIFY_12
:
1924 case WRITE_VERIFY_16
:
1925 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1926 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1933 DPRINTF("Write %s(sector %" PRId64
", count %u)\n",
1934 (command
& 0xe) == 0xe ? "And Verify " : "",
1935 r
->req
.cmd
.lba
, len
);
1936 if (r
->req
.cmd
.buf
[1] & 0xe0) {
1937 goto illegal_request
;
1939 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
1942 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1943 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1948 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1951 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1954 if (r
->sector_count
== 0) {
1955 scsi_req_complete(&r
->req
, GOOD
);
1957 assert(r
->iov
.iov_len
== 0);
1958 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1959 return -r
->sector_count
* 512;
1961 return r
->sector_count
* 512;
1965 static void scsi_disk_reset(DeviceState
*dev
)
1967 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1968 uint64_t nb_sectors
;
1970 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1972 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1973 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1977 s
->qdev
.max_lba
= nb_sectors
;
1980 static void scsi_destroy(SCSIDevice
*dev
)
1982 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1984 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1985 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1988 static void scsi_disk_resize_cb(void *opaque
)
1990 SCSIDiskState
*s
= opaque
;
1992 /* SPC lists this sense code as available only for
1993 * direct-access devices.
1995 if (s
->qdev
.type
== TYPE_DISK
) {
1996 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
2000 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
2002 SCSIDiskState
*s
= opaque
;
2005 * When a CD gets changed, we have to report an ejected state and
2006 * then a loaded state to guests so that they detect tray
2007 * open/close and media change events. Guests that do not use
2008 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2009 * states rely on this behavior.
2011 * media_changed governs the state machine used for unit attention
2012 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2014 s
->media_changed
= load
;
2015 s
->tray_open
= !load
;
2016 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
2017 s
->media_event
= true;
2018 s
->eject_request
= false;
2021 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2023 SCSIDiskState
*s
= opaque
;
2025 s
->eject_request
= true;
2027 s
->tray_locked
= false;
2031 static bool scsi_cd_is_tray_open(void *opaque
)
2033 return ((SCSIDiskState
*)opaque
)->tray_open
;
2036 static bool scsi_cd_is_medium_locked(void *opaque
)
2038 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2041 static const BlockDevOps scsi_disk_removable_block_ops
= {
2042 .change_media_cb
= scsi_cd_change_media_cb
,
2043 .eject_request_cb
= scsi_cd_eject_request_cb
,
2044 .is_tray_open
= scsi_cd_is_tray_open
,
2045 .is_medium_locked
= scsi_cd_is_medium_locked
,
2047 .resize_cb
= scsi_disk_resize_cb
,
2050 static const BlockDevOps scsi_disk_block_ops
= {
2051 .resize_cb
= scsi_disk_resize_cb
,
2054 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2056 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2057 if (s
->media_changed
) {
2058 s
->media_changed
= false;
2059 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2063 static int scsi_initfn(SCSIDevice
*dev
)
2065 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2067 if (!s
->qdev
.conf
.bs
) {
2068 error_report("drive property not set");
2072 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2073 !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
2074 error_report("Device needs media, but drive is empty");
2078 blkconf_serial(&s
->qdev
.conf
, &s
->serial
);
2079 if (dev
->type
== TYPE_DISK
2080 && blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255) < 0) {
2084 if (s
->qdev
.conf
.discard_granularity
== -1) {
2085 s
->qdev
.conf
.discard_granularity
=
2086 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2090 s
->version
= g_strdup(qemu_get_version());
2093 s
->vendor
= g_strdup("QEMU");
2096 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
2097 error_report("unwanted /dev/sg*");
2101 if (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) {
2102 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_removable_block_ops
, s
);
2104 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_block_ops
, s
);
2106 bdrv_set_buffer_alignment(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
2108 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
2109 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, NULL
);
2113 static int scsi_hd_initfn(SCSIDevice
*dev
)
2115 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2116 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2117 s
->qdev
.type
= TYPE_DISK
;
2119 s
->product
= g_strdup("QEMU HARDDISK");
2121 return scsi_initfn(&s
->qdev
);
2124 static int scsi_cd_initfn(SCSIDevice
*dev
)
2126 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2127 s
->qdev
.blocksize
= 2048;
2128 s
->qdev
.type
= TYPE_ROM
;
2129 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2131 s
->product
= g_strdup("QEMU CD-ROM");
2133 return scsi_initfn(&s
->qdev
);
2136 static int scsi_disk_initfn(SCSIDevice
*dev
)
2140 if (!dev
->conf
.bs
) {
2141 return scsi_initfn(dev
); /* ... and die there */
2144 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
2145 if (dinfo
->media_cd
) {
2146 return scsi_cd_initfn(dev
);
2148 return scsi_hd_initfn(dev
);
2152 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2153 .size
= sizeof(SCSIDiskReq
),
2154 .free_req
= scsi_free_request
,
2155 .send_command
= scsi_disk_emulate_command
,
2156 .read_data
= scsi_disk_emulate_read_data
,
2157 .write_data
= scsi_disk_emulate_write_data
,
2158 .get_buf
= scsi_get_buf
,
2161 static const SCSIReqOps scsi_disk_dma_reqops
= {
2162 .size
= sizeof(SCSIDiskReq
),
2163 .free_req
= scsi_free_request
,
2164 .send_command
= scsi_disk_dma_command
,
2165 .read_data
= scsi_read_data
,
2166 .write_data
= scsi_write_data
,
2167 .cancel_io
= scsi_cancel_io
,
2168 .get_buf
= scsi_get_buf
,
2169 .load_request
= scsi_disk_load_request
,
2170 .save_request
= scsi_disk_save_request
,
2173 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2174 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2175 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2176 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2177 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2178 [START_STOP
] = &scsi_disk_emulate_reqops
,
2179 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2180 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2181 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2182 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2183 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2184 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2185 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2186 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2187 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2188 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2189 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2190 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2191 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2192 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2193 [UNMAP
] = &scsi_disk_emulate_reqops
,
2194 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2195 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2197 [READ_6
] = &scsi_disk_dma_reqops
,
2198 [READ_10
] = &scsi_disk_dma_reqops
,
2199 [READ_12
] = &scsi_disk_dma_reqops
,
2200 [READ_16
] = &scsi_disk_dma_reqops
,
2201 [VERIFY_10
] = &scsi_disk_dma_reqops
,
2202 [VERIFY_12
] = &scsi_disk_dma_reqops
,
2203 [VERIFY_16
] = &scsi_disk_dma_reqops
,
2204 [WRITE_6
] = &scsi_disk_dma_reqops
,
2205 [WRITE_10
] = &scsi_disk_dma_reqops
,
2206 [WRITE_12
] = &scsi_disk_dma_reqops
,
2207 [WRITE_16
] = &scsi_disk_dma_reqops
,
2208 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2209 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2210 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2213 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2214 uint8_t *buf
, void *hba_private
)
2216 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2218 const SCSIReqOps
*ops
;
2222 ops
= scsi_disk_reqops_dispatch
[command
];
2224 ops
= &scsi_disk_emulate_reqops
;
2226 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2229 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
2232 for (i
= 1; i
< req
->cmd
.len
; i
++) {
2233 printf(" 0x%02x", buf
[i
]);
2243 static int get_device_type(SCSIDiskState
*s
)
2245 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
2248 uint8_t sensebuf
[8];
2249 sg_io_hdr_t io_header
;
2252 memset(cmd
, 0, sizeof(cmd
));
2253 memset(buf
, 0, sizeof(buf
));
2255 cmd
[4] = sizeof(buf
);
2257 memset(&io_header
, 0, sizeof(io_header
));
2258 io_header
.interface_id
= 'S';
2259 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2260 io_header
.dxfer_len
= sizeof(buf
);
2261 io_header
.dxferp
= buf
;
2262 io_header
.cmdp
= cmd
;
2263 io_header
.cmd_len
= sizeof(cmd
);
2264 io_header
.mx_sb_len
= sizeof(sensebuf
);
2265 io_header
.sbp
= sensebuf
;
2266 io_header
.timeout
= 6000; /* XXX */
2268 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
2269 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
2272 s
->qdev
.type
= buf
[0];
2273 if (buf
[1] & 0x80) {
2274 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2279 static int scsi_block_initfn(SCSIDevice
*dev
)
2281 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2285 if (!s
->qdev
.conf
.bs
) {
2286 error_report("scsi-block: drive property not set");
2290 /* check we are using a driver managing SG_IO (version 3 and after) */
2291 if (bdrv_ioctl(s
->qdev
.conf
.bs
, SG_GET_VERSION_NUM
, &sg_version
) < 0 ||
2292 sg_version
< 30000) {
2293 error_report("scsi-block: scsi generic interface too old");
2297 /* get device type from INQUIRY data */
2298 rc
= get_device_type(s
);
2300 error_report("scsi-block: INQUIRY failed");
2304 /* Make a guess for the block size, we'll fix it when the guest sends.
2305 * READ CAPACITY. If they don't, they likely would assume these sizes
2306 * anyway. (TODO: check in /sys).
2308 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2309 s
->qdev
.blocksize
= 2048;
2311 s
->qdev
.blocksize
= 512;
2313 return scsi_initfn(&s
->qdev
);
2316 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2317 uint32_t lun
, uint8_t *buf
,
2320 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2334 case WRITE_VERIFY_10
:
2335 case WRITE_VERIFY_12
:
2336 case WRITE_VERIFY_16
:
2337 /* If we are not using O_DIRECT, we might read stale data from the
2338 * host cache if writes were made using other commands than these
2339 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2340 * O_DIRECT everything must go through SG_IO.
2342 if (bdrv_get_flags(s
->qdev
.conf
.bs
) & BDRV_O_NOCACHE
) {
2346 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2347 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2348 * And once you do these writes, reading from the block device is
2349 * unreliable, too. It is even possible that reads deliver random data
2350 * from the host page cache (this is probably a Linux bug).
2352 * We might use scsi_disk_dma_reqops as long as no writing commands are
2353 * seen, but performance usually isn't paramount on optical media. So,
2354 * just make scsi-block operate the same as scsi-generic for them.
2356 if (s
->qdev
.type
!= TYPE_ROM
) {
2357 return scsi_req_alloc(&scsi_disk_dma_reqops
, &s
->qdev
, tag
, lun
,
2362 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2367 #define DEFINE_SCSI_DISK_PROPERTIES() \
2368 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2369 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2370 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2371 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2372 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2374 static Property scsi_hd_properties
[] = {
2375 DEFINE_SCSI_DISK_PROPERTIES(),
2376 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2377 SCSI_DISK_F_REMOVABLE
, false),
2378 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2379 SCSI_DISK_F_DPOFUA
, false),
2380 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2381 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
2382 DEFINE_PROP_END_OF_LIST(),
2385 static const VMStateDescription vmstate_scsi_disk_state
= {
2386 .name
= "scsi-disk",
2388 .minimum_version_id
= 1,
2389 .minimum_version_id_old
= 1,
2390 .fields
= (VMStateField
[]) {
2391 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
2392 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
2393 VMSTATE_BOOL(media_event
, SCSIDiskState
),
2394 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
2395 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
2396 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
2397 VMSTATE_END_OF_LIST()
2401 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
2403 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2404 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2406 sc
->init
= scsi_hd_initfn
;
2407 sc
->destroy
= scsi_destroy
;
2408 sc
->alloc_req
= scsi_new_request
;
2409 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2410 dc
->fw_name
= "disk";
2411 dc
->desc
= "virtual SCSI disk";
2412 dc
->reset
= scsi_disk_reset
;
2413 dc
->props
= scsi_hd_properties
;
2414 dc
->vmsd
= &vmstate_scsi_disk_state
;
2417 static const TypeInfo scsi_hd_info
= {
2419 .parent
= TYPE_SCSI_DEVICE
,
2420 .instance_size
= sizeof(SCSIDiskState
),
2421 .class_init
= scsi_hd_class_initfn
,
2424 static Property scsi_cd_properties
[] = {
2425 DEFINE_SCSI_DISK_PROPERTIES(),
2426 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2427 DEFINE_PROP_END_OF_LIST(),
2430 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
2432 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2433 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2435 sc
->init
= scsi_cd_initfn
;
2436 sc
->destroy
= scsi_destroy
;
2437 sc
->alloc_req
= scsi_new_request
;
2438 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2439 dc
->fw_name
= "disk";
2440 dc
->desc
= "virtual SCSI CD-ROM";
2441 dc
->reset
= scsi_disk_reset
;
2442 dc
->props
= scsi_cd_properties
;
2443 dc
->vmsd
= &vmstate_scsi_disk_state
;
2446 static const TypeInfo scsi_cd_info
= {
2448 .parent
= TYPE_SCSI_DEVICE
,
2449 .instance_size
= sizeof(SCSIDiskState
),
2450 .class_init
= scsi_cd_class_initfn
,
2454 static Property scsi_block_properties
[] = {
2455 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.bs
),
2456 DEFINE_PROP_INT32("bootindex", SCSIDiskState
, qdev
.conf
.bootindex
, -1),
2457 DEFINE_PROP_END_OF_LIST(),
2460 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
2462 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2463 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2465 sc
->init
= scsi_block_initfn
;
2466 sc
->destroy
= scsi_destroy
;
2467 sc
->alloc_req
= scsi_block_new_request
;
2468 dc
->fw_name
= "disk";
2469 dc
->desc
= "SCSI block device passthrough";
2470 dc
->reset
= scsi_disk_reset
;
2471 dc
->props
= scsi_block_properties
;
2472 dc
->vmsd
= &vmstate_scsi_disk_state
;
2475 static const TypeInfo scsi_block_info
= {
2476 .name
= "scsi-block",
2477 .parent
= TYPE_SCSI_DEVICE
,
2478 .instance_size
= sizeof(SCSIDiskState
),
2479 .class_init
= scsi_block_class_initfn
,
2483 static Property scsi_disk_properties
[] = {
2484 DEFINE_SCSI_DISK_PROPERTIES(),
2485 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2486 SCSI_DISK_F_REMOVABLE
, false),
2487 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2488 SCSI_DISK_F_DPOFUA
, false),
2489 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2490 DEFINE_PROP_END_OF_LIST(),
2493 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
2495 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2496 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2498 sc
->init
= scsi_disk_initfn
;
2499 sc
->destroy
= scsi_destroy
;
2500 sc
->alloc_req
= scsi_new_request
;
2501 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2502 dc
->fw_name
= "disk";
2503 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
2504 dc
->reset
= scsi_disk_reset
;
2505 dc
->props
= scsi_disk_properties
;
2506 dc
->vmsd
= &vmstate_scsi_disk_state
;
2509 static const TypeInfo scsi_disk_info
= {
2510 .name
= "scsi-disk",
2511 .parent
= TYPE_SCSI_DEVICE
,
2512 .instance_size
= sizeof(SCSIDiskState
),
2513 .class_init
= scsi_disk_class_initfn
,
2516 static void scsi_disk_register_types(void)
2518 type_register_static(&scsi_hd_info
);
2519 type_register_static(&scsi_cd_info
);
2521 type_register_static(&scsi_block_info
);
2523 type_register_static(&scsi_disk_info
);
2526 type_init(scsi_disk_register_types
)