xref: /openbmc/qemu/hw/ide/atapi.c (revision a9262f551eba44d4d0f9e396d7124c059a93e204)
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 
2653239262SPeter Maydell #include "qemu/osdep.h"
2733231e0eSKevin Wolf #include "hw/ide/internal.h"
280d09e41aSPaolo Bonzini #include "hw/scsi/scsi.h"
294be74634SMarkus Armbruster #include "sysemu/block-backend.h"
3082a13ff8SJohn Snow #include "trace.h"
3133231e0eSKevin Wolf 
3226a122d3SEric Blake #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
3326a122d3SEric Blake #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
3426a122d3SEric Blake 
3533231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
3633231e0eSKevin Wolf 
3733231e0eSKevin Wolf static void padstr8(uint8_t *buf, int buf_size, const char *src)
3833231e0eSKevin Wolf {
3933231e0eSKevin Wolf     int i;
4033231e0eSKevin Wolf     for(i = 0; i < buf_size; i++) {
4133231e0eSKevin Wolf         if (*src)
4233231e0eSKevin Wolf             buf[i] = *src++;
4333231e0eSKevin Wolf         else
4433231e0eSKevin Wolf             buf[i] = ' ';
4533231e0eSKevin Wolf     }
4633231e0eSKevin Wolf }
4733231e0eSKevin Wolf 
4833231e0eSKevin Wolf static void lba_to_msf(uint8_t *buf, int lba)
4933231e0eSKevin Wolf {
5033231e0eSKevin Wolf     lba += 150;
5133231e0eSKevin Wolf     buf[0] = (lba / 75) / 60;
5233231e0eSKevin Wolf     buf[1] = (lba / 75) % 60;
5333231e0eSKevin Wolf     buf[2] = lba % 75;
5433231e0eSKevin Wolf }
5533231e0eSKevin Wolf 
5633231e0eSKevin Wolf static inline int media_present(IDEState *s)
5733231e0eSKevin Wolf {
58a1aff5bfSMarkus Armbruster     return !s->tray_open && s->nb_sectors > 0;
5933231e0eSKevin Wolf }
6033231e0eSKevin Wolf 
61a7acf552SAmit Shah /* XXX: DVDs that could fit on a CD will be reported as a CD */
6233231e0eSKevin Wolf static inline int media_is_dvd(IDEState *s)
6333231e0eSKevin Wolf {
6433231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
6533231e0eSKevin Wolf }
6633231e0eSKevin Wolf 
6733231e0eSKevin Wolf static inline int media_is_cd(IDEState *s)
6833231e0eSKevin Wolf {
6933231e0eSKevin Wolf     return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
7033231e0eSKevin Wolf }
7133231e0eSKevin Wolf 
7233231e0eSKevin Wolf static void cd_data_to_raw(uint8_t *buf, int lba)
7333231e0eSKevin Wolf {
7433231e0eSKevin Wolf     /* sync bytes */
7533231e0eSKevin Wolf     buf[0] = 0x00;
7633231e0eSKevin Wolf     memset(buf + 1, 0xff, 10);
7733231e0eSKevin Wolf     buf[11] = 0x00;
7833231e0eSKevin Wolf     buf += 12;
7933231e0eSKevin Wolf     /* MSF */
8033231e0eSKevin Wolf     lba_to_msf(buf, lba);
8133231e0eSKevin Wolf     buf[3] = 0x01; /* mode 1 data */
8233231e0eSKevin Wolf     buf += 4;
8333231e0eSKevin Wolf     /* data */
8433231e0eSKevin Wolf     buf += 2048;
8533231e0eSKevin Wolf     /* XXX: ECC not computed */
8633231e0eSKevin Wolf     memset(buf, 0, 288);
8733231e0eSKevin Wolf }
8833231e0eSKevin Wolf 
895f81724dSPeter Lieven static int
905f81724dSPeter Lieven cd_read_sector_sync(IDEState *s)
9133231e0eSKevin Wolf {
9233231e0eSKevin Wolf     int ret;
93ece2d05eSAlberto Garcia     block_acct_start(blk_get_stats(s->blk), &s->acct,
9426a122d3SEric Blake                      ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
9533231e0eSKevin Wolf 
9682a13ff8SJohn Snow     trace_cd_read_sector_sync(s->lba);
975f81724dSPeter Lieven 
985f81724dSPeter Lieven     switch (s->cd_sector_size) {
9933231e0eSKevin Wolf     case 2048:
10026a122d3SEric Blake         ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
101*a9262f55SAlberto Faria                         ATAPI_SECTOR_SIZE, s->io_buffer, 0);
10233231e0eSKevin Wolf         break;
10333231e0eSKevin Wolf     case 2352:
10426a122d3SEric Blake         ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
105*a9262f55SAlberto Faria                         ATAPI_SECTOR_SIZE, s->io_buffer + 16, 0);
106ece2d05eSAlberto Garcia         if (ret >= 0) {
1075f81724dSPeter Lieven             cd_data_to_raw(s->io_buffer, s->lba);
108ece2d05eSAlberto Garcia         }
10933231e0eSKevin Wolf         break;
11033231e0eSKevin Wolf     default:
111ece2d05eSAlberto Garcia         block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
112ece2d05eSAlberto Garcia         return -EIO;
11333231e0eSKevin Wolf     }
114ece2d05eSAlberto Garcia 
115ece2d05eSAlberto Garcia     if (ret < 0) {
116ece2d05eSAlberto Garcia         block_acct_failed(blk_get_stats(s->blk), &s->acct);
117ece2d05eSAlberto Garcia     } else {
118ece2d05eSAlberto Garcia         block_acct_done(blk_get_stats(s->blk), &s->acct);
1195f81724dSPeter Lieven         s->lba++;
1205f81724dSPeter Lieven         s->io_buffer_index = 0;
121ece2d05eSAlberto Garcia     }
122ece2d05eSAlberto Garcia 
12333231e0eSKevin Wolf     return ret;
12433231e0eSKevin Wolf }
12533231e0eSKevin Wolf 
1265f81724dSPeter Lieven static void cd_read_sector_cb(void *opaque, int ret)
1275f81724dSPeter Lieven {
1285f81724dSPeter Lieven     IDEState *s = opaque;
1295f81724dSPeter Lieven 
13082a13ff8SJohn Snow     trace_cd_read_sector_cb(s->lba, ret);
1315f81724dSPeter Lieven 
1325f81724dSPeter Lieven     if (ret < 0) {
13336be0929SAlberto Garcia         block_acct_failed(blk_get_stats(s->blk), &s->acct);
1345f81724dSPeter Lieven         ide_atapi_io_error(s, ret);
1355f81724dSPeter Lieven         return;
1365f81724dSPeter Lieven     }
1375f81724dSPeter Lieven 
13836be0929SAlberto Garcia     block_acct_done(blk_get_stats(s->blk), &s->acct);
13936be0929SAlberto Garcia 
1405f81724dSPeter Lieven     if (s->cd_sector_size == 2352) {
1415f81724dSPeter Lieven         cd_data_to_raw(s->io_buffer, s->lba);
1425f81724dSPeter Lieven     }
1435f81724dSPeter Lieven 
1445f81724dSPeter Lieven     s->lba++;
1455f81724dSPeter Lieven     s->io_buffer_index = 0;
1465f81724dSPeter Lieven     s->status &= ~BUSY_STAT;
1475f81724dSPeter Lieven 
1485f81724dSPeter Lieven     ide_atapi_cmd_reply_end(s);
1495f81724dSPeter Lieven }
1505f81724dSPeter Lieven 
1515f81724dSPeter Lieven static int cd_read_sector(IDEState *s)
1525f81724dSPeter Lieven {
153e5863d49SVladimir Sementsov-Ogievskiy     void *buf;
154e5863d49SVladimir Sementsov-Ogievskiy 
1555f81724dSPeter Lieven     if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
15636be0929SAlberto Garcia         block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
1575f81724dSPeter Lieven         return -EINVAL;
1585f81724dSPeter Lieven     }
1595f81724dSPeter Lieven 
160e5863d49SVladimir Sementsov-Ogievskiy     buf = (s->cd_sector_size == 2352) ? s->io_buffer + 16 : s->io_buffer;
161e5863d49SVladimir Sementsov-Ogievskiy     qemu_iovec_init_buf(&s->qiov, buf, ATAPI_SECTOR_SIZE);
1625f81724dSPeter Lieven 
16382a13ff8SJohn Snow     trace_cd_read_sector(s->lba);
1645f81724dSPeter Lieven 
1655f81724dSPeter Lieven     block_acct_start(blk_get_stats(s->blk), &s->acct,
16626a122d3SEric Blake                      ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
1675f81724dSPeter Lieven 
16802506b20SPeter Lieven     ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
1695f81724dSPeter Lieven                        cd_read_sector_cb, s);
1705f81724dSPeter Lieven 
1715f81724dSPeter Lieven     s->status |= BUSY_STAT;
1725f81724dSPeter Lieven     return 0;
1735f81724dSPeter Lieven }
1745f81724dSPeter Lieven 
17533231e0eSKevin Wolf void ide_atapi_cmd_ok(IDEState *s)
17633231e0eSKevin Wolf {
17733231e0eSKevin Wolf     s->error = 0;
17833231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT;
17933231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
180d735b620SJohn Snow     ide_transfer_stop(s);
18133231e0eSKevin Wolf     ide_set_irq(s->bus);
18233231e0eSKevin Wolf }
18333231e0eSKevin Wolf 
18433231e0eSKevin Wolf void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
18533231e0eSKevin Wolf {
18682a13ff8SJohn Snow     trace_ide_atapi_cmd_error(s, sense_key, asc);
18733231e0eSKevin Wolf     s->error = sense_key << 4;
18833231e0eSKevin Wolf     s->status = READY_STAT | ERR_STAT;
18933231e0eSKevin Wolf     s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
19033231e0eSKevin Wolf     s->sense_key = sense_key;
19133231e0eSKevin Wolf     s->asc = asc;
192d735b620SJohn Snow     ide_transfer_stop(s);
19333231e0eSKevin Wolf     ide_set_irq(s->bus);
19433231e0eSKevin Wolf }
19533231e0eSKevin Wolf 
19633231e0eSKevin Wolf void ide_atapi_io_error(IDEState *s, int ret)
19733231e0eSKevin Wolf {
19833231e0eSKevin Wolf     /* XXX: handle more errors */
19933231e0eSKevin Wolf     if (ret == -ENOMEDIUM) {
20067cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, NOT_READY,
20133231e0eSKevin Wolf                             ASC_MEDIUM_NOT_PRESENT);
20233231e0eSKevin Wolf     } else {
20367cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
20433231e0eSKevin Wolf                             ASC_LOGICAL_BLOCK_OOR);
20533231e0eSKevin Wolf     }
20633231e0eSKevin Wolf }
20733231e0eSKevin Wolf 
208af0e00dbSJohn Snow static uint16_t atapi_byte_count_limit(IDEState *s)
209af0e00dbSJohn Snow {
210af0e00dbSJohn Snow     uint16_t bcl;
211af0e00dbSJohn Snow 
212af0e00dbSJohn Snow     bcl = s->lcyl | (s->hcyl << 8);
213af0e00dbSJohn Snow     if (bcl == 0xffff) {
214af0e00dbSJohn Snow         return 0xfffe;
215af0e00dbSJohn Snow     }
216af0e00dbSJohn Snow     return bcl;
217af0e00dbSJohn Snow }
218af0e00dbSJohn Snow 
21933231e0eSKevin Wolf /* The whole ATAPI transfer logic is handled in this function */
22033231e0eSKevin Wolf void ide_atapi_cmd_reply_end(IDEState *s)
22133231e0eSKevin Wolf {
22233231e0eSKevin Wolf     int byte_count_limit, size, ret;
223c173723fSPaolo Bonzini     while (s->packet_transfer_size > 0) {
22482a13ff8SJohn Snow         trace_ide_atapi_cmd_reply_end(s, s->packet_transfer_size,
22533231e0eSKevin Wolf                                       s->elementary_transfer_size,
22633231e0eSKevin Wolf                                       s->io_buffer_index);
227c173723fSPaolo Bonzini 
22833231e0eSKevin Wolf         /* see if a new sector must be read */
22933231e0eSKevin Wolf         if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
2305f81724dSPeter Lieven             if (!s->elementary_transfer_size) {
2315f81724dSPeter Lieven                 ret = cd_read_sector(s);
2325f81724dSPeter Lieven                 if (ret < 0) {
2335f81724dSPeter Lieven                     ide_atapi_io_error(s, ret);
2345f81724dSPeter Lieven                 }
2355f81724dSPeter Lieven                 return;
2365f81724dSPeter Lieven             } else {
2375f81724dSPeter Lieven                 /* rebuffering within an elementary transfer is
2385f81724dSPeter Lieven                  * only possible with a sync request because we
2395f81724dSPeter Lieven                  * end up with a race condition otherwise */
2405f81724dSPeter Lieven                 ret = cd_read_sector_sync(s);
24133231e0eSKevin Wolf                 if (ret < 0) {
24233231e0eSKevin Wolf                     ide_atapi_io_error(s, ret);
24333231e0eSKevin Wolf                     return;
24433231e0eSKevin Wolf                 }
2455f81724dSPeter Lieven             }
24633231e0eSKevin Wolf         }
24733231e0eSKevin Wolf         if (s->elementary_transfer_size > 0) {
24833231e0eSKevin Wolf             /* there are some data left to transmit in this elementary
24933231e0eSKevin Wolf                transfer */
25033231e0eSKevin Wolf             size = s->cd_sector_size - s->io_buffer_index;
25133231e0eSKevin Wolf             if (size > s->elementary_transfer_size)
25233231e0eSKevin Wolf                 size = s->elementary_transfer_size;
25333231e0eSKevin Wolf         } else {
25433231e0eSKevin Wolf             /* a new transfer is needed */
25533231e0eSKevin Wolf             s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
256d02cea64SPaolo Bonzini             ide_set_irq(s->bus);
257af0e00dbSJohn Snow             byte_count_limit = atapi_byte_count_limit(s);
25882a13ff8SJohn Snow             trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
25933231e0eSKevin Wolf             size = s->packet_transfer_size;
26033231e0eSKevin Wolf             if (size > byte_count_limit) {
26133231e0eSKevin Wolf                 /* byte count limit must be even if this case */
26233231e0eSKevin Wolf                 if (byte_count_limit & 1)
26333231e0eSKevin Wolf                     byte_count_limit--;
26433231e0eSKevin Wolf                 size = byte_count_limit;
26533231e0eSKevin Wolf             }
26633231e0eSKevin Wolf             s->lcyl = size;
26733231e0eSKevin Wolf             s->hcyl = size >> 8;
26833231e0eSKevin Wolf             s->elementary_transfer_size = size;
26933231e0eSKevin Wolf             /* we cannot transmit more than one sector at a time */
27033231e0eSKevin Wolf             if (s->lba != -1) {
27133231e0eSKevin Wolf                 if (size > (s->cd_sector_size - s->io_buffer_index))
27233231e0eSKevin Wolf                     size = (s->cd_sector_size - s->io_buffer_index);
27333231e0eSKevin Wolf             }
274d02cea64SPaolo Bonzini             trace_ide_atapi_cmd_reply_end_new(s, s->status);
275c173723fSPaolo Bonzini         }
27633231e0eSKevin Wolf         s->packet_transfer_size -= size;
27733231e0eSKevin Wolf         s->elementary_transfer_size -= size;
27833231e0eSKevin Wolf         s->io_buffer_index += size;
27981321228SPaolo Bonzini         assert(size <= s->io_buffer_total_len);
28081321228SPaolo Bonzini         assert(s->io_buffer_index <= s->io_buffer_total_len);
281c173723fSPaolo Bonzini 
282c173723fSPaolo Bonzini         /* Some adapters process PIO data right away.  In that case, we need
283c173723fSPaolo Bonzini          * to avoid mutual recursion between ide_transfer_start
284c173723fSPaolo Bonzini          * and ide_atapi_cmd_reply_end.
285c173723fSPaolo Bonzini          */
286c173723fSPaolo Bonzini         if (!ide_transfer_start_norecurse(s,
287c173723fSPaolo Bonzini                                           s->io_buffer + s->io_buffer_index - size,
288c173723fSPaolo Bonzini                                           size, ide_atapi_cmd_reply_end)) {
289c173723fSPaolo Bonzini             return;
29033231e0eSKevin Wolf         }
29133231e0eSKevin Wolf     }
292c173723fSPaolo Bonzini 
293c173723fSPaolo Bonzini     /* end of transfer */
294c173723fSPaolo Bonzini     trace_ide_atapi_cmd_reply_end_eot(s, s->status);
295c173723fSPaolo Bonzini     ide_atapi_cmd_ok(s);
296c173723fSPaolo Bonzini     ide_set_irq(s->bus);
29733231e0eSKevin Wolf }
29833231e0eSKevin Wolf 
29933231e0eSKevin Wolf /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
30033231e0eSKevin Wolf static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
30133231e0eSKevin Wolf {
30233231e0eSKevin Wolf     if (size > max_size)
30333231e0eSKevin Wolf         size = max_size;
30433231e0eSKevin Wolf     s->lba = -1; /* no sector read */
30533231e0eSKevin Wolf     s->packet_transfer_size = size;
30633231e0eSKevin Wolf     s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
30733231e0eSKevin Wolf     s->elementary_transfer_size = 0;
30833231e0eSKevin Wolf 
30933231e0eSKevin Wolf     if (s->atapi_dma) {
3104be74634SMarkus Armbruster         block_acct_start(blk_get_stats(s->blk), &s->acct, size,
3115366d0c8SBenoît Canet                          BLOCK_ACCT_READ);
31233231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
3134855b576SPaolo Bonzini         ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
31433231e0eSKevin Wolf     } else {
31533231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
316c71c06d4SPaolo Bonzini         s->io_buffer_index = 0;
31733231e0eSKevin Wolf         ide_atapi_cmd_reply_end(s);
31833231e0eSKevin Wolf     }
31933231e0eSKevin Wolf }
32033231e0eSKevin Wolf 
32199337bd1SLev Kujawski /* start a CD-ROM read command */
32233231e0eSKevin Wolf static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
32333231e0eSKevin Wolf                                    int sector_size)
32433231e0eSKevin Wolf {
325b8d7f1bcSPrasad J Pandit     assert(0 <= lba && lba < (s->nb_sectors >> 2));
326b8d7f1bcSPrasad J Pandit 
32733231e0eSKevin Wolf     s->lba = lba;
32833231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
32933231e0eSKevin Wolf     s->elementary_transfer_size = 0;
33033231e0eSKevin Wolf     s->io_buffer_index = sector_size;
33133231e0eSKevin Wolf     s->cd_sector_size = sector_size;
33233231e0eSKevin Wolf 
33333231e0eSKevin Wolf     ide_atapi_cmd_reply_end(s);
33433231e0eSKevin Wolf }
33533231e0eSKevin Wolf 
33633231e0eSKevin Wolf static void ide_atapi_cmd_check_status(IDEState *s)
33733231e0eSKevin Wolf {
33882a13ff8SJohn Snow     trace_ide_atapi_cmd_check_status(s);
33967cc61e4SPaolo Bonzini     s->error = MC_ERR | (UNIT_ATTENTION << 4);
34033231e0eSKevin Wolf     s->status = ERR_STAT;
34133231e0eSKevin Wolf     s->nsector = 0;
34233231e0eSKevin Wolf     ide_set_irq(s->bus);
34333231e0eSKevin Wolf }
34433231e0eSKevin Wolf /* ATAPI DMA support */
34533231e0eSKevin Wolf 
34633231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
34733231e0eSKevin Wolf {
34833231e0eSKevin Wolf     IDEState *s = opaque;
34933231e0eSKevin Wolf     int data_offset, n;
35033231e0eSKevin Wolf 
35133231e0eSKevin Wolf     if (ret < 0) {
352502356eeSPavel Butsykin         if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
353502356eeSPavel Butsykin             if (s->bus->error_status) {
3547f951b2dSJohn Snow                 s->bus->dma->aiocb = NULL;
355502356eeSPavel Butsykin                 return;
356502356eeSPavel Butsykin             }
35733231e0eSKevin Wolf             goto eot;
35833231e0eSKevin Wolf         }
359502356eeSPavel Butsykin     }
36033231e0eSKevin Wolf 
36133231e0eSKevin Wolf     if (s->io_buffer_size > 0) {
36233231e0eSKevin Wolf         /*
36333231e0eSKevin Wolf          * For a cdrom read sector command (s->lba != -1),
36433231e0eSKevin Wolf          * adjust the lba for the next s->io_buffer_size chunk
36533231e0eSKevin Wolf          * and dma the current chunk.
36633231e0eSKevin Wolf          * For a command != read (s->lba == -1), just transfer
36733231e0eSKevin Wolf          * the reply data.
36833231e0eSKevin Wolf          */
36933231e0eSKevin Wolf         if (s->lba != -1) {
37033231e0eSKevin Wolf             if (s->cd_sector_size == 2352) {
37133231e0eSKevin Wolf                 n = 1;
37233231e0eSKevin Wolf                 cd_data_to_raw(s->io_buffer, s->lba);
37333231e0eSKevin Wolf             } else {
37433231e0eSKevin Wolf                 n = s->io_buffer_size >> 11;
37533231e0eSKevin Wolf             }
37633231e0eSKevin Wolf             s->lba += n;
37733231e0eSKevin Wolf         }
37833231e0eSKevin Wolf         s->packet_transfer_size -= s->io_buffer_size;
37933231e0eSKevin Wolf         if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
38033231e0eSKevin Wolf             goto eot;
38133231e0eSKevin Wolf     }
38233231e0eSKevin Wolf 
38333231e0eSKevin Wolf     if (s->packet_transfer_size <= 0) {
38433231e0eSKevin Wolf         s->status = READY_STAT | SEEK_STAT;
38533231e0eSKevin Wolf         s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
38633231e0eSKevin Wolf         ide_set_irq(s->bus);
387a597e79cSChristoph Hellwig         goto eot;
38833231e0eSKevin Wolf     }
38933231e0eSKevin Wolf 
39033231e0eSKevin Wolf     s->io_buffer_index = 0;
39133231e0eSKevin Wolf     if (s->cd_sector_size == 2352) {
39233231e0eSKevin Wolf         n = 1;
39333231e0eSKevin Wolf         s->io_buffer_size = s->cd_sector_size;
39433231e0eSKevin Wolf         data_offset = 16;
39533231e0eSKevin Wolf     } else {
39633231e0eSKevin Wolf         n = s->packet_transfer_size >> 11;
39733231e0eSKevin Wolf         if (n > (IDE_DMA_BUF_SECTORS / 4))
39833231e0eSKevin Wolf             n = (IDE_DMA_BUF_SECTORS / 4);
39933231e0eSKevin Wolf         s->io_buffer_size = n * 2048;
40033231e0eSKevin Wolf         data_offset = 0;
40133231e0eSKevin Wolf     }
4020e168d35SJohn Snow     trace_ide_atapi_cmd_read_dma_cb_aio(s, s->lba, n);
4039942586bSVladimir Sementsov-Ogievskiy     qemu_iovec_init_buf(&s->bus->dma->qiov, s->io_buffer + data_offset,
4049942586bSVladimir Sementsov-Ogievskiy                         n * ATAPI_SECTOR_SIZE);
405a597e79cSChristoph Hellwig 
40602506b20SPeter Lieven     s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
40733231e0eSKevin Wolf                                             &s->bus->dma->qiov, n * 4,
40833231e0eSKevin Wolf                                             ide_atapi_cmd_read_dma_cb, s);
409a597e79cSChristoph Hellwig     return;
410ad54ae80SPaolo Bonzini 
411a597e79cSChristoph Hellwig eot:
412ece2d05eSAlberto Garcia     if (ret < 0) {
413ece2d05eSAlberto Garcia         block_acct_failed(blk_get_stats(s->blk), &s->acct);
414ece2d05eSAlberto Garcia     } else {
4154be74634SMarkus Armbruster         block_acct_done(blk_get_stats(s->blk), &s->acct);
416ece2d05eSAlberto Garcia     }
4170e7ce54cSPaolo Bonzini     ide_set_inactive(s, false);
41833231e0eSKevin Wolf }
41933231e0eSKevin Wolf 
42099337bd1SLev Kujawski /* start a CD-ROM read command with DMA */
42133231e0eSKevin Wolf /* XXX: test if DMA is available */
42233231e0eSKevin Wolf static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
42333231e0eSKevin Wolf                                    int sector_size)
42433231e0eSKevin Wolf {
425b8d7f1bcSPrasad J Pandit     assert(0 <= lba && lba < (s->nb_sectors >> 2));
426b8d7f1bcSPrasad J Pandit 
42733231e0eSKevin Wolf     s->lba = lba;
42833231e0eSKevin Wolf     s->packet_transfer_size = nb_sectors * sector_size;
42933231e0eSKevin Wolf     s->io_buffer_size = 0;
43033231e0eSKevin Wolf     s->cd_sector_size = sector_size;
43133231e0eSKevin Wolf 
4324be74634SMarkus Armbruster     block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
4335366d0c8SBenoît Canet                      BLOCK_ACCT_READ);
434a597e79cSChristoph Hellwig 
43533231e0eSKevin Wolf     /* XXX: check if BUSY_STAT should be set */
43633231e0eSKevin Wolf     s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
4374855b576SPaolo Bonzini     ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
43833231e0eSKevin Wolf }
43933231e0eSKevin Wolf 
44033231e0eSKevin Wolf static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
44133231e0eSKevin Wolf                                int sector_size)
44233231e0eSKevin Wolf {
44382a13ff8SJohn Snow     trace_ide_atapi_cmd_read(s, s->atapi_dma ? "dma" : "pio",
44433231e0eSKevin Wolf                              lba, nb_sectors);
44533231e0eSKevin Wolf     if (s->atapi_dma) {
44633231e0eSKevin Wolf         ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
44733231e0eSKevin Wolf     } else {
44833231e0eSKevin Wolf         ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
44933231e0eSKevin Wolf     }
45033231e0eSKevin Wolf }
45133231e0eSKevin Wolf 
452a71754e5SDr. David Alan Gilbert void ide_atapi_dma_restart(IDEState *s)
453a71754e5SDr. David Alan Gilbert {
454a71754e5SDr. David Alan Gilbert     /*
4559a41826fSPavel Butsykin      * At this point we can just re-evaluate the packet command and start over.
4569a41826fSPavel Butsykin      * The presence of ->dma_cb callback in the pre_save ensures that the packet
4579a41826fSPavel Butsykin      * command has been completely sent and we can safely restart command.
458a71754e5SDr. David Alan Gilbert      */
4599a41826fSPavel Butsykin     s->unit = s->bus->retry_unit;
4609a41826fSPavel Butsykin     s->bus->dma->ops->restart_dma(s->bus->dma);
4619a41826fSPavel Butsykin     ide_atapi_cmd(s);
462a71754e5SDr. David Alan Gilbert }
463a71754e5SDr. David Alan Gilbert 
46433231e0eSKevin Wolf static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
46533231e0eSKevin Wolf                                             uint16_t profile)
46633231e0eSKevin Wolf {
46733231e0eSKevin Wolf     uint8_t *buf_profile = buf + 12; /* start of profiles */
46833231e0eSKevin Wolf 
46933231e0eSKevin Wolf     buf_profile += ((*index) * 4); /* start of indexed profile */
470614ab7d1SPhilippe Mathieu-Daudé     stw_be_p(buf_profile, profile);
47133231e0eSKevin Wolf     buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
47233231e0eSKevin Wolf 
47333231e0eSKevin Wolf     /* each profile adds 4 bytes to the response */
47433231e0eSKevin Wolf     (*index)++;
47533231e0eSKevin Wolf     buf[11] += 4; /* Additional Length */
47633231e0eSKevin Wolf 
47733231e0eSKevin Wolf     return 4;
47833231e0eSKevin Wolf }
47933231e0eSKevin Wolf 
48033231e0eSKevin Wolf static int ide_dvd_read_structure(IDEState *s, int format,
48133231e0eSKevin Wolf                                   const uint8_t *packet, uint8_t *buf)
48233231e0eSKevin Wolf {
48333231e0eSKevin Wolf     switch (format) {
48433231e0eSKevin Wolf         case 0x0: /* Physical format information */
48533231e0eSKevin Wolf             {
48633231e0eSKevin Wolf                 int layer = packet[6];
48733231e0eSKevin Wolf                 uint64_t total_sectors;
48833231e0eSKevin Wolf 
48933231e0eSKevin Wolf                 if (layer != 0)
49033231e0eSKevin Wolf                     return -ASC_INV_FIELD_IN_CMD_PACKET;
49133231e0eSKevin Wolf 
492e119bcacSKevin Wolf                 total_sectors = s->nb_sectors >> 2;
493e119bcacSKevin Wolf                 if (total_sectors == 0) {
49433231e0eSKevin Wolf                     return -ASC_MEDIUM_NOT_PRESENT;
495e119bcacSKevin Wolf                 }
49633231e0eSKevin Wolf 
49733231e0eSKevin Wolf                 buf[4] = 1;   /* DVD-ROM, part version 1 */
49833231e0eSKevin Wolf                 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
49933231e0eSKevin Wolf                 buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
50033231e0eSKevin Wolf                 buf[7] = 0;   /* default densities */
50133231e0eSKevin Wolf 
50233231e0eSKevin Wolf                 /* FIXME: 0x30000 per spec? */
503614ab7d1SPhilippe Mathieu-Daudé                 stl_be_p(buf + 8, 0); /* start sector */
504614ab7d1SPhilippe Mathieu-Daudé                 stl_be_p(buf + 12, total_sectors - 1); /* end sector */
505614ab7d1SPhilippe Mathieu-Daudé                 stl_be_p(buf + 16, total_sectors - 1); /* l0 end sector */
50633231e0eSKevin Wolf 
50733231e0eSKevin Wolf                 /* Size of buffer, not including 2 byte size field */
508d8ee2591SPeter Maydell                 stw_be_p(buf, 2048 + 2);
50933231e0eSKevin Wolf 
51033231e0eSKevin Wolf                 /* 2k data + 4 byte header */
51133231e0eSKevin Wolf                 return (2048 + 4);
51233231e0eSKevin Wolf             }
51333231e0eSKevin Wolf 
51433231e0eSKevin Wolf         case 0x01: /* DVD copyright information */
51533231e0eSKevin Wolf             buf[4] = 0; /* no copyright data */
51633231e0eSKevin Wolf             buf[5] = 0; /* no region restrictions */
51733231e0eSKevin Wolf 
51833231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
519d8ee2591SPeter Maydell             stw_be_p(buf, 4 + 2);
52033231e0eSKevin Wolf 
52133231e0eSKevin Wolf             /* 4 byte header + 4 byte data */
52233231e0eSKevin Wolf             return (4 + 4);
52333231e0eSKevin Wolf 
52433231e0eSKevin Wolf         case 0x03: /* BCA information - invalid field for no BCA info */
52533231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
52633231e0eSKevin Wolf 
52733231e0eSKevin Wolf         case 0x04: /* DVD disc manufacturing information */
52833231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
529d8ee2591SPeter Maydell             stw_be_p(buf, 2048 + 2);
53033231e0eSKevin Wolf 
53133231e0eSKevin Wolf             /* 2k data + 4 byte header */
53233231e0eSKevin Wolf             return (2048 + 4);
53333231e0eSKevin Wolf 
53433231e0eSKevin Wolf         case 0xff:
53533231e0eSKevin Wolf             /*
53633231e0eSKevin Wolf              * This lists all the command capabilities above.  Add new ones
53733231e0eSKevin Wolf              * in order and update the length and buffer return values.
53833231e0eSKevin Wolf              */
53933231e0eSKevin Wolf 
54033231e0eSKevin Wolf             buf[4] = 0x00; /* Physical format */
54133231e0eSKevin Wolf             buf[5] = 0x40; /* Not writable, is readable */
542d8ee2591SPeter Maydell             stw_be_p(buf + 6, 2048 + 4);
54333231e0eSKevin Wolf 
54433231e0eSKevin Wolf             buf[8] = 0x01; /* Copyright info */
54533231e0eSKevin Wolf             buf[9] = 0x40; /* Not writable, is readable */
546d8ee2591SPeter Maydell             stw_be_p(buf + 10, 4 + 4);
54733231e0eSKevin Wolf 
54833231e0eSKevin Wolf             buf[12] = 0x03; /* BCA info */
54933231e0eSKevin Wolf             buf[13] = 0x40; /* Not writable, is readable */
550d8ee2591SPeter Maydell             stw_be_p(buf + 14, 188 + 4);
55133231e0eSKevin Wolf 
55233231e0eSKevin Wolf             buf[16] = 0x04; /* Manufacturing info */
55333231e0eSKevin Wolf             buf[17] = 0x40; /* Not writable, is readable */
554d8ee2591SPeter Maydell             stw_be_p(buf + 18, 2048 + 4);
55533231e0eSKevin Wolf 
55633231e0eSKevin Wolf             /* Size of buffer, not including 2 byte size field */
557d8ee2591SPeter Maydell             stw_be_p(buf, 16 + 2);
55833231e0eSKevin Wolf 
55933231e0eSKevin Wolf             /* data written + 4 byte header */
56033231e0eSKevin Wolf             return (16 + 4);
56133231e0eSKevin Wolf 
56233231e0eSKevin Wolf         default: /* TODO: formats beyond DVD-ROM requires */
56333231e0eSKevin Wolf             return -ASC_INV_FIELD_IN_CMD_PACKET;
56433231e0eSKevin Wolf     }
56533231e0eSKevin Wolf }
56633231e0eSKevin Wolf 
56733231e0eSKevin Wolf static unsigned int event_status_media(IDEState *s,
56833231e0eSKevin Wolf                                        uint8_t *buf)
56933231e0eSKevin Wolf {
57033231e0eSKevin Wolf     uint8_t event_code, media_status;
57133231e0eSKevin Wolf 
57233231e0eSKevin Wolf     media_status = 0;
573dd063333SMarkus Armbruster     if (s->tray_open) {
57433231e0eSKevin Wolf         media_status = MS_TRAY_OPEN;
5754be74634SMarkus Armbruster     } else if (blk_is_inserted(s->blk)) {
57633231e0eSKevin Wolf         media_status = MS_MEDIA_PRESENT;
57733231e0eSKevin Wolf     }
57833231e0eSKevin Wolf 
57933231e0eSKevin Wolf     /* Event notification descriptor */
58033231e0eSKevin Wolf     event_code = MEC_NO_CHANGE;
5812df0a3a3SPaolo Bonzini     if (media_status != MS_TRAY_OPEN) {
5822df0a3a3SPaolo Bonzini         if (s->events.new_media) {
58333231e0eSKevin Wolf             event_code = MEC_NEW_MEDIA;
58433231e0eSKevin Wolf             s->events.new_media = false;
5852df0a3a3SPaolo Bonzini         } else if (s->events.eject_request) {
5862df0a3a3SPaolo Bonzini             event_code = MEC_EJECT_REQUESTED;
5872df0a3a3SPaolo Bonzini             s->events.eject_request = false;
5882df0a3a3SPaolo Bonzini         }
58933231e0eSKevin Wolf     }
59033231e0eSKevin Wolf 
59133231e0eSKevin Wolf     buf[4] = event_code;
59233231e0eSKevin Wolf     buf[5] = media_status;
59333231e0eSKevin Wolf 
59433231e0eSKevin Wolf     /* These fields are reserved, just clear them. */
59533231e0eSKevin Wolf     buf[6] = 0;
59633231e0eSKevin Wolf     buf[7] = 0;
59733231e0eSKevin Wolf 
59833231e0eSKevin Wolf     return 8; /* We wrote to 4 extra bytes from the header */
59933231e0eSKevin Wolf }
60033231e0eSKevin Wolf 
601e7bd708eSJohn Snow /*
602e7bd708eSJohn Snow  * Before transferring data or otherwise signalling acceptance of a command
603e7bd708eSJohn Snow  * marked CONDDATA, we must check the validity of the byte_count_limit.
604e7bd708eSJohn Snow  */
605e7bd708eSJohn Snow static bool validate_bcl(IDEState *s)
606e7bd708eSJohn Snow {
607e7bd708eSJohn Snow     /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
608e7bd708eSJohn Snow     if (s->atapi_dma || atapi_byte_count_limit(s)) {
609e7bd708eSJohn Snow         return true;
610e7bd708eSJohn Snow     }
611e7bd708eSJohn Snow 
612e7bd708eSJohn Snow     /* TODO: Move abort back into core.c and introduce proper error flow between
613e7bd708eSJohn Snow      *       ATAPI layer and IDE core layer */
614e7bd708eSJohn Snow     ide_abort_command(s);
615e7bd708eSJohn Snow     return false;
616e7bd708eSJohn Snow }
617e7bd708eSJohn Snow 
618e1a064f9SKevin Wolf static void cmd_get_event_status_notification(IDEState *s,
619e1a064f9SKevin Wolf                                               uint8_t *buf)
62033231e0eSKevin Wolf {
621e1a064f9SKevin Wolf     const uint8_t *packet = buf;
622e1a064f9SKevin Wolf 
62333231e0eSKevin Wolf     struct {
62433231e0eSKevin Wolf         uint8_t opcode;
62533231e0eSKevin Wolf         uint8_t polled;        /* lsb bit is polled; others are reserved */
62633231e0eSKevin Wolf         uint8_t reserved2[2];
62733231e0eSKevin Wolf         uint8_t class;
62833231e0eSKevin Wolf         uint8_t reserved3[2];
62933231e0eSKevin Wolf         uint16_t len;
63033231e0eSKevin Wolf         uint8_t control;
631541dc0d4SStefan Weil     } QEMU_PACKED *gesn_cdb;
63233231e0eSKevin Wolf 
63333231e0eSKevin Wolf     struct {
63433231e0eSKevin Wolf         uint16_t len;
63533231e0eSKevin Wolf         uint8_t notification_class;
63633231e0eSKevin Wolf         uint8_t supported_events;
637541dc0d4SStefan Weil     } QEMU_PACKED *gesn_event_header;
63833231e0eSKevin Wolf     unsigned int max_len, used_len;
63933231e0eSKevin Wolf 
64033231e0eSKevin Wolf     gesn_cdb = (void *)packet;
64133231e0eSKevin Wolf     gesn_event_header = (void *)buf;
64233231e0eSKevin Wolf 
64333231e0eSKevin Wolf     max_len = be16_to_cpu(gesn_cdb->len);
64433231e0eSKevin Wolf 
64533231e0eSKevin Wolf     /* It is fine by the MMC spec to not support async mode operations */
64633231e0eSKevin Wolf     if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
64733231e0eSKevin Wolf         /* Only polling is supported, asynchronous mode is not. */
64867cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
64933231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
65033231e0eSKevin Wolf         return;
65133231e0eSKevin Wolf     }
65233231e0eSKevin Wolf 
65333231e0eSKevin Wolf     /* polling mode operation */
65433231e0eSKevin Wolf 
65533231e0eSKevin Wolf     /*
65633231e0eSKevin Wolf      * These are the supported events.
65733231e0eSKevin Wolf      *
65833231e0eSKevin Wolf      * We currently only support requests of the 'media' type.
659f0f992e6SPaolo Bonzini      * Notification class requests and supported event classes are bitmasks,
660f0f992e6SPaolo Bonzini      * but they are build from the same values as the "notification class"
661f0f992e6SPaolo Bonzini      * field.
66233231e0eSKevin Wolf      */
663f0f992e6SPaolo Bonzini     gesn_event_header->supported_events = 1 << GESN_MEDIA;
66433231e0eSKevin Wolf 
66533231e0eSKevin Wolf     /*
66633231e0eSKevin Wolf      * We use |= below to set the class field; other bits in this byte
66733231e0eSKevin Wolf      * are reserved now but this is useful to do if we have to use the
66833231e0eSKevin Wolf      * reserved fields later.
66933231e0eSKevin Wolf      */
67033231e0eSKevin Wolf     gesn_event_header->notification_class = 0;
67133231e0eSKevin Wolf 
67233231e0eSKevin Wolf     /*
67333231e0eSKevin Wolf      * Responses to requests are to be based on request priority.  The
67433231e0eSKevin Wolf      * notification_class_request_type enum above specifies the
67533231e0eSKevin Wolf      * priority: upper elements are higher prio than lower ones.
67633231e0eSKevin Wolf      */
677f0f992e6SPaolo Bonzini     if (gesn_cdb->class & (1 << GESN_MEDIA)) {
678f0f992e6SPaolo Bonzini         gesn_event_header->notification_class |= GESN_MEDIA;
67933231e0eSKevin Wolf         used_len = event_status_media(s, buf);
68033231e0eSKevin Wolf     } else {
68133231e0eSKevin Wolf         gesn_event_header->notification_class = 0x80; /* No event available */
68233231e0eSKevin Wolf         used_len = sizeof(*gesn_event_header);
68333231e0eSKevin Wolf     }
68433231e0eSKevin Wolf     gesn_event_header->len = cpu_to_be16(used_len
68533231e0eSKevin Wolf                                          - sizeof(*gesn_event_header));
68633231e0eSKevin Wolf     ide_atapi_cmd_reply(s, used_len, max_len);
68733231e0eSKevin Wolf }
68833231e0eSKevin Wolf 
689a60cf7e7SKevin Wolf static void cmd_request_sense(IDEState *s, uint8_t *buf)
69033231e0eSKevin Wolf {
691a60cf7e7SKevin Wolf     int max_len = buf[4];
692a60cf7e7SKevin Wolf 
693a60cf7e7SKevin Wolf     memset(buf, 0, 18);
694a60cf7e7SKevin Wolf     buf[0] = 0x70 | (1 << 7);
695a60cf7e7SKevin Wolf     buf[2] = s->sense_key;
696a60cf7e7SKevin Wolf     buf[7] = 10;
697a60cf7e7SKevin Wolf     buf[12] = s->asc;
698a60cf7e7SKevin Wolf 
69967cc61e4SPaolo Bonzini     if (s->sense_key == UNIT_ATTENTION) {
70067cc61e4SPaolo Bonzini         s->sense_key = NO_SENSE;
701a60cf7e7SKevin Wolf     }
702a60cf7e7SKevin Wolf 
703a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, 18, max_len);
704a60cf7e7SKevin Wolf }
705a60cf7e7SKevin Wolf 
706a60cf7e7SKevin Wolf static void cmd_inquiry(IDEState *s, uint8_t *buf)
707a60cf7e7SKevin Wolf {
7089a502563SJohn Snow     uint8_t page_code = buf[2];
709a60cf7e7SKevin Wolf     int max_len = buf[4];
710a60cf7e7SKevin Wolf 
7119a502563SJohn Snow     unsigned idx = 0;
7129a502563SJohn Snow     unsigned size_idx;
7139a502563SJohn Snow     unsigned preamble_len;
7149a502563SJohn Snow 
7159a502563SJohn Snow     /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
7169a502563SJohn Snow      * we are being asked for a specific page of info indicated by byte 2. */
7179a502563SJohn Snow     if (buf[1] & 0x01) {
7189a502563SJohn Snow         preamble_len = 4;
7199a502563SJohn Snow         size_idx = 3;
7209a502563SJohn Snow 
7219a502563SJohn Snow         buf[idx++] = 0x05;      /* CD-ROM */
7229a502563SJohn Snow         buf[idx++] = page_code; /* Page Code */
7239a502563SJohn Snow         buf[idx++] = 0x00;      /* reserved */
7249a502563SJohn Snow         idx++;                  /* length (set later) */
7259a502563SJohn Snow 
7269a502563SJohn Snow         switch (page_code) {
7279a502563SJohn Snow         case 0x00:
7289a502563SJohn Snow             /* Supported Pages: List of supported VPD responses. */
7299a502563SJohn Snow             buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
7309a502563SJohn Snow             buf[idx++] = 0x83; /* 0x83: Device Identification. */
7319a502563SJohn Snow             break;
7329a502563SJohn Snow 
7339a502563SJohn Snow         case 0x83:
7349a502563SJohn Snow             /* Device Identification. Each entry is optional, but the entries
7359a502563SJohn Snow              * included here are modeled after libata's VPD responses.
7369a502563SJohn Snow              * If the response is given, at least one entry must be present. */
7379a502563SJohn Snow 
7389a502563SJohn Snow             /* Entry 1: Serial */
7399a502563SJohn Snow             if (idx + 24 > max_len) {
7409a502563SJohn Snow                 /* Not enough room for even the first entry: */
7419a502563SJohn Snow                 /* 4 byte header + 20 byte string */
7429a502563SJohn Snow                 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
7439a502563SJohn Snow                                     ASC_DATA_PHASE_ERROR);
7449a502563SJohn Snow                 return;
7459a502563SJohn Snow             }
7469a502563SJohn Snow             buf[idx++] = 0x02; /* Ascii */
7479a502563SJohn Snow             buf[idx++] = 0x00; /* Vendor Specific */
7489a502563SJohn Snow             buf[idx++] = 0x00;
7499a502563SJohn Snow             buf[idx++] = 20;   /* Remaining length */
7509a502563SJohn Snow             padstr8(buf + idx, 20, s->drive_serial_str);
7519a502563SJohn Snow             idx += 20;
7529a502563SJohn Snow 
7539a502563SJohn Snow             /* Entry 2: Drive Model and Serial */
7549a502563SJohn Snow             if (idx + 72 > max_len) {
7559a502563SJohn Snow                 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
7569a502563SJohn Snow                 goto out;
7579a502563SJohn Snow             }
7589a502563SJohn Snow             buf[idx++] = 0x02; /* Ascii */
7599a502563SJohn Snow             buf[idx++] = 0x01; /* T10 Vendor */
7609a502563SJohn Snow             buf[idx++] = 0x00;
7619a502563SJohn Snow             buf[idx++] = 68;
7629a502563SJohn Snow             padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
7639a502563SJohn Snow             idx += 8;
7649a502563SJohn Snow             padstr8(buf + idx, 40, s->drive_model_str);
7659a502563SJohn Snow             idx += 40;
7669a502563SJohn Snow             padstr8(buf + idx, 20, s->drive_serial_str);
7679a502563SJohn Snow             idx += 20;
7689a502563SJohn Snow 
7699a502563SJohn Snow             /* Entry 3: WWN */
7709a502563SJohn Snow             if (s->wwn && (idx + 12 <= max_len)) {
7719a502563SJohn Snow                 /* 4 byte header + 8 byte wwn */
7729a502563SJohn Snow                 buf[idx++] = 0x01; /* Binary */
7739a502563SJohn Snow                 buf[idx++] = 0x03; /* NAA */
7749a502563SJohn Snow                 buf[idx++] = 0x00;
7759a502563SJohn Snow                 buf[idx++] = 0x08;
7769a502563SJohn Snow                 stq_be_p(&buf[idx], s->wwn);
7779a502563SJohn Snow                 idx += 8;
7789a502563SJohn Snow             }
7799a502563SJohn Snow             break;
7809a502563SJohn Snow 
7819a502563SJohn Snow         default:
7829a502563SJohn Snow             /* SPC-3, revision 23 sec. 6.4 */
7839a502563SJohn Snow             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
7849a502563SJohn Snow                                 ASC_INV_FIELD_IN_CMD_PACKET);
7859a502563SJohn Snow             return;
7869a502563SJohn Snow         }
7879a502563SJohn Snow     } else {
7889a502563SJohn Snow         preamble_len = 5;
7899a502563SJohn Snow         size_idx = 4;
7909a502563SJohn Snow 
791a60cf7e7SKevin Wolf         buf[0] = 0x05; /* CD-ROM */
792a60cf7e7SKevin Wolf         buf[1] = 0x80; /* removable */
793a60cf7e7SKevin Wolf         buf[2] = 0x00; /* ISO */
794a60cf7e7SKevin Wolf         buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
7959a502563SJohn Snow         /* buf[size_idx] set below. */
796a60cf7e7SKevin Wolf         buf[5] = 0;    /* reserved */
797a60cf7e7SKevin Wolf         buf[6] = 0;    /* reserved */
798a60cf7e7SKevin Wolf         buf[7] = 0;    /* reserved */
799a60cf7e7SKevin Wolf         padstr8(buf + 8, 8, "QEMU");
800a60cf7e7SKevin Wolf         padstr8(buf + 16, 16, "QEMU DVD-ROM");
801a60cf7e7SKevin Wolf         padstr8(buf + 32, 4, s->version);
8029a502563SJohn Snow         idx = 36;
8039a502563SJohn Snow     }
8049a502563SJohn Snow 
8059a502563SJohn Snow  out:
8069a502563SJohn Snow     buf[size_idx] = idx - preamble_len;
8079a502563SJohn Snow     ide_atapi_cmd_reply(s, idx, max_len);
808a60cf7e7SKevin Wolf }
809a60cf7e7SKevin Wolf 
810a60cf7e7SKevin Wolf static void cmd_get_configuration(IDEState *s, uint8_t *buf)
811a60cf7e7SKevin Wolf {
812a60cf7e7SKevin Wolf     uint32_t len;
813a60cf7e7SKevin Wolf     uint8_t index = 0;
81433231e0eSKevin Wolf     int max_len;
81533231e0eSKevin Wolf 
816a60cf7e7SKevin Wolf     /* only feature 0 is supported */
817a60cf7e7SKevin Wolf     if (buf[2] != 0 || buf[3] != 0) {
81867cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
819a60cf7e7SKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
82033231e0eSKevin Wolf         return;
82133231e0eSKevin Wolf     }
82233231e0eSKevin Wolf 
823a60cf7e7SKevin Wolf     /* XXX: could result in alignment problems in some architectures */
824614ab7d1SPhilippe Mathieu-Daudé     max_len = lduw_be_p(buf + 7);
825a60cf7e7SKevin Wolf 
826a60cf7e7SKevin Wolf     /*
827a60cf7e7SKevin Wolf      * XXX: avoid overflow for io_buffer if max_len is bigger than
828a60cf7e7SKevin Wolf      *      the size of that buffer (dimensioned to max number of
829a60cf7e7SKevin Wolf      *      sectors to transfer at once)
830a60cf7e7SKevin Wolf      *
831a60cf7e7SKevin Wolf      *      Only a problem if the feature/profiles grow.
832a60cf7e7SKevin Wolf      */
833a71f2d22SPhilippe Mathieu-Daudé     if (max_len > BDRV_SECTOR_SIZE) {
834a60cf7e7SKevin Wolf         /* XXX: assume 1 sector */
835a71f2d22SPhilippe Mathieu-Daudé         max_len = BDRV_SECTOR_SIZE;
83633231e0eSKevin Wolf     }
837a60cf7e7SKevin Wolf 
838a60cf7e7SKevin Wolf     memset(buf, 0, max_len);
839a60cf7e7SKevin Wolf     /*
840a60cf7e7SKevin Wolf      * the number of sectors from the media tells us which profile
841a60cf7e7SKevin Wolf      * to use as current.  0 means there is no media
842a60cf7e7SKevin Wolf      */
843a60cf7e7SKevin Wolf     if (media_is_dvd(s)) {
844614ab7d1SPhilippe Mathieu-Daudé         stw_be_p(buf + 6, MMC_PROFILE_DVD_ROM);
845a60cf7e7SKevin Wolf     } else if (media_is_cd(s)) {
846614ab7d1SPhilippe Mathieu-Daudé         stw_be_p(buf + 6, MMC_PROFILE_CD_ROM);
84733231e0eSKevin Wolf     }
848a60cf7e7SKevin Wolf 
849a60cf7e7SKevin Wolf     buf[10] = 0x02 | 0x01; /* persistent and current */
850a60cf7e7SKevin Wolf     len = 12; /* headers: 8 + 4 */
851a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
852a60cf7e7SKevin Wolf     len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
853614ab7d1SPhilippe Mathieu-Daudé     stl_be_p(buf, len - 4); /* data length */
854a60cf7e7SKevin Wolf 
855a60cf7e7SKevin Wolf     ide_atapi_cmd_reply(s, len, max_len);
856a60cf7e7SKevin Wolf }
857a60cf7e7SKevin Wolf 
858a60cf7e7SKevin Wolf static void cmd_mode_sense(IDEState *s, uint8_t *buf)
85933231e0eSKevin Wolf {
86033231e0eSKevin Wolf     int action, code;
861a60cf7e7SKevin Wolf     int max_len;
862a60cf7e7SKevin Wolf 
863614ab7d1SPhilippe Mathieu-Daudé     max_len = lduw_be_p(buf + 7);
864a60cf7e7SKevin Wolf     action = buf[2] >> 6;
865a60cf7e7SKevin Wolf     code = buf[2] & 0x3f;
866a60cf7e7SKevin Wolf 
86733231e0eSKevin Wolf     switch(action) {
86833231e0eSKevin Wolf     case 0: /* current values */
86933231e0eSKevin Wolf         switch(code) {
87067cc61e4SPaolo Bonzini         case MODE_PAGE_R_W_ERROR: /* error recovery */
871614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[0], 16 - 2);
87233231e0eSKevin Wolf             buf[2] = 0x70;
87333231e0eSKevin Wolf             buf[3] = 0;
87433231e0eSKevin Wolf             buf[4] = 0;
87533231e0eSKevin Wolf             buf[5] = 0;
87633231e0eSKevin Wolf             buf[6] = 0;
87733231e0eSKevin Wolf             buf[7] = 0;
87833231e0eSKevin Wolf 
879af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_R_W_ERROR;
880af0e1ea2SPaolo Bonzini             buf[9] = 16 - 10;
88133231e0eSKevin Wolf             buf[10] = 0x00;
88233231e0eSKevin Wolf             buf[11] = 0x05;
88333231e0eSKevin Wolf             buf[12] = 0x00;
88433231e0eSKevin Wolf             buf[13] = 0x00;
88533231e0eSKevin Wolf             buf[14] = 0x00;
88633231e0eSKevin Wolf             buf[15] = 0x00;
88733231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 16, max_len);
88833231e0eSKevin Wolf             break;
88967cc61e4SPaolo Bonzini         case MODE_PAGE_AUDIO_CTL:
890614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[0], 24 - 2);
89133231e0eSKevin Wolf             buf[2] = 0x70;
89233231e0eSKevin Wolf             buf[3] = 0;
89333231e0eSKevin Wolf             buf[4] = 0;
89433231e0eSKevin Wolf             buf[5] = 0;
89533231e0eSKevin Wolf             buf[6] = 0;
89633231e0eSKevin Wolf             buf[7] = 0;
89733231e0eSKevin Wolf 
898af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_AUDIO_CTL;
899af0e1ea2SPaolo Bonzini             buf[9] = 24 - 10;
90033231e0eSKevin Wolf             /* Fill with CDROM audio volume */
90133231e0eSKevin Wolf             buf[17] = 0;
90233231e0eSKevin Wolf             buf[19] = 0;
90333231e0eSKevin Wolf             buf[21] = 0;
90433231e0eSKevin Wolf             buf[23] = 0;
90533231e0eSKevin Wolf 
90633231e0eSKevin Wolf             ide_atapi_cmd_reply(s, 24, max_len);
90733231e0eSKevin Wolf             break;
90867cc61e4SPaolo Bonzini         case MODE_PAGE_CAPABILITIES:
909614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[0], 30 - 2);
91033231e0eSKevin Wolf             buf[2] = 0x70;
91133231e0eSKevin Wolf             buf[3] = 0;
91233231e0eSKevin Wolf             buf[4] = 0;
91333231e0eSKevin Wolf             buf[5] = 0;
91433231e0eSKevin Wolf             buf[6] = 0;
91533231e0eSKevin Wolf             buf[7] = 0;
91633231e0eSKevin Wolf 
917af0e1ea2SPaolo Bonzini             buf[8] = MODE_PAGE_CAPABILITIES;
9182c20ae11SPaolo Bonzini             buf[9] = 30 - 10;
919a07c7dcdSPaolo Bonzini             buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
92033231e0eSKevin Wolf             buf[11] = 0x00;
92133231e0eSKevin Wolf 
92233231e0eSKevin Wolf             /* Claim PLAY_AUDIO capability (0x01) since some Linux
92333231e0eSKevin Wolf                code checks for this to automount media. */
92433231e0eSKevin Wolf             buf[12] = 0x71;
92533231e0eSKevin Wolf             buf[13] = 3 << 5;
92633231e0eSKevin Wolf             buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
927a0a7573bSMarkus Armbruster             if (s->tray_locked) {
928a07c7dcdSPaolo Bonzini                 buf[14] |= 1 << 1;
929a0a7573bSMarkus Armbruster             }
930a07c7dcdSPaolo Bonzini             buf[15] = 0x00; /* No volume & mute control, no changer */
931614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[16], 704); /* 4x read speed */
932a07c7dcdSPaolo Bonzini             buf[18] = 0; /* Two volume levels */
93333231e0eSKevin Wolf             buf[19] = 2;
934614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[20], 512); /* 512k buffer */
935614ab7d1SPhilippe Mathieu-Daudé             stw_be_p(&buf[22], 704); /* 4x read speed current */
93633231e0eSKevin Wolf             buf[24] = 0;
93733231e0eSKevin Wolf             buf[25] = 0;
93833231e0eSKevin Wolf             buf[26] = 0;
93933231e0eSKevin Wolf             buf[27] = 0;
9402c20ae11SPaolo Bonzini             buf[28] = 0;
9412c20ae11SPaolo Bonzini             buf[29] = 0;
9422c20ae11SPaolo Bonzini             ide_atapi_cmd_reply(s, 30, max_len);
94333231e0eSKevin Wolf             break;
94433231e0eSKevin Wolf         default:
94533231e0eSKevin Wolf             goto error_cmd;
94633231e0eSKevin Wolf         }
94733231e0eSKevin Wolf         break;
94833231e0eSKevin Wolf     case 1: /* changeable values */
94933231e0eSKevin Wolf         goto error_cmd;
95033231e0eSKevin Wolf     case 2: /* default values */
95133231e0eSKevin Wolf         goto error_cmd;
95233231e0eSKevin Wolf     default:
95333231e0eSKevin Wolf     case 3: /* saved values */
95467cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
95533231e0eSKevin Wolf                             ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
95633231e0eSKevin Wolf         break;
95733231e0eSKevin Wolf     }
958a60cf7e7SKevin Wolf     return;
959a60cf7e7SKevin Wolf 
960a60cf7e7SKevin Wolf error_cmd:
96167cc61e4SPaolo Bonzini     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
96233231e0eSKevin Wolf }
963a60cf7e7SKevin Wolf 
964a60cf7e7SKevin Wolf static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
965a60cf7e7SKevin Wolf {
9667a2c4b82SKevin Wolf     /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
9677a2c4b82SKevin Wolf      * come here, we know that it's ready. */
96833231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
969a60cf7e7SKevin Wolf }
970a60cf7e7SKevin Wolf 
971a60cf7e7SKevin Wolf static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
972a60cf7e7SKevin Wolf {
973a0a7573bSMarkus Armbruster     s->tray_locked = buf[4] & 1;
9744be74634SMarkus Armbruster     blk_lock_medium(s->blk, buf[4] & 1);
975a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
976a60cf7e7SKevin Wolf }
977a60cf7e7SKevin Wolf 
978a60cf7e7SKevin Wolf static void cmd_read(IDEState *s, uint8_t* buf)
97933231e0eSKevin Wolf {
980b8d7f1bcSPrasad J Pandit     unsigned int nb_sectors, lba;
981b8d7f1bcSPrasad J Pandit 
982b8d7f1bcSPrasad J Pandit     /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
983b8d7f1bcSPrasad J Pandit     uint64_t total_sectors = s->nb_sectors >> 2;
98433231e0eSKevin Wolf 
985a60cf7e7SKevin Wolf     if (buf[0] == GPCMD_READ_10) {
986614ab7d1SPhilippe Mathieu-Daudé         nb_sectors = lduw_be_p(buf + 7);
987a60cf7e7SKevin Wolf     } else {
988614ab7d1SPhilippe Mathieu-Daudé         nb_sectors = ldl_be_p(buf + 6);
989a60cf7e7SKevin Wolf     }
99033231e0eSKevin Wolf     if (nb_sectors == 0) {
99133231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
992a60cf7e7SKevin Wolf         return;
99333231e0eSKevin Wolf     }
994a60cf7e7SKevin Wolf 
995b8d7f1bcSPrasad J Pandit     lba = ldl_be_p(buf + 2);
996b8d7f1bcSPrasad J Pandit     if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
997b8d7f1bcSPrasad J Pandit         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
998b8d7f1bcSPrasad J Pandit         return;
999b8d7f1bcSPrasad J Pandit     }
1000b8d7f1bcSPrasad J Pandit 
100133231e0eSKevin Wolf     ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
100233231e0eSKevin Wolf }
1003a60cf7e7SKevin Wolf 
1004a60cf7e7SKevin Wolf static void cmd_read_cd(IDEState *s, uint8_t* buf)
100533231e0eSKevin Wolf {
1006b8d7f1bcSPrasad J Pandit     unsigned int nb_sectors, lba, transfer_request;
1007b8d7f1bcSPrasad J Pandit 
1008b8d7f1bcSPrasad J Pandit     /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
1009b8d7f1bcSPrasad J Pandit     uint64_t total_sectors = s->nb_sectors >> 2;
101033231e0eSKevin Wolf 
1011a60cf7e7SKevin Wolf     nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
101233231e0eSKevin Wolf     if (nb_sectors == 0) {
101333231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
1014a60cf7e7SKevin Wolf         return;
101533231e0eSKevin Wolf     }
1016a60cf7e7SKevin Wolf 
1017b8d7f1bcSPrasad J Pandit     lba = ldl_be_p(buf + 2);
1018b8d7f1bcSPrasad J Pandit     if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
1019b8d7f1bcSPrasad J Pandit         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1020b8d7f1bcSPrasad J Pandit         return;
1021b8d7f1bcSPrasad J Pandit     }
1022b8d7f1bcSPrasad J Pandit 
1023e7bd708eSJohn Snow     transfer_request = buf[9] & 0xf8;
1024e7bd708eSJohn Snow     if (transfer_request == 0x00) {
102533231e0eSKevin Wolf         /* nothing */
102633231e0eSKevin Wolf         ide_atapi_cmd_ok(s);
1027e7bd708eSJohn Snow         return;
1028e7bd708eSJohn Snow     }
1029e7bd708eSJohn Snow 
1030e7bd708eSJohn Snow     /* Check validity of BCL before transferring data */
1031e7bd708eSJohn Snow     if (!validate_bcl(s)) {
1032e7bd708eSJohn Snow         return;
1033e7bd708eSJohn Snow     }
1034e7bd708eSJohn Snow 
1035e7bd708eSJohn Snow     switch (transfer_request) {
103633231e0eSKevin Wolf     case 0x10:
103733231e0eSKevin Wolf         /* normal read */
103833231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
103933231e0eSKevin Wolf         break;
104033231e0eSKevin Wolf     case 0xf8:
104133231e0eSKevin Wolf         /* read all data */
104233231e0eSKevin Wolf         ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
104333231e0eSKevin Wolf         break;
104433231e0eSKevin Wolf     default:
104567cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
104633231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
104733231e0eSKevin Wolf         break;
104833231e0eSKevin Wolf     }
104933231e0eSKevin Wolf }
1050a60cf7e7SKevin Wolf 
1051a60cf7e7SKevin Wolf static void cmd_seek(IDEState *s, uint8_t* buf)
105233231e0eSKevin Wolf {
105333231e0eSKevin Wolf     unsigned int lba;
1054e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
105533231e0eSKevin Wolf 
1056614ab7d1SPhilippe Mathieu-Daudé     lba = ldl_be_p(buf + 2);
105733231e0eSKevin Wolf     if (lba >= total_sectors) {
105867cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1059a60cf7e7SKevin Wolf         return;
106033231e0eSKevin Wolf     }
1061a60cf7e7SKevin Wolf 
106233231e0eSKevin Wolf     ide_atapi_cmd_ok(s);
106333231e0eSKevin Wolf }
1064a60cf7e7SKevin Wolf 
1065a60cf7e7SKevin Wolf static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
106633231e0eSKevin Wolf {
1067fdec4404SMarkus Armbruster     int sense;
1068f0776564SMarkus Armbruster     bool start = buf[4] & 1;
1069f0776564SMarkus Armbruster     bool loej = buf[4] & 2;     /* load on start, eject on !start */
1070ce560dcfSRonnie Sahlberg     int pwrcnd = buf[4] & 0xf0;
1071ce560dcfSRonnie Sahlberg 
1072ce560dcfSRonnie Sahlberg     if (pwrcnd) {
1073ce560dcfSRonnie Sahlberg         /* eject/load only happens for power condition == 0 */
107403441c3aSKevin Wolf         ide_atapi_cmd_ok(s);
1075ce560dcfSRonnie Sahlberg         return;
1076ce560dcfSRonnie Sahlberg     }
107733231e0eSKevin Wolf 
1078f0776564SMarkus Armbruster     if (loej) {
107948f65b3fSMarkus Armbruster         if (!start && !s->tray_open && s->tray_locked) {
10804be74634SMarkus Armbruster             sense = blk_is_inserted(s->blk)
108167cc61e4SPaolo Bonzini                 ? NOT_READY : ILLEGAL_REQUEST;
1082a60cf7e7SKevin Wolf             ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1083fdec4404SMarkus Armbruster             return;
108433231e0eSKevin Wolf         }
1085d88b1819SLuiz Capitulino 
1086d88b1819SLuiz Capitulino         if (s->tray_open != !start) {
10874be74634SMarkus Armbruster             blk_eject(s->blk, !start);
1088dd063333SMarkus Armbruster             s->tray_open = !start;
1089dd063333SMarkus Armbruster         }
1090d88b1819SLuiz Capitulino     }
1091fdec4404SMarkus Armbruster 
1092fdec4404SMarkus Armbruster     ide_atapi_cmd_ok(s);
109333231e0eSKevin Wolf }
1094a60cf7e7SKevin Wolf 
1095a60cf7e7SKevin Wolf static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
109633231e0eSKevin Wolf {
1097614ab7d1SPhilippe Mathieu-Daudé     int max_len = lduw_be_p(buf + 8);
1098a60cf7e7SKevin Wolf 
1099614ab7d1SPhilippe Mathieu-Daudé     stw_be_p(buf, 0);
110033231e0eSKevin Wolf     /* no current LBA */
110133231e0eSKevin Wolf     buf[2] = 0;
110233231e0eSKevin Wolf     buf[3] = 0;
110333231e0eSKevin Wolf     buf[4] = 0;
110433231e0eSKevin Wolf     buf[5] = 1;
1105614ab7d1SPhilippe Mathieu-Daudé     stw_be_p(buf + 6, 0);
110633231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, max_len);
110733231e0eSKevin Wolf }
1108a60cf7e7SKevin Wolf 
1109a60cf7e7SKevin Wolf static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
111033231e0eSKevin Wolf {
111133231e0eSKevin Wolf     int format, msf, start_track, len;
1112a60cf7e7SKevin Wolf     int max_len;
11137a2c4b82SKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
1114a60cf7e7SKevin Wolf 
1115614ab7d1SPhilippe Mathieu-Daudé     max_len = lduw_be_p(buf + 7);
1116a60cf7e7SKevin Wolf     format = buf[9] >> 6;
1117a60cf7e7SKevin Wolf     msf = (buf[1] >> 1) & 1;
1118a60cf7e7SKevin Wolf     start_track = buf[6];
1119a60cf7e7SKevin Wolf 
112033231e0eSKevin Wolf     switch(format) {
112133231e0eSKevin Wolf     case 0:
112233231e0eSKevin Wolf         len = cdrom_read_toc(total_sectors, buf, msf, start_track);
112333231e0eSKevin Wolf         if (len < 0)
112433231e0eSKevin Wolf             goto error_cmd;
112533231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
112633231e0eSKevin Wolf         break;
112733231e0eSKevin Wolf     case 1:
112833231e0eSKevin Wolf         /* multi session : only a single session defined */
112933231e0eSKevin Wolf         memset(buf, 0, 12);
113033231e0eSKevin Wolf         buf[1] = 0x0a;
113133231e0eSKevin Wolf         buf[2] = 0x01;
113233231e0eSKevin Wolf         buf[3] = 0x01;
113333231e0eSKevin Wolf         ide_atapi_cmd_reply(s, 12, max_len);
113433231e0eSKevin Wolf         break;
113533231e0eSKevin Wolf     case 2:
113633231e0eSKevin Wolf         len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
113733231e0eSKevin Wolf         if (len < 0)
113833231e0eSKevin Wolf             goto error_cmd;
113933231e0eSKevin Wolf         ide_atapi_cmd_reply(s, len, max_len);
114033231e0eSKevin Wolf         break;
114133231e0eSKevin Wolf     default:
114233231e0eSKevin Wolf     error_cmd:
114367cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
114433231e0eSKevin Wolf                             ASC_INV_FIELD_IN_CMD_PACKET);
114533231e0eSKevin Wolf     }
114633231e0eSKevin Wolf }
1147a60cf7e7SKevin Wolf 
1148a60cf7e7SKevin Wolf static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
114933231e0eSKevin Wolf {
1150e119bcacSKevin Wolf     uint64_t total_sectors = s->nb_sectors >> 2;
115133231e0eSKevin Wolf 
115233231e0eSKevin Wolf     /* NOTE: it is really the number of sectors minus 1 */
1153614ab7d1SPhilippe Mathieu-Daudé     stl_be_p(buf, total_sectors - 1);
1154614ab7d1SPhilippe Mathieu-Daudé     stl_be_p(buf + 4, 2048);
115533231e0eSKevin Wolf     ide_atapi_cmd_reply(s, 8, 8);
115633231e0eSKevin Wolf }
1157a60cf7e7SKevin Wolf 
115855042b95SPaolo Bonzini static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
115955042b95SPaolo Bonzini {
116055042b95SPaolo Bonzini     uint8_t type = buf[1] & 7;
1161614ab7d1SPhilippe Mathieu-Daudé     uint32_t max_len = lduw_be_p(buf + 7);
116255042b95SPaolo Bonzini 
116355042b95SPaolo Bonzini     /* Types 1/2 are only defined for Blu-Ray.  */
116455042b95SPaolo Bonzini     if (type != 0) {
116555042b95SPaolo Bonzini         ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
116655042b95SPaolo Bonzini                             ASC_INV_FIELD_IN_CMD_PACKET);
116755042b95SPaolo Bonzini         return;
116855042b95SPaolo Bonzini     }
116955042b95SPaolo Bonzini 
117055042b95SPaolo Bonzini     memset(buf, 0, 34);
117155042b95SPaolo Bonzini     buf[1] = 32;
117255042b95SPaolo Bonzini     buf[2] = 0xe; /* last session complete, disc finalized */
117355042b95SPaolo Bonzini     buf[3] = 1;   /* first track on disc */
117455042b95SPaolo Bonzini     buf[4] = 1;   /* # of sessions */
117555042b95SPaolo Bonzini     buf[5] = 1;   /* first track of last session */
117655042b95SPaolo Bonzini     buf[6] = 1;   /* last track of last session */
117755042b95SPaolo Bonzini     buf[7] = 0x20; /* unrestricted use */
117855042b95SPaolo Bonzini     buf[8] = 0x00; /* CD-ROM or DVD-ROM */
117955042b95SPaolo Bonzini     /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
118055042b95SPaolo Bonzini     /* 12-23: not meaningful for CD-ROM or DVD-ROM */
118155042b95SPaolo Bonzini     /* 24-31: disc bar code */
118255042b95SPaolo Bonzini     /* 32: disc application code */
118355042b95SPaolo Bonzini     /* 33: number of OPC tables */
118455042b95SPaolo Bonzini 
118555042b95SPaolo Bonzini     ide_atapi_cmd_reply(s, 34, max_len);
118655042b95SPaolo Bonzini }
118755042b95SPaolo Bonzini 
1188a60cf7e7SKevin Wolf static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
118933231e0eSKevin Wolf {
1190a60cf7e7SKevin Wolf     int max_len;
1191a60cf7e7SKevin Wolf     int media = buf[1];
1192a60cf7e7SKevin Wolf     int format = buf[7];
119333231e0eSKevin Wolf     int ret;
119433231e0eSKevin Wolf 
1195614ab7d1SPhilippe Mathieu-Daudé     max_len = lduw_be_p(buf + 8);
119633231e0eSKevin Wolf 
119733231e0eSKevin Wolf     if (format < 0xff) {
119833231e0eSKevin Wolf         if (media_is_cd(s)) {
119967cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
120033231e0eSKevin Wolf                                 ASC_INCOMPATIBLE_FORMAT);
1201a60cf7e7SKevin Wolf             return;
120233231e0eSKevin Wolf         } else if (!media_present(s)) {
120367cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
120433231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
1205a60cf7e7SKevin Wolf             return;
120633231e0eSKevin Wolf         }
120733231e0eSKevin Wolf     }
120833231e0eSKevin Wolf 
1209a71f2d22SPhilippe Mathieu-Daudé     memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * BDRV_SECTOR_SIZE + 4 ?
1210a71f2d22SPhilippe Mathieu-Daudé            IDE_DMA_BUF_SECTORS * BDRV_SECTOR_SIZE + 4 : max_len);
121133231e0eSKevin Wolf 
121233231e0eSKevin Wolf     switch (format) {
121333231e0eSKevin Wolf         case 0x00 ... 0x7f:
121433231e0eSKevin Wolf         case 0xff:
121533231e0eSKevin Wolf             if (media == 0) {
1216a60cf7e7SKevin Wolf                 ret = ide_dvd_read_structure(s, format, buf, buf);
121733231e0eSKevin Wolf 
1218a60cf7e7SKevin Wolf                 if (ret < 0) {
121967cc61e4SPaolo Bonzini                     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1220a60cf7e7SKevin Wolf                 } else {
122133231e0eSKevin Wolf                     ide_atapi_cmd_reply(s, ret, max_len);
1222a60cf7e7SKevin Wolf                 }
122333231e0eSKevin Wolf 
122433231e0eSKevin Wolf                 break;
122533231e0eSKevin Wolf             }
122633231e0eSKevin Wolf             /* TODO: BD support, fall through for now */
122733231e0eSKevin Wolf 
122833231e0eSKevin Wolf         /* Generic disk structures */
122933231e0eSKevin Wolf         case 0x80: /* TODO: AACS volume identifier */
123033231e0eSKevin Wolf         case 0x81: /* TODO: AACS media serial number */
123133231e0eSKevin Wolf         case 0x82: /* TODO: AACS media identifier */
123233231e0eSKevin Wolf         case 0x83: /* TODO: AACS media key block */
123333231e0eSKevin Wolf         case 0x90: /* TODO: List of recognized format layers */
123433231e0eSKevin Wolf         case 0xc0: /* TODO: Write protection status */
123533231e0eSKevin Wolf         default:
123667cc61e4SPaolo Bonzini             ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
123733231e0eSKevin Wolf                                 ASC_INV_FIELD_IN_CMD_PACKET);
123833231e0eSKevin Wolf             break;
123933231e0eSKevin Wolf     }
124033231e0eSKevin Wolf }
1241a60cf7e7SKevin Wolf 
1242a60cf7e7SKevin Wolf static void cmd_set_speed(IDEState *s, uint8_t* buf)
1243a60cf7e7SKevin Wolf {
1244a60cf7e7SKevin Wolf     ide_atapi_cmd_ok(s);
1245a60cf7e7SKevin Wolf }
1246a60cf7e7SKevin Wolf 
1247e1a064f9SKevin Wolf enum {
1248e1a064f9SKevin Wolf     /*
1249e1a064f9SKevin Wolf      * Only commands flagged as ALLOW_UA are allowed to run under a
1250e1a064f9SKevin Wolf      * unit attention condition. (See MMC-5, section 4.1.6.1)
1251e1a064f9SKevin Wolf      */
1252e1a064f9SKevin Wolf     ALLOW_UA = 0x01,
12537a2c4b82SKevin Wolf 
12547a2c4b82SKevin Wolf     /*
12557a2c4b82SKevin Wolf      * Commands flagged with CHECK_READY can only execute if a medium is present.
12567a2c4b82SKevin Wolf      * Otherwise they report the Not Ready Condition. (See MMC-5, section
12577a2c4b82SKevin Wolf      * 4.1.8)
12587a2c4b82SKevin Wolf      */
12597a2c4b82SKevin Wolf     CHECK_READY = 0x02,
12609ef2e93fSJohn Snow 
12619ef2e93fSJohn Snow     /*
12629ef2e93fSJohn Snow      * Commands flagged with NONDATA do not in any circumstances return
12639ef2e93fSJohn Snow      * any data via ide_atapi_cmd_reply. These commands are exempt from
12649ef2e93fSJohn Snow      * the normal byte_count_limit constraints.
12659ef2e93fSJohn Snow      * See ATA8-ACS3 "7.21.5 Byte Count Limit"
12669ef2e93fSJohn Snow      */
12679ef2e93fSJohn Snow     NONDATA = 0x04,
1268e7bd708eSJohn Snow 
1269e7bd708eSJohn Snow     /*
1270e7bd708eSJohn Snow      * CONDDATA implies a command that transfers data only conditionally based
1271e7bd708eSJohn Snow      * on the presence of suboptions. It should be exempt from the BCL check at
1272e7bd708eSJohn Snow      * command validation time, but it needs to be checked at the command
1273e7bd708eSJohn Snow      * handler level instead.
1274e7bd708eSJohn Snow      */
1275e7bd708eSJohn Snow     CONDDATA = 0x08,
1276e1a064f9SKevin Wolf };
1277e1a064f9SKevin Wolf 
1278f36aa12dSJohn Snow static const struct AtapiCmd {
1279e1a064f9SKevin Wolf     void (*handler)(IDEState *s, uint8_t *buf);
1280e1a064f9SKevin Wolf     int flags;
1281e1a064f9SKevin Wolf } atapi_cmd_table[0x100] = {
12829ef2e93fSJohn Snow     [ 0x00 ] = { cmd_test_unit_ready,               CHECK_READY | NONDATA },
1283e1a064f9SKevin Wolf     [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
1284e1a064f9SKevin Wolf     [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
12859ef2e93fSJohn Snow     [ 0x1b ] = { cmd_start_stop_unit,               NONDATA }, /* [1] */
12869ef2e93fSJohn Snow     [ 0x1e ] = { cmd_prevent_allow_medium_removal,  NONDATA },
12877a2c4b82SKevin Wolf     [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
1288a1aff5bfSMarkus Armbruster     [ 0x28 ] = { cmd_read, /* (10) */               CHECK_READY },
12899ef2e93fSJohn Snow     [ 0x2b ] = { cmd_seek,                          CHECK_READY | NONDATA },
12907a2c4b82SKevin Wolf     [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
1291e1a064f9SKevin Wolf     [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
1292e1a064f9SKevin Wolf     [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
129355042b95SPaolo Bonzini     [ 0x51 ] = { cmd_read_disc_information,         CHECK_READY },
1294e1a064f9SKevin Wolf     [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
1295a1aff5bfSMarkus Armbruster     [ 0xa8 ] = { cmd_read, /* (12) */               CHECK_READY },
1296a1aff5bfSMarkus Armbruster     [ 0xad ] = { cmd_read_dvd_structure,            CHECK_READY },
12979ef2e93fSJohn Snow     [ 0xbb ] = { cmd_set_speed,                     NONDATA },
1298e1a064f9SKevin Wolf     [ 0xbd ] = { cmd_mechanism_status,              0 },
1299e7bd708eSJohn Snow     [ 0xbe ] = { cmd_read_cd,                       CHECK_READY | CONDDATA },
1300a1aff5bfSMarkus Armbruster     /* [1] handler detects and reports not ready condition itself */
1301e1a064f9SKevin Wolf };
1302e1a064f9SKevin Wolf 
1303a60cf7e7SKevin Wolf void ide_atapi_cmd(IDEState *s)
1304a60cf7e7SKevin Wolf {
1305f36aa12dSJohn Snow     uint8_t *buf = s->io_buffer;
1306f36aa12dSJohn Snow     const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
1307a60cf7e7SKevin Wolf 
130882a13ff8SJohn Snow     trace_ide_atapi_cmd(s, s->io_buffer[0]);
130982a13ff8SJohn Snow 
131082a13ff8SJohn Snow     if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET)) {
131182a13ff8SJohn Snow         /* Each pretty-printed byte needs two bytes and a space; */
131282a13ff8SJohn Snow         char *ppacket = g_malloc(ATAPI_PACKET_SIZE * 3 + 1);
1313a60cf7e7SKevin Wolf         int i;
1314a60cf7e7SKevin Wolf         for (i = 0; i < ATAPI_PACKET_SIZE; i++) {
131582a13ff8SJohn Snow             sprintf(ppacket + (i * 3), "%02x ", buf[i]);
1316a60cf7e7SKevin Wolf         }
131782a13ff8SJohn Snow         trace_ide_atapi_cmd_packet(s, s->lcyl | (s->hcyl << 8), ppacket);
131882a13ff8SJohn Snow         g_free(ppacket);
1319a60cf7e7SKevin Wolf     }
1320f36aa12dSJohn Snow 
1321a60cf7e7SKevin Wolf     /*
1322e1a064f9SKevin Wolf      * If there's a UNIT_ATTENTION condition pending, only command flagged with
1323e1a064f9SKevin Wolf      * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1324e1a064f9SKevin Wolf      * condition response unless a higher priority status, defined by the drive
1325a60cf7e7SKevin Wolf      * here, is pending.
1326a60cf7e7SKevin Wolf      */
1327f36aa12dSJohn Snow     if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
1328a60cf7e7SKevin Wolf         ide_atapi_cmd_check_status(s);
1329a60cf7e7SKevin Wolf         return;
1330a60cf7e7SKevin Wolf     }
13314a737d14SAmit Shah     /*
13324a737d14SAmit Shah      * When a CD gets changed, we have to report an ejected state and
13334a737d14SAmit Shah      * then a loaded state to guests so that they detect tray
13344a737d14SAmit Shah      * open/close and media change events.  Guests that do not use
13354a737d14SAmit Shah      * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
13364a737d14SAmit Shah      * states rely on this behavior.
13374a737d14SAmit Shah      */
1338f36aa12dSJohn Snow     if (!(cmd->flags & ALLOW_UA) &&
13394be74634SMarkus Armbruster         !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1340a60cf7e7SKevin Wolf 
13410c6f08b0SPavel Hrdina         if (s->cdrom_changed == 1) {
13420c6f08b0SPavel Hrdina             ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
13430c6f08b0SPavel Hrdina             s->cdrom_changed = 2;
13440c6f08b0SPavel Hrdina         } else {
13450c6f08b0SPavel Hrdina             ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1346a60cf7e7SKevin Wolf             s->cdrom_changed = 0;
13470c6f08b0SPavel Hrdina         }
13480c6f08b0SPavel Hrdina 
1349a60cf7e7SKevin Wolf         return;
1350a60cf7e7SKevin Wolf     }
1351e1a064f9SKevin Wolf 
13527a2c4b82SKevin Wolf     /* Report a Not Ready condition if appropriate for the command */
1353f36aa12dSJohn Snow     if ((cmd->flags & CHECK_READY) &&
13544be74634SMarkus Armbruster         (!media_present(s) || !blk_is_inserted(s->blk)))
13557a2c4b82SKevin Wolf     {
135667cc61e4SPaolo Bonzini         ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
13577a2c4b82SKevin Wolf         return;
13587a2c4b82SKevin Wolf     }
13597a2c4b82SKevin Wolf 
1360e7bd708eSJohn Snow     /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
13619ef2e93fSJohn Snow      * If this is a data-transferring PIO command and BCL is 0,
13629ef2e93fSJohn Snow      * we abort at the /ATA/ level, not the ATAPI level.
13639ef2e93fSJohn Snow      * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1364e7bd708eSJohn Snow     if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
1365e7bd708eSJohn Snow         if (!validate_bcl(s)) {
13669ef2e93fSJohn Snow             return;
13679ef2e93fSJohn Snow         }
13689ef2e93fSJohn Snow     }
13699ef2e93fSJohn Snow 
1370e1a064f9SKevin Wolf     /* Execute the command */
1371f36aa12dSJohn Snow     if (cmd->handler) {
1372f36aa12dSJohn Snow         cmd->handler(s, buf);
1373e1a064f9SKevin Wolf         return;
137433231e0eSKevin Wolf     }
1375e1a064f9SKevin Wolf 
137667cc61e4SPaolo Bonzini     ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
137733231e0eSKevin Wolf }
1378