4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "hw/ide/internal.h"
28 #include "hw/scsi/scsi.h"
29 #include "sysemu/block-backend.h"
32 #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
33 #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
35 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
37 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
40 for(i
= 0; i
< buf_size
; i
++) {
48 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
54 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
62 static inline int ube16_to_cpu(const uint8_t *buf
)
64 return (buf
[0] << 8) | buf
[1];
67 static inline int ube32_to_cpu(const uint8_t *buf
)
69 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
72 static void lba_to_msf(uint8_t *buf
, int lba
)
75 buf
[0] = (lba
/ 75) / 60;
76 buf
[1] = (lba
/ 75) % 60;
80 static inline int media_present(IDEState
*s
)
82 return !s
->tray_open
&& s
->nb_sectors
> 0;
85 /* XXX: DVDs that could fit on a CD will be reported as a CD */
86 static inline int media_is_dvd(IDEState
*s
)
88 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
91 static inline int media_is_cd(IDEState
*s
)
93 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
96 static void cd_data_to_raw(uint8_t *buf
, int lba
)
100 memset(buf
+ 1, 0xff, 10);
104 lba_to_msf(buf
, lba
);
105 buf
[3] = 0x01; /* mode 1 data */
109 /* XXX: ECC not computed */
114 cd_read_sector_sync(IDEState
*s
)
117 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
118 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
120 trace_cd_read_sector_sync(s
->lba
);
122 switch (s
->cd_sector_size
) {
124 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
125 s
->io_buffer
, ATAPI_SECTOR_SIZE
);
128 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
129 s
->io_buffer
+ 16, ATAPI_SECTOR_SIZE
);
131 cd_data_to_raw(s
->io_buffer
, s
->lba
);
135 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
140 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
142 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
144 s
->io_buffer_index
= 0;
150 static void cd_read_sector_cb(void *opaque
, int ret
)
152 IDEState
*s
= opaque
;
154 trace_cd_read_sector_cb(s
->lba
, ret
);
157 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
158 ide_atapi_io_error(s
, ret
);
162 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
164 if (s
->cd_sector_size
== 2352) {
165 cd_data_to_raw(s
->io_buffer
, s
->lba
);
169 s
->io_buffer_index
= 0;
170 s
->status
&= ~BUSY_STAT
;
172 ide_atapi_cmd_reply_end(s
);
175 static int cd_read_sector(IDEState
*s
)
179 if (s
->cd_sector_size
!= 2048 && s
->cd_sector_size
!= 2352) {
180 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
184 buf
= (s
->cd_sector_size
== 2352) ? s
->io_buffer
+ 16 : s
->io_buffer
;
185 qemu_iovec_init_buf(&s
->qiov
, buf
, ATAPI_SECTOR_SIZE
);
187 trace_cd_read_sector(s
->lba
);
189 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
190 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
192 ide_buffered_readv(s
, (int64_t)s
->lba
<< 2, &s
->qiov
, 4,
193 cd_read_sector_cb
, s
);
195 s
->status
|= BUSY_STAT
;
199 void ide_atapi_cmd_ok(IDEState
*s
)
202 s
->status
= READY_STAT
| SEEK_STAT
;
203 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
204 ide_transfer_stop(s
);
208 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
210 trace_ide_atapi_cmd_error(s
, sense_key
, asc
);
211 s
->error
= sense_key
<< 4;
212 s
->status
= READY_STAT
| ERR_STAT
;
213 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
214 s
->sense_key
= sense_key
;
216 ide_transfer_stop(s
);
220 void ide_atapi_io_error(IDEState
*s
, int ret
)
222 /* XXX: handle more errors */
223 if (ret
== -ENOMEDIUM
) {
224 ide_atapi_cmd_error(s
, NOT_READY
,
225 ASC_MEDIUM_NOT_PRESENT
);
227 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
228 ASC_LOGICAL_BLOCK_OOR
);
232 static uint16_t atapi_byte_count_limit(IDEState
*s
)
236 bcl
= s
->lcyl
| (s
->hcyl
<< 8);
243 /* The whole ATAPI transfer logic is handled in this function */
244 void ide_atapi_cmd_reply_end(IDEState
*s
)
246 int byte_count_limit
, size
, ret
;
247 while (s
->packet_transfer_size
> 0) {
248 trace_ide_atapi_cmd_reply_end(s
, s
->packet_transfer_size
,
249 s
->elementary_transfer_size
,
252 /* see if a new sector must be read */
253 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
254 if (!s
->elementary_transfer_size
) {
255 ret
= cd_read_sector(s
);
257 ide_atapi_io_error(s
, ret
);
261 /* rebuffering within an elementary transfer is
262 * only possible with a sync request because we
263 * end up with a race condition otherwise */
264 ret
= cd_read_sector_sync(s
);
266 ide_atapi_io_error(s
, ret
);
271 if (s
->elementary_transfer_size
> 0) {
272 /* there are some data left to transmit in this elementary
274 size
= s
->cd_sector_size
- s
->io_buffer_index
;
275 if (size
> s
->elementary_transfer_size
)
276 size
= s
->elementary_transfer_size
;
278 /* a new transfer is needed */
279 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
281 byte_count_limit
= atapi_byte_count_limit(s
);
282 trace_ide_atapi_cmd_reply_end_bcl(s
, byte_count_limit
);
283 size
= s
->packet_transfer_size
;
284 if (size
> byte_count_limit
) {
285 /* byte count limit must be even if this case */
286 if (byte_count_limit
& 1)
288 size
= byte_count_limit
;
292 s
->elementary_transfer_size
= size
;
293 /* we cannot transmit more than one sector at a time */
295 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
296 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
298 trace_ide_atapi_cmd_reply_end_new(s
, s
->status
);
300 s
->packet_transfer_size
-= size
;
301 s
->elementary_transfer_size
-= size
;
302 s
->io_buffer_index
+= size
;
304 /* Some adapters process PIO data right away. In that case, we need
305 * to avoid mutual recursion between ide_transfer_start
306 * and ide_atapi_cmd_reply_end.
308 if (!ide_transfer_start_norecurse(s
,
309 s
->io_buffer
+ s
->io_buffer_index
- size
,
310 size
, ide_atapi_cmd_reply_end
)) {
315 /* end of transfer */
316 trace_ide_atapi_cmd_reply_end_eot(s
, s
->status
);
321 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
322 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
326 s
->lba
= -1; /* no sector read */
327 s
->packet_transfer_size
= size
;
328 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
329 s
->elementary_transfer_size
= 0;
332 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, size
,
334 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
335 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
337 s
->status
= READY_STAT
| SEEK_STAT
;
338 s
->io_buffer_index
= 0;
339 ide_atapi_cmd_reply_end(s
);
343 /* start a CD-CDROM read command */
344 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
348 s
->packet_transfer_size
= nb_sectors
* sector_size
;
349 s
->elementary_transfer_size
= 0;
350 s
->io_buffer_index
= sector_size
;
351 s
->cd_sector_size
= sector_size
;
353 ide_atapi_cmd_reply_end(s
);
356 static void ide_atapi_cmd_check_status(IDEState
*s
)
358 trace_ide_atapi_cmd_check_status(s
);
359 s
->error
= MC_ERR
| (UNIT_ATTENTION
<< 4);
360 s
->status
= ERR_STAT
;
364 /* ATAPI DMA support */
366 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
368 IDEState
*s
= opaque
;
372 if (ide_handle_rw_error(s
, -ret
, ide_dma_cmd_to_retry(s
->dma_cmd
))) {
373 if (s
->bus
->error_status
) {
374 s
->bus
->dma
->aiocb
= NULL
;
381 if (s
->io_buffer_size
> 0) {
383 * For a cdrom read sector command (s->lba != -1),
384 * adjust the lba for the next s->io_buffer_size chunk
385 * and dma the current chunk.
386 * For a command != read (s->lba == -1), just transfer
390 if (s
->cd_sector_size
== 2352) {
392 cd_data_to_raw(s
->io_buffer
, s
->lba
);
394 n
= s
->io_buffer_size
>> 11;
398 s
->packet_transfer_size
-= s
->io_buffer_size
;
399 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
403 if (s
->packet_transfer_size
<= 0) {
404 s
->status
= READY_STAT
| SEEK_STAT
;
405 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
410 s
->io_buffer_index
= 0;
411 if (s
->cd_sector_size
== 2352) {
413 s
->io_buffer_size
= s
->cd_sector_size
;
416 n
= s
->packet_transfer_size
>> 11;
417 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
418 n
= (IDE_DMA_BUF_SECTORS
/ 4);
419 s
->io_buffer_size
= n
* 2048;
422 trace_ide_atapi_cmd_read_dma_cb_aio(s
, s
->lba
, n
);
423 qemu_iovec_init_buf(&s
->bus
->dma
->qiov
, s
->io_buffer
+ data_offset
,
424 n
* ATAPI_SECTOR_SIZE
);
426 s
->bus
->dma
->aiocb
= ide_buffered_readv(s
, (int64_t)s
->lba
<< 2,
427 &s
->bus
->dma
->qiov
, n
* 4,
428 ide_atapi_cmd_read_dma_cb
, s
);
433 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
435 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
437 ide_set_inactive(s
, false);
440 /* start a CD-CDROM read command with DMA */
441 /* XXX: test if DMA is available */
442 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
446 s
->packet_transfer_size
= nb_sectors
* sector_size
;
447 s
->io_buffer_size
= 0;
448 s
->cd_sector_size
= sector_size
;
450 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, s
->packet_transfer_size
,
453 /* XXX: check if BUSY_STAT should be set */
454 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
455 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
458 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
461 trace_ide_atapi_cmd_read(s
, s
->atapi_dma
? "dma" : "pio",
464 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
466 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
470 void ide_atapi_dma_restart(IDEState
*s
)
473 * At this point we can just re-evaluate the packet command and start over.
474 * The presence of ->dma_cb callback in the pre_save ensures that the packet
475 * command has been completely sent and we can safely restart command.
477 s
->unit
= s
->bus
->retry_unit
;
478 s
->bus
->dma
->ops
->restart_dma(s
->bus
->dma
);
482 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
485 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
487 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
488 cpu_to_ube16 (buf_profile
, profile
);
489 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
491 /* each profile adds 4 bytes to the response */
493 buf
[11] += 4; /* Additional Length */
498 static int ide_dvd_read_structure(IDEState
*s
, int format
,
499 const uint8_t *packet
, uint8_t *buf
)
502 case 0x0: /* Physical format information */
504 int layer
= packet
[6];
505 uint64_t total_sectors
;
508 return -ASC_INV_FIELD_IN_CMD_PACKET
;
510 total_sectors
= s
->nb_sectors
>> 2;
511 if (total_sectors
== 0) {
512 return -ASC_MEDIUM_NOT_PRESENT
;
515 buf
[4] = 1; /* DVD-ROM, part version 1 */
516 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
517 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
518 buf
[7] = 0; /* default densities */
520 /* FIXME: 0x30000 per spec? */
521 cpu_to_ube32(buf
+ 8, 0); /* start sector */
522 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
523 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
525 /* Size of buffer, not including 2 byte size field */
526 stw_be_p(buf
, 2048 + 2);
528 /* 2k data + 4 byte header */
532 case 0x01: /* DVD copyright information */
533 buf
[4] = 0; /* no copyright data */
534 buf
[5] = 0; /* no region restrictions */
536 /* Size of buffer, not including 2 byte size field */
537 stw_be_p(buf
, 4 + 2);
539 /* 4 byte header + 4 byte data */
542 case 0x03: /* BCA information - invalid field for no BCA info */
543 return -ASC_INV_FIELD_IN_CMD_PACKET
;
545 case 0x04: /* DVD disc manufacturing information */
546 /* Size of buffer, not including 2 byte size field */
547 stw_be_p(buf
, 2048 + 2);
549 /* 2k data + 4 byte header */
554 * This lists all the command capabilities above. Add new ones
555 * in order and update the length and buffer return values.
558 buf
[4] = 0x00; /* Physical format */
559 buf
[5] = 0x40; /* Not writable, is readable */
560 stw_be_p(buf
+ 6, 2048 + 4);
562 buf
[8] = 0x01; /* Copyright info */
563 buf
[9] = 0x40; /* Not writable, is readable */
564 stw_be_p(buf
+ 10, 4 + 4);
566 buf
[12] = 0x03; /* BCA info */
567 buf
[13] = 0x40; /* Not writable, is readable */
568 stw_be_p(buf
+ 14, 188 + 4);
570 buf
[16] = 0x04; /* Manufacturing info */
571 buf
[17] = 0x40; /* Not writable, is readable */
572 stw_be_p(buf
+ 18, 2048 + 4);
574 /* Size of buffer, not including 2 byte size field */
575 stw_be_p(buf
, 16 + 2);
577 /* data written + 4 byte header */
580 default: /* TODO: formats beyond DVD-ROM requires */
581 return -ASC_INV_FIELD_IN_CMD_PACKET
;
585 static unsigned int event_status_media(IDEState
*s
,
588 uint8_t event_code
, media_status
;
592 media_status
= MS_TRAY_OPEN
;
593 } else if (blk_is_inserted(s
->blk
)) {
594 media_status
= MS_MEDIA_PRESENT
;
597 /* Event notification descriptor */
598 event_code
= MEC_NO_CHANGE
;
599 if (media_status
!= MS_TRAY_OPEN
) {
600 if (s
->events
.new_media
) {
601 event_code
= MEC_NEW_MEDIA
;
602 s
->events
.new_media
= false;
603 } else if (s
->events
.eject_request
) {
604 event_code
= MEC_EJECT_REQUESTED
;
605 s
->events
.eject_request
= false;
610 buf
[5] = media_status
;
612 /* These fields are reserved, just clear them. */
616 return 8; /* We wrote to 4 extra bytes from the header */
620 * Before transferring data or otherwise signalling acceptance of a command
621 * marked CONDDATA, we must check the validity of the byte_count_limit.
623 static bool validate_bcl(IDEState
*s
)
625 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
626 if (s
->atapi_dma
|| atapi_byte_count_limit(s
)) {
630 /* TODO: Move abort back into core.c and introduce proper error flow between
631 * ATAPI layer and IDE core layer */
632 ide_abort_command(s
);
636 static void cmd_get_event_status_notification(IDEState
*s
,
639 const uint8_t *packet
= buf
;
643 uint8_t polled
; /* lsb bit is polled; others are reserved */
644 uint8_t reserved2
[2];
646 uint8_t reserved3
[2];
649 } QEMU_PACKED
*gesn_cdb
;
653 uint8_t notification_class
;
654 uint8_t supported_events
;
655 } QEMU_PACKED
*gesn_event_header
;
656 unsigned int max_len
, used_len
;
658 gesn_cdb
= (void *)packet
;
659 gesn_event_header
= (void *)buf
;
661 max_len
= be16_to_cpu(gesn_cdb
->len
);
663 /* It is fine by the MMC spec to not support async mode operations */
664 if (!(gesn_cdb
->polled
& 0x01)) { /* asynchronous mode */
665 /* Only polling is supported, asynchronous mode is not. */
666 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
667 ASC_INV_FIELD_IN_CMD_PACKET
);
671 /* polling mode operation */
674 * These are the supported events.
676 * We currently only support requests of the 'media' type.
677 * Notification class requests and supported event classes are bitmasks,
678 * but they are build from the same values as the "notification class"
681 gesn_event_header
->supported_events
= 1 << GESN_MEDIA
;
684 * We use |= below to set the class field; other bits in this byte
685 * are reserved now but this is useful to do if we have to use the
686 * reserved fields later.
688 gesn_event_header
->notification_class
= 0;
691 * Responses to requests are to be based on request priority. The
692 * notification_class_request_type enum above specifies the
693 * priority: upper elements are higher prio than lower ones.
695 if (gesn_cdb
->class & (1 << GESN_MEDIA
)) {
696 gesn_event_header
->notification_class
|= GESN_MEDIA
;
697 used_len
= event_status_media(s
, buf
);
699 gesn_event_header
->notification_class
= 0x80; /* No event available */
700 used_len
= sizeof(*gesn_event_header
);
702 gesn_event_header
->len
= cpu_to_be16(used_len
703 - sizeof(*gesn_event_header
));
704 ide_atapi_cmd_reply(s
, used_len
, max_len
);
707 static void cmd_request_sense(IDEState
*s
, uint8_t *buf
)
709 int max_len
= buf
[4];
712 buf
[0] = 0x70 | (1 << 7);
713 buf
[2] = s
->sense_key
;
717 if (s
->sense_key
== UNIT_ATTENTION
) {
718 s
->sense_key
= NO_SENSE
;
721 ide_atapi_cmd_reply(s
, 18, max_len
);
724 static void cmd_inquiry(IDEState
*s
, uint8_t *buf
)
726 uint8_t page_code
= buf
[2];
727 int max_len
= buf
[4];
731 unsigned preamble_len
;
733 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
734 * we are being asked for a specific page of info indicated by byte 2. */
739 buf
[idx
++] = 0x05; /* CD-ROM */
740 buf
[idx
++] = page_code
; /* Page Code */
741 buf
[idx
++] = 0x00; /* reserved */
742 idx
++; /* length (set later) */
746 /* Supported Pages: List of supported VPD responses. */
747 buf
[idx
++] = 0x00; /* 0x00: Supported Pages, and: */
748 buf
[idx
++] = 0x83; /* 0x83: Device Identification. */
752 /* Device Identification. Each entry is optional, but the entries
753 * included here are modeled after libata's VPD responses.
754 * If the response is given, at least one entry must be present. */
756 /* Entry 1: Serial */
757 if (idx
+ 24 > max_len
) {
758 /* Not enough room for even the first entry: */
759 /* 4 byte header + 20 byte string */
760 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
761 ASC_DATA_PHASE_ERROR
);
764 buf
[idx
++] = 0x02; /* Ascii */
765 buf
[idx
++] = 0x00; /* Vendor Specific */
767 buf
[idx
++] = 20; /* Remaining length */
768 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
771 /* Entry 2: Drive Model and Serial */
772 if (idx
+ 72 > max_len
) {
773 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
776 buf
[idx
++] = 0x02; /* Ascii */
777 buf
[idx
++] = 0x01; /* T10 Vendor */
780 padstr8(buf
+ idx
, 8, "ATA"); /* Generic T10 vendor */
782 padstr8(buf
+ idx
, 40, s
->drive_model_str
);
784 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
788 if (s
->wwn
&& (idx
+ 12 <= max_len
)) {
789 /* 4 byte header + 8 byte wwn */
790 buf
[idx
++] = 0x01; /* Binary */
791 buf
[idx
++] = 0x03; /* NAA */
794 stq_be_p(&buf
[idx
], s
->wwn
);
800 /* SPC-3, revision 23 sec. 6.4 */
801 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
802 ASC_INV_FIELD_IN_CMD_PACKET
);
809 buf
[0] = 0x05; /* CD-ROM */
810 buf
[1] = 0x80; /* removable */
811 buf
[2] = 0x00; /* ISO */
812 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
813 /* buf[size_idx] set below. */
814 buf
[5] = 0; /* reserved */
815 buf
[6] = 0; /* reserved */
816 buf
[7] = 0; /* reserved */
817 padstr8(buf
+ 8, 8, "QEMU");
818 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
819 padstr8(buf
+ 32, 4, s
->version
);
824 buf
[size_idx
] = idx
- preamble_len
;
825 ide_atapi_cmd_reply(s
, idx
, max_len
);
828 static void cmd_get_configuration(IDEState
*s
, uint8_t *buf
)
834 /* only feature 0 is supported */
835 if (buf
[2] != 0 || buf
[3] != 0) {
836 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
837 ASC_INV_FIELD_IN_CMD_PACKET
);
841 /* XXX: could result in alignment problems in some architectures */
842 max_len
= ube16_to_cpu(buf
+ 7);
845 * XXX: avoid overflow for io_buffer if max_len is bigger than
846 * the size of that buffer (dimensioned to max number of
847 * sectors to transfer at once)
849 * Only a problem if the feature/profiles grow.
852 /* XXX: assume 1 sector */
856 memset(buf
, 0, max_len
);
858 * the number of sectors from the media tells us which profile
859 * to use as current. 0 means there is no media
861 if (media_is_dvd(s
)) {
862 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
863 } else if (media_is_cd(s
)) {
864 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
867 buf
[10] = 0x02 | 0x01; /* persistent and current */
868 len
= 12; /* headers: 8 + 4 */
869 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
870 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
871 cpu_to_ube32(buf
, len
- 4); /* data length */
873 ide_atapi_cmd_reply(s
, len
, max_len
);
876 static void cmd_mode_sense(IDEState
*s
, uint8_t *buf
)
881 max_len
= ube16_to_cpu(buf
+ 7);
882 action
= buf
[2] >> 6;
883 code
= buf
[2] & 0x3f;
886 case 0: /* current values */
888 case MODE_PAGE_R_W_ERROR
: /* error recovery */
889 cpu_to_ube16(&buf
[0], 16 - 2);
897 buf
[8] = MODE_PAGE_R_W_ERROR
;
905 ide_atapi_cmd_reply(s
, 16, max_len
);
907 case MODE_PAGE_AUDIO_CTL
:
908 cpu_to_ube16(&buf
[0], 24 - 2);
916 buf
[8] = MODE_PAGE_AUDIO_CTL
;
918 /* Fill with CDROM audio volume */
924 ide_atapi_cmd_reply(s
, 24, max_len
);
926 case MODE_PAGE_CAPABILITIES
:
927 cpu_to_ube16(&buf
[0], 30 - 2);
935 buf
[8] = MODE_PAGE_CAPABILITIES
;
937 buf
[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
940 /* Claim PLAY_AUDIO capability (0x01) since some Linux
941 code checks for this to automount media. */
944 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
945 if (s
->tray_locked
) {
948 buf
[15] = 0x00; /* No volume & mute control, no changer */
949 cpu_to_ube16(&buf
[16], 704); /* 4x read speed */
950 buf
[18] = 0; /* Two volume levels */
952 cpu_to_ube16(&buf
[20], 512); /* 512k buffer */
953 cpu_to_ube16(&buf
[22], 704); /* 4x read speed current */
960 ide_atapi_cmd_reply(s
, 30, max_len
);
966 case 1: /* changeable values */
968 case 2: /* default values */
971 case 3: /* saved values */
972 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
973 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
979 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_INV_FIELD_IN_CMD_PACKET
);
982 static void cmd_test_unit_ready(IDEState
*s
, uint8_t *buf
)
984 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
985 * come here, we know that it's ready. */
989 static void cmd_prevent_allow_medium_removal(IDEState
*s
, uint8_t* buf
)
991 s
->tray_locked
= buf
[4] & 1;
992 blk_lock_medium(s
->blk
, buf
[4] & 1);
996 static void cmd_read(IDEState
*s
, uint8_t* buf
)
1000 if (buf
[0] == GPCMD_READ_10
) {
1001 nb_sectors
= ube16_to_cpu(buf
+ 7);
1003 nb_sectors
= ube32_to_cpu(buf
+ 6);
1006 lba
= ube32_to_cpu(buf
+ 2);
1007 if (nb_sectors
== 0) {
1008 ide_atapi_cmd_ok(s
);
1012 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1015 static void cmd_read_cd(IDEState
*s
, uint8_t* buf
)
1017 int nb_sectors
, lba
, transfer_request
;
1019 nb_sectors
= (buf
[6] << 16) | (buf
[7] << 8) | buf
[8];
1020 lba
= ube32_to_cpu(buf
+ 2);
1022 if (nb_sectors
== 0) {
1023 ide_atapi_cmd_ok(s
);
1027 transfer_request
= buf
[9] & 0xf8;
1028 if (transfer_request
== 0x00) {
1030 ide_atapi_cmd_ok(s
);
1034 /* Check validity of BCL before transferring data */
1035 if (!validate_bcl(s
)) {
1039 switch (transfer_request
) {
1042 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1046 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1049 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1050 ASC_INV_FIELD_IN_CMD_PACKET
);
1055 static void cmd_seek(IDEState
*s
, uint8_t* buf
)
1058 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1060 lba
= ube32_to_cpu(buf
+ 2);
1061 if (lba
>= total_sectors
) {
1062 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1066 ide_atapi_cmd_ok(s
);
1069 static void cmd_start_stop_unit(IDEState
*s
, uint8_t* buf
)
1072 bool start
= buf
[4] & 1;
1073 bool loej
= buf
[4] & 2; /* load on start, eject on !start */
1074 int pwrcnd
= buf
[4] & 0xf0;
1077 /* eject/load only happens for power condition == 0 */
1078 ide_atapi_cmd_ok(s
);
1083 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1084 sense
= blk_is_inserted(s
->blk
)
1085 ? NOT_READY
: ILLEGAL_REQUEST
;
1086 ide_atapi_cmd_error(s
, sense
, ASC_MEDIA_REMOVAL_PREVENTED
);
1090 if (s
->tray_open
!= !start
) {
1091 blk_eject(s
->blk
, !start
);
1092 s
->tray_open
= !start
;
1096 ide_atapi_cmd_ok(s
);
1099 static void cmd_mechanism_status(IDEState
*s
, uint8_t* buf
)
1101 int max_len
= ube16_to_cpu(buf
+ 8);
1103 cpu_to_ube16(buf
, 0);
1104 /* no current LBA */
1109 cpu_to_ube16(buf
+ 6, 0);
1110 ide_atapi_cmd_reply(s
, 8, max_len
);
1113 static void cmd_read_toc_pma_atip(IDEState
*s
, uint8_t* buf
)
1115 int format
, msf
, start_track
, len
;
1117 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1119 max_len
= ube16_to_cpu(buf
+ 7);
1120 format
= buf
[9] >> 6;
1121 msf
= (buf
[1] >> 1) & 1;
1122 start_track
= buf
[6];
1126 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1129 ide_atapi_cmd_reply(s
, len
, max_len
);
1132 /* multi session : only a single session defined */
1137 ide_atapi_cmd_reply(s
, 12, max_len
);
1140 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1143 ide_atapi_cmd_reply(s
, len
, max_len
);
1147 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1148 ASC_INV_FIELD_IN_CMD_PACKET
);
1152 static void cmd_read_cdvd_capacity(IDEState
*s
, uint8_t* buf
)
1154 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1156 /* NOTE: it is really the number of sectors minus 1 */
1157 cpu_to_ube32(buf
, total_sectors
- 1);
1158 cpu_to_ube32(buf
+ 4, 2048);
1159 ide_atapi_cmd_reply(s
, 8, 8);
1162 static void cmd_read_disc_information(IDEState
*s
, uint8_t* buf
)
1164 uint8_t type
= buf
[1] & 7;
1165 uint32_t max_len
= ube16_to_cpu(buf
+ 7);
1167 /* Types 1/2 are only defined for Blu-Ray. */
1169 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1170 ASC_INV_FIELD_IN_CMD_PACKET
);
1176 buf
[2] = 0xe; /* last session complete, disc finalized */
1177 buf
[3] = 1; /* first track on disc */
1178 buf
[4] = 1; /* # of sessions */
1179 buf
[5] = 1; /* first track of last session */
1180 buf
[6] = 1; /* last track of last session */
1181 buf
[7] = 0x20; /* unrestricted use */
1182 buf
[8] = 0x00; /* CD-ROM or DVD-ROM */
1183 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1184 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1185 /* 24-31: disc bar code */
1186 /* 32: disc application code */
1187 /* 33: number of OPC tables */
1189 ide_atapi_cmd_reply(s
, 34, max_len
);
1192 static void cmd_read_dvd_structure(IDEState
*s
, uint8_t* buf
)
1196 int format
= buf
[7];
1199 max_len
= ube16_to_cpu(buf
+ 8);
1201 if (format
< 0xff) {
1202 if (media_is_cd(s
)) {
1203 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1204 ASC_INCOMPATIBLE_FORMAT
);
1206 } else if (!media_present(s
)) {
1207 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1208 ASC_INV_FIELD_IN_CMD_PACKET
);
1213 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1214 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1220 ret
= ide_dvd_read_structure(s
, format
, buf
, buf
);
1223 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, -ret
);
1225 ide_atapi_cmd_reply(s
, ret
, max_len
);
1230 /* TODO: BD support, fall through for now */
1232 /* Generic disk structures */
1233 case 0x80: /* TODO: AACS volume identifier */
1234 case 0x81: /* TODO: AACS media serial number */
1235 case 0x82: /* TODO: AACS media identifier */
1236 case 0x83: /* TODO: AACS media key block */
1237 case 0x90: /* TODO: List of recognized format layers */
1238 case 0xc0: /* TODO: Write protection status */
1240 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1241 ASC_INV_FIELD_IN_CMD_PACKET
);
1246 static void cmd_set_speed(IDEState
*s
, uint8_t* buf
)
1248 ide_atapi_cmd_ok(s
);
1253 * Only commands flagged as ALLOW_UA are allowed to run under a
1254 * unit attention condition. (See MMC-5, section 4.1.6.1)
1259 * Commands flagged with CHECK_READY can only execute if a medium is present.
1260 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1266 * Commands flagged with NONDATA do not in any circumstances return
1267 * any data via ide_atapi_cmd_reply. These commands are exempt from
1268 * the normal byte_count_limit constraints.
1269 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1274 * CONDDATA implies a command that transfers data only conditionally based
1275 * on the presence of suboptions. It should be exempt from the BCL check at
1276 * command validation time, but it needs to be checked at the command
1277 * handler level instead.
1282 static const struct AtapiCmd
{
1283 void (*handler
)(IDEState
*s
, uint8_t *buf
);
1285 } atapi_cmd_table
[0x100] = {
1286 [ 0x00 ] = { cmd_test_unit_ready
, CHECK_READY
| NONDATA
},
1287 [ 0x03 ] = { cmd_request_sense
, ALLOW_UA
},
1288 [ 0x12 ] = { cmd_inquiry
, ALLOW_UA
},
1289 [ 0x1b ] = { cmd_start_stop_unit
, NONDATA
}, /* [1] */
1290 [ 0x1e ] = { cmd_prevent_allow_medium_removal
, NONDATA
},
1291 [ 0x25 ] = { cmd_read_cdvd_capacity
, CHECK_READY
},
1292 [ 0x28 ] = { cmd_read
, /* (10) */ CHECK_READY
},
1293 [ 0x2b ] = { cmd_seek
, CHECK_READY
| NONDATA
},
1294 [ 0x43 ] = { cmd_read_toc_pma_atip
, CHECK_READY
},
1295 [ 0x46 ] = { cmd_get_configuration
, ALLOW_UA
},
1296 [ 0x4a ] = { cmd_get_event_status_notification
, ALLOW_UA
},
1297 [ 0x51 ] = { cmd_read_disc_information
, CHECK_READY
},
1298 [ 0x5a ] = { cmd_mode_sense
, /* (10) */ 0 },
1299 [ 0xa8 ] = { cmd_read
, /* (12) */ CHECK_READY
},
1300 [ 0xad ] = { cmd_read_dvd_structure
, CHECK_READY
},
1301 [ 0xbb ] = { cmd_set_speed
, NONDATA
},
1302 [ 0xbd ] = { cmd_mechanism_status
, 0 },
1303 [ 0xbe ] = { cmd_read_cd
, CHECK_READY
| CONDDATA
},
1304 /* [1] handler detects and reports not ready condition itself */
1307 void ide_atapi_cmd(IDEState
*s
)
1309 uint8_t *buf
= s
->io_buffer
;
1310 const struct AtapiCmd
*cmd
= &atapi_cmd_table
[s
->io_buffer
[0]];
1312 trace_ide_atapi_cmd(s
, s
->io_buffer
[0]);
1314 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET
)) {
1315 /* Each pretty-printed byte needs two bytes and a space; */
1316 char *ppacket
= g_malloc(ATAPI_PACKET_SIZE
* 3 + 1);
1318 for (i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1319 sprintf(ppacket
+ (i
* 3), "%02x ", buf
[i
]);
1321 trace_ide_atapi_cmd_packet(s
, s
->lcyl
| (s
->hcyl
<< 8), ppacket
);
1326 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1327 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1328 * condition response unless a higher priority status, defined by the drive
1331 if (s
->sense_key
== UNIT_ATTENTION
&& !(cmd
->flags
& ALLOW_UA
)) {
1332 ide_atapi_cmd_check_status(s
);
1336 * When a CD gets changed, we have to report an ejected state and
1337 * then a loaded state to guests so that they detect tray
1338 * open/close and media change events. Guests that do not use
1339 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1340 * states rely on this behavior.
1342 if (!(cmd
->flags
& ALLOW_UA
) &&
1343 !s
->tray_open
&& blk_is_inserted(s
->blk
) && s
->cdrom_changed
) {
1345 if (s
->cdrom_changed
== 1) {
1346 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1347 s
->cdrom_changed
= 2;
1349 ide_atapi_cmd_error(s
, UNIT_ATTENTION
, ASC_MEDIUM_MAY_HAVE_CHANGED
);
1350 s
->cdrom_changed
= 0;
1356 /* Report a Not Ready condition if appropriate for the command */
1357 if ((cmd
->flags
& CHECK_READY
) &&
1358 (!media_present(s
) || !blk_is_inserted(s
->blk
)))
1360 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1364 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1365 * If this is a data-transferring PIO command and BCL is 0,
1366 * we abort at the /ATA/ level, not the ATAPI level.
1367 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1368 if (cmd
->handler
&& !(cmd
->flags
& (NONDATA
| CONDDATA
))) {
1369 if (!validate_bcl(s
)) {
1374 /* Execute the command */
1376 cmd
->handler(s
, buf
);
1380 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_ILLEGAL_OPCODE
);