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
;
67 /* The qemu block layer uses a fixed 512 byte sector size.
68 This is the number of 512 byte blocks in a single scsi sector. */
78 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
79 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
);
81 static void scsi_free_request(SCSIRequest
*req
)
83 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
85 qemu_vfree(r
->iov
.iov_base
);
88 /* Helper function for command completion with sense. */
89 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
91 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
92 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
93 scsi_req_build_sense(&r
->req
, sense
);
94 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
97 /* Cancel a pending data transfer. */
98 static void scsi_cancel_io(SCSIRequest
*req
)
100 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
102 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
104 bdrv_aio_cancel(r
->req
.aiocb
);
109 static void scsi_read_complete(void * opaque
, int ret
)
111 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
112 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
115 if (r
->req
.aiocb
!= NULL
) {
117 bdrv_acct_done(s
->bs
, &r
->acct
);
121 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
126 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->iov
.iov_len
);
128 n
= r
->iov
.iov_len
/ 512;
130 r
->sector_count
-= n
;
131 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
134 static void scsi_flush_complete(void * opaque
, int ret
)
136 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
137 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
139 if (r
->req
.aiocb
!= NULL
) {
141 bdrv_acct_done(s
->bs
, &r
->acct
);
145 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
150 scsi_req_complete(&r
->req
, GOOD
);
153 /* Read more data from scsi device into buffer. */
154 static void scsi_read_data(SCSIRequest
*req
)
156 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
157 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
160 if (r
->sector_count
== (uint32_t)-1) {
161 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
163 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
166 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
167 if (r
->sector_count
== 0) {
168 /* This also clears the sense buffer for REQUEST SENSE. */
169 scsi_req_complete(&r
->req
, GOOD
);
173 /* No data transfer may already be in progress */
174 assert(r
->req
.aiocb
== NULL
);
176 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
177 DPRINTF("Data transfer direction invalid\n");
178 scsi_read_complete(r
, -EINVAL
);
183 if (n
> SCSI_DMA_BUF_SIZE
/ 512)
184 n
= SCSI_DMA_BUF_SIZE
/ 512;
186 r
->iov
.iov_len
= n
* 512;
187 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
189 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
190 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
191 scsi_read_complete
, r
);
192 if (r
->req
.aiocb
== NULL
) {
193 scsi_read_complete(r
, -EIO
);
197 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
199 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
200 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
201 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
203 if (action
== BLOCK_ERR_IGNORE
) {
204 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
208 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
209 || action
== BLOCK_ERR_STOP_ANY
) {
211 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
212 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
214 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
215 vm_stop(VMSTOP_DISKFULL
);
219 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
222 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
225 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
228 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
233 static void scsi_write_complete(void * opaque
, int ret
)
235 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
236 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
240 if (r
->req
.aiocb
!= NULL
) {
242 bdrv_acct_done(s
->bs
, &r
->acct
);
246 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
251 n
= r
->iov
.iov_len
/ 512;
253 r
->sector_count
-= n
;
254 if (r
->sector_count
== 0) {
255 scsi_req_complete(&r
->req
, GOOD
);
257 len
= r
->sector_count
* 512;
258 if (len
> SCSI_DMA_BUF_SIZE
) {
259 len
= SCSI_DMA_BUF_SIZE
;
261 r
->iov
.iov_len
= len
;
262 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, len
);
263 scsi_req_data(&r
->req
, len
);
267 static void scsi_write_data(SCSIRequest
*req
)
269 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
270 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
273 /* No data transfer may already be in progress */
274 assert(r
->req
.aiocb
== NULL
);
276 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
277 DPRINTF("Data transfer direction invalid\n");
278 scsi_write_complete(r
, -EINVAL
);
282 n
= r
->iov
.iov_len
/ 512;
284 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
286 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
287 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
288 scsi_write_complete
, r
);
289 if (r
->req
.aiocb
== NULL
) {
290 scsi_write_complete(r
, -ENOMEM
);
293 /* Invoke completion routine to fetch data from host. */
294 scsi_write_complete(r
, 0);
298 static void scsi_dma_restart_bh(void *opaque
)
300 SCSIDiskState
*s
= opaque
;
304 qemu_bh_delete(s
->bh
);
307 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
308 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
309 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
310 int status
= r
->status
;
314 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
316 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
317 case SCSI_REQ_STATUS_RETRY_READ
:
318 scsi_read_data(&r
->req
);
320 case SCSI_REQ_STATUS_RETRY_WRITE
:
321 scsi_write_data(&r
->req
);
323 case SCSI_REQ_STATUS_RETRY_FLUSH
:
324 ret
= scsi_disk_emulate_command(r
, r
->iov
.iov_base
);
326 scsi_req_complete(&r
->req
, GOOD
);
333 static void scsi_dma_restart_cb(void *opaque
, int running
, int reason
)
335 SCSIDiskState
*s
= opaque
;
341 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
342 qemu_bh_schedule(s
->bh
);
346 /* Return a pointer to the data buffer. */
347 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
349 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
351 return (uint8_t *)r
->iov
.iov_base
;
354 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
356 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
359 if (req
->cmd
.buf
[1] & 0x2) {
360 /* Command support data - optional, not implemented */
361 BADF("optional INQUIRY command support request not implemented\n");
365 if (req
->cmd
.buf
[1] & 0x1) {
366 /* Vital product data */
367 uint8_t page_code
= req
->cmd
.buf
[2];
368 if (req
->cmd
.xfer
< 4) {
369 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
370 "less than 4\n", page_code
, req
->cmd
.xfer
);
374 if (s
->qdev
.type
== TYPE_ROM
) {
375 outbuf
[buflen
++] = 5;
377 outbuf
[buflen
++] = 0;
379 outbuf
[buflen
++] = page_code
; // this page
380 outbuf
[buflen
++] = 0x00;
383 case 0x00: /* Supported page codes, mandatory */
386 DPRINTF("Inquiry EVPD[Supported pages] "
387 "buffer size %zd\n", req
->cmd
.xfer
);
389 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
391 outbuf
[buflen
++] = 0x80; // unit serial number
392 outbuf
[buflen
++] = 0x83; // device identification
393 if (s
->qdev
.type
== TYPE_DISK
) {
394 outbuf
[buflen
++] = 0xb0; // block limits
395 outbuf
[buflen
++] = 0xb2; // thin provisioning
397 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
400 case 0x80: /* Device serial number, optional */
405 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
409 l
= strlen(s
->serial
);
410 if (l
> req
->cmd
.xfer
)
415 DPRINTF("Inquiry EVPD[Serial number] "
416 "buffer size %zd\n", req
->cmd
.xfer
);
417 outbuf
[buflen
++] = l
;
418 memcpy(outbuf
+buflen
, s
->serial
, l
);
423 case 0x83: /* Device identification page, mandatory */
425 int max_len
= 255 - 8;
426 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
428 if (id_len
> max_len
)
430 DPRINTF("Inquiry EVPD[Device identification] "
431 "buffer size %zd\n", req
->cmd
.xfer
);
433 outbuf
[buflen
++] = 4 + id_len
;
434 outbuf
[buflen
++] = 0x2; // ASCII
435 outbuf
[buflen
++] = 0; // not officially assigned
436 outbuf
[buflen
++] = 0; // reserved
437 outbuf
[buflen
++] = id_len
; // length of data following
439 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
443 case 0xb0: /* block limits */
445 unsigned int unmap_sectors
=
446 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
447 unsigned int min_io_size
=
448 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
449 unsigned int opt_io_size
=
450 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
452 if (s
->qdev
.type
== TYPE_ROM
) {
453 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
457 /* required VPD size with unmap support */
458 outbuf
[3] = buflen
= 0x3c;
460 memset(outbuf
+ 4, 0, buflen
- 4);
462 /* optimal transfer length granularity */
463 outbuf
[6] = (min_io_size
>> 8) & 0xff;
464 outbuf
[7] = min_io_size
& 0xff;
466 /* optimal transfer length */
467 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
468 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
469 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
470 outbuf
[15] = opt_io_size
& 0xff;
472 /* optimal unmap granularity */
473 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
474 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
475 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
476 outbuf
[31] = unmap_sectors
& 0xff;
479 case 0xb2: /* thin provisioning */
481 outbuf
[3] = buflen
= 8;
483 outbuf
[5] = 0x40; /* write same with unmap supported */
489 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
490 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
497 /* Standard INQUIRY data */
498 if (req
->cmd
.buf
[2] != 0) {
499 BADF("Error: Inquiry (STANDARD) page or code "
500 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
505 if (req
->cmd
.xfer
< 5) {
506 BADF("Error: Inquiry (STANDARD) buffer size %zd "
507 "is less than 5\n", req
->cmd
.xfer
);
511 buflen
= req
->cmd
.xfer
;
512 if (buflen
> SCSI_MAX_INQUIRY_LEN
)
513 buflen
= SCSI_MAX_INQUIRY_LEN
;
515 memset(outbuf
, 0, buflen
);
517 outbuf
[0] = s
->qdev
.type
& 0x1f;
518 if (s
->qdev
.type
== TYPE_ROM
) {
520 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
522 outbuf
[1] = s
->removable
? 0x80 : 0;
523 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
525 memcpy(&outbuf
[8], "QEMU ", 8);
526 memset(&outbuf
[32], 0, 4);
527 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
529 * We claim conformance to SPC-3, which is required for guests
530 * to ask for modern features like READ CAPACITY(16) or the
531 * block characteristics VPD page by default. Not all of SPC-3
532 * is actually implemented, but we're good enough.
535 outbuf
[3] = 2; /* Format 2 */
538 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
540 /* If the allocation length of CDB is too small,
541 the additional length is not adjusted */
545 /* Sync data transfer and TCQ. */
546 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
550 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
553 BlockDriverState
*bdrv
= s
->bs
;
554 int cylinders
, heads
, secs
;
555 uint8_t *p
= *p_outbuf
;
558 * If Changeable Values are requested, a mask denoting those mode parameters
559 * that are changeable shall be returned. As we currently don't support
560 * parameter changes via MODE_SELECT all bits are returned set to zero.
561 * The buffer was already menset to zero by the caller of this function.
564 case 4: /* Rigid disk device geometry page. */
565 if (s
->qdev
.type
== TYPE_ROM
) {
570 if (page_control
== 1) { /* Changeable Values */
573 /* if a geometry hint is available, use it */
574 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
575 p
[2] = (cylinders
>> 16) & 0xff;
576 p
[3] = (cylinders
>> 8) & 0xff;
577 p
[4] = cylinders
& 0xff;
579 /* Write precomp start cylinder, disabled */
580 p
[6] = (cylinders
>> 16) & 0xff;
581 p
[7] = (cylinders
>> 8) & 0xff;
582 p
[8] = cylinders
& 0xff;
583 /* Reduced current start cylinder, disabled */
584 p
[9] = (cylinders
>> 16) & 0xff;
585 p
[10] = (cylinders
>> 8) & 0xff;
586 p
[11] = cylinders
& 0xff;
587 /* Device step rate [ns], 200ns */
590 /* Landing zone cylinder */
594 /* Medium rotation rate [rpm], 5400 rpm */
595 p
[20] = (5400 >> 8) & 0xff;
599 case 5: /* Flexible disk device geometry page. */
600 if (s
->qdev
.type
== TYPE_ROM
) {
605 if (page_control
== 1) { /* Changeable Values */
608 /* Transfer rate [kbit/s], 5Mbit/s */
611 /* if a geometry hint is available, use it */
612 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
615 p
[6] = s
->cluster_size
* 2;
616 p
[8] = (cylinders
>> 8) & 0xff;
617 p
[9] = cylinders
& 0xff;
618 /* Write precomp start cylinder, disabled */
619 p
[10] = (cylinders
>> 8) & 0xff;
620 p
[11] = cylinders
& 0xff;
621 /* Reduced current start cylinder, disabled */
622 p
[12] = (cylinders
>> 8) & 0xff;
623 p
[13] = cylinders
& 0xff;
624 /* Device step rate [100us], 100us */
627 /* Device step pulse width [us], 1us */
629 /* Device head settle delay [100us], 100us */
632 /* Motor on delay [0.1s], 0.1s */
634 /* Motor off delay [0.1s], 0.1s */
636 /* Medium rotation rate [rpm], 5400 rpm */
637 p
[28] = (5400 >> 8) & 0xff;
641 case 8: /* Caching page. */
644 if (page_control
== 1) { /* Changeable Values */
647 if (bdrv_enable_write_cache(s
->bs
)) {
652 case 0x2a: /* CD Capabilities and Mechanical Status page. */
653 if (s
->qdev
.type
!= TYPE_ROM
) {
658 if (page_control
== 1) { /* Changeable Values */
661 p
[2] = 3; // CD-R & CD-RW read
662 p
[3] = 0; // Writing not supported
663 p
[4] = 0x7f; /* Audio, composite, digital out,
664 mode 2 form 1&2, multi session */
665 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
666 RW corrected, C2 errors, ISRC,
668 p
[6] = 0x2d | (bdrv_is_locked(s
->bs
)? 2 : 0);
669 /* Locking supported, jumper present, eject, tray */
670 p
[7] = 0; /* no volume & mute control, no
672 p
[8] = (50 * 176) >> 8; // 50x read speed
673 p
[9] = (50 * 176) & 0xff;
674 p
[10] = 0 >> 8; // No volume
676 p
[12] = 2048 >> 8; // 2M buffer
678 p
[14] = (16 * 176) >> 8; // 16x read speed current
679 p
[15] = (16 * 176) & 0xff;
680 p
[18] = (16 * 176) >> 8; // 16x write speed
681 p
[19] = (16 * 176) & 0xff;
682 p
[20] = (16 * 176) >> 8; // 16x write speed current
683 p
[21] = (16 * 176) & 0xff;
690 *p_outbuf
+= p
[1] + 2;
694 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
696 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
698 int page
, dbd
, buflen
, ret
, page_control
;
700 uint8_t dev_specific_param
;
702 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
703 page
= r
->req
.cmd
.buf
[2] & 0x3f;
704 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
705 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
706 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
707 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
710 if (bdrv_is_read_only(s
->bs
)) {
711 dev_specific_param
= 0x80; /* Readonly. */
713 dev_specific_param
= 0x00;
716 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
717 p
[1] = 0; /* Default media type. */
718 p
[2] = dev_specific_param
;
719 p
[3] = 0; /* Block descriptor length. */
721 } else { /* MODE_SENSE_10 */
722 p
[2] = 0; /* Default media type. */
723 p
[3] = dev_specific_param
;
724 p
[6] = p
[7] = 0; /* Block descriptor length. */
728 bdrv_get_geometry(s
->bs
, &nb_sectors
);
729 if (!dbd
&& nb_sectors
) {
730 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
731 outbuf
[3] = 8; /* Block descriptor length */
732 } else { /* MODE_SENSE_10 */
733 outbuf
[7] = 8; /* Block descriptor length */
735 nb_sectors
/= s
->cluster_size
;
736 if (nb_sectors
> 0xffffff)
738 p
[0] = 0; /* media density code */
739 p
[1] = (nb_sectors
>> 16) & 0xff;
740 p
[2] = (nb_sectors
>> 8) & 0xff;
741 p
[3] = nb_sectors
& 0xff;
742 p
[4] = 0; /* reserved */
743 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
744 p
[6] = s
->cluster_size
* 2;
749 if (page_control
== 3) {
751 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
756 for (page
= 0; page
<= 0x3e; page
++) {
757 mode_sense_page(s
, page
, &p
, page_control
);
760 ret
= mode_sense_page(s
, page
, &p
, page_control
);
768 * The mode data length field specifies the length in bytes of the
769 * following data that is available to be transferred. The mode data
770 * length does not include itself.
772 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
773 outbuf
[0] = buflen
- 1;
774 } else { /* MODE_SENSE_10 */
775 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
776 outbuf
[1] = (buflen
- 2) & 0xff;
778 if (buflen
> r
->req
.cmd
.xfer
)
779 buflen
= r
->req
.cmd
.xfer
;
783 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
785 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
786 int start_track
, format
, msf
, toclen
;
789 msf
= req
->cmd
.buf
[1] & 2;
790 format
= req
->cmd
.buf
[2] & 0xf;
791 start_track
= req
->cmd
.buf
[6];
792 bdrv_get_geometry(s
->bs
, &nb_sectors
);
793 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
794 nb_sectors
/= s
->cluster_size
;
797 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
800 /* multi session : only a single session defined */
802 memset(outbuf
, 0, 12);
808 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
813 if (toclen
> req
->cmd
.xfer
)
814 toclen
= req
->cmd
.xfer
;
818 static void scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
820 SCSIRequest
*req
= &r
->req
;
821 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
822 bool start
= req
->cmd
.buf
[4] & 1;
823 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
825 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
826 bdrv_eject(s
->bs
, !start
);
827 s
->tray_open
= !start
;
831 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
)
833 SCSIRequest
*req
= &r
->req
;
834 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
838 switch (req
->cmd
.buf
[0]) {
839 case TEST_UNIT_READY
:
840 if (!bdrv_is_inserted(s
->bs
))
844 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
846 goto illegal_request
;
850 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
852 goto illegal_request
;
855 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
857 goto illegal_request
;
860 if (req
->cmd
.buf
[1] & 1)
861 goto illegal_request
;
864 if (req
->cmd
.buf
[1] & 3)
865 goto illegal_request
;
868 if (req
->cmd
.buf
[1] & 1)
869 goto illegal_request
;
872 if (req
->cmd
.buf
[1] & 3)
873 goto illegal_request
;
876 scsi_disk_emulate_start_stop(r
);
878 case ALLOW_MEDIUM_REMOVAL
:
879 bdrv_set_locked(s
->bs
, req
->cmd
.buf
[4] & 1);
881 case READ_CAPACITY_10
:
882 /* The normal LEN field for this command is zero. */
883 memset(outbuf
, 0, 8);
884 bdrv_get_geometry(s
->bs
, &nb_sectors
);
887 nb_sectors
/= s
->cluster_size
;
888 /* Returned value is the address of the last sector. */
890 /* Remember the new size for read/write sanity checking. */
891 s
->max_lba
= nb_sectors
;
892 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
893 if (nb_sectors
> UINT32_MAX
)
894 nb_sectors
= UINT32_MAX
;
895 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
896 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
897 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
898 outbuf
[3] = nb_sectors
& 0xff;
901 outbuf
[6] = s
->cluster_size
* 2;
905 case GET_CONFIGURATION
:
906 memset(outbuf
, 0, 8);
907 /* ??? This should probably return much more information. For now
908 just return the basic header indicating the CD-ROM profile. */
909 outbuf
[7] = 8; // CD-ROM
912 case SERVICE_ACTION_IN_16
:
913 /* Service Action In subcommands. */
914 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
915 DPRINTF("SAI READ CAPACITY(16)\n");
916 memset(outbuf
, 0, req
->cmd
.xfer
);
917 bdrv_get_geometry(s
->bs
, &nb_sectors
);
920 nb_sectors
/= s
->cluster_size
;
921 /* Returned value is the address of the last sector. */
923 /* Remember the new size for read/write sanity checking. */
924 s
->max_lba
= nb_sectors
;
925 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
926 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
927 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
928 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
929 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
930 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
931 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
932 outbuf
[7] = nb_sectors
& 0xff;
935 outbuf
[10] = s
->cluster_size
* 2;
938 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
940 /* set TPE bit if the format supports discard */
941 if (s
->qdev
.conf
.discard_granularity
) {
945 /* Protection, exponent and lowest lba field left blank. */
946 buflen
= req
->cmd
.xfer
;
949 DPRINTF("Unsupported Service Action In\n");
950 goto illegal_request
;
954 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
960 if (!bdrv_is_inserted(s
->bs
)) {
961 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
963 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
968 if (r
->req
.status
== -1) {
969 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
974 /* Execute a scsi command. Returns the length of the data expected by the
975 command. This will be Positive for data transfers from the device
976 (eg. disk reads), negative for transfers to the device (eg. disk writes),
977 and zero if the command does not transfer any data. */
979 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
981 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
982 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
989 outbuf
= (uint8_t *)r
->iov
.iov_base
;
990 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
995 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
996 printf(" 0x%02x", buf
[i
]);
1003 case TEST_UNIT_READY
:
1012 case ALLOW_MEDIUM_REMOVAL
:
1013 case READ_CAPACITY_10
:
1015 case GET_CONFIGURATION
:
1016 case SERVICE_ACTION_IN_16
:
1018 rc
= scsi_disk_emulate_command(r
, outbuf
);
1023 r
->iov
.iov_len
= rc
;
1025 case SYNCHRONIZE_CACHE
:
1026 bdrv_acct_start(s
->bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1027 r
->req
.aiocb
= bdrv_aio_flush(s
->bs
, scsi_flush_complete
, r
);
1028 if (r
->req
.aiocb
== NULL
) {
1029 scsi_flush_complete(r
, -EIO
);
1036 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1037 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1038 if (r
->req
.cmd
.lba
> s
->max_lba
)
1040 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1041 r
->sector_count
= len
* s
->cluster_size
;
1047 case WRITE_VERIFY_10
:
1048 case WRITE_VERIFY_12
:
1049 case WRITE_VERIFY_16
:
1050 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1051 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1052 (command
& 0xe) == 0xe ? "And Verify " : "",
1053 r
->req
.cmd
.lba
, len
);
1054 if (r
->req
.cmd
.lba
> s
->max_lba
)
1056 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1057 r
->sector_count
= len
* s
->cluster_size
;
1060 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1061 /* We don't support mode parameter changes.
1062 Allow the mode parameter header + block descriptors only. */
1063 if (r
->req
.cmd
.xfer
> 12) {
1067 case MODE_SELECT_10
:
1068 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1069 /* We don't support mode parameter changes.
1070 Allow the mode parameter header + block descriptors only. */
1071 if (r
->req
.cmd
.xfer
> 16) {
1077 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1079 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1084 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1086 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1087 r
->req
.cmd
.lba
, len
);
1089 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1094 * We only support WRITE SAME with the unmap bit set for now.
1096 if (!(buf
[1] & 0x8)) {
1100 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1101 len
* s
->cluster_size
);
1103 /* XXX: better error code ?*/
1111 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1112 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1115 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1118 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1121 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1122 scsi_req_complete(&r
->req
, GOOD
);
1124 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1125 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1128 if (!r
->sector_count
)
1129 r
->sector_count
= -1;
1134 static void scsi_disk_reset(DeviceState
*dev
)
1136 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1137 uint64_t nb_sectors
;
1139 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1141 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1142 nb_sectors
/= s
->cluster_size
;
1146 s
->max_lba
= nb_sectors
;
1149 static void scsi_destroy(SCSIDevice
*dev
)
1151 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1153 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1154 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1157 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1159 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1162 if (!s
->qdev
.conf
.bs
) {
1163 error_report("scsi-disk: drive property not set");
1166 s
->bs
= s
->qdev
.conf
.bs
;
1168 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1169 error_report("Device needs media, but drive is empty");
1174 /* try to fall back to value set with legacy -drive serial=... */
1175 dinfo
= drive_get_by_blockdev(s
->bs
);
1176 if (*dinfo
->serial
) {
1177 s
->serial
= g_strdup(dinfo
->serial
);
1182 s
->version
= g_strdup(QEMU_VERSION
);
1185 if (bdrv_is_sg(s
->bs
)) {
1186 error_report("scsi-disk: unwanted /dev/sg*");
1190 if (scsi_type
== TYPE_ROM
) {
1191 s
->qdev
.blocksize
= 2048;
1192 } else if (scsi_type
== TYPE_DISK
) {
1193 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1195 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1198 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1199 s
->bs
->buffer_alignment
= s
->qdev
.blocksize
;
1201 s
->qdev
.type
= scsi_type
;
1202 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1203 bdrv_set_removable(s
->bs
, scsi_type
== TYPE_ROM
);
1204 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1208 static int scsi_hd_initfn(SCSIDevice
*dev
)
1210 return scsi_initfn(dev
, TYPE_DISK
);
1213 static int scsi_cd_initfn(SCSIDevice
*dev
)
1215 return scsi_initfn(dev
, TYPE_ROM
);
1218 static int scsi_disk_initfn(SCSIDevice
*dev
)
1223 if (!dev
->conf
.bs
) {
1224 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1226 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1227 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1230 return scsi_initfn(dev
, scsi_type
);
1233 static SCSIReqOps scsi_disk_reqops
= {
1234 .size
= sizeof(SCSIDiskReq
),
1235 .free_req
= scsi_free_request
,
1236 .send_command
= scsi_send_command
,
1237 .read_data
= scsi_read_data
,
1238 .write_data
= scsi_write_data
,
1239 .cancel_io
= scsi_cancel_io
,
1240 .get_buf
= scsi_get_buf
,
1243 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1244 uint32_t lun
, void *hba_private
)
1246 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1250 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1251 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1252 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, SCSI_DMA_BUF_SIZE
);
1256 #define DEFINE_SCSI_DISK_PROPERTIES() \
1257 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1258 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1259 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1261 static SCSIDeviceInfo scsi_disk_info
[] = {
1263 .qdev
.name
= "scsi-hd",
1264 .qdev
.fw_name
= "disk",
1265 .qdev
.desc
= "virtual SCSI disk",
1266 .qdev
.size
= sizeof(SCSIDiskState
),
1267 .qdev
.reset
= scsi_disk_reset
,
1268 .init
= scsi_hd_initfn
,
1269 .destroy
= scsi_destroy
,
1270 .alloc_req
= scsi_new_request
,
1271 .qdev
.props
= (Property
[]) {
1272 DEFINE_SCSI_DISK_PROPERTIES(),
1273 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1274 DEFINE_PROP_END_OF_LIST(),
1277 .qdev
.name
= "scsi-cd",
1278 .qdev
.fw_name
= "disk",
1279 .qdev
.desc
= "virtual SCSI CD-ROM",
1280 .qdev
.size
= sizeof(SCSIDiskState
),
1281 .qdev
.reset
= scsi_disk_reset
,
1282 .init
= scsi_cd_initfn
,
1283 .destroy
= scsi_destroy
,
1284 .alloc_req
= scsi_new_request
,
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 .qdev
.props
= (Property
[]) {
1299 DEFINE_SCSI_DISK_PROPERTIES(),
1300 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1301 DEFINE_PROP_END_OF_LIST(),
1306 static void scsi_disk_register_devices(void)
1310 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1311 scsi_qdev_register(&scsi_disk_info
[i
]);
1314 device_init(scsi_disk_register_devices
)