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"
33 #include "hw/scsi/scsi.h"
34 #include "block/scsi.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/blockdev.h"
38 #include "hw/block/block.h"
39 #include "sysemu/dma.h"
45 #define SCSI_WRITE_SAME_MAX 524288
46 #define SCSI_DMA_BUF_SIZE 131072
47 #define SCSI_MAX_INQUIRY_LEN 256
48 #define SCSI_MAX_MODE_LEN 256
50 #define DEFAULT_DISCARD_GRANULARITY 4096
51 #define DEFAULT_MAX_UNMAP_SIZE (1 << 30) /* 1 GB */
52 #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */
54 typedef struct SCSIDiskState SCSIDiskState
;
56 typedef struct SCSIDiskReq
{
58 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
60 uint32_t sector_count
;
68 #define SCSI_DISK_F_REMOVABLE 0
69 #define SCSI_DISK_F_DPOFUA 1
70 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2
82 uint64_t max_unmap_size
;
93 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
95 static void scsi_free_request(SCSIRequest
*req
)
97 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
99 qemu_vfree(r
->iov
.iov_base
);
102 /* Helper function for command completion with sense. */
103 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
105 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
106 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
107 scsi_req_build_sense(&r
->req
, sense
);
108 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
111 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
113 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
115 if (!r
->iov
.iov_base
) {
117 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
119 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
120 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
121 return r
->qiov
.size
/ 512;
124 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
126 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
128 qemu_put_be64s(f
, &r
->sector
);
129 qemu_put_be32s(f
, &r
->sector_count
);
130 qemu_put_be32s(f
, &r
->buflen
);
132 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
133 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
134 } else if (!req
->retry
) {
135 uint32_t len
= r
->iov
.iov_len
;
136 qemu_put_be32s(f
, &len
);
137 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
142 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
144 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
146 qemu_get_be64s(f
, &r
->sector
);
147 qemu_get_be32s(f
, &r
->sector_count
);
148 qemu_get_be32s(f
, &r
->buflen
);
150 scsi_init_iovec(r
, r
->buflen
);
151 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
152 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
153 } else if (!r
->req
.retry
) {
155 qemu_get_be32s(f
, &len
);
156 r
->iov
.iov_len
= len
;
157 assert(r
->iov
.iov_len
<= r
->buflen
);
158 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
162 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
165 static void scsi_aio_complete(void *opaque
, int ret
)
167 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
168 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
170 assert(r
->req
.aiocb
!= NULL
);
172 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
173 if (r
->req
.io_canceled
) {
174 scsi_req_cancel_complete(&r
->req
);
179 if (scsi_handle_rw_error(r
, -ret
)) {
184 scsi_req_complete(&r
->req
, GOOD
);
187 scsi_req_unref(&r
->req
);
190 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
192 switch (cmd
->buf
[0]) {
199 return (cmd
->buf
[1] & 8) != 0;
204 case WRITE_VERIFY_10
:
205 case WRITE_VERIFY_12
:
206 case WRITE_VERIFY_16
:
216 static void scsi_write_do_fua(SCSIDiskReq
*r
)
218 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
220 assert(r
->req
.aiocb
== NULL
);
222 if (r
->req
.io_canceled
) {
223 scsi_req_cancel_complete(&r
->req
);
227 if (scsi_is_cmd_fua(&r
->req
.cmd
)) {
228 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
230 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
234 scsi_req_complete(&r
->req
, GOOD
);
237 scsi_req_unref(&r
->req
);
240 static void scsi_dma_complete_noio(SCSIDiskReq
*r
, int ret
)
242 assert(r
->req
.aiocb
== NULL
);
244 if (r
->req
.io_canceled
) {
245 scsi_req_cancel_complete(&r
->req
);
250 if (scsi_handle_rw_error(r
, -ret
)) {
255 r
->sector
+= r
->sector_count
;
257 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
258 scsi_write_do_fua(r
);
261 scsi_req_complete(&r
->req
, GOOD
);
265 scsi_req_unref(&r
->req
);
268 static void scsi_dma_complete(void *opaque
, int ret
)
270 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
271 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
273 assert(r
->req
.aiocb
!= NULL
);
276 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
277 scsi_dma_complete_noio(r
, ret
);
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 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
289 if (r
->req
.io_canceled
) {
290 scsi_req_cancel_complete(&r
->req
);
295 if (scsi_handle_rw_error(r
, -ret
)) {
300 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
302 n
= r
->qiov
.size
/ 512;
304 r
->sector_count
-= n
;
305 scsi_req_data(&r
->req
, r
->qiov
.size
);
308 scsi_req_unref(&r
->req
);
311 /* Actually issue a read to the block device. */
312 static void scsi_do_read(SCSIDiskReq
*r
, int ret
)
314 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
317 assert (r
->req
.aiocb
== NULL
);
319 if (r
->req
.io_canceled
) {
320 scsi_req_cancel_complete(&r
->req
);
325 if (scsi_handle_rw_error(r
, -ret
)) {
330 /* The request is used as the AIO opaque value, so add a ref. */
331 scsi_req_ref(&r
->req
);
334 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_READ
);
335 r
->req
.resid
-= r
->req
.sg
->size
;
336 r
->req
.aiocb
= dma_blk_read(s
->qdev
.conf
.blk
, r
->req
.sg
, r
->sector
,
337 scsi_dma_complete
, r
);
339 n
= scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
340 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
341 n
* BDRV_SECTOR_SIZE
, BLOCK_ACCT_READ
);
342 r
->req
.aiocb
= blk_aio_readv(s
->qdev
.conf
.blk
, r
->sector
, &r
->qiov
, n
,
343 scsi_read_complete
, r
);
347 scsi_req_unref(&r
->req
);
350 static void scsi_do_read_cb(void *opaque
, int ret
)
352 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
353 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
355 assert (r
->req
.aiocb
!= NULL
);
358 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
359 scsi_do_read(opaque
, ret
);
362 /* Read more data from scsi device into buffer. */
363 static void scsi_read_data(SCSIRequest
*req
)
365 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
366 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
369 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
370 if (r
->sector_count
== 0) {
371 /* This also clears the sense buffer for REQUEST SENSE. */
372 scsi_req_complete(&r
->req
, GOOD
);
376 /* No data transfer may already be in progress */
377 assert(r
->req
.aiocb
== NULL
);
379 /* The request is used as the AIO opaque value, so add a ref. */
380 scsi_req_ref(&r
->req
);
381 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
382 DPRINTF("Data transfer direction invalid\n");
383 scsi_read_complete(r
, -EINVAL
);
388 scsi_read_complete(r
, -ENOMEDIUM
);
394 if (first
&& scsi_is_cmd_fua(&r
->req
.cmd
)) {
395 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
397 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_do_read_cb
, r
);
404 * scsi_handle_rw_error has two return values. 0 means that the error
405 * must be ignored, 1 means that the error has been processed and the
406 * caller should not do anything else for this request. Note that
407 * scsi_handle_rw_error always manages its reference counts, independent
408 * of the return value.
410 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
412 bool is_read
= (r
->req
.cmd
.mode
== SCSI_XFER_FROM_DEV
);
413 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
414 BlockErrorAction action
= blk_get_error_action(s
->qdev
.conf
.blk
,
417 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
420 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
423 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
426 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
429 scsi_check_condition(r
, SENSE_CODE(SPACE_ALLOC_FAILED
));
432 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
436 blk_error_action(s
->qdev
.conf
.blk
, action
, is_read
, error
);
437 if (action
== BLOCK_ERROR_ACTION_STOP
) {
438 scsi_req_retry(&r
->req
);
440 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
443 static void scsi_write_complete_noio(SCSIDiskReq
*r
, int ret
)
447 assert (r
->req
.aiocb
== NULL
);
449 if (r
->req
.io_canceled
) {
450 scsi_req_cancel_complete(&r
->req
);
455 if (scsi_handle_rw_error(r
, -ret
)) {
460 n
= r
->qiov
.size
/ 512;
462 r
->sector_count
-= n
;
463 if (r
->sector_count
== 0) {
464 scsi_write_do_fua(r
);
467 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
468 DPRINTF("Write complete tag=0x%x more=%zd\n", r
->req
.tag
, r
->qiov
.size
);
469 scsi_req_data(&r
->req
, r
->qiov
.size
);
473 scsi_req_unref(&r
->req
);
476 static void scsi_write_complete(void * opaque
, int ret
)
478 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
479 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
481 assert (r
->req
.aiocb
!= NULL
);
484 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
485 scsi_write_complete_noio(r
, ret
);
488 static void scsi_write_data(SCSIRequest
*req
)
490 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
491 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
494 /* No data transfer may already be in progress */
495 assert(r
->req
.aiocb
== NULL
);
497 /* The request is used as the AIO opaque value, so add a ref. */
498 scsi_req_ref(&r
->req
);
499 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
500 DPRINTF("Data transfer direction invalid\n");
501 scsi_write_complete_noio(r
, -EINVAL
);
505 if (!r
->req
.sg
&& !r
->qiov
.size
) {
506 /* Called for the first time. Ask the driver to send us more data. */
508 scsi_write_complete_noio(r
, 0);
512 scsi_write_complete_noio(r
, -ENOMEDIUM
);
516 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
517 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
519 scsi_dma_complete_noio(r
, 0);
521 scsi_write_complete_noio(r
, 0);
527 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_WRITE
);
528 r
->req
.resid
-= r
->req
.sg
->size
;
529 r
->req
.aiocb
= dma_blk_write(s
->qdev
.conf
.blk
, r
->req
.sg
, r
->sector
,
530 scsi_dma_complete
, r
);
532 n
= r
->qiov
.size
/ 512;
533 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
534 n
* BDRV_SECTOR_SIZE
, BLOCK_ACCT_WRITE
);
535 r
->req
.aiocb
= blk_aio_writev(s
->qdev
.conf
.blk
, r
->sector
, &r
->qiov
, n
,
536 scsi_write_complete
, r
);
540 /* Return a pointer to the data buffer. */
541 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
543 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
545 return (uint8_t *)r
->iov
.iov_base
;
548 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
550 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
554 if (req
->cmd
.buf
[1] & 0x1) {
555 /* Vital product data */
556 uint8_t page_code
= req
->cmd
.buf
[2];
558 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
559 outbuf
[buflen
++] = page_code
; // this page
560 outbuf
[buflen
++] = 0x00;
561 outbuf
[buflen
++] = 0x00;
565 case 0x00: /* Supported page codes, mandatory */
567 DPRINTF("Inquiry EVPD[Supported pages] "
568 "buffer size %zd\n", req
->cmd
.xfer
);
569 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
571 outbuf
[buflen
++] = 0x80; // unit serial number
573 outbuf
[buflen
++] = 0x83; // device identification
574 if (s
->qdev
.type
== TYPE_DISK
) {
575 outbuf
[buflen
++] = 0xb0; // block limits
576 outbuf
[buflen
++] = 0xb2; // thin provisioning
580 case 0x80: /* Device serial number, optional */
585 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
589 l
= strlen(s
->serial
);
594 DPRINTF("Inquiry EVPD[Serial number] "
595 "buffer size %zd\n", req
->cmd
.xfer
);
596 memcpy(outbuf
+buflen
, s
->serial
, l
);
601 case 0x83: /* Device identification page, mandatory */
603 const char *str
= s
->serial
?: blk_name(s
->qdev
.conf
.blk
);
604 int max_len
= s
->serial
? 20 : 255 - 8;
605 int id_len
= strlen(str
);
607 if (id_len
> max_len
) {
610 DPRINTF("Inquiry EVPD[Device identification] "
611 "buffer size %zd\n", req
->cmd
.xfer
);
613 outbuf
[buflen
++] = 0x2; // ASCII
614 outbuf
[buflen
++] = 0; // not officially assigned
615 outbuf
[buflen
++] = 0; // reserved
616 outbuf
[buflen
++] = id_len
; // length of data following
617 memcpy(outbuf
+buflen
, str
, id_len
);
621 outbuf
[buflen
++] = 0x1; // Binary
622 outbuf
[buflen
++] = 0x3; // NAA
623 outbuf
[buflen
++] = 0; // reserved
624 outbuf
[buflen
++] = 8;
625 stq_be_p(&outbuf
[buflen
], s
->wwn
);
630 outbuf
[buflen
++] = 0x61; // SAS / Binary
631 outbuf
[buflen
++] = 0x93; // PIV / Target port / NAA
632 outbuf
[buflen
++] = 0; // reserved
633 outbuf
[buflen
++] = 8;
634 stq_be_p(&outbuf
[buflen
], s
->port_wwn
);
639 outbuf
[buflen
++] = 0x61; // SAS / Binary
640 outbuf
[buflen
++] = 0x94; // PIV / Target port / relative target port
641 outbuf
[buflen
++] = 0; // reserved
642 outbuf
[buflen
++] = 4;
643 stw_be_p(&outbuf
[buflen
+ 2], s
->port_index
);
648 case 0xb0: /* block limits */
650 unsigned int unmap_sectors
=
651 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
652 unsigned int min_io_size
=
653 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
654 unsigned int opt_io_size
=
655 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
656 unsigned int max_unmap_sectors
=
657 s
->max_unmap_size
/ s
->qdev
.blocksize
;
658 unsigned int max_io_sectors
=
659 s
->max_io_size
/ s
->qdev
.blocksize
;
661 if (s
->qdev
.type
== TYPE_ROM
) {
662 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
666 /* required VPD size with unmap support */
668 memset(outbuf
+ 4, 0, buflen
- 4);
670 outbuf
[4] = 0x1; /* wsnz */
672 /* optimal transfer length granularity */
673 outbuf
[6] = (min_io_size
>> 8) & 0xff;
674 outbuf
[7] = min_io_size
& 0xff;
676 /* maximum transfer length */
677 outbuf
[8] = (max_io_sectors
>> 24) & 0xff;
678 outbuf
[9] = (max_io_sectors
>> 16) & 0xff;
679 outbuf
[10] = (max_io_sectors
>> 8) & 0xff;
680 outbuf
[11] = max_io_sectors
& 0xff;
682 /* optimal transfer length */
683 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
684 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
685 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
686 outbuf
[15] = opt_io_size
& 0xff;
688 /* max unmap LBA count, default is 1GB */
689 outbuf
[20] = (max_unmap_sectors
>> 24) & 0xff;
690 outbuf
[21] = (max_unmap_sectors
>> 16) & 0xff;
691 outbuf
[22] = (max_unmap_sectors
>> 8) & 0xff;
692 outbuf
[23] = max_unmap_sectors
& 0xff;
694 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header. */
700 /* optimal unmap granularity */
701 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
702 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
703 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
704 outbuf
[31] = unmap_sectors
& 0xff;
706 /* max write same size */
712 outbuf
[40] = (max_io_sectors
>> 24) & 0xff;
713 outbuf
[41] = (max_io_sectors
>> 16) & 0xff;
714 outbuf
[42] = (max_io_sectors
>> 8) & 0xff;
715 outbuf
[43] = max_io_sectors
& 0xff;
718 case 0xb2: /* thin provisioning */
722 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
723 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
731 assert(buflen
- start
<= 255);
732 outbuf
[start
- 1] = buflen
- start
;
736 /* Standard INQUIRY data */
737 if (req
->cmd
.buf
[2] != 0) {
742 buflen
= req
->cmd
.xfer
;
743 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
744 buflen
= SCSI_MAX_INQUIRY_LEN
;
747 outbuf
[0] = s
->qdev
.type
& 0x1f;
748 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
750 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
751 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
753 memset(&outbuf
[32], 0, 4);
754 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
756 * We claim conformance to SPC-3, which is required for guests
757 * to ask for modern features like READ CAPACITY(16) or the
758 * block characteristics VPD page by default. Not all of SPC-3
759 * is actually implemented, but we're good enough.
762 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
765 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
767 /* If the allocation length of CDB is too small,
768 the additional length is not adjusted */
772 /* Sync data transfer and TCQ. */
773 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
777 static inline bool media_is_dvd(SCSIDiskState
*s
)
780 if (s
->qdev
.type
!= TYPE_ROM
) {
783 if (!blk_is_inserted(s
->qdev
.conf
.blk
)) {
789 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
790 return nb_sectors
> CD_MAX_SECTORS
;
793 static inline bool media_is_cd(SCSIDiskState
*s
)
796 if (s
->qdev
.type
!= TYPE_ROM
) {
799 if (!blk_is_inserted(s
->qdev
.conf
.blk
)) {
805 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
806 return nb_sectors
<= CD_MAX_SECTORS
;
809 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
812 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
814 if (s
->qdev
.type
!= TYPE_ROM
) {
818 /* Types 1/2 are only defined for Blu-Ray. */
820 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
824 memset(outbuf
, 0, 34);
826 outbuf
[2] = 0xe; /* last session complete, disc finalized */
827 outbuf
[3] = 1; /* first track on disc */
828 outbuf
[4] = 1; /* # of sessions */
829 outbuf
[5] = 1; /* first track of last session */
830 outbuf
[6] = 1; /* last track of last session */
831 outbuf
[7] = 0x20; /* unrestricted use */
832 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
833 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
834 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
835 /* 24-31: disc bar code */
836 /* 32: disc application code */
837 /* 33: number of OPC tables */
842 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
845 static const int rds_caps_size
[5] = {
852 uint8_t media
= r
->req
.cmd
.buf
[1];
853 uint8_t layer
= r
->req
.cmd
.buf
[6];
854 uint8_t format
= r
->req
.cmd
.buf
[7];
857 if (s
->qdev
.type
!= TYPE_ROM
) {
861 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
865 if (format
!= 0xff) {
866 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
867 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
870 if (media_is_cd(s
)) {
871 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
874 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
877 size
= rds_caps_size
[format
];
878 memset(outbuf
, 0, size
);
883 /* Physical format information */
888 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
890 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
891 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
892 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
893 outbuf
[7] = 0; /* default densities */
895 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
896 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
900 case 0x01: /* DVD copyright information, all zeros */
903 case 0x03: /* BCA information - invalid field for no BCA info */
906 case 0x04: /* DVD disc manufacturing information, all zeros */
909 case 0xff: { /* List capabilities */
912 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
913 if (!rds_caps_size
[i
]) {
917 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
918 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
928 /* Size of buffer, not including 2 byte size field */
929 stw_be_p(outbuf
, size
- 2);
936 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
938 uint8_t event_code
, media_status
;
942 media_status
= MS_TRAY_OPEN
;
943 } else if (blk_is_inserted(s
->qdev
.conf
.blk
)) {
944 media_status
= MS_MEDIA_PRESENT
;
947 /* Event notification descriptor */
948 event_code
= MEC_NO_CHANGE
;
949 if (media_status
!= MS_TRAY_OPEN
) {
950 if (s
->media_event
) {
951 event_code
= MEC_NEW_MEDIA
;
952 s
->media_event
= false;
953 } else if (s
->eject_request
) {
954 event_code
= MEC_EJECT_REQUESTED
;
955 s
->eject_request
= false;
959 outbuf
[0] = event_code
;
960 outbuf
[1] = media_status
;
962 /* These fields are reserved, just clear them. */
968 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
972 uint8_t *buf
= r
->req
.cmd
.buf
;
973 uint8_t notification_class_request
= buf
[4];
974 if (s
->qdev
.type
!= TYPE_ROM
) {
977 if ((buf
[1] & 1) == 0) {
983 outbuf
[0] = outbuf
[1] = 0;
984 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
985 if (notification_class_request
& (1 << GESN_MEDIA
)) {
986 outbuf
[2] = GESN_MEDIA
;
987 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
991 stw_be_p(outbuf
, size
- 4);
995 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
999 if (s
->qdev
.type
!= TYPE_ROM
) {
1003 if (media_is_dvd(s
)) {
1004 current
= MMC_PROFILE_DVD_ROM
;
1005 } else if (media_is_cd(s
)) {
1006 current
= MMC_PROFILE_CD_ROM
;
1008 current
= MMC_PROFILE_NONE
;
1011 memset(outbuf
, 0, 40);
1012 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
1013 stw_be_p(&outbuf
[6], current
);
1014 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
1015 outbuf
[10] = 0x03; /* persistent, current */
1016 outbuf
[11] = 8; /* two profiles */
1017 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
1018 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
1019 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
1020 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
1021 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
1022 stw_be_p(&outbuf
[20], 1);
1023 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
1025 stl_be_p(&outbuf
[24], 1); /* SCSI */
1026 outbuf
[28] = 1; /* DBE = 1, mandatory */
1027 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
1028 stw_be_p(&outbuf
[32], 3);
1029 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
1031 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1032 /* TODO: Random readable, CD read, DVD read, drive serial number,
1037 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
1039 if (s
->qdev
.type
!= TYPE_ROM
) {
1042 memset(outbuf
, 0, 8);
1043 outbuf
[5] = 1; /* CD-ROM */
1047 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
1050 static const int mode_sense_valid
[0x3f] = {
1051 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
1052 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
1053 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1054 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1055 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
1056 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
1059 uint8_t *p
= *p_outbuf
+ 2;
1062 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
1067 * If Changeable Values are requested, a mask denoting those mode parameters
1068 * that are changeable shall be returned. As we currently don't support
1069 * parameter changes via MODE_SELECT all bits are returned set to zero.
1070 * The buffer was already menset to zero by the caller of this function.
1072 * The offsets here are off by two compared to the descriptions in the
1073 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1074 * but it is done so that offsets are consistent within our implementation
1075 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1076 * 2-byte and 4-byte headers.
1079 case MODE_PAGE_HD_GEOMETRY
:
1081 if (page_control
== 1) { /* Changeable Values */
1084 /* if a geometry hint is available, use it */
1085 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1086 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1087 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
1088 p
[3] = s
->qdev
.conf
.heads
& 0xff;
1089 /* Write precomp start cylinder, disabled */
1090 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1091 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1092 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
1093 /* Reduced current start cylinder, disabled */
1094 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1095 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1096 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1097 /* Device step rate [ns], 200ns */
1100 /* Landing zone cylinder */
1104 /* Medium rotation rate [rpm], 5400 rpm */
1105 p
[18] = (5400 >> 8) & 0xff;
1106 p
[19] = 5400 & 0xff;
1109 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1111 if (page_control
== 1) { /* Changeable Values */
1114 /* Transfer rate [kbit/s], 5Mbit/s */
1117 /* if a geometry hint is available, use it */
1118 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1119 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1120 p
[4] = s
->qdev
.blocksize
>> 8;
1121 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1122 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1123 /* Write precomp start cylinder, disabled */
1124 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1125 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1126 /* Reduced current start cylinder, disabled */
1127 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1128 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1129 /* Device step rate [100us], 100us */
1132 /* Device step pulse width [us], 1us */
1134 /* Device head settle delay [100us], 100us */
1137 /* Motor on delay [0.1s], 0.1s */
1139 /* Motor off delay [0.1s], 0.1s */
1141 /* Medium rotation rate [rpm], 5400 rpm */
1142 p
[26] = (5400 >> 8) & 0xff;
1143 p
[27] = 5400 & 0xff;
1146 case MODE_PAGE_CACHING
:
1148 if (page_control
== 1 || /* Changeable Values */
1149 blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1154 case MODE_PAGE_R_W_ERROR
:
1156 if (page_control
== 1) { /* Changeable Values */
1159 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1160 if (s
->qdev
.type
== TYPE_ROM
) {
1161 p
[1] = 0x20; /* Read Retry Count */
1165 case MODE_PAGE_AUDIO_CTL
:
1169 case MODE_PAGE_CAPABILITIES
:
1171 if (page_control
== 1) { /* Changeable Values */
1175 p
[0] = 0x3b; /* CD-R & CD-RW read */
1176 p
[1] = 0; /* Writing not supported */
1177 p
[2] = 0x7f; /* Audio, composite, digital out,
1178 mode 2 form 1&2, multi session */
1179 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1180 RW corrected, C2 errors, ISRC,
1182 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1183 /* Locking supported, jumper present, eject, tray */
1184 p
[5] = 0; /* no volume & mute control, no
1186 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1187 p
[7] = (50 * 176) & 0xff;
1188 p
[8] = 2 >> 8; /* Two volume levels */
1190 p
[10] = 2048 >> 8; /* 2M buffer */
1191 p
[11] = 2048 & 0xff;
1192 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1193 p
[13] = (16 * 176) & 0xff;
1194 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1195 p
[17] = (16 * 176) & 0xff;
1196 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1197 p
[19] = (16 * 176) & 0xff;
1204 assert(length
< 256);
1205 (*p_outbuf
)[0] = page
;
1206 (*p_outbuf
)[1] = length
;
1207 *p_outbuf
+= length
+ 2;
1211 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1213 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1214 uint64_t nb_sectors
;
1216 int page
, buflen
, ret
, page_control
;
1218 uint8_t dev_specific_param
;
1220 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1221 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1222 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1223 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1224 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
1225 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1228 if (s
->qdev
.type
== TYPE_DISK
) {
1229 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1230 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1231 dev_specific_param
|= 0x80; /* Readonly. */
1234 /* MMC prescribes that CD/DVD drives have no block descriptors,
1235 * and defines no device-specific parameter. */
1236 dev_specific_param
= 0x00;
1240 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1241 p
[1] = 0; /* Default media type. */
1242 p
[2] = dev_specific_param
;
1243 p
[3] = 0; /* Block descriptor length. */
1245 } else { /* MODE_SENSE_10 */
1246 p
[2] = 0; /* Default media type. */
1247 p
[3] = dev_specific_param
;
1248 p
[6] = p
[7] = 0; /* Block descriptor length. */
1252 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1253 if (!dbd
&& nb_sectors
) {
1254 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1255 outbuf
[3] = 8; /* Block descriptor length */
1256 } else { /* MODE_SENSE_10 */
1257 outbuf
[7] = 8; /* Block descriptor length */
1259 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1260 if (nb_sectors
> 0xffffff) {
1263 p
[0] = 0; /* media density code */
1264 p
[1] = (nb_sectors
>> 16) & 0xff;
1265 p
[2] = (nb_sectors
>> 8) & 0xff;
1266 p
[3] = nb_sectors
& 0xff;
1267 p
[4] = 0; /* reserved */
1268 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1269 p
[6] = s
->qdev
.blocksize
>> 8;
1274 if (page_control
== 3) {
1276 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1281 for (page
= 0; page
<= 0x3e; page
++) {
1282 mode_sense_page(s
, page
, &p
, page_control
);
1285 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1291 buflen
= p
- outbuf
;
1293 * The mode data length field specifies the length in bytes of the
1294 * following data that is available to be transferred. The mode data
1295 * length does not include itself.
1297 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1298 outbuf
[0] = buflen
- 1;
1299 } else { /* MODE_SENSE_10 */
1300 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1301 outbuf
[1] = (buflen
- 2) & 0xff;
1306 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1308 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1309 int start_track
, format
, msf
, toclen
;
1310 uint64_t nb_sectors
;
1312 msf
= req
->cmd
.buf
[1] & 2;
1313 format
= req
->cmd
.buf
[2] & 0xf;
1314 start_track
= req
->cmd
.buf
[6];
1315 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1316 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1317 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1320 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1323 /* multi session : only a single session defined */
1325 memset(outbuf
, 0, 12);
1331 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1339 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1341 SCSIRequest
*req
= &r
->req
;
1342 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1343 bool start
= req
->cmd
.buf
[4] & 1;
1344 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1345 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1348 /* eject/load only happens for power condition == 0 */
1352 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1353 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1354 scsi_check_condition(r
,
1355 blk_is_inserted(s
->qdev
.conf
.blk
)
1356 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1357 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1361 if (s
->tray_open
!= !start
) {
1362 blk_eject(s
->qdev
.conf
.blk
, !start
);
1363 s
->tray_open
= !start
;
1369 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1371 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1372 int buflen
= r
->iov
.iov_len
;
1375 DPRINTF("Read buf_len=%d\n", buflen
);
1378 scsi_req_data(&r
->req
, buflen
);
1382 /* This also clears the sense buffer for REQUEST SENSE. */
1383 scsi_req_complete(&r
->req
, GOOD
);
1386 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1387 uint8_t *inbuf
, int inlen
)
1389 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1390 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1392 int len
, expected_len
, changeable_len
, i
;
1394 /* The input buffer does not include the page header, so it is
1397 expected_len
= inlen
+ 2;
1398 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1403 memset(mode_current
, 0, inlen
+ 2);
1404 len
= mode_sense_page(s
, page
, &p
, 0);
1405 if (len
< 0 || len
!= expected_len
) {
1409 p
= mode_changeable
;
1410 memset(mode_changeable
, 0, inlen
+ 2);
1411 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1412 assert(changeable_len
== len
);
1414 /* Check that unchangeable bits are the same as what MODE SENSE
1417 for (i
= 2; i
< len
; i
++) {
1418 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1425 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1428 case MODE_PAGE_CACHING
:
1429 blk_set_enable_write_cache(s
->qdev
.conf
.blk
, (p
[0] & 4) != 0);
1437 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1439 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1442 int page
, subpage
, page_len
;
1444 /* Parse both possible formats for the mode page headers. */
1448 goto invalid_param_len
;
1451 page_len
= lduw_be_p(&p
[2]);
1456 goto invalid_param_len
;
1467 if (page_len
> len
) {
1468 goto invalid_param_len
;
1472 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1476 scsi_disk_apply_mode_select(s
, page
, p
);
1485 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1489 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1493 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1495 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1497 int cmd
= r
->req
.cmd
.buf
[0];
1498 int len
= r
->req
.cmd
.xfer
;
1499 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1503 /* We only support PF=1, SP=0. */
1504 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1508 if (len
< hdr_len
) {
1509 goto invalid_param_len
;
1512 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1516 goto invalid_param_len
;
1518 if (bd_len
!= 0 && bd_len
!= 8) {
1525 /* Ensure no change is made if there is an error! */
1526 for (pass
= 0; pass
< 2; pass
++) {
1527 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1532 if (!blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1533 /* The request is used as the AIO opaque value, so add a ref. */
1534 scsi_req_ref(&r
->req
);
1535 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
1537 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
1541 scsi_req_complete(&r
->req
, GOOD
);
1545 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1549 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1553 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1556 static inline bool check_lba_range(SCSIDiskState
*s
,
1557 uint64_t sector_num
, uint32_t nb_sectors
)
1560 * The first line tests that no overflow happens when computing the last
1561 * sector. The second line tests that the last accessed sector is in
1564 * Careful, the computations should not underflow for nb_sectors == 0,
1565 * and a 0-block read to the first LBA beyond the end of device is
1568 return (sector_num
<= sector_num
+ nb_sectors
&&
1569 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1572 typedef struct UnmapCBData
{
1578 static void scsi_unmap_complete(void *opaque
, int ret
);
1580 static void scsi_unmap_complete_noio(UnmapCBData
*data
, int ret
)
1582 SCSIDiskReq
*r
= data
->r
;
1583 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1584 uint64_t sector_num
;
1585 uint32_t nb_sectors
;
1587 assert(r
->req
.aiocb
== NULL
);
1589 if (r
->req
.io_canceled
) {
1590 scsi_req_cancel_complete(&r
->req
);
1595 if (scsi_handle_rw_error(r
, -ret
)) {
1600 if (data
->count
> 0) {
1601 sector_num
= ldq_be_p(&data
->inbuf
[0]);
1602 nb_sectors
= ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
;
1603 if (!check_lba_range(s
, sector_num
, nb_sectors
)) {
1604 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1608 r
->req
.aiocb
= blk_aio_discard(s
->qdev
.conf
.blk
,
1609 sector_num
* (s
->qdev
.blocksize
/ 512),
1610 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1611 scsi_unmap_complete
, data
);
1617 scsi_req_complete(&r
->req
, GOOD
);
1620 scsi_req_unref(&r
->req
);
1624 static void scsi_unmap_complete(void *opaque
, int ret
)
1626 UnmapCBData
*data
= opaque
;
1627 SCSIDiskReq
*r
= data
->r
;
1629 assert(r
->req
.aiocb
!= NULL
);
1630 r
->req
.aiocb
= NULL
;
1632 scsi_unmap_complete_noio(data
, ret
);
1635 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1637 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1639 int len
= r
->req
.cmd
.xfer
;
1642 /* Reject ANCHOR=1. */
1643 if (r
->req
.cmd
.buf
[1] & 0x1) {
1648 goto invalid_param_len
;
1650 if (len
< lduw_be_p(&p
[0]) + 2) {
1651 goto invalid_param_len
;
1653 if (len
< lduw_be_p(&p
[2]) + 8) {
1654 goto invalid_param_len
;
1656 if (lduw_be_p(&p
[2]) & 15) {
1657 goto invalid_param_len
;
1660 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1661 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1665 data
= g_new0(UnmapCBData
, 1);
1667 data
->inbuf
= &p
[8];
1668 data
->count
= lduw_be_p(&p
[2]) >> 4;
1670 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1671 scsi_req_ref(&r
->req
);
1672 scsi_unmap_complete_noio(data
, 0);
1676 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1680 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1683 typedef struct WriteSameCBData
{
1691 static void scsi_write_same_complete(void *opaque
, int ret
)
1693 WriteSameCBData
*data
= opaque
;
1694 SCSIDiskReq
*r
= data
->r
;
1695 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1697 assert(r
->req
.aiocb
!= NULL
);
1698 r
->req
.aiocb
= NULL
;
1699 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
1700 if (r
->req
.io_canceled
) {
1701 scsi_req_cancel_complete(&r
->req
);
1706 if (scsi_handle_rw_error(r
, -ret
)) {
1711 data
->nb_sectors
-= data
->iov
.iov_len
/ 512;
1712 data
->sector
+= data
->iov
.iov_len
/ 512;
1713 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, data
->iov
.iov_len
);
1714 if (data
->iov
.iov_len
) {
1715 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1716 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1717 /* blk_aio_write doesn't like the qiov size being different from
1718 * nb_sectors, make sure they match.
1720 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1721 r
->req
.aiocb
= blk_aio_writev(s
->qdev
.conf
.blk
, data
->sector
,
1722 &data
->qiov
, data
->iov
.iov_len
/ 512,
1723 scsi_write_same_complete
, data
);
1727 scsi_req_complete(&r
->req
, GOOD
);
1730 scsi_req_unref(&r
->req
);
1731 qemu_vfree(data
->iov
.iov_base
);
1735 static void scsi_disk_emulate_write_same(SCSIDiskReq
*r
, uint8_t *inbuf
)
1737 SCSIRequest
*req
= &r
->req
;
1738 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1739 uint32_t nb_sectors
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
1740 WriteSameCBData
*data
;
1744 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1745 if (nb_sectors
== 0 || (req
->cmd
.buf
[1] & 0x16)) {
1746 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1750 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1751 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1754 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1755 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1759 if (buffer_is_zero(inbuf
, s
->qdev
.blocksize
)) {
1760 int flags
= (req
->cmd
.buf
[1] & 0x8) ? BDRV_REQ_MAY_UNMAP
: 0;
1762 /* The request is used as the AIO opaque value, so add a ref. */
1763 scsi_req_ref(&r
->req
);
1764 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1765 nb_sectors
* s
->qdev
.blocksize
,
1767 r
->req
.aiocb
= blk_aio_write_zeroes(s
->qdev
.conf
.blk
,
1768 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1769 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1770 flags
, scsi_aio_complete
, r
);
1774 data
= g_new0(WriteSameCBData
, 1);
1776 data
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1777 data
->nb_sectors
= nb_sectors
* (s
->qdev
.blocksize
/ 512);
1778 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, SCSI_WRITE_SAME_MAX
);
1779 data
->iov
.iov_base
= buf
= blk_blockalign(s
->qdev
.conf
.blk
,
1781 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1783 for (i
= 0; i
< data
->iov
.iov_len
; i
+= s
->qdev
.blocksize
) {
1784 memcpy(&buf
[i
], inbuf
, s
->qdev
.blocksize
);
1787 scsi_req_ref(&r
->req
);
1788 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1789 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1790 r
->req
.aiocb
= blk_aio_writev(s
->qdev
.conf
.blk
, data
->sector
,
1791 &data
->qiov
, data
->iov
.iov_len
/ 512,
1792 scsi_write_same_complete
, data
);
1795 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1797 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1799 if (r
->iov
.iov_len
) {
1800 int buflen
= r
->iov
.iov_len
;
1801 DPRINTF("Write buf_len=%d\n", buflen
);
1803 scsi_req_data(&r
->req
, buflen
);
1807 switch (req
->cmd
.buf
[0]) {
1809 case MODE_SELECT_10
:
1810 /* This also clears the sense buffer for REQUEST SENSE. */
1811 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1815 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1821 if (r
->req
.status
== -1) {
1822 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1828 scsi_disk_emulate_write_same(r
, r
->iov
.iov_base
);
1836 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1838 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1839 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1840 uint64_t nb_sectors
;
1844 switch (req
->cmd
.buf
[0]) {
1853 case ALLOW_MEDIUM_REMOVAL
:
1854 case GET_CONFIGURATION
:
1855 case GET_EVENT_STATUS_NOTIFICATION
:
1856 case MECHANISM_STATUS
:
1861 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
1862 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1869 * FIXME: we shouldn't return anything bigger than 4k, but the code
1870 * requires the buffer to be as big as req->cmd.xfer in several
1871 * places. So, do not allow CDBs with a very large ALLOCATION
1872 * LENGTH. The real fix would be to modify scsi_read_data and
1873 * dma_buf_read, so that they return data beyond the buflen
1876 if (req
->cmd
.xfer
> 65536) {
1877 goto illegal_request
;
1879 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1881 if (!r
->iov
.iov_base
) {
1882 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
1885 buflen
= req
->cmd
.xfer
;
1886 outbuf
= r
->iov
.iov_base
;
1887 memset(outbuf
, 0, r
->buflen
);
1888 switch (req
->cmd
.buf
[0]) {
1889 case TEST_UNIT_READY
:
1890 assert(!s
->tray_open
&& blk_is_inserted(s
->qdev
.conf
.blk
));
1893 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1895 goto illegal_request
;
1900 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1902 goto illegal_request
;
1906 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1908 goto illegal_request
;
1912 if (req
->cmd
.buf
[1] & 1) {
1913 goto illegal_request
;
1917 if (req
->cmd
.buf
[1] & 3) {
1918 goto illegal_request
;
1922 if (req
->cmd
.buf
[1] & 1) {
1923 goto illegal_request
;
1927 if (req
->cmd
.buf
[1] & 3) {
1928 goto illegal_request
;
1932 if (scsi_disk_emulate_start_stop(r
) < 0) {
1936 case ALLOW_MEDIUM_REMOVAL
:
1937 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1938 blk_lock_medium(s
->qdev
.conf
.blk
, req
->cmd
.buf
[4] & 1);
1940 case READ_CAPACITY_10
:
1941 /* The normal LEN field for this command is zero. */
1942 memset(outbuf
, 0, 8);
1943 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1945 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1948 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1949 goto illegal_request
;
1951 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1952 /* Returned value is the address of the last sector. */
1954 /* Remember the new size for read/write sanity checking. */
1955 s
->qdev
.max_lba
= nb_sectors
;
1956 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1957 if (nb_sectors
> UINT32_MAX
) {
1958 nb_sectors
= UINT32_MAX
;
1960 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1961 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1962 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1963 outbuf
[3] = nb_sectors
& 0xff;
1966 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1970 /* Just return "NO SENSE". */
1971 buflen
= scsi_build_sense(NULL
, 0, outbuf
, r
->buflen
,
1972 (req
->cmd
.buf
[1] & 1) == 0);
1974 goto illegal_request
;
1977 case MECHANISM_STATUS
:
1978 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1980 goto illegal_request
;
1983 case GET_CONFIGURATION
:
1984 buflen
= scsi_get_configuration(s
, outbuf
);
1986 goto illegal_request
;
1989 case GET_EVENT_STATUS_NOTIFICATION
:
1990 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1992 goto illegal_request
;
1995 case READ_DISC_INFORMATION
:
1996 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
1998 goto illegal_request
;
2001 case READ_DVD_STRUCTURE
:
2002 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
2004 goto illegal_request
;
2007 case SERVICE_ACTION_IN_16
:
2008 /* Service Action In subcommands. */
2009 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
2010 DPRINTF("SAI READ CAPACITY(16)\n");
2011 memset(outbuf
, 0, req
->cmd
.xfer
);
2012 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2014 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
2017 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
2018 goto illegal_request
;
2020 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2021 /* Returned value is the address of the last sector. */
2023 /* Remember the new size for read/write sanity checking. */
2024 s
->qdev
.max_lba
= nb_sectors
;
2025 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
2026 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
2027 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
2028 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
2029 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
2030 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
2031 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
2032 outbuf
[7] = nb_sectors
& 0xff;
2035 outbuf
[10] = s
->qdev
.blocksize
>> 8;
2038 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
2040 /* set TPE bit if the format supports discard */
2041 if (s
->qdev
.conf
.discard_granularity
) {
2045 /* Protection, exponent and lowest lba field left blank. */
2048 DPRINTF("Unsupported Service Action In\n");
2049 goto illegal_request
;
2050 case SYNCHRONIZE_CACHE
:
2051 /* The request is used as the AIO opaque value, so add a ref. */
2052 scsi_req_ref(&r
->req
);
2053 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
2055 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
2058 DPRINTF("Seek(10) (sector %" PRId64
")\n", r
->req
.cmd
.lba
);
2059 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
2064 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2066 case MODE_SELECT_10
:
2067 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2070 DPRINTF("Unmap (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2075 DPRINTF("Verify (bytchk %d)\n", (req
->cmd
.buf
[1] >> 1) & 3);
2076 if (req
->cmd
.buf
[1] & 6) {
2077 goto illegal_request
;
2082 DPRINTF("WRITE SAME %d (len %lu)\n",
2083 req
->cmd
.buf
[0] == WRITE_SAME_10
? 10 : 16,
2084 (long)r
->req
.cmd
.xfer
);
2087 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf
[0],
2088 scsi_command_name(buf
[0]));
2089 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
2092 assert(!r
->req
.aiocb
);
2093 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
2094 if (r
->iov
.iov_len
== 0) {
2095 scsi_req_complete(&r
->req
, GOOD
);
2097 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2098 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
2099 return -r
->iov
.iov_len
;
2101 return r
->iov
.iov_len
;
2105 if (r
->req
.status
== -1) {
2106 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2111 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2115 /* Execute a scsi command. Returns the length of the data expected by the
2116 command. This will be Positive for data transfers from the device
2117 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2118 and zero if the command does not transfer any data. */
2120 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2122 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
2123 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
2129 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
2130 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
2134 len
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
2140 DPRINTF("Read (sector %" PRId64
", count %u)\n", r
->req
.cmd
.lba
, len
);
2141 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2142 goto illegal_request
;
2144 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2147 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2148 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2154 case WRITE_VERIFY_10
:
2155 case WRITE_VERIFY_12
:
2156 case WRITE_VERIFY_16
:
2157 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
2158 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
2161 DPRINTF("Write %s(sector %" PRId64
", count %u)\n",
2162 (command
& 0xe) == 0xe ? "And Verify " : "",
2163 r
->req
.cmd
.lba
, len
);
2164 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2165 goto illegal_request
;
2167 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2170 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2171 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2176 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2179 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2182 if (r
->sector_count
== 0) {
2183 scsi_req_complete(&r
->req
, GOOD
);
2185 assert(r
->iov
.iov_len
== 0);
2186 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2187 return -r
->sector_count
* 512;
2189 return r
->sector_count
* 512;
2193 static void scsi_disk_reset(DeviceState
*dev
)
2195 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
2196 uint64_t nb_sectors
;
2198 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
2200 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2201 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2205 s
->qdev
.max_lba
= nb_sectors
;
2206 /* reset tray statuses */
2211 static void scsi_disk_resize_cb(void *opaque
)
2213 SCSIDiskState
*s
= opaque
;
2215 /* SPC lists this sense code as available only for
2216 * direct-access devices.
2218 if (s
->qdev
.type
== TYPE_DISK
) {
2219 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
2223 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
2225 SCSIDiskState
*s
= opaque
;
2228 * When a CD gets changed, we have to report an ejected state and
2229 * then a loaded state to guests so that they detect tray
2230 * open/close and media change events. Guests that do not use
2231 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2232 * states rely on this behavior.
2234 * media_changed governs the state machine used for unit attention
2235 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2237 s
->media_changed
= load
;
2238 s
->tray_open
= !load
;
2239 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
2240 s
->media_event
= true;
2241 s
->eject_request
= false;
2244 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2246 SCSIDiskState
*s
= opaque
;
2248 s
->eject_request
= true;
2250 s
->tray_locked
= false;
2254 static bool scsi_cd_is_tray_open(void *opaque
)
2256 return ((SCSIDiskState
*)opaque
)->tray_open
;
2259 static bool scsi_cd_is_medium_locked(void *opaque
)
2261 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2264 static const BlockDevOps scsi_disk_removable_block_ops
= {
2265 .change_media_cb
= scsi_cd_change_media_cb
,
2266 .eject_request_cb
= scsi_cd_eject_request_cb
,
2267 .is_tray_open
= scsi_cd_is_tray_open
,
2268 .is_medium_locked
= scsi_cd_is_medium_locked
,
2270 .resize_cb
= scsi_disk_resize_cb
,
2273 static const BlockDevOps scsi_disk_block_ops
= {
2274 .resize_cb
= scsi_disk_resize_cb
,
2277 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2279 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2280 if (s
->media_changed
) {
2281 s
->media_changed
= false;
2282 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2286 static void scsi_realize(SCSIDevice
*dev
, Error
**errp
)
2288 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2291 if (!s
->qdev
.conf
.blk
) {
2292 error_setg(errp
, "drive property not set");
2296 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2297 !blk_is_inserted(s
->qdev
.conf
.blk
)) {
2298 error_setg(errp
, "Device needs media, but drive is empty");
2302 blkconf_serial(&s
->qdev
.conf
, &s
->serial
);
2303 blkconf_blocksizes(&s
->qdev
.conf
);
2304 if (dev
->type
== TYPE_DISK
) {
2305 blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255, &err
);
2307 error_propagate(errp
, err
);
2312 if (s
->qdev
.conf
.discard_granularity
== -1) {
2313 s
->qdev
.conf
.discard_granularity
=
2314 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2318 s
->version
= g_strdup(qemu_get_version());
2321 s
->vendor
= g_strdup("QEMU");
2324 if (blk_is_sg(s
->qdev
.conf
.blk
)) {
2325 error_setg(errp
, "unwanted /dev/sg*");
2329 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2330 !(s
->features
& (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
))) {
2331 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_removable_block_ops
, s
);
2333 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_block_ops
, s
);
2335 blk_set_guest_block_size(s
->qdev
.conf
.blk
, s
->qdev
.blocksize
);
2337 blk_iostatus_enable(s
->qdev
.conf
.blk
);
2340 static void scsi_hd_realize(SCSIDevice
*dev
, Error
**errp
)
2342 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2343 /* can happen for devices without drive. The error message for missing
2344 * backend will be issued in scsi_realize
2346 if (s
->qdev
.conf
.blk
) {
2347 blkconf_blocksizes(&s
->qdev
.conf
);
2349 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2350 s
->qdev
.type
= TYPE_DISK
;
2352 s
->product
= g_strdup("QEMU HARDDISK");
2354 scsi_realize(&s
->qdev
, errp
);
2357 static void scsi_cd_realize(SCSIDevice
*dev
, Error
**errp
)
2359 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2360 s
->qdev
.blocksize
= 2048;
2361 s
->qdev
.type
= TYPE_ROM
;
2362 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2364 s
->product
= g_strdup("QEMU CD-ROM");
2366 scsi_realize(&s
->qdev
, errp
);
2369 static void scsi_disk_realize(SCSIDevice
*dev
, Error
**errp
)
2372 Error
*local_err
= NULL
;
2374 if (!dev
->conf
.blk
) {
2375 scsi_realize(dev
, &local_err
);
2377 error_propagate(errp
, local_err
);
2381 dinfo
= blk_legacy_dinfo(dev
->conf
.blk
);
2382 if (dinfo
&& dinfo
->media_cd
) {
2383 scsi_cd_realize(dev
, errp
);
2385 scsi_hd_realize(dev
, errp
);
2389 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2390 .size
= sizeof(SCSIDiskReq
),
2391 .free_req
= scsi_free_request
,
2392 .send_command
= scsi_disk_emulate_command
,
2393 .read_data
= scsi_disk_emulate_read_data
,
2394 .write_data
= scsi_disk_emulate_write_data
,
2395 .get_buf
= scsi_get_buf
,
2398 static const SCSIReqOps scsi_disk_dma_reqops
= {
2399 .size
= sizeof(SCSIDiskReq
),
2400 .free_req
= scsi_free_request
,
2401 .send_command
= scsi_disk_dma_command
,
2402 .read_data
= scsi_read_data
,
2403 .write_data
= scsi_write_data
,
2404 .get_buf
= scsi_get_buf
,
2405 .load_request
= scsi_disk_load_request
,
2406 .save_request
= scsi_disk_save_request
,
2409 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2410 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2411 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2412 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2413 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2414 [START_STOP
] = &scsi_disk_emulate_reqops
,
2415 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2416 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2417 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2418 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2419 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2420 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2421 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2422 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2423 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2424 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2425 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2426 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2427 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2428 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2429 [UNMAP
] = &scsi_disk_emulate_reqops
,
2430 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2431 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2432 [VERIFY_10
] = &scsi_disk_emulate_reqops
,
2433 [VERIFY_12
] = &scsi_disk_emulate_reqops
,
2434 [VERIFY_16
] = &scsi_disk_emulate_reqops
,
2436 [READ_6
] = &scsi_disk_dma_reqops
,
2437 [READ_10
] = &scsi_disk_dma_reqops
,
2438 [READ_12
] = &scsi_disk_dma_reqops
,
2439 [READ_16
] = &scsi_disk_dma_reqops
,
2440 [WRITE_6
] = &scsi_disk_dma_reqops
,
2441 [WRITE_10
] = &scsi_disk_dma_reqops
,
2442 [WRITE_12
] = &scsi_disk_dma_reqops
,
2443 [WRITE_16
] = &scsi_disk_dma_reqops
,
2444 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2445 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2446 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2449 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2450 uint8_t *buf
, void *hba_private
)
2452 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2454 const SCSIReqOps
*ops
;
2458 ops
= scsi_disk_reqops_dispatch
[command
];
2460 ops
= &scsi_disk_emulate_reqops
;
2462 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2465 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
2468 for (i
= 1; i
< scsi_cdb_length(buf
); i
++) {
2469 printf(" 0x%02x", buf
[i
]);
2479 static int get_device_type(SCSIDiskState
*s
)
2483 uint8_t sensebuf
[8];
2484 sg_io_hdr_t io_header
;
2487 memset(cmd
, 0, sizeof(cmd
));
2488 memset(buf
, 0, sizeof(buf
));
2490 cmd
[4] = sizeof(buf
);
2492 memset(&io_header
, 0, sizeof(io_header
));
2493 io_header
.interface_id
= 'S';
2494 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2495 io_header
.dxfer_len
= sizeof(buf
);
2496 io_header
.dxferp
= buf
;
2497 io_header
.cmdp
= cmd
;
2498 io_header
.cmd_len
= sizeof(cmd
);
2499 io_header
.mx_sb_len
= sizeof(sensebuf
);
2500 io_header
.sbp
= sensebuf
;
2501 io_header
.timeout
= 6000; /* XXX */
2503 ret
= blk_ioctl(s
->qdev
.conf
.blk
, SG_IO
, &io_header
);
2504 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
2507 s
->qdev
.type
= buf
[0];
2508 if (buf
[1] & 0x80) {
2509 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2514 static void scsi_block_realize(SCSIDevice
*dev
, Error
**errp
)
2516 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2520 if (!s
->qdev
.conf
.blk
) {
2521 error_setg(errp
, "drive property not set");
2525 /* check we are using a driver managing SG_IO (version 3 and after) */
2526 rc
= blk_ioctl(s
->qdev
.conf
.blk
, SG_GET_VERSION_NUM
, &sg_version
);
2528 error_setg(errp
, "cannot get SG_IO version number: %s. "
2529 "Is this a SCSI device?",
2533 if (sg_version
< 30000) {
2534 error_setg(errp
, "scsi generic interface too old");
2538 /* get device type from INQUIRY data */
2539 rc
= get_device_type(s
);
2541 error_setg(errp
, "INQUIRY failed");
2545 /* Make a guess for the block size, we'll fix it when the guest sends.
2546 * READ CAPACITY. If they don't, they likely would assume these sizes
2547 * anyway. (TODO: check in /sys).
2549 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2550 s
->qdev
.blocksize
= 2048;
2552 s
->qdev
.blocksize
= 512;
2555 /* Makes the scsi-block device not removable by using HMP and QMP eject
2558 s
->features
|= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
);
2560 scsi_realize(&s
->qdev
, errp
);
2563 static bool scsi_block_is_passthrough(SCSIDiskState
*s
, uint8_t *buf
)
2577 case WRITE_VERIFY_10
:
2578 case WRITE_VERIFY_12
:
2579 case WRITE_VERIFY_16
:
2580 /* If we are not using O_DIRECT, we might read stale data from the
2581 * host cache if writes were made using other commands than these
2582 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2583 * O_DIRECT everything must go through SG_IO.
2585 if (!(blk_get_flags(s
->qdev
.conf
.blk
) & BDRV_O_NOCACHE
)) {
2589 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2590 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2591 * And once you do these writes, reading from the block device is
2592 * unreliable, too. It is even possible that reads deliver random data
2593 * from the host page cache (this is probably a Linux bug).
2595 * We might use scsi_disk_dma_reqops as long as no writing commands are
2596 * seen, but performance usually isn't paramount on optical media. So,
2597 * just make scsi-block operate the same as scsi-generic for them.
2599 if (s
->qdev
.type
!= TYPE_ROM
) {
2612 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2613 uint32_t lun
, uint8_t *buf
,
2616 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2618 if (scsi_block_is_passthrough(s
, buf
)) {
2619 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2622 return scsi_req_alloc(&scsi_disk_dma_reqops
, &s
->qdev
, tag
, lun
,
2627 static int scsi_block_parse_cdb(SCSIDevice
*d
, SCSICommand
*cmd
,
2628 uint8_t *buf
, void *hba_private
)
2630 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2632 if (scsi_block_is_passthrough(s
, buf
)) {
2633 return scsi_bus_parse_cdb(&s
->qdev
, cmd
, buf
, hba_private
);
2635 return scsi_req_parse_cdb(&s
->qdev
, cmd
, buf
);
2641 #define DEFINE_SCSI_DISK_PROPERTIES() \
2642 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2643 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2644 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2645 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2646 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2648 static Property scsi_hd_properties
[] = {
2649 DEFINE_SCSI_DISK_PROPERTIES(),
2650 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2651 SCSI_DISK_F_REMOVABLE
, false),
2652 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2653 SCSI_DISK_F_DPOFUA
, false),
2654 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2655 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2656 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2657 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2658 DEFAULT_MAX_UNMAP_SIZE
),
2659 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2660 DEFAULT_MAX_IO_SIZE
),
2661 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
2662 DEFINE_PROP_END_OF_LIST(),
2665 static const VMStateDescription vmstate_scsi_disk_state
= {
2666 .name
= "scsi-disk",
2668 .minimum_version_id
= 1,
2669 .fields
= (VMStateField
[]) {
2670 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
2671 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
2672 VMSTATE_BOOL(media_event
, SCSIDiskState
),
2673 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
2674 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
2675 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
2676 VMSTATE_END_OF_LIST()
2680 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
2682 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2683 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2685 sc
->realize
= scsi_hd_realize
;
2686 sc
->alloc_req
= scsi_new_request
;
2687 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2688 dc
->fw_name
= "disk";
2689 dc
->desc
= "virtual SCSI disk";
2690 dc
->reset
= scsi_disk_reset
;
2691 dc
->props
= scsi_hd_properties
;
2692 dc
->vmsd
= &vmstate_scsi_disk_state
;
2695 static const TypeInfo scsi_hd_info
= {
2697 .parent
= TYPE_SCSI_DEVICE
,
2698 .instance_size
= sizeof(SCSIDiskState
),
2699 .class_init
= scsi_hd_class_initfn
,
2702 static Property scsi_cd_properties
[] = {
2703 DEFINE_SCSI_DISK_PROPERTIES(),
2704 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2705 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2706 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2707 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2708 DEFAULT_MAX_IO_SIZE
),
2709 DEFINE_PROP_END_OF_LIST(),
2712 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
2714 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2715 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2717 sc
->realize
= scsi_cd_realize
;
2718 sc
->alloc_req
= scsi_new_request
;
2719 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2720 dc
->fw_name
= "disk";
2721 dc
->desc
= "virtual SCSI CD-ROM";
2722 dc
->reset
= scsi_disk_reset
;
2723 dc
->props
= scsi_cd_properties
;
2724 dc
->vmsd
= &vmstate_scsi_disk_state
;
2727 static const TypeInfo scsi_cd_info
= {
2729 .parent
= TYPE_SCSI_DEVICE
,
2730 .instance_size
= sizeof(SCSIDiskState
),
2731 .class_init
= scsi_cd_class_initfn
,
2735 static Property scsi_block_properties
[] = {
2736 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.blk
),
2737 DEFINE_PROP_END_OF_LIST(),
2740 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
2742 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2743 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2745 sc
->realize
= scsi_block_realize
;
2746 sc
->alloc_req
= scsi_block_new_request
;
2747 sc
->parse_cdb
= scsi_block_parse_cdb
;
2748 dc
->fw_name
= "disk";
2749 dc
->desc
= "SCSI block device passthrough";
2750 dc
->reset
= scsi_disk_reset
;
2751 dc
->props
= scsi_block_properties
;
2752 dc
->vmsd
= &vmstate_scsi_disk_state
;
2755 static const TypeInfo scsi_block_info
= {
2756 .name
= "scsi-block",
2757 .parent
= TYPE_SCSI_DEVICE
,
2758 .instance_size
= sizeof(SCSIDiskState
),
2759 .class_init
= scsi_block_class_initfn
,
2763 static Property scsi_disk_properties
[] = {
2764 DEFINE_SCSI_DISK_PROPERTIES(),
2765 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2766 SCSI_DISK_F_REMOVABLE
, false),
2767 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2768 SCSI_DISK_F_DPOFUA
, false),
2769 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2770 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2771 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2772 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2773 DEFAULT_MAX_UNMAP_SIZE
),
2774 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2775 DEFAULT_MAX_IO_SIZE
),
2776 DEFINE_PROP_END_OF_LIST(),
2779 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
2781 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2782 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2784 sc
->realize
= scsi_disk_realize
;
2785 sc
->alloc_req
= scsi_new_request
;
2786 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2787 dc
->fw_name
= "disk";
2788 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
2789 dc
->reset
= scsi_disk_reset
;
2790 dc
->props
= scsi_disk_properties
;
2791 dc
->vmsd
= &vmstate_scsi_disk_state
;
2794 static const TypeInfo scsi_disk_info
= {
2795 .name
= "scsi-disk",
2796 .parent
= TYPE_SCSI_DEVICE
,
2797 .instance_size
= sizeof(SCSIDiskState
),
2798 .class_init
= scsi_disk_class_initfn
,
2801 static void scsi_disk_register_types(void)
2803 type_register_static(&scsi_hd_info
);
2804 type_register_static(&scsi_cd_info
);
2806 type_register_static(&scsi_block_info
);
2808 type_register_static(&scsi_disk_info
);
2811 type_init(scsi_disk_register_types
)