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)
177 if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
178 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
182 s->iov.iov_base = (s->cd_sector_size == 2352) ?
183 s->io_buffer + 16 : s->io_buffer;
185 s->iov.iov_len = ATAPI_SECTOR_SIZE;
186 qemu_iovec_init_external(&s->qiov, &s->iov, 1);
188 trace_cd_read_sector(s->lba);
190 block_acct_start(blk_get_stats(s->blk), &s->acct,
191 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
193 ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
194 cd_read_sector_cb, s);
196 s->status |= BUSY_STAT;
200 void ide_atapi_cmd_ok(IDEState *s)
203 s->status = READY_STAT | SEEK_STAT;
204 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
205 ide_transfer_stop(s);
209 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
211 trace_ide_atapi_cmd_error(s, sense_key, asc);
212 s->error = sense_key << 4;
213 s->status = READY_STAT | ERR_STAT;
214 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
215 s->sense_key = sense_key;
217 ide_transfer_stop(s);
221 void ide_atapi_io_error(IDEState *s, int ret)
223 /* XXX: handle more errors */
224 if (ret == -ENOMEDIUM) {
225 ide_atapi_cmd_error(s, NOT_READY,
226 ASC_MEDIUM_NOT_PRESENT);
228 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
229 ASC_LOGICAL_BLOCK_OOR);
233 static uint16_t atapi_byte_count_limit(IDEState *s)
237 bcl = s->lcyl | (s->hcyl << 8);
244 /* The whole ATAPI transfer logic is handled in this function */
245 void ide_atapi_cmd_reply_end(IDEState *s)
247 int byte_count_limit, size, ret;
248 trace_ide_atapi_cmd_reply_end(s, s->packet_transfer_size,
249 s->elementary_transfer_size,
251 if (s->packet_transfer_size <= 0) {
252 /* end of transfer */
255 trace_ide_atapi_cmd_reply_end_eot(s, s->status);
257 /* see if a new sector must be read */
258 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
259 if (!s->elementary_transfer_size) {
260 ret = cd_read_sector(s);
262 ide_atapi_io_error(s, ret);
266 /* rebuffering within an elementary transfer is
267 * only possible with a sync request because we
268 * end up with a race condition otherwise */
269 ret = cd_read_sector_sync(s);
271 ide_atapi_io_error(s, ret);
276 if (s->elementary_transfer_size > 0) {
277 /* there are some data left to transmit in this elementary
279 size = s->cd_sector_size - s->io_buffer_index;
280 if (size > s->elementary_transfer_size)
281 size = s->elementary_transfer_size;
282 s->packet_transfer_size -= size;
283 s->elementary_transfer_size -= size;
284 s->io_buffer_index += size;
285 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
286 size, ide_atapi_cmd_reply_end);
288 /* a new transfer is needed */
289 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
290 byte_count_limit = atapi_byte_count_limit(s);
291 trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
292 size = s->packet_transfer_size;
293 if (size > byte_count_limit) {
294 /* byte count limit must be even if this case */
295 if (byte_count_limit & 1)
297 size = byte_count_limit;
301 s->elementary_transfer_size = size;
302 /* we cannot transmit more than one sector at a time */
304 if (size > (s->cd_sector_size - s->io_buffer_index))
305 size = (s->cd_sector_size - s->io_buffer_index);
307 s->packet_transfer_size -= size;
308 s->elementary_transfer_size -= size;
309 s->io_buffer_index += size;
310 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
311 size, ide_atapi_cmd_reply_end);
313 trace_ide_atapi_cmd_reply_end_new(s, s->status);
318 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
319 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
323 s->lba = -1; /* no sector read */
324 s->packet_transfer_size = size;
325 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
326 s->elementary_transfer_size = 0;
329 block_acct_start(blk_get_stats(s->blk), &s->acct, size,
331 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
332 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
334 s->status = READY_STAT | SEEK_STAT;
335 s->io_buffer_index = 0;
336 ide_atapi_cmd_reply_end(s);
340 /* start a CD-CDROM read command */
341 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
345 s->packet_transfer_size = nb_sectors * sector_size;
346 s->elementary_transfer_size = 0;
347 s->io_buffer_index = sector_size;
348 s->cd_sector_size = sector_size;
350 ide_atapi_cmd_reply_end(s);
353 static void ide_atapi_cmd_check_status(IDEState *s)
355 trace_ide_atapi_cmd_check_status(s);
356 s->error = MC_ERR | (UNIT_ATTENTION << 4);
357 s->status = ERR_STAT;
361 /* ATAPI DMA support */
363 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
365 IDEState *s = opaque;
369 if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
370 if (s->bus->error_status) {
371 s->bus->dma->aiocb = NULL;
378 if (s->io_buffer_size > 0) {
380 * For a cdrom read sector command (s->lba != -1),
381 * adjust the lba for the next s->io_buffer_size chunk
382 * and dma the current chunk.
383 * For a command != read (s->lba == -1), just transfer
387 if (s->cd_sector_size == 2352) {
389 cd_data_to_raw(s->io_buffer, s->lba);
391 n = s->io_buffer_size >> 11;
395 s->packet_transfer_size -= s->io_buffer_size;
396 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
400 if (s->packet_transfer_size <= 0) {
401 s->status = READY_STAT | SEEK_STAT;
402 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
407 s->io_buffer_index = 0;
408 if (s->cd_sector_size == 2352) {
410 s->io_buffer_size = s->cd_sector_size;
413 n = s->packet_transfer_size >> 11;
414 if (n > (IDE_DMA_BUF_SECTORS / 4))
415 n = (IDE_DMA_BUF_SECTORS / 4);
416 s->io_buffer_size = n * 2048;
419 trace_ide_atapi_cmd_read_dma_cb_aio(s, s->lba, n);
420 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
421 s->bus->dma->iov.iov_len = n * ATAPI_SECTOR_SIZE;
422 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
424 s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
425 &s->bus->dma->qiov, n * 4,
426 ide_atapi_cmd_read_dma_cb, s);
431 block_acct_failed(blk_get_stats(s->blk), &s->acct);
433 block_acct_done(blk_get_stats(s->blk), &s->acct);
435 ide_set_inactive(s, false);
438 /* start a CD-CDROM read command with DMA */
439 /* XXX: test if DMA is available */
440 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
444 s->packet_transfer_size = nb_sectors * sector_size;
445 s->io_buffer_size = 0;
446 s->cd_sector_size = sector_size;
448 block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
451 /* XXX: check if BUSY_STAT should be set */
452 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
453 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
456 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
459 trace_ide_atapi_cmd_read(s, s->atapi_dma ? "dma" : "pio",
462 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
464 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
468 void ide_atapi_dma_restart(IDEState *s)
471 * At this point we can just re-evaluate the packet command and start over.
472 * The presence of ->dma_cb callback in the pre_save ensures that the packet
473 * command has been completely sent and we can safely restart command.
475 s->unit = s->bus->retry_unit;
476 s->bus->dma->ops->restart_dma(s->bus->dma);
480 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
483 uint8_t *buf_profile = buf + 12; /* start of profiles */
485 buf_profile += ((*index) * 4); /* start of indexed profile */
486 cpu_to_ube16 (buf_profile, profile);
487 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
489 /* each profile adds 4 bytes to the response */
491 buf[11] += 4; /* Additional Length */
496 static int ide_dvd_read_structure(IDEState *s, int format,
497 const uint8_t *packet, uint8_t *buf)
500 case 0x0: /* Physical format information */
502 int layer = packet[6];
503 uint64_t total_sectors;
506 return -ASC_INV_FIELD_IN_CMD_PACKET;
508 total_sectors = s->nb_sectors >> 2;
509 if (total_sectors == 0) {
510 return -ASC_MEDIUM_NOT_PRESENT;
513 buf[4] = 1; /* DVD-ROM, part version 1 */
514 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
515 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
516 buf[7] = 0; /* default densities */
518 /* FIXME: 0x30000 per spec? */
519 cpu_to_ube32(buf + 8, 0); /* start sector */
520 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
521 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
523 /* Size of buffer, not including 2 byte size field */
524 stw_be_p(buf, 2048 + 2);
526 /* 2k data + 4 byte header */
530 case 0x01: /* DVD copyright information */
531 buf[4] = 0; /* no copyright data */
532 buf[5] = 0; /* no region restrictions */
534 /* Size of buffer, not including 2 byte size field */
535 stw_be_p(buf, 4 + 2);
537 /* 4 byte header + 4 byte data */
540 case 0x03: /* BCA information - invalid field for no BCA info */
541 return -ASC_INV_FIELD_IN_CMD_PACKET;
543 case 0x04: /* DVD disc manufacturing information */
544 /* Size of buffer, not including 2 byte size field */
545 stw_be_p(buf, 2048 + 2);
547 /* 2k data + 4 byte header */
552 * This lists all the command capabilities above. Add new ones
553 * in order and update the length and buffer return values.
556 buf[4] = 0x00; /* Physical format */
557 buf[5] = 0x40; /* Not writable, is readable */
558 stw_be_p(buf + 6, 2048 + 4);
560 buf[8] = 0x01; /* Copyright info */
561 buf[9] = 0x40; /* Not writable, is readable */
562 stw_be_p(buf + 10, 4 + 4);
564 buf[12] = 0x03; /* BCA info */
565 buf[13] = 0x40; /* Not writable, is readable */
566 stw_be_p(buf + 14, 188 + 4);
568 buf[16] = 0x04; /* Manufacturing info */
569 buf[17] = 0x40; /* Not writable, is readable */
570 stw_be_p(buf + 18, 2048 + 4);
572 /* Size of buffer, not including 2 byte size field */
573 stw_be_p(buf, 16 + 2);
575 /* data written + 4 byte header */
578 default: /* TODO: formats beyond DVD-ROM requires */
579 return -ASC_INV_FIELD_IN_CMD_PACKET;
583 static unsigned int event_status_media(IDEState *s,
586 uint8_t event_code, media_status;
590 media_status = MS_TRAY_OPEN;
591 } else if (blk_is_inserted(s->blk)) {
592 media_status = MS_MEDIA_PRESENT;
595 /* Event notification descriptor */
596 event_code = MEC_NO_CHANGE;
597 if (media_status != MS_TRAY_OPEN) {
598 if (s->events.new_media) {
599 event_code = MEC_NEW_MEDIA;
600 s->events.new_media = false;
601 } else if (s->events.eject_request) {
602 event_code = MEC_EJECT_REQUESTED;
603 s->events.eject_request = false;
608 buf[5] = media_status;
610 /* These fields are reserved, just clear them. */
614 return 8; /* We wrote to 4 extra bytes from the header */
618 * Before transferring data or otherwise signalling acceptance of a command
619 * marked CONDDATA, we must check the validity of the byte_count_limit.
621 static bool validate_bcl(IDEState *s)
623 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
624 if (s->atapi_dma || atapi_byte_count_limit(s)) {
628 /* TODO: Move abort back into core.c and introduce proper error flow between
629 * ATAPI layer and IDE core layer */
630 ide_abort_command(s);
634 static void cmd_get_event_status_notification(IDEState *s,
637 const uint8_t *packet = buf;
641 uint8_t polled; /* lsb bit is polled; others are reserved */
642 uint8_t reserved2[2];
644 uint8_t reserved3[2];
647 } QEMU_PACKED *gesn_cdb;
651 uint8_t notification_class;
652 uint8_t supported_events;
653 } QEMU_PACKED *gesn_event_header;
654 unsigned int max_len, used_len;
656 gesn_cdb = (void *)packet;
657 gesn_event_header = (void *)buf;
659 max_len = be16_to_cpu(gesn_cdb->len);
661 /* It is fine by the MMC spec to not support async mode operations */
662 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
663 /* Only polling is supported, asynchronous mode is not. */
664 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
665 ASC_INV_FIELD_IN_CMD_PACKET);
669 /* polling mode operation */
672 * These are the supported events.
674 * We currently only support requests of the 'media' type.
675 * Notification class requests and supported event classes are bitmasks,
676 * but they are build from the same values as the "notification class"
679 gesn_event_header->supported_events = 1 << GESN_MEDIA;
682 * We use |= below to set the class field; other bits in this byte
683 * are reserved now but this is useful to do if we have to use the
684 * reserved fields later.
686 gesn_event_header->notification_class = 0;
689 * Responses to requests are to be based on request priority. The
690 * notification_class_request_type enum above specifies the
691 * priority: upper elements are higher prio than lower ones.
693 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
694 gesn_event_header->notification_class |= GESN_MEDIA;
695 used_len = event_status_media(s, buf);
697 gesn_event_header->notification_class = 0x80; /* No event available */
698 used_len = sizeof(*gesn_event_header);
700 gesn_event_header->len = cpu_to_be16(used_len
701 - sizeof(*gesn_event_header));
702 ide_atapi_cmd_reply(s, used_len, max_len);
705 static void cmd_request_sense(IDEState *s, uint8_t *buf)
707 int max_len = buf[4];
710 buf[0] = 0x70 | (1 << 7);
711 buf[2] = s->sense_key;
715 if (s->sense_key == UNIT_ATTENTION) {
716 s->sense_key = NO_SENSE;
719 ide_atapi_cmd_reply(s, 18, max_len);
722 static void cmd_inquiry(IDEState *s, uint8_t *buf)
724 uint8_t page_code = buf[2];
725 int max_len = buf[4];
729 unsigned preamble_len;
731 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
732 * we are being asked for a specific page of info indicated by byte 2. */
737 buf[idx++] = 0x05; /* CD-ROM */
738 buf[idx++] = page_code; /* Page Code */
739 buf[idx++] = 0x00; /* reserved */
740 idx++; /* length (set later) */
744 /* Supported Pages: List of supported VPD responses. */
745 buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
746 buf[idx++] = 0x83; /* 0x83: Device Identification. */
750 /* Device Identification. Each entry is optional, but the entries
751 * included here are modeled after libata's VPD responses.
752 * If the response is given, at least one entry must be present. */
754 /* Entry 1: Serial */
755 if (idx + 24 > max_len) {
756 /* Not enough room for even the first entry: */
757 /* 4 byte header + 20 byte string */
758 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
759 ASC_DATA_PHASE_ERROR);
762 buf[idx++] = 0x02; /* Ascii */
763 buf[idx++] = 0x00; /* Vendor Specific */
765 buf[idx++] = 20; /* Remaining length */
766 padstr8(buf + idx, 20, s->drive_serial_str);
769 /* Entry 2: Drive Model and Serial */
770 if (idx + 72 > max_len) {
771 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
774 buf[idx++] = 0x02; /* Ascii */
775 buf[idx++] = 0x01; /* T10 Vendor */
778 padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
780 padstr8(buf + idx, 40, s->drive_model_str);
782 padstr8(buf + idx, 20, s->drive_serial_str);
786 if (s->wwn && (idx + 12 <= max_len)) {
787 /* 4 byte header + 8 byte wwn */
788 buf[idx++] = 0x01; /* Binary */
789 buf[idx++] = 0x03; /* NAA */
792 stq_be_p(&buf[idx], s->wwn);
798 /* SPC-3, revision 23 sec. 6.4 */
799 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
800 ASC_INV_FIELD_IN_CMD_PACKET);
807 buf[0] = 0x05; /* CD-ROM */
808 buf[1] = 0x80; /* removable */
809 buf[2] = 0x00; /* ISO */
810 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
811 /* buf[size_idx] set below. */
812 buf[5] = 0; /* reserved */
813 buf[6] = 0; /* reserved */
814 buf[7] = 0; /* reserved */
815 padstr8(buf + 8, 8, "QEMU");
816 padstr8(buf + 16, 16, "QEMU DVD-ROM");
817 padstr8(buf + 32, 4, s->version);
822 buf[size_idx] = idx - preamble_len;
823 ide_atapi_cmd_reply(s, idx, max_len);
826 static void cmd_get_configuration(IDEState *s, uint8_t *buf)
832 /* only feature 0 is supported */
833 if (buf[2] != 0 || buf[3] != 0) {
834 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
835 ASC_INV_FIELD_IN_CMD_PACKET);
839 /* XXX: could result in alignment problems in some architectures */
840 max_len = ube16_to_cpu(buf + 7);
843 * XXX: avoid overflow for io_buffer if max_len is bigger than
844 * the size of that buffer (dimensioned to max number of
845 * sectors to transfer at once)
847 * Only a problem if the feature/profiles grow.
850 /* XXX: assume 1 sector */
854 memset(buf, 0, max_len);
856 * the number of sectors from the media tells us which profile
857 * to use as current. 0 means there is no media
859 if (media_is_dvd(s)) {
860 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
861 } else if (media_is_cd(s)) {
862 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
865 buf[10] = 0x02 | 0x01; /* persistent and current */
866 len = 12; /* headers: 8 + 4 */
867 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
868 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
869 cpu_to_ube32(buf, len - 4); /* data length */
871 ide_atapi_cmd_reply(s, len, max_len);
874 static void cmd_mode_sense(IDEState *s, uint8_t *buf)
879 max_len = ube16_to_cpu(buf + 7);
880 action = buf[2] >> 6;
881 code = buf[2] & 0x3f;
884 case 0: /* current values */
886 case MODE_PAGE_R_W_ERROR: /* error recovery */
887 cpu_to_ube16(&buf[0], 16 - 2);
895 buf[8] = MODE_PAGE_R_W_ERROR;
903 ide_atapi_cmd_reply(s, 16, max_len);
905 case MODE_PAGE_AUDIO_CTL:
906 cpu_to_ube16(&buf[0], 24 - 2);
914 buf[8] = MODE_PAGE_AUDIO_CTL;
916 /* Fill with CDROM audio volume */
922 ide_atapi_cmd_reply(s, 24, max_len);
924 case MODE_PAGE_CAPABILITIES:
925 cpu_to_ube16(&buf[0], 30 - 2);
933 buf[8] = MODE_PAGE_CAPABILITIES;
935 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
938 /* Claim PLAY_AUDIO capability (0x01) since some Linux
939 code checks for this to automount media. */
942 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
943 if (s->tray_locked) {
946 buf[15] = 0x00; /* No volume & mute control, no changer */
947 cpu_to_ube16(&buf[16], 704); /* 4x read speed */
948 buf[18] = 0; /* Two volume levels */
950 cpu_to_ube16(&buf[20], 512); /* 512k buffer */
951 cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
958 ide_atapi_cmd_reply(s, 30, max_len);
964 case 1: /* changeable values */
966 case 2: /* default values */
969 case 3: /* saved values */
970 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
971 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
977 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
980 static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
982 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
983 * come here, we know that it's ready. */
987 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
989 s->tray_locked = buf[4] & 1;
990 blk_lock_medium(s->blk, buf[4] & 1);
994 static void cmd_read(IDEState *s, uint8_t* buf)
998 if (buf[0] == GPCMD_READ_10) {
999 nb_sectors = ube16_to_cpu(buf + 7);
1001 nb_sectors = ube32_to_cpu(buf + 6);
1004 lba = ube32_to_cpu(buf + 2);
1005 if (nb_sectors == 0) {
1006 ide_atapi_cmd_ok(s);
1010 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1013 static void cmd_read_cd(IDEState *s, uint8_t* buf)
1015 int nb_sectors, lba, transfer_request;
1017 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
1018 lba = ube32_to_cpu(buf + 2);
1020 if (nb_sectors == 0) {
1021 ide_atapi_cmd_ok(s);
1025 transfer_request = buf[9] & 0xf8;
1026 if (transfer_request == 0x00) {
1028 ide_atapi_cmd_ok(s);
1032 /* Check validity of BCL before transferring data */
1033 if (!validate_bcl(s)) {
1037 switch (transfer_request) {
1040 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1044 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1047 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1048 ASC_INV_FIELD_IN_CMD_PACKET);
1053 static void cmd_seek(IDEState *s, uint8_t* buf)
1056 uint64_t total_sectors = s->nb_sectors >> 2;
1058 lba = ube32_to_cpu(buf + 2);
1059 if (lba >= total_sectors) {
1060 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1064 ide_atapi_cmd_ok(s);
1067 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
1070 bool start = buf[4] & 1;
1071 bool loej = buf[4] & 2; /* load on start, eject on !start */
1072 int pwrcnd = buf[4] & 0xf0;
1075 /* eject/load only happens for power condition == 0 */
1076 ide_atapi_cmd_ok(s);
1081 if (!start && !s->tray_open && s->tray_locked) {
1082 sense = blk_is_inserted(s->blk)
1083 ? NOT_READY : ILLEGAL_REQUEST;
1084 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1088 if (s->tray_open != !start) {
1089 blk_eject(s->blk, !start);
1090 s->tray_open = !start;
1094 ide_atapi_cmd_ok(s);
1097 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
1099 int max_len = ube16_to_cpu(buf + 8);
1101 cpu_to_ube16(buf, 0);
1102 /* no current LBA */
1107 cpu_to_ube16(buf + 6, 0);
1108 ide_atapi_cmd_reply(s, 8, max_len);
1111 static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
1113 int format, msf, start_track, len;
1115 uint64_t total_sectors = s->nb_sectors >> 2;
1117 max_len = ube16_to_cpu(buf + 7);
1118 format = buf[9] >> 6;
1119 msf = (buf[1] >> 1) & 1;
1120 start_track = buf[6];
1124 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1127 ide_atapi_cmd_reply(s, len, max_len);
1130 /* multi session : only a single session defined */
1135 ide_atapi_cmd_reply(s, 12, max_len);
1138 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1141 ide_atapi_cmd_reply(s, len, max_len);
1145 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1146 ASC_INV_FIELD_IN_CMD_PACKET);
1150 static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
1152 uint64_t total_sectors = s->nb_sectors >> 2;
1154 /* NOTE: it is really the number of sectors minus 1 */
1155 cpu_to_ube32(buf, total_sectors - 1);
1156 cpu_to_ube32(buf + 4, 2048);
1157 ide_atapi_cmd_reply(s, 8, 8);
1160 static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
1162 uint8_t type = buf[1] & 7;
1163 uint32_t max_len = ube16_to_cpu(buf + 7);
1165 /* Types 1/2 are only defined for Blu-Ray. */
1167 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1168 ASC_INV_FIELD_IN_CMD_PACKET);
1174 buf[2] = 0xe; /* last session complete, disc finalized */
1175 buf[3] = 1; /* first track on disc */
1176 buf[4] = 1; /* # of sessions */
1177 buf[5] = 1; /* first track of last session */
1178 buf[6] = 1; /* last track of last session */
1179 buf[7] = 0x20; /* unrestricted use */
1180 buf[8] = 0x00; /* CD-ROM or DVD-ROM */
1181 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1182 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1183 /* 24-31: disc bar code */
1184 /* 32: disc application code */
1185 /* 33: number of OPC tables */
1187 ide_atapi_cmd_reply(s, 34, max_len);
1190 static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1194 int format = buf[7];
1197 max_len = ube16_to_cpu(buf + 8);
1199 if (format < 0xff) {
1200 if (media_is_cd(s)) {
1201 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1202 ASC_INCOMPATIBLE_FORMAT);
1204 } else if (!media_present(s)) {
1205 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1206 ASC_INV_FIELD_IN_CMD_PACKET);
1211 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1212 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1218 ret = ide_dvd_read_structure(s, format, buf, buf);
1221 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1223 ide_atapi_cmd_reply(s, ret, max_len);
1228 /* TODO: BD support, fall through for now */
1230 /* Generic disk structures */
1231 case 0x80: /* TODO: AACS volume identifier */
1232 case 0x81: /* TODO: AACS media serial number */
1233 case 0x82: /* TODO: AACS media identifier */
1234 case 0x83: /* TODO: AACS media key block */
1235 case 0x90: /* TODO: List of recognized format layers */
1236 case 0xc0: /* TODO: Write protection status */
1238 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1239 ASC_INV_FIELD_IN_CMD_PACKET);
1244 static void cmd_set_speed(IDEState *s, uint8_t* buf)
1246 ide_atapi_cmd_ok(s);
1251 * Only commands flagged as ALLOW_UA are allowed to run under a
1252 * unit attention condition. (See MMC-5, section 4.1.6.1)
1257 * Commands flagged with CHECK_READY can only execute if a medium is present.
1258 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1264 * Commands flagged with NONDATA do not in any circumstances return
1265 * any data via ide_atapi_cmd_reply. These commands are exempt from
1266 * the normal byte_count_limit constraints.
1267 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1272 * CONDDATA implies a command that transfers data only conditionally based
1273 * on the presence of suboptions. It should be exempt from the BCL check at
1274 * command validation time, but it needs to be checked at the command
1275 * handler level instead.
1280 static const struct AtapiCmd {
1281 void (*handler)(IDEState *s, uint8_t *buf);
1283 } atapi_cmd_table[0x100] = {
1284 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
1285 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1286 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
1287 [ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */
1288 [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
1289 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
1290 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
1291 [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
1292 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
1293 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1294 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1295 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
1296 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
1297 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1298 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
1299 [ 0xbb ] = { cmd_set_speed, NONDATA },
1300 [ 0xbd ] = { cmd_mechanism_status, 0 },
1301 [ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA },
1302 /* [1] handler detects and reports not ready condition itself */
1305 void ide_atapi_cmd(IDEState *s)
1307 uint8_t *buf = s->io_buffer;
1308 const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
1310 trace_ide_atapi_cmd(s, s->io_buffer[0]);
1312 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET)) {
1313 /* Each pretty-printed byte needs two bytes and a space; */
1314 char *ppacket = g_malloc(ATAPI_PACKET_SIZE * 3 + 1);
1316 for (i = 0; i < ATAPI_PACKET_SIZE; i++) {
1317 sprintf(ppacket + (i * 3), "%02x ", buf[i]);
1319 trace_ide_atapi_cmd_packet(s, s->lcyl | (s->hcyl << 8), ppacket);
1324 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1325 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1326 * condition response unless a higher priority status, defined by the drive
1329 if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
1330 ide_atapi_cmd_check_status(s);
1334 * When a CD gets changed, we have to report an ejected state and
1335 * then a loaded state to guests so that they detect tray
1336 * open/close and media change events. Guests that do not use
1337 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1338 * states rely on this behavior.
1340 if (!(cmd->flags & ALLOW_UA) &&
1341 !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1343 if (s->cdrom_changed == 1) {
1344 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1345 s->cdrom_changed = 2;
1347 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1348 s->cdrom_changed = 0;
1354 /* Report a Not Ready condition if appropriate for the command */
1355 if ((cmd->flags & CHECK_READY) &&
1356 (!media_present(s) || !blk_is_inserted(s->blk)))
1358 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1362 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1363 * If this is a data-transferring PIO command and BCL is 0,
1364 * we abort at the /ATA/ level, not the ATAPI level.
1365 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1366 if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
1367 if (!validate_bcl(s)) {
1372 /* Execute the command */
1374 cmd->handler(s, buf);
1378 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);