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. */
83 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
84 static int scsi_disk_emulate_command(SCSIDiskReq
*r
);
86 static void scsi_free_request(SCSIRequest
*req
)
88 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
90 if (r
->iov
.iov_base
) {
91 qemu_vfree(r
->iov
.iov_base
);
95 /* Helper function for command completion with sense. */
96 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
98 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
99 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
100 scsi_req_build_sense(&r
->req
, sense
);
101 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
104 /* Cancel a pending data transfer. */
105 static void scsi_cancel_io(SCSIRequest
*req
)
107 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
109 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
111 bdrv_aio_cancel(r
->req
.aiocb
);
116 static uint32_t scsi_init_iovec(SCSIDiskReq
*r
)
118 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
120 if (!r
->iov
.iov_base
) {
121 r
->buflen
= SCSI_DMA_BUF_SIZE
;
122 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, r
->buflen
);
124 r
->iov
.iov_len
= MIN(r
->sector_count
* 512, r
->buflen
);
125 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
126 return r
->qiov
.size
/ 512;
129 static void scsi_read_complete(void * opaque
, int ret
)
131 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
132 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
135 if (r
->req
.aiocb
!= NULL
) {
137 bdrv_acct_done(s
->bs
, &r
->acct
);
141 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
146 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->qiov
.size
);
148 n
= r
->qiov
.size
/ 512;
150 r
->sector_count
-= n
;
151 scsi_req_data(&r
->req
, r
->qiov
.size
);
154 static void scsi_flush_complete(void * opaque
, int ret
)
156 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
157 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
159 if (r
->req
.aiocb
!= NULL
) {
161 bdrv_acct_done(s
->bs
, &r
->acct
);
165 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
170 scsi_req_complete(&r
->req
, GOOD
);
173 /* Read more data from scsi device into buffer. */
174 static void scsi_read_data(SCSIRequest
*req
)
176 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
177 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
180 if (r
->sector_count
== (uint32_t)-1) {
181 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
183 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
186 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
187 if (r
->sector_count
== 0) {
188 /* This also clears the sense buffer for REQUEST SENSE. */
189 scsi_req_complete(&r
->req
, GOOD
);
193 /* No data transfer may already be in progress */
194 assert(r
->req
.aiocb
== NULL
);
196 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
197 DPRINTF("Data transfer direction invalid\n");
198 scsi_read_complete(r
, -EINVAL
);
203 scsi_read_complete(r
, -ENOMEDIUM
);
205 n
= scsi_init_iovec(r
);
206 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
207 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
208 scsi_read_complete
, r
);
209 if (r
->req
.aiocb
== NULL
) {
210 scsi_read_complete(r
, -EIO
);
214 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
216 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
217 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
218 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
220 if (action
== BLOCK_ERR_IGNORE
) {
221 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
225 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
226 || action
== BLOCK_ERR_STOP_ANY
) {
228 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
229 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
231 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
232 vm_stop(RUN_STATE_IO_ERROR
);
233 bdrv_iostatus_set_err(s
->bs
, error
);
237 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
240 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
243 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
246 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
249 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
254 static void scsi_write_complete(void * opaque
, int ret
)
256 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
257 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
260 if (r
->req
.aiocb
!= NULL
) {
262 bdrv_acct_done(s
->bs
, &r
->acct
);
266 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
271 n
= r
->qiov
.size
/ 512;
273 r
->sector_count
-= n
;
274 if (r
->sector_count
== 0) {
275 scsi_req_complete(&r
->req
, GOOD
);
278 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, r
->qiov
.size
);
279 scsi_req_data(&r
->req
, r
->qiov
.size
);
283 static void scsi_write_data(SCSIRequest
*req
)
285 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
286 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
289 /* No data transfer may already be in progress */
290 assert(r
->req
.aiocb
== NULL
);
292 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
293 DPRINTF("Data transfer direction invalid\n");
294 scsi_write_complete(r
, -EINVAL
);
298 n
= r
->qiov
.size
/ 512;
301 scsi_write_complete(r
, -ENOMEDIUM
);
303 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
304 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
305 scsi_write_complete
, r
);
306 if (r
->req
.aiocb
== NULL
) {
307 scsi_write_complete(r
, -ENOMEM
);
310 /* Called for the first time. Ask the driver to send us more data. */
311 scsi_write_complete(r
, 0);
315 static void scsi_dma_restart_bh(void *opaque
)
317 SCSIDiskState
*s
= opaque
;
321 qemu_bh_delete(s
->bh
);
324 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
325 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
326 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
327 int status
= r
->status
;
331 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
333 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
334 case SCSI_REQ_STATUS_RETRY_READ
:
335 scsi_read_data(&r
->req
);
337 case SCSI_REQ_STATUS_RETRY_WRITE
:
338 scsi_write_data(&r
->req
);
340 case SCSI_REQ_STATUS_RETRY_FLUSH
:
341 ret
= scsi_disk_emulate_command(r
);
343 scsi_req_complete(&r
->req
, GOOD
);
350 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
352 SCSIDiskState
*s
= opaque
;
358 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
359 qemu_bh_schedule(s
->bh
);
363 /* Return a pointer to the data buffer. */
364 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
366 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
368 return (uint8_t *)r
->iov
.iov_base
;
371 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
373 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
376 if (req
->cmd
.buf
[1] & 0x2) {
377 /* Command support data - optional, not implemented */
378 BADF("optional INQUIRY command support request not implemented\n");
382 if (req
->cmd
.buf
[1] & 0x1) {
383 /* Vital product data */
384 uint8_t page_code
= req
->cmd
.buf
[2];
385 if (req
->cmd
.xfer
< 4) {
386 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
387 "less than 4\n", page_code
, req
->cmd
.xfer
);
391 if (s
->qdev
.type
== TYPE_ROM
) {
392 outbuf
[buflen
++] = 5;
394 outbuf
[buflen
++] = 0;
396 outbuf
[buflen
++] = page_code
; // this page
397 outbuf
[buflen
++] = 0x00;
400 case 0x00: /* Supported page codes, mandatory */
403 DPRINTF("Inquiry EVPD[Supported pages] "
404 "buffer size %zd\n", req
->cmd
.xfer
);
406 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
408 outbuf
[buflen
++] = 0x80; // unit serial number
410 outbuf
[buflen
++] = 0x83; // device identification
411 if (s
->qdev
.type
== TYPE_DISK
) {
412 outbuf
[buflen
++] = 0xb0; // block limits
413 outbuf
[buflen
++] = 0xb2; // thin provisioning
415 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
418 case 0x80: /* Device serial number, optional */
423 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
427 l
= strlen(s
->serial
);
428 if (l
> req
->cmd
.xfer
) {
435 DPRINTF("Inquiry EVPD[Serial number] "
436 "buffer size %zd\n", req
->cmd
.xfer
);
437 outbuf
[buflen
++] = l
;
438 memcpy(outbuf
+buflen
, s
->serial
, l
);
443 case 0x83: /* Device identification page, mandatory */
445 int max_len
= 255 - 8;
446 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
448 if (id_len
> max_len
) {
451 DPRINTF("Inquiry EVPD[Device identification] "
452 "buffer size %zd\n", req
->cmd
.xfer
);
454 outbuf
[buflen
++] = 4 + id_len
;
455 outbuf
[buflen
++] = 0x2; // ASCII
456 outbuf
[buflen
++] = 0; // not officially assigned
457 outbuf
[buflen
++] = 0; // reserved
458 outbuf
[buflen
++] = id_len
; // length of data following
460 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
464 case 0xb0: /* block limits */
466 unsigned int unmap_sectors
=
467 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
468 unsigned int min_io_size
=
469 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
470 unsigned int opt_io_size
=
471 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
473 if (s
->qdev
.type
== TYPE_ROM
) {
474 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
478 /* required VPD size with unmap support */
479 outbuf
[3] = buflen
= 0x3c;
481 memset(outbuf
+ 4, 0, buflen
- 4);
483 /* optimal transfer length granularity */
484 outbuf
[6] = (min_io_size
>> 8) & 0xff;
485 outbuf
[7] = min_io_size
& 0xff;
487 /* optimal transfer length */
488 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
489 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
490 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
491 outbuf
[15] = opt_io_size
& 0xff;
493 /* optimal unmap granularity */
494 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
495 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
496 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
497 outbuf
[31] = unmap_sectors
& 0xff;
500 case 0xb2: /* thin provisioning */
502 outbuf
[3] = buflen
= 8;
504 outbuf
[5] = 0x40; /* write same with unmap supported */
510 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
511 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
518 /* Standard INQUIRY data */
519 if (req
->cmd
.buf
[2] != 0) {
520 BADF("Error: Inquiry (STANDARD) page or code "
521 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
526 if (req
->cmd
.xfer
< 5) {
527 BADF("Error: Inquiry (STANDARD) buffer size %zd "
528 "is less than 5\n", req
->cmd
.xfer
);
532 buflen
= req
->cmd
.xfer
;
533 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
534 buflen
= SCSI_MAX_INQUIRY_LEN
;
536 memset(outbuf
, 0, buflen
);
538 outbuf
[0] = s
->qdev
.type
& 0x1f;
539 if (s
->qdev
.type
== TYPE_ROM
) {
541 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
543 outbuf
[1] = s
->removable
? 0x80 : 0;
544 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
546 memcpy(&outbuf
[8], "QEMU ", 8);
547 memset(&outbuf
[32], 0, 4);
548 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
550 * We claim conformance to SPC-3, which is required for guests
551 * to ask for modern features like READ CAPACITY(16) or the
552 * block characteristics VPD page by default. Not all of SPC-3
553 * is actually implemented, but we're good enough.
556 outbuf
[3] = 2; /* Format 2 */
559 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
561 /* If the allocation length of CDB is too small,
562 the additional length is not adjusted */
566 /* Sync data transfer and TCQ. */
567 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
571 static inline bool media_is_dvd(SCSIDiskState
*s
)
574 if (s
->qdev
.type
!= TYPE_ROM
) {
577 if (!bdrv_is_inserted(s
->bs
)) {
580 bdrv_get_geometry(s
->bs
, &nb_sectors
);
581 return nb_sectors
> CD_MAX_SECTORS
;
584 static inline bool media_is_cd(SCSIDiskState
*s
)
587 if (s
->qdev
.type
!= TYPE_ROM
) {
590 if (!bdrv_is_inserted(s
->bs
)) {
593 bdrv_get_geometry(s
->bs
, &nb_sectors
);
594 return nb_sectors
<= CD_MAX_SECTORS
;
597 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
600 static const int rds_caps_size
[5] = {
607 uint8_t media
= r
->req
.cmd
.buf
[1];
608 uint8_t layer
= r
->req
.cmd
.buf
[6];
609 uint8_t format
= r
->req
.cmd
.buf
[7];
612 if (s
->qdev
.type
!= TYPE_ROM
) {
616 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
620 if (format
!= 0xff) {
621 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
)) {
622 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
625 if (media_is_cd(s
)) {
626 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
629 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
632 size
= rds_caps_size
[format
];
633 memset(outbuf
, 0, size
);
638 /* Physical format information */
643 bdrv_get_geometry(s
->bs
, &nb_sectors
);
645 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
646 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
647 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
648 outbuf
[7] = 0; /* default densities */
650 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
651 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
655 case 0x01: /* DVD copyright information, all zeros */
658 case 0x03: /* BCA information - invalid field for no BCA info */
661 case 0x04: /* DVD disc manufacturing information, all zeros */
664 case 0xff: { /* List capabilities */
667 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
668 if (!rds_caps_size
[i
]) {
672 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
673 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
683 /* Size of buffer, not including 2 byte size field */
684 stw_be_p(outbuf
, size
- 2);
691 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
693 uint8_t event_code
, media_status
;
697 media_status
= MS_TRAY_OPEN
;
698 } else if (bdrv_is_inserted(s
->bs
)) {
699 media_status
= MS_MEDIA_PRESENT
;
702 /* Event notification descriptor */
703 event_code
= MEC_NO_CHANGE
;
704 if (media_status
!= MS_TRAY_OPEN
&& s
->media_event
) {
705 event_code
= MEC_NEW_MEDIA
;
706 s
->media_event
= false;
709 outbuf
[0] = event_code
;
710 outbuf
[1] = media_status
;
712 /* These fields are reserved, just clear them. */
718 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
722 uint8_t *buf
= r
->req
.cmd
.buf
;
723 uint8_t notification_class_request
= buf
[4];
724 if (s
->qdev
.type
!= TYPE_ROM
) {
727 if ((buf
[1] & 1) == 0) {
733 outbuf
[0] = outbuf
[1] = 0;
734 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
735 if (notification_class_request
& (1 << GESN_MEDIA
)) {
736 outbuf
[2] = GESN_MEDIA
;
737 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
741 stw_be_p(outbuf
, size
- 4);
745 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
749 if (s
->qdev
.type
!= TYPE_ROM
) {
752 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
753 memset(outbuf
, 0, 40);
754 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
755 stw_be_p(&outbuf
[6], current
);
756 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
757 outbuf
[10] = 0x03; /* persistent, current */
758 outbuf
[11] = 8; /* two profiles */
759 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
760 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
761 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
762 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
763 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
764 stw_be_p(&outbuf
[20], 1);
765 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
767 stl_be_p(&outbuf
[24], 1); /* SCSI */
768 outbuf
[28] = 1; /* DBE = 1, mandatory */
769 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
770 stw_be_p(&outbuf
[32], 3);
771 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
773 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
774 /* TODO: Random readable, CD read, DVD read, drive serial number,
779 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
781 if (s
->qdev
.type
!= TYPE_ROM
) {
784 memset(outbuf
, 0, 8);
785 outbuf
[5] = 1; /* CD-ROM */
789 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
792 static const int mode_sense_valid
[0x3f] = {
793 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
794 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
795 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
796 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
797 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
798 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
801 BlockDriverState
*bdrv
= s
->bs
;
802 int cylinders
, heads
, secs
;
803 uint8_t *p
= *p_outbuf
;
805 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
812 * If Changeable Values are requested, a mask denoting those mode parameters
813 * that are changeable shall be returned. As we currently don't support
814 * parameter changes via MODE_SELECT all bits are returned set to zero.
815 * The buffer was already menset to zero by the caller of this function.
818 case MODE_PAGE_HD_GEOMETRY
:
820 if (page_control
== 1) { /* Changeable Values */
823 /* if a geometry hint is available, use it */
824 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
825 p
[2] = (cylinders
>> 16) & 0xff;
826 p
[3] = (cylinders
>> 8) & 0xff;
827 p
[4] = cylinders
& 0xff;
829 /* Write precomp start cylinder, disabled */
830 p
[6] = (cylinders
>> 16) & 0xff;
831 p
[7] = (cylinders
>> 8) & 0xff;
832 p
[8] = cylinders
& 0xff;
833 /* Reduced current start cylinder, disabled */
834 p
[9] = (cylinders
>> 16) & 0xff;
835 p
[10] = (cylinders
>> 8) & 0xff;
836 p
[11] = cylinders
& 0xff;
837 /* Device step rate [ns], 200ns */
840 /* Landing zone cylinder */
844 /* Medium rotation rate [rpm], 5400 rpm */
845 p
[20] = (5400 >> 8) & 0xff;
849 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
851 if (page_control
== 1) { /* Changeable Values */
854 /* Transfer rate [kbit/s], 5Mbit/s */
857 /* if a geometry hint is available, use it */
858 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
861 p
[6] = s
->cluster_size
* 2;
862 p
[8] = (cylinders
>> 8) & 0xff;
863 p
[9] = cylinders
& 0xff;
864 /* Write precomp start cylinder, disabled */
865 p
[10] = (cylinders
>> 8) & 0xff;
866 p
[11] = cylinders
& 0xff;
867 /* Reduced current start cylinder, disabled */
868 p
[12] = (cylinders
>> 8) & 0xff;
869 p
[13] = cylinders
& 0xff;
870 /* Device step rate [100us], 100us */
873 /* Device step pulse width [us], 1us */
875 /* Device head settle delay [100us], 100us */
878 /* Motor on delay [0.1s], 0.1s */
880 /* Motor off delay [0.1s], 0.1s */
882 /* Medium rotation rate [rpm], 5400 rpm */
883 p
[28] = (5400 >> 8) & 0xff;
887 case MODE_PAGE_CACHING
:
890 if (page_control
== 1) { /* Changeable Values */
893 if (bdrv_enable_write_cache(s
->bs
)) {
898 case MODE_PAGE_R_W_ERROR
:
900 p
[2] = 0x80; /* Automatic Write Reallocation Enabled */
901 if (s
->qdev
.type
== TYPE_ROM
) {
902 p
[3] = 0x20; /* Read Retry Count */
906 case MODE_PAGE_AUDIO_CTL
:
910 case MODE_PAGE_CAPABILITIES
:
912 if (page_control
== 1) { /* Changeable Values */
916 p
[2] = 0x3b; /* CD-R & CD-RW read */
917 p
[3] = 0; /* Writing not supported */
918 p
[4] = 0x7f; /* Audio, composite, digital out,
919 mode 2 form 1&2, multi session */
920 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
921 RW corrected, C2 errors, ISRC,
923 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
924 /* Locking supported, jumper present, eject, tray */
925 p
[7] = 0; /* no volume & mute control, no
927 p
[8] = (50 * 176) >> 8; /* 50x read speed */
928 p
[9] = (50 * 176) & 0xff;
929 p
[10] = 2 >> 8; /* Two volume levels */
931 p
[12] = 2048 >> 8; /* 2M buffer */
933 p
[14] = (16 * 176) >> 8; /* 16x read speed current */
934 p
[15] = (16 * 176) & 0xff;
935 p
[18] = (16 * 176) >> 8; /* 16x write speed */
936 p
[19] = (16 * 176) & 0xff;
937 p
[20] = (16 * 176) >> 8; /* 16x write speed current */
938 p
[21] = (16 * 176) & 0xff;
945 *p_outbuf
+= p
[1] + 2;
949 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
951 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
953 int page
, dbd
, buflen
, ret
, page_control
;
955 uint8_t dev_specific_param
;
957 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
958 page
= r
->req
.cmd
.buf
[2] & 0x3f;
959 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
960 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
961 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
962 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
965 if (bdrv_is_read_only(s
->bs
)) {
966 dev_specific_param
= 0x80; /* Readonly. */
968 dev_specific_param
= 0x00;
971 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
972 p
[1] = 0; /* Default media type. */
973 p
[2] = dev_specific_param
;
974 p
[3] = 0; /* Block descriptor length. */
976 } else { /* MODE_SENSE_10 */
977 p
[2] = 0; /* Default media type. */
978 p
[3] = dev_specific_param
;
979 p
[6] = p
[7] = 0; /* Block descriptor length. */
983 bdrv_get_geometry(s
->bs
, &nb_sectors
);
984 if (!dbd
&& nb_sectors
) {
985 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
986 outbuf
[3] = 8; /* Block descriptor length */
987 } else { /* MODE_SENSE_10 */
988 outbuf
[7] = 8; /* Block descriptor length */
990 nb_sectors
/= s
->cluster_size
;
991 if (nb_sectors
> 0xffffff) {
994 p
[0] = 0; /* media density code */
995 p
[1] = (nb_sectors
>> 16) & 0xff;
996 p
[2] = (nb_sectors
>> 8) & 0xff;
997 p
[3] = nb_sectors
& 0xff;
998 p
[4] = 0; /* reserved */
999 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
1000 p
[6] = s
->cluster_size
* 2;
1005 if (page_control
== 3) {
1007 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1012 for (page
= 0; page
<= 0x3e; page
++) {
1013 mode_sense_page(s
, page
, &p
, page_control
);
1016 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1022 buflen
= p
- outbuf
;
1024 * The mode data length field specifies the length in bytes of the
1025 * following data that is available to be transferred. The mode data
1026 * length does not include itself.
1028 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1029 outbuf
[0] = buflen
- 1;
1030 } else { /* MODE_SENSE_10 */
1031 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1032 outbuf
[1] = (buflen
- 2) & 0xff;
1034 if (buflen
> r
->req
.cmd
.xfer
) {
1035 buflen
= r
->req
.cmd
.xfer
;
1040 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1042 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1043 int start_track
, format
, msf
, toclen
;
1044 uint64_t nb_sectors
;
1046 msf
= req
->cmd
.buf
[1] & 2;
1047 format
= req
->cmd
.buf
[2] & 0xf;
1048 start_track
= req
->cmd
.buf
[6];
1049 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1050 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1051 nb_sectors
/= s
->cluster_size
;
1054 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1057 /* multi session : only a single session defined */
1059 memset(outbuf
, 0, 12);
1065 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1070 if (toclen
> req
->cmd
.xfer
) {
1071 toclen
= req
->cmd
.xfer
;
1076 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1078 SCSIRequest
*req
= &r
->req
;
1079 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1080 bool start
= req
->cmd
.buf
[4] & 1;
1081 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1083 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
1084 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1085 scsi_check_condition(r
,
1086 bdrv_is_inserted(s
->bs
)
1087 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1088 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1091 bdrv_eject(s
->bs
, !start
);
1092 s
->tray_open
= !start
;
1097 static int scsi_disk_emulate_command(SCSIDiskReq
*r
)
1099 SCSIRequest
*req
= &r
->req
;
1100 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1101 uint64_t nb_sectors
;
1105 if (!r
->iov
.iov_base
) {
1107 * FIXME: we shouldn't return anything bigger than 4k, but the code
1108 * requires the buffer to be as big as req->cmd.xfer in several
1109 * places. So, do not allow CDBs with a very large ALLOCATION
1110 * LENGTH. The real fix would be to modify scsi_read_data and
1111 * dma_buf_read, so that they return data beyond the buflen
1114 if (req
->cmd
.xfer
> 65536) {
1115 goto illegal_request
;
1117 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1118 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, r
->buflen
);
1121 outbuf
= r
->iov
.iov_base
;
1122 switch (req
->cmd
.buf
[0]) {
1123 case TEST_UNIT_READY
:
1124 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
)) {
1129 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1131 goto illegal_request
;
1136 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1138 goto illegal_request
;
1142 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1144 goto illegal_request
;
1148 if (req
->cmd
.buf
[1] & 1) {
1149 goto illegal_request
;
1153 if (req
->cmd
.buf
[1] & 3) {
1154 goto illegal_request
;
1158 if (req
->cmd
.buf
[1] & 1) {
1159 goto illegal_request
;
1163 if (req
->cmd
.buf
[1] & 3) {
1164 goto illegal_request
;
1168 if (scsi_disk_emulate_start_stop(r
) < 0) {
1172 case ALLOW_MEDIUM_REMOVAL
:
1173 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1174 bdrv_lock_medium(s
->bs
, req
->cmd
.buf
[4] & 1);
1176 case READ_CAPACITY_10
:
1177 /* The normal LEN field for this command is zero. */
1178 memset(outbuf
, 0, 8);
1179 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1183 nb_sectors
/= s
->cluster_size
;
1184 /* Returned value is the address of the last sector. */
1186 /* Remember the new size for read/write sanity checking. */
1187 s
->max_lba
= nb_sectors
;
1188 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1189 if (nb_sectors
> UINT32_MAX
) {
1190 nb_sectors
= UINT32_MAX
;
1192 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1193 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1194 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1195 outbuf
[3] = nb_sectors
& 0xff;
1198 outbuf
[6] = s
->cluster_size
* 2;
1202 case MECHANISM_STATUS
:
1203 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1205 goto illegal_request
;
1208 case GET_CONFIGURATION
:
1209 buflen
= scsi_get_configuration(s
, outbuf
);
1211 goto illegal_request
;
1214 case GET_EVENT_STATUS_NOTIFICATION
:
1215 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1217 goto illegal_request
;
1220 case READ_DVD_STRUCTURE
:
1221 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1223 goto illegal_request
;
1226 case SERVICE_ACTION_IN_16
:
1227 /* Service Action In subcommands. */
1228 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1229 DPRINTF("SAI READ CAPACITY(16)\n");
1230 memset(outbuf
, 0, req
->cmd
.xfer
);
1231 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1235 nb_sectors
/= s
->cluster_size
;
1236 /* Returned value is the address of the last sector. */
1238 /* Remember the new size for read/write sanity checking. */
1239 s
->max_lba
= nb_sectors
;
1240 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1241 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1242 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1243 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1244 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1245 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1246 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1247 outbuf
[7] = nb_sectors
& 0xff;
1250 outbuf
[10] = s
->cluster_size
* 2;
1253 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1255 /* set TPE bit if the format supports discard */
1256 if (s
->qdev
.conf
.discard_granularity
) {
1260 /* Protection, exponent and lowest lba field left blank. */
1261 buflen
= req
->cmd
.xfer
;
1264 DPRINTF("Unsupported Service Action In\n");
1265 goto illegal_request
;
1269 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1275 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
)) {
1276 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1278 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1283 if (r
->req
.status
== -1) {
1284 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1289 /* Execute a scsi command. Returns the length of the data expected by the
1290 command. This will be Positive for data transfers from the device
1291 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1292 and zero if the command does not transfer any data. */
1294 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1296 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1297 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1303 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1308 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1309 printf(" 0x%02x", buf
[i
]);
1316 case TEST_UNIT_READY
:
1325 case ALLOW_MEDIUM_REMOVAL
:
1326 case READ_CAPACITY_10
:
1328 case READ_DVD_STRUCTURE
:
1329 case GET_CONFIGURATION
:
1330 case GET_EVENT_STATUS_NOTIFICATION
:
1331 case MECHANISM_STATUS
:
1332 case SERVICE_ACTION_IN_16
:
1334 rc
= scsi_disk_emulate_command(r
);
1339 r
->iov
.iov_len
= rc
;
1341 case SYNCHRONIZE_CACHE
:
1342 bdrv_acct_start(s
->bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1343 r
->req
.aiocb
= bdrv_aio_flush(s
->bs
, scsi_flush_complete
, r
);
1344 if (r
->req
.aiocb
== NULL
) {
1345 scsi_flush_complete(r
, -EIO
);
1352 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1353 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1354 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1357 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1358 r
->sector_count
= len
* s
->cluster_size
;
1364 case WRITE_VERIFY_10
:
1365 case WRITE_VERIFY_12
:
1366 case WRITE_VERIFY_16
:
1367 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1368 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1369 (command
& 0xe) == 0xe ? "And Verify " : "",
1370 r
->req
.cmd
.lba
, len
);
1371 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1374 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1375 r
->sector_count
= len
* s
->cluster_size
;
1378 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1379 /* We don't support mode parameter changes.
1380 Allow the mode parameter header + block descriptors only. */
1381 if (r
->req
.cmd
.xfer
> 12) {
1385 case MODE_SELECT_10
:
1386 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1387 /* We don't support mode parameter changes.
1388 Allow the mode parameter header + block descriptors only. */
1389 if (r
->req
.cmd
.xfer
> 16) {
1395 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1397 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1402 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1404 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1405 r
->req
.cmd
.lba
, len
);
1407 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1412 * We only support WRITE SAME with the unmap bit set for now.
1414 if (!(buf
[1] & 0x8)) {
1418 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1419 len
* s
->cluster_size
);
1421 /* XXX: better error code ?*/
1429 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1430 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1433 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1436 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1439 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1440 scsi_req_complete(&r
->req
, GOOD
);
1442 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1443 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1446 if (!r
->sector_count
) {
1447 r
->sector_count
= -1;
1453 static void scsi_disk_reset(DeviceState
*dev
)
1455 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1456 uint64_t nb_sectors
;
1458 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1460 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1461 nb_sectors
/= s
->cluster_size
;
1465 s
->max_lba
= nb_sectors
;
1468 static void scsi_destroy(SCSIDevice
*dev
)
1470 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1472 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1473 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1476 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1478 SCSIDiskState
*s
= opaque
;
1481 * When a CD gets changed, we have to report an ejected state and
1482 * then a loaded state to guests so that they detect tray
1483 * open/close and media change events. Guests that do not use
1484 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1485 * states rely on this behavior.
1487 * media_changed governs the state machine used for unit attention
1488 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1490 s
->media_changed
= load
;
1491 s
->tray_open
= !load
;
1492 s
->qdev
.unit_attention
= SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
);
1493 s
->media_event
= true;
1496 static bool scsi_cd_is_tray_open(void *opaque
)
1498 return ((SCSIDiskState
*)opaque
)->tray_open
;
1501 static bool scsi_cd_is_medium_locked(void *opaque
)
1503 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1506 static const BlockDevOps scsi_cd_block_ops
= {
1507 .change_media_cb
= scsi_cd_change_media_cb
,
1508 .is_tray_open
= scsi_cd_is_tray_open
,
1509 .is_medium_locked
= scsi_cd_is_medium_locked
,
1512 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
1514 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1515 if (s
->media_changed
) {
1516 s
->media_changed
= false;
1517 s
->qdev
.unit_attention
= SENSE_CODE(MEDIUM_CHANGED
);
1521 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1523 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1526 if (!s
->qdev
.conf
.bs
) {
1527 error_report("scsi-disk: drive property not set");
1530 s
->bs
= s
->qdev
.conf
.bs
;
1532 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1533 error_report("Device needs media, but drive is empty");
1538 /* try to fall back to value set with legacy -drive serial=... */
1539 dinfo
= drive_get_by_blockdev(s
->bs
);
1540 if (*dinfo
->serial
) {
1541 s
->serial
= g_strdup(dinfo
->serial
);
1546 s
->version
= g_strdup(QEMU_VERSION
);
1549 if (bdrv_is_sg(s
->bs
)) {
1550 error_report("scsi-disk: unwanted /dev/sg*");
1554 if (scsi_type
== TYPE_ROM
) {
1555 bdrv_set_dev_ops(s
->bs
, &scsi_cd_block_ops
, s
);
1556 s
->qdev
.blocksize
= 2048;
1557 } else if (scsi_type
== TYPE_DISK
) {
1558 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1560 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1563 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1564 bdrv_set_buffer_alignment(s
->bs
, s
->qdev
.blocksize
);
1566 s
->qdev
.type
= scsi_type
;
1567 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1568 bdrv_iostatus_enable(s
->bs
);
1569 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1573 static int scsi_hd_initfn(SCSIDevice
*dev
)
1575 return scsi_initfn(dev
, TYPE_DISK
);
1578 static int scsi_cd_initfn(SCSIDevice
*dev
)
1580 return scsi_initfn(dev
, TYPE_ROM
);
1583 static int scsi_disk_initfn(SCSIDevice
*dev
)
1588 if (!dev
->conf
.bs
) {
1589 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1591 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1592 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1595 return scsi_initfn(dev
, scsi_type
);
1598 static SCSIReqOps scsi_disk_reqops
= {
1599 .size
= sizeof(SCSIDiskReq
),
1600 .free_req
= scsi_free_request
,
1601 .send_command
= scsi_send_command
,
1602 .read_data
= scsi_read_data
,
1603 .write_data
= scsi_write_data
,
1604 .cancel_io
= scsi_cancel_io
,
1605 .get_buf
= scsi_get_buf
,
1608 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1609 uint32_t lun
, void *hba_private
)
1611 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1614 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1618 #define DEFINE_SCSI_DISK_PROPERTIES() \
1619 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1620 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1621 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1623 static SCSIDeviceInfo scsi_disk_info
[] = {
1625 .qdev
.name
= "scsi-hd",
1626 .qdev
.fw_name
= "disk",
1627 .qdev
.desc
= "virtual SCSI disk",
1628 .qdev
.size
= sizeof(SCSIDiskState
),
1629 .qdev
.reset
= scsi_disk_reset
,
1630 .init
= scsi_hd_initfn
,
1631 .destroy
= scsi_destroy
,
1632 .alloc_req
= scsi_new_request
,
1633 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1634 .qdev
.props
= (Property
[]) {
1635 DEFINE_SCSI_DISK_PROPERTIES(),
1636 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1637 DEFINE_PROP_END_OF_LIST(),
1640 .qdev
.name
= "scsi-cd",
1641 .qdev
.fw_name
= "disk",
1642 .qdev
.desc
= "virtual SCSI CD-ROM",
1643 .qdev
.size
= sizeof(SCSIDiskState
),
1644 .qdev
.reset
= scsi_disk_reset
,
1645 .init
= scsi_cd_initfn
,
1646 .destroy
= scsi_destroy
,
1647 .alloc_req
= scsi_new_request
,
1648 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1649 .qdev
.props
= (Property
[]) {
1650 DEFINE_SCSI_DISK_PROPERTIES(),
1651 DEFINE_PROP_END_OF_LIST(),
1654 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1655 .qdev
.fw_name
= "disk",
1656 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1657 .qdev
.size
= sizeof(SCSIDiskState
),
1658 .qdev
.reset
= scsi_disk_reset
,
1659 .init
= scsi_disk_initfn
,
1660 .destroy
= scsi_destroy
,
1661 .alloc_req
= scsi_new_request
,
1662 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1663 .qdev
.props
= (Property
[]) {
1664 DEFINE_SCSI_DISK_PROPERTIES(),
1665 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1666 DEFINE_PROP_END_OF_LIST(),
1671 static void scsi_disk_register_devices(void)
1675 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1676 scsi_qdev_register(&scsi_disk_info
[i
]);
1679 device_init(scsi_disk_register_devices
)