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 "scsi/constants.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;
108 * 0x0000 - rotation rate not reported
109 * 0x0001 - non-rotating medium (SSD)
110 * 0x0002-0x0400 - reserved
111 * 0x0401-0xffe - rotations per minute
114 uint16_t rotation_rate;
117 static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed);
119 static void scsi_free_request(SCSIRequest *req)
121 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
123 qemu_vfree(r->iov.iov_base);
126 /* Helper function for command completion with sense. */
127 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
129 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
130 r->req.tag, sense.key, sense.asc, sense.ascq);
131 scsi_req_build_sense(&r->req, sense);
132 scsi_req_complete(&r->req, CHECK_CONDITION);
135 static void scsi_init_iovec(SCSIDiskReq *r, size_t size)
137 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
139 if (!r->iov.iov_base) {
141 r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen);
143 r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
144 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
147 static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req)
149 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
151 qemu_put_be64s(f, &r->sector);
152 qemu_put_be32s(f, &r->sector_count);
153 qemu_put_be32s(f, &r->buflen);
155 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
156 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
157 } else if (!req->retry) {
158 uint32_t len = r->iov.iov_len;
159 qemu_put_be32s(f, &len);
160 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
165 static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req)
167 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
169 qemu_get_be64s(f, &r->sector);
170 qemu_get_be32s(f, &r->sector_count);
171 qemu_get_be32s(f, &r->buflen);
173 scsi_init_iovec(r, r->buflen);
174 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
175 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
176 } else if (!r->req.retry) {
178 qemu_get_be32s(f, &len);
179 r->iov.iov_len = len;
180 assert(r->iov.iov_len <= r->buflen);
181 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
185 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
188 static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed)
190 if (r->req.io_canceled) {
191 scsi_req_cancel_complete(&r->req);
195 if (ret < 0 || (r->status && *r->status)) {
196 return scsi_handle_rw_error(r, -ret, acct_failed);
202 static void scsi_aio_complete(void *opaque, int ret)
204 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
205 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
207 assert(r->req.aiocb != NULL);
209 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
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 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
219 scsi_req_unref(&r->req);
222 static bool scsi_is_cmd_fua(SCSICommand *cmd)
224 switch (cmd->buf[0]) {
231 return (cmd->buf[1] & 8) != 0;
236 case WRITE_VERIFY_10:
237 case WRITE_VERIFY_12:
238 case WRITE_VERIFY_16:
248 static void scsi_write_do_fua(SCSIDiskReq *r)
250 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
252 assert(r->req.aiocb == NULL);
253 assert(!r->req.io_canceled);
255 if (r->need_fua_emulation) {
256 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
258 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r);
262 scsi_req_complete(&r->req, GOOD);
263 scsi_req_unref(&r->req);
266 static void scsi_dma_complete_noio(SCSIDiskReq *r, int ret)
268 assert(r->req.aiocb == NULL);
269 if (scsi_disk_req_check_error(r, ret, false)) {
273 r->sector += r->sector_count;
275 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
276 scsi_write_do_fua(r);
279 scsi_req_complete(&r->req, GOOD);
283 scsi_req_unref(&r->req);
286 static void scsi_dma_complete(void *opaque, int ret)
288 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
289 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
291 assert(r->req.aiocb != NULL);
294 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
296 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
298 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
300 scsi_dma_complete_noio(r, ret);
301 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
304 static void scsi_read_complete(void * opaque, int ret)
306 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
307 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
310 assert(r->req.aiocb != NULL);
312 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
313 if (scsi_disk_req_check_error(r, ret, true)) {
317 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
318 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size);
320 n = r->qiov.size / 512;
322 r->sector_count -= n;
323 scsi_req_data(&r->req, r->qiov.size);
326 scsi_req_unref(&r->req);
327 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
330 /* Actually issue a read to the block device. */
331 static void scsi_do_read(SCSIDiskReq *r, int ret)
333 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
334 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
336 assert (r->req.aiocb == NULL);
337 if (scsi_disk_req_check_error(r, ret, false)) {
341 /* The request is used as the AIO opaque value, so add a ref. */
342 scsi_req_ref(&r->req);
345 dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_READ);
346 r->req.resid -= r->req.sg->size;
347 r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
348 r->req.sg, r->sector << BDRV_SECTOR_BITS,
350 sdc->dma_readv, r, scsi_dma_complete, r,
351 DMA_DIRECTION_FROM_DEVICE);
353 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
354 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
355 r->qiov.size, BLOCK_ACCT_READ);
356 r->req.aiocb = sdc->dma_readv(r->sector << BDRV_SECTOR_BITS, &r->qiov,
357 scsi_read_complete, r, r);
361 scsi_req_unref(&r->req);
364 static void scsi_do_read_cb(void *opaque, int ret)
366 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
367 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
369 assert (r->req.aiocb != NULL);
372 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
374 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
376 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
378 scsi_do_read(opaque, ret);
379 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
382 /* Read more data from scsi device into buffer. */
383 static void scsi_read_data(SCSIRequest *req)
385 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
386 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
389 DPRINTF("Read sector_count=%d\n", r->sector_count);
390 if (r->sector_count == 0) {
391 /* This also clears the sense buffer for REQUEST SENSE. */
392 scsi_req_complete(&r->req, GOOD);
396 /* No data transfer may already be in progress */
397 assert(r->req.aiocb == NULL);
399 /* The request is used as the AIO opaque value, so add a ref. */
400 scsi_req_ref(&r->req);
401 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
402 DPRINTF("Data transfer direction invalid\n");
403 scsi_read_complete(r, -EINVAL);
407 if (!blk_is_available(req->dev->conf.blk)) {
408 scsi_read_complete(r, -ENOMEDIUM);
414 if (first && r->need_fua_emulation) {
415 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
417 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_do_read_cb, r);
424 * scsi_handle_rw_error has two return values. False means that the error
425 * must be ignored, true means that the error has been processed and the
426 * caller should not do anything else for this request. Note that
427 * scsi_handle_rw_error always manages its reference counts, independent
428 * of the return value.
430 static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed)
432 bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV);
433 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
434 BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk,
437 if (action == BLOCK_ERROR_ACTION_REPORT) {
439 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
443 /* The command has run, no need to fake sense. */
444 assert(r->status && *r->status);
445 scsi_req_complete(&r->req, *r->status);
448 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
451 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
454 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
457 scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED));
460 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
465 assert(r->status && *r->status);
466 error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense));
468 if (error == ECANCELED || error == EAGAIN || error == ENOTCONN ||
470 /* These errors are handled by guest. */
471 scsi_req_complete(&r->req, *r->status);
476 blk_error_action(s->qdev.conf.blk, action, is_read, error);
477 if (action == BLOCK_ERROR_ACTION_STOP) {
478 scsi_req_retry(&r->req);
480 return action != BLOCK_ERROR_ACTION_IGNORE;
483 static void scsi_write_complete_noio(SCSIDiskReq *r, int ret)
487 assert (r->req.aiocb == NULL);
488 if (scsi_disk_req_check_error(r, ret, false)) {
492 n = r->qiov.size / 512;
494 r->sector_count -= n;
495 if (r->sector_count == 0) {
496 scsi_write_do_fua(r);
499 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
500 DPRINTF("Write complete tag=0x%x more=%zd\n", r->req.tag, r->qiov.size);
501 scsi_req_data(&r->req, r->qiov.size);
505 scsi_req_unref(&r->req);
508 static void scsi_write_complete(void * opaque, int ret)
510 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
511 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
513 assert (r->req.aiocb != NULL);
516 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
518 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
520 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
522 scsi_write_complete_noio(r, ret);
523 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
526 static void scsi_write_data(SCSIRequest *req)
528 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
529 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
530 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
532 /* No data transfer may already be in progress */
533 assert(r->req.aiocb == NULL);
535 /* The request is used as the AIO opaque value, so add a ref. */
536 scsi_req_ref(&r->req);
537 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
538 DPRINTF("Data transfer direction invalid\n");
539 scsi_write_complete_noio(r, -EINVAL);
543 if (!r->req.sg && !r->qiov.size) {
544 /* Called for the first time. Ask the driver to send us more data. */
546 scsi_write_complete_noio(r, 0);
549 if (!blk_is_available(req->dev->conf.blk)) {
550 scsi_write_complete_noio(r, -ENOMEDIUM);
554 if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 ||
555 r->req.cmd.buf[0] == VERIFY_16) {
557 scsi_dma_complete_noio(r, 0);
559 scsi_write_complete_noio(r, 0);
565 dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_WRITE);
566 r->req.resid -= r->req.sg->size;
567 r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
568 r->req.sg, r->sector << BDRV_SECTOR_BITS,
570 sdc->dma_writev, r, scsi_dma_complete, r,
571 DMA_DIRECTION_TO_DEVICE);
573 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
574 r->qiov.size, BLOCK_ACCT_WRITE);
575 r->req.aiocb = sdc->dma_writev(r->sector << BDRV_SECTOR_BITS, &r->qiov,
576 scsi_write_complete, r, r);
580 /* Return a pointer to the data buffer. */
581 static uint8_t *scsi_get_buf(SCSIRequest *req)
583 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
585 return (uint8_t *)r->iov.iov_base;
588 int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf)
590 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
591 uint8_t page_code = req->cmd.buf[2];
592 int start, buflen = 0;
594 outbuf[buflen++] = s->qdev.type & 0x1f;
595 outbuf[buflen++] = page_code;
596 outbuf[buflen++] = 0x00;
597 outbuf[buflen++] = 0x00;
601 case 0x00: /* Supported page codes, mandatory */
603 DPRINTF("Inquiry EVPD[Supported pages] "
604 "buffer size %zd\n", req->cmd.xfer);
605 outbuf[buflen++] = 0x00; /* list of supported pages (this page) */
607 outbuf[buflen++] = 0x80; /* unit serial number */
609 outbuf[buflen++] = 0x83; /* device identification */
610 if (s->qdev.type == TYPE_DISK) {
611 outbuf[buflen++] = 0xb0; /* block limits */
612 outbuf[buflen++] = 0xb1; /* block device characteristics */
613 outbuf[buflen++] = 0xb2; /* thin provisioning */
617 case 0x80: /* Device serial number, optional */
622 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
626 l = strlen(s->serial);
631 DPRINTF("Inquiry EVPD[Serial number] "
632 "buffer size %zd\n", req->cmd.xfer);
633 memcpy(outbuf + buflen, s->serial, l);
638 case 0x83: /* Device identification page, mandatory */
640 const char *str = s->serial ?: blk_name(s->qdev.conf.blk);
641 int max_len = s->serial ? 20 : 255 - 8;
642 int id_len = strlen(str);
644 if (id_len > max_len) {
647 DPRINTF("Inquiry EVPD[Device identification] "
648 "buffer size %zd\n", req->cmd.xfer);
650 outbuf[buflen++] = 0x2; /* ASCII */
651 outbuf[buflen++] = 0; /* not officially assigned */
652 outbuf[buflen++] = 0; /* reserved */
653 outbuf[buflen++] = id_len; /* length of data following */
654 memcpy(outbuf + buflen, str, id_len);
658 outbuf[buflen++] = 0x1; /* Binary */
659 outbuf[buflen++] = 0x3; /* NAA */
660 outbuf[buflen++] = 0; /* reserved */
661 outbuf[buflen++] = 8;
662 stq_be_p(&outbuf[buflen], s->qdev.wwn);
666 if (s->qdev.port_wwn) {
667 outbuf[buflen++] = 0x61; /* SAS / Binary */
668 outbuf[buflen++] = 0x93; /* PIV / Target port / NAA */
669 outbuf[buflen++] = 0; /* reserved */
670 outbuf[buflen++] = 8;
671 stq_be_p(&outbuf[buflen], s->qdev.port_wwn);
676 outbuf[buflen++] = 0x61; /* SAS / Binary */
678 /* PIV/Target port/relative target port */
679 outbuf[buflen++] = 0x94;
681 outbuf[buflen++] = 0; /* reserved */
682 outbuf[buflen++] = 4;
683 stw_be_p(&outbuf[buflen + 2], s->port_index);
688 case 0xb0: /* block limits */
690 unsigned int unmap_sectors =
691 s->qdev.conf.discard_granularity / s->qdev.blocksize;
692 unsigned int min_io_size =
693 s->qdev.conf.min_io_size / s->qdev.blocksize;
694 unsigned int opt_io_size =
695 s->qdev.conf.opt_io_size / s->qdev.blocksize;
696 unsigned int max_unmap_sectors =
697 s->max_unmap_size / s->qdev.blocksize;
698 unsigned int max_io_sectors =
699 s->max_io_size / s->qdev.blocksize;
701 if (s->qdev.type == TYPE_ROM) {
702 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
706 if (s->qdev.type == TYPE_DISK) {
707 int max_transfer_blk = blk_get_max_transfer(s->qdev.conf.blk);
708 int max_io_sectors_blk =
709 max_transfer_blk / s->qdev.blocksize;
712 MIN_NON_ZERO(max_io_sectors_blk, max_io_sectors);
714 /* min_io_size and opt_io_size can't be greater than
717 min_io_size = MIN(min_io_size, max_io_sectors);
720 opt_io_size = MIN(opt_io_size, max_io_sectors);
723 /* required VPD size with unmap support */
725 memset(outbuf + 4, 0, buflen - 4);
727 outbuf[4] = 0x1; /* wsnz */
729 /* optimal transfer length granularity */
730 outbuf[6] = (min_io_size >> 8) & 0xff;
731 outbuf[7] = min_io_size & 0xff;
733 /* maximum transfer length */
734 outbuf[8] = (max_io_sectors >> 24) & 0xff;
735 outbuf[9] = (max_io_sectors >> 16) & 0xff;
736 outbuf[10] = (max_io_sectors >> 8) & 0xff;
737 outbuf[11] = max_io_sectors & 0xff;
739 /* optimal transfer length */
740 outbuf[12] = (opt_io_size >> 24) & 0xff;
741 outbuf[13] = (opt_io_size >> 16) & 0xff;
742 outbuf[14] = (opt_io_size >> 8) & 0xff;
743 outbuf[15] = opt_io_size & 0xff;
745 /* max unmap LBA count, default is 1GB */
746 outbuf[20] = (max_unmap_sectors >> 24) & 0xff;
747 outbuf[21] = (max_unmap_sectors >> 16) & 0xff;
748 outbuf[22] = (max_unmap_sectors >> 8) & 0xff;
749 outbuf[23] = max_unmap_sectors & 0xff;
751 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header */
757 /* optimal unmap granularity */
758 outbuf[28] = (unmap_sectors >> 24) & 0xff;
759 outbuf[29] = (unmap_sectors >> 16) & 0xff;
760 outbuf[30] = (unmap_sectors >> 8) & 0xff;
761 outbuf[31] = unmap_sectors & 0xff;
763 /* max write same size */
769 outbuf[40] = (max_io_sectors >> 24) & 0xff;
770 outbuf[41] = (max_io_sectors >> 16) & 0xff;
771 outbuf[42] = (max_io_sectors >> 8) & 0xff;
772 outbuf[43] = max_io_sectors & 0xff;
775 case 0xb1: /* block device characteristics */
778 outbuf[4] = (s->rotation_rate >> 8) & 0xff;
779 outbuf[5] = s->rotation_rate & 0xff;
784 case 0xb2: /* thin provisioning */
788 outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */
789 outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1;
797 assert(buflen - start <= 255);
798 outbuf[start - 1] = buflen - start;
802 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
804 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
807 if (req->cmd.buf[1] & 0x1) {
808 /* Vital product data */
809 return scsi_disk_emulate_vpd_page(req, outbuf);
812 /* Standard INQUIRY data */
813 if (req->cmd.buf[2] != 0) {
818 buflen = req->cmd.xfer;
819 if (buflen > SCSI_MAX_INQUIRY_LEN) {
820 buflen = SCSI_MAX_INQUIRY_LEN;
823 outbuf[0] = s->qdev.type & 0x1f;
824 outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0;
826 strpadcpy((char *) &outbuf[16], 16, s->product, ' ');
827 strpadcpy((char *) &outbuf[8], 8, s->vendor, ' ');
829 memset(&outbuf[32], 0, 4);
830 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
832 * We claim conformance to SPC-3, which is required for guests
833 * to ask for modern features like READ CAPACITY(16) or the
834 * block characteristics VPD page by default. Not all of SPC-3
835 * is actually implemented, but we're good enough.
837 outbuf[2] = s->qdev.default_scsi_version;
838 outbuf[3] = 2 | 0x10; /* Format 2, HiSup */
841 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
843 /* If the allocation length of CDB is too small,
844 the additional length is not adjusted */
848 /* Sync data transfer and TCQ. */
849 outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0);
853 static inline bool media_is_dvd(SCSIDiskState *s)
856 if (s->qdev.type != TYPE_ROM) {
859 if (!blk_is_available(s->qdev.conf.blk)) {
862 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
863 return nb_sectors > CD_MAX_SECTORS;
866 static inline bool media_is_cd(SCSIDiskState *s)
869 if (s->qdev.type != TYPE_ROM) {
872 if (!blk_is_available(s->qdev.conf.blk)) {
875 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
876 return nb_sectors <= CD_MAX_SECTORS;
879 static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r,
882 uint8_t type = r->req.cmd.buf[1] & 7;
884 if (s->qdev.type != TYPE_ROM) {
888 /* Types 1/2 are only defined for Blu-Ray. */
890 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
894 memset(outbuf, 0, 34);
896 outbuf[2] = 0xe; /* last session complete, disc finalized */
897 outbuf[3] = 1; /* first track on disc */
898 outbuf[4] = 1; /* # of sessions */
899 outbuf[5] = 1; /* first track of last session */
900 outbuf[6] = 1; /* last track of last session */
901 outbuf[7] = 0x20; /* unrestricted use */
902 outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */
903 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
904 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
905 /* 24-31: disc bar code */
906 /* 32: disc application code */
907 /* 33: number of OPC tables */
912 static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
915 static const int rds_caps_size[5] = {
922 uint8_t media = r->req.cmd.buf[1];
923 uint8_t layer = r->req.cmd.buf[6];
924 uint8_t format = r->req.cmd.buf[7];
927 if (s->qdev.type != TYPE_ROM) {
931 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
935 if (format != 0xff) {
936 if (!blk_is_available(s->qdev.conf.blk)) {
937 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
940 if (media_is_cd(s)) {
941 scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT));
944 if (format >= ARRAY_SIZE(rds_caps_size)) {
947 size = rds_caps_size[format];
948 memset(outbuf, 0, size);
953 /* Physical format information */
958 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
960 outbuf[4] = 1; /* DVD-ROM, part version 1 */
961 outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
962 outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
963 outbuf[7] = 0; /* default densities */
965 stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */
966 stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */
970 case 0x01: /* DVD copyright information, all zeros */
973 case 0x03: /* BCA information - invalid field for no BCA info */
976 case 0x04: /* DVD disc manufacturing information, all zeros */
979 case 0xff: { /* List capabilities */
982 for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) {
983 if (!rds_caps_size[i]) {
987 outbuf[size + 1] = 0x40; /* Not writable, readable */
988 stw_be_p(&outbuf[size + 2], rds_caps_size[i]);
998 /* Size of buffer, not including 2 byte size field */
999 stw_be_p(outbuf, size - 2);
1006 static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
1008 uint8_t event_code, media_status;
1012 media_status = MS_TRAY_OPEN;
1013 } else if (blk_is_inserted(s->qdev.conf.blk)) {
1014 media_status = MS_MEDIA_PRESENT;
1017 /* Event notification descriptor */
1018 event_code = MEC_NO_CHANGE;
1019 if (media_status != MS_TRAY_OPEN) {
1020 if (s->media_event) {
1021 event_code = MEC_NEW_MEDIA;
1022 s->media_event = false;
1023 } else if (s->eject_request) {
1024 event_code = MEC_EJECT_REQUESTED;
1025 s->eject_request = false;
1029 outbuf[0] = event_code;
1030 outbuf[1] = media_status;
1032 /* These fields are reserved, just clear them. */
1038 static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r,
1042 uint8_t *buf = r->req.cmd.buf;
1043 uint8_t notification_class_request = buf[4];
1044 if (s->qdev.type != TYPE_ROM) {
1047 if ((buf[1] & 1) == 0) {
1053 outbuf[0] = outbuf[1] = 0;
1054 outbuf[3] = 1 << GESN_MEDIA; /* supported events */
1055 if (notification_class_request & (1 << GESN_MEDIA)) {
1056 outbuf[2] = GESN_MEDIA;
1057 size += scsi_event_status_media(s, &outbuf[size]);
1061 stw_be_p(outbuf, size - 4);
1065 static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
1069 if (s->qdev.type != TYPE_ROM) {
1073 if (media_is_dvd(s)) {
1074 current = MMC_PROFILE_DVD_ROM;
1075 } else if (media_is_cd(s)) {
1076 current = MMC_PROFILE_CD_ROM;
1078 current = MMC_PROFILE_NONE;
1081 memset(outbuf, 0, 40);
1082 stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */
1083 stw_be_p(&outbuf[6], current);
1084 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
1085 outbuf[10] = 0x03; /* persistent, current */
1086 outbuf[11] = 8; /* two profiles */
1087 stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM);
1088 outbuf[14] = (current == MMC_PROFILE_DVD_ROM);
1089 stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM);
1090 outbuf[18] = (current == MMC_PROFILE_CD_ROM);
1091 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
1092 stw_be_p(&outbuf[20], 1);
1093 outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */
1095 stl_be_p(&outbuf[24], 1); /* SCSI */
1096 outbuf[28] = 1; /* DBE = 1, mandatory */
1097 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
1098 stw_be_p(&outbuf[32], 3);
1099 outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */
1101 outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1102 /* TODO: Random readable, CD read, DVD read, drive serial number,
1107 static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf)
1109 if (s->qdev.type != TYPE_ROM) {
1112 memset(outbuf, 0, 8);
1113 outbuf[5] = 1; /* CD-ROM */
1117 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
1120 static const int mode_sense_valid[0x3f] = {
1121 [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK),
1122 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK),
1123 [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1124 [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1125 [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM),
1126 [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM),
1129 uint8_t *p = *p_outbuf + 2;
1132 if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
1137 * If Changeable Values are requested, a mask denoting those mode parameters
1138 * that are changeable shall be returned. As we currently don't support
1139 * parameter changes via MODE_SELECT all bits are returned set to zero.
1140 * The buffer was already menset to zero by the caller of this function.
1142 * The offsets here are off by two compared to the descriptions in the
1143 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1144 * but it is done so that offsets are consistent within our implementation
1145 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1146 * 2-byte and 4-byte headers.
1149 case MODE_PAGE_HD_GEOMETRY:
1151 if (page_control == 1) { /* Changeable Values */
1154 /* if a geometry hint is available, use it */
1155 p[0] = (s->qdev.conf.cyls >> 16) & 0xff;
1156 p[1] = (s->qdev.conf.cyls >> 8) & 0xff;
1157 p[2] = s->qdev.conf.cyls & 0xff;
1158 p[3] = s->qdev.conf.heads & 0xff;
1159 /* Write precomp start cylinder, disabled */
1160 p[4] = (s->qdev.conf.cyls >> 16) & 0xff;
1161 p[5] = (s->qdev.conf.cyls >> 8) & 0xff;
1162 p[6] = s->qdev.conf.cyls & 0xff;
1163 /* Reduced current start cylinder, disabled */
1164 p[7] = (s->qdev.conf.cyls >> 16) & 0xff;
1165 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1166 p[9] = s->qdev.conf.cyls & 0xff;
1167 /* Device step rate [ns], 200ns */
1170 /* Landing zone cylinder */
1174 /* Medium rotation rate [rpm], 5400 rpm */
1175 p[18] = (5400 >> 8) & 0xff;
1176 p[19] = 5400 & 0xff;
1179 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
1181 if (page_control == 1) { /* Changeable Values */
1184 /* Transfer rate [kbit/s], 5Mbit/s */
1187 /* if a geometry hint is available, use it */
1188 p[2] = s->qdev.conf.heads & 0xff;
1189 p[3] = s->qdev.conf.secs & 0xff;
1190 p[4] = s->qdev.blocksize >> 8;
1191 p[6] = (s->qdev.conf.cyls >> 8) & 0xff;
1192 p[7] = s->qdev.conf.cyls & 0xff;
1193 /* Write precomp start cylinder, disabled */
1194 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1195 p[9] = s->qdev.conf.cyls & 0xff;
1196 /* Reduced current start cylinder, disabled */
1197 p[10] = (s->qdev.conf.cyls >> 8) & 0xff;
1198 p[11] = s->qdev.conf.cyls & 0xff;
1199 /* Device step rate [100us], 100us */
1202 /* Device step pulse width [us], 1us */
1204 /* Device head settle delay [100us], 100us */
1207 /* Motor on delay [0.1s], 0.1s */
1209 /* Motor off delay [0.1s], 0.1s */
1211 /* Medium rotation rate [rpm], 5400 rpm */
1212 p[26] = (5400 >> 8) & 0xff;
1213 p[27] = 5400 & 0xff;
1216 case MODE_PAGE_CACHING:
1218 if (page_control == 1 || /* Changeable Values */
1219 blk_enable_write_cache(s->qdev.conf.blk)) {
1224 case MODE_PAGE_R_W_ERROR:
1226 if (page_control == 1) { /* Changeable Values */
1229 p[0] = 0x80; /* Automatic Write Reallocation Enabled */
1230 if (s->qdev.type == TYPE_ROM) {
1231 p[1] = 0x20; /* Read Retry Count */
1235 case MODE_PAGE_AUDIO_CTL:
1239 case MODE_PAGE_CAPABILITIES:
1241 if (page_control == 1) { /* Changeable Values */
1245 p[0] = 0x3b; /* CD-R & CD-RW read */
1246 p[1] = 0; /* Writing not supported */
1247 p[2] = 0x7f; /* Audio, composite, digital out,
1248 mode 2 form 1&2, multi session */
1249 p[3] = 0xff; /* CD DA, DA accurate, RW supported,
1250 RW corrected, C2 errors, ISRC,
1252 p[4] = 0x2d | (s->tray_locked ? 2 : 0);
1253 /* Locking supported, jumper present, eject, tray */
1254 p[5] = 0; /* no volume & mute control, no
1256 p[6] = (50 * 176) >> 8; /* 50x read speed */
1257 p[7] = (50 * 176) & 0xff;
1258 p[8] = 2 >> 8; /* Two volume levels */
1260 p[10] = 2048 >> 8; /* 2M buffer */
1261 p[11] = 2048 & 0xff;
1262 p[12] = (16 * 176) >> 8; /* 16x read speed current */
1263 p[13] = (16 * 176) & 0xff;
1264 p[16] = (16 * 176) >> 8; /* 16x write speed */
1265 p[17] = (16 * 176) & 0xff;
1266 p[18] = (16 * 176) >> 8; /* 16x write speed current */
1267 p[19] = (16 * 176) & 0xff;
1274 assert(length < 256);
1275 (*p_outbuf)[0] = page;
1276 (*p_outbuf)[1] = length;
1277 *p_outbuf += length + 2;
1281 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
1283 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1284 uint64_t nb_sectors;
1286 int page, buflen, ret, page_control;
1288 uint8_t dev_specific_param;
1290 dbd = (r->req.cmd.buf[1] & 0x8) != 0;
1291 page = r->req.cmd.buf[2] & 0x3f;
1292 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
1293 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1294 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
1295 memset(outbuf, 0, r->req.cmd.xfer);
1298 if (s->qdev.type == TYPE_DISK) {
1299 dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0;
1300 if (blk_is_read_only(s->qdev.conf.blk)) {
1301 dev_specific_param |= 0x80; /* Readonly. */
1304 /* MMC prescribes that CD/DVD drives have no block descriptors,
1305 * and defines no device-specific parameter. */
1306 dev_specific_param = 0x00;
1310 if (r->req.cmd.buf[0] == MODE_SENSE) {
1311 p[1] = 0; /* Default media type. */
1312 p[2] = dev_specific_param;
1313 p[3] = 0; /* Block descriptor length. */
1315 } else { /* MODE_SENSE_10 */
1316 p[2] = 0; /* Default media type. */
1317 p[3] = dev_specific_param;
1318 p[6] = p[7] = 0; /* Block descriptor length. */
1322 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
1323 if (!dbd && nb_sectors) {
1324 if (r->req.cmd.buf[0] == MODE_SENSE) {
1325 outbuf[3] = 8; /* Block descriptor length */
1326 } else { /* MODE_SENSE_10 */
1327 outbuf[7] = 8; /* Block descriptor length */
1329 nb_sectors /= (s->qdev.blocksize / 512);
1330 if (nb_sectors > 0xffffff) {
1333 p[0] = 0; /* media density code */
1334 p[1] = (nb_sectors >> 16) & 0xff;
1335 p[2] = (nb_sectors >> 8) & 0xff;
1336 p[3] = nb_sectors & 0xff;
1337 p[4] = 0; /* reserved */
1338 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
1339 p[6] = s->qdev.blocksize >> 8;
1344 if (page_control == 3) {
1346 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
1351 for (page = 0; page <= 0x3e; page++) {
1352 mode_sense_page(s, page, &p, page_control);
1355 ret = mode_sense_page(s, page, &p, page_control);
1361 buflen = p - outbuf;
1363 * The mode data length field specifies the length in bytes of the
1364 * following data that is available to be transferred. The mode data
1365 * length does not include itself.
1367 if (r->req.cmd.buf[0] == MODE_SENSE) {
1368 outbuf[0] = buflen - 1;
1369 } else { /* MODE_SENSE_10 */
1370 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1371 outbuf[1] = (buflen - 2) & 0xff;
1376 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1378 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1379 int start_track, format, msf, toclen;
1380 uint64_t nb_sectors;
1382 msf = req->cmd.buf[1] & 2;
1383 format = req->cmd.buf[2] & 0xf;
1384 start_track = req->cmd.buf[6];
1385 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
1386 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
1387 nb_sectors /= s->qdev.blocksize / 512;
1390 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
1393 /* multi session : only a single session defined */
1395 memset(outbuf, 0, 12);
1401 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
1409 static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
1411 SCSIRequest *req = &r->req;
1412 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1413 bool start = req->cmd.buf[4] & 1;
1414 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
1415 int pwrcnd = req->cmd.buf[4] & 0xf0;
1418 /* eject/load only happens for power condition == 0 */
1422 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) {
1423 if (!start && !s->tray_open && s->tray_locked) {
1424 scsi_check_condition(r,
1425 blk_is_inserted(s->qdev.conf.blk)
1426 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1427 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1431 if (s->tray_open != !start) {
1432 blk_eject(s->qdev.conf.blk, !start);
1433 s->tray_open = !start;
1439 static void scsi_disk_emulate_read_data(SCSIRequest *req)
1441 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1442 int buflen = r->iov.iov_len;
1445 DPRINTF("Read buf_len=%d\n", buflen);
1448 scsi_req_data(&r->req, buflen);
1452 /* This also clears the sense buffer for REQUEST SENSE. */
1453 scsi_req_complete(&r->req, GOOD);
1456 static int scsi_disk_check_mode_select(SCSIDiskState *s, int page,
1457 uint8_t *inbuf, int inlen)
1459 uint8_t mode_current[SCSI_MAX_MODE_LEN];
1460 uint8_t mode_changeable[SCSI_MAX_MODE_LEN];
1462 int len, expected_len, changeable_len, i;
1464 /* The input buffer does not include the page header, so it is
1467 expected_len = inlen + 2;
1468 if (expected_len > SCSI_MAX_MODE_LEN) {
1473 memset(mode_current, 0, inlen + 2);
1474 len = mode_sense_page(s, page, &p, 0);
1475 if (len < 0 || len != expected_len) {
1479 p = mode_changeable;
1480 memset(mode_changeable, 0, inlen + 2);
1481 changeable_len = mode_sense_page(s, page, &p, 1);
1482 assert(changeable_len == len);
1484 /* Check that unchangeable bits are the same as what MODE SENSE
1487 for (i = 2; i < len; i++) {
1488 if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) {
1495 static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p)
1498 case MODE_PAGE_CACHING:
1499 blk_set_enable_write_cache(s->qdev.conf.blk, (p[0] & 4) != 0);
1507 static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change)
1509 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1512 int page, subpage, page_len;
1514 /* Parse both possible formats for the mode page headers. */
1518 goto invalid_param_len;
1521 page_len = lduw_be_p(&p[2]);
1526 goto invalid_param_len;
1537 if (page_len > len) {
1538 goto invalid_param_len;
1542 if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) {
1546 scsi_disk_apply_mode_select(s, page, p);
1555 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1559 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1563 static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
1565 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1567 int cmd = r->req.cmd.buf[0];
1568 int len = r->req.cmd.xfer;
1569 int hdr_len = (cmd == MODE_SELECT ? 4 : 8);
1573 /* We only support PF=1, SP=0. */
1574 if ((r->req.cmd.buf[1] & 0x11) != 0x10) {
1578 if (len < hdr_len) {
1579 goto invalid_param_len;
1582 bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6]));
1586 goto invalid_param_len;
1588 if (bd_len != 0 && bd_len != 8) {
1595 /* Ensure no change is made if there is an error! */
1596 for (pass = 0; pass < 2; pass++) {
1597 if (mode_select_pages(r, p, len, pass == 1) < 0) {
1602 if (!blk_enable_write_cache(s->qdev.conf.blk)) {
1603 /* The request is used as the AIO opaque value, so add a ref. */
1604 scsi_req_ref(&r->req);
1605 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
1607 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r);
1611 scsi_req_complete(&r->req, GOOD);
1615 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1619 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1623 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1626 static inline bool check_lba_range(SCSIDiskState *s,
1627 uint64_t sector_num, uint32_t nb_sectors)
1630 * The first line tests that no overflow happens when computing the last
1631 * sector. The second line tests that the last accessed sector is in
1634 * Careful, the computations should not underflow for nb_sectors == 0,
1635 * and a 0-block read to the first LBA beyond the end of device is
1638 return (sector_num <= sector_num + nb_sectors &&
1639 sector_num + nb_sectors <= s->qdev.max_lba + 1);
1642 typedef struct UnmapCBData {
1648 static void scsi_unmap_complete(void *opaque, int ret);
1650 static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
1652 SCSIDiskReq *r = data->r;
1653 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1654 uint64_t sector_num;
1655 uint32_t nb_sectors;
1657 assert(r->req.aiocb == NULL);
1658 if (scsi_disk_req_check_error(r, ret, false)) {
1662 if (data->count > 0) {
1663 sector_num = ldq_be_p(&data->inbuf[0]);
1664 nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
1665 if (!check_lba_range(s, sector_num, nb_sectors)) {
1666 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1670 r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk,
1671 sector_num * s->qdev.blocksize,
1672 nb_sectors * s->qdev.blocksize,
1673 scsi_unmap_complete, data);
1679 scsi_req_complete(&r->req, GOOD);
1682 scsi_req_unref(&r->req);
1686 static void scsi_unmap_complete(void *opaque, int ret)
1688 UnmapCBData *data = opaque;
1689 SCSIDiskReq *r = data->r;
1690 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1692 assert(r->req.aiocb != NULL);
1693 r->req.aiocb = NULL;
1695 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
1696 scsi_unmap_complete_noio(data, ret);
1697 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1700 static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
1702 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1704 int len = r->req.cmd.xfer;
1707 /* Reject ANCHOR=1. */
1708 if (r->req.cmd.buf[1] & 0x1) {
1713 goto invalid_param_len;
1715 if (len < lduw_be_p(&p[0]) + 2) {
1716 goto invalid_param_len;
1718 if (len < lduw_be_p(&p[2]) + 8) {
1719 goto invalid_param_len;
1721 if (lduw_be_p(&p[2]) & 15) {
1722 goto invalid_param_len;
1725 if (blk_is_read_only(s->qdev.conf.blk)) {
1726 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1730 data = g_new0(UnmapCBData, 1);
1732 data->inbuf = &p[8];
1733 data->count = lduw_be_p(&p[2]) >> 4;
1735 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1736 scsi_req_ref(&r->req);
1737 scsi_unmap_complete_noio(data, 0);
1741 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1745 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1748 typedef struct WriteSameCBData {
1756 static void scsi_write_same_complete(void *opaque, int ret)
1758 WriteSameCBData *data = opaque;
1759 SCSIDiskReq *r = data->r;
1760 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1762 assert(r->req.aiocb != NULL);
1763 r->req.aiocb = NULL;
1764 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
1765 if (scsi_disk_req_check_error(r, ret, true)) {
1769 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
1771 data->nb_sectors -= data->iov.iov_len / 512;
1772 data->sector += data->iov.iov_len / 512;
1773 data->iov.iov_len = MIN(data->nb_sectors * 512, data->iov.iov_len);
1774 if (data->iov.iov_len) {
1775 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1776 data->iov.iov_len, BLOCK_ACCT_WRITE);
1777 /* Reinitialize qiov, to handle unaligned WRITE SAME request
1778 * where final qiov may need smaller size */
1779 qemu_iovec_init_external(&data->qiov, &data->iov, 1);
1780 r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk,
1781 data->sector << BDRV_SECTOR_BITS,
1783 scsi_write_same_complete, data);
1784 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1788 scsi_req_complete(&r->req, GOOD);
1791 scsi_req_unref(&r->req);
1792 qemu_vfree(data->iov.iov_base);
1794 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1797 static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
1799 SCSIRequest *req = &r->req;
1800 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1801 uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf);
1802 WriteSameCBData *data;
1806 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1807 if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) {
1808 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1812 if (blk_is_read_only(s->qdev.conf.blk)) {
1813 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1816 if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
1817 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1821 if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, s->qdev.blocksize)) {
1822 int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0;
1824 /* The request is used as the AIO opaque value, so add a ref. */
1825 scsi_req_ref(&r->req);
1826 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1827 nb_sectors * s->qdev.blocksize,
1829 r->req.aiocb = blk_aio_pwrite_zeroes(s->qdev.conf.blk,
1830 r->req.cmd.lba * s->qdev.blocksize,
1831 nb_sectors * s->qdev.blocksize,
1832 flags, scsi_aio_complete, r);
1836 data = g_new0(WriteSameCBData, 1);
1838 data->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1839 data->nb_sectors = nb_sectors * (s->qdev.blocksize / 512);
1840 data->iov.iov_len = MIN(data->nb_sectors * 512, SCSI_WRITE_SAME_MAX);
1841 data->iov.iov_base = buf = blk_blockalign(s->qdev.conf.blk,
1843 qemu_iovec_init_external(&data->qiov, &data->iov, 1);
1845 for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) {
1846 memcpy(&buf[i], inbuf, s->qdev.blocksize);
1849 scsi_req_ref(&r->req);
1850 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1851 data->iov.iov_len, BLOCK_ACCT_WRITE);
1852 r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk,
1853 data->sector << BDRV_SECTOR_BITS,
1855 scsi_write_same_complete, data);
1858 static void scsi_disk_emulate_write_data(SCSIRequest *req)
1860 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1862 if (r->iov.iov_len) {
1863 int buflen = r->iov.iov_len;
1864 DPRINTF("Write buf_len=%d\n", buflen);
1866 scsi_req_data(&r->req, buflen);
1870 switch (req->cmd.buf[0]) {
1872 case MODE_SELECT_10:
1873 /* This also clears the sense buffer for REQUEST SENSE. */
1874 scsi_disk_emulate_mode_select(r, r->iov.iov_base);
1878 scsi_disk_emulate_unmap(r, r->iov.iov_base);
1884 if (r->req.status == -1) {
1885 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1891 scsi_disk_emulate_write_same(r, r->iov.iov_base);
1899 static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
1901 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1902 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1903 uint64_t nb_sectors;
1907 switch (req->cmd.buf[0]) {
1916 case ALLOW_MEDIUM_REMOVAL:
1917 case GET_CONFIGURATION:
1918 case GET_EVENT_STATUS_NOTIFICATION:
1919 case MECHANISM_STATUS:
1924 if (!blk_is_available(s->qdev.conf.blk)) {
1925 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1932 * FIXME: we shouldn't return anything bigger than 4k, but the code
1933 * requires the buffer to be as big as req->cmd.xfer in several
1934 * places. So, do not allow CDBs with a very large ALLOCATION
1935 * LENGTH. The real fix would be to modify scsi_read_data and
1936 * dma_buf_read, so that they return data beyond the buflen
1939 if (req->cmd.xfer > 65536) {
1940 goto illegal_request;
1942 r->buflen = MAX(4096, req->cmd.xfer);
1944 if (!r->iov.iov_base) {
1945 r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen);
1948 buflen = req->cmd.xfer;
1949 outbuf = r->iov.iov_base;
1950 memset(outbuf, 0, r->buflen);
1951 switch (req->cmd.buf[0]) {
1952 case TEST_UNIT_READY:
1953 assert(blk_is_available(s->qdev.conf.blk));
1956 buflen = scsi_disk_emulate_inquiry(req, outbuf);
1958 goto illegal_request;
1963 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
1965 goto illegal_request;
1969 buflen = scsi_disk_emulate_read_toc(req, outbuf);
1971 goto illegal_request;
1975 if (req->cmd.buf[1] & 1) {
1976 goto illegal_request;
1980 if (req->cmd.buf[1] & 3) {
1981 goto illegal_request;
1985 if (req->cmd.buf[1] & 1) {
1986 goto illegal_request;
1990 if (req->cmd.buf[1] & 3) {
1991 goto illegal_request;
1995 if (scsi_disk_emulate_start_stop(r) < 0) {
1999 case ALLOW_MEDIUM_REMOVAL:
2000 s->tray_locked = req->cmd.buf[4] & 1;
2001 blk_lock_medium(s->qdev.conf.blk, req->cmd.buf[4] & 1);
2003 case READ_CAPACITY_10:
2004 /* The normal LEN field for this command is zero. */
2005 memset(outbuf, 0, 8);
2006 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2008 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
2011 if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
2012 goto illegal_request;
2014 nb_sectors /= s->qdev.blocksize / 512;
2015 /* Returned value is the address of the last sector. */
2017 /* Remember the new size for read/write sanity checking. */
2018 s->qdev.max_lba = nb_sectors;
2019 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
2020 if (nb_sectors > UINT32_MAX) {
2021 nb_sectors = UINT32_MAX;
2023 outbuf[0] = (nb_sectors >> 24) & 0xff;
2024 outbuf[1] = (nb_sectors >> 16) & 0xff;
2025 outbuf[2] = (nb_sectors >> 8) & 0xff;
2026 outbuf[3] = nb_sectors & 0xff;
2029 outbuf[6] = s->qdev.blocksize >> 8;
2033 /* Just return "NO SENSE". */
2034 buflen = scsi_convert_sense(NULL, 0, outbuf, r->buflen,
2035 (req->cmd.buf[1] & 1) == 0);
2037 goto illegal_request;
2040 case MECHANISM_STATUS:
2041 buflen = scsi_emulate_mechanism_status(s, outbuf);
2043 goto illegal_request;
2046 case GET_CONFIGURATION:
2047 buflen = scsi_get_configuration(s, outbuf);
2049 goto illegal_request;
2052 case GET_EVENT_STATUS_NOTIFICATION:
2053 buflen = scsi_get_event_status_notification(s, r, outbuf);
2055 goto illegal_request;
2058 case READ_DISC_INFORMATION:
2059 buflen = scsi_read_disc_information(s, r, outbuf);
2061 goto illegal_request;
2064 case READ_DVD_STRUCTURE:
2065 buflen = scsi_read_dvd_structure(s, r, outbuf);
2067 goto illegal_request;
2070 case SERVICE_ACTION_IN_16:
2071 /* Service Action In subcommands. */
2072 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
2073 DPRINTF("SAI READ CAPACITY(16)\n");
2074 memset(outbuf, 0, req->cmd.xfer);
2075 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2077 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
2080 if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
2081 goto illegal_request;
2083 nb_sectors /= s->qdev.blocksize / 512;
2084 /* Returned value is the address of the last sector. */
2086 /* Remember the new size for read/write sanity checking. */
2087 s->qdev.max_lba = nb_sectors;
2088 outbuf[0] = (nb_sectors >> 56) & 0xff;
2089 outbuf[1] = (nb_sectors >> 48) & 0xff;
2090 outbuf[2] = (nb_sectors >> 40) & 0xff;
2091 outbuf[3] = (nb_sectors >> 32) & 0xff;
2092 outbuf[4] = (nb_sectors >> 24) & 0xff;
2093 outbuf[5] = (nb_sectors >> 16) & 0xff;
2094 outbuf[6] = (nb_sectors >> 8) & 0xff;
2095 outbuf[7] = nb_sectors & 0xff;
2098 outbuf[10] = s->qdev.blocksize >> 8;
2101 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
2103 /* set TPE bit if the format supports discard */
2104 if (s->qdev.conf.discard_granularity) {
2108 /* Protection, exponent and lowest lba field left blank. */
2111 DPRINTF("Unsupported Service Action In\n");
2112 goto illegal_request;
2113 case SYNCHRONIZE_CACHE:
2114 /* The request is used as the AIO opaque value, so add a ref. */
2115 scsi_req_ref(&r->req);
2116 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
2118 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r);
2121 DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
2122 if (r->req.cmd.lba > s->qdev.max_lba) {
2127 DPRINTF("Mode Select(6) (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2129 case MODE_SELECT_10:
2130 DPRINTF("Mode Select(10) (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2133 DPRINTF("Unmap (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2138 DPRINTF("Verify (bytchk %d)\n", (req->cmd.buf[1] >> 1) & 3);
2139 if (req->cmd.buf[1] & 6) {
2140 goto illegal_request;
2145 DPRINTF("WRITE SAME %d (len %lu)\n",
2146 req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16,
2147 (unsigned long)r->req.cmd.xfer);
2150 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf[0],
2151 scsi_command_name(buf[0]));
2152 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
2155 assert(!r->req.aiocb);
2156 r->iov.iov_len = MIN(r->buflen, req->cmd.xfer);
2157 if (r->iov.iov_len == 0) {
2158 scsi_req_complete(&r->req, GOOD);
2160 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2161 assert(r->iov.iov_len == req->cmd.xfer);
2162 return -r->iov.iov_len;
2164 return r->iov.iov_len;
2168 if (r->req.status == -1) {
2169 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2174 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2178 /* Execute a scsi command. Returns the length of the data expected by the
2179 command. This will be Positive for data transfers from the device
2180 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2181 and zero if the command does not transfer any data. */
2183 static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
2185 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2186 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
2187 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
2193 if (!blk_is_available(s->qdev.conf.blk)) {
2194 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
2198 len = scsi_data_cdb_xfer(r->req.cmd.buf);
2204 DPRINTF("Read (sector %" PRId64 ", count %u)\n", r->req.cmd.lba, len);
2205 /* Protection information is not supported. For SCSI versions 2 and
2206 * older (as determined by snooping the guest's INQUIRY commands),
2207 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2209 if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) {
2210 goto illegal_request;
2212 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2215 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2216 r->sector_count = len * (s->qdev.blocksize / 512);
2222 case WRITE_VERIFY_10:
2223 case WRITE_VERIFY_12:
2224 case WRITE_VERIFY_16:
2225 if (blk_is_read_only(s->qdev.conf.blk)) {
2226 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
2229 DPRINTF("Write %s(sector %" PRId64 ", count %u)\n",
2230 (command & 0xe) == 0xe ? "And Verify " : "",
2231 r->req.cmd.lba, len);
2236 /* We get here only for BYTCHK == 0x01 and only for scsi-block.
2237 * As far as DMA is concerned, we can treat it the same as a write;
2238 * scsi_block_do_sgio will send VERIFY commands.
2240 if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) {
2241 goto illegal_request;
2243 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2246 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2247 r->sector_count = len * (s->qdev.blocksize / 512);
2252 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2255 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2258 r->need_fua_emulation = sdc->need_fua_emulation(&r->req.cmd);
2259 if (r->sector_count == 0) {
2260 scsi_req_complete(&r->req, GOOD);
2262 assert(r->iov.iov_len == 0);
2263 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2264 return -r->sector_count * 512;
2266 return r->sector_count * 512;
2270 static void scsi_disk_reset(DeviceState *dev)
2272 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
2273 uint64_t nb_sectors;
2275 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
2277 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2278 nb_sectors /= s->qdev.blocksize / 512;
2282 s->qdev.max_lba = nb_sectors;
2283 /* reset tray statuses */
2287 s->qdev.scsi_version = s->qdev.default_scsi_version;
2290 static void scsi_disk_resize_cb(void *opaque)
2292 SCSIDiskState *s = opaque;
2294 /* SPC lists this sense code as available only for
2295 * direct-access devices.
2297 if (s->qdev.type == TYPE_DISK) {
2298 scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED));
2302 static void scsi_cd_change_media_cb(void *opaque, bool load, Error **errp)
2304 SCSIDiskState *s = opaque;
2307 * When a CD gets changed, we have to report an ejected state and
2308 * then a loaded state to guests so that they detect tray
2309 * open/close and media change events. Guests that do not use
2310 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2311 * states rely on this behavior.
2313 * media_changed governs the state machine used for unit attention
2314 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2316 s->media_changed = load;
2317 s->tray_open = !load;
2318 scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM));
2319 s->media_event = true;
2320 s->eject_request = false;
2323 static void scsi_cd_eject_request_cb(void *opaque, bool force)
2325 SCSIDiskState *s = opaque;
2327 s->eject_request = true;
2329 s->tray_locked = false;
2333 static bool scsi_cd_is_tray_open(void *opaque)
2335 return ((SCSIDiskState *)opaque)->tray_open;
2338 static bool scsi_cd_is_medium_locked(void *opaque)
2340 return ((SCSIDiskState *)opaque)->tray_locked;
2343 static const BlockDevOps scsi_disk_removable_block_ops = {
2344 .change_media_cb = scsi_cd_change_media_cb,
2345 .eject_request_cb = scsi_cd_eject_request_cb,
2346 .is_tray_open = scsi_cd_is_tray_open,
2347 .is_medium_locked = scsi_cd_is_medium_locked,
2349 .resize_cb = scsi_disk_resize_cb,
2352 static const BlockDevOps scsi_disk_block_ops = {
2353 .resize_cb = scsi_disk_resize_cb,
2356 static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
2358 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2359 if (s->media_changed) {
2360 s->media_changed = false;
2361 scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED));
2365 static void scsi_realize(SCSIDevice *dev, Error **errp)
2367 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2369 if (!s->qdev.conf.blk) {
2370 error_setg(errp, "drive property not set");
2374 if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2375 !blk_is_inserted(s->qdev.conf.blk)) {
2376 error_setg(errp, "Device needs media, but drive is empty");
2380 blkconf_blocksizes(&s->qdev.conf);
2382 if (s->qdev.conf.logical_block_size >
2383 s->qdev.conf.physical_block_size) {
2385 "logical_block_size > physical_block_size not supported");
2389 if (dev->type == TYPE_DISK) {
2390 if (!blkconf_geometry(&dev->conf, NULL, 65535, 255, 255, errp)) {
2394 if (!blkconf_apply_backend_options(&dev->conf,
2395 blk_is_read_only(s->qdev.conf.blk),
2396 dev->type == TYPE_DISK, errp)) {
2400 if (s->qdev.conf.discard_granularity == -1) {
2401 s->qdev.conf.discard_granularity =
2402 MAX(s->qdev.conf.logical_block_size, DEFAULT_DISCARD_GRANULARITY);
2406 s->version = g_strdup(qemu_hw_version());
2409 s->vendor = g_strdup("QEMU");
2412 if (blk_is_sg(s->qdev.conf.blk)) {
2413 error_setg(errp, "unwanted /dev/sg*");
2417 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2418 !(s->features & (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS))) {
2419 blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_removable_block_ops, s);
2421 blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_block_ops, s);
2423 blk_set_guest_block_size(s->qdev.conf.blk, s->qdev.blocksize);
2425 blk_iostatus_enable(s->qdev.conf.blk);
2428 static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
2430 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2431 /* can happen for devices without drive. The error message for missing
2432 * backend will be issued in scsi_realize
2434 if (s->qdev.conf.blk) {
2435 blkconf_blocksizes(&s->qdev.conf);
2437 s->qdev.blocksize = s->qdev.conf.logical_block_size;
2438 s->qdev.type = TYPE_DISK;
2440 s->product = g_strdup("QEMU HARDDISK");
2442 scsi_realize(&s->qdev, errp);
2445 static void scsi_cd_realize(SCSIDevice *dev, Error **errp)
2447 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2450 if (!dev->conf.blk) {
2451 /* Anonymous BlockBackend for an empty drive. As we put it into
2452 * dev->conf, qdev takes care of detaching on unplug. */
2453 dev->conf.blk = blk_new(0, BLK_PERM_ALL);
2454 ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
2458 s->qdev.blocksize = 2048;
2459 s->qdev.type = TYPE_ROM;
2460 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2462 s->product = g_strdup("QEMU CD-ROM");
2464 scsi_realize(&s->qdev, errp);
2467 static void scsi_disk_realize(SCSIDevice *dev, Error **errp)
2470 Error *local_err = NULL;
2472 if (!dev->conf.blk) {
2473 scsi_realize(dev, &local_err);
2475 error_propagate(errp, local_err);
2479 dinfo = blk_legacy_dinfo(dev->conf.blk);
2480 if (dinfo && dinfo->media_cd) {
2481 scsi_cd_realize(dev, errp);
2483 scsi_hd_realize(dev, errp);
2487 static const SCSIReqOps scsi_disk_emulate_reqops = {
2488 .size = sizeof(SCSIDiskReq),
2489 .free_req = scsi_free_request,
2490 .send_command = scsi_disk_emulate_command,
2491 .read_data = scsi_disk_emulate_read_data,
2492 .write_data = scsi_disk_emulate_write_data,
2493 .get_buf = scsi_get_buf,
2496 static const SCSIReqOps scsi_disk_dma_reqops = {
2497 .size = sizeof(SCSIDiskReq),
2498 .free_req = scsi_free_request,
2499 .send_command = scsi_disk_dma_command,
2500 .read_data = scsi_read_data,
2501 .write_data = scsi_write_data,
2502 .get_buf = scsi_get_buf,
2503 .load_request = scsi_disk_load_request,
2504 .save_request = scsi_disk_save_request,
2507 static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {
2508 [TEST_UNIT_READY] = &scsi_disk_emulate_reqops,
2509 [INQUIRY] = &scsi_disk_emulate_reqops,
2510 [MODE_SENSE] = &scsi_disk_emulate_reqops,
2511 [MODE_SENSE_10] = &scsi_disk_emulate_reqops,
2512 [START_STOP] = &scsi_disk_emulate_reqops,
2513 [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops,
2514 [READ_CAPACITY_10] = &scsi_disk_emulate_reqops,
2515 [READ_TOC] = &scsi_disk_emulate_reqops,
2516 [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops,
2517 [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops,
2518 [GET_CONFIGURATION] = &scsi_disk_emulate_reqops,
2519 [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops,
2520 [MECHANISM_STATUS] = &scsi_disk_emulate_reqops,
2521 [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops,
2522 [REQUEST_SENSE] = &scsi_disk_emulate_reqops,
2523 [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops,
2524 [SEEK_10] = &scsi_disk_emulate_reqops,
2525 [MODE_SELECT] = &scsi_disk_emulate_reqops,
2526 [MODE_SELECT_10] = &scsi_disk_emulate_reqops,
2527 [UNMAP] = &scsi_disk_emulate_reqops,
2528 [WRITE_SAME_10] = &scsi_disk_emulate_reqops,
2529 [WRITE_SAME_16] = &scsi_disk_emulate_reqops,
2530 [VERIFY_10] = &scsi_disk_emulate_reqops,
2531 [VERIFY_12] = &scsi_disk_emulate_reqops,
2532 [VERIFY_16] = &scsi_disk_emulate_reqops,
2534 [READ_6] = &scsi_disk_dma_reqops,
2535 [READ_10] = &scsi_disk_dma_reqops,
2536 [READ_12] = &scsi_disk_dma_reqops,
2537 [READ_16] = &scsi_disk_dma_reqops,
2538 [WRITE_6] = &scsi_disk_dma_reqops,
2539 [WRITE_10] = &scsi_disk_dma_reqops,
2540 [WRITE_12] = &scsi_disk_dma_reqops,
2541 [WRITE_16] = &scsi_disk_dma_reqops,
2542 [WRITE_VERIFY_10] = &scsi_disk_dma_reqops,
2543 [WRITE_VERIFY_12] = &scsi_disk_dma_reqops,
2544 [WRITE_VERIFY_16] = &scsi_disk_dma_reqops,
2547 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
2548 uint8_t *buf, void *hba_private)
2550 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2552 const SCSIReqOps *ops;
2556 ops = scsi_disk_reqops_dispatch[command];
2558 ops = &scsi_disk_emulate_reqops;
2560 req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private);
2563 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
2566 for (i = 1; i < scsi_cdb_length(buf); i++) {
2567 printf(" 0x%02x", buf[i]);
2577 static int get_device_type(SCSIDiskState *s)
2583 memset(cmd, 0, sizeof(cmd));
2584 memset(buf, 0, sizeof(buf));
2586 cmd[4] = sizeof(buf);
2588 ret = scsi_SG_IO_FROM_DEV(s->qdev.conf.blk, cmd, sizeof(cmd),
2593 s->qdev.type = buf[0];
2594 if (buf[1] & 0x80) {
2595 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2600 static void scsi_block_realize(SCSIDevice *dev, Error **errp)
2602 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2606 if (!s->qdev.conf.blk) {
2607 error_setg(errp, "drive property not set");
2611 /* check we are using a driver managing SG_IO (version 3 and after) */
2612 rc = blk_ioctl(s->qdev.conf.blk, SG_GET_VERSION_NUM, &sg_version);
2614 error_setg_errno(errp, -rc, "cannot get SG_IO version number");
2616 error_append_hint(errp, "Is this a SCSI device?\n");
2620 if (sg_version < 30000) {
2621 error_setg(errp, "scsi generic interface too old");
2625 /* get device type from INQUIRY data */
2626 rc = get_device_type(s);
2628 error_setg(errp, "INQUIRY failed");
2632 /* Make a guess for the block size, we'll fix it when the guest sends.
2633 * READ CAPACITY. If they don't, they likely would assume these sizes
2634 * anyway. (TODO: check in /sys).
2636 if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
2637 s->qdev.blocksize = 2048;
2639 s->qdev.blocksize = 512;
2642 /* Makes the scsi-block device not removable by using HMP and QMP eject
2645 s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS);
2647 scsi_realize(&s->qdev, errp);
2648 scsi_generic_read_device_inquiry(&s->qdev);
2651 typedef struct SCSIBlockReq {
2653 sg_io_hdr_t io_header;
2655 /* Selected bytes of the original CDB, copied into our own CDB. */
2656 uint8_t cmd, cdb1, group_number;
2658 /* CDB passed to SG_IO. */
2662 static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req,
2663 int64_t offset, QEMUIOVector *iov,
2665 BlockCompletionFunc *cb, void *opaque)
2667 sg_io_hdr_t *io_header = &req->io_header;
2668 SCSIDiskReq *r = &req->req;
2669 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2670 int nb_logical_blocks;
2674 /* This is not supported yet. It can only happen if the guest does
2675 * reads and writes that are not aligned to one logical sectors
2676 * _and_ cover multiple MemoryRegions.
2678 assert(offset % s->qdev.blocksize == 0);
2679 assert(iov->size % s->qdev.blocksize == 0);
2681 io_header->interface_id = 'S';
2683 /* The data transfer comes from the QEMUIOVector. */
2684 io_header->dxfer_direction = direction;
2685 io_header->dxfer_len = iov->size;
2686 io_header->dxferp = (void *)iov->iov;
2687 io_header->iovec_count = iov->niov;
2688 assert(io_header->iovec_count == iov->niov); /* no overflow! */
2690 /* Build a new CDB with the LBA and length patched in, in case
2691 * DMA helpers split the transfer in multiple segments. Do not
2692 * build a CDB smaller than what the guest wanted, and only build
2693 * a larger one if strictly necessary.
2695 io_header->cmdp = req->cdb;
2696 lba = offset / s->qdev.blocksize;
2697 nb_logical_blocks = io_header->dxfer_len / s->qdev.blocksize;
2699 if ((req->cmd >> 5) == 0 && lba <= 0x1ffff) {
2701 stl_be_p(&req->cdb[0], lba | (req->cmd << 24));
2702 req->cdb[4] = nb_logical_blocks;
2704 io_header->cmd_len = 6;
2705 } else if ((req->cmd >> 5) <= 1 && lba <= 0xffffffffULL) {
2707 req->cdb[0] = (req->cmd & 0x1f) | 0x20;
2708 req->cdb[1] = req->cdb1;
2709 stl_be_p(&req->cdb[2], lba);
2710 req->cdb[6] = req->group_number;
2711 stw_be_p(&req->cdb[7], nb_logical_blocks);
2713 io_header->cmd_len = 10;
2714 } else if ((req->cmd >> 5) != 4 && lba <= 0xffffffffULL) {
2716 req->cdb[0] = (req->cmd & 0x1f) | 0xA0;
2717 req->cdb[1] = req->cdb1;
2718 stl_be_p(&req->cdb[2], lba);
2719 stl_be_p(&req->cdb[6], nb_logical_blocks);
2720 req->cdb[10] = req->group_number;
2722 io_header->cmd_len = 12;
2725 req->cdb[0] = (req->cmd & 0x1f) | 0x80;
2726 req->cdb[1] = req->cdb1;
2727 stq_be_p(&req->cdb[2], lba);
2728 stl_be_p(&req->cdb[10], nb_logical_blocks);
2729 req->cdb[14] = req->group_number;
2731 io_header->cmd_len = 16;
2734 /* The rest is as in scsi-generic.c. */
2735 io_header->mx_sb_len = sizeof(r->req.sense);
2736 io_header->sbp = r->req.sense;
2737 io_header->timeout = UINT_MAX;
2738 io_header->usr_ptr = r;
2739 io_header->flags |= SG_FLAG_DIRECT_IO;
2741 aiocb = blk_aio_ioctl(s->qdev.conf.blk, SG_IO, io_header, cb, opaque);
2742 assert(aiocb != NULL);
2746 static bool scsi_block_no_fua(SCSICommand *cmd)
2751 static BlockAIOCB *scsi_block_dma_readv(int64_t offset,
2753 BlockCompletionFunc *cb, void *cb_opaque,
2756 SCSIBlockReq *r = opaque;
2757 return scsi_block_do_sgio(r, offset, iov,
2758 SG_DXFER_FROM_DEV, cb, cb_opaque);
2761 static BlockAIOCB *scsi_block_dma_writev(int64_t offset,
2763 BlockCompletionFunc *cb, void *cb_opaque,
2766 SCSIBlockReq *r = opaque;
2767 return scsi_block_do_sgio(r, offset, iov,
2768 SG_DXFER_TO_DEV, cb, cb_opaque);
2771 static bool scsi_block_is_passthrough(SCSIDiskState *s, uint8_t *buf)
2777 /* Check if BYTCHK == 0x01 (data-out buffer contains data
2778 * for the number of logical blocks specified in the length
2779 * field). For other modes, do not use scatter/gather operation.
2781 if ((buf[1] & 6) == 2) {
2794 case WRITE_VERIFY_10:
2795 case WRITE_VERIFY_12:
2796 case WRITE_VERIFY_16:
2797 /* MMC writing cannot be done via DMA helpers, because it sometimes
2798 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2799 * We might use scsi_block_dma_reqops as long as no writing commands are
2800 * seen, but performance usually isn't paramount on optical media. So,
2801 * just make scsi-block operate the same as scsi-generic for them.
2803 if (s->qdev.type != TYPE_ROM) {
2816 static int32_t scsi_block_dma_command(SCSIRequest *req, uint8_t *buf)
2818 SCSIBlockReq *r = (SCSIBlockReq *)req;
2819 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
2821 r->cmd = req->cmd.buf[0];
2822 switch (r->cmd >> 5) {
2825 r->cdb1 = r->group_number = 0;
2829 r->cdb1 = req->cmd.buf[1];
2830 r->group_number = req->cmd.buf[6];
2834 r->cdb1 = req->cmd.buf[1];
2835 r->group_number = req->cmd.buf[10];
2839 r->cdb1 = req->cmd.buf[1];
2840 r->group_number = req->cmd.buf[14];
2846 /* Protection information is not supported. For SCSI versions 2 and
2847 * older (as determined by snooping the guest's INQUIRY commands),
2848 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2850 if (s->qdev.scsi_version > 2 && (req->cmd.buf[1] & 0xe0)) {
2851 scsi_check_condition(&r->req, SENSE_CODE(INVALID_FIELD));
2855 r->req.status = &r->io_header.status;
2856 return scsi_disk_dma_command(req, buf);
2859 static const SCSIReqOps scsi_block_dma_reqops = {
2860 .size = sizeof(SCSIBlockReq),
2861 .free_req = scsi_free_request,
2862 .send_command = scsi_block_dma_command,
2863 .read_data = scsi_read_data,
2864 .write_data = scsi_write_data,
2865 .get_buf = scsi_get_buf,
2866 .load_request = scsi_disk_load_request,
2867 .save_request = scsi_disk_save_request,
2870 static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
2871 uint32_t lun, uint8_t *buf,
2874 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2876 if (scsi_block_is_passthrough(s, buf)) {
2877 return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
2880 return scsi_req_alloc(&scsi_block_dma_reqops, &s->qdev, tag, lun,
2885 static int scsi_block_parse_cdb(SCSIDevice *d, SCSICommand *cmd,
2886 uint8_t *buf, void *hba_private)
2888 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2890 if (scsi_block_is_passthrough(s, buf)) {
2891 return scsi_bus_parse_cdb(&s->qdev, cmd, buf, hba_private);
2893 return scsi_req_parse_cdb(&s->qdev, cmd, buf);
2900 BlockAIOCB *scsi_dma_readv(int64_t offset, QEMUIOVector *iov,
2901 BlockCompletionFunc *cb, void *cb_opaque,
2904 SCSIDiskReq *r = opaque;
2905 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2906 return blk_aio_preadv(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque);
2910 BlockAIOCB *scsi_dma_writev(int64_t offset, QEMUIOVector *iov,
2911 BlockCompletionFunc *cb, void *cb_opaque,
2914 SCSIDiskReq *r = opaque;
2915 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2916 return blk_aio_pwritev(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque);
2919 static void scsi_disk_base_class_initfn(ObjectClass *klass, void *data)
2921 DeviceClass *dc = DEVICE_CLASS(klass);
2922 SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass);
2924 dc->fw_name = "disk";
2925 dc->reset = scsi_disk_reset;
2926 sdc->dma_readv = scsi_dma_readv;
2927 sdc->dma_writev = scsi_dma_writev;
2928 sdc->need_fua_emulation = scsi_is_cmd_fua;
2931 static const TypeInfo scsi_disk_base_info = {
2932 .name = TYPE_SCSI_DISK_BASE,
2933 .parent = TYPE_SCSI_DEVICE,
2934 .class_init = scsi_disk_base_class_initfn,
2935 .instance_size = sizeof(SCSIDiskState),
2936 .class_size = sizeof(SCSIDiskClass),
2940 #define DEFINE_SCSI_DISK_PROPERTIES() \
2941 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2942 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \
2943 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2944 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2945 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2946 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2948 static Property scsi_hd_properties[] = {
2949 DEFINE_SCSI_DISK_PROPERTIES(),
2950 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2951 SCSI_DISK_F_REMOVABLE, false),
2952 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2953 SCSI_DISK_F_DPOFUA, false),
2954 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
2955 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
2956 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
2957 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
2958 DEFAULT_MAX_UNMAP_SIZE),
2959 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
2960 DEFAULT_MAX_IO_SIZE),
2961 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0),
2962 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
2964 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
2965 DEFINE_PROP_END_OF_LIST(),
2968 static const VMStateDescription vmstate_scsi_disk_state = {
2969 .name = "scsi-disk",
2971 .minimum_version_id = 1,
2972 .fields = (VMStateField[]) {
2973 VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
2974 VMSTATE_BOOL(media_changed, SCSIDiskState),
2975 VMSTATE_BOOL(media_event, SCSIDiskState),
2976 VMSTATE_BOOL(eject_request, SCSIDiskState),
2977 VMSTATE_BOOL(tray_open, SCSIDiskState),
2978 VMSTATE_BOOL(tray_locked, SCSIDiskState),
2979 VMSTATE_END_OF_LIST()
2983 static void scsi_hd_class_initfn(ObjectClass *klass, void *data)
2985 DeviceClass *dc = DEVICE_CLASS(klass);
2986 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2988 sc->realize = scsi_hd_realize;
2989 sc->alloc_req = scsi_new_request;
2990 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2991 dc->desc = "virtual SCSI disk";
2992 dc->props = scsi_hd_properties;
2993 dc->vmsd = &vmstate_scsi_disk_state;
2996 static const TypeInfo scsi_hd_info = {
2998 .parent = TYPE_SCSI_DISK_BASE,
2999 .class_init = scsi_hd_class_initfn,
3002 static Property scsi_cd_properties[] = {
3003 DEFINE_SCSI_DISK_PROPERTIES(),
3004 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
3005 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
3006 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
3007 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3008 DEFAULT_MAX_IO_SIZE),
3009 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3011 DEFINE_PROP_END_OF_LIST(),
3014 static void scsi_cd_class_initfn(ObjectClass *klass, void *data)
3016 DeviceClass *dc = DEVICE_CLASS(klass);
3017 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3019 sc->realize = scsi_cd_realize;
3020 sc->alloc_req = scsi_new_request;
3021 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
3022 dc->desc = "virtual SCSI CD-ROM";
3023 dc->props = scsi_cd_properties;
3024 dc->vmsd = &vmstate_scsi_disk_state;
3027 static const TypeInfo scsi_cd_info = {
3029 .parent = TYPE_SCSI_DISK_BASE,
3030 .class_init = scsi_cd_class_initfn,
3034 static Property scsi_block_properties[] = {
3035 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \
3036 DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.blk),
3037 DEFINE_PROP_BOOL("share-rw", SCSIDiskState, qdev.conf.share_rw, false),
3038 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0),
3039 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
3040 DEFAULT_MAX_UNMAP_SIZE),
3041 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3042 DEFAULT_MAX_IO_SIZE),
3043 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3045 DEFINE_PROP_END_OF_LIST(),
3048 static void scsi_block_class_initfn(ObjectClass *klass, void *data)
3050 DeviceClass *dc = DEVICE_CLASS(klass);
3051 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3052 SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass);
3054 sc->realize = scsi_block_realize;
3055 sc->alloc_req = scsi_block_new_request;
3056 sc->parse_cdb = scsi_block_parse_cdb;
3057 sdc->dma_readv = scsi_block_dma_readv;
3058 sdc->dma_writev = scsi_block_dma_writev;
3059 sdc->need_fua_emulation = scsi_block_no_fua;
3060 dc->desc = "SCSI block device passthrough";
3061 dc->props = scsi_block_properties;
3062 dc->vmsd = &vmstate_scsi_disk_state;
3065 static const TypeInfo scsi_block_info = {
3066 .name = "scsi-block",
3067 .parent = TYPE_SCSI_DISK_BASE,
3068 .class_init = scsi_block_class_initfn,
3072 static Property scsi_disk_properties[] = {
3073 DEFINE_SCSI_DISK_PROPERTIES(),
3074 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
3075 SCSI_DISK_F_REMOVABLE, false),
3076 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
3077 SCSI_DISK_F_DPOFUA, false),
3078 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
3079 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
3080 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
3081 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
3082 DEFAULT_MAX_UNMAP_SIZE),
3083 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3084 DEFAULT_MAX_IO_SIZE),
3085 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3087 DEFINE_PROP_END_OF_LIST(),
3090 static void scsi_disk_class_initfn(ObjectClass *klass, void *data)
3092 DeviceClass *dc = DEVICE_CLASS(klass);
3093 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3095 sc->realize = scsi_disk_realize;
3096 sc->alloc_req = scsi_new_request;
3097 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
3098 dc->fw_name = "disk";
3099 dc->desc = "virtual SCSI disk or CD-ROM (legacy)";
3100 dc->reset = scsi_disk_reset;
3101 dc->props = scsi_disk_properties;
3102 dc->vmsd = &vmstate_scsi_disk_state;
3105 static const TypeInfo scsi_disk_info = {
3106 .name = "scsi-disk",
3107 .parent = TYPE_SCSI_DISK_BASE,
3108 .class_init = scsi_disk_class_initfn,
3111 static void scsi_disk_register_types(void)
3113 type_register_static(&scsi_disk_base_info);
3114 type_register_static(&scsi_hd_info);
3115 type_register_static(&scsi_cd_info);
3117 type_register_static(&scsi_block_info);
3119 type_register_static(&scsi_disk_info);
3122 type_init(scsi_disk_register_types)