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 licenced 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 SCSISense
{
56 typedef struct SCSIDiskReq
{
58 /* ??? We should probably keep track of whether the data transfer is
59 a read or a write. Currently we rely on the host getting it right. */
60 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
62 uint32_t sector_count
;
68 typedef enum { SCSI_HD
, SCSI_CD
} SCSIDriveKind
;
74 /* The qemu block layer uses a fixed 512 byte sector size.
75 This is the number of 512 byte blocks in a single scsi sector. */
83 SCSIDriveKind drive_kind
;
86 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
87 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
);
89 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
92 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
96 req
= scsi_req_alloc(sizeof(SCSIDiskReq
), &s
->qdev
, tag
, lun
);
97 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
98 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, SCSI_DMA_BUF_SIZE
);
102 static void scsi_free_request(SCSIRequest
*req
)
104 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
106 qemu_vfree(r
->iov
.iov_base
);
109 static void scsi_disk_clear_sense(SCSIDiskState
*s
)
111 memset(&s
->sense
, 0, sizeof(s
->sense
));
114 static void scsi_disk_set_sense(SCSIDiskState
*s
, uint8_t key
)
119 static void scsi_req_set_status(SCSIDiskReq
*r
, int status
, int sense_code
)
121 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
123 r
->req
.status
= status
;
124 scsi_disk_set_sense(s
, sense_code
);
127 /* Helper function for command completion. */
128 static void scsi_command_complete(SCSIDiskReq
*r
, int status
, int sense
)
130 DPRINTF("Command complete tag=0x%x status=%d sense=%d\n",
131 r
->req
.tag
, status
, sense
);
132 scsi_req_set_status(r
, status
, sense
);
133 scsi_req_complete(&r
->req
);
136 /* Cancel a pending data transfer. */
137 static void scsi_cancel_io(SCSIRequest
*req
)
139 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
141 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
143 bdrv_aio_cancel(r
->req
.aiocb
);
148 static void scsi_read_complete(void * opaque
, int ret
)
150 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
156 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
161 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->iov
.iov_len
);
163 n
= r
->iov
.iov_len
/ 512;
165 r
->sector_count
-= n
;
166 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
170 /* Read more data from scsi device into buffer. */
171 static void scsi_read_data(SCSIRequest
*req
)
173 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
174 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
177 if (r
->sector_count
== (uint32_t)-1) {
178 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
180 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
183 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
184 if (r
->sector_count
== 0) {
185 scsi_command_complete(r
, GOOD
, NO_SENSE
);
189 /* No data transfer may already be in progress */
190 assert(r
->req
.aiocb
== NULL
);
193 if (n
> SCSI_DMA_BUF_SIZE
/ 512)
194 n
= SCSI_DMA_BUF_SIZE
/ 512;
196 r
->iov
.iov_len
= n
* 512;
197 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
198 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
199 scsi_read_complete
, r
);
200 if (r
->req
.aiocb
== NULL
) {
201 scsi_read_complete(r
, -EIO
);
205 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
207 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
208 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
209 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
211 if (action
== BLOCK_ERR_IGNORE
) {
212 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
216 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
217 || action
== BLOCK_ERR_STOP_ANY
) {
219 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
220 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
222 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
223 vm_stop(VMSTOP_DISKFULL
);
225 if (type
== SCSI_REQ_STATUS_RETRY_READ
) {
226 scsi_req_data(&r
->req
, 0);
228 scsi_command_complete(r
, CHECK_CONDITION
,
230 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
236 static void scsi_write_complete(void * opaque
, int ret
)
238 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
245 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
250 n
= r
->iov
.iov_len
/ 512;
252 r
->sector_count
-= n
;
253 if (r
->sector_count
== 0) {
254 scsi_command_complete(r
, GOOD
, NO_SENSE
);
256 len
= r
->sector_count
* 512;
257 if (len
> SCSI_DMA_BUF_SIZE
) {
258 len
= SCSI_DMA_BUF_SIZE
;
260 r
->iov
.iov_len
= len
;
261 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, len
);
262 scsi_req_data(&r
->req
, len
);
266 static int scsi_write_data(SCSIRequest
*req
)
268 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
269 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
272 /* No data transfer may already be in progress */
273 assert(r
->req
.aiocb
== NULL
);
275 n
= r
->iov
.iov_len
/ 512;
277 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
278 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
279 scsi_write_complete
, r
);
280 if (r
->req
.aiocb
== NULL
) {
281 scsi_write_complete(r
, -EIO
);
284 /* Invoke completion routine to fetch data from host. */
285 scsi_write_complete(r
, 0);
291 static void scsi_dma_restart_bh(void *opaque
)
293 SCSIDiskState
*s
= opaque
;
297 qemu_bh_delete(s
->bh
);
300 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
301 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
302 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
303 int status
= r
->status
;
307 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
309 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
310 case SCSI_REQ_STATUS_RETRY_READ
:
311 scsi_read_data(&r
->req
);
313 case SCSI_REQ_STATUS_RETRY_WRITE
:
314 scsi_write_data(&r
->req
);
316 case SCSI_REQ_STATUS_RETRY_FLUSH
:
317 ret
= scsi_disk_emulate_command(r
, r
->iov
.iov_base
);
319 scsi_command_complete(r
, GOOD
, NO_SENSE
);
326 static void scsi_dma_restart_cb(void *opaque
, int running
, int reason
)
328 SCSIDiskState
*s
= opaque
;
334 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
335 qemu_bh_schedule(s
->bh
);
339 /* Return a pointer to the data buffer. */
340 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
342 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
344 return (uint8_t *)r
->iov
.iov_base
;
347 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
349 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
352 if (req
->cmd
.buf
[1] & 0x2) {
353 /* Command support data - optional, not implemented */
354 BADF("optional INQUIRY command support request not implemented\n");
358 if (req
->cmd
.buf
[1] & 0x1) {
359 /* Vital product data */
360 uint8_t page_code
= req
->cmd
.buf
[2];
361 if (req
->cmd
.xfer
< 4) {
362 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
363 "less than 4\n", page_code
, req
->cmd
.xfer
);
367 if (s
->drive_kind
== SCSI_CD
) {
368 outbuf
[buflen
++] = 5;
370 outbuf
[buflen
++] = 0;
372 outbuf
[buflen
++] = page_code
; // this page
373 outbuf
[buflen
++] = 0x00;
376 case 0x00: /* Supported page codes, mandatory */
379 DPRINTF("Inquiry EVPD[Supported pages] "
380 "buffer size %zd\n", req
->cmd
.xfer
);
382 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
383 outbuf
[buflen
++] = 0x80; // unit serial number
384 outbuf
[buflen
++] = 0x83; // device identification
385 if (s
->drive_kind
== SCSI_HD
) {
386 outbuf
[buflen
++] = 0xb0; // block limits
387 outbuf
[buflen
++] = 0xb2; // thin provisioning
389 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
392 case 0x80: /* Device serial number, optional */
394 int l
= strlen(s
->serial
);
396 if (l
> req
->cmd
.xfer
)
401 DPRINTF("Inquiry EVPD[Serial number] "
402 "buffer size %zd\n", req
->cmd
.xfer
);
403 outbuf
[buflen
++] = l
;
404 memcpy(outbuf
+buflen
, s
->serial
, l
);
409 case 0x83: /* Device identification page, mandatory */
411 int max_len
= 255 - 8;
412 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
414 if (id_len
> max_len
)
416 DPRINTF("Inquiry EVPD[Device identification] "
417 "buffer size %zd\n", req
->cmd
.xfer
);
419 outbuf
[buflen
++] = 4 + id_len
;
420 outbuf
[buflen
++] = 0x2; // ASCII
421 outbuf
[buflen
++] = 0; // not officially assigned
422 outbuf
[buflen
++] = 0; // reserved
423 outbuf
[buflen
++] = id_len
; // length of data following
425 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
429 case 0xb0: /* block limits */
431 unsigned int unmap_sectors
=
432 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
433 unsigned int min_io_size
=
434 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
435 unsigned int opt_io_size
=
436 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
438 if (s
->drive_kind
== SCSI_CD
) {
439 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
443 /* required VPD size with unmap support */
444 outbuf
[3] = buflen
= 0x3c;
446 memset(outbuf
+ 4, 0, buflen
- 4);
448 /* optimal transfer length granularity */
449 outbuf
[6] = (min_io_size
>> 8) & 0xff;
450 outbuf
[7] = min_io_size
& 0xff;
452 /* optimal transfer length */
453 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
454 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
455 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
456 outbuf
[15] = opt_io_size
& 0xff;
458 /* optimal unmap granularity */
459 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
460 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
461 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
462 outbuf
[31] = unmap_sectors
& 0xff;
465 case 0xb2: /* thin provisioning */
467 outbuf
[3] = buflen
= 8;
469 outbuf
[5] = 0x40; /* write same with unmap supported */
475 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
476 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
483 /* Standard INQUIRY data */
484 if (req
->cmd
.buf
[2] != 0) {
485 BADF("Error: Inquiry (STANDARD) page or code "
486 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
491 if (req
->cmd
.xfer
< 5) {
492 BADF("Error: Inquiry (STANDARD) buffer size %zd "
493 "is less than 5\n", req
->cmd
.xfer
);
497 buflen
= req
->cmd
.xfer
;
498 if (buflen
> SCSI_MAX_INQUIRY_LEN
)
499 buflen
= SCSI_MAX_INQUIRY_LEN
;
501 memset(outbuf
, 0, buflen
);
503 if (req
->lun
|| req
->cmd
.buf
[1] >> 5) {
504 outbuf
[0] = 0x7f; /* LUN not supported */
508 if (s
->drive_kind
== SCSI_CD
) {
511 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
514 outbuf
[1] = s
->removable
? 0x80 : 0;
515 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
517 memcpy(&outbuf
[8], "QEMU ", 8);
518 memset(&outbuf
[32], 0, 4);
519 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
521 * We claim conformance to SPC-3, which is required for guests
522 * to ask for modern features like READ CAPACITY(16) or the
523 * block characteristics VPD page by default. Not all of SPC-3
524 * is actually implemented, but we're good enough.
527 outbuf
[3] = 2; /* Format 2 */
530 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
532 /* If the allocation length of CDB is too small,
533 the additional length is not adjusted */
537 /* Sync data transfer and TCQ. */
538 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
542 static int mode_sense_page(SCSIRequest
*req
, int page
, uint8_t *p
,
545 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
546 BlockDriverState
*bdrv
= s
->bs
;
547 int cylinders
, heads
, secs
;
550 * If Changeable Values are requested, a mask denoting those mode parameters
551 * that are changeable shall be returned. As we currently don't support
552 * parameter changes via MODE_SELECT all bits are returned set to zero.
553 * The buffer was already menset to zero by the caller of this function.
556 case 4: /* Rigid disk device geometry page. */
559 if (page_control
== 1) { /* Changeable Values */
562 /* if a geometry hint is available, use it */
563 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
564 p
[2] = (cylinders
>> 16) & 0xff;
565 p
[3] = (cylinders
>> 8) & 0xff;
566 p
[4] = cylinders
& 0xff;
568 /* Write precomp start cylinder, disabled */
569 p
[6] = (cylinders
>> 16) & 0xff;
570 p
[7] = (cylinders
>> 8) & 0xff;
571 p
[8] = cylinders
& 0xff;
572 /* Reduced current start cylinder, disabled */
573 p
[9] = (cylinders
>> 16) & 0xff;
574 p
[10] = (cylinders
>> 8) & 0xff;
575 p
[11] = cylinders
& 0xff;
576 /* Device step rate [ns], 200ns */
579 /* Landing zone cylinder */
583 /* Medium rotation rate [rpm], 5400 rpm */
584 p
[20] = (5400 >> 8) & 0xff;
588 case 5: /* Flexible disk device geometry page. */
591 if (page_control
== 1) { /* Changeable Values */
594 /* Transfer rate [kbit/s], 5Mbit/s */
597 /* if a geometry hint is available, use it */
598 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
601 p
[6] = s
->cluster_size
* 2;
602 p
[8] = (cylinders
>> 8) & 0xff;
603 p
[9] = cylinders
& 0xff;
604 /* Write precomp start cylinder, disabled */
605 p
[10] = (cylinders
>> 8) & 0xff;
606 p
[11] = cylinders
& 0xff;
607 /* Reduced current start cylinder, disabled */
608 p
[12] = (cylinders
>> 8) & 0xff;
609 p
[13] = cylinders
& 0xff;
610 /* Device step rate [100us], 100us */
613 /* Device step pulse width [us], 1us */
615 /* Device head settle delay [100us], 100us */
618 /* Motor on delay [0.1s], 0.1s */
620 /* Motor off delay [0.1s], 0.1s */
622 /* Medium rotation rate [rpm], 5400 rpm */
623 p
[28] = (5400 >> 8) & 0xff;
627 case 8: /* Caching page. */
630 if (page_control
== 1) { /* Changeable Values */
633 if (bdrv_enable_write_cache(s
->bs
)) {
638 case 0x2a: /* CD Capabilities and Mechanical Status page. */
639 if (s
->drive_kind
!= SCSI_CD
)
643 if (page_control
== 1) { /* Changeable Values */
646 p
[2] = 3; // CD-R & CD-RW read
647 p
[3] = 0; // Writing not supported
648 p
[4] = 0x7f; /* Audio, composite, digital out,
649 mode 2 form 1&2, multi session */
650 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
651 RW corrected, C2 errors, ISRC,
653 p
[6] = 0x2d | (bdrv_is_locked(s
->bs
)? 2 : 0);
654 /* Locking supported, jumper present, eject, tray */
655 p
[7] = 0; /* no volume & mute control, no
657 p
[8] = (50 * 176) >> 8; // 50x read speed
658 p
[9] = (50 * 176) & 0xff;
659 p
[10] = 0 >> 8; // No volume
661 p
[12] = 2048 >> 8; // 2M buffer
663 p
[14] = (16 * 176) >> 8; // 16x read speed current
664 p
[15] = (16 * 176) & 0xff;
665 p
[18] = (16 * 176) >> 8; // 16x write speed
666 p
[19] = (16 * 176) & 0xff;
667 p
[20] = (16 * 176) >> 8; // 16x write speed current
668 p
[21] = (16 * 176) & 0xff;
676 static int scsi_disk_emulate_mode_sense(SCSIRequest
*req
, uint8_t *outbuf
)
678 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
680 int page
, dbd
, buflen
, page_control
;
682 uint8_t dev_specific_param
;
684 dbd
= req
->cmd
.buf
[1] & 0x8;
685 page
= req
->cmd
.buf
[2] & 0x3f;
686 page_control
= (req
->cmd
.buf
[2] & 0xc0) >> 6;
687 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
688 (req
->cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, req
->cmd
.xfer
, page_control
);
689 memset(outbuf
, 0, req
->cmd
.xfer
);
692 if (bdrv_is_read_only(s
->bs
)) {
693 dev_specific_param
= 0x80; /* Readonly. */
695 dev_specific_param
= 0x00;
698 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
699 p
[1] = 0; /* Default media type. */
700 p
[2] = dev_specific_param
;
701 p
[3] = 0; /* Block descriptor length. */
703 } else { /* MODE_SENSE_10 */
704 p
[2] = 0; /* Default media type. */
705 p
[3] = dev_specific_param
;
706 p
[6] = p
[7] = 0; /* Block descriptor length. */
710 bdrv_get_geometry(s
->bs
, &nb_sectors
);
711 if (!dbd
&& nb_sectors
) {
712 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
713 outbuf
[3] = 8; /* Block descriptor length */
714 } else { /* MODE_SENSE_10 */
715 outbuf
[7] = 8; /* Block descriptor length */
717 nb_sectors
/= s
->cluster_size
;
718 if (nb_sectors
> 0xffffff)
720 p
[0] = 0; /* media density code */
721 p
[1] = (nb_sectors
>> 16) & 0xff;
722 p
[2] = (nb_sectors
>> 8) & 0xff;
723 p
[3] = nb_sectors
& 0xff;
724 p
[4] = 0; /* reserved */
725 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
726 p
[6] = s
->cluster_size
* 2;
731 if (page_control
== 3) { /* Saved Values */
732 return -1; /* ILLEGAL_REQUEST */
740 p
+= mode_sense_page(req
, page
, p
, page_control
);
743 p
+= mode_sense_page(req
, 0x08, p
, page_control
);
744 p
+= mode_sense_page(req
, 0x2a, p
, page_control
);
747 return -1; /* ILLEGAL_REQUEST */
752 * The mode data length field specifies the length in bytes of the
753 * following data that is available to be transferred. The mode data
754 * length does not include itself.
756 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
757 outbuf
[0] = buflen
- 1;
758 } else { /* MODE_SENSE_10 */
759 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
760 outbuf
[1] = (buflen
- 2) & 0xff;
762 if (buflen
> req
->cmd
.xfer
)
763 buflen
= req
->cmd
.xfer
;
767 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
769 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
770 int start_track
, format
, msf
, toclen
;
773 msf
= req
->cmd
.buf
[1] & 2;
774 format
= req
->cmd
.buf
[2] & 0xf;
775 start_track
= req
->cmd
.buf
[6];
776 bdrv_get_geometry(s
->bs
, &nb_sectors
);
777 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
778 nb_sectors
/= s
->cluster_size
;
781 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
784 /* multi session : only a single session defined */
786 memset(outbuf
, 0, 12);
792 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
797 if (toclen
> req
->cmd
.xfer
)
798 toclen
= req
->cmd
.xfer
;
802 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
)
804 SCSIRequest
*req
= &r
->req
;
805 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
810 switch (req
->cmd
.buf
[0]) {
811 case TEST_UNIT_READY
:
812 if (!bdrv_is_inserted(s
->bs
))
816 if (req
->cmd
.xfer
< 4)
817 goto illegal_request
;
818 memset(outbuf
, 0, 4);
820 if (s
->sense
.key
== NOT_READY
&& req
->cmd
.xfer
>= 18) {
821 memset(outbuf
, 0, 18);
824 /* asc 0x3a, ascq 0: Medium not present */
830 outbuf
[2] = s
->sense
.key
;
831 scsi_disk_clear_sense(s
);
834 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
836 goto illegal_request
;
840 buflen
= scsi_disk_emulate_mode_sense(req
, outbuf
);
842 goto illegal_request
;
845 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
847 goto illegal_request
;
850 if (req
->cmd
.buf
[1] & 1)
851 goto illegal_request
;
854 if (req
->cmd
.buf
[1] & 3)
855 goto illegal_request
;
858 if (req
->cmd
.buf
[1] & 1)
859 goto illegal_request
;
862 if (req
->cmd
.buf
[1] & 3)
863 goto illegal_request
;
866 if (s
->drive_kind
== SCSI_CD
&& (req
->cmd
.buf
[4] & 2)) {
867 /* load/eject medium */
868 bdrv_eject(s
->bs
, !(req
->cmd
.buf
[4] & 1));
871 case ALLOW_MEDIUM_REMOVAL
:
872 bdrv_set_locked(s
->bs
, req
->cmd
.buf
[4] & 1);
875 /* The normal LEN field for this command is zero. */
876 memset(outbuf
, 0, 8);
877 bdrv_get_geometry(s
->bs
, &nb_sectors
);
880 nb_sectors
/= s
->cluster_size
;
881 /* Returned value is the address of the last sector. */
883 /* Remember the new size for read/write sanity checking. */
884 s
->max_lba
= nb_sectors
;
885 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
886 if (nb_sectors
> UINT32_MAX
)
887 nb_sectors
= UINT32_MAX
;
888 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
889 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
890 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
891 outbuf
[3] = nb_sectors
& 0xff;
894 outbuf
[6] = s
->cluster_size
* 2;
898 case SYNCHRONIZE_CACHE
:
899 ret
= bdrv_flush(s
->bs
);
901 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
906 case GET_CONFIGURATION
:
907 memset(outbuf
, 0, 8);
908 /* ??? This should probably return much more information. For now
909 just return the basic header indicating the CD-ROM profile. */
910 outbuf
[7] = 8; // CD-ROM
913 case SERVICE_ACTION_IN
:
914 /* Service Action In subcommands. */
915 if ((req
->cmd
.buf
[1] & 31) == 0x10) {
916 DPRINTF("SAI READ CAPACITY(16)\n");
917 memset(outbuf
, 0, req
->cmd
.xfer
);
918 bdrv_get_geometry(s
->bs
, &nb_sectors
);
921 nb_sectors
/= s
->cluster_size
;
922 /* Returned value is the address of the last sector. */
924 /* Remember the new size for read/write sanity checking. */
925 s
->max_lba
= nb_sectors
;
926 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
927 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
928 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
929 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
930 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
931 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
932 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
933 outbuf
[7] = nb_sectors
& 0xff;
936 outbuf
[10] = s
->cluster_size
* 2;
939 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
941 /* set TPE bit if the format supports discard */
942 if (s
->qdev
.conf
.discard_granularity
) {
946 /* Protection, exponent and lowest lba field left blank. */
947 buflen
= req
->cmd
.xfer
;
950 DPRINTF("Unsupported Service Action In\n");
951 goto illegal_request
;
953 if (req
->cmd
.xfer
< 16)
954 goto illegal_request
;
955 memset(outbuf
, 0, 16);
962 DPRINTF("Rezero Unit\n");
963 if (!bdrv_is_inserted(s
->bs
)) {
968 goto illegal_request
;
970 scsi_req_set_status(r
, GOOD
, NO_SENSE
);
974 scsi_command_complete(r
, CHECK_CONDITION
, NOT_READY
);
978 scsi_command_complete(r
, CHECK_CONDITION
, ILLEGAL_REQUEST
);
982 /* Execute a scsi command. Returns the length of the data expected by the
983 command. This will be Positive for data transfers from the device
984 (eg. disk reads), negative for transfers to the device (eg. disk writes),
985 and zero if the command does not transfer any data. */
987 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
989 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
990 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
997 scsi_req_enqueue(req
);
999 outbuf
= (uint8_t *)r
->iov
.iov_base
;
1001 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
1003 if (scsi_req_parse(&r
->req
, buf
) != 0) {
1004 BADF("Unsupported command length, command %x\n", command
);
1010 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1011 printf(" 0x%02x", buf
[i
]);
1017 if (req
->lun
|| buf
[1] >> 5) {
1018 /* Only LUN 0 supported. */
1019 DPRINTF("Unimplemented LUN %d\n", req
->lun
? req
->lun
: buf
[1] >> 5);
1020 if (command
!= REQUEST_SENSE
&& command
!= INQUIRY
)
1024 case TEST_UNIT_READY
:
1034 case ALLOW_MEDIUM_REMOVAL
:
1036 case SYNCHRONIZE_CACHE
:
1038 case GET_CONFIGURATION
:
1039 case SERVICE_ACTION_IN
:
1043 rc
= scsi_disk_emulate_command(r
, outbuf
);
1048 r
->iov
.iov_len
= rc
;
1054 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1055 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1056 if (r
->req
.cmd
.lba
> s
->max_lba
)
1058 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1059 r
->sector_count
= len
* s
->cluster_size
;
1066 case WRITE_VERIFY_12
:
1067 case WRITE_VERIFY_16
:
1068 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1069 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1070 (command
& 0xe) == 0xe ? "And Verify " : "",
1071 r
->req
.cmd
.lba
, len
);
1072 if (r
->req
.cmd
.lba
> s
->max_lba
)
1074 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1075 r
->sector_count
= len
* s
->cluster_size
;
1079 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1080 /* We don't support mode parameter changes.
1081 Allow the mode parameter header + block descriptors only. */
1082 if (r
->req
.cmd
.xfer
> 12) {
1086 case MODE_SELECT_10
:
1087 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1088 /* We don't support mode parameter changes.
1089 Allow the mode parameter header + block descriptors only. */
1090 if (r
->req
.cmd
.xfer
> 16) {
1096 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1098 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1103 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1105 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1106 r
->req
.cmd
.lba
, len
);
1108 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1113 * We only support WRITE SAME with the unmap bit set for now.
1115 if (!(buf
[1] & 0x8)) {
1119 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1120 len
* s
->cluster_size
);
1122 /* XXX: better error code ?*/
1128 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1130 scsi_command_complete(r
, CHECK_CONDITION
, ILLEGAL_REQUEST
);
1133 scsi_command_complete(r
, CHECK_CONDITION
, HARDWARE_ERROR
);
1136 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1137 scsi_command_complete(r
, GOOD
, NO_SENSE
);
1139 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1143 if (!r
->sector_count
)
1144 r
->sector_count
= -1;
1149 static void scsi_disk_reset(DeviceState
*dev
)
1151 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1152 uint64_t nb_sectors
;
1154 scsi_device_purge_requests(&s
->qdev
);
1156 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1157 nb_sectors
/= s
->cluster_size
;
1161 s
->max_lba
= nb_sectors
;
1164 static void scsi_destroy(SCSIDevice
*dev
)
1166 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1168 scsi_device_purge_requests(&s
->qdev
);
1169 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1172 static int scsi_initfn(SCSIDevice
*dev
, SCSIDriveKind kind
)
1174 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1177 if (!s
->qdev
.conf
.bs
) {
1178 error_report("scsi-disk: drive property not set");
1181 s
->bs
= s
->qdev
.conf
.bs
;
1182 s
->drive_kind
= kind
;
1184 if (kind
== SCSI_HD
&& !bdrv_is_inserted(s
->bs
)) {
1185 error_report("Device needs media, but drive is empty");
1190 /* try to fall back to value set with legacy -drive serial=... */
1191 dinfo
= drive_get_by_blockdev(s
->bs
);
1192 s
->serial
= qemu_strdup(*dinfo
->serial
? dinfo
->serial
: "0");
1196 s
->version
= qemu_strdup(QEMU_VERSION
);
1199 if (bdrv_is_sg(s
->bs
)) {
1200 error_report("scsi-disk: unwanted /dev/sg*");
1204 if (kind
== SCSI_CD
) {
1205 s
->qdev
.blocksize
= 2048;
1207 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1209 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1210 s
->bs
->buffer_alignment
= s
->qdev
.blocksize
;
1212 s
->qdev
.type
= TYPE_DISK
;
1213 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1214 bdrv_set_removable(s
->bs
, kind
== SCSI_CD
);
1215 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1219 static int scsi_hd_initfn(SCSIDevice
*dev
)
1221 return scsi_initfn(dev
, SCSI_HD
);
1224 static int scsi_cd_initfn(SCSIDevice
*dev
)
1226 return scsi_initfn(dev
, SCSI_CD
);
1229 static int scsi_disk_initfn(SCSIDevice
*dev
)
1234 if (!dev
->conf
.bs
) {
1235 kind
= SCSI_HD
; /* will die in scsi_initfn() */
1237 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1238 kind
= dinfo
->media_cd
? SCSI_CD
: SCSI_HD
;
1241 return scsi_initfn(dev
, kind
);
1244 #define DEFINE_SCSI_DISK_PROPERTIES() \
1245 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1246 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1247 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1249 static SCSIDeviceInfo scsi_disk_info
[] = {
1251 .qdev
.name
= "scsi-hd",
1252 .qdev
.fw_name
= "disk",
1253 .qdev
.desc
= "virtual SCSI disk",
1254 .qdev
.size
= sizeof(SCSIDiskState
),
1255 .qdev
.reset
= scsi_disk_reset
,
1256 .init
= scsi_hd_initfn
,
1257 .destroy
= scsi_destroy
,
1258 .alloc_req
= scsi_new_request
,
1259 .free_req
= scsi_free_request
,
1260 .send_command
= scsi_send_command
,
1261 .read_data
= scsi_read_data
,
1262 .write_data
= scsi_write_data
,
1263 .cancel_io
= scsi_cancel_io
,
1264 .get_buf
= scsi_get_buf
,
1265 .qdev
.props
= (Property
[]) {
1266 DEFINE_SCSI_DISK_PROPERTIES(),
1267 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1268 DEFINE_PROP_END_OF_LIST(),
1271 .qdev
.name
= "scsi-cd",
1272 .qdev
.fw_name
= "disk",
1273 .qdev
.desc
= "virtual SCSI CD-ROM",
1274 .qdev
.size
= sizeof(SCSIDiskState
),
1275 .qdev
.reset
= scsi_disk_reset
,
1276 .init
= scsi_cd_initfn
,
1277 .destroy
= scsi_destroy
,
1278 .alloc_req
= scsi_new_request
,
1279 .free_req
= scsi_free_request
,
1280 .send_command
= scsi_send_command
,
1281 .read_data
= scsi_read_data
,
1282 .write_data
= scsi_write_data
,
1283 .cancel_io
= scsi_cancel_io
,
1284 .get_buf
= scsi_get_buf
,
1285 .qdev
.props
= (Property
[]) {
1286 DEFINE_SCSI_DISK_PROPERTIES(),
1287 DEFINE_PROP_END_OF_LIST(),
1290 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1291 .qdev
.fw_name
= "disk",
1292 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1293 .qdev
.size
= sizeof(SCSIDiskState
),
1294 .qdev
.reset
= scsi_disk_reset
,
1295 .init
= scsi_disk_initfn
,
1296 .destroy
= scsi_destroy
,
1297 .alloc_req
= scsi_new_request
,
1298 .free_req
= scsi_free_request
,
1299 .send_command
= scsi_send_command
,
1300 .read_data
= scsi_read_data
,
1301 .write_data
= scsi_write_data
,
1302 .cancel_io
= scsi_cancel_io
,
1303 .get_buf
= scsi_get_buf
,
1304 .qdev
.props
= (Property
[]) {
1305 DEFINE_SCSI_DISK_PROPERTIES(),
1306 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1307 DEFINE_PROP_END_OF_LIST(),
1312 static void scsi_disk_register_devices(void)
1316 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1317 scsi_qdev_register(&scsi_disk_info
[i
]);
1320 device_init(scsi_disk_register_devices
)