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.
22 #include "qemu/osdep.h"
23 #include "qemu/units.h"
24 #include "qapi/error.h"
25 #include "qemu/error-report.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/module.h"
28 #include "hw/scsi/scsi.h"
29 #include "migration/qemu-file-types.h"
30 #include "migration/vmstate.h"
31 #include "hw/scsi/emulation.h"
32 #include "scsi/constants.h"
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "hw/qdev-properties.h"
37 #include "sysemu/dma.h"
38 #include "sysemu/sysemu.h"
39 #include "qemu/cutils.h"
46 #define SCSI_WRITE_SAME_MAX (512 * KiB)
47 #define SCSI_DMA_BUF_SIZE (128 * KiB)
48 #define SCSI_MAX_INQUIRY_LEN 256
49 #define SCSI_MAX_MODE_LEN 256
51 #define DEFAULT_DISCARD_GRANULARITY (4 * KiB)
52 #define DEFAULT_MAX_UNMAP_SIZE (1 * GiB)
53 #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */
55 #define TYPE_SCSI_DISK_BASE "scsi-disk-base"
57 #define SCSI_DISK_BASE(obj) \
58 OBJECT_CHECK(SCSIDiskState, (obj), TYPE_SCSI_DISK_BASE)
59 #define SCSI_DISK_BASE_CLASS(klass) \
60 OBJECT_CLASS_CHECK(SCSIDiskClass, (klass), TYPE_SCSI_DISK_BASE)
61 #define SCSI_DISK_BASE_GET_CLASS(obj) \
62 OBJECT_GET_CLASS(SCSIDiskClass, (obj), TYPE_SCSI_DISK_BASE)
64 typedef struct SCSIDiskClass
{
65 SCSIDeviceClass parent_class
;
67 DMAIOFunc
*dma_writev
;
68 bool (*need_fua_emulation
)(SCSICommand
*cmd
);
69 void (*update_sense
)(SCSIRequest
*r
);
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 * 0x0000 - rotation rate not reported
110 * 0x0001 - non-rotating medium (SSD)
111 * 0x0002-0x0400 - reserved
112 * 0x0401-0xffe - rotations per minute
115 uint16_t rotation_rate
;
118 static bool scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, bool acct_failed
);
120 static void scsi_free_request(SCSIRequest
*req
)
122 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
124 qemu_vfree(r
->iov
.iov_base
);
127 /* Helper function for command completion with sense. */
128 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
130 trace_scsi_disk_check_condition(r
->req
.tag
, sense
.key
, sense
.asc
,
132 scsi_req_build_sense(&r
->req
, sense
);
133 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
136 static void scsi_init_iovec(SCSIDiskReq
*r
, size_t size
)
138 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
140 if (!r
->iov
.iov_base
) {
142 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
144 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
145 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
148 static void scsi_disk_save_request(QEMUFile
*f
, SCSIRequest
*req
)
150 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
152 qemu_put_be64s(f
, &r
->sector
);
153 qemu_put_be32s(f
, &r
->sector_count
);
154 qemu_put_be32s(f
, &r
->buflen
);
156 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
157 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
158 } else if (!req
->retry
) {
159 uint32_t len
= r
->iov
.iov_len
;
160 qemu_put_be32s(f
, &len
);
161 qemu_put_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
166 static void scsi_disk_load_request(QEMUFile
*f
, SCSIRequest
*req
)
168 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
170 qemu_get_be64s(f
, &r
->sector
);
171 qemu_get_be32s(f
, &r
->sector_count
);
172 qemu_get_be32s(f
, &r
->buflen
);
174 scsi_init_iovec(r
, r
->buflen
);
175 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
176 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
177 } else if (!r
->req
.retry
) {
179 qemu_get_be32s(f
, &len
);
180 r
->iov
.iov_len
= len
;
181 assert(r
->iov
.iov_len
<= r
->buflen
);
182 qemu_get_buffer(f
, r
->iov
.iov_base
, r
->iov
.iov_len
);
186 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
189 static bool scsi_disk_req_check_error(SCSIDiskReq
*r
, int ret
, bool acct_failed
)
191 if (r
->req
.io_canceled
) {
192 scsi_req_cancel_complete(&r
->req
);
196 if (ret
< 0 || (r
->status
&& *r
->status
)) {
197 return scsi_handle_rw_error(r
, -ret
, acct_failed
);
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 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
211 if (scsi_disk_req_check_error(r
, ret
, true)) {
215 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
216 scsi_req_complete(&r
->req
, GOOD
);
219 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
220 scsi_req_unref(&r
->req
);
223 static bool scsi_is_cmd_fua(SCSICommand
*cmd
)
225 switch (cmd
->buf
[0]) {
232 return (cmd
->buf
[1] & 8) != 0;
237 case WRITE_VERIFY_10
:
238 case WRITE_VERIFY_12
:
239 case WRITE_VERIFY_16
:
249 static void scsi_write_do_fua(SCSIDiskReq
*r
)
251 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
253 assert(r
->req
.aiocb
== NULL
);
254 assert(!r
->req
.io_canceled
);
256 if (r
->need_fua_emulation
) {
257 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
259 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
263 scsi_req_complete(&r
->req
, GOOD
);
264 scsi_req_unref(&r
->req
);
267 static void scsi_dma_complete_noio(SCSIDiskReq
*r
, int ret
)
269 assert(r
->req
.aiocb
== NULL
);
270 if (scsi_disk_req_check_error(r
, ret
, false)) {
274 r
->sector
+= r
->sector_count
;
276 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
277 scsi_write_do_fua(r
);
280 scsi_req_complete(&r
->req
, GOOD
);
284 scsi_req_unref(&r
->req
);
287 static void scsi_dma_complete(void *opaque
, int ret
)
289 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
290 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
292 assert(r
->req
.aiocb
!= NULL
);
295 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
297 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
299 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
301 scsi_dma_complete_noio(r
, ret
);
302 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
305 static void scsi_read_complete_noio(SCSIDiskReq
*r
, int ret
)
309 assert(r
->req
.aiocb
== NULL
);
310 if (scsi_disk_req_check_error(r
, ret
, false)) {
314 n
= r
->qiov
.size
/ 512;
316 r
->sector_count
-= n
;
317 scsi_req_data(&r
->req
, r
->qiov
.size
);
320 scsi_req_unref(&r
->req
);
323 static void scsi_read_complete(void *opaque
, int ret
)
325 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
326 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
328 assert(r
->req
.aiocb
!= NULL
);
331 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
333 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
335 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
336 trace_scsi_disk_read_complete(r
->req
.tag
, r
->qiov
.size
);
338 scsi_read_complete_noio(r
, ret
);
339 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
342 /* Actually issue a read to the block device. */
343 static void scsi_do_read(SCSIDiskReq
*r
, int ret
)
345 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
346 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
348 assert (r
->req
.aiocb
== NULL
);
349 if (scsi_disk_req_check_error(r
, ret
, false)) {
353 /* The request is used as the AIO opaque value, so add a ref. */
354 scsi_req_ref(&r
->req
);
357 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_READ
);
358 r
->req
.resid
-= r
->req
.sg
->size
;
359 r
->req
.aiocb
= dma_blk_io(blk_get_aio_context(s
->qdev
.conf
.blk
),
360 r
->req
.sg
, r
->sector
<< BDRV_SECTOR_BITS
,
362 sdc
->dma_readv
, r
, scsi_dma_complete
, r
,
363 DMA_DIRECTION_FROM_DEVICE
);
365 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
366 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
367 r
->qiov
.size
, BLOCK_ACCT_READ
);
368 r
->req
.aiocb
= sdc
->dma_readv(r
->sector
<< BDRV_SECTOR_BITS
, &r
->qiov
,
369 scsi_read_complete
, r
, r
);
373 scsi_req_unref(&r
->req
);
376 static void scsi_do_read_cb(void *opaque
, int ret
)
378 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
379 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
381 assert (r
->req
.aiocb
!= NULL
);
384 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
386 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
388 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
390 scsi_do_read(opaque
, ret
);
391 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
394 /* Read more data from scsi device into buffer. */
395 static void scsi_read_data(SCSIRequest
*req
)
397 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
398 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
401 trace_scsi_disk_read_data_count(r
->sector_count
);
402 if (r
->sector_count
== 0) {
403 /* This also clears the sense buffer for REQUEST SENSE. */
404 scsi_req_complete(&r
->req
, GOOD
);
408 /* No data transfer may already be in progress */
409 assert(r
->req
.aiocb
== NULL
);
411 /* The request is used as the AIO opaque value, so add a ref. */
412 scsi_req_ref(&r
->req
);
413 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
414 trace_scsi_disk_read_data_invalid();
415 scsi_read_complete_noio(r
, -EINVAL
);
419 if (!blk_is_available(req
->dev
->conf
.blk
)) {
420 scsi_read_complete_noio(r
, -ENOMEDIUM
);
426 if (first
&& r
->need_fua_emulation
) {
427 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
429 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_do_read_cb
, r
);
436 * scsi_handle_rw_error has two return values. False means that the error
437 * must be ignored, true means that the error has been processed and the
438 * caller should not do anything else for this request. Note that
439 * scsi_handle_rw_error always manages its reference counts, independent
440 * of the return value.
442 static bool scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, bool acct_failed
)
444 bool is_read
= (r
->req
.cmd
.mode
== SCSI_XFER_FROM_DEV
);
445 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
446 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
447 BlockErrorAction action
= blk_get_error_action(s
->qdev
.conf
.blk
,
450 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
452 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
456 /* A passthrough command has run and has produced sense data; check
457 * whether the error has to be handled by the guest or should rather
460 assert(r
->status
&& *r
->status
);
461 if (scsi_sense_buf_is_guest_recoverable(r
->req
.sense
, sizeof(r
->req
.sense
))) {
462 /* These errors are handled by guest. */
463 sdc
->update_sense(&r
->req
);
464 scsi_req_complete(&r
->req
, *r
->status
);
467 error
= scsi_sense_buf_to_errno(r
->req
.sense
, sizeof(r
->req
.sense
));
470 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
473 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
476 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
479 scsi_check_condition(r
, SENSE_CODE(SPACE_ALLOC_FAILED
));
482 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
487 blk_error_action(s
->qdev
.conf
.blk
, action
, is_read
, error
);
488 if (action
== BLOCK_ERROR_ACTION_IGNORE
) {
489 scsi_req_complete(&r
->req
, 0);
493 if (action
== BLOCK_ERROR_ACTION_STOP
) {
494 scsi_req_retry(&r
->req
);
499 static void scsi_write_complete_noio(SCSIDiskReq
*r
, int ret
)
503 assert (r
->req
.aiocb
== NULL
);
504 if (scsi_disk_req_check_error(r
, ret
, false)) {
508 n
= r
->qiov
.size
/ 512;
510 r
->sector_count
-= n
;
511 if (r
->sector_count
== 0) {
512 scsi_write_do_fua(r
);
515 scsi_init_iovec(r
, SCSI_DMA_BUF_SIZE
);
516 trace_scsi_disk_write_complete_noio(r
->req
.tag
, r
->qiov
.size
);
517 scsi_req_data(&r
->req
, r
->qiov
.size
);
521 scsi_req_unref(&r
->req
);
524 static void scsi_write_complete(void * opaque
, int ret
)
526 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
527 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
529 assert (r
->req
.aiocb
!= NULL
);
532 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
534 block_acct_failed(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
536 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
538 scsi_write_complete_noio(r
, ret
);
539 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
542 static void scsi_write_data(SCSIRequest
*req
)
544 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
545 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
546 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
548 /* No data transfer may already be in progress */
549 assert(r
->req
.aiocb
== NULL
);
551 /* The request is used as the AIO opaque value, so add a ref. */
552 scsi_req_ref(&r
->req
);
553 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
554 trace_scsi_disk_write_data_invalid();
555 scsi_write_complete_noio(r
, -EINVAL
);
559 if (!r
->req
.sg
&& !r
->qiov
.size
) {
560 /* Called for the first time. Ask the driver to send us more data. */
562 scsi_write_complete_noio(r
, 0);
565 if (!blk_is_available(req
->dev
->conf
.blk
)) {
566 scsi_write_complete_noio(r
, -ENOMEDIUM
);
570 if (r
->req
.cmd
.buf
[0] == VERIFY_10
|| r
->req
.cmd
.buf
[0] == VERIFY_12
||
571 r
->req
.cmd
.buf
[0] == VERIFY_16
) {
573 scsi_dma_complete_noio(r
, 0);
575 scsi_write_complete_noio(r
, 0);
581 dma_acct_start(s
->qdev
.conf
.blk
, &r
->acct
, r
->req
.sg
, BLOCK_ACCT_WRITE
);
582 r
->req
.resid
-= r
->req
.sg
->size
;
583 r
->req
.aiocb
= dma_blk_io(blk_get_aio_context(s
->qdev
.conf
.blk
),
584 r
->req
.sg
, r
->sector
<< BDRV_SECTOR_BITS
,
586 sdc
->dma_writev
, r
, scsi_dma_complete
, r
,
587 DMA_DIRECTION_TO_DEVICE
);
589 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
590 r
->qiov
.size
, BLOCK_ACCT_WRITE
);
591 r
->req
.aiocb
= sdc
->dma_writev(r
->sector
<< BDRV_SECTOR_BITS
, &r
->qiov
,
592 scsi_write_complete
, r
, r
);
596 /* Return a pointer to the data buffer. */
597 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
599 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
601 return (uint8_t *)r
->iov
.iov_base
;
604 static int scsi_disk_emulate_vpd_page(SCSIRequest
*req
, uint8_t *outbuf
)
606 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
607 uint8_t page_code
= req
->cmd
.buf
[2];
608 int start
, buflen
= 0;
610 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
611 outbuf
[buflen
++] = page_code
;
612 outbuf
[buflen
++] = 0x00;
613 outbuf
[buflen
++] = 0x00;
617 case 0x00: /* Supported page codes, mandatory */
619 trace_scsi_disk_emulate_vpd_page_00(req
->cmd
.xfer
);
620 outbuf
[buflen
++] = 0x00; /* list of supported pages (this page) */
622 outbuf
[buflen
++] = 0x80; /* unit serial number */
624 outbuf
[buflen
++] = 0x83; /* device identification */
625 if (s
->qdev
.type
== TYPE_DISK
) {
626 outbuf
[buflen
++] = 0xb0; /* block limits */
627 outbuf
[buflen
++] = 0xb1; /* block device characteristics */
628 outbuf
[buflen
++] = 0xb2; /* thin provisioning */
632 case 0x80: /* Device serial number, optional */
637 trace_scsi_disk_emulate_vpd_page_80_not_supported();
641 l
= strlen(s
->serial
);
646 trace_scsi_disk_emulate_vpd_page_80(req
->cmd
.xfer
);
647 memcpy(outbuf
+ buflen
, s
->serial
, l
);
652 case 0x83: /* Device identification page, mandatory */
654 int id_len
= s
->device_id
? MIN(strlen(s
->device_id
), 255 - 8) : 0;
656 trace_scsi_disk_emulate_vpd_page_83(req
->cmd
.xfer
);
659 outbuf
[buflen
++] = 0x2; /* ASCII */
660 outbuf
[buflen
++] = 0; /* not officially assigned */
661 outbuf
[buflen
++] = 0; /* reserved */
662 outbuf
[buflen
++] = id_len
; /* length of data following */
663 memcpy(outbuf
+ buflen
, s
->device_id
, id_len
);
668 outbuf
[buflen
++] = 0x1; /* Binary */
669 outbuf
[buflen
++] = 0x3; /* NAA */
670 outbuf
[buflen
++] = 0; /* reserved */
671 outbuf
[buflen
++] = 8;
672 stq_be_p(&outbuf
[buflen
], s
->qdev
.wwn
);
676 if (s
->qdev
.port_wwn
) {
677 outbuf
[buflen
++] = 0x61; /* SAS / Binary */
678 outbuf
[buflen
++] = 0x93; /* PIV / Target port / NAA */
679 outbuf
[buflen
++] = 0; /* reserved */
680 outbuf
[buflen
++] = 8;
681 stq_be_p(&outbuf
[buflen
], s
->qdev
.port_wwn
);
686 outbuf
[buflen
++] = 0x61; /* SAS / Binary */
688 /* PIV/Target port/relative target port */
689 outbuf
[buflen
++] = 0x94;
691 outbuf
[buflen
++] = 0; /* reserved */
692 outbuf
[buflen
++] = 4;
693 stw_be_p(&outbuf
[buflen
+ 2], s
->port_index
);
698 case 0xb0: /* block limits */
700 SCSIBlockLimits bl
= {};
702 if (s
->qdev
.type
== TYPE_ROM
) {
703 trace_scsi_disk_emulate_vpd_page_b0_not_supported();
708 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
710 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
712 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
713 bl
.max_unmap_sectors
=
714 s
->max_unmap_size
/ s
->qdev
.blocksize
;
716 s
->max_io_size
/ s
->qdev
.blocksize
;
717 /* 255 descriptors fit in 4 KiB with an 8-byte header */
718 bl
.max_unmap_descr
= 255;
720 if (s
->qdev
.type
== TYPE_DISK
) {
721 int max_transfer_blk
= blk_get_max_transfer(s
->qdev
.conf
.blk
);
722 int max_io_sectors_blk
=
723 max_transfer_blk
/ s
->qdev
.blocksize
;
726 MIN_NON_ZERO(max_io_sectors_blk
, bl
.max_io_sectors
);
728 buflen
+= scsi_emulate_block_limits(outbuf
+ buflen
, &bl
);
731 case 0xb1: /* block device characteristics */
734 outbuf
[4] = (s
->rotation_rate
>> 8) & 0xff;
735 outbuf
[5] = s
->rotation_rate
& 0xff;
736 outbuf
[6] = 0; /* PRODUCT TYPE */
737 outbuf
[7] = 0; /* WABEREQ | WACEREQ | NOMINAL FORM FACTOR */
738 outbuf
[8] = 0; /* VBULS */
741 case 0xb2: /* thin provisioning */
745 outbuf
[5] = 0xe0; /* unmap & write_same 10/16 all supported */
746 outbuf
[6] = s
->qdev
.conf
.discard_granularity
? 2 : 1;
754 assert(buflen
- start
<= 255);
755 outbuf
[start
- 1] = buflen
- start
;
759 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
761 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
764 if (req
->cmd
.buf
[1] & 0x1) {
765 /* Vital product data */
766 return scsi_disk_emulate_vpd_page(req
, outbuf
);
769 /* Standard INQUIRY data */
770 if (req
->cmd
.buf
[2] != 0) {
775 buflen
= req
->cmd
.xfer
;
776 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
777 buflen
= SCSI_MAX_INQUIRY_LEN
;
780 outbuf
[0] = s
->qdev
.type
& 0x1f;
781 outbuf
[1] = (s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) ? 0x80 : 0;
783 strpadcpy((char *) &outbuf
[16], 16, s
->product
, ' ');
784 strpadcpy((char *) &outbuf
[8], 8, s
->vendor
, ' ');
786 memset(&outbuf
[32], 0, 4);
787 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
789 * We claim conformance to SPC-3, which is required for guests
790 * to ask for modern features like READ CAPACITY(16) or the
791 * block characteristics VPD page by default. Not all of SPC-3
792 * is actually implemented, but we're good enough.
794 outbuf
[2] = s
->qdev
.default_scsi_version
;
795 outbuf
[3] = 2 | 0x10; /* Format 2, HiSup */
798 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
800 /* If the allocation length of CDB is too small,
801 the additional length is not adjusted */
805 /* Sync data transfer and TCQ. */
806 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
810 static inline bool media_is_dvd(SCSIDiskState
*s
)
813 if (s
->qdev
.type
!= TYPE_ROM
) {
816 if (!blk_is_available(s
->qdev
.conf
.blk
)) {
819 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
820 return nb_sectors
> CD_MAX_SECTORS
;
823 static inline bool media_is_cd(SCSIDiskState
*s
)
826 if (s
->qdev
.type
!= TYPE_ROM
) {
829 if (!blk_is_available(s
->qdev
.conf
.blk
)) {
832 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
833 return nb_sectors
<= CD_MAX_SECTORS
;
836 static int scsi_read_disc_information(SCSIDiskState
*s
, SCSIDiskReq
*r
,
839 uint8_t type
= r
->req
.cmd
.buf
[1] & 7;
841 if (s
->qdev
.type
!= TYPE_ROM
) {
845 /* Types 1/2 are only defined for Blu-Ray. */
847 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
851 memset(outbuf
, 0, 34);
853 outbuf
[2] = 0xe; /* last session complete, disc finalized */
854 outbuf
[3] = 1; /* first track on disc */
855 outbuf
[4] = 1; /* # of sessions */
856 outbuf
[5] = 1; /* first track of last session */
857 outbuf
[6] = 1; /* last track of last session */
858 outbuf
[7] = 0x20; /* unrestricted use */
859 outbuf
[8] = 0x00; /* CD-ROM or DVD-ROM */
860 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
861 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
862 /* 24-31: disc bar code */
863 /* 32: disc application code */
864 /* 33: number of OPC tables */
869 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
872 static const int rds_caps_size
[5] = {
879 uint8_t media
= r
->req
.cmd
.buf
[1];
880 uint8_t layer
= r
->req
.cmd
.buf
[6];
881 uint8_t format
= r
->req
.cmd
.buf
[7];
884 if (s
->qdev
.type
!= TYPE_ROM
) {
888 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
892 if (format
!= 0xff) {
893 if (!blk_is_available(s
->qdev
.conf
.blk
)) {
894 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
897 if (media_is_cd(s
)) {
898 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
901 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
904 size
= rds_caps_size
[format
];
905 memset(outbuf
, 0, size
);
910 /* Physical format information */
915 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
917 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
918 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
919 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
920 outbuf
[7] = 0; /* default densities */
922 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
923 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
927 case 0x01: /* DVD copyright information, all zeros */
930 case 0x03: /* BCA information - invalid field for no BCA info */
933 case 0x04: /* DVD disc manufacturing information, all zeros */
936 case 0xff: { /* List capabilities */
939 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
940 if (!rds_caps_size
[i
]) {
944 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
945 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
955 /* Size of buffer, not including 2 byte size field */
956 stw_be_p(outbuf
, size
- 2);
963 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
965 uint8_t event_code
, media_status
;
969 media_status
= MS_TRAY_OPEN
;
970 } else if (blk_is_inserted(s
->qdev
.conf
.blk
)) {
971 media_status
= MS_MEDIA_PRESENT
;
974 /* Event notification descriptor */
975 event_code
= MEC_NO_CHANGE
;
976 if (media_status
!= MS_TRAY_OPEN
) {
977 if (s
->media_event
) {
978 event_code
= MEC_NEW_MEDIA
;
979 s
->media_event
= false;
980 } else if (s
->eject_request
) {
981 event_code
= MEC_EJECT_REQUESTED
;
982 s
->eject_request
= false;
986 outbuf
[0] = event_code
;
987 outbuf
[1] = media_status
;
989 /* These fields are reserved, just clear them. */
995 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
999 uint8_t *buf
= r
->req
.cmd
.buf
;
1000 uint8_t notification_class_request
= buf
[4];
1001 if (s
->qdev
.type
!= TYPE_ROM
) {
1004 if ((buf
[1] & 1) == 0) {
1010 outbuf
[0] = outbuf
[1] = 0;
1011 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
1012 if (notification_class_request
& (1 << GESN_MEDIA
)) {
1013 outbuf
[2] = GESN_MEDIA
;
1014 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
1018 stw_be_p(outbuf
, size
- 4);
1022 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
1026 if (s
->qdev
.type
!= TYPE_ROM
) {
1030 if (media_is_dvd(s
)) {
1031 current
= MMC_PROFILE_DVD_ROM
;
1032 } else if (media_is_cd(s
)) {
1033 current
= MMC_PROFILE_CD_ROM
;
1035 current
= MMC_PROFILE_NONE
;
1038 memset(outbuf
, 0, 40);
1039 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
1040 stw_be_p(&outbuf
[6], current
);
1041 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
1042 outbuf
[10] = 0x03; /* persistent, current */
1043 outbuf
[11] = 8; /* two profiles */
1044 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
1045 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
1046 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
1047 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
1048 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
1049 stw_be_p(&outbuf
[20], 1);
1050 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
1052 stl_be_p(&outbuf
[24], 1); /* SCSI */
1053 outbuf
[28] = 1; /* DBE = 1, mandatory */
1054 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
1055 stw_be_p(&outbuf
[32], 3);
1056 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
1058 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1059 /* TODO: Random readable, CD read, DVD read, drive serial number,
1064 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
1066 if (s
->qdev
.type
!= TYPE_ROM
) {
1069 memset(outbuf
, 0, 8);
1070 outbuf
[5] = 1; /* CD-ROM */
1074 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
1077 static const int mode_sense_valid
[0x3f] = {
1078 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
1079 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
1080 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1081 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
1082 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
1083 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
1086 uint8_t *p
= *p_outbuf
+ 2;
1089 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
1094 * If Changeable Values are requested, a mask denoting those mode parameters
1095 * that are changeable shall be returned. As we currently don't support
1096 * parameter changes via MODE_SELECT all bits are returned set to zero.
1097 * The buffer was already menset to zero by the caller of this function.
1099 * The offsets here are off by two compared to the descriptions in the
1100 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1101 * but it is done so that offsets are consistent within our implementation
1102 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1103 * 2-byte and 4-byte headers.
1106 case MODE_PAGE_HD_GEOMETRY
:
1108 if (page_control
== 1) { /* Changeable Values */
1111 /* if a geometry hint is available, use it */
1112 p
[0] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1113 p
[1] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1114 p
[2] = s
->qdev
.conf
.cyls
& 0xff;
1115 p
[3] = s
->qdev
.conf
.heads
& 0xff;
1116 /* Write precomp start cylinder, disabled */
1117 p
[4] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1118 p
[5] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1119 p
[6] = s
->qdev
.conf
.cyls
& 0xff;
1120 /* Reduced current start cylinder, disabled */
1121 p
[7] = (s
->qdev
.conf
.cyls
>> 16) & 0xff;
1122 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1123 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1124 /* Device step rate [ns], 200ns */
1127 /* Landing zone cylinder */
1131 /* Medium rotation rate [rpm], 5400 rpm */
1132 p
[18] = (5400 >> 8) & 0xff;
1133 p
[19] = 5400 & 0xff;
1136 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
1138 if (page_control
== 1) { /* Changeable Values */
1141 /* Transfer rate [kbit/s], 5Mbit/s */
1144 /* if a geometry hint is available, use it */
1145 p
[2] = s
->qdev
.conf
.heads
& 0xff;
1146 p
[3] = s
->qdev
.conf
.secs
& 0xff;
1147 p
[4] = s
->qdev
.blocksize
>> 8;
1148 p
[6] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1149 p
[7] = s
->qdev
.conf
.cyls
& 0xff;
1150 /* Write precomp start cylinder, disabled */
1151 p
[8] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1152 p
[9] = s
->qdev
.conf
.cyls
& 0xff;
1153 /* Reduced current start cylinder, disabled */
1154 p
[10] = (s
->qdev
.conf
.cyls
>> 8) & 0xff;
1155 p
[11] = s
->qdev
.conf
.cyls
& 0xff;
1156 /* Device step rate [100us], 100us */
1159 /* Device step pulse width [us], 1us */
1161 /* Device head settle delay [100us], 100us */
1164 /* Motor on delay [0.1s], 0.1s */
1166 /* Motor off delay [0.1s], 0.1s */
1168 /* Medium rotation rate [rpm], 5400 rpm */
1169 p
[26] = (5400 >> 8) & 0xff;
1170 p
[27] = 5400 & 0xff;
1173 case MODE_PAGE_CACHING
:
1175 if (page_control
== 1 || /* Changeable Values */
1176 blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1181 case MODE_PAGE_R_W_ERROR
:
1183 if (page_control
== 1) { /* Changeable Values */
1186 p
[0] = 0x80; /* Automatic Write Reallocation Enabled */
1187 if (s
->qdev
.type
== TYPE_ROM
) {
1188 p
[1] = 0x20; /* Read Retry Count */
1192 case MODE_PAGE_AUDIO_CTL
:
1196 case MODE_PAGE_CAPABILITIES
:
1198 if (page_control
== 1) { /* Changeable Values */
1202 p
[0] = 0x3b; /* CD-R & CD-RW read */
1203 p
[1] = 0; /* Writing not supported */
1204 p
[2] = 0x7f; /* Audio, composite, digital out,
1205 mode 2 form 1&2, multi session */
1206 p
[3] = 0xff; /* CD DA, DA accurate, RW supported,
1207 RW corrected, C2 errors, ISRC,
1209 p
[4] = 0x2d | (s
->tray_locked
? 2 : 0);
1210 /* Locking supported, jumper present, eject, tray */
1211 p
[5] = 0; /* no volume & mute control, no
1213 p
[6] = (50 * 176) >> 8; /* 50x read speed */
1214 p
[7] = (50 * 176) & 0xff;
1215 p
[8] = 2 >> 8; /* Two volume levels */
1217 p
[10] = 2048 >> 8; /* 2M buffer */
1218 p
[11] = 2048 & 0xff;
1219 p
[12] = (16 * 176) >> 8; /* 16x read speed current */
1220 p
[13] = (16 * 176) & 0xff;
1221 p
[16] = (16 * 176) >> 8; /* 16x write speed */
1222 p
[17] = (16 * 176) & 0xff;
1223 p
[18] = (16 * 176) >> 8; /* 16x write speed current */
1224 p
[19] = (16 * 176) & 0xff;
1231 assert(length
< 256);
1232 (*p_outbuf
)[0] = page
;
1233 (*p_outbuf
)[1] = length
;
1234 *p_outbuf
+= length
+ 2;
1238 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
1240 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1241 uint64_t nb_sectors
;
1243 int page
, buflen
, ret
, page_control
;
1245 uint8_t dev_specific_param
;
1247 dbd
= (r
->req
.cmd
.buf
[1] & 0x8) != 0;
1248 page
= r
->req
.cmd
.buf
[2] & 0x3f;
1249 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
1251 trace_scsi_disk_emulate_mode_sense((r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 :
1252 10, page
, r
->req
.cmd
.xfer
, page_control
);
1253 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
1256 if (s
->qdev
.type
== TYPE_DISK
) {
1257 dev_specific_param
= s
->features
& (1 << SCSI_DISK_F_DPOFUA
) ? 0x10 : 0;
1258 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1259 dev_specific_param
|= 0x80; /* Readonly. */
1262 /* MMC prescribes that CD/DVD drives have no block descriptors,
1263 * and defines no device-specific parameter. */
1264 dev_specific_param
= 0x00;
1268 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1269 p
[1] = 0; /* Default media type. */
1270 p
[2] = dev_specific_param
;
1271 p
[3] = 0; /* Block descriptor length. */
1273 } else { /* MODE_SENSE_10 */
1274 p
[2] = 0; /* Default media type. */
1275 p
[3] = dev_specific_param
;
1276 p
[6] = p
[7] = 0; /* Block descriptor length. */
1280 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1281 if (!dbd
&& nb_sectors
) {
1282 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1283 outbuf
[3] = 8; /* Block descriptor length */
1284 } else { /* MODE_SENSE_10 */
1285 outbuf
[7] = 8; /* Block descriptor length */
1287 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
1288 if (nb_sectors
> 0xffffff) {
1291 p
[0] = 0; /* media density code */
1292 p
[1] = (nb_sectors
>> 16) & 0xff;
1293 p
[2] = (nb_sectors
>> 8) & 0xff;
1294 p
[3] = nb_sectors
& 0xff;
1295 p
[4] = 0; /* reserved */
1296 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1297 p
[6] = s
->qdev
.blocksize
>> 8;
1302 if (page_control
== 3) {
1304 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1309 for (page
= 0; page
<= 0x3e; page
++) {
1310 mode_sense_page(s
, page
, &p
, page_control
);
1313 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1319 buflen
= p
- outbuf
;
1321 * The mode data length field specifies the length in bytes of the
1322 * following data that is available to be transferred. The mode data
1323 * length does not include itself.
1325 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1326 outbuf
[0] = buflen
- 1;
1327 } else { /* MODE_SENSE_10 */
1328 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1329 outbuf
[1] = (buflen
- 2) & 0xff;
1334 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1336 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1337 int start_track
, format
, msf
, toclen
;
1338 uint64_t nb_sectors
;
1340 msf
= req
->cmd
.buf
[1] & 2;
1341 format
= req
->cmd
.buf
[2] & 0xf;
1342 start_track
= req
->cmd
.buf
[6];
1343 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1344 trace_scsi_disk_emulate_read_toc(start_track
, format
, msf
>> 1);
1345 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1348 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1351 /* multi session : only a single session defined */
1353 memset(outbuf
, 0, 12);
1359 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1367 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1369 SCSIRequest
*req
= &r
->req
;
1370 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1371 bool start
= req
->cmd
.buf
[4] & 1;
1372 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1373 int pwrcnd
= req
->cmd
.buf
[4] & 0xf0;
1376 /* eject/load only happens for power condition == 0 */
1380 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) && loej
) {
1381 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1382 scsi_check_condition(r
,
1383 blk_is_inserted(s
->qdev
.conf
.blk
)
1384 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1385 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1389 if (s
->tray_open
!= !start
) {
1390 blk_eject(s
->qdev
.conf
.blk
, !start
);
1391 s
->tray_open
= !start
;
1397 static void scsi_disk_emulate_read_data(SCSIRequest
*req
)
1399 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1400 int buflen
= r
->iov
.iov_len
;
1403 trace_scsi_disk_emulate_read_data(buflen
);
1406 scsi_req_data(&r
->req
, buflen
);
1410 /* This also clears the sense buffer for REQUEST SENSE. */
1411 scsi_req_complete(&r
->req
, GOOD
);
1414 static int scsi_disk_check_mode_select(SCSIDiskState
*s
, int page
,
1415 uint8_t *inbuf
, int inlen
)
1417 uint8_t mode_current
[SCSI_MAX_MODE_LEN
];
1418 uint8_t mode_changeable
[SCSI_MAX_MODE_LEN
];
1420 int len
, expected_len
, changeable_len
, i
;
1422 /* The input buffer does not include the page header, so it is
1425 expected_len
= inlen
+ 2;
1426 if (expected_len
> SCSI_MAX_MODE_LEN
) {
1431 memset(mode_current
, 0, inlen
+ 2);
1432 len
= mode_sense_page(s
, page
, &p
, 0);
1433 if (len
< 0 || len
!= expected_len
) {
1437 p
= mode_changeable
;
1438 memset(mode_changeable
, 0, inlen
+ 2);
1439 changeable_len
= mode_sense_page(s
, page
, &p
, 1);
1440 assert(changeable_len
== len
);
1442 /* Check that unchangeable bits are the same as what MODE SENSE
1445 for (i
= 2; i
< len
; i
++) {
1446 if (((mode_current
[i
] ^ inbuf
[i
- 2]) & ~mode_changeable
[i
]) != 0) {
1453 static void scsi_disk_apply_mode_select(SCSIDiskState
*s
, int page
, uint8_t *p
)
1456 case MODE_PAGE_CACHING
:
1457 blk_set_enable_write_cache(s
->qdev
.conf
.blk
, (p
[0] & 4) != 0);
1465 static int mode_select_pages(SCSIDiskReq
*r
, uint8_t *p
, int len
, bool change
)
1467 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1470 int page
, subpage
, page_len
;
1472 /* Parse both possible formats for the mode page headers. */
1476 goto invalid_param_len
;
1479 page_len
= lduw_be_p(&p
[2]);
1484 goto invalid_param_len
;
1495 if (page_len
> len
) {
1496 goto invalid_param_len
;
1500 if (scsi_disk_check_mode_select(s
, page
, p
, page_len
) < 0) {
1504 scsi_disk_apply_mode_select(s
, page
, p
);
1513 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1517 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1521 static void scsi_disk_emulate_mode_select(SCSIDiskReq
*r
, uint8_t *inbuf
)
1523 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1525 int cmd
= r
->req
.cmd
.buf
[0];
1526 int len
= r
->req
.cmd
.xfer
;
1527 int hdr_len
= (cmd
== MODE_SELECT
? 4 : 8);
1531 /* We only support PF=1, SP=0. */
1532 if ((r
->req
.cmd
.buf
[1] & 0x11) != 0x10) {
1536 if (len
< hdr_len
) {
1537 goto invalid_param_len
;
1540 bd_len
= (cmd
== MODE_SELECT
? p
[3] : lduw_be_p(&p
[6]));
1544 goto invalid_param_len
;
1546 if (bd_len
!= 0 && bd_len
!= 8) {
1553 /* Ensure no change is made if there is an error! */
1554 for (pass
= 0; pass
< 2; pass
++) {
1555 if (mode_select_pages(r
, p
, len
, pass
== 1) < 0) {
1560 if (!blk_enable_write_cache(s
->qdev
.conf
.blk
)) {
1561 /* The request is used as the AIO opaque value, so add a ref. */
1562 scsi_req_ref(&r
->req
);
1563 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
1565 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
1569 scsi_req_complete(&r
->req
, GOOD
);
1573 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM
));
1577 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1581 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1584 static inline bool check_lba_range(SCSIDiskState
*s
,
1585 uint64_t sector_num
, uint32_t nb_sectors
)
1588 * The first line tests that no overflow happens when computing the last
1589 * sector. The second line tests that the last accessed sector is in
1592 * Careful, the computations should not underflow for nb_sectors == 0,
1593 * and a 0-block read to the first LBA beyond the end of device is
1596 return (sector_num
<= sector_num
+ nb_sectors
&&
1597 sector_num
+ nb_sectors
<= s
->qdev
.max_lba
+ 1);
1600 typedef struct UnmapCBData
{
1606 static void scsi_unmap_complete(void *opaque
, int ret
);
1608 static void scsi_unmap_complete_noio(UnmapCBData
*data
, int ret
)
1610 SCSIDiskReq
*r
= data
->r
;
1611 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1613 assert(r
->req
.aiocb
== NULL
);
1615 if (data
->count
> 0) {
1616 r
->sector
= ldq_be_p(&data
->inbuf
[0])
1617 * (s
->qdev
.blocksize
/ BDRV_SECTOR_SIZE
);
1618 r
->sector_count
= (ldl_be_p(&data
->inbuf
[8]) & 0xffffffffULL
)
1619 * (s
->qdev
.blocksize
/ BDRV_SECTOR_SIZE
);
1620 if (!check_lba_range(s
, r
->sector
, r
->sector_count
)) {
1621 block_acct_invalid(blk_get_stats(s
->qdev
.conf
.blk
),
1623 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1627 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1628 r
->sector_count
* BDRV_SECTOR_SIZE
,
1631 r
->req
.aiocb
= blk_aio_pdiscard(s
->qdev
.conf
.blk
,
1632 r
->sector
* BDRV_SECTOR_SIZE
,
1633 r
->sector_count
* BDRV_SECTOR_SIZE
,
1634 scsi_unmap_complete
, data
);
1640 scsi_req_complete(&r
->req
, GOOD
);
1643 scsi_req_unref(&r
->req
);
1647 static void scsi_unmap_complete(void *opaque
, int ret
)
1649 UnmapCBData
*data
= opaque
;
1650 SCSIDiskReq
*r
= data
->r
;
1651 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1653 assert(r
->req
.aiocb
!= NULL
);
1654 r
->req
.aiocb
= NULL
;
1656 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
1657 if (scsi_disk_req_check_error(r
, ret
, true)) {
1658 scsi_req_unref(&r
->req
);
1661 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
1662 scsi_unmap_complete_noio(data
, ret
);
1664 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
1667 static void scsi_disk_emulate_unmap(SCSIDiskReq
*r
, uint8_t *inbuf
)
1669 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1671 int len
= r
->req
.cmd
.xfer
;
1674 /* Reject ANCHOR=1. */
1675 if (r
->req
.cmd
.buf
[1] & 0x1) {
1680 goto invalid_param_len
;
1682 if (len
< lduw_be_p(&p
[0]) + 2) {
1683 goto invalid_param_len
;
1685 if (len
< lduw_be_p(&p
[2]) + 8) {
1686 goto invalid_param_len
;
1688 if (lduw_be_p(&p
[2]) & 15) {
1689 goto invalid_param_len
;
1692 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1693 block_acct_invalid(blk_get_stats(s
->qdev
.conf
.blk
), BLOCK_ACCT_UNMAP
);
1694 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1698 data
= g_new0(UnmapCBData
, 1);
1700 data
->inbuf
= &p
[8];
1701 data
->count
= lduw_be_p(&p
[2]) >> 4;
1703 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1704 scsi_req_ref(&r
->req
);
1705 scsi_unmap_complete_noio(data
, 0);
1709 block_acct_invalid(blk_get_stats(s
->qdev
.conf
.blk
), BLOCK_ACCT_UNMAP
);
1710 scsi_check_condition(r
, SENSE_CODE(INVALID_PARAM_LEN
));
1714 block_acct_invalid(blk_get_stats(s
->qdev
.conf
.blk
), BLOCK_ACCT_UNMAP
);
1715 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1718 typedef struct WriteSameCBData
{
1726 static void scsi_write_same_complete(void *opaque
, int ret
)
1728 WriteSameCBData
*data
= opaque
;
1729 SCSIDiskReq
*r
= data
->r
;
1730 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
1732 assert(r
->req
.aiocb
!= NULL
);
1733 r
->req
.aiocb
= NULL
;
1734 aio_context_acquire(blk_get_aio_context(s
->qdev
.conf
.blk
));
1735 if (scsi_disk_req_check_error(r
, ret
, true)) {
1739 block_acct_done(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
);
1741 data
->nb_sectors
-= data
->iov
.iov_len
/ 512;
1742 data
->sector
+= data
->iov
.iov_len
/ 512;
1743 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, data
->iov
.iov_len
);
1744 if (data
->iov
.iov_len
) {
1745 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1746 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1747 /* Reinitialize qiov, to handle unaligned WRITE SAME request
1748 * where final qiov may need smaller size */
1749 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1750 r
->req
.aiocb
= blk_aio_pwritev(s
->qdev
.conf
.blk
,
1751 data
->sector
<< BDRV_SECTOR_BITS
,
1753 scsi_write_same_complete
, data
);
1754 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
1758 scsi_req_complete(&r
->req
, GOOD
);
1761 scsi_req_unref(&r
->req
);
1762 qemu_vfree(data
->iov
.iov_base
);
1764 aio_context_release(blk_get_aio_context(s
->qdev
.conf
.blk
));
1767 static void scsi_disk_emulate_write_same(SCSIDiskReq
*r
, uint8_t *inbuf
)
1769 SCSIRequest
*req
= &r
->req
;
1770 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1771 uint32_t nb_sectors
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
1772 WriteSameCBData
*data
;
1776 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1777 if (nb_sectors
== 0 || (req
->cmd
.buf
[1] & 0x16)) {
1778 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1782 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
1783 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
1786 if (!check_lba_range(s
, r
->req
.cmd
.lba
, nb_sectors
)) {
1787 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1791 if ((req
->cmd
.buf
[1] & 0x1) || buffer_is_zero(inbuf
, s
->qdev
.blocksize
)) {
1792 int flags
= (req
->cmd
.buf
[1] & 0x8) ? BDRV_REQ_MAY_UNMAP
: 0;
1794 /* The request is used as the AIO opaque value, so add a ref. */
1795 scsi_req_ref(&r
->req
);
1796 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1797 nb_sectors
* s
->qdev
.blocksize
,
1799 r
->req
.aiocb
= blk_aio_pwrite_zeroes(s
->qdev
.conf
.blk
,
1800 r
->req
.cmd
.lba
* s
->qdev
.blocksize
,
1801 nb_sectors
* s
->qdev
.blocksize
,
1802 flags
, scsi_aio_complete
, r
);
1806 data
= g_new0(WriteSameCBData
, 1);
1808 data
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1809 data
->nb_sectors
= nb_sectors
* (s
->qdev
.blocksize
/ 512);
1810 data
->iov
.iov_len
= MIN(data
->nb_sectors
* 512, SCSI_WRITE_SAME_MAX
);
1811 data
->iov
.iov_base
= buf
= blk_blockalign(s
->qdev
.conf
.blk
,
1813 qemu_iovec_init_external(&data
->qiov
, &data
->iov
, 1);
1815 for (i
= 0; i
< data
->iov
.iov_len
; i
+= s
->qdev
.blocksize
) {
1816 memcpy(&buf
[i
], inbuf
, s
->qdev
.blocksize
);
1819 scsi_req_ref(&r
->req
);
1820 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
,
1821 data
->iov
.iov_len
, BLOCK_ACCT_WRITE
);
1822 r
->req
.aiocb
= blk_aio_pwritev(s
->qdev
.conf
.blk
,
1823 data
->sector
<< BDRV_SECTOR_BITS
,
1825 scsi_write_same_complete
, data
);
1828 static void scsi_disk_emulate_write_data(SCSIRequest
*req
)
1830 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1832 if (r
->iov
.iov_len
) {
1833 int buflen
= r
->iov
.iov_len
;
1834 trace_scsi_disk_emulate_write_data(buflen
);
1836 scsi_req_data(&r
->req
, buflen
);
1840 switch (req
->cmd
.buf
[0]) {
1842 case MODE_SELECT_10
:
1843 /* This also clears the sense buffer for REQUEST SENSE. */
1844 scsi_disk_emulate_mode_select(r
, r
->iov
.iov_base
);
1848 scsi_disk_emulate_unmap(r
, r
->iov
.iov_base
);
1854 if (r
->req
.status
== -1) {
1855 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1861 scsi_disk_emulate_write_same(r
, r
->iov
.iov_base
);
1869 static int32_t scsi_disk_emulate_command(SCSIRequest
*req
, uint8_t *buf
)
1871 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1872 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1873 uint64_t nb_sectors
;
1877 switch (req
->cmd
.buf
[0]) {
1886 case ALLOW_MEDIUM_REMOVAL
:
1887 case GET_CONFIGURATION
:
1888 case GET_EVENT_STATUS_NOTIFICATION
:
1889 case MECHANISM_STATUS
:
1894 if (!blk_is_available(s
->qdev
.conf
.blk
)) {
1895 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1902 * FIXME: we shouldn't return anything bigger than 4k, but the code
1903 * requires the buffer to be as big as req->cmd.xfer in several
1904 * places. So, do not allow CDBs with a very large ALLOCATION
1905 * LENGTH. The real fix would be to modify scsi_read_data and
1906 * dma_buf_read, so that they return data beyond the buflen
1909 if (req
->cmd
.xfer
> 65536) {
1910 goto illegal_request
;
1912 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1914 if (!r
->iov
.iov_base
) {
1915 r
->iov
.iov_base
= blk_blockalign(s
->qdev
.conf
.blk
, r
->buflen
);
1918 outbuf
= r
->iov
.iov_base
;
1919 memset(outbuf
, 0, r
->buflen
);
1920 switch (req
->cmd
.buf
[0]) {
1921 case TEST_UNIT_READY
:
1922 assert(blk_is_available(s
->qdev
.conf
.blk
));
1925 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1927 goto illegal_request
;
1932 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1934 goto illegal_request
;
1938 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1940 goto illegal_request
;
1944 if (req
->cmd
.buf
[1] & 1) {
1945 goto illegal_request
;
1949 if (req
->cmd
.buf
[1] & 3) {
1950 goto illegal_request
;
1954 if (req
->cmd
.buf
[1] & 1) {
1955 goto illegal_request
;
1959 if (req
->cmd
.buf
[1] & 3) {
1960 goto illegal_request
;
1964 if (scsi_disk_emulate_start_stop(r
) < 0) {
1968 case ALLOW_MEDIUM_REMOVAL
:
1969 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1970 blk_lock_medium(s
->qdev
.conf
.blk
, req
->cmd
.buf
[4] & 1);
1972 case READ_CAPACITY_10
:
1973 /* The normal LEN field for this command is zero. */
1974 memset(outbuf
, 0, 8);
1975 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
1977 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1980 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1981 goto illegal_request
;
1983 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1984 /* Returned value is the address of the last sector. */
1986 /* Remember the new size for read/write sanity checking. */
1987 s
->qdev
.max_lba
= nb_sectors
;
1988 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1989 if (nb_sectors
> UINT32_MAX
) {
1990 nb_sectors
= UINT32_MAX
;
1992 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1993 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1994 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1995 outbuf
[3] = nb_sectors
& 0xff;
1998 outbuf
[6] = s
->qdev
.blocksize
>> 8;
2002 /* Just return "NO SENSE". */
2003 buflen
= scsi_convert_sense(NULL
, 0, outbuf
, r
->buflen
,
2004 (req
->cmd
.buf
[1] & 1) == 0);
2006 goto illegal_request
;
2009 case MECHANISM_STATUS
:
2010 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
2012 goto illegal_request
;
2015 case GET_CONFIGURATION
:
2016 buflen
= scsi_get_configuration(s
, outbuf
);
2018 goto illegal_request
;
2021 case GET_EVENT_STATUS_NOTIFICATION
:
2022 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
2024 goto illegal_request
;
2027 case READ_DISC_INFORMATION
:
2028 buflen
= scsi_read_disc_information(s
, r
, outbuf
);
2030 goto illegal_request
;
2033 case READ_DVD_STRUCTURE
:
2034 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
2036 goto illegal_request
;
2039 case SERVICE_ACTION_IN_16
:
2040 /* Service Action In subcommands. */
2041 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
2042 trace_scsi_disk_emulate_command_SAI_16();
2043 memset(outbuf
, 0, req
->cmd
.xfer
);
2044 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2046 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
2049 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
2050 goto illegal_request
;
2052 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2053 /* Returned value is the address of the last sector. */
2055 /* Remember the new size for read/write sanity checking. */
2056 s
->qdev
.max_lba
= nb_sectors
;
2057 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
2058 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
2059 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
2060 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
2061 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
2062 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
2063 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
2064 outbuf
[7] = nb_sectors
& 0xff;
2067 outbuf
[10] = s
->qdev
.blocksize
>> 8;
2070 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
2072 /* set TPE bit if the format supports discard */
2073 if (s
->qdev
.conf
.discard_granularity
) {
2077 /* Protection, exponent and lowest lba field left blank. */
2080 trace_scsi_disk_emulate_command_SAI_unsupported();
2081 goto illegal_request
;
2082 case SYNCHRONIZE_CACHE
:
2083 /* The request is used as the AIO opaque value, so add a ref. */
2084 scsi_req_ref(&r
->req
);
2085 block_acct_start(blk_get_stats(s
->qdev
.conf
.blk
), &r
->acct
, 0,
2087 r
->req
.aiocb
= blk_aio_flush(s
->qdev
.conf
.blk
, scsi_aio_complete
, r
);
2090 trace_scsi_disk_emulate_command_SEEK_10(r
->req
.cmd
.lba
);
2091 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
2096 trace_scsi_disk_emulate_command_MODE_SELECT(r
->req
.cmd
.xfer
);
2098 case MODE_SELECT_10
:
2099 trace_scsi_disk_emulate_command_MODE_SELECT_10(r
->req
.cmd
.xfer
);
2102 trace_scsi_disk_emulate_command_UNMAP(r
->req
.cmd
.xfer
);
2107 trace_scsi_disk_emulate_command_VERIFY((req
->cmd
.buf
[1] >> 1) & 3);
2108 if (req
->cmd
.buf
[1] & 6) {
2109 goto illegal_request
;
2114 trace_scsi_disk_emulate_command_WRITE_SAME(
2115 req
->cmd
.buf
[0] == WRITE_SAME_10
? 10 : 16, r
->req
.cmd
.xfer
);
2118 trace_scsi_disk_emulate_command_UNKNOWN(buf
[0],
2119 scsi_command_name(buf
[0]));
2120 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
2123 assert(!r
->req
.aiocb
);
2124 r
->iov
.iov_len
= MIN(r
->buflen
, req
->cmd
.xfer
);
2125 if (r
->iov
.iov_len
== 0) {
2126 scsi_req_complete(&r
->req
, GOOD
);
2128 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2129 assert(r
->iov
.iov_len
== req
->cmd
.xfer
);
2130 return -r
->iov
.iov_len
;
2132 return r
->iov
.iov_len
;
2136 if (r
->req
.status
== -1) {
2137 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2142 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2146 /* Execute a scsi command. Returns the length of the data expected by the
2147 command. This will be Positive for data transfers from the device
2148 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2149 and zero if the command does not transfer any data. */
2151 static int32_t scsi_disk_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2153 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
2154 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
2155 SCSIDiskClass
*sdc
= (SCSIDiskClass
*) object_get_class(OBJECT(s
));
2161 if (!blk_is_available(s
->qdev
.conf
.blk
)) {
2162 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
2166 len
= scsi_data_cdb_xfer(r
->req
.cmd
.buf
);
2172 trace_scsi_disk_dma_command_READ(r
->req
.cmd
.lba
, len
);
2173 /* Protection information is not supported. For SCSI versions 2 and
2174 * older (as determined by snooping the guest's INQUIRY commands),
2175 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2177 if (s
->qdev
.scsi_version
> 2 && (r
->req
.cmd
.buf
[1] & 0xe0)) {
2178 goto illegal_request
;
2180 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2183 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2184 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2190 case WRITE_VERIFY_10
:
2191 case WRITE_VERIFY_12
:
2192 case WRITE_VERIFY_16
:
2193 if (blk_is_read_only(s
->qdev
.conf
.blk
)) {
2194 scsi_check_condition(r
, SENSE_CODE(WRITE_PROTECTED
));
2197 trace_scsi_disk_dma_command_WRITE(
2198 (command
& 0xe) == 0xe ? "And Verify " : "",
2199 r
->req
.cmd
.lba
, len
);
2204 /* We get here only for BYTCHK == 0x01 and only for scsi-block.
2205 * As far as DMA is concerned, we can treat it the same as a write;
2206 * scsi_block_do_sgio will send VERIFY commands.
2208 if (s
->qdev
.scsi_version
> 2 && (r
->req
.cmd
.buf
[1] & 0xe0)) {
2209 goto illegal_request
;
2211 if (!check_lba_range(s
, r
->req
.cmd
.lba
, len
)) {
2214 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
2215 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
2220 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
2223 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
2226 r
->need_fua_emulation
= sdc
->need_fua_emulation(&r
->req
.cmd
);
2227 if (r
->sector_count
== 0) {
2228 scsi_req_complete(&r
->req
, GOOD
);
2230 assert(r
->iov
.iov_len
== 0);
2231 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
2232 return -r
->sector_count
* 512;
2234 return r
->sector_count
* 512;
2238 static void scsi_disk_reset(DeviceState
*dev
)
2240 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
2241 uint64_t nb_sectors
;
2243 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
2245 blk_get_geometry(s
->qdev
.conf
.blk
, &nb_sectors
);
2246 nb_sectors
/= s
->qdev
.blocksize
/ 512;
2250 s
->qdev
.max_lba
= nb_sectors
;
2251 /* reset tray statuses */
2255 s
->qdev
.scsi_version
= s
->qdev
.default_scsi_version
;
2258 static void scsi_disk_resize_cb(void *opaque
)
2260 SCSIDiskState
*s
= opaque
;
2262 /* SPC lists this sense code as available only for
2263 * direct-access devices.
2265 if (s
->qdev
.type
== TYPE_DISK
) {
2266 scsi_device_report_change(&s
->qdev
, SENSE_CODE(CAPACITY_CHANGED
));
2270 static void scsi_cd_change_media_cb(void *opaque
, bool load
, Error
**errp
)
2272 SCSIDiskState
*s
= opaque
;
2275 * When a CD gets changed, we have to report an ejected state and
2276 * then a loaded state to guests so that they detect tray
2277 * open/close and media change events. Guests that do not use
2278 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2279 * states rely on this behavior.
2281 * media_changed governs the state machine used for unit attention
2282 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2284 s
->media_changed
= load
;
2285 s
->tray_open
= !load
;
2286 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
));
2287 s
->media_event
= true;
2288 s
->eject_request
= false;
2291 static void scsi_cd_eject_request_cb(void *opaque
, bool force
)
2293 SCSIDiskState
*s
= opaque
;
2295 s
->eject_request
= true;
2297 s
->tray_locked
= false;
2301 static bool scsi_cd_is_tray_open(void *opaque
)
2303 return ((SCSIDiskState
*)opaque
)->tray_open
;
2306 static bool scsi_cd_is_medium_locked(void *opaque
)
2308 return ((SCSIDiskState
*)opaque
)->tray_locked
;
2311 static const BlockDevOps scsi_disk_removable_block_ops
= {
2312 .change_media_cb
= scsi_cd_change_media_cb
,
2313 .eject_request_cb
= scsi_cd_eject_request_cb
,
2314 .is_tray_open
= scsi_cd_is_tray_open
,
2315 .is_medium_locked
= scsi_cd_is_medium_locked
,
2317 .resize_cb
= scsi_disk_resize_cb
,
2320 static const BlockDevOps scsi_disk_block_ops
= {
2321 .resize_cb
= scsi_disk_resize_cb
,
2324 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
2326 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2327 if (s
->media_changed
) {
2328 s
->media_changed
= false;
2329 scsi_device_set_ua(&s
->qdev
, SENSE_CODE(MEDIUM_CHANGED
));
2333 static void scsi_realize(SCSIDevice
*dev
, Error
**errp
)
2335 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2338 if (!s
->qdev
.conf
.blk
) {
2339 error_setg(errp
, "drive property not set");
2343 if (!(s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2344 !blk_is_inserted(s
->qdev
.conf
.blk
)) {
2345 error_setg(errp
, "Device needs media, but drive is empty");
2349 blkconf_blocksizes(&s
->qdev
.conf
);
2351 if (s
->qdev
.conf
.logical_block_size
>
2352 s
->qdev
.conf
.physical_block_size
) {
2354 "logical_block_size > physical_block_size not supported");
2358 if (blk_get_aio_context(s
->qdev
.conf
.blk
) != qemu_get_aio_context() &&
2359 !s
->qdev
.hba_supports_iothread
)
2361 error_setg(errp
, "HBA does not support iothreads");
2365 if (dev
->type
== TYPE_DISK
) {
2366 if (!blkconf_geometry(&dev
->conf
, NULL
, 65535, 255, 255, errp
)) {
2371 read_only
= blk_is_read_only(s
->qdev
.conf
.blk
);
2372 if (dev
->type
== TYPE_ROM
) {
2376 if (!blkconf_apply_backend_options(&dev
->conf
, read_only
,
2377 dev
->type
== TYPE_DISK
, errp
)) {
2381 if (s
->qdev
.conf
.discard_granularity
== -1) {
2382 s
->qdev
.conf
.discard_granularity
=
2383 MAX(s
->qdev
.conf
.logical_block_size
, DEFAULT_DISCARD_GRANULARITY
);
2387 s
->version
= g_strdup(qemu_hw_version());
2390 s
->vendor
= g_strdup("QEMU");
2392 if (!s
->device_id
) {
2394 s
->device_id
= g_strdup_printf("%.20s", s
->serial
);
2396 const char *str
= blk_name(s
->qdev
.conf
.blk
);
2398 s
->device_id
= g_strdup(str
);
2403 if (blk_is_sg(s
->qdev
.conf
.blk
)) {
2404 error_setg(errp
, "unwanted /dev/sg*");
2408 if ((s
->features
& (1 << SCSI_DISK_F_REMOVABLE
)) &&
2409 !(s
->features
& (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
))) {
2410 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_removable_block_ops
, s
);
2412 blk_set_dev_ops(s
->qdev
.conf
.blk
, &scsi_disk_block_ops
, s
);
2414 blk_set_guest_block_size(s
->qdev
.conf
.blk
, s
->qdev
.blocksize
);
2416 blk_iostatus_enable(s
->qdev
.conf
.blk
);
2418 add_boot_device_lchs(&dev
->qdev
, NULL
,
2424 static void scsi_unrealize(SCSIDevice
*dev
, Error
**errp
)
2426 del_boot_device_lchs(&dev
->qdev
, NULL
);
2429 static void scsi_hd_realize(SCSIDevice
*dev
, Error
**errp
)
2431 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2432 AioContext
*ctx
= NULL
;
2433 /* can happen for devices without drive. The error message for missing
2434 * backend will be issued in scsi_realize
2436 if (s
->qdev
.conf
.blk
) {
2437 ctx
= blk_get_aio_context(s
->qdev
.conf
.blk
);
2438 aio_context_acquire(ctx
);
2439 blkconf_blocksizes(&s
->qdev
.conf
);
2441 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
2442 s
->qdev
.type
= TYPE_DISK
;
2444 s
->product
= g_strdup("QEMU HARDDISK");
2446 scsi_realize(&s
->qdev
, errp
);
2448 aio_context_release(ctx
);
2452 static void scsi_cd_realize(SCSIDevice
*dev
, Error
**errp
)
2454 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2458 if (!dev
->conf
.blk
) {
2459 /* Anonymous BlockBackend for an empty drive. As we put it into
2460 * dev->conf, qdev takes care of detaching on unplug. */
2461 dev
->conf
.blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
2462 ret
= blk_attach_dev(dev
->conf
.blk
, &dev
->qdev
);
2466 ctx
= blk_get_aio_context(dev
->conf
.blk
);
2467 aio_context_acquire(ctx
);
2468 s
->qdev
.blocksize
= 2048;
2469 s
->qdev
.type
= TYPE_ROM
;
2470 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2472 s
->product
= g_strdup("QEMU CD-ROM");
2474 scsi_realize(&s
->qdev
, errp
);
2475 aio_context_release(ctx
);
2478 static void scsi_disk_realize(SCSIDevice
*dev
, Error
**errp
)
2481 Error
*local_err
= NULL
;
2483 warn_report("'scsi-disk' is deprecated, "
2484 "please use 'scsi-hd' or 'scsi-cd' instead");
2486 if (!dev
->conf
.blk
) {
2487 scsi_realize(dev
, &local_err
);
2489 error_propagate(errp
, local_err
);
2493 dinfo
= blk_legacy_dinfo(dev
->conf
.blk
);
2494 if (dinfo
&& dinfo
->media_cd
) {
2495 scsi_cd_realize(dev
, errp
);
2497 scsi_hd_realize(dev
, errp
);
2501 static const SCSIReqOps scsi_disk_emulate_reqops
= {
2502 .size
= sizeof(SCSIDiskReq
),
2503 .free_req
= scsi_free_request
,
2504 .send_command
= scsi_disk_emulate_command
,
2505 .read_data
= scsi_disk_emulate_read_data
,
2506 .write_data
= scsi_disk_emulate_write_data
,
2507 .get_buf
= scsi_get_buf
,
2510 static const SCSIReqOps scsi_disk_dma_reqops
= {
2511 .size
= sizeof(SCSIDiskReq
),
2512 .free_req
= scsi_free_request
,
2513 .send_command
= scsi_disk_dma_command
,
2514 .read_data
= scsi_read_data
,
2515 .write_data
= scsi_write_data
,
2516 .get_buf
= scsi_get_buf
,
2517 .load_request
= scsi_disk_load_request
,
2518 .save_request
= scsi_disk_save_request
,
2521 static const SCSIReqOps
*const scsi_disk_reqops_dispatch
[256] = {
2522 [TEST_UNIT_READY
] = &scsi_disk_emulate_reqops
,
2523 [INQUIRY
] = &scsi_disk_emulate_reqops
,
2524 [MODE_SENSE
] = &scsi_disk_emulate_reqops
,
2525 [MODE_SENSE_10
] = &scsi_disk_emulate_reqops
,
2526 [START_STOP
] = &scsi_disk_emulate_reqops
,
2527 [ALLOW_MEDIUM_REMOVAL
] = &scsi_disk_emulate_reqops
,
2528 [READ_CAPACITY_10
] = &scsi_disk_emulate_reqops
,
2529 [READ_TOC
] = &scsi_disk_emulate_reqops
,
2530 [READ_DVD_STRUCTURE
] = &scsi_disk_emulate_reqops
,
2531 [READ_DISC_INFORMATION
] = &scsi_disk_emulate_reqops
,
2532 [GET_CONFIGURATION
] = &scsi_disk_emulate_reqops
,
2533 [GET_EVENT_STATUS_NOTIFICATION
] = &scsi_disk_emulate_reqops
,
2534 [MECHANISM_STATUS
] = &scsi_disk_emulate_reqops
,
2535 [SERVICE_ACTION_IN_16
] = &scsi_disk_emulate_reqops
,
2536 [REQUEST_SENSE
] = &scsi_disk_emulate_reqops
,
2537 [SYNCHRONIZE_CACHE
] = &scsi_disk_emulate_reqops
,
2538 [SEEK_10
] = &scsi_disk_emulate_reqops
,
2539 [MODE_SELECT
] = &scsi_disk_emulate_reqops
,
2540 [MODE_SELECT_10
] = &scsi_disk_emulate_reqops
,
2541 [UNMAP
] = &scsi_disk_emulate_reqops
,
2542 [WRITE_SAME_10
] = &scsi_disk_emulate_reqops
,
2543 [WRITE_SAME_16
] = &scsi_disk_emulate_reqops
,
2544 [VERIFY_10
] = &scsi_disk_emulate_reqops
,
2545 [VERIFY_12
] = &scsi_disk_emulate_reqops
,
2546 [VERIFY_16
] = &scsi_disk_emulate_reqops
,
2548 [READ_6
] = &scsi_disk_dma_reqops
,
2549 [READ_10
] = &scsi_disk_dma_reqops
,
2550 [READ_12
] = &scsi_disk_dma_reqops
,
2551 [READ_16
] = &scsi_disk_dma_reqops
,
2552 [WRITE_6
] = &scsi_disk_dma_reqops
,
2553 [WRITE_10
] = &scsi_disk_dma_reqops
,
2554 [WRITE_12
] = &scsi_disk_dma_reqops
,
2555 [WRITE_16
] = &scsi_disk_dma_reqops
,
2556 [WRITE_VERIFY_10
] = &scsi_disk_dma_reqops
,
2557 [WRITE_VERIFY_12
] = &scsi_disk_dma_reqops
,
2558 [WRITE_VERIFY_16
] = &scsi_disk_dma_reqops
,
2561 static void scsi_disk_new_request_dump(uint32_t lun
, uint32_t tag
, uint8_t *buf
)
2564 int len
= scsi_cdb_length(buf
);
2565 char *line_buffer
, *p
;
2567 line_buffer
= g_malloc(len
* 5 + 1);
2569 for (i
= 0, p
= line_buffer
; i
< len
; i
++) {
2570 p
+= sprintf(p
, " 0x%02x", buf
[i
]);
2572 trace_scsi_disk_new_request(lun
, tag
, line_buffer
);
2574 g_free(line_buffer
);
2577 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
2578 uint8_t *buf
, void *hba_private
)
2580 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2582 const SCSIReqOps
*ops
;
2586 ops
= scsi_disk_reqops_dispatch
[command
];
2588 ops
= &scsi_disk_emulate_reqops
;
2590 req
= scsi_req_alloc(ops
, &s
->qdev
, tag
, lun
, hba_private
);
2592 if (trace_event_get_state_backends(TRACE_SCSI_DISK_NEW_REQUEST
)) {
2593 scsi_disk_new_request_dump(lun
, tag
, buf
);
2600 static int get_device_type(SCSIDiskState
*s
)
2606 memset(cmd
, 0, sizeof(cmd
));
2607 memset(buf
, 0, sizeof(buf
));
2609 cmd
[4] = sizeof(buf
);
2611 ret
= scsi_SG_IO_FROM_DEV(s
->qdev
.conf
.blk
, cmd
, sizeof(cmd
),
2616 s
->qdev
.type
= buf
[0];
2617 if (buf
[1] & 0x80) {
2618 s
->features
|= 1 << SCSI_DISK_F_REMOVABLE
;
2623 static void scsi_block_realize(SCSIDevice
*dev
, Error
**errp
)
2625 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
2630 if (!s
->qdev
.conf
.blk
) {
2631 error_setg(errp
, "drive property not set");
2635 if (s
->rotation_rate
) {
2636 error_report_once("rotation_rate is specified for scsi-block but is "
2637 "not implemented. This option is deprecated and will "
2638 "be removed in a future version");
2641 ctx
= blk_get_aio_context(s
->qdev
.conf
.blk
);
2642 aio_context_acquire(ctx
);
2644 /* check we are using a driver managing SG_IO (version 3 and after) */
2645 rc
= blk_ioctl(s
->qdev
.conf
.blk
, SG_GET_VERSION_NUM
, &sg_version
);
2647 error_setg_errno(errp
, -rc
, "cannot get SG_IO version number");
2649 error_append_hint(errp
, "Is this a SCSI device?\n");
2653 if (sg_version
< 30000) {
2654 error_setg(errp
, "scsi generic interface too old");
2658 /* get device type from INQUIRY data */
2659 rc
= get_device_type(s
);
2661 error_setg(errp
, "INQUIRY failed");
2665 /* Make a guess for the block size, we'll fix it when the guest sends.
2666 * READ CAPACITY. If they don't, they likely would assume these sizes
2667 * anyway. (TODO: check in /sys).
2669 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
2670 s
->qdev
.blocksize
= 2048;
2672 s
->qdev
.blocksize
= 512;
2675 /* Makes the scsi-block device not removable by using HMP and QMP eject
2678 s
->features
|= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS
);
2680 scsi_realize(&s
->qdev
, errp
);
2681 scsi_generic_read_device_inquiry(&s
->qdev
);
2684 aio_context_release(ctx
);
2687 typedef struct SCSIBlockReq
{
2689 sg_io_hdr_t io_header
;
2691 /* Selected bytes of the original CDB, copied into our own CDB. */
2692 uint8_t cmd
, cdb1
, group_number
;
2694 /* CDB passed to SG_IO. */
2698 static BlockAIOCB
*scsi_block_do_sgio(SCSIBlockReq
*req
,
2699 int64_t offset
, QEMUIOVector
*iov
,
2701 BlockCompletionFunc
*cb
, void *opaque
)
2703 sg_io_hdr_t
*io_header
= &req
->io_header
;
2704 SCSIDiskReq
*r
= &req
->req
;
2705 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2706 int nb_logical_blocks
;
2710 /* This is not supported yet. It can only happen if the guest does
2711 * reads and writes that are not aligned to one logical sectors
2712 * _and_ cover multiple MemoryRegions.
2714 assert(offset
% s
->qdev
.blocksize
== 0);
2715 assert(iov
->size
% s
->qdev
.blocksize
== 0);
2717 io_header
->interface_id
= 'S';
2719 /* The data transfer comes from the QEMUIOVector. */
2720 io_header
->dxfer_direction
= direction
;
2721 io_header
->dxfer_len
= iov
->size
;
2722 io_header
->dxferp
= (void *)iov
->iov
;
2723 io_header
->iovec_count
= iov
->niov
;
2724 assert(io_header
->iovec_count
== iov
->niov
); /* no overflow! */
2726 /* Build a new CDB with the LBA and length patched in, in case
2727 * DMA helpers split the transfer in multiple segments. Do not
2728 * build a CDB smaller than what the guest wanted, and only build
2729 * a larger one if strictly necessary.
2731 io_header
->cmdp
= req
->cdb
;
2732 lba
= offset
/ s
->qdev
.blocksize
;
2733 nb_logical_blocks
= io_header
->dxfer_len
/ s
->qdev
.blocksize
;
2735 if ((req
->cmd
>> 5) == 0 && lba
<= 0x1ffff) {
2737 stl_be_p(&req
->cdb
[0], lba
| (req
->cmd
<< 24));
2738 req
->cdb
[4] = nb_logical_blocks
;
2740 io_header
->cmd_len
= 6;
2741 } else if ((req
->cmd
>> 5) <= 1 && lba
<= 0xffffffffULL
) {
2743 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0x20;
2744 req
->cdb
[1] = req
->cdb1
;
2745 stl_be_p(&req
->cdb
[2], lba
);
2746 req
->cdb
[6] = req
->group_number
;
2747 stw_be_p(&req
->cdb
[7], nb_logical_blocks
);
2749 io_header
->cmd_len
= 10;
2750 } else if ((req
->cmd
>> 5) != 4 && lba
<= 0xffffffffULL
) {
2752 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0xA0;
2753 req
->cdb
[1] = req
->cdb1
;
2754 stl_be_p(&req
->cdb
[2], lba
);
2755 stl_be_p(&req
->cdb
[6], nb_logical_blocks
);
2756 req
->cdb
[10] = req
->group_number
;
2758 io_header
->cmd_len
= 12;
2761 req
->cdb
[0] = (req
->cmd
& 0x1f) | 0x80;
2762 req
->cdb
[1] = req
->cdb1
;
2763 stq_be_p(&req
->cdb
[2], lba
);
2764 stl_be_p(&req
->cdb
[10], nb_logical_blocks
);
2765 req
->cdb
[14] = req
->group_number
;
2767 io_header
->cmd_len
= 16;
2770 /* The rest is as in scsi-generic.c. */
2771 io_header
->mx_sb_len
= sizeof(r
->req
.sense
);
2772 io_header
->sbp
= r
->req
.sense
;
2773 io_header
->timeout
= UINT_MAX
;
2774 io_header
->usr_ptr
= r
;
2775 io_header
->flags
|= SG_FLAG_DIRECT_IO
;
2777 aiocb
= blk_aio_ioctl(s
->qdev
.conf
.blk
, SG_IO
, io_header
, cb
, opaque
);
2778 assert(aiocb
!= NULL
);
2782 static bool scsi_block_no_fua(SCSICommand
*cmd
)
2787 static BlockAIOCB
*scsi_block_dma_readv(int64_t offset
,
2789 BlockCompletionFunc
*cb
, void *cb_opaque
,
2792 SCSIBlockReq
*r
= opaque
;
2793 return scsi_block_do_sgio(r
, offset
, iov
,
2794 SG_DXFER_FROM_DEV
, cb
, cb_opaque
);
2797 static BlockAIOCB
*scsi_block_dma_writev(int64_t offset
,
2799 BlockCompletionFunc
*cb
, void *cb_opaque
,
2802 SCSIBlockReq
*r
= opaque
;
2803 return scsi_block_do_sgio(r
, offset
, iov
,
2804 SG_DXFER_TO_DEV
, cb
, cb_opaque
);
2807 static bool scsi_block_is_passthrough(SCSIDiskState
*s
, uint8_t *buf
)
2813 /* Check if BYTCHK == 0x01 (data-out buffer contains data
2814 * for the number of logical blocks specified in the length
2815 * field). For other modes, do not use scatter/gather operation.
2817 if ((buf
[1] & 6) == 2) {
2830 case WRITE_VERIFY_10
:
2831 case WRITE_VERIFY_12
:
2832 case WRITE_VERIFY_16
:
2833 /* MMC writing cannot be done via DMA helpers, because it sometimes
2834 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2835 * We might use scsi_block_dma_reqops as long as no writing commands are
2836 * seen, but performance usually isn't paramount on optical media. So,
2837 * just make scsi-block operate the same as scsi-generic for them.
2839 if (s
->qdev
.type
!= TYPE_ROM
) {
2852 static int32_t scsi_block_dma_command(SCSIRequest
*req
, uint8_t *buf
)
2854 SCSIBlockReq
*r
= (SCSIBlockReq
*)req
;
2855 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
2857 r
->cmd
= req
->cmd
.buf
[0];
2858 switch (r
->cmd
>> 5) {
2861 r
->cdb1
= r
->group_number
= 0;
2865 r
->cdb1
= req
->cmd
.buf
[1];
2866 r
->group_number
= req
->cmd
.buf
[6];
2870 r
->cdb1
= req
->cmd
.buf
[1];
2871 r
->group_number
= req
->cmd
.buf
[10];
2875 r
->cdb1
= req
->cmd
.buf
[1];
2876 r
->group_number
= req
->cmd
.buf
[14];
2882 /* Protection information is not supported. For SCSI versions 2 and
2883 * older (as determined by snooping the guest's INQUIRY commands),
2884 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2886 if (s
->qdev
.scsi_version
> 2 && (req
->cmd
.buf
[1] & 0xe0)) {
2887 scsi_check_condition(&r
->req
, SENSE_CODE(INVALID_FIELD
));
2891 r
->req
.status
= &r
->io_header
.status
;
2892 return scsi_disk_dma_command(req
, buf
);
2895 static const SCSIReqOps scsi_block_dma_reqops
= {
2896 .size
= sizeof(SCSIBlockReq
),
2897 .free_req
= scsi_free_request
,
2898 .send_command
= scsi_block_dma_command
,
2899 .read_data
= scsi_read_data
,
2900 .write_data
= scsi_write_data
,
2901 .get_buf
= scsi_get_buf
,
2902 .load_request
= scsi_disk_load_request
,
2903 .save_request
= scsi_disk_save_request
,
2906 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
2907 uint32_t lun
, uint8_t *buf
,
2910 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2912 if (scsi_block_is_passthrough(s
, buf
)) {
2913 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
2916 return scsi_req_alloc(&scsi_block_dma_reqops
, &s
->qdev
, tag
, lun
,
2921 static int scsi_block_parse_cdb(SCSIDevice
*d
, SCSICommand
*cmd
,
2922 uint8_t *buf
, void *hba_private
)
2924 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
2926 if (scsi_block_is_passthrough(s
, buf
)) {
2927 return scsi_bus_parse_cdb(&s
->qdev
, cmd
, buf
, hba_private
);
2929 return scsi_req_parse_cdb(&s
->qdev
, cmd
, buf
);
2933 static void scsi_block_update_sense(SCSIRequest
*req
)
2935 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
2936 SCSIBlockReq
*br
= DO_UPCAST(SCSIBlockReq
, req
, r
);
2937 r
->req
.sense_len
= MIN(br
->io_header
.sb_len_wr
, sizeof(r
->req
.sense
));
2942 BlockAIOCB
*scsi_dma_readv(int64_t offset
, QEMUIOVector
*iov
,
2943 BlockCompletionFunc
*cb
, void *cb_opaque
,
2946 SCSIDiskReq
*r
= opaque
;
2947 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2948 return blk_aio_preadv(s
->qdev
.conf
.blk
, offset
, iov
, 0, cb
, cb_opaque
);
2952 BlockAIOCB
*scsi_dma_writev(int64_t offset
, QEMUIOVector
*iov
,
2953 BlockCompletionFunc
*cb
, void *cb_opaque
,
2956 SCSIDiskReq
*r
= opaque
;
2957 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
2958 return blk_aio_pwritev(s
->qdev
.conf
.blk
, offset
, iov
, 0, cb
, cb_opaque
);
2961 static void scsi_disk_base_class_initfn(ObjectClass
*klass
, void *data
)
2963 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2964 SCSIDiskClass
*sdc
= SCSI_DISK_BASE_CLASS(klass
);
2966 dc
->fw_name
= "disk";
2967 dc
->reset
= scsi_disk_reset
;
2968 sdc
->dma_readv
= scsi_dma_readv
;
2969 sdc
->dma_writev
= scsi_dma_writev
;
2970 sdc
->need_fua_emulation
= scsi_is_cmd_fua
;
2973 static const TypeInfo scsi_disk_base_info
= {
2974 .name
= TYPE_SCSI_DISK_BASE
,
2975 .parent
= TYPE_SCSI_DEVICE
,
2976 .class_init
= scsi_disk_base_class_initfn
,
2977 .instance_size
= sizeof(SCSIDiskState
),
2978 .class_size
= sizeof(SCSIDiskClass
),
2982 #define DEFINE_SCSI_DISK_PROPERTIES() \
2983 DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), \
2984 DEFINE_BLOCK_PROPERTIES_BASE(SCSIDiskState, qdev.conf), \
2985 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \
2986 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2987 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2988 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2989 DEFINE_PROP_STRING("product", SCSIDiskState, product), \
2990 DEFINE_PROP_STRING("device_id", SCSIDiskState, device_id)
2993 static Property scsi_hd_properties
[] = {
2994 DEFINE_SCSI_DISK_PROPERTIES(),
2995 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
2996 SCSI_DISK_F_REMOVABLE
, false),
2997 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
2998 SCSI_DISK_F_DPOFUA
, false),
2999 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
3000 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
3001 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
3002 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
3003 DEFAULT_MAX_UNMAP_SIZE
),
3004 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
3005 DEFAULT_MAX_IO_SIZE
),
3006 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState
, rotation_rate
, 0),
3007 DEFINE_PROP_INT32("scsi_version", SCSIDiskState
, qdev
.default_scsi_version
,
3009 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState
, qdev
.conf
),
3010 DEFINE_PROP_END_OF_LIST(),
3013 static const VMStateDescription vmstate_scsi_disk_state
= {
3014 .name
= "scsi-disk",
3016 .minimum_version_id
= 1,
3017 .fields
= (VMStateField
[]) {
3018 VMSTATE_SCSI_DEVICE(qdev
, SCSIDiskState
),
3019 VMSTATE_BOOL(media_changed
, SCSIDiskState
),
3020 VMSTATE_BOOL(media_event
, SCSIDiskState
),
3021 VMSTATE_BOOL(eject_request
, SCSIDiskState
),
3022 VMSTATE_BOOL(tray_open
, SCSIDiskState
),
3023 VMSTATE_BOOL(tray_locked
, SCSIDiskState
),
3024 VMSTATE_END_OF_LIST()
3028 static void scsi_hd_class_initfn(ObjectClass
*klass
, void *data
)
3030 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3031 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
3033 sc
->realize
= scsi_hd_realize
;
3034 sc
->unrealize
= scsi_unrealize
;
3035 sc
->alloc_req
= scsi_new_request
;
3036 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
3037 dc
->desc
= "virtual SCSI disk";
3038 device_class_set_props(dc
, scsi_hd_properties
);
3039 dc
->vmsd
= &vmstate_scsi_disk_state
;
3042 static const TypeInfo scsi_hd_info
= {
3044 .parent
= TYPE_SCSI_DISK_BASE
,
3045 .class_init
= scsi_hd_class_initfn
,
3048 static Property scsi_cd_properties
[] = {
3049 DEFINE_SCSI_DISK_PROPERTIES(),
3050 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
3051 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
3052 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
3053 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
3054 DEFAULT_MAX_IO_SIZE
),
3055 DEFINE_PROP_INT32("scsi_version", SCSIDiskState
, qdev
.default_scsi_version
,
3057 DEFINE_PROP_END_OF_LIST(),
3060 static void scsi_cd_class_initfn(ObjectClass
*klass
, void *data
)
3062 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3063 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
3065 sc
->realize
= scsi_cd_realize
;
3066 sc
->alloc_req
= scsi_new_request
;
3067 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
3068 dc
->desc
= "virtual SCSI CD-ROM";
3069 device_class_set_props(dc
, scsi_cd_properties
);
3070 dc
->vmsd
= &vmstate_scsi_disk_state
;
3073 static const TypeInfo scsi_cd_info
= {
3075 .parent
= TYPE_SCSI_DISK_BASE
,
3076 .class_init
= scsi_cd_class_initfn
,
3080 static Property scsi_block_properties
[] = {
3081 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState
, qdev
.conf
), \
3082 DEFINE_PROP_DRIVE("drive", SCSIDiskState
, qdev
.conf
.blk
),
3083 DEFINE_PROP_BOOL("share-rw", SCSIDiskState
, qdev
.conf
.share_rw
, false),
3084 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState
, rotation_rate
, 0),
3085 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
3086 DEFAULT_MAX_UNMAP_SIZE
),
3087 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
3088 DEFAULT_MAX_IO_SIZE
),
3089 DEFINE_PROP_INT32("scsi_version", SCSIDiskState
, qdev
.default_scsi_version
,
3091 DEFINE_PROP_END_OF_LIST(),
3094 static void scsi_block_class_initfn(ObjectClass
*klass
, void *data
)
3096 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3097 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
3098 SCSIDiskClass
*sdc
= SCSI_DISK_BASE_CLASS(klass
);
3100 sc
->realize
= scsi_block_realize
;
3101 sc
->alloc_req
= scsi_block_new_request
;
3102 sc
->parse_cdb
= scsi_block_parse_cdb
;
3103 sdc
->dma_readv
= scsi_block_dma_readv
;
3104 sdc
->dma_writev
= scsi_block_dma_writev
;
3105 sdc
->update_sense
= scsi_block_update_sense
;
3106 sdc
->need_fua_emulation
= scsi_block_no_fua
;
3107 dc
->desc
= "SCSI block device passthrough";
3108 device_class_set_props(dc
, scsi_block_properties
);
3109 dc
->vmsd
= &vmstate_scsi_disk_state
;
3112 static const TypeInfo scsi_block_info
= {
3113 .name
= "scsi-block",
3114 .parent
= TYPE_SCSI_DISK_BASE
,
3115 .class_init
= scsi_block_class_initfn
,
3119 static Property scsi_disk_properties
[] = {
3120 DEFINE_SCSI_DISK_PROPERTIES(),
3121 DEFINE_PROP_BIT("removable", SCSIDiskState
, features
,
3122 SCSI_DISK_F_REMOVABLE
, false),
3123 DEFINE_PROP_BIT("dpofua", SCSIDiskState
, features
,
3124 SCSI_DISK_F_DPOFUA
, false),
3125 DEFINE_PROP_UINT64("wwn", SCSIDiskState
, qdev
.wwn
, 0),
3126 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState
, qdev
.port_wwn
, 0),
3127 DEFINE_PROP_UINT16("port_index", SCSIDiskState
, port_index
, 0),
3128 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState
, max_unmap_size
,
3129 DEFAULT_MAX_UNMAP_SIZE
),
3130 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState
, max_io_size
,
3131 DEFAULT_MAX_IO_SIZE
),
3132 DEFINE_PROP_INT32("scsi_version", SCSIDiskState
, qdev
.default_scsi_version
,
3134 DEFINE_PROP_END_OF_LIST(),
3137 static void scsi_disk_class_initfn(ObjectClass
*klass
, void *data
)
3139 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3140 SCSIDeviceClass
*sc
= SCSI_DEVICE_CLASS(klass
);
3142 sc
->realize
= scsi_disk_realize
;
3143 sc
->alloc_req
= scsi_new_request
;
3144 sc
->unit_attention_reported
= scsi_disk_unit_attention_reported
;
3145 dc
->fw_name
= "disk";
3146 dc
->desc
= "virtual SCSI disk or CD-ROM (legacy)";
3147 dc
->reset
= scsi_disk_reset
;
3148 device_class_set_props(dc
, scsi_disk_properties
);
3149 dc
->vmsd
= &vmstate_scsi_disk_state
;
3152 static const TypeInfo scsi_disk_info
= {
3153 .name
= "scsi-disk",
3154 .parent
= TYPE_SCSI_DISK_BASE
,
3155 .class_init
= scsi_disk_class_initfn
,
3158 static void scsi_disk_register_types(void)
3160 type_register_static(&scsi_disk_base_info
);
3161 type_register_static(&scsi_hd_info
);
3162 type_register_static(&scsi_cd_info
);
3164 type_register_static(&scsi_block_info
);
3166 type_register_static(&scsi_disk_info
);
3169 type_init(scsi_disk_register_types
)