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
;
79 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
80 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
);
82 static void scsi_free_request(SCSIRequest
*req
)
84 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
86 if (r
->iov
.iov_base
) {
87 qemu_vfree(r
->iov
.iov_base
);
91 /* Helper function for command completion with sense. */
92 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
94 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
95 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
96 scsi_req_build_sense(&r
->req
, sense
);
97 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
100 /* Cancel a pending data transfer. */
101 static void scsi_cancel_io(SCSIRequest
*req
)
103 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
105 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
107 bdrv_aio_cancel(r
->req
.aiocb
);
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
, SCSI_REQ_STATUS_RETRY_READ
)) {
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 static void scsi_flush_complete(void * opaque
, int ret
)
152 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
153 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
155 if (r
->req
.aiocb
!= NULL
) {
157 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
161 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
166 scsi_req_complete(&r
->req
, GOOD
);
169 /* Read more data from scsi device into buffer. */
170 static void scsi_read_data(SCSIRequest
*req
)
172 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
173 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
176 if (r
->sector_count
== (uint32_t)-1) {
177 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
179 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
182 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
183 if (r
->sector_count
== 0) {
184 /* This also clears the sense buffer for REQUEST SENSE. */
185 scsi_req_complete(&r
->req
, GOOD
);
189 /* No data transfer may already be in progress */
190 assert(r
->req
.aiocb
== NULL
);
192 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
193 DPRINTF("Data transfer direction invalid\n");
194 scsi_read_complete(r
, -EINVAL
);
199 scsi_read_complete(r
, -ENOMEDIUM
);
201 n
= scsi_init_iovec(r
);
202 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
203 r
->req
.aiocb
= bdrv_aio_readv(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
204 scsi_read_complete
, r
);
205 if (r
->req
.aiocb
== NULL
) {
206 scsi_read_complete(r
, -EIO
);
210 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
212 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
213 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
214 BlockErrorAction action
= bdrv_get_on_error(s
->qdev
.conf
.bs
, is_read
);
216 if (action
== BLOCK_ERR_IGNORE
) {
217 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_IGNORE
, is_read
);
221 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
222 || action
== BLOCK_ERR_STOP_ANY
) {
224 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
225 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
227 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_STOP
, is_read
);
228 vm_stop(RUN_STATE_IO_ERROR
);
229 bdrv_iostatus_set_err(s
->qdev
.conf
.bs
, error
);
233 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
236 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
239 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
242 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
245 bdrv_mon_event(s
->qdev
.conf
.bs
, BDRV_ACTION_REPORT
, is_read
);
250 static void scsi_write_complete(void * opaque
, int ret
)
252 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
253 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
256 if (r
->req
.aiocb
!= NULL
) {
258 bdrv_acct_done(s
->qdev
.conf
.bs
, &r
->acct
);
262 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
267 n
= r
->qiov
.size
/ 512;
269 r
->sector_count
-= n
;
270 if (r
->sector_count
== 0) {
271 scsi_req_complete(&r
->req
, GOOD
);
274 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, r
->qiov
.size
);
275 scsi_req_data(&r
->req
, r
->qiov
.size
);
279 static void scsi_write_data(SCSIRequest
*req
)
281 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
282 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
285 /* No data transfer may already be in progress */
286 assert(r
->req
.aiocb
== NULL
);
288 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
289 DPRINTF("Data transfer direction invalid\n");
290 scsi_write_complete(r
, -EINVAL
);
294 n
= r
->qiov
.size
/ 512;
297 scsi_write_complete(r
, -ENOMEDIUM
);
299 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
300 r
->req
.aiocb
= bdrv_aio_writev(s
->qdev
.conf
.bs
, r
->sector
, &r
->qiov
, n
,
301 scsi_write_complete
, r
);
302 if (r
->req
.aiocb
== NULL
) {
303 scsi_write_complete(r
, -ENOMEM
);
306 /* Called for the first time. Ask the driver to send us more data. */
307 scsi_write_complete(r
, 0);
311 static void scsi_dma_restart_bh(void *opaque
)
313 SCSIDiskState
*s
= opaque
;
317 qemu_bh_delete(s
->bh
);
320 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
321 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
322 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
323 int status
= r
->status
;
326 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
328 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
329 case SCSI_REQ_STATUS_RETRY_READ
:
330 scsi_read_data(&r
->req
);
332 case SCSI_REQ_STATUS_RETRY_WRITE
:
333 scsi_write_data(&r
->req
);
335 case SCSI_REQ_STATUS_RETRY_FLUSH
:
336 scsi_send_command(&r
->req
, r
->req
.cmd
.buf
);
343 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
345 SCSIDiskState
*s
= opaque
;
351 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
352 qemu_bh_schedule(s
->bh
);
356 /* Return a pointer to the data buffer. */
357 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
359 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
361 return (uint8_t *)r
->iov
.iov_base
;
364 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
366 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
369 if (req
->cmd
.buf
[1] & 0x2) {
370 /* Command support data - optional, not implemented */
371 BADF("optional INQUIRY command support request not implemented\n");
375 if (req
->cmd
.buf
[1] & 0x1) {
376 /* Vital product data */
377 uint8_t page_code
= req
->cmd
.buf
[2];
378 if (req
->cmd
.xfer
< 4) {
379 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
380 "less than 4\n", page_code
, req
->cmd
.xfer
);
384 outbuf
[buflen
++] = s
->qdev
.type
& 0x1f;
385 outbuf
[buflen
++] = page_code
; // this page
386 outbuf
[buflen
++] = 0x00;
389 case 0x00: /* Supported page codes, mandatory */
392 DPRINTF("Inquiry EVPD[Supported pages] "
393 "buffer size %zd\n", req
->cmd
.xfer
);
395 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
397 outbuf
[buflen
++] = 0x80; // unit serial number
399 outbuf
[buflen
++] = 0x83; // device identification
400 if (s
->qdev
.type
== TYPE_DISK
) {
401 outbuf
[buflen
++] = 0xb0; // block limits
402 outbuf
[buflen
++] = 0xb2; // thin provisioning
404 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
407 case 0x80: /* Device serial number, optional */
412 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
416 l
= strlen(s
->serial
);
417 if (l
> req
->cmd
.xfer
) {
424 DPRINTF("Inquiry EVPD[Serial number] "
425 "buffer size %zd\n", req
->cmd
.xfer
);
426 outbuf
[buflen
++] = l
;
427 memcpy(outbuf
+buflen
, s
->serial
, l
);
432 case 0x83: /* Device identification page, mandatory */
434 int max_len
= 255 - 8;
435 int id_len
= strlen(bdrv_get_device_name(s
->qdev
.conf
.bs
));
437 if (id_len
> max_len
) {
440 DPRINTF("Inquiry EVPD[Device identification] "
441 "buffer size %zd\n", req
->cmd
.xfer
);
443 outbuf
[buflen
++] = 4 + id_len
;
444 outbuf
[buflen
++] = 0x2; // ASCII
445 outbuf
[buflen
++] = 0; // not officially assigned
446 outbuf
[buflen
++] = 0; // reserved
447 outbuf
[buflen
++] = id_len
; // length of data following
449 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->qdev
.conf
.bs
), id_len
);
453 case 0xb0: /* block limits */
455 unsigned int unmap_sectors
=
456 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
457 unsigned int min_io_size
=
458 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
459 unsigned int opt_io_size
=
460 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
462 if (s
->qdev
.type
== TYPE_ROM
) {
463 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
467 /* required VPD size with unmap support */
468 outbuf
[3] = buflen
= 0x3c;
470 memset(outbuf
+ 4, 0, buflen
- 4);
472 /* optimal transfer length granularity */
473 outbuf
[6] = (min_io_size
>> 8) & 0xff;
474 outbuf
[7] = min_io_size
& 0xff;
476 /* optimal transfer length */
477 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
478 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
479 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
480 outbuf
[15] = opt_io_size
& 0xff;
482 /* optimal unmap granularity */
483 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
484 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
485 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
486 outbuf
[31] = unmap_sectors
& 0xff;
489 case 0xb2: /* thin provisioning */
491 outbuf
[3] = buflen
= 8;
493 outbuf
[5] = 0x40; /* write same with unmap supported */
499 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
500 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
507 /* Standard INQUIRY data */
508 if (req
->cmd
.buf
[2] != 0) {
509 BADF("Error: Inquiry (STANDARD) page or code "
510 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
515 if (req
->cmd
.xfer
< 5) {
516 BADF("Error: Inquiry (STANDARD) buffer size %zd "
517 "is less than 5\n", req
->cmd
.xfer
);
521 buflen
= req
->cmd
.xfer
;
522 if (buflen
> SCSI_MAX_INQUIRY_LEN
) {
523 buflen
= SCSI_MAX_INQUIRY_LEN
;
525 memset(outbuf
, 0, buflen
);
527 outbuf
[0] = s
->qdev
.type
& 0x1f;
528 outbuf
[1] = s
->removable
? 0x80 : 0;
529 if (s
->qdev
.type
== TYPE_ROM
) {
530 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
532 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
534 memcpy(&outbuf
[8], "QEMU ", 8);
535 memset(&outbuf
[32], 0, 4);
536 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
538 * We claim conformance to SPC-3, which is required for guests
539 * to ask for modern features like READ CAPACITY(16) or the
540 * block characteristics VPD page by default. Not all of SPC-3
541 * is actually implemented, but we're good enough.
544 outbuf
[3] = 2; /* Format 2 */
547 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
549 /* If the allocation length of CDB is too small,
550 the additional length is not adjusted */
554 /* Sync data transfer and TCQ. */
555 outbuf
[7] = 0x10 | (req
->bus
->info
->tcq
? 0x02 : 0);
559 static inline bool media_is_dvd(SCSIDiskState
*s
)
562 if (s
->qdev
.type
!= TYPE_ROM
) {
565 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
568 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
569 return nb_sectors
> CD_MAX_SECTORS
;
572 static inline bool media_is_cd(SCSIDiskState
*s
)
575 if (s
->qdev
.type
!= TYPE_ROM
) {
578 if (!bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
581 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
582 return nb_sectors
<= CD_MAX_SECTORS
;
585 static int scsi_read_dvd_structure(SCSIDiskState
*s
, SCSIDiskReq
*r
,
588 static const int rds_caps_size
[5] = {
595 uint8_t media
= r
->req
.cmd
.buf
[1];
596 uint8_t layer
= r
->req
.cmd
.buf
[6];
597 uint8_t format
= r
->req
.cmd
.buf
[7];
600 if (s
->qdev
.type
!= TYPE_ROM
) {
604 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
608 if (format
!= 0xff) {
609 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
610 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
613 if (media_is_cd(s
)) {
614 scsi_check_condition(r
, SENSE_CODE(INCOMPATIBLE_FORMAT
));
617 if (format
>= ARRAY_SIZE(rds_caps_size
)) {
620 size
= rds_caps_size
[format
];
621 memset(outbuf
, 0, size
);
626 /* Physical format information */
631 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
633 outbuf
[4] = 1; /* DVD-ROM, part version 1 */
634 outbuf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
635 outbuf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
636 outbuf
[7] = 0; /* default densities */
638 stl_be_p(&outbuf
[12], (nb_sectors
>> 2) - 1); /* end sector */
639 stl_be_p(&outbuf
[16], (nb_sectors
>> 2) - 1); /* l0 end sector */
643 case 0x01: /* DVD copyright information, all zeros */
646 case 0x03: /* BCA information - invalid field for no BCA info */
649 case 0x04: /* DVD disc manufacturing information, all zeros */
652 case 0xff: { /* List capabilities */
655 for (i
= 0; i
< ARRAY_SIZE(rds_caps_size
); i
++) {
656 if (!rds_caps_size
[i
]) {
660 outbuf
[size
+ 1] = 0x40; /* Not writable, readable */
661 stw_be_p(&outbuf
[size
+ 2], rds_caps_size
[i
]);
671 /* Size of buffer, not including 2 byte size field */
672 stw_be_p(outbuf
, size
- 2);
679 static int scsi_event_status_media(SCSIDiskState
*s
, uint8_t *outbuf
)
681 uint8_t event_code
, media_status
;
685 media_status
= MS_TRAY_OPEN
;
686 } else if (bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
687 media_status
= MS_MEDIA_PRESENT
;
690 /* Event notification descriptor */
691 event_code
= MEC_NO_CHANGE
;
692 if (media_status
!= MS_TRAY_OPEN
&& s
->media_event
) {
693 event_code
= MEC_NEW_MEDIA
;
694 s
->media_event
= false;
697 outbuf
[0] = event_code
;
698 outbuf
[1] = media_status
;
700 /* These fields are reserved, just clear them. */
706 static int scsi_get_event_status_notification(SCSIDiskState
*s
, SCSIDiskReq
*r
,
710 uint8_t *buf
= r
->req
.cmd
.buf
;
711 uint8_t notification_class_request
= buf
[4];
712 if (s
->qdev
.type
!= TYPE_ROM
) {
715 if ((buf
[1] & 1) == 0) {
721 outbuf
[0] = outbuf
[1] = 0;
722 outbuf
[3] = 1 << GESN_MEDIA
; /* supported events */
723 if (notification_class_request
& (1 << GESN_MEDIA
)) {
724 outbuf
[2] = GESN_MEDIA
;
725 size
+= scsi_event_status_media(s
, &outbuf
[size
]);
729 stw_be_p(outbuf
, size
- 4);
733 static int scsi_get_configuration(SCSIDiskState
*s
, uint8_t *outbuf
)
737 if (s
->qdev
.type
!= TYPE_ROM
) {
740 current
= media_is_dvd(s
) ? MMC_PROFILE_DVD_ROM
: MMC_PROFILE_CD_ROM
;
741 memset(outbuf
, 0, 40);
742 stl_be_p(&outbuf
[0], 36); /* Bytes after the data length field */
743 stw_be_p(&outbuf
[6], current
);
744 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
745 outbuf
[10] = 0x03; /* persistent, current */
746 outbuf
[11] = 8; /* two profiles */
747 stw_be_p(&outbuf
[12], MMC_PROFILE_DVD_ROM
);
748 outbuf
[14] = (current
== MMC_PROFILE_DVD_ROM
);
749 stw_be_p(&outbuf
[16], MMC_PROFILE_CD_ROM
);
750 outbuf
[18] = (current
== MMC_PROFILE_CD_ROM
);
751 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
752 stw_be_p(&outbuf
[20], 1);
753 outbuf
[22] = 0x08 | 0x03; /* version 2, persistent, current */
755 stl_be_p(&outbuf
[24], 1); /* SCSI */
756 outbuf
[28] = 1; /* DBE = 1, mandatory */
757 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
758 stw_be_p(&outbuf
[32], 3);
759 outbuf
[34] = 0x08 | 0x03; /* version 2, persistent, current */
761 outbuf
[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
762 /* TODO: Random readable, CD read, DVD read, drive serial number,
767 static int scsi_emulate_mechanism_status(SCSIDiskState
*s
, uint8_t *outbuf
)
769 if (s
->qdev
.type
!= TYPE_ROM
) {
772 memset(outbuf
, 0, 8);
773 outbuf
[5] = 1; /* CD-ROM */
777 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
780 static const int mode_sense_valid
[0x3f] = {
781 [MODE_PAGE_HD_GEOMETRY
] = (1 << TYPE_DISK
),
782 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
] = (1 << TYPE_DISK
),
783 [MODE_PAGE_CACHING
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
784 [MODE_PAGE_R_W_ERROR
] = (1 << TYPE_DISK
) | (1 << TYPE_ROM
),
785 [MODE_PAGE_AUDIO_CTL
] = (1 << TYPE_ROM
),
786 [MODE_PAGE_CAPABILITIES
] = (1 << TYPE_ROM
),
789 BlockDriverState
*bdrv
= s
->qdev
.conf
.bs
;
790 int cylinders
, heads
, secs
;
791 uint8_t *p
= *p_outbuf
;
793 if ((mode_sense_valid
[page
] & (1 << s
->qdev
.type
)) == 0) {
800 * If Changeable Values are requested, a mask denoting those mode parameters
801 * that are changeable shall be returned. As we currently don't support
802 * parameter changes via MODE_SELECT all bits are returned set to zero.
803 * The buffer was already menset to zero by the caller of this function.
806 case MODE_PAGE_HD_GEOMETRY
:
808 if (page_control
== 1) { /* Changeable Values */
811 /* if a geometry hint is available, use it */
812 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
813 p
[2] = (cylinders
>> 16) & 0xff;
814 p
[3] = (cylinders
>> 8) & 0xff;
815 p
[4] = cylinders
& 0xff;
817 /* Write precomp start cylinder, disabled */
818 p
[6] = (cylinders
>> 16) & 0xff;
819 p
[7] = (cylinders
>> 8) & 0xff;
820 p
[8] = cylinders
& 0xff;
821 /* Reduced current start cylinder, disabled */
822 p
[9] = (cylinders
>> 16) & 0xff;
823 p
[10] = (cylinders
>> 8) & 0xff;
824 p
[11] = cylinders
& 0xff;
825 /* Device step rate [ns], 200ns */
828 /* Landing zone cylinder */
832 /* Medium rotation rate [rpm], 5400 rpm */
833 p
[20] = (5400 >> 8) & 0xff;
837 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY
:
839 if (page_control
== 1) { /* Changeable Values */
842 /* Transfer rate [kbit/s], 5Mbit/s */
845 /* if a geometry hint is available, use it */
846 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
849 p
[6] = s
->qdev
.blocksize
>> 8;
850 p
[8] = (cylinders
>> 8) & 0xff;
851 p
[9] = cylinders
& 0xff;
852 /* Write precomp start cylinder, disabled */
853 p
[10] = (cylinders
>> 8) & 0xff;
854 p
[11] = cylinders
& 0xff;
855 /* Reduced current start cylinder, disabled */
856 p
[12] = (cylinders
>> 8) & 0xff;
857 p
[13] = cylinders
& 0xff;
858 /* Device step rate [100us], 100us */
861 /* Device step pulse width [us], 1us */
863 /* Device head settle delay [100us], 100us */
866 /* Motor on delay [0.1s], 0.1s */
868 /* Motor off delay [0.1s], 0.1s */
870 /* Medium rotation rate [rpm], 5400 rpm */
871 p
[28] = (5400 >> 8) & 0xff;
875 case MODE_PAGE_CACHING
:
878 if (page_control
== 1) { /* Changeable Values */
881 if (bdrv_enable_write_cache(s
->qdev
.conf
.bs
)) {
886 case MODE_PAGE_R_W_ERROR
:
888 p
[2] = 0x80; /* Automatic Write Reallocation Enabled */
889 if (s
->qdev
.type
== TYPE_ROM
) {
890 p
[3] = 0x20; /* Read Retry Count */
894 case MODE_PAGE_AUDIO_CTL
:
898 case MODE_PAGE_CAPABILITIES
:
900 if (page_control
== 1) { /* Changeable Values */
904 p
[2] = 0x3b; /* CD-R & CD-RW read */
905 p
[3] = 0; /* Writing not supported */
906 p
[4] = 0x7f; /* Audio, composite, digital out,
907 mode 2 form 1&2, multi session */
908 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
909 RW corrected, C2 errors, ISRC,
911 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
912 /* Locking supported, jumper present, eject, tray */
913 p
[7] = 0; /* no volume & mute control, no
915 p
[8] = (50 * 176) >> 8; /* 50x read speed */
916 p
[9] = (50 * 176) & 0xff;
917 p
[10] = 2 >> 8; /* Two volume levels */
919 p
[12] = 2048 >> 8; /* 2M buffer */
921 p
[14] = (16 * 176) >> 8; /* 16x read speed current */
922 p
[15] = (16 * 176) & 0xff;
923 p
[18] = (16 * 176) >> 8; /* 16x write speed */
924 p
[19] = (16 * 176) & 0xff;
925 p
[20] = (16 * 176) >> 8; /* 16x write speed current */
926 p
[21] = (16 * 176) & 0xff;
933 *p_outbuf
+= p
[1] + 2;
937 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
939 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
941 int page
, dbd
, buflen
, ret
, page_control
;
943 uint8_t dev_specific_param
;
945 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
946 page
= r
->req
.cmd
.buf
[2] & 0x3f;
947 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
948 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
949 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
950 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
953 if (bdrv_is_read_only(s
->qdev
.conf
.bs
)) {
954 dev_specific_param
= 0x80; /* Readonly. */
956 dev_specific_param
= 0x00;
959 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
960 p
[1] = 0; /* Default media type. */
961 p
[2] = dev_specific_param
;
962 p
[3] = 0; /* Block descriptor length. */
964 } else { /* MODE_SENSE_10 */
965 p
[2] = 0; /* Default media type. */
966 p
[3] = dev_specific_param
;
967 p
[6] = p
[7] = 0; /* Block descriptor length. */
971 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
972 if (!dbd
&& nb_sectors
) {
973 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
974 outbuf
[3] = 8; /* Block descriptor length */
975 } else { /* MODE_SENSE_10 */
976 outbuf
[7] = 8; /* Block descriptor length */
978 nb_sectors
/= (s
->qdev
.blocksize
/ 512);
979 if (nb_sectors
> 0xffffff) {
982 p
[0] = 0; /* media density code */
983 p
[1] = (nb_sectors
>> 16) & 0xff;
984 p
[2] = (nb_sectors
>> 8) & 0xff;
985 p
[3] = nb_sectors
& 0xff;
986 p
[4] = 0; /* reserved */
987 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
988 p
[6] = s
->qdev
.blocksize
>> 8;
993 if (page_control
== 3) {
995 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
1000 for (page
= 0; page
<= 0x3e; page
++) {
1001 mode_sense_page(s
, page
, &p
, page_control
);
1004 ret
= mode_sense_page(s
, page
, &p
, page_control
);
1010 buflen
= p
- outbuf
;
1012 * The mode data length field specifies the length in bytes of the
1013 * following data that is available to be transferred. The mode data
1014 * length does not include itself.
1016 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
1017 outbuf
[0] = buflen
- 1;
1018 } else { /* MODE_SENSE_10 */
1019 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
1020 outbuf
[1] = (buflen
- 2) & 0xff;
1022 if (buflen
> r
->req
.cmd
.xfer
) {
1023 buflen
= r
->req
.cmd
.xfer
;
1028 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
1030 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1031 int start_track
, format
, msf
, toclen
;
1032 uint64_t nb_sectors
;
1034 msf
= req
->cmd
.buf
[1] & 2;
1035 format
= req
->cmd
.buf
[2] & 0xf;
1036 start_track
= req
->cmd
.buf
[6];
1037 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1038 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
1039 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1042 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
1045 /* multi session : only a single session defined */
1047 memset(outbuf
, 0, 12);
1053 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
1058 if (toclen
> req
->cmd
.xfer
) {
1059 toclen
= req
->cmd
.xfer
;
1064 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
1066 SCSIRequest
*req
= &r
->req
;
1067 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1068 bool start
= req
->cmd
.buf
[4] & 1;
1069 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
1071 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
1072 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1073 scsi_check_condition(r
,
1074 bdrv_is_inserted(s
->qdev
.conf
.bs
)
1075 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
1076 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
1079 bdrv_eject(s
->qdev
.conf
.bs
, !start
);
1080 s
->tray_open
= !start
;
1085 static int scsi_disk_emulate_command(SCSIDiskReq
*r
)
1087 SCSIRequest
*req
= &r
->req
;
1088 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1089 uint64_t nb_sectors
;
1093 if (!r
->iov
.iov_base
) {
1095 * FIXME: we shouldn't return anything bigger than 4k, but the code
1096 * requires the buffer to be as big as req->cmd.xfer in several
1097 * places. So, do not allow CDBs with a very large ALLOCATION
1098 * LENGTH. The real fix would be to modify scsi_read_data and
1099 * dma_buf_read, so that they return data beyond the buflen
1102 if (req
->cmd
.xfer
> 65536) {
1103 goto illegal_request
;
1105 r
->buflen
= MAX(4096, req
->cmd
.xfer
);
1106 r
->iov
.iov_base
= qemu_blockalign(s
->qdev
.conf
.bs
, r
->buflen
);
1109 outbuf
= r
->iov
.iov_base
;
1110 switch (req
->cmd
.buf
[0]) {
1111 case TEST_UNIT_READY
:
1112 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1117 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
1119 goto illegal_request
;
1124 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
1126 goto illegal_request
;
1130 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
1132 goto illegal_request
;
1136 if (req
->cmd
.buf
[1] & 1) {
1137 goto illegal_request
;
1141 if (req
->cmd
.buf
[1] & 3) {
1142 goto illegal_request
;
1146 if (req
->cmd
.buf
[1] & 1) {
1147 goto illegal_request
;
1151 if (req
->cmd
.buf
[1] & 3) {
1152 goto illegal_request
;
1156 if (scsi_disk_emulate_start_stop(r
) < 0) {
1160 case ALLOW_MEDIUM_REMOVAL
:
1161 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
1162 bdrv_lock_medium(s
->qdev
.conf
.bs
, req
->cmd
.buf
[4] & 1);
1164 case READ_CAPACITY_10
:
1165 /* The normal LEN field for this command is zero. */
1166 memset(outbuf
, 0, 8);
1167 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1171 if ((req
->cmd
.buf
[8] & 1) == 0 && req
->cmd
.lba
) {
1172 goto illegal_request
;
1174 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1175 /* Returned value is the address of the last sector. */
1177 /* Remember the new size for read/write sanity checking. */
1178 s
->max_lba
= nb_sectors
;
1179 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1180 if (nb_sectors
> UINT32_MAX
) {
1181 nb_sectors
= UINT32_MAX
;
1183 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
1184 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
1185 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
1186 outbuf
[3] = nb_sectors
& 0xff;
1189 outbuf
[6] = s
->qdev
.blocksize
>> 8;
1193 case MECHANISM_STATUS
:
1194 buflen
= scsi_emulate_mechanism_status(s
, outbuf
);
1196 goto illegal_request
;
1199 case GET_CONFIGURATION
:
1200 buflen
= scsi_get_configuration(s
, outbuf
);
1202 goto illegal_request
;
1205 case GET_EVENT_STATUS_NOTIFICATION
:
1206 buflen
= scsi_get_event_status_notification(s
, r
, outbuf
);
1208 goto illegal_request
;
1211 case READ_DVD_STRUCTURE
:
1212 buflen
= scsi_read_dvd_structure(s
, r
, outbuf
);
1214 goto illegal_request
;
1217 case SERVICE_ACTION_IN_16
:
1218 /* Service Action In subcommands. */
1219 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
1220 DPRINTF("SAI READ CAPACITY(16)\n");
1221 memset(outbuf
, 0, req
->cmd
.xfer
);
1222 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1226 if ((req
->cmd
.buf
[14] & 1) == 0 && req
->cmd
.lba
) {
1227 goto illegal_request
;
1229 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1230 /* Returned value is the address of the last sector. */
1232 /* Remember the new size for read/write sanity checking. */
1233 s
->max_lba
= nb_sectors
;
1234 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
1235 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
1236 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
1237 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
1238 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
1239 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
1240 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
1241 outbuf
[7] = nb_sectors
& 0xff;
1244 outbuf
[10] = s
->qdev
.blocksize
>> 8;
1247 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
1249 /* set TPE bit if the format supports discard */
1250 if (s
->qdev
.conf
.discard_granularity
) {
1254 /* Protection, exponent and lowest lba field left blank. */
1255 buflen
= req
->cmd
.xfer
;
1258 DPRINTF("Unsupported Service Action In\n");
1259 goto illegal_request
;
1263 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1269 if (s
->tray_open
|| !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1270 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
1272 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
1277 if (r
->req
.status
== -1) {
1278 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1283 /* Execute a scsi command. Returns the length of the data expected by the
1284 command. This will be Positive for data transfers from the device
1285 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1286 and zero if the command does not transfer any data. */
1288 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1290 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1291 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1297 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1302 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1303 printf(" 0x%02x", buf
[i
]);
1310 case TEST_UNIT_READY
:
1319 case ALLOW_MEDIUM_REMOVAL
:
1320 case READ_CAPACITY_10
:
1322 case READ_DVD_STRUCTURE
:
1323 case GET_CONFIGURATION
:
1324 case GET_EVENT_STATUS_NOTIFICATION
:
1325 case MECHANISM_STATUS
:
1326 case SERVICE_ACTION_IN_16
:
1328 rc
= scsi_disk_emulate_command(r
);
1333 r
->iov
.iov_len
= rc
;
1335 case SYNCHRONIZE_CACHE
:
1336 bdrv_acct_start(s
->qdev
.conf
.bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1337 r
->req
.aiocb
= bdrv_aio_flush(s
->qdev
.conf
.bs
, scsi_flush_complete
, r
);
1338 if (r
->req
.aiocb
== NULL
) {
1339 scsi_flush_complete(r
, -EIO
);
1346 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1347 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1348 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1351 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1352 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1358 case WRITE_VERIFY_10
:
1359 case WRITE_VERIFY_12
:
1360 case WRITE_VERIFY_16
:
1361 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1362 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1363 (command
& 0xe) == 0xe ? "And Verify " : "",
1364 r
->req
.cmd
.lba
, len
);
1365 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1368 r
->sector
= r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512);
1369 r
->sector_count
= len
* (s
->qdev
.blocksize
/ 512);
1372 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1373 /* We don't support mode parameter changes.
1374 Allow the mode parameter header + block descriptors only. */
1375 if (r
->req
.cmd
.xfer
> 12) {
1379 case MODE_SELECT_10
:
1380 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1381 /* We don't support mode parameter changes.
1382 Allow the mode parameter header + block descriptors only. */
1383 if (r
->req
.cmd
.xfer
> 16) {
1389 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1391 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1396 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1398 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1399 r
->req
.cmd
.lba
, len
);
1401 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1406 * We only support WRITE SAME with the unmap bit set for now.
1408 if (!(buf
[1] & 0x8)) {
1412 rc
= bdrv_discard(s
->qdev
.conf
.bs
,
1413 r
->req
.cmd
.lba
* (s
->qdev
.blocksize
/ 512),
1414 len
* (s
->qdev
.blocksize
/ 512));
1416 /* XXX: better error code ?*/
1424 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1425 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1428 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1431 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1434 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1435 scsi_req_complete(&r
->req
, GOOD
);
1437 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1438 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1441 if (!r
->sector_count
) {
1442 r
->sector_count
= -1;
1448 static void scsi_disk_reset(DeviceState
*dev
)
1450 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1451 uint64_t nb_sectors
;
1453 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1455 bdrv_get_geometry(s
->qdev
.conf
.bs
, &nb_sectors
);
1456 nb_sectors
/= s
->qdev
.blocksize
/ 512;
1460 s
->max_lba
= nb_sectors
;
1463 static void scsi_destroy(SCSIDevice
*dev
)
1465 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1467 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1468 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1471 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1473 SCSIDiskState
*s
= opaque
;
1476 * When a CD gets changed, we have to report an ejected state and
1477 * then a loaded state to guests so that they detect tray
1478 * open/close and media change events. Guests that do not use
1479 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1480 * states rely on this behavior.
1482 * media_changed governs the state machine used for unit attention
1483 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1485 s
->media_changed
= load
;
1486 s
->tray_open
= !load
;
1487 s
->qdev
.unit_attention
= SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM
);
1488 s
->media_event
= true;
1491 static bool scsi_cd_is_tray_open(void *opaque
)
1493 return ((SCSIDiskState
*)opaque
)->tray_open
;
1496 static bool scsi_cd_is_medium_locked(void *opaque
)
1498 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1501 static const BlockDevOps scsi_cd_block_ops
= {
1502 .change_media_cb
= scsi_cd_change_media_cb
,
1503 .is_tray_open
= scsi_cd_is_tray_open
,
1504 .is_medium_locked
= scsi_cd_is_medium_locked
,
1507 static void scsi_disk_unit_attention_reported(SCSIDevice
*dev
)
1509 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1510 if (s
->media_changed
) {
1511 s
->media_changed
= false;
1512 s
->qdev
.unit_attention
= SENSE_CODE(MEDIUM_CHANGED
);
1516 static int scsi_initfn(SCSIDevice
*dev
)
1518 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1521 if (!s
->qdev
.conf
.bs
) {
1522 error_report("scsi-disk: drive property not set");
1526 if (!s
->removable
&& !bdrv_is_inserted(s
->qdev
.conf
.bs
)) {
1527 error_report("Device needs media, but drive is empty");
1532 /* try to fall back to value set with legacy -drive serial=... */
1533 dinfo
= drive_get_by_blockdev(s
->qdev
.conf
.bs
);
1534 if (*dinfo
->serial
) {
1535 s
->serial
= g_strdup(dinfo
->serial
);
1540 s
->version
= g_strdup(QEMU_VERSION
);
1543 if (bdrv_is_sg(s
->qdev
.conf
.bs
)) {
1544 error_report("scsi-disk: unwanted /dev/sg*");
1549 bdrv_set_dev_ops(s
->qdev
.conf
.bs
, &scsi_cd_block_ops
, s
);
1551 bdrv_set_buffer_alignment(s
->qdev
.conf
.bs
, s
->qdev
.blocksize
);
1553 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1554 bdrv_iostatus_enable(s
->qdev
.conf
.bs
);
1555 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1559 static int scsi_hd_initfn(SCSIDevice
*dev
)
1561 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1562 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1563 s
->qdev
.type
= TYPE_DISK
;
1564 return scsi_initfn(&s
->qdev
);
1567 static int scsi_cd_initfn(SCSIDevice
*dev
)
1569 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1570 s
->qdev
.blocksize
= 2048;
1571 s
->qdev
.type
= TYPE_ROM
;
1572 s
->removable
= true;
1573 return scsi_initfn(&s
->qdev
);
1576 static int scsi_disk_initfn(SCSIDevice
*dev
)
1580 if (!dev
->conf
.bs
) {
1581 return scsi_initfn(dev
); /* ... and die there */
1584 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1585 if (dinfo
->media_cd
) {
1586 return scsi_cd_initfn(dev
);
1588 return scsi_hd_initfn(dev
);
1592 static SCSIReqOps scsi_disk_reqops
= {
1593 .size
= sizeof(SCSIDiskReq
),
1594 .free_req
= scsi_free_request
,
1595 .send_command
= scsi_send_command
,
1596 .read_data
= scsi_read_data
,
1597 .write_data
= scsi_write_data
,
1598 .cancel_io
= scsi_cancel_io
,
1599 .get_buf
= scsi_get_buf
,
1602 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1603 uint32_t lun
, void *hba_private
)
1605 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1608 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1612 #define DEFINE_SCSI_DISK_PROPERTIES() \
1613 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1614 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1615 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1617 static SCSIDeviceInfo scsi_disk_info
[] = {
1619 .qdev
.name
= "scsi-hd",
1620 .qdev
.fw_name
= "disk",
1621 .qdev
.desc
= "virtual SCSI disk",
1622 .qdev
.size
= sizeof(SCSIDiskState
),
1623 .qdev
.reset
= scsi_disk_reset
,
1624 .init
= scsi_hd_initfn
,
1625 .destroy
= scsi_destroy
,
1626 .alloc_req
= scsi_new_request
,
1627 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1628 .qdev
.props
= (Property
[]) {
1629 DEFINE_SCSI_DISK_PROPERTIES(),
1630 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1631 DEFINE_PROP_END_OF_LIST(),
1634 .qdev
.name
= "scsi-cd",
1635 .qdev
.fw_name
= "disk",
1636 .qdev
.desc
= "virtual SCSI CD-ROM",
1637 .qdev
.size
= sizeof(SCSIDiskState
),
1638 .qdev
.reset
= scsi_disk_reset
,
1639 .init
= scsi_cd_initfn
,
1640 .destroy
= scsi_destroy
,
1641 .alloc_req
= scsi_new_request
,
1642 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1643 .qdev
.props
= (Property
[]) {
1644 DEFINE_SCSI_DISK_PROPERTIES(),
1645 DEFINE_PROP_END_OF_LIST(),
1648 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1649 .qdev
.fw_name
= "disk",
1650 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1651 .qdev
.size
= sizeof(SCSIDiskState
),
1652 .qdev
.reset
= scsi_disk_reset
,
1653 .init
= scsi_disk_initfn
,
1654 .destroy
= scsi_destroy
,
1655 .alloc_req
= scsi_new_request
,
1656 .unit_attention_reported
= scsi_disk_unit_attention_reported
,
1657 .qdev
.props
= (Property
[]) {
1658 DEFINE_SCSI_DISK_PROPERTIES(),
1659 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1660 DEFINE_PROP_END_OF_LIST(),
1665 static void scsi_disk_register_devices(void)
1669 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1670 scsi_qdev_register(&scsi_disk_info
[i
]);
1673 device_init(scsi_disk_register_devices
)