xref: /openbmc/qemu/hw/ide/atapi.c (revision 541dc0d47f10973c241e9955afc2aefc96adec51)
133231e0eSKevin Wolf /*
233231e0eSKevin Wolf  * QEMU ATAPI Emulation
333231e0eSKevin Wolf  *
433231e0eSKevin Wolf  * Copyright (c) 2003 Fabrice Bellard
533231e0eSKevin Wolf  * Copyright (c) 2006 Openedhand Ltd.
633231e0eSKevin Wolf  *
733231e0eSKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
833231e0eSKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
933231e0eSKevin Wolf  * in the Software without restriction, including without limitation the rights
1033231e0eSKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1133231e0eSKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
1233231e0eSKevin Wolf  * furnished to do so, subject to the following conditions:
1333231e0eSKevin Wolf  *
1433231e0eSKevin Wolf  * The above copyright notice and this permission notice shall be included in
1533231e0eSKevin Wolf  * all copies or substantial portions of the Software.
1633231e0eSKevin Wolf  *
1733231e0eSKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1833231e0eSKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1933231e0eSKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2033231e0eSKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2133231e0eSKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2233231e0eSKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2333231e0eSKevin Wolf  * THE SOFTWARE.
2433231e0eSKevin Wolf  */
2533231e0eSKevin Wolf 
2633231e0eSKevin Wolf #include "hw/ide/internal.h"
2733231e0eSKevin Wolf #include "hw/scsi.h"
2833231e0eSKevin Wolf 
2933231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
3033231e0eSKevin Wolf 
3133231e0eSKevin Wolf static void padstr8(uint8_t *buf, int buf_size, const char *src)
3233231e0eSKevin Wolf {
3333231e0eSKevin Wolf     int i;
3433231e0eSKevin Wolf     for(i = 0; i < buf_size; i++) {
3533231e0eSKevin Wolf         if (*src)
3633231e0eSKevin Wolf             buf[i] = *src++;
3733231e0eSKevin Wolf         else
3833231e0eSKevin Wolf             buf[i] = ' ';
3933231e0eSKevin Wolf     }
4033231e0eSKevin Wolf }
4133231e0eSKevin Wolf 
4233231e0eSKevin Wolf static inline void cpu_to_ube16(uint8_t *buf, int val)
4333231e0eSKevin Wolf {
4433231e0eSKevin Wolf     buf[0] = val >> 8;
4533231e0eSKevin Wolf     buf[1] = val & 0xff;
4633231e0eSKevin Wolf }
4733231e0eSKevin Wolf 
4833231e0eSKevin Wolf static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
4933231e0eSKevin Wolf {
5033231e0eSKevin Wolf     buf[0] = val >> 24;
5133231e0eSKevin Wolf     buf[1] = val >> 16;
5233231e0eSKevin Wolf     buf[2] = val >> 8;
5333231e0eSKevin Wolf     buf[3] = val & 0xff;
5433231e0eSKevin Wolf }
5533231e0eSKevin Wolf 
5633231e0eSKevin Wolf static inline int ube16_to_cpu(const uint8_t *buf)
5733231e0eSKevin Wolf {
5833231e0eSKevin Wolf     return (buf[0] << 8) | buf[1];
5933231e0eSKevin Wolf }
6033231e0eSKevin Wolf 
6133231e0eSKevin Wolf static inline int ube32_to_cpu(const uint8_t *buf)
6233231e0eSKevin Wolf {
6333231e0eSKevin Wolf     return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
6433231e0eSKevin Wolf }
6533231e0eSKevin Wolf 
6633231e0eSKevin Wolf static void lba_to_msf(uint8_t *buf, int lba)
6733231e0eSKevin Wolf {
6833231e0eSKevin Wolf     lba += 150;
6933231e0eSKevin Wolf     buf[0] = (lba / 75) / 60;
7033231e0eSKevin Wolf     buf[1] = (lba / 75) % 60;
7133231e0eSKevin Wolf     buf[2] = lba % 75;
7233231e0eSKevin Wolf }
7333231e0eSKevin Wolf 
7433231e0eSKevin Wolf static inline int media_present(IDEState *s)
7533231e0eSKevin Wolf {
7633231e0eSKevin Wolf     return (s->nb_sectors > 0);
7733231e0eSKevin Wolf }
7833231e0eSKevin Wolf 
79a7acf552SAmit Shah /* XXX: DVDs that could fit on a CD will be reported as a CD */
8033231e0eSKevin Wolf static inline int media_is_dvd(IDEState *s)
8133231e0eSKevin Wolf {
8233231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
8333231e0eSKevin Wolf }
8433231e0eSKevin Wolf 
8533231e0eSKevin Wolf static inline int media_is_cd(IDEState *s)
8633231e0eSKevin Wolf {
8733231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
8833231e0eSKevin Wolf }
8933231e0eSKevin Wolf 
9033231e0eSKevin Wolf static void cd_data_to_raw(uint8_t *buf, int lba)
9133231e0eSKevin Wolf {
9233231e0eSKevin Wolf     /* sync bytes */
9333231e0eSKevin Wolf     buf[0] = 0x00;
9433231e0eSKevin Wolf     memset(buf + 1, 0xff, 10);
9533231e0eSKevin Wolf     buf[11] = 0x00;
9633231e0eSKevin Wolf     buf += 12;
9733231e0eSKevin Wolf     /* MSF */
9833231e0eSKevin Wolf     lba_to_msf(buf, lba);
9933231e0eSKevin Wolf     buf[3] = 0x01; /* mode 1 data */
10033231e0eSKevin Wolf     buf += 4;
10133231e0eSKevin Wolf     /* data */
10233231e0eSKevin Wolf     buf += 2048;
10333231e0eSKevin Wolf     /* XXX: ECC not computed */
10433231e0eSKevin Wolf     memset(buf, 0, 288);
10533231e0eSKevin Wolf }
10633231e0eSKevin Wolf 
107a597e79cSChristoph Hellwig static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
10833231e0eSKevin Wolf {
10933231e0eSKevin Wolf     int ret;
11033231e0eSKevin Wolf 
11133231e0eSKevin Wolf     switch(sector_size) {
11233231e0eSKevin Wolf     case 2048:
113a597e79cSChristoph Hellwig         bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
114a597e79cSChristoph Hellwig         ret = bdrv_read(s->bs, (int64_t)lba << 2, buf, 4);
115a597e79cSChristoph Hellwig         bdrv_acct_done(s->bs, &s->acct);
11633231e0eSKevin Wolf         break;
11733231e0eSKevin Wolf     case 2352:
118a597e79cSChristoph Hellwig         bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
119a597e79cSChristoph Hellwig         ret = bdrv_read(s->bs, (int64_t)lba << 2, buf + 16, 4);
120a597e79cSChristoph Hellwig         bdrv_acct_done(s->bs, &s->acct);
12133231e0eSKevin Wolf         if (ret < 0)
12233231e0eSKevin Wolf             return ret;
12333231e0eSKevin Wolf         cd_data_to_raw(buf, lba);
12433231e0eSKevin Wolf         break;
12533231e0eSKevin Wolf     default:
12633231e0eSKevin Wolf         ret = -EIO;
12733231e0eSKevin Wolf         break;
12833231e0eSKevin Wolf     }
12933231e0eSKevin Wolf     return ret;
13033231e0eSKevin Wolf }
13133231e0eSKevin Wolf 
13233231e0eSKevin Wolf void ide_atapi_cmd_ok(IDEState *s)
13333231e0eSKevin Wolf {
13433231e0eSKevin Wolf     s->error = 0;
13533231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT;
13633231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
13733231e0eSKevin Wolf     ide_set_irq(s->bus);
13833231e0eSKevin Wolf }
13933231e0eSKevin Wolf 
14033231e0eSKevin Wolf void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
14133231e0eSKevin Wolf {
14233231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
14333231e0eSKevin Wolf     printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
14433231e0eSKevin Wolf #endif
14533231e0eSKevin Wolf     s->error = sense_key << 4;
14633231e0eSKevin Wolf     s->status = READY_STAT | ERR_STAT;
14733231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
14833231e0eSKevin Wolf     s->sense_key = sense_key;
14933231e0eSKevin Wolf     s->asc = asc;
15033231e0eSKevin Wolf     ide_set_irq(s->bus);
15133231e0eSKevin Wolf }
15233231e0eSKevin Wolf 
15333231e0eSKevin Wolf void ide_atapi_io_error(IDEState *s, int ret)
15433231e0eSKevin Wolf {
15533231e0eSKevin Wolf     /* XXX: handle more errors */
15633231e0eSKevin Wolf     if (ret == -ENOMEDIUM) {
15733231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_NOT_READY,
15833231e0eSKevin Wolf                             ASC_MEDIUM_NOT_PRESENT);
15933231e0eSKevin Wolf     } else {
16033231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
16133231e0eSKevin Wolf                             ASC_LOGICAL_BLOCK_OOR);
16233231e0eSKevin Wolf     }
16333231e0eSKevin Wolf }
16433231e0eSKevin Wolf 
16533231e0eSKevin Wolf /* The whole ATAPI transfer logic is handled in this function */
16633231e0eSKevin Wolf void ide_atapi_cmd_reply_end(IDEState *s)
16733231e0eSKevin Wolf {
16833231e0eSKevin Wolf     int byte_count_limit, size, ret;
16933231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
17033231e0eSKevin Wolf     printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
17133231e0eSKevin Wolf            s->packet_transfer_size,
17233231e0eSKevin Wolf            s->elementary_transfer_size,
17333231e0eSKevin Wolf            s->io_buffer_index);
17433231e0eSKevin Wolf #endif
17533231e0eSKevin Wolf     if (s->packet_transfer_size <= 0) {
17633231e0eSKevin Wolf         /* end of transfer */
17733231e0eSKevin Wolf         ide_transfer_stop(s);
17833231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
17933231e0eSKevin Wolf         s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
18033231e0eSKevin Wolf         ide_set_irq(s->bus);
18133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
18233231e0eSKevin Wolf         printf("status=0x%x\n", s->status);
18333231e0eSKevin Wolf #endif
18433231e0eSKevin Wolf     } else {
18533231e0eSKevin Wolf         /* see if a new sector must be read */
18633231e0eSKevin Wolf         if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
187a597e79cSChristoph Hellwig             ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
18833231e0eSKevin Wolf             if (ret < 0) {
18933231e0eSKevin Wolf                 ide_transfer_stop(s);
19033231e0eSKevin Wolf                 ide_atapi_io_error(s, ret);
19133231e0eSKevin Wolf                 return;
19233231e0eSKevin Wolf             }
19333231e0eSKevin Wolf             s->lba++;
19433231e0eSKevin Wolf             s->io_buffer_index = 0;
19533231e0eSKevin Wolf         }
19633231e0eSKevin Wolf         if (s->elementary_transfer_size > 0) {
19733231e0eSKevin Wolf             /* there are some data left to transmit in this elementary
19833231e0eSKevin Wolf                transfer */
19933231e0eSKevin Wolf             size = s->cd_sector_size - s->io_buffer_index;
20033231e0eSKevin Wolf             if (size > s->elementary_transfer_size)
20133231e0eSKevin Wolf                 size = s->elementary_transfer_size;
20233231e0eSKevin Wolf             s->packet_transfer_size -= size;
20333231e0eSKevin Wolf             s->elementary_transfer_size -= size;
20433231e0eSKevin Wolf             s->io_buffer_index += size;
20533231e0eSKevin Wolf             ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
20633231e0eSKevin Wolf                                size, ide_atapi_cmd_reply_end);
20733231e0eSKevin Wolf         } else {
20833231e0eSKevin Wolf             /* a new transfer is needed */
20933231e0eSKevin Wolf             s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
21033231e0eSKevin Wolf             byte_count_limit = s->lcyl | (s->hcyl << 8);
21133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
21233231e0eSKevin Wolf             printf("byte_count_limit=%d\n", byte_count_limit);
21333231e0eSKevin Wolf #endif
21433231e0eSKevin Wolf             if (byte_count_limit == 0xffff)
21533231e0eSKevin Wolf                 byte_count_limit--;
21633231e0eSKevin Wolf             size = s->packet_transfer_size;
21733231e0eSKevin Wolf             if (size > byte_count_limit) {
21833231e0eSKevin Wolf                 /* byte count limit must be even if this case */
21933231e0eSKevin Wolf                 if (byte_count_limit & 1)
22033231e0eSKevin Wolf                     byte_count_limit--;
22133231e0eSKevin Wolf                 size = byte_count_limit;
22233231e0eSKevin Wolf             }
22333231e0eSKevin Wolf             s->lcyl = size;
22433231e0eSKevin Wolf             s->hcyl = size >> 8;
22533231e0eSKevin Wolf             s->elementary_transfer_size = size;
22633231e0eSKevin Wolf             /* we cannot transmit more than one sector at a time */
22733231e0eSKevin Wolf             if (s->lba != -1) {
22833231e0eSKevin Wolf                 if (size > (s->cd_sector_size - s->io_buffer_index))
22933231e0eSKevin Wolf                     size = (s->cd_sector_size - s->io_buffer_index);
23033231e0eSKevin Wolf             }
23133231e0eSKevin Wolf             s->packet_transfer_size -= size;
23233231e0eSKevin Wolf             s->elementary_transfer_size -= size;
23333231e0eSKevin Wolf             s->io_buffer_index += size;
23433231e0eSKevin Wolf             ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
23533231e0eSKevin Wolf                                size, ide_atapi_cmd_reply_end);
23633231e0eSKevin Wolf             ide_set_irq(s->bus);
23733231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
23833231e0eSKevin Wolf             printf("status=0x%x\n", s->status);
23933231e0eSKevin Wolf #endif
24033231e0eSKevin Wolf         }
24133231e0eSKevin Wolf     }
24233231e0eSKevin Wolf }
24333231e0eSKevin Wolf 
24433231e0eSKevin Wolf /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
24533231e0eSKevin Wolf static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
24633231e0eSKevin Wolf {
24733231e0eSKevin Wolf     if (size > max_size)
24833231e0eSKevin Wolf         size = max_size;
24933231e0eSKevin Wolf     s->lba = -1; /* no sector read */
25033231e0eSKevin Wolf     s->packet_transfer_size = size;
25133231e0eSKevin Wolf     s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
25233231e0eSKevin Wolf     s->elementary_transfer_size = 0;
25333231e0eSKevin Wolf     s->io_buffer_index = 0;
25433231e0eSKevin Wolf 
25533231e0eSKevin Wolf     if (s->atapi_dma) {
256a597e79cSChristoph Hellwig         bdrv_acct_start(s->bs, &s->acct, size, BDRV_ACCT_READ);
25733231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
25833231e0eSKevin Wolf         s->bus->dma->ops->start_dma(s->bus->dma, s,
25933231e0eSKevin Wolf                                    ide_atapi_cmd_read_dma_cb);
26033231e0eSKevin Wolf     } else {
26133231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
26233231e0eSKevin Wolf         ide_atapi_cmd_reply_end(s);
26333231e0eSKevin Wolf     }
26433231e0eSKevin Wolf }
26533231e0eSKevin Wolf 
26633231e0eSKevin Wolf /* start a CD-CDROM read command */
26733231e0eSKevin Wolf static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
26833231e0eSKevin Wolf                                    int sector_size)
26933231e0eSKevin Wolf {
27033231e0eSKevin Wolf     s->lba = lba;
27133231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
27233231e0eSKevin Wolf     s->elementary_transfer_size = 0;
27333231e0eSKevin Wolf     s->io_buffer_index = sector_size;
27433231e0eSKevin Wolf     s->cd_sector_size = sector_size;
27533231e0eSKevin Wolf 
27633231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT;
27733231e0eSKevin Wolf     ide_atapi_cmd_reply_end(s);
27833231e0eSKevin Wolf }
27933231e0eSKevin Wolf 
28033231e0eSKevin Wolf static void ide_atapi_cmd_check_status(IDEState *s)
28133231e0eSKevin Wolf {
28233231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
28333231e0eSKevin Wolf     printf("atapi_cmd_check_status\n");
28433231e0eSKevin Wolf #endif
28533231e0eSKevin Wolf     s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
28633231e0eSKevin Wolf     s->status = ERR_STAT;
28733231e0eSKevin Wolf     s->nsector = 0;
28833231e0eSKevin Wolf     ide_set_irq(s->bus);
28933231e0eSKevin Wolf }
29033231e0eSKevin Wolf /* ATAPI DMA support */
29133231e0eSKevin Wolf 
29233231e0eSKevin Wolf /* XXX: handle read errors */
29333231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
29433231e0eSKevin Wolf {
29533231e0eSKevin Wolf     IDEState *s = opaque;
29633231e0eSKevin Wolf     int data_offset, n;
29733231e0eSKevin Wolf 
29833231e0eSKevin Wolf     if (ret < 0) {
29933231e0eSKevin Wolf         ide_atapi_io_error(s, ret);
30033231e0eSKevin Wolf         goto eot;
30133231e0eSKevin Wolf     }
30233231e0eSKevin Wolf 
30333231e0eSKevin Wolf     if (s->io_buffer_size > 0) {
30433231e0eSKevin Wolf         /*
30533231e0eSKevin Wolf          * For a cdrom read sector command (s->lba != -1),
30633231e0eSKevin Wolf          * adjust the lba for the next s->io_buffer_size chunk
30733231e0eSKevin Wolf          * and dma the current chunk.
30833231e0eSKevin Wolf          * For a command != read (s->lba == -1), just transfer
30933231e0eSKevin Wolf          * the reply data.
31033231e0eSKevin Wolf          */
31133231e0eSKevin Wolf         if (s->lba != -1) {
31233231e0eSKevin Wolf             if (s->cd_sector_size == 2352) {
31333231e0eSKevin Wolf                 n = 1;
31433231e0eSKevin Wolf                 cd_data_to_raw(s->io_buffer, s->lba);
31533231e0eSKevin Wolf             } else {
31633231e0eSKevin Wolf                 n = s->io_buffer_size >> 11;
31733231e0eSKevin Wolf             }
31833231e0eSKevin Wolf             s->lba += n;
31933231e0eSKevin Wolf         }
32033231e0eSKevin Wolf         s->packet_transfer_size -= s->io_buffer_size;
32133231e0eSKevin Wolf         if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
32233231e0eSKevin Wolf             goto eot;
32333231e0eSKevin Wolf     }
32433231e0eSKevin Wolf 
32533231e0eSKevin Wolf     if (s->packet_transfer_size <= 0) {
32633231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
32733231e0eSKevin Wolf         s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
32833231e0eSKevin Wolf         ide_set_irq(s->bus);
329a597e79cSChristoph Hellwig         goto eot;
33033231e0eSKevin Wolf     }
33133231e0eSKevin Wolf 
33233231e0eSKevin Wolf     s->io_buffer_index = 0;
33333231e0eSKevin Wolf     if (s->cd_sector_size == 2352) {
33433231e0eSKevin Wolf         n = 1;
33533231e0eSKevin Wolf         s->io_buffer_size = s->cd_sector_size;
33633231e0eSKevin Wolf         data_offset = 16;
33733231e0eSKevin Wolf     } else {
33833231e0eSKevin Wolf         n = s->packet_transfer_size >> 11;
33933231e0eSKevin Wolf         if (n > (IDE_DMA_BUF_SECTORS / 4))
34033231e0eSKevin Wolf             n = (IDE_DMA_BUF_SECTORS / 4);
34133231e0eSKevin Wolf         s->io_buffer_size = n * 2048;
34233231e0eSKevin Wolf         data_offset = 0;
34333231e0eSKevin Wolf     }
34433231e0eSKevin Wolf #ifdef DEBUG_AIO
34533231e0eSKevin Wolf     printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
34633231e0eSKevin Wolf #endif
347a597e79cSChristoph Hellwig 
34833231e0eSKevin Wolf     s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
34933231e0eSKevin Wolf     s->bus->dma->iov.iov_len = n * 4 * 512;
35033231e0eSKevin Wolf     qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
351a597e79cSChristoph Hellwig 
35233231e0eSKevin Wolf     s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
35333231e0eSKevin Wolf                                        &s->bus->dma->qiov, n * 4,
35433231e0eSKevin Wolf                                        ide_atapi_cmd_read_dma_cb, s);
35533231e0eSKevin Wolf     if (!s->bus->dma->aiocb) {
35633231e0eSKevin Wolf         /* Note: media not present is the most likely case */
35733231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_NOT_READY,
35833231e0eSKevin Wolf                             ASC_MEDIUM_NOT_PRESENT);
35933231e0eSKevin Wolf         goto eot;
36033231e0eSKevin Wolf     }
361a597e79cSChristoph Hellwig 
362a597e79cSChristoph Hellwig     return;
363a597e79cSChristoph Hellwig eot:
364a597e79cSChristoph Hellwig     bdrv_acct_done(s->bs, &s->acct);
365a597e79cSChristoph Hellwig     s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
366a597e79cSChristoph Hellwig     ide_set_inactive(s);
36733231e0eSKevin Wolf }
36833231e0eSKevin Wolf 
36933231e0eSKevin Wolf /* start a CD-CDROM read command with DMA */
37033231e0eSKevin Wolf /* XXX: test if DMA is available */
37133231e0eSKevin Wolf static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
37233231e0eSKevin Wolf                                    int sector_size)
37333231e0eSKevin Wolf {
37433231e0eSKevin Wolf     s->lba = lba;
37533231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
37633231e0eSKevin Wolf     s->io_buffer_index = 0;
37733231e0eSKevin Wolf     s->io_buffer_size = 0;
37833231e0eSKevin Wolf     s->cd_sector_size = sector_size;
37933231e0eSKevin Wolf 
380a597e79cSChristoph Hellwig     bdrv_acct_start(s->bs, &s->acct, s->packet_transfer_size, BDRV_ACCT_READ);
381a597e79cSChristoph Hellwig 
38233231e0eSKevin Wolf     /* XXX: check if BUSY_STAT should be set */
38333231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
38433231e0eSKevin Wolf     s->bus->dma->ops->start_dma(s->bus->dma, s,
38533231e0eSKevin Wolf                                ide_atapi_cmd_read_dma_cb);
38633231e0eSKevin Wolf }
38733231e0eSKevin Wolf 
38833231e0eSKevin Wolf static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
38933231e0eSKevin Wolf                                int sector_size)
39033231e0eSKevin Wolf {
39133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
39233231e0eSKevin Wolf     printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
39333231e0eSKevin Wolf         lba, nb_sectors);
39433231e0eSKevin Wolf #endif
39533231e0eSKevin Wolf     if (s->atapi_dma) {
39633231e0eSKevin Wolf         ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
39733231e0eSKevin Wolf     } else {
39833231e0eSKevin Wolf         ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
39933231e0eSKevin Wolf     }
40033231e0eSKevin Wolf }
40133231e0eSKevin Wolf 
40233231e0eSKevin Wolf static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
40333231e0eSKevin Wolf                                             uint16_t profile)
40433231e0eSKevin Wolf {
40533231e0eSKevin Wolf     uint8_t *buf_profile = buf + 12; /* start of profiles */
40633231e0eSKevin Wolf 
40733231e0eSKevin Wolf     buf_profile += ((*index) * 4); /* start of indexed profile */
40833231e0eSKevin Wolf     cpu_to_ube16 (buf_profile, profile);
40933231e0eSKevin Wolf     buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
41033231e0eSKevin Wolf 
41133231e0eSKevin Wolf     /* each profile adds 4 bytes to the response */
41233231e0eSKevin Wolf     (*index)++;
41333231e0eSKevin Wolf     buf[11] += 4; /* Additional Length */
41433231e0eSKevin Wolf 
41533231e0eSKevin Wolf     return 4;
41633231e0eSKevin Wolf }
41733231e0eSKevin Wolf 
41833231e0eSKevin Wolf static int ide_dvd_read_structure(IDEState *s, int format,
41933231e0eSKevin Wolf                                   const uint8_t *packet, uint8_t *buf)
42033231e0eSKevin Wolf {
42133231e0eSKevin Wolf     switch (format) {
42233231e0eSKevin Wolf         case 0x0: /* Physical format information */
42333231e0eSKevin Wolf             {
42433231e0eSKevin Wolf                 int layer = packet[6];
42533231e0eSKevin Wolf                 uint64_t total_sectors;
42633231e0eSKevin Wolf 
42733231e0eSKevin Wolf                 if (layer != 0)
42833231e0eSKevin Wolf                     return -ASC_INV_FIELD_IN_CMD_PACKET;
42933231e0eSKevin Wolf 
430e119bcacSKevin Wolf                 total_sectors = s->nb_sectors >> 2;
431e119bcacSKevin Wolf                 if (total_sectors == 0) {
43233231e0eSKevin Wolf                     return -ASC_MEDIUM_NOT_PRESENT;
433e119bcacSKevin Wolf                 }
43433231e0eSKevin Wolf 
43533231e0eSKevin Wolf                 buf[4] = 1;   /* DVD-ROM, part version 1 */
43633231e0eSKevin Wolf                 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
43733231e0eSKevin Wolf                 buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
43833231e0eSKevin Wolf                 buf[7] = 0;   /* default densities */
43933231e0eSKevin Wolf 
44033231e0eSKevin Wolf                 /* FIXME: 0x30000 per spec? */
44133231e0eSKevin Wolf                 cpu_to_ube32(buf + 8, 0); /* start sector */
44233231e0eSKevin Wolf                 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
44333231e0eSKevin Wolf                 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
44433231e0eSKevin Wolf 
44533231e0eSKevin Wolf                 /* Size of buffer, not including 2 byte size field */
44633231e0eSKevin Wolf                 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
44733231e0eSKevin Wolf 
44833231e0eSKevin Wolf                 /* 2k data + 4 byte header */
44933231e0eSKevin Wolf                 return (2048 + 4);
45033231e0eSKevin Wolf             }
45133231e0eSKevin Wolf 
45233231e0eSKevin Wolf         case 0x01: /* DVD copyright information */
45333231e0eSKevin Wolf             buf[4] = 0; /* no copyright data */
45433231e0eSKevin Wolf             buf[5] = 0; /* no region restrictions */
45533231e0eSKevin Wolf 
45633231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
45733231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)buf, 4 + 2);
45833231e0eSKevin Wolf 
45933231e0eSKevin Wolf             /* 4 byte header + 4 byte data */
46033231e0eSKevin Wolf             return (4 + 4);
46133231e0eSKevin Wolf 
46233231e0eSKevin Wolf         case 0x03: /* BCA information - invalid field for no BCA info */
46333231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
46433231e0eSKevin Wolf 
46533231e0eSKevin Wolf         case 0x04: /* DVD disc manufacturing information */
46633231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
46733231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
46833231e0eSKevin Wolf 
46933231e0eSKevin Wolf             /* 2k data + 4 byte header */
47033231e0eSKevin Wolf             return (2048 + 4);
47133231e0eSKevin Wolf 
47233231e0eSKevin Wolf         case 0xff:
47333231e0eSKevin Wolf             /*
47433231e0eSKevin Wolf              * This lists all the command capabilities above.  Add new ones
47533231e0eSKevin Wolf              * in order and update the length and buffer return values.
47633231e0eSKevin Wolf              */
47733231e0eSKevin Wolf 
47833231e0eSKevin Wolf             buf[4] = 0x00; /* Physical format */
47933231e0eSKevin Wolf             buf[5] = 0x40; /* Not writable, is readable */
48033231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
48133231e0eSKevin Wolf 
48233231e0eSKevin Wolf             buf[8] = 0x01; /* Copyright info */
48333231e0eSKevin Wolf             buf[9] = 0x40; /* Not writable, is readable */
48433231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
48533231e0eSKevin Wolf 
48633231e0eSKevin Wolf             buf[12] = 0x03; /* BCA info */
48733231e0eSKevin Wolf             buf[13] = 0x40; /* Not writable, is readable */
48833231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
48933231e0eSKevin Wolf 
49033231e0eSKevin Wolf             buf[16] = 0x04; /* Manufacturing info */
49133231e0eSKevin Wolf             buf[17] = 0x40; /* Not writable, is readable */
49233231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
49333231e0eSKevin Wolf 
49433231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
49533231e0eSKevin Wolf             cpu_to_be16wu((uint16_t *)buf, 16 + 2);
49633231e0eSKevin Wolf 
49733231e0eSKevin Wolf             /* data written + 4 byte header */
49833231e0eSKevin Wolf             return (16 + 4);
49933231e0eSKevin Wolf 
50033231e0eSKevin Wolf         default: /* TODO: formats beyond DVD-ROM requires */
50133231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
50233231e0eSKevin Wolf     }
50333231e0eSKevin Wolf }
50433231e0eSKevin Wolf 
50533231e0eSKevin Wolf static unsigned int event_status_media(IDEState *s,
50633231e0eSKevin Wolf                                        uint8_t *buf)
50733231e0eSKevin Wolf {
50833231e0eSKevin Wolf     enum media_event_code {
50933231e0eSKevin Wolf         MEC_NO_CHANGE = 0,       /* Status unchanged */
51033231e0eSKevin Wolf         MEC_EJECT_REQUESTED,     /* received a request from user to eject */
51133231e0eSKevin Wolf         MEC_NEW_MEDIA,           /* new media inserted and ready for access */
51233231e0eSKevin Wolf         MEC_MEDIA_REMOVAL,       /* only for media changers */
51333231e0eSKevin Wolf         MEC_MEDIA_CHANGED,       /* only for media changers */
51433231e0eSKevin Wolf         MEC_BG_FORMAT_COMPLETED, /* MRW or DVD+RW b/g format completed */
51533231e0eSKevin Wolf         MEC_BG_FORMAT_RESTARTED, /* MRW or DVD+RW b/g format restarted */
51633231e0eSKevin Wolf     };
51733231e0eSKevin Wolf     enum media_status {
51833231e0eSKevin Wolf         MS_TRAY_OPEN = 1,
51933231e0eSKevin Wolf         MS_MEDIA_PRESENT = 2,
52033231e0eSKevin Wolf     };
52133231e0eSKevin Wolf     uint8_t event_code, media_status;
52233231e0eSKevin Wolf 
52333231e0eSKevin Wolf     media_status = 0;
52433231e0eSKevin Wolf     if (s->bs->tray_open) {
52533231e0eSKevin Wolf         media_status = MS_TRAY_OPEN;
52633231e0eSKevin Wolf     } else if (bdrv_is_inserted(s->bs)) {
52733231e0eSKevin Wolf         media_status = MS_MEDIA_PRESENT;
52833231e0eSKevin Wolf     }
52933231e0eSKevin Wolf 
53033231e0eSKevin Wolf     /* Event notification descriptor */
53133231e0eSKevin Wolf     event_code = MEC_NO_CHANGE;
53233231e0eSKevin Wolf     if (media_status != MS_TRAY_OPEN && s->events.new_media) {
53333231e0eSKevin Wolf         event_code = MEC_NEW_MEDIA;
53433231e0eSKevin Wolf         s->events.new_media = false;
53533231e0eSKevin Wolf     }
53633231e0eSKevin Wolf 
53733231e0eSKevin Wolf     buf[4] = event_code;
53833231e0eSKevin Wolf     buf[5] = media_status;
53933231e0eSKevin Wolf 
54033231e0eSKevin Wolf     /* These fields are reserved, just clear them. */
54133231e0eSKevin Wolf     buf[6] = 0;
54233231e0eSKevin Wolf     buf[7] = 0;
54333231e0eSKevin Wolf 
54433231e0eSKevin Wolf     return 8; /* We wrote to 4 extra bytes from the header */
54533231e0eSKevin Wolf }
54633231e0eSKevin Wolf 
547e1a064f9SKevin Wolf static void cmd_get_event_status_notification(IDEState *s,
548e1a064f9SKevin Wolf                                               uint8_t *buf)
54933231e0eSKevin Wolf {
550e1a064f9SKevin Wolf     const uint8_t *packet = buf;
551e1a064f9SKevin Wolf 
55233231e0eSKevin Wolf     struct {
55333231e0eSKevin Wolf         uint8_t opcode;
55433231e0eSKevin Wolf         uint8_t polled;        /* lsb bit is polled; others are reserved */
55533231e0eSKevin Wolf         uint8_t reserved2[2];
55633231e0eSKevin Wolf         uint8_t class;
55733231e0eSKevin Wolf         uint8_t reserved3[2];
55833231e0eSKevin Wolf         uint16_t len;
55933231e0eSKevin Wolf         uint8_t control;
560*541dc0d4SStefan Weil     } QEMU_PACKED *gesn_cdb;
56133231e0eSKevin Wolf 
56233231e0eSKevin Wolf     struct {
56333231e0eSKevin Wolf         uint16_t len;
56433231e0eSKevin Wolf         uint8_t notification_class;
56533231e0eSKevin Wolf         uint8_t supported_events;
566*541dc0d4SStefan Weil     } QEMU_PACKED *gesn_event_header;
56733231e0eSKevin Wolf 
56833231e0eSKevin Wolf     enum notification_class_request_type {
56933231e0eSKevin Wolf         NCR_RESERVED1 = 1 << 0,
57033231e0eSKevin Wolf         NCR_OPERATIONAL_CHANGE = 1 << 1,
57133231e0eSKevin Wolf         NCR_POWER_MANAGEMENT = 1 << 2,
57233231e0eSKevin Wolf         NCR_EXTERNAL_REQUEST = 1 << 3,
57333231e0eSKevin Wolf         NCR_MEDIA = 1 << 4,
57433231e0eSKevin Wolf         NCR_MULTI_HOST = 1 << 5,
57533231e0eSKevin Wolf         NCR_DEVICE_BUSY = 1 << 6,
57633231e0eSKevin Wolf         NCR_RESERVED2 = 1 << 7,
57733231e0eSKevin Wolf     };
57833231e0eSKevin Wolf     enum event_notification_class_field {
57933231e0eSKevin Wolf         ENC_NO_EVENTS = 0,
58033231e0eSKevin Wolf         ENC_OPERATIONAL_CHANGE,
58133231e0eSKevin Wolf         ENC_POWER_MANAGEMENT,
58233231e0eSKevin Wolf         ENC_EXTERNAL_REQUEST,
58333231e0eSKevin Wolf         ENC_MEDIA,
58433231e0eSKevin Wolf         ENC_MULTIPLE_HOSTS,
58533231e0eSKevin Wolf         ENC_DEVICE_BUSY,
58633231e0eSKevin Wolf         ENC_RESERVED,
58733231e0eSKevin Wolf     };
58833231e0eSKevin Wolf     unsigned int max_len, used_len;
58933231e0eSKevin Wolf 
59033231e0eSKevin Wolf     gesn_cdb = (void *)packet;
59133231e0eSKevin Wolf     gesn_event_header = (void *)buf;
59233231e0eSKevin Wolf 
59333231e0eSKevin Wolf     max_len = be16_to_cpu(gesn_cdb->len);
59433231e0eSKevin Wolf 
59533231e0eSKevin Wolf     /* It is fine by the MMC spec to not support async mode operations */
59633231e0eSKevin Wolf     if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
59733231e0eSKevin Wolf         /* Only polling is supported, asynchronous mode is not. */
59833231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
59933231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
60033231e0eSKevin Wolf         return;
60133231e0eSKevin Wolf     }
60233231e0eSKevin Wolf 
60333231e0eSKevin Wolf     /* polling mode operation */
60433231e0eSKevin Wolf 
60533231e0eSKevin Wolf     /*
60633231e0eSKevin Wolf      * These are the supported events.
60733231e0eSKevin Wolf      *
60833231e0eSKevin Wolf      * We currently only support requests of the 'media' type.
60933231e0eSKevin Wolf      */
61033231e0eSKevin Wolf     gesn_event_header->supported_events = NCR_MEDIA;
61133231e0eSKevin Wolf 
61233231e0eSKevin Wolf     /*
61333231e0eSKevin Wolf      * We use |= below to set the class field; other bits in this byte
61433231e0eSKevin Wolf      * are reserved now but this is useful to do if we have to use the
61533231e0eSKevin Wolf      * reserved fields later.
61633231e0eSKevin Wolf      */
61733231e0eSKevin Wolf     gesn_event_header->notification_class = 0;
61833231e0eSKevin Wolf 
61933231e0eSKevin Wolf     /*
62033231e0eSKevin Wolf      * Responses to requests are to be based on request priority.  The
62133231e0eSKevin Wolf      * notification_class_request_type enum above specifies the
62233231e0eSKevin Wolf      * priority: upper elements are higher prio than lower ones.
62333231e0eSKevin Wolf      */
62433231e0eSKevin Wolf     if (gesn_cdb->class & NCR_MEDIA) {
62533231e0eSKevin Wolf         gesn_event_header->notification_class |= ENC_MEDIA;
62633231e0eSKevin Wolf         used_len = event_status_media(s, buf);
62733231e0eSKevin Wolf     } else {
62833231e0eSKevin Wolf         gesn_event_header->notification_class = 0x80; /* No event available */
62933231e0eSKevin Wolf         used_len = sizeof(*gesn_event_header);
63033231e0eSKevin Wolf     }
63133231e0eSKevin Wolf     gesn_event_header->len = cpu_to_be16(used_len
63233231e0eSKevin Wolf                                          - sizeof(*gesn_event_header));
63333231e0eSKevin Wolf     ide_atapi_cmd_reply(s, used_len, max_len);
63433231e0eSKevin Wolf }
63533231e0eSKevin Wolf 
636a60cf7e7SKevin Wolf static void cmd_request_sense(IDEState *s, uint8_t *buf)
63733231e0eSKevin Wolf {
638a60cf7e7SKevin Wolf     int max_len = buf[4];
639a60cf7e7SKevin Wolf 
640a60cf7e7SKevin Wolf     memset(buf, 0, 18);
641a60cf7e7SKevin Wolf     buf[0] = 0x70 | (1 << 7);
642a60cf7e7SKevin Wolf     buf[2] = s->sense_key;
643a60cf7e7SKevin Wolf     buf[7] = 10;
644a60cf7e7SKevin Wolf     buf[12] = s->asc;
645a60cf7e7SKevin Wolf 
646a60cf7e7SKevin Wolf     if (s->sense_key == SENSE_UNIT_ATTENTION) {
647a60cf7e7SKevin Wolf         s->sense_key = SENSE_NONE;
648a60cf7e7SKevin Wolf     }
649a60cf7e7SKevin Wolf 
650a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, 18, max_len);
651a60cf7e7SKevin Wolf }
652a60cf7e7SKevin Wolf 
653a60cf7e7SKevin Wolf static void cmd_inquiry(IDEState *s, uint8_t *buf)
654a60cf7e7SKevin Wolf {
655a60cf7e7SKevin Wolf     int max_len = buf[4];
656a60cf7e7SKevin Wolf 
657a60cf7e7SKevin Wolf     buf[0] = 0x05; /* CD-ROM */
658a60cf7e7SKevin Wolf     buf[1] = 0x80; /* removable */
659a60cf7e7SKevin Wolf     buf[2] = 0x00; /* ISO */
660a60cf7e7SKevin Wolf     buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
661a60cf7e7SKevin Wolf     buf[4] = 31; /* additional length */
662a60cf7e7SKevin Wolf     buf[5] = 0; /* reserved */
663a60cf7e7SKevin Wolf     buf[6] = 0; /* reserved */
664a60cf7e7SKevin Wolf     buf[7] = 0; /* reserved */
665a60cf7e7SKevin Wolf     padstr8(buf + 8, 8, "QEMU");
666a60cf7e7SKevin Wolf     padstr8(buf + 16, 16, "QEMU DVD-ROM");
667a60cf7e7SKevin Wolf     padstr8(buf + 32, 4, s->version);
668a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, 36, max_len);
669a60cf7e7SKevin Wolf }
670a60cf7e7SKevin Wolf 
671a60cf7e7SKevin Wolf static void cmd_get_configuration(IDEState *s, uint8_t *buf)
672a60cf7e7SKevin Wolf {
673a60cf7e7SKevin Wolf     uint32_t len;
674a60cf7e7SKevin Wolf     uint8_t index = 0;
67533231e0eSKevin Wolf     int max_len;
67633231e0eSKevin Wolf 
677a60cf7e7SKevin Wolf     /* only feature 0 is supported */
678a60cf7e7SKevin Wolf     if (buf[2] != 0 || buf[3] != 0) {
679a60cf7e7SKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
680a60cf7e7SKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
68133231e0eSKevin Wolf         return;
68233231e0eSKevin Wolf     }
68333231e0eSKevin Wolf 
684a60cf7e7SKevin Wolf     /* XXX: could result in alignment problems in some architectures */
685a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 7);
686a60cf7e7SKevin Wolf 
687a60cf7e7SKevin Wolf     /*
688a60cf7e7SKevin Wolf      * XXX: avoid overflow for io_buffer if max_len is bigger than
689a60cf7e7SKevin Wolf      *      the size of that buffer (dimensioned to max number of
690a60cf7e7SKevin Wolf      *      sectors to transfer at once)
691a60cf7e7SKevin Wolf      *
692a60cf7e7SKevin Wolf      *      Only a problem if the feature/profiles grow.
693a60cf7e7SKevin Wolf      */
694a60cf7e7SKevin Wolf     if (max_len > 512) {
695a60cf7e7SKevin Wolf         /* XXX: assume 1 sector */
696a60cf7e7SKevin Wolf         max_len = 512;
69733231e0eSKevin Wolf     }
698a60cf7e7SKevin Wolf 
699a60cf7e7SKevin Wolf     memset(buf, 0, max_len);
700a60cf7e7SKevin Wolf     /*
701a60cf7e7SKevin Wolf      * the number of sectors from the media tells us which profile
702a60cf7e7SKevin Wolf      * to use as current.  0 means there is no media
703a60cf7e7SKevin Wolf      */
704a60cf7e7SKevin Wolf     if (media_is_dvd(s)) {
705a60cf7e7SKevin Wolf         cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
706a60cf7e7SKevin Wolf     } else if (media_is_cd(s)) {
707a60cf7e7SKevin Wolf         cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
70833231e0eSKevin Wolf     }
709a60cf7e7SKevin Wolf 
710a60cf7e7SKevin Wolf     buf[10] = 0x02 | 0x01; /* persistent and current */
711a60cf7e7SKevin Wolf     len = 12; /* headers: 8 + 4 */
712a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
713a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
714a60cf7e7SKevin Wolf     cpu_to_ube32(buf, len - 4); /* data length */
715a60cf7e7SKevin Wolf 
716a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, len, max_len);
717a60cf7e7SKevin Wolf }
718a60cf7e7SKevin Wolf 
719a60cf7e7SKevin Wolf static void cmd_mode_sense(IDEState *s, uint8_t *buf)
72033231e0eSKevin Wolf {
72133231e0eSKevin Wolf     int action, code;
722a60cf7e7SKevin Wolf     int max_len;
723a60cf7e7SKevin Wolf 
724a60cf7e7SKevin Wolf     if (buf[0] == GPCMD_MODE_SENSE_10) {
725a60cf7e7SKevin Wolf         max_len = ube16_to_cpu(buf + 7);
726a60cf7e7SKevin Wolf     } else {
727a60cf7e7SKevin Wolf         max_len = buf[4];
728a60cf7e7SKevin Wolf     }
729a60cf7e7SKevin Wolf 
730a60cf7e7SKevin Wolf     action = buf[2] >> 6;
731a60cf7e7SKevin Wolf     code = buf[2] & 0x3f;
732a60cf7e7SKevin Wolf 
73333231e0eSKevin Wolf     switch(action) {
73433231e0eSKevin Wolf     case 0: /* current values */
73533231e0eSKevin Wolf         switch(code) {
73633231e0eSKevin Wolf         case GPMODE_R_W_ERROR_PAGE: /* error recovery */
73733231e0eSKevin Wolf             cpu_to_ube16(&buf[0], 16 + 6);
73833231e0eSKevin Wolf             buf[2] = 0x70;
73933231e0eSKevin Wolf             buf[3] = 0;
74033231e0eSKevin Wolf             buf[4] = 0;
74133231e0eSKevin Wolf             buf[5] = 0;
74233231e0eSKevin Wolf             buf[6] = 0;
74333231e0eSKevin Wolf             buf[7] = 0;
74433231e0eSKevin Wolf 
74533231e0eSKevin Wolf             buf[8] = 0x01;
74633231e0eSKevin Wolf             buf[9] = 0x06;
74733231e0eSKevin Wolf             buf[10] = 0x00;
74833231e0eSKevin Wolf             buf[11] = 0x05;
74933231e0eSKevin Wolf             buf[12] = 0x00;
75033231e0eSKevin Wolf             buf[13] = 0x00;
75133231e0eSKevin Wolf             buf[14] = 0x00;
75233231e0eSKevin Wolf             buf[15] = 0x00;
75333231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 16, max_len);
75433231e0eSKevin Wolf             break;
75533231e0eSKevin Wolf         case GPMODE_AUDIO_CTL_PAGE:
75633231e0eSKevin Wolf             cpu_to_ube16(&buf[0], 24 + 6);
75733231e0eSKevin Wolf             buf[2] = 0x70;
75833231e0eSKevin Wolf             buf[3] = 0;
75933231e0eSKevin Wolf             buf[4] = 0;
76033231e0eSKevin Wolf             buf[5] = 0;
76133231e0eSKevin Wolf             buf[6] = 0;
76233231e0eSKevin Wolf             buf[7] = 0;
76333231e0eSKevin Wolf 
76433231e0eSKevin Wolf             /* Fill with CDROM audio volume */
76533231e0eSKevin Wolf             buf[17] = 0;
76633231e0eSKevin Wolf             buf[19] = 0;
76733231e0eSKevin Wolf             buf[21] = 0;
76833231e0eSKevin Wolf             buf[23] = 0;
76933231e0eSKevin Wolf 
77033231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 24, max_len);
77133231e0eSKevin Wolf             break;
77233231e0eSKevin Wolf         case GPMODE_CAPABILITIES_PAGE:
77333231e0eSKevin Wolf             cpu_to_ube16(&buf[0], 28 + 6);
77433231e0eSKevin Wolf             buf[2] = 0x70;
77533231e0eSKevin Wolf             buf[3] = 0;
77633231e0eSKevin Wolf             buf[4] = 0;
77733231e0eSKevin Wolf             buf[5] = 0;
77833231e0eSKevin Wolf             buf[6] = 0;
77933231e0eSKevin Wolf             buf[7] = 0;
78033231e0eSKevin Wolf 
78133231e0eSKevin Wolf             buf[8] = 0x2a;
78233231e0eSKevin Wolf             buf[9] = 0x12;
78333231e0eSKevin Wolf             buf[10] = 0x00;
78433231e0eSKevin Wolf             buf[11] = 0x00;
78533231e0eSKevin Wolf 
78633231e0eSKevin Wolf             /* Claim PLAY_AUDIO capability (0x01) since some Linux
78733231e0eSKevin Wolf                code checks for this to automount media. */
78833231e0eSKevin Wolf             buf[12] = 0x71;
78933231e0eSKevin Wolf             buf[13] = 3 << 5;
79033231e0eSKevin Wolf             buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
79133231e0eSKevin Wolf             if (bdrv_is_locked(s->bs))
79233231e0eSKevin Wolf                 buf[6] |= 1 << 1;
79333231e0eSKevin Wolf             buf[15] = 0x00;
79433231e0eSKevin Wolf             cpu_to_ube16(&buf[16], 706);
79533231e0eSKevin Wolf             buf[18] = 0;
79633231e0eSKevin Wolf             buf[19] = 2;
79733231e0eSKevin Wolf             cpu_to_ube16(&buf[20], 512);
79833231e0eSKevin Wolf             cpu_to_ube16(&buf[22], 706);
79933231e0eSKevin Wolf             buf[24] = 0;
80033231e0eSKevin Wolf             buf[25] = 0;
80133231e0eSKevin Wolf             buf[26] = 0;
80233231e0eSKevin Wolf             buf[27] = 0;
80333231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 28, max_len);
80433231e0eSKevin Wolf             break;
80533231e0eSKevin Wolf         default:
80633231e0eSKevin Wolf             goto error_cmd;
80733231e0eSKevin Wolf         }
80833231e0eSKevin Wolf         break;
80933231e0eSKevin Wolf     case 1: /* changeable values */
81033231e0eSKevin Wolf         goto error_cmd;
81133231e0eSKevin Wolf     case 2: /* default values */
81233231e0eSKevin Wolf         goto error_cmd;
81333231e0eSKevin Wolf     default:
81433231e0eSKevin Wolf     case 3: /* saved values */
81533231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
81633231e0eSKevin Wolf                             ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
81733231e0eSKevin Wolf         break;
81833231e0eSKevin Wolf     }
819a60cf7e7SKevin Wolf     return;
820a60cf7e7SKevin Wolf 
821a60cf7e7SKevin Wolf error_cmd:
822a60cf7e7SKevin Wolf     ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
82333231e0eSKevin Wolf }
824a60cf7e7SKevin Wolf 
825a60cf7e7SKevin Wolf static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
826a60cf7e7SKevin Wolf {
8277a2c4b82SKevin Wolf     /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
8287a2c4b82SKevin Wolf      * come here, we know that it's ready. */
82933231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
830a60cf7e7SKevin Wolf }
831a60cf7e7SKevin Wolf 
832a60cf7e7SKevin Wolf static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
833a60cf7e7SKevin Wolf {
834a60cf7e7SKevin Wolf     bdrv_set_locked(s->bs, buf[4] & 1);
835a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
836a60cf7e7SKevin Wolf }
837a60cf7e7SKevin Wolf 
838a60cf7e7SKevin Wolf static void cmd_read(IDEState *s, uint8_t* buf)
83933231e0eSKevin Wolf {
84033231e0eSKevin Wolf     int nb_sectors, lba;
84133231e0eSKevin Wolf 
842a60cf7e7SKevin Wolf     if (buf[0] == GPCMD_READ_10) {
843a60cf7e7SKevin Wolf         nb_sectors = ube16_to_cpu(buf + 7);
844a60cf7e7SKevin Wolf     } else {
845a60cf7e7SKevin Wolf         nb_sectors = ube32_to_cpu(buf + 6);
846a60cf7e7SKevin Wolf     }
847a60cf7e7SKevin Wolf 
848a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
84933231e0eSKevin Wolf     if (nb_sectors == 0) {
85033231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
851a60cf7e7SKevin Wolf         return;
85233231e0eSKevin Wolf     }
853a60cf7e7SKevin Wolf 
85433231e0eSKevin Wolf     ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
85533231e0eSKevin Wolf }
856a60cf7e7SKevin Wolf 
857a60cf7e7SKevin Wolf static void cmd_read_cd(IDEState *s, uint8_t* buf)
85833231e0eSKevin Wolf {
85933231e0eSKevin Wolf     int nb_sectors, lba, transfer_request;
86033231e0eSKevin Wolf 
861a60cf7e7SKevin Wolf     nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
862a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
863a60cf7e7SKevin Wolf 
86433231e0eSKevin Wolf     if (nb_sectors == 0) {
86533231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
866a60cf7e7SKevin Wolf         return;
86733231e0eSKevin Wolf     }
868a60cf7e7SKevin Wolf 
869a60cf7e7SKevin Wolf     transfer_request = buf[9];
87033231e0eSKevin Wolf     switch(transfer_request & 0xf8) {
87133231e0eSKevin Wolf     case 0x00:
87233231e0eSKevin Wolf         /* nothing */
87333231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
87433231e0eSKevin Wolf         break;
87533231e0eSKevin Wolf     case 0x10:
87633231e0eSKevin Wolf         /* normal read */
87733231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
87833231e0eSKevin Wolf         break;
87933231e0eSKevin Wolf     case 0xf8:
88033231e0eSKevin Wolf         /* read all data */
88133231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
88233231e0eSKevin Wolf         break;
88333231e0eSKevin Wolf     default:
88433231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
88533231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
88633231e0eSKevin Wolf         break;
88733231e0eSKevin Wolf     }
88833231e0eSKevin Wolf }
889a60cf7e7SKevin Wolf 
890a60cf7e7SKevin Wolf static void cmd_seek(IDEState *s, uint8_t* buf)
89133231e0eSKevin Wolf {
89233231e0eSKevin Wolf     unsigned int lba;
893e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
89433231e0eSKevin Wolf 
895a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
89633231e0eSKevin Wolf     if (lba >= total_sectors) {
897a60cf7e7SKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
898a60cf7e7SKevin Wolf         return;
89933231e0eSKevin Wolf     }
900a60cf7e7SKevin Wolf 
90133231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
90233231e0eSKevin Wolf }
903a60cf7e7SKevin Wolf 
904a60cf7e7SKevin Wolf static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
90533231e0eSKevin Wolf {
90633231e0eSKevin Wolf     int start, eject, sense, err = 0;
907a60cf7e7SKevin Wolf     start = buf[4] & 1;
908a60cf7e7SKevin Wolf     eject = (buf[4] >> 1) & 1;
90933231e0eSKevin Wolf 
91033231e0eSKevin Wolf     if (eject) {
91133231e0eSKevin Wolf         err = bdrv_eject(s->bs, !start);
91233231e0eSKevin Wolf     }
91333231e0eSKevin Wolf 
91433231e0eSKevin Wolf     switch (err) {
91533231e0eSKevin Wolf     case 0:
91633231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
91733231e0eSKevin Wolf         break;
91833231e0eSKevin Wolf     case -EBUSY:
91933231e0eSKevin Wolf         sense = SENSE_NOT_READY;
92033231e0eSKevin Wolf         if (bdrv_is_inserted(s->bs)) {
92133231e0eSKevin Wolf             sense = SENSE_ILLEGAL_REQUEST;
92233231e0eSKevin Wolf         }
923a60cf7e7SKevin Wolf         ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
92433231e0eSKevin Wolf         break;
92533231e0eSKevin Wolf     default:
926a60cf7e7SKevin Wolf         ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
92733231e0eSKevin Wolf         break;
92833231e0eSKevin Wolf     }
92933231e0eSKevin Wolf }
930a60cf7e7SKevin Wolf 
931a60cf7e7SKevin Wolf static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
93233231e0eSKevin Wolf {
933a60cf7e7SKevin Wolf     int max_len = ube16_to_cpu(buf + 8);
934a60cf7e7SKevin Wolf 
93533231e0eSKevin Wolf     cpu_to_ube16(buf, 0);
93633231e0eSKevin Wolf     /* no current LBA */
93733231e0eSKevin Wolf     buf[2] = 0;
93833231e0eSKevin Wolf     buf[3] = 0;
93933231e0eSKevin Wolf     buf[4] = 0;
94033231e0eSKevin Wolf     buf[5] = 1;
94133231e0eSKevin Wolf     cpu_to_ube16(buf + 6, 0);
94233231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, max_len);
94333231e0eSKevin Wolf }
944a60cf7e7SKevin Wolf 
945a60cf7e7SKevin Wolf static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
94633231e0eSKevin Wolf {
94733231e0eSKevin Wolf     int format, msf, start_track, len;
948a60cf7e7SKevin Wolf     int max_len;
9497a2c4b82SKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
950a60cf7e7SKevin Wolf 
951a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 7);
952a60cf7e7SKevin Wolf     format = buf[9] >> 6;
953a60cf7e7SKevin Wolf     msf = (buf[1] >> 1) & 1;
954a60cf7e7SKevin Wolf     start_track = buf[6];
955a60cf7e7SKevin Wolf 
95633231e0eSKevin Wolf     switch(format) {
95733231e0eSKevin Wolf     case 0:
95833231e0eSKevin Wolf         len = cdrom_read_toc(total_sectors, buf, msf, start_track);
95933231e0eSKevin Wolf         if (len < 0)
96033231e0eSKevin Wolf             goto error_cmd;
96133231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
96233231e0eSKevin Wolf         break;
96333231e0eSKevin Wolf     case 1:
96433231e0eSKevin Wolf         /* multi session : only a single session defined */
96533231e0eSKevin Wolf         memset(buf, 0, 12);
96633231e0eSKevin Wolf         buf[1] = 0x0a;
96733231e0eSKevin Wolf         buf[2] = 0x01;
96833231e0eSKevin Wolf         buf[3] = 0x01;
96933231e0eSKevin Wolf         ide_atapi_cmd_reply(s, 12, max_len);
97033231e0eSKevin Wolf         break;
97133231e0eSKevin Wolf     case 2:
97233231e0eSKevin Wolf         len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
97333231e0eSKevin Wolf         if (len < 0)
97433231e0eSKevin Wolf             goto error_cmd;
97533231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
97633231e0eSKevin Wolf         break;
97733231e0eSKevin Wolf     default:
97833231e0eSKevin Wolf     error_cmd:
97933231e0eSKevin Wolf         ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
98033231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
98133231e0eSKevin Wolf     }
98233231e0eSKevin Wolf }
983a60cf7e7SKevin Wolf 
984a60cf7e7SKevin Wolf static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
98533231e0eSKevin Wolf {
986e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
98733231e0eSKevin Wolf 
98833231e0eSKevin Wolf     /* NOTE: it is really the number of sectors minus 1 */
98933231e0eSKevin Wolf     cpu_to_ube32(buf, total_sectors - 1);
99033231e0eSKevin Wolf     cpu_to_ube32(buf + 4, 2048);
99133231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, 8);
99233231e0eSKevin Wolf }
993a60cf7e7SKevin Wolf 
994a60cf7e7SKevin Wolf static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
99533231e0eSKevin Wolf {
996a60cf7e7SKevin Wolf     int max_len;
997a60cf7e7SKevin Wolf     int media = buf[1];
998a60cf7e7SKevin Wolf     int format = buf[7];
99933231e0eSKevin Wolf     int ret;
100033231e0eSKevin Wolf 
1001a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 8);
100233231e0eSKevin Wolf 
100333231e0eSKevin Wolf     if (format < 0xff) {
100433231e0eSKevin Wolf         if (media_is_cd(s)) {
100533231e0eSKevin Wolf             ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
100633231e0eSKevin Wolf                                 ASC_INCOMPATIBLE_FORMAT);
1007a60cf7e7SKevin Wolf             return;
100833231e0eSKevin Wolf         } else if (!media_present(s)) {
100933231e0eSKevin Wolf             ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
101033231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
1011a60cf7e7SKevin Wolf             return;
101233231e0eSKevin Wolf         }
101333231e0eSKevin Wolf     }
101433231e0eSKevin Wolf 
101533231e0eSKevin Wolf     memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
101633231e0eSKevin Wolf            IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
101733231e0eSKevin Wolf 
101833231e0eSKevin Wolf     switch (format) {
101933231e0eSKevin Wolf         case 0x00 ... 0x7f:
102033231e0eSKevin Wolf         case 0xff:
102133231e0eSKevin Wolf             if (media == 0) {
1022a60cf7e7SKevin Wolf                 ret = ide_dvd_read_structure(s, format, buf, buf);
102333231e0eSKevin Wolf 
1024a60cf7e7SKevin Wolf                 if (ret < 0) {
102533231e0eSKevin Wolf                     ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1026a60cf7e7SKevin Wolf                 } else {
102733231e0eSKevin Wolf                     ide_atapi_cmd_reply(s, ret, max_len);
1028a60cf7e7SKevin Wolf                 }
102933231e0eSKevin Wolf 
103033231e0eSKevin Wolf                 break;
103133231e0eSKevin Wolf             }
103233231e0eSKevin Wolf             /* TODO: BD support, fall through for now */
103333231e0eSKevin Wolf 
103433231e0eSKevin Wolf         /* Generic disk structures */
103533231e0eSKevin Wolf         case 0x80: /* TODO: AACS volume identifier */
103633231e0eSKevin Wolf         case 0x81: /* TODO: AACS media serial number */
103733231e0eSKevin Wolf         case 0x82: /* TODO: AACS media identifier */
103833231e0eSKevin Wolf         case 0x83: /* TODO: AACS media key block */
103933231e0eSKevin Wolf         case 0x90: /* TODO: List of recognized format layers */
104033231e0eSKevin Wolf         case 0xc0: /* TODO: Write protection status */
104133231e0eSKevin Wolf         default:
104233231e0eSKevin Wolf             ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
104333231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
104433231e0eSKevin Wolf             break;
104533231e0eSKevin Wolf     }
104633231e0eSKevin Wolf }
1047a60cf7e7SKevin Wolf 
1048a60cf7e7SKevin Wolf static void cmd_set_speed(IDEState *s, uint8_t* buf)
1049a60cf7e7SKevin Wolf {
1050a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
1051a60cf7e7SKevin Wolf }
1052a60cf7e7SKevin Wolf 
1053e1a064f9SKevin Wolf enum {
1054e1a064f9SKevin Wolf     /*
1055e1a064f9SKevin Wolf      * Only commands flagged as ALLOW_UA are allowed to run under a
1056e1a064f9SKevin Wolf      * unit attention condition. (See MMC-5, section 4.1.6.1)
1057e1a064f9SKevin Wolf      */
1058e1a064f9SKevin Wolf     ALLOW_UA = 0x01,
10597a2c4b82SKevin Wolf 
10607a2c4b82SKevin Wolf     /*
10617a2c4b82SKevin Wolf      * Commands flagged with CHECK_READY can only execute if a medium is present.
10627a2c4b82SKevin Wolf      * Otherwise they report the Not Ready Condition. (See MMC-5, section
10637a2c4b82SKevin Wolf      * 4.1.8)
10647a2c4b82SKevin Wolf      */
10657a2c4b82SKevin Wolf     CHECK_READY = 0x02,
1066e1a064f9SKevin Wolf };
1067e1a064f9SKevin Wolf 
1068e1a064f9SKevin Wolf static const struct {
1069e1a064f9SKevin Wolf     void (*handler)(IDEState *s, uint8_t *buf);
1070e1a064f9SKevin Wolf     int flags;
1071e1a064f9SKevin Wolf } atapi_cmd_table[0x100] = {
10727a2c4b82SKevin Wolf     [ 0x00 ] = { cmd_test_unit_ready,               CHECK_READY },
1073e1a064f9SKevin Wolf     [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
1074e1a064f9SKevin Wolf     [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
1075e1a064f9SKevin Wolf     [ 0x1a ] = { cmd_mode_sense, /* (6) */          0 },
1076e1a064f9SKevin Wolf     [ 0x1b ] = { cmd_start_stop_unit,               0 },
1077e1a064f9SKevin Wolf     [ 0x1e ] = { cmd_prevent_allow_medium_removal,  0 },
10787a2c4b82SKevin Wolf     [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
1079e1a064f9SKevin Wolf     [ 0x28 ] = { cmd_read, /* (10) */               0 },
10807a2c4b82SKevin Wolf     [ 0x2b ] = { cmd_seek,                          CHECK_READY },
10817a2c4b82SKevin Wolf     [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
1082e1a064f9SKevin Wolf     [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
1083e1a064f9SKevin Wolf     [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1084e1a064f9SKevin Wolf     [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
1085e1a064f9SKevin Wolf     [ 0xa8 ] = { cmd_read, /* (12) */               0 },
1086e1a064f9SKevin Wolf     [ 0xad ] = { cmd_read_dvd_structure,            0 },
1087e1a064f9SKevin Wolf     [ 0xbb ] = { cmd_set_speed,                     0 },
1088e1a064f9SKevin Wolf     [ 0xbd ] = { cmd_mechanism_status,              0 },
1089e1a064f9SKevin Wolf     [ 0xbe ] = { cmd_read_cd,                       0 },
1090e1a064f9SKevin Wolf };
1091e1a064f9SKevin Wolf 
1092a60cf7e7SKevin Wolf void ide_atapi_cmd(IDEState *s)
1093a60cf7e7SKevin Wolf {
1094a60cf7e7SKevin Wolf     uint8_t *buf;
1095a60cf7e7SKevin Wolf 
1096a60cf7e7SKevin Wolf     buf = s->io_buffer;
1097a60cf7e7SKevin Wolf #ifdef DEBUG_IDE_ATAPI
1098a60cf7e7SKevin Wolf     {
1099a60cf7e7SKevin Wolf         int i;
1100a60cf7e7SKevin Wolf         printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1101a60cf7e7SKevin Wolf         for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1102ab719827SAlon Levy             printf(" %02x", buf[i]);
1103a60cf7e7SKevin Wolf         }
1104a60cf7e7SKevin Wolf         printf("\n");
1105a60cf7e7SKevin Wolf     }
1106a60cf7e7SKevin Wolf #endif
1107a60cf7e7SKevin Wolf     /*
1108e1a064f9SKevin Wolf      * If there's a UNIT_ATTENTION condition pending, only command flagged with
1109e1a064f9SKevin Wolf      * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1110e1a064f9SKevin Wolf      * condition response unless a higher priority status, defined by the drive
1111a60cf7e7SKevin Wolf      * here, is pending.
1112a60cf7e7SKevin Wolf      */
1113a60cf7e7SKevin Wolf     if (s->sense_key == SENSE_UNIT_ATTENTION &&
1114e1a064f9SKevin Wolf         !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
1115a60cf7e7SKevin Wolf         ide_atapi_cmd_check_status(s);
1116a60cf7e7SKevin Wolf         return;
1117a60cf7e7SKevin Wolf     }
11184a737d14SAmit Shah     /*
11194a737d14SAmit Shah      * When a CD gets changed, we have to report an ejected state and
11204a737d14SAmit Shah      * then a loaded state to guests so that they detect tray
11214a737d14SAmit Shah      * open/close and media change events.  Guests that do not use
11224a737d14SAmit Shah      * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
11234a737d14SAmit Shah      * states rely on this behavior.
11244a737d14SAmit Shah      */
1125a60cf7e7SKevin Wolf     if (bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1126a60cf7e7SKevin Wolf         ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1127a60cf7e7SKevin Wolf 
1128a60cf7e7SKevin Wolf         s->cdrom_changed = 0;
1129a60cf7e7SKevin Wolf         s->sense_key = SENSE_UNIT_ATTENTION;
1130a60cf7e7SKevin Wolf         s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1131a60cf7e7SKevin Wolf         return;
1132a60cf7e7SKevin Wolf     }
1133e1a064f9SKevin Wolf 
11347a2c4b82SKevin Wolf     /* Report a Not Ready condition if appropriate for the command */
11357a2c4b82SKevin Wolf     if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
11367a2c4b82SKevin Wolf         (!media_present(s) || !bdrv_is_inserted(s->bs)))
11377a2c4b82SKevin Wolf     {
11387a2c4b82SKevin Wolf         ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
11397a2c4b82SKevin Wolf         return;
11407a2c4b82SKevin Wolf     }
11417a2c4b82SKevin Wolf 
1142e1a064f9SKevin Wolf     /* Execute the command */
1143e1a064f9SKevin Wolf     if (atapi_cmd_table[s->io_buffer[0]].handler) {
1144e1a064f9SKevin Wolf         atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1145e1a064f9SKevin Wolf         return;
114633231e0eSKevin Wolf     }
1147e1a064f9SKevin Wolf 
1148e1a064f9SKevin Wolf     ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
114933231e0eSKevin Wolf }
1150