2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
9 * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case
10 * when the allocation length of CDB is smaller
12 * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the
13 * MODE SENSE response.
15 * This code is licensed under the LGPL.
17 * Note that this file only handles the SCSI architecture model and device
18 * commands. Emulation of interface/link layer protocols is handled by
19 * the host adapter emulator.
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
31 #include "qemu-common.h"
32 #include "qemu/error-report.h"
33 #include "hw/scsi/scsi.h"
34 #include "block/scsi.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/blockdev.h"
37 #include "hw/block/block.h"
38 #include "sysemu/dma.h"
44 #define SCSI_WRITE_SAME_MAX 524288
45 #define SCSI_DMA_BUF_SIZE 131072
46 #define SCSI_MAX_INQUIRY_LEN 256
47 #define SCSI_MAX_MODE_LEN 256
49 #define DEFAULT_DISCARD_GRANULARITY 4096
50 #define DEFAULT_MAX_UNMAP_SIZE (1 << 30) /* 1 GB */
52 typedef struct SCSIDiskState SCSIDiskState;
54 typedef struct SCSIDiskReq {
56 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
58 uint32_t sector_count;
66 #define SCSI_DISK_F_REMOVABLE 0
67 #define SCSI_DISK_F_DPOFUA 1
68 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2
80 uint64_t max_unmap_size;
90 static int scsi_handle_rw_error(SCSIDiskReq *r, int error);
92 static void scsi_free_request(SCSIRequest *req)
94 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
96 qemu_vfree(r->iov.iov_base);
99 /* Helper function for command completion with sense. */
100 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
102 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
103 r->req.tag, sense.key, sense.asc, sense.ascq);
104 scsi_req_build_sense(&r->req, sense);
105 scsi_req_complete(&r->req, CHECK_CONDITION);
108 /* Cancel a pending data transfer. */
109 static void scsi_cancel_io(SCSIRequest *req)
111 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
113 DPRINTF("Cancel tag=0x%x\n", req->tag);
115 bdrv_aio_cancel(r->req.aiocb);
117 /* This reference was left in by scsi_*_data. We take ownership of
118 * it the moment scsi_req_cancel is called, independent of whether
119 * bdrv_aio_cancel completes the request or not. */
120 scsi_req_unref(&r->req);
125 static uint32_t scsi_init_iovec(SCSIDiskReq *r, size_t size)
127 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
129 if (!r->iov.iov_base) {
131 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
133 r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
134 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
135 return r->qiov.size / 512;
138 static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req)
140 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
142 qemu_put_be64s(f, &r->sector);
143 qemu_put_be32s(f, &r->sector_count);
144 qemu_put_be32s(f, &r->buflen);
146 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
147 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
148 } else if (!req->retry) {
149 uint32_t len = r->iov.iov_len;
150 qemu_put_be32s(f, &len);
151 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
156 static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req)
158 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
160 qemu_get_be64s(f, &r->sector);
161 qemu_get_be32s(f, &r->sector_count);
162 qemu_get_be32s(f, &r->buflen);
164 scsi_init_iovec(r, r->buflen);
165 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
166 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
167 } else if (!r->req.retry) {
169 qemu_get_be32s(f, &len);
170 r->iov.iov_len = len;
171 assert(r->iov.iov_len <= r->buflen);
172 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
176 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
179 static void scsi_aio_complete(void *opaque, int ret)
181 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
182 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
184 assert(r->req.aiocb != NULL);
186 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
187 if (r->req.io_canceled) {
192 if (scsi_handle_rw_error(r, -ret)) {
197 scsi_req_complete(&r->req, GOOD);
200 if (!r->req.io_canceled) {
201 scsi_req_unref(&r->req);
205 static bool scsi_is_cmd_fua(SCSICommand *cmd)
207 switch (cmd->buf[0]) {
214 return (cmd->buf[1] & 8) != 0;
219 case WRITE_VERIFY_10:
220 case WRITE_VERIFY_12:
221 case WRITE_VERIFY_16:
231 static void scsi_write_do_fua(SCSIDiskReq *r)
233 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
235 if (r->req.io_canceled) {
239 if (scsi_is_cmd_fua(&r->req.cmd)) {
240 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct, 0,
242 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
246 scsi_req_complete(&r->req, GOOD);
249 if (!r->req.io_canceled) {
250 scsi_req_unref(&r->req);
254 static void scsi_dma_complete_noio(void *opaque, int ret)
256 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
257 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
259 if (r->req.aiocb != NULL) {
261 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
263 if (r->req.io_canceled) {
268 if (scsi_handle_rw_error(r, -ret)) {
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 if (!r->req.io_canceled) {
284 scsi_req_unref(&r->req);
288 static void scsi_dma_complete(void *opaque, int ret)
290 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
292 assert(r->req.aiocb != NULL);
293 scsi_dma_complete_noio(opaque, ret);
296 static void scsi_read_complete(void * opaque, int ret)
298 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
299 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
302 assert(r->req.aiocb != NULL);
304 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
305 if (r->req.io_canceled) {
310 if (scsi_handle_rw_error(r, -ret)) {
315 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size);
317 n = r->qiov.size / 512;
319 r->sector_count -= n;
320 scsi_req_data(&r->req, r->qiov.size);
323 if (!r->req.io_canceled) {
324 scsi_req_unref(&r->req);
328 /* Actually issue a read to the block device. */
329 static void scsi_do_read(void *opaque, int ret)
331 SCSIDiskReq *r = opaque;
332 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
335 if (r->req.aiocb != NULL) {
337 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
339 if (r->req.io_canceled) {
344 if (scsi_handle_rw_error(r, -ret)) {
349 /* The request is used as the AIO opaque value, so add a ref. */
350 scsi_req_ref(&r->req);
353 dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BLOCK_ACCT_READ);
354 r->req.resid -= r->req.sg->size;
355 r->req.aiocb = dma_bdrv_read(s->qdev.conf.bs, r->req.sg, r->sector,
356 scsi_dma_complete, r);
358 n = scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
359 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct,
360 n * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
361 r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n,
362 scsi_read_complete, r);
366 if (!r->req.io_canceled) {
367 scsi_req_unref(&r->req);
371 /* Read more data from scsi device into buffer. */
372 static void scsi_read_data(SCSIRequest *req)
374 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
375 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
378 DPRINTF("Read sector_count=%d\n", r->sector_count);
379 if (r->sector_count == 0) {
380 /* This also clears the sense buffer for REQUEST SENSE. */
381 scsi_req_complete(&r->req, GOOD);
385 /* No data transfer may already be in progress */
386 assert(r->req.aiocb == NULL);
388 /* The request is used as the AIO opaque value, so add a ref. */
389 scsi_req_ref(&r->req);
390 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
391 DPRINTF("Data transfer direction invalid\n");
392 scsi_read_complete(r, -EINVAL);
397 scsi_read_complete(r, -ENOMEDIUM);
403 if (first && scsi_is_cmd_fua(&r->req.cmd)) {
404 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct, 0,
406 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_do_read, r);
413 * scsi_handle_rw_error has two return values. 0 means that the error
414 * must be ignored, 1 means that the error has been processed and the
415 * caller should not do anything else for this request. Note that
416 * scsi_handle_rw_error always manages its reference counts, independent
417 * of the return value.
419 static int scsi_handle_rw_error(SCSIDiskReq *r, int error)
421 bool is_read = (r->req.cmd.xfer == SCSI_XFER_FROM_DEV);
422 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
423 BlockErrorAction action = bdrv_get_error_action(s->qdev.conf.bs, is_read, error);
425 if (action == BLOCK_ERROR_ACTION_REPORT) {
428 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
431 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
434 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
437 scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED));
440 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
444 bdrv_error_action(s->qdev.conf.bs, action, is_read, error);
445 if (action == BLOCK_ERROR_ACTION_STOP) {
446 scsi_req_retry(&r->req);
448 return action != BLOCK_ERROR_ACTION_IGNORE;
451 static void scsi_write_complete(void * opaque, int ret)
453 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
454 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
457 if (r->req.aiocb != NULL) {
459 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
461 if (r->req.io_canceled) {
466 if (scsi_handle_rw_error(r, -ret)) {
471 n = r->qiov.size / 512;
473 r->sector_count -= n;
474 if (r->sector_count == 0) {
475 scsi_write_do_fua(r);
478 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
479 DPRINTF("Write complete tag=0x%x more=%zd\n", r->req.tag, r->qiov.size);
480 scsi_req_data(&r->req, r->qiov.size);
484 if (!r->req.io_canceled) {
485 scsi_req_unref(&r->req);
489 static void scsi_write_data(SCSIRequest *req)
491 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
492 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
495 /* No data transfer may already be in progress */
496 assert(r->req.aiocb == NULL);
498 /* The request is used as the AIO opaque value, so add a ref. */
499 scsi_req_ref(&r->req);
500 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
501 DPRINTF("Data transfer direction invalid\n");
502 scsi_write_complete(r, -EINVAL);
506 if (!r->req.sg && !r->qiov.size) {
507 /* Called for the first time. Ask the driver to send us more data. */
509 scsi_write_complete(r, 0);
513 scsi_write_complete(r, -ENOMEDIUM);
517 if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 ||
518 r->req.cmd.buf[0] == VERIFY_16) {
520 scsi_dma_complete_noio(r, 0);
522 scsi_write_complete(r, 0);
528 dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BLOCK_ACCT_WRITE);
529 r->req.resid -= r->req.sg->size;
530 r->req.aiocb = dma_bdrv_write(s->qdev.conf.bs, r->req.sg, r->sector,
531 scsi_dma_complete, r);
533 n = r->qiov.size / 512;
534 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct,
535 n * BDRV_SECTOR_SIZE, BLOCK_ACCT_WRITE);
536 r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, r->sector, &r->qiov, n,
537 scsi_write_complete, r);
541 /* Return a pointer to the data buffer. */
542 static uint8_t *scsi_get_buf(SCSIRequest *req)
544 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
546 return (uint8_t *)r->iov.iov_base;
549 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
551 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
555 if (req->cmd.buf[1] & 0x1) {
556 /* Vital product data */
557 uint8_t page_code = req->cmd.buf[2];
559 outbuf[buflen++] = s->qdev.type & 0x1f;
560 outbuf[buflen++] = page_code ; // this page
561 outbuf[buflen++] = 0x00;
562 outbuf[buflen++] = 0x00;
566 case 0x00: /* Supported page codes, mandatory */
568 DPRINTF("Inquiry EVPD[Supported pages] "
569 "buffer size %zd\n", req->cmd.xfer);
570 outbuf[buflen++] = 0x00; // list of supported pages (this page)
572 outbuf[buflen++] = 0x80; // unit serial number
574 outbuf[buflen++] = 0x83; // device identification
575 if (s->qdev.type == TYPE_DISK) {
576 outbuf[buflen++] = 0xb0; // block limits
577 outbuf[buflen++] = 0xb2; // thin provisioning
581 case 0x80: /* Device serial number, optional */
586 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
590 l = strlen(s->serial);
595 DPRINTF("Inquiry EVPD[Serial number] "
596 "buffer size %zd\n", req->cmd.xfer);
597 memcpy(outbuf+buflen, s->serial, l);
602 case 0x83: /* Device identification page, mandatory */
604 const char *str = s->serial ?: bdrv_get_device_name(s->qdev.conf.bs);
605 int max_len = s->serial ? 20 : 255 - 8;
606 int id_len = strlen(str);
608 if (id_len > max_len) {
611 DPRINTF("Inquiry EVPD[Device identification] "
612 "buffer size %zd\n", req->cmd.xfer);
614 outbuf[buflen++] = 0x2; // ASCII
615 outbuf[buflen++] = 0; // not officially assigned
616 outbuf[buflen++] = 0; // reserved
617 outbuf[buflen++] = id_len; // length of data following
618 memcpy(outbuf+buflen, str, id_len);
622 outbuf[buflen++] = 0x1; // Binary
623 outbuf[buflen++] = 0x3; // NAA
624 outbuf[buflen++] = 0; // reserved
625 outbuf[buflen++] = 8;
626 stq_be_p(&outbuf[buflen], s->wwn);
631 outbuf[buflen++] = 0x61; // SAS / Binary
632 outbuf[buflen++] = 0x93; // PIV / Target port / NAA
633 outbuf[buflen++] = 0; // reserved
634 outbuf[buflen++] = 8;
635 stq_be_p(&outbuf[buflen], s->port_wwn);
640 outbuf[buflen++] = 0x61; // SAS / Binary
641 outbuf[buflen++] = 0x94; // PIV / Target port / relative target port
642 outbuf[buflen++] = 0; // reserved
643 outbuf[buflen++] = 4;
644 stw_be_p(&outbuf[buflen + 2], s->port_index);
649 case 0xb0: /* block limits */
651 unsigned int unmap_sectors =
652 s->qdev.conf.discard_granularity / s->qdev.blocksize;
653 unsigned int min_io_size =
654 s->qdev.conf.min_io_size / s->qdev.blocksize;
655 unsigned int opt_io_size =
656 s->qdev.conf.opt_io_size / s->qdev.blocksize;
657 unsigned int max_unmap_sectors =
658 s->max_unmap_size / s->qdev.blocksize;
660 if (s->qdev.type == TYPE_ROM) {
661 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
665 /* required VPD size with unmap support */
667 memset(outbuf + 4, 0, buflen - 4);
669 outbuf[4] = 0x1; /* wsnz */
671 /* optimal transfer length granularity */
672 outbuf[6] = (min_io_size >> 8) & 0xff;
673 outbuf[7] = min_io_size & 0xff;
675 /* optimal transfer length */
676 outbuf[12] = (opt_io_size >> 24) & 0xff;
677 outbuf[13] = (opt_io_size >> 16) & 0xff;
678 outbuf[14] = (opt_io_size >> 8) & 0xff;
679 outbuf[15] = opt_io_size & 0xff;
681 /* max unmap LBA count, default is 1GB */
682 outbuf[20] = (max_unmap_sectors >> 24) & 0xff;
683 outbuf[21] = (max_unmap_sectors >> 16) & 0xff;
684 outbuf[22] = (max_unmap_sectors >> 8) & 0xff;
685 outbuf[23] = max_unmap_sectors & 0xff;
687 /* max unmap descriptors, 255 fit in 4 kb with an 8-byte header. */
693 /* optimal unmap granularity */
694 outbuf[28] = (unmap_sectors >> 24) & 0xff;
695 outbuf[29] = (unmap_sectors >> 16) & 0xff;
696 outbuf[30] = (unmap_sectors >> 8) & 0xff;
697 outbuf[31] = unmap_sectors & 0xff;
700 case 0xb2: /* thin provisioning */
704 outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */
705 outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1;
713 assert(buflen - start <= 255);
714 outbuf[start - 1] = buflen - start;
718 /* Standard INQUIRY data */
719 if (req->cmd.buf[2] != 0) {
724 buflen = req->cmd.xfer;
725 if (buflen > SCSI_MAX_INQUIRY_LEN) {
726 buflen = SCSI_MAX_INQUIRY_LEN;
729 outbuf[0] = s->qdev.type & 0x1f;
730 outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0;
732 strpadcpy((char *) &outbuf[16], 16, s->product, ' ');
733 strpadcpy((char *) &outbuf[8], 8, s->vendor, ' ');
735 memset(&outbuf[32], 0, 4);
736 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
738 * We claim conformance to SPC-3, which is required for guests
739 * to ask for modern features like READ CAPACITY(16) or the
740 * block characteristics VPD page by default. Not all of SPC-3
741 * is actually implemented, but we're good enough.
744 outbuf[3] = 2 | 0x10; /* Format 2, HiSup */
747 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
749 /* If the allocation length of CDB is too small,
750 the additional length is not adjusted */
754 /* Sync data transfer and TCQ. */
755 outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0);
759 static inline bool media_is_dvd(SCSIDiskState *s)
762 if (s->qdev.type != TYPE_ROM) {
765 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
768 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
769 return nb_sectors > CD_MAX_SECTORS;
772 static inline bool media_is_cd(SCSIDiskState *s)
775 if (s->qdev.type != TYPE_ROM) {
778 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
781 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
782 return nb_sectors <= CD_MAX_SECTORS;
785 static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r,
788 uint8_t type = r->req.cmd.buf[1] & 7;
790 if (s->qdev.type != TYPE_ROM) {
794 /* Types 1/2 are only defined for Blu-Ray. */
796 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
800 memset(outbuf, 0, 34);
802 outbuf[2] = 0xe; /* last session complete, disc finalized */
803 outbuf[3] = 1; /* first track on disc */
804 outbuf[4] = 1; /* # of sessions */
805 outbuf[5] = 1; /* first track of last session */
806 outbuf[6] = 1; /* last track of last session */
807 outbuf[7] = 0x20; /* unrestricted use */
808 outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */
809 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
810 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
811 /* 24-31: disc bar code */
812 /* 32: disc application code */
813 /* 33: number of OPC tables */
818 static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
821 static const int rds_caps_size[5] = {
828 uint8_t media = r->req.cmd.buf[1];
829 uint8_t layer = r->req.cmd.buf[6];
830 uint8_t format = r->req.cmd.buf[7];
833 if (s->qdev.type != TYPE_ROM) {
837 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
841 if (format != 0xff) {
842 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
843 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
846 if (media_is_cd(s)) {
847 scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT));
850 if (format >= ARRAY_SIZE(rds_caps_size)) {
853 size = rds_caps_size[format];
854 memset(outbuf, 0, size);
859 /* Physical format information */
864 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
866 outbuf[4] = 1; /* DVD-ROM, part version 1 */
867 outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
868 outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
869 outbuf[7] = 0; /* default densities */
871 stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */
872 stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */
876 case 0x01: /* DVD copyright information, all zeros */
879 case 0x03: /* BCA information - invalid field for no BCA info */
882 case 0x04: /* DVD disc manufacturing information, all zeros */
885 case 0xff: { /* List capabilities */
888 for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) {
889 if (!rds_caps_size[i]) {
893 outbuf[size + 1] = 0x40; /* Not writable, readable */
894 stw_be_p(&outbuf[size + 2], rds_caps_size[i]);
904 /* Size of buffer, not including 2 byte size field */
905 stw_be_p(outbuf, size - 2);
912 static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
914 uint8_t event_code, media_status;
918 media_status = MS_TRAY_OPEN;
919 } else if (bdrv_is_inserted(s->qdev.conf.bs)) {
920 media_status = MS_MEDIA_PRESENT;
923 /* Event notification descriptor */
924 event_code = MEC_NO_CHANGE;
925 if (media_status != MS_TRAY_OPEN) {
926 if (s->media_event) {
927 event_code = MEC_NEW_MEDIA;
928 s->media_event = false;
929 } else if (s->eject_request) {
930 event_code = MEC_EJECT_REQUESTED;
931 s->eject_request = false;
935 outbuf[0] = event_code;
936 outbuf[1] = media_status;
938 /* These fields are reserved, just clear them. */
944 static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r,
948 uint8_t *buf = r->req.cmd.buf;
949 uint8_t notification_class_request = buf[4];
950 if (s->qdev.type != TYPE_ROM) {
953 if ((buf[1] & 1) == 0) {
959 outbuf[0] = outbuf[1] = 0;
960 outbuf[3] = 1 << GESN_MEDIA; /* supported events */
961 if (notification_class_request & (1 << GESN_MEDIA)) {
962 outbuf[2] = GESN_MEDIA;
963 size += scsi_event_status_media(s, &outbuf[size]);
967 stw_be_p(outbuf, size - 4);
971 static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
975 if (s->qdev.type != TYPE_ROM) {
978 current = media_is_dvd(s) ? MMC_PROFILE_DVD_ROM : MMC_PROFILE_CD_ROM;
979 memset(outbuf, 0, 40);
980 stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */
981 stw_be_p(&outbuf[6], current);
982 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
983 outbuf[10] = 0x03; /* persistent, current */
984 outbuf[11] = 8; /* two profiles */
985 stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM);
986 outbuf[14] = (current == MMC_PROFILE_DVD_ROM);
987 stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM);
988 outbuf[18] = (current == MMC_PROFILE_CD_ROM);
989 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
990 stw_be_p(&outbuf[20], 1);
991 outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */
993 stl_be_p(&outbuf[24], 1); /* SCSI */
994 outbuf[28] = 1; /* DBE = 1, mandatory */
995 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
996 stw_be_p(&outbuf[32], 3);
997 outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */
999 outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
1000 /* TODO: Random readable, CD read, DVD read, drive serial number,
1005 static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf)
1007 if (s->qdev.type != TYPE_ROM) {
1010 memset(outbuf, 0, 8);
1011 outbuf[5] = 1; /* CD-ROM */
1015 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
1018 static const int mode_sense_valid[0x3f] = {
1019 [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK),
1020 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK),
1021 [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1022 [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
1023 [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM),
1024 [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM),
1027 uint8_t *p = *p_outbuf + 2;
1030 if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
1035 * If Changeable Values are requested, a mask denoting those mode parameters
1036 * that are changeable shall be returned. As we currently don't support
1037 * parameter changes via MODE_SELECT all bits are returned set to zero.
1038 * The buffer was already menset to zero by the caller of this function.
1040 * The offsets here are off by two compared to the descriptions in the
1041 * SCSI specs, because those include a 2-byte header. This is unfortunate,
1042 * but it is done so that offsets are consistent within our implementation
1043 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
1044 * 2-byte and 4-byte headers.
1047 case MODE_PAGE_HD_GEOMETRY:
1049 if (page_control == 1) { /* Changeable Values */
1052 /* if a geometry hint is available, use it */
1053 p[0] = (s->qdev.conf.cyls >> 16) & 0xff;
1054 p[1] = (s->qdev.conf.cyls >> 8) & 0xff;
1055 p[2] = s->qdev.conf.cyls & 0xff;
1056 p[3] = s->qdev.conf.heads & 0xff;
1057 /* Write precomp start cylinder, disabled */
1058 p[4] = (s->qdev.conf.cyls >> 16) & 0xff;
1059 p[5] = (s->qdev.conf.cyls >> 8) & 0xff;
1060 p[6] = s->qdev.conf.cyls & 0xff;
1061 /* Reduced current start cylinder, disabled */
1062 p[7] = (s->qdev.conf.cyls >> 16) & 0xff;
1063 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1064 p[9] = s->qdev.conf.cyls & 0xff;
1065 /* Device step rate [ns], 200ns */
1068 /* Landing zone cylinder */
1072 /* Medium rotation rate [rpm], 5400 rpm */
1073 p[18] = (5400 >> 8) & 0xff;
1074 p[19] = 5400 & 0xff;
1077 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
1079 if (page_control == 1) { /* Changeable Values */
1082 /* Transfer rate [kbit/s], 5Mbit/s */
1085 /* if a geometry hint is available, use it */
1086 p[2] = s->qdev.conf.heads & 0xff;
1087 p[3] = s->qdev.conf.secs & 0xff;
1088 p[4] = s->qdev.blocksize >> 8;
1089 p[6] = (s->qdev.conf.cyls >> 8) & 0xff;
1090 p[7] = s->qdev.conf.cyls & 0xff;
1091 /* Write precomp start cylinder, disabled */
1092 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1093 p[9] = s->qdev.conf.cyls & 0xff;
1094 /* Reduced current start cylinder, disabled */
1095 p[10] = (s->qdev.conf.cyls >> 8) & 0xff;
1096 p[11] = s->qdev.conf.cyls & 0xff;
1097 /* Device step rate [100us], 100us */
1100 /* Device step pulse width [us], 1us */
1102 /* Device head settle delay [100us], 100us */
1105 /* Motor on delay [0.1s], 0.1s */
1107 /* Motor off delay [0.1s], 0.1s */
1109 /* Medium rotation rate [rpm], 5400 rpm */
1110 p[26] = (5400 >> 8) & 0xff;
1111 p[27] = 5400 & 0xff;
1114 case MODE_PAGE_CACHING:
1116 if (page_control == 1 || /* Changeable Values */
1117 bdrv_enable_write_cache(s->qdev.conf.bs)) {
1122 case MODE_PAGE_R_W_ERROR:
1124 if (page_control == 1) { /* Changeable Values */
1127 p[0] = 0x80; /* Automatic Write Reallocation Enabled */
1128 if (s->qdev.type == TYPE_ROM) {
1129 p[1] = 0x20; /* Read Retry Count */
1133 case MODE_PAGE_AUDIO_CTL:
1137 case MODE_PAGE_CAPABILITIES:
1139 if (page_control == 1) { /* Changeable Values */
1143 p[0] = 0x3b; /* CD-R & CD-RW read */
1144 p[1] = 0; /* Writing not supported */
1145 p[2] = 0x7f; /* Audio, composite, digital out,
1146 mode 2 form 1&2, multi session */
1147 p[3] = 0xff; /* CD DA, DA accurate, RW supported,
1148 RW corrected, C2 errors, ISRC,
1150 p[4] = 0x2d | (s->tray_locked ? 2 : 0);
1151 /* Locking supported, jumper present, eject, tray */
1152 p[5] = 0; /* no volume & mute control, no
1154 p[6] = (50 * 176) >> 8; /* 50x read speed */
1155 p[7] = (50 * 176) & 0xff;
1156 p[8] = 2 >> 8; /* Two volume levels */
1158 p[10] = 2048 >> 8; /* 2M buffer */
1159 p[11] = 2048 & 0xff;
1160 p[12] = (16 * 176) >> 8; /* 16x read speed current */
1161 p[13] = (16 * 176) & 0xff;
1162 p[16] = (16 * 176) >> 8; /* 16x write speed */
1163 p[17] = (16 * 176) & 0xff;
1164 p[18] = (16 * 176) >> 8; /* 16x write speed current */
1165 p[19] = (16 * 176) & 0xff;
1172 assert(length < 256);
1173 (*p_outbuf)[0] = page;
1174 (*p_outbuf)[1] = length;
1175 *p_outbuf += length + 2;
1179 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
1181 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1182 uint64_t nb_sectors;
1184 int page, buflen, ret, page_control;
1186 uint8_t dev_specific_param;
1188 dbd = (r->req.cmd.buf[1] & 0x8) != 0;
1189 page = r->req.cmd.buf[2] & 0x3f;
1190 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
1191 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
1192 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
1193 memset(outbuf, 0, r->req.cmd.xfer);
1196 if (s->qdev.type == TYPE_DISK) {
1197 dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0;
1198 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1199 dev_specific_param |= 0x80; /* Readonly. */
1202 /* MMC prescribes that CD/DVD drives have no block descriptors,
1203 * and defines no device-specific parameter. */
1204 dev_specific_param = 0x00;
1208 if (r->req.cmd.buf[0] == MODE_SENSE) {
1209 p[1] = 0; /* Default media type. */
1210 p[2] = dev_specific_param;
1211 p[3] = 0; /* Block descriptor length. */
1213 } else { /* MODE_SENSE_10 */
1214 p[2] = 0; /* Default media type. */
1215 p[3] = dev_specific_param;
1216 p[6] = p[7] = 0; /* Block descriptor length. */
1220 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1221 if (!dbd && nb_sectors) {
1222 if (r->req.cmd.buf[0] == MODE_SENSE) {
1223 outbuf[3] = 8; /* Block descriptor length */
1224 } else { /* MODE_SENSE_10 */
1225 outbuf[7] = 8; /* Block descriptor length */
1227 nb_sectors /= (s->qdev.blocksize / 512);
1228 if (nb_sectors > 0xffffff) {
1231 p[0] = 0; /* media density code */
1232 p[1] = (nb_sectors >> 16) & 0xff;
1233 p[2] = (nb_sectors >> 8) & 0xff;
1234 p[3] = nb_sectors & 0xff;
1235 p[4] = 0; /* reserved */
1236 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
1237 p[6] = s->qdev.blocksize >> 8;
1242 if (page_control == 3) {
1244 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
1249 for (page = 0; page <= 0x3e; page++) {
1250 mode_sense_page(s, page, &p, page_control);
1253 ret = mode_sense_page(s, page, &p, page_control);
1259 buflen = p - outbuf;
1261 * The mode data length field specifies the length in bytes of the
1262 * following data that is available to be transferred. The mode data
1263 * length does not include itself.
1265 if (r->req.cmd.buf[0] == MODE_SENSE) {
1266 outbuf[0] = buflen - 1;
1267 } else { /* MODE_SENSE_10 */
1268 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1269 outbuf[1] = (buflen - 2) & 0xff;
1274 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1276 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1277 int start_track, format, msf, toclen;
1278 uint64_t nb_sectors;
1280 msf = req->cmd.buf[1] & 2;
1281 format = req->cmd.buf[2] & 0xf;
1282 start_track = req->cmd.buf[6];
1283 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1284 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
1285 nb_sectors /= s->qdev.blocksize / 512;
1288 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
1291 /* multi session : only a single session defined */
1293 memset(outbuf, 0, 12);
1299 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
1307 static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
1309 SCSIRequest *req = &r->req;
1310 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1311 bool start = req->cmd.buf[4] & 1;
1312 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
1313 int pwrcnd = req->cmd.buf[4] & 0xf0;
1316 /* eject/load only happens for power condition == 0 */
1320 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) {
1321 if (!start && !s->tray_open && s->tray_locked) {
1322 scsi_check_condition(r,
1323 bdrv_is_inserted(s->qdev.conf.bs)
1324 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1325 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1329 if (s->tray_open != !start) {
1330 bdrv_eject(s->qdev.conf.bs, !start);
1331 s->tray_open = !start;
1337 static void scsi_disk_emulate_read_data(SCSIRequest *req)
1339 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1340 int buflen = r->iov.iov_len;
1343 DPRINTF("Read buf_len=%d\n", buflen);
1346 scsi_req_data(&r->req, buflen);
1350 /* This also clears the sense buffer for REQUEST SENSE. */
1351 scsi_req_complete(&r->req, GOOD);
1354 static int scsi_disk_check_mode_select(SCSIDiskState *s, int page,
1355 uint8_t *inbuf, int inlen)
1357 uint8_t mode_current[SCSI_MAX_MODE_LEN];
1358 uint8_t mode_changeable[SCSI_MAX_MODE_LEN];
1360 int len, expected_len, changeable_len, i;
1362 /* The input buffer does not include the page header, so it is
1365 expected_len = inlen + 2;
1366 if (expected_len > SCSI_MAX_MODE_LEN) {
1371 memset(mode_current, 0, inlen + 2);
1372 len = mode_sense_page(s, page, &p, 0);
1373 if (len < 0 || len != expected_len) {
1377 p = mode_changeable;
1378 memset(mode_changeable, 0, inlen + 2);
1379 changeable_len = mode_sense_page(s, page, &p, 1);
1380 assert(changeable_len == len);
1382 /* Check that unchangeable bits are the same as what MODE SENSE
1385 for (i = 2; i < len; i++) {
1386 if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) {
1393 static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p)
1396 case MODE_PAGE_CACHING:
1397 bdrv_set_enable_write_cache(s->qdev.conf.bs, (p[0] & 4) != 0);
1405 static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change)
1407 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1410 int page, subpage, page_len;
1412 /* Parse both possible formats for the mode page headers. */
1416 goto invalid_param_len;
1419 page_len = lduw_be_p(&p[2]);
1424 goto invalid_param_len;
1435 if (page_len > len) {
1436 goto invalid_param_len;
1440 if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) {
1444 scsi_disk_apply_mode_select(s, page, p);
1453 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1457 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1461 static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
1463 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1465 int cmd = r->req.cmd.buf[0];
1466 int len = r->req.cmd.xfer;
1467 int hdr_len = (cmd == MODE_SELECT ? 4 : 8);
1471 /* We only support PF=1, SP=0. */
1472 if ((r->req.cmd.buf[1] & 0x11) != 0x10) {
1476 if (len < hdr_len) {
1477 goto invalid_param_len;
1480 bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6]));
1484 goto invalid_param_len;
1486 if (bd_len != 0 && bd_len != 8) {
1493 /* Ensure no change is made if there is an error! */
1494 for (pass = 0; pass < 2; pass++) {
1495 if (mode_select_pages(r, p, len, pass == 1) < 0) {
1500 if (!bdrv_enable_write_cache(s->qdev.conf.bs)) {
1501 /* The request is used as the AIO opaque value, so add a ref. */
1502 scsi_req_ref(&r->req);
1503 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct, 0,
1505 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
1509 scsi_req_complete(&r->req, GOOD);
1513 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1517 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1521 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1524 static inline bool check_lba_range(SCSIDiskState *s,
1525 uint64_t sector_num, uint32_t nb_sectors)
1528 * The first line tests that no overflow happens when computing the last
1529 * sector. The second line tests that the last accessed sector is in
1532 * Careful, the computations should not underflow for nb_sectors == 0,
1533 * and a 0-block read to the first LBA beyond the end of device is
1536 return (sector_num <= sector_num + nb_sectors &&
1537 sector_num + nb_sectors <= s->qdev.max_lba + 1);
1540 typedef struct UnmapCBData {
1546 static void scsi_unmap_complete(void *opaque, int ret)
1548 UnmapCBData *data = opaque;
1549 SCSIDiskReq *r = data->r;
1550 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1551 uint64_t sector_num;
1552 uint32_t nb_sectors;
1554 r->req.aiocb = NULL;
1555 if (r->req.io_canceled) {
1560 if (scsi_handle_rw_error(r, -ret)) {
1565 if (data->count > 0) {
1566 sector_num = ldq_be_p(&data->inbuf[0]);
1567 nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
1568 if (!check_lba_range(s, sector_num, nb_sectors)) {
1569 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1573 r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs,
1574 sector_num * (s->qdev.blocksize / 512),
1575 nb_sectors * (s->qdev.blocksize / 512),
1576 scsi_unmap_complete, data);
1582 scsi_req_complete(&r->req, GOOD);
1585 if (!r->req.io_canceled) {
1586 scsi_req_unref(&r->req);
1591 static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
1593 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1595 int len = r->req.cmd.xfer;
1598 /* Reject ANCHOR=1. */
1599 if (r->req.cmd.buf[1] & 0x1) {
1604 goto invalid_param_len;
1606 if (len < lduw_be_p(&p[0]) + 2) {
1607 goto invalid_param_len;
1609 if (len < lduw_be_p(&p[2]) + 8) {
1610 goto invalid_param_len;
1612 if (lduw_be_p(&p[2]) & 15) {
1613 goto invalid_param_len;
1616 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1617 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1621 data = g_new0(UnmapCBData, 1);
1623 data->inbuf = &p[8];
1624 data->count = lduw_be_p(&p[2]) >> 4;
1626 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1627 scsi_req_ref(&r->req);
1628 scsi_unmap_complete(data, 0);
1632 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1636 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1639 typedef struct WriteSameCBData {
1647 static void scsi_write_same_complete(void *opaque, int ret)
1649 WriteSameCBData *data = opaque;
1650 SCSIDiskReq *r = data->r;
1651 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1653 assert(r->req.aiocb != NULL);
1654 r->req.aiocb = NULL;
1655 block_acct_done(bdrv_get_stats(s->qdev.conf.bs), &r->acct);
1656 if (r->req.io_canceled) {
1661 if (scsi_handle_rw_error(r, -ret)) {
1666 data->nb_sectors -= data->iov.iov_len / 512;
1667 data->sector += data->iov.iov_len / 512;
1668 data->iov.iov_len = MIN(data->nb_sectors * 512, data->iov.iov_len);
1669 if (data->iov.iov_len) {
1670 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct,
1671 data->iov.iov_len, BLOCK_ACCT_WRITE);
1672 r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, data->sector,
1673 &data->qiov, data->iov.iov_len / 512,
1674 scsi_write_same_complete, data);
1678 scsi_req_complete(&r->req, GOOD);
1681 if (!r->req.io_canceled) {
1682 scsi_req_unref(&r->req);
1684 qemu_vfree(data->iov.iov_base);
1688 static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
1690 SCSIRequest *req = &r->req;
1691 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1692 uint32_t nb_sectors = scsi_data_cdb_length(r->req.cmd.buf);
1693 WriteSameCBData *data;
1697 /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */
1698 if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) {
1699 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1703 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1704 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1707 if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
1708 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1712 if (buffer_is_zero(inbuf, s->qdev.blocksize)) {
1713 int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0;
1715 /* The request is used as the AIO opaque value, so add a ref. */
1716 scsi_req_ref(&r->req);
1717 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct,
1718 nb_sectors * s->qdev.blocksize,
1720 r->req.aiocb = bdrv_aio_write_zeroes(s->qdev.conf.bs,
1721 r->req.cmd.lba * (s->qdev.blocksize / 512),
1722 nb_sectors * (s->qdev.blocksize / 512),
1723 flags, scsi_aio_complete, r);
1727 data = g_new0(WriteSameCBData, 1);
1729 data->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1730 data->nb_sectors = nb_sectors * (s->qdev.blocksize / 512);
1731 data->iov.iov_len = MIN(data->nb_sectors * 512, SCSI_WRITE_SAME_MAX);
1732 data->iov.iov_base = buf = qemu_blockalign(s->qdev.conf.bs, data->iov.iov_len);
1733 qemu_iovec_init_external(&data->qiov, &data->iov, 1);
1735 for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) {
1736 memcpy(&buf[i], inbuf, s->qdev.blocksize);
1739 scsi_req_ref(&r->req);
1740 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct,
1741 data->iov.iov_len, BLOCK_ACCT_WRITE);
1742 r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, data->sector,
1743 &data->qiov, data->iov.iov_len / 512,
1744 scsi_write_same_complete, data);
1747 static void scsi_disk_emulate_write_data(SCSIRequest *req)
1749 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1751 if (r->iov.iov_len) {
1752 int buflen = r->iov.iov_len;
1753 DPRINTF("Write buf_len=%d\n", buflen);
1755 scsi_req_data(&r->req, buflen);
1759 switch (req->cmd.buf[0]) {
1761 case MODE_SELECT_10:
1762 /* This also clears the sense buffer for REQUEST SENSE. */
1763 scsi_disk_emulate_mode_select(r, r->iov.iov_base);
1767 scsi_disk_emulate_unmap(r, r->iov.iov_base);
1773 if (r->req.status == -1) {
1774 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1780 scsi_disk_emulate_write_same(r, r->iov.iov_base);
1788 static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
1790 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1791 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1792 uint64_t nb_sectors;
1796 switch (req->cmd.buf[0]) {
1805 case ALLOW_MEDIUM_REMOVAL:
1806 case GET_CONFIGURATION:
1807 case GET_EVENT_STATUS_NOTIFICATION:
1808 case MECHANISM_STATUS:
1813 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1814 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1821 * FIXME: we shouldn't return anything bigger than 4k, but the code
1822 * requires the buffer to be as big as req->cmd.xfer in several
1823 * places. So, do not allow CDBs with a very large ALLOCATION
1824 * LENGTH. The real fix would be to modify scsi_read_data and
1825 * dma_buf_read, so that they return data beyond the buflen
1828 if (req->cmd.xfer > 65536) {
1829 goto illegal_request;
1831 r->buflen = MAX(4096, req->cmd.xfer);
1833 if (!r->iov.iov_base) {
1834 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
1837 buflen = req->cmd.xfer;
1838 outbuf = r->iov.iov_base;
1839 memset(outbuf, 0, r->buflen);
1840 switch (req->cmd.buf[0]) {
1841 case TEST_UNIT_READY:
1842 assert(!s->tray_open && bdrv_is_inserted(s->qdev.conf.bs));
1845 buflen = scsi_disk_emulate_inquiry(req, outbuf);
1847 goto illegal_request;
1852 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
1854 goto illegal_request;
1858 buflen = scsi_disk_emulate_read_toc(req, outbuf);
1860 goto illegal_request;
1864 if (req->cmd.buf[1] & 1) {
1865 goto illegal_request;
1869 if (req->cmd.buf[1] & 3) {
1870 goto illegal_request;
1874 if (req->cmd.buf[1] & 1) {
1875 goto illegal_request;
1879 if (req->cmd.buf[1] & 3) {
1880 goto illegal_request;
1884 if (scsi_disk_emulate_start_stop(r) < 0) {
1888 case ALLOW_MEDIUM_REMOVAL:
1889 s->tray_locked = req->cmd.buf[4] & 1;
1890 bdrv_lock_medium(s->qdev.conf.bs, req->cmd.buf[4] & 1);
1892 case READ_CAPACITY_10:
1893 /* The normal LEN field for this command is zero. */
1894 memset(outbuf, 0, 8);
1895 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1897 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1900 if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
1901 goto illegal_request;
1903 nb_sectors /= s->qdev.blocksize / 512;
1904 /* Returned value is the address of the last sector. */
1906 /* Remember the new size for read/write sanity checking. */
1907 s->qdev.max_lba = nb_sectors;
1908 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1909 if (nb_sectors > UINT32_MAX) {
1910 nb_sectors = UINT32_MAX;
1912 outbuf[0] = (nb_sectors >> 24) & 0xff;
1913 outbuf[1] = (nb_sectors >> 16) & 0xff;
1914 outbuf[2] = (nb_sectors >> 8) & 0xff;
1915 outbuf[3] = nb_sectors & 0xff;
1918 outbuf[6] = s->qdev.blocksize >> 8;
1922 /* Just return "NO SENSE". */
1923 buflen = scsi_build_sense(NULL, 0, outbuf, r->buflen,
1924 (req->cmd.buf[1] & 1) == 0);
1926 goto illegal_request;
1929 case MECHANISM_STATUS:
1930 buflen = scsi_emulate_mechanism_status(s, outbuf);
1932 goto illegal_request;
1935 case GET_CONFIGURATION:
1936 buflen = scsi_get_configuration(s, outbuf);
1938 goto illegal_request;
1941 case GET_EVENT_STATUS_NOTIFICATION:
1942 buflen = scsi_get_event_status_notification(s, r, outbuf);
1944 goto illegal_request;
1947 case READ_DISC_INFORMATION:
1948 buflen = scsi_read_disc_information(s, r, outbuf);
1950 goto illegal_request;
1953 case READ_DVD_STRUCTURE:
1954 buflen = scsi_read_dvd_structure(s, r, outbuf);
1956 goto illegal_request;
1959 case SERVICE_ACTION_IN_16:
1960 /* Service Action In subcommands. */
1961 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
1962 DPRINTF("SAI READ CAPACITY(16)\n");
1963 memset(outbuf, 0, req->cmd.xfer);
1964 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1966 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1969 if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
1970 goto illegal_request;
1972 nb_sectors /= s->qdev.blocksize / 512;
1973 /* Returned value is the address of the last sector. */
1975 /* Remember the new size for read/write sanity checking. */
1976 s->qdev.max_lba = nb_sectors;
1977 outbuf[0] = (nb_sectors >> 56) & 0xff;
1978 outbuf[1] = (nb_sectors >> 48) & 0xff;
1979 outbuf[2] = (nb_sectors >> 40) & 0xff;
1980 outbuf[3] = (nb_sectors >> 32) & 0xff;
1981 outbuf[4] = (nb_sectors >> 24) & 0xff;
1982 outbuf[5] = (nb_sectors >> 16) & 0xff;
1983 outbuf[6] = (nb_sectors >> 8) & 0xff;
1984 outbuf[7] = nb_sectors & 0xff;
1987 outbuf[10] = s->qdev.blocksize >> 8;
1990 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
1992 /* set TPE bit if the format supports discard */
1993 if (s->qdev.conf.discard_granularity) {
1997 /* Protection, exponent and lowest lba field left blank. */
2000 DPRINTF("Unsupported Service Action In\n");
2001 goto illegal_request;
2002 case SYNCHRONIZE_CACHE:
2003 /* The request is used as the AIO opaque value, so add a ref. */
2004 scsi_req_ref(&r->req);
2005 block_acct_start(bdrv_get_stats(s->qdev.conf.bs), &r->acct, 0,
2007 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
2010 DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
2011 if (r->req.cmd.lba > s->qdev.max_lba) {
2016 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
2018 case MODE_SELECT_10:
2019 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
2022 DPRINTF("Unmap (len %lu)\n", (long)r->req.cmd.xfer);
2027 DPRINTF("Verify (bytchk %d)\n", (req->cmd.buf[1] >> 1) & 3);
2028 if (req->cmd.buf[1] & 6) {
2029 goto illegal_request;
2034 DPRINTF("WRITE SAME %d (len %lu)\n",
2035 req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16,
2036 (long)r->req.cmd.xfer);
2039 DPRINTF("Unknown SCSI command (%2.2x=%s)\n", buf[0],
2040 scsi_command_name(buf[0]));
2041 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
2044 assert(!r->req.aiocb);
2045 r->iov.iov_len = MIN(r->buflen, req->cmd.xfer);
2046 if (r->iov.iov_len == 0) {
2047 scsi_req_complete(&r->req, GOOD);
2049 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2050 assert(r->iov.iov_len == req->cmd.xfer);
2051 return -r->iov.iov_len;
2053 return r->iov.iov_len;
2057 if (r->req.status == -1) {
2058 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2063 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2067 /* Execute a scsi command. Returns the length of the data expected by the
2068 command. This will be Positive for data transfers from the device
2069 (eg. disk reads), negative for transfers to the device (eg. disk writes),
2070 and zero if the command does not transfer any data. */
2072 static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
2074 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2075 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
2081 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
2082 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
2086 len = scsi_data_cdb_length(r->req.cmd.buf);
2092 DPRINTF("Read (sector %" PRId64 ", count %u)\n", r->req.cmd.lba, len);
2093 if (r->req.cmd.buf[1] & 0xe0) {
2094 goto illegal_request;
2096 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2099 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2100 r->sector_count = len * (s->qdev.blocksize / 512);
2106 case WRITE_VERIFY_10:
2107 case WRITE_VERIFY_12:
2108 case WRITE_VERIFY_16:
2109 if (bdrv_is_read_only(s->qdev.conf.bs)) {
2110 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
2113 DPRINTF("Write %s(sector %" PRId64 ", count %u)\n",
2114 (command & 0xe) == 0xe ? "And Verify " : "",
2115 r->req.cmd.lba, len);
2116 if (r->req.cmd.buf[1] & 0xe0) {
2117 goto illegal_request;
2119 if (!check_lba_range(s, r->req.cmd.lba, len)) {
2122 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
2123 r->sector_count = len * (s->qdev.blocksize / 512);
2128 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2131 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
2134 if (r->sector_count == 0) {
2135 scsi_req_complete(&r->req, GOOD);
2137 assert(r->iov.iov_len == 0);
2138 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
2139 return -r->sector_count * 512;
2141 return r->sector_count * 512;
2145 static void scsi_disk_reset(DeviceState *dev)
2147 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
2148 uint64_t nb_sectors;
2150 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
2152 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
2153 nb_sectors /= s->qdev.blocksize / 512;
2157 s->qdev.max_lba = nb_sectors;
2158 /* reset tray statuses */
2163 static void scsi_unrealize(SCSIDevice *dev, Error **errp)
2165 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2167 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
2168 blockdev_mark_auto_del(s->qdev.conf.bs);
2171 static void scsi_disk_resize_cb(void *opaque)
2173 SCSIDiskState *s = opaque;
2175 /* SPC lists this sense code as available only for
2176 * direct-access devices.
2178 if (s->qdev.type == TYPE_DISK) {
2179 scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED));
2183 static void scsi_cd_change_media_cb(void *opaque, bool load)
2185 SCSIDiskState *s = opaque;
2188 * When a CD gets changed, we have to report an ejected state and
2189 * then a loaded state to guests so that they detect tray
2190 * open/close and media change events. Guests that do not use
2191 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
2192 * states rely on this behavior.
2194 * media_changed governs the state machine used for unit attention
2195 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
2197 s->media_changed = load;
2198 s->tray_open = !load;
2199 scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM));
2200 s->media_event = true;
2201 s->eject_request = false;
2204 static void scsi_cd_eject_request_cb(void *opaque, bool force)
2206 SCSIDiskState *s = opaque;
2208 s->eject_request = true;
2210 s->tray_locked = false;
2214 static bool scsi_cd_is_tray_open(void *opaque)
2216 return ((SCSIDiskState *)opaque)->tray_open;
2219 static bool scsi_cd_is_medium_locked(void *opaque)
2221 return ((SCSIDiskState *)opaque)->tray_locked;
2224 static const BlockDevOps scsi_disk_removable_block_ops = {
2225 .change_media_cb = scsi_cd_change_media_cb,
2226 .eject_request_cb = scsi_cd_eject_request_cb,
2227 .is_tray_open = scsi_cd_is_tray_open,
2228 .is_medium_locked = scsi_cd_is_medium_locked,
2230 .resize_cb = scsi_disk_resize_cb,
2233 static const BlockDevOps scsi_disk_block_ops = {
2234 .resize_cb = scsi_disk_resize_cb,
2237 static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
2239 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2240 if (s->media_changed) {
2241 s->media_changed = false;
2242 scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED));
2246 static void scsi_realize(SCSIDevice *dev, Error **errp)
2248 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2251 if (!s->qdev.conf.bs) {
2252 error_setg(errp, "drive property not set");
2256 if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2257 !bdrv_is_inserted(s->qdev.conf.bs)) {
2258 error_setg(errp, "Device needs media, but drive is empty");
2262 blkconf_serial(&s->qdev.conf, &s->serial);
2263 if (dev->type == TYPE_DISK) {
2264 blkconf_geometry(&dev->conf, NULL, 65535, 255, 255, &err);
2266 error_propagate(errp, err);
2271 if (s->qdev.conf.discard_granularity == -1) {
2272 s->qdev.conf.discard_granularity =
2273 MAX(s->qdev.conf.logical_block_size, DEFAULT_DISCARD_GRANULARITY);
2277 s->version = g_strdup(qemu_get_version());
2280 s->vendor = g_strdup("QEMU");
2283 if (bdrv_is_sg(s->qdev.conf.bs)) {
2284 error_setg(errp, "unwanted /dev/sg*");
2288 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2289 !(s->features & (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS))) {
2290 bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_removable_block_ops, s);
2292 bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_block_ops, s);
2294 bdrv_set_guest_block_size(s->qdev.conf.bs, s->qdev.blocksize);
2296 bdrv_iostatus_enable(s->qdev.conf.bs);
2297 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, NULL);
2300 static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
2302 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2303 s->qdev.blocksize = s->qdev.conf.logical_block_size;
2304 s->qdev.type = TYPE_DISK;
2306 s->product = g_strdup("QEMU HARDDISK");
2308 scsi_realize(&s->qdev, errp);
2311 static void scsi_cd_realize(SCSIDevice *dev, Error **errp)
2313 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2314 s->qdev.blocksize = 2048;
2315 s->qdev.type = TYPE_ROM;
2316 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2318 s->product = g_strdup("QEMU CD-ROM");
2320 scsi_realize(&s->qdev, errp);
2323 static void scsi_disk_realize(SCSIDevice *dev, Error **errp)
2326 Error *local_err = NULL;
2328 if (!dev->conf.bs) {
2329 scsi_realize(dev, &local_err);
2331 error_propagate(errp, local_err);
2335 dinfo = drive_get_by_blockdev(dev->conf.bs);
2336 if (dinfo->media_cd) {
2337 scsi_cd_realize(dev, errp);
2339 scsi_hd_realize(dev, errp);
2343 static const SCSIReqOps scsi_disk_emulate_reqops = {
2344 .size = sizeof(SCSIDiskReq),
2345 .free_req = scsi_free_request,
2346 .send_command = scsi_disk_emulate_command,
2347 .read_data = scsi_disk_emulate_read_data,
2348 .write_data = scsi_disk_emulate_write_data,
2349 .cancel_io = scsi_cancel_io,
2350 .get_buf = scsi_get_buf,
2353 static const SCSIReqOps scsi_disk_dma_reqops = {
2354 .size = sizeof(SCSIDiskReq),
2355 .free_req = scsi_free_request,
2356 .send_command = scsi_disk_dma_command,
2357 .read_data = scsi_read_data,
2358 .write_data = scsi_write_data,
2359 .cancel_io = scsi_cancel_io,
2360 .get_buf = scsi_get_buf,
2361 .load_request = scsi_disk_load_request,
2362 .save_request = scsi_disk_save_request,
2365 static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {
2366 [TEST_UNIT_READY] = &scsi_disk_emulate_reqops,
2367 [INQUIRY] = &scsi_disk_emulate_reqops,
2368 [MODE_SENSE] = &scsi_disk_emulate_reqops,
2369 [MODE_SENSE_10] = &scsi_disk_emulate_reqops,
2370 [START_STOP] = &scsi_disk_emulate_reqops,
2371 [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops,
2372 [READ_CAPACITY_10] = &scsi_disk_emulate_reqops,
2373 [READ_TOC] = &scsi_disk_emulate_reqops,
2374 [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops,
2375 [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops,
2376 [GET_CONFIGURATION] = &scsi_disk_emulate_reqops,
2377 [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops,
2378 [MECHANISM_STATUS] = &scsi_disk_emulate_reqops,
2379 [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops,
2380 [REQUEST_SENSE] = &scsi_disk_emulate_reqops,
2381 [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops,
2382 [SEEK_10] = &scsi_disk_emulate_reqops,
2383 [MODE_SELECT] = &scsi_disk_emulate_reqops,
2384 [MODE_SELECT_10] = &scsi_disk_emulate_reqops,
2385 [UNMAP] = &scsi_disk_emulate_reqops,
2386 [WRITE_SAME_10] = &scsi_disk_emulate_reqops,
2387 [WRITE_SAME_16] = &scsi_disk_emulate_reqops,
2388 [VERIFY_10] = &scsi_disk_emulate_reqops,
2389 [VERIFY_12] = &scsi_disk_emulate_reqops,
2390 [VERIFY_16] = &scsi_disk_emulate_reqops,
2392 [READ_6] = &scsi_disk_dma_reqops,
2393 [READ_10] = &scsi_disk_dma_reqops,
2394 [READ_12] = &scsi_disk_dma_reqops,
2395 [READ_16] = &scsi_disk_dma_reqops,
2396 [WRITE_6] = &scsi_disk_dma_reqops,
2397 [WRITE_10] = &scsi_disk_dma_reqops,
2398 [WRITE_12] = &scsi_disk_dma_reqops,
2399 [WRITE_16] = &scsi_disk_dma_reqops,
2400 [WRITE_VERIFY_10] = &scsi_disk_dma_reqops,
2401 [WRITE_VERIFY_12] = &scsi_disk_dma_reqops,
2402 [WRITE_VERIFY_16] = &scsi_disk_dma_reqops,
2405 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
2406 uint8_t *buf, void *hba_private)
2408 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2410 const SCSIReqOps *ops;
2414 ops = scsi_disk_reqops_dispatch[command];
2416 ops = &scsi_disk_emulate_reqops;
2418 req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private);
2421 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
2424 for (i = 1; i < req->cmd.len; i++) {
2425 printf(" 0x%02x", buf[i]);
2435 static int get_device_type(SCSIDiskState *s)
2437 BlockDriverState *bdrv = s->qdev.conf.bs;
2440 uint8_t sensebuf[8];
2441 sg_io_hdr_t io_header;
2444 memset(cmd, 0, sizeof(cmd));
2445 memset(buf, 0, sizeof(buf));
2447 cmd[4] = sizeof(buf);
2449 memset(&io_header, 0, sizeof(io_header));
2450 io_header.interface_id = 'S';
2451 io_header.dxfer_direction = SG_DXFER_FROM_DEV;
2452 io_header.dxfer_len = sizeof(buf);
2453 io_header.dxferp = buf;
2454 io_header.cmdp = cmd;
2455 io_header.cmd_len = sizeof(cmd);
2456 io_header.mx_sb_len = sizeof(sensebuf);
2457 io_header.sbp = sensebuf;
2458 io_header.timeout = 6000; /* XXX */
2460 ret = bdrv_ioctl(bdrv, SG_IO, &io_header);
2461 if (ret < 0 || io_header.driver_status || io_header.host_status) {
2464 s->qdev.type = buf[0];
2465 if (buf[1] & 0x80) {
2466 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2471 static void scsi_block_realize(SCSIDevice *dev, Error **errp)
2473 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2477 if (!s->qdev.conf.bs) {
2478 error_setg(errp, "drive property not set");
2482 /* check we are using a driver managing SG_IO (version 3 and after) */
2483 rc = bdrv_ioctl(s->qdev.conf.bs, SG_GET_VERSION_NUM, &sg_version);
2485 error_setg(errp, "cannot get SG_IO version number: %s. "
2486 "Is this a SCSI device?",
2490 if (sg_version < 30000) {
2491 error_setg(errp, "scsi generic interface too old");
2495 /* get device type from INQUIRY data */
2496 rc = get_device_type(s);
2498 error_setg(errp, "INQUIRY failed");
2502 /* Make a guess for the block size, we'll fix it when the guest sends.
2503 * READ CAPACITY. If they don't, they likely would assume these sizes
2504 * anyway. (TODO: check in /sys).
2506 if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
2507 s->qdev.blocksize = 2048;
2509 s->qdev.blocksize = 512;
2512 /* Makes the scsi-block device not removable by using HMP and QMP eject
2515 s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS);
2517 scsi_realize(&s->qdev, errp);
2520 static bool scsi_block_is_passthrough(SCSIDiskState *s, uint8_t *buf)
2534 case WRITE_VERIFY_10:
2535 case WRITE_VERIFY_12:
2536 case WRITE_VERIFY_16:
2537 /* If we are not using O_DIRECT, we might read stale data from the
2538 * host cache if writes were made using other commands than these
2539 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2540 * O_DIRECT everything must go through SG_IO.
2542 if (!(bdrv_get_flags(s->qdev.conf.bs) & BDRV_O_NOCACHE)) {
2546 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2547 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2548 * And once you do these writes, reading from the block device is
2549 * unreliable, too. It is even possible that reads deliver random data
2550 * from the host page cache (this is probably a Linux bug).
2552 * We might use scsi_disk_dma_reqops as long as no writing commands are
2553 * seen, but performance usually isn't paramount on optical media. So,
2554 * just make scsi-block operate the same as scsi-generic for them.
2556 if (s->qdev.type != TYPE_ROM) {
2569 static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
2570 uint32_t lun, uint8_t *buf,
2573 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2575 if (scsi_block_is_passthrough(s, buf)) {
2576 return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
2579 return scsi_req_alloc(&scsi_disk_dma_reqops, &s->qdev, tag, lun,
2584 static int scsi_block_parse_cdb(SCSIDevice *d, SCSICommand *cmd,
2585 uint8_t *buf, void *hba_private)
2587 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2589 if (scsi_block_is_passthrough(s, buf)) {
2590 return scsi_bus_parse_cdb(&s->qdev, cmd, buf, hba_private);
2592 return scsi_req_parse_cdb(&s->qdev, cmd, buf);
2598 #define DEFINE_SCSI_DISK_PROPERTIES() \
2599 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2600 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2601 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2602 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2603 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2605 static Property scsi_hd_properties[] = {
2606 DEFINE_SCSI_DISK_PROPERTIES(),
2607 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2608 SCSI_DISK_F_REMOVABLE, false),
2609 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2610 SCSI_DISK_F_DPOFUA, false),
2611 DEFINE_PROP_UINT64("wwn", SCSIDiskState, wwn, 0),
2612 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, port_wwn, 0),
2613 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
2614 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
2615 DEFAULT_MAX_UNMAP_SIZE),
2616 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
2617 DEFINE_PROP_END_OF_LIST(),
2620 static const VMStateDescription vmstate_scsi_disk_state = {
2621 .name = "scsi-disk",
2623 .minimum_version_id = 1,
2624 .fields = (VMStateField[]) {
2625 VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
2626 VMSTATE_BOOL(media_changed, SCSIDiskState),
2627 VMSTATE_BOOL(media_event, SCSIDiskState),
2628 VMSTATE_BOOL(eject_request, SCSIDiskState),
2629 VMSTATE_BOOL(tray_open, SCSIDiskState),
2630 VMSTATE_BOOL(tray_locked, SCSIDiskState),
2631 VMSTATE_END_OF_LIST()
2635 static void scsi_hd_class_initfn(ObjectClass *klass, void *data)
2637 DeviceClass *dc = DEVICE_CLASS(klass);
2638 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2640 sc->realize = scsi_hd_realize;
2641 sc->unrealize = scsi_unrealize;
2642 sc->alloc_req = scsi_new_request;
2643 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2644 dc->fw_name = "disk";
2645 dc->desc = "virtual SCSI disk";
2646 dc->reset = scsi_disk_reset;
2647 dc->props = scsi_hd_properties;
2648 dc->vmsd = &vmstate_scsi_disk_state;
2651 static const TypeInfo scsi_hd_info = {
2653 .parent = TYPE_SCSI_DEVICE,
2654 .instance_size = sizeof(SCSIDiskState),
2655 .class_init = scsi_hd_class_initfn,
2658 static Property scsi_cd_properties[] = {
2659 DEFINE_SCSI_DISK_PROPERTIES(),
2660 DEFINE_PROP_UINT64("wwn", SCSIDiskState, wwn, 0),
2661 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, port_wwn, 0),
2662 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
2663 DEFINE_PROP_END_OF_LIST(),
2666 static void scsi_cd_class_initfn(ObjectClass *klass, void *data)
2668 DeviceClass *dc = DEVICE_CLASS(klass);
2669 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2671 sc->realize = scsi_cd_realize;
2672 sc->unrealize = scsi_unrealize;
2673 sc->alloc_req = scsi_new_request;
2674 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2675 dc->fw_name = "disk";
2676 dc->desc = "virtual SCSI CD-ROM";
2677 dc->reset = scsi_disk_reset;
2678 dc->props = scsi_cd_properties;
2679 dc->vmsd = &vmstate_scsi_disk_state;
2682 static const TypeInfo scsi_cd_info = {
2684 .parent = TYPE_SCSI_DEVICE,
2685 .instance_size = sizeof(SCSIDiskState),
2686 .class_init = scsi_cd_class_initfn,
2690 static Property scsi_block_properties[] = {
2691 DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.bs),
2692 DEFINE_PROP_INT32("bootindex", SCSIDiskState, qdev.conf.bootindex, -1),
2693 DEFINE_PROP_END_OF_LIST(),
2696 static void scsi_block_class_initfn(ObjectClass *klass, void *data)
2698 DeviceClass *dc = DEVICE_CLASS(klass);
2699 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2701 sc->realize = scsi_block_realize;
2702 sc->unrealize = scsi_unrealize;
2703 sc->alloc_req = scsi_block_new_request;
2704 sc->parse_cdb = scsi_block_parse_cdb;
2705 dc->fw_name = "disk";
2706 dc->desc = "SCSI block device passthrough";
2707 dc->reset = scsi_disk_reset;
2708 dc->props = scsi_block_properties;
2709 dc->vmsd = &vmstate_scsi_disk_state;
2712 static const TypeInfo scsi_block_info = {
2713 .name = "scsi-block",
2714 .parent = TYPE_SCSI_DEVICE,
2715 .instance_size = sizeof(SCSIDiskState),
2716 .class_init = scsi_block_class_initfn,
2720 static Property scsi_disk_properties[] = {
2721 DEFINE_SCSI_DISK_PROPERTIES(),
2722 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2723 SCSI_DISK_F_REMOVABLE, false),
2724 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2725 SCSI_DISK_F_DPOFUA, false),
2726 DEFINE_PROP_UINT64("wwn", SCSIDiskState, wwn, 0),
2727 DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, port_wwn, 0),
2728 DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0),
2729 DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size,
2730 DEFAULT_MAX_UNMAP_SIZE),
2731 DEFINE_PROP_END_OF_LIST(),
2734 static void scsi_disk_class_initfn(ObjectClass *klass, void *data)
2736 DeviceClass *dc = DEVICE_CLASS(klass);
2737 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2739 sc->realize = scsi_disk_realize;
2740 sc->unrealize = scsi_unrealize;
2741 sc->alloc_req = scsi_new_request;
2742 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2743 dc->fw_name = "disk";
2744 dc->desc = "virtual SCSI disk or CD-ROM (legacy)";
2745 dc->reset = scsi_disk_reset;
2746 dc->props = scsi_disk_properties;
2747 dc->vmsd = &vmstate_scsi_disk_state;
2750 static const TypeInfo scsi_disk_info = {
2751 .name = "scsi-disk",
2752 .parent = TYPE_SCSI_DEVICE,
2753 .instance_size = sizeof(SCSIDiskState),
2754 .class_init = scsi_disk_class_initfn,
2757 static void scsi_disk_register_types(void)
2759 type_register_static(&scsi_hd_info);
2760 type_register_static(&scsi_cd_info);
2762 type_register_static(&scsi_block_info);
2764 type_register_static(&scsi_disk_info);
2767 type_init(scsi_disk_register_types)