xref: /openbmc/qemu/hw/ide/atapi.c (revision af0e00db0e389dfa33d597f917a21454643bd314)
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"
270d09e41aSPaolo Bonzini #include "hw/scsi/scsi.h"
284be74634SMarkus Armbruster #include "sysemu/block-backend.h"
2933231e0eSKevin Wolf 
3033231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
3133231e0eSKevin Wolf 
3233231e0eSKevin Wolf static void padstr8(uint8_t *buf, int buf_size, const char *src)
3333231e0eSKevin Wolf {
3433231e0eSKevin Wolf     int i;
3533231e0eSKevin Wolf     for(i = 0; i < buf_size; i++) {
3633231e0eSKevin Wolf         if (*src)
3733231e0eSKevin Wolf             buf[i] = *src++;
3833231e0eSKevin Wolf         else
3933231e0eSKevin Wolf             buf[i] = ' ';
4033231e0eSKevin Wolf     }
4133231e0eSKevin Wolf }
4233231e0eSKevin Wolf 
4333231e0eSKevin Wolf static inline void cpu_to_ube16(uint8_t *buf, int val)
4433231e0eSKevin Wolf {
4533231e0eSKevin Wolf     buf[0] = val >> 8;
4633231e0eSKevin Wolf     buf[1] = val & 0xff;
4733231e0eSKevin Wolf }
4833231e0eSKevin Wolf 
4933231e0eSKevin Wolf static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
5033231e0eSKevin Wolf {
5133231e0eSKevin Wolf     buf[0] = val >> 24;
5233231e0eSKevin Wolf     buf[1] = val >> 16;
5333231e0eSKevin Wolf     buf[2] = val >> 8;
5433231e0eSKevin Wolf     buf[3] = val & 0xff;
5533231e0eSKevin Wolf }
5633231e0eSKevin Wolf 
5733231e0eSKevin Wolf static inline int ube16_to_cpu(const uint8_t *buf)
5833231e0eSKevin Wolf {
5933231e0eSKevin Wolf     return (buf[0] << 8) | buf[1];
6033231e0eSKevin Wolf }
6133231e0eSKevin Wolf 
6233231e0eSKevin Wolf static inline int ube32_to_cpu(const uint8_t *buf)
6333231e0eSKevin Wolf {
6433231e0eSKevin Wolf     return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
6533231e0eSKevin Wolf }
6633231e0eSKevin Wolf 
6733231e0eSKevin Wolf static void lba_to_msf(uint8_t *buf, int lba)
6833231e0eSKevin Wolf {
6933231e0eSKevin Wolf     lba += 150;
7033231e0eSKevin Wolf     buf[0] = (lba / 75) / 60;
7133231e0eSKevin Wolf     buf[1] = (lba / 75) % 60;
7233231e0eSKevin Wolf     buf[2] = lba % 75;
7333231e0eSKevin Wolf }
7433231e0eSKevin Wolf 
7533231e0eSKevin Wolf static inline int media_present(IDEState *s)
7633231e0eSKevin Wolf {
77a1aff5bfSMarkus Armbruster     return !s->tray_open && s->nb_sectors > 0;
7833231e0eSKevin Wolf }
7933231e0eSKevin Wolf 
80a7acf552SAmit Shah /* XXX: DVDs that could fit on a CD will be reported as a CD */
8133231e0eSKevin Wolf static inline int media_is_dvd(IDEState *s)
8233231e0eSKevin Wolf {
8333231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
8433231e0eSKevin Wolf }
8533231e0eSKevin Wolf 
8633231e0eSKevin Wolf static inline int media_is_cd(IDEState *s)
8733231e0eSKevin Wolf {
8833231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
8933231e0eSKevin Wolf }
9033231e0eSKevin Wolf 
9133231e0eSKevin Wolf static void cd_data_to_raw(uint8_t *buf, int lba)
9233231e0eSKevin Wolf {
9333231e0eSKevin Wolf     /* sync bytes */
9433231e0eSKevin Wolf     buf[0] = 0x00;
9533231e0eSKevin Wolf     memset(buf + 1, 0xff, 10);
9633231e0eSKevin Wolf     buf[11] = 0x00;
9733231e0eSKevin Wolf     buf += 12;
9833231e0eSKevin Wolf     /* MSF */
9933231e0eSKevin Wolf     lba_to_msf(buf, lba);
10033231e0eSKevin Wolf     buf[3] = 0x01; /* mode 1 data */
10133231e0eSKevin Wolf     buf += 4;
10233231e0eSKevin Wolf     /* data */
10333231e0eSKevin Wolf     buf += 2048;
10433231e0eSKevin Wolf     /* XXX: ECC not computed */
10533231e0eSKevin Wolf     memset(buf, 0, 288);
10633231e0eSKevin Wolf }
10733231e0eSKevin Wolf 
108a597e79cSChristoph Hellwig static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
10933231e0eSKevin Wolf {
11033231e0eSKevin Wolf     int ret;
111ece2d05eSAlberto Garcia     block_acct_start(blk_get_stats(s->blk), &s->acct,
112ece2d05eSAlberto Garcia                      4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
11333231e0eSKevin Wolf 
11433231e0eSKevin Wolf     switch(sector_size) {
11533231e0eSKevin Wolf     case 2048:
1164be74634SMarkus Armbruster         ret = blk_read(s->blk, (int64_t)lba << 2, buf, 4);
11733231e0eSKevin Wolf         break;
11833231e0eSKevin Wolf     case 2352:
1194be74634SMarkus Armbruster         ret = blk_read(s->blk, (int64_t)lba << 2, buf + 16, 4);
120ece2d05eSAlberto Garcia         if (ret >= 0) {
12133231e0eSKevin Wolf             cd_data_to_raw(buf, lba);
122ece2d05eSAlberto Garcia         }
12333231e0eSKevin Wolf         break;
12433231e0eSKevin Wolf     default:
125ece2d05eSAlberto Garcia         block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
126ece2d05eSAlberto Garcia         return -EIO;
12733231e0eSKevin Wolf     }
128ece2d05eSAlberto Garcia 
129ece2d05eSAlberto Garcia     if (ret < 0) {
130ece2d05eSAlberto Garcia         block_acct_failed(blk_get_stats(s->blk), &s->acct);
131ece2d05eSAlberto Garcia     } else {
132ece2d05eSAlberto Garcia         block_acct_done(blk_get_stats(s->blk), &s->acct);
133ece2d05eSAlberto Garcia     }
134ece2d05eSAlberto Garcia 
13533231e0eSKevin Wolf     return ret;
13633231e0eSKevin Wolf }
13733231e0eSKevin Wolf 
13833231e0eSKevin Wolf void ide_atapi_cmd_ok(IDEState *s)
13933231e0eSKevin Wolf {
14033231e0eSKevin Wolf     s->error = 0;
14133231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT;
14233231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
143d735b620SJohn Snow     ide_transfer_stop(s);
14433231e0eSKevin Wolf     ide_set_irq(s->bus);
14533231e0eSKevin Wolf }
14633231e0eSKevin Wolf 
14733231e0eSKevin Wolf void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
14833231e0eSKevin Wolf {
14933231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
15033231e0eSKevin Wolf     printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
15133231e0eSKevin Wolf #endif
15233231e0eSKevin Wolf     s->error = sense_key << 4;
15333231e0eSKevin Wolf     s->status = READY_STAT | ERR_STAT;
15433231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
15533231e0eSKevin Wolf     s->sense_key = sense_key;
15633231e0eSKevin Wolf     s->asc = asc;
157d735b620SJohn Snow     ide_transfer_stop(s);
15833231e0eSKevin Wolf     ide_set_irq(s->bus);
15933231e0eSKevin Wolf }
16033231e0eSKevin Wolf 
16133231e0eSKevin Wolf void ide_atapi_io_error(IDEState *s, int ret)
16233231e0eSKevin Wolf {
16333231e0eSKevin Wolf     /* XXX: handle more errors */
16433231e0eSKevin Wolf     if (ret == -ENOMEDIUM) {
16567cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, NOT_READY,
16633231e0eSKevin Wolf                             ASC_MEDIUM_NOT_PRESENT);
16733231e0eSKevin Wolf     } else {
16867cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
16933231e0eSKevin Wolf                             ASC_LOGICAL_BLOCK_OOR);
17033231e0eSKevin Wolf     }
17133231e0eSKevin Wolf }
17233231e0eSKevin Wolf 
173*af0e00dbSJohn Snow static uint16_t atapi_byte_count_limit(IDEState *s)
174*af0e00dbSJohn Snow {
175*af0e00dbSJohn Snow     uint16_t bcl;
176*af0e00dbSJohn Snow 
177*af0e00dbSJohn Snow     bcl = s->lcyl | (s->hcyl << 8);
178*af0e00dbSJohn Snow     if (bcl == 0xffff) {
179*af0e00dbSJohn Snow         return 0xfffe;
180*af0e00dbSJohn Snow     }
181*af0e00dbSJohn Snow     return bcl;
182*af0e00dbSJohn Snow }
183*af0e00dbSJohn Snow 
18433231e0eSKevin Wolf /* The whole ATAPI transfer logic is handled in this function */
18533231e0eSKevin Wolf void ide_atapi_cmd_reply_end(IDEState *s)
18633231e0eSKevin Wolf {
18733231e0eSKevin Wolf     int byte_count_limit, size, ret;
18833231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
18933231e0eSKevin Wolf     printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
19033231e0eSKevin Wolf            s->packet_transfer_size,
19133231e0eSKevin Wolf            s->elementary_transfer_size,
19233231e0eSKevin Wolf            s->io_buffer_index);
19333231e0eSKevin Wolf #endif
19433231e0eSKevin Wolf     if (s->packet_transfer_size <= 0) {
19533231e0eSKevin Wolf         /* end of transfer */
196d735b620SJohn Snow         ide_atapi_cmd_ok(s);
19733231e0eSKevin Wolf         ide_set_irq(s->bus);
19833231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
19933231e0eSKevin Wolf         printf("status=0x%x\n", s->status);
20033231e0eSKevin Wolf #endif
20133231e0eSKevin Wolf     } else {
20233231e0eSKevin Wolf         /* see if a new sector must be read */
20333231e0eSKevin Wolf         if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
204a597e79cSChristoph Hellwig             ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
20533231e0eSKevin Wolf             if (ret < 0) {
20633231e0eSKevin Wolf                 ide_atapi_io_error(s, ret);
20733231e0eSKevin Wolf                 return;
20833231e0eSKevin Wolf             }
20933231e0eSKevin Wolf             s->lba++;
21033231e0eSKevin Wolf             s->io_buffer_index = 0;
21133231e0eSKevin Wolf         }
21233231e0eSKevin Wolf         if (s->elementary_transfer_size > 0) {
21333231e0eSKevin Wolf             /* there are some data left to transmit in this elementary
21433231e0eSKevin Wolf                transfer */
21533231e0eSKevin Wolf             size = s->cd_sector_size - s->io_buffer_index;
21633231e0eSKevin Wolf             if (size > s->elementary_transfer_size)
21733231e0eSKevin Wolf                 size = s->elementary_transfer_size;
21833231e0eSKevin Wolf             s->packet_transfer_size -= size;
21933231e0eSKevin Wolf             s->elementary_transfer_size -= size;
22033231e0eSKevin Wolf             s->io_buffer_index += size;
22133231e0eSKevin Wolf             ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
22233231e0eSKevin Wolf                                size, ide_atapi_cmd_reply_end);
22333231e0eSKevin Wolf         } else {
22433231e0eSKevin Wolf             /* a new transfer is needed */
22533231e0eSKevin Wolf             s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
226*af0e00dbSJohn Snow             byte_count_limit = atapi_byte_count_limit(s);
22733231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
22833231e0eSKevin Wolf             printf("byte_count_limit=%d\n", byte_count_limit);
22933231e0eSKevin Wolf #endif
23033231e0eSKevin Wolf             size = s->packet_transfer_size;
23133231e0eSKevin Wolf             if (size > byte_count_limit) {
23233231e0eSKevin Wolf                 /* byte count limit must be even if this case */
23333231e0eSKevin Wolf                 if (byte_count_limit & 1)
23433231e0eSKevin Wolf                     byte_count_limit--;
23533231e0eSKevin Wolf                 size = byte_count_limit;
23633231e0eSKevin Wolf             }
23733231e0eSKevin Wolf             s->lcyl = size;
23833231e0eSKevin Wolf             s->hcyl = size >> 8;
23933231e0eSKevin Wolf             s->elementary_transfer_size = size;
24033231e0eSKevin Wolf             /* we cannot transmit more than one sector at a time */
24133231e0eSKevin Wolf             if (s->lba != -1) {
24233231e0eSKevin Wolf                 if (size > (s->cd_sector_size - s->io_buffer_index))
24333231e0eSKevin Wolf                     size = (s->cd_sector_size - s->io_buffer_index);
24433231e0eSKevin Wolf             }
24533231e0eSKevin Wolf             s->packet_transfer_size -= size;
24633231e0eSKevin Wolf             s->elementary_transfer_size -= size;
24733231e0eSKevin Wolf             s->io_buffer_index += size;
24833231e0eSKevin Wolf             ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
24933231e0eSKevin Wolf                                size, ide_atapi_cmd_reply_end);
25033231e0eSKevin Wolf             ide_set_irq(s->bus);
25133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
25233231e0eSKevin Wolf             printf("status=0x%x\n", s->status);
25333231e0eSKevin Wolf #endif
25433231e0eSKevin Wolf         }
25533231e0eSKevin Wolf     }
25633231e0eSKevin Wolf }
25733231e0eSKevin Wolf 
25833231e0eSKevin Wolf /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
25933231e0eSKevin Wolf static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
26033231e0eSKevin Wolf {
26133231e0eSKevin Wolf     if (size > max_size)
26233231e0eSKevin Wolf         size = max_size;
26333231e0eSKevin Wolf     s->lba = -1; /* no sector read */
26433231e0eSKevin Wolf     s->packet_transfer_size = size;
26533231e0eSKevin Wolf     s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
26633231e0eSKevin Wolf     s->elementary_transfer_size = 0;
26733231e0eSKevin Wolf 
26833231e0eSKevin Wolf     if (s->atapi_dma) {
2694be74634SMarkus Armbruster         block_acct_start(blk_get_stats(s->blk), &s->acct, size,
2705366d0c8SBenoît Canet                          BLOCK_ACCT_READ);
27133231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
2724855b576SPaolo Bonzini         ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
27333231e0eSKevin Wolf     } else {
27433231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
275c71c06d4SPaolo Bonzini         s->io_buffer_index = 0;
27633231e0eSKevin Wolf         ide_atapi_cmd_reply_end(s);
27733231e0eSKevin Wolf     }
27833231e0eSKevin Wolf }
27933231e0eSKevin Wolf 
28033231e0eSKevin Wolf /* start a CD-CDROM read command */
28133231e0eSKevin Wolf static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
28233231e0eSKevin Wolf                                    int sector_size)
28333231e0eSKevin Wolf {
28433231e0eSKevin Wolf     s->lba = lba;
28533231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
28633231e0eSKevin Wolf     s->elementary_transfer_size = 0;
28733231e0eSKevin Wolf     s->io_buffer_index = sector_size;
28833231e0eSKevin Wolf     s->cd_sector_size = sector_size;
28933231e0eSKevin Wolf 
29033231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT;
29133231e0eSKevin Wolf     ide_atapi_cmd_reply_end(s);
29233231e0eSKevin Wolf }
29333231e0eSKevin Wolf 
29433231e0eSKevin Wolf static void ide_atapi_cmd_check_status(IDEState *s)
29533231e0eSKevin Wolf {
29633231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
29733231e0eSKevin Wolf     printf("atapi_cmd_check_status\n");
29833231e0eSKevin Wolf #endif
29967cc61e4SPaolo Bonzini     s->error = MC_ERR | (UNIT_ATTENTION << 4);
30033231e0eSKevin Wolf     s->status = ERR_STAT;
30133231e0eSKevin Wolf     s->nsector = 0;
30233231e0eSKevin Wolf     ide_set_irq(s->bus);
30333231e0eSKevin Wolf }
30433231e0eSKevin Wolf /* ATAPI DMA support */
30533231e0eSKevin Wolf 
30633231e0eSKevin Wolf /* XXX: handle read errors */
30733231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
30833231e0eSKevin Wolf {
30933231e0eSKevin Wolf     IDEState *s = opaque;
31033231e0eSKevin Wolf     int data_offset, n;
31133231e0eSKevin Wolf 
31233231e0eSKevin Wolf     if (ret < 0) {
31333231e0eSKevin Wolf         ide_atapi_io_error(s, ret);
31433231e0eSKevin Wolf         goto eot;
31533231e0eSKevin Wolf     }
31633231e0eSKevin Wolf 
31733231e0eSKevin Wolf     if (s->io_buffer_size > 0) {
31833231e0eSKevin Wolf         /*
31933231e0eSKevin Wolf          * For a cdrom read sector command (s->lba != -1),
32033231e0eSKevin Wolf          * adjust the lba for the next s->io_buffer_size chunk
32133231e0eSKevin Wolf          * and dma the current chunk.
32233231e0eSKevin Wolf          * For a command != read (s->lba == -1), just transfer
32333231e0eSKevin Wolf          * the reply data.
32433231e0eSKevin Wolf          */
32533231e0eSKevin Wolf         if (s->lba != -1) {
32633231e0eSKevin Wolf             if (s->cd_sector_size == 2352) {
32733231e0eSKevin Wolf                 n = 1;
32833231e0eSKevin Wolf                 cd_data_to_raw(s->io_buffer, s->lba);
32933231e0eSKevin Wolf             } else {
33033231e0eSKevin Wolf                 n = s->io_buffer_size >> 11;
33133231e0eSKevin Wolf             }
33233231e0eSKevin Wolf             s->lba += n;
33333231e0eSKevin Wolf         }
33433231e0eSKevin Wolf         s->packet_transfer_size -= s->io_buffer_size;
33533231e0eSKevin Wolf         if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
33633231e0eSKevin Wolf             goto eot;
33733231e0eSKevin Wolf     }
33833231e0eSKevin Wolf 
33933231e0eSKevin Wolf     if (s->packet_transfer_size <= 0) {
34033231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
34133231e0eSKevin Wolf         s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
34233231e0eSKevin Wolf         ide_set_irq(s->bus);
343a597e79cSChristoph Hellwig         goto eot;
34433231e0eSKevin Wolf     }
34533231e0eSKevin Wolf 
34633231e0eSKevin Wolf     s->io_buffer_index = 0;
34733231e0eSKevin Wolf     if (s->cd_sector_size == 2352) {
34833231e0eSKevin Wolf         n = 1;
34933231e0eSKevin Wolf         s->io_buffer_size = s->cd_sector_size;
35033231e0eSKevin Wolf         data_offset = 16;
35133231e0eSKevin Wolf     } else {
35233231e0eSKevin Wolf         n = s->packet_transfer_size >> 11;
35333231e0eSKevin Wolf         if (n > (IDE_DMA_BUF_SECTORS / 4))
35433231e0eSKevin Wolf             n = (IDE_DMA_BUF_SECTORS / 4);
35533231e0eSKevin Wolf         s->io_buffer_size = n * 2048;
35633231e0eSKevin Wolf         data_offset = 0;
35733231e0eSKevin Wolf     }
35833231e0eSKevin Wolf #ifdef DEBUG_AIO
35933231e0eSKevin Wolf     printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
36033231e0eSKevin Wolf #endif
361a597e79cSChristoph Hellwig 
36233231e0eSKevin Wolf     s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
36333231e0eSKevin Wolf     s->bus->dma->iov.iov_len = n * 4 * 512;
36433231e0eSKevin Wolf     qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
365a597e79cSChristoph Hellwig 
3664be74634SMarkus Armbruster     s->bus->dma->aiocb = blk_aio_readv(s->blk, (int64_t)s->lba << 2,
36733231e0eSKevin Wolf                                        &s->bus->dma->qiov, n * 4,
36833231e0eSKevin Wolf                                        ide_atapi_cmd_read_dma_cb, s);
369a597e79cSChristoph Hellwig     return;
370ad54ae80SPaolo Bonzini 
371a597e79cSChristoph Hellwig eot:
372ece2d05eSAlberto Garcia     if (ret < 0) {
373ece2d05eSAlberto Garcia         block_acct_failed(blk_get_stats(s->blk), &s->acct);
374ece2d05eSAlberto Garcia     } else {
3754be74634SMarkus Armbruster         block_acct_done(blk_get_stats(s->blk), &s->acct);
376ece2d05eSAlberto Garcia     }
3770e7ce54cSPaolo Bonzini     ide_set_inactive(s, false);
37833231e0eSKevin Wolf }
37933231e0eSKevin Wolf 
38033231e0eSKevin Wolf /* start a CD-CDROM read command with DMA */
38133231e0eSKevin Wolf /* XXX: test if DMA is available */
38233231e0eSKevin Wolf static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
38333231e0eSKevin Wolf                                    int sector_size)
38433231e0eSKevin Wolf {
38533231e0eSKevin Wolf     s->lba = lba;
38633231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
38733231e0eSKevin Wolf     s->io_buffer_size = 0;
38833231e0eSKevin Wolf     s->cd_sector_size = sector_size;
38933231e0eSKevin Wolf 
3904be74634SMarkus Armbruster     block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
3915366d0c8SBenoît Canet                      BLOCK_ACCT_READ);
392a597e79cSChristoph Hellwig 
39333231e0eSKevin Wolf     /* XXX: check if BUSY_STAT should be set */
39433231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
3954855b576SPaolo Bonzini     ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
39633231e0eSKevin Wolf }
39733231e0eSKevin Wolf 
39833231e0eSKevin Wolf static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
39933231e0eSKevin Wolf                                int sector_size)
40033231e0eSKevin Wolf {
40133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI
40233231e0eSKevin Wolf     printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
40333231e0eSKevin Wolf         lba, nb_sectors);
40433231e0eSKevin Wolf #endif
40533231e0eSKevin Wolf     if (s->atapi_dma) {
40633231e0eSKevin Wolf         ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
40733231e0eSKevin Wolf     } else {
40833231e0eSKevin Wolf         ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
40933231e0eSKevin Wolf     }
41033231e0eSKevin Wolf }
41133231e0eSKevin Wolf 
412a71754e5SDr. David Alan Gilbert 
413a71754e5SDr. David Alan Gilbert /* Called by *_restart_bh when the transfer function points
414a71754e5SDr. David Alan Gilbert  * to ide_atapi_cmd
415a71754e5SDr. David Alan Gilbert  */
416a71754e5SDr. David Alan Gilbert void ide_atapi_dma_restart(IDEState *s)
417a71754e5SDr. David Alan Gilbert {
418a71754e5SDr. David Alan Gilbert     /*
419a71754e5SDr. David Alan Gilbert      * I'm not sure we have enough stored to restart the command
420a71754e5SDr. David Alan Gilbert      * safely, so give the guest an error it should recover from.
421a71754e5SDr. David Alan Gilbert      * I'm assuming most guests will try to recover from something
422a71754e5SDr. David Alan Gilbert      * listed as a medium error on a CD; it seems to work on Linux.
423a71754e5SDr. David Alan Gilbert      * This would be more of a problem if we did any other type of
424a71754e5SDr. David Alan Gilbert      * DMA operation.
425a71754e5SDr. David Alan Gilbert      */
426a71754e5SDr. David Alan Gilbert     ide_atapi_cmd_error(s, MEDIUM_ERROR, ASC_NO_SEEK_COMPLETE);
427a71754e5SDr. David Alan Gilbert }
428a71754e5SDr. David Alan Gilbert 
42933231e0eSKevin Wolf static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
43033231e0eSKevin Wolf                                             uint16_t profile)
43133231e0eSKevin Wolf {
43233231e0eSKevin Wolf     uint8_t *buf_profile = buf + 12; /* start of profiles */
43333231e0eSKevin Wolf 
43433231e0eSKevin Wolf     buf_profile += ((*index) * 4); /* start of indexed profile */
43533231e0eSKevin Wolf     cpu_to_ube16 (buf_profile, profile);
43633231e0eSKevin Wolf     buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
43733231e0eSKevin Wolf 
43833231e0eSKevin Wolf     /* each profile adds 4 bytes to the response */
43933231e0eSKevin Wolf     (*index)++;
44033231e0eSKevin Wolf     buf[11] += 4; /* Additional Length */
44133231e0eSKevin Wolf 
44233231e0eSKevin Wolf     return 4;
44333231e0eSKevin Wolf }
44433231e0eSKevin Wolf 
44533231e0eSKevin Wolf static int ide_dvd_read_structure(IDEState *s, int format,
44633231e0eSKevin Wolf                                   const uint8_t *packet, uint8_t *buf)
44733231e0eSKevin Wolf {
44833231e0eSKevin Wolf     switch (format) {
44933231e0eSKevin Wolf         case 0x0: /* Physical format information */
45033231e0eSKevin Wolf             {
45133231e0eSKevin Wolf                 int layer = packet[6];
45233231e0eSKevin Wolf                 uint64_t total_sectors;
45333231e0eSKevin Wolf 
45433231e0eSKevin Wolf                 if (layer != 0)
45533231e0eSKevin Wolf                     return -ASC_INV_FIELD_IN_CMD_PACKET;
45633231e0eSKevin Wolf 
457e119bcacSKevin Wolf                 total_sectors = s->nb_sectors >> 2;
458e119bcacSKevin Wolf                 if (total_sectors == 0) {
45933231e0eSKevin Wolf                     return -ASC_MEDIUM_NOT_PRESENT;
460e119bcacSKevin Wolf                 }
46133231e0eSKevin Wolf 
46233231e0eSKevin Wolf                 buf[4] = 1;   /* DVD-ROM, part version 1 */
46333231e0eSKevin Wolf                 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
46433231e0eSKevin Wolf                 buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
46533231e0eSKevin Wolf                 buf[7] = 0;   /* default densities */
46633231e0eSKevin Wolf 
46733231e0eSKevin Wolf                 /* FIXME: 0x30000 per spec? */
46833231e0eSKevin Wolf                 cpu_to_ube32(buf + 8, 0); /* start sector */
46933231e0eSKevin Wolf                 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
47033231e0eSKevin Wolf                 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
47133231e0eSKevin Wolf 
47233231e0eSKevin Wolf                 /* Size of buffer, not including 2 byte size field */
473d8ee2591SPeter Maydell                 stw_be_p(buf, 2048 + 2);
47433231e0eSKevin Wolf 
47533231e0eSKevin Wolf                 /* 2k data + 4 byte header */
47633231e0eSKevin Wolf                 return (2048 + 4);
47733231e0eSKevin Wolf             }
47833231e0eSKevin Wolf 
47933231e0eSKevin Wolf         case 0x01: /* DVD copyright information */
48033231e0eSKevin Wolf             buf[4] = 0; /* no copyright data */
48133231e0eSKevin Wolf             buf[5] = 0; /* no region restrictions */
48233231e0eSKevin Wolf 
48333231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
484d8ee2591SPeter Maydell             stw_be_p(buf, 4 + 2);
48533231e0eSKevin Wolf 
48633231e0eSKevin Wolf             /* 4 byte header + 4 byte data */
48733231e0eSKevin Wolf             return (4 + 4);
48833231e0eSKevin Wolf 
48933231e0eSKevin Wolf         case 0x03: /* BCA information - invalid field for no BCA info */
49033231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
49133231e0eSKevin Wolf 
49233231e0eSKevin Wolf         case 0x04: /* DVD disc manufacturing information */
49333231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
494d8ee2591SPeter Maydell             stw_be_p(buf, 2048 + 2);
49533231e0eSKevin Wolf 
49633231e0eSKevin Wolf             /* 2k data + 4 byte header */
49733231e0eSKevin Wolf             return (2048 + 4);
49833231e0eSKevin Wolf 
49933231e0eSKevin Wolf         case 0xff:
50033231e0eSKevin Wolf             /*
50133231e0eSKevin Wolf              * This lists all the command capabilities above.  Add new ones
50233231e0eSKevin Wolf              * in order and update the length and buffer return values.
50333231e0eSKevin Wolf              */
50433231e0eSKevin Wolf 
50533231e0eSKevin Wolf             buf[4] = 0x00; /* Physical format */
50633231e0eSKevin Wolf             buf[5] = 0x40; /* Not writable, is readable */
507d8ee2591SPeter Maydell             stw_be_p(buf + 6, 2048 + 4);
50833231e0eSKevin Wolf 
50933231e0eSKevin Wolf             buf[8] = 0x01; /* Copyright info */
51033231e0eSKevin Wolf             buf[9] = 0x40; /* Not writable, is readable */
511d8ee2591SPeter Maydell             stw_be_p(buf + 10, 4 + 4);
51233231e0eSKevin Wolf 
51333231e0eSKevin Wolf             buf[12] = 0x03; /* BCA info */
51433231e0eSKevin Wolf             buf[13] = 0x40; /* Not writable, is readable */
515d8ee2591SPeter Maydell             stw_be_p(buf + 14, 188 + 4);
51633231e0eSKevin Wolf 
51733231e0eSKevin Wolf             buf[16] = 0x04; /* Manufacturing info */
51833231e0eSKevin Wolf             buf[17] = 0x40; /* Not writable, is readable */
519d8ee2591SPeter Maydell             stw_be_p(buf + 18, 2048 + 4);
52033231e0eSKevin Wolf 
52133231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
522d8ee2591SPeter Maydell             stw_be_p(buf, 16 + 2);
52333231e0eSKevin Wolf 
52433231e0eSKevin Wolf             /* data written + 4 byte header */
52533231e0eSKevin Wolf             return (16 + 4);
52633231e0eSKevin Wolf 
52733231e0eSKevin Wolf         default: /* TODO: formats beyond DVD-ROM requires */
52833231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
52933231e0eSKevin Wolf     }
53033231e0eSKevin Wolf }
53133231e0eSKevin Wolf 
53233231e0eSKevin Wolf static unsigned int event_status_media(IDEState *s,
53333231e0eSKevin Wolf                                        uint8_t *buf)
53433231e0eSKevin Wolf {
53533231e0eSKevin Wolf     uint8_t event_code, media_status;
53633231e0eSKevin Wolf 
53733231e0eSKevin Wolf     media_status = 0;
538dd063333SMarkus Armbruster     if (s->tray_open) {
53933231e0eSKevin Wolf         media_status = MS_TRAY_OPEN;
5404be74634SMarkus Armbruster     } else if (blk_is_inserted(s->blk)) {
54133231e0eSKevin Wolf         media_status = MS_MEDIA_PRESENT;
54233231e0eSKevin Wolf     }
54333231e0eSKevin Wolf 
54433231e0eSKevin Wolf     /* Event notification descriptor */
54533231e0eSKevin Wolf     event_code = MEC_NO_CHANGE;
5462df0a3a3SPaolo Bonzini     if (media_status != MS_TRAY_OPEN) {
5472df0a3a3SPaolo Bonzini         if (s->events.new_media) {
54833231e0eSKevin Wolf             event_code = MEC_NEW_MEDIA;
54933231e0eSKevin Wolf             s->events.new_media = false;
5502df0a3a3SPaolo Bonzini         } else if (s->events.eject_request) {
5512df0a3a3SPaolo Bonzini             event_code = MEC_EJECT_REQUESTED;
5522df0a3a3SPaolo Bonzini             s->events.eject_request = false;
5532df0a3a3SPaolo Bonzini         }
55433231e0eSKevin Wolf     }
55533231e0eSKevin Wolf 
55633231e0eSKevin Wolf     buf[4] = event_code;
55733231e0eSKevin Wolf     buf[5] = media_status;
55833231e0eSKevin Wolf 
55933231e0eSKevin Wolf     /* These fields are reserved, just clear them. */
56033231e0eSKevin Wolf     buf[6] = 0;
56133231e0eSKevin Wolf     buf[7] = 0;
56233231e0eSKevin Wolf 
56333231e0eSKevin Wolf     return 8; /* We wrote to 4 extra bytes from the header */
56433231e0eSKevin Wolf }
56533231e0eSKevin Wolf 
566e1a064f9SKevin Wolf static void cmd_get_event_status_notification(IDEState *s,
567e1a064f9SKevin Wolf                                               uint8_t *buf)
56833231e0eSKevin Wolf {
569e1a064f9SKevin Wolf     const uint8_t *packet = buf;
570e1a064f9SKevin Wolf 
57133231e0eSKevin Wolf     struct {
57233231e0eSKevin Wolf         uint8_t opcode;
57333231e0eSKevin Wolf         uint8_t polled;        /* lsb bit is polled; others are reserved */
57433231e0eSKevin Wolf         uint8_t reserved2[2];
57533231e0eSKevin Wolf         uint8_t class;
57633231e0eSKevin Wolf         uint8_t reserved3[2];
57733231e0eSKevin Wolf         uint16_t len;
57833231e0eSKevin Wolf         uint8_t control;
579541dc0d4SStefan Weil     } QEMU_PACKED *gesn_cdb;
58033231e0eSKevin Wolf 
58133231e0eSKevin Wolf     struct {
58233231e0eSKevin Wolf         uint16_t len;
58333231e0eSKevin Wolf         uint8_t notification_class;
58433231e0eSKevin Wolf         uint8_t supported_events;
585541dc0d4SStefan Weil     } QEMU_PACKED *gesn_event_header;
58633231e0eSKevin Wolf     unsigned int max_len, used_len;
58733231e0eSKevin Wolf 
58833231e0eSKevin Wolf     gesn_cdb = (void *)packet;
58933231e0eSKevin Wolf     gesn_event_header = (void *)buf;
59033231e0eSKevin Wolf 
59133231e0eSKevin Wolf     max_len = be16_to_cpu(gesn_cdb->len);
59233231e0eSKevin Wolf 
59333231e0eSKevin Wolf     /* It is fine by the MMC spec to not support async mode operations */
59433231e0eSKevin Wolf     if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
59533231e0eSKevin Wolf         /* Only polling is supported, asynchronous mode is not. */
59667cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
59733231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
59833231e0eSKevin Wolf         return;
59933231e0eSKevin Wolf     }
60033231e0eSKevin Wolf 
60133231e0eSKevin Wolf     /* polling mode operation */
60233231e0eSKevin Wolf 
60333231e0eSKevin Wolf     /*
60433231e0eSKevin Wolf      * These are the supported events.
60533231e0eSKevin Wolf      *
60633231e0eSKevin Wolf      * We currently only support requests of the 'media' type.
607f0f992e6SPaolo Bonzini      * Notification class requests and supported event classes are bitmasks,
608f0f992e6SPaolo Bonzini      * but they are build from the same values as the "notification class"
609f0f992e6SPaolo Bonzini      * field.
61033231e0eSKevin Wolf      */
611f0f992e6SPaolo Bonzini     gesn_event_header->supported_events = 1 << GESN_MEDIA;
61233231e0eSKevin Wolf 
61333231e0eSKevin Wolf     /*
61433231e0eSKevin Wolf      * We use |= below to set the class field; other bits in this byte
61533231e0eSKevin Wolf      * are reserved now but this is useful to do if we have to use the
61633231e0eSKevin Wolf      * reserved fields later.
61733231e0eSKevin Wolf      */
61833231e0eSKevin Wolf     gesn_event_header->notification_class = 0;
61933231e0eSKevin Wolf 
62033231e0eSKevin Wolf     /*
62133231e0eSKevin Wolf      * Responses to requests are to be based on request priority.  The
62233231e0eSKevin Wolf      * notification_class_request_type enum above specifies the
62333231e0eSKevin Wolf      * priority: upper elements are higher prio than lower ones.
62433231e0eSKevin Wolf      */
625f0f992e6SPaolo Bonzini     if (gesn_cdb->class & (1 << GESN_MEDIA)) {
626f0f992e6SPaolo Bonzini         gesn_event_header->notification_class |= GESN_MEDIA;
62733231e0eSKevin Wolf         used_len = event_status_media(s, buf);
62833231e0eSKevin Wolf     } else {
62933231e0eSKevin Wolf         gesn_event_header->notification_class = 0x80; /* No event available */
63033231e0eSKevin Wolf         used_len = sizeof(*gesn_event_header);
63133231e0eSKevin Wolf     }
63233231e0eSKevin Wolf     gesn_event_header->len = cpu_to_be16(used_len
63333231e0eSKevin Wolf                                          - sizeof(*gesn_event_header));
63433231e0eSKevin Wolf     ide_atapi_cmd_reply(s, used_len, max_len);
63533231e0eSKevin Wolf }
63633231e0eSKevin Wolf 
637a60cf7e7SKevin Wolf static void cmd_request_sense(IDEState *s, uint8_t *buf)
63833231e0eSKevin Wolf {
639a60cf7e7SKevin Wolf     int max_len = buf[4];
640a60cf7e7SKevin Wolf 
641a60cf7e7SKevin Wolf     memset(buf, 0, 18);
642a60cf7e7SKevin Wolf     buf[0] = 0x70 | (1 << 7);
643a60cf7e7SKevin Wolf     buf[2] = s->sense_key;
644a60cf7e7SKevin Wolf     buf[7] = 10;
645a60cf7e7SKevin Wolf     buf[12] = s->asc;
646a60cf7e7SKevin Wolf 
64767cc61e4SPaolo Bonzini     if (s->sense_key == UNIT_ATTENTION) {
64867cc61e4SPaolo Bonzini         s->sense_key = NO_SENSE;
649a60cf7e7SKevin Wolf     }
650a60cf7e7SKevin Wolf 
651a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, 18, max_len);
652a60cf7e7SKevin Wolf }
653a60cf7e7SKevin Wolf 
654a60cf7e7SKevin Wolf static void cmd_inquiry(IDEState *s, uint8_t *buf)
655a60cf7e7SKevin Wolf {
6569a502563SJohn Snow     uint8_t page_code = buf[2];
657a60cf7e7SKevin Wolf     int max_len = buf[4];
658a60cf7e7SKevin Wolf 
6599a502563SJohn Snow     unsigned idx = 0;
6609a502563SJohn Snow     unsigned size_idx;
6619a502563SJohn Snow     unsigned preamble_len;
6629a502563SJohn Snow 
6639a502563SJohn Snow     /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
6649a502563SJohn Snow      * we are being asked for a specific page of info indicated by byte 2. */
6659a502563SJohn Snow     if (buf[1] & 0x01) {
6669a502563SJohn Snow         preamble_len = 4;
6679a502563SJohn Snow         size_idx = 3;
6689a502563SJohn Snow 
6699a502563SJohn Snow         buf[idx++] = 0x05;      /* CD-ROM */
6709a502563SJohn Snow         buf[idx++] = page_code; /* Page Code */
6719a502563SJohn Snow         buf[idx++] = 0x00;      /* reserved */
6729a502563SJohn Snow         idx++;                  /* length (set later) */
6739a502563SJohn Snow 
6749a502563SJohn Snow         switch (page_code) {
6759a502563SJohn Snow         case 0x00:
6769a502563SJohn Snow             /* Supported Pages: List of supported VPD responses. */
6779a502563SJohn Snow             buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
6789a502563SJohn Snow             buf[idx++] = 0x83; /* 0x83: Device Identification. */
6799a502563SJohn Snow             break;
6809a502563SJohn Snow 
6819a502563SJohn Snow         case 0x83:
6829a502563SJohn Snow             /* Device Identification. Each entry is optional, but the entries
6839a502563SJohn Snow              * included here are modeled after libata's VPD responses.
6849a502563SJohn Snow              * If the response is given, at least one entry must be present. */
6859a502563SJohn Snow 
6869a502563SJohn Snow             /* Entry 1: Serial */
6879a502563SJohn Snow             if (idx + 24 > max_len) {
6889a502563SJohn Snow                 /* Not enough room for even the first entry: */
6899a502563SJohn Snow                 /* 4 byte header + 20 byte string */
6909a502563SJohn Snow                 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
6919a502563SJohn Snow                                     ASC_DATA_PHASE_ERROR);
6929a502563SJohn Snow                 return;
6939a502563SJohn Snow             }
6949a502563SJohn Snow             buf[idx++] = 0x02; /* Ascii */
6959a502563SJohn Snow             buf[idx++] = 0x00; /* Vendor Specific */
6969a502563SJohn Snow             buf[idx++] = 0x00;
6979a502563SJohn Snow             buf[idx++] = 20;   /* Remaining length */
6989a502563SJohn Snow             padstr8(buf + idx, 20, s->drive_serial_str);
6999a502563SJohn Snow             idx += 20;
7009a502563SJohn Snow 
7019a502563SJohn Snow             /* Entry 2: Drive Model and Serial */
7029a502563SJohn Snow             if (idx + 72 > max_len) {
7039a502563SJohn Snow                 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
7049a502563SJohn Snow                 goto out;
7059a502563SJohn Snow             }
7069a502563SJohn Snow             buf[idx++] = 0x02; /* Ascii */
7079a502563SJohn Snow             buf[idx++] = 0x01; /* T10 Vendor */
7089a502563SJohn Snow             buf[idx++] = 0x00;
7099a502563SJohn Snow             buf[idx++] = 68;
7109a502563SJohn Snow             padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
7119a502563SJohn Snow             idx += 8;
7129a502563SJohn Snow             padstr8(buf + idx, 40, s->drive_model_str);
7139a502563SJohn Snow             idx += 40;
7149a502563SJohn Snow             padstr8(buf + idx, 20, s->drive_serial_str);
7159a502563SJohn Snow             idx += 20;
7169a502563SJohn Snow 
7179a502563SJohn Snow             /* Entry 3: WWN */
7189a502563SJohn Snow             if (s->wwn && (idx + 12 <= max_len)) {
7199a502563SJohn Snow                 /* 4 byte header + 8 byte wwn */
7209a502563SJohn Snow                 buf[idx++] = 0x01; /* Binary */
7219a502563SJohn Snow                 buf[idx++] = 0x03; /* NAA */
7229a502563SJohn Snow                 buf[idx++] = 0x00;
7239a502563SJohn Snow                 buf[idx++] = 0x08;
7249a502563SJohn Snow                 stq_be_p(&buf[idx], s->wwn);
7259a502563SJohn Snow                 idx += 8;
7269a502563SJohn Snow             }
7279a502563SJohn Snow             break;
7289a502563SJohn Snow 
7299a502563SJohn Snow         default:
7309a502563SJohn Snow             /* SPC-3, revision 23 sec. 6.4 */
7319a502563SJohn Snow             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
7329a502563SJohn Snow                                 ASC_INV_FIELD_IN_CMD_PACKET);
7339a502563SJohn Snow             return;
7349a502563SJohn Snow         }
7359a502563SJohn Snow     } else {
7369a502563SJohn Snow         preamble_len = 5;
7379a502563SJohn Snow         size_idx = 4;
7389a502563SJohn Snow 
739a60cf7e7SKevin Wolf         buf[0] = 0x05; /* CD-ROM */
740a60cf7e7SKevin Wolf         buf[1] = 0x80; /* removable */
741a60cf7e7SKevin Wolf         buf[2] = 0x00; /* ISO */
742a60cf7e7SKevin Wolf         buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
7439a502563SJohn Snow         /* buf[size_idx] set below. */
744a60cf7e7SKevin Wolf         buf[5] = 0;    /* reserved */
745a60cf7e7SKevin Wolf         buf[6] = 0;    /* reserved */
746a60cf7e7SKevin Wolf         buf[7] = 0;    /* reserved */
747a60cf7e7SKevin Wolf         padstr8(buf + 8, 8, "QEMU");
748a60cf7e7SKevin Wolf         padstr8(buf + 16, 16, "QEMU DVD-ROM");
749a60cf7e7SKevin Wolf         padstr8(buf + 32, 4, s->version);
7509a502563SJohn Snow         idx = 36;
7519a502563SJohn Snow     }
7529a502563SJohn Snow 
7539a502563SJohn Snow  out:
7549a502563SJohn Snow     buf[size_idx] = idx - preamble_len;
7559a502563SJohn Snow     ide_atapi_cmd_reply(s, idx, max_len);
7569a502563SJohn Snow     return;
757a60cf7e7SKevin Wolf }
758a60cf7e7SKevin Wolf 
759a60cf7e7SKevin Wolf static void cmd_get_configuration(IDEState *s, uint8_t *buf)
760a60cf7e7SKevin Wolf {
761a60cf7e7SKevin Wolf     uint32_t len;
762a60cf7e7SKevin Wolf     uint8_t index = 0;
76333231e0eSKevin Wolf     int max_len;
76433231e0eSKevin Wolf 
765a60cf7e7SKevin Wolf     /* only feature 0 is supported */
766a60cf7e7SKevin Wolf     if (buf[2] != 0 || buf[3] != 0) {
76767cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
768a60cf7e7SKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
76933231e0eSKevin Wolf         return;
77033231e0eSKevin Wolf     }
77133231e0eSKevin Wolf 
772a60cf7e7SKevin Wolf     /* XXX: could result in alignment problems in some architectures */
773a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 7);
774a60cf7e7SKevin Wolf 
775a60cf7e7SKevin Wolf     /*
776a60cf7e7SKevin Wolf      * XXX: avoid overflow for io_buffer if max_len is bigger than
777a60cf7e7SKevin Wolf      *      the size of that buffer (dimensioned to max number of
778a60cf7e7SKevin Wolf      *      sectors to transfer at once)
779a60cf7e7SKevin Wolf      *
780a60cf7e7SKevin Wolf      *      Only a problem if the feature/profiles grow.
781a60cf7e7SKevin Wolf      */
782a60cf7e7SKevin Wolf     if (max_len > 512) {
783a60cf7e7SKevin Wolf         /* XXX: assume 1 sector */
784a60cf7e7SKevin Wolf         max_len = 512;
78533231e0eSKevin Wolf     }
786a60cf7e7SKevin Wolf 
787a60cf7e7SKevin Wolf     memset(buf, 0, max_len);
788a60cf7e7SKevin Wolf     /*
789a60cf7e7SKevin Wolf      * the number of sectors from the media tells us which profile
790a60cf7e7SKevin Wolf      * to use as current.  0 means there is no media
791a60cf7e7SKevin Wolf      */
792a60cf7e7SKevin Wolf     if (media_is_dvd(s)) {
793a60cf7e7SKevin Wolf         cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
794a60cf7e7SKevin Wolf     } else if (media_is_cd(s)) {
795a60cf7e7SKevin Wolf         cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
79633231e0eSKevin Wolf     }
797a60cf7e7SKevin Wolf 
798a60cf7e7SKevin Wolf     buf[10] = 0x02 | 0x01; /* persistent and current */
799a60cf7e7SKevin Wolf     len = 12; /* headers: 8 + 4 */
800a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
801a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
802a60cf7e7SKevin Wolf     cpu_to_ube32(buf, len - 4); /* data length */
803a60cf7e7SKevin Wolf 
804a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, len, max_len);
805a60cf7e7SKevin Wolf }
806a60cf7e7SKevin Wolf 
807a60cf7e7SKevin Wolf static void cmd_mode_sense(IDEState *s, uint8_t *buf)
80833231e0eSKevin Wolf {
80933231e0eSKevin Wolf     int action, code;
810a60cf7e7SKevin Wolf     int max_len;
811a60cf7e7SKevin Wolf 
812a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 7);
813a60cf7e7SKevin Wolf     action = buf[2] >> 6;
814a60cf7e7SKevin Wolf     code = buf[2] & 0x3f;
815a60cf7e7SKevin Wolf 
81633231e0eSKevin Wolf     switch(action) {
81733231e0eSKevin Wolf     case 0: /* current values */
81833231e0eSKevin Wolf         switch(code) {
81967cc61e4SPaolo Bonzini         case MODE_PAGE_R_W_ERROR: /* error recovery */
8202c20ae11SPaolo Bonzini             cpu_to_ube16(&buf[0], 16 - 2);
82133231e0eSKevin Wolf             buf[2] = 0x70;
82233231e0eSKevin Wolf             buf[3] = 0;
82333231e0eSKevin Wolf             buf[4] = 0;
82433231e0eSKevin Wolf             buf[5] = 0;
82533231e0eSKevin Wolf             buf[6] = 0;
82633231e0eSKevin Wolf             buf[7] = 0;
82733231e0eSKevin Wolf 
828af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_R_W_ERROR;
829af0e1ea2SPaolo Bonzini             buf[9] = 16 - 10;
83033231e0eSKevin Wolf             buf[10] = 0x00;
83133231e0eSKevin Wolf             buf[11] = 0x05;
83233231e0eSKevin Wolf             buf[12] = 0x00;
83333231e0eSKevin Wolf             buf[13] = 0x00;
83433231e0eSKevin Wolf             buf[14] = 0x00;
83533231e0eSKevin Wolf             buf[15] = 0x00;
83633231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 16, max_len);
83733231e0eSKevin Wolf             break;
83867cc61e4SPaolo Bonzini         case MODE_PAGE_AUDIO_CTL:
8392c20ae11SPaolo Bonzini             cpu_to_ube16(&buf[0], 24 - 2);
84033231e0eSKevin Wolf             buf[2] = 0x70;
84133231e0eSKevin Wolf             buf[3] = 0;
84233231e0eSKevin Wolf             buf[4] = 0;
84333231e0eSKevin Wolf             buf[5] = 0;
84433231e0eSKevin Wolf             buf[6] = 0;
84533231e0eSKevin Wolf             buf[7] = 0;
84633231e0eSKevin Wolf 
847af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_AUDIO_CTL;
848af0e1ea2SPaolo Bonzini             buf[9] = 24 - 10;
84933231e0eSKevin Wolf             /* Fill with CDROM audio volume */
85033231e0eSKevin Wolf             buf[17] = 0;
85133231e0eSKevin Wolf             buf[19] = 0;
85233231e0eSKevin Wolf             buf[21] = 0;
85333231e0eSKevin Wolf             buf[23] = 0;
85433231e0eSKevin Wolf 
85533231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 24, max_len);
85633231e0eSKevin Wolf             break;
85767cc61e4SPaolo Bonzini         case MODE_PAGE_CAPABILITIES:
8582c20ae11SPaolo Bonzini             cpu_to_ube16(&buf[0], 30 - 2);
85933231e0eSKevin Wolf             buf[2] = 0x70;
86033231e0eSKevin Wolf             buf[3] = 0;
86133231e0eSKevin Wolf             buf[4] = 0;
86233231e0eSKevin Wolf             buf[5] = 0;
86333231e0eSKevin Wolf             buf[6] = 0;
86433231e0eSKevin Wolf             buf[7] = 0;
86533231e0eSKevin Wolf 
866af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_CAPABILITIES;
8672c20ae11SPaolo Bonzini             buf[9] = 30 - 10;
868a07c7dcdSPaolo Bonzini             buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
86933231e0eSKevin Wolf             buf[11] = 0x00;
87033231e0eSKevin Wolf 
87133231e0eSKevin Wolf             /* Claim PLAY_AUDIO capability (0x01) since some Linux
87233231e0eSKevin Wolf                code checks for this to automount media. */
87333231e0eSKevin Wolf             buf[12] = 0x71;
87433231e0eSKevin Wolf             buf[13] = 3 << 5;
87533231e0eSKevin Wolf             buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
876a0a7573bSMarkus Armbruster             if (s->tray_locked) {
877a07c7dcdSPaolo Bonzini                 buf[14] |= 1 << 1;
878a0a7573bSMarkus Armbruster             }
879a07c7dcdSPaolo Bonzini             buf[15] = 0x00; /* No volume & mute control, no changer */
880a07c7dcdSPaolo Bonzini             cpu_to_ube16(&buf[16], 704); /* 4x read speed */
881a07c7dcdSPaolo Bonzini             buf[18] = 0; /* Two volume levels */
88233231e0eSKevin Wolf             buf[19] = 2;
883a07c7dcdSPaolo Bonzini             cpu_to_ube16(&buf[20], 512); /* 512k buffer */
884a07c7dcdSPaolo Bonzini             cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
88533231e0eSKevin Wolf             buf[24] = 0;
88633231e0eSKevin Wolf             buf[25] = 0;
88733231e0eSKevin Wolf             buf[26] = 0;
88833231e0eSKevin Wolf             buf[27] = 0;
8892c20ae11SPaolo Bonzini             buf[28] = 0;
8902c20ae11SPaolo Bonzini             buf[29] = 0;
8912c20ae11SPaolo Bonzini             ide_atapi_cmd_reply(s, 30, max_len);
89233231e0eSKevin Wolf             break;
89333231e0eSKevin Wolf         default:
89433231e0eSKevin Wolf             goto error_cmd;
89533231e0eSKevin Wolf         }
89633231e0eSKevin Wolf         break;
89733231e0eSKevin Wolf     case 1: /* changeable values */
89833231e0eSKevin Wolf         goto error_cmd;
89933231e0eSKevin Wolf     case 2: /* default values */
90033231e0eSKevin Wolf         goto error_cmd;
90133231e0eSKevin Wolf     default:
90233231e0eSKevin Wolf     case 3: /* saved values */
90367cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
90433231e0eSKevin Wolf                             ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
90533231e0eSKevin Wolf         break;
90633231e0eSKevin Wolf     }
907a60cf7e7SKevin Wolf     return;
908a60cf7e7SKevin Wolf 
909a60cf7e7SKevin Wolf error_cmd:
91067cc61e4SPaolo Bonzini     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
91133231e0eSKevin Wolf }
912a60cf7e7SKevin Wolf 
913a60cf7e7SKevin Wolf static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
914a60cf7e7SKevin Wolf {
9157a2c4b82SKevin Wolf     /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
9167a2c4b82SKevin Wolf      * come here, we know that it's ready. */
91733231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
918a60cf7e7SKevin Wolf }
919a60cf7e7SKevin Wolf 
920a60cf7e7SKevin Wolf static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
921a60cf7e7SKevin Wolf {
922a0a7573bSMarkus Armbruster     s->tray_locked = buf[4] & 1;
9234be74634SMarkus Armbruster     blk_lock_medium(s->blk, buf[4] & 1);
924a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
925a60cf7e7SKevin Wolf }
926a60cf7e7SKevin Wolf 
927a60cf7e7SKevin Wolf static void cmd_read(IDEState *s, uint8_t* buf)
92833231e0eSKevin Wolf {
92933231e0eSKevin Wolf     int nb_sectors, lba;
93033231e0eSKevin Wolf 
931a60cf7e7SKevin Wolf     if (buf[0] == GPCMD_READ_10) {
932a60cf7e7SKevin Wolf         nb_sectors = ube16_to_cpu(buf + 7);
933a60cf7e7SKevin Wolf     } else {
934a60cf7e7SKevin Wolf         nb_sectors = ube32_to_cpu(buf + 6);
935a60cf7e7SKevin Wolf     }
936a60cf7e7SKevin Wolf 
937a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
93833231e0eSKevin Wolf     if (nb_sectors == 0) {
93933231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
940a60cf7e7SKevin Wolf         return;
94133231e0eSKevin Wolf     }
942a60cf7e7SKevin Wolf 
94333231e0eSKevin Wolf     ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
94433231e0eSKevin Wolf }
945a60cf7e7SKevin Wolf 
946a60cf7e7SKevin Wolf static void cmd_read_cd(IDEState *s, uint8_t* buf)
94733231e0eSKevin Wolf {
94833231e0eSKevin Wolf     int nb_sectors, lba, transfer_request;
94933231e0eSKevin Wolf 
950a60cf7e7SKevin Wolf     nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
951a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
952a60cf7e7SKevin Wolf 
95333231e0eSKevin Wolf     if (nb_sectors == 0) {
95433231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
955a60cf7e7SKevin Wolf         return;
95633231e0eSKevin Wolf     }
957a60cf7e7SKevin Wolf 
958a60cf7e7SKevin Wolf     transfer_request = buf[9];
95933231e0eSKevin Wolf     switch(transfer_request & 0xf8) {
96033231e0eSKevin Wolf     case 0x00:
96133231e0eSKevin Wolf         /* nothing */
96233231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
96333231e0eSKevin Wolf         break;
96433231e0eSKevin Wolf     case 0x10:
96533231e0eSKevin Wolf         /* normal read */
96633231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
96733231e0eSKevin Wolf         break;
96833231e0eSKevin Wolf     case 0xf8:
96933231e0eSKevin Wolf         /* read all data */
97033231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
97133231e0eSKevin Wolf         break;
97233231e0eSKevin Wolf     default:
97367cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
97433231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
97533231e0eSKevin Wolf         break;
97633231e0eSKevin Wolf     }
97733231e0eSKevin Wolf }
978a60cf7e7SKevin Wolf 
979a60cf7e7SKevin Wolf static void cmd_seek(IDEState *s, uint8_t* buf)
98033231e0eSKevin Wolf {
98133231e0eSKevin Wolf     unsigned int lba;
982e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
98333231e0eSKevin Wolf 
984a60cf7e7SKevin Wolf     lba = ube32_to_cpu(buf + 2);
98533231e0eSKevin Wolf     if (lba >= total_sectors) {
98667cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
987a60cf7e7SKevin Wolf         return;
98833231e0eSKevin Wolf     }
989a60cf7e7SKevin Wolf 
99033231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
99133231e0eSKevin Wolf }
992a60cf7e7SKevin Wolf 
993a60cf7e7SKevin Wolf static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
99433231e0eSKevin Wolf {
995fdec4404SMarkus Armbruster     int sense;
996f0776564SMarkus Armbruster     bool start = buf[4] & 1;
997f0776564SMarkus Armbruster     bool loej = buf[4] & 2;     /* load on start, eject on !start */
998ce560dcfSRonnie Sahlberg     int pwrcnd = buf[4] & 0xf0;
999ce560dcfSRonnie Sahlberg 
1000ce560dcfSRonnie Sahlberg     if (pwrcnd) {
1001ce560dcfSRonnie Sahlberg         /* eject/load only happens for power condition == 0 */
100203441c3aSKevin Wolf         ide_atapi_cmd_ok(s);
1003ce560dcfSRonnie Sahlberg         return;
1004ce560dcfSRonnie Sahlberg     }
100533231e0eSKevin Wolf 
1006f0776564SMarkus Armbruster     if (loej) {
100748f65b3fSMarkus Armbruster         if (!start && !s->tray_open && s->tray_locked) {
10084be74634SMarkus Armbruster             sense = blk_is_inserted(s->blk)
100967cc61e4SPaolo Bonzini                 ? NOT_READY : ILLEGAL_REQUEST;
1010a60cf7e7SKevin Wolf             ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1011fdec4404SMarkus Armbruster             return;
101233231e0eSKevin Wolf         }
1013d88b1819SLuiz Capitulino 
1014d88b1819SLuiz Capitulino         if (s->tray_open != !start) {
10154be74634SMarkus Armbruster             blk_eject(s->blk, !start);
1016dd063333SMarkus Armbruster             s->tray_open = !start;
1017dd063333SMarkus Armbruster         }
1018d88b1819SLuiz Capitulino     }
1019fdec4404SMarkus Armbruster 
1020fdec4404SMarkus Armbruster     ide_atapi_cmd_ok(s);
102133231e0eSKevin Wolf }
1022a60cf7e7SKevin Wolf 
1023a60cf7e7SKevin Wolf static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
102433231e0eSKevin Wolf {
1025a60cf7e7SKevin Wolf     int max_len = ube16_to_cpu(buf + 8);
1026a60cf7e7SKevin Wolf 
102733231e0eSKevin Wolf     cpu_to_ube16(buf, 0);
102833231e0eSKevin Wolf     /* no current LBA */
102933231e0eSKevin Wolf     buf[2] = 0;
103033231e0eSKevin Wolf     buf[3] = 0;
103133231e0eSKevin Wolf     buf[4] = 0;
103233231e0eSKevin Wolf     buf[5] = 1;
103333231e0eSKevin Wolf     cpu_to_ube16(buf + 6, 0);
103433231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, max_len);
103533231e0eSKevin Wolf }
1036a60cf7e7SKevin Wolf 
1037a60cf7e7SKevin Wolf static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
103833231e0eSKevin Wolf {
103933231e0eSKevin Wolf     int format, msf, start_track, len;
1040a60cf7e7SKevin Wolf     int max_len;
10417a2c4b82SKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
1042a60cf7e7SKevin Wolf 
1043a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 7);
1044a60cf7e7SKevin Wolf     format = buf[9] >> 6;
1045a60cf7e7SKevin Wolf     msf = (buf[1] >> 1) & 1;
1046a60cf7e7SKevin Wolf     start_track = buf[6];
1047a60cf7e7SKevin Wolf 
104833231e0eSKevin Wolf     switch(format) {
104933231e0eSKevin Wolf     case 0:
105033231e0eSKevin Wolf         len = cdrom_read_toc(total_sectors, buf, msf, start_track);
105133231e0eSKevin Wolf         if (len < 0)
105233231e0eSKevin Wolf             goto error_cmd;
105333231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
105433231e0eSKevin Wolf         break;
105533231e0eSKevin Wolf     case 1:
105633231e0eSKevin Wolf         /* multi session : only a single session defined */
105733231e0eSKevin Wolf         memset(buf, 0, 12);
105833231e0eSKevin Wolf         buf[1] = 0x0a;
105933231e0eSKevin Wolf         buf[2] = 0x01;
106033231e0eSKevin Wolf         buf[3] = 0x01;
106133231e0eSKevin Wolf         ide_atapi_cmd_reply(s, 12, max_len);
106233231e0eSKevin Wolf         break;
106333231e0eSKevin Wolf     case 2:
106433231e0eSKevin Wolf         len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
106533231e0eSKevin Wolf         if (len < 0)
106633231e0eSKevin Wolf             goto error_cmd;
106733231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
106833231e0eSKevin Wolf         break;
106933231e0eSKevin Wolf     default:
107033231e0eSKevin Wolf     error_cmd:
107167cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
107233231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
107333231e0eSKevin Wolf     }
107433231e0eSKevin Wolf }
1075a60cf7e7SKevin Wolf 
1076a60cf7e7SKevin Wolf static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
107733231e0eSKevin Wolf {
1078e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
107933231e0eSKevin Wolf 
108033231e0eSKevin Wolf     /* NOTE: it is really the number of sectors minus 1 */
108133231e0eSKevin Wolf     cpu_to_ube32(buf, total_sectors - 1);
108233231e0eSKevin Wolf     cpu_to_ube32(buf + 4, 2048);
108333231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, 8);
108433231e0eSKevin Wolf }
1085a60cf7e7SKevin Wolf 
108655042b95SPaolo Bonzini static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
108755042b95SPaolo Bonzini {
108855042b95SPaolo Bonzini     uint8_t type = buf[1] & 7;
108955042b95SPaolo Bonzini     uint32_t max_len = ube16_to_cpu(buf + 7);
109055042b95SPaolo Bonzini 
109155042b95SPaolo Bonzini     /* Types 1/2 are only defined for Blu-Ray.  */
109255042b95SPaolo Bonzini     if (type != 0) {
109355042b95SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
109455042b95SPaolo Bonzini                             ASC_INV_FIELD_IN_CMD_PACKET);
109555042b95SPaolo Bonzini         return;
109655042b95SPaolo Bonzini     }
109755042b95SPaolo Bonzini 
109855042b95SPaolo Bonzini     memset(buf, 0, 34);
109955042b95SPaolo Bonzini     buf[1] = 32;
110055042b95SPaolo Bonzini     buf[2] = 0xe; /* last session complete, disc finalized */
110155042b95SPaolo Bonzini     buf[3] = 1;   /* first track on disc */
110255042b95SPaolo Bonzini     buf[4] = 1;   /* # of sessions */
110355042b95SPaolo Bonzini     buf[5] = 1;   /* first track of last session */
110455042b95SPaolo Bonzini     buf[6] = 1;   /* last track of last session */
110555042b95SPaolo Bonzini     buf[7] = 0x20; /* unrestricted use */
110655042b95SPaolo Bonzini     buf[8] = 0x00; /* CD-ROM or DVD-ROM */
110755042b95SPaolo Bonzini     /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
110855042b95SPaolo Bonzini     /* 12-23: not meaningful for CD-ROM or DVD-ROM */
110955042b95SPaolo Bonzini     /* 24-31: disc bar code */
111055042b95SPaolo Bonzini     /* 32: disc application code */
111155042b95SPaolo Bonzini     /* 33: number of OPC tables */
111255042b95SPaolo Bonzini 
111355042b95SPaolo Bonzini     ide_atapi_cmd_reply(s, 34, max_len);
111455042b95SPaolo Bonzini }
111555042b95SPaolo Bonzini 
1116a60cf7e7SKevin Wolf static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
111733231e0eSKevin Wolf {
1118a60cf7e7SKevin Wolf     int max_len;
1119a60cf7e7SKevin Wolf     int media = buf[1];
1120a60cf7e7SKevin Wolf     int format = buf[7];
112133231e0eSKevin Wolf     int ret;
112233231e0eSKevin Wolf 
1123a60cf7e7SKevin Wolf     max_len = ube16_to_cpu(buf + 8);
112433231e0eSKevin Wolf 
112533231e0eSKevin Wolf     if (format < 0xff) {
112633231e0eSKevin Wolf         if (media_is_cd(s)) {
112767cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
112833231e0eSKevin Wolf                                 ASC_INCOMPATIBLE_FORMAT);
1129a60cf7e7SKevin Wolf             return;
113033231e0eSKevin Wolf         } else if (!media_present(s)) {
113167cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
113233231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
1133a60cf7e7SKevin Wolf             return;
113433231e0eSKevin Wolf         }
113533231e0eSKevin Wolf     }
113633231e0eSKevin Wolf 
113733231e0eSKevin Wolf     memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
113833231e0eSKevin Wolf            IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
113933231e0eSKevin Wolf 
114033231e0eSKevin Wolf     switch (format) {
114133231e0eSKevin Wolf         case 0x00 ... 0x7f:
114233231e0eSKevin Wolf         case 0xff:
114333231e0eSKevin Wolf             if (media == 0) {
1144a60cf7e7SKevin Wolf                 ret = ide_dvd_read_structure(s, format, buf, buf);
114533231e0eSKevin Wolf 
1146a60cf7e7SKevin Wolf                 if (ret < 0) {
114767cc61e4SPaolo Bonzini                     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1148a60cf7e7SKevin Wolf                 } else {
114933231e0eSKevin Wolf                     ide_atapi_cmd_reply(s, ret, max_len);
1150a60cf7e7SKevin Wolf                 }
115133231e0eSKevin Wolf 
115233231e0eSKevin Wolf                 break;
115333231e0eSKevin Wolf             }
115433231e0eSKevin Wolf             /* TODO: BD support, fall through for now */
115533231e0eSKevin Wolf 
115633231e0eSKevin Wolf         /* Generic disk structures */
115733231e0eSKevin Wolf         case 0x80: /* TODO: AACS volume identifier */
115833231e0eSKevin Wolf         case 0x81: /* TODO: AACS media serial number */
115933231e0eSKevin Wolf         case 0x82: /* TODO: AACS media identifier */
116033231e0eSKevin Wolf         case 0x83: /* TODO: AACS media key block */
116133231e0eSKevin Wolf         case 0x90: /* TODO: List of recognized format layers */
116233231e0eSKevin Wolf         case 0xc0: /* TODO: Write protection status */
116333231e0eSKevin Wolf         default:
116467cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
116533231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
116633231e0eSKevin Wolf             break;
116733231e0eSKevin Wolf     }
116833231e0eSKevin Wolf }
1169a60cf7e7SKevin Wolf 
1170a60cf7e7SKevin Wolf static void cmd_set_speed(IDEState *s, uint8_t* buf)
1171a60cf7e7SKevin Wolf {
1172a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
1173a60cf7e7SKevin Wolf }
1174a60cf7e7SKevin Wolf 
1175e1a064f9SKevin Wolf enum {
1176e1a064f9SKevin Wolf     /*
1177e1a064f9SKevin Wolf      * Only commands flagged as ALLOW_UA are allowed to run under a
1178e1a064f9SKevin Wolf      * unit attention condition. (See MMC-5, section 4.1.6.1)
1179e1a064f9SKevin Wolf      */
1180e1a064f9SKevin Wolf     ALLOW_UA = 0x01,
11817a2c4b82SKevin Wolf 
11827a2c4b82SKevin Wolf     /*
11837a2c4b82SKevin Wolf      * Commands flagged with CHECK_READY can only execute if a medium is present.
11847a2c4b82SKevin Wolf      * Otherwise they report the Not Ready Condition. (See MMC-5, section
11857a2c4b82SKevin Wolf      * 4.1.8)
11867a2c4b82SKevin Wolf      */
11877a2c4b82SKevin Wolf     CHECK_READY = 0x02,
11889ef2e93fSJohn Snow 
11899ef2e93fSJohn Snow     /*
11909ef2e93fSJohn Snow      * Commands flagged with NONDATA do not in any circumstances return
11919ef2e93fSJohn Snow      * any data via ide_atapi_cmd_reply. These commands are exempt from
11929ef2e93fSJohn Snow      * the normal byte_count_limit constraints.
11939ef2e93fSJohn Snow      * See ATA8-ACS3 "7.21.5 Byte Count Limit"
11949ef2e93fSJohn Snow      */
11959ef2e93fSJohn Snow     NONDATA = 0x04,
1196e1a064f9SKevin Wolf };
1197e1a064f9SKevin Wolf 
1198e1a064f9SKevin Wolf static const struct {
1199e1a064f9SKevin Wolf     void (*handler)(IDEState *s, uint8_t *buf);
1200e1a064f9SKevin Wolf     int flags;
1201e1a064f9SKevin Wolf } atapi_cmd_table[0x100] = {
12029ef2e93fSJohn Snow     [ 0x00 ] = { cmd_test_unit_ready,               CHECK_READY | NONDATA },
1203e1a064f9SKevin Wolf     [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
1204e1a064f9SKevin Wolf     [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
12059ef2e93fSJohn Snow     [ 0x1b ] = { cmd_start_stop_unit,               NONDATA }, /* [1] */
12069ef2e93fSJohn Snow     [ 0x1e ] = { cmd_prevent_allow_medium_removal,  NONDATA },
12077a2c4b82SKevin Wolf     [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
1208a1aff5bfSMarkus Armbruster     [ 0x28 ] = { cmd_read, /* (10) */               CHECK_READY },
12099ef2e93fSJohn Snow     [ 0x2b ] = { cmd_seek,                          CHECK_READY | NONDATA },
12107a2c4b82SKevin Wolf     [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
1211e1a064f9SKevin Wolf     [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
1212e1a064f9SKevin Wolf     [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
121355042b95SPaolo Bonzini     [ 0x51 ] = { cmd_read_disc_information,         CHECK_READY },
1214e1a064f9SKevin Wolf     [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
1215a1aff5bfSMarkus Armbruster     [ 0xa8 ] = { cmd_read, /* (12) */               CHECK_READY },
1216a1aff5bfSMarkus Armbruster     [ 0xad ] = { cmd_read_dvd_structure,            CHECK_READY },
12179ef2e93fSJohn Snow     [ 0xbb ] = { cmd_set_speed,                     NONDATA },
1218e1a064f9SKevin Wolf     [ 0xbd ] = { cmd_mechanism_status,              0 },
1219a1aff5bfSMarkus Armbruster     [ 0xbe ] = { cmd_read_cd,                       CHECK_READY },
1220a1aff5bfSMarkus Armbruster     /* [1] handler detects and reports not ready condition itself */
1221e1a064f9SKevin Wolf };
1222e1a064f9SKevin Wolf 
1223a60cf7e7SKevin Wolf void ide_atapi_cmd(IDEState *s)
1224a60cf7e7SKevin Wolf {
1225a60cf7e7SKevin Wolf     uint8_t *buf;
1226a60cf7e7SKevin Wolf 
1227a60cf7e7SKevin Wolf     buf = s->io_buffer;
1228a60cf7e7SKevin Wolf #ifdef DEBUG_IDE_ATAPI
1229a60cf7e7SKevin Wolf     {
1230a60cf7e7SKevin Wolf         int i;
1231a60cf7e7SKevin Wolf         printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1232a60cf7e7SKevin Wolf         for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1233ab719827SAlon Levy             printf(" %02x", buf[i]);
1234a60cf7e7SKevin Wolf         }
1235a60cf7e7SKevin Wolf         printf("\n");
1236a60cf7e7SKevin Wolf     }
1237a60cf7e7SKevin Wolf #endif
1238a60cf7e7SKevin Wolf     /*
1239e1a064f9SKevin Wolf      * If there's a UNIT_ATTENTION condition pending, only command flagged with
1240e1a064f9SKevin Wolf      * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1241e1a064f9SKevin Wolf      * condition response unless a higher priority status, defined by the drive
1242a60cf7e7SKevin Wolf      * here, is pending.
1243a60cf7e7SKevin Wolf      */
124467cc61e4SPaolo Bonzini     if (s->sense_key == UNIT_ATTENTION &&
1245e1a064f9SKevin Wolf         !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
1246a60cf7e7SKevin Wolf         ide_atapi_cmd_check_status(s);
1247a60cf7e7SKevin Wolf         return;
1248a60cf7e7SKevin Wolf     }
12494a737d14SAmit Shah     /*
12504a737d14SAmit Shah      * When a CD gets changed, we have to report an ejected state and
12514a737d14SAmit Shah      * then a loaded state to guests so that they detect tray
12524a737d14SAmit Shah      * open/close and media change events.  Guests that do not use
12534a737d14SAmit Shah      * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
12544a737d14SAmit Shah      * states rely on this behavior.
12554a737d14SAmit Shah      */
12560c6f08b0SPavel Hrdina     if (!(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA) &&
12574be74634SMarkus Armbruster         !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1258a60cf7e7SKevin Wolf 
12590c6f08b0SPavel Hrdina         if (s->cdrom_changed == 1) {
12600c6f08b0SPavel Hrdina             ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
12610c6f08b0SPavel Hrdina             s->cdrom_changed = 2;
12620c6f08b0SPavel Hrdina         } else {
12630c6f08b0SPavel Hrdina             ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1264a60cf7e7SKevin Wolf             s->cdrom_changed = 0;
12650c6f08b0SPavel Hrdina         }
12660c6f08b0SPavel Hrdina 
1267a60cf7e7SKevin Wolf         return;
1268a60cf7e7SKevin Wolf     }
1269e1a064f9SKevin Wolf 
12707a2c4b82SKevin Wolf     /* Report a Not Ready condition if appropriate for the command */
12717a2c4b82SKevin Wolf     if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
12724be74634SMarkus Armbruster         (!media_present(s) || !blk_is_inserted(s->blk)))
12737a2c4b82SKevin Wolf     {
127467cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
12757a2c4b82SKevin Wolf         return;
12767a2c4b82SKevin Wolf     }
12777a2c4b82SKevin Wolf 
12789ef2e93fSJohn Snow     /* Nondata commands permit the byte_count_limit to be 0.
12799ef2e93fSJohn Snow      * If this is a data-transferring PIO command and BCL is 0,
12809ef2e93fSJohn Snow      * we abort at the /ATA/ level, not the ATAPI level.
12819ef2e93fSJohn Snow      * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
12829ef2e93fSJohn Snow     if (!(atapi_cmd_table[s->io_buffer[0]].flags & NONDATA)) {
12839ef2e93fSJohn Snow         /* TODO: Check IDENTIFY data word 125 for default BCL (currently 0) */
1284*af0e00dbSJohn Snow         if (!(atapi_byte_count_limit(s) || s->atapi_dma)) {
12859ef2e93fSJohn Snow             /* TODO: Move abort back into core.c and make static inline again */
12869ef2e93fSJohn Snow             ide_abort_command(s);
12879ef2e93fSJohn Snow             return;
12889ef2e93fSJohn Snow         }
12899ef2e93fSJohn Snow     }
12909ef2e93fSJohn Snow 
1291e1a064f9SKevin Wolf     /* Execute the command */
1292e1a064f9SKevin Wolf     if (atapi_cmd_table[s->io_buffer[0]].handler) {
1293e1a064f9SKevin Wolf         atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1294e1a064f9SKevin Wolf         return;
129533231e0eSKevin Wolf     }
1296e1a064f9SKevin Wolf 
129767cc61e4SPaolo Bonzini     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
129833231e0eSKevin Wolf }
1299