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" 3033231e0eSKevin Wolf 3126a122d3SEric Blake #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS) 3226a122d3SEric Blake #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS) 3326a122d3SEric Blake 3433231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret); 3533231e0eSKevin Wolf 3633231e0eSKevin Wolf static void padstr8(uint8_t *buf, int buf_size, const char *src) 3733231e0eSKevin Wolf { 3833231e0eSKevin Wolf int i; 3933231e0eSKevin Wolf for(i = 0; i < buf_size; i++) { 4033231e0eSKevin Wolf if (*src) 4133231e0eSKevin Wolf buf[i] = *src++; 4233231e0eSKevin Wolf else 4333231e0eSKevin Wolf buf[i] = ' '; 4433231e0eSKevin Wolf } 4533231e0eSKevin Wolf } 4633231e0eSKevin Wolf 4733231e0eSKevin Wolf static inline void cpu_to_ube16(uint8_t *buf, int val) 4833231e0eSKevin Wolf { 4933231e0eSKevin Wolf buf[0] = val >> 8; 5033231e0eSKevin Wolf buf[1] = val & 0xff; 5133231e0eSKevin Wolf } 5233231e0eSKevin Wolf 5333231e0eSKevin Wolf static inline void cpu_to_ube32(uint8_t *buf, unsigned int val) 5433231e0eSKevin Wolf { 5533231e0eSKevin Wolf buf[0] = val >> 24; 5633231e0eSKevin Wolf buf[1] = val >> 16; 5733231e0eSKevin Wolf buf[2] = val >> 8; 5833231e0eSKevin Wolf buf[3] = val & 0xff; 5933231e0eSKevin Wolf } 6033231e0eSKevin Wolf 6133231e0eSKevin Wolf static inline int ube16_to_cpu(const uint8_t *buf) 6233231e0eSKevin Wolf { 6333231e0eSKevin Wolf return (buf[0] << 8) | buf[1]; 6433231e0eSKevin Wolf } 6533231e0eSKevin Wolf 6633231e0eSKevin Wolf static inline int ube32_to_cpu(const uint8_t *buf) 6733231e0eSKevin Wolf { 6833231e0eSKevin Wolf return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; 6933231e0eSKevin Wolf } 7033231e0eSKevin Wolf 7133231e0eSKevin Wolf static void lba_to_msf(uint8_t *buf, int lba) 7233231e0eSKevin Wolf { 7333231e0eSKevin Wolf lba += 150; 7433231e0eSKevin Wolf buf[0] = (lba / 75) / 60; 7533231e0eSKevin Wolf buf[1] = (lba / 75) % 60; 7633231e0eSKevin Wolf buf[2] = lba % 75; 7733231e0eSKevin Wolf } 7833231e0eSKevin Wolf 7933231e0eSKevin Wolf static inline int media_present(IDEState *s) 8033231e0eSKevin Wolf { 81a1aff5bfSMarkus Armbruster return !s->tray_open && s->nb_sectors > 0; 8233231e0eSKevin Wolf } 8333231e0eSKevin Wolf 84a7acf552SAmit Shah /* XXX: DVDs that could fit on a CD will be reported as a CD */ 8533231e0eSKevin Wolf static inline int media_is_dvd(IDEState *s) 8633231e0eSKevin Wolf { 8733231e0eSKevin Wolf return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS); 8833231e0eSKevin Wolf } 8933231e0eSKevin Wolf 9033231e0eSKevin Wolf static inline int media_is_cd(IDEState *s) 9133231e0eSKevin Wolf { 9233231e0eSKevin Wolf return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS); 9333231e0eSKevin Wolf } 9433231e0eSKevin Wolf 9533231e0eSKevin Wolf static void cd_data_to_raw(uint8_t *buf, int lba) 9633231e0eSKevin Wolf { 9733231e0eSKevin Wolf /* sync bytes */ 9833231e0eSKevin Wolf buf[0] = 0x00; 9933231e0eSKevin Wolf memset(buf + 1, 0xff, 10); 10033231e0eSKevin Wolf buf[11] = 0x00; 10133231e0eSKevin Wolf buf += 12; 10233231e0eSKevin Wolf /* MSF */ 10333231e0eSKevin Wolf lba_to_msf(buf, lba); 10433231e0eSKevin Wolf buf[3] = 0x01; /* mode 1 data */ 10533231e0eSKevin Wolf buf += 4; 10633231e0eSKevin Wolf /* data */ 10733231e0eSKevin Wolf buf += 2048; 10833231e0eSKevin Wolf /* XXX: ECC not computed */ 10933231e0eSKevin Wolf memset(buf, 0, 288); 11033231e0eSKevin Wolf } 11133231e0eSKevin Wolf 1125f81724dSPeter Lieven static int 1135f81724dSPeter Lieven cd_read_sector_sync(IDEState *s) 11433231e0eSKevin Wolf { 11533231e0eSKevin Wolf int ret; 116ece2d05eSAlberto Garcia block_acct_start(blk_get_stats(s->blk), &s->acct, 11726a122d3SEric Blake ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ); 11833231e0eSKevin Wolf 1195f81724dSPeter Lieven #ifdef DEBUG_IDE_ATAPI 1205f81724dSPeter Lieven printf("cd_read_sector_sync: lba=%d\n", s->lba); 1215f81724dSPeter Lieven #endif 1225f81724dSPeter Lieven 1235f81724dSPeter Lieven switch (s->cd_sector_size) { 12433231e0eSKevin Wolf case 2048: 12526a122d3SEric Blake ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS, 12626a122d3SEric Blake s->io_buffer, ATAPI_SECTOR_SIZE); 12733231e0eSKevin Wolf break; 12833231e0eSKevin Wolf case 2352: 12926a122d3SEric Blake ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS, 13026a122d3SEric Blake s->io_buffer + 16, ATAPI_SECTOR_SIZE); 131ece2d05eSAlberto Garcia if (ret >= 0) { 1325f81724dSPeter Lieven cd_data_to_raw(s->io_buffer, s->lba); 133ece2d05eSAlberto Garcia } 13433231e0eSKevin Wolf break; 13533231e0eSKevin Wolf default: 136ece2d05eSAlberto Garcia block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ); 137ece2d05eSAlberto Garcia return -EIO; 13833231e0eSKevin Wolf } 139ece2d05eSAlberto Garcia 140ece2d05eSAlberto Garcia if (ret < 0) { 141ece2d05eSAlberto Garcia block_acct_failed(blk_get_stats(s->blk), &s->acct); 142ece2d05eSAlberto Garcia } else { 143ece2d05eSAlberto Garcia block_acct_done(blk_get_stats(s->blk), &s->acct); 1445f81724dSPeter Lieven s->lba++; 1455f81724dSPeter Lieven s->io_buffer_index = 0; 146ece2d05eSAlberto Garcia } 147ece2d05eSAlberto Garcia 14833231e0eSKevin Wolf return ret; 14933231e0eSKevin Wolf } 15033231e0eSKevin Wolf 1515f81724dSPeter Lieven static void cd_read_sector_cb(void *opaque, int ret) 1525f81724dSPeter Lieven { 1535f81724dSPeter Lieven IDEState *s = opaque; 1545f81724dSPeter Lieven 1555f81724dSPeter Lieven #ifdef DEBUG_IDE_ATAPI 1565f81724dSPeter Lieven printf("cd_read_sector_cb: lba=%d ret=%d\n", s->lba, ret); 1575f81724dSPeter Lieven #endif 1585f81724dSPeter Lieven 1595f81724dSPeter Lieven if (ret < 0) { 16036be0929SAlberto Garcia block_acct_failed(blk_get_stats(s->blk), &s->acct); 1615f81724dSPeter Lieven ide_atapi_io_error(s, ret); 1625f81724dSPeter Lieven return; 1635f81724dSPeter Lieven } 1645f81724dSPeter Lieven 16536be0929SAlberto Garcia block_acct_done(blk_get_stats(s->blk), &s->acct); 16636be0929SAlberto Garcia 1675f81724dSPeter Lieven if (s->cd_sector_size == 2352) { 1685f81724dSPeter Lieven cd_data_to_raw(s->io_buffer, s->lba); 1695f81724dSPeter Lieven } 1705f81724dSPeter Lieven 1715f81724dSPeter Lieven s->lba++; 1725f81724dSPeter Lieven s->io_buffer_index = 0; 1735f81724dSPeter Lieven s->status &= ~BUSY_STAT; 1745f81724dSPeter Lieven 1755f81724dSPeter Lieven ide_atapi_cmd_reply_end(s); 1765f81724dSPeter Lieven } 1775f81724dSPeter Lieven 1785f81724dSPeter Lieven static int cd_read_sector(IDEState *s) 1795f81724dSPeter Lieven { 1805f81724dSPeter Lieven if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) { 18136be0929SAlberto Garcia block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ); 1825f81724dSPeter Lieven return -EINVAL; 1835f81724dSPeter Lieven } 1845f81724dSPeter Lieven 1855f81724dSPeter Lieven s->iov.iov_base = (s->cd_sector_size == 2352) ? 1865f81724dSPeter Lieven s->io_buffer + 16 : s->io_buffer; 1875f81724dSPeter Lieven 18826a122d3SEric Blake s->iov.iov_len = ATAPI_SECTOR_SIZE; 1895f81724dSPeter Lieven qemu_iovec_init_external(&s->qiov, &s->iov, 1); 1905f81724dSPeter Lieven 1915f81724dSPeter Lieven #ifdef DEBUG_IDE_ATAPI 1925f81724dSPeter Lieven printf("cd_read_sector: lba=%d\n", s->lba); 1935f81724dSPeter Lieven #endif 1945f81724dSPeter Lieven 1955f81724dSPeter Lieven block_acct_start(blk_get_stats(s->blk), &s->acct, 19626a122d3SEric Blake ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ); 1975f81724dSPeter Lieven 19802506b20SPeter Lieven ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4, 1995f81724dSPeter Lieven cd_read_sector_cb, s); 2005f81724dSPeter Lieven 2015f81724dSPeter Lieven s->status |= BUSY_STAT; 2025f81724dSPeter Lieven return 0; 2035f81724dSPeter Lieven } 2045f81724dSPeter Lieven 20533231e0eSKevin Wolf void ide_atapi_cmd_ok(IDEState *s) 20633231e0eSKevin Wolf { 20733231e0eSKevin Wolf s->error = 0; 20833231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 20933231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 210d735b620SJohn Snow ide_transfer_stop(s); 21133231e0eSKevin Wolf ide_set_irq(s->bus); 21233231e0eSKevin Wolf } 21333231e0eSKevin Wolf 21433231e0eSKevin Wolf void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc) 21533231e0eSKevin Wolf { 21633231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 21733231e0eSKevin Wolf printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc); 21833231e0eSKevin Wolf #endif 21933231e0eSKevin Wolf s->error = sense_key << 4; 22033231e0eSKevin Wolf s->status = READY_STAT | ERR_STAT; 22133231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 22233231e0eSKevin Wolf s->sense_key = sense_key; 22333231e0eSKevin Wolf s->asc = asc; 224d735b620SJohn Snow ide_transfer_stop(s); 22533231e0eSKevin Wolf ide_set_irq(s->bus); 22633231e0eSKevin Wolf } 22733231e0eSKevin Wolf 22833231e0eSKevin Wolf void ide_atapi_io_error(IDEState *s, int ret) 22933231e0eSKevin Wolf { 23033231e0eSKevin Wolf /* XXX: handle more errors */ 23133231e0eSKevin Wolf if (ret == -ENOMEDIUM) { 23267cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, NOT_READY, 23333231e0eSKevin Wolf ASC_MEDIUM_NOT_PRESENT); 23433231e0eSKevin Wolf } else { 23567cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 23633231e0eSKevin Wolf ASC_LOGICAL_BLOCK_OOR); 23733231e0eSKevin Wolf } 23833231e0eSKevin Wolf } 23933231e0eSKevin Wolf 240af0e00dbSJohn Snow static uint16_t atapi_byte_count_limit(IDEState *s) 241af0e00dbSJohn Snow { 242af0e00dbSJohn Snow uint16_t bcl; 243af0e00dbSJohn Snow 244af0e00dbSJohn Snow bcl = s->lcyl | (s->hcyl << 8); 245af0e00dbSJohn Snow if (bcl == 0xffff) { 246af0e00dbSJohn Snow return 0xfffe; 247af0e00dbSJohn Snow } 248af0e00dbSJohn Snow return bcl; 249af0e00dbSJohn Snow } 250af0e00dbSJohn Snow 25133231e0eSKevin Wolf /* The whole ATAPI transfer logic is handled in this function */ 25233231e0eSKevin Wolf void ide_atapi_cmd_reply_end(IDEState *s) 25333231e0eSKevin Wolf { 25433231e0eSKevin Wolf int byte_count_limit, size, ret; 25533231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 25633231e0eSKevin Wolf printf("reply: tx_size=%d elem_tx_size=%d index=%d\n", 25733231e0eSKevin Wolf s->packet_transfer_size, 25833231e0eSKevin Wolf s->elementary_transfer_size, 25933231e0eSKevin Wolf s->io_buffer_index); 26033231e0eSKevin Wolf #endif 26133231e0eSKevin Wolf if (s->packet_transfer_size <= 0) { 26233231e0eSKevin Wolf /* end of transfer */ 263d735b620SJohn Snow ide_atapi_cmd_ok(s); 26433231e0eSKevin Wolf ide_set_irq(s->bus); 26533231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 2665f81724dSPeter Lieven printf("end of transfer, status=0x%x\n", s->status); 26733231e0eSKevin Wolf #endif 26833231e0eSKevin Wolf } else { 26933231e0eSKevin Wolf /* see if a new sector must be read */ 27033231e0eSKevin Wolf if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) { 2715f81724dSPeter Lieven if (!s->elementary_transfer_size) { 2725f81724dSPeter Lieven ret = cd_read_sector(s); 2735f81724dSPeter Lieven if (ret < 0) { 2745f81724dSPeter Lieven ide_atapi_io_error(s, ret); 2755f81724dSPeter Lieven } 2765f81724dSPeter Lieven return; 2775f81724dSPeter Lieven } else { 2785f81724dSPeter Lieven /* rebuffering within an elementary transfer is 2795f81724dSPeter Lieven * only possible with a sync request because we 2805f81724dSPeter Lieven * end up with a race condition otherwise */ 2815f81724dSPeter Lieven ret = cd_read_sector_sync(s); 28233231e0eSKevin Wolf if (ret < 0) { 28333231e0eSKevin Wolf ide_atapi_io_error(s, ret); 28433231e0eSKevin Wolf return; 28533231e0eSKevin Wolf } 2865f81724dSPeter Lieven } 28733231e0eSKevin Wolf } 28833231e0eSKevin Wolf if (s->elementary_transfer_size > 0) { 28933231e0eSKevin Wolf /* there are some data left to transmit in this elementary 29033231e0eSKevin Wolf transfer */ 29133231e0eSKevin Wolf size = s->cd_sector_size - s->io_buffer_index; 29233231e0eSKevin Wolf if (size > s->elementary_transfer_size) 29333231e0eSKevin Wolf size = s->elementary_transfer_size; 29433231e0eSKevin Wolf s->packet_transfer_size -= size; 29533231e0eSKevin Wolf s->elementary_transfer_size -= size; 29633231e0eSKevin Wolf s->io_buffer_index += size; 29733231e0eSKevin Wolf ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size, 29833231e0eSKevin Wolf size, ide_atapi_cmd_reply_end); 29933231e0eSKevin Wolf } else { 30033231e0eSKevin Wolf /* a new transfer is needed */ 30133231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO; 302af0e00dbSJohn Snow byte_count_limit = atapi_byte_count_limit(s); 30333231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 30433231e0eSKevin Wolf printf("byte_count_limit=%d\n", byte_count_limit); 30533231e0eSKevin Wolf #endif 30633231e0eSKevin Wolf size = s->packet_transfer_size; 30733231e0eSKevin Wolf if (size > byte_count_limit) { 30833231e0eSKevin Wolf /* byte count limit must be even if this case */ 30933231e0eSKevin Wolf if (byte_count_limit & 1) 31033231e0eSKevin Wolf byte_count_limit--; 31133231e0eSKevin Wolf size = byte_count_limit; 31233231e0eSKevin Wolf } 31333231e0eSKevin Wolf s->lcyl = size; 31433231e0eSKevin Wolf s->hcyl = size >> 8; 31533231e0eSKevin Wolf s->elementary_transfer_size = size; 31633231e0eSKevin Wolf /* we cannot transmit more than one sector at a time */ 31733231e0eSKevin Wolf if (s->lba != -1) { 31833231e0eSKevin Wolf if (size > (s->cd_sector_size - s->io_buffer_index)) 31933231e0eSKevin Wolf size = (s->cd_sector_size - s->io_buffer_index); 32033231e0eSKevin Wolf } 32133231e0eSKevin Wolf s->packet_transfer_size -= size; 32233231e0eSKevin Wolf s->elementary_transfer_size -= size; 32333231e0eSKevin Wolf s->io_buffer_index += size; 32433231e0eSKevin Wolf ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size, 32533231e0eSKevin Wolf size, ide_atapi_cmd_reply_end); 32633231e0eSKevin Wolf ide_set_irq(s->bus); 32733231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 32833231e0eSKevin Wolf printf("status=0x%x\n", s->status); 32933231e0eSKevin Wolf #endif 33033231e0eSKevin Wolf } 33133231e0eSKevin Wolf } 33233231e0eSKevin Wolf } 33333231e0eSKevin Wolf 33433231e0eSKevin Wolf /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */ 33533231e0eSKevin Wolf static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size) 33633231e0eSKevin Wolf { 33733231e0eSKevin Wolf if (size > max_size) 33833231e0eSKevin Wolf size = max_size; 33933231e0eSKevin Wolf s->lba = -1; /* no sector read */ 34033231e0eSKevin Wolf s->packet_transfer_size = size; 34133231e0eSKevin Wolf s->io_buffer_size = size; /* dma: send the reply data as one chunk */ 34233231e0eSKevin Wolf s->elementary_transfer_size = 0; 34333231e0eSKevin Wolf 34433231e0eSKevin Wolf if (s->atapi_dma) { 3454be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, size, 3465366d0c8SBenoît Canet BLOCK_ACCT_READ); 34733231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT | DRQ_STAT; 3484855b576SPaolo Bonzini ide_start_dma(s, ide_atapi_cmd_read_dma_cb); 34933231e0eSKevin Wolf } else { 35033231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 351c71c06d4SPaolo Bonzini s->io_buffer_index = 0; 35233231e0eSKevin Wolf ide_atapi_cmd_reply_end(s); 35333231e0eSKevin Wolf } 35433231e0eSKevin Wolf } 35533231e0eSKevin Wolf 35633231e0eSKevin Wolf /* start a CD-CDROM read command */ 35733231e0eSKevin Wolf static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors, 35833231e0eSKevin Wolf int sector_size) 35933231e0eSKevin Wolf { 36033231e0eSKevin Wolf s->lba = lba; 36133231e0eSKevin Wolf s->packet_transfer_size = nb_sectors * sector_size; 36233231e0eSKevin Wolf s->elementary_transfer_size = 0; 36333231e0eSKevin Wolf s->io_buffer_index = sector_size; 36433231e0eSKevin Wolf s->cd_sector_size = sector_size; 36533231e0eSKevin Wolf 36633231e0eSKevin Wolf ide_atapi_cmd_reply_end(s); 36733231e0eSKevin Wolf } 36833231e0eSKevin Wolf 36933231e0eSKevin Wolf static void ide_atapi_cmd_check_status(IDEState *s) 37033231e0eSKevin Wolf { 37133231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 37233231e0eSKevin Wolf printf("atapi_cmd_check_status\n"); 37333231e0eSKevin Wolf #endif 37467cc61e4SPaolo Bonzini s->error = MC_ERR | (UNIT_ATTENTION << 4); 37533231e0eSKevin Wolf s->status = ERR_STAT; 37633231e0eSKevin Wolf s->nsector = 0; 37733231e0eSKevin Wolf ide_set_irq(s->bus); 37833231e0eSKevin Wolf } 37933231e0eSKevin Wolf /* ATAPI DMA support */ 38033231e0eSKevin Wolf 38133231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret) 38233231e0eSKevin Wolf { 38333231e0eSKevin Wolf IDEState *s = opaque; 38433231e0eSKevin Wolf int data_offset, n; 38533231e0eSKevin Wolf 38633231e0eSKevin Wolf if (ret < 0) { 387502356eeSPavel Butsykin if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) { 388502356eeSPavel Butsykin if (s->bus->error_status) { 3897f951b2dSJohn Snow s->bus->dma->aiocb = NULL; 390502356eeSPavel Butsykin return; 391502356eeSPavel Butsykin } 39233231e0eSKevin Wolf goto eot; 39333231e0eSKevin Wolf } 394502356eeSPavel Butsykin } 39533231e0eSKevin Wolf 39633231e0eSKevin Wolf if (s->io_buffer_size > 0) { 39733231e0eSKevin Wolf /* 39833231e0eSKevin Wolf * For a cdrom read sector command (s->lba != -1), 39933231e0eSKevin Wolf * adjust the lba for the next s->io_buffer_size chunk 40033231e0eSKevin Wolf * and dma the current chunk. 40133231e0eSKevin Wolf * For a command != read (s->lba == -1), just transfer 40233231e0eSKevin Wolf * the reply data. 40333231e0eSKevin Wolf */ 40433231e0eSKevin Wolf if (s->lba != -1) { 40533231e0eSKevin Wolf if (s->cd_sector_size == 2352) { 40633231e0eSKevin Wolf n = 1; 40733231e0eSKevin Wolf cd_data_to_raw(s->io_buffer, s->lba); 40833231e0eSKevin Wolf } else { 40933231e0eSKevin Wolf n = s->io_buffer_size >> 11; 41033231e0eSKevin Wolf } 41133231e0eSKevin Wolf s->lba += n; 41233231e0eSKevin Wolf } 41333231e0eSKevin Wolf s->packet_transfer_size -= s->io_buffer_size; 41433231e0eSKevin Wolf if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0) 41533231e0eSKevin Wolf goto eot; 41633231e0eSKevin Wolf } 41733231e0eSKevin Wolf 41833231e0eSKevin Wolf if (s->packet_transfer_size <= 0) { 41933231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 42033231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 42133231e0eSKevin Wolf ide_set_irq(s->bus); 422a597e79cSChristoph Hellwig goto eot; 42333231e0eSKevin Wolf } 42433231e0eSKevin Wolf 42533231e0eSKevin Wolf s->io_buffer_index = 0; 42633231e0eSKevin Wolf if (s->cd_sector_size == 2352) { 42733231e0eSKevin Wolf n = 1; 42833231e0eSKevin Wolf s->io_buffer_size = s->cd_sector_size; 42933231e0eSKevin Wolf data_offset = 16; 43033231e0eSKevin Wolf } else { 43133231e0eSKevin Wolf n = s->packet_transfer_size >> 11; 43233231e0eSKevin Wolf if (n > (IDE_DMA_BUF_SECTORS / 4)) 43333231e0eSKevin Wolf n = (IDE_DMA_BUF_SECTORS / 4); 43433231e0eSKevin Wolf s->io_buffer_size = n * 2048; 43533231e0eSKevin Wolf data_offset = 0; 43633231e0eSKevin Wolf } 43733231e0eSKevin Wolf #ifdef DEBUG_AIO 43833231e0eSKevin Wolf printf("aio_read_cd: lba=%u n=%d\n", s->lba, n); 43933231e0eSKevin Wolf #endif 440a597e79cSChristoph Hellwig 44133231e0eSKevin Wolf s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset); 44226a122d3SEric Blake s->bus->dma->iov.iov_len = n * ATAPI_SECTOR_SIZE; 44333231e0eSKevin Wolf qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1); 444a597e79cSChristoph Hellwig 44502506b20SPeter Lieven s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2, 44633231e0eSKevin Wolf &s->bus->dma->qiov, n * 4, 44733231e0eSKevin Wolf ide_atapi_cmd_read_dma_cb, s); 448a597e79cSChristoph Hellwig return; 449ad54ae80SPaolo Bonzini 450a597e79cSChristoph Hellwig eot: 451ece2d05eSAlberto Garcia if (ret < 0) { 452ece2d05eSAlberto Garcia block_acct_failed(blk_get_stats(s->blk), &s->acct); 453ece2d05eSAlberto Garcia } else { 4544be74634SMarkus Armbruster block_acct_done(blk_get_stats(s->blk), &s->acct); 455ece2d05eSAlberto Garcia } 4560e7ce54cSPaolo Bonzini ide_set_inactive(s, false); 45733231e0eSKevin Wolf } 45833231e0eSKevin Wolf 45933231e0eSKevin Wolf /* start a CD-CDROM read command with DMA */ 46033231e0eSKevin Wolf /* XXX: test if DMA is available */ 46133231e0eSKevin Wolf static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors, 46233231e0eSKevin Wolf int sector_size) 46333231e0eSKevin Wolf { 46433231e0eSKevin Wolf s->lba = lba; 46533231e0eSKevin Wolf s->packet_transfer_size = nb_sectors * sector_size; 46633231e0eSKevin Wolf s->io_buffer_size = 0; 46733231e0eSKevin Wolf s->cd_sector_size = sector_size; 46833231e0eSKevin Wolf 4694be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size, 4705366d0c8SBenoît Canet BLOCK_ACCT_READ); 471a597e79cSChristoph Hellwig 47233231e0eSKevin Wolf /* XXX: check if BUSY_STAT should be set */ 47333231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT; 4744855b576SPaolo Bonzini ide_start_dma(s, ide_atapi_cmd_read_dma_cb); 47533231e0eSKevin Wolf } 47633231e0eSKevin Wolf 47733231e0eSKevin Wolf static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors, 47833231e0eSKevin Wolf int sector_size) 47933231e0eSKevin Wolf { 48033231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 48133231e0eSKevin Wolf printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio", 48233231e0eSKevin Wolf lba, nb_sectors); 48333231e0eSKevin Wolf #endif 48433231e0eSKevin Wolf if (s->atapi_dma) { 48533231e0eSKevin Wolf ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size); 48633231e0eSKevin Wolf } else { 48733231e0eSKevin Wolf ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size); 48833231e0eSKevin Wolf } 48933231e0eSKevin Wolf } 49033231e0eSKevin Wolf 491a71754e5SDr. David Alan Gilbert void ide_atapi_dma_restart(IDEState *s) 492a71754e5SDr. David Alan Gilbert { 493a71754e5SDr. David Alan Gilbert /* 4949a41826fSPavel Butsykin * At this point we can just re-evaluate the packet command and start over. 4959a41826fSPavel Butsykin * The presence of ->dma_cb callback in the pre_save ensures that the packet 4969a41826fSPavel Butsykin * command has been completely sent and we can safely restart command. 497a71754e5SDr. David Alan Gilbert */ 4989a41826fSPavel Butsykin s->unit = s->bus->retry_unit; 4999a41826fSPavel Butsykin s->bus->dma->ops->restart_dma(s->bus->dma); 5009a41826fSPavel Butsykin ide_atapi_cmd(s); 501a71754e5SDr. David Alan Gilbert } 502a71754e5SDr. David Alan Gilbert 50333231e0eSKevin Wolf static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index, 50433231e0eSKevin Wolf uint16_t profile) 50533231e0eSKevin Wolf { 50633231e0eSKevin Wolf uint8_t *buf_profile = buf + 12; /* start of profiles */ 50733231e0eSKevin Wolf 50833231e0eSKevin Wolf buf_profile += ((*index) * 4); /* start of indexed profile */ 50933231e0eSKevin Wolf cpu_to_ube16 (buf_profile, profile); 51033231e0eSKevin Wolf buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7])); 51133231e0eSKevin Wolf 51233231e0eSKevin Wolf /* each profile adds 4 bytes to the response */ 51333231e0eSKevin Wolf (*index)++; 51433231e0eSKevin Wolf buf[11] += 4; /* Additional Length */ 51533231e0eSKevin Wolf 51633231e0eSKevin Wolf return 4; 51733231e0eSKevin Wolf } 51833231e0eSKevin Wolf 51933231e0eSKevin Wolf static int ide_dvd_read_structure(IDEState *s, int format, 52033231e0eSKevin Wolf const uint8_t *packet, uint8_t *buf) 52133231e0eSKevin Wolf { 52233231e0eSKevin Wolf switch (format) { 52333231e0eSKevin Wolf case 0x0: /* Physical format information */ 52433231e0eSKevin Wolf { 52533231e0eSKevin Wolf int layer = packet[6]; 52633231e0eSKevin Wolf uint64_t total_sectors; 52733231e0eSKevin Wolf 52833231e0eSKevin Wolf if (layer != 0) 52933231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 53033231e0eSKevin Wolf 531e119bcacSKevin Wolf total_sectors = s->nb_sectors >> 2; 532e119bcacSKevin Wolf if (total_sectors == 0) { 53333231e0eSKevin Wolf return -ASC_MEDIUM_NOT_PRESENT; 534e119bcacSKevin Wolf } 53533231e0eSKevin Wolf 53633231e0eSKevin Wolf buf[4] = 1; /* DVD-ROM, part version 1 */ 53733231e0eSKevin Wolf buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */ 53833231e0eSKevin Wolf buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */ 53933231e0eSKevin Wolf buf[7] = 0; /* default densities */ 54033231e0eSKevin Wolf 54133231e0eSKevin Wolf /* FIXME: 0x30000 per spec? */ 54233231e0eSKevin Wolf cpu_to_ube32(buf + 8, 0); /* start sector */ 54333231e0eSKevin Wolf cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */ 54433231e0eSKevin Wolf cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */ 54533231e0eSKevin Wolf 54633231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 547d8ee2591SPeter Maydell stw_be_p(buf, 2048 + 2); 54833231e0eSKevin Wolf 54933231e0eSKevin Wolf /* 2k data + 4 byte header */ 55033231e0eSKevin Wolf return (2048 + 4); 55133231e0eSKevin Wolf } 55233231e0eSKevin Wolf 55333231e0eSKevin Wolf case 0x01: /* DVD copyright information */ 55433231e0eSKevin Wolf buf[4] = 0; /* no copyright data */ 55533231e0eSKevin Wolf buf[5] = 0; /* no region restrictions */ 55633231e0eSKevin Wolf 55733231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 558d8ee2591SPeter Maydell stw_be_p(buf, 4 + 2); 55933231e0eSKevin Wolf 56033231e0eSKevin Wolf /* 4 byte header + 4 byte data */ 56133231e0eSKevin Wolf return (4 + 4); 56233231e0eSKevin Wolf 56333231e0eSKevin Wolf case 0x03: /* BCA information - invalid field for no BCA info */ 56433231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 56533231e0eSKevin Wolf 56633231e0eSKevin Wolf case 0x04: /* DVD disc manufacturing information */ 56733231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 568d8ee2591SPeter Maydell stw_be_p(buf, 2048 + 2); 56933231e0eSKevin Wolf 57033231e0eSKevin Wolf /* 2k data + 4 byte header */ 57133231e0eSKevin Wolf return (2048 + 4); 57233231e0eSKevin Wolf 57333231e0eSKevin Wolf case 0xff: 57433231e0eSKevin Wolf /* 57533231e0eSKevin Wolf * This lists all the command capabilities above. Add new ones 57633231e0eSKevin Wolf * in order and update the length and buffer return values. 57733231e0eSKevin Wolf */ 57833231e0eSKevin Wolf 57933231e0eSKevin Wolf buf[4] = 0x00; /* Physical format */ 58033231e0eSKevin Wolf buf[5] = 0x40; /* Not writable, is readable */ 581d8ee2591SPeter Maydell stw_be_p(buf + 6, 2048 + 4); 58233231e0eSKevin Wolf 58333231e0eSKevin Wolf buf[8] = 0x01; /* Copyright info */ 58433231e0eSKevin Wolf buf[9] = 0x40; /* Not writable, is readable */ 585d8ee2591SPeter Maydell stw_be_p(buf + 10, 4 + 4); 58633231e0eSKevin Wolf 58733231e0eSKevin Wolf buf[12] = 0x03; /* BCA info */ 58833231e0eSKevin Wolf buf[13] = 0x40; /* Not writable, is readable */ 589d8ee2591SPeter Maydell stw_be_p(buf + 14, 188 + 4); 59033231e0eSKevin Wolf 59133231e0eSKevin Wolf buf[16] = 0x04; /* Manufacturing info */ 59233231e0eSKevin Wolf buf[17] = 0x40; /* Not writable, is readable */ 593d8ee2591SPeter Maydell stw_be_p(buf + 18, 2048 + 4); 59433231e0eSKevin Wolf 59533231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 596d8ee2591SPeter Maydell stw_be_p(buf, 16 + 2); 59733231e0eSKevin Wolf 59833231e0eSKevin Wolf /* data written + 4 byte header */ 59933231e0eSKevin Wolf return (16 + 4); 60033231e0eSKevin Wolf 60133231e0eSKevin Wolf default: /* TODO: formats beyond DVD-ROM requires */ 60233231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 60333231e0eSKevin Wolf } 60433231e0eSKevin Wolf } 60533231e0eSKevin Wolf 60633231e0eSKevin Wolf static unsigned int event_status_media(IDEState *s, 60733231e0eSKevin Wolf uint8_t *buf) 60833231e0eSKevin Wolf { 60933231e0eSKevin Wolf uint8_t event_code, media_status; 61033231e0eSKevin Wolf 61133231e0eSKevin Wolf media_status = 0; 612dd063333SMarkus Armbruster if (s->tray_open) { 61333231e0eSKevin Wolf media_status = MS_TRAY_OPEN; 6144be74634SMarkus Armbruster } else if (blk_is_inserted(s->blk)) { 61533231e0eSKevin Wolf media_status = MS_MEDIA_PRESENT; 61633231e0eSKevin Wolf } 61733231e0eSKevin Wolf 61833231e0eSKevin Wolf /* Event notification descriptor */ 61933231e0eSKevin Wolf event_code = MEC_NO_CHANGE; 6202df0a3a3SPaolo Bonzini if (media_status != MS_TRAY_OPEN) { 6212df0a3a3SPaolo Bonzini if (s->events.new_media) { 62233231e0eSKevin Wolf event_code = MEC_NEW_MEDIA; 62333231e0eSKevin Wolf s->events.new_media = false; 6242df0a3a3SPaolo Bonzini } else if (s->events.eject_request) { 6252df0a3a3SPaolo Bonzini event_code = MEC_EJECT_REQUESTED; 6262df0a3a3SPaolo Bonzini s->events.eject_request = false; 6272df0a3a3SPaolo Bonzini } 62833231e0eSKevin Wolf } 62933231e0eSKevin Wolf 63033231e0eSKevin Wolf buf[4] = event_code; 63133231e0eSKevin Wolf buf[5] = media_status; 63233231e0eSKevin Wolf 63333231e0eSKevin Wolf /* These fields are reserved, just clear them. */ 63433231e0eSKevin Wolf buf[6] = 0; 63533231e0eSKevin Wolf buf[7] = 0; 63633231e0eSKevin Wolf 63733231e0eSKevin Wolf return 8; /* We wrote to 4 extra bytes from the header */ 63833231e0eSKevin Wolf } 63933231e0eSKevin Wolf 640*e7bd708eSJohn Snow /* 641*e7bd708eSJohn Snow * Before transferring data or otherwise signalling acceptance of a command 642*e7bd708eSJohn Snow * marked CONDDATA, we must check the validity of the byte_count_limit. 643*e7bd708eSJohn Snow */ 644*e7bd708eSJohn Snow static bool validate_bcl(IDEState *s) 645*e7bd708eSJohn Snow { 646*e7bd708eSJohn Snow /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */ 647*e7bd708eSJohn Snow if (s->atapi_dma || atapi_byte_count_limit(s)) { 648*e7bd708eSJohn Snow return true; 649*e7bd708eSJohn Snow } 650*e7bd708eSJohn Snow 651*e7bd708eSJohn Snow /* TODO: Move abort back into core.c and introduce proper error flow between 652*e7bd708eSJohn Snow * ATAPI layer and IDE core layer */ 653*e7bd708eSJohn Snow ide_abort_command(s); 654*e7bd708eSJohn Snow return false; 655*e7bd708eSJohn Snow } 656*e7bd708eSJohn Snow 657e1a064f9SKevin Wolf static void cmd_get_event_status_notification(IDEState *s, 658e1a064f9SKevin Wolf uint8_t *buf) 65933231e0eSKevin Wolf { 660e1a064f9SKevin Wolf const uint8_t *packet = buf; 661e1a064f9SKevin Wolf 66233231e0eSKevin Wolf struct { 66333231e0eSKevin Wolf uint8_t opcode; 66433231e0eSKevin Wolf uint8_t polled; /* lsb bit is polled; others are reserved */ 66533231e0eSKevin Wolf uint8_t reserved2[2]; 66633231e0eSKevin Wolf uint8_t class; 66733231e0eSKevin Wolf uint8_t reserved3[2]; 66833231e0eSKevin Wolf uint16_t len; 66933231e0eSKevin Wolf uint8_t control; 670541dc0d4SStefan Weil } QEMU_PACKED *gesn_cdb; 67133231e0eSKevin Wolf 67233231e0eSKevin Wolf struct { 67333231e0eSKevin Wolf uint16_t len; 67433231e0eSKevin Wolf uint8_t notification_class; 67533231e0eSKevin Wolf uint8_t supported_events; 676541dc0d4SStefan Weil } QEMU_PACKED *gesn_event_header; 67733231e0eSKevin Wolf unsigned int max_len, used_len; 67833231e0eSKevin Wolf 67933231e0eSKevin Wolf gesn_cdb = (void *)packet; 68033231e0eSKevin Wolf gesn_event_header = (void *)buf; 68133231e0eSKevin Wolf 68233231e0eSKevin Wolf max_len = be16_to_cpu(gesn_cdb->len); 68333231e0eSKevin Wolf 68433231e0eSKevin Wolf /* It is fine by the MMC spec to not support async mode operations */ 68533231e0eSKevin Wolf if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */ 68633231e0eSKevin Wolf /* Only polling is supported, asynchronous mode is not. */ 68767cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 68833231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 68933231e0eSKevin Wolf return; 69033231e0eSKevin Wolf } 69133231e0eSKevin Wolf 69233231e0eSKevin Wolf /* polling mode operation */ 69333231e0eSKevin Wolf 69433231e0eSKevin Wolf /* 69533231e0eSKevin Wolf * These are the supported events. 69633231e0eSKevin Wolf * 69733231e0eSKevin Wolf * We currently only support requests of the 'media' type. 698f0f992e6SPaolo Bonzini * Notification class requests and supported event classes are bitmasks, 699f0f992e6SPaolo Bonzini * but they are build from the same values as the "notification class" 700f0f992e6SPaolo Bonzini * field. 70133231e0eSKevin Wolf */ 702f0f992e6SPaolo Bonzini gesn_event_header->supported_events = 1 << GESN_MEDIA; 70333231e0eSKevin Wolf 70433231e0eSKevin Wolf /* 70533231e0eSKevin Wolf * We use |= below to set the class field; other bits in this byte 70633231e0eSKevin Wolf * are reserved now but this is useful to do if we have to use the 70733231e0eSKevin Wolf * reserved fields later. 70833231e0eSKevin Wolf */ 70933231e0eSKevin Wolf gesn_event_header->notification_class = 0; 71033231e0eSKevin Wolf 71133231e0eSKevin Wolf /* 71233231e0eSKevin Wolf * Responses to requests are to be based on request priority. The 71333231e0eSKevin Wolf * notification_class_request_type enum above specifies the 71433231e0eSKevin Wolf * priority: upper elements are higher prio than lower ones. 71533231e0eSKevin Wolf */ 716f0f992e6SPaolo Bonzini if (gesn_cdb->class & (1 << GESN_MEDIA)) { 717f0f992e6SPaolo Bonzini gesn_event_header->notification_class |= GESN_MEDIA; 71833231e0eSKevin Wolf used_len = event_status_media(s, buf); 71933231e0eSKevin Wolf } else { 72033231e0eSKevin Wolf gesn_event_header->notification_class = 0x80; /* No event available */ 72133231e0eSKevin Wolf used_len = sizeof(*gesn_event_header); 72233231e0eSKevin Wolf } 72333231e0eSKevin Wolf gesn_event_header->len = cpu_to_be16(used_len 72433231e0eSKevin Wolf - sizeof(*gesn_event_header)); 72533231e0eSKevin Wolf ide_atapi_cmd_reply(s, used_len, max_len); 72633231e0eSKevin Wolf } 72733231e0eSKevin Wolf 728a60cf7e7SKevin Wolf static void cmd_request_sense(IDEState *s, uint8_t *buf) 72933231e0eSKevin Wolf { 730a60cf7e7SKevin Wolf int max_len = buf[4]; 731a60cf7e7SKevin Wolf 732a60cf7e7SKevin Wolf memset(buf, 0, 18); 733a60cf7e7SKevin Wolf buf[0] = 0x70 | (1 << 7); 734a60cf7e7SKevin Wolf buf[2] = s->sense_key; 735a60cf7e7SKevin Wolf buf[7] = 10; 736a60cf7e7SKevin Wolf buf[12] = s->asc; 737a60cf7e7SKevin Wolf 73867cc61e4SPaolo Bonzini if (s->sense_key == UNIT_ATTENTION) { 73967cc61e4SPaolo Bonzini s->sense_key = NO_SENSE; 740a60cf7e7SKevin Wolf } 741a60cf7e7SKevin Wolf 742a60cf7e7SKevin Wolf ide_atapi_cmd_reply(s, 18, max_len); 743a60cf7e7SKevin Wolf } 744a60cf7e7SKevin Wolf 745a60cf7e7SKevin Wolf static void cmd_inquiry(IDEState *s, uint8_t *buf) 746a60cf7e7SKevin Wolf { 7479a502563SJohn Snow uint8_t page_code = buf[2]; 748a60cf7e7SKevin Wolf int max_len = buf[4]; 749a60cf7e7SKevin Wolf 7509a502563SJohn Snow unsigned idx = 0; 7519a502563SJohn Snow unsigned size_idx; 7529a502563SJohn Snow unsigned preamble_len; 7539a502563SJohn Snow 7549a502563SJohn Snow /* If the EVPD (Enable Vital Product Data) bit is set in byte 1, 7559a502563SJohn Snow * we are being asked for a specific page of info indicated by byte 2. */ 7569a502563SJohn Snow if (buf[1] & 0x01) { 7579a502563SJohn Snow preamble_len = 4; 7589a502563SJohn Snow size_idx = 3; 7599a502563SJohn Snow 7609a502563SJohn Snow buf[idx++] = 0x05; /* CD-ROM */ 7619a502563SJohn Snow buf[idx++] = page_code; /* Page Code */ 7629a502563SJohn Snow buf[idx++] = 0x00; /* reserved */ 7639a502563SJohn Snow idx++; /* length (set later) */ 7649a502563SJohn Snow 7659a502563SJohn Snow switch (page_code) { 7669a502563SJohn Snow case 0x00: 7679a502563SJohn Snow /* Supported Pages: List of supported VPD responses. */ 7689a502563SJohn Snow buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */ 7699a502563SJohn Snow buf[idx++] = 0x83; /* 0x83: Device Identification. */ 7709a502563SJohn Snow break; 7719a502563SJohn Snow 7729a502563SJohn Snow case 0x83: 7739a502563SJohn Snow /* Device Identification. Each entry is optional, but the entries 7749a502563SJohn Snow * included here are modeled after libata's VPD responses. 7759a502563SJohn Snow * If the response is given, at least one entry must be present. */ 7769a502563SJohn Snow 7779a502563SJohn Snow /* Entry 1: Serial */ 7789a502563SJohn Snow if (idx + 24 > max_len) { 7799a502563SJohn Snow /* Not enough room for even the first entry: */ 7809a502563SJohn Snow /* 4 byte header + 20 byte string */ 7819a502563SJohn Snow ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 7829a502563SJohn Snow ASC_DATA_PHASE_ERROR); 7839a502563SJohn Snow return; 7849a502563SJohn Snow } 7859a502563SJohn Snow buf[idx++] = 0x02; /* Ascii */ 7869a502563SJohn Snow buf[idx++] = 0x00; /* Vendor Specific */ 7879a502563SJohn Snow buf[idx++] = 0x00; 7889a502563SJohn Snow buf[idx++] = 20; /* Remaining length */ 7899a502563SJohn Snow padstr8(buf + idx, 20, s->drive_serial_str); 7909a502563SJohn Snow idx += 20; 7919a502563SJohn Snow 7929a502563SJohn Snow /* Entry 2: Drive Model and Serial */ 7939a502563SJohn Snow if (idx + 72 > max_len) { 7949a502563SJohn Snow /* 4 (header) + 8 (vendor) + 60 (model & serial) */ 7959a502563SJohn Snow goto out; 7969a502563SJohn Snow } 7979a502563SJohn Snow buf[idx++] = 0x02; /* Ascii */ 7989a502563SJohn Snow buf[idx++] = 0x01; /* T10 Vendor */ 7999a502563SJohn Snow buf[idx++] = 0x00; 8009a502563SJohn Snow buf[idx++] = 68; 8019a502563SJohn Snow padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */ 8029a502563SJohn Snow idx += 8; 8039a502563SJohn Snow padstr8(buf + idx, 40, s->drive_model_str); 8049a502563SJohn Snow idx += 40; 8059a502563SJohn Snow padstr8(buf + idx, 20, s->drive_serial_str); 8069a502563SJohn Snow idx += 20; 8079a502563SJohn Snow 8089a502563SJohn Snow /* Entry 3: WWN */ 8099a502563SJohn Snow if (s->wwn && (idx + 12 <= max_len)) { 8109a502563SJohn Snow /* 4 byte header + 8 byte wwn */ 8119a502563SJohn Snow buf[idx++] = 0x01; /* Binary */ 8129a502563SJohn Snow buf[idx++] = 0x03; /* NAA */ 8139a502563SJohn Snow buf[idx++] = 0x00; 8149a502563SJohn Snow buf[idx++] = 0x08; 8159a502563SJohn Snow stq_be_p(&buf[idx], s->wwn); 8169a502563SJohn Snow idx += 8; 8179a502563SJohn Snow } 8189a502563SJohn Snow break; 8199a502563SJohn Snow 8209a502563SJohn Snow default: 8219a502563SJohn Snow /* SPC-3, revision 23 sec. 6.4 */ 8229a502563SJohn Snow ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 8239a502563SJohn Snow ASC_INV_FIELD_IN_CMD_PACKET); 8249a502563SJohn Snow return; 8259a502563SJohn Snow } 8269a502563SJohn Snow } else { 8279a502563SJohn Snow preamble_len = 5; 8289a502563SJohn Snow size_idx = 4; 8299a502563SJohn Snow 830a60cf7e7SKevin Wolf buf[0] = 0x05; /* CD-ROM */ 831a60cf7e7SKevin Wolf buf[1] = 0x80; /* removable */ 832a60cf7e7SKevin Wolf buf[2] = 0x00; /* ISO */ 833a60cf7e7SKevin Wolf buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */ 8349a502563SJohn Snow /* buf[size_idx] set below. */ 835a60cf7e7SKevin Wolf buf[5] = 0; /* reserved */ 836a60cf7e7SKevin Wolf buf[6] = 0; /* reserved */ 837a60cf7e7SKevin Wolf buf[7] = 0; /* reserved */ 838a60cf7e7SKevin Wolf padstr8(buf + 8, 8, "QEMU"); 839a60cf7e7SKevin Wolf padstr8(buf + 16, 16, "QEMU DVD-ROM"); 840a60cf7e7SKevin Wolf padstr8(buf + 32, 4, s->version); 8419a502563SJohn Snow idx = 36; 8429a502563SJohn Snow } 8439a502563SJohn Snow 8449a502563SJohn Snow out: 8459a502563SJohn Snow buf[size_idx] = idx - preamble_len; 8469a502563SJohn Snow ide_atapi_cmd_reply(s, idx, max_len); 847a60cf7e7SKevin Wolf } 848a60cf7e7SKevin Wolf 849a60cf7e7SKevin Wolf static void cmd_get_configuration(IDEState *s, uint8_t *buf) 850a60cf7e7SKevin Wolf { 851a60cf7e7SKevin Wolf uint32_t len; 852a60cf7e7SKevin Wolf uint8_t index = 0; 85333231e0eSKevin Wolf int max_len; 85433231e0eSKevin Wolf 855a60cf7e7SKevin Wolf /* only feature 0 is supported */ 856a60cf7e7SKevin Wolf if (buf[2] != 0 || buf[3] != 0) { 85767cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 858a60cf7e7SKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 85933231e0eSKevin Wolf return; 86033231e0eSKevin Wolf } 86133231e0eSKevin Wolf 862a60cf7e7SKevin Wolf /* XXX: could result in alignment problems in some architectures */ 863a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 864a60cf7e7SKevin Wolf 865a60cf7e7SKevin Wolf /* 866a60cf7e7SKevin Wolf * XXX: avoid overflow for io_buffer if max_len is bigger than 867a60cf7e7SKevin Wolf * the size of that buffer (dimensioned to max number of 868a60cf7e7SKevin Wolf * sectors to transfer at once) 869a60cf7e7SKevin Wolf * 870a60cf7e7SKevin Wolf * Only a problem if the feature/profiles grow. 871a60cf7e7SKevin Wolf */ 872a60cf7e7SKevin Wolf if (max_len > 512) { 873a60cf7e7SKevin Wolf /* XXX: assume 1 sector */ 874a60cf7e7SKevin Wolf max_len = 512; 87533231e0eSKevin Wolf } 876a60cf7e7SKevin Wolf 877a60cf7e7SKevin Wolf memset(buf, 0, max_len); 878a60cf7e7SKevin Wolf /* 879a60cf7e7SKevin Wolf * the number of sectors from the media tells us which profile 880a60cf7e7SKevin Wolf * to use as current. 0 means there is no media 881a60cf7e7SKevin Wolf */ 882a60cf7e7SKevin Wolf if (media_is_dvd(s)) { 883a60cf7e7SKevin Wolf cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM); 884a60cf7e7SKevin Wolf } else if (media_is_cd(s)) { 885a60cf7e7SKevin Wolf cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM); 88633231e0eSKevin Wolf } 887a60cf7e7SKevin Wolf 888a60cf7e7SKevin Wolf buf[10] = 0x02 | 0x01; /* persistent and current */ 889a60cf7e7SKevin Wolf len = 12; /* headers: 8 + 4 */ 890a60cf7e7SKevin Wolf len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM); 891a60cf7e7SKevin Wolf len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM); 892a60cf7e7SKevin Wolf cpu_to_ube32(buf, len - 4); /* data length */ 893a60cf7e7SKevin Wolf 894a60cf7e7SKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 895a60cf7e7SKevin Wolf } 896a60cf7e7SKevin Wolf 897a60cf7e7SKevin Wolf static void cmd_mode_sense(IDEState *s, uint8_t *buf) 89833231e0eSKevin Wolf { 89933231e0eSKevin Wolf int action, code; 900a60cf7e7SKevin Wolf int max_len; 901a60cf7e7SKevin Wolf 902a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 903a60cf7e7SKevin Wolf action = buf[2] >> 6; 904a60cf7e7SKevin Wolf code = buf[2] & 0x3f; 905a60cf7e7SKevin Wolf 90633231e0eSKevin Wolf switch(action) { 90733231e0eSKevin Wolf case 0: /* current values */ 90833231e0eSKevin Wolf switch(code) { 90967cc61e4SPaolo Bonzini case MODE_PAGE_R_W_ERROR: /* error recovery */ 9102c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 16 - 2); 91133231e0eSKevin Wolf buf[2] = 0x70; 91233231e0eSKevin Wolf buf[3] = 0; 91333231e0eSKevin Wolf buf[4] = 0; 91433231e0eSKevin Wolf buf[5] = 0; 91533231e0eSKevin Wolf buf[6] = 0; 91633231e0eSKevin Wolf buf[7] = 0; 91733231e0eSKevin Wolf 918af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_R_W_ERROR; 919af0e1ea2SPaolo Bonzini buf[9] = 16 - 10; 92033231e0eSKevin Wolf buf[10] = 0x00; 92133231e0eSKevin Wolf buf[11] = 0x05; 92233231e0eSKevin Wolf buf[12] = 0x00; 92333231e0eSKevin Wolf buf[13] = 0x00; 92433231e0eSKevin Wolf buf[14] = 0x00; 92533231e0eSKevin Wolf buf[15] = 0x00; 92633231e0eSKevin Wolf ide_atapi_cmd_reply(s, 16, max_len); 92733231e0eSKevin Wolf break; 92867cc61e4SPaolo Bonzini case MODE_PAGE_AUDIO_CTL: 9292c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 24 - 2); 93033231e0eSKevin Wolf buf[2] = 0x70; 93133231e0eSKevin Wolf buf[3] = 0; 93233231e0eSKevin Wolf buf[4] = 0; 93333231e0eSKevin Wolf buf[5] = 0; 93433231e0eSKevin Wolf buf[6] = 0; 93533231e0eSKevin Wolf buf[7] = 0; 93633231e0eSKevin Wolf 937af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_AUDIO_CTL; 938af0e1ea2SPaolo Bonzini buf[9] = 24 - 10; 93933231e0eSKevin Wolf /* Fill with CDROM audio volume */ 94033231e0eSKevin Wolf buf[17] = 0; 94133231e0eSKevin Wolf buf[19] = 0; 94233231e0eSKevin Wolf buf[21] = 0; 94333231e0eSKevin Wolf buf[23] = 0; 94433231e0eSKevin Wolf 94533231e0eSKevin Wolf ide_atapi_cmd_reply(s, 24, max_len); 94633231e0eSKevin Wolf break; 94767cc61e4SPaolo Bonzini case MODE_PAGE_CAPABILITIES: 9482c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 30 - 2); 94933231e0eSKevin Wolf buf[2] = 0x70; 95033231e0eSKevin Wolf buf[3] = 0; 95133231e0eSKevin Wolf buf[4] = 0; 95233231e0eSKevin Wolf buf[5] = 0; 95333231e0eSKevin Wolf buf[6] = 0; 95433231e0eSKevin Wolf buf[7] = 0; 95533231e0eSKevin Wolf 956af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_CAPABILITIES; 9572c20ae11SPaolo Bonzini buf[9] = 30 - 10; 958a07c7dcdSPaolo Bonzini buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */ 95933231e0eSKevin Wolf buf[11] = 0x00; 96033231e0eSKevin Wolf 96133231e0eSKevin Wolf /* Claim PLAY_AUDIO capability (0x01) since some Linux 96233231e0eSKevin Wolf code checks for this to automount media. */ 96333231e0eSKevin Wolf buf[12] = 0x71; 96433231e0eSKevin Wolf buf[13] = 3 << 5; 96533231e0eSKevin Wolf buf[14] = (1 << 0) | (1 << 3) | (1 << 5); 966a0a7573bSMarkus Armbruster if (s->tray_locked) { 967a07c7dcdSPaolo Bonzini buf[14] |= 1 << 1; 968a0a7573bSMarkus Armbruster } 969a07c7dcdSPaolo Bonzini buf[15] = 0x00; /* No volume & mute control, no changer */ 970a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[16], 704); /* 4x read speed */ 971a07c7dcdSPaolo Bonzini buf[18] = 0; /* Two volume levels */ 97233231e0eSKevin Wolf buf[19] = 2; 973a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[20], 512); /* 512k buffer */ 974a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[22], 704); /* 4x read speed current */ 97533231e0eSKevin Wolf buf[24] = 0; 97633231e0eSKevin Wolf buf[25] = 0; 97733231e0eSKevin Wolf buf[26] = 0; 97833231e0eSKevin Wolf buf[27] = 0; 9792c20ae11SPaolo Bonzini buf[28] = 0; 9802c20ae11SPaolo Bonzini buf[29] = 0; 9812c20ae11SPaolo Bonzini ide_atapi_cmd_reply(s, 30, max_len); 98233231e0eSKevin Wolf break; 98333231e0eSKevin Wolf default: 98433231e0eSKevin Wolf goto error_cmd; 98533231e0eSKevin Wolf } 98633231e0eSKevin Wolf break; 98733231e0eSKevin Wolf case 1: /* changeable values */ 98833231e0eSKevin Wolf goto error_cmd; 98933231e0eSKevin Wolf case 2: /* default values */ 99033231e0eSKevin Wolf goto error_cmd; 99133231e0eSKevin Wolf default: 99233231e0eSKevin Wolf case 3: /* saved values */ 99367cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 99433231e0eSKevin Wolf ASC_SAVING_PARAMETERS_NOT_SUPPORTED); 99533231e0eSKevin Wolf break; 99633231e0eSKevin Wolf } 997a60cf7e7SKevin Wolf return; 998a60cf7e7SKevin Wolf 999a60cf7e7SKevin Wolf error_cmd: 100067cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET); 100133231e0eSKevin Wolf } 1002a60cf7e7SKevin Wolf 1003a60cf7e7SKevin Wolf static void cmd_test_unit_ready(IDEState *s, uint8_t *buf) 1004a60cf7e7SKevin Wolf { 10057a2c4b82SKevin Wolf /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we 10067a2c4b82SKevin Wolf * come here, we know that it's ready. */ 100733231e0eSKevin Wolf ide_atapi_cmd_ok(s); 1008a60cf7e7SKevin Wolf } 1009a60cf7e7SKevin Wolf 1010a60cf7e7SKevin Wolf static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf) 1011a60cf7e7SKevin Wolf { 1012a0a7573bSMarkus Armbruster s->tray_locked = buf[4] & 1; 10134be74634SMarkus Armbruster blk_lock_medium(s->blk, buf[4] & 1); 1014a60cf7e7SKevin Wolf ide_atapi_cmd_ok(s); 1015a60cf7e7SKevin Wolf } 1016a60cf7e7SKevin Wolf 1017a60cf7e7SKevin Wolf static void cmd_read(IDEState *s, uint8_t* buf) 101833231e0eSKevin Wolf { 101933231e0eSKevin Wolf int nb_sectors, lba; 102033231e0eSKevin Wolf 1021a60cf7e7SKevin Wolf if (buf[0] == GPCMD_READ_10) { 1022a60cf7e7SKevin Wolf nb_sectors = ube16_to_cpu(buf + 7); 1023a60cf7e7SKevin Wolf } else { 1024a60cf7e7SKevin Wolf nb_sectors = ube32_to_cpu(buf + 6); 1025a60cf7e7SKevin Wolf } 1026a60cf7e7SKevin Wolf 1027a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 102833231e0eSKevin Wolf if (nb_sectors == 0) { 102933231e0eSKevin Wolf ide_atapi_cmd_ok(s); 1030a60cf7e7SKevin Wolf return; 103133231e0eSKevin Wolf } 1032a60cf7e7SKevin Wolf 103333231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2048); 103433231e0eSKevin Wolf } 1035a60cf7e7SKevin Wolf 1036a60cf7e7SKevin Wolf static void cmd_read_cd(IDEState *s, uint8_t* buf) 103733231e0eSKevin Wolf { 103833231e0eSKevin Wolf int nb_sectors, lba, transfer_request; 103933231e0eSKevin Wolf 1040a60cf7e7SKevin Wolf nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8]; 1041a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 1042a60cf7e7SKevin Wolf 104333231e0eSKevin Wolf if (nb_sectors == 0) { 104433231e0eSKevin Wolf ide_atapi_cmd_ok(s); 1045a60cf7e7SKevin Wolf return; 104633231e0eSKevin Wolf } 1047a60cf7e7SKevin Wolf 1048*e7bd708eSJohn Snow transfer_request = buf[9] & 0xf8; 1049*e7bd708eSJohn Snow if (transfer_request == 0x00) { 105033231e0eSKevin Wolf /* nothing */ 105133231e0eSKevin Wolf ide_atapi_cmd_ok(s); 1052*e7bd708eSJohn Snow return; 1053*e7bd708eSJohn Snow } 1054*e7bd708eSJohn Snow 1055*e7bd708eSJohn Snow /* Check validity of BCL before transferring data */ 1056*e7bd708eSJohn Snow if (!validate_bcl(s)) { 1057*e7bd708eSJohn Snow return; 1058*e7bd708eSJohn Snow } 1059*e7bd708eSJohn Snow 1060*e7bd708eSJohn Snow switch (transfer_request) { 106133231e0eSKevin Wolf case 0x10: 106233231e0eSKevin Wolf /* normal read */ 106333231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2048); 106433231e0eSKevin Wolf break; 106533231e0eSKevin Wolf case 0xf8: 106633231e0eSKevin Wolf /* read all data */ 106733231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2352); 106833231e0eSKevin Wolf break; 106933231e0eSKevin Wolf default: 107067cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 107133231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 107233231e0eSKevin Wolf break; 107333231e0eSKevin Wolf } 107433231e0eSKevin Wolf } 1075a60cf7e7SKevin Wolf 1076a60cf7e7SKevin Wolf static void cmd_seek(IDEState *s, uint8_t* buf) 107733231e0eSKevin Wolf { 107833231e0eSKevin Wolf unsigned int lba; 1079e119bcacSKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 108033231e0eSKevin Wolf 1081a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 108233231e0eSKevin Wolf if (lba >= total_sectors) { 108367cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR); 1084a60cf7e7SKevin Wolf return; 108533231e0eSKevin Wolf } 1086a60cf7e7SKevin Wolf 108733231e0eSKevin Wolf ide_atapi_cmd_ok(s); 108833231e0eSKevin Wolf } 1089a60cf7e7SKevin Wolf 1090a60cf7e7SKevin Wolf static void cmd_start_stop_unit(IDEState *s, uint8_t* buf) 109133231e0eSKevin Wolf { 1092fdec4404SMarkus Armbruster int sense; 1093f0776564SMarkus Armbruster bool start = buf[4] & 1; 1094f0776564SMarkus Armbruster bool loej = buf[4] & 2; /* load on start, eject on !start */ 1095ce560dcfSRonnie Sahlberg int pwrcnd = buf[4] & 0xf0; 1096ce560dcfSRonnie Sahlberg 1097ce560dcfSRonnie Sahlberg if (pwrcnd) { 1098ce560dcfSRonnie Sahlberg /* eject/load only happens for power condition == 0 */ 109903441c3aSKevin Wolf ide_atapi_cmd_ok(s); 1100ce560dcfSRonnie Sahlberg return; 1101ce560dcfSRonnie Sahlberg } 110233231e0eSKevin Wolf 1103f0776564SMarkus Armbruster if (loej) { 110448f65b3fSMarkus Armbruster if (!start && !s->tray_open && s->tray_locked) { 11054be74634SMarkus Armbruster sense = blk_is_inserted(s->blk) 110667cc61e4SPaolo Bonzini ? NOT_READY : ILLEGAL_REQUEST; 1107a60cf7e7SKevin Wolf ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED); 1108fdec4404SMarkus Armbruster return; 110933231e0eSKevin Wolf } 1110d88b1819SLuiz Capitulino 1111d88b1819SLuiz Capitulino if (s->tray_open != !start) { 11124be74634SMarkus Armbruster blk_eject(s->blk, !start); 1113dd063333SMarkus Armbruster s->tray_open = !start; 1114dd063333SMarkus Armbruster } 1115d88b1819SLuiz Capitulino } 1116fdec4404SMarkus Armbruster 1117fdec4404SMarkus Armbruster ide_atapi_cmd_ok(s); 111833231e0eSKevin Wolf } 1119a60cf7e7SKevin Wolf 1120a60cf7e7SKevin Wolf static void cmd_mechanism_status(IDEState *s, uint8_t* buf) 112133231e0eSKevin Wolf { 1122a60cf7e7SKevin Wolf int max_len = ube16_to_cpu(buf + 8); 1123a60cf7e7SKevin Wolf 112433231e0eSKevin Wolf cpu_to_ube16(buf, 0); 112533231e0eSKevin Wolf /* no current LBA */ 112633231e0eSKevin Wolf buf[2] = 0; 112733231e0eSKevin Wolf buf[3] = 0; 112833231e0eSKevin Wolf buf[4] = 0; 112933231e0eSKevin Wolf buf[5] = 1; 113033231e0eSKevin Wolf cpu_to_ube16(buf + 6, 0); 113133231e0eSKevin Wolf ide_atapi_cmd_reply(s, 8, max_len); 113233231e0eSKevin Wolf } 1133a60cf7e7SKevin Wolf 1134a60cf7e7SKevin Wolf static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf) 113533231e0eSKevin Wolf { 113633231e0eSKevin Wolf int format, msf, start_track, len; 1137a60cf7e7SKevin Wolf int max_len; 11387a2c4b82SKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 1139a60cf7e7SKevin Wolf 1140a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 1141a60cf7e7SKevin Wolf format = buf[9] >> 6; 1142a60cf7e7SKevin Wolf msf = (buf[1] >> 1) & 1; 1143a60cf7e7SKevin Wolf start_track = buf[6]; 1144a60cf7e7SKevin Wolf 114533231e0eSKevin Wolf switch(format) { 114633231e0eSKevin Wolf case 0: 114733231e0eSKevin Wolf len = cdrom_read_toc(total_sectors, buf, msf, start_track); 114833231e0eSKevin Wolf if (len < 0) 114933231e0eSKevin Wolf goto error_cmd; 115033231e0eSKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 115133231e0eSKevin Wolf break; 115233231e0eSKevin Wolf case 1: 115333231e0eSKevin Wolf /* multi session : only a single session defined */ 115433231e0eSKevin Wolf memset(buf, 0, 12); 115533231e0eSKevin Wolf buf[1] = 0x0a; 115633231e0eSKevin Wolf buf[2] = 0x01; 115733231e0eSKevin Wolf buf[3] = 0x01; 115833231e0eSKevin Wolf ide_atapi_cmd_reply(s, 12, max_len); 115933231e0eSKevin Wolf break; 116033231e0eSKevin Wolf case 2: 116133231e0eSKevin Wolf len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track); 116233231e0eSKevin Wolf if (len < 0) 116333231e0eSKevin Wolf goto error_cmd; 116433231e0eSKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 116533231e0eSKevin Wolf break; 116633231e0eSKevin Wolf default: 116733231e0eSKevin Wolf error_cmd: 116867cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 116933231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 117033231e0eSKevin Wolf } 117133231e0eSKevin Wolf } 1172a60cf7e7SKevin Wolf 1173a60cf7e7SKevin Wolf static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf) 117433231e0eSKevin Wolf { 1175e119bcacSKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 117633231e0eSKevin Wolf 117733231e0eSKevin Wolf /* NOTE: it is really the number of sectors minus 1 */ 117833231e0eSKevin Wolf cpu_to_ube32(buf, total_sectors - 1); 117933231e0eSKevin Wolf cpu_to_ube32(buf + 4, 2048); 118033231e0eSKevin Wolf ide_atapi_cmd_reply(s, 8, 8); 118133231e0eSKevin Wolf } 1182a60cf7e7SKevin Wolf 118355042b95SPaolo Bonzini static void cmd_read_disc_information(IDEState *s, uint8_t* buf) 118455042b95SPaolo Bonzini { 118555042b95SPaolo Bonzini uint8_t type = buf[1] & 7; 118655042b95SPaolo Bonzini uint32_t max_len = ube16_to_cpu(buf + 7); 118755042b95SPaolo Bonzini 118855042b95SPaolo Bonzini /* Types 1/2 are only defined for Blu-Ray. */ 118955042b95SPaolo Bonzini if (type != 0) { 119055042b95SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 119155042b95SPaolo Bonzini ASC_INV_FIELD_IN_CMD_PACKET); 119255042b95SPaolo Bonzini return; 119355042b95SPaolo Bonzini } 119455042b95SPaolo Bonzini 119555042b95SPaolo Bonzini memset(buf, 0, 34); 119655042b95SPaolo Bonzini buf[1] = 32; 119755042b95SPaolo Bonzini buf[2] = 0xe; /* last session complete, disc finalized */ 119855042b95SPaolo Bonzini buf[3] = 1; /* first track on disc */ 119955042b95SPaolo Bonzini buf[4] = 1; /* # of sessions */ 120055042b95SPaolo Bonzini buf[5] = 1; /* first track of last session */ 120155042b95SPaolo Bonzini buf[6] = 1; /* last track of last session */ 120255042b95SPaolo Bonzini buf[7] = 0x20; /* unrestricted use */ 120355042b95SPaolo Bonzini buf[8] = 0x00; /* CD-ROM or DVD-ROM */ 120455042b95SPaolo Bonzini /* 9-10-11: most significant byte corresponding bytes 4-5-6 */ 120555042b95SPaolo Bonzini /* 12-23: not meaningful for CD-ROM or DVD-ROM */ 120655042b95SPaolo Bonzini /* 24-31: disc bar code */ 120755042b95SPaolo Bonzini /* 32: disc application code */ 120855042b95SPaolo Bonzini /* 33: number of OPC tables */ 120955042b95SPaolo Bonzini 121055042b95SPaolo Bonzini ide_atapi_cmd_reply(s, 34, max_len); 121155042b95SPaolo Bonzini } 121255042b95SPaolo Bonzini 1213a60cf7e7SKevin Wolf static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf) 121433231e0eSKevin Wolf { 1215a60cf7e7SKevin Wolf int max_len; 1216a60cf7e7SKevin Wolf int media = buf[1]; 1217a60cf7e7SKevin Wolf int format = buf[7]; 121833231e0eSKevin Wolf int ret; 121933231e0eSKevin Wolf 1220a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 8); 122133231e0eSKevin Wolf 122233231e0eSKevin Wolf if (format < 0xff) { 122333231e0eSKevin Wolf if (media_is_cd(s)) { 122467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 122533231e0eSKevin Wolf ASC_INCOMPATIBLE_FORMAT); 1226a60cf7e7SKevin Wolf return; 122733231e0eSKevin Wolf } else if (!media_present(s)) { 122867cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 122933231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 1230a60cf7e7SKevin Wolf return; 123133231e0eSKevin Wolf } 123233231e0eSKevin Wolf } 123333231e0eSKevin Wolf 123433231e0eSKevin Wolf memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ? 123533231e0eSKevin Wolf IDE_DMA_BUF_SECTORS * 512 + 4 : max_len); 123633231e0eSKevin Wolf 123733231e0eSKevin Wolf switch (format) { 123833231e0eSKevin Wolf case 0x00 ... 0x7f: 123933231e0eSKevin Wolf case 0xff: 124033231e0eSKevin Wolf if (media == 0) { 1241a60cf7e7SKevin Wolf ret = ide_dvd_read_structure(s, format, buf, buf); 124233231e0eSKevin Wolf 1243a60cf7e7SKevin Wolf if (ret < 0) { 124467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret); 1245a60cf7e7SKevin Wolf } else { 124633231e0eSKevin Wolf ide_atapi_cmd_reply(s, ret, max_len); 1247a60cf7e7SKevin Wolf } 124833231e0eSKevin Wolf 124933231e0eSKevin Wolf break; 125033231e0eSKevin Wolf } 125133231e0eSKevin Wolf /* TODO: BD support, fall through for now */ 125233231e0eSKevin Wolf 125333231e0eSKevin Wolf /* Generic disk structures */ 125433231e0eSKevin Wolf case 0x80: /* TODO: AACS volume identifier */ 125533231e0eSKevin Wolf case 0x81: /* TODO: AACS media serial number */ 125633231e0eSKevin Wolf case 0x82: /* TODO: AACS media identifier */ 125733231e0eSKevin Wolf case 0x83: /* TODO: AACS media key block */ 125833231e0eSKevin Wolf case 0x90: /* TODO: List of recognized format layers */ 125933231e0eSKevin Wolf case 0xc0: /* TODO: Write protection status */ 126033231e0eSKevin Wolf default: 126167cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 126233231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 126333231e0eSKevin Wolf break; 126433231e0eSKevin Wolf } 126533231e0eSKevin Wolf } 1266a60cf7e7SKevin Wolf 1267a60cf7e7SKevin Wolf static void cmd_set_speed(IDEState *s, uint8_t* buf) 1268a60cf7e7SKevin Wolf { 1269a60cf7e7SKevin Wolf ide_atapi_cmd_ok(s); 1270a60cf7e7SKevin Wolf } 1271a60cf7e7SKevin Wolf 1272e1a064f9SKevin Wolf enum { 1273e1a064f9SKevin Wolf /* 1274e1a064f9SKevin Wolf * Only commands flagged as ALLOW_UA are allowed to run under a 1275e1a064f9SKevin Wolf * unit attention condition. (See MMC-5, section 4.1.6.1) 1276e1a064f9SKevin Wolf */ 1277e1a064f9SKevin Wolf ALLOW_UA = 0x01, 12787a2c4b82SKevin Wolf 12797a2c4b82SKevin Wolf /* 12807a2c4b82SKevin Wolf * Commands flagged with CHECK_READY can only execute if a medium is present. 12817a2c4b82SKevin Wolf * Otherwise they report the Not Ready Condition. (See MMC-5, section 12827a2c4b82SKevin Wolf * 4.1.8) 12837a2c4b82SKevin Wolf */ 12847a2c4b82SKevin Wolf CHECK_READY = 0x02, 12859ef2e93fSJohn Snow 12869ef2e93fSJohn Snow /* 12879ef2e93fSJohn Snow * Commands flagged with NONDATA do not in any circumstances return 12889ef2e93fSJohn Snow * any data via ide_atapi_cmd_reply. These commands are exempt from 12899ef2e93fSJohn Snow * the normal byte_count_limit constraints. 12909ef2e93fSJohn Snow * See ATA8-ACS3 "7.21.5 Byte Count Limit" 12919ef2e93fSJohn Snow */ 12929ef2e93fSJohn Snow NONDATA = 0x04, 1293*e7bd708eSJohn Snow 1294*e7bd708eSJohn Snow /* 1295*e7bd708eSJohn Snow * CONDDATA implies a command that transfers data only conditionally based 1296*e7bd708eSJohn Snow * on the presence of suboptions. It should be exempt from the BCL check at 1297*e7bd708eSJohn Snow * command validation time, but it needs to be checked at the command 1298*e7bd708eSJohn Snow * handler level instead. 1299*e7bd708eSJohn Snow */ 1300*e7bd708eSJohn Snow CONDDATA = 0x08, 1301e1a064f9SKevin Wolf }; 1302e1a064f9SKevin Wolf 1303f36aa12dSJohn Snow static const struct AtapiCmd { 1304e1a064f9SKevin Wolf void (*handler)(IDEState *s, uint8_t *buf); 1305e1a064f9SKevin Wolf int flags; 1306e1a064f9SKevin Wolf } atapi_cmd_table[0x100] = { 13079ef2e93fSJohn Snow [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA }, 1308e1a064f9SKevin Wolf [ 0x03 ] = { cmd_request_sense, ALLOW_UA }, 1309e1a064f9SKevin Wolf [ 0x12 ] = { cmd_inquiry, ALLOW_UA }, 13109ef2e93fSJohn Snow [ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */ 13119ef2e93fSJohn Snow [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA }, 13127a2c4b82SKevin Wolf [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY }, 1313a1aff5bfSMarkus Armbruster [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY }, 13149ef2e93fSJohn Snow [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA }, 13157a2c4b82SKevin Wolf [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY }, 1316e1a064f9SKevin Wolf [ 0x46 ] = { cmd_get_configuration, ALLOW_UA }, 1317e1a064f9SKevin Wolf [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA }, 131855042b95SPaolo Bonzini [ 0x51 ] = { cmd_read_disc_information, CHECK_READY }, 1319e1a064f9SKevin Wolf [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 }, 1320a1aff5bfSMarkus Armbruster [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY }, 1321a1aff5bfSMarkus Armbruster [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY }, 13229ef2e93fSJohn Snow [ 0xbb ] = { cmd_set_speed, NONDATA }, 1323e1a064f9SKevin Wolf [ 0xbd ] = { cmd_mechanism_status, 0 }, 1324*e7bd708eSJohn Snow [ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA }, 1325a1aff5bfSMarkus Armbruster /* [1] handler detects and reports not ready condition itself */ 1326e1a064f9SKevin Wolf }; 1327e1a064f9SKevin Wolf 1328a60cf7e7SKevin Wolf void ide_atapi_cmd(IDEState *s) 1329a60cf7e7SKevin Wolf { 1330f36aa12dSJohn Snow uint8_t *buf = s->io_buffer; 1331f36aa12dSJohn Snow const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]]; 1332a60cf7e7SKevin Wolf 1333a60cf7e7SKevin Wolf #ifdef DEBUG_IDE_ATAPI 1334a60cf7e7SKevin Wolf { 1335a60cf7e7SKevin Wolf int i; 1336a60cf7e7SKevin Wolf printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8)); 1337a60cf7e7SKevin Wolf for(i = 0; i < ATAPI_PACKET_SIZE; i++) { 1338ab719827SAlon Levy printf(" %02x", buf[i]); 1339a60cf7e7SKevin Wolf } 1340a60cf7e7SKevin Wolf printf("\n"); 1341a60cf7e7SKevin Wolf } 1342a60cf7e7SKevin Wolf #endif 1343f36aa12dSJohn Snow 1344a60cf7e7SKevin Wolf /* 1345e1a064f9SKevin Wolf * If there's a UNIT_ATTENTION condition pending, only command flagged with 1346e1a064f9SKevin Wolf * ALLOW_UA are allowed to complete. with other commands getting a CHECK 1347e1a064f9SKevin Wolf * condition response unless a higher priority status, defined by the drive 1348a60cf7e7SKevin Wolf * here, is pending. 1349a60cf7e7SKevin Wolf */ 1350f36aa12dSJohn Snow if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) { 1351a60cf7e7SKevin Wolf ide_atapi_cmd_check_status(s); 1352a60cf7e7SKevin Wolf return; 1353a60cf7e7SKevin Wolf } 13544a737d14SAmit Shah /* 13554a737d14SAmit Shah * When a CD gets changed, we have to report an ejected state and 13564a737d14SAmit Shah * then a loaded state to guests so that they detect tray 13574a737d14SAmit Shah * open/close and media change events. Guests that do not use 13584a737d14SAmit Shah * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close 13594a737d14SAmit Shah * states rely on this behavior. 13604a737d14SAmit Shah */ 1361f36aa12dSJohn Snow if (!(cmd->flags & ALLOW_UA) && 13624be74634SMarkus Armbruster !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) { 1363a60cf7e7SKevin Wolf 13640c6f08b0SPavel Hrdina if (s->cdrom_changed == 1) { 13650c6f08b0SPavel Hrdina ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT); 13660c6f08b0SPavel Hrdina s->cdrom_changed = 2; 13670c6f08b0SPavel Hrdina } else { 13680c6f08b0SPavel Hrdina ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED); 1369a60cf7e7SKevin Wolf s->cdrom_changed = 0; 13700c6f08b0SPavel Hrdina } 13710c6f08b0SPavel Hrdina 1372a60cf7e7SKevin Wolf return; 1373a60cf7e7SKevin Wolf } 1374e1a064f9SKevin Wolf 13757a2c4b82SKevin Wolf /* Report a Not Ready condition if appropriate for the command */ 1376f36aa12dSJohn Snow if ((cmd->flags & CHECK_READY) && 13774be74634SMarkus Armbruster (!media_present(s) || !blk_is_inserted(s->blk))) 13787a2c4b82SKevin Wolf { 137967cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT); 13807a2c4b82SKevin Wolf return; 13817a2c4b82SKevin Wolf } 13827a2c4b82SKevin Wolf 1383*e7bd708eSJohn Snow /* Commands that don't transfer DATA permit the byte_count_limit to be 0. 13849ef2e93fSJohn Snow * If this is a data-transferring PIO command and BCL is 0, 13859ef2e93fSJohn Snow * we abort at the /ATA/ level, not the ATAPI level. 13869ef2e93fSJohn Snow * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */ 1387*e7bd708eSJohn Snow if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) { 1388*e7bd708eSJohn Snow if (!validate_bcl(s)) { 13899ef2e93fSJohn Snow return; 13909ef2e93fSJohn Snow } 13919ef2e93fSJohn Snow } 13929ef2e93fSJohn Snow 1393e1a064f9SKevin Wolf /* Execute the command */ 1394f36aa12dSJohn Snow if (cmd->handler) { 1395f36aa12dSJohn Snow cmd->handler(s, buf); 1396e1a064f9SKevin Wolf return; 139733231e0eSKevin Wolf } 1398e1a064f9SKevin Wolf 139967cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE); 140033231e0eSKevin Wolf } 1401