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 "hw/ide/internal.h"
27 #include "hw/scsi/scsi.h"
28 #include "sysemu/block-backend.h"
30 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
32 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
35 for(i
= 0; i
< buf_size
; i
++) {
43 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
49 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
57 static inline int ube16_to_cpu(const uint8_t *buf
)
59 return (buf
[0] << 8) | buf
[1];
62 static inline int ube32_to_cpu(const uint8_t *buf
)
64 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
67 static void lba_to_msf(uint8_t *buf
, int lba
)
70 buf
[0] = (lba
/ 75) / 60;
71 buf
[1] = (lba
/ 75) % 60;
75 static inline int media_present(IDEState
*s
)
77 return !s
->tray_open
&& s
->nb_sectors
> 0;
80 /* XXX: DVDs that could fit on a CD will be reported as a CD */
81 static inline int media_is_dvd(IDEState
*s
)
83 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
86 static inline int media_is_cd(IDEState
*s
)
88 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
91 static void cd_data_to_raw(uint8_t *buf
, int lba
)
95 memset(buf
+ 1, 0xff, 10);
100 buf
[3] = 0x01; /* mode 1 data */
104 /* XXX: ECC not computed */
108 static int cd_read_sector(IDEState
*s
, int lba
, uint8_t *buf
, int sector_size
)
112 switch(sector_size
) {
114 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
115 4 * BDRV_SECTOR_SIZE
, BLOCK_ACCT_READ
);
116 ret
= blk_read(s
->blk
, (int64_t)lba
<< 2, buf
, 4);
117 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
120 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
121 4 * BDRV_SECTOR_SIZE
, BLOCK_ACCT_READ
);
122 ret
= blk_read(s
->blk
, (int64_t)lba
<< 2, buf
+ 16, 4);
123 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
126 cd_data_to_raw(buf
, lba
);
135 void ide_atapi_cmd_ok(IDEState
*s
)
138 s
->status
= READY_STAT
| SEEK_STAT
;
139 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
140 ide_transfer_stop(s
);
144 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
146 #ifdef DEBUG_IDE_ATAPI
147 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
149 s
->error
= sense_key
<< 4;
150 s
->status
= READY_STAT
| ERR_STAT
;
151 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
152 s
->sense_key
= sense_key
;
154 ide_transfer_stop(s
);
158 void ide_atapi_io_error(IDEState
*s
, int ret
)
160 /* XXX: handle more errors */
161 if (ret
== -ENOMEDIUM
) {
162 ide_atapi_cmd_error(s
, NOT_READY
,
163 ASC_MEDIUM_NOT_PRESENT
);
165 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
166 ASC_LOGICAL_BLOCK_OOR
);
170 /* The whole ATAPI transfer logic is handled in this function */
171 void ide_atapi_cmd_reply_end(IDEState
*s
)
173 int byte_count_limit
, size
, ret
;
174 #ifdef DEBUG_IDE_ATAPI
175 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
176 s
->packet_transfer_size
,
177 s
->elementary_transfer_size
,
180 if (s
->packet_transfer_size
<= 0) {
181 /* end of transfer */
184 #ifdef DEBUG_IDE_ATAPI
185 printf("status=0x%x\n", s
->status
);
188 /* see if a new sector must be read */
189 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
190 ret
= cd_read_sector(s
, s
->lba
, s
->io_buffer
, s
->cd_sector_size
);
192 ide_atapi_io_error(s
, ret
);
196 s
->io_buffer_index
= 0;
198 if (s
->elementary_transfer_size
> 0) {
199 /* there are some data left to transmit in this elementary
201 size
= s
->cd_sector_size
- s
->io_buffer_index
;
202 if (size
> s
->elementary_transfer_size
)
203 size
= s
->elementary_transfer_size
;
204 s
->packet_transfer_size
-= size
;
205 s
->elementary_transfer_size
-= size
;
206 s
->io_buffer_index
+= size
;
207 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
208 size
, ide_atapi_cmd_reply_end
);
210 /* a new transfer is needed */
211 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
212 byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
213 #ifdef DEBUG_IDE_ATAPI
214 printf("byte_count_limit=%d\n", byte_count_limit
);
216 if (byte_count_limit
== 0xffff)
218 size
= s
->packet_transfer_size
;
219 if (size
> byte_count_limit
) {
220 /* byte count limit must be even if this case */
221 if (byte_count_limit
& 1)
223 size
= byte_count_limit
;
227 s
->elementary_transfer_size
= size
;
228 /* we cannot transmit more than one sector at a time */
230 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
231 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
233 s
->packet_transfer_size
-= size
;
234 s
->elementary_transfer_size
-= size
;
235 s
->io_buffer_index
+= size
;
236 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
237 size
, ide_atapi_cmd_reply_end
);
239 #ifdef DEBUG_IDE_ATAPI
240 printf("status=0x%x\n", s
->status
);
246 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
247 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
251 s
->lba
= -1; /* no sector read */
252 s
->packet_transfer_size
= size
;
253 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
254 s
->elementary_transfer_size
= 0;
257 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, size
,
259 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
260 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
262 s
->status
= READY_STAT
| SEEK_STAT
;
263 s
->io_buffer_index
= 0;
264 ide_atapi_cmd_reply_end(s
);
268 /* start a CD-CDROM read command */
269 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
273 s
->packet_transfer_size
= nb_sectors
* sector_size
;
274 s
->elementary_transfer_size
= 0;
275 s
->io_buffer_index
= sector_size
;
276 s
->cd_sector_size
= sector_size
;
278 s
->status
= READY_STAT
| SEEK_STAT
;
279 ide_atapi_cmd_reply_end(s
);
282 static void ide_atapi_cmd_check_status(IDEState
*s
)
284 #ifdef DEBUG_IDE_ATAPI
285 printf("atapi_cmd_check_status\n");
287 s
->error
= MC_ERR
| (UNIT_ATTENTION
<< 4);
288 s
->status
= ERR_STAT
;
292 /* ATAPI DMA support */
294 /* XXX: handle read errors */
295 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
297 IDEState
*s
= opaque
;
301 ide_atapi_io_error(s
, ret
);
305 if (s
->io_buffer_size
> 0) {
307 * For a cdrom read sector command (s->lba != -1),
308 * adjust the lba for the next s->io_buffer_size chunk
309 * and dma the current chunk.
310 * For a command != read (s->lba == -1), just transfer
314 if (s
->cd_sector_size
== 2352) {
316 cd_data_to_raw(s
->io_buffer
, s
->lba
);
318 n
= s
->io_buffer_size
>> 11;
322 s
->packet_transfer_size
-= s
->io_buffer_size
;
323 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
327 if (s
->packet_transfer_size
<= 0) {
328 s
->status
= READY_STAT
| SEEK_STAT
;
329 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
334 s
->io_buffer_index
= 0;
335 if (s
->cd_sector_size
== 2352) {
337 s
->io_buffer_size
= s
->cd_sector_size
;
340 n
= s
->packet_transfer_size
>> 11;
341 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
342 n
= (IDE_DMA_BUF_SECTORS
/ 4);
343 s
->io_buffer_size
= n
* 2048;
347 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
350 s
->bus
->dma
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
351 s
->bus
->dma
->iov
.iov_len
= n
* 4 * 512;
352 qemu_iovec_init_external(&s
->bus
->dma
->qiov
, &s
->bus
->dma
->iov
, 1);
354 s
->bus
->dma
->aiocb
= blk_aio_readv(s
->blk
, (int64_t)s
->lba
<< 2,
355 &s
->bus
->dma
->qiov
, n
* 4,
356 ide_atapi_cmd_read_dma_cb
, s
);
360 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
361 ide_set_inactive(s
, false);
364 /* start a CD-CDROM read command with DMA */
365 /* XXX: test if DMA is available */
366 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
370 s
->packet_transfer_size
= nb_sectors
* sector_size
;
371 s
->io_buffer_size
= 0;
372 s
->cd_sector_size
= sector_size
;
374 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, s
->packet_transfer_size
,
377 /* XXX: check if BUSY_STAT should be set */
378 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
379 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
382 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
385 #ifdef DEBUG_IDE_ATAPI
386 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
390 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
392 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
397 /* Called by *_restart_bh when the transfer function points
400 void ide_atapi_dma_restart(IDEState
*s
)
403 * I'm not sure we have enough stored to restart the command
404 * safely, so give the guest an error it should recover from.
405 * I'm assuming most guests will try to recover from something
406 * listed as a medium error on a CD; it seems to work on Linux.
407 * This would be more of a problem if we did any other type of
410 ide_atapi_cmd_error(s
, MEDIUM_ERROR
, ASC_NO_SEEK_COMPLETE
);
413 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
416 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
418 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
419 cpu_to_ube16 (buf_profile
, profile
);
420 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
422 /* each profile adds 4 bytes to the response */
424 buf
[11] += 4; /* Additional Length */
429 static int ide_dvd_read_structure(IDEState
*s
, int format
,
430 const uint8_t *packet
, uint8_t *buf
)
433 case 0x0: /* Physical format information */
435 int layer
= packet
[6];
436 uint64_t total_sectors
;
439 return -ASC_INV_FIELD_IN_CMD_PACKET
;
441 total_sectors
= s
->nb_sectors
>> 2;
442 if (total_sectors
== 0) {
443 return -ASC_MEDIUM_NOT_PRESENT
;
446 buf
[4] = 1; /* DVD-ROM, part version 1 */
447 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
448 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
449 buf
[7] = 0; /* default densities */
451 /* FIXME: 0x30000 per spec? */
452 cpu_to_ube32(buf
+ 8, 0); /* start sector */
453 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
454 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
456 /* Size of buffer, not including 2 byte size field */
457 stw_be_p(buf
, 2048 + 2);
459 /* 2k data + 4 byte header */
463 case 0x01: /* DVD copyright information */
464 buf
[4] = 0; /* no copyright data */
465 buf
[5] = 0; /* no region restrictions */
467 /* Size of buffer, not including 2 byte size field */
468 stw_be_p(buf
, 4 + 2);
470 /* 4 byte header + 4 byte data */
473 case 0x03: /* BCA information - invalid field for no BCA info */
474 return -ASC_INV_FIELD_IN_CMD_PACKET
;
476 case 0x04: /* DVD disc manufacturing information */
477 /* Size of buffer, not including 2 byte size field */
478 stw_be_p(buf
, 2048 + 2);
480 /* 2k data + 4 byte header */
485 * This lists all the command capabilities above. Add new ones
486 * in order and update the length and buffer return values.
489 buf
[4] = 0x00; /* Physical format */
490 buf
[5] = 0x40; /* Not writable, is readable */
491 stw_be_p(buf
+ 6, 2048 + 4);
493 buf
[8] = 0x01; /* Copyright info */
494 buf
[9] = 0x40; /* Not writable, is readable */
495 stw_be_p(buf
+ 10, 4 + 4);
497 buf
[12] = 0x03; /* BCA info */
498 buf
[13] = 0x40; /* Not writable, is readable */
499 stw_be_p(buf
+ 14, 188 + 4);
501 buf
[16] = 0x04; /* Manufacturing info */
502 buf
[17] = 0x40; /* Not writable, is readable */
503 stw_be_p(buf
+ 18, 2048 + 4);
505 /* Size of buffer, not including 2 byte size field */
506 stw_be_p(buf
, 16 + 2);
508 /* data written + 4 byte header */
511 default: /* TODO: formats beyond DVD-ROM requires */
512 return -ASC_INV_FIELD_IN_CMD_PACKET
;
516 static unsigned int event_status_media(IDEState
*s
,
519 uint8_t event_code
, media_status
;
523 media_status
= MS_TRAY_OPEN
;
524 } else if (blk_is_inserted(s
->blk
)) {
525 media_status
= MS_MEDIA_PRESENT
;
528 /* Event notification descriptor */
529 event_code
= MEC_NO_CHANGE
;
530 if (media_status
!= MS_TRAY_OPEN
) {
531 if (s
->events
.new_media
) {
532 event_code
= MEC_NEW_MEDIA
;
533 s
->events
.new_media
= false;
534 } else if (s
->events
.eject_request
) {
535 event_code
= MEC_EJECT_REQUESTED
;
536 s
->events
.eject_request
= false;
541 buf
[5] = media_status
;
543 /* These fields are reserved, just clear them. */
547 return 8; /* We wrote to 4 extra bytes from the header */
550 static void cmd_get_event_status_notification(IDEState
*s
,
553 const uint8_t *packet
= buf
;
557 uint8_t polled
; /* lsb bit is polled; others are reserved */
558 uint8_t reserved2
[2];
560 uint8_t reserved3
[2];
563 } QEMU_PACKED
*gesn_cdb
;
567 uint8_t notification_class
;
568 uint8_t supported_events
;
569 } QEMU_PACKED
*gesn_event_header
;
570 unsigned int max_len
, used_len
;
572 gesn_cdb
= (void *)packet
;
573 gesn_event_header
= (void *)buf
;
575 max_len
= be16_to_cpu(gesn_cdb
->len
);
577 /* It is fine by the MMC spec to not support async mode operations */
578 if (!(gesn_cdb
->polled
& 0x01)) { /* asynchronous mode */
579 /* Only polling is supported, asynchronous mode is not. */
580 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
581 ASC_INV_FIELD_IN_CMD_PACKET
);
585 /* polling mode operation */
588 * These are the supported events.
590 * We currently only support requests of the 'media' type.
591 * Notification class requests and supported event classes are bitmasks,
592 * but they are build from the same values as the "notification class"
595 gesn_event_header
->supported_events
= 1 << GESN_MEDIA
;
598 * We use |= below to set the class field; other bits in this byte
599 * are reserved now but this is useful to do if we have to use the
600 * reserved fields later.
602 gesn_event_header
->notification_class
= 0;
605 * Responses to requests are to be based on request priority. The
606 * notification_class_request_type enum above specifies the
607 * priority: upper elements are higher prio than lower ones.
609 if (gesn_cdb
->class & (1 << GESN_MEDIA
)) {
610 gesn_event_header
->notification_class
|= GESN_MEDIA
;
611 used_len
= event_status_media(s
, buf
);
613 gesn_event_header
->notification_class
= 0x80; /* No event available */
614 used_len
= sizeof(*gesn_event_header
);
616 gesn_event_header
->len
= cpu_to_be16(used_len
617 - sizeof(*gesn_event_header
));
618 ide_atapi_cmd_reply(s
, used_len
, max_len
);
621 static void cmd_request_sense(IDEState
*s
, uint8_t *buf
)
623 int max_len
= buf
[4];
626 buf
[0] = 0x70 | (1 << 7);
627 buf
[2] = s
->sense_key
;
631 if (s
->sense_key
== UNIT_ATTENTION
) {
632 s
->sense_key
= NO_SENSE
;
635 ide_atapi_cmd_reply(s
, 18, max_len
);
638 static void cmd_inquiry(IDEState
*s
, uint8_t *buf
)
640 uint8_t page_code
= buf
[2];
641 int max_len
= buf
[4];
645 unsigned preamble_len
;
647 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
648 * we are being asked for a specific page of info indicated by byte 2. */
653 buf
[idx
++] = 0x05; /* CD-ROM */
654 buf
[idx
++] = page_code
; /* Page Code */
655 buf
[idx
++] = 0x00; /* reserved */
656 idx
++; /* length (set later) */
660 /* Supported Pages: List of supported VPD responses. */
661 buf
[idx
++] = 0x00; /* 0x00: Supported Pages, and: */
662 buf
[idx
++] = 0x83; /* 0x83: Device Identification. */
666 /* Device Identification. Each entry is optional, but the entries
667 * included here are modeled after libata's VPD responses.
668 * If the response is given, at least one entry must be present. */
670 /* Entry 1: Serial */
671 if (idx
+ 24 > max_len
) {
672 /* Not enough room for even the first entry: */
673 /* 4 byte header + 20 byte string */
674 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
675 ASC_DATA_PHASE_ERROR
);
678 buf
[idx
++] = 0x02; /* Ascii */
679 buf
[idx
++] = 0x00; /* Vendor Specific */
681 buf
[idx
++] = 20; /* Remaining length */
682 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
685 /* Entry 2: Drive Model and Serial */
686 if (idx
+ 72 > max_len
) {
687 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
690 buf
[idx
++] = 0x02; /* Ascii */
691 buf
[idx
++] = 0x01; /* T10 Vendor */
694 padstr8(buf
+ idx
, 8, "ATA"); /* Generic T10 vendor */
696 padstr8(buf
+ idx
, 40, s
->drive_model_str
);
698 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
702 if (s
->wwn
&& (idx
+ 12 <= max_len
)) {
703 /* 4 byte header + 8 byte wwn */
704 buf
[idx
++] = 0x01; /* Binary */
705 buf
[idx
++] = 0x03; /* NAA */
708 stq_be_p(&buf
[idx
], s
->wwn
);
714 /* SPC-3, revision 23 sec. 6.4 */
715 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
716 ASC_INV_FIELD_IN_CMD_PACKET
);
723 buf
[0] = 0x05; /* CD-ROM */
724 buf
[1] = 0x80; /* removable */
725 buf
[2] = 0x00; /* ISO */
726 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
727 /* buf[size_idx] set below. */
728 buf
[5] = 0; /* reserved */
729 buf
[6] = 0; /* reserved */
730 buf
[7] = 0; /* reserved */
731 padstr8(buf
+ 8, 8, "QEMU");
732 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
733 padstr8(buf
+ 32, 4, s
->version
);
738 buf
[size_idx
] = idx
- preamble_len
;
739 ide_atapi_cmd_reply(s
, idx
, max_len
);
743 static void cmd_get_configuration(IDEState
*s
, uint8_t *buf
)
749 /* only feature 0 is supported */
750 if (buf
[2] != 0 || buf
[3] != 0) {
751 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
752 ASC_INV_FIELD_IN_CMD_PACKET
);
756 /* XXX: could result in alignment problems in some architectures */
757 max_len
= ube16_to_cpu(buf
+ 7);
760 * XXX: avoid overflow for io_buffer if max_len is bigger than
761 * the size of that buffer (dimensioned to max number of
762 * sectors to transfer at once)
764 * Only a problem if the feature/profiles grow.
767 /* XXX: assume 1 sector */
771 memset(buf
, 0, max_len
);
773 * the number of sectors from the media tells us which profile
774 * to use as current. 0 means there is no media
776 if (media_is_dvd(s
)) {
777 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
778 } else if (media_is_cd(s
)) {
779 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
782 buf
[10] = 0x02 | 0x01; /* persistent and current */
783 len
= 12; /* headers: 8 + 4 */
784 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
785 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
786 cpu_to_ube32(buf
, len
- 4); /* data length */
788 ide_atapi_cmd_reply(s
, len
, max_len
);
791 static void cmd_mode_sense(IDEState
*s
, uint8_t *buf
)
796 max_len
= ube16_to_cpu(buf
+ 7);
797 action
= buf
[2] >> 6;
798 code
= buf
[2] & 0x3f;
801 case 0: /* current values */
803 case MODE_PAGE_R_W_ERROR
: /* error recovery */
804 cpu_to_ube16(&buf
[0], 16 - 2);
812 buf
[8] = MODE_PAGE_R_W_ERROR
;
820 ide_atapi_cmd_reply(s
, 16, max_len
);
822 case MODE_PAGE_AUDIO_CTL
:
823 cpu_to_ube16(&buf
[0], 24 - 2);
831 buf
[8] = MODE_PAGE_AUDIO_CTL
;
833 /* Fill with CDROM audio volume */
839 ide_atapi_cmd_reply(s
, 24, max_len
);
841 case MODE_PAGE_CAPABILITIES
:
842 cpu_to_ube16(&buf
[0], 30 - 2);
850 buf
[8] = MODE_PAGE_CAPABILITIES
;
852 buf
[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
855 /* Claim PLAY_AUDIO capability (0x01) since some Linux
856 code checks for this to automount media. */
859 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
860 if (s
->tray_locked
) {
863 buf
[15] = 0x00; /* No volume & mute control, no changer */
864 cpu_to_ube16(&buf
[16], 704); /* 4x read speed */
865 buf
[18] = 0; /* Two volume levels */
867 cpu_to_ube16(&buf
[20], 512); /* 512k buffer */
868 cpu_to_ube16(&buf
[22], 704); /* 4x read speed current */
875 ide_atapi_cmd_reply(s
, 30, max_len
);
881 case 1: /* changeable values */
883 case 2: /* default values */
886 case 3: /* saved values */
887 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
888 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
894 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_INV_FIELD_IN_CMD_PACKET
);
897 static void cmd_test_unit_ready(IDEState
*s
, uint8_t *buf
)
899 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
900 * come here, we know that it's ready. */
904 static void cmd_prevent_allow_medium_removal(IDEState
*s
, uint8_t* buf
)
906 s
->tray_locked
= buf
[4] & 1;
907 blk_lock_medium(s
->blk
, buf
[4] & 1);
911 static void cmd_read(IDEState
*s
, uint8_t* buf
)
915 if (buf
[0] == GPCMD_READ_10
) {
916 nb_sectors
= ube16_to_cpu(buf
+ 7);
918 nb_sectors
= ube32_to_cpu(buf
+ 6);
921 lba
= ube32_to_cpu(buf
+ 2);
922 if (nb_sectors
== 0) {
927 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
930 static void cmd_read_cd(IDEState
*s
, uint8_t* buf
)
932 int nb_sectors
, lba
, transfer_request
;
934 nb_sectors
= (buf
[6] << 16) | (buf
[7] << 8) | buf
[8];
935 lba
= ube32_to_cpu(buf
+ 2);
937 if (nb_sectors
== 0) {
942 transfer_request
= buf
[9];
943 switch(transfer_request
& 0xf8) {
950 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
954 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
957 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
958 ASC_INV_FIELD_IN_CMD_PACKET
);
963 static void cmd_seek(IDEState
*s
, uint8_t* buf
)
966 uint64_t total_sectors
= s
->nb_sectors
>> 2;
968 lba
= ube32_to_cpu(buf
+ 2);
969 if (lba
>= total_sectors
) {
970 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
977 static void cmd_start_stop_unit(IDEState
*s
, uint8_t* buf
)
980 bool start
= buf
[4] & 1;
981 bool loej
= buf
[4] & 2; /* load on start, eject on !start */
982 int pwrcnd
= buf
[4] & 0xf0;
985 /* eject/load only happens for power condition == 0 */
991 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
992 sense
= blk_is_inserted(s
->blk
)
993 ? NOT_READY
: ILLEGAL_REQUEST
;
994 ide_atapi_cmd_error(s
, sense
, ASC_MEDIA_REMOVAL_PREVENTED
);
998 if (s
->tray_open
!= !start
) {
999 blk_eject(s
->blk
, !start
);
1000 s
->tray_open
= !start
;
1004 ide_atapi_cmd_ok(s
);
1007 static void cmd_mechanism_status(IDEState
*s
, uint8_t* buf
)
1009 int max_len
= ube16_to_cpu(buf
+ 8);
1011 cpu_to_ube16(buf
, 0);
1012 /* no current LBA */
1017 cpu_to_ube16(buf
+ 6, 0);
1018 ide_atapi_cmd_reply(s
, 8, max_len
);
1021 static void cmd_read_toc_pma_atip(IDEState
*s
, uint8_t* buf
)
1023 int format
, msf
, start_track
, len
;
1025 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1027 max_len
= ube16_to_cpu(buf
+ 7);
1028 format
= buf
[9] >> 6;
1029 msf
= (buf
[1] >> 1) & 1;
1030 start_track
= buf
[6];
1034 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1037 ide_atapi_cmd_reply(s
, len
, max_len
);
1040 /* multi session : only a single session defined */
1045 ide_atapi_cmd_reply(s
, 12, max_len
);
1048 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1051 ide_atapi_cmd_reply(s
, len
, max_len
);
1055 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1056 ASC_INV_FIELD_IN_CMD_PACKET
);
1060 static void cmd_read_cdvd_capacity(IDEState
*s
, uint8_t* buf
)
1062 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1064 /* NOTE: it is really the number of sectors minus 1 */
1065 cpu_to_ube32(buf
, total_sectors
- 1);
1066 cpu_to_ube32(buf
+ 4, 2048);
1067 ide_atapi_cmd_reply(s
, 8, 8);
1070 static void cmd_read_disc_information(IDEState
*s
, uint8_t* buf
)
1072 uint8_t type
= buf
[1] & 7;
1073 uint32_t max_len
= ube16_to_cpu(buf
+ 7);
1075 /* Types 1/2 are only defined for Blu-Ray. */
1077 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1078 ASC_INV_FIELD_IN_CMD_PACKET
);
1084 buf
[2] = 0xe; /* last session complete, disc finalized */
1085 buf
[3] = 1; /* first track on disc */
1086 buf
[4] = 1; /* # of sessions */
1087 buf
[5] = 1; /* first track of last session */
1088 buf
[6] = 1; /* last track of last session */
1089 buf
[7] = 0x20; /* unrestricted use */
1090 buf
[8] = 0x00; /* CD-ROM or DVD-ROM */
1091 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1092 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1093 /* 24-31: disc bar code */
1094 /* 32: disc application code */
1095 /* 33: number of OPC tables */
1097 ide_atapi_cmd_reply(s
, 34, max_len
);
1100 static void cmd_read_dvd_structure(IDEState
*s
, uint8_t* buf
)
1104 int format
= buf
[7];
1107 max_len
= ube16_to_cpu(buf
+ 8);
1109 if (format
< 0xff) {
1110 if (media_is_cd(s
)) {
1111 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1112 ASC_INCOMPATIBLE_FORMAT
);
1114 } else if (!media_present(s
)) {
1115 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1116 ASC_INV_FIELD_IN_CMD_PACKET
);
1121 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1122 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1128 ret
= ide_dvd_read_structure(s
, format
, buf
, buf
);
1131 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, -ret
);
1133 ide_atapi_cmd_reply(s
, ret
, max_len
);
1138 /* TODO: BD support, fall through for now */
1140 /* Generic disk structures */
1141 case 0x80: /* TODO: AACS volume identifier */
1142 case 0x81: /* TODO: AACS media serial number */
1143 case 0x82: /* TODO: AACS media identifier */
1144 case 0x83: /* TODO: AACS media key block */
1145 case 0x90: /* TODO: List of recognized format layers */
1146 case 0xc0: /* TODO: Write protection status */
1148 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1149 ASC_INV_FIELD_IN_CMD_PACKET
);
1154 static void cmd_set_speed(IDEState
*s
, uint8_t* buf
)
1156 ide_atapi_cmd_ok(s
);
1161 * Only commands flagged as ALLOW_UA are allowed to run under a
1162 * unit attention condition. (See MMC-5, section 4.1.6.1)
1167 * Commands flagged with CHECK_READY can only execute if a medium is present.
1168 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1174 * Commands flagged with NONDATA do not in any circumstances return
1175 * any data via ide_atapi_cmd_reply. These commands are exempt from
1176 * the normal byte_count_limit constraints.
1177 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1182 static const struct {
1183 void (*handler
)(IDEState
*s
, uint8_t *buf
);
1185 } atapi_cmd_table
[0x100] = {
1186 [ 0x00 ] = { cmd_test_unit_ready
, CHECK_READY
| NONDATA
},
1187 [ 0x03 ] = { cmd_request_sense
, ALLOW_UA
},
1188 [ 0x12 ] = { cmd_inquiry
, ALLOW_UA
},
1189 [ 0x1b ] = { cmd_start_stop_unit
, NONDATA
}, /* [1] */
1190 [ 0x1e ] = { cmd_prevent_allow_medium_removal
, NONDATA
},
1191 [ 0x25 ] = { cmd_read_cdvd_capacity
, CHECK_READY
},
1192 [ 0x28 ] = { cmd_read
, /* (10) */ CHECK_READY
},
1193 [ 0x2b ] = { cmd_seek
, CHECK_READY
| NONDATA
},
1194 [ 0x43 ] = { cmd_read_toc_pma_atip
, CHECK_READY
},
1195 [ 0x46 ] = { cmd_get_configuration
, ALLOW_UA
},
1196 [ 0x4a ] = { cmd_get_event_status_notification
, ALLOW_UA
},
1197 [ 0x51 ] = { cmd_read_disc_information
, CHECK_READY
},
1198 [ 0x5a ] = { cmd_mode_sense
, /* (10) */ 0 },
1199 [ 0xa8 ] = { cmd_read
, /* (12) */ CHECK_READY
},
1200 [ 0xad ] = { cmd_read_dvd_structure
, CHECK_READY
},
1201 [ 0xbb ] = { cmd_set_speed
, NONDATA
},
1202 [ 0xbd ] = { cmd_mechanism_status
, 0 },
1203 [ 0xbe ] = { cmd_read_cd
, CHECK_READY
},
1204 /* [1] handler detects and reports not ready condition itself */
1207 void ide_atapi_cmd(IDEState
*s
)
1212 #ifdef DEBUG_IDE_ATAPI
1215 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1216 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1217 printf(" %02x", buf
[i
]);
1223 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1224 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1225 * condition response unless a higher priority status, defined by the drive
1228 if (s
->sense_key
== UNIT_ATTENTION
&&
1229 !(atapi_cmd_table
[s
->io_buffer
[0]].flags
& ALLOW_UA
)) {
1230 ide_atapi_cmd_check_status(s
);
1234 * When a CD gets changed, we have to report an ejected state and
1235 * then a loaded state to guests so that they detect tray
1236 * open/close and media change events. Guests that do not use
1237 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1238 * states rely on this behavior.
1240 if (!(atapi_cmd_table
[s
->io_buffer
[0]].flags
& ALLOW_UA
) &&
1241 !s
->tray_open
&& blk_is_inserted(s
->blk
) && s
->cdrom_changed
) {
1243 if (s
->cdrom_changed
== 1) {
1244 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1245 s
->cdrom_changed
= 2;
1247 ide_atapi_cmd_error(s
, UNIT_ATTENTION
, ASC_MEDIUM_MAY_HAVE_CHANGED
);
1248 s
->cdrom_changed
= 0;
1254 /* Report a Not Ready condition if appropriate for the command */
1255 if ((atapi_cmd_table
[s
->io_buffer
[0]].flags
& CHECK_READY
) &&
1256 (!media_present(s
) || !blk_is_inserted(s
->blk
)))
1258 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1262 /* Nondata commands permit the byte_count_limit to be 0.
1263 * If this is a data-transferring PIO command and BCL is 0,
1264 * we abort at the /ATA/ level, not the ATAPI level.
1265 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1266 if (!(atapi_cmd_table
[s
->io_buffer
[0]].flags
& NONDATA
)) {
1267 /* TODO: Check IDENTIFY data word 125 for default BCL (currently 0) */
1268 uint16_t byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
1269 if (!(byte_count_limit
|| s
->atapi_dma
)) {
1270 /* TODO: Move abort back into core.c and make static inline again */
1271 ide_abort_command(s
);
1276 /* Execute the command */
1277 if (atapi_cmd_table
[s
->io_buffer
[0]].handler
) {
1278 atapi_cmd_table
[s
->io_buffer
[0]].handler(s
, buf
);
1282 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_ILLEGAL_OPCODE
);