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/blockdev.h"
37 #include "hw/block/block.h"
38 #include "sysemu/dma.h"
44 #define SCSI_WRITE_SAME_MAX 524288
45 #define SCSI_DMA_BUF_SIZE 131072
46 #define SCSI_MAX_INQUIRY_LEN 256
47 #define SCSI_MAX_MODE_LEN 256
49 #define DEFAULT_DISCARD_GRANULARITY 4096
50 #define DEFAULT_MAX_UNMAP_SIZE (1 << 30) /* 1 GB */
52 typedef struct SCSIDiskState SCSIDiskState
;
54 typedef struct SCSIDiskReq
{
56 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
58 uint32_t sector_count
;
66 #define SCSI_DISK_F_REMOVABLE 0
67 #define SCSI_DISK_F_DPOFUA 1
68 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2
80 uint64_t max_unmap_size
;
90 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
92 static void scsi_free_request(SCSIRequest
*req
)
94 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
96 qemu_vfree(r
->iov
.iov_base
);
99 /* Helper function for command completion with sense. */
100 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
102 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
103 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
104 scsi_req_build_sense(&r
->req
, sense
);
105 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
108 /* Cancel a pending data transfer. */
109 static void scsi_cancel_io(SCSIRequest
*req
)
111 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
113 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
115 bdrv_aio_cancel(r
->req
.aiocb
);
117 /* This reference was left in by scsi_*_data. We take ownership of
118 * it the moment scsi_req_cancel is called, independent of whether
119 * bdrv_aio_cancel completes the request or not. */
120 scsi_req_unref(&r
->req
);
125 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
127 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
129 if (!r
->iov
.iov_base
) {
131 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
133 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
134 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
135 return r
->qiov
.size
/ 512;
138 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
140 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
142 qemu_put_be64s(f
, &r
->sector
);
143 qemu_put_be32s(f
, &r
->sector_count
);
144 qemu_put_be32s(f
, &r
->buflen
);
146 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
147 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
148 } else if (!req
->retry
) {
149 uint32_t len
= r
->iov
.iov_len
;
150 qemu_put_be32s(f
, &len
);
151 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
156 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
158 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
160 qemu_get_be64s(f
, &r
->sector
);
161 qemu_get_be32s(f
, &r
->sector_count
);
162 qemu_get_be32s(f
, &r
->buflen
);
164 scsi_init_iovec(r
, r
->buflen
);
165 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
166 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
167 } else if (!r
->req
.retry
) {
169 qemu_get_be32s(f
, &len
);
170 r
->iov
.iov_len
= len
;
171 assert(r
->iov
.iov_len
<= r
->buflen
);
172 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
176 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
179 static void scsi_aio_complete(void *opaque
, int ret
)
181 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
182 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
184 assert(r
->req
.aiocb
!= NULL
);
186 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
187 if (r
->req
.io_canceled
) {
192 if (scsi_handle_rw_error(r
, -ret
)) {
197 scsi_req_complete(&r
->req
, GOOD
);
200 if (!r
->req
.io_canceled
) {
201 scsi_req_unref(&r
->req
);
205 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
207 switch (cmd
->buf
[0]) {
214 return (cmd
->buf
[1] & 8) != 0;
219 case WRITE_VERIFY_10
:
220 case WRITE_VERIFY_12
:
221 case WRITE_VERIFY_16
:
231 static void scsi_write_do_fua(SCSIDiskReq
*r
)
233 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
235 if (r
->req
.io_canceled
) {
239 if (scsi_is_cmd_fua(&r
->req
.cmd
)) {
240 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
241 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
245 scsi_req_complete(&r
->req
, GOOD
);
248 if (!r
->req
.io_canceled
) {
249 scsi_req_unref(&r
->req
);
253 static void scsi_dma_complete_noio(void *opaque
, int ret
)
255 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
256 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
258 if (r
->req
.aiocb
!= NULL
) {
260 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
262 if (r
->req
.io_canceled
) {
267 if (scsi_handle_rw_error(r
, -ret
)) {
272 r
->sector
+= r
->sector_count
;
274 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
275 scsi_write_do_fua(r
);
278 scsi_req_complete(&r
->req
, GOOD
);
282 if (!r
->req
.io_canceled
) {
283 scsi_req_unref(&r
->req
);
287 static void scsi_dma_complete(void *opaque
, int ret
)
289 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
291 assert(r
->req
.aiocb
!= NULL
);
292 scsi_dma_complete_noio(opaque
, ret
);
295 static void scsi_read_complete(void * opaque
, int ret
)
297 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
298 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
301 assert(r
->req
.aiocb
!= NULL
);
303 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
304 if (r
->req
.io_canceled
) {
309 if (scsi_handle_rw_error(r
, -ret
)) {
314 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
316 n
= r
->qiov
.size
/ 512;
318 r
->sector_count
-= n
;
319 scsi_req_data(&r
->req
, r
->qiov
.size
);
322 if (!r
->req
.io_canceled
) {
323 scsi_req_unref(&r
->req
);
327 /* Actually issue a read to the block device. */
328 static void scsi_do_read(void *opaque
, int ret
)
330 SCSIDiskReq
*r
= opaque
;
331 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
334 if (r
->req
.aiocb
!= NULL
) {
336 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
338 if (r
->req
.io_canceled
) {
343 if (scsi_handle_rw_error(r
, -ret
)) {
348 /* The request is used as the AIO opaque value, so add a ref. */
349 scsi_req_ref(&r
->req
);
352 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_READ
);
353 r
->req
.resid
-= r
->req
.sg
->size
;
354 r
->req
.aiocb
= dma_bdrv_read(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
355 scsi_dma_complete
, r
);
357 n
= scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
358 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
359 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
360 scsi_read_complete
, r
);
364 if (!r
->req
.io_canceled
) {
365 scsi_req_unref(&r
->req
);
369 /* Read more data from scsi device into buffer. */
370 static void scsi_read_data(SCSIRequest
*req
)
372 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
373 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
376 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
377 if (r
->sector_count
== 0) {
378 /* This also clears the sense buffer for REQUEST SENSE. */
379 scsi_req_complete(&r
->req
, GOOD
);
383 /* No data transfer may already be in progress */
384 assert(r
->req
.aiocb
== NULL
);
386 /* The request is used as the AIO opaque value, so add a ref. */
387 scsi_req_ref(&r
->req
);
388 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
389 DPRINTF("Data transfer direction invalid\n");
390 scsi_read_complete(r
, -EINVAL
);
395 scsi_read_complete(r
, -ENOMEDIUM
);
401 if (first
&& scsi_is_cmd_fua(&r
->req
.cmd
)) {
402 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
403 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_do_read
, r
);
410 * scsi_handle_rw_error has two return values. 0 means that the error
411 * must be ignored, 1 means that the error has been processed and the
412 * caller should not do anything else for this request. Note that
413 * scsi_handle_rw_error always manages its reference counts, independent
414 * of the return value.
416 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
418 bool is_read
= (r
->req
.cmd
.xfer
== SCSI_XFER_FROM_DEV
);
419 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
420 BlockErrorAction action
= bdrv_get_error_action(s
->qdev
.conf
.bs
, is_read
, error
);
422 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
425 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
428 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
431 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
434 scsi_check_condition(r
, SENSE_CODE(SPACE_ALLOC_FAILED
));
437 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
441 bdrv_error_action(s
->qdev
.conf
.bs
, action
, is_read
, error
);
442 if (action
== BLOCK_ERROR_ACTION_STOP
) {
443 scsi_req_retry(&r
->req
);
445 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
448 static void scsi_write_complete(void * opaque
, int ret
)
450 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
451 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
454 if (r
->req
.aiocb
!= NULL
) {
456 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
458 if (r
->req
.io_canceled
) {
463 if (scsi_handle_rw_error(r
, -ret
)) {
468 n
= r
->qiov
.size
/ 512;
470 r
->sector_count
-= n
;
471 if (r
->sector_count
== 0) {
472 scsi_write_do_fua(r
);
475 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
476 DPRINTF("Write complete tag=0x%x more=%zd\n", r
->req
.tag
, r
->qiov
.size
);
477 scsi_req_data(&r
->req
, r
->qiov
.size
);
481 if (!r
->req
.io_canceled
) {
482 scsi_req_unref(&r
->req
);
486 static void scsi_write_data(SCSIRequest
*req
)
488 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
489 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
492 /* No data transfer may already be in progress */
493 assert(r
->req
.aiocb
== NULL
);
495 /* The request is used as the AIO opaque value, so add a ref. */
496 scsi_req_ref(&r
->req
);
497 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
498 DPRINTF("Data transfer direction invalid\n");
499 scsi_write_complete(r
, -EINVAL
);
503 if (!r
->req
.sg
&& !r
->qiov
.size
) {
504 /* Called for the first time. Ask the driver to send us more data. */
506 scsi_write_complete(r
, 0);
510 scsi_write_complete(r
, -ENOMEDIUM
);
514 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
515 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
517 scsi_dma_complete_noio(r
, 0);
519 scsi_write_complete(r
, 0);
525 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_WRITE
);
526 r
->req
.resid
-= r
->req
.sg
->size
;
527 r
->req
.aiocb
= dma_bdrv_write(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
528 scsi_dma_complete
, r
);
530 n
= r
->qiov
.size
/ 512;
531 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
532 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
533 scsi_write_complete
, r
);
537 /* Return a pointer to the data buffer. */
538 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
540 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
542 return (uint8_t *)r
->iov
.iov_base
;
545 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
547 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
551 if (req
->cmd
.buf
[1] & 0x1) {
552 /* Vital product data */
553 uint8_t page_code
= req
->cmd
.buf
[2];
555 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
556 outbuf
[buflen
++] = page_code
; // this page
557 outbuf
[buflen
++] = 0x00;
558 outbuf
[buflen
++] = 0x00;
562 case 0x00: /* Supported page codes, mandatory */
564 DPRINTF("Inquiry EVPD[Supported pages] "
565 "buffer size %zd\n", req
->cmd
.xfer
);
566 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
568 outbuf
[buflen
++] = 0x80; // unit serial number
570 outbuf
[buflen
++] = 0x83; // device identification
571 if (s
->qdev
.type
== TYPE_DISK
) {
572 outbuf
[buflen
++] = 0xb0; // block limits
573 outbuf
[buflen
++] = 0xb2; // thin provisioning
577 case 0x80: /* Device serial number, optional */
582 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
586 l
= strlen(s
->serial
);
591 DPRINTF("Inquiry EVPD[Serial number] "
592 "buffer size %zd\n", req
->cmd
.xfer
);
593 memcpy(outbuf
+buflen
, s
->serial
, l
);
598 case 0x83: /* Device identification page, mandatory */
600 const char *str
= s
->serial
?: bdrv_get_device_name(s
->qdev
.conf
.bs
);
601 int max_len
= s
->serial
? 20 : 255 - 8;
602 int id_len
= strlen(str
);
604 if (id_len
> max_len
) {
607 DPRINTF("Inquiry EVPD[Device identification] "
608 "buffer size %zd\n", req
->cmd
.xfer
);
610 outbuf
[buflen
++] = 0x2; // ASCII
611 outbuf
[buflen
++] = 0; // not officially assigned
612 outbuf
[buflen
++] = 0; // reserved
613 outbuf
[buflen
++] = id_len
; // length of data following
614 memcpy(outbuf
+buflen
, str
, id_len
);
618 outbuf
[buflen
++] = 0x1; // Binary
619 outbuf
[buflen
++] = 0x3; // NAA
620 outbuf
[buflen
++] = 0; // reserved
621 outbuf
[buflen
++] = 8;
622 stq_be_p(&outbuf
[buflen
], s
->wwn
);
627 outbuf
[buflen
++] = 0x61; // SAS / Binary
628 outbuf
[buflen
++] = 0x93; // PIV / Target port / NAA
629 outbuf
[buflen
++] = 0; // reserved
630 outbuf
[buflen
++] = 8;
631 stq_be_p(&outbuf
[buflen
], s
->port_wwn
);
636 outbuf
[buflen
++] = 0x61; // SAS / Binary
637 outbuf
[buflen
++] = 0x94; // PIV / Target port / relative target port
638 outbuf
[buflen
++] = 0; // reserved
639 outbuf
[buflen
++] = 4;
640 stw_be_p(&outbuf
[buflen
+ 2], s
->port_index
);
645 case 0xb0: /* block limits */
647 unsigned int unmap_sectors
=
648 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
649 unsigned int min_io_size
=
650 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
651 unsigned int opt_io_size
=
652 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
653 unsigned int max_unmap_sectors
=
654 s
->max_unmap_size
/ s
->qdev
.blocksize
;
656 if (s
->qdev
.type
== TYPE_ROM
) {
657 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
661 /* required VPD size with unmap support */
663 memset(outbuf
+ 4, 0, buflen
- 4);
665 outbuf
[4] = 0x1; /* wsnz */
667 /* optimal transfer length granularity */
668 outbuf
[6] = (min_io_size
>> 8) & 0xff;
669 outbuf
[7] = min_io_size
& 0xff;
671 /* optimal transfer length */
672 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
673 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
674 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
675 outbuf
[15] = opt_io_size
& 0xff;
677 /* max unmap LBA count, default is 1GB */
678 outbuf
[20] = (max_unmap_sectors
>> 24) & 0xff;
679 outbuf
[21] = (max_unmap_sectors
>> 16) & 0xff;
680 outbuf
[22] = (max_unmap_sectors
>> 8) & 0xff;
681 outbuf
[23] = max_unmap_sectors
& 0xff;
683 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header. */
689 /* optimal unmap granularity */
690 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
691 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
692 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
693 outbuf
[31] = unmap_sectors
& 0xff;
696 case 0xb2: /* thin provisioning */
700 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
701 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
709 assert(buflen
- start
<= 255);
710 outbuf
[start
- 1] = buflen
- start
;
714 /* Standard INQUIRY data */
715 if (req
->cmd
.buf
[2] != 0) {
720 buflen
= req
->cmd
.xfer
;
721 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
722 buflen
= SCSI_MAX_INQUIRY_LEN
;
725 outbuf
[0] = s
->qdev
.type
& 0x1f;
726 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
728 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
729 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
731 memset(&outbuf
[32], 0, 4);
732 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
734 * We claim conformance to SPC-3, which is required for guests
735 * to ask for modern features like READ CAPACITY(16) or the
736 * block characteristics VPD page by default. Not all of SPC-3
737 * is actually implemented, but we're good enough.
740 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
743 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
745 /* If the allocation length of CDB is too small,
746 the additional length is not adjusted */
750 /* Sync data transfer and TCQ. */
751 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
755 static inline bool media_is_dvd(SCSIDiskState
*s
)
758 if (s
->qdev
.type
!= TYPE_ROM
) {
761 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
764 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
765 return nb_sectors
> CD_MAX_SECTORS
;
768 static inline bool media_is_cd(SCSIDiskState
*s
)
771 if (s
->qdev
.type
!= TYPE_ROM
) {
774 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
777 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
778 return nb_sectors
<= CD_MAX_SECTORS
;
781 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
784 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
786 if (s
->qdev
.type
!= TYPE_ROM
) {
790 /* Types 1/2 are only defined for Blu-Ray. */
792 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
796 memset(outbuf
, 0, 34);
798 outbuf
[2] = 0xe; /* last session complete, disc finalized */
799 outbuf
[3] = 1; /* first track on disc */
800 outbuf
[4] = 1; /* # of sessions */
801 outbuf
[5] = 1; /* first track of last session */
802 outbuf
[6] = 1; /* last track of last session */
803 outbuf
[7] = 0x20; /* unrestricted use */
804 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
805 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
806 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
807 /* 24-31: disc bar code */
808 /* 32: disc application code */
809 /* 33: number of OPC tables */
814 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
817 static const int rds_caps_size
[5] = {
824 uint8_t media
= r
->req
.cmd
.buf
[1];
825 uint8_t layer
= r
->req
.cmd
.buf
[6];
826 uint8_t format
= r
->req
.cmd
.buf
[7];
829 if (s
->qdev
.type
!= TYPE_ROM
) {
833 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
837 if (format
!= 0xff) {
838 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
839 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
842 if (media_is_cd(s
)) {
843 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
846 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
849 size
= rds_caps_size
[format
];
850 memset(outbuf
, 0, size
);
855 /* Physical format information */
860 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
862 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
863 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
864 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
865 outbuf
[7] = 0; /* default densities */
867 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
868 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
872 case 0x01: /* DVD copyright information, all zeros */
875 case 0x03: /* BCA information - invalid field for no BCA info */
878 case 0x04: /* DVD disc manufacturing information, all zeros */
881 case 0xff: { /* List capabilities */
884 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
885 if (!rds_caps_size
[i
]) {
889 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
890 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
900 /* Size of buffer, not including 2 byte size field */
901 stw_be_p(outbuf
, size
- 2);
908 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
910 uint8_t event_code
, media_status
;
914 media_status
= MS_TRAY_OPEN
;
915 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
916 media_status
= MS_MEDIA_PRESENT
;
919 /* Event notification descriptor */
920 event_code
= MEC_NO_CHANGE
;
921 if (media_status
!= MS_TRAY_OPEN
) {
922 if (s
->media_event
) {
923 event_code
= MEC_NEW_MEDIA
;
924 s
->media_event
= false;
925 } else if (s
->eject_request
) {
926 event_code
= MEC_EJECT_REQUESTED
;
927 s
->eject_request
= false;
931 outbuf
[0] = event_code
;
932 outbuf
[1] = media_status
;
934 /* These fields are reserved, just clear them. */
940 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
944 uint8_t *buf
= r
->req
.cmd
.buf
;
945 uint8_t notification_class_request
= buf
[4];
946 if (s
->qdev
.type
!= TYPE_ROM
) {
949 if ((buf
[1] & 1) == 0) {
955 outbuf
[0] = outbuf
[1] = 0;
956 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
957 if (notification_class_request
& (1 << GESN_MEDIA
)) {
958 outbuf
[2] = GESN_MEDIA
;
959 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
963 stw_be_p(outbuf
, size
- 4);
967 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
971 if (s
->qdev
.type
!= TYPE_ROM
) {
974 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
975 memset(outbuf
, 0, 40);
976 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
977 stw_be_p(&outbuf
[6], current
);
978 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
979 outbuf
[10] = 0x03; /* persistent, current */
980 outbuf
[11] = 8; /* two profiles */
981 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
982 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
983 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
984 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
985 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
986 stw_be_p(&outbuf
[20], 1);
987 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
989 stl_be_p(&outbuf
[24], 1); /* SCSI */
990 outbuf
[28] = 1; /* DBE = 1, mandatory */
991 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
992 stw_be_p(&outbuf
[32], 3);
993 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
995 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
996 /* TODO: Random readable, CD read, DVD read, drive serial number,
1001 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
1003 if (s
->qdev
.type
!= TYPE_ROM
) {
1006 memset(outbuf
, 0, 8);
1007 outbuf
[5] = 1; /* CD-ROM */
1011 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
1014 static const int mode_sense_valid
[0x3f] = {
1015 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
1016 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
1017 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1018 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1019 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
1020 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
1023 uint8_t *p
= *p_outbuf
+ 2;
1026 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
1031 * If Changeable Values are requested, a mask denoting those mode parameters
1032 * that are changeable shall be returned. As we currently don't support
1033 * parameter changes via MODE_SELECT all bits are returned set to zero.
1034 * The buffer was already menset to zero by the caller of this function.
1036 * The offsets here are off by two compared to the descriptions in the
1037 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1038 * but it is done so that offsets are consistent within our implementation
1039 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1040 * 2-byte and 4-byte headers.
1043 case MODE_PAGE_HD_GEOMETRY
:
1045 if (page_control
== 1) { /* Changeable Values */
1048 /* if a geometry hint is available, use it */
1049 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1050 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1051 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
1052 p
[3] = s
->qdev
.conf
.heads
& 0xff;
1053 /* Write precomp start cylinder, disabled */
1054 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1055 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1056 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
1057 /* Reduced current start cylinder, disabled */
1058 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1059 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1060 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1061 /* Device step rate [ns], 200ns */
1064 /* Landing zone cylinder */
1068 /* Medium rotation rate [rpm], 5400 rpm */
1069 p
[18] = (5400 >> 8) & 0xff;
1070 p
[19] = 5400 & 0xff;
1073 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1075 if (page_control
== 1) { /* Changeable Values */
1078 /* Transfer rate [kbit/s], 5Mbit/s */
1081 /* if a geometry hint is available, use it */
1082 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1083 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1084 p
[4] = s
->qdev
.blocksize
>> 8;
1085 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1086 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1087 /* Write precomp start cylinder, disabled */
1088 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1089 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1090 /* Reduced current start cylinder, disabled */
1091 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1092 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1093 /* Device step rate [100us], 100us */
1096 /* Device step pulse width [us], 1us */
1098 /* Device head settle delay [100us], 100us */
1101 /* Motor on delay [0.1s], 0.1s */
1103 /* Motor off delay [0.1s], 0.1s */
1105 /* Medium rotation rate [rpm], 5400 rpm */
1106 p
[26] = (5400 >> 8) & 0xff;
1107 p
[27] = 5400 & 0xff;
1110 case MODE_PAGE_CACHING
:
1112 if (page_control
== 1 || /* Changeable Values */
1113 bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1118 case MODE_PAGE_R_W_ERROR
:
1120 if (page_control
== 1) { /* Changeable Values */
1123 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1124 if (s
->qdev
.type
== TYPE_ROM
) {
1125 p
[1] = 0x20; /* Read Retry Count */
1129 case MODE_PAGE_AUDIO_CTL
:
1133 case MODE_PAGE_CAPABILITIES
:
1135 if (page_control
== 1) { /* Changeable Values */
1139 p
[0] = 0x3b; /* CD-R & CD-RW read */
1140 p
[1] = 0; /* Writing not supported */
1141 p
[2] = 0x7f; /* Audio, composite, digital out,
1142 mode 2 form 1&2, multi session */
1143 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1144 RW corrected, C2 errors, ISRC,
1146 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1147 /* Locking supported, jumper present, eject, tray */
1148 p
[5] = 0; /* no volume & mute control, no
1150 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1151 p
[7] = (50 * 176) & 0xff;
1152 p
[8] = 2 >> 8; /* Two volume levels */
1154 p
[10] = 2048 >> 8; /* 2M buffer */
1155 p
[11] = 2048 & 0xff;
1156 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1157 p
[13] = (16 * 176) & 0xff;
1158 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1159 p
[17] = (16 * 176) & 0xff;
1160 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1161 p
[19] = (16 * 176) & 0xff;
1168 assert(length
< 256);
1169 (*p_outbuf
)[0] = page
;
1170 (*p_outbuf
)[1] = length
;
1171 *p_outbuf
+= length
+ 2;
1175 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1177 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1178 uint64_t nb_sectors
;
1180 int page
, buflen
, ret
, page_control
;
1182 uint8_t dev_specific_param
;
1184 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1185 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1186 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1187 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1188 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
1189 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1192 if (s
->qdev
.type
== TYPE_DISK
) {
1193 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1194 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1195 dev_specific_param
|= 0x80; /* Readonly. */
1198 /* MMC prescribes that CD/DVD drives have no block descriptors,
1199 * and defines no device-specific parameter. */
1200 dev_specific_param
= 0x00;
1204 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1205 p
[1] = 0; /* Default media type. */
1206 p
[2] = dev_specific_param
;
1207 p
[3] = 0; /* Block descriptor length. */
1209 } else { /* MODE_SENSE_10 */
1210 p
[2] = 0; /* Default media type. */
1211 p
[3] = dev_specific_param
;
1212 p
[6] = p
[7] = 0; /* Block descriptor length. */
1216 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1217 if (!dbd
&& nb_sectors
) {
1218 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1219 outbuf
[3] = 8; /* Block descriptor length */
1220 } else { /* MODE_SENSE_10 */
1221 outbuf
[7] = 8; /* Block descriptor length */
1223 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1224 if (nb_sectors
> 0xffffff) {
1227 p
[0] = 0; /* media density code */
1228 p
[1] = (nb_sectors
>> 16) & 0xff;
1229 p
[2] = (nb_sectors
>> 8) & 0xff;
1230 p
[3] = nb_sectors
& 0xff;
1231 p
[4] = 0; /* reserved */
1232 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1233 p
[6] = s
->qdev
.blocksize
>> 8;
1238 if (page_control
== 3) {
1240 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1245 for (page
= 0; page
<= 0x3e; page
++) {
1246 mode_sense_page(s
, page
, &p
, page_control
);
1249 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1255 buflen
= p
- outbuf
;
1257 * The mode data length field specifies the length in bytes of the
1258 * following data that is available to be transferred. The mode data
1259 * length does not include itself.
1261 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1262 outbuf
[0] = buflen
- 1;
1263 } else { /* MODE_SENSE_10 */
1264 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1265 outbuf
[1] = (buflen
- 2) & 0xff;
1270 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1272 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1273 int start_track
, format
, msf
, toclen
;
1274 uint64_t nb_sectors
;
1276 msf
= req
->cmd
.buf
[1] & 2;
1277 format
= req
->cmd
.buf
[2] & 0xf;
1278 start_track
= req
->cmd
.buf
[6];
1279 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1280 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1281 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1284 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1287 /* multi session : only a single session defined */
1289 memset(outbuf
, 0, 12);
1295 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1303 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1305 SCSIRequest
*req
= &r
->req
;
1306 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1307 bool start
= req
->cmd
.buf
[4] & 1;
1308 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1309 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1312 /* eject/load only happens for power condition == 0 */
1316 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1317 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1318 scsi_check_condition(r
,
1319 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1320 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1321 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1325 if (s
->tray_open
!= !start
) {
1326 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1327 s
->tray_open
= !start
;
1333 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1335 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1336 int buflen
= r
->iov
.iov_len
;
1339 DPRINTF("Read buf_len=%d\n", buflen
);
1342 scsi_req_data(&r
->req
, buflen
);
1346 /* This also clears the sense buffer for REQUEST SENSE. */
1347 scsi_req_complete(&r
->req
, GOOD
);
1350 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1351 uint8_t *inbuf
, int inlen
)
1353 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1354 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1356 int len
, expected_len
, changeable_len
, i
;
1358 /* The input buffer does not include the page header, so it is
1361 expected_len
= inlen
+ 2;
1362 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1367 memset(mode_current
, 0, inlen
+ 2);
1368 len
= mode_sense_page(s
, page
, &p
, 0);
1369 if (len
< 0 || len
!= expected_len
) {
1373 p
= mode_changeable
;
1374 memset(mode_changeable
, 0, inlen
+ 2);
1375 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1376 assert(changeable_len
== len
);
1378 /* Check that unchangeable bits are the same as what MODE SENSE
1381 for (i
= 2; i
< len
; i
++) {
1382 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1389 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1392 case MODE_PAGE_CACHING
:
1393 bdrv_set_enable_write_cache(s
->qdev
.conf
.bs
, (p
[0] & 4) != 0);
1401 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1403 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1406 int page
, subpage
, page_len
;
1408 /* Parse both possible formats for the mode page headers. */
1412 goto invalid_param_len
;
1415 page_len
= lduw_be_p(&p
[2]);
1420 goto invalid_param_len
;
1431 if (page_len
> len
) {
1432 goto invalid_param_len
;
1436 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1440 scsi_disk_apply_mode_select(s
, page
, p
);
1449 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1453 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1457 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1459 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1461 int cmd
= r
->req
.cmd
.buf
[0];
1462 int len
= r
->req
.cmd
.xfer
;
1463 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1467 /* We only support PF=1, SP=0. */
1468 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1472 if (len
< hdr_len
) {
1473 goto invalid_param_len
;
1476 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1480 goto invalid_param_len
;
1482 if (bd_len
!= 0 && bd_len
!= 8) {
1489 /* Ensure no change is made if there is an error! */
1490 for (pass
= 0; pass
< 2; pass
++) {
1491 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1496 if (!bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1497 /* The request is used as the AIO opaque value, so add a ref. */
1498 scsi_req_ref(&r
->req
);
1499 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1500 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
1504 scsi_req_complete(&r
->req
, GOOD
);
1508 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1512 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1516 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1519 static inline bool check_lba_range(SCSIDiskState
*s
,
1520 uint64_t sector_num
, uint32_t nb_sectors
)
1523 * The first line tests that no overflow happens when computing the last
1524 * sector. The second line tests that the last accessed sector is in
1527 * Careful, the computations should not underflow for nb_sectors == 0,
1528 * and a 0-block read to the first LBA beyond the end of device is
1531 return (sector_num
<= sector_num
+ nb_sectors
&&
1532 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1535 typedef struct UnmapCBData
{
1541 static void scsi_unmap_complete(void *opaque
, int ret
)
1543 UnmapCBData
*data
= opaque
;
1544 SCSIDiskReq
*r
= data
->r
;
1545 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1546 uint64_t sector_num
;
1547 uint32_t nb_sectors
;
1549 r
->req
.aiocb
= NULL
;
1550 if (r
->req
.io_canceled
) {
1555 if (scsi_handle_rw_error(r
, -ret
)) {
1560 if (data
->count
> 0) {
1561 sector_num
= ldq_be_p(&data
->inbuf
[0]);
1562 nb_sectors
= ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
;
1563 if (!check_lba_range(s
, sector_num
, nb_sectors
)) {
1564 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1568 r
->req
.aiocb
= bdrv_aio_discard(s
->qdev
.conf
.bs
,
1569 sector_num
* (s
->qdev
.blocksize
/ 512),
1570 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1571 scsi_unmap_complete
, data
);
1577 scsi_req_complete(&r
->req
, GOOD
);
1580 if (!r
->req
.io_canceled
) {
1581 scsi_req_unref(&r
->req
);
1586 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1588 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1590 int len
= r
->req
.cmd
.xfer
;
1593 /* Reject ANCHOR=1. */
1594 if (r
->req
.cmd
.buf
[1] & 0x1) {
1599 goto invalid_param_len
;
1601 if (len
< lduw_be_p(&p
[0]) + 2) {
1602 goto invalid_param_len
;
1604 if (len
< lduw_be_p(&p
[2]) + 8) {
1605 goto invalid_param_len
;
1607 if (lduw_be_p(&p
[2]) & 15) {
1608 goto invalid_param_len
;
1611 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1612 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1616 data
= g_new0(UnmapCBData
, 1);
1618 data
->inbuf
= &p
[8];
1619 data
->count
= lduw_be_p(&p
[2]) >> 4;
1621 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1622 scsi_req_ref(&r
->req
);
1623 scsi_unmap_complete(data
, 0);
1627 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1631 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1634 typedef struct WriteSameCBData
{
1642 static void scsi_write_same_complete(void *opaque
, int ret
)
1644 WriteSameCBData
*data
= opaque
;
1645 SCSIDiskReq
*r
= data
->r
;
1646 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1648 assert(r
->req
.aiocb
!= NULL
);
1649 r
->req
.aiocb
= NULL
;
1650 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
1651 if (r
->req
.io_canceled
) {
1656 if (scsi_handle_rw_error(r
, -ret
)) {
1661 data
->nb_sectors
-= data
->iov
.iov_len
/ 512;
1662 data
->sector
+= data
->iov
.iov_len
/ 512;
1663 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, data
->iov
.iov_len
);
1664 if (data
->iov
.iov_len
) {
1665 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, data
->iov
.iov_len
, BDRV_ACCT_WRITE
);
1666 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, data
->sector
,
1667 &data
->qiov
, data
->iov
.iov_len
/ 512,
1668 scsi_write_same_complete
, data
);
1672 scsi_req_complete(&r
->req
, GOOD
);
1675 if (!r
->req
.io_canceled
) {
1676 scsi_req_unref(&r
->req
);
1678 qemu_vfree(data
->iov
.iov_base
);
1682 static void scsi_disk_emulate_write_same(SCSIDiskReq
*r
, uint8_t *inbuf
)
1684 SCSIRequest
*req
= &r
->req
;
1685 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1686 uint32_t nb_sectors
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
1687 WriteSameCBData
*data
;
1691 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1692 if (nb_sectors
== 0 || (req
->cmd
.buf
[1] & 0x16)) {
1693 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1697 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1698 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1701 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1702 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1706 if (buffer_is_zero(inbuf
, s
->qdev
.blocksize
)) {
1707 int flags
= (req
->cmd
.buf
[1] & 0x8) ? BDRV_REQ_MAY_UNMAP
: 0;
1709 /* The request is used as the AIO opaque value, so add a ref. */
1710 scsi_req_ref(&r
->req
);
1711 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, nb_sectors
* s
->qdev
.blocksize
,
1713 r
->req
.aiocb
= bdrv_aio_write_zeroes(s
->qdev
.conf
.bs
,
1714 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1715 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1716 flags
, scsi_aio_complete
, r
);
1720 data
= g_new0(WriteSameCBData
, 1);
1722 data
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1723 data
->nb_sectors
= nb_sectors
* (s
->qdev
.blocksize
/ 512);
1724 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, SCSI_WRITE_SAME_MAX
);
1725 data
->iov
.iov_base
= buf
= qemu_blockalign(s
->qdev
.conf
.bs
, data
->iov
.iov_len
);
1726 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1728 for (i
= 0; i
< data
->iov
.iov_len
; i
+= s
->qdev
.blocksize
) {
1729 memcpy(&buf
[i
], inbuf
, s
->qdev
.blocksize
);
1732 scsi_req_ref(&r
->req
);
1733 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, data
->iov
.iov_len
, BDRV_ACCT_WRITE
);
1734 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, data
->sector
,
1735 &data
->qiov
, data
->iov
.iov_len
/ 512,
1736 scsi_write_same_complete
, data
);
1739 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1741 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1743 if (r
->iov
.iov_len
) {
1744 int buflen
= r
->iov
.iov_len
;
1745 DPRINTF("Write buf_len=%d\n", buflen
);
1747 scsi_req_data(&r
->req
, buflen
);
1751 switch (req
->cmd
.buf
[0]) {
1753 case MODE_SELECT_10
:
1754 /* This also clears the sense buffer for REQUEST SENSE. */
1755 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1759 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1765 if (r
->req
.status
== -1) {
1766 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1772 scsi_disk_emulate_write_same(r
, r
->iov
.iov_base
);
1780 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1782 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1783 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1784 uint64_t nb_sectors
;
1788 switch (req
->cmd
.buf
[0]) {
1797 case ALLOW_MEDIUM_REMOVAL
:
1798 case GET_CONFIGURATION
:
1799 case GET_EVENT_STATUS_NOTIFICATION
:
1800 case MECHANISM_STATUS
:
1805 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1806 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1813 * FIXME: we shouldn't return anything bigger than 4k, but the code
1814 * requires the buffer to be as big as req->cmd.xfer in several
1815 * places. So, do not allow CDBs with a very large ALLOCATION
1816 * LENGTH. The real fix would be to modify scsi_read_data and
1817 * dma_buf_read, so that they return data beyond the buflen
1820 if (req
->cmd
.xfer
> 65536) {
1821 goto illegal_request
;
1823 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1825 if (!r
->iov
.iov_base
) {
1826 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1829 buflen
= req
->cmd
.xfer
;
1830 outbuf
= r
->iov
.iov_base
;
1831 memset(outbuf
, 0, r
->buflen
);
1832 switch (req
->cmd
.buf
[0]) {
1833 case TEST_UNIT_READY
:
1834 assert(!s
->tray_open
&& bdrv_is_inserted(s
->qdev
.conf
.bs
));
1837 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1839 goto illegal_request
;
1844 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1846 goto illegal_request
;
1850 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1852 goto illegal_request
;
1856 if (req
->cmd
.buf
[1] & 1) {
1857 goto illegal_request
;
1861 if (req
->cmd
.buf
[1] & 3) {
1862 goto illegal_request
;
1866 if (req
->cmd
.buf
[1] & 1) {
1867 goto illegal_request
;
1871 if (req
->cmd
.buf
[1] & 3) {
1872 goto illegal_request
;
1876 if (scsi_disk_emulate_start_stop(r
) < 0) {
1880 case ALLOW_MEDIUM_REMOVAL
:
1881 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1882 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1884 case READ_CAPACITY_10
:
1885 /* The normal LEN field for this command is zero. */
1886 memset(outbuf
, 0, 8);
1887 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1889 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1892 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1893 goto illegal_request
;
1895 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1896 /* Returned value is the address of the last sector. */
1898 /* Remember the new size for read/write sanity checking. */
1899 s
->qdev
.max_lba
= nb_sectors
;
1900 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1901 if (nb_sectors
> UINT32_MAX
) {
1902 nb_sectors
= UINT32_MAX
;
1904 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1905 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1906 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1907 outbuf
[3] = nb_sectors
& 0xff;
1910 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1914 /* Just return "NO SENSE". */
1915 buflen
= scsi_build_sense(NULL
, 0, outbuf
, r
->buflen
,
1916 (req
->cmd
.buf
[1] & 1) == 0);
1918 goto illegal_request
;
1921 case MECHANISM_STATUS
:
1922 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1924 goto illegal_request
;
1927 case GET_CONFIGURATION
:
1928 buflen
= scsi_get_configuration(s
, outbuf
);
1930 goto illegal_request
;
1933 case GET_EVENT_STATUS_NOTIFICATION
:
1934 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1936 goto illegal_request
;
1939 case READ_DISC_INFORMATION
:
1940 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
1942 goto illegal_request
;
1945 case READ_DVD_STRUCTURE
:
1946 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1948 goto illegal_request
;
1951 case SERVICE_ACTION_IN_16
:
1952 /* Service Action In subcommands. */
1953 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1954 DPRINTF("SAI READ CAPACITY(16)\n");
1955 memset(outbuf
, 0, req
->cmd
.xfer
);
1956 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1958 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1961 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1962 goto illegal_request
;
1964 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1965 /* Returned value is the address of the last sector. */
1967 /* Remember the new size for read/write sanity checking. */
1968 s
->qdev
.max_lba
= nb_sectors
;
1969 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1970 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1971 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1972 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1973 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1974 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1975 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1976 outbuf
[7] = nb_sectors
& 0xff;
1979 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1982 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1984 /* set TPE bit if the format supports discard */
1985 if (s
->qdev
.conf
.discard_granularity
) {
1989 /* Protection, exponent and lowest lba field left blank. */
1992 DPRINTF("Unsupported Service Action In\n");
1993 goto illegal_request
;
1994 case SYNCHRONIZE_CACHE
:
1995 /* The request is used as the AIO opaque value, so add a ref. */
1996 scsi_req_ref(&r
->req
);
1997 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1998 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
2001 DPRINTF("Seek(10) (sector %" PRId64
")\n", r
->req
.cmd
.lba
);
2002 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
2007 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2009 case MODE_SELECT_10
:
2010 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2013 DPRINTF("Unmap (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2018 DPRINTF("Verify (bytchk %d)\n", (req
->cmd
.buf
[1] >> 1) & 3);
2019 if (req
->cmd
.buf
[1] & 6) {
2020 goto illegal_request
;
2025 DPRINTF("WRITE SAME %d (len %lu)\n",
2026 req
->cmd
.buf
[0] == WRITE_SAME_10
? 10 : 16,
2027 (long)r
->req
.cmd
.xfer
);
2030 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf
[0],
2031 scsi_command_name(buf
[0]));
2032 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
2035 assert(!r
->req
.aiocb
);
2036 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
2037 if (r
->iov
.iov_len
== 0) {
2038 scsi_req_complete(&r
->req
, GOOD
);
2040 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2041 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
2042 return -r
->iov
.iov_len
;
2044 return r
->iov
.iov_len
;
2048 if (r
->req
.status
== -1) {
2049 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2054 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2058 /* Execute a scsi command. Returns the length of the data expected by the
2059 command. This will be Positive for data transfers from the device
2060 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2061 and zero if the command does not transfer any data. */
2063 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2065 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
2066 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
2072 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
2073 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
2077 len
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
2083 DPRINTF("Read (sector %" PRId64
", count %u)\n", r
->req
.cmd
.lba
, len
);
2084 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2085 goto illegal_request
;
2087 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2090 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2091 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2097 case WRITE_VERIFY_10
:
2098 case WRITE_VERIFY_12
:
2099 case WRITE_VERIFY_16
:
2100 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
2101 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
2104 DPRINTF("Write %s(sector %" PRId64
", count %u)\n",
2105 (command
& 0xe) == 0xe ? "And Verify " : "",
2106 r
->req
.cmd
.lba
, len
);
2107 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2108 goto illegal_request
;
2110 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2113 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2114 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2119 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2122 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2125 if (r
->sector_count
== 0) {
2126 scsi_req_complete(&r
->req
, GOOD
);
2128 assert(r
->iov
.iov_len
== 0);
2129 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2130 return -r
->sector_count
* 512;
2132 return r
->sector_count
* 512;
2136 static void scsi_disk_reset(DeviceState
*dev
)
2138 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
2139 uint64_t nb_sectors
;
2141 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
2143 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
2144 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2148 s
->qdev
.max_lba
= nb_sectors
;
2149 /* reset tray statuses */
2154 static void scsi_destroy(SCSIDevice
*dev
)
2156 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2158 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
2159 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
2162 static void scsi_disk_resize_cb(void *opaque
)
2164 SCSIDiskState
*s
= opaque
;
2166 /* SPC lists this sense code as available only for
2167 * direct-access devices.
2169 if (s
->qdev
.type
== TYPE_DISK
) {
2170 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
2174 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
2176 SCSIDiskState
*s
= opaque
;
2179 * When a CD gets changed, we have to report an ejected state and
2180 * then a loaded state to guests so that they detect tray
2181 * open/close and media change events. Guests that do not use
2182 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2183 * states rely on this behavior.
2185 * media_changed governs the state machine used for unit attention
2186 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2188 s
->media_changed
= load
;
2189 s
->tray_open
= !load
;
2190 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
2191 s
->media_event
= true;
2192 s
->eject_request
= false;
2195 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2197 SCSIDiskState
*s
= opaque
;
2199 s
->eject_request
= true;
2201 s
->tray_locked
= false;
2205 static bool scsi_cd_is_tray_open(void *opaque
)
2207 return ((SCSIDiskState
*)opaque
)->tray_open
;
2210 static bool scsi_cd_is_medium_locked(void *opaque
)
2212 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2215 static const BlockDevOps scsi_disk_removable_block_ops
= {
2216 .change_media_cb
= scsi_cd_change_media_cb
,
2217 .eject_request_cb
= scsi_cd_eject_request_cb
,
2218 .is_tray_open
= scsi_cd_is_tray_open
,
2219 .is_medium_locked
= scsi_cd_is_medium_locked
,
2221 .resize_cb
= scsi_disk_resize_cb
,
2224 static const BlockDevOps scsi_disk_block_ops
= {
2225 .resize_cb
= scsi_disk_resize_cb
,
2228 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2230 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2231 if (s
->media_changed
) {
2232 s
->media_changed
= false;
2233 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2237 static int scsi_initfn(SCSIDevice
*dev
)
2239 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2241 if (!s
->qdev
.conf
.bs
) {
2242 error_report("drive property not set");
2246 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2247 !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
2248 error_report("Device needs media, but drive is empty");
2252 blkconf_serial(&s
->qdev
.conf
, &s
->serial
);
2253 if (dev
->type
== TYPE_DISK
2254 && blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255) < 0) {
2258 if (s
->qdev
.conf
.discard_granularity
== -1) {
2259 s
->qdev
.conf
.discard_granularity
=
2260 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2264 s
->version
= g_strdup(qemu_get_version());
2267 s
->vendor
= g_strdup("QEMU");
2270 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
2271 error_report("unwanted /dev/sg*");
2275 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2276 !(s
->features
& (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
))) {
2277 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_removable_block_ops
, s
);
2279 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_block_ops
, s
);
2281 bdrv_set_guest_block_size(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
2283 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
2284 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, NULL
);
2288 static int scsi_hd_initfn(SCSIDevice
*dev
)
2290 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2291 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2292 s
->qdev
.type
= TYPE_DISK
;
2294 s
->product
= g_strdup("QEMU HARDDISK");
2296 return scsi_initfn(&s
->qdev
);
2299 static int scsi_cd_initfn(SCSIDevice
*dev
)
2301 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2302 s
->qdev
.blocksize
= 2048;
2303 s
->qdev
.type
= TYPE_ROM
;
2304 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2306 s
->product
= g_strdup("QEMU CD-ROM");
2308 return scsi_initfn(&s
->qdev
);
2311 static int scsi_disk_initfn(SCSIDevice
*dev
)
2315 if (!dev
->conf
.bs
) {
2316 return scsi_initfn(dev
); /* ... and die there */
2319 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
2320 if (dinfo
->media_cd
) {
2321 return scsi_cd_initfn(dev
);
2323 return scsi_hd_initfn(dev
);
2327 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2328 .size
= sizeof(SCSIDiskReq
),
2329 .free_req
= scsi_free_request
,
2330 .send_command
= scsi_disk_emulate_command
,
2331 .read_data
= scsi_disk_emulate_read_data
,
2332 .write_data
= scsi_disk_emulate_write_data
,
2333 .cancel_io
= scsi_cancel_io
,
2334 .get_buf
= scsi_get_buf
,
2337 static const SCSIReqOps scsi_disk_dma_reqops
= {
2338 .size
= sizeof(SCSIDiskReq
),
2339 .free_req
= scsi_free_request
,
2340 .send_command
= scsi_disk_dma_command
,
2341 .read_data
= scsi_read_data
,
2342 .write_data
= scsi_write_data
,
2343 .cancel_io
= scsi_cancel_io
,
2344 .get_buf
= scsi_get_buf
,
2345 .load_request
= scsi_disk_load_request
,
2346 .save_request
= scsi_disk_save_request
,
2349 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2350 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2351 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2352 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2353 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2354 [START_STOP
] = &scsi_disk_emulate_reqops
,
2355 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2356 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2357 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2358 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2359 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2360 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2361 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2362 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2363 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2364 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2365 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2366 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2367 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2368 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2369 [UNMAP
] = &scsi_disk_emulate_reqops
,
2370 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2371 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2372 [VERIFY_10
] = &scsi_disk_emulate_reqops
,
2373 [VERIFY_12
] = &scsi_disk_emulate_reqops
,
2374 [VERIFY_16
] = &scsi_disk_emulate_reqops
,
2376 [READ_6
] = &scsi_disk_dma_reqops
,
2377 [READ_10
] = &scsi_disk_dma_reqops
,
2378 [READ_12
] = &scsi_disk_dma_reqops
,
2379 [READ_16
] = &scsi_disk_dma_reqops
,
2380 [WRITE_6
] = &scsi_disk_dma_reqops
,
2381 [WRITE_10
] = &scsi_disk_dma_reqops
,
2382 [WRITE_12
] = &scsi_disk_dma_reqops
,
2383 [WRITE_16
] = &scsi_disk_dma_reqops
,
2384 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2385 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2386 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2389 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2390 uint8_t *buf
, void *hba_private
)
2392 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2394 const SCSIReqOps
*ops
;
2398 ops
= scsi_disk_reqops_dispatch
[command
];
2400 ops
= &scsi_disk_emulate_reqops
;
2402 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2405 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
2408 for (i
= 1; i
< req
->cmd
.len
; i
++) {
2409 printf(" 0x%02x", buf
[i
]);
2419 static int get_device_type(SCSIDiskState
*s
)
2421 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
2424 uint8_t sensebuf
[8];
2425 sg_io_hdr_t io_header
;
2428 memset(cmd
, 0, sizeof(cmd
));
2429 memset(buf
, 0, sizeof(buf
));
2431 cmd
[4] = sizeof(buf
);
2433 memset(&io_header
, 0, sizeof(io_header
));
2434 io_header
.interface_id
= 'S';
2435 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2436 io_header
.dxfer_len
= sizeof(buf
);
2437 io_header
.dxferp
= buf
;
2438 io_header
.cmdp
= cmd
;
2439 io_header
.cmd_len
= sizeof(cmd
);
2440 io_header
.mx_sb_len
= sizeof(sensebuf
);
2441 io_header
.sbp
= sensebuf
;
2442 io_header
.timeout
= 6000; /* XXX */
2444 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
2445 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
2448 s
->qdev
.type
= buf
[0];
2449 if (buf
[1] & 0x80) {
2450 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2455 static int scsi_block_initfn(SCSIDevice
*dev
)
2457 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2461 if (!s
->qdev
.conf
.bs
) {
2462 error_report("drive property not set");
2466 /* check we are using a driver managing SG_IO (version 3 and after) */
2467 rc
= bdrv_ioctl(s
->qdev
.conf
.bs
, SG_GET_VERSION_NUM
, &sg_version
);
2469 error_report("cannot get SG_IO version number: %s. "
2470 "Is this a SCSI device?",
2474 if (sg_version
< 30000) {
2475 error_report("scsi generic interface too old");
2479 /* get device type from INQUIRY data */
2480 rc
= get_device_type(s
);
2482 error_report("INQUIRY failed");
2486 /* Make a guess for the block size, we'll fix it when the guest sends.
2487 * READ CAPACITY. If they don't, they likely would assume these sizes
2488 * anyway. (TODO: check in /sys).
2490 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2491 s
->qdev
.blocksize
= 2048;
2493 s
->qdev
.blocksize
= 512;
2496 /* Makes the scsi-block device not removable by using HMP and QMP eject
2499 s
->features
|= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
);
2501 return scsi_initfn(&s
->qdev
);
2504 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2505 uint32_t lun
, uint8_t *buf
,
2508 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2522 case WRITE_VERIFY_10
:
2523 case WRITE_VERIFY_12
:
2524 case WRITE_VERIFY_16
:
2525 /* If we are not using O_DIRECT, we might read stale data from the
2526 * host cache if writes were made using other commands than these
2527 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2528 * O_DIRECT everything must go through SG_IO.
2530 if (!(bdrv_get_flags(s
->qdev
.conf
.bs
) & BDRV_O_NOCACHE
)) {
2534 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2535 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2536 * And once you do these writes, reading from the block device is
2537 * unreliable, too. It is even possible that reads deliver random data
2538 * from the host page cache (this is probably a Linux bug).
2540 * We might use scsi_disk_dma_reqops as long as no writing commands are
2541 * seen, but performance usually isn't paramount on optical media. So,
2542 * just make scsi-block operate the same as scsi-generic for them.
2544 if (s
->qdev
.type
!= TYPE_ROM
) {
2545 return scsi_req_alloc(&scsi_disk_dma_reqops
, &s
->qdev
, tag
, lun
,
2550 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2555 #define DEFINE_SCSI_DISK_PROPERTIES() \
2556 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2557 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2558 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2559 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2560 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2562 static Property scsi_hd_properties
[] = {
2563 DEFINE_SCSI_DISK_PROPERTIES(),
2564 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2565 SCSI_DISK_F_REMOVABLE
, false),
2566 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2567 SCSI_DISK_F_DPOFUA
, false),
2568 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2569 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2570 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2571 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2572 DEFAULT_MAX_UNMAP_SIZE
),
2573 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
2574 DEFINE_PROP_END_OF_LIST(),
2577 static const VMStateDescription vmstate_scsi_disk_state
= {
2578 .name
= "scsi-disk",
2580 .minimum_version_id
= 1,
2581 .fields
= (VMStateField
[]) {
2582 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
2583 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
2584 VMSTATE_BOOL(media_event
, SCSIDiskState
),
2585 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
2586 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
2587 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
2588 VMSTATE_END_OF_LIST()
2592 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
2594 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2595 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2597 sc
->init
= scsi_hd_initfn
;
2598 sc
->destroy
= scsi_destroy
;
2599 sc
->alloc_req
= scsi_new_request
;
2600 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2601 dc
->fw_name
= "disk";
2602 dc
->desc
= "virtual SCSI disk";
2603 dc
->reset
= scsi_disk_reset
;
2604 dc
->props
= scsi_hd_properties
;
2605 dc
->vmsd
= &vmstate_scsi_disk_state
;
2608 static const TypeInfo scsi_hd_info
= {
2610 .parent
= TYPE_SCSI_DEVICE
,
2611 .instance_size
= sizeof(SCSIDiskState
),
2612 .class_init
= scsi_hd_class_initfn
,
2615 static Property scsi_cd_properties
[] = {
2616 DEFINE_SCSI_DISK_PROPERTIES(),
2617 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2618 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2619 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2620 DEFINE_PROP_END_OF_LIST(),
2623 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
2625 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2626 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2628 sc
->init
= scsi_cd_initfn
;
2629 sc
->destroy
= scsi_destroy
;
2630 sc
->alloc_req
= scsi_new_request
;
2631 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2632 dc
->fw_name
= "disk";
2633 dc
->desc
= "virtual SCSI CD-ROM";
2634 dc
->reset
= scsi_disk_reset
;
2635 dc
->props
= scsi_cd_properties
;
2636 dc
->vmsd
= &vmstate_scsi_disk_state
;
2639 static const TypeInfo scsi_cd_info
= {
2641 .parent
= TYPE_SCSI_DEVICE
,
2642 .instance_size
= sizeof(SCSIDiskState
),
2643 .class_init
= scsi_cd_class_initfn
,
2647 static Property scsi_block_properties
[] = {
2648 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.bs
),
2649 DEFINE_PROP_INT32("bootindex", SCSIDiskState
, qdev
.conf
.bootindex
, -1),
2650 DEFINE_PROP_END_OF_LIST(),
2653 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
2655 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2656 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2658 sc
->init
= scsi_block_initfn
;
2659 sc
->destroy
= scsi_destroy
;
2660 sc
->alloc_req
= scsi_block_new_request
;
2661 dc
->fw_name
= "disk";
2662 dc
->desc
= "SCSI block device passthrough";
2663 dc
->reset
= scsi_disk_reset
;
2664 dc
->props
= scsi_block_properties
;
2665 dc
->vmsd
= &vmstate_scsi_disk_state
;
2668 static const TypeInfo scsi_block_info
= {
2669 .name
= "scsi-block",
2670 .parent
= TYPE_SCSI_DEVICE
,
2671 .instance_size
= sizeof(SCSIDiskState
),
2672 .class_init
= scsi_block_class_initfn
,
2676 static Property scsi_disk_properties
[] = {
2677 DEFINE_SCSI_DISK_PROPERTIES(),
2678 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2679 SCSI_DISK_F_REMOVABLE
, false),
2680 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2681 SCSI_DISK_F_DPOFUA
, false),
2682 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, wwn
, 0),
2683 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, port_wwn
, 0),
2684 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2685 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2686 DEFAULT_MAX_UNMAP_SIZE
),
2687 DEFINE_PROP_END_OF_LIST(),
2690 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
2692 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2693 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2695 sc
->init
= scsi_disk_initfn
;
2696 sc
->destroy
= scsi_destroy
;
2697 sc
->alloc_req
= scsi_new_request
;
2698 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2699 dc
->fw_name
= "disk";
2700 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
2701 dc
->reset
= scsi_disk_reset
;
2702 dc
->props
= scsi_disk_properties
;
2703 dc
->vmsd
= &vmstate_scsi_disk_state
;
2706 static const TypeInfo scsi_disk_info
= {
2707 .name
= "scsi-disk",
2708 .parent
= TYPE_SCSI_DEVICE
,
2709 .instance_size
= sizeof(SCSIDiskState
),
2710 .class_init
= scsi_disk_class_initfn
,
2713 static void scsi_disk_register_types(void)
2715 type_register_static(&scsi_hd_info
);
2716 type_register_static(&scsi_cd_info
);
2718 type_register_static(&scsi_block_info
);
2720 type_register_static(&scsi_disk_info
);
2723 type_init(scsi_disk_register_types
)