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"
29 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
31 static void padstr8(uint8_t *buf, int buf_size, const char *src)
34 for(i = 0; i < buf_size; i++) {
42 static inline void cpu_to_ube16(uint8_t *buf, int val)
48 static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
56 static inline int ube16_to_cpu(const uint8_t *buf)
58 return (buf[0] << 8) | buf[1];
61 static inline int ube32_to_cpu(const uint8_t *buf)
63 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
66 static void lba_to_msf(uint8_t *buf, int lba)
69 buf[0] = (lba / 75) / 60;
70 buf[1] = (lba / 75) % 60;
74 static inline int media_present(IDEState *s)
76 return !s->tray_open && s->nb_sectors > 0;
79 /* XXX: DVDs that could fit on a CD will be reported as a CD */
80 static inline int media_is_dvd(IDEState *s)
82 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
85 static inline int media_is_cd(IDEState *s)
87 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
90 static void cd_data_to_raw(uint8_t *buf, int lba)
94 memset(buf + 1, 0xff, 10);
99 buf[3] = 0x01; /* mode 1 data */
103 /* XXX: ECC not computed */
107 static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
111 switch(sector_size) {
113 bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
114 ret = bdrv_read(s->bs, (int64_t)lba << 2, buf, 4);
115 bdrv_acct_done(s->bs, &s->acct);
118 bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
119 ret = bdrv_read(s->bs, (int64_t)lba << 2, buf + 16, 4);
120 bdrv_acct_done(s->bs, &s->acct);
123 cd_data_to_raw(buf, lba);
132 void ide_atapi_cmd_ok(IDEState *s)
135 s->status = READY_STAT | SEEK_STAT;
136 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
140 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
142 #ifdef DEBUG_IDE_ATAPI
143 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
145 s->error = sense_key << 4;
146 s->status = READY_STAT | ERR_STAT;
147 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
148 s->sense_key = sense_key;
153 void ide_atapi_io_error(IDEState *s, int ret)
155 /* XXX: handle more errors */
156 if (ret == -ENOMEDIUM) {
157 ide_atapi_cmd_error(s, SENSE_NOT_READY,
158 ASC_MEDIUM_NOT_PRESENT);
160 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
161 ASC_LOGICAL_BLOCK_OOR);
165 /* The whole ATAPI transfer logic is handled in this function */
166 void ide_atapi_cmd_reply_end(IDEState *s)
168 int byte_count_limit, size, ret;
169 #ifdef DEBUG_IDE_ATAPI
170 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
171 s->packet_transfer_size,
172 s->elementary_transfer_size,
175 if (s->packet_transfer_size <= 0) {
176 /* end of transfer */
177 ide_transfer_stop(s);
178 s->status = READY_STAT | SEEK_STAT;
179 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
181 #ifdef DEBUG_IDE_ATAPI
182 printf("status=0x%x\n", s->status);
185 /* see if a new sector must be read */
186 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
187 ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
189 ide_transfer_stop(s);
190 ide_atapi_io_error(s, ret);
194 s->io_buffer_index = 0;
196 if (s->elementary_transfer_size > 0) {
197 /* there are some data left to transmit in this elementary
199 size = s->cd_sector_size - s->io_buffer_index;
200 if (size > s->elementary_transfer_size)
201 size = s->elementary_transfer_size;
202 s->packet_transfer_size -= size;
203 s->elementary_transfer_size -= size;
204 s->io_buffer_index += size;
205 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
206 size, ide_atapi_cmd_reply_end);
208 /* a new transfer is needed */
209 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
210 byte_count_limit = s->lcyl | (s->hcyl << 8);
211 #ifdef DEBUG_IDE_ATAPI
212 printf("byte_count_limit=%d\n", byte_count_limit);
214 if (byte_count_limit == 0xffff)
216 size = s->packet_transfer_size;
217 if (size > byte_count_limit) {
218 /* byte count limit must be even if this case */
219 if (byte_count_limit & 1)
221 size = byte_count_limit;
225 s->elementary_transfer_size = size;
226 /* we cannot transmit more than one sector at a time */
228 if (size > (s->cd_sector_size - s->io_buffer_index))
229 size = (s->cd_sector_size - s->io_buffer_index);
231 s->packet_transfer_size -= size;
232 s->elementary_transfer_size -= size;
233 s->io_buffer_index += size;
234 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
235 size, ide_atapi_cmd_reply_end);
237 #ifdef DEBUG_IDE_ATAPI
238 printf("status=0x%x\n", s->status);
244 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
245 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
249 s->lba = -1; /* no sector read */
250 s->packet_transfer_size = size;
251 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
252 s->elementary_transfer_size = 0;
253 s->io_buffer_index = 0;
256 bdrv_acct_start(s->bs, &s->acct, size, BDRV_ACCT_READ);
257 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
258 s->bus->dma->ops->start_dma(s->bus->dma, s,
259 ide_atapi_cmd_read_dma_cb);
261 s->status = READY_STAT | SEEK_STAT;
262 ide_atapi_cmd_reply_end(s);
266 /* start a CD-CDROM read command */
267 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
271 s->packet_transfer_size = nb_sectors * sector_size;
272 s->elementary_transfer_size = 0;
273 s->io_buffer_index = sector_size;
274 s->cd_sector_size = sector_size;
276 s->status = READY_STAT | SEEK_STAT;
277 ide_atapi_cmd_reply_end(s);
280 static void ide_atapi_cmd_check_status(IDEState *s)
282 #ifdef DEBUG_IDE_ATAPI
283 printf("atapi_cmd_check_status\n");
285 s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
286 s->status = ERR_STAT;
290 /* ATAPI DMA support */
292 /* XXX: handle read errors */
293 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
295 IDEState *s = opaque;
299 ide_atapi_io_error(s, ret);
303 if (s->io_buffer_size > 0) {
305 * For a cdrom read sector command (s->lba != -1),
306 * adjust the lba for the next s->io_buffer_size chunk
307 * and dma the current chunk.
308 * For a command != read (s->lba == -1), just transfer
312 if (s->cd_sector_size == 2352) {
314 cd_data_to_raw(s->io_buffer, s->lba);
316 n = s->io_buffer_size >> 11;
320 s->packet_transfer_size -= s->io_buffer_size;
321 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
325 if (s->packet_transfer_size <= 0) {
326 s->status = READY_STAT | SEEK_STAT;
327 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
332 s->io_buffer_index = 0;
333 if (s->cd_sector_size == 2352) {
335 s->io_buffer_size = s->cd_sector_size;
338 n = s->packet_transfer_size >> 11;
339 if (n > (IDE_DMA_BUF_SECTORS / 4))
340 n = (IDE_DMA_BUF_SECTORS / 4);
341 s->io_buffer_size = n * 2048;
345 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
348 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
349 s->bus->dma->iov.iov_len = n * 4 * 512;
350 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
352 s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
353 &s->bus->dma->qiov, n * 4,
354 ide_atapi_cmd_read_dma_cb, s);
355 if (!s->bus->dma->aiocb) {
356 /* Note: media not present is the most likely case */
357 ide_atapi_cmd_error(s, SENSE_NOT_READY,
358 ASC_MEDIUM_NOT_PRESENT);
364 bdrv_acct_done(s->bs, &s->acct);
365 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
369 /* start a CD-CDROM read command with DMA */
370 /* XXX: test if DMA is available */
371 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
375 s->packet_transfer_size = nb_sectors * sector_size;
376 s->io_buffer_index = 0;
377 s->io_buffer_size = 0;
378 s->cd_sector_size = sector_size;
380 bdrv_acct_start(s->bs, &s->acct, s->packet_transfer_size, BDRV_ACCT_READ);
382 /* XXX: check if BUSY_STAT should be set */
383 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
384 s->bus->dma->ops->start_dma(s->bus->dma, s,
385 ide_atapi_cmd_read_dma_cb);
388 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
391 #ifdef DEBUG_IDE_ATAPI
392 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
396 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
398 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
402 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
405 uint8_t *buf_profile = buf + 12; /* start of profiles */
407 buf_profile += ((*index) * 4); /* start of indexed profile */
408 cpu_to_ube16 (buf_profile, profile);
409 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
411 /* each profile adds 4 bytes to the response */
413 buf[11] += 4; /* Additional Length */
418 static int ide_dvd_read_structure(IDEState *s, int format,
419 const uint8_t *packet, uint8_t *buf)
422 case 0x0: /* Physical format information */
424 int layer = packet[6];
425 uint64_t total_sectors;
428 return -ASC_INV_FIELD_IN_CMD_PACKET;
430 total_sectors = s->nb_sectors >> 2;
431 if (total_sectors == 0) {
432 return -ASC_MEDIUM_NOT_PRESENT;
435 buf[4] = 1; /* DVD-ROM, part version 1 */
436 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
437 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
438 buf[7] = 0; /* default densities */
440 /* FIXME: 0x30000 per spec? */
441 cpu_to_ube32(buf + 8, 0); /* start sector */
442 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
443 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
445 /* Size of buffer, not including 2 byte size field */
446 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
448 /* 2k data + 4 byte header */
452 case 0x01: /* DVD copyright information */
453 buf[4] = 0; /* no copyright data */
454 buf[5] = 0; /* no region restrictions */
456 /* Size of buffer, not including 2 byte size field */
457 cpu_to_be16wu((uint16_t *)buf, 4 + 2);
459 /* 4 byte header + 4 byte data */
462 case 0x03: /* BCA information - invalid field for no BCA info */
463 return -ASC_INV_FIELD_IN_CMD_PACKET;
465 case 0x04: /* DVD disc manufacturing information */
466 /* Size of buffer, not including 2 byte size field */
467 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
469 /* 2k data + 4 byte header */
474 * This lists all the command capabilities above. Add new ones
475 * in order and update the length and buffer return values.
478 buf[4] = 0x00; /* Physical format */
479 buf[5] = 0x40; /* Not writable, is readable */
480 cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
482 buf[8] = 0x01; /* Copyright info */
483 buf[9] = 0x40; /* Not writable, is readable */
484 cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
486 buf[12] = 0x03; /* BCA info */
487 buf[13] = 0x40; /* Not writable, is readable */
488 cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
490 buf[16] = 0x04; /* Manufacturing info */
491 buf[17] = 0x40; /* Not writable, is readable */
492 cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
494 /* Size of buffer, not including 2 byte size field */
495 cpu_to_be16wu((uint16_t *)buf, 16 + 2);
497 /* data written + 4 byte header */
500 default: /* TODO: formats beyond DVD-ROM requires */
501 return -ASC_INV_FIELD_IN_CMD_PACKET;
505 static unsigned int event_status_media(IDEState *s,
508 enum media_event_code {
509 MEC_NO_CHANGE = 0, /* Status unchanged */
510 MEC_EJECT_REQUESTED, /* received a request from user to eject */
511 MEC_NEW_MEDIA, /* new media inserted and ready for access */
512 MEC_MEDIA_REMOVAL, /* only for media changers */
513 MEC_MEDIA_CHANGED, /* only for media changers */
514 MEC_BG_FORMAT_COMPLETED, /* MRW or DVD+RW b/g format completed */
515 MEC_BG_FORMAT_RESTARTED, /* MRW or DVD+RW b/g format restarted */
519 MS_MEDIA_PRESENT = 2,
521 uint8_t event_code, media_status;
525 media_status = MS_TRAY_OPEN;
526 } else if (bdrv_is_inserted(s->bs)) {
527 media_status = MS_MEDIA_PRESENT;
530 /* Event notification descriptor */
531 event_code = MEC_NO_CHANGE;
532 if (media_status != MS_TRAY_OPEN && s->events.new_media) {
533 event_code = MEC_NEW_MEDIA;
534 s->events.new_media = false;
538 buf[5] = media_status;
540 /* These fields are reserved, just clear them. */
544 return 8; /* We wrote to 4 extra bytes from the header */
547 static void cmd_get_event_status_notification(IDEState *s,
550 const uint8_t *packet = buf;
554 uint8_t polled; /* lsb bit is polled; others are reserved */
555 uint8_t reserved2[2];
557 uint8_t reserved3[2];
560 } QEMU_PACKED *gesn_cdb;
564 uint8_t notification_class;
565 uint8_t supported_events;
566 } QEMU_PACKED *gesn_event_header;
568 enum notification_class_request_type {
569 NCR_RESERVED1 = 1 << 0,
570 NCR_OPERATIONAL_CHANGE = 1 << 1,
571 NCR_POWER_MANAGEMENT = 1 << 2,
572 NCR_EXTERNAL_REQUEST = 1 << 3,
574 NCR_MULTI_HOST = 1 << 5,
575 NCR_DEVICE_BUSY = 1 << 6,
576 NCR_RESERVED2 = 1 << 7,
578 enum event_notification_class_field {
580 ENC_OPERATIONAL_CHANGE,
581 ENC_POWER_MANAGEMENT,
582 ENC_EXTERNAL_REQUEST,
588 unsigned int max_len, used_len;
590 gesn_cdb = (void *)packet;
591 gesn_event_header = (void *)buf;
593 max_len = be16_to_cpu(gesn_cdb->len);
595 /* It is fine by the MMC spec to not support async mode operations */
596 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
597 /* Only polling is supported, asynchronous mode is not. */
598 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
599 ASC_INV_FIELD_IN_CMD_PACKET);
603 /* polling mode operation */
606 * These are the supported events.
608 * We currently only support requests of the 'media' type.
610 gesn_event_header->supported_events = NCR_MEDIA;
613 * We use |= below to set the class field; other bits in this byte
614 * are reserved now but this is useful to do if we have to use the
615 * reserved fields later.
617 gesn_event_header->notification_class = 0;
620 * Responses to requests are to be based on request priority. The
621 * notification_class_request_type enum above specifies the
622 * priority: upper elements are higher prio than lower ones.
624 if (gesn_cdb->class & NCR_MEDIA) {
625 gesn_event_header->notification_class |= ENC_MEDIA;
626 used_len = event_status_media(s, buf);
628 gesn_event_header->notification_class = 0x80; /* No event available */
629 used_len = sizeof(*gesn_event_header);
631 gesn_event_header->len = cpu_to_be16(used_len
632 - sizeof(*gesn_event_header));
633 ide_atapi_cmd_reply(s, used_len, max_len);
636 static void cmd_request_sense(IDEState *s, uint8_t *buf)
638 int max_len = buf[4];
641 buf[0] = 0x70 | (1 << 7);
642 buf[2] = s->sense_key;
646 if (s->sense_key == SENSE_UNIT_ATTENTION) {
647 s->sense_key = SENSE_NONE;
650 ide_atapi_cmd_reply(s, 18, max_len);
653 static void cmd_inquiry(IDEState *s, uint8_t *buf)
655 int max_len = buf[4];
657 buf[0] = 0x05; /* CD-ROM */
658 buf[1] = 0x80; /* removable */
659 buf[2] = 0x00; /* ISO */
660 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
661 buf[4] = 31; /* additional length */
662 buf[5] = 0; /* reserved */
663 buf[6] = 0; /* reserved */
664 buf[7] = 0; /* reserved */
665 padstr8(buf + 8, 8, "QEMU");
666 padstr8(buf + 16, 16, "QEMU DVD-ROM");
667 padstr8(buf + 32, 4, s->version);
668 ide_atapi_cmd_reply(s, 36, max_len);
671 static void cmd_get_configuration(IDEState *s, uint8_t *buf)
677 /* only feature 0 is supported */
678 if (buf[2] != 0 || buf[3] != 0) {
679 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
680 ASC_INV_FIELD_IN_CMD_PACKET);
684 /* XXX: could result in alignment problems in some architectures */
685 max_len = ube16_to_cpu(buf + 7);
688 * XXX: avoid overflow for io_buffer if max_len is bigger than
689 * the size of that buffer (dimensioned to max number of
690 * sectors to transfer at once)
692 * Only a problem if the feature/profiles grow.
695 /* XXX: assume 1 sector */
699 memset(buf, 0, max_len);
701 * the number of sectors from the media tells us which profile
702 * to use as current. 0 means there is no media
704 if (media_is_dvd(s)) {
705 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
706 } else if (media_is_cd(s)) {
707 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
710 buf[10] = 0x02 | 0x01; /* persistent and current */
711 len = 12; /* headers: 8 + 4 */
712 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
713 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
714 cpu_to_ube32(buf, len - 4); /* data length */
716 ide_atapi_cmd_reply(s, len, max_len);
719 static void cmd_mode_sense(IDEState *s, uint8_t *buf)
724 if (buf[0] == GPCMD_MODE_SENSE_10) {
725 max_len = ube16_to_cpu(buf + 7);
730 action = buf[2] >> 6;
731 code = buf[2] & 0x3f;
734 case 0: /* current values */
736 case GPMODE_R_W_ERROR_PAGE: /* error recovery */
737 cpu_to_ube16(&buf[0], 16 + 6);
753 ide_atapi_cmd_reply(s, 16, max_len);
755 case GPMODE_AUDIO_CTL_PAGE:
756 cpu_to_ube16(&buf[0], 24 + 6);
764 /* Fill with CDROM audio volume */
770 ide_atapi_cmd_reply(s, 24, max_len);
772 case GPMODE_CAPABILITIES_PAGE:
773 cpu_to_ube16(&buf[0], 28 + 6);
786 /* Claim PLAY_AUDIO capability (0x01) since some Linux
787 code checks for this to automount media. */
790 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
791 if (s->tray_locked) {
795 cpu_to_ube16(&buf[16], 706);
798 cpu_to_ube16(&buf[20], 512);
799 cpu_to_ube16(&buf[22], 706);
804 ide_atapi_cmd_reply(s, 28, max_len);
810 case 1: /* changeable values */
812 case 2: /* default values */
815 case 3: /* saved values */
816 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
817 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
823 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
826 static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
828 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
829 * come here, we know that it's ready. */
833 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
835 s->tray_locked = buf[4] & 1;
836 bdrv_set_locked(s->bs, buf[4] & 1);
840 static void cmd_read(IDEState *s, uint8_t* buf)
844 if (buf[0] == GPCMD_READ_10) {
845 nb_sectors = ube16_to_cpu(buf + 7);
847 nb_sectors = ube32_to_cpu(buf + 6);
850 lba = ube32_to_cpu(buf + 2);
851 if (nb_sectors == 0) {
856 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
859 static void cmd_read_cd(IDEState *s, uint8_t* buf)
861 int nb_sectors, lba, transfer_request;
863 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
864 lba = ube32_to_cpu(buf + 2);
866 if (nb_sectors == 0) {
871 transfer_request = buf[9];
872 switch(transfer_request & 0xf8) {
879 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
883 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
886 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
887 ASC_INV_FIELD_IN_CMD_PACKET);
892 static void cmd_seek(IDEState *s, uint8_t* buf)
895 uint64_t total_sectors = s->nb_sectors >> 2;
897 lba = ube32_to_cpu(buf + 2);
898 if (lba >= total_sectors) {
899 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
906 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
909 bool start = buf[4] & 1;
910 bool loej = buf[4] & 2; /* load on start, eject on !start */
913 err = bdrv_eject(s->bs, !start);
921 sense = SENSE_NOT_READY;
922 if (bdrv_is_inserted(s->bs)) {
923 sense = SENSE_ILLEGAL_REQUEST;
925 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
928 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
933 s->tray_open = !start;
937 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
939 int max_len = ube16_to_cpu(buf + 8);
941 cpu_to_ube16(buf, 0);
947 cpu_to_ube16(buf + 6, 0);
948 ide_atapi_cmd_reply(s, 8, max_len);
951 static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
953 int format, msf, start_track, len;
955 uint64_t total_sectors = s->nb_sectors >> 2;
957 max_len = ube16_to_cpu(buf + 7);
958 format = buf[9] >> 6;
959 msf = (buf[1] >> 1) & 1;
960 start_track = buf[6];
964 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
967 ide_atapi_cmd_reply(s, len, max_len);
970 /* multi session : only a single session defined */
975 ide_atapi_cmd_reply(s, 12, max_len);
978 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
981 ide_atapi_cmd_reply(s, len, max_len);
985 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
986 ASC_INV_FIELD_IN_CMD_PACKET);
990 static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
992 uint64_t total_sectors = s->nb_sectors >> 2;
994 /* NOTE: it is really the number of sectors minus 1 */
995 cpu_to_ube32(buf, total_sectors - 1);
996 cpu_to_ube32(buf + 4, 2048);
997 ide_atapi_cmd_reply(s, 8, 8);
1000 static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1004 int format = buf[7];
1007 max_len = ube16_to_cpu(buf + 8);
1009 if (format < 0xff) {
1010 if (media_is_cd(s)) {
1011 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1012 ASC_INCOMPATIBLE_FORMAT);
1014 } else if (!media_present(s)) {
1015 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1016 ASC_INV_FIELD_IN_CMD_PACKET);
1021 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1022 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1028 ret = ide_dvd_read_structure(s, format, buf, buf);
1031 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1033 ide_atapi_cmd_reply(s, ret, max_len);
1038 /* TODO: BD support, fall through for now */
1040 /* Generic disk structures */
1041 case 0x80: /* TODO: AACS volume identifier */
1042 case 0x81: /* TODO: AACS media serial number */
1043 case 0x82: /* TODO: AACS media identifier */
1044 case 0x83: /* TODO: AACS media key block */
1045 case 0x90: /* TODO: List of recognized format layers */
1046 case 0xc0: /* TODO: Write protection status */
1048 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1049 ASC_INV_FIELD_IN_CMD_PACKET);
1054 static void cmd_set_speed(IDEState *s, uint8_t* buf)
1056 ide_atapi_cmd_ok(s);
1061 * Only commands flagged as ALLOW_UA are allowed to run under a
1062 * unit attention condition. (See MMC-5, section 4.1.6.1)
1067 * Commands flagged with CHECK_READY can only execute if a medium is present.
1068 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1074 static const struct {
1075 void (*handler)(IDEState *s, uint8_t *buf);
1077 } atapi_cmd_table[0x100] = {
1078 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY },
1079 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1080 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
1081 [ 0x1a ] = { cmd_mode_sense, /* (6) */ 0 },
1082 [ 0x1b ] = { cmd_start_stop_unit, 0 }, /* [1] */
1083 [ 0x1e ] = { cmd_prevent_allow_medium_removal, 0 },
1084 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
1085 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
1086 [ 0x2b ] = { cmd_seek, CHECK_READY },
1087 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
1088 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1089 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1090 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
1091 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1092 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
1093 [ 0xbb ] = { cmd_set_speed, 0 },
1094 [ 0xbd ] = { cmd_mechanism_status, 0 },
1095 [ 0xbe ] = { cmd_read_cd, CHECK_READY },
1096 /* [1] handler detects and reports not ready condition itself */
1099 void ide_atapi_cmd(IDEState *s)
1104 #ifdef DEBUG_IDE_ATAPI
1107 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1108 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1109 printf(" %02x", buf[i]);
1115 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1116 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1117 * condition response unless a higher priority status, defined by the drive
1120 if (s->sense_key == SENSE_UNIT_ATTENTION &&
1121 !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
1122 ide_atapi_cmd_check_status(s);
1126 * When a CD gets changed, we have to report an ejected state and
1127 * then a loaded state to guests so that they detect tray
1128 * open/close and media change events. Guests that do not use
1129 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1130 * states rely on this behavior.
1132 if (!s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1133 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1135 s->cdrom_changed = 0;
1136 s->sense_key = SENSE_UNIT_ATTENTION;
1137 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1141 /* Report a Not Ready condition if appropriate for the command */
1142 if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
1143 (!media_present(s) || !bdrv_is_inserted(s->bs)))
1145 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1149 /* Execute the command */
1150 if (atapi_cmd_table[s->io_buffer[0]].handler) {
1151 atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1155 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);