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 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
34 #include "qemu-common.h"
35 #include "qemu-error.h"
37 #include "scsi-defs.h"
41 #define SCSI_DMA_BUF_SIZE 131072
42 #define SCSI_MAX_INQUIRY_LEN 256
44 #define SCSI_REQ_STATUS_RETRY 0x01
45 #define SCSI_REQ_STATUS_RETRY_TYPE_MASK 0x06
46 #define SCSI_REQ_STATUS_RETRY_READ 0x00
47 #define SCSI_REQ_STATUS_RETRY_WRITE 0x02
48 #define SCSI_REQ_STATUS_RETRY_FLUSH 0x04
50 typedef struct SCSIDiskState SCSIDiskState;
52 typedef struct SCSIDiskReq {
54 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
56 uint32_t sector_count;
66 /* The qemu block layer uses a fixed 512 byte sector size.
67 This is the number of 512 byte blocks in a single scsi sector. */
76 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
77 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
79 static void scsi_free_request(SCSIRequest *req)
81 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
83 qemu_vfree(r->iov.iov_base);
86 /* Helper function for command completion with sense. */
87 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
89 DPRINTF("Command complete tag=0x%x status=%d sense=%d/%d/%d\n",
90 r->req.tag, status, sense.key, sense.asc, sense.ascq);
91 scsi_req_build_sense(&r->req, sense);
92 scsi_req_complete(&r->req, CHECK_CONDITION);
95 /* Cancel a pending data transfer. */
96 static void scsi_cancel_io(SCSIRequest *req)
98 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
100 DPRINTF("Cancel tag=0x%x\n", req->tag);
102 bdrv_aio_cancel(r->req.aiocb);
107 static void scsi_read_complete(void * opaque, int ret)
109 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
115 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
120 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
122 n = r->iov.iov_len / 512;
124 r->sector_count -= n;
125 scsi_req_data(&r->req, r->iov.iov_len);
129 /* Read more data from scsi device into buffer. */
130 static void scsi_read_data(SCSIRequest *req)
132 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
133 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
136 if (r->sector_count == (uint32_t)-1) {
137 DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
139 scsi_req_data(&r->req, r->iov.iov_len);
142 DPRINTF("Read sector_count=%d\n", r->sector_count);
143 if (r->sector_count == 0) {
144 /* This also clears the sense buffer for REQUEST SENSE. */
145 scsi_req_complete(&r->req, GOOD);
149 /* No data transfer may already be in progress */
150 assert(r->req.aiocb == NULL);
152 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
153 DPRINTF("Data transfer direction invalid\n");
154 scsi_read_complete(r, -EINVAL);
159 if (n > SCSI_DMA_BUF_SIZE / 512)
160 n = SCSI_DMA_BUF_SIZE / 512;
162 r->iov.iov_len = n * 512;
163 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
164 r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
165 scsi_read_complete, r);
166 if (r->req.aiocb == NULL) {
167 scsi_read_complete(r, -EIO);
171 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
173 int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
174 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
175 BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
177 if (action == BLOCK_ERR_IGNORE) {
178 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
182 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
183 || action == BLOCK_ERR_STOP_ANY) {
185 type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
186 r->status |= SCSI_REQ_STATUS_RETRY | type;
188 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
189 vm_stop(VMSTOP_DISKFULL);
193 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
196 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
199 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
202 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
207 static void scsi_write_complete(void * opaque, int ret)
209 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
216 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
221 n = r->iov.iov_len / 512;
223 r->sector_count -= n;
224 if (r->sector_count == 0) {
225 scsi_req_complete(&r->req, GOOD);
227 len = r->sector_count * 512;
228 if (len > SCSI_DMA_BUF_SIZE) {
229 len = SCSI_DMA_BUF_SIZE;
231 r->iov.iov_len = len;
232 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
233 scsi_req_data(&r->req, len);
237 static void scsi_write_data(SCSIRequest *req)
239 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
240 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
243 /* No data transfer may already be in progress */
244 assert(r->req.aiocb == NULL);
246 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
247 DPRINTF("Data transfer direction invalid\n");
248 scsi_write_complete(r, -EINVAL);
252 n = r->iov.iov_len / 512;
254 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
255 r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
256 scsi_write_complete, r);
257 if (r->req.aiocb == NULL) {
258 scsi_write_complete(r, -ENOMEM);
261 /* Invoke completion routine to fetch data from host. */
262 scsi_write_complete(r, 0);
266 static void scsi_dma_restart_bh(void *opaque)
268 SCSIDiskState *s = opaque;
272 qemu_bh_delete(s->bh);
275 QTAILQ_FOREACH(req, &s->qdev.requests, next) {
276 r = DO_UPCAST(SCSIDiskReq, req, req);
277 if (r->status & SCSI_REQ_STATUS_RETRY) {
278 int status = r->status;
282 ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
284 switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
285 case SCSI_REQ_STATUS_RETRY_READ:
286 scsi_read_data(&r->req);
288 case SCSI_REQ_STATUS_RETRY_WRITE:
289 scsi_write_data(&r->req);
291 case SCSI_REQ_STATUS_RETRY_FLUSH:
292 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
294 scsi_req_complete(&r->req, GOOD);
301 static void scsi_dma_restart_cb(void *opaque, int running, int reason)
303 SCSIDiskState *s = opaque;
309 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
310 qemu_bh_schedule(s->bh);
314 /* Return a pointer to the data buffer. */
315 static uint8_t *scsi_get_buf(SCSIRequest *req)
317 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
319 return (uint8_t *)r->iov.iov_base;
322 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
324 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
327 if (req->cmd.buf[1] & 0x2) {
328 /* Command support data - optional, not implemented */
329 BADF("optional INQUIRY command support request not implemented\n");
333 if (req->cmd.buf[1] & 0x1) {
334 /* Vital product data */
335 uint8_t page_code = req->cmd.buf[2];
336 if (req->cmd.xfer < 4) {
337 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
338 "less than 4\n", page_code, req->cmd.xfer);
342 if (s->qdev.type == TYPE_ROM) {
343 outbuf[buflen++] = 5;
345 outbuf[buflen++] = 0;
347 outbuf[buflen++] = page_code ; // this page
348 outbuf[buflen++] = 0x00;
351 case 0x00: /* Supported page codes, mandatory */
354 DPRINTF("Inquiry EVPD[Supported pages] "
355 "buffer size %zd\n", req->cmd.xfer);
357 outbuf[buflen++] = 0x00; // list of supported pages (this page)
359 outbuf[buflen++] = 0x80; // unit serial number
360 outbuf[buflen++] = 0x83; // device identification
361 if (s->qdev.type == TYPE_DISK) {
362 outbuf[buflen++] = 0xb0; // block limits
363 outbuf[buflen++] = 0xb2; // thin provisioning
365 outbuf[pages] = buflen - pages - 1; // number of pages
368 case 0x80: /* Device serial number, optional */
373 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
377 l = strlen(s->serial);
378 if (l > req->cmd.xfer)
383 DPRINTF("Inquiry EVPD[Serial number] "
384 "buffer size %zd\n", req->cmd.xfer);
385 outbuf[buflen++] = l;
386 memcpy(outbuf+buflen, s->serial, l);
391 case 0x83: /* Device identification page, mandatory */
393 int max_len = 255 - 8;
394 int id_len = strlen(bdrv_get_device_name(s->bs));
396 if (id_len > max_len)
398 DPRINTF("Inquiry EVPD[Device identification] "
399 "buffer size %zd\n", req->cmd.xfer);
401 outbuf[buflen++] = 4 + id_len;
402 outbuf[buflen++] = 0x2; // ASCII
403 outbuf[buflen++] = 0; // not officially assigned
404 outbuf[buflen++] = 0; // reserved
405 outbuf[buflen++] = id_len; // length of data following
407 memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
411 case 0xb0: /* block limits */
413 unsigned int unmap_sectors =
414 s->qdev.conf.discard_granularity / s->qdev.blocksize;
415 unsigned int min_io_size =
416 s->qdev.conf.min_io_size / s->qdev.blocksize;
417 unsigned int opt_io_size =
418 s->qdev.conf.opt_io_size / s->qdev.blocksize;
420 if (s->qdev.type == TYPE_ROM) {
421 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
425 /* required VPD size with unmap support */
426 outbuf[3] = buflen = 0x3c;
428 memset(outbuf + 4, 0, buflen - 4);
430 /* optimal transfer length granularity */
431 outbuf[6] = (min_io_size >> 8) & 0xff;
432 outbuf[7] = min_io_size & 0xff;
434 /* optimal transfer length */
435 outbuf[12] = (opt_io_size >> 24) & 0xff;
436 outbuf[13] = (opt_io_size >> 16) & 0xff;
437 outbuf[14] = (opt_io_size >> 8) & 0xff;
438 outbuf[15] = opt_io_size & 0xff;
440 /* optimal unmap granularity */
441 outbuf[28] = (unmap_sectors >> 24) & 0xff;
442 outbuf[29] = (unmap_sectors >> 16) & 0xff;
443 outbuf[30] = (unmap_sectors >> 8) & 0xff;
444 outbuf[31] = unmap_sectors & 0xff;
447 case 0xb2: /* thin provisioning */
449 outbuf[3] = buflen = 8;
451 outbuf[5] = 0x40; /* write same with unmap supported */
457 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
458 "buffer size %zd\n", page_code, req->cmd.xfer);
465 /* Standard INQUIRY data */
466 if (req->cmd.buf[2] != 0) {
467 BADF("Error: Inquiry (STANDARD) page or code "
468 "is non-zero [%02X]\n", req->cmd.buf[2]);
473 if (req->cmd.xfer < 5) {
474 BADF("Error: Inquiry (STANDARD) buffer size %zd "
475 "is less than 5\n", req->cmd.xfer);
479 buflen = req->cmd.xfer;
480 if (buflen > SCSI_MAX_INQUIRY_LEN)
481 buflen = SCSI_MAX_INQUIRY_LEN;
483 memset(outbuf, 0, buflen);
486 outbuf[0] = 0x7f; /* LUN not supported */
490 outbuf[0] = s->qdev.type & 0x1f;
491 if (s->qdev.type == TYPE_ROM) {
493 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
495 outbuf[1] = s->removable ? 0x80 : 0;
496 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
498 memcpy(&outbuf[8], "QEMU ", 8);
499 memset(&outbuf[32], 0, 4);
500 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
502 * We claim conformance to SPC-3, which is required for guests
503 * to ask for modern features like READ CAPACITY(16) or the
504 * block characteristics VPD page by default. Not all of SPC-3
505 * is actually implemented, but we're good enough.
508 outbuf[3] = 2; /* Format 2 */
511 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
513 /* If the allocation length of CDB is too small,
514 the additional length is not adjusted */
518 /* Sync data transfer and TCQ. */
519 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
523 static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
526 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
527 BlockDriverState *bdrv = s->bs;
528 int cylinders, heads, secs;
531 * If Changeable Values are requested, a mask denoting those mode parameters
532 * that are changeable shall be returned. As we currently don't support
533 * parameter changes via MODE_SELECT all bits are returned set to zero.
534 * The buffer was already menset to zero by the caller of this function.
537 case 4: /* Rigid disk device geometry page. */
540 if (page_control == 1) { /* Changeable Values */
543 /* if a geometry hint is available, use it */
544 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
545 p[2] = (cylinders >> 16) & 0xff;
546 p[3] = (cylinders >> 8) & 0xff;
547 p[4] = cylinders & 0xff;
549 /* Write precomp start cylinder, disabled */
550 p[6] = (cylinders >> 16) & 0xff;
551 p[7] = (cylinders >> 8) & 0xff;
552 p[8] = cylinders & 0xff;
553 /* Reduced current start cylinder, disabled */
554 p[9] = (cylinders >> 16) & 0xff;
555 p[10] = (cylinders >> 8) & 0xff;
556 p[11] = cylinders & 0xff;
557 /* Device step rate [ns], 200ns */
560 /* Landing zone cylinder */
564 /* Medium rotation rate [rpm], 5400 rpm */
565 p[20] = (5400 >> 8) & 0xff;
569 case 5: /* Flexible disk device geometry page. */
572 if (page_control == 1) { /* Changeable Values */
575 /* Transfer rate [kbit/s], 5Mbit/s */
578 /* if a geometry hint is available, use it */
579 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
582 p[6] = s->cluster_size * 2;
583 p[8] = (cylinders >> 8) & 0xff;
584 p[9] = cylinders & 0xff;
585 /* Write precomp start cylinder, disabled */
586 p[10] = (cylinders >> 8) & 0xff;
587 p[11] = cylinders & 0xff;
588 /* Reduced current start cylinder, disabled */
589 p[12] = (cylinders >> 8) & 0xff;
590 p[13] = cylinders & 0xff;
591 /* Device step rate [100us], 100us */
594 /* Device step pulse width [us], 1us */
596 /* Device head settle delay [100us], 100us */
599 /* Motor on delay [0.1s], 0.1s */
601 /* Motor off delay [0.1s], 0.1s */
603 /* Medium rotation rate [rpm], 5400 rpm */
604 p[28] = (5400 >> 8) & 0xff;
608 case 8: /* Caching page. */
611 if (page_control == 1) { /* Changeable Values */
614 if (bdrv_enable_write_cache(s->bs)) {
619 case 0x2a: /* CD Capabilities and Mechanical Status page. */
620 if (s->qdev.type != TYPE_ROM)
624 if (page_control == 1) { /* Changeable Values */
627 p[2] = 3; // CD-R & CD-RW read
628 p[3] = 0; // Writing not supported
629 p[4] = 0x7f; /* Audio, composite, digital out,
630 mode 2 form 1&2, multi session */
631 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
632 RW corrected, C2 errors, ISRC,
634 p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
635 /* Locking supported, jumper present, eject, tray */
636 p[7] = 0; /* no volume & mute control, no
638 p[8] = (50 * 176) >> 8; // 50x read speed
639 p[9] = (50 * 176) & 0xff;
640 p[10] = 0 >> 8; // No volume
642 p[12] = 2048 >> 8; // 2M buffer
644 p[14] = (16 * 176) >> 8; // 16x read speed current
645 p[15] = (16 * 176) & 0xff;
646 p[18] = (16 * 176) >> 8; // 16x write speed
647 p[19] = (16 * 176) & 0xff;
648 p[20] = (16 * 176) >> 8; // 16x write speed current
649 p[21] = (16 * 176) & 0xff;
657 static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
659 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
661 int page, dbd, buflen, page_control;
663 uint8_t dev_specific_param;
665 dbd = req->cmd.buf[1] & 0x8;
666 page = req->cmd.buf[2] & 0x3f;
667 page_control = (req->cmd.buf[2] & 0xc0) >> 6;
668 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
669 (req->cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, req->cmd.xfer, page_control);
670 memset(outbuf, 0, req->cmd.xfer);
673 if (bdrv_is_read_only(s->bs)) {
674 dev_specific_param = 0x80; /* Readonly. */
676 dev_specific_param = 0x00;
679 if (req->cmd.buf[0] == MODE_SENSE) {
680 p[1] = 0; /* Default media type. */
681 p[2] = dev_specific_param;
682 p[3] = 0; /* Block descriptor length. */
684 } else { /* MODE_SENSE_10 */
685 p[2] = 0; /* Default media type. */
686 p[3] = dev_specific_param;
687 p[6] = p[7] = 0; /* Block descriptor length. */
691 bdrv_get_geometry(s->bs, &nb_sectors);
692 if (!dbd && nb_sectors) {
693 if (req->cmd.buf[0] == MODE_SENSE) {
694 outbuf[3] = 8; /* Block descriptor length */
695 } else { /* MODE_SENSE_10 */
696 outbuf[7] = 8; /* Block descriptor length */
698 nb_sectors /= s->cluster_size;
699 if (nb_sectors > 0xffffff)
701 p[0] = 0; /* media density code */
702 p[1] = (nb_sectors >> 16) & 0xff;
703 p[2] = (nb_sectors >> 8) & 0xff;
704 p[3] = nb_sectors & 0xff;
705 p[4] = 0; /* reserved */
706 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
707 p[6] = s->cluster_size * 2;
712 if (page_control == 3) { /* Saved Values */
713 return -1; /* ILLEGAL_REQUEST */
721 p += mode_sense_page(req, page, p, page_control);
724 p += mode_sense_page(req, 0x08, p, page_control);
725 p += mode_sense_page(req, 0x2a, p, page_control);
728 return -1; /* ILLEGAL_REQUEST */
733 * The mode data length field specifies the length in bytes of the
734 * following data that is available to be transferred. The mode data
735 * length does not include itself.
737 if (req->cmd.buf[0] == MODE_SENSE) {
738 outbuf[0] = buflen - 1;
739 } else { /* MODE_SENSE_10 */
740 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
741 outbuf[1] = (buflen - 2) & 0xff;
743 if (buflen > req->cmd.xfer)
744 buflen = req->cmd.xfer;
748 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
750 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
751 int start_track, format, msf, toclen;
754 msf = req->cmd.buf[1] & 2;
755 format = req->cmd.buf[2] & 0xf;
756 start_track = req->cmd.buf[6];
757 bdrv_get_geometry(s->bs, &nb_sectors);
758 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
759 nb_sectors /= s->cluster_size;
762 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
765 /* multi session : only a single session defined */
767 memset(outbuf, 0, 12);
773 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
778 if (toclen > req->cmd.xfer)
779 toclen = req->cmd.xfer;
783 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
785 SCSIRequest *req = &r->req;
786 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
791 switch (req->cmd.buf[0]) {
792 case TEST_UNIT_READY:
793 if (!bdrv_is_inserted(s->bs))
797 if (req->cmd.xfer < 4)
798 goto illegal_request;
799 buflen = scsi_device_get_sense(&s->qdev, outbuf, req->cmd.xfer,
800 (req->cmd.buf[1] & 1) == 0);
803 buflen = scsi_disk_emulate_inquiry(req, outbuf);
805 goto illegal_request;
809 buflen = scsi_disk_emulate_mode_sense(req, outbuf);
811 goto illegal_request;
814 buflen = scsi_disk_emulate_read_toc(req, outbuf);
816 goto illegal_request;
819 if (req->cmd.buf[1] & 1)
820 goto illegal_request;
823 if (req->cmd.buf[1] & 3)
824 goto illegal_request;
827 if (req->cmd.buf[1] & 1)
828 goto illegal_request;
831 if (req->cmd.buf[1] & 3)
832 goto illegal_request;
835 if (s->qdev.type == TYPE_ROM && (req->cmd.buf[4] & 2)) {
836 /* load/eject medium */
837 bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
840 case ALLOW_MEDIUM_REMOVAL:
841 bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
843 case READ_CAPACITY_10:
844 /* The normal LEN field for this command is zero. */
845 memset(outbuf, 0, 8);
846 bdrv_get_geometry(s->bs, &nb_sectors);
849 nb_sectors /= s->cluster_size;
850 /* Returned value is the address of the last sector. */
852 /* Remember the new size for read/write sanity checking. */
853 s->max_lba = nb_sectors;
854 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
855 if (nb_sectors > UINT32_MAX)
856 nb_sectors = UINT32_MAX;
857 outbuf[0] = (nb_sectors >> 24) & 0xff;
858 outbuf[1] = (nb_sectors >> 16) & 0xff;
859 outbuf[2] = (nb_sectors >> 8) & 0xff;
860 outbuf[3] = nb_sectors & 0xff;
863 outbuf[6] = s->cluster_size * 2;
867 case SYNCHRONIZE_CACHE:
868 ret = bdrv_flush(s->bs);
870 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
875 case GET_CONFIGURATION:
876 memset(outbuf, 0, 8);
877 /* ??? This should probably return much more information. For now
878 just return the basic header indicating the CD-ROM profile. */
879 outbuf[7] = 8; // CD-ROM
882 case SERVICE_ACTION_IN:
883 /* Service Action In subcommands. */
884 if ((req->cmd.buf[1] & 31) == 0x10) {
885 DPRINTF("SAI READ CAPACITY(16)\n");
886 memset(outbuf, 0, req->cmd.xfer);
887 bdrv_get_geometry(s->bs, &nb_sectors);
890 nb_sectors /= s->cluster_size;
891 /* Returned value is the address of the last sector. */
893 /* Remember the new size for read/write sanity checking. */
894 s->max_lba = nb_sectors;
895 outbuf[0] = (nb_sectors >> 56) & 0xff;
896 outbuf[1] = (nb_sectors >> 48) & 0xff;
897 outbuf[2] = (nb_sectors >> 40) & 0xff;
898 outbuf[3] = (nb_sectors >> 32) & 0xff;
899 outbuf[4] = (nb_sectors >> 24) & 0xff;
900 outbuf[5] = (nb_sectors >> 16) & 0xff;
901 outbuf[6] = (nb_sectors >> 8) & 0xff;
902 outbuf[7] = nb_sectors & 0xff;
905 outbuf[10] = s->cluster_size * 2;
908 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
910 /* set TPE bit if the format supports discard */
911 if (s->qdev.conf.discard_granularity) {
915 /* Protection, exponent and lowest lba field left blank. */
916 buflen = req->cmd.xfer;
919 DPRINTF("Unsupported Service Action In\n");
920 goto illegal_request;
922 if (req->cmd.xfer < 16)
923 goto illegal_request;
924 memset(outbuf, 0, 16);
931 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
937 if (!bdrv_is_inserted(s->bs)) {
938 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
940 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
945 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
949 /* Execute a scsi command. Returns the length of the data expected by the
950 command. This will be Positive for data transfers from the device
951 (eg. disk reads), negative for transfers to the device (eg. disk writes),
952 and zero if the command does not transfer any data. */
954 static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
956 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
957 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
964 outbuf = (uint8_t *)r->iov.iov_base;
965 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
970 for (i = 1; i < r->req.cmd.len; i++) {
971 printf(" 0x%02x", buf[i]);
978 /* Only LUN 0 supported. */
979 DPRINTF("Unimplemented LUN %d\n", req->lun);
980 if (command != REQUEST_SENSE && command != INQUIRY) {
981 scsi_check_condition(r, SENSE_CODE(LUN_NOT_SUPPORTED));
986 case TEST_UNIT_READY:
996 case ALLOW_MEDIUM_REMOVAL:
997 case READ_CAPACITY_10:
998 case SYNCHRONIZE_CACHE:
1000 case GET_CONFIGURATION:
1001 case SERVICE_ACTION_IN:
1004 rc = scsi_disk_emulate_command(r, outbuf);
1009 r->iov.iov_len = rc;
1015 len = r->req.cmd.xfer / s->qdev.blocksize;
1016 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1017 if (r->req.cmd.lba > s->max_lba)
1019 r->sector = r->req.cmd.lba * s->cluster_size;
1020 r->sector_count = len * s->cluster_size;
1026 case WRITE_VERIFY_10:
1027 case WRITE_VERIFY_12:
1028 case WRITE_VERIFY_16:
1029 len = r->req.cmd.xfer / s->qdev.blocksize;
1030 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1031 (command & 0xe) == 0xe ? "And Verify " : "",
1032 r->req.cmd.lba, len);
1033 if (r->req.cmd.lba > s->max_lba)
1035 r->sector = r->req.cmd.lba * s->cluster_size;
1036 r->sector_count = len * s->cluster_size;
1039 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1040 /* We don't support mode parameter changes.
1041 Allow the mode parameter header + block descriptors only. */
1042 if (r->req.cmd.xfer > 12) {
1046 case MODE_SELECT_10:
1047 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1048 /* We don't support mode parameter changes.
1049 Allow the mode parameter header + block descriptors only. */
1050 if (r->req.cmd.xfer > 16) {
1056 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1058 if (r->req.cmd.lba > s->max_lba) {
1063 len = r->req.cmd.xfer / s->qdev.blocksize;
1065 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1066 r->req.cmd.lba, len);
1068 if (r->req.cmd.lba > s->max_lba) {
1073 * We only support WRITE SAME with the unmap bit set for now.
1075 if (!(buf[1] & 0x8)) {
1079 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1080 len * s->cluster_size);
1082 /* XXX: better error code ?*/
1088 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1089 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1092 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1095 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1098 if (r->sector_count == 0 && r->iov.iov_len == 0) {
1099 scsi_req_complete(&r->req, GOOD);
1101 len = r->sector_count * 512 + r->iov.iov_len;
1102 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1105 if (!r->sector_count)
1106 r->sector_count = -1;
1111 static void scsi_disk_reset(DeviceState *dev)
1113 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1114 uint64_t nb_sectors;
1116 scsi_device_purge_requests(&s->qdev);
1118 bdrv_get_geometry(s->bs, &nb_sectors);
1119 nb_sectors /= s->cluster_size;
1123 s->max_lba = nb_sectors;
1126 static void scsi_destroy(SCSIDevice *dev)
1128 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1130 scsi_device_purge_requests(&s->qdev);
1131 blockdev_mark_auto_del(s->qdev.conf.bs);
1134 static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
1136 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1139 if (!s->qdev.conf.bs) {
1140 error_report("scsi-disk: drive property not set");
1143 s->bs = s->qdev.conf.bs;
1145 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
1146 error_report("Device needs media, but drive is empty");
1151 /* try to fall back to value set with legacy -drive serial=... */
1152 dinfo = drive_get_by_blockdev(s->bs);
1153 if (*dinfo->serial) {
1154 s->serial = qemu_strdup(dinfo->serial);
1159 s->version = qemu_strdup(QEMU_VERSION);
1162 if (bdrv_is_sg(s->bs)) {
1163 error_report("scsi-disk: unwanted /dev/sg*");
1167 if (scsi_type == TYPE_ROM) {
1168 s->qdev.blocksize = 2048;
1169 } else if (scsi_type == TYPE_DISK) {
1170 s->qdev.blocksize = s->qdev.conf.logical_block_size;
1172 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1175 s->cluster_size = s->qdev.blocksize / 512;
1176 s->bs->buffer_alignment = s->qdev.blocksize;
1178 s->qdev.type = scsi_type;
1179 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1180 bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
1181 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1185 static int scsi_hd_initfn(SCSIDevice *dev)
1187 return scsi_initfn(dev, TYPE_DISK);
1190 static int scsi_cd_initfn(SCSIDevice *dev)
1192 return scsi_initfn(dev, TYPE_ROM);
1195 static int scsi_disk_initfn(SCSIDevice *dev)
1200 if (!dev->conf.bs) {
1201 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
1203 dinfo = drive_get_by_blockdev(dev->conf.bs);
1204 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
1207 return scsi_initfn(dev, scsi_type);
1210 static SCSIReqOps scsi_disk_reqops = {
1211 .size = sizeof(SCSIDiskReq),
1212 .free_req = scsi_free_request,
1213 .send_command = scsi_send_command,
1214 .read_data = scsi_read_data,
1215 .write_data = scsi_write_data,
1216 .cancel_io = scsi_cancel_io,
1217 .get_buf = scsi_get_buf,
1220 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
1221 uint32_t lun, void *hba_private)
1223 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1227 req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1228 r = DO_UPCAST(SCSIDiskReq, req, req);
1229 r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
1233 #define DEFINE_SCSI_DISK_PROPERTIES() \
1234 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1235 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1236 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1238 static SCSIDeviceInfo scsi_disk_info[] = {
1240 .qdev.name = "scsi-hd",
1241 .qdev.fw_name = "disk",
1242 .qdev.desc = "virtual SCSI disk",
1243 .qdev.size = sizeof(SCSIDiskState),
1244 .qdev.reset = scsi_disk_reset,
1245 .init = scsi_hd_initfn,
1246 .destroy = scsi_destroy,
1247 .alloc_req = scsi_new_request,
1248 .qdev.props = (Property[]) {
1249 DEFINE_SCSI_DISK_PROPERTIES(),
1250 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1251 DEFINE_PROP_END_OF_LIST(),
1254 .qdev.name = "scsi-cd",
1255 .qdev.fw_name = "disk",
1256 .qdev.desc = "virtual SCSI CD-ROM",
1257 .qdev.size = sizeof(SCSIDiskState),
1258 .qdev.reset = scsi_disk_reset,
1259 .init = scsi_cd_initfn,
1260 .destroy = scsi_destroy,
1261 .alloc_req = scsi_new_request,
1262 .qdev.props = (Property[]) {
1263 DEFINE_SCSI_DISK_PROPERTIES(),
1264 DEFINE_PROP_END_OF_LIST(),
1267 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1268 .qdev.fw_name = "disk",
1269 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1270 .qdev.size = sizeof(SCSIDiskState),
1271 .qdev.reset = scsi_disk_reset,
1272 .init = scsi_disk_initfn,
1273 .destroy = scsi_destroy,
1274 .alloc_req = scsi_new_request,
1275 .qdev.props = (Property[]) {
1276 DEFINE_SCSI_DISK_PROPERTIES(),
1277 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1278 DEFINE_PROP_END_OF_LIST(),
1283 static void scsi_disk_register_devices(void)
1287 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1288 scsi_qdev_register(&scsi_disk_info[i]);
1291 device_init(scsi_disk_register_devices)