2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
9 * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case
10 * when the allocation length of CDB is smaller
12 * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the
13 * MODE SENSE response.
15 * This code is licensed under the LGPL.
17 * Note that this file only handles the SCSI architecture model and device
18 * commands. Emulation of interface/link layer protocols is handled by
19 * the host adapter emulator.
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
31 #include "qemu-common.h"
32 #include "qemu/error-report.h"
34 #include "scsi-defs.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/blockdev.h"
37 #include "hw/block-common.h"
38 #include "sysemu/dma.h"
44 #define SCSI_DMA_BUF_SIZE 131072
45 #define SCSI_MAX_INQUIRY_LEN 256
46 #define SCSI_MAX_MODE_LEN 256
48 #define DEFAULT_DISCARD_GRANULARITY 4096
50 typedef struct SCSIDiskState SCSIDiskState
;
52 typedef struct SCSIDiskReq
{
54 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
56 uint32_t sector_count
;
64 #define SCSI_DISK_F_REMOVABLE 0
65 #define SCSI_DISK_F_DPOFUA 1
84 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
86 static void scsi_free_request(SCSIRequest
*req
)
88 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
90 qemu_vfree(r
->iov
.iov_base
);
93 /* Helper function for command completion with sense. */
94 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
96 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
97 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
98 scsi_req_build_sense(&r
->req
, sense
);
99 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
102 /* Cancel a pending data transfer. */
103 static void scsi_cancel_io(SCSIRequest
*req
)
105 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
107 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
109 bdrv_aio_cancel(r
->req
.aiocb
);
111 /* This reference was left in by scsi_*_data. We take ownership of
112 * it the moment scsi_req_cancel is called, independent of whether
113 * bdrv_aio_cancel completes the request or not. */
114 scsi_req_unref(&r
->req
);
119 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
121 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
123 if (!r
->iov
.iov_base
) {
125 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
127 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
128 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
129 return r
->qiov
.size
/ 512;
132 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
134 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
136 qemu_put_be64s(f
, &r
->sector
);
137 qemu_put_be32s(f
, &r
->sector_count
);
138 qemu_put_be32s(f
, &r
->buflen
);
140 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
141 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
142 } else if (!req
->retry
) {
143 uint32_t len
= r
->iov
.iov_len
;
144 qemu_put_be32s(f
, &len
);
145 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
150 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
152 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
154 qemu_get_be64s(f
, &r
->sector
);
155 qemu_get_be32s(f
, &r
->sector_count
);
156 qemu_get_be32s(f
, &r
->buflen
);
158 scsi_init_iovec(r
, r
->buflen
);
159 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
160 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
161 } else if (!r
->req
.retry
) {
163 qemu_get_be32s(f
, &len
);
164 r
->iov
.iov_len
= len
;
165 assert(r
->iov
.iov_len
<= r
->buflen
);
166 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
170 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
173 static void scsi_aio_complete(void *opaque
, int ret
)
175 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
176 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
178 assert(r
->req
.aiocb
!= NULL
);
180 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
183 if (scsi_handle_rw_error(r
, -ret
)) {
188 scsi_req_complete(&r
->req
, GOOD
);
191 if (!r
->req
.io_canceled
) {
192 scsi_req_unref(&r
->req
);
196 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
198 switch (cmd
->buf
[0]) {
205 return (cmd
->buf
[1] & 8) != 0;
210 case WRITE_VERIFY_10
:
211 case WRITE_VERIFY_12
:
212 case WRITE_VERIFY_16
:
222 static void scsi_write_do_fua(SCSIDiskReq
*r
)
224 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
226 if (scsi_is_cmd_fua(&r
->req
.cmd
)) {
227 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
228 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
232 scsi_req_complete(&r
->req
, GOOD
);
233 if (!r
->req
.io_canceled
) {
234 scsi_req_unref(&r
->req
);
238 static void scsi_dma_complete(void *opaque
, int ret
)
240 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
241 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
243 assert(r
->req
.aiocb
!= NULL
);
245 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
248 if (scsi_handle_rw_error(r
, -ret
)) {
253 r
->sector
+= r
->sector_count
;
255 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
256 scsi_write_do_fua(r
);
259 scsi_req_complete(&r
->req
, GOOD
);
263 if (!r
->req
.io_canceled
) {
264 scsi_req_unref(&r
->req
);
268 static void scsi_read_complete(void * opaque
, int ret
)
270 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
271 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
274 assert(r
->req
.aiocb
!= NULL
);
276 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
279 if (scsi_handle_rw_error(r
, -ret
)) {
284 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
286 n
= r
->qiov
.size
/ 512;
288 r
->sector_count
-= n
;
289 scsi_req_data(&r
->req
, r
->qiov
.size
);
292 if (!r
->req
.io_canceled
) {
293 scsi_req_unref(&r
->req
);
297 /* Actually issue a read to the block device. */
298 static void scsi_do_read(void *opaque
, int ret
)
300 SCSIDiskReq
*r
= opaque
;
301 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
304 if (r
->req
.aiocb
!= NULL
) {
306 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
310 if (scsi_handle_rw_error(r
, -ret
)) {
315 if (r
->req
.io_canceled
) {
319 /* The request is used as the AIO opaque value, so add a ref. */
320 scsi_req_ref(&r
->req
);
323 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_READ
);
324 r
->req
.resid
-= r
->req
.sg
->size
;
325 r
->req
.aiocb
= dma_bdrv_read(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
326 scsi_dma_complete
, r
);
328 n
= scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
329 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
330 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
331 scsi_read_complete
, r
);
335 if (!r
->req
.io_canceled
) {
336 scsi_req_unref(&r
->req
);
340 /* Read more data from scsi device into buffer. */
341 static void scsi_read_data(SCSIRequest
*req
)
343 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
344 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
347 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
348 if (r
->sector_count
== 0) {
349 /* This also clears the sense buffer for REQUEST SENSE. */
350 scsi_req_complete(&r
->req
, GOOD
);
354 /* No data transfer may already be in progress */
355 assert(r
->req
.aiocb
== NULL
);
357 /* The request is used as the AIO opaque value, so add a ref. */
358 scsi_req_ref(&r
->req
);
359 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
360 DPRINTF("Data transfer direction invalid\n");
361 scsi_read_complete(r
, -EINVAL
);
366 scsi_read_complete(r
, -ENOMEDIUM
);
372 if (first
&& scsi_is_cmd_fua(&r
->req
.cmd
)) {
373 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
374 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_do_read
, r
);
381 * scsi_handle_rw_error has two return values. 0 means that the error
382 * must be ignored, 1 means that the error has been processed and the
383 * caller should not do anything else for this request. Note that
384 * scsi_handle_rw_error always manages its reference counts, independent
385 * of the return value.
387 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
389 bool is_read
= (r
->req
.cmd
.xfer
== SCSI_XFER_FROM_DEV
);
390 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
391 BlockErrorAction action
= bdrv_get_error_action(s
->qdev
.conf
.bs
, is_read
, error
);
393 if (action
== BDRV_ACTION_REPORT
) {
396 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
399 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
402 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
405 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
409 bdrv_error_action(s
->qdev
.conf
.bs
, action
, is_read
, error
);
410 if (action
== BDRV_ACTION_STOP
) {
411 scsi_req_retry(&r
->req
);
413 return action
!= BDRV_ACTION_IGNORE
;
416 static void scsi_write_complete(void * opaque
, int ret
)
418 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
419 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
422 if (r
->req
.aiocb
!= NULL
) {
424 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
428 if (scsi_handle_rw_error(r
, -ret
)) {
433 n
= r
->qiov
.size
/ 512;
435 r
->sector_count
-= n
;
436 if (r
->sector_count
== 0) {
437 scsi_write_do_fua(r
);
440 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
441 DPRINTF("Write complete tag=0x%x more=%zd\n", r
->req
.tag
, r
->qiov
.size
);
442 scsi_req_data(&r
->req
, r
->qiov
.size
);
446 if (!r
->req
.io_canceled
) {
447 scsi_req_unref(&r
->req
);
451 static void scsi_write_data(SCSIRequest
*req
)
453 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
454 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
457 /* No data transfer may already be in progress */
458 assert(r
->req
.aiocb
== NULL
);
460 /* The request is used as the AIO opaque value, so add a ref. */
461 scsi_req_ref(&r
->req
);
462 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
463 DPRINTF("Data transfer direction invalid\n");
464 scsi_write_complete(r
, -EINVAL
);
468 if (!r
->req
.sg
&& !r
->qiov
.size
) {
469 /* Called for the first time. Ask the driver to send us more data. */
471 scsi_write_complete(r
, 0);
475 scsi_write_complete(r
, -ENOMEDIUM
);
479 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
480 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
482 scsi_dma_complete(r
, 0);
484 scsi_write_complete(r
, 0);
490 dma_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, r
->req
.sg
, BDRV_ACCT_WRITE
);
491 r
->req
.resid
-= r
->req
.sg
->size
;
492 r
->req
.aiocb
= dma_bdrv_write(s
->qdev
.conf
.bs
, r
->req
.sg
, r
->sector
,
493 scsi_dma_complete
, r
);
495 n
= r
->qiov
.size
/ 512;
496 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
497 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
498 scsi_write_complete
, r
);
502 /* Return a pointer to the data buffer. */
503 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
505 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
507 return (uint8_t *)r
->iov
.iov_base
;
510 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
512 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
516 if (req
->cmd
.buf
[1] & 0x1) {
517 /* Vital product data */
518 uint8_t page_code
= req
->cmd
.buf
[2];
520 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
521 outbuf
[buflen
++] = page_code
; // this page
522 outbuf
[buflen
++] = 0x00;
523 outbuf
[buflen
++] = 0x00;
527 case 0x00: /* Supported page codes, mandatory */
529 DPRINTF("Inquiry EVPD[Supported pages] "
530 "buffer size %zd\n", req
->cmd
.xfer
);
531 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
533 outbuf
[buflen
++] = 0x80; // unit serial number
535 outbuf
[buflen
++] = 0x83; // device identification
536 if (s
->qdev
.type
== TYPE_DISK
) {
537 outbuf
[buflen
++] = 0xb0; // block limits
538 outbuf
[buflen
++] = 0xb2; // thin provisioning
542 case 0x80: /* Device serial number, optional */
547 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
551 l
= strlen(s
->serial
);
556 DPRINTF("Inquiry EVPD[Serial number] "
557 "buffer size %zd\n", req
->cmd
.xfer
);
558 memcpy(outbuf
+buflen
, s
->serial
, l
);
563 case 0x83: /* Device identification page, mandatory */
565 const char *str
= s
->serial
?: bdrv_get_device_name(s
->qdev
.conf
.bs
);
566 int max_len
= s
->serial
? 20 : 255 - 8;
567 int id_len
= strlen(str
);
569 if (id_len
> max_len
) {
572 DPRINTF("Inquiry EVPD[Device identification] "
573 "buffer size %zd\n", req
->cmd
.xfer
);
575 outbuf
[buflen
++] = 0x2; // ASCII
576 outbuf
[buflen
++] = 0; // not officially assigned
577 outbuf
[buflen
++] = 0; // reserved
578 outbuf
[buflen
++] = id_len
; // length of data following
579 memcpy(outbuf
+buflen
, str
, id_len
);
583 outbuf
[buflen
++] = 0x1; // Binary
584 outbuf
[buflen
++] = 0x3; // NAA
585 outbuf
[buflen
++] = 0; // reserved
586 outbuf
[buflen
++] = 8;
587 stq_be_p(&outbuf
[buflen
], s
->wwn
);
592 case 0xb0: /* block limits */
594 unsigned int unmap_sectors
=
595 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
596 unsigned int min_io_size
=
597 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
598 unsigned int opt_io_size
=
599 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
601 if (s
->qdev
.type
== TYPE_ROM
) {
602 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
606 /* required VPD size with unmap support */
608 memset(outbuf
+ 4, 0, buflen
- 4);
610 /* optimal transfer length granularity */
611 outbuf
[6] = (min_io_size
>> 8) & 0xff;
612 outbuf
[7] = min_io_size
& 0xff;
614 /* optimal transfer length */
615 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
616 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
617 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
618 outbuf
[15] = opt_io_size
& 0xff;
620 /* optimal unmap granularity */
621 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
622 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
623 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
624 outbuf
[31] = unmap_sectors
& 0xff;
627 case 0xb2: /* thin provisioning */
631 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
632 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
640 assert(buflen
- start
<= 255);
641 outbuf
[start
- 1] = buflen
- start
;
645 /* Standard INQUIRY data */
646 if (req
->cmd
.buf
[2] != 0) {
651 buflen
= req
->cmd
.xfer
;
652 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
653 buflen
= SCSI_MAX_INQUIRY_LEN
;
656 outbuf
[0] = s
->qdev
.type
& 0x1f;
657 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
659 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
660 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
662 memset(&outbuf
[32], 0, 4);
663 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
665 * We claim conformance to SPC-3, which is required for guests
666 * to ask for modern features like READ CAPACITY(16) or the
667 * block characteristics VPD page by default. Not all of SPC-3
668 * is actually implemented, but we're good enough.
671 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
674 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
676 /* If the allocation length of CDB is too small,
677 the additional length is not adjusted */
681 /* Sync data transfer and TCQ. */
682 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
686 static inline bool media_is_dvd(SCSIDiskState
*s
)
689 if (s
->qdev
.type
!= TYPE_ROM
) {
692 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
695 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
696 return nb_sectors
> CD_MAX_SECTORS
;
699 static inline bool media_is_cd(SCSIDiskState
*s
)
702 if (s
->qdev
.type
!= TYPE_ROM
) {
705 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
708 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
709 return nb_sectors
<= CD_MAX_SECTORS
;
712 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
715 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
717 if (s
->qdev
.type
!= TYPE_ROM
) {
721 /* Types 1/2 are only defined for Blu-Ray. */
723 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
727 memset(outbuf
, 0, 34);
729 outbuf
[2] = 0xe; /* last session complete, disc finalized */
730 outbuf
[3] = 1; /* first track on disc */
731 outbuf
[4] = 1; /* # of sessions */
732 outbuf
[5] = 1; /* first track of last session */
733 outbuf
[6] = 1; /* last track of last session */
734 outbuf
[7] = 0x20; /* unrestricted use */
735 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
736 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
737 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
738 /* 24-31: disc bar code */
739 /* 32: disc application code */
740 /* 33: number of OPC tables */
745 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
748 static const int rds_caps_size
[5] = {
755 uint8_t media
= r
->req
.cmd
.buf
[1];
756 uint8_t layer
= r
->req
.cmd
.buf
[6];
757 uint8_t format
= r
->req
.cmd
.buf
[7];
760 if (s
->qdev
.type
!= TYPE_ROM
) {
764 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
768 if (format
!= 0xff) {
769 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
770 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
773 if (media_is_cd(s
)) {
774 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
777 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
780 size
= rds_caps_size
[format
];
781 memset(outbuf
, 0, size
);
786 /* Physical format information */
791 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
793 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
794 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
795 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
796 outbuf
[7] = 0; /* default densities */
798 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
799 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
803 case 0x01: /* DVD copyright information, all zeros */
806 case 0x03: /* BCA information - invalid field for no BCA info */
809 case 0x04: /* DVD disc manufacturing information, all zeros */
812 case 0xff: { /* List capabilities */
815 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
816 if (!rds_caps_size
[i
]) {
820 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
821 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
831 /* Size of buffer, not including 2 byte size field */
832 stw_be_p(outbuf
, size
- 2);
839 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
841 uint8_t event_code
, media_status
;
845 media_status
= MS_TRAY_OPEN
;
846 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
847 media_status
= MS_MEDIA_PRESENT
;
850 /* Event notification descriptor */
851 event_code
= MEC_NO_CHANGE
;
852 if (media_status
!= MS_TRAY_OPEN
) {
853 if (s
->media_event
) {
854 event_code
= MEC_NEW_MEDIA
;
855 s
->media_event
= false;
856 } else if (s
->eject_request
) {
857 event_code
= MEC_EJECT_REQUESTED
;
858 s
->eject_request
= false;
862 outbuf
[0] = event_code
;
863 outbuf
[1] = media_status
;
865 /* These fields are reserved, just clear them. */
871 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
875 uint8_t *buf
= r
->req
.cmd
.buf
;
876 uint8_t notification_class_request
= buf
[4];
877 if (s
->qdev
.type
!= TYPE_ROM
) {
880 if ((buf
[1] & 1) == 0) {
886 outbuf
[0] = outbuf
[1] = 0;
887 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
888 if (notification_class_request
& (1 << GESN_MEDIA
)) {
889 outbuf
[2] = GESN_MEDIA
;
890 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
894 stw_be_p(outbuf
, size
- 4);
898 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
902 if (s
->qdev
.type
!= TYPE_ROM
) {
905 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
906 memset(outbuf
, 0, 40);
907 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
908 stw_be_p(&outbuf
[6], current
);
909 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
910 outbuf
[10] = 0x03; /* persistent, current */
911 outbuf
[11] = 8; /* two profiles */
912 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
913 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
914 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
915 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
916 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
917 stw_be_p(&outbuf
[20], 1);
918 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
920 stl_be_p(&outbuf
[24], 1); /* SCSI */
921 outbuf
[28] = 1; /* DBE = 1, mandatory */
922 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
923 stw_be_p(&outbuf
[32], 3);
924 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
926 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
927 /* TODO: Random readable, CD read, DVD read, drive serial number,
932 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
934 if (s
->qdev
.type
!= TYPE_ROM
) {
937 memset(outbuf
, 0, 8);
938 outbuf
[5] = 1; /* CD-ROM */
942 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
945 static const int mode_sense_valid
[0x3f] = {
946 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
947 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
948 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
949 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
950 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
951 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
954 uint8_t *p
= *p_outbuf
+ 2;
957 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
962 * If Changeable Values are requested, a mask denoting those mode parameters
963 * that are changeable shall be returned. As we currently don't support
964 * parameter changes via MODE_SELECT all bits are returned set to zero.
965 * The buffer was already menset to zero by the caller of this function.
967 * The offsets here are off by two compared to the descriptions in the
968 * SCSI specs, because those include a 2-byte header. This is unfortunate,
969 * but it is done so that offsets are consistent within our implementation
970 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
971 * 2-byte and 4-byte headers.
974 case MODE_PAGE_HD_GEOMETRY
:
976 if (page_control
== 1) { /* Changeable Values */
979 /* if a geometry hint is available, use it */
980 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
981 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
982 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
983 p
[3] = s
->qdev
.conf
.heads
& 0xff;
984 /* Write precomp start cylinder, disabled */
985 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
986 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
987 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
988 /* Reduced current start cylinder, disabled */
989 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
990 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
991 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
992 /* Device step rate [ns], 200ns */
995 /* Landing zone cylinder */
999 /* Medium rotation rate [rpm], 5400 rpm */
1000 p
[18] = (5400 >> 8) & 0xff;
1001 p
[19] = 5400 & 0xff;
1004 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1006 if (page_control
== 1) { /* Changeable Values */
1009 /* Transfer rate [kbit/s], 5Mbit/s */
1012 /* if a geometry hint is available, use it */
1013 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1014 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1015 p
[4] = s
->qdev
.blocksize
>> 8;
1016 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1017 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1018 /* Write precomp start cylinder, disabled */
1019 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1020 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1021 /* Reduced current start cylinder, disabled */
1022 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1023 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1024 /* Device step rate [100us], 100us */
1027 /* Device step pulse width [us], 1us */
1029 /* Device head settle delay [100us], 100us */
1032 /* Motor on delay [0.1s], 0.1s */
1034 /* Motor off delay [0.1s], 0.1s */
1036 /* Medium rotation rate [rpm], 5400 rpm */
1037 p
[26] = (5400 >> 8) & 0xff;
1038 p
[27] = 5400 & 0xff;
1041 case MODE_PAGE_CACHING
:
1043 if (page_control
== 1 || /* Changeable Values */
1044 bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1049 case MODE_PAGE_R_W_ERROR
:
1051 if (page_control
== 1) { /* Changeable Values */
1054 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1055 if (s
->qdev
.type
== TYPE_ROM
) {
1056 p
[1] = 0x20; /* Read Retry Count */
1060 case MODE_PAGE_AUDIO_CTL
:
1064 case MODE_PAGE_CAPABILITIES
:
1066 if (page_control
== 1) { /* Changeable Values */
1070 p
[0] = 0x3b; /* CD-R & CD-RW read */
1071 p
[1] = 0; /* Writing not supported */
1072 p
[2] = 0x7f; /* Audio, composite, digital out,
1073 mode 2 form 1&2, multi session */
1074 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1075 RW corrected, C2 errors, ISRC,
1077 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1078 /* Locking supported, jumper present, eject, tray */
1079 p
[5] = 0; /* no volume & mute control, no
1081 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1082 p
[7] = (50 * 176) & 0xff;
1083 p
[8] = 2 >> 8; /* Two volume levels */
1085 p
[10] = 2048 >> 8; /* 2M buffer */
1086 p
[11] = 2048 & 0xff;
1087 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1088 p
[13] = (16 * 176) & 0xff;
1089 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1090 p
[17] = (16 * 176) & 0xff;
1091 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1092 p
[19] = (16 * 176) & 0xff;
1099 assert(length
< 256);
1100 (*p_outbuf
)[0] = page
;
1101 (*p_outbuf
)[1] = length
;
1102 *p_outbuf
+= length
+ 2;
1106 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1108 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1109 uint64_t nb_sectors
;
1111 int page
, buflen
, ret
, page_control
;
1113 uint8_t dev_specific_param
;
1115 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1116 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1117 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1118 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1119 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
1120 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1123 if (s
->qdev
.type
== TYPE_DISK
) {
1124 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1125 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1126 dev_specific_param
|= 0x80; /* Readonly. */
1129 /* MMC prescribes that CD/DVD drives have no block descriptors,
1130 * and defines no device-specific parameter. */
1131 dev_specific_param
= 0x00;
1135 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1136 p
[1] = 0; /* Default media type. */
1137 p
[2] = dev_specific_param
;
1138 p
[3] = 0; /* Block descriptor length. */
1140 } else { /* MODE_SENSE_10 */
1141 p
[2] = 0; /* Default media type. */
1142 p
[3] = dev_specific_param
;
1143 p
[6] = p
[7] = 0; /* Block descriptor length. */
1147 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1148 if (!dbd
&& nb_sectors
) {
1149 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1150 outbuf
[3] = 8; /* Block descriptor length */
1151 } else { /* MODE_SENSE_10 */
1152 outbuf
[7] = 8; /* Block descriptor length */
1154 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1155 if (nb_sectors
> 0xffffff) {
1158 p
[0] = 0; /* media density code */
1159 p
[1] = (nb_sectors
>> 16) & 0xff;
1160 p
[2] = (nb_sectors
>> 8) & 0xff;
1161 p
[3] = nb_sectors
& 0xff;
1162 p
[4] = 0; /* reserved */
1163 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1164 p
[6] = s
->qdev
.blocksize
>> 8;
1169 if (page_control
== 3) {
1171 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1176 for (page
= 0; page
<= 0x3e; page
++) {
1177 mode_sense_page(s
, page
, &p
, page_control
);
1180 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1186 buflen
= p
- outbuf
;
1188 * The mode data length field specifies the length in bytes of the
1189 * following data that is available to be transferred. The mode data
1190 * length does not include itself.
1192 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1193 outbuf
[0] = buflen
- 1;
1194 } else { /* MODE_SENSE_10 */
1195 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1196 outbuf
[1] = (buflen
- 2) & 0xff;
1201 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1203 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1204 int start_track
, format
, msf
, toclen
;
1205 uint64_t nb_sectors
;
1207 msf
= req
->cmd
.buf
[1] & 2;
1208 format
= req
->cmd
.buf
[2] & 0xf;
1209 start_track
= req
->cmd
.buf
[6];
1210 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1211 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1212 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1215 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1218 /* multi session : only a single session defined */
1220 memset(outbuf
, 0, 12);
1226 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1234 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1236 SCSIRequest
*req
= &r
->req
;
1237 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1238 bool start
= req
->cmd
.buf
[4] & 1;
1239 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1240 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1243 /* eject/load only happens for power condition == 0 */
1247 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1248 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1249 scsi_check_condition(r
,
1250 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1251 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1252 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1256 if (s
->tray_open
!= !start
) {
1257 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1258 s
->tray_open
= !start
;
1264 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1266 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1267 int buflen
= r
->iov
.iov_len
;
1270 DPRINTF("Read buf_len=%d\n", buflen
);
1273 scsi_req_data(&r
->req
, buflen
);
1277 /* This also clears the sense buffer for REQUEST SENSE. */
1278 scsi_req_complete(&r
->req
, GOOD
);
1281 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1282 uint8_t *inbuf
, int inlen
)
1284 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1285 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1287 int len
, expected_len
, changeable_len
, i
;
1289 /* The input buffer does not include the page header, so it is
1292 expected_len
= inlen
+ 2;
1293 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1298 memset(mode_current
, 0, inlen
+ 2);
1299 len
= mode_sense_page(s
, page
, &p
, 0);
1300 if (len
< 0 || len
!= expected_len
) {
1304 p
= mode_changeable
;
1305 memset(mode_changeable
, 0, inlen
+ 2);
1306 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1307 assert(changeable_len
== len
);
1309 /* Check that unchangeable bits are the same as what MODE SENSE
1312 for (i
= 2; i
< len
; i
++) {
1313 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1320 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1323 case MODE_PAGE_CACHING
:
1324 bdrv_set_enable_write_cache(s
->qdev
.conf
.bs
, (p
[0] & 4) != 0);
1332 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1334 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1337 int page
, subpage
, page_len
;
1339 /* Parse both possible formats for the mode page headers. */
1343 goto invalid_param_len
;
1346 page_len
= lduw_be_p(&p
[2]);
1351 goto invalid_param_len
;
1362 if (page_len
> len
) {
1363 goto invalid_param_len
;
1367 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1371 scsi_disk_apply_mode_select(s
, page
, p
);
1380 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1384 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1388 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1390 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1392 int cmd
= r
->req
.cmd
.buf
[0];
1393 int len
= r
->req
.cmd
.xfer
;
1394 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1398 /* We only support PF=1, SP=0. */
1399 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1403 if (len
< hdr_len
) {
1404 goto invalid_param_len
;
1407 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1411 goto invalid_param_len
;
1413 if (bd_len
!= 0 && bd_len
!= 8) {
1420 /* Ensure no change is made if there is an error! */
1421 for (pass
= 0; pass
< 2; pass
++) {
1422 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1427 if (!bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
1428 /* The request is used as the AIO opaque value, so add a ref. */
1429 scsi_req_ref(&r
->req
);
1430 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1431 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
1435 scsi_req_complete(&r
->req
, GOOD
);
1439 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1443 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1447 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1450 static inline bool check_lba_range(SCSIDiskState
*s
,
1451 uint64_t sector_num
, uint32_t nb_sectors
)
1454 * The first line tests that no overflow happens when computing the last
1455 * sector. The second line tests that the last accessed sector is in
1458 * Careful, the computations should not underflow for nb_sectors == 0,
1459 * and a 0-block read to the first LBA beyond the end of device is
1462 return (sector_num
<= sector_num
+ nb_sectors
&&
1463 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1466 typedef struct UnmapCBData
{
1472 static void scsi_unmap_complete(void *opaque
, int ret
)
1474 UnmapCBData
*data
= opaque
;
1475 SCSIDiskReq
*r
= data
->r
;
1476 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1477 uint64_t sector_num
;
1478 uint32_t nb_sectors
;
1480 r
->req
.aiocb
= NULL
;
1482 if (scsi_handle_rw_error(r
, -ret
)) {
1487 if (data
->count
> 0 && !r
->req
.io_canceled
) {
1488 sector_num
= ldq_be_p(&data
->inbuf
[0]);
1489 nb_sectors
= ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
;
1490 if (!check_lba_range(s
, sector_num
, nb_sectors
)) {
1491 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1495 r
->req
.aiocb
= bdrv_aio_discard(s
->qdev
.conf
.bs
,
1496 sector_num
* (s
->qdev
.blocksize
/ 512),
1497 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1498 scsi_unmap_complete
, data
);
1505 if (data
->count
== 0) {
1506 scsi_req_complete(&r
->req
, GOOD
);
1508 if (!r
->req
.io_canceled
) {
1509 scsi_req_unref(&r
->req
);
1514 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1517 int len
= r
->req
.cmd
.xfer
;
1521 goto invalid_param_len
;
1523 if (len
< lduw_be_p(&p
[0]) + 2) {
1524 goto invalid_param_len
;
1526 if (len
< lduw_be_p(&p
[2]) + 8) {
1527 goto invalid_param_len
;
1529 if (lduw_be_p(&p
[2]) & 15) {
1530 goto invalid_param_len
;
1533 data
= g_new0(UnmapCBData
, 1);
1535 data
->inbuf
= &p
[8];
1536 data
->count
= lduw_be_p(&p
[2]) >> 4;
1538 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1539 scsi_req_ref(&r
->req
);
1540 scsi_unmap_complete(data
, 0);
1544 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1547 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1549 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1551 if (r
->iov
.iov_len
) {
1552 int buflen
= r
->iov
.iov_len
;
1553 DPRINTF("Write buf_len=%d\n", buflen
);
1555 scsi_req_data(&r
->req
, buflen
);
1559 switch (req
->cmd
.buf
[0]) {
1561 case MODE_SELECT_10
:
1562 /* This also clears the sense buffer for REQUEST SENSE. */
1563 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1567 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1575 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1577 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1578 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1579 uint64_t nb_sectors
;
1583 switch (req
->cmd
.buf
[0]) {
1592 case ALLOW_MEDIUM_REMOVAL
:
1593 case GET_CONFIGURATION
:
1594 case GET_EVENT_STATUS_NOTIFICATION
:
1595 case MECHANISM_STATUS
:
1600 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1601 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1608 * FIXME: we shouldn't return anything bigger than 4k, but the code
1609 * requires the buffer to be as big as req->cmd.xfer in several
1610 * places. So, do not allow CDBs with a very large ALLOCATION
1611 * LENGTH. The real fix would be to modify scsi_read_data and
1612 * dma_buf_read, so that they return data beyond the buflen
1615 if (req
->cmd
.xfer
> 65536) {
1616 goto illegal_request
;
1618 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1620 if (!r
->iov
.iov_base
) {
1621 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1624 buflen
= req
->cmd
.xfer
;
1625 outbuf
= r
->iov
.iov_base
;
1626 memset(outbuf
, 0, r
->buflen
);
1627 switch (req
->cmd
.buf
[0]) {
1628 case TEST_UNIT_READY
:
1629 assert(!s
->tray_open
&& bdrv_is_inserted(s
->qdev
.conf
.bs
));
1632 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1634 goto illegal_request
;
1639 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1641 goto illegal_request
;
1645 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1647 goto illegal_request
;
1651 if (req
->cmd
.buf
[1] & 1) {
1652 goto illegal_request
;
1656 if (req
->cmd
.buf
[1] & 3) {
1657 goto illegal_request
;
1661 if (req
->cmd
.buf
[1] & 1) {
1662 goto illegal_request
;
1666 if (req
->cmd
.buf
[1] & 3) {
1667 goto illegal_request
;
1671 if (scsi_disk_emulate_start_stop(r
) < 0) {
1675 case ALLOW_MEDIUM_REMOVAL
:
1676 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1677 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1679 case READ_CAPACITY_10
:
1680 /* The normal LEN field for this command is zero. */
1681 memset(outbuf
, 0, 8);
1682 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1684 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1687 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1688 goto illegal_request
;
1690 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1691 /* Returned value is the address of the last sector. */
1693 /* Remember the new size for read/write sanity checking. */
1694 s
->qdev
.max_lba
= nb_sectors
;
1695 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1696 if (nb_sectors
> UINT32_MAX
) {
1697 nb_sectors
= UINT32_MAX
;
1699 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1700 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1701 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1702 outbuf
[3] = nb_sectors
& 0xff;
1705 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1709 /* Just return "NO SENSE". */
1710 buflen
= scsi_build_sense(NULL
, 0, outbuf
, r
->buflen
,
1711 (req
->cmd
.buf
[1] & 1) == 0);
1713 goto illegal_request
;
1716 case MECHANISM_STATUS
:
1717 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1719 goto illegal_request
;
1722 case GET_CONFIGURATION
:
1723 buflen
= scsi_get_configuration(s
, outbuf
);
1725 goto illegal_request
;
1728 case GET_EVENT_STATUS_NOTIFICATION
:
1729 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1731 goto illegal_request
;
1734 case READ_DISC_INFORMATION
:
1735 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
1737 goto illegal_request
;
1740 case READ_DVD_STRUCTURE
:
1741 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1743 goto illegal_request
;
1746 case SERVICE_ACTION_IN_16
:
1747 /* Service Action In subcommands. */
1748 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1749 DPRINTF("SAI READ CAPACITY(16)\n");
1750 memset(outbuf
, 0, req
->cmd
.xfer
);
1751 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1753 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1756 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1757 goto illegal_request
;
1759 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1760 /* Returned value is the address of the last sector. */
1762 /* Remember the new size for read/write sanity checking. */
1763 s
->qdev
.max_lba
= nb_sectors
;
1764 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1765 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1766 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1767 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1768 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1769 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1770 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1771 outbuf
[7] = nb_sectors
& 0xff;
1774 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1777 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1779 /* set TPE bit if the format supports discard */
1780 if (s
->qdev
.conf
.discard_granularity
) {
1784 /* Protection, exponent and lowest lba field left blank. */
1787 DPRINTF("Unsupported Service Action In\n");
1788 goto illegal_request
;
1789 case SYNCHRONIZE_CACHE
:
1790 /* The request is used as the AIO opaque value, so add a ref. */
1791 scsi_req_ref(&r
->req
);
1792 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1793 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_aio_complete
, r
);
1796 DPRINTF("Seek(10) (sector %" PRId64
")\n", r
->req
.cmd
.lba
);
1797 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1802 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1804 case MODE_SELECT_10
:
1805 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1808 DPRINTF("Unmap (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1812 nb_sectors
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
1813 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1814 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1817 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1822 * We only support WRITE SAME with the unmap bit set for now.
1824 if (!(req
->cmd
.buf
[1] & 0x8)) {
1825 goto illegal_request
;
1828 /* The request is used as the AIO opaque value, so add a ref. */
1829 scsi_req_ref(&r
->req
);
1830 r
->req
.aiocb
= bdrv_aio_discard(s
->qdev
.conf
.bs
,
1831 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1832 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1833 scsi_aio_complete
, r
);
1836 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1837 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1840 assert(!r
->req
.aiocb
);
1841 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
1842 if (r
->iov
.iov_len
== 0) {
1843 scsi_req_complete(&r
->req
, GOOD
);
1845 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1846 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
1847 return -r
->iov
.iov_len
;
1849 return r
->iov
.iov_len
;
1853 if (r
->req
.status
== -1) {
1854 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1859 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1863 /* Execute a scsi command. Returns the length of the data expected by the
1864 command. This will be Positive for data transfers from the device
1865 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1866 and zero if the command does not transfer any data. */
1868 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
1870 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1871 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1877 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1878 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1882 len
= scsi_data_cdb_length(r
->req
.cmd
.buf
);
1888 DPRINTF("Read (sector %" PRId64
", count %u)\n", r
->req
.cmd
.lba
, len
);
1889 if (r
->req
.cmd
.buf
[1] & 0xe0) {
1890 goto illegal_request
;
1892 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
1895 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1896 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1902 case WRITE_VERIFY_10
:
1903 case WRITE_VERIFY_12
:
1904 case WRITE_VERIFY_16
:
1905 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
1906 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1913 DPRINTF("Write %s(sector %" PRId64
", count %u)\n",
1914 (command
& 0xe) == 0xe ? "And Verify " : "",
1915 r
->req
.cmd
.lba
, len
);
1916 if (r
->req
.cmd
.buf
[1] & 0xe0) {
1917 goto illegal_request
;
1919 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
1922 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1923 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1928 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1931 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1934 if (r
->sector_count
== 0) {
1935 scsi_req_complete(&r
->req
, GOOD
);
1937 assert(r
->iov
.iov_len
== 0);
1938 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1939 return -r
->sector_count
* 512;
1941 return r
->sector_count
* 512;
1945 static void scsi_disk_reset(DeviceState
*dev
)
1947 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1948 uint64_t nb_sectors
;
1950 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1952 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1953 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1957 s
->qdev
.max_lba
= nb_sectors
;
1960 static void scsi_destroy(SCSIDevice
*dev
)
1962 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1964 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1965 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1968 static void scsi_disk_resize_cb(void *opaque
)
1970 SCSIDiskState
*s
= opaque
;
1972 /* SPC lists this sense code as available only for
1973 * direct-access devices.
1975 if (s
->qdev
.type
== TYPE_DISK
) {
1976 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
1980 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1982 SCSIDiskState
*s
= opaque
;
1985 * When a CD gets changed, we have to report an ejected state and
1986 * then a loaded state to guests so that they detect tray
1987 * open/close and media change events. Guests that do not use
1988 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1989 * states rely on this behavior.
1991 * media_changed governs the state machine used for unit attention
1992 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1994 s
->media_changed
= load
;
1995 s
->tray_open
= !load
;
1996 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
1997 s
->media_event
= true;
1998 s
->eject_request
= false;
2001 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2003 SCSIDiskState
*s
= opaque
;
2005 s
->eject_request
= true;
2007 s
->tray_locked
= false;
2011 static bool scsi_cd_is_tray_open(void *opaque
)
2013 return ((SCSIDiskState
*)opaque
)->tray_open
;
2016 static bool scsi_cd_is_medium_locked(void *opaque
)
2018 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2021 static const BlockDevOps scsi_disk_removable_block_ops
= {
2022 .change_media_cb
= scsi_cd_change_media_cb
,
2023 .eject_request_cb
= scsi_cd_eject_request_cb
,
2024 .is_tray_open
= scsi_cd_is_tray_open
,
2025 .is_medium_locked
= scsi_cd_is_medium_locked
,
2027 .resize_cb
= scsi_disk_resize_cb
,
2030 static const BlockDevOps scsi_disk_block_ops
= {
2031 .resize_cb
= scsi_disk_resize_cb
,
2034 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2036 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2037 if (s
->media_changed
) {
2038 s
->media_changed
= false;
2039 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2043 static int scsi_initfn(SCSIDevice
*dev
)
2045 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2047 if (!s
->qdev
.conf
.bs
) {
2048 error_report("drive property not set");
2052 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2053 !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
2054 error_report("Device needs media, but drive is empty");
2058 blkconf_serial(&s
->qdev
.conf
, &s
->serial
);
2059 if (dev
->type
== TYPE_DISK
2060 && blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255) < 0) {
2064 if (s
->qdev
.conf
.discard_granularity
== -1) {
2065 s
->qdev
.conf
.discard_granularity
=
2066 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2070 s
->version
= g_strdup(qemu_get_version());
2073 s
->vendor
= g_strdup("QEMU");
2076 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
2077 error_report("unwanted /dev/sg*");
2081 if (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) {
2082 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_removable_block_ops
, s
);
2084 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_disk_block_ops
, s
);
2086 bdrv_set_buffer_alignment(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
2088 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
2089 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, NULL
);
2093 static int scsi_hd_initfn(SCSIDevice
*dev
)
2095 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2096 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2097 s
->qdev
.type
= TYPE_DISK
;
2099 s
->product
= g_strdup("QEMU HARDDISK");
2101 return scsi_initfn(&s
->qdev
);
2104 static int scsi_cd_initfn(SCSIDevice
*dev
)
2106 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2107 s
->qdev
.blocksize
= 2048;
2108 s
->qdev
.type
= TYPE_ROM
;
2109 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2111 s
->product
= g_strdup("QEMU CD-ROM");
2113 return scsi_initfn(&s
->qdev
);
2116 static int scsi_disk_initfn(SCSIDevice
*dev
)
2120 if (!dev
->conf
.bs
) {
2121 return scsi_initfn(dev
); /* ... and die there */
2124 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
2125 if (dinfo
->media_cd
) {
2126 return scsi_cd_initfn(dev
);
2128 return scsi_hd_initfn(dev
);
2132 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2133 .size
= sizeof(SCSIDiskReq
),
2134 .free_req
= scsi_free_request
,
2135 .send_command
= scsi_disk_emulate_command
,
2136 .read_data
= scsi_disk_emulate_read_data
,
2137 .write_data
= scsi_disk_emulate_write_data
,
2138 .get_buf
= scsi_get_buf
,
2141 static const SCSIReqOps scsi_disk_dma_reqops
= {
2142 .size
= sizeof(SCSIDiskReq
),
2143 .free_req
= scsi_free_request
,
2144 .send_command
= scsi_disk_dma_command
,
2145 .read_data
= scsi_read_data
,
2146 .write_data
= scsi_write_data
,
2147 .cancel_io
= scsi_cancel_io
,
2148 .get_buf
= scsi_get_buf
,
2149 .load_request
= scsi_disk_load_request
,
2150 .save_request
= scsi_disk_save_request
,
2153 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2154 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2155 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2156 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2157 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2158 [START_STOP
] = &scsi_disk_emulate_reqops
,
2159 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2160 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2161 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2162 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2163 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2164 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2165 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2166 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2167 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2168 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2169 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2170 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2171 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2172 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2173 [UNMAP
] = &scsi_disk_emulate_reqops
,
2174 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2175 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2177 [READ_6
] = &scsi_disk_dma_reqops
,
2178 [READ_10
] = &scsi_disk_dma_reqops
,
2179 [READ_12
] = &scsi_disk_dma_reqops
,
2180 [READ_16
] = &scsi_disk_dma_reqops
,
2181 [VERIFY_10
] = &scsi_disk_dma_reqops
,
2182 [VERIFY_12
] = &scsi_disk_dma_reqops
,
2183 [VERIFY_16
] = &scsi_disk_dma_reqops
,
2184 [WRITE_6
] = &scsi_disk_dma_reqops
,
2185 [WRITE_10
] = &scsi_disk_dma_reqops
,
2186 [WRITE_12
] = &scsi_disk_dma_reqops
,
2187 [WRITE_16
] = &scsi_disk_dma_reqops
,
2188 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2189 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2190 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2193 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2194 uint8_t *buf
, void *hba_private
)
2196 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2198 const SCSIReqOps
*ops
;
2202 ops
= scsi_disk_reqops_dispatch
[command
];
2204 ops
= &scsi_disk_emulate_reqops
;
2206 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2209 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
2212 for (i
= 1; i
< req
->cmd
.len
; i
++) {
2213 printf(" 0x%02x", buf
[i
]);
2223 static int get_device_type(SCSIDiskState
*s
)
2225 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
2228 uint8_t sensebuf
[8];
2229 sg_io_hdr_t io_header
;
2232 memset(cmd
, 0, sizeof(cmd
));
2233 memset(buf
, 0, sizeof(buf
));
2235 cmd
[4] = sizeof(buf
);
2237 memset(&io_header
, 0, sizeof(io_header
));
2238 io_header
.interface_id
= 'S';
2239 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2240 io_header
.dxfer_len
= sizeof(buf
);
2241 io_header
.dxferp
= buf
;
2242 io_header
.cmdp
= cmd
;
2243 io_header
.cmd_len
= sizeof(cmd
);
2244 io_header
.mx_sb_len
= sizeof(sensebuf
);
2245 io_header
.sbp
= sensebuf
;
2246 io_header
.timeout
= 6000; /* XXX */
2248 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
2249 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
2252 s
->qdev
.type
= buf
[0];
2253 if (buf
[1] & 0x80) {
2254 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2259 static int scsi_block_initfn(SCSIDevice
*dev
)
2261 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2265 if (!s
->qdev
.conf
.bs
) {
2266 error_report("scsi-block: drive property not set");
2270 /* check we are using a driver managing SG_IO (version 3 and after) */
2271 if (bdrv_ioctl(s
->qdev
.conf
.bs
, SG_GET_VERSION_NUM
, &sg_version
) < 0 ||
2272 sg_version
< 30000) {
2273 error_report("scsi-block: scsi generic interface too old");
2277 /* get device type from INQUIRY data */
2278 rc
= get_device_type(s
);
2280 error_report("scsi-block: INQUIRY failed");
2284 /* Make a guess for the block size, we'll fix it when the guest sends.
2285 * READ CAPACITY. If they don't, they likely would assume these sizes
2286 * anyway. (TODO: check in /sys).
2288 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2289 s
->qdev
.blocksize
= 2048;
2291 s
->qdev
.blocksize
= 512;
2293 return scsi_initfn(&s
->qdev
);
2296 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2297 uint32_t lun
, uint8_t *buf
,
2300 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2314 case WRITE_VERIFY_10
:
2315 case WRITE_VERIFY_12
:
2316 case WRITE_VERIFY_16
:
2317 /* If we are not using O_DIRECT, we might read stale data from the
2318 * host cache if writes were made using other commands than these
2319 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2320 * O_DIRECT everything must go through SG_IO.
2322 if (bdrv_get_flags(s
->qdev
.conf
.bs
) & BDRV_O_NOCACHE
) {
2326 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2327 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2328 * And once you do these writes, reading from the block device is
2329 * unreliable, too. It is even possible that reads deliver random data
2330 * from the host page cache (this is probably a Linux bug).
2332 * We might use scsi_disk_dma_reqops as long as no writing commands are
2333 * seen, but performance usually isn't paramount on optical media. So,
2334 * just make scsi-block operate the same as scsi-generic for them.
2336 if (s
->qdev
.type
!= TYPE_ROM
) {
2337 return scsi_req_alloc(&scsi_disk_dma_reqops
, &s
->qdev
, tag
, lun
,
2342 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2347 #define DEFINE_SCSI_DISK_PROPERTIES() \
2348 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2349 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2350 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2351 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2352 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2354 static Property scsi_hd_properties
[] = {
2355 DEFINE_SCSI_DISK_PROPERTIES(),
2356 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2357 SCSI_DISK_F_REMOVABLE
, false),
2358 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2359 SCSI_DISK_F_DPOFUA
, false),
2360 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2361 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
2362 DEFINE_PROP_END_OF_LIST(),
2365 static const VMStateDescription vmstate_scsi_disk_state
= {
2366 .name
= "scsi-disk",
2368 .minimum_version_id
= 1,
2369 .minimum_version_id_old
= 1,
2370 .fields
= (VMStateField
[]) {
2371 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
2372 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
2373 VMSTATE_BOOL(media_event
, SCSIDiskState
),
2374 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
2375 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
2376 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
2377 VMSTATE_END_OF_LIST()
2381 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
2383 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2384 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2386 sc
->init
= scsi_hd_initfn
;
2387 sc
->destroy
= scsi_destroy
;
2388 sc
->alloc_req
= scsi_new_request
;
2389 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2390 dc
->fw_name
= "disk";
2391 dc
->desc
= "virtual SCSI disk";
2392 dc
->reset
= scsi_disk_reset
;
2393 dc
->props
= scsi_hd_properties
;
2394 dc
->vmsd
= &vmstate_scsi_disk_state
;
2397 static const TypeInfo scsi_hd_info
= {
2399 .parent
= TYPE_SCSI_DEVICE
,
2400 .instance_size
= sizeof(SCSIDiskState
),
2401 .class_init
= scsi_hd_class_initfn
,
2404 static Property scsi_cd_properties
[] = {
2405 DEFINE_SCSI_DISK_PROPERTIES(),
2406 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2407 DEFINE_PROP_END_OF_LIST(),
2410 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
2412 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2413 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2415 sc
->init
= scsi_cd_initfn
;
2416 sc
->destroy
= scsi_destroy
;
2417 sc
->alloc_req
= scsi_new_request
;
2418 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2419 dc
->fw_name
= "disk";
2420 dc
->desc
= "virtual SCSI CD-ROM";
2421 dc
->reset
= scsi_disk_reset
;
2422 dc
->props
= scsi_cd_properties
;
2423 dc
->vmsd
= &vmstate_scsi_disk_state
;
2426 static const TypeInfo scsi_cd_info
= {
2428 .parent
= TYPE_SCSI_DEVICE
,
2429 .instance_size
= sizeof(SCSIDiskState
),
2430 .class_init
= scsi_cd_class_initfn
,
2434 static Property scsi_block_properties
[] = {
2435 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.bs
),
2436 DEFINE_PROP_INT32("bootindex", SCSIDiskState
, qdev
.conf
.bootindex
, -1),
2437 DEFINE_PROP_END_OF_LIST(),
2440 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
2442 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2443 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2445 sc
->init
= scsi_block_initfn
;
2446 sc
->destroy
= scsi_destroy
;
2447 sc
->alloc_req
= scsi_block_new_request
;
2448 dc
->fw_name
= "disk";
2449 dc
->desc
= "SCSI block device passthrough";
2450 dc
->reset
= scsi_disk_reset
;
2451 dc
->props
= scsi_block_properties
;
2452 dc
->vmsd
= &vmstate_scsi_disk_state
;
2455 static const TypeInfo scsi_block_info
= {
2456 .name
= "scsi-block",
2457 .parent
= TYPE_SCSI_DEVICE
,
2458 .instance_size
= sizeof(SCSIDiskState
),
2459 .class_init
= scsi_block_class_initfn
,
2463 static Property scsi_disk_properties
[] = {
2464 DEFINE_SCSI_DISK_PROPERTIES(),
2465 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2466 SCSI_DISK_F_REMOVABLE
, false),
2467 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2468 SCSI_DISK_F_DPOFUA
, false),
2469 DEFINE_PROP_HEX64("wwn", SCSIDiskState
, wwn
, 0),
2470 DEFINE_PROP_END_OF_LIST(),
2473 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
2475 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2476 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2478 sc
->init
= scsi_disk_initfn
;
2479 sc
->destroy
= scsi_destroy
;
2480 sc
->alloc_req
= scsi_new_request
;
2481 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2482 dc
->fw_name
= "disk";
2483 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
2484 dc
->reset
= scsi_disk_reset
;
2485 dc
->props
= scsi_disk_properties
;
2486 dc
->vmsd
= &vmstate_scsi_disk_state
;
2489 static const TypeInfo scsi_disk_info
= {
2490 .name
= "scsi-disk",
2491 .parent
= TYPE_SCSI_DEVICE
,
2492 .instance_size
= sizeof(SCSIDiskState
),
2493 .class_init
= scsi_disk_class_initfn
,
2496 static void scsi_disk_register_types(void)
2498 type_register_static(&scsi_hd_info
);
2499 type_register_static(&scsi_cd_info
);
2501 type_register_static(&scsi_block_info
);
2503 type_register_static(&scsi_disk_info
);
2506 type_init(scsi_disk_register_types
)