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"
46 #define SCSI_DMA_BUF_SIZE 131072
47 #define SCSI_MAX_INQUIRY_LEN 256
49 typedef struct SCSIDiskState SCSIDiskState
;
51 typedef struct SCSIDiskReq
{
53 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
55 uint32_t sector_count
;
75 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
77 static void scsi_free_request(SCSIRequest
*req
)
79 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
81 if (r
->iov
.iov_base
) {
82 qemu_vfree(r
->iov
.iov_base
);
86 /* Helper function for command completion with sense. */
87 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
89 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
90 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
91 scsi_req_build_sense(&r
->req
, sense
);
92 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
95 /* Cancel a pending data transfer. */
96 static void scsi_cancel_io(SCSIRequest
*req
)
98 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
100 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
102 bdrv_aio_cancel(r
->req
.aiocb
);
104 /* This reference was left in by scsi_*_data. We take ownership of
105 * it the moment scsi_req_cancel is called, independent of whether
106 * bdrv_aio_cancel completes the request or not. */
107 scsi_req_unref(&r
->req
);
112 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
)
114 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
116 if (!r
->iov
.iov_base
) {
117 r
->buflen
= SCSI_DMA_BUF_SIZE
;
118 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
120 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
121 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
122 return r
->qiov
.size
/ 512;
125 static void scsi_read_complete(void * opaque
, int ret
)
127 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
128 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
131 if (r
->req
.aiocb
!= NULL
) {
133 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
137 if (scsi_handle_rw_error(r
, -ret
)) {
142 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
144 n
= r
->qiov
.size
/ 512;
146 r
->sector_count
-= n
;
147 scsi_req_data(&r
->req
, r
->qiov
.size
);
150 if (!r
->req
.io_canceled
) {
151 scsi_req_unref(&r
->req
);
155 static void scsi_flush_complete(void * opaque
, int ret
)
157 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
158 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
160 if (r
->req
.aiocb
!= NULL
) {
162 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
166 if (scsi_handle_rw_error(r
, -ret
)) {
171 scsi_req_complete(&r
->req
, GOOD
);
174 if (!r
->req
.io_canceled
) {
175 scsi_req_unref(&r
->req
);
179 /* Read more data from scsi device into buffer. */
180 static void scsi_read_data(SCSIRequest
*req
)
182 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
183 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
186 if (r
->sector_count
== (uint32_t)-1) {
187 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
189 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
192 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
193 if (r
->sector_count
== 0) {
194 /* This also clears the sense buffer for REQUEST SENSE. */
195 scsi_req_complete(&r
->req
, GOOD
);
199 /* No data transfer may already be in progress */
200 assert(r
->req
.aiocb
== NULL
);
202 /* The request is used as the AIO opaque value, so add a ref. */
203 scsi_req_ref(&r
->req
);
204 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
205 DPRINTF("Data transfer direction invalid\n");
206 scsi_read_complete(r
, -EINVAL
);
211 scsi_read_complete(r
, -ENOMEDIUM
);
215 n
= scsi_init_iovec(r
);
216 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
217 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
218 scsi_read_complete
, r
);
219 if (r
->req
.aiocb
== NULL
) {
220 scsi_read_complete(r
, -EIO
);
225 * scsi_handle_rw_error has two return values. 0 means that the error
226 * must be ignored, 1 means that the error has been processed and the
227 * caller should not do anything else for this request. Note that
228 * scsi_handle_rw_error always manages its reference counts, independent
229 * of the return value.
231 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
233 int is_read
= (r
->req
.cmd
.xfer
== SCSI_XFER_FROM_DEV
);
234 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
235 BlockErrorAction action
= bdrv_get_on_error(s
->qdev
.conf
.bs
, is_read
);
237 if (action
== BLOCK_ERR_IGNORE
) {
238 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_IGNORE
, is_read
);
242 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
243 || action
== BLOCK_ERR_STOP_ANY
) {
245 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_STOP
, is_read
);
246 vm_stop(RUN_STATE_IO_ERROR
);
247 bdrv_iostatus_set_err(s
->qdev
.conf
.bs
, error
);
248 scsi_req_retry(&r
->req
);
252 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
255 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
258 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
261 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
264 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_REPORT
, is_read
);
269 static void scsi_write_complete(void * opaque
, int ret
)
271 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
272 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
275 if (r
->req
.aiocb
!= NULL
) {
277 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
281 if (scsi_handle_rw_error(r
, -ret
)) {
286 n
= r
->qiov
.size
/ 512;
288 r
->sector_count
-= n
;
289 if (r
->sector_count
== 0) {
290 scsi_req_complete(&r
->req
, GOOD
);
293 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, r
->qiov
.size
);
294 scsi_req_data(&r
->req
, r
->qiov
.size
);
298 if (!r
->req
.io_canceled
) {
299 scsi_req_unref(&r
->req
);
303 static void scsi_write_data(SCSIRequest
*req
)
305 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
306 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
309 /* No data transfer may already be in progress */
310 assert(r
->req
.aiocb
== NULL
);
312 /* The request is used as the AIO opaque value, so add a ref. */
313 scsi_req_ref(&r
->req
);
314 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
315 DPRINTF("Data transfer direction invalid\n");
316 scsi_write_complete(r
, -EINVAL
);
320 n
= r
->qiov
.size
/ 512;
323 scsi_write_complete(r
, -ENOMEDIUM
);
326 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
327 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
328 scsi_write_complete
, r
);
329 if (r
->req
.aiocb
== NULL
) {
330 scsi_write_complete(r
, -ENOMEM
);
333 /* Called for the first time. Ask the driver to send us more data. */
334 scsi_write_complete(r
, 0);
338 /* Return a pointer to the data buffer. */
339 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
341 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
343 return (uint8_t *)r
->iov
.iov_base
;
346 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
348 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
351 if (req
->cmd
.buf
[1] & 0x2) {
352 /* Command support data - optional, not implemented */
353 BADF("optional INQUIRY command support request not implemented\n");
357 if (req
->cmd
.buf
[1] & 0x1) {
358 /* Vital product data */
359 uint8_t page_code
= req
->cmd
.buf
[2];
360 if (req
->cmd
.xfer
< 4) {
361 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
362 "less than 4\n", page_code
, req
->cmd
.xfer
);
366 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
367 outbuf
[buflen
++] = page_code
; // this page
368 outbuf
[buflen
++] = 0x00;
371 case 0x00: /* Supported page codes, mandatory */
374 DPRINTF("Inquiry EVPD[Supported pages] "
375 "buffer size %zd\n", req
->cmd
.xfer
);
377 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
379 outbuf
[buflen
++] = 0x80; // unit serial number
381 outbuf
[buflen
++] = 0x83; // device identification
382 if (s
->qdev
.type
== TYPE_DISK
) {
383 outbuf
[buflen
++] = 0xb0; // block limits
384 outbuf
[buflen
++] = 0xb2; // thin provisioning
386 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
389 case 0x80: /* Device serial number, optional */
394 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
398 l
= strlen(s
->serial
);
399 if (l
> req
->cmd
.xfer
) {
406 DPRINTF("Inquiry EVPD[Serial number] "
407 "buffer size %zd\n", req
->cmd
.xfer
);
408 outbuf
[buflen
++] = l
;
409 memcpy(outbuf
+buflen
, s
->serial
, l
);
414 case 0x83: /* Device identification page, mandatory */
416 int max_len
= 255 - 8;
417 int id_len
= strlen(bdrv_get_device_name(s
->qdev
.conf
.bs
));
419 if (id_len
> max_len
) {
422 DPRINTF("Inquiry EVPD[Device identification] "
423 "buffer size %zd\n", req
->cmd
.xfer
);
425 outbuf
[buflen
++] = 4 + id_len
;
426 outbuf
[buflen
++] = 0x2; // ASCII
427 outbuf
[buflen
++] = 0; // not officially assigned
428 outbuf
[buflen
++] = 0; // reserved
429 outbuf
[buflen
++] = id_len
; // length of data following
431 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->qdev
.conf
.bs
), id_len
);
435 case 0xb0: /* block limits */
437 unsigned int unmap_sectors
=
438 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
439 unsigned int min_io_size
=
440 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
441 unsigned int opt_io_size
=
442 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
444 if (s
->qdev
.type
== TYPE_ROM
) {
445 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
449 /* required VPD size with unmap support */
450 outbuf
[3] = buflen
= 0x3c;
452 memset(outbuf
+ 4, 0, buflen
- 4);
454 /* optimal transfer length granularity */
455 outbuf
[6] = (min_io_size
>> 8) & 0xff;
456 outbuf
[7] = min_io_size
& 0xff;
458 /* optimal transfer length */
459 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
460 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
461 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
462 outbuf
[15] = opt_io_size
& 0xff;
464 /* optimal unmap granularity */
465 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
466 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
467 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
468 outbuf
[31] = unmap_sectors
& 0xff;
471 case 0xb2: /* thin provisioning */
473 outbuf
[3] = buflen
= 8;
475 outbuf
[5] = 0x40; /* write same with unmap supported */
481 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
482 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
489 /* Standard INQUIRY data */
490 if (req
->cmd
.buf
[2] != 0) {
491 BADF("Error: Inquiry (STANDARD) page or code "
492 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
497 if (req
->cmd
.xfer
< 5) {
498 BADF("Error: Inquiry (STANDARD) buffer size %zd "
499 "is less than 5\n", req
->cmd
.xfer
);
503 buflen
= req
->cmd
.xfer
;
504 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
505 buflen
= SCSI_MAX_INQUIRY_LEN
;
507 memset(outbuf
, 0, buflen
);
509 outbuf
[0] = s
->qdev
.type
& 0x1f;
510 outbuf
[1] = s
->removable
? 0x80 : 0;
511 if (s
->qdev
.type
== TYPE_ROM
) {
512 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
514 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
516 memcpy(&outbuf
[8], "QEMU ", 8);
517 memset(&outbuf
[32], 0, 4);
518 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
520 * We claim conformance to SPC-3, which is required for guests
521 * to ask for modern features like READ CAPACITY(16) or the
522 * block characteristics VPD page by default. Not all of SPC-3
523 * is actually implemented, but we're good enough.
526 outbuf
[3] = 2; /* Format 2 */
529 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
531 /* If the allocation length of CDB is too small,
532 the additional length is not adjusted */
536 /* Sync data transfer and TCQ. */
537 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
541 static inline bool media_is_dvd(SCSIDiskState
*s
)
544 if (s
->qdev
.type
!= TYPE_ROM
) {
547 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
550 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
551 return nb_sectors
> CD_MAX_SECTORS
;
554 static inline bool media_is_cd(SCSIDiskState
*s
)
557 if (s
->qdev
.type
!= TYPE_ROM
) {
560 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
563 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
564 return nb_sectors
<= CD_MAX_SECTORS
;
567 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
570 static const int rds_caps_size
[5] = {
577 uint8_t media
= r
->req
.cmd
.buf
[1];
578 uint8_t layer
= r
->req
.cmd
.buf
[6];
579 uint8_t format
= r
->req
.cmd
.buf
[7];
582 if (s
->qdev
.type
!= TYPE_ROM
) {
586 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
590 if (format
!= 0xff) {
591 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
592 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
595 if (media_is_cd(s
)) {
596 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
599 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
602 size
= rds_caps_size
[format
];
603 memset(outbuf
, 0, size
);
608 /* Physical format information */
613 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
615 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
616 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
617 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
618 outbuf
[7] = 0; /* default densities */
620 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
621 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
625 case 0x01: /* DVD copyright information, all zeros */
628 case 0x03: /* BCA information - invalid field for no BCA info */
631 case 0x04: /* DVD disc manufacturing information, all zeros */
634 case 0xff: { /* List capabilities */
637 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
638 if (!rds_caps_size
[i
]) {
642 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
643 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
653 /* Size of buffer, not including 2 byte size field */
654 stw_be_p(outbuf
, size
- 2);
661 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
663 uint8_t event_code
, media_status
;
667 media_status
= MS_TRAY_OPEN
;
668 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
669 media_status
= MS_MEDIA_PRESENT
;
672 /* Event notification descriptor */
673 event_code
= MEC_NO_CHANGE
;
674 if (media_status
!= MS_TRAY_OPEN
&& s
->media_event
) {
675 event_code
= MEC_NEW_MEDIA
;
676 s
->media_event
= false;
679 outbuf
[0] = event_code
;
680 outbuf
[1] = media_status
;
682 /* These fields are reserved, just clear them. */
688 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
692 uint8_t *buf
= r
->req
.cmd
.buf
;
693 uint8_t notification_class_request
= buf
[4];
694 if (s
->qdev
.type
!= TYPE_ROM
) {
697 if ((buf
[1] & 1) == 0) {
703 outbuf
[0] = outbuf
[1] = 0;
704 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
705 if (notification_class_request
& (1 << GESN_MEDIA
)) {
706 outbuf
[2] = GESN_MEDIA
;
707 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
711 stw_be_p(outbuf
, size
- 4);
715 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
719 if (s
->qdev
.type
!= TYPE_ROM
) {
722 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
723 memset(outbuf
, 0, 40);
724 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
725 stw_be_p(&outbuf
[6], current
);
726 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
727 outbuf
[10] = 0x03; /* persistent, current */
728 outbuf
[11] = 8; /* two profiles */
729 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
730 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
731 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
732 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
733 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
734 stw_be_p(&outbuf
[20], 1);
735 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
737 stl_be_p(&outbuf
[24], 1); /* SCSI */
738 outbuf
[28] = 1; /* DBE = 1, mandatory */
739 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
740 stw_be_p(&outbuf
[32], 3);
741 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
743 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
744 /* TODO: Random readable, CD read, DVD read, drive serial number,
749 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
751 if (s
->qdev
.type
!= TYPE_ROM
) {
754 memset(outbuf
, 0, 8);
755 outbuf
[5] = 1; /* CD-ROM */
759 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
762 static const int mode_sense_valid
[0x3f] = {
763 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
764 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
765 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
766 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
767 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
768 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
771 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
772 int cylinders
, heads
, secs
;
773 uint8_t *p
= *p_outbuf
;
775 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
782 * If Changeable Values are requested, a mask denoting those mode parameters
783 * that are changeable shall be returned. As we currently don't support
784 * parameter changes via MODE_SELECT all bits are returned set to zero.
785 * The buffer was already menset to zero by the caller of this function.
788 case MODE_PAGE_HD_GEOMETRY
:
790 if (page_control
== 1) { /* Changeable Values */
793 /* if a geometry hint is available, use it */
794 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
795 p
[2] = (cylinders
>> 16) & 0xff;
796 p
[3] = (cylinders
>> 8) & 0xff;
797 p
[4] = cylinders
& 0xff;
799 /* Write precomp start cylinder, disabled */
800 p
[6] = (cylinders
>> 16) & 0xff;
801 p
[7] = (cylinders
>> 8) & 0xff;
802 p
[8] = cylinders
& 0xff;
803 /* Reduced current start cylinder, disabled */
804 p
[9] = (cylinders
>> 16) & 0xff;
805 p
[10] = (cylinders
>> 8) & 0xff;
806 p
[11] = cylinders
& 0xff;
807 /* Device step rate [ns], 200ns */
810 /* Landing zone cylinder */
814 /* Medium rotation rate [rpm], 5400 rpm */
815 p
[20] = (5400 >> 8) & 0xff;
819 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
821 if (page_control
== 1) { /* Changeable Values */
824 /* Transfer rate [kbit/s], 5Mbit/s */
827 /* if a geometry hint is available, use it */
828 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
831 p
[6] = s
->qdev
.blocksize
>> 8;
832 p
[8] = (cylinders
>> 8) & 0xff;
833 p
[9] = cylinders
& 0xff;
834 /* Write precomp start cylinder, disabled */
835 p
[10] = (cylinders
>> 8) & 0xff;
836 p
[11] = cylinders
& 0xff;
837 /* Reduced current start cylinder, disabled */
838 p
[12] = (cylinders
>> 8) & 0xff;
839 p
[13] = cylinders
& 0xff;
840 /* Device step rate [100us], 100us */
843 /* Device step pulse width [us], 1us */
845 /* Device head settle delay [100us], 100us */
848 /* Motor on delay [0.1s], 0.1s */
850 /* Motor off delay [0.1s], 0.1s */
852 /* Medium rotation rate [rpm], 5400 rpm */
853 p
[28] = (5400 >> 8) & 0xff;
857 case MODE_PAGE_CACHING
:
860 if (page_control
== 1) { /* Changeable Values */
863 if (bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
868 case MODE_PAGE_R_W_ERROR
:
870 p
[2] = 0x80; /* Automatic Write Reallocation Enabled */
871 if (s
->qdev
.type
== TYPE_ROM
) {
872 p
[3] = 0x20; /* Read Retry Count */
876 case MODE_PAGE_AUDIO_CTL
:
880 case MODE_PAGE_CAPABILITIES
:
882 if (page_control
== 1) { /* Changeable Values */
886 p
[2] = 0x3b; /* CD-R & CD-RW read */
887 p
[3] = 0; /* Writing not supported */
888 p
[4] = 0x7f; /* Audio, composite, digital out,
889 mode 2 form 1&2, multi session */
890 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
891 RW corrected, C2 errors, ISRC,
893 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
894 /* Locking supported, jumper present, eject, tray */
895 p
[7] = 0; /* no volume & mute control, no
897 p
[8] = (50 * 176) >> 8; /* 50x read speed */
898 p
[9] = (50 * 176) & 0xff;
899 p
[10] = 2 >> 8; /* Two volume levels */
901 p
[12] = 2048 >> 8; /* 2M buffer */
903 p
[14] = (16 * 176) >> 8; /* 16x read speed current */
904 p
[15] = (16 * 176) & 0xff;
905 p
[18] = (16 * 176) >> 8; /* 16x write speed */
906 p
[19] = (16 * 176) & 0xff;
907 p
[20] = (16 * 176) >> 8; /* 16x write speed current */
908 p
[21] = (16 * 176) & 0xff;
915 *p_outbuf
+= p
[1] + 2;
919 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
921 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
923 int page
, dbd
, buflen
, ret
, page_control
;
925 uint8_t dev_specific_param
;
927 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
928 page
= r
->req
.cmd
.buf
[2] & 0x3f;
929 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
930 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
931 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
932 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
935 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
936 dev_specific_param
= 0x80; /* Readonly. */
938 dev_specific_param
= 0x00;
941 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
942 p
[1] = 0; /* Default media type. */
943 p
[2] = dev_specific_param
;
944 p
[3] = 0; /* Block descriptor length. */
946 } else { /* MODE_SENSE_10 */
947 p
[2] = 0; /* Default media type. */
948 p
[3] = dev_specific_param
;
949 p
[6] = p
[7] = 0; /* Block descriptor length. */
953 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
954 if (!dbd
&& nb_sectors
) {
955 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
956 outbuf
[3] = 8; /* Block descriptor length */
957 } else { /* MODE_SENSE_10 */
958 outbuf
[7] = 8; /* Block descriptor length */
960 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
961 if (nb_sectors
> 0xffffff) {
964 p
[0] = 0; /* media density code */
965 p
[1] = (nb_sectors
>> 16) & 0xff;
966 p
[2] = (nb_sectors
>> 8) & 0xff;
967 p
[3] = nb_sectors
& 0xff;
968 p
[4] = 0; /* reserved */
969 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
970 p
[6] = s
->qdev
.blocksize
>> 8;
975 if (page_control
== 3) {
977 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
982 for (page
= 0; page
<= 0x3e; page
++) {
983 mode_sense_page(s
, page
, &p
, page_control
);
986 ret
= mode_sense_page(s
, page
, &p
, page_control
);
994 * The mode data length field specifies the length in bytes of the
995 * following data that is available to be transferred. The mode data
996 * length does not include itself.
998 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
999 outbuf
[0] = buflen
- 1;
1000 } else { /* MODE_SENSE_10 */
1001 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1002 outbuf
[1] = (buflen
- 2) & 0xff;
1004 if (buflen
> r
->req
.cmd
.xfer
) {
1005 buflen
= r
->req
.cmd
.xfer
;
1010 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1012 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1013 int start_track
, format
, msf
, toclen
;
1014 uint64_t nb_sectors
;
1016 msf
= req
->cmd
.buf
[1] & 2;
1017 format
= req
->cmd
.buf
[2] & 0xf;
1018 start_track
= req
->cmd
.buf
[6];
1019 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1020 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1021 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1024 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1027 /* multi session : only a single session defined */
1029 memset(outbuf
, 0, 12);
1035 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1040 if (toclen
> req
->cmd
.xfer
) {
1041 toclen
= req
->cmd
.xfer
;
1046 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1048 SCSIRequest
*req
= &r
->req
;
1049 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1050 bool start
= req
->cmd
.buf
[4] & 1;
1051 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1053 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
1054 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1055 scsi_check_condition(r
,
1056 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1057 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1058 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1061 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1062 s
->tray_open
= !start
;
1067 static int scsi_disk_emulate_command(SCSIDiskReq
*r
)
1069 SCSIRequest
*req
= &r
->req
;
1070 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1071 uint64_t nb_sectors
;
1075 if (!r
->iov
.iov_base
) {
1077 * FIXME: we shouldn't return anything bigger than 4k, but the code
1078 * requires the buffer to be as big as req->cmd.xfer in several
1079 * places. So, do not allow CDBs with a very large ALLOCATION
1080 * LENGTH. The real fix would be to modify scsi_read_data and
1081 * dma_buf_read, so that they return data beyond the buflen
1084 if (req
->cmd
.xfer
> 65536) {
1085 goto illegal_request
;
1087 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1088 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1091 outbuf
= r
->iov
.iov_base
;
1092 switch (req
->cmd
.buf
[0]) {
1093 case TEST_UNIT_READY
:
1094 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1099 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1101 goto illegal_request
;
1106 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1108 goto illegal_request
;
1112 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1114 goto illegal_request
;
1118 if (req
->cmd
.buf
[1] & 1) {
1119 goto illegal_request
;
1123 if (req
->cmd
.buf
[1] & 3) {
1124 goto illegal_request
;
1128 if (req
->cmd
.buf
[1] & 1) {
1129 goto illegal_request
;
1133 if (req
->cmd
.buf
[1] & 3) {
1134 goto illegal_request
;
1138 if (scsi_disk_emulate_start_stop(r
) < 0) {
1142 case ALLOW_MEDIUM_REMOVAL
:
1143 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1144 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1146 case READ_CAPACITY_10
:
1147 /* The normal LEN field for this command is zero. */
1148 memset(outbuf
, 0, 8);
1149 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1153 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1154 goto illegal_request
;
1156 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1157 /* Returned value is the address of the last sector. */
1159 /* Remember the new size for read/write sanity checking. */
1160 s
->qdev
.max_lba
= nb_sectors
;
1161 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1162 if (nb_sectors
> UINT32_MAX
) {
1163 nb_sectors
= UINT32_MAX
;
1165 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1166 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1167 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1168 outbuf
[3] = nb_sectors
& 0xff;
1171 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1175 case MECHANISM_STATUS
:
1176 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1178 goto illegal_request
;
1181 case GET_CONFIGURATION
:
1182 buflen
= scsi_get_configuration(s
, outbuf
);
1184 goto illegal_request
;
1187 case GET_EVENT_STATUS_NOTIFICATION
:
1188 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1190 goto illegal_request
;
1193 case READ_DVD_STRUCTURE
:
1194 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1196 goto illegal_request
;
1199 case SERVICE_ACTION_IN_16
:
1200 /* Service Action In subcommands. */
1201 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1202 DPRINTF("SAI READ CAPACITY(16)\n");
1203 memset(outbuf
, 0, req
->cmd
.xfer
);
1204 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1208 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1209 goto illegal_request
;
1211 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1212 /* Returned value is the address of the last sector. */
1214 /* Remember the new size for read/write sanity checking. */
1215 s
->qdev
.max_lba
= nb_sectors
;
1216 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1217 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1218 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1219 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1220 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1221 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1222 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1223 outbuf
[7] = nb_sectors
& 0xff;
1226 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1229 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1231 /* set TPE bit if the format supports discard */
1232 if (s
->qdev
.conf
.discard_granularity
) {
1236 /* Protection, exponent and lowest lba field left blank. */
1237 buflen
= req
->cmd
.xfer
;
1240 DPRINTF("Unsupported Service Action In\n");
1241 goto illegal_request
;
1245 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1251 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1252 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1254 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1259 if (r
->req
.status
== -1) {
1260 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1265 /* Execute a scsi command. Returns the length of the data expected by the
1266 command. This will be Positive for data transfers from the device
1267 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1268 and zero if the command does not transfer any data. */
1270 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1272 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1273 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1279 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1284 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1285 printf(" 0x%02x", buf
[i
]);
1292 case TEST_UNIT_READY
:
1301 case ALLOW_MEDIUM_REMOVAL
:
1302 case READ_CAPACITY_10
:
1304 case READ_DVD_STRUCTURE
:
1305 case GET_CONFIGURATION
:
1306 case GET_EVENT_STATUS_NOTIFICATION
:
1307 case MECHANISM_STATUS
:
1308 case SERVICE_ACTION_IN_16
:
1310 rc
= scsi_disk_emulate_command(r
);
1315 r
->iov
.iov_len
= rc
;
1317 case SYNCHRONIZE_CACHE
:
1318 /* The request is used as the AIO opaque value, so add a ref. */
1319 scsi_req_ref(&r
->req
);
1320 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1321 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_flush_complete
, r
);
1322 if (r
->req
.aiocb
== NULL
) {
1323 scsi_flush_complete(r
, -EIO
);
1330 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1331 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1332 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1335 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1336 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1342 case WRITE_VERIFY_10
:
1343 case WRITE_VERIFY_12
:
1344 case WRITE_VERIFY_16
:
1345 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1346 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1347 (command
& 0xe) == 0xe ? "And Verify " : "",
1348 r
->req
.cmd
.lba
, len
);
1349 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1352 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1353 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1356 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1357 /* We don't support mode parameter changes.
1358 Allow the mode parameter header + block descriptors only. */
1359 if (r
->req
.cmd
.xfer
> 12) {
1363 case MODE_SELECT_10
:
1364 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1365 /* We don't support mode parameter changes.
1366 Allow the mode parameter header + block descriptors only. */
1367 if (r
->req
.cmd
.xfer
> 16) {
1373 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1375 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1380 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1382 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1383 r
->req
.cmd
.lba
, len
);
1385 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1390 * We only support WRITE SAME with the unmap bit set for now.
1392 if (!(buf
[1] & 0x8)) {
1396 rc
= bdrv_discard(s
->qdev
.conf
.bs
,
1397 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1398 len
* (s
->qdev
.blocksize
/ 512));
1400 /* XXX: better error code ?*/
1408 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1409 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1412 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1415 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1418 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1419 scsi_req_complete(&r
->req
, GOOD
);
1421 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1422 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1425 if (!r
->sector_count
) {
1426 r
->sector_count
= -1;
1432 static void scsi_disk_reset(DeviceState
*dev
)
1434 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1435 uint64_t nb_sectors
;
1437 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1439 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1440 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1444 s
->qdev
.max_lba
= nb_sectors
;
1447 static void scsi_destroy(SCSIDevice
*dev
)
1449 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1451 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1452 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1455 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1457 SCSIDiskState
*s
= opaque
;
1460 * When a CD gets changed, we have to report an ejected state and
1461 * then a loaded state to guests so that they detect tray
1462 * open/close and media change events. Guests that do not use
1463 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1464 * states rely on this behavior.
1466 * media_changed governs the state machine used for unit attention
1467 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1469 s
->media_changed
= load
;
1470 s
->tray_open
= !load
;
1471 s
->qdev
.unit_attention
= SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
);
1472 s
->media_event
= true;
1475 static bool scsi_cd_is_tray_open(void *opaque
)
1477 return ((SCSIDiskState
*)opaque
)->tray_open
;
1480 static bool scsi_cd_is_medium_locked(void *opaque
)
1482 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1485 static const BlockDevOps scsi_cd_block_ops
= {
1486 .change_media_cb
= scsi_cd_change_media_cb
,
1487 .is_tray_open
= scsi_cd_is_tray_open
,
1488 .is_medium_locked
= scsi_cd_is_medium_locked
,
1491 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
1493 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1494 if (s
->media_changed
) {
1495 s
->media_changed
= false;
1496 s
->qdev
.unit_attention
= SENSE_CODE(MEDIUM_CHANGED
);
1500 static int scsi_initfn(SCSIDevice
*dev
)
1502 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1505 if (!s
->qdev
.conf
.bs
) {
1506 error_report("scsi-disk: drive property not set");
1510 if (!s
->removable
&& !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1511 error_report("Device needs media, but drive is empty");
1516 /* try to fall back to value set with legacy -drive serial=... */
1517 dinfo
= drive_get_by_blockdev(s
->qdev
.conf
.bs
);
1518 if (*dinfo
->serial
) {
1519 s
->serial
= g_strdup(dinfo
->serial
);
1524 s
->version
= g_strdup(QEMU_VERSION
);
1527 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
1528 error_report("scsi-disk: unwanted /dev/sg*");
1533 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_cd_block_ops
, s
);
1535 bdrv_set_buffer_alignment(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
1537 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
1538 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1542 static int scsi_hd_initfn(SCSIDevice
*dev
)
1544 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1545 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1546 s
->qdev
.type
= TYPE_DISK
;
1547 return scsi_initfn(&s
->qdev
);
1550 static int scsi_cd_initfn(SCSIDevice
*dev
)
1552 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1553 s
->qdev
.blocksize
= 2048;
1554 s
->qdev
.type
= TYPE_ROM
;
1555 s
->removable
= true;
1556 return scsi_initfn(&s
->qdev
);
1559 static int scsi_disk_initfn(SCSIDevice
*dev
)
1563 if (!dev
->conf
.bs
) {
1564 return scsi_initfn(dev
); /* ... and die there */
1567 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1568 if (dinfo
->media_cd
) {
1569 return scsi_cd_initfn(dev
);
1571 return scsi_hd_initfn(dev
);
1575 static const SCSIReqOps scsi_disk_reqops
= {
1576 .size
= sizeof(SCSIDiskReq
),
1577 .free_req
= scsi_free_request
,
1578 .send_command
= scsi_send_command
,
1579 .read_data
= scsi_read_data
,
1580 .write_data
= scsi_write_data
,
1581 .cancel_io
= scsi_cancel_io
,
1582 .get_buf
= scsi_get_buf
,
1585 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
1586 uint8_t *buf
, void *hba_private
)
1588 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1591 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1596 static int get_device_type(SCSIDiskState
*s
)
1598 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
1601 uint8_t sensebuf
[8];
1602 sg_io_hdr_t io_header
;
1605 memset(cmd
, 0, sizeof(cmd
));
1606 memset(buf
, 0, sizeof(buf
));
1608 cmd
[4] = sizeof(buf
);
1610 memset(&io_header
, 0, sizeof(io_header
));
1611 io_header
.interface_id
= 'S';
1612 io_header
.dxfer_direction
= SG_DXFER_FROM_DEV
;
1613 io_header
.dxfer_len
= sizeof(buf
);
1614 io_header
.dxferp
= buf
;
1615 io_header
.cmdp
= cmd
;
1616 io_header
.cmd_len
= sizeof(cmd
);
1617 io_header
.mx_sb_len
= sizeof(sensebuf
);
1618 io_header
.sbp
= sensebuf
;
1619 io_header
.timeout
= 6000; /* XXX */
1621 ret
= bdrv_ioctl(bdrv
, SG_IO
, &io_header
);
1622 if (ret
< 0 || io_header
.driver_status
|| io_header
.host_status
) {
1625 s
->qdev
.type
= buf
[0];
1626 s
->removable
= (buf
[1] & 0x80) != 0;
1630 static int scsi_block_initfn(SCSIDevice
*dev
)
1632 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1636 if (!s
->qdev
.conf
.bs
) {
1637 error_report("scsi-block: drive property not set");
1641 /* check we are using a driver managing SG_IO (version 3 and after) */
1642 if (bdrv_ioctl(s
->qdev
.conf
.bs
, SG_GET_VERSION_NUM
, &sg_version
) < 0 ||
1643 sg_version
< 30000) {
1644 error_report("scsi-block: scsi generic interface too old");
1648 /* get device type from INQUIRY data */
1649 rc
= get_device_type(s
);
1651 error_report("scsi-block: INQUIRY failed");
1655 /* Make a guess for the block size, we'll fix it when the guest sends.
1656 * READ CAPACITY. If they don't, they likely would assume these sizes
1657 * anyway. (TODO: check in /sys).
1659 if (s
->qdev
.type
== TYPE_ROM
|| s
->qdev
.type
== TYPE_WORM
) {
1660 s
->qdev
.blocksize
= 2048;
1662 s
->qdev
.blocksize
= 512;
1664 return scsi_initfn(&s
->qdev
);
1667 static SCSIRequest
*scsi_block_new_request(SCSIDevice
*d
, uint32_t tag
,
1668 uint32_t lun
, uint8_t *buf
,
1671 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1682 case WRITE_VERIFY_10
:
1683 case WRITE_VERIFY_12
:
1684 case WRITE_VERIFY_16
:
1685 return scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
,
1689 return scsi_req_alloc(&scsi_generic_req_ops
, &s
->qdev
, tag
, lun
,
1694 #define DEFINE_SCSI_DISK_PROPERTIES() \
1695 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1696 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1697 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1699 static SCSIDeviceInfo scsi_disk_info
[] = {
1701 .qdev
.name
= "scsi-hd",
1702 .qdev
.fw_name
= "disk",
1703 .qdev
.desc
= "virtual SCSI disk",
1704 .qdev
.size
= sizeof(SCSIDiskState
),
1705 .qdev
.reset
= scsi_disk_reset
,
1706 .init
= scsi_hd_initfn
,
1707 .destroy
= scsi_destroy
,
1708 .alloc_req
= scsi_new_request
,
1709 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1710 .qdev
.props
= (Property
[]) {
1711 DEFINE_SCSI_DISK_PROPERTIES(),
1712 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1713 DEFINE_PROP_END_OF_LIST(),
1716 .qdev
.name
= "scsi-cd",
1717 .qdev
.fw_name
= "disk",
1718 .qdev
.desc
= "virtual SCSI CD-ROM",
1719 .qdev
.size
= sizeof(SCSIDiskState
),
1720 .qdev
.reset
= scsi_disk_reset
,
1721 .init
= scsi_cd_initfn
,
1722 .destroy
= scsi_destroy
,
1723 .alloc_req
= scsi_new_request
,
1724 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1725 .qdev
.props
= (Property
[]) {
1726 DEFINE_SCSI_DISK_PROPERTIES(),
1727 DEFINE_PROP_END_OF_LIST(),
1731 .qdev
.name
= "scsi-block",
1732 .qdev
.fw_name
= "disk",
1733 .qdev
.desc
= "SCSI block device passthrough",
1734 .qdev
.size
= sizeof(SCSIDiskState
),
1735 .qdev
.reset
= scsi_disk_reset
,
1736 .init
= scsi_block_initfn
,
1737 .destroy
= scsi_destroy
,
1738 .alloc_req
= scsi_block_new_request
,
1739 .qdev
.props
= (Property
[]) {
1740 DEFINE_SCSI_DISK_PROPERTIES(),
1741 DEFINE_PROP_END_OF_LIST(),
1745 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1746 .qdev
.fw_name
= "disk",
1747 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1748 .qdev
.size
= sizeof(SCSIDiskState
),
1749 .qdev
.reset
= scsi_disk_reset
,
1750 .init
= scsi_disk_initfn
,
1751 .destroy
= scsi_destroy
,
1752 .alloc_req
= scsi_new_request
,
1753 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1754 .qdev
.props
= (Property
[]) {
1755 DEFINE_SCSI_DISK_PROPERTIES(),
1756 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1757 DEFINE_PROP_END_OF_LIST(),
1762 static void scsi_disk_register_devices(void)
1766 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1767 scsi_qdev_register(&scsi_disk_info
[i
]);
1770 device_init(scsi_disk_register_devices
)