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/osdep.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "hw/scsi/scsi.h"
35 #include "block/scsi.h"
36 #include "sysemu/sysemu.h"
37 #include "sysemu/block-backend.h"
38 #include "sysemu/blockdev.h"
39 #include "hw/block/block.h"
40 #include "sysemu/dma.h"
41 #include "qemu/cutils.h"
47 #define SCSI_WRITE_SAME_MAX 524288
48 #define SCSI_DMA_BUF_SIZE 131072
49 #define SCSI_MAX_INQUIRY_LEN 256
50 #define SCSI_MAX_MODE_LEN 256
52 #define DEFAULT_DISCARD_GRANULARITY 4096
53 #define DEFAULT_MAX_UNMAP_SIZE (1 << 30) /* 1 GB */
54 #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */
56 #define TYPE_SCSI_DISK_BASE "scsi-disk-base"
58 #define SCSI_DISK_BASE(obj) \
59 OBJECT_CHECK(SCSIDiskState, (obj), TYPE_SCSI_DISK_BASE)
60 #define SCSI_DISK_BASE_CLASS(klass) \
61 OBJECT_CLASS_CHECK(SCSIDiskClass, (klass), TYPE_SCSI_DISK_BASE)
62 #define SCSI_DISK_BASE_GET_CLASS(obj) \
63 OBJECT_GET_CLASS(SCSIDiskClass, (obj), TYPE_SCSI_DISK_BASE)
65 typedef struct SCSIDiskClass
{
66 SCSIDeviceClass parent_class
;
68 DMAIOFunc
*dma_writev
;
69 bool (*need_fua_emulation
)(SCSICommand
*cmd
);
72 typedef struct SCSIDiskReq
{
74 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
76 uint32_t sector_count
;
79 bool need_fua_emulation
;
83 unsigned char *status
;
86 #define SCSI_DISK_F_REMOVABLE 0
87 #define SCSI_DISK_F_DPOFUA 1
88 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2
90 typedef struct SCSIDiskState
98 uint64_t max_unmap_size
;
109 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, bool acct_failed
);
111 static void scsi_free_request(SCSIRequest
*req
)
113 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
115 qemu_vfree(r
->iov
.iov_base
);
118 /* Helper function for command completion with sense. */
119 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
121 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
122 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
123 scsi_req_build_sense(&r
->req
, sense
);
124 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
127 static void scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
129 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
131 if (!r
->iov
.iov_base
) {
133 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
135 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
136 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
139 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
141 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
143 qemu_put_be64s(f
, &r
->sector
);
144 qemu_put_be32s(f
, &r
->sector_count
);
145 qemu_put_be32s(f
, &r
->buflen
);
147 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
148 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
149 } else if (!req
->retry
) {
150 uint32_t len
= r
->iov
.iov_len
;
151 qemu_put_be32s(f
, &len
);
152 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
157 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
159 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
161 qemu_get_be64s(f
, &r
->sector
);
162 qemu_get_be32s(f
, &r
->sector_count
);
163 qemu_get_be32s(f
, &r
->buflen
);
165 scsi_init_iovec(r
, r
->buflen
);
166 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
167 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
168 } else if (!r
->req
.retry
) {
170 qemu_get_be32s(f
, &len
);
171 r
->iov
.iov_len
= len
;
172 assert(r
->iov
.iov_len
<= r
->buflen
);
173 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
177 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
180 static bool scsi_disk_req_check_error(SCSIDiskReq
*r
, int ret
, bool acct_failed
)
182 if (r
->req
.io_canceled
) {
183 scsi_req_cancel_complete(&r
->req
);
188 return scsi_handle_rw_error(r
, -ret
, acct_failed
);
191 if (r
->status
&& *r
->status
) {
193 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
194 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
196 scsi_req_complete(&r
->req
, *r
->status
);
203 static void scsi_aio_complete(void *opaque
, int ret
)
205 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
206 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
208 assert(r
->req
.aiocb
!= NULL
);
210 if (scsi_disk_req_check_error(r
, ret
, true)) {
214 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
215 scsi_req_complete(&r
->req
, GOOD
);
218 scsi_req_unref(&r
->req
);
221 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
223 switch (cmd
->buf
[0]) {
230 return (cmd
->buf
[1] & 8) != 0;
235 case WRITE_VERIFY_10
:
236 case WRITE_VERIFY_12
:
237 case WRITE_VERIFY_16
:
247 static void scsi_write_do_fua(SCSIDiskReq
*r
)
249 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
251 assert(r
->req
.aiocb
== NULL
);
252 assert(!r
->req
.io_canceled
);
254 if (r
->need_fua_emulation
) {
255 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
257 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
261 scsi_req_complete(&r
->req
, GOOD
);
262 scsi_req_unref(&r
->req
);
265 static void scsi_dma_complete_noio(SCSIDiskReq
*r
, int ret
)
267 assert(r
->req
.aiocb
== NULL
);
268 if (scsi_disk_req_check_error(r
, ret
, false)) {
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 scsi_req_unref(&r
->req
);
285 static void scsi_dma_complete(void *opaque
, int ret
)
287 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
288 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
290 assert(r
->req
.aiocb
!= NULL
);
294 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
296 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
298 scsi_dma_complete_noio(r
, ret
);
301 static void scsi_read_complete(void * opaque
, int ret
)
303 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
304 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
307 assert(r
->req
.aiocb
!= NULL
);
309 if (scsi_disk_req_check_error(r
, ret
, true)) {
313 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
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 scsi_req_unref(&r
->req
);
325 /* Actually issue a read to the block device. */
326 static void scsi_do_read(SCSIDiskReq
*r
, int ret
)
328 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
329 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
331 assert (r
->req
.aiocb
== NULL
);
332 if (scsi_disk_req_check_error(r
, ret
, false)) {
336 /* The request is used as the AIO opaque value, so add a ref. */
337 scsi_req_ref(&r
->req
);
340 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_READ
);
341 r
->req
.resid
-= r
->req
.sg
->size
;
342 r
->req
.aiocb
= dma_blk_io(blk_get_aio_context(s
->qdev
.conf
.blk
),
343 r
->req
.sg
, r
->sector
<< BDRV_SECTOR_BITS
,
344 sdc
->dma_readv
, r
, scsi_dma_complete
, r
,
345 DMA_DIRECTION_FROM_DEVICE
);
347 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
348 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
349 r
->qiov
.size
, BLOCK_ACCT_READ
);
350 r
->req
.aiocb
= sdc
->dma_readv(r
->sector
<< BDRV_SECTOR_BITS
, &r
->qiov
,
351 scsi_read_complete
, r
, r
);
355 scsi_req_unref(&r
->req
);
358 static void scsi_do_read_cb(void *opaque
, int ret
)
360 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
361 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
363 assert (r
->req
.aiocb
!= NULL
);
367 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
369 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
371 scsi_do_read(opaque
, ret
);
374 /* Read more data from scsi device into buffer. */
375 static void scsi_read_data(SCSIRequest
*req
)
377 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
378 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
381 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
382 if (r
->sector_count
== 0) {
383 /* This also clears the sense buffer for REQUEST SENSE. */
384 scsi_req_complete(&r
->req
, GOOD
);
388 /* No data transfer may already be in progress */
389 assert(r
->req
.aiocb
== NULL
);
391 /* The request is used as the AIO opaque value, so add a ref. */
392 scsi_req_ref(&r
->req
);
393 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
394 DPRINTF("Data transfer direction invalid\n");
395 scsi_read_complete(r
, -EINVAL
);
400 scsi_read_complete(r
, -ENOMEDIUM
);
406 if (first
&& r
->need_fua_emulation
) {
407 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
409 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_do_read_cb
, r
);
416 * scsi_handle_rw_error has two return values. 0 means that the error
417 * must be ignored, 1 means that the error has been processed and the
418 * caller should not do anything else for this request. Note that
419 * scsi_handle_rw_error always manages its reference counts, independent
420 * of the return value.
422 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, bool acct_failed
)
424 bool is_read
= (r
->req
.cmd
.mode
== SCSI_XFER_FROM_DEV
);
425 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
426 BlockErrorAction action
= blk_get_error_action(s
->qdev
.conf
.blk
,
429 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
431 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
435 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
438 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
441 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
444 scsi_check_condition(r
, SENSE_CODE(SPACE_ALLOC_FAILED
));
447 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
451 blk_error_action(s
->qdev
.conf
.blk
, action
, is_read
, error
);
452 if (action
== BLOCK_ERROR_ACTION_STOP
) {
453 scsi_req_retry(&r
->req
);
455 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
458 static void scsi_write_complete_noio(SCSIDiskReq
*r
, int ret
)
462 assert (r
->req
.aiocb
== NULL
);
463 if (scsi_disk_req_check_error(r
, ret
, false)) {
467 n
= r
->qiov
.size
/ 512;
469 r
->sector_count
-= n
;
470 if (r
->sector_count
== 0) {
471 scsi_write_do_fua(r
);
474 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
475 DPRINTF("Write complete tag=0x%x more=%zd\n", r
->req
.tag
, r
->qiov
.size
);
476 scsi_req_data(&r
->req
, r
->qiov
.size
);
480 scsi_req_unref(&r
->req
);
483 static void scsi_write_complete(void * opaque
, int ret
)
485 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
486 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
488 assert (r
->req
.aiocb
!= NULL
);
492 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
494 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
496 scsi_write_complete_noio(r
, ret
);
499 static void scsi_write_data(SCSIRequest
*req
)
501 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
502 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
503 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
505 /* No data transfer may already be in progress */
506 assert(r
->req
.aiocb
== NULL
);
508 /* The request is used as the AIO opaque value, so add a ref. */
509 scsi_req_ref(&r
->req
);
510 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
511 DPRINTF("Data transfer direction invalid\n");
512 scsi_write_complete_noio(r
, -EINVAL
);
516 if (!r
->req
.sg
&& !r
->qiov
.size
) {
517 /* Called for the first time. Ask the driver to send us more data. */
519 scsi_write_complete_noio(r
, 0);
523 scsi_write_complete_noio(r
, -ENOMEDIUM
);
527 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
528 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
530 scsi_dma_complete_noio(r
, 0);
532 scsi_write_complete_noio(r
, 0);
538 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_WRITE
);
539 r
->req
.resid
-= r
->req
.sg
->size
;
540 r
->req
.aiocb
= dma_blk_io(blk_get_aio_context(s
->qdev
.conf
.blk
),
541 r
->req
.sg
, r
->sector
<< BDRV_SECTOR_BITS
,
542 sdc
->dma_writev
, r
, scsi_dma_complete
, r
,
543 DMA_DIRECTION_TO_DEVICE
);
545 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
546 r
->qiov
.size
, BLOCK_ACCT_WRITE
);
547 r
->req
.aiocb
= sdc
->dma_writev(r
->sector
<< BDRV_SECTOR_BITS
, &r
->qiov
,
548 scsi_write_complete
, r
, r
);
552 /* Return a pointer to the data buffer. */
553 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
555 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
557 return (uint8_t *)r
->iov
.iov_base
;
560 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
562 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
566 if (req
->cmd
.buf
[1] & 0x1) {
567 /* Vital product data */
568 uint8_t page_code
= req
->cmd
.buf
[2];
570 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
571 outbuf
[buflen
++] = page_code
; // this page
572 outbuf
[buflen
++] = 0x00;
573 outbuf
[buflen
++] = 0x00;
577 case 0x00: /* Supported page codes, mandatory */
579 DPRINTF("Inquiry EVPD[Supported pages] "
580 "buffer size %zd\n", req
->cmd
.xfer
);
581 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
583 outbuf
[buflen
++] = 0x80; // unit serial number
585 outbuf
[buflen
++] = 0x83; // device identification
586 if (s
->qdev
.type
== TYPE_DISK
) {
587 outbuf
[buflen
++] = 0xb0; // block limits
588 outbuf
[buflen
++] = 0xb2; // thin provisioning
592 case 0x80: /* Device serial number, optional */
597 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
601 l
= strlen(s
->serial
);
606 DPRINTF("Inquiry EVPD[Serial number] "
607 "buffer size %zd\n", req
->cmd
.xfer
);
608 memcpy(outbuf
+buflen
, s
->serial
, l
);
613 case 0x83: /* Device identification page, mandatory */
615 const char *str
= s
->serial
?: blk_name(s
->qdev
.conf
.blk
);
616 int max_len
= s
->serial
? 20 : 255 - 8;
617 int id_len
= strlen(str
);
619 if (id_len
> max_len
) {
622 DPRINTF("Inquiry EVPD[Device identification] "
623 "buffer size %zd\n", req
->cmd
.xfer
);
625 outbuf
[buflen
++] = 0x2; // ASCII
626 outbuf
[buflen
++] = 0; // not officially assigned
627 outbuf
[buflen
++] = 0; // reserved
628 outbuf
[buflen
++] = id_len
; // length of data following
629 memcpy(outbuf
+buflen
, str
, id_len
);
633 outbuf
[buflen
++] = 0x1; // Binary
634 outbuf
[buflen
++] = 0x3; // NAA
635 outbuf
[buflen
++] = 0; // reserved
636 outbuf
[buflen
++] = 8;
637 stq_be_p(&outbuf
[buflen
], s
->qdev
.wwn
);
641 if (s
->qdev
.port_wwn
) {
642 outbuf
[buflen
++] = 0x61; // SAS / Binary
643 outbuf
[buflen
++] = 0x93; // PIV / Target port / NAA
644 outbuf
[buflen
++] = 0; // reserved
645 outbuf
[buflen
++] = 8;
646 stq_be_p(&outbuf
[buflen
], s
->qdev
.port_wwn
);
651 outbuf
[buflen
++] = 0x61; // SAS / Binary
652 outbuf
[buflen
++] = 0x94; // PIV / Target port / relative target port
653 outbuf
[buflen
++] = 0; // reserved
654 outbuf
[buflen
++] = 4;
655 stw_be_p(&outbuf
[buflen
+ 2], s
->port_index
);
660 case 0xb0: /* block limits */
662 unsigned int unmap_sectors
=
663 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
664 unsigned int min_io_size
=
665 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
666 unsigned int opt_io_size
=
667 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
668 unsigned int max_unmap_sectors
=
669 s
->max_unmap_size
/ s
->qdev
.blocksize
;
670 unsigned int max_io_sectors
=
671 s
->max_io_size
/ s
->qdev
.blocksize
;
673 if (s
->qdev
.type
== TYPE_ROM
) {
674 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
678 /* required VPD size with unmap support */
680 memset(outbuf
+ 4, 0, buflen
- 4);
682 outbuf
[4] = 0x1; /* wsnz */
684 /* optimal transfer length granularity */
685 outbuf
[6] = (min_io_size
>> 8) & 0xff;
686 outbuf
[7] = min_io_size
& 0xff;
688 /* maximum transfer length */
689 outbuf
[8] = (max_io_sectors
>> 24) & 0xff;
690 outbuf
[9] = (max_io_sectors
>> 16) & 0xff;
691 outbuf
[10] = (max_io_sectors
>> 8) & 0xff;
692 outbuf
[11] = max_io_sectors
& 0xff;
694 /* optimal transfer length */
695 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
696 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
697 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
698 outbuf
[15] = opt_io_size
& 0xff;
700 /* max unmap LBA count, default is 1GB */
701 outbuf
[20] = (max_unmap_sectors
>> 24) & 0xff;
702 outbuf
[21] = (max_unmap_sectors
>> 16) & 0xff;
703 outbuf
[22] = (max_unmap_sectors
>> 8) & 0xff;
704 outbuf
[23] = max_unmap_sectors
& 0xff;
706 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header. */
712 /* optimal unmap granularity */
713 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
714 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
715 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
716 outbuf
[31] = unmap_sectors
& 0xff;
718 /* max write same size */
724 outbuf
[40] = (max_io_sectors
>> 24) & 0xff;
725 outbuf
[41] = (max_io_sectors
>> 16) & 0xff;
726 outbuf
[42] = (max_io_sectors
>> 8) & 0xff;
727 outbuf
[43] = max_io_sectors
& 0xff;
730 case 0xb2: /* thin provisioning */
734 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
735 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
743 assert(buflen
- start
<= 255);
744 outbuf
[start
- 1] = buflen
- start
;
748 /* Standard INQUIRY data */
749 if (req
->cmd
.buf
[2] != 0) {
754 buflen
= req
->cmd
.xfer
;
755 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
756 buflen
= SCSI_MAX_INQUIRY_LEN
;
759 outbuf
[0] = s
->qdev
.type
& 0x1f;
760 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
762 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
763 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
765 memset(&outbuf
[32], 0, 4);
766 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
768 * We claim conformance to SPC-3, which is required for guests
769 * to ask for modern features like READ CAPACITY(16) or the
770 * block characteristics VPD page by default. Not all of SPC-3
771 * is actually implemented, but we're good enough.
774 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
777 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
779 /* If the allocation length of CDB is too small,
780 the additional length is not adjusted */
784 /* Sync data transfer and TCQ. */
785 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
789 static inline bool media_is_dvd(SCSIDiskState
*s
)
792 if (s
->qdev
.type
!= TYPE_ROM
) {
795 if (!blk_is_inserted(s
->qdev
.conf
.blk
)) {
801 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
802 return nb_sectors
> CD_MAX_SECTORS
;
805 static inline bool media_is_cd(SCSIDiskState
*s
)
808 if (s
->qdev
.type
!= TYPE_ROM
) {
811 if (!blk_is_inserted(s
->qdev
.conf
.blk
)) {
817 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
818 return nb_sectors
<= CD_MAX_SECTORS
;
821 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
824 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
826 if (s
->qdev
.type
!= TYPE_ROM
) {
830 /* Types 1/2 are only defined for Blu-Ray. */
832 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
836 memset(outbuf
, 0, 34);
838 outbuf
[2] = 0xe; /* last session complete, disc finalized */
839 outbuf
[3] = 1; /* first track on disc */
840 outbuf
[4] = 1; /* # of sessions */
841 outbuf
[5] = 1; /* first track of last session */
842 outbuf
[6] = 1; /* last track of last session */
843 outbuf
[7] = 0x20; /* unrestricted use */
844 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
845 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
846 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
847 /* 24-31: disc bar code */
848 /* 32: disc application code */
849 /* 33: number of OPC tables */
854 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
857 static const int rds_caps_size
[5] = {
864 uint8_t media
= r
->req
.cmd
.buf
[1];
865 uint8_t layer
= r
->req
.cmd
.buf
[6];
866 uint8_t format
= r
->req
.cmd
.buf
[7];
869 if (s
->qdev
.type
!= TYPE_ROM
) {
873 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
877 if (format
!= 0xff) {
878 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
879 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
882 if (media_is_cd(s
)) {
883 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
886 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
889 size
= rds_caps_size
[format
];
890 memset(outbuf
, 0, size
);
895 /* Physical format information */
900 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
902 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
903 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
904 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
905 outbuf
[7] = 0; /* default densities */
907 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
908 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
912 case 0x01: /* DVD copyright information, all zeros */
915 case 0x03: /* BCA information - invalid field for no BCA info */
918 case 0x04: /* DVD disc manufacturing information, all zeros */
921 case 0xff: { /* List capabilities */
924 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
925 if (!rds_caps_size
[i
]) {
929 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
930 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
940 /* Size of buffer, not including 2 byte size field */
941 stw_be_p(outbuf
, size
- 2);
948 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
950 uint8_t event_code
, media_status
;
954 media_status
= MS_TRAY_OPEN
;
955 } else if (blk_is_inserted(s
->qdev
.conf
.blk
)) {
956 media_status
= MS_MEDIA_PRESENT
;
959 /* Event notification descriptor */
960 event_code
= MEC_NO_CHANGE
;
961 if (media_status
!= MS_TRAY_OPEN
) {
962 if (s
->media_event
) {
963 event_code
= MEC_NEW_MEDIA
;
964 s
->media_event
= false;
965 } else if (s
->eject_request
) {
966 event_code
= MEC_EJECT_REQUESTED
;
967 s
->eject_request
= false;
971 outbuf
[0] = event_code
;
972 outbuf
[1] = media_status
;
974 /* These fields are reserved, just clear them. */
980 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
984 uint8_t *buf
= r
->req
.cmd
.buf
;
985 uint8_t notification_class_request
= buf
[4];
986 if (s
->qdev
.type
!= TYPE_ROM
) {
989 if ((buf
[1] & 1) == 0) {
995 outbuf
[0] = outbuf
[1] = 0;
996 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
997 if (notification_class_request
& (1 << GESN_MEDIA
)) {
998 outbuf
[2] = GESN_MEDIA
;
999 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
1003 stw_be_p(outbuf
, size
- 4);
1007 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
1011 if (s
->qdev
.type
!= TYPE_ROM
) {
1015 if (media_is_dvd(s
)) {
1016 current
= MMC_PROFILE_DVD_ROM
;
1017 } else if (media_is_cd(s
)) {
1018 current
= MMC_PROFILE_CD_ROM
;
1020 current
= MMC_PROFILE_NONE
;
1023 memset(outbuf
, 0, 40);
1024 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
1025 stw_be_p(&outbuf
[6], current
);
1026 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
1027 outbuf
[10] = 0x03; /* persistent, current */
1028 outbuf
[11] = 8; /* two profiles */
1029 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
1030 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
1031 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
1032 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
1033 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
1034 stw_be_p(&outbuf
[20], 1);
1035 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
1037 stl_be_p(&outbuf
[24], 1); /* SCSI */
1038 outbuf
[28] = 1; /* DBE = 1, mandatory */
1039 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
1040 stw_be_p(&outbuf
[32], 3);
1041 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
1043 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1044 /* TODO: Random readable, CD read, DVD read, drive serial number,
1049 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
1051 if (s
->qdev
.type
!= TYPE_ROM
) {
1054 memset(outbuf
, 0, 8);
1055 outbuf
[5] = 1; /* CD-ROM */
1059 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
1062 static const int mode_sense_valid
[0x3f] = {
1063 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
1064 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
1065 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1066 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1067 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
1068 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
1071 uint8_t *p
= *p_outbuf
+ 2;
1074 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
1079 * If Changeable Values are requested, a mask denoting those mode parameters
1080 * that are changeable shall be returned. As we currently don't support
1081 * parameter changes via MODE_SELECT all bits are returned set to zero.
1082 * The buffer was already menset to zero by the caller of this function.
1084 * The offsets here are off by two compared to the descriptions in the
1085 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1086 * but it is done so that offsets are consistent within our implementation
1087 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1088 * 2-byte and 4-byte headers.
1091 case MODE_PAGE_HD_GEOMETRY
:
1093 if (page_control
== 1) { /* Changeable Values */
1096 /* if a geometry hint is available, use it */
1097 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1098 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1099 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
1100 p
[3] = s
->qdev
.conf
.heads
& 0xff;
1101 /* Write precomp start cylinder, disabled */
1102 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1103 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1104 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
1105 /* Reduced current start cylinder, disabled */
1106 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1107 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1108 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1109 /* Device step rate [ns], 200ns */
1112 /* Landing zone cylinder */
1116 /* Medium rotation rate [rpm], 5400 rpm */
1117 p
[18] = (5400 >> 8) & 0xff;
1118 p
[19] = 5400 & 0xff;
1121 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1123 if (page_control
== 1) { /* Changeable Values */
1126 /* Transfer rate [kbit/s], 5Mbit/s */
1129 /* if a geometry hint is available, use it */
1130 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1131 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1132 p
[4] = s
->qdev
.blocksize
>> 8;
1133 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1134 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1135 /* Write precomp start cylinder, disabled */
1136 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1137 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1138 /* Reduced current start cylinder, disabled */
1139 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1140 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1141 /* Device step rate [100us], 100us */
1144 /* Device step pulse width [us], 1us */
1146 /* Device head settle delay [100us], 100us */
1149 /* Motor on delay [0.1s], 0.1s */
1151 /* Motor off delay [0.1s], 0.1s */
1153 /* Medium rotation rate [rpm], 5400 rpm */
1154 p
[26] = (5400 >> 8) & 0xff;
1155 p
[27] = 5400 & 0xff;
1158 case MODE_PAGE_CACHING
:
1160 if (page_control
== 1 || /* Changeable Values */
1161 blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1166 case MODE_PAGE_R_W_ERROR
:
1168 if (page_control
== 1) { /* Changeable Values */
1171 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1172 if (s
->qdev
.type
== TYPE_ROM
) {
1173 p
[1] = 0x20; /* Read Retry Count */
1177 case MODE_PAGE_AUDIO_CTL
:
1181 case MODE_PAGE_CAPABILITIES
:
1183 if (page_control
== 1) { /* Changeable Values */
1187 p
[0] = 0x3b; /* CD-R & CD-RW read */
1188 p
[1] = 0; /* Writing not supported */
1189 p
[2] = 0x7f; /* Audio, composite, digital out,
1190 mode 2 form 1&2, multi session */
1191 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1192 RW corrected, C2 errors, ISRC,
1194 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1195 /* Locking supported, jumper present, eject, tray */
1196 p
[5] = 0; /* no volume & mute control, no
1198 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1199 p
[7] = (50 * 176) & 0xff;
1200 p
[8] = 2 >> 8; /* Two volume levels */
1202 p
[10] = 2048 >> 8; /* 2M buffer */
1203 p
[11] = 2048 & 0xff;
1204 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1205 p
[13] = (16 * 176) & 0xff;
1206 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1207 p
[17] = (16 * 176) & 0xff;
1208 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1209 p
[19] = (16 * 176) & 0xff;
1216 assert(length
< 256);
1217 (*p_outbuf
)[0] = page
;
1218 (*p_outbuf
)[1] = length
;
1219 *p_outbuf
+= length
+ 2;
1223 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1225 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1226 uint64_t nb_sectors
;
1228 int page
, buflen
, ret
, page_control
;
1230 uint8_t dev_specific_param
;
1232 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1233 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1234 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1235 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1236 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
1237 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1240 if (s
->qdev
.type
== TYPE_DISK
) {
1241 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1242 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1243 dev_specific_param
|= 0x80; /* Readonly. */
1246 /* MMC prescribes that CD/DVD drives have no block descriptors,
1247 * and defines no device-specific parameter. */
1248 dev_specific_param
= 0x00;
1252 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1253 p
[1] = 0; /* Default media type. */
1254 p
[2] = dev_specific_param
;
1255 p
[3] = 0; /* Block descriptor length. */
1257 } else { /* MODE_SENSE_10 */
1258 p
[2] = 0; /* Default media type. */
1259 p
[3] = dev_specific_param
;
1260 p
[6] = p
[7] = 0; /* Block descriptor length. */
1264 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1265 if (!dbd
&& nb_sectors
) {
1266 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1267 outbuf
[3] = 8; /* Block descriptor length */
1268 } else { /* MODE_SENSE_10 */
1269 outbuf
[7] = 8; /* Block descriptor length */
1271 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1272 if (nb_sectors
> 0xffffff) {
1275 p
[0] = 0; /* media density code */
1276 p
[1] = (nb_sectors
>> 16) & 0xff;
1277 p
[2] = (nb_sectors
>> 8) & 0xff;
1278 p
[3] = nb_sectors
& 0xff;
1279 p
[4] = 0; /* reserved */
1280 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1281 p
[6] = s
->qdev
.blocksize
>> 8;
1286 if (page_control
== 3) {
1288 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1293 for (page
= 0; page
<= 0x3e; page
++) {
1294 mode_sense_page(s
, page
, &p
, page_control
);
1297 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1303 buflen
= p
- outbuf
;
1305 * The mode data length field specifies the length in bytes of the
1306 * following data that is available to be transferred. The mode data
1307 * length does not include itself.
1309 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1310 outbuf
[0] = buflen
- 1;
1311 } else { /* MODE_SENSE_10 */
1312 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1313 outbuf
[1] = (buflen
- 2) & 0xff;
1318 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1320 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1321 int start_track
, format
, msf
, toclen
;
1322 uint64_t nb_sectors
;
1324 msf
= req
->cmd
.buf
[1] & 2;
1325 format
= req
->cmd
.buf
[2] & 0xf;
1326 start_track
= req
->cmd
.buf
[6];
1327 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1328 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1329 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1332 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1335 /* multi session : only a single session defined */
1337 memset(outbuf
, 0, 12);
1343 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1351 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1353 SCSIRequest
*req
= &r
->req
;
1354 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1355 bool start
= req
->cmd
.buf
[4] & 1;
1356 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1357 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1360 /* eject/load only happens for power condition == 0 */
1364 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1365 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1366 scsi_check_condition(r
,
1367 blk_is_inserted(s
->qdev
.conf
.blk
)
1368 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1369 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1373 if (s
->tray_open
!= !start
) {
1374 blk_eject(s
->qdev
.conf
.blk
, !start
);
1375 s
->tray_open
= !start
;
1381 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1383 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1384 int buflen
= r
->iov
.iov_len
;
1387 DPRINTF("Read buf_len=%d\n", buflen
);
1390 scsi_req_data(&r
->req
, buflen
);
1394 /* This also clears the sense buffer for REQUEST SENSE. */
1395 scsi_req_complete(&r
->req
, GOOD
);
1398 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1399 uint8_t *inbuf
, int inlen
)
1401 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1402 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1404 int len
, expected_len
, changeable_len
, i
;
1406 /* The input buffer does not include the page header, so it is
1409 expected_len
= inlen
+ 2;
1410 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1415 memset(mode_current
, 0, inlen
+ 2);
1416 len
= mode_sense_page(s
, page
, &p
, 0);
1417 if (len
< 0 || len
!= expected_len
) {
1421 p
= mode_changeable
;
1422 memset(mode_changeable
, 0, inlen
+ 2);
1423 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1424 assert(changeable_len
== len
);
1426 /* Check that unchangeable bits are the same as what MODE SENSE
1429 for (i
= 2; i
< len
; i
++) {
1430 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1437 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1440 case MODE_PAGE_CACHING
:
1441 blk_set_enable_write_cache(s
->qdev
.conf
.blk
, (p
[0] & 4) != 0);
1449 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1451 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1454 int page
, subpage
, page_len
;
1456 /* Parse both possible formats for the mode page headers. */
1460 goto invalid_param_len
;
1463 page_len
= lduw_be_p(&p
[2]);
1468 goto invalid_param_len
;
1479 if (page_len
> len
) {
1480 goto invalid_param_len
;
1484 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1488 scsi_disk_apply_mode_select(s
, page
, p
);
1497 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1501 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1505 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1507 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1509 int cmd
= r
->req
.cmd
.buf
[0];
1510 int len
= r
->req
.cmd
.xfer
;
1511 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1515 /* We only support PF=1, SP=0. */
1516 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1520 if (len
< hdr_len
) {
1521 goto invalid_param_len
;
1524 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1528 goto invalid_param_len
;
1530 if (bd_len
!= 0 && bd_len
!= 8) {
1537 /* Ensure no change is made if there is an error! */
1538 for (pass
= 0; pass
< 2; pass
++) {
1539 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1544 if (!blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1545 /* The request is used as the AIO opaque value, so add a ref. */
1546 scsi_req_ref(&r
->req
);
1547 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
1549 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
1553 scsi_req_complete(&r
->req
, GOOD
);
1557 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1561 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1565 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1568 static inline bool check_lba_range(SCSIDiskState
*s
,
1569 uint64_t sector_num
, uint32_t nb_sectors
)
1572 * The first line tests that no overflow happens when computing the last
1573 * sector. The second line tests that the last accessed sector is in
1576 * Careful, the computations should not underflow for nb_sectors == 0,
1577 * and a 0-block read to the first LBA beyond the end of device is
1580 return (sector_num
<= sector_num
+ nb_sectors
&&
1581 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1584 typedef struct UnmapCBData
{
1590 static void scsi_unmap_complete(void *opaque
, int ret
);
1592 static void scsi_unmap_complete_noio(UnmapCBData
*data
, int ret
)
1594 SCSIDiskReq
*r
= data
->r
;
1595 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1596 uint64_t sector_num
;
1597 uint32_t nb_sectors
;
1599 assert(r
->req
.aiocb
== NULL
);
1600 if (scsi_disk_req_check_error(r
, ret
, false)) {
1604 if (data
->count
> 0) {
1605 sector_num
= ldq_be_p(&data
->inbuf
[0]);
1606 nb_sectors
= ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
;
1607 if (!check_lba_range(s
, sector_num
, nb_sectors
)) {
1608 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1612 r
->req
.aiocb
= blk_aio_discard(s
->qdev
.conf
.blk
,
1613 sector_num
* (s
->qdev
.blocksize
/ 512),
1614 nb_sectors
* (s
->qdev
.blocksize
/ 512),
1615 scsi_unmap_complete
, data
);
1621 scsi_req_complete(&r
->req
, GOOD
);
1624 scsi_req_unref(&r
->req
);
1628 static void scsi_unmap_complete(void *opaque
, int ret
)
1630 UnmapCBData
*data
= opaque
;
1631 SCSIDiskReq
*r
= data
->r
;
1633 assert(r
->req
.aiocb
!= NULL
);
1634 r
->req
.aiocb
= NULL
;
1636 scsi_unmap_complete_noio(data
, ret
);
1639 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1641 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1643 int len
= r
->req
.cmd
.xfer
;
1646 /* Reject ANCHOR=1. */
1647 if (r
->req
.cmd
.buf
[1] & 0x1) {
1652 goto invalid_param_len
;
1654 if (len
< lduw_be_p(&p
[0]) + 2) {
1655 goto invalid_param_len
;
1657 if (len
< lduw_be_p(&p
[2]) + 8) {
1658 goto invalid_param_len
;
1660 if (lduw_be_p(&p
[2]) & 15) {
1661 goto invalid_param_len
;
1664 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1665 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1669 data
= g_new0(UnmapCBData
, 1);
1671 data
->inbuf
= &p
[8];
1672 data
->count
= lduw_be_p(&p
[2]) >> 4;
1674 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1675 scsi_req_ref(&r
->req
);
1676 scsi_unmap_complete_noio(data
, 0);
1680 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1684 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1687 typedef struct WriteSameCBData
{
1695 static void scsi_write_same_complete(void *opaque
, int ret
)
1697 WriteSameCBData
*data
= opaque
;
1698 SCSIDiskReq
*r
= data
->r
;
1699 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1701 assert(r
->req
.aiocb
!= NULL
);
1702 r
->req
.aiocb
= NULL
;
1703 if (scsi_disk_req_check_error(r
, ret
, true)) {
1707 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
1709 data
->nb_sectors
-= data
->iov
.iov_len
/ 512;
1710 data
->sector
+= data
->iov
.iov_len
/ 512;
1711 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, data
->iov
.iov_len
);
1712 if (data
->iov
.iov_len
) {
1713 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1714 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1715 /* Reinitialize qiov, to handle unaligned WRITE SAME request
1716 * where final qiov may need smaller size */
1717 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1718 r
->req
.aiocb
= blk_aio_pwritev(s
->qdev
.conf
.blk
,
1719 data
->sector
<< BDRV_SECTOR_BITS
,
1721 scsi_write_same_complete
, data
);
1725 scsi_req_complete(&r
->req
, GOOD
);
1728 scsi_req_unref(&r
->req
);
1729 qemu_vfree(data
->iov
.iov_base
);
1733 static void scsi_disk_emulate_write_same(SCSIDiskReq
*r
, uint8_t *inbuf
)
1735 SCSIRequest
*req
= &r
->req
;
1736 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1737 uint32_t nb_sectors
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
1738 WriteSameCBData
*data
;
1742 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1743 if (nb_sectors
== 0 || (req
->cmd
.buf
[1] & 0x16)) {
1744 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1748 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1749 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1752 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1753 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1757 if (buffer_is_zero(inbuf
, s
->qdev
.blocksize
)) {
1758 int flags
= (req
->cmd
.buf
[1] & 0x8) ? BDRV_REQ_MAY_UNMAP
: 0;
1760 /* The request is used as the AIO opaque value, so add a ref. */
1761 scsi_req_ref(&r
->req
);
1762 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1763 nb_sectors
* s
->qdev
.blocksize
,
1765 r
->req
.aiocb
= blk_aio_pwrite_zeroes(s
->qdev
.conf
.blk
,
1766 r
->req
.cmd
.lba
* s
->qdev
.blocksize
,
1767 nb_sectors
* s
->qdev
.blocksize
,
1768 flags
, scsi_aio_complete
, r
);
1772 data
= g_new0(WriteSameCBData
, 1);
1774 data
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1775 data
->nb_sectors
= nb_sectors
* (s
->qdev
.blocksize
/ 512);
1776 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, SCSI_WRITE_SAME_MAX
);
1777 data
->iov
.iov_base
= buf
= blk_blockalign(s
->qdev
.conf
.blk
,
1779 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1781 for (i
= 0; i
< data
->iov
.iov_len
; i
+= s
->qdev
.blocksize
) {
1782 memcpy(&buf
[i
], inbuf
, s
->qdev
.blocksize
);
1785 scsi_req_ref(&r
->req
);
1786 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1787 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1788 r
->req
.aiocb
= blk_aio_pwritev(s
->qdev
.conf
.blk
,
1789 data
->sector
<< BDRV_SECTOR_BITS
,
1791 scsi_write_same_complete
, data
);
1794 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1796 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1798 if (r
->iov
.iov_len
) {
1799 int buflen
= r
->iov
.iov_len
;
1800 DPRINTF("Write buf_len=%d\n", buflen
);
1802 scsi_req_data(&r
->req
, buflen
);
1806 switch (req
->cmd
.buf
[0]) {
1808 case MODE_SELECT_10
:
1809 /* This also clears the sense buffer for REQUEST SENSE. */
1810 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1814 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1820 if (r
->req
.status
== -1) {
1821 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1827 scsi_disk_emulate_write_same(r
, r
->iov
.iov_base
);
1835 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1837 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1838 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1839 uint64_t nb_sectors
;
1843 switch (req
->cmd
.buf
[0]) {
1852 case ALLOW_MEDIUM_REMOVAL
:
1853 case GET_CONFIGURATION
:
1854 case GET_EVENT_STATUS_NOTIFICATION
:
1855 case MECHANISM_STATUS
:
1860 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
1861 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1868 * FIXME: we shouldn't return anything bigger than 4k, but the code
1869 * requires the buffer to be as big as req->cmd.xfer in several
1870 * places. So, do not allow CDBs with a very large ALLOCATION
1871 * LENGTH. The real fix would be to modify scsi_read_data and
1872 * dma_buf_read, so that they return data beyond the buflen
1875 if (req
->cmd
.xfer
> 65536) {
1876 goto illegal_request
;
1878 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1880 if (!r
->iov
.iov_base
) {
1881 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
1884 buflen
= req
->cmd
.xfer
;
1885 outbuf
= r
->iov
.iov_base
;
1886 memset(outbuf
, 0, r
->buflen
);
1887 switch (req
->cmd
.buf
[0]) {
1888 case TEST_UNIT_READY
:
1889 assert(!s
->tray_open
&& blk_is_inserted(s
->qdev
.conf
.blk
));
1892 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1894 goto illegal_request
;
1899 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1901 goto illegal_request
;
1905 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1907 goto illegal_request
;
1911 if (req
->cmd
.buf
[1] & 1) {
1912 goto illegal_request
;
1916 if (req
->cmd
.buf
[1] & 3) {
1917 goto illegal_request
;
1921 if (req
->cmd
.buf
[1] & 1) {
1922 goto illegal_request
;
1926 if (req
->cmd
.buf
[1] & 3) {
1927 goto illegal_request
;
1931 if (scsi_disk_emulate_start_stop(r
) < 0) {
1935 case ALLOW_MEDIUM_REMOVAL
:
1936 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1937 blk_lock_medium(s
->qdev
.conf
.blk
, req
->cmd
.buf
[4] & 1);
1939 case READ_CAPACITY_10
:
1940 /* The normal LEN field for this command is zero. */
1941 memset(outbuf
, 0, 8);
1942 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1944 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1947 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1948 goto illegal_request
;
1950 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1951 /* Returned value is the address of the last sector. */
1953 /* Remember the new size for read/write sanity checking. */
1954 s
->qdev
.max_lba
= nb_sectors
;
1955 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1956 if (nb_sectors
> UINT32_MAX
) {
1957 nb_sectors
= UINT32_MAX
;
1959 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1960 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1961 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1962 outbuf
[3] = nb_sectors
& 0xff;
1965 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1969 /* Just return "NO SENSE". */
1970 buflen
= scsi_build_sense(NULL
, 0, outbuf
, r
->buflen
,
1971 (req
->cmd
.buf
[1] & 1) == 0);
1973 goto illegal_request
;
1976 case MECHANISM_STATUS
:
1977 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1979 goto illegal_request
;
1982 case GET_CONFIGURATION
:
1983 buflen
= scsi_get_configuration(s
, outbuf
);
1985 goto illegal_request
;
1988 case GET_EVENT_STATUS_NOTIFICATION
:
1989 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1991 goto illegal_request
;
1994 case READ_DISC_INFORMATION
:
1995 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
1997 goto illegal_request
;
2000 case READ_DVD_STRUCTURE
:
2001 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
2003 goto illegal_request
;
2006 case SERVICE_ACTION_IN_16
:
2007 /* Service Action In subcommands. */
2008 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
2009 DPRINTF("SAI READ CAPACITY(16)\n");
2010 memset(outbuf
, 0, req
->cmd
.xfer
);
2011 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2013 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
2016 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
2017 goto illegal_request
;
2019 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2020 /* Returned value is the address of the last sector. */
2022 /* Remember the new size for read/write sanity checking. */
2023 s
->qdev
.max_lba
= nb_sectors
;
2024 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
2025 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
2026 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
2027 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
2028 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
2029 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
2030 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
2031 outbuf
[7] = nb_sectors
& 0xff;
2034 outbuf
[10] = s
->qdev
.blocksize
>> 8;
2037 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
2039 /* set TPE bit if the format supports discard */
2040 if (s
->qdev
.conf
.discard_granularity
) {
2044 /* Protection, exponent and lowest lba field left blank. */
2047 DPRINTF("Unsupported Service Action In\n");
2048 goto illegal_request
;
2049 case SYNCHRONIZE_CACHE
:
2050 /* The request is used as the AIO opaque value, so add a ref. */
2051 scsi_req_ref(&r
->req
);
2052 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
2054 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
2057 DPRINTF("Seek(10) (sector %" PRId64
")\n", r
->req
.cmd
.lba
);
2058 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
2063 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2065 case MODE_SELECT_10
:
2066 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2069 DPRINTF("Unmap (len %lu)\n", (long)r
->req
.cmd
.xfer
);
2074 DPRINTF("Verify (bytchk %d)\n", (req
->cmd
.buf
[1] >> 1) & 3);
2075 if (req
->cmd
.buf
[1] & 6) {
2076 goto illegal_request
;
2081 DPRINTF("WRITE SAME %d (len %lu)\n",
2082 req
->cmd
.buf
[0] == WRITE_SAME_10
? 10 : 16,
2083 (long)r
->req
.cmd
.xfer
);
2086 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf
[0],
2087 scsi_command_name(buf
[0]));
2088 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
2091 assert(!r
->req
.aiocb
);
2092 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
2093 if (r
->iov
.iov_len
== 0) {
2094 scsi_req_complete(&r
->req
, GOOD
);
2096 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2097 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
2098 return -r
->iov
.iov_len
;
2100 return r
->iov
.iov_len
;
2104 if (r
->req
.status
== -1) {
2105 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2110 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2114 /* Execute a scsi command. Returns the length of the data expected by the
2115 command. This will be Positive for data transfers from the device
2116 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2117 and zero if the command does not transfer any data. */
2119 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2121 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
2122 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
2123 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
2129 if (s
->tray_open
|| !blk_is_inserted(s
->qdev
.conf
.blk
)) {
2130 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
2134 len
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
2140 DPRINTF("Read (sector %" PRId64
", count %u)\n", r
->req
.cmd
.lba
, len
);
2141 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2142 goto illegal_request
;
2144 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2147 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2148 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2154 case WRITE_VERIFY_10
:
2155 case WRITE_VERIFY_12
:
2156 case WRITE_VERIFY_16
:
2157 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
2158 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
2161 DPRINTF("Write %s(sector %" PRId64
", count %u)\n",
2162 (command
& 0xe) == 0xe ? "And Verify " : "",
2163 r
->req
.cmd
.lba
, len
);
2164 if (r
->req
.cmd
.buf
[1] & 0xe0) {
2165 goto illegal_request
;
2167 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2170 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2171 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2176 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2179 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2182 r
->need_fua_emulation
= sdc
->need_fua_emulation(&r
->req
.cmd
);
2183 if (r
->sector_count
== 0) {
2184 scsi_req_complete(&r
->req
, GOOD
);
2186 assert(r
->iov
.iov_len
== 0);
2187 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2188 return -r
->sector_count
* 512;
2190 return r
->sector_count
* 512;
2194 static void scsi_disk_reset(DeviceState
*dev
)
2196 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
2197 uint64_t nb_sectors
;
2199 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
2201 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2202 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2206 s
->qdev
.max_lba
= nb_sectors
;
2207 /* reset tray statuses */
2212 static void scsi_disk_resize_cb(void *opaque
)
2214 SCSIDiskState
*s
= opaque
;
2216 /* SPC lists this sense code as available only for
2217 * direct-access devices.
2219 if (s
->qdev
.type
== TYPE_DISK
) {
2220 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
2224 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
2226 SCSIDiskState
*s
= opaque
;
2229 * When a CD gets changed, we have to report an ejected state and
2230 * then a loaded state to guests so that they detect tray
2231 * open/close and media change events. Guests that do not use
2232 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2233 * states rely on this behavior.
2235 * media_changed governs the state machine used for unit attention
2236 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2238 s
->media_changed
= load
;
2239 s
->tray_open
= !load
;
2240 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
2241 s
->media_event
= true;
2242 s
->eject_request
= false;
2245 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2247 SCSIDiskState
*s
= opaque
;
2249 s
->eject_request
= true;
2251 s
->tray_locked
= false;
2255 static bool scsi_cd_is_tray_open(void *opaque
)
2257 return ((SCSIDiskState
*)opaque
)->tray_open
;
2260 static bool scsi_cd_is_medium_locked(void *opaque
)
2262 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2265 static const BlockDevOps scsi_disk_removable_block_ops
= {
2266 .change_media_cb
= scsi_cd_change_media_cb
,
2267 .eject_request_cb
= scsi_cd_eject_request_cb
,
2268 .is_tray_open
= scsi_cd_is_tray_open
,
2269 .is_medium_locked
= scsi_cd_is_medium_locked
,
2271 .resize_cb
= scsi_disk_resize_cb
,
2274 static const BlockDevOps scsi_disk_block_ops
= {
2275 .resize_cb
= scsi_disk_resize_cb
,
2278 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2280 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2281 if (s
->media_changed
) {
2282 s
->media_changed
= false;
2283 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2287 static void scsi_realize(SCSIDevice
*dev
, Error
**errp
)
2289 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2292 if (!s
->qdev
.conf
.blk
) {
2293 error_setg(errp
, "drive property not set");
2297 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2298 !blk_is_inserted(s
->qdev
.conf
.blk
)) {
2299 error_setg(errp
, "Device needs media, but drive is empty");
2303 blkconf_serial(&s
->qdev
.conf
, &s
->serial
);
2304 blkconf_blocksizes(&s
->qdev
.conf
);
2305 if (dev
->type
== TYPE_DISK
) {
2306 blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255, &err
);
2308 error_propagate(errp
, err
);
2313 if (s
->qdev
.conf
.discard_granularity
== -1) {
2314 s
->qdev
.conf
.discard_granularity
=
2315 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2319 s
->version
= g_strdup(qemu_hw_version());
2322 s
->vendor
= g_strdup("QEMU");
2325 if (blk_is_sg(s
->qdev
.conf
.blk
)) {
2326 error_setg(errp
, "unwanted /dev/sg*");
2330 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2331 !(s
->features
& (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
))) {
2332 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_removable_block_ops
, s
);
2334 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_block_ops
, s
);
2336 blk_set_guest_block_size(s
->qdev
.conf
.blk
, s
->qdev
.blocksize
);
2338 blk_iostatus_enable(s
->qdev
.conf
.blk
);
2341 static void scsi_hd_realize(SCSIDevice
*dev
, Error
**errp
)
2343 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2344 /* can happen for devices without drive. The error message for missing
2345 * backend will be issued in scsi_realize
2347 if (s
->qdev
.conf
.blk
) {
2348 blkconf_blocksizes(&s
->qdev
.conf
);
2350 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2351 s
->qdev
.type
= TYPE_DISK
;
2353 s
->product
= g_strdup("QEMU HARDDISK");
2355 scsi_realize(&s
->qdev
, errp
);
2358 static void scsi_cd_realize(SCSIDevice
*dev
, Error
**errp
)
2360 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2361 s
->qdev
.blocksize
= 2048;
2362 s
->qdev
.type
= TYPE_ROM
;
2363 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2365 s
->product
= g_strdup("QEMU CD-ROM");
2367 scsi_realize(&s
->qdev
, errp
);
2370 static void scsi_disk_realize(SCSIDevice
*dev
, Error
**errp
)
2373 Error
*local_err
= NULL
;
2375 if (!dev
->conf
.blk
) {
2376 scsi_realize(dev
, &local_err
);
2378 error_propagate(errp
, local_err
);
2382 dinfo
= blk_legacy_dinfo(dev
->conf
.blk
);
2383 if (dinfo
&& dinfo
->media_cd
) {
2384 scsi_cd_realize(dev
, errp
);
2386 scsi_hd_realize(dev
, errp
);
2390 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2391 .size
= sizeof(SCSIDiskReq
),
2392 .free_req
= scsi_free_request
,
2393 .send_command
= scsi_disk_emulate_command
,
2394 .read_data
= scsi_disk_emulate_read_data
,
2395 .write_data
= scsi_disk_emulate_write_data
,
2396 .get_buf
= scsi_get_buf
,
2399 static const SCSIReqOps scsi_disk_dma_reqops
= {
2400 .size
= sizeof(SCSIDiskReq
),
2401 .free_req
= scsi_free_request
,
2402 .send_command
= scsi_disk_dma_command
,
2403 .read_data
= scsi_read_data
,
2404 .write_data
= scsi_write_data
,
2405 .get_buf
= scsi_get_buf
,
2406 .load_request
= scsi_disk_load_request
,
2407 .save_request
= scsi_disk_save_request
,
2410 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2411 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2412 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2413 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2414 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2415 [START_STOP
] = &scsi_disk_emulate_reqops
,
2416 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2417 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2418 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2419 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2420 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2421 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2422 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2423 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2424 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2425 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2426 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2427 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2428 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2429 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2430 [UNMAP
] = &scsi_disk_emulate_reqops
,
2431 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2432 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2433 [VERIFY_10
] = &scsi_disk_emulate_reqops
,
2434 [VERIFY_12
] = &scsi_disk_emulate_reqops
,
2435 [VERIFY_16
] = &scsi_disk_emulate_reqops
,
2437 [READ_6
] = &scsi_disk_dma_reqops
,
2438 [READ_10
] = &scsi_disk_dma_reqops
,
2439 [READ_12
] = &scsi_disk_dma_reqops
,
2440 [READ_16
] = &scsi_disk_dma_reqops
,
2441 [WRITE_6
] = &scsi_disk_dma_reqops
,
2442 [WRITE_10
] = &scsi_disk_dma_reqops
,
2443 [WRITE_12
] = &scsi_disk_dma_reqops
,
2444 [WRITE_16
] = &scsi_disk_dma_reqops
,
2445 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2446 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2447 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2450 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2451 uint8_t *buf
, void *hba_private
)
2453 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2455 const SCSIReqOps
*ops
;
2459 ops
= scsi_disk_reqops_dispatch
[command
];
2461 ops
= &scsi_disk_emulate_reqops
;
2463 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2466 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
2469 for (i
= 1; i
< scsi_cdb_length(buf
); i
++) {
2470 printf(" 0x%02x", buf
[i
]);
2480 static int get_device_type(SCSIDiskState
*s
)
2484 uint8_t sensebuf
[8];
2485 sg_io_hdr_t io_header
;
2488 memset(cmd
, 0, sizeof(cmd
));
2489 memset(buf
, 0, sizeof(buf
));
2491 cmd
[4] = sizeof(buf
);
2493 memset(&io_header
, 0, sizeof(io_header
));
2494 io_header
.interface_id
= 'S';
2495 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2496 io_header
.dxfer_len
= sizeof(buf
);
2497 io_header
.dxferp
= buf
;
2498 io_header
.cmdp
= cmd
;
2499 io_header
.cmd_len
= sizeof(cmd
);
2500 io_header
.mx_sb_len
= sizeof(sensebuf
);
2501 io_header
.sbp
= sensebuf
;
2502 io_header
.timeout
= 6000; /* XXX */
2504 ret
= blk_ioctl(s
->qdev
.conf
.blk
, SG_IO
, &io_header
);
2505 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
2508 s
->qdev
.type
= buf
[0];
2509 if (buf
[1] & 0x80) {
2510 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2515 static void scsi_block_realize(SCSIDevice
*dev
, Error
**errp
)
2517 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2521 if (!s
->qdev
.conf
.blk
) {
2522 error_setg(errp
, "drive property not set");
2526 /* check we are using a driver managing SG_IO (version 3 and after) */
2527 rc
= blk_ioctl(s
->qdev
.conf
.blk
, SG_GET_VERSION_NUM
, &sg_version
);
2529 error_setg(errp
, "cannot get SG_IO version number: %s. "
2530 "Is this a SCSI device?",
2534 if (sg_version
< 30000) {
2535 error_setg(errp
, "scsi generic interface too old");
2539 /* get device type from INQUIRY data */
2540 rc
= get_device_type(s
);
2542 error_setg(errp
, "INQUIRY failed");
2546 /* Make a guess for the block size, we'll fix it when the guest sends.
2547 * READ CAPACITY. If they don't, they likely would assume these sizes
2548 * anyway. (TODO: check in /sys).
2550 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2551 s
->qdev
.blocksize
= 2048;
2553 s
->qdev
.blocksize
= 512;
2556 /* Makes the scsi-block device not removable by using HMP and QMP eject
2559 s
->features
|= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
);
2561 scsi_realize(&s
->qdev
, errp
);
2562 scsi_generic_read_device_identification(&s
->qdev
);
2565 typedef struct SCSIBlockReq
{
2567 sg_io_hdr_t io_header
;
2569 /* Selected bytes of the original CDB, copied into our own CDB. */
2570 uint8_t cmd
, cdb1
, group_number
;
2572 /* CDB passed to SG_IO. */
2576 static BlockAIOCB
*scsi_block_do_sgio(SCSIBlockReq
*req
,
2577 int64_t offset
, QEMUIOVector
*iov
,
2579 BlockCompletionFunc
*cb
, void *opaque
)
2581 sg_io_hdr_t
*io_header
= &req
->io_header
;
2582 SCSIDiskReq
*r
= &req
->req
;
2583 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2584 int nb_logical_blocks
;
2588 /* This is not supported yet. It can only happen if the guest does
2589 * reads and writes that are not aligned to one logical sectors
2590 * _and_ cover multiple MemoryRegions.
2592 assert(offset
% s
->qdev
.blocksize
== 0);
2593 assert(iov
->size
% s
->qdev
.blocksize
== 0);
2595 io_header
->interface_id
= 'S';
2597 /* The data transfer comes from the QEMUIOVector. */
2598 io_header
->dxfer_direction
= direction
;
2599 io_header
->dxfer_len
= iov
->size
;
2600 io_header
->dxferp
= (void *)iov
->iov
;
2601 io_header
->iovec_count
= iov
->niov
;
2602 assert(io_header
->iovec_count
== iov
->niov
); /* no overflow! */
2604 /* Build a new CDB with the LBA and length patched in, in case
2605 * DMA helpers split the transfer in multiple segments. Do not
2606 * build a CDB smaller than what the guest wanted, and only build
2607 * a larger one if strictly necessary.
2609 io_header
->cmdp
= req
->cdb
;
2610 lba
= offset
/ s
->qdev
.blocksize
;
2611 nb_logical_blocks
= io_header
->dxfer_len
/ s
->qdev
.blocksize
;
2613 if ((req
->cmd
>> 5) == 0 && lba
<= 0x1ffff) {
2615 stl_be_p(&req
->cdb
[0], lba
| (req
->cmd
<< 24));
2616 req
->cdb
[4] = nb_logical_blocks
;
2618 io_header
->cmd_len
= 6;
2619 } else if ((req
->cmd
>> 5) <= 1 && lba
<= 0xffffffffULL
) {
2621 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0x20;
2622 req
->cdb
[1] = req
->cdb1
;
2623 stl_be_p(&req
->cdb
[2], lba
);
2624 req
->cdb
[6] = req
->group_number
;
2625 stw_be_p(&req
->cdb
[7], nb_logical_blocks
);
2627 io_header
->cmd_len
= 10;
2628 } else if ((req
->cmd
>> 5) != 4 && lba
<= 0xffffffffULL
) {
2630 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0xA0;
2631 req
->cdb
[1] = req
->cdb1
;
2632 stl_be_p(&req
->cdb
[2], lba
);
2633 stl_be_p(&req
->cdb
[6], nb_logical_blocks
);
2634 req
->cdb
[10] = req
->group_number
;
2636 io_header
->cmd_len
= 12;
2639 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0x80;
2640 req
->cdb
[1] = req
->cdb1
;
2641 stq_be_p(&req
->cdb
[2], lba
);
2642 stl_be_p(&req
->cdb
[10], nb_logical_blocks
);
2643 req
->cdb
[14] = req
->group_number
;
2645 io_header
->cmd_len
= 16;
2648 /* The rest is as in scsi-generic.c. */
2649 io_header
->mx_sb_len
= sizeof(r
->req
.sense
);
2650 io_header
->sbp
= r
->req
.sense
;
2651 io_header
->timeout
= UINT_MAX
;
2652 io_header
->usr_ptr
= r
;
2653 io_header
->flags
|= SG_FLAG_DIRECT_IO
;
2655 aiocb
= blk_aio_ioctl(s
->qdev
.conf
.blk
, SG_IO
, io_header
, cb
, opaque
);
2656 assert(aiocb
!= NULL
);
2660 static bool scsi_block_no_fua(SCSICommand
*cmd
)
2665 static BlockAIOCB
*scsi_block_dma_readv(int64_t offset
,
2667 BlockCompletionFunc
*cb
, void *cb_opaque
,
2670 SCSIBlockReq
*r
= opaque
;
2671 return scsi_block_do_sgio(r
, offset
, iov
,
2672 SG_DXFER_FROM_DEV
, cb
, cb_opaque
);
2675 static BlockAIOCB
*scsi_block_dma_writev(int64_t offset
,
2677 BlockCompletionFunc
*cb
, void *cb_opaque
,
2680 SCSIBlockReq
*r
= opaque
;
2681 return scsi_block_do_sgio(r
, offset
, iov
,
2682 SG_DXFER_TO_DEV
, cb
, cb_opaque
);
2685 static bool scsi_block_is_passthrough(SCSIDiskState
*s
, uint8_t *buf
)
2691 /* Check if BYTCHK == 0x01 (data-out buffer contains data
2692 * for the number of logical blocks specified in the length
2693 * field). For other modes, do not use scatter/gather operation.
2695 if ((buf
[1] & 6) != 2) {
2708 case WRITE_VERIFY_10
:
2709 case WRITE_VERIFY_12
:
2710 case WRITE_VERIFY_16
:
2711 /* MMC writing cannot be done via DMA helpers, because it sometimes
2712 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2713 * We might use scsi_disk_dma_reqops as long as no writing commands are
2714 * seen, but performance usually isn't paramount on optical media. So,
2715 * just make scsi-block operate the same as scsi-generic for them.
2717 if (s
->qdev
.type
!= TYPE_ROM
) {
2730 static int32_t scsi_block_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2732 SCSIBlockReq
*r
= (SCSIBlockReq
*)req
;
2733 r
->cmd
= req
->cmd
.buf
[0];
2734 switch (r
->cmd
>> 5) {
2737 r
->cdb1
= r
->group_number
= 0;
2741 r
->cdb1
= req
->cmd
.buf
[1];
2742 r
->group_number
= req
->cmd
.buf
[6];
2745 r
->cdb1
= req
->cmd
.buf
[1];
2746 r
->group_number
= req
->cmd
.buf
[10];
2750 r
->cdb1
= req
->cmd
.buf
[1];
2751 r
->group_number
= req
->cmd
.buf
[14];
2757 if (r
->cdb1
& 0xe0) {
2758 /* Protection information is not supported. */
2759 scsi_check_condition(&r
->req
, SENSE_CODE(INVALID_FIELD
));
2763 r
->req
.status
= &r
->io_header
.status
;
2764 return scsi_disk_dma_command(req
, buf
);
2767 static const SCSIReqOps scsi_block_dma_reqops
= {
2768 .size
= sizeof(SCSIBlockReq
),
2769 .free_req
= scsi_free_request
,
2770 .send_command
= scsi_block_dma_command
,
2771 .read_data
= scsi_read_data
,
2772 .write_data
= scsi_write_data
,
2773 .get_buf
= scsi_get_buf
,
2774 .load_request
= scsi_disk_load_request
,
2775 .save_request
= scsi_disk_save_request
,
2778 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2779 uint32_t lun
, uint8_t *buf
,
2782 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2784 if (scsi_block_is_passthrough(s
, buf
)) {
2785 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2788 return scsi_req_alloc(&scsi_block_dma_reqops
, &s
->qdev
, tag
, lun
,
2793 static int scsi_block_parse_cdb(SCSIDevice
*d
, SCSICommand
*cmd
,
2794 uint8_t *buf
, void *hba_private
)
2796 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2798 if (scsi_block_is_passthrough(s
, buf
)) {
2799 return scsi_bus_parse_cdb(&s
->qdev
, cmd
, buf
, hba_private
);
2801 return scsi_req_parse_cdb(&s
->qdev
, cmd
, buf
);
2808 BlockAIOCB
*scsi_dma_readv(int64_t offset
, QEMUIOVector
*iov
,
2809 BlockCompletionFunc
*cb
, void *cb_opaque
,
2812 SCSIDiskReq
*r
= opaque
;
2813 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2814 return blk_aio_preadv(s
->qdev
.conf
.blk
, offset
, iov
, 0, cb
, cb_opaque
);
2818 BlockAIOCB
*scsi_dma_writev(int64_t offset
, QEMUIOVector
*iov
,
2819 BlockCompletionFunc
*cb
, void *cb_opaque
,
2822 SCSIDiskReq
*r
= opaque
;
2823 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2824 return blk_aio_pwritev(s
->qdev
.conf
.blk
, offset
, iov
, 0, cb
, cb_opaque
);
2827 static void scsi_disk_base_class_initfn(ObjectClass
*klass
, void *data
)
2829 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2830 SCSIDiskClass
*sdc
= SCSI_DISK_BASE_CLASS(klass
);
2832 dc
->fw_name
= "disk";
2833 dc
->reset
= scsi_disk_reset
;
2834 sdc
->dma_readv
= scsi_dma_readv
;
2835 sdc
->dma_writev
= scsi_dma_writev
;
2836 sdc
->need_fua_emulation
= scsi_is_cmd_fua
;
2839 static const TypeInfo scsi_disk_base_info
= {
2840 .name
= TYPE_SCSI_DISK_BASE
,
2841 .parent
= TYPE_SCSI_DEVICE
,
2842 .class_init
= scsi_disk_base_class_initfn
,
2843 .instance_size
= sizeof(SCSIDiskState
),
2844 .class_size
= sizeof(SCSIDiskClass
),
2848 #define DEFINE_SCSI_DISK_PROPERTIES() \
2849 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2850 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2851 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2852 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2853 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2855 static Property scsi_hd_properties
[] = {
2856 DEFINE_SCSI_DISK_PROPERTIES(),
2857 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2858 SCSI_DISK_F_REMOVABLE
, false),
2859 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2860 SCSI_DISK_F_DPOFUA
, false),
2861 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
2862 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
2863 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2864 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2865 DEFAULT_MAX_UNMAP_SIZE
),
2866 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2867 DEFAULT_MAX_IO_SIZE
),
2868 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
2869 DEFINE_PROP_END_OF_LIST(),
2872 static const VMStateDescription vmstate_scsi_disk_state
= {
2873 .name
= "scsi-disk",
2875 .minimum_version_id
= 1,
2876 .fields
= (VMStateField
[]) {
2877 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
2878 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
2879 VMSTATE_BOOL(media_event
, SCSIDiskState
),
2880 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
2881 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
2882 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
2883 VMSTATE_END_OF_LIST()
2887 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
2889 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2890 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2892 sc
->realize
= scsi_hd_realize
;
2893 sc
->alloc_req
= scsi_new_request
;
2894 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2895 dc
->desc
= "virtual SCSI disk";
2896 dc
->props
= scsi_hd_properties
;
2897 dc
->vmsd
= &vmstate_scsi_disk_state
;
2900 static const TypeInfo scsi_hd_info
= {
2902 .parent
= TYPE_SCSI_DISK_BASE
,
2903 .class_init
= scsi_hd_class_initfn
,
2906 static Property scsi_cd_properties
[] = {
2907 DEFINE_SCSI_DISK_PROPERTIES(),
2908 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
2909 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
2910 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2911 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2912 DEFAULT_MAX_IO_SIZE
),
2913 DEFINE_PROP_END_OF_LIST(),
2916 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
2918 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2919 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2921 sc
->realize
= scsi_cd_realize
;
2922 sc
->alloc_req
= scsi_new_request
;
2923 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2924 dc
->desc
= "virtual SCSI CD-ROM";
2925 dc
->props
= scsi_cd_properties
;
2926 dc
->vmsd
= &vmstate_scsi_disk_state
;
2929 static const TypeInfo scsi_cd_info
= {
2931 .parent
= TYPE_SCSI_DISK_BASE
,
2932 .class_init
= scsi_cd_class_initfn
,
2936 static Property scsi_block_properties
[] = {
2937 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.blk
),
2938 DEFINE_PROP_END_OF_LIST(),
2941 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
2943 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2944 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2945 SCSIDiskClass
*sdc
= SCSI_DISK_BASE_CLASS(klass
);
2947 sc
->realize
= scsi_block_realize
;
2948 sc
->alloc_req
= scsi_block_new_request
;
2949 sc
->parse_cdb
= scsi_block_parse_cdb
;
2950 sdc
->dma_readv
= scsi_block_dma_readv
;
2951 sdc
->dma_writev
= scsi_block_dma_writev
;
2952 sdc
->need_fua_emulation
= scsi_block_no_fua
;
2953 dc
->desc
= "SCSI block device passthrough";
2954 dc
->props
= scsi_block_properties
;
2955 dc
->vmsd
= &vmstate_scsi_disk_state
;
2958 static const TypeInfo scsi_block_info
= {
2959 .name
= "scsi-block",
2960 .parent
= TYPE_SCSI_DISK_BASE
,
2961 .class_init
= scsi_block_class_initfn
,
2965 static Property scsi_disk_properties
[] = {
2966 DEFINE_SCSI_DISK_PROPERTIES(),
2967 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2968 SCSI_DISK_F_REMOVABLE
, false),
2969 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2970 SCSI_DISK_F_DPOFUA
, false),
2971 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
2972 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
2973 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
2974 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
2975 DEFAULT_MAX_UNMAP_SIZE
),
2976 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
2977 DEFAULT_MAX_IO_SIZE
),
2978 DEFINE_PROP_END_OF_LIST(),
2981 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
2983 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2984 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
2986 sc
->realize
= scsi_disk_realize
;
2987 sc
->alloc_req
= scsi_new_request
;
2988 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
2989 dc
->fw_name
= "disk";
2990 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
2991 dc
->reset
= scsi_disk_reset
;
2992 dc
->props
= scsi_disk_properties
;
2993 dc
->vmsd
= &vmstate_scsi_disk_state
;
2996 static const TypeInfo scsi_disk_info
= {
2997 .name
= "scsi-disk",
2998 .parent
= TYPE_SCSI_DISK_BASE
,
2999 .class_init
= scsi_disk_class_initfn
,
3002 static void scsi_disk_register_types(void)
3004 type_register_static(&scsi_disk_base_info
);
3005 type_register_static(&scsi_hd_info
);
3006 type_register_static(&scsi_cd_info
);
3008 type_register_static(&scsi_block_info
);
3010 type_register_static(&scsi_disk_info
);
3013 type_init(scsi_disk_register_types
)