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 typedef struct SCSIDiskState SCSIDiskState
;
47 typedef struct SCSIDiskReq
{
49 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
51 uint32_t sector_count
;
71 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
);
73 static void scsi_free_request(SCSIRequest
*req
)
75 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
77 if (r
->iov
.iov_base
) {
78 qemu_vfree(r
->iov
.iov_base
);
82 /* Helper function for command completion with sense. */
83 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
85 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
86 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
87 scsi_req_build_sense(&r
->req
, sense
);
88 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
91 /* Cancel a pending data transfer. */
92 static void scsi_cancel_io(SCSIRequest
*req
)
94 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
96 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
98 bdrv_aio_cancel(r
->req
.aiocb
);
100 /* This reference was left in by scsi_*_data. We take ownership of
101 * it the moment scsi_req_cancel is called, independent of whether
102 * bdrv_aio_cancel completes the request or not. */
103 scsi_req_unref(&r
->req
);
108 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
)
110 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
112 if (!r
->iov
.iov_base
) {
113 r
->buflen
= SCSI_DMA_BUF_SIZE
;
114 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
116 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
117 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
118 return r
->qiov
.size
/ 512;
121 static void scsi_read_complete(void * opaque
, int ret
)
123 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
124 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
127 if (r
->req
.aiocb
!= NULL
) {
129 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
133 if (scsi_handle_rw_error(r
, -ret
)) {
138 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
140 n
= r
->qiov
.size
/ 512;
142 r
->sector_count
-= n
;
143 scsi_req_data(&r
->req
, r
->qiov
.size
);
146 if (!r
->req
.io_canceled
) {
147 scsi_req_unref(&r
->req
);
151 static void scsi_flush_complete(void * opaque
, int ret
)
153 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
154 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
156 if (r
->req
.aiocb
!= NULL
) {
158 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
162 if (scsi_handle_rw_error(r
, -ret
)) {
167 scsi_req_complete(&r
->req
, GOOD
);
170 if (!r
->req
.io_canceled
) {
171 scsi_req_unref(&r
->req
);
175 /* Read more data from scsi device into buffer. */
176 static void scsi_read_data(SCSIRequest
*req
)
178 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
179 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
182 if (r
->sector_count
== (uint32_t)-1) {
183 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
185 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
188 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
189 if (r
->sector_count
== 0) {
190 /* This also clears the sense buffer for REQUEST SENSE. */
191 scsi_req_complete(&r
->req
, GOOD
);
195 /* No data transfer may already be in progress */
196 assert(r
->req
.aiocb
== NULL
);
198 /* The request is used as the AIO opaque value, so add a ref. */
199 scsi_req_ref(&r
->req
);
200 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
201 DPRINTF("Data transfer direction invalid\n");
202 scsi_read_complete(r
, -EINVAL
);
207 scsi_read_complete(r
, -ENOMEDIUM
);
211 n
= scsi_init_iovec(r
);
212 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
213 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
214 scsi_read_complete
, r
);
215 if (r
->req
.aiocb
== NULL
) {
216 scsi_read_complete(r
, -EIO
);
221 * scsi_handle_rw_error has two return values. 0 means that the error
222 * must be ignored, 1 means that the error has been processed and the
223 * caller should not do anything else for this request. Note that
224 * scsi_handle_rw_error always manages its reference counts, independent
225 * of the return value.
227 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
)
229 int is_read
= (r
->req
.cmd
.xfer
== SCSI_XFER_FROM_DEV
);
230 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
231 BlockErrorAction action
= bdrv_get_on_error(s
->qdev
.conf
.bs
, is_read
);
233 if (action
== BLOCK_ERR_IGNORE
) {
234 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_IGNORE
, is_read
);
238 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
239 || action
== BLOCK_ERR_STOP_ANY
) {
241 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_STOP
, is_read
);
242 vm_stop(RUN_STATE_IO_ERROR
);
243 bdrv_iostatus_set_err(s
->qdev
.conf
.bs
, error
);
244 scsi_req_retry(&r
->req
);
248 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
251 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
254 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
257 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
260 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_REPORT
, is_read
);
265 static void scsi_write_complete(void * opaque
, int ret
)
267 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
268 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
271 if (r
->req
.aiocb
!= NULL
) {
273 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
277 if (scsi_handle_rw_error(r
, -ret
)) {
282 n
= r
->qiov
.size
/ 512;
284 r
->sector_count
-= n
;
285 if (r
->sector_count
== 0) {
286 scsi_req_complete(&r
->req
, GOOD
);
289 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, r
->qiov
.size
);
290 scsi_req_data(&r
->req
, r
->qiov
.size
);
294 if (!r
->req
.io_canceled
) {
295 scsi_req_unref(&r
->req
);
299 static void scsi_write_data(SCSIRequest
*req
)
301 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
302 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
305 /* No data transfer may already be in progress */
306 assert(r
->req
.aiocb
== NULL
);
308 /* The request is used as the AIO opaque value, so add a ref. */
309 scsi_req_ref(&r
->req
);
310 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
311 DPRINTF("Data transfer direction invalid\n");
312 scsi_write_complete(r
, -EINVAL
);
316 n
= r
->qiov
.size
/ 512;
319 scsi_write_complete(r
, -ENOMEDIUM
);
322 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
323 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
324 scsi_write_complete
, r
);
325 if (r
->req
.aiocb
== NULL
) {
326 scsi_write_complete(r
, -ENOMEM
);
329 /* Called for the first time. Ask the driver to send us more data. */
330 scsi_write_complete(r
, 0);
334 /* Return a pointer to the data buffer. */
335 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
337 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
339 return (uint8_t *)r
->iov
.iov_base
;
342 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
344 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
347 if (req
->cmd
.buf
[1] & 0x2) {
348 /* Command support data - optional, not implemented */
349 BADF("optional INQUIRY command support request not implemented\n");
353 if (req
->cmd
.buf
[1] & 0x1) {
354 /* Vital product data */
355 uint8_t page_code
= req
->cmd
.buf
[2];
356 if (req
->cmd
.xfer
< 4) {
357 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
358 "less than 4\n", page_code
, req
->cmd
.xfer
);
362 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
363 outbuf
[buflen
++] = page_code
; // this page
364 outbuf
[buflen
++] = 0x00;
367 case 0x00: /* Supported page codes, mandatory */
370 DPRINTF("Inquiry EVPD[Supported pages] "
371 "buffer size %zd\n", req
->cmd
.xfer
);
373 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
375 outbuf
[buflen
++] = 0x80; // unit serial number
377 outbuf
[buflen
++] = 0x83; // device identification
378 if (s
->qdev
.type
== TYPE_DISK
) {
379 outbuf
[buflen
++] = 0xb0; // block limits
380 outbuf
[buflen
++] = 0xb2; // thin provisioning
382 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
385 case 0x80: /* Device serial number, optional */
390 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
394 l
= strlen(s
->serial
);
395 if (l
> req
->cmd
.xfer
) {
402 DPRINTF("Inquiry EVPD[Serial number] "
403 "buffer size %zd\n", req
->cmd
.xfer
);
404 outbuf
[buflen
++] = l
;
405 memcpy(outbuf
+buflen
, s
->serial
, l
);
410 case 0x83: /* Device identification page, mandatory */
412 int max_len
= 255 - 8;
413 int id_len
= strlen(bdrv_get_device_name(s
->qdev
.conf
.bs
));
415 if (id_len
> max_len
) {
418 DPRINTF("Inquiry EVPD[Device identification] "
419 "buffer size %zd\n", req
->cmd
.xfer
);
421 outbuf
[buflen
++] = 4 + id_len
;
422 outbuf
[buflen
++] = 0x2; // ASCII
423 outbuf
[buflen
++] = 0; // not officially assigned
424 outbuf
[buflen
++] = 0; // reserved
425 outbuf
[buflen
++] = id_len
; // length of data following
427 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->qdev
.conf
.bs
), id_len
);
431 case 0xb0: /* block limits */
433 unsigned int unmap_sectors
=
434 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
435 unsigned int min_io_size
=
436 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
437 unsigned int opt_io_size
=
438 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
440 if (s
->qdev
.type
== TYPE_ROM
) {
441 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
445 /* required VPD size with unmap support */
446 outbuf
[3] = buflen
= 0x3c;
448 memset(outbuf
+ 4, 0, buflen
- 4);
450 /* optimal transfer length granularity */
451 outbuf
[6] = (min_io_size
>> 8) & 0xff;
452 outbuf
[7] = min_io_size
& 0xff;
454 /* optimal transfer length */
455 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
456 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
457 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
458 outbuf
[15] = opt_io_size
& 0xff;
460 /* optimal unmap granularity */
461 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
462 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
463 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
464 outbuf
[31] = unmap_sectors
& 0xff;
467 case 0xb2: /* thin provisioning */
469 outbuf
[3] = buflen
= 8;
471 outbuf
[5] = 0x40; /* write same with unmap supported */
477 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
478 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
485 /* Standard INQUIRY data */
486 if (req
->cmd
.buf
[2] != 0) {
487 BADF("Error: Inquiry (STANDARD) page or code "
488 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
493 if (req
->cmd
.xfer
< 5) {
494 BADF("Error: Inquiry (STANDARD) buffer size %zd "
495 "is less than 5\n", req
->cmd
.xfer
);
499 buflen
= req
->cmd
.xfer
;
500 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
501 buflen
= SCSI_MAX_INQUIRY_LEN
;
503 memset(outbuf
, 0, buflen
);
505 outbuf
[0] = s
->qdev
.type
& 0x1f;
506 outbuf
[1] = s
->removable
? 0x80 : 0;
507 if (s
->qdev
.type
== TYPE_ROM
) {
508 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
510 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
512 memcpy(&outbuf
[8], "QEMU ", 8);
513 memset(&outbuf
[32], 0, 4);
514 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
516 * We claim conformance to SPC-3, which is required for guests
517 * to ask for modern features like READ CAPACITY(16) or the
518 * block characteristics VPD page by default. Not all of SPC-3
519 * is actually implemented, but we're good enough.
522 outbuf
[3] = 2; /* Format 2 */
525 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
527 /* If the allocation length of CDB is too small,
528 the additional length is not adjusted */
532 /* Sync data transfer and TCQ. */
533 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
537 static inline bool media_is_dvd(SCSIDiskState
*s
)
540 if (s
->qdev
.type
!= TYPE_ROM
) {
543 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
546 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
547 return nb_sectors
> CD_MAX_SECTORS
;
550 static inline bool media_is_cd(SCSIDiskState
*s
)
553 if (s
->qdev
.type
!= TYPE_ROM
) {
556 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
559 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
560 return nb_sectors
<= CD_MAX_SECTORS
;
563 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
566 static const int rds_caps_size
[5] = {
573 uint8_t media
= r
->req
.cmd
.buf
[1];
574 uint8_t layer
= r
->req
.cmd
.buf
[6];
575 uint8_t format
= r
->req
.cmd
.buf
[7];
578 if (s
->qdev
.type
!= TYPE_ROM
) {
582 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
586 if (format
!= 0xff) {
587 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
588 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
591 if (media_is_cd(s
)) {
592 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
595 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
598 size
= rds_caps_size
[format
];
599 memset(outbuf
, 0, size
);
604 /* Physical format information */
609 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
611 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
612 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
613 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
614 outbuf
[7] = 0; /* default densities */
616 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
617 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
621 case 0x01: /* DVD copyright information, all zeros */
624 case 0x03: /* BCA information - invalid field for no BCA info */
627 case 0x04: /* DVD disc manufacturing information, all zeros */
630 case 0xff: { /* List capabilities */
633 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
634 if (!rds_caps_size
[i
]) {
638 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
639 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
649 /* Size of buffer, not including 2 byte size field */
650 stw_be_p(outbuf
, size
- 2);
657 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
659 uint8_t event_code
, media_status
;
663 media_status
= MS_TRAY_OPEN
;
664 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
665 media_status
= MS_MEDIA_PRESENT
;
668 /* Event notification descriptor */
669 event_code
= MEC_NO_CHANGE
;
670 if (media_status
!= MS_TRAY_OPEN
&& s
->media_event
) {
671 event_code
= MEC_NEW_MEDIA
;
672 s
->media_event
= false;
675 outbuf
[0] = event_code
;
676 outbuf
[1] = media_status
;
678 /* These fields are reserved, just clear them. */
684 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
688 uint8_t *buf
= r
->req
.cmd
.buf
;
689 uint8_t notification_class_request
= buf
[4];
690 if (s
->qdev
.type
!= TYPE_ROM
) {
693 if ((buf
[1] & 1) == 0) {
699 outbuf
[0] = outbuf
[1] = 0;
700 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
701 if (notification_class_request
& (1 << GESN_MEDIA
)) {
702 outbuf
[2] = GESN_MEDIA
;
703 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
707 stw_be_p(outbuf
, size
- 4);
711 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
715 if (s
->qdev
.type
!= TYPE_ROM
) {
718 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
719 memset(outbuf
, 0, 40);
720 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
721 stw_be_p(&outbuf
[6], current
);
722 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
723 outbuf
[10] = 0x03; /* persistent, current */
724 outbuf
[11] = 8; /* two profiles */
725 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
726 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
727 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
728 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
729 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
730 stw_be_p(&outbuf
[20], 1);
731 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
733 stl_be_p(&outbuf
[24], 1); /* SCSI */
734 outbuf
[28] = 1; /* DBE = 1, mandatory */
735 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
736 stw_be_p(&outbuf
[32], 3);
737 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
739 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
740 /* TODO: Random readable, CD read, DVD read, drive serial number,
745 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
747 if (s
->qdev
.type
!= TYPE_ROM
) {
750 memset(outbuf
, 0, 8);
751 outbuf
[5] = 1; /* CD-ROM */
755 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
758 static const int mode_sense_valid
[0x3f] = {
759 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
760 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
761 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
762 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
763 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
764 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
767 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
768 int cylinders
, heads
, secs
;
769 uint8_t *p
= *p_outbuf
;
771 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
778 * If Changeable Values are requested, a mask denoting those mode parameters
779 * that are changeable shall be returned. As we currently don't support
780 * parameter changes via MODE_SELECT all bits are returned set to zero.
781 * The buffer was already menset to zero by the caller of this function.
784 case MODE_PAGE_HD_GEOMETRY
:
786 if (page_control
== 1) { /* Changeable Values */
789 /* if a geometry hint is available, use it */
790 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
791 p
[2] = (cylinders
>> 16) & 0xff;
792 p
[3] = (cylinders
>> 8) & 0xff;
793 p
[4] = cylinders
& 0xff;
795 /* Write precomp start cylinder, disabled */
796 p
[6] = (cylinders
>> 16) & 0xff;
797 p
[7] = (cylinders
>> 8) & 0xff;
798 p
[8] = cylinders
& 0xff;
799 /* Reduced current start cylinder, disabled */
800 p
[9] = (cylinders
>> 16) & 0xff;
801 p
[10] = (cylinders
>> 8) & 0xff;
802 p
[11] = cylinders
& 0xff;
803 /* Device step rate [ns], 200ns */
806 /* Landing zone cylinder */
810 /* Medium rotation rate [rpm], 5400 rpm */
811 p
[20] = (5400 >> 8) & 0xff;
815 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
817 if (page_control
== 1) { /* Changeable Values */
820 /* Transfer rate [kbit/s], 5Mbit/s */
823 /* if a geometry hint is available, use it */
824 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
827 p
[6] = s
->qdev
.blocksize
>> 8;
828 p
[8] = (cylinders
>> 8) & 0xff;
829 p
[9] = cylinders
& 0xff;
830 /* Write precomp start cylinder, disabled */
831 p
[10] = (cylinders
>> 8) & 0xff;
832 p
[11] = cylinders
& 0xff;
833 /* Reduced current start cylinder, disabled */
834 p
[12] = (cylinders
>> 8) & 0xff;
835 p
[13] = cylinders
& 0xff;
836 /* Device step rate [100us], 100us */
839 /* Device step pulse width [us], 1us */
841 /* Device head settle delay [100us], 100us */
844 /* Motor on delay [0.1s], 0.1s */
846 /* Motor off delay [0.1s], 0.1s */
848 /* Medium rotation rate [rpm], 5400 rpm */
849 p
[28] = (5400 >> 8) & 0xff;
853 case MODE_PAGE_CACHING
:
856 if (page_control
== 1) { /* Changeable Values */
859 if (bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
864 case MODE_PAGE_R_W_ERROR
:
866 p
[2] = 0x80; /* Automatic Write Reallocation Enabled */
867 if (s
->qdev
.type
== TYPE_ROM
) {
868 p
[3] = 0x20; /* Read Retry Count */
872 case MODE_PAGE_AUDIO_CTL
:
876 case MODE_PAGE_CAPABILITIES
:
878 if (page_control
== 1) { /* Changeable Values */
882 p
[2] = 0x3b; /* CD-R & CD-RW read */
883 p
[3] = 0; /* Writing not supported */
884 p
[4] = 0x7f; /* Audio, composite, digital out,
885 mode 2 form 1&2, multi session */
886 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
887 RW corrected, C2 errors, ISRC,
889 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
890 /* Locking supported, jumper present, eject, tray */
891 p
[7] = 0; /* no volume & mute control, no
893 p
[8] = (50 * 176) >> 8; /* 50x read speed */
894 p
[9] = (50 * 176) & 0xff;
895 p
[10] = 2 >> 8; /* Two volume levels */
897 p
[12] = 2048 >> 8; /* 2M buffer */
899 p
[14] = (16 * 176) >> 8; /* 16x read speed current */
900 p
[15] = (16 * 176) & 0xff;
901 p
[18] = (16 * 176) >> 8; /* 16x write speed */
902 p
[19] = (16 * 176) & 0xff;
903 p
[20] = (16 * 176) >> 8; /* 16x write speed current */
904 p
[21] = (16 * 176) & 0xff;
911 *p_outbuf
+= p
[1] + 2;
915 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
917 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
919 int page
, dbd
, buflen
, ret
, page_control
;
921 uint8_t dev_specific_param
;
923 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
924 page
= r
->req
.cmd
.buf
[2] & 0x3f;
925 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
926 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
927 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
928 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
931 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
932 dev_specific_param
= 0x80; /* Readonly. */
934 dev_specific_param
= 0x00;
937 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
938 p
[1] = 0; /* Default media type. */
939 p
[2] = dev_specific_param
;
940 p
[3] = 0; /* Block descriptor length. */
942 } else { /* MODE_SENSE_10 */
943 p
[2] = 0; /* Default media type. */
944 p
[3] = dev_specific_param
;
945 p
[6] = p
[7] = 0; /* Block descriptor length. */
949 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
950 if (!dbd
&& nb_sectors
) {
951 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
952 outbuf
[3] = 8; /* Block descriptor length */
953 } else { /* MODE_SENSE_10 */
954 outbuf
[7] = 8; /* Block descriptor length */
956 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
957 if (nb_sectors
> 0xffffff) {
960 p
[0] = 0; /* media density code */
961 p
[1] = (nb_sectors
>> 16) & 0xff;
962 p
[2] = (nb_sectors
>> 8) & 0xff;
963 p
[3] = nb_sectors
& 0xff;
964 p
[4] = 0; /* reserved */
965 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
966 p
[6] = s
->qdev
.blocksize
>> 8;
971 if (page_control
== 3) {
973 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
978 for (page
= 0; page
<= 0x3e; page
++) {
979 mode_sense_page(s
, page
, &p
, page_control
);
982 ret
= mode_sense_page(s
, page
, &p
, page_control
);
990 * The mode data length field specifies the length in bytes of the
991 * following data that is available to be transferred. The mode data
992 * length does not include itself.
994 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
995 outbuf
[0] = buflen
- 1;
996 } else { /* MODE_SENSE_10 */
997 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
998 outbuf
[1] = (buflen
- 2) & 0xff;
1000 if (buflen
> r
->req
.cmd
.xfer
) {
1001 buflen
= r
->req
.cmd
.xfer
;
1006 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1008 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1009 int start_track
, format
, msf
, toclen
;
1010 uint64_t nb_sectors
;
1012 msf
= req
->cmd
.buf
[1] & 2;
1013 format
= req
->cmd
.buf
[2] & 0xf;
1014 start_track
= req
->cmd
.buf
[6];
1015 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1016 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1017 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1020 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1023 /* multi session : only a single session defined */
1025 memset(outbuf
, 0, 12);
1031 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1036 if (toclen
> req
->cmd
.xfer
) {
1037 toclen
= req
->cmd
.xfer
;
1042 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1044 SCSIRequest
*req
= &r
->req
;
1045 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1046 bool start
= req
->cmd
.buf
[4] & 1;
1047 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1049 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
1050 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1051 scsi_check_condition(r
,
1052 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1053 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1054 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1057 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1058 s
->tray_open
= !start
;
1063 static int scsi_disk_emulate_command(SCSIDiskReq
*r
)
1065 SCSIRequest
*req
= &r
->req
;
1066 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1067 uint64_t nb_sectors
;
1071 if (!r
->iov
.iov_base
) {
1073 * FIXME: we shouldn't return anything bigger than 4k, but the code
1074 * requires the buffer to be as big as req->cmd.xfer in several
1075 * places. So, do not allow CDBs with a very large ALLOCATION
1076 * LENGTH. The real fix would be to modify scsi_read_data and
1077 * dma_buf_read, so that they return data beyond the buflen
1080 if (req
->cmd
.xfer
> 65536) {
1081 goto illegal_request
;
1083 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1084 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1087 outbuf
= r
->iov
.iov_base
;
1088 switch (req
->cmd
.buf
[0]) {
1089 case TEST_UNIT_READY
:
1090 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1095 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1097 goto illegal_request
;
1102 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1104 goto illegal_request
;
1108 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1110 goto illegal_request
;
1114 if (req
->cmd
.buf
[1] & 1) {
1115 goto illegal_request
;
1119 if (req
->cmd
.buf
[1] & 3) {
1120 goto illegal_request
;
1124 if (req
->cmd
.buf
[1] & 1) {
1125 goto illegal_request
;
1129 if (req
->cmd
.buf
[1] & 3) {
1130 goto illegal_request
;
1134 if (scsi_disk_emulate_start_stop(r
) < 0) {
1138 case ALLOW_MEDIUM_REMOVAL
:
1139 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1140 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1142 case READ_CAPACITY_10
:
1143 /* The normal LEN field for this command is zero. */
1144 memset(outbuf
, 0, 8);
1145 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1149 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1150 goto illegal_request
;
1152 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1153 /* Returned value is the address of the last sector. */
1155 /* Remember the new size for read/write sanity checking. */
1156 s
->qdev
.max_lba
= nb_sectors
;
1157 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1158 if (nb_sectors
> UINT32_MAX
) {
1159 nb_sectors
= UINT32_MAX
;
1161 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1162 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1163 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1164 outbuf
[3] = nb_sectors
& 0xff;
1167 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1171 case MECHANISM_STATUS
:
1172 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1174 goto illegal_request
;
1177 case GET_CONFIGURATION
:
1178 buflen
= scsi_get_configuration(s
, outbuf
);
1180 goto illegal_request
;
1183 case GET_EVENT_STATUS_NOTIFICATION
:
1184 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1186 goto illegal_request
;
1189 case READ_DVD_STRUCTURE
:
1190 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1192 goto illegal_request
;
1195 case SERVICE_ACTION_IN_16
:
1196 /* Service Action In subcommands. */
1197 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1198 DPRINTF("SAI READ CAPACITY(16)\n");
1199 memset(outbuf
, 0, req
->cmd
.xfer
);
1200 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1204 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1205 goto illegal_request
;
1207 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1208 /* Returned value is the address of the last sector. */
1210 /* Remember the new size for read/write sanity checking. */
1211 s
->qdev
.max_lba
= nb_sectors
;
1212 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1213 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1214 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1215 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1216 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1217 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1218 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1219 outbuf
[7] = nb_sectors
& 0xff;
1222 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1225 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1227 /* set TPE bit if the format supports discard */
1228 if (s
->qdev
.conf
.discard_granularity
) {
1232 /* Protection, exponent and lowest lba field left blank. */
1233 buflen
= req
->cmd
.xfer
;
1236 DPRINTF("Unsupported Service Action In\n");
1237 goto illegal_request
;
1241 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1247 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1248 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1250 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1255 if (r
->req
.status
== -1) {
1256 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1261 /* Execute a scsi command. Returns the length of the data expected by the
1262 command. This will be Positive for data transfers from the device
1263 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1264 and zero if the command does not transfer any data. */
1266 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1268 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1269 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1275 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1280 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1281 printf(" 0x%02x", buf
[i
]);
1288 case TEST_UNIT_READY
:
1297 case ALLOW_MEDIUM_REMOVAL
:
1298 case READ_CAPACITY_10
:
1300 case READ_DVD_STRUCTURE
:
1301 case GET_CONFIGURATION
:
1302 case GET_EVENT_STATUS_NOTIFICATION
:
1303 case MECHANISM_STATUS
:
1304 case SERVICE_ACTION_IN_16
:
1306 rc
= scsi_disk_emulate_command(r
);
1311 r
->iov
.iov_len
= rc
;
1313 case SYNCHRONIZE_CACHE
:
1314 /* The request is used as the AIO opaque value, so add a ref. */
1315 scsi_req_ref(&r
->req
);
1316 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1317 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_flush_complete
, r
);
1318 if (r
->req
.aiocb
== NULL
) {
1319 scsi_flush_complete(r
, -EIO
);
1326 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1327 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1328 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1331 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1332 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1338 case WRITE_VERIFY_10
:
1339 case WRITE_VERIFY_12
:
1340 case WRITE_VERIFY_16
:
1341 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1342 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1343 (command
& 0xe) == 0xe ? "And Verify " : "",
1344 r
->req
.cmd
.lba
, len
);
1345 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1348 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1349 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1352 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1353 /* We don't support mode parameter changes.
1354 Allow the mode parameter header + block descriptors only. */
1355 if (r
->req
.cmd
.xfer
> 12) {
1359 case MODE_SELECT_10
:
1360 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1361 /* We don't support mode parameter changes.
1362 Allow the mode parameter header + block descriptors only. */
1363 if (r
->req
.cmd
.xfer
> 16) {
1369 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1371 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1376 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1378 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1379 r
->req
.cmd
.lba
, len
);
1381 if (r
->req
.cmd
.lba
> s
->qdev
.max_lba
) {
1386 * We only support WRITE SAME with the unmap bit set for now.
1388 if (!(buf
[1] & 0x8)) {
1392 rc
= bdrv_discard(s
->qdev
.conf
.bs
,
1393 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1394 len
* (s
->qdev
.blocksize
/ 512));
1396 /* XXX: better error code ?*/
1404 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1405 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1408 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1411 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1414 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1415 scsi_req_complete(&r
->req
, GOOD
);
1417 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1418 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1421 if (!r
->sector_count
) {
1422 r
->sector_count
= -1;
1428 static void scsi_disk_reset(DeviceState
*dev
)
1430 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1431 uint64_t nb_sectors
;
1433 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1435 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1436 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1440 s
->qdev
.max_lba
= nb_sectors
;
1443 static void scsi_destroy(SCSIDevice
*dev
)
1445 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1447 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1448 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1451 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1453 SCSIDiskState
*s
= opaque
;
1456 * When a CD gets changed, we have to report an ejected state and
1457 * then a loaded state to guests so that they detect tray
1458 * open/close and media change events. Guests that do not use
1459 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1460 * states rely on this behavior.
1462 * media_changed governs the state machine used for unit attention
1463 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1465 s
->media_changed
= load
;
1466 s
->tray_open
= !load
;
1467 s
->qdev
.unit_attention
= SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
);
1468 s
->media_event
= true;
1471 static bool scsi_cd_is_tray_open(void *opaque
)
1473 return ((SCSIDiskState
*)opaque
)->tray_open
;
1476 static bool scsi_cd_is_medium_locked(void *opaque
)
1478 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1481 static const BlockDevOps scsi_cd_block_ops
= {
1482 .change_media_cb
= scsi_cd_change_media_cb
,
1483 .is_tray_open
= scsi_cd_is_tray_open
,
1484 .is_medium_locked
= scsi_cd_is_medium_locked
,
1487 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
1489 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1490 if (s
->media_changed
) {
1491 s
->media_changed
= false;
1492 s
->qdev
.unit_attention
= SENSE_CODE(MEDIUM_CHANGED
);
1496 static int scsi_initfn(SCSIDevice
*dev
)
1498 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1501 if (!s
->qdev
.conf
.bs
) {
1502 error_report("scsi-disk: drive property not set");
1506 if (!s
->removable
&& !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1507 error_report("Device needs media, but drive is empty");
1512 /* try to fall back to value set with legacy -drive serial=... */
1513 dinfo
= drive_get_by_blockdev(s
->qdev
.conf
.bs
);
1514 if (*dinfo
->serial
) {
1515 s
->serial
= g_strdup(dinfo
->serial
);
1520 s
->version
= g_strdup(QEMU_VERSION
);
1523 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
1524 error_report("scsi-disk: unwanted /dev/sg*");
1529 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_cd_block_ops
, s
);
1531 bdrv_set_buffer_alignment(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
1533 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
1534 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1538 static int scsi_hd_initfn(SCSIDevice
*dev
)
1540 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1541 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1542 s
->qdev
.type
= TYPE_DISK
;
1543 return scsi_initfn(&s
->qdev
);
1546 static int scsi_cd_initfn(SCSIDevice
*dev
)
1548 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1549 s
->qdev
.blocksize
= 2048;
1550 s
->qdev
.type
= TYPE_ROM
;
1551 s
->removable
= true;
1552 return scsi_initfn(&s
->qdev
);
1555 static int scsi_disk_initfn(SCSIDevice
*dev
)
1559 if (!dev
->conf
.bs
) {
1560 return scsi_initfn(dev
); /* ... and die there */
1563 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1564 if (dinfo
->media_cd
) {
1565 return scsi_cd_initfn(dev
);
1567 return scsi_hd_initfn(dev
);
1571 static const SCSIReqOps scsi_disk_reqops
= {
1572 .size
= sizeof(SCSIDiskReq
),
1573 .free_req
= scsi_free_request
,
1574 .send_command
= scsi_send_command
,
1575 .read_data
= scsi_read_data
,
1576 .write_data
= scsi_write_data
,
1577 .cancel_io
= scsi_cancel_io
,
1578 .get_buf
= scsi_get_buf
,
1581 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
1582 uint8_t *buf
, void *hba_private
)
1584 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1587 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1591 #define DEFINE_SCSI_DISK_PROPERTIES() \
1592 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1593 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1594 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1596 static SCSIDeviceInfo scsi_disk_info
[] = {
1598 .qdev
.name
= "scsi-hd",
1599 .qdev
.fw_name
= "disk",
1600 .qdev
.desc
= "virtual SCSI disk",
1601 .qdev
.size
= sizeof(SCSIDiskState
),
1602 .qdev
.reset
= scsi_disk_reset
,
1603 .init
= scsi_hd_initfn
,
1604 .destroy
= scsi_destroy
,
1605 .alloc_req
= scsi_new_request
,
1606 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1607 .qdev
.props
= (Property
[]) {
1608 DEFINE_SCSI_DISK_PROPERTIES(),
1609 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1610 DEFINE_PROP_END_OF_LIST(),
1613 .qdev
.name
= "scsi-cd",
1614 .qdev
.fw_name
= "disk",
1615 .qdev
.desc
= "virtual SCSI CD-ROM",
1616 .qdev
.size
= sizeof(SCSIDiskState
),
1617 .qdev
.reset
= scsi_disk_reset
,
1618 .init
= scsi_cd_initfn
,
1619 .destroy
= scsi_destroy
,
1620 .alloc_req
= scsi_new_request
,
1621 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1622 .qdev
.props
= (Property
[]) {
1623 DEFINE_SCSI_DISK_PROPERTIES(),
1624 DEFINE_PROP_END_OF_LIST(),
1627 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1628 .qdev
.fw_name
= "disk",
1629 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1630 .qdev
.size
= sizeof(SCSIDiskState
),
1631 .qdev
.reset
= scsi_disk_reset
,
1632 .init
= scsi_disk_initfn
,
1633 .destroy
= scsi_destroy
,
1634 .alloc_req
= scsi_new_request
,
1635 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1636 .qdev
.props
= (Property
[]) {
1637 DEFINE_SCSI_DISK_PROPERTIES(),
1638 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1639 DEFINE_PROP_END_OF_LIST(),
1644 static void scsi_disk_register_devices(void)
1648 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1649 scsi_qdev_register(&scsi_disk_info
[i
]);
1652 device_init(scsi_disk_register_devices
)