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 "qemu/units.h"
33 #include "qapi/error.h"
34 #include "qemu/error-report.h"
35 #include "hw/scsi/scsi.h"
36 #include "scsi/constants.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/block-backend.h"
39 #include "sysemu/blockdev.h"
40 #include "hw/block/block.h"
41 #include "sysemu/dma.h"
42 #include "qemu/cutils.h"
48 #define SCSI_WRITE_SAME_MAX (512 * KiB)
49 #define SCSI_DMA_BUF_SIZE (128 * KiB)
50 #define SCSI_MAX_INQUIRY_LEN 256
51 #define SCSI_MAX_MODE_LEN 256
53 #define DEFAULT_DISCARD_GRANULARITY (4 * KiB)
54 #define DEFAULT_MAX_UNMAP_SIZE (1 * GiB)
55 #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */
57 #define TYPE_SCSI_DISK_BASE "scsi-disk-base"
59 #define SCSI_DISK_BASE(obj) \
60 OBJECT_CHECK(SCSIDiskState, (obj), TYPE_SCSI_DISK_BASE)
61 #define SCSI_DISK_BASE_CLASS(klass) \
62 OBJECT_CLASS_CHECK(SCSIDiskClass, (klass), TYPE_SCSI_DISK_BASE)
63 #define SCSI_DISK_BASE_GET_CLASS(obj) \
64 OBJECT_GET_CLASS(SCSIDiskClass, (obj), TYPE_SCSI_DISK_BASE)
66 typedef struct SCSIDiskClass {
67 SCSIDeviceClass parent_class;
69 DMAIOFunc *dma_writev;
70 bool (*need_fua_emulation)(SCSICommand *cmd);
73 typedef struct SCSIDiskReq {
75 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
77 uint32_t sector_count;
80 bool need_fua_emulation;
84 unsigned char *status;
87 #define SCSI_DISK_F_REMOVABLE 0
88 #define SCSI_DISK_F_DPOFUA 1
89 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2
91 typedef struct SCSIDiskState
99 uint64_t max_unmap_size;
100 uint64_t max_io_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 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
131 r->req.tag, sense.key, sense.asc, sense.ascq);
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(void * opaque, int ret)
307 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
308 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
311 assert(r->req.aiocb != NULL);
313 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
314 if (scsi_disk_req_check_error(r, ret, true)) {
318 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
319 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size);
321 n = r->qiov.size / 512;
323 r->sector_count -= n;
324 scsi_req_data(&r->req, r->qiov.size);
327 scsi_req_unref(&r->req);
328 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
331 /* Actually issue a read to the block device. */
332 static void scsi_do_read(SCSIDiskReq *r, int ret)
334 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
335 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
337 assert (r->req.aiocb == NULL);
338 if (scsi_disk_req_check_error(r, ret, false)) {
342 /* The request is used as the AIO opaque value, so add a ref. */
343 scsi_req_ref(&r->req);
346 dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_READ);
347 r->req.resid -= r->req.sg->size;
348 r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
349 r->req.sg, r->sector << BDRV_SECTOR_BITS,
351 sdc->dma_readv, r, scsi_dma_complete, r,
352 DMA_DIRECTION_FROM_DEVICE);
354 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
355 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
356 r->qiov.size, BLOCK_ACCT_READ);
357 r->req.aiocb = sdc->dma_readv(r->sector << BDRV_SECTOR_BITS, &r->qiov,
358 scsi_read_complete, r, r);
362 scsi_req_unref(&r->req);
365 static void scsi_do_read_cb(void *opaque, int ret)
367 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
368 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
370 assert (r->req.aiocb != NULL);
373 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
375 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
377 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
379 scsi_do_read(opaque, ret);
380 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
383 /* Read more data from scsi device into buffer. */
384 static void scsi_read_data(SCSIRequest *req)
386 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
387 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
390 DPRINTF("Read sector_count=%d\n", r->sector_count);
391 if (r->sector_count == 0) {
392 /* This also clears the sense buffer for REQUEST SENSE. */
393 scsi_req_complete(&r->req, GOOD);
397 /* No data transfer may already be in progress */
398 assert(r->req.aiocb == NULL);
400 /* The request is used as the AIO opaque value, so add a ref. */
401 scsi_req_ref(&r->req);
402 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
403 DPRINTF("Data transfer direction invalid\n");
404 scsi_read_complete(r, -EINVAL);
408 if (!blk_is_available(req->dev->conf.blk)) {
409 scsi_read_complete(r, -ENOMEDIUM);
415 if (first && r->need_fua_emulation) {
416 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
418 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_do_read_cb, r);
425 * scsi_handle_rw_error has two return values. False means that the error
426 * must be ignored, true means that the error has been processed and the
427 * caller should not do anything else for this request. Note that
428 * scsi_handle_rw_error always manages its reference counts, independent
429 * of the return value.
431 static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed)
433 bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV);
434 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
435 BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk,
438 if (action == BLOCK_ERROR_ACTION_REPORT) {
440 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
444 /* The command has run, no need to fake sense. */
445 assert(r->status && *r->status);
446 scsi_req_complete(&r->req, *r->status);
449 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
452 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
455 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
458 scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED));
461 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
466 assert(r->status && *r->status);
467 error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense));
469 if (error == ECANCELED || error == EAGAIN || error == ENOTCONN ||
471 /* These errors are handled by guest. */
472 scsi_req_complete(&r->req, *r->status);
477 blk_error_action(s->qdev.conf.blk, action, is_read, error);
478 if (action == BLOCK_ERROR_ACTION_STOP) {
479 scsi_req_retry(&r->req);
481 return action != BLOCK_ERROR_ACTION_IGNORE;
484 static void scsi_write_complete_noio(SCSIDiskReq *r, int ret)
488 assert (r->req.aiocb == NULL);
489 if (scsi_disk_req_check_error(r, ret, false)) {
493 n = r->qiov.size / 512;
495 r->sector_count -= n;
496 if (r->sector_count == 0) {
497 scsi_write_do_fua(r);
500 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
501 DPRINTF("Write complete tag=0x%x more=%zd\n", r->req.tag, r->qiov.size);
502 scsi_req_data(&r->req, r->qiov.size);
506 scsi_req_unref(&r->req);
509 static void scsi_write_complete(void * opaque, int ret)
511 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
512 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
514 assert (r->req.aiocb != NULL);
517 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
519 block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct);
521 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
523 scsi_write_complete_noio(r, ret);
524 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
527 static void scsi_write_data(SCSIRequest *req)
529 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
530 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
531 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
533 /* No data transfer may already be in progress */
534 assert(r->req.aiocb == NULL);
536 /* The request is used as the AIO opaque value, so add a ref. */
537 scsi_req_ref(&r->req);
538 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
539 DPRINTF("Data transfer direction invalid\n");
540 scsi_write_complete_noio(r, -EINVAL);
544 if (!r->req.sg && !r->qiov.size) {
545 /* Called for the first time. Ask the driver to send us more data. */
547 scsi_write_complete_noio(r, 0);
550 if (!blk_is_available(req->dev->conf.blk)) {
551 scsi_write_complete_noio(r, -ENOMEDIUM);
555 if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 ||
556 r->req.cmd.buf[0] == VERIFY_16) {
558 scsi_dma_complete_noio(r, 0);
560 scsi_write_complete_noio(r, 0);
566 dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_WRITE);
567 r->req.resid -= r->req.sg->size;
568 r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
569 r->req.sg, r->sector << BDRV_SECTOR_BITS,
571 sdc->dma_writev, r, scsi_dma_complete, r,
572 DMA_DIRECTION_TO_DEVICE);
574 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
575 r->qiov.size, BLOCK_ACCT_WRITE);
576 r->req.aiocb = sdc->dma_writev(r->sector << BDRV_SECTOR_BITS, &r->qiov,
577 scsi_write_complete, r, r);
581 /* Return a pointer to the data buffer. */
582 static uint8_t *scsi_get_buf(SCSIRequest *req)
584 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
586 return (uint8_t *)r->iov.iov_base;
589 int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf)
591 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
592 uint8_t page_code = req->cmd.buf[2];
593 int start, buflen = 0;
595 outbuf[buflen++] = s->qdev.type & 0x1f;
596 outbuf[buflen++] = page_code;
597 outbuf[buflen++] = 0x00;
598 outbuf[buflen++] = 0x00;
602 case 0x00: /* Supported page codes, mandatory */
604 DPRINTF("Inquiry EVPD[Supported pages] "
605 "buffer size %zd\n", req->cmd.xfer);
606 outbuf[buflen++] = 0x00; /* list of supported pages (this page) */
608 outbuf[buflen++] = 0x80; /* unit serial number */
610 outbuf[buflen++] = 0x83; /* device identification */
611 if (s->qdev.type == TYPE_DISK) {
612 outbuf[buflen++] = 0xb0; /* block limits */
613 outbuf[buflen++] = 0xb1; /* block device characteristics */
614 outbuf[buflen++] = 0xb2; /* thin provisioning */
618 case 0x80: /* Device serial number, optional */
623 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
627 l = strlen(s->serial);
632 DPRINTF("Inquiry EVPD[Serial number] "
633 "buffer size %zd\n", req->cmd.xfer);
634 memcpy(outbuf + buflen, s->serial, l);
639 case 0x83: /* Device identification page, mandatory */
641 const char *str = s->serial ?: blk_name(s->qdev.conf.blk);
642 int max_len = s->serial ? 20 : 255 - 8;
643 int id_len = strlen(str);
645 if (id_len > max_len) {
648 DPRINTF("Inquiry EVPD[Device identification] "
649 "buffer size %zd\n", req->cmd.xfer);
651 outbuf[buflen++] = 0x2; /* ASCII */
652 outbuf[buflen++] = 0; /* not officially assigned */
653 outbuf[buflen++] = 0; /* reserved */
654 outbuf[buflen++] = id_len; /* length of data following */
655 memcpy(outbuf + buflen, str, id_len);
659 outbuf[buflen++] = 0x1; /* Binary */
660 outbuf[buflen++] = 0x3; /* NAA */
661 outbuf[buflen++] = 0; /* reserved */
662 outbuf[buflen++] = 8;
663 stq_be_p(&outbuf[buflen], s->qdev.wwn);
667 if (s->qdev.port_wwn) {
668 outbuf[buflen++] = 0x61; /* SAS / Binary */
669 outbuf[buflen++] = 0x93; /* PIV / Target port / NAA */
670 outbuf[buflen++] = 0; /* reserved */
671 outbuf[buflen++] = 8;
672 stq_be_p(&outbuf[buflen], s->qdev.port_wwn);
677 outbuf[buflen++] = 0x61; /* SAS / Binary */
679 /* PIV/Target port/relative target port */
680 outbuf[buflen++] = 0x94;
682 outbuf[buflen++] = 0; /* reserved */
683 outbuf[buflen++] = 4;
684 stw_be_p(&outbuf[buflen + 2], s->port_index);
689 case 0xb0: /* block limits */
691 unsigned int unmap_sectors =
692 s->qdev.conf.discard_granularity / s->qdev.blocksize;
693 unsigned int min_io_size =
694 s->qdev.conf.min_io_size / s->qdev.blocksize;
695 unsigned int opt_io_size =
696 s->qdev.conf.opt_io_size / s->qdev.blocksize;
697 unsigned int max_unmap_sectors =
698 s->max_unmap_size / s->qdev.blocksize;
699 unsigned int max_io_sectors =
700 s->max_io_size / s->qdev.blocksize;
702 if (s->qdev.type == TYPE_ROM) {
703 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
707 if (s->qdev.type == TYPE_DISK) {
708 int max_transfer_blk = blk_get_max_transfer(s->qdev.conf.blk);
709 int max_io_sectors_blk =
710 max_transfer_blk / s->qdev.blocksize;
713 MIN_NON_ZERO(max_io_sectors_blk, max_io_sectors);
715 /* min_io_size and opt_io_size can't be greater than
718 min_io_size = MIN(min_io_size, max_io_sectors);
721 opt_io_size = MIN(opt_io_size, max_io_sectors);
724 /* required VPD size with unmap support */
726 memset(outbuf + 4, 0, buflen - 4);
728 outbuf[4] = 0x1; /* wsnz */
730 /* optimal transfer length granularity */
731 outbuf[6] = (min_io_size >> 8) & 0xff;
732 outbuf[7] = min_io_size & 0xff;
734 /* maximum transfer length */
735 outbuf[8] = (max_io_sectors >> 24) & 0xff;
736 outbuf[9] = (max_io_sectors >> 16) & 0xff;
737 outbuf[10] = (max_io_sectors >> 8) & 0xff;
738 outbuf[11] = max_io_sectors & 0xff;
740 /* optimal transfer length */
741 outbuf[12] = (opt_io_size >> 24) & 0xff;
742 outbuf[13] = (opt_io_size >> 16) & 0xff;
743 outbuf[14] = (opt_io_size >> 8) & 0xff;
744 outbuf[15] = opt_io_size & 0xff;
746 /* max unmap LBA count, default is 1GB */
747 outbuf[20] = (max_unmap_sectors >> 24) & 0xff;
748 outbuf[21] = (max_unmap_sectors >> 16) & 0xff;
749 outbuf[22] = (max_unmap_sectors >> 8) & 0xff;
750 outbuf[23] = max_unmap_sectors & 0xff;
752 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header */
758 /* optimal unmap granularity */
759 outbuf[28] = (unmap_sectors >> 24) & 0xff;
760 outbuf[29] = (unmap_sectors >> 16) & 0xff;
761 outbuf[30] = (unmap_sectors >> 8) & 0xff;
762 outbuf[31] = unmap_sectors & 0xff;
764 /* max write same size */
770 outbuf[40] = (max_io_sectors >> 24) & 0xff;
771 outbuf[41] = (max_io_sectors >> 16) & 0xff;
772 outbuf[42] = (max_io_sectors >> 8) & 0xff;
773 outbuf[43] = max_io_sectors & 0xff;
776 case 0xb1: /* block device characteristics */
779 outbuf[4] = (s->rotation_rate >> 8) & 0xff;
780 outbuf[5] = s->rotation_rate & 0xff;
785 case 0xb2: /* thin provisioning */
789 outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */
790 outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1;
798 assert(buflen - start <= 255);
799 outbuf[start - 1] = buflen - start;
803 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
805 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
808 if (req->cmd.buf[1] & 0x1) {
809 /* Vital product data */
810 return scsi_disk_emulate_vpd_page(req, outbuf);
813 /* Standard INQUIRY data */
814 if (req->cmd.buf[2] != 0) {
819 buflen = req->cmd.xfer;
820 if (buflen > SCSI_MAX_INQUIRY_LEN) {
821 buflen = SCSI_MAX_INQUIRY_LEN;
824 outbuf[0] = s->qdev.type & 0x1f;
825 outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0;
827 strpadcpy((char *) &outbuf[16], 16, s->product, ' ');
828 strpadcpy((char *) &outbuf[8], 8, s->vendor, ' ');
830 memset(&outbuf[32], 0, 4);
831 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
833 * We claim conformance to SPC-3, which is required for guests
834 * to ask for modern features like READ CAPACITY(16) or the
835 * block characteristics VPD page by default. Not all of SPC-3
836 * is actually implemented, but we're good enough.
838 outbuf[2] = s->qdev.default_scsi_version;
839 outbuf[3] = 2 | 0x10; /* Format 2, HiSup */
842 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
844 /* If the allocation length of CDB is too small,
845 the additional length is not adjusted */
849 /* Sync data transfer and TCQ. */
850 outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0);
854 static inline bool media_is_dvd(SCSIDiskState *s)
857 if (s->qdev.type != TYPE_ROM) {
860 if (!blk_is_available(s->qdev.conf.blk)) {
863 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
864 return nb_sectors > CD_MAX_SECTORS;
867 static inline bool media_is_cd(SCSIDiskState *s)
870 if (s->qdev.type != TYPE_ROM) {
873 if (!blk_is_available(s->qdev.conf.blk)) {
876 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
877 return nb_sectors <= CD_MAX_SECTORS;
880 static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r,
883 uint8_t type = r->req.cmd.buf[1] & 7;
885 if (s->qdev.type != TYPE_ROM) {
889 /* Types 1/2 are only defined for Blu-Ray. */
891 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
895 memset(outbuf, 0, 34);
897 outbuf[2] = 0xe; /* last session complete, disc finalized */
898 outbuf[3] = 1; /* first track on disc */
899 outbuf[4] = 1; /* # of sessions */
900 outbuf[5] = 1; /* first track of last session */
901 outbuf[6] = 1; /* last track of last session */
902 outbuf[7] = 0x20; /* unrestricted use */
903 outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */
904 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
905 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
906 /* 24-31: disc bar code */
907 /* 32: disc application code */
908 /* 33: number of OPC tables */
913 static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
916 static const int rds_caps_size[5] = {
923 uint8_t media = r->req.cmd.buf[1];
924 uint8_t layer = r->req.cmd.buf[6];
925 uint8_t format = r->req.cmd.buf[7];
928 if (s->qdev.type != TYPE_ROM) {
932 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
936 if (format != 0xff) {
937 if (!blk_is_available(s->qdev.conf.blk)) {
938 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
941 if (media_is_cd(s)) {
942 scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT));
945 if (format >= ARRAY_SIZE(rds_caps_size)) {
948 size = rds_caps_size[format];
949 memset(outbuf, 0, size);
954 /* Physical format information */
959 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
961 outbuf[4] = 1; /* DVD-ROM, part version 1 */
962 outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
963 outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
964 outbuf[7] = 0; /* default densities */
966 stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */
967 stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */
971 case 0x01: /* DVD copyright information, all zeros */
974 case 0x03: /* BCA information - invalid field for no BCA info */
977 case 0x04: /* DVD disc manufacturing information, all zeros */
980 case 0xff: { /* List capabilities */
983 for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) {
984 if (!rds_caps_size[i]) {
988 outbuf[size + 1] = 0x40; /* Not writable, readable */
989 stw_be_p(&outbuf[size + 2], rds_caps_size[i]);
999 /* Size of buffer, not including 2 byte size field */
1000 stw_be_p(outbuf, size - 2);
1007 static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
1009 uint8_t event_code, media_status;
1013 media_status = MS_TRAY_OPEN;
1014 } else if (blk_is_inserted(s->qdev.conf.blk)) {
1015 media_status = MS_MEDIA_PRESENT;
1018 /* Event notification descriptor */
1019 event_code = MEC_NO_CHANGE;
1020 if (media_status != MS_TRAY_OPEN) {
1021 if (s->media_event) {
1022 event_code = MEC_NEW_MEDIA;
1023 s->media_event = false;
1024 } else if (s->eject_request) {
1025 event_code = MEC_EJECT_REQUESTED;
1026 s->eject_request = false;
1030 outbuf[0] = event_code;
1031 outbuf[1] = media_status;
1033 /* These fields are reserved, just clear them. */
1039 static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r,
1043 uint8_t *buf = r->req.cmd.buf;
1044 uint8_t notification_class_request = buf[4];
1045 if (s->qdev.type != TYPE_ROM) {
1048 if ((buf[1] & 1) == 0) {
1054 outbuf[0] = outbuf[1] = 0;
1055 outbuf[3] = 1 << GESN_MEDIA; /* supported events */
1056 if (notification_class_request & (1 << GESN_MEDIA)) {
1057 outbuf[2] = GESN_MEDIA;
1058 size += scsi_event_status_media(s, &outbuf[size]);
1062 stw_be_p(outbuf, size - 4);
1066 static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
1070 if (s->qdev.type != TYPE_ROM) {
1074 if (media_is_dvd(s)) {
1075 current = MMC_PROFILE_DVD_ROM;
1076 } else if (media_is_cd(s)) {
1077 current = MMC_PROFILE_CD_ROM;
1079 current = MMC_PROFILE_NONE;
1082 memset(outbuf, 0, 40);
1083 stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */
1084 stw_be_p(&outbuf[6], current);
1085 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
1086 outbuf[10] = 0x03; /* persistent, current */
1087 outbuf[11] = 8; /* two profiles */
1088 stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM);
1089 outbuf[14] = (current == MMC_PROFILE_DVD_ROM);
1090 stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM);
1091 outbuf[18] = (current == MMC_PROFILE_CD_ROM);
1092 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
1093 stw_be_p(&outbuf[20], 1);
1094 outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */
1096 stl_be_p(&outbuf[24], 1); /* SCSI */
1097 outbuf[28] = 1; /* DBE = 1, mandatory */
1098 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
1099 stw_be_p(&outbuf[32], 3);
1100 outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */
1102 outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1103 /* TODO: Random readable, CD read, DVD read, drive serial number,
1108 static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf)
1110 if (s->qdev.type != TYPE_ROM) {
1113 memset(outbuf, 0, 8);
1114 outbuf[5] = 1; /* CD-ROM */
1118 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
1121 static const int mode_sense_valid[0x3f] = {
1122 [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK),
1123 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK),
1124 [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1125 [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1126 [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM),
1127 [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM),
1130 uint8_t *p = *p_outbuf + 2;
1133 if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
1138 * If Changeable Values are requested, a mask denoting those mode parameters
1139 * that are changeable shall be returned. As we currently don't support
1140 * parameter changes via MODE_SELECT all bits are returned set to zero.
1141 * The buffer was already menset to zero by the caller of this function.
1143 * The offsets here are off by two compared to the descriptions in the
1144 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1145 * but it is done so that offsets are consistent within our implementation
1146 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1147 * 2-byte and 4-byte headers.
1150 case MODE_PAGE_HD_GEOMETRY:
1152 if (page_control == 1) { /* Changeable Values */
1155 /* if a geometry hint is available, use it */
1156 p[0] = (s->qdev.conf.cyls >> 16) & 0xff;
1157 p[1] = (s->qdev.conf.cyls >> 8) & 0xff;
1158 p[2] = s->qdev.conf.cyls & 0xff;
1159 p[3] = s->qdev.conf.heads & 0xff;
1160 /* Write precomp start cylinder, disabled */
1161 p[4] = (s->qdev.conf.cyls >> 16) & 0xff;
1162 p[5] = (s->qdev.conf.cyls >> 8) & 0xff;
1163 p[6] = s->qdev.conf.cyls & 0xff;
1164 /* Reduced current start cylinder, disabled */
1165 p[7] = (s->qdev.conf.cyls >> 16) & 0xff;
1166 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1167 p[9] = s->qdev.conf.cyls & 0xff;
1168 /* Device step rate [ns], 200ns */
1171 /* Landing zone cylinder */
1175 /* Medium rotation rate [rpm], 5400 rpm */
1176 p[18] = (5400 >> 8) & 0xff;
1177 p[19] = 5400 & 0xff;
1180 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
1182 if (page_control == 1) { /* Changeable Values */
1185 /* Transfer rate [kbit/s], 5Mbit/s */
1188 /* if a geometry hint is available, use it */
1189 p[2] = s->qdev.conf.heads & 0xff;
1190 p[3] = s->qdev.conf.secs & 0xff;
1191 p[4] = s->qdev.blocksize >> 8;
1192 p[6] = (s->qdev.conf.cyls >> 8) & 0xff;
1193 p[7] = s->qdev.conf.cyls & 0xff;
1194 /* Write precomp start cylinder, disabled */
1195 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1196 p[9] = s->qdev.conf.cyls & 0xff;
1197 /* Reduced current start cylinder, disabled */
1198 p[10] = (s->qdev.conf.cyls >> 8) & 0xff;
1199 p[11] = s->qdev.conf.cyls & 0xff;
1200 /* Device step rate [100us], 100us */
1203 /* Device step pulse width [us], 1us */
1205 /* Device head settle delay [100us], 100us */
1208 /* Motor on delay [0.1s], 0.1s */
1210 /* Motor off delay [0.1s], 0.1s */
1212 /* Medium rotation rate [rpm], 5400 rpm */
1213 p[26] = (5400 >> 8) & 0xff;
1214 p[27] = 5400 & 0xff;
1217 case MODE_PAGE_CACHING:
1219 if (page_control == 1 || /* Changeable Values */
1220 blk_enable_write_cache(s->qdev.conf.blk)) {
1225 case MODE_PAGE_R_W_ERROR:
1227 if (page_control == 1) { /* Changeable Values */
1230 p[0] = 0x80; /* Automatic Write Reallocation Enabled */
1231 if (s->qdev.type == TYPE_ROM) {
1232 p[1] = 0x20; /* Read Retry Count */
1236 case MODE_PAGE_AUDIO_CTL:
1240 case MODE_PAGE_CAPABILITIES:
1242 if (page_control == 1) { /* Changeable Values */
1246 p[0] = 0x3b; /* CD-R & CD-RW read */
1247 p[1] = 0; /* Writing not supported */
1248 p[2] = 0x7f; /* Audio, composite, digital out,
1249 mode 2 form 1&2, multi session */
1250 p[3] = 0xff; /* CD DA, DA accurate, RW supported,
1251 RW corrected, C2 errors, ISRC,
1253 p[4] = 0x2d | (s->tray_locked ? 2 : 0);
1254 /* Locking supported, jumper present, eject, tray */
1255 p[5] = 0; /* no volume & mute control, no
1257 p[6] = (50 * 176) >> 8; /* 50x read speed */
1258 p[7] = (50 * 176) & 0xff;
1259 p[8] = 2 >> 8; /* Two volume levels */
1261 p[10] = 2048 >> 8; /* 2M buffer */
1262 p[11] = 2048 & 0xff;
1263 p[12] = (16 * 176) >> 8; /* 16x read speed current */
1264 p[13] = (16 * 176) & 0xff;
1265 p[16] = (16 * 176) >> 8; /* 16x write speed */
1266 p[17] = (16 * 176) & 0xff;
1267 p[18] = (16 * 176) >> 8; /* 16x write speed current */
1268 p[19] = (16 * 176) & 0xff;
1275 assert(length < 256);
1276 (*p_outbuf)[0] = page;
1277 (*p_outbuf)[1] = length;
1278 *p_outbuf += length + 2;
1282 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
1284 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1285 uint64_t nb_sectors;
1287 int page, buflen, ret, page_control;
1289 uint8_t dev_specific_param;
1291 dbd = (r->req.cmd.buf[1] & 0x8) != 0;
1292 page = r->req.cmd.buf[2] & 0x3f;
1293 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
1294 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1295 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
1296 memset(outbuf, 0, r->req.cmd.xfer);
1299 if (s->qdev.type == TYPE_DISK) {
1300 dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0;
1301 if (blk_is_read_only(s->qdev.conf.blk)) {
1302 dev_specific_param |= 0x80; /* Readonly. */
1305 /* MMC prescribes that CD/DVD drives have no block descriptors,
1306 * and defines no device-specific parameter. */
1307 dev_specific_param = 0x00;
1311 if (r->req.cmd.buf[0] == MODE_SENSE) {
1312 p[1] = 0; /* Default media type. */
1313 p[2] = dev_specific_param;
1314 p[3] = 0; /* Block descriptor length. */
1316 } else { /* MODE_SENSE_10 */
1317 p[2] = 0; /* Default media type. */
1318 p[3] = dev_specific_param;
1319 p[6] = p[7] = 0; /* Block descriptor length. */
1323 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
1324 if (!dbd && nb_sectors) {
1325 if (r->req.cmd.buf[0] == MODE_SENSE) {
1326 outbuf[3] = 8; /* Block descriptor length */
1327 } else { /* MODE_SENSE_10 */
1328 outbuf[7] = 8; /* Block descriptor length */
1330 nb_sectors /= (s->qdev.blocksize / 512);
1331 if (nb_sectors > 0xffffff) {
1334 p[0] = 0; /* media density code */
1335 p[1] = (nb_sectors >> 16) & 0xff;
1336 p[2] = (nb_sectors >> 8) & 0xff;
1337 p[3] = nb_sectors & 0xff;
1338 p[4] = 0; /* reserved */
1339 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
1340 p[6] = s->qdev.blocksize >> 8;
1345 if (page_control == 3) {
1347 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
1352 for (page = 0; page <= 0x3e; page++) {
1353 mode_sense_page(s, page, &p, page_control);
1356 ret = mode_sense_page(s, page, &p, page_control);
1362 buflen = p - outbuf;
1364 * The mode data length field specifies the length in bytes of the
1365 * following data that is available to be transferred. The mode data
1366 * length does not include itself.
1368 if (r->req.cmd.buf[0] == MODE_SENSE) {
1369 outbuf[0] = buflen - 1;
1370 } else { /* MODE_SENSE_10 */
1371 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1372 outbuf[1] = (buflen - 2) & 0xff;
1377 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1379 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1380 int start_track, format, msf, toclen;
1381 uint64_t nb_sectors;
1383 msf = req->cmd.buf[1] & 2;
1384 format = req->cmd.buf[2] & 0xf;
1385 start_track = req->cmd.buf[6];
1386 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
1387 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
1388 nb_sectors /= s->qdev.blocksize / 512;
1391 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
1394 /* multi session : only a single session defined */
1396 memset(outbuf, 0, 12);
1402 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
1410 static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
1412 SCSIRequest *req = &r->req;
1413 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1414 bool start = req->cmd.buf[4] & 1;
1415 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
1416 int pwrcnd = req->cmd.buf[4] & 0xf0;
1419 /* eject/load only happens for power condition == 0 */
1423 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) {
1424 if (!start && !s->tray_open && s->tray_locked) {
1425 scsi_check_condition(r,
1426 blk_is_inserted(s->qdev.conf.blk)
1427 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1428 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1432 if (s->tray_open != !start) {
1433 blk_eject(s->qdev.conf.blk, !start);
1434 s->tray_open = !start;
1440 static void scsi_disk_emulate_read_data(SCSIRequest *req)
1442 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1443 int buflen = r->iov.iov_len;
1446 DPRINTF("Read buf_len=%d\n", buflen);
1449 scsi_req_data(&r->req, buflen);
1453 /* This also clears the sense buffer for REQUEST SENSE. */
1454 scsi_req_complete(&r->req, GOOD);
1457 static int scsi_disk_check_mode_select(SCSIDiskState *s, int page,
1458 uint8_t *inbuf, int inlen)
1460 uint8_t mode_current[SCSI_MAX_MODE_LEN];
1461 uint8_t mode_changeable[SCSI_MAX_MODE_LEN];
1463 int len, expected_len, changeable_len, i;
1465 /* The input buffer does not include the page header, so it is
1468 expected_len = inlen + 2;
1469 if (expected_len > SCSI_MAX_MODE_LEN) {
1474 memset(mode_current, 0, inlen + 2);
1475 len = mode_sense_page(s, page, &p, 0);
1476 if (len < 0 || len != expected_len) {
1480 p = mode_changeable;
1481 memset(mode_changeable, 0, inlen + 2);
1482 changeable_len = mode_sense_page(s, page, &p, 1);
1483 assert(changeable_len == len);
1485 /* Check that unchangeable bits are the same as what MODE SENSE
1488 for (i = 2; i < len; i++) {
1489 if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) {
1496 static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p)
1499 case MODE_PAGE_CACHING:
1500 blk_set_enable_write_cache(s->qdev.conf.blk, (p[0] & 4) != 0);
1508 static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change)
1510 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1513 int page, subpage, page_len;
1515 /* Parse both possible formats for the mode page headers. */
1519 goto invalid_param_len;
1522 page_len = lduw_be_p(&p[2]);
1527 goto invalid_param_len;
1538 if (page_len > len) {
1539 goto invalid_param_len;
1543 if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) {
1547 scsi_disk_apply_mode_select(s, page, p);
1556 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1560 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1564 static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
1566 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1568 int cmd = r->req.cmd.buf[0];
1569 int len = r->req.cmd.xfer;
1570 int hdr_len = (cmd == MODE_SELECT ? 4 : 8);
1574 /* We only support PF=1, SP=0. */
1575 if ((r->req.cmd.buf[1] & 0x11) != 0x10) {
1579 if (len < hdr_len) {
1580 goto invalid_param_len;
1583 bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6]));
1587 goto invalid_param_len;
1589 if (bd_len != 0 && bd_len != 8) {
1596 /* Ensure no change is made if there is an error! */
1597 for (pass = 0; pass < 2; pass++) {
1598 if (mode_select_pages(r, p, len, pass == 1) < 0) {
1603 if (!blk_enable_write_cache(s->qdev.conf.blk)) {
1604 /* The request is used as the AIO opaque value, so add a ref. */
1605 scsi_req_ref(&r->req);
1606 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
1608 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r);
1612 scsi_req_complete(&r->req, GOOD);
1616 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1620 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1624 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1627 static inline bool check_lba_range(SCSIDiskState *s,
1628 uint64_t sector_num, uint32_t nb_sectors)
1631 * The first line tests that no overflow happens when computing the last
1632 * sector. The second line tests that the last accessed sector is in
1635 * Careful, the computations should not underflow for nb_sectors == 0,
1636 * and a 0-block read to the first LBA beyond the end of device is
1639 return (sector_num <= sector_num + nb_sectors &&
1640 sector_num + nb_sectors <= s->qdev.max_lba + 1);
1643 typedef struct UnmapCBData {
1649 static void scsi_unmap_complete(void *opaque, int ret);
1651 static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
1653 SCSIDiskReq *r = data->r;
1654 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1655 uint64_t sector_num;
1656 uint32_t nb_sectors;
1658 assert(r->req.aiocb == NULL);
1659 if (scsi_disk_req_check_error(r, ret, false)) {
1663 if (data->count > 0) {
1664 sector_num = ldq_be_p(&data->inbuf[0]);
1665 nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
1666 if (!check_lba_range(s, sector_num, nb_sectors)) {
1667 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1671 r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk,
1672 sector_num * s->qdev.blocksize,
1673 nb_sectors * s->qdev.blocksize,
1674 scsi_unmap_complete, data);
1680 scsi_req_complete(&r->req, GOOD);
1683 scsi_req_unref(&r->req);
1687 static void scsi_unmap_complete(void *opaque, int ret)
1689 UnmapCBData *data = opaque;
1690 SCSIDiskReq *r = data->r;
1691 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1693 assert(r->req.aiocb != NULL);
1694 r->req.aiocb = NULL;
1696 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
1697 scsi_unmap_complete_noio(data, ret);
1698 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1701 static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
1703 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1705 int len = r->req.cmd.xfer;
1708 /* Reject ANCHOR=1. */
1709 if (r->req.cmd.buf[1] & 0x1) {
1714 goto invalid_param_len;
1716 if (len < lduw_be_p(&p[0]) + 2) {
1717 goto invalid_param_len;
1719 if (len < lduw_be_p(&p[2]) + 8) {
1720 goto invalid_param_len;
1722 if (lduw_be_p(&p[2]) & 15) {
1723 goto invalid_param_len;
1726 if (blk_is_read_only(s->qdev.conf.blk)) {
1727 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1731 data = g_new0(UnmapCBData, 1);
1733 data->inbuf = &p[8];
1734 data->count = lduw_be_p(&p[2]) >> 4;
1736 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1737 scsi_req_ref(&r->req);
1738 scsi_unmap_complete_noio(data, 0);
1742 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1746 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1749 typedef struct WriteSameCBData {
1757 static void scsi_write_same_complete(void *opaque, int ret)
1759 WriteSameCBData *data = opaque;
1760 SCSIDiskReq *r = data->r;
1761 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1763 assert(r->req.aiocb != NULL);
1764 r->req.aiocb = NULL;
1765 aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
1766 if (scsi_disk_req_check_error(r, ret, true)) {
1770 block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
1772 data->nb_sectors -= data->iov.iov_len / 512;
1773 data->sector += data->iov.iov_len / 512;
1774 data->iov.iov_len = MIN(data->nb_sectors * 512, data->iov.iov_len);
1775 if (data->iov.iov_len) {
1776 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1777 data->iov.iov_len, BLOCK_ACCT_WRITE);
1778 /* Reinitialize qiov, to handle unaligned WRITE SAME request
1779 * where final qiov may need smaller size */
1780 qemu_iovec_init_external(&data->qiov, &data->iov, 1);
1781 r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk,
1782 data->sector << BDRV_SECTOR_BITS,
1784 scsi_write_same_complete, data);
1785 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1789 scsi_req_complete(&r->req, GOOD);
1792 scsi_req_unref(&r->req);
1793 qemu_vfree(data->iov.iov_base);
1795 aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
1798 static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
1800 SCSIRequest *req = &r->req;
1801 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1802 uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf);
1803 WriteSameCBData *data;
1807 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1808 if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) {
1809 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1813 if (blk_is_read_only(s->qdev.conf.blk)) {
1814 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1817 if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
1818 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1822 if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, s->qdev.blocksize)) {
1823 int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0;
1825 /* The request is used as the AIO opaque value, so add a ref. */
1826 scsi_req_ref(&r->req);
1827 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1828 nb_sectors * s->qdev.blocksize,
1830 r->req.aiocb = blk_aio_pwrite_zeroes(s->qdev.conf.blk,
1831 r->req.cmd.lba * s->qdev.blocksize,
1832 nb_sectors * s->qdev.blocksize,
1833 flags, scsi_aio_complete, r);
1837 data = g_new0(WriteSameCBData, 1);
1839 data->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1840 data->nb_sectors = nb_sectors * (s->qdev.blocksize / 512);
1841 data->iov.iov_len = MIN(data->nb_sectors * 512, SCSI_WRITE_SAME_MAX);
1842 data->iov.iov_base = buf = blk_blockalign(s->qdev.conf.blk,
1844 qemu_iovec_init_external(&data->qiov, &data->iov, 1);
1846 for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) {
1847 memcpy(&buf[i], inbuf, s->qdev.blocksize);
1850 scsi_req_ref(&r->req);
1851 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
1852 data->iov.iov_len, BLOCK_ACCT_WRITE);
1853 r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk,
1854 data->sector << BDRV_SECTOR_BITS,
1856 scsi_write_same_complete, data);
1859 static void scsi_disk_emulate_write_data(SCSIRequest *req)
1861 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1863 if (r->iov.iov_len) {
1864 int buflen = r->iov.iov_len;
1865 DPRINTF("Write buf_len=%d\n", buflen);
1867 scsi_req_data(&r->req, buflen);
1871 switch (req->cmd.buf[0]) {
1873 case MODE_SELECT_10:
1874 /* This also clears the sense buffer for REQUEST SENSE. */
1875 scsi_disk_emulate_mode_select(r, r->iov.iov_base);
1879 scsi_disk_emulate_unmap(r, r->iov.iov_base);
1885 if (r->req.status == -1) {
1886 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1892 scsi_disk_emulate_write_same(r, r->iov.iov_base);
1900 static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
1902 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1903 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1904 uint64_t nb_sectors;
1908 switch (req->cmd.buf[0]) {
1917 case ALLOW_MEDIUM_REMOVAL:
1918 case GET_CONFIGURATION:
1919 case GET_EVENT_STATUS_NOTIFICATION:
1920 case MECHANISM_STATUS:
1925 if (!blk_is_available(s->qdev.conf.blk)) {
1926 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1933 * FIXME: we shouldn't return anything bigger than 4k, but the code
1934 * requires the buffer to be as big as req->cmd.xfer in several
1935 * places. So, do not allow CDBs with a very large ALLOCATION
1936 * LENGTH. The real fix would be to modify scsi_read_data and
1937 * dma_buf_read, so that they return data beyond the buflen
1940 if (req->cmd.xfer > 65536) {
1941 goto illegal_request;
1943 r->buflen = MAX(4096, req->cmd.xfer);
1945 if (!r->iov.iov_base) {
1946 r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen);
1949 buflen = req->cmd.xfer;
1950 outbuf = r->iov.iov_base;
1951 memset(outbuf, 0, r->buflen);
1952 switch (req->cmd.buf[0]) {
1953 case TEST_UNIT_READY:
1954 assert(blk_is_available(s->qdev.conf.blk));
1957 buflen = scsi_disk_emulate_inquiry(req, outbuf);
1959 goto illegal_request;
1964 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
1966 goto illegal_request;
1970 buflen = scsi_disk_emulate_read_toc(req, outbuf);
1972 goto illegal_request;
1976 if (req->cmd.buf[1] & 1) {
1977 goto illegal_request;
1981 if (req->cmd.buf[1] & 3) {
1982 goto illegal_request;
1986 if (req->cmd.buf[1] & 1) {
1987 goto illegal_request;
1991 if (req->cmd.buf[1] & 3) {
1992 goto illegal_request;
1996 if (scsi_disk_emulate_start_stop(r) < 0) {
2000 case ALLOW_MEDIUM_REMOVAL:
2001 s->tray_locked = req->cmd.buf[4] & 1;
2002 blk_lock_medium(s->qdev.conf.blk, req->cmd.buf[4] & 1);
2004 case READ_CAPACITY_10:
2005 /* The normal LEN field for this command is zero. */
2006 memset(outbuf, 0, 8);
2007 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2009 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
2012 if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
2013 goto illegal_request;
2015 nb_sectors /= s->qdev.blocksize / 512;
2016 /* Returned value is the address of the last sector. */
2018 /* Remember the new size for read/write sanity checking. */
2019 s->qdev.max_lba = nb_sectors;
2020 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
2021 if (nb_sectors > UINT32_MAX) {
2022 nb_sectors = UINT32_MAX;
2024 outbuf[0] = (nb_sectors >> 24) & 0xff;
2025 outbuf[1] = (nb_sectors >> 16) & 0xff;
2026 outbuf[2] = (nb_sectors >> 8) & 0xff;
2027 outbuf[3] = nb_sectors & 0xff;
2030 outbuf[6] = s->qdev.blocksize >> 8;
2034 /* Just return "NO SENSE". */
2035 buflen = scsi_convert_sense(NULL, 0, outbuf, r->buflen,
2036 (req->cmd.buf[1] & 1) == 0);
2038 goto illegal_request;
2041 case MECHANISM_STATUS:
2042 buflen = scsi_emulate_mechanism_status(s, outbuf);
2044 goto illegal_request;
2047 case GET_CONFIGURATION:
2048 buflen = scsi_get_configuration(s, outbuf);
2050 goto illegal_request;
2053 case GET_EVENT_STATUS_NOTIFICATION:
2054 buflen = scsi_get_event_status_notification(s, r, outbuf);
2056 goto illegal_request;
2059 case READ_DISC_INFORMATION:
2060 buflen = scsi_read_disc_information(s, r, outbuf);
2062 goto illegal_request;
2065 case READ_DVD_STRUCTURE:
2066 buflen = scsi_read_dvd_structure(s, r, outbuf);
2068 goto illegal_request;
2071 case SERVICE_ACTION_IN_16:
2072 /* Service Action In subcommands. */
2073 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
2074 DPRINTF("SAI READ CAPACITY(16)\n");
2075 memset(outbuf, 0, req->cmd.xfer);
2076 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2078 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
2081 if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
2082 goto illegal_request;
2084 nb_sectors /= s->qdev.blocksize / 512;
2085 /* Returned value is the address of the last sector. */
2087 /* Remember the new size for read/write sanity checking. */
2088 s->qdev.max_lba = nb_sectors;
2089 outbuf[0] = (nb_sectors >> 56) & 0xff;
2090 outbuf[1] = (nb_sectors >> 48) & 0xff;
2091 outbuf[2] = (nb_sectors >> 40) & 0xff;
2092 outbuf[3] = (nb_sectors >> 32) & 0xff;
2093 outbuf[4] = (nb_sectors >> 24) & 0xff;
2094 outbuf[5] = (nb_sectors >> 16) & 0xff;
2095 outbuf[6] = (nb_sectors >> 8) & 0xff;
2096 outbuf[7] = nb_sectors & 0xff;
2099 outbuf[10] = s->qdev.blocksize >> 8;
2102 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
2104 /* set TPE bit if the format supports discard */
2105 if (s->qdev.conf.discard_granularity) {
2109 /* Protection, exponent and lowest lba field left blank. */
2112 DPRINTF("Unsupported Service Action In\n");
2113 goto illegal_request;
2114 case SYNCHRONIZE_CACHE:
2115 /* The request is used as the AIO opaque value, so add a ref. */
2116 scsi_req_ref(&r->req);
2117 block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0,
2119 r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r);
2122 DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
2123 if (r->req.cmd.lba > s->qdev.max_lba) {
2128 DPRINTF("Mode Select(6) (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2130 case MODE_SELECT_10:
2131 DPRINTF("Mode Select(10) (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2134 DPRINTF("Unmap (len %lu)\n", (unsigned long)r->req.cmd.xfer);
2139 DPRINTF("Verify (bytchk %d)\n", (req->cmd.buf[1] >> 1) & 3);
2140 if (req->cmd.buf[1] & 6) {
2141 goto illegal_request;
2146 DPRINTF("WRITE SAME %d (len %lu)\n",
2147 req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16,
2148 (unsigned long)r->req.cmd.xfer);
2151 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf[0],
2152 scsi_command_name(buf[0]));
2153 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
2156 assert(!r->req.aiocb);
2157 r->iov.iov_len = MIN(r->buflen, req->cmd.xfer);
2158 if (r->iov.iov_len == 0) {
2159 scsi_req_complete(&r->req, GOOD);
2161 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2162 assert(r->iov.iov_len == req->cmd.xfer);
2163 return -r->iov.iov_len;
2165 return r->iov.iov_len;
2169 if (r->req.status == -1) {
2170 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2175 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2179 /* Execute a scsi command. Returns the length of the data expected by the
2180 command. This will be Positive for data transfers from the device
2181 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2182 and zero if the command does not transfer any data. */
2184 static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
2186 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2187 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
2188 SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s));
2194 if (!blk_is_available(s->qdev.conf.blk)) {
2195 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
2199 len = scsi_data_cdb_xfer(r->req.cmd.buf);
2205 DPRINTF("Read (sector %" PRId64 ", count %u)\n", r->req.cmd.lba, len);
2206 /* Protection information is not supported. For SCSI versions 2 and
2207 * older (as determined by snooping the guest's INQUIRY commands),
2208 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2210 if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) {
2211 goto illegal_request;
2213 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2216 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2217 r->sector_count = len * (s->qdev.blocksize / 512);
2223 case WRITE_VERIFY_10:
2224 case WRITE_VERIFY_12:
2225 case WRITE_VERIFY_16:
2226 if (blk_is_read_only(s->qdev.conf.blk)) {
2227 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
2230 DPRINTF("Write %s(sector %" PRId64 ", count %u)\n",
2231 (command & 0xe) == 0xe ? "And Verify " : "",
2232 r->req.cmd.lba, len);
2237 /* We get here only for BYTCHK == 0x01 and only for scsi-block.
2238 * As far as DMA is concerned, we can treat it the same as a write;
2239 * scsi_block_do_sgio will send VERIFY commands.
2241 if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) {
2242 goto illegal_request;
2244 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2247 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2248 r->sector_count = len * (s->qdev.blocksize / 512);
2253 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2256 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2259 r->need_fua_emulation = sdc->need_fua_emulation(&r->req.cmd);
2260 if (r->sector_count == 0) {
2261 scsi_req_complete(&r->req, GOOD);
2263 assert(r->iov.iov_len == 0);
2264 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2265 return -r->sector_count * 512;
2267 return r->sector_count * 512;
2271 static void scsi_disk_reset(DeviceState *dev)
2273 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
2274 uint64_t nb_sectors;
2276 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
2278 blk_get_geometry(s->qdev.conf.blk, &nb_sectors);
2279 nb_sectors /= s->qdev.blocksize / 512;
2283 s->qdev.max_lba = nb_sectors;
2284 /* reset tray statuses */
2288 s->qdev.scsi_version = s->qdev.default_scsi_version;
2291 static void scsi_disk_resize_cb(void *opaque)
2293 SCSIDiskState *s = opaque;
2295 /* SPC lists this sense code as available only for
2296 * direct-access devices.
2298 if (s->qdev.type == TYPE_DISK) {
2299 scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED));
2303 static void scsi_cd_change_media_cb(void *opaque, bool load, Error **errp)
2305 SCSIDiskState *s = opaque;
2308 * When a CD gets changed, we have to report an ejected state and
2309 * then a loaded state to guests so that they detect tray
2310 * open/close and media change events. Guests that do not use
2311 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2312 * states rely on this behavior.
2314 * media_changed governs the state machine used for unit attention
2315 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2317 s->media_changed = load;
2318 s->tray_open = !load;
2319 scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM));
2320 s->media_event = true;
2321 s->eject_request = false;
2324 static void scsi_cd_eject_request_cb(void *opaque, bool force)
2326 SCSIDiskState *s = opaque;
2328 s->eject_request = true;
2330 s->tray_locked = false;
2334 static bool scsi_cd_is_tray_open(void *opaque)
2336 return ((SCSIDiskState *)opaque)->tray_open;
2339 static bool scsi_cd_is_medium_locked(void *opaque)
2341 return ((SCSIDiskState *)opaque)->tray_locked;
2344 static const BlockDevOps scsi_disk_removable_block_ops = {
2345 .change_media_cb = scsi_cd_change_media_cb,
2346 .eject_request_cb = scsi_cd_eject_request_cb,
2347 .is_tray_open = scsi_cd_is_tray_open,
2348 .is_medium_locked = scsi_cd_is_medium_locked,
2350 .resize_cb = scsi_disk_resize_cb,
2353 static const BlockDevOps scsi_disk_block_ops = {
2354 .resize_cb = scsi_disk_resize_cb,
2357 static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
2359 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2360 if (s->media_changed) {
2361 s->media_changed = false;
2362 scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED));
2366 static void scsi_realize(SCSIDevice *dev, Error **errp)
2368 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2370 if (!s->qdev.conf.blk) {
2371 error_setg(errp, "drive property not set");
2375 if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2376 !blk_is_inserted(s->qdev.conf.blk)) {
2377 error_setg(errp, "Device needs media, but drive is empty");
2381 blkconf_serial(&s->qdev.conf, &s->serial);
2382 blkconf_blocksizes(&s->qdev.conf);
2384 if (s->qdev.conf.logical_block_size >
2385 s->qdev.conf.physical_block_size) {
2387 "logical_block_size > physical_block_size not supported");
2391 if (dev->type == TYPE_DISK) {
2392 if (!blkconf_geometry(&dev->conf, NULL, 65535, 255, 255, errp)) {
2396 if (!blkconf_apply_backend_options(&dev->conf,
2397 blk_is_read_only(s->qdev.conf.blk),
2398 dev->type == TYPE_DISK, errp)) {
2402 if (s->qdev.conf.discard_granularity == -1) {
2403 s->qdev.conf.discard_granularity =
2404 MAX(s->qdev.conf.logical_block_size, DEFAULT_DISCARD_GRANULARITY);
2408 s->version = g_strdup(qemu_hw_version());
2411 s->vendor = g_strdup("QEMU");
2414 if (blk_is_sg(s->qdev.conf.blk)) {
2415 error_setg(errp, "unwanted /dev/sg*");
2419 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2420 !(s->features & (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS))) {
2421 blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_removable_block_ops, s);
2423 blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_block_ops, s);
2425 blk_set_guest_block_size(s->qdev.conf.blk, s->qdev.blocksize);
2427 blk_iostatus_enable(s->qdev.conf.blk);
2430 static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
2432 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
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 blkconf_blocksizes(&s->qdev.conf);
2439 s->qdev.blocksize = s->qdev.conf.logical_block_size;
2440 s->qdev.type = TYPE_DISK;
2442 s->product = g_strdup("QEMU HARDDISK");
2444 scsi_realize(&s->qdev, errp);
2447 static void scsi_cd_realize(SCSIDevice *dev, Error **errp)
2449 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2452 if (!dev->conf.blk) {
2453 /* Anonymous BlockBackend for an empty drive. As we put it into
2454 * dev->conf, qdev takes care of detaching on unplug. */
2455 dev->conf.blk = blk_new(0, BLK_PERM_ALL);
2456 ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
2460 s->qdev.blocksize = 2048;
2461 s->qdev.type = TYPE_ROM;
2462 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2464 s->product = g_strdup("QEMU CD-ROM");
2466 scsi_realize(&s->qdev, errp);
2469 static void scsi_disk_realize(SCSIDevice *dev, Error **errp)
2472 Error *local_err = NULL;
2474 if (!dev->conf.blk) {
2475 scsi_realize(dev, &local_err);
2477 error_propagate(errp, local_err);
2481 dinfo = blk_legacy_dinfo(dev->conf.blk);
2482 if (dinfo && dinfo->media_cd) {
2483 scsi_cd_realize(dev, errp);
2485 scsi_hd_realize(dev, errp);
2489 static const SCSIReqOps scsi_disk_emulate_reqops = {
2490 .size = sizeof(SCSIDiskReq),
2491 .free_req = scsi_free_request,
2492 .send_command = scsi_disk_emulate_command,
2493 .read_data = scsi_disk_emulate_read_data,
2494 .write_data = scsi_disk_emulate_write_data,
2495 .get_buf = scsi_get_buf,
2498 static const SCSIReqOps scsi_disk_dma_reqops = {
2499 .size = sizeof(SCSIDiskReq),
2500 .free_req = scsi_free_request,
2501 .send_command = scsi_disk_dma_command,
2502 .read_data = scsi_read_data,
2503 .write_data = scsi_write_data,
2504 .get_buf = scsi_get_buf,
2505 .load_request = scsi_disk_load_request,
2506 .save_request = scsi_disk_save_request,
2509 static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {
2510 [TEST_UNIT_READY] = &scsi_disk_emulate_reqops,
2511 [INQUIRY] = &scsi_disk_emulate_reqops,
2512 [MODE_SENSE] = &scsi_disk_emulate_reqops,
2513 [MODE_SENSE_10] = &scsi_disk_emulate_reqops,
2514 [START_STOP] = &scsi_disk_emulate_reqops,
2515 [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops,
2516 [READ_CAPACITY_10] = &scsi_disk_emulate_reqops,
2517 [READ_TOC] = &scsi_disk_emulate_reqops,
2518 [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops,
2519 [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops,
2520 [GET_CONFIGURATION] = &scsi_disk_emulate_reqops,
2521 [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops,
2522 [MECHANISM_STATUS] = &scsi_disk_emulate_reqops,
2523 [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops,
2524 [REQUEST_SENSE] = &scsi_disk_emulate_reqops,
2525 [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops,
2526 [SEEK_10] = &scsi_disk_emulate_reqops,
2527 [MODE_SELECT] = &scsi_disk_emulate_reqops,
2528 [MODE_SELECT_10] = &scsi_disk_emulate_reqops,
2529 [UNMAP] = &scsi_disk_emulate_reqops,
2530 [WRITE_SAME_10] = &scsi_disk_emulate_reqops,
2531 [WRITE_SAME_16] = &scsi_disk_emulate_reqops,
2532 [VERIFY_10] = &scsi_disk_emulate_reqops,
2533 [VERIFY_12] = &scsi_disk_emulate_reqops,
2534 [VERIFY_16] = &scsi_disk_emulate_reqops,
2536 [READ_6] = &scsi_disk_dma_reqops,
2537 [READ_10] = &scsi_disk_dma_reqops,
2538 [READ_12] = &scsi_disk_dma_reqops,
2539 [READ_16] = &scsi_disk_dma_reqops,
2540 [WRITE_6] = &scsi_disk_dma_reqops,
2541 [WRITE_10] = &scsi_disk_dma_reqops,
2542 [WRITE_12] = &scsi_disk_dma_reqops,
2543 [WRITE_16] = &scsi_disk_dma_reqops,
2544 [WRITE_VERIFY_10] = &scsi_disk_dma_reqops,
2545 [WRITE_VERIFY_12] = &scsi_disk_dma_reqops,
2546 [WRITE_VERIFY_16] = &scsi_disk_dma_reqops,
2549 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
2550 uint8_t *buf, void *hba_private)
2552 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2554 const SCSIReqOps *ops;
2558 ops = scsi_disk_reqops_dispatch[command];
2560 ops = &scsi_disk_emulate_reqops;
2562 req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private);
2565 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
2568 for (i = 1; i < scsi_cdb_length(buf); i++) {
2569 printf(" 0x%02x", buf[i]);
2579 static int get_device_type(SCSIDiskState *s)
2585 memset(cmd, 0, sizeof(cmd));
2586 memset(buf, 0, sizeof(buf));
2588 cmd[4] = sizeof(buf);
2590 ret = scsi_SG_IO_FROM_DEV(s->qdev.conf.blk, cmd, sizeof(cmd),
2595 s->qdev.type = buf[0];
2596 if (buf[1] & 0x80) {
2597 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2602 static void scsi_block_realize(SCSIDevice *dev, Error **errp)
2604 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2608 if (!s->qdev.conf.blk) {
2609 error_setg(errp, "drive property not set");
2613 /* check we are using a driver managing SG_IO (version 3 and after) */
2614 rc = blk_ioctl(s->qdev.conf.blk, SG_GET_VERSION_NUM, &sg_version);
2616 error_setg_errno(errp, -rc, "cannot get SG_IO version number");
2618 error_append_hint(errp, "Is this a SCSI device?\n");
2622 if (sg_version < 30000) {
2623 error_setg(errp, "scsi generic interface too old");
2627 /* get device type from INQUIRY data */
2628 rc = get_device_type(s);
2630 error_setg(errp, "INQUIRY failed");
2634 /* Make a guess for the block size, we'll fix it when the guest sends.
2635 * READ CAPACITY. If they don't, they likely would assume these sizes
2636 * anyway. (TODO: check in /sys).
2638 if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
2639 s->qdev.blocksize = 2048;
2641 s->qdev.blocksize = 512;
2644 /* Makes the scsi-block device not removable by using HMP and QMP eject
2647 s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS);
2649 scsi_realize(&s->qdev, errp);
2650 scsi_generic_read_device_inquiry(&s->qdev);
2653 typedef struct SCSIBlockReq {
2655 sg_io_hdr_t io_header;
2657 /* Selected bytes of the original CDB, copied into our own CDB. */
2658 uint8_t cmd, cdb1, group_number;
2660 /* CDB passed to SG_IO. */
2664 static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req,
2665 int64_t offset, QEMUIOVector *iov,
2667 BlockCompletionFunc *cb, void *opaque)
2669 sg_io_hdr_t *io_header = &req->io_header;
2670 SCSIDiskReq *r = &req->req;
2671 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2672 int nb_logical_blocks;
2676 /* This is not supported yet. It can only happen if the guest does
2677 * reads and writes that are not aligned to one logical sectors
2678 * _and_ cover multiple MemoryRegions.
2680 assert(offset % s->qdev.blocksize == 0);
2681 assert(iov->size % s->qdev.blocksize == 0);
2683 io_header->interface_id = 'S';
2685 /* The data transfer comes from the QEMUIOVector. */
2686 io_header->dxfer_direction = direction;
2687 io_header->dxfer_len = iov->size;
2688 io_header->dxferp = (void *)iov->iov;
2689 io_header->iovec_count = iov->niov;
2690 assert(io_header->iovec_count == iov->niov); /* no overflow! */
2692 /* Build a new CDB with the LBA and length patched in, in case
2693 * DMA helpers split the transfer in multiple segments. Do not
2694 * build a CDB smaller than what the guest wanted, and only build
2695 * a larger one if strictly necessary.
2697 io_header->cmdp = req->cdb;
2698 lba = offset / s->qdev.blocksize;
2699 nb_logical_blocks = io_header->dxfer_len / s->qdev.blocksize;
2701 if ((req->cmd >> 5) == 0 && lba <= 0x1ffff) {
2703 stl_be_p(&req->cdb[0], lba | (req->cmd << 24));
2704 req->cdb[4] = nb_logical_blocks;
2706 io_header->cmd_len = 6;
2707 } else if ((req->cmd >> 5) <= 1 && lba <= 0xffffffffULL) {
2709 req->cdb[0] = (req->cmd & 0x1f) | 0x20;
2710 req->cdb[1] = req->cdb1;
2711 stl_be_p(&req->cdb[2], lba);
2712 req->cdb[6] = req->group_number;
2713 stw_be_p(&req->cdb[7], nb_logical_blocks);
2715 io_header->cmd_len = 10;
2716 } else if ((req->cmd >> 5) != 4 && lba <= 0xffffffffULL) {
2718 req->cdb[0] = (req->cmd & 0x1f) | 0xA0;
2719 req->cdb[1] = req->cdb1;
2720 stl_be_p(&req->cdb[2], lba);
2721 stl_be_p(&req->cdb[6], nb_logical_blocks);
2722 req->cdb[10] = req->group_number;
2724 io_header->cmd_len = 12;
2727 req->cdb[0] = (req->cmd & 0x1f) | 0x80;
2728 req->cdb[1] = req->cdb1;
2729 stq_be_p(&req->cdb[2], lba);
2730 stl_be_p(&req->cdb[10], nb_logical_blocks);
2731 req->cdb[14] = req->group_number;
2733 io_header->cmd_len = 16;
2736 /* The rest is as in scsi-generic.c. */
2737 io_header->mx_sb_len = sizeof(r->req.sense);
2738 io_header->sbp = r->req.sense;
2739 io_header->timeout = UINT_MAX;
2740 io_header->usr_ptr = r;
2741 io_header->flags |= SG_FLAG_DIRECT_IO;
2743 aiocb = blk_aio_ioctl(s->qdev.conf.blk, SG_IO, io_header, cb, opaque);
2744 assert(aiocb != NULL);
2748 static bool scsi_block_no_fua(SCSICommand *cmd)
2753 static BlockAIOCB *scsi_block_dma_readv(int64_t offset,
2755 BlockCompletionFunc *cb, void *cb_opaque,
2758 SCSIBlockReq *r = opaque;
2759 return scsi_block_do_sgio(r, offset, iov,
2760 SG_DXFER_FROM_DEV, cb, cb_opaque);
2763 static BlockAIOCB *scsi_block_dma_writev(int64_t offset,
2765 BlockCompletionFunc *cb, void *cb_opaque,
2768 SCSIBlockReq *r = opaque;
2769 return scsi_block_do_sgio(r, offset, iov,
2770 SG_DXFER_TO_DEV, cb, cb_opaque);
2773 static bool scsi_block_is_passthrough(SCSIDiskState *s, uint8_t *buf)
2779 /* Check if BYTCHK == 0x01 (data-out buffer contains data
2780 * for the number of logical blocks specified in the length
2781 * field). For other modes, do not use scatter/gather operation.
2783 if ((buf[1] & 6) == 2) {
2796 case WRITE_VERIFY_10:
2797 case WRITE_VERIFY_12:
2798 case WRITE_VERIFY_16:
2799 /* MMC writing cannot be done via DMA helpers, because it sometimes
2800 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2801 * We might use scsi_block_dma_reqops as long as no writing commands are
2802 * seen, but performance usually isn't paramount on optical media. So,
2803 * just make scsi-block operate the same as scsi-generic for them.
2805 if (s->qdev.type != TYPE_ROM) {
2818 static int32_t scsi_block_dma_command(SCSIRequest *req, uint8_t *buf)
2820 SCSIBlockReq *r = (SCSIBlockReq *)req;
2821 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
2823 r->cmd = req->cmd.buf[0];
2824 switch (r->cmd >> 5) {
2827 r->cdb1 = r->group_number = 0;
2831 r->cdb1 = req->cmd.buf[1];
2832 r->group_number = req->cmd.buf[6];
2836 r->cdb1 = req->cmd.buf[1];
2837 r->group_number = req->cmd.buf[10];
2841 r->cdb1 = req->cmd.buf[1];
2842 r->group_number = req->cmd.buf[14];
2848 /* Protection information is not supported. For SCSI versions 2 and
2849 * older (as determined by snooping the guest's INQUIRY commands),
2850 * there is no RD/WR/VRPROTECT, so skip this check in these versions.
2852 if (s->qdev.scsi_version > 2 && (req->cmd.buf[1] & 0xe0)) {
2853 scsi_check_condition(&r->req, SENSE_CODE(INVALID_FIELD));
2857 r->req.status = &r->io_header.status;
2858 return scsi_disk_dma_command(req, buf);
2861 static const SCSIReqOps scsi_block_dma_reqops = {
2862 .size = sizeof(SCSIBlockReq),
2863 .free_req = scsi_free_request,
2864 .send_command = scsi_block_dma_command,
2865 .read_data = scsi_read_data,
2866 .write_data = scsi_write_data,
2867 .get_buf = scsi_get_buf,
2868 .load_request = scsi_disk_load_request,
2869 .save_request = scsi_disk_save_request,
2872 static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
2873 uint32_t lun, uint8_t *buf,
2876 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2878 if (scsi_block_is_passthrough(s, buf)) {
2879 return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
2882 return scsi_req_alloc(&scsi_block_dma_reqops, &s->qdev, tag, lun,
2887 static int scsi_block_parse_cdb(SCSIDevice *d, SCSICommand *cmd,
2888 uint8_t *buf, void *hba_private)
2890 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2892 if (scsi_block_is_passthrough(s, buf)) {
2893 return scsi_bus_parse_cdb(&s->qdev, cmd, buf, hba_private);
2895 return scsi_req_parse_cdb(&s->qdev, cmd, buf);
2902 BlockAIOCB *scsi_dma_readv(int64_t offset, QEMUIOVector *iov,
2903 BlockCompletionFunc *cb, void *cb_opaque,
2906 SCSIDiskReq *r = opaque;
2907 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2908 return blk_aio_preadv(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque);
2912 BlockAIOCB *scsi_dma_writev(int64_t offset, QEMUIOVector *iov,
2913 BlockCompletionFunc *cb, void *cb_opaque,
2916 SCSIDiskReq *r = opaque;
2917 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2918 return blk_aio_pwritev(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque);
2921 static void scsi_disk_base_class_initfn(ObjectClass *klass, void *data)
2923 DeviceClass *dc = DEVICE_CLASS(klass);
2924 SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass);
2926 dc->fw_name = "disk";
2927 dc->reset = scsi_disk_reset;
2928 sdc->dma_readv = scsi_dma_readv;
2929 sdc->dma_writev = scsi_dma_writev;
2930 sdc->need_fua_emulation = scsi_is_cmd_fua;
2933 static const TypeInfo scsi_disk_base_info = {
2934 .name = TYPE_SCSI_DISK_BASE,
2935 .parent = TYPE_SCSI_DEVICE,
2936 .class_init = scsi_disk_base_class_initfn,
2937 .instance_size = sizeof(SCSIDiskState),
2938 .class_size = sizeof(SCSIDiskClass),
2942 #define DEFINE_SCSI_DISK_PROPERTIES() \
2943 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2944 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \
2945 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2946 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2947 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2948 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2950 static Property scsi_hd_properties[] = {
2951 DEFINE_SCSI_DISK_PROPERTIES(),
2952 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2953 SCSI_DISK_F_REMOVABLE, false),
2954 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2955 SCSI_DISK_F_DPOFUA, false),
2956 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
2957 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
2958 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
2959 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
2960 DEFAULT_MAX_UNMAP_SIZE),
2961 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
2962 DEFAULT_MAX_IO_SIZE),
2963 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0),
2964 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
2966 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
2967 DEFINE_PROP_END_OF_LIST(),
2970 static const VMStateDescription vmstate_scsi_disk_state = {
2971 .name = "scsi-disk",
2973 .minimum_version_id = 1,
2974 .fields = (VMStateField[]) {
2975 VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
2976 VMSTATE_BOOL(media_changed, SCSIDiskState),
2977 VMSTATE_BOOL(media_event, SCSIDiskState),
2978 VMSTATE_BOOL(eject_request, SCSIDiskState),
2979 VMSTATE_BOOL(tray_open, SCSIDiskState),
2980 VMSTATE_BOOL(tray_locked, SCSIDiskState),
2981 VMSTATE_END_OF_LIST()
2985 static void scsi_hd_class_initfn(ObjectClass *klass, void *data)
2987 DeviceClass *dc = DEVICE_CLASS(klass);
2988 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2990 sc->realize = scsi_hd_realize;
2991 sc->alloc_req = scsi_new_request;
2992 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2993 dc->desc = "virtual SCSI disk";
2994 dc->props = scsi_hd_properties;
2995 dc->vmsd = &vmstate_scsi_disk_state;
2998 static const TypeInfo scsi_hd_info = {
3000 .parent = TYPE_SCSI_DISK_BASE,
3001 .class_init = scsi_hd_class_initfn,
3004 static Property scsi_cd_properties[] = {
3005 DEFINE_SCSI_DISK_PROPERTIES(),
3006 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
3007 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
3008 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
3009 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3010 DEFAULT_MAX_IO_SIZE),
3011 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3013 DEFINE_PROP_END_OF_LIST(),
3016 static void scsi_cd_class_initfn(ObjectClass *klass, void *data)
3018 DeviceClass *dc = DEVICE_CLASS(klass);
3019 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3021 sc->realize = scsi_cd_realize;
3022 sc->alloc_req = scsi_new_request;
3023 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
3024 dc->desc = "virtual SCSI CD-ROM";
3025 dc->props = scsi_cd_properties;
3026 dc->vmsd = &vmstate_scsi_disk_state;
3029 static const TypeInfo scsi_cd_info = {
3031 .parent = TYPE_SCSI_DISK_BASE,
3032 .class_init = scsi_cd_class_initfn,
3036 static Property scsi_block_properties[] = {
3037 DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \
3038 DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.blk),
3039 DEFINE_PROP_BOOL("share-rw", SCSIDiskState, qdev.conf.share_rw, false),
3040 DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0),
3041 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
3042 DEFAULT_MAX_UNMAP_SIZE),
3043 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3044 DEFAULT_MAX_IO_SIZE),
3045 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3047 DEFINE_PROP_END_OF_LIST(),
3050 static void scsi_block_class_initfn(ObjectClass *klass, void *data)
3052 DeviceClass *dc = DEVICE_CLASS(klass);
3053 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3054 SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass);
3056 sc->realize = scsi_block_realize;
3057 sc->alloc_req = scsi_block_new_request;
3058 sc->parse_cdb = scsi_block_parse_cdb;
3059 sdc->dma_readv = scsi_block_dma_readv;
3060 sdc->dma_writev = scsi_block_dma_writev;
3061 sdc->need_fua_emulation = scsi_block_no_fua;
3062 dc->desc = "SCSI block device passthrough";
3063 dc->props = scsi_block_properties;
3064 dc->vmsd = &vmstate_scsi_disk_state;
3067 static const TypeInfo scsi_block_info = {
3068 .name = "scsi-block",
3069 .parent = TYPE_SCSI_DISK_BASE,
3070 .class_init = scsi_block_class_initfn,
3074 static Property scsi_disk_properties[] = {
3075 DEFINE_SCSI_DISK_PROPERTIES(),
3076 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
3077 SCSI_DISK_F_REMOVABLE, false),
3078 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
3079 SCSI_DISK_F_DPOFUA, false),
3080 DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0),
3081 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0),
3082 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
3083 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
3084 DEFAULT_MAX_UNMAP_SIZE),
3085 DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size,
3086 DEFAULT_MAX_IO_SIZE),
3087 DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version,
3089 DEFINE_PROP_END_OF_LIST(),
3092 static void scsi_disk_class_initfn(ObjectClass *klass, void *data)
3094 DeviceClass *dc = DEVICE_CLASS(klass);
3095 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
3097 sc->realize = scsi_disk_realize;
3098 sc->alloc_req = scsi_new_request;
3099 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
3100 dc->fw_name = "disk";
3101 dc->desc = "virtual SCSI disk or CD-ROM (legacy)";
3102 dc->reset = scsi_disk_reset;
3103 dc->props = scsi_disk_properties;
3104 dc->vmsd = &vmstate_scsi_disk_state;
3107 static const TypeInfo scsi_disk_info = {
3108 .name = "scsi-disk",
3109 .parent = TYPE_SCSI_DISK_BASE,
3110 .class_init = scsi_disk_class_initfn,
3113 static void scsi_disk_register_types(void)
3115 type_register_static(&scsi_disk_base_info);
3116 type_register_static(&scsi_hd_info);
3117 type_register_static(&scsi_cd_info);
3119 type_register_static(&scsi_block_info);
3121 type_register_static(&scsi_disk_info);
3124 type_init(scsi_disk_register_types)