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"
40 #include "block_int.h"
42 #define SCSI_DMA_BUF_SIZE 131072
43 #define SCSI_MAX_INQUIRY_LEN 256
45 #define SCSI_REQ_STATUS_RETRY 0x01
46 #define SCSI_REQ_STATUS_RETRY_TYPE_MASK 0x06
47 #define SCSI_REQ_STATUS_RETRY_READ 0x00
48 #define SCSI_REQ_STATUS_RETRY_WRITE 0x02
49 #define SCSI_REQ_STATUS_RETRY_FLUSH 0x04
51 typedef struct SCSIDiskState SCSIDiskState
;
53 typedef struct SCSIDiskReq
{
55 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
57 uint32_t sector_count
;
69 /* The qemu block layer uses a fixed 512 byte sector size.
70 This is the number of 512 byte blocks in a single scsi sector. */
81 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
82 static int scsi_disk_emulate_command(SCSIDiskReq
*r
);
84 static void scsi_free_request(SCSIRequest
*req
)
86 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
88 if (r
->iov
.iov_base
) {
89 qemu_vfree(r
->iov
.iov_base
);
93 /* Helper function for command completion with sense. */
94 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
96 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
97 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
98 scsi_req_build_sense(&r
->req
, sense
);
99 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
102 /* Cancel a pending data transfer. */
103 static void scsi_cancel_io(SCSIRequest
*req
)
105 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
107 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
109 bdrv_aio_cancel(r
->req
.aiocb
);
114 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
)
116 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
118 if (!r
->iov
.iov_base
) {
119 r
->buflen
= SCSI_DMA_BUF_SIZE
;
120 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, r
->buflen
);
122 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
123 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
124 return r
->qiov
.size
/ 512;
127 static void scsi_read_complete(void * opaque
, int ret
)
129 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
130 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
133 if (r
->req
.aiocb
!= NULL
) {
135 bdrv_acct_done(s
->bs
, &r
->acct
);
139 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
144 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
146 n
= r
->qiov
.size
/ 512;
148 r
->sector_count
-= n
;
149 scsi_req_data(&r
->req
, r
->qiov
.size
);
152 static void scsi_flush_complete(void * opaque
, int ret
)
154 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
155 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
157 if (r
->req
.aiocb
!= NULL
) {
159 bdrv_acct_done(s
->bs
, &r
->acct
);
163 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
168 scsi_req_complete(&r
->req
, GOOD
);
171 /* Read more data from scsi device into buffer. */
172 static void scsi_read_data(SCSIRequest
*req
)
174 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
175 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
178 if (r
->sector_count
== (uint32_t)-1) {
179 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
181 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
184 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
185 if (r
->sector_count
== 0) {
186 /* This also clears the sense buffer for REQUEST SENSE. */
187 scsi_req_complete(&r
->req
, GOOD
);
191 /* No data transfer may already be in progress */
192 assert(r
->req
.aiocb
== NULL
);
194 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
195 DPRINTF("Data transfer direction invalid\n");
196 scsi_read_complete(r
, -EINVAL
);
201 scsi_read_complete(r
, -ENOMEDIUM
);
203 n
= scsi_init_iovec(r
);
204 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
205 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
206 scsi_read_complete
, r
);
207 if (r
->req
.aiocb
== NULL
) {
208 scsi_read_complete(r
, -EIO
);
212 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
214 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
215 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
216 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
218 if (action
== BLOCK_ERR_IGNORE
) {
219 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
223 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
224 || action
== BLOCK_ERR_STOP_ANY
) {
226 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
227 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
229 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
230 vm_stop(RUN_STATE_IO_ERROR
);
231 bdrv_iostatus_set_err(s
->bs
, error
);
235 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
238 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
241 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
244 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
249 static void scsi_write_complete(void * opaque
, int ret
)
251 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
252 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
255 if (r
->req
.aiocb
!= NULL
) {
257 bdrv_acct_done(s
->bs
, &r
->acct
);
261 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
266 n
= r
->qiov
.size
/ 512;
268 r
->sector_count
-= n
;
269 if (r
->sector_count
== 0) {
270 scsi_req_complete(&r
->req
, GOOD
);
273 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, r
->qiov
.size
);
274 scsi_req_data(&r
->req
, r
->qiov
.size
);
278 static void scsi_write_data(SCSIRequest
*req
)
280 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
281 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
284 /* No data transfer may already be in progress */
285 assert(r
->req
.aiocb
== NULL
);
287 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
288 DPRINTF("Data transfer direction invalid\n");
289 scsi_write_complete(r
, -EINVAL
);
293 n
= r
->qiov
.size
/ 512;
296 scsi_write_complete(r
, -ENOMEDIUM
);
298 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
299 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
300 scsi_write_complete
, r
);
301 if (r
->req
.aiocb
== NULL
) {
302 scsi_write_complete(r
, -ENOMEM
);
305 /* Called for the first time. Ask the driver to send us more data. */
306 scsi_write_complete(r
, 0);
310 static void scsi_dma_restart_bh(void *opaque
)
312 SCSIDiskState
*s
= opaque
;
316 qemu_bh_delete(s
->bh
);
319 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
320 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
321 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
322 int status
= r
->status
;
326 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
328 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
329 case SCSI_REQ_STATUS_RETRY_READ
:
330 scsi_read_data(&r
->req
);
332 case SCSI_REQ_STATUS_RETRY_WRITE
:
333 scsi_write_data(&r
->req
);
335 case SCSI_REQ_STATUS_RETRY_FLUSH
:
336 ret
= scsi_disk_emulate_command(r
);
338 scsi_req_complete(&r
->req
, GOOD
);
345 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
347 SCSIDiskState
*s
= opaque
;
353 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
354 qemu_bh_schedule(s
->bh
);
358 /* Return a pointer to the data buffer. */
359 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
361 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
363 return (uint8_t *)r
->iov
.iov_base
;
366 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
368 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
371 if (req
->cmd
.buf
[1] & 0x2) {
372 /* Command support data - optional, not implemented */
373 BADF("optional INQUIRY command support request not implemented\n");
377 if (req
->cmd
.buf
[1] & 0x1) {
378 /* Vital product data */
379 uint8_t page_code
= req
->cmd
.buf
[2];
380 if (req
->cmd
.xfer
< 4) {
381 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
382 "less than 4\n", page_code
, req
->cmd
.xfer
);
386 if (s
->qdev
.type
== TYPE_ROM
) {
387 outbuf
[buflen
++] = 5;
389 outbuf
[buflen
++] = 0;
391 outbuf
[buflen
++] = page_code
; // this page
392 outbuf
[buflen
++] = 0x00;
395 case 0x00: /* Supported page codes, mandatory */
398 DPRINTF("Inquiry EVPD[Supported pages] "
399 "buffer size %zd\n", req
->cmd
.xfer
);
401 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
403 outbuf
[buflen
++] = 0x80; // unit serial number
404 outbuf
[buflen
++] = 0x83; // device identification
405 if (s
->qdev
.type
== TYPE_DISK
) {
406 outbuf
[buflen
++] = 0xb0; // block limits
407 outbuf
[buflen
++] = 0xb2; // thin provisioning
409 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
412 case 0x80: /* Device serial number, optional */
417 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
421 l
= strlen(s
->serial
);
422 if (l
> req
->cmd
.xfer
)
427 DPRINTF("Inquiry EVPD[Serial number] "
428 "buffer size %zd\n", req
->cmd
.xfer
);
429 outbuf
[buflen
++] = l
;
430 memcpy(outbuf
+buflen
, s
->serial
, l
);
435 case 0x83: /* Device identification page, mandatory */
437 int max_len
= 255 - 8;
438 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
440 if (id_len
> max_len
)
442 DPRINTF("Inquiry EVPD[Device identification] "
443 "buffer size %zd\n", req
->cmd
.xfer
);
445 outbuf
[buflen
++] = 4 + id_len
;
446 outbuf
[buflen
++] = 0x2; // ASCII
447 outbuf
[buflen
++] = 0; // not officially assigned
448 outbuf
[buflen
++] = 0; // reserved
449 outbuf
[buflen
++] = id_len
; // length of data following
451 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
455 case 0xb0: /* block limits */
457 unsigned int unmap_sectors
=
458 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
459 unsigned int min_io_size
=
460 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
461 unsigned int opt_io_size
=
462 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
464 if (s
->qdev
.type
== TYPE_ROM
) {
465 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
469 /* required VPD size with unmap support */
470 outbuf
[3] = buflen
= 0x3c;
472 memset(outbuf
+ 4, 0, buflen
- 4);
474 /* optimal transfer length granularity */
475 outbuf
[6] = (min_io_size
>> 8) & 0xff;
476 outbuf
[7] = min_io_size
& 0xff;
478 /* optimal transfer length */
479 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
480 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
481 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
482 outbuf
[15] = opt_io_size
& 0xff;
484 /* optimal unmap granularity */
485 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
486 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
487 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
488 outbuf
[31] = unmap_sectors
& 0xff;
491 case 0xb2: /* thin provisioning */
493 outbuf
[3] = buflen
= 8;
495 outbuf
[5] = 0x40; /* write same with unmap supported */
501 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
502 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
509 /* Standard INQUIRY data */
510 if (req
->cmd
.buf
[2] != 0) {
511 BADF("Error: Inquiry (STANDARD) page or code "
512 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
517 if (req
->cmd
.xfer
< 5) {
518 BADF("Error: Inquiry (STANDARD) buffer size %zd "
519 "is less than 5\n", req
->cmd
.xfer
);
523 buflen
= req
->cmd
.xfer
;
524 if (buflen
> SCSI_MAX_INQUIRY_LEN
)
525 buflen
= SCSI_MAX_INQUIRY_LEN
;
527 memset(outbuf
, 0, buflen
);
529 outbuf
[0] = s
->qdev
.type
& 0x1f;
530 if (s
->qdev
.type
== TYPE_ROM
) {
532 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
534 outbuf
[1] = s
->removable
? 0x80 : 0;
535 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
537 memcpy(&outbuf
[8], "QEMU ", 8);
538 memset(&outbuf
[32], 0, 4);
539 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
541 * We claim conformance to SPC-3, which is required for guests
542 * to ask for modern features like READ CAPACITY(16) or the
543 * block characteristics VPD page by default. Not all of SPC-3
544 * is actually implemented, but we're good enough.
547 outbuf
[3] = 2; /* Format 2 */
550 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
552 /* If the allocation length of CDB is too small,
553 the additional length is not adjusted */
557 /* Sync data transfer and TCQ. */
558 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
562 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
565 BlockDriverState
*bdrv
= s
->bs
;
566 int cylinders
, heads
, secs
;
567 uint8_t *p
= *p_outbuf
;
570 * If Changeable Values are requested, a mask denoting those mode parameters
571 * that are changeable shall be returned. As we currently don't support
572 * parameter changes via MODE_SELECT all bits are returned set to zero.
573 * The buffer was already menset to zero by the caller of this function.
576 case 4: /* Rigid disk device geometry page. */
577 if (s
->qdev
.type
== TYPE_ROM
) {
582 if (page_control
== 1) { /* Changeable Values */
585 /* if a geometry hint is available, use it */
586 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
587 p
[2] = (cylinders
>> 16) & 0xff;
588 p
[3] = (cylinders
>> 8) & 0xff;
589 p
[4] = cylinders
& 0xff;
591 /* Write precomp start cylinder, disabled */
592 p
[6] = (cylinders
>> 16) & 0xff;
593 p
[7] = (cylinders
>> 8) & 0xff;
594 p
[8] = cylinders
& 0xff;
595 /* Reduced current start cylinder, disabled */
596 p
[9] = (cylinders
>> 16) & 0xff;
597 p
[10] = (cylinders
>> 8) & 0xff;
598 p
[11] = cylinders
& 0xff;
599 /* Device step rate [ns], 200ns */
602 /* Landing zone cylinder */
606 /* Medium rotation rate [rpm], 5400 rpm */
607 p
[20] = (5400 >> 8) & 0xff;
611 case 5: /* Flexible disk device geometry page. */
612 if (s
->qdev
.type
== TYPE_ROM
) {
617 if (page_control
== 1) { /* Changeable Values */
620 /* Transfer rate [kbit/s], 5Mbit/s */
623 /* if a geometry hint is available, use it */
624 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
627 p
[6] = s
->cluster_size
* 2;
628 p
[8] = (cylinders
>> 8) & 0xff;
629 p
[9] = cylinders
& 0xff;
630 /* Write precomp start cylinder, disabled */
631 p
[10] = (cylinders
>> 8) & 0xff;
632 p
[11] = cylinders
& 0xff;
633 /* Reduced current start cylinder, disabled */
634 p
[12] = (cylinders
>> 8) & 0xff;
635 p
[13] = cylinders
& 0xff;
636 /* Device step rate [100us], 100us */
639 /* Device step pulse width [us], 1us */
641 /* Device head settle delay [100us], 100us */
644 /* Motor on delay [0.1s], 0.1s */
646 /* Motor off delay [0.1s], 0.1s */
648 /* Medium rotation rate [rpm], 5400 rpm */
649 p
[28] = (5400 >> 8) & 0xff;
653 case 8: /* Caching page. */
656 if (page_control
== 1) { /* Changeable Values */
659 if (bdrv_enable_write_cache(s
->bs
)) {
664 case 0x2a: /* CD Capabilities and Mechanical Status page. */
665 if (s
->qdev
.type
!= TYPE_ROM
) {
670 if (page_control
== 1) { /* Changeable Values */
673 p
[2] = 3; // CD-R & CD-RW read
674 p
[3] = 0; // Writing not supported
675 p
[4] = 0x7f; /* Audio, composite, digital out,
676 mode 2 form 1&2, multi session */
677 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
678 RW corrected, C2 errors, ISRC,
680 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
681 /* Locking supported, jumper present, eject, tray */
682 p
[7] = 0; /* no volume & mute control, no
684 p
[8] = (50 * 176) >> 8; // 50x read speed
685 p
[9] = (50 * 176) & 0xff;
686 p
[10] = 0 >> 8; // No volume
688 p
[12] = 2048 >> 8; // 2M buffer
690 p
[14] = (16 * 176) >> 8; // 16x read speed current
691 p
[15] = (16 * 176) & 0xff;
692 p
[18] = (16 * 176) >> 8; // 16x write speed
693 p
[19] = (16 * 176) & 0xff;
694 p
[20] = (16 * 176) >> 8; // 16x write speed current
695 p
[21] = (16 * 176) & 0xff;
702 *p_outbuf
+= p
[1] + 2;
706 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
708 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
710 int page
, dbd
, buflen
, ret
, page_control
;
712 uint8_t dev_specific_param
;
714 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
715 page
= r
->req
.cmd
.buf
[2] & 0x3f;
716 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
717 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
718 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
719 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
722 if (bdrv_is_read_only(s
->bs
)) {
723 dev_specific_param
= 0x80; /* Readonly. */
725 dev_specific_param
= 0x00;
728 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
729 p
[1] = 0; /* Default media type. */
730 p
[2] = dev_specific_param
;
731 p
[3] = 0; /* Block descriptor length. */
733 } else { /* MODE_SENSE_10 */
734 p
[2] = 0; /* Default media type. */
735 p
[3] = dev_specific_param
;
736 p
[6] = p
[7] = 0; /* Block descriptor length. */
740 bdrv_get_geometry(s
->bs
, &nb_sectors
);
741 if (!dbd
&& nb_sectors
) {
742 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
743 outbuf
[3] = 8; /* Block descriptor length */
744 } else { /* MODE_SENSE_10 */
745 outbuf
[7] = 8; /* Block descriptor length */
747 nb_sectors
/= s
->cluster_size
;
748 if (nb_sectors
> 0xffffff)
750 p
[0] = 0; /* media density code */
751 p
[1] = (nb_sectors
>> 16) & 0xff;
752 p
[2] = (nb_sectors
>> 8) & 0xff;
753 p
[3] = nb_sectors
& 0xff;
754 p
[4] = 0; /* reserved */
755 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
756 p
[6] = s
->cluster_size
* 2;
761 if (page_control
== 3) {
763 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
768 for (page
= 0; page
<= 0x3e; page
++) {
769 mode_sense_page(s
, page
, &p
, page_control
);
772 ret
= mode_sense_page(s
, page
, &p
, page_control
);
780 * The mode data length field specifies the length in bytes of the
781 * following data that is available to be transferred. The mode data
782 * length does not include itself.
784 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
785 outbuf
[0] = buflen
- 1;
786 } else { /* MODE_SENSE_10 */
787 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
788 outbuf
[1] = (buflen
- 2) & 0xff;
790 if (buflen
> r
->req
.cmd
.xfer
)
791 buflen
= r
->req
.cmd
.xfer
;
795 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
797 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
798 int start_track
, format
, msf
, toclen
;
801 msf
= req
->cmd
.buf
[1] & 2;
802 format
= req
->cmd
.buf
[2] & 0xf;
803 start_track
= req
->cmd
.buf
[6];
804 bdrv_get_geometry(s
->bs
, &nb_sectors
);
805 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
806 nb_sectors
/= s
->cluster_size
;
809 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
812 /* multi session : only a single session defined */
814 memset(outbuf
, 0, 12);
820 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
825 if (toclen
> req
->cmd
.xfer
)
826 toclen
= req
->cmd
.xfer
;
830 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
832 SCSIRequest
*req
= &r
->req
;
833 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
834 bool start
= req
->cmd
.buf
[4] & 1;
835 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
837 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
838 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
839 scsi_check_condition(r
,
840 bdrv_is_inserted(s
->bs
)
841 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
842 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
845 bdrv_eject(s
->bs
, !start
);
846 s
->tray_open
= !start
;
851 static int scsi_disk_emulate_command(SCSIDiskReq
*r
)
853 SCSIRequest
*req
= &r
->req
;
854 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
859 if (!r
->iov
.iov_base
) {
861 * FIXME: we shouldn't return anything bigger than 4k, but the code
862 * requires the buffer to be as big as req->cmd.xfer in several
863 * places. So, do not allow CDBs with a very large ALLOCATION
864 * LENGTH. The real fix would be to modify scsi_read_data and
865 * dma_buf_read, so that they return data beyond the buflen
868 if (req
->cmd
.xfer
> 65536) {
869 goto illegal_request
;
871 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
872 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, r
->buflen
);
875 outbuf
= r
->iov
.iov_base
;
876 switch (req
->cmd
.buf
[0]) {
877 case TEST_UNIT_READY
:
878 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
))
882 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
884 goto illegal_request
;
888 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
890 goto illegal_request
;
893 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
895 goto illegal_request
;
898 if (req
->cmd
.buf
[1] & 1)
899 goto illegal_request
;
902 if (req
->cmd
.buf
[1] & 3)
903 goto illegal_request
;
906 if (req
->cmd
.buf
[1] & 1)
907 goto illegal_request
;
910 if (req
->cmd
.buf
[1] & 3)
911 goto illegal_request
;
914 if (scsi_disk_emulate_start_stop(r
) < 0) {
918 case ALLOW_MEDIUM_REMOVAL
:
919 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
920 bdrv_lock_medium(s
->bs
, req
->cmd
.buf
[4] & 1);
922 case READ_CAPACITY_10
:
923 /* The normal LEN field for this command is zero. */
924 memset(outbuf
, 0, 8);
925 bdrv_get_geometry(s
->bs
, &nb_sectors
);
928 nb_sectors
/= s
->cluster_size
;
929 /* Returned value is the address of the last sector. */
931 /* Remember the new size for read/write sanity checking. */
932 s
->max_lba
= nb_sectors
;
933 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
934 if (nb_sectors
> UINT32_MAX
)
935 nb_sectors
= UINT32_MAX
;
936 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
937 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
938 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
939 outbuf
[3] = nb_sectors
& 0xff;
942 outbuf
[6] = s
->cluster_size
* 2;
946 case GET_CONFIGURATION
:
947 memset(outbuf
, 0, 8);
948 /* ??? This should probably return much more information. For now
949 just return the basic header indicating the CD-ROM profile. */
950 outbuf
[7] = 8; // CD-ROM
953 case SERVICE_ACTION_IN_16
:
954 /* Service Action In subcommands. */
955 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
956 DPRINTF("SAI READ CAPACITY(16)\n");
957 memset(outbuf
, 0, req
->cmd
.xfer
);
958 bdrv_get_geometry(s
->bs
, &nb_sectors
);
961 nb_sectors
/= s
->cluster_size
;
962 /* Returned value is the address of the last sector. */
964 /* Remember the new size for read/write sanity checking. */
965 s
->max_lba
= nb_sectors
;
966 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
967 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
968 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
969 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
970 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
971 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
972 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
973 outbuf
[7] = nb_sectors
& 0xff;
976 outbuf
[10] = s
->cluster_size
* 2;
979 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
981 /* set TPE bit if the format supports discard */
982 if (s
->qdev
.conf
.discard_granularity
) {
986 /* Protection, exponent and lowest lba field left blank. */
987 buflen
= req
->cmd
.xfer
;
990 DPRINTF("Unsupported Service Action In\n");
991 goto illegal_request
;
995 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1001 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
)) {
1002 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1004 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1009 if (r
->req
.status
== -1) {
1010 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1015 /* Execute a scsi command. Returns the length of the data expected by the
1016 command. This will be Positive for data transfers from the device
1017 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1018 and zero if the command does not transfer any data. */
1020 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1022 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1023 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1029 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1034 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1035 printf(" 0x%02x", buf
[i
]);
1042 case TEST_UNIT_READY
:
1051 case ALLOW_MEDIUM_REMOVAL
:
1052 case READ_CAPACITY_10
:
1054 case GET_CONFIGURATION
:
1055 case SERVICE_ACTION_IN_16
:
1057 rc
= scsi_disk_emulate_command(r
);
1062 r
->iov
.iov_len
= rc
;
1064 case SYNCHRONIZE_CACHE
:
1065 bdrv_acct_start(s
->bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1066 r
->req
.aiocb
= bdrv_aio_flush(s
->bs
, scsi_flush_complete
, r
);
1067 if (r
->req
.aiocb
== NULL
) {
1068 scsi_flush_complete(r
, -EIO
);
1075 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1076 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1077 if (r
->req
.cmd
.lba
> s
->max_lba
)
1079 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1080 r
->sector_count
= len
* s
->cluster_size
;
1086 case WRITE_VERIFY_10
:
1087 case WRITE_VERIFY_12
:
1088 case WRITE_VERIFY_16
:
1089 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1090 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1091 (command
& 0xe) == 0xe ? "And Verify " : "",
1092 r
->req
.cmd
.lba
, len
);
1093 if (r
->req
.cmd
.lba
> s
->max_lba
)
1095 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1096 r
->sector_count
= len
* s
->cluster_size
;
1099 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1100 /* We don't support mode parameter changes.
1101 Allow the mode parameter header + block descriptors only. */
1102 if (r
->req
.cmd
.xfer
> 12) {
1106 case MODE_SELECT_10
:
1107 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1108 /* We don't support mode parameter changes.
1109 Allow the mode parameter header + block descriptors only. */
1110 if (r
->req
.cmd
.xfer
> 16) {
1116 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1118 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1123 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1125 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1126 r
->req
.cmd
.lba
, len
);
1128 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1133 * We only support WRITE SAME with the unmap bit set for now.
1135 if (!(buf
[1] & 0x8)) {
1139 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1140 len
* s
->cluster_size
);
1142 /* XXX: better error code ?*/
1150 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1151 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1154 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1157 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1160 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1161 scsi_req_complete(&r
->req
, GOOD
);
1163 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1164 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1167 if (!r
->sector_count
)
1168 r
->sector_count
= -1;
1173 static void scsi_disk_reset(DeviceState
*dev
)
1175 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1176 uint64_t nb_sectors
;
1178 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1180 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1181 nb_sectors
/= s
->cluster_size
;
1185 s
->max_lba
= nb_sectors
;
1188 static void scsi_destroy(SCSIDevice
*dev
)
1190 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1192 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1193 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1196 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1198 ((SCSIDiskState
*)opaque
)->tray_open
= !load
;
1201 static bool scsi_cd_is_tray_open(void *opaque
)
1203 return ((SCSIDiskState
*)opaque
)->tray_open
;
1206 static bool scsi_cd_is_medium_locked(void *opaque
)
1208 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1211 static const BlockDevOps scsi_cd_block_ops
= {
1212 .change_media_cb
= scsi_cd_change_media_cb
,
1213 .is_tray_open
= scsi_cd_is_tray_open
,
1214 .is_medium_locked
= scsi_cd_is_medium_locked
,
1217 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1219 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1222 if (!s
->qdev
.conf
.bs
) {
1223 error_report("scsi-disk: drive property not set");
1226 s
->bs
= s
->qdev
.conf
.bs
;
1228 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1229 error_report("Device needs media, but drive is empty");
1234 /* try to fall back to value set with legacy -drive serial=... */
1235 dinfo
= drive_get_by_blockdev(s
->bs
);
1236 if (*dinfo
->serial
) {
1237 s
->serial
= g_strdup(dinfo
->serial
);
1242 s
->version
= g_strdup(QEMU_VERSION
);
1245 if (bdrv_is_sg(s
->bs
)) {
1246 error_report("scsi-disk: unwanted /dev/sg*");
1250 if (scsi_type
== TYPE_ROM
) {
1251 bdrv_set_dev_ops(s
->bs
, &scsi_cd_block_ops
, s
);
1252 s
->qdev
.blocksize
= 2048;
1253 } else if (scsi_type
== TYPE_DISK
) {
1254 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1256 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1259 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1260 bdrv_set_buffer_alignment(s
->bs
, s
->qdev
.blocksize
);
1262 s
->qdev
.type
= scsi_type
;
1263 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1264 bdrv_iostatus_enable(s
->bs
);
1265 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1269 static int scsi_hd_initfn(SCSIDevice
*dev
)
1271 return scsi_initfn(dev
, TYPE_DISK
);
1274 static int scsi_cd_initfn(SCSIDevice
*dev
)
1276 return scsi_initfn(dev
, TYPE_ROM
);
1279 static int scsi_disk_initfn(SCSIDevice
*dev
)
1284 if (!dev
->conf
.bs
) {
1285 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1287 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1288 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1291 return scsi_initfn(dev
, scsi_type
);
1294 static SCSIReqOps scsi_disk_reqops
= {
1295 .size
= sizeof(SCSIDiskReq
),
1296 .free_req
= scsi_free_request
,
1297 .send_command
= scsi_send_command
,
1298 .read_data
= scsi_read_data
,
1299 .write_data
= scsi_write_data
,
1300 .cancel_io
= scsi_cancel_io
,
1301 .get_buf
= scsi_get_buf
,
1304 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1305 uint32_t lun
, void *hba_private
)
1307 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1310 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1314 #define DEFINE_SCSI_DISK_PROPERTIES() \
1315 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1316 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1317 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1319 static SCSIDeviceInfo scsi_disk_info
[] = {
1321 .qdev
.name
= "scsi-hd",
1322 .qdev
.fw_name
= "disk",
1323 .qdev
.desc
= "virtual SCSI disk",
1324 .qdev
.size
= sizeof(SCSIDiskState
),
1325 .qdev
.reset
= scsi_disk_reset
,
1326 .init
= scsi_hd_initfn
,
1327 .destroy
= scsi_destroy
,
1328 .alloc_req
= scsi_new_request
,
1329 .qdev
.props
= (Property
[]) {
1330 DEFINE_SCSI_DISK_PROPERTIES(),
1331 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1332 DEFINE_PROP_END_OF_LIST(),
1335 .qdev
.name
= "scsi-cd",
1336 .qdev
.fw_name
= "disk",
1337 .qdev
.desc
= "virtual SCSI CD-ROM",
1338 .qdev
.size
= sizeof(SCSIDiskState
),
1339 .qdev
.reset
= scsi_disk_reset
,
1340 .init
= scsi_cd_initfn
,
1341 .destroy
= scsi_destroy
,
1342 .alloc_req
= scsi_new_request
,
1343 .qdev
.props
= (Property
[]) {
1344 DEFINE_SCSI_DISK_PROPERTIES(),
1345 DEFINE_PROP_END_OF_LIST(),
1348 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1349 .qdev
.fw_name
= "disk",
1350 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1351 .qdev
.size
= sizeof(SCSIDiskState
),
1352 .qdev
.reset
= scsi_disk_reset
,
1353 .init
= scsi_disk_initfn
,
1354 .destroy
= scsi_destroy
,
1355 .alloc_req
= scsi_new_request
,
1356 .qdev
.props
= (Property
[]) {
1357 DEFINE_SCSI_DISK_PROPERTIES(),
1358 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1359 DEFINE_PROP_END_OF_LIST(),
1364 static void scsi_disk_register_devices(void)
1368 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1369 scsi_qdev_register(&scsi_disk_info
[i
]);
1372 device_init(scsi_disk_register_devices
)