133231e0eSKevin Wolf /* 233231e0eSKevin Wolf * QEMU ATAPI Emulation 333231e0eSKevin Wolf * 433231e0eSKevin Wolf * Copyright (c) 2003 Fabrice Bellard 533231e0eSKevin Wolf * Copyright (c) 2006 Openedhand Ltd. 633231e0eSKevin Wolf * 733231e0eSKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 833231e0eSKevin Wolf * of this software and associated documentation files (the "Software"), to deal 933231e0eSKevin Wolf * in the Software without restriction, including without limitation the rights 1033231e0eSKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1133231e0eSKevin Wolf * copies of the Software, and to permit persons to whom the Software is 1233231e0eSKevin Wolf * furnished to do so, subject to the following conditions: 1333231e0eSKevin Wolf * 1433231e0eSKevin Wolf * The above copyright notice and this permission notice shall be included in 1533231e0eSKevin Wolf * all copies or substantial portions of the Software. 1633231e0eSKevin Wolf * 1733231e0eSKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1833231e0eSKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1933231e0eSKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2033231e0eSKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2133231e0eSKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2233231e0eSKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2333231e0eSKevin Wolf * THE SOFTWARE. 2433231e0eSKevin Wolf */ 2533231e0eSKevin Wolf 2633231e0eSKevin Wolf #include "hw/ide/internal.h" 270d09e41aSPaolo Bonzini #include "hw/scsi/scsi.h" 284be74634SMarkus Armbruster #include "sysemu/block-backend.h" 2933231e0eSKevin Wolf 3033231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret); 3133231e0eSKevin Wolf 3233231e0eSKevin Wolf static void padstr8(uint8_t *buf, int buf_size, const char *src) 3333231e0eSKevin Wolf { 3433231e0eSKevin Wolf int i; 3533231e0eSKevin Wolf for(i = 0; i < buf_size; i++) { 3633231e0eSKevin Wolf if (*src) 3733231e0eSKevin Wolf buf[i] = *src++; 3833231e0eSKevin Wolf else 3933231e0eSKevin Wolf buf[i] = ' '; 4033231e0eSKevin Wolf } 4133231e0eSKevin Wolf } 4233231e0eSKevin Wolf 4333231e0eSKevin Wolf static inline void cpu_to_ube16(uint8_t *buf, int val) 4433231e0eSKevin Wolf { 4533231e0eSKevin Wolf buf[0] = val >> 8; 4633231e0eSKevin Wolf buf[1] = val & 0xff; 4733231e0eSKevin Wolf } 4833231e0eSKevin Wolf 4933231e0eSKevin Wolf static inline void cpu_to_ube32(uint8_t *buf, unsigned int val) 5033231e0eSKevin Wolf { 5133231e0eSKevin Wolf buf[0] = val >> 24; 5233231e0eSKevin Wolf buf[1] = val >> 16; 5333231e0eSKevin Wolf buf[2] = val >> 8; 5433231e0eSKevin Wolf buf[3] = val & 0xff; 5533231e0eSKevin Wolf } 5633231e0eSKevin Wolf 5733231e0eSKevin Wolf static inline int ube16_to_cpu(const uint8_t *buf) 5833231e0eSKevin Wolf { 5933231e0eSKevin Wolf return (buf[0] << 8) | buf[1]; 6033231e0eSKevin Wolf } 6133231e0eSKevin Wolf 6233231e0eSKevin Wolf static inline int ube32_to_cpu(const uint8_t *buf) 6333231e0eSKevin Wolf { 6433231e0eSKevin Wolf return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; 6533231e0eSKevin Wolf } 6633231e0eSKevin Wolf 6733231e0eSKevin Wolf static void lba_to_msf(uint8_t *buf, int lba) 6833231e0eSKevin Wolf { 6933231e0eSKevin Wolf lba += 150; 7033231e0eSKevin Wolf buf[0] = (lba / 75) / 60; 7133231e0eSKevin Wolf buf[1] = (lba / 75) % 60; 7233231e0eSKevin Wolf buf[2] = lba % 75; 7333231e0eSKevin Wolf } 7433231e0eSKevin Wolf 7533231e0eSKevin Wolf static inline int media_present(IDEState *s) 7633231e0eSKevin Wolf { 77a1aff5bfSMarkus Armbruster return !s->tray_open && s->nb_sectors > 0; 7833231e0eSKevin Wolf } 7933231e0eSKevin Wolf 80a7acf552SAmit Shah /* XXX: DVDs that could fit on a CD will be reported as a CD */ 8133231e0eSKevin Wolf static inline int media_is_dvd(IDEState *s) 8233231e0eSKevin Wolf { 8333231e0eSKevin Wolf return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS); 8433231e0eSKevin Wolf } 8533231e0eSKevin Wolf 8633231e0eSKevin Wolf static inline int media_is_cd(IDEState *s) 8733231e0eSKevin Wolf { 8833231e0eSKevin Wolf return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS); 8933231e0eSKevin Wolf } 9033231e0eSKevin Wolf 9133231e0eSKevin Wolf static void cd_data_to_raw(uint8_t *buf, int lba) 9233231e0eSKevin Wolf { 9333231e0eSKevin Wolf /* sync bytes */ 9433231e0eSKevin Wolf buf[0] = 0x00; 9533231e0eSKevin Wolf memset(buf + 1, 0xff, 10); 9633231e0eSKevin Wolf buf[11] = 0x00; 9733231e0eSKevin Wolf buf += 12; 9833231e0eSKevin Wolf /* MSF */ 9933231e0eSKevin Wolf lba_to_msf(buf, lba); 10033231e0eSKevin Wolf buf[3] = 0x01; /* mode 1 data */ 10133231e0eSKevin Wolf buf += 4; 10233231e0eSKevin Wolf /* data */ 10333231e0eSKevin Wolf buf += 2048; 10433231e0eSKevin Wolf /* XXX: ECC not computed */ 10533231e0eSKevin Wolf memset(buf, 0, 288); 10633231e0eSKevin Wolf } 10733231e0eSKevin Wolf 108a597e79cSChristoph Hellwig static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size) 10933231e0eSKevin Wolf { 11033231e0eSKevin Wolf int ret; 11133231e0eSKevin Wolf 11233231e0eSKevin Wolf switch(sector_size) { 11333231e0eSKevin Wolf case 2048: 1144be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, 1155366d0c8SBenoît Canet 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ); 1164be74634SMarkus Armbruster ret = blk_read(s->blk, (int64_t)lba << 2, buf, 4); 1174be74634SMarkus Armbruster block_acct_done(blk_get_stats(s->blk), &s->acct); 11833231e0eSKevin Wolf break; 11933231e0eSKevin Wolf case 2352: 1204be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, 1215366d0c8SBenoît Canet 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ); 1224be74634SMarkus Armbruster ret = blk_read(s->blk, (int64_t)lba << 2, buf + 16, 4); 1234be74634SMarkus Armbruster block_acct_done(blk_get_stats(s->blk), &s->acct); 12433231e0eSKevin Wolf if (ret < 0) 12533231e0eSKevin Wolf return ret; 12633231e0eSKevin Wolf cd_data_to_raw(buf, lba); 12733231e0eSKevin Wolf break; 12833231e0eSKevin Wolf default: 12933231e0eSKevin Wolf ret = -EIO; 13033231e0eSKevin Wolf break; 13133231e0eSKevin Wolf } 13233231e0eSKevin Wolf return ret; 13333231e0eSKevin Wolf } 13433231e0eSKevin Wolf 13533231e0eSKevin Wolf void ide_atapi_cmd_ok(IDEState *s) 13633231e0eSKevin Wolf { 13733231e0eSKevin Wolf s->error = 0; 13833231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 13933231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 140d735b620SJohn Snow ide_transfer_stop(s); 14133231e0eSKevin Wolf ide_set_irq(s->bus); 14233231e0eSKevin Wolf } 14333231e0eSKevin Wolf 14433231e0eSKevin Wolf void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc) 14533231e0eSKevin Wolf { 14633231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 14733231e0eSKevin Wolf printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc); 14833231e0eSKevin Wolf #endif 14933231e0eSKevin Wolf s->error = sense_key << 4; 15033231e0eSKevin Wolf s->status = READY_STAT | ERR_STAT; 15133231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 15233231e0eSKevin Wolf s->sense_key = sense_key; 15333231e0eSKevin Wolf s->asc = asc; 154d735b620SJohn Snow ide_transfer_stop(s); 15533231e0eSKevin Wolf ide_set_irq(s->bus); 15633231e0eSKevin Wolf } 15733231e0eSKevin Wolf 15833231e0eSKevin Wolf void ide_atapi_io_error(IDEState *s, int ret) 15933231e0eSKevin Wolf { 16033231e0eSKevin Wolf /* XXX: handle more errors */ 16133231e0eSKevin Wolf if (ret == -ENOMEDIUM) { 16267cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, NOT_READY, 16333231e0eSKevin Wolf ASC_MEDIUM_NOT_PRESENT); 16433231e0eSKevin Wolf } else { 16567cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 16633231e0eSKevin Wolf ASC_LOGICAL_BLOCK_OOR); 16733231e0eSKevin Wolf } 16833231e0eSKevin Wolf } 16933231e0eSKevin Wolf 17033231e0eSKevin Wolf /* The whole ATAPI transfer logic is handled in this function */ 17133231e0eSKevin Wolf void ide_atapi_cmd_reply_end(IDEState *s) 17233231e0eSKevin Wolf { 17333231e0eSKevin Wolf int byte_count_limit, size, ret; 17433231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 17533231e0eSKevin Wolf printf("reply: tx_size=%d elem_tx_size=%d index=%d\n", 17633231e0eSKevin Wolf s->packet_transfer_size, 17733231e0eSKevin Wolf s->elementary_transfer_size, 17833231e0eSKevin Wolf s->io_buffer_index); 17933231e0eSKevin Wolf #endif 18033231e0eSKevin Wolf if (s->packet_transfer_size <= 0) { 18133231e0eSKevin Wolf /* end of transfer */ 182d735b620SJohn Snow ide_atapi_cmd_ok(s); 18333231e0eSKevin Wolf ide_set_irq(s->bus); 18433231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 18533231e0eSKevin Wolf printf("status=0x%x\n", s->status); 18633231e0eSKevin Wolf #endif 18733231e0eSKevin Wolf } else { 18833231e0eSKevin Wolf /* see if a new sector must be read */ 18933231e0eSKevin Wolf if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) { 190a597e79cSChristoph Hellwig ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size); 19133231e0eSKevin Wolf if (ret < 0) { 19233231e0eSKevin Wolf ide_atapi_io_error(s, ret); 19333231e0eSKevin Wolf return; 19433231e0eSKevin Wolf } 19533231e0eSKevin Wolf s->lba++; 19633231e0eSKevin Wolf s->io_buffer_index = 0; 19733231e0eSKevin Wolf } 19833231e0eSKevin Wolf if (s->elementary_transfer_size > 0) { 19933231e0eSKevin Wolf /* there are some data left to transmit in this elementary 20033231e0eSKevin Wolf transfer */ 20133231e0eSKevin Wolf size = s->cd_sector_size - s->io_buffer_index; 20233231e0eSKevin Wolf if (size > s->elementary_transfer_size) 20333231e0eSKevin Wolf size = s->elementary_transfer_size; 20433231e0eSKevin Wolf s->packet_transfer_size -= size; 20533231e0eSKevin Wolf s->elementary_transfer_size -= size; 20633231e0eSKevin Wolf s->io_buffer_index += size; 20733231e0eSKevin Wolf ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size, 20833231e0eSKevin Wolf size, ide_atapi_cmd_reply_end); 20933231e0eSKevin Wolf } else { 21033231e0eSKevin Wolf /* a new transfer is needed */ 21133231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO; 21233231e0eSKevin Wolf byte_count_limit = s->lcyl | (s->hcyl << 8); 21333231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 21433231e0eSKevin Wolf printf("byte_count_limit=%d\n", byte_count_limit); 21533231e0eSKevin Wolf #endif 21633231e0eSKevin Wolf if (byte_count_limit == 0xffff) 21733231e0eSKevin Wolf byte_count_limit--; 21833231e0eSKevin Wolf size = s->packet_transfer_size; 21933231e0eSKevin Wolf if (size > byte_count_limit) { 22033231e0eSKevin Wolf /* byte count limit must be even if this case */ 22133231e0eSKevin Wolf if (byte_count_limit & 1) 22233231e0eSKevin Wolf byte_count_limit--; 22333231e0eSKevin Wolf size = byte_count_limit; 22433231e0eSKevin Wolf } 22533231e0eSKevin Wolf s->lcyl = size; 22633231e0eSKevin Wolf s->hcyl = size >> 8; 22733231e0eSKevin Wolf s->elementary_transfer_size = size; 22833231e0eSKevin Wolf /* we cannot transmit more than one sector at a time */ 22933231e0eSKevin Wolf if (s->lba != -1) { 23033231e0eSKevin Wolf if (size > (s->cd_sector_size - s->io_buffer_index)) 23133231e0eSKevin Wolf size = (s->cd_sector_size - s->io_buffer_index); 23233231e0eSKevin Wolf } 23333231e0eSKevin Wolf s->packet_transfer_size -= size; 23433231e0eSKevin Wolf s->elementary_transfer_size -= size; 23533231e0eSKevin Wolf s->io_buffer_index += size; 23633231e0eSKevin Wolf ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size, 23733231e0eSKevin Wolf size, ide_atapi_cmd_reply_end); 23833231e0eSKevin Wolf ide_set_irq(s->bus); 23933231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 24033231e0eSKevin Wolf printf("status=0x%x\n", s->status); 24133231e0eSKevin Wolf #endif 24233231e0eSKevin Wolf } 24333231e0eSKevin Wolf } 24433231e0eSKevin Wolf } 24533231e0eSKevin Wolf 24633231e0eSKevin Wolf /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */ 24733231e0eSKevin Wolf static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size) 24833231e0eSKevin Wolf { 24933231e0eSKevin Wolf if (size > max_size) 25033231e0eSKevin Wolf size = max_size; 25133231e0eSKevin Wolf s->lba = -1; /* no sector read */ 25233231e0eSKevin Wolf s->packet_transfer_size = size; 25333231e0eSKevin Wolf s->io_buffer_size = size; /* dma: send the reply data as one chunk */ 25433231e0eSKevin Wolf s->elementary_transfer_size = 0; 25533231e0eSKevin Wolf s->io_buffer_index = 0; 25633231e0eSKevin Wolf 25733231e0eSKevin Wolf if (s->atapi_dma) { 2584be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, size, 2595366d0c8SBenoît Canet BLOCK_ACCT_READ); 26033231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT | DRQ_STAT; 2614855b576SPaolo Bonzini ide_start_dma(s, ide_atapi_cmd_read_dma_cb); 26233231e0eSKevin Wolf } else { 26333231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 26433231e0eSKevin Wolf ide_atapi_cmd_reply_end(s); 26533231e0eSKevin Wolf } 26633231e0eSKevin Wolf } 26733231e0eSKevin Wolf 26833231e0eSKevin Wolf /* start a CD-CDROM read command */ 26933231e0eSKevin Wolf static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors, 27033231e0eSKevin Wolf int sector_size) 27133231e0eSKevin Wolf { 27233231e0eSKevin Wolf s->lba = lba; 27333231e0eSKevin Wolf s->packet_transfer_size = nb_sectors * sector_size; 27433231e0eSKevin Wolf s->elementary_transfer_size = 0; 27533231e0eSKevin Wolf s->io_buffer_index = sector_size; 27633231e0eSKevin Wolf s->cd_sector_size = sector_size; 27733231e0eSKevin Wolf 27833231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 27933231e0eSKevin Wolf ide_atapi_cmd_reply_end(s); 28033231e0eSKevin Wolf } 28133231e0eSKevin Wolf 28233231e0eSKevin Wolf static void ide_atapi_cmd_check_status(IDEState *s) 28333231e0eSKevin Wolf { 28433231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 28533231e0eSKevin Wolf printf("atapi_cmd_check_status\n"); 28633231e0eSKevin Wolf #endif 28767cc61e4SPaolo Bonzini s->error = MC_ERR | (UNIT_ATTENTION << 4); 28833231e0eSKevin Wolf s->status = ERR_STAT; 28933231e0eSKevin Wolf s->nsector = 0; 29033231e0eSKevin Wolf ide_set_irq(s->bus); 29133231e0eSKevin Wolf } 29233231e0eSKevin Wolf /* ATAPI DMA support */ 29333231e0eSKevin Wolf 29433231e0eSKevin Wolf /* XXX: handle read errors */ 29533231e0eSKevin Wolf static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret) 29633231e0eSKevin Wolf { 29733231e0eSKevin Wolf IDEState *s = opaque; 29833231e0eSKevin Wolf int data_offset, n; 29933231e0eSKevin Wolf 30033231e0eSKevin Wolf if (ret < 0) { 30133231e0eSKevin Wolf ide_atapi_io_error(s, ret); 30233231e0eSKevin Wolf goto eot; 30333231e0eSKevin Wolf } 30433231e0eSKevin Wolf 30533231e0eSKevin Wolf if (s->io_buffer_size > 0) { 30633231e0eSKevin Wolf /* 30733231e0eSKevin Wolf * For a cdrom read sector command (s->lba != -1), 30833231e0eSKevin Wolf * adjust the lba for the next s->io_buffer_size chunk 30933231e0eSKevin Wolf * and dma the current chunk. 31033231e0eSKevin Wolf * For a command != read (s->lba == -1), just transfer 31133231e0eSKevin Wolf * the reply data. 31233231e0eSKevin Wolf */ 31333231e0eSKevin Wolf if (s->lba != -1) { 31433231e0eSKevin Wolf if (s->cd_sector_size == 2352) { 31533231e0eSKevin Wolf n = 1; 31633231e0eSKevin Wolf cd_data_to_raw(s->io_buffer, s->lba); 31733231e0eSKevin Wolf } else { 31833231e0eSKevin Wolf n = s->io_buffer_size >> 11; 31933231e0eSKevin Wolf } 32033231e0eSKevin Wolf s->lba += n; 32133231e0eSKevin Wolf } 32233231e0eSKevin Wolf s->packet_transfer_size -= s->io_buffer_size; 32333231e0eSKevin Wolf if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0) 32433231e0eSKevin Wolf goto eot; 32533231e0eSKevin Wolf } 32633231e0eSKevin Wolf 32733231e0eSKevin Wolf if (s->packet_transfer_size <= 0) { 32833231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT; 32933231e0eSKevin Wolf s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; 33033231e0eSKevin Wolf ide_set_irq(s->bus); 331a597e79cSChristoph Hellwig goto eot; 33233231e0eSKevin Wolf } 33333231e0eSKevin Wolf 33433231e0eSKevin Wolf s->io_buffer_index = 0; 33533231e0eSKevin Wolf if (s->cd_sector_size == 2352) { 33633231e0eSKevin Wolf n = 1; 33733231e0eSKevin Wolf s->io_buffer_size = s->cd_sector_size; 33833231e0eSKevin Wolf data_offset = 16; 33933231e0eSKevin Wolf } else { 34033231e0eSKevin Wolf n = s->packet_transfer_size >> 11; 34133231e0eSKevin Wolf if (n > (IDE_DMA_BUF_SECTORS / 4)) 34233231e0eSKevin Wolf n = (IDE_DMA_BUF_SECTORS / 4); 34333231e0eSKevin Wolf s->io_buffer_size = n * 2048; 34433231e0eSKevin Wolf data_offset = 0; 34533231e0eSKevin Wolf } 34633231e0eSKevin Wolf #ifdef DEBUG_AIO 34733231e0eSKevin Wolf printf("aio_read_cd: lba=%u n=%d\n", s->lba, n); 34833231e0eSKevin Wolf #endif 349a597e79cSChristoph Hellwig 35033231e0eSKevin Wolf s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset); 35133231e0eSKevin Wolf s->bus->dma->iov.iov_len = n * 4 * 512; 35233231e0eSKevin Wolf qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1); 353a597e79cSChristoph Hellwig 3544be74634SMarkus Armbruster s->bus->dma->aiocb = blk_aio_readv(s->blk, (int64_t)s->lba << 2, 35533231e0eSKevin Wolf &s->bus->dma->qiov, n * 4, 35633231e0eSKevin Wolf ide_atapi_cmd_read_dma_cb, s); 357a597e79cSChristoph Hellwig return; 358ad54ae80SPaolo Bonzini 359a597e79cSChristoph Hellwig eot: 3604be74634SMarkus Armbruster block_acct_done(blk_get_stats(s->blk), &s->acct); 3610e7ce54cSPaolo Bonzini ide_set_inactive(s, false); 36233231e0eSKevin Wolf } 36333231e0eSKevin Wolf 36433231e0eSKevin Wolf /* start a CD-CDROM read command with DMA */ 36533231e0eSKevin Wolf /* XXX: test if DMA is available */ 36633231e0eSKevin Wolf static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors, 36733231e0eSKevin Wolf int sector_size) 36833231e0eSKevin Wolf { 36933231e0eSKevin Wolf s->lba = lba; 37033231e0eSKevin Wolf s->packet_transfer_size = nb_sectors * sector_size; 37133231e0eSKevin Wolf s->io_buffer_index = 0; 37233231e0eSKevin Wolf s->io_buffer_size = 0; 37333231e0eSKevin Wolf s->cd_sector_size = sector_size; 37433231e0eSKevin Wolf 3754be74634SMarkus Armbruster block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size, 3765366d0c8SBenoît Canet BLOCK_ACCT_READ); 377a597e79cSChristoph Hellwig 37833231e0eSKevin Wolf /* XXX: check if BUSY_STAT should be set */ 37933231e0eSKevin Wolf s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT; 3804855b576SPaolo Bonzini ide_start_dma(s, ide_atapi_cmd_read_dma_cb); 38133231e0eSKevin Wolf } 38233231e0eSKevin Wolf 38333231e0eSKevin Wolf static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors, 38433231e0eSKevin Wolf int sector_size) 38533231e0eSKevin Wolf { 38633231e0eSKevin Wolf #ifdef DEBUG_IDE_ATAPI 38733231e0eSKevin Wolf printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio", 38833231e0eSKevin Wolf lba, nb_sectors); 38933231e0eSKevin Wolf #endif 39033231e0eSKevin Wolf if (s->atapi_dma) { 39133231e0eSKevin Wolf ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size); 39233231e0eSKevin Wolf } else { 39333231e0eSKevin Wolf ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size); 39433231e0eSKevin Wolf } 39533231e0eSKevin Wolf } 39633231e0eSKevin Wolf 39733231e0eSKevin Wolf static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index, 39833231e0eSKevin Wolf uint16_t profile) 39933231e0eSKevin Wolf { 40033231e0eSKevin Wolf uint8_t *buf_profile = buf + 12; /* start of profiles */ 40133231e0eSKevin Wolf 40233231e0eSKevin Wolf buf_profile += ((*index) * 4); /* start of indexed profile */ 40333231e0eSKevin Wolf cpu_to_ube16 (buf_profile, profile); 40433231e0eSKevin Wolf buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7])); 40533231e0eSKevin Wolf 40633231e0eSKevin Wolf /* each profile adds 4 bytes to the response */ 40733231e0eSKevin Wolf (*index)++; 40833231e0eSKevin Wolf buf[11] += 4; /* Additional Length */ 40933231e0eSKevin Wolf 41033231e0eSKevin Wolf return 4; 41133231e0eSKevin Wolf } 41233231e0eSKevin Wolf 41333231e0eSKevin Wolf static int ide_dvd_read_structure(IDEState *s, int format, 41433231e0eSKevin Wolf const uint8_t *packet, uint8_t *buf) 41533231e0eSKevin Wolf { 41633231e0eSKevin Wolf switch (format) { 41733231e0eSKevin Wolf case 0x0: /* Physical format information */ 41833231e0eSKevin Wolf { 41933231e0eSKevin Wolf int layer = packet[6]; 42033231e0eSKevin Wolf uint64_t total_sectors; 42133231e0eSKevin Wolf 42233231e0eSKevin Wolf if (layer != 0) 42333231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 42433231e0eSKevin Wolf 425e119bcacSKevin Wolf total_sectors = s->nb_sectors >> 2; 426e119bcacSKevin Wolf if (total_sectors == 0) { 42733231e0eSKevin Wolf return -ASC_MEDIUM_NOT_PRESENT; 428e119bcacSKevin Wolf } 42933231e0eSKevin Wolf 43033231e0eSKevin Wolf buf[4] = 1; /* DVD-ROM, part version 1 */ 43133231e0eSKevin Wolf buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */ 43233231e0eSKevin Wolf buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */ 43333231e0eSKevin Wolf buf[7] = 0; /* default densities */ 43433231e0eSKevin Wolf 43533231e0eSKevin Wolf /* FIXME: 0x30000 per spec? */ 43633231e0eSKevin Wolf cpu_to_ube32(buf + 8, 0); /* start sector */ 43733231e0eSKevin Wolf cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */ 43833231e0eSKevin Wolf cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */ 43933231e0eSKevin Wolf 44033231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 441d8ee2591SPeter Maydell stw_be_p(buf, 2048 + 2); 44233231e0eSKevin Wolf 44333231e0eSKevin Wolf /* 2k data + 4 byte header */ 44433231e0eSKevin Wolf return (2048 + 4); 44533231e0eSKevin Wolf } 44633231e0eSKevin Wolf 44733231e0eSKevin Wolf case 0x01: /* DVD copyright information */ 44833231e0eSKevin Wolf buf[4] = 0; /* no copyright data */ 44933231e0eSKevin Wolf buf[5] = 0; /* no region restrictions */ 45033231e0eSKevin Wolf 45133231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 452d8ee2591SPeter Maydell stw_be_p(buf, 4 + 2); 45333231e0eSKevin Wolf 45433231e0eSKevin Wolf /* 4 byte header + 4 byte data */ 45533231e0eSKevin Wolf return (4 + 4); 45633231e0eSKevin Wolf 45733231e0eSKevin Wolf case 0x03: /* BCA information - invalid field for no BCA info */ 45833231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 45933231e0eSKevin Wolf 46033231e0eSKevin Wolf case 0x04: /* DVD disc manufacturing information */ 46133231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 462d8ee2591SPeter Maydell stw_be_p(buf, 2048 + 2); 46333231e0eSKevin Wolf 46433231e0eSKevin Wolf /* 2k data + 4 byte header */ 46533231e0eSKevin Wolf return (2048 + 4); 46633231e0eSKevin Wolf 46733231e0eSKevin Wolf case 0xff: 46833231e0eSKevin Wolf /* 46933231e0eSKevin Wolf * This lists all the command capabilities above. Add new ones 47033231e0eSKevin Wolf * in order and update the length and buffer return values. 47133231e0eSKevin Wolf */ 47233231e0eSKevin Wolf 47333231e0eSKevin Wolf buf[4] = 0x00; /* Physical format */ 47433231e0eSKevin Wolf buf[5] = 0x40; /* Not writable, is readable */ 475d8ee2591SPeter Maydell stw_be_p(buf + 6, 2048 + 4); 47633231e0eSKevin Wolf 47733231e0eSKevin Wolf buf[8] = 0x01; /* Copyright info */ 47833231e0eSKevin Wolf buf[9] = 0x40; /* Not writable, is readable */ 479d8ee2591SPeter Maydell stw_be_p(buf + 10, 4 + 4); 48033231e0eSKevin Wolf 48133231e0eSKevin Wolf buf[12] = 0x03; /* BCA info */ 48233231e0eSKevin Wolf buf[13] = 0x40; /* Not writable, is readable */ 483d8ee2591SPeter Maydell stw_be_p(buf + 14, 188 + 4); 48433231e0eSKevin Wolf 48533231e0eSKevin Wolf buf[16] = 0x04; /* Manufacturing info */ 48633231e0eSKevin Wolf buf[17] = 0x40; /* Not writable, is readable */ 487d8ee2591SPeter Maydell stw_be_p(buf + 18, 2048 + 4); 48833231e0eSKevin Wolf 48933231e0eSKevin Wolf /* Size of buffer, not including 2 byte size field */ 490d8ee2591SPeter Maydell stw_be_p(buf, 16 + 2); 49133231e0eSKevin Wolf 49233231e0eSKevin Wolf /* data written + 4 byte header */ 49333231e0eSKevin Wolf return (16 + 4); 49433231e0eSKevin Wolf 49533231e0eSKevin Wolf default: /* TODO: formats beyond DVD-ROM requires */ 49633231e0eSKevin Wolf return -ASC_INV_FIELD_IN_CMD_PACKET; 49733231e0eSKevin Wolf } 49833231e0eSKevin Wolf } 49933231e0eSKevin Wolf 50033231e0eSKevin Wolf static unsigned int event_status_media(IDEState *s, 50133231e0eSKevin Wolf uint8_t *buf) 50233231e0eSKevin Wolf { 50333231e0eSKevin Wolf uint8_t event_code, media_status; 50433231e0eSKevin Wolf 50533231e0eSKevin Wolf media_status = 0; 506dd063333SMarkus Armbruster if (s->tray_open) { 50733231e0eSKevin Wolf media_status = MS_TRAY_OPEN; 5084be74634SMarkus Armbruster } else if (blk_is_inserted(s->blk)) { 50933231e0eSKevin Wolf media_status = MS_MEDIA_PRESENT; 51033231e0eSKevin Wolf } 51133231e0eSKevin Wolf 51233231e0eSKevin Wolf /* Event notification descriptor */ 51333231e0eSKevin Wolf event_code = MEC_NO_CHANGE; 5142df0a3a3SPaolo Bonzini if (media_status != MS_TRAY_OPEN) { 5152df0a3a3SPaolo Bonzini if (s->events.new_media) { 51633231e0eSKevin Wolf event_code = MEC_NEW_MEDIA; 51733231e0eSKevin Wolf s->events.new_media = false; 5182df0a3a3SPaolo Bonzini } else if (s->events.eject_request) { 5192df0a3a3SPaolo Bonzini event_code = MEC_EJECT_REQUESTED; 5202df0a3a3SPaolo Bonzini s->events.eject_request = false; 5212df0a3a3SPaolo Bonzini } 52233231e0eSKevin Wolf } 52333231e0eSKevin Wolf 52433231e0eSKevin Wolf buf[4] = event_code; 52533231e0eSKevin Wolf buf[5] = media_status; 52633231e0eSKevin Wolf 52733231e0eSKevin Wolf /* These fields are reserved, just clear them. */ 52833231e0eSKevin Wolf buf[6] = 0; 52933231e0eSKevin Wolf buf[7] = 0; 53033231e0eSKevin Wolf 53133231e0eSKevin Wolf return 8; /* We wrote to 4 extra bytes from the header */ 53233231e0eSKevin Wolf } 53333231e0eSKevin Wolf 534e1a064f9SKevin Wolf static void cmd_get_event_status_notification(IDEState *s, 535e1a064f9SKevin Wolf uint8_t *buf) 53633231e0eSKevin Wolf { 537e1a064f9SKevin Wolf const uint8_t *packet = buf; 538e1a064f9SKevin Wolf 53933231e0eSKevin Wolf struct { 54033231e0eSKevin Wolf uint8_t opcode; 54133231e0eSKevin Wolf uint8_t polled; /* lsb bit is polled; others are reserved */ 54233231e0eSKevin Wolf uint8_t reserved2[2]; 54333231e0eSKevin Wolf uint8_t class; 54433231e0eSKevin Wolf uint8_t reserved3[2]; 54533231e0eSKevin Wolf uint16_t len; 54633231e0eSKevin Wolf uint8_t control; 547541dc0d4SStefan Weil } QEMU_PACKED *gesn_cdb; 54833231e0eSKevin Wolf 54933231e0eSKevin Wolf struct { 55033231e0eSKevin Wolf uint16_t len; 55133231e0eSKevin Wolf uint8_t notification_class; 55233231e0eSKevin Wolf uint8_t supported_events; 553541dc0d4SStefan Weil } QEMU_PACKED *gesn_event_header; 55433231e0eSKevin Wolf unsigned int max_len, used_len; 55533231e0eSKevin Wolf 55633231e0eSKevin Wolf gesn_cdb = (void *)packet; 55733231e0eSKevin Wolf gesn_event_header = (void *)buf; 55833231e0eSKevin Wolf 55933231e0eSKevin Wolf max_len = be16_to_cpu(gesn_cdb->len); 56033231e0eSKevin Wolf 56133231e0eSKevin Wolf /* It is fine by the MMC spec to not support async mode operations */ 56233231e0eSKevin Wolf if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */ 56333231e0eSKevin Wolf /* Only polling is supported, asynchronous mode is not. */ 56467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 56533231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 56633231e0eSKevin Wolf return; 56733231e0eSKevin Wolf } 56833231e0eSKevin Wolf 56933231e0eSKevin Wolf /* polling mode operation */ 57033231e0eSKevin Wolf 57133231e0eSKevin Wolf /* 57233231e0eSKevin Wolf * These are the supported events. 57333231e0eSKevin Wolf * 57433231e0eSKevin Wolf * We currently only support requests of the 'media' type. 575f0f992e6SPaolo Bonzini * Notification class requests and supported event classes are bitmasks, 576f0f992e6SPaolo Bonzini * but they are build from the same values as the "notification class" 577f0f992e6SPaolo Bonzini * field. 57833231e0eSKevin Wolf */ 579f0f992e6SPaolo Bonzini gesn_event_header->supported_events = 1 << GESN_MEDIA; 58033231e0eSKevin Wolf 58133231e0eSKevin Wolf /* 58233231e0eSKevin Wolf * We use |= below to set the class field; other bits in this byte 58333231e0eSKevin Wolf * are reserved now but this is useful to do if we have to use the 58433231e0eSKevin Wolf * reserved fields later. 58533231e0eSKevin Wolf */ 58633231e0eSKevin Wolf gesn_event_header->notification_class = 0; 58733231e0eSKevin Wolf 58833231e0eSKevin Wolf /* 58933231e0eSKevin Wolf * Responses to requests are to be based on request priority. The 59033231e0eSKevin Wolf * notification_class_request_type enum above specifies the 59133231e0eSKevin Wolf * priority: upper elements are higher prio than lower ones. 59233231e0eSKevin Wolf */ 593f0f992e6SPaolo Bonzini if (gesn_cdb->class & (1 << GESN_MEDIA)) { 594f0f992e6SPaolo Bonzini gesn_event_header->notification_class |= GESN_MEDIA; 59533231e0eSKevin Wolf used_len = event_status_media(s, buf); 59633231e0eSKevin Wolf } else { 59733231e0eSKevin Wolf gesn_event_header->notification_class = 0x80; /* No event available */ 59833231e0eSKevin Wolf used_len = sizeof(*gesn_event_header); 59933231e0eSKevin Wolf } 60033231e0eSKevin Wolf gesn_event_header->len = cpu_to_be16(used_len 60133231e0eSKevin Wolf - sizeof(*gesn_event_header)); 60233231e0eSKevin Wolf ide_atapi_cmd_reply(s, used_len, max_len); 60333231e0eSKevin Wolf } 60433231e0eSKevin Wolf 605a60cf7e7SKevin Wolf static void cmd_request_sense(IDEState *s, uint8_t *buf) 60633231e0eSKevin Wolf { 607a60cf7e7SKevin Wolf int max_len = buf[4]; 608a60cf7e7SKevin Wolf 609a60cf7e7SKevin Wolf memset(buf, 0, 18); 610a60cf7e7SKevin Wolf buf[0] = 0x70 | (1 << 7); 611a60cf7e7SKevin Wolf buf[2] = s->sense_key; 612a60cf7e7SKevin Wolf buf[7] = 10; 613a60cf7e7SKevin Wolf buf[12] = s->asc; 614a60cf7e7SKevin Wolf 61567cc61e4SPaolo Bonzini if (s->sense_key == UNIT_ATTENTION) { 61667cc61e4SPaolo Bonzini s->sense_key = NO_SENSE; 617a60cf7e7SKevin Wolf } 618a60cf7e7SKevin Wolf 619a60cf7e7SKevin Wolf ide_atapi_cmd_reply(s, 18, max_len); 620a60cf7e7SKevin Wolf } 621a60cf7e7SKevin Wolf 622a60cf7e7SKevin Wolf static void cmd_inquiry(IDEState *s, uint8_t *buf) 623a60cf7e7SKevin Wolf { 624*9a502563SJohn Snow uint8_t page_code = buf[2]; 625a60cf7e7SKevin Wolf int max_len = buf[4]; 626a60cf7e7SKevin Wolf 627*9a502563SJohn Snow unsigned idx = 0; 628*9a502563SJohn Snow unsigned size_idx; 629*9a502563SJohn Snow unsigned preamble_len; 630*9a502563SJohn Snow 631*9a502563SJohn Snow /* If the EVPD (Enable Vital Product Data) bit is set in byte 1, 632*9a502563SJohn Snow * we are being asked for a specific page of info indicated by byte 2. */ 633*9a502563SJohn Snow if (buf[1] & 0x01) { 634*9a502563SJohn Snow preamble_len = 4; 635*9a502563SJohn Snow size_idx = 3; 636*9a502563SJohn Snow 637*9a502563SJohn Snow buf[idx++] = 0x05; /* CD-ROM */ 638*9a502563SJohn Snow buf[idx++] = page_code; /* Page Code */ 639*9a502563SJohn Snow buf[idx++] = 0x00; /* reserved */ 640*9a502563SJohn Snow idx++; /* length (set later) */ 641*9a502563SJohn Snow 642*9a502563SJohn Snow switch (page_code) { 643*9a502563SJohn Snow case 0x00: 644*9a502563SJohn Snow /* Supported Pages: List of supported VPD responses. */ 645*9a502563SJohn Snow buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */ 646*9a502563SJohn Snow buf[idx++] = 0x83; /* 0x83: Device Identification. */ 647*9a502563SJohn Snow break; 648*9a502563SJohn Snow 649*9a502563SJohn Snow case 0x83: 650*9a502563SJohn Snow /* Device Identification. Each entry is optional, but the entries 651*9a502563SJohn Snow * included here are modeled after libata's VPD responses. 652*9a502563SJohn Snow * If the response is given, at least one entry must be present. */ 653*9a502563SJohn Snow 654*9a502563SJohn Snow /* Entry 1: Serial */ 655*9a502563SJohn Snow if (idx + 24 > max_len) { 656*9a502563SJohn Snow /* Not enough room for even the first entry: */ 657*9a502563SJohn Snow /* 4 byte header + 20 byte string */ 658*9a502563SJohn Snow ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 659*9a502563SJohn Snow ASC_DATA_PHASE_ERROR); 660*9a502563SJohn Snow return; 661*9a502563SJohn Snow } 662*9a502563SJohn Snow buf[idx++] = 0x02; /* Ascii */ 663*9a502563SJohn Snow buf[idx++] = 0x00; /* Vendor Specific */ 664*9a502563SJohn Snow buf[idx++] = 0x00; 665*9a502563SJohn Snow buf[idx++] = 20; /* Remaining length */ 666*9a502563SJohn Snow padstr8(buf + idx, 20, s->drive_serial_str); 667*9a502563SJohn Snow idx += 20; 668*9a502563SJohn Snow 669*9a502563SJohn Snow /* Entry 2: Drive Model and Serial */ 670*9a502563SJohn Snow if (idx + 72 > max_len) { 671*9a502563SJohn Snow /* 4 (header) + 8 (vendor) + 60 (model & serial) */ 672*9a502563SJohn Snow goto out; 673*9a502563SJohn Snow } 674*9a502563SJohn Snow buf[idx++] = 0x02; /* Ascii */ 675*9a502563SJohn Snow buf[idx++] = 0x01; /* T10 Vendor */ 676*9a502563SJohn Snow buf[idx++] = 0x00; 677*9a502563SJohn Snow buf[idx++] = 68; 678*9a502563SJohn Snow padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */ 679*9a502563SJohn Snow idx += 8; 680*9a502563SJohn Snow padstr8(buf + idx, 40, s->drive_model_str); 681*9a502563SJohn Snow idx += 40; 682*9a502563SJohn Snow padstr8(buf + idx, 20, s->drive_serial_str); 683*9a502563SJohn Snow idx += 20; 684*9a502563SJohn Snow 685*9a502563SJohn Snow /* Entry 3: WWN */ 686*9a502563SJohn Snow if (s->wwn && (idx + 12 <= max_len)) { 687*9a502563SJohn Snow /* 4 byte header + 8 byte wwn */ 688*9a502563SJohn Snow buf[idx++] = 0x01; /* Binary */ 689*9a502563SJohn Snow buf[idx++] = 0x03; /* NAA */ 690*9a502563SJohn Snow buf[idx++] = 0x00; 691*9a502563SJohn Snow buf[idx++] = 0x08; 692*9a502563SJohn Snow stq_be_p(&buf[idx], s->wwn); 693*9a502563SJohn Snow idx += 8; 694*9a502563SJohn Snow } 695*9a502563SJohn Snow break; 696*9a502563SJohn Snow 697*9a502563SJohn Snow default: 698*9a502563SJohn Snow /* SPC-3, revision 23 sec. 6.4 */ 699*9a502563SJohn Snow ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 700*9a502563SJohn Snow ASC_INV_FIELD_IN_CMD_PACKET); 701*9a502563SJohn Snow return; 702*9a502563SJohn Snow } 703*9a502563SJohn Snow } else { 704*9a502563SJohn Snow preamble_len = 5; 705*9a502563SJohn Snow size_idx = 4; 706*9a502563SJohn Snow 707a60cf7e7SKevin Wolf buf[0] = 0x05; /* CD-ROM */ 708a60cf7e7SKevin Wolf buf[1] = 0x80; /* removable */ 709a60cf7e7SKevin Wolf buf[2] = 0x00; /* ISO */ 710a60cf7e7SKevin Wolf buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */ 711*9a502563SJohn Snow /* buf[size_idx] set below. */ 712a60cf7e7SKevin Wolf buf[5] = 0; /* reserved */ 713a60cf7e7SKevin Wolf buf[6] = 0; /* reserved */ 714a60cf7e7SKevin Wolf buf[7] = 0; /* reserved */ 715a60cf7e7SKevin Wolf padstr8(buf + 8, 8, "QEMU"); 716a60cf7e7SKevin Wolf padstr8(buf + 16, 16, "QEMU DVD-ROM"); 717a60cf7e7SKevin Wolf padstr8(buf + 32, 4, s->version); 718*9a502563SJohn Snow idx = 36; 719*9a502563SJohn Snow } 720*9a502563SJohn Snow 721*9a502563SJohn Snow out: 722*9a502563SJohn Snow buf[size_idx] = idx - preamble_len; 723*9a502563SJohn Snow ide_atapi_cmd_reply(s, idx, max_len); 724*9a502563SJohn Snow return; 725a60cf7e7SKevin Wolf } 726a60cf7e7SKevin Wolf 727a60cf7e7SKevin Wolf static void cmd_get_configuration(IDEState *s, uint8_t *buf) 728a60cf7e7SKevin Wolf { 729a60cf7e7SKevin Wolf uint32_t len; 730a60cf7e7SKevin Wolf uint8_t index = 0; 73133231e0eSKevin Wolf int max_len; 73233231e0eSKevin Wolf 733a60cf7e7SKevin Wolf /* only feature 0 is supported */ 734a60cf7e7SKevin Wolf if (buf[2] != 0 || buf[3] != 0) { 73567cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 736a60cf7e7SKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 73733231e0eSKevin Wolf return; 73833231e0eSKevin Wolf } 73933231e0eSKevin Wolf 740a60cf7e7SKevin Wolf /* XXX: could result in alignment problems in some architectures */ 741a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 742a60cf7e7SKevin Wolf 743a60cf7e7SKevin Wolf /* 744a60cf7e7SKevin Wolf * XXX: avoid overflow for io_buffer if max_len is bigger than 745a60cf7e7SKevin Wolf * the size of that buffer (dimensioned to max number of 746a60cf7e7SKevin Wolf * sectors to transfer at once) 747a60cf7e7SKevin Wolf * 748a60cf7e7SKevin Wolf * Only a problem if the feature/profiles grow. 749a60cf7e7SKevin Wolf */ 750a60cf7e7SKevin Wolf if (max_len > 512) { 751a60cf7e7SKevin Wolf /* XXX: assume 1 sector */ 752a60cf7e7SKevin Wolf max_len = 512; 75333231e0eSKevin Wolf } 754a60cf7e7SKevin Wolf 755a60cf7e7SKevin Wolf memset(buf, 0, max_len); 756a60cf7e7SKevin Wolf /* 757a60cf7e7SKevin Wolf * the number of sectors from the media tells us which profile 758a60cf7e7SKevin Wolf * to use as current. 0 means there is no media 759a60cf7e7SKevin Wolf */ 760a60cf7e7SKevin Wolf if (media_is_dvd(s)) { 761a60cf7e7SKevin Wolf cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM); 762a60cf7e7SKevin Wolf } else if (media_is_cd(s)) { 763a60cf7e7SKevin Wolf cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM); 76433231e0eSKevin Wolf } 765a60cf7e7SKevin Wolf 766a60cf7e7SKevin Wolf buf[10] = 0x02 | 0x01; /* persistent and current */ 767a60cf7e7SKevin Wolf len = 12; /* headers: 8 + 4 */ 768a60cf7e7SKevin Wolf len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM); 769a60cf7e7SKevin Wolf len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM); 770a60cf7e7SKevin Wolf cpu_to_ube32(buf, len - 4); /* data length */ 771a60cf7e7SKevin Wolf 772a60cf7e7SKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 773a60cf7e7SKevin Wolf } 774a60cf7e7SKevin Wolf 775a60cf7e7SKevin Wolf static void cmd_mode_sense(IDEState *s, uint8_t *buf) 77633231e0eSKevin Wolf { 77733231e0eSKevin Wolf int action, code; 778a60cf7e7SKevin Wolf int max_len; 779a60cf7e7SKevin Wolf 780a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 781a60cf7e7SKevin Wolf action = buf[2] >> 6; 782a60cf7e7SKevin Wolf code = buf[2] & 0x3f; 783a60cf7e7SKevin Wolf 78433231e0eSKevin Wolf switch(action) { 78533231e0eSKevin Wolf case 0: /* current values */ 78633231e0eSKevin Wolf switch(code) { 78767cc61e4SPaolo Bonzini case MODE_PAGE_R_W_ERROR: /* error recovery */ 7882c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 16 - 2); 78933231e0eSKevin Wolf buf[2] = 0x70; 79033231e0eSKevin Wolf buf[3] = 0; 79133231e0eSKevin Wolf buf[4] = 0; 79233231e0eSKevin Wolf buf[5] = 0; 79333231e0eSKevin Wolf buf[6] = 0; 79433231e0eSKevin Wolf buf[7] = 0; 79533231e0eSKevin Wolf 796af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_R_W_ERROR; 797af0e1ea2SPaolo Bonzini buf[9] = 16 - 10; 79833231e0eSKevin Wolf buf[10] = 0x00; 79933231e0eSKevin Wolf buf[11] = 0x05; 80033231e0eSKevin Wolf buf[12] = 0x00; 80133231e0eSKevin Wolf buf[13] = 0x00; 80233231e0eSKevin Wolf buf[14] = 0x00; 80333231e0eSKevin Wolf buf[15] = 0x00; 80433231e0eSKevin Wolf ide_atapi_cmd_reply(s, 16, max_len); 80533231e0eSKevin Wolf break; 80667cc61e4SPaolo Bonzini case MODE_PAGE_AUDIO_CTL: 8072c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 24 - 2); 80833231e0eSKevin Wolf buf[2] = 0x70; 80933231e0eSKevin Wolf buf[3] = 0; 81033231e0eSKevin Wolf buf[4] = 0; 81133231e0eSKevin Wolf buf[5] = 0; 81233231e0eSKevin Wolf buf[6] = 0; 81333231e0eSKevin Wolf buf[7] = 0; 81433231e0eSKevin Wolf 815af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_AUDIO_CTL; 816af0e1ea2SPaolo Bonzini buf[9] = 24 - 10; 81733231e0eSKevin Wolf /* Fill with CDROM audio volume */ 81833231e0eSKevin Wolf buf[17] = 0; 81933231e0eSKevin Wolf buf[19] = 0; 82033231e0eSKevin Wolf buf[21] = 0; 82133231e0eSKevin Wolf buf[23] = 0; 82233231e0eSKevin Wolf 82333231e0eSKevin Wolf ide_atapi_cmd_reply(s, 24, max_len); 82433231e0eSKevin Wolf break; 82567cc61e4SPaolo Bonzini case MODE_PAGE_CAPABILITIES: 8262c20ae11SPaolo Bonzini cpu_to_ube16(&buf[0], 30 - 2); 82733231e0eSKevin Wolf buf[2] = 0x70; 82833231e0eSKevin Wolf buf[3] = 0; 82933231e0eSKevin Wolf buf[4] = 0; 83033231e0eSKevin Wolf buf[5] = 0; 83133231e0eSKevin Wolf buf[6] = 0; 83233231e0eSKevin Wolf buf[7] = 0; 83333231e0eSKevin Wolf 834af0e1ea2SPaolo Bonzini buf[8] = MODE_PAGE_CAPABILITIES; 8352c20ae11SPaolo Bonzini buf[9] = 30 - 10; 836a07c7dcdSPaolo Bonzini buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */ 83733231e0eSKevin Wolf buf[11] = 0x00; 83833231e0eSKevin Wolf 83933231e0eSKevin Wolf /* Claim PLAY_AUDIO capability (0x01) since some Linux 84033231e0eSKevin Wolf code checks for this to automount media. */ 84133231e0eSKevin Wolf buf[12] = 0x71; 84233231e0eSKevin Wolf buf[13] = 3 << 5; 84333231e0eSKevin Wolf buf[14] = (1 << 0) | (1 << 3) | (1 << 5); 844a0a7573bSMarkus Armbruster if (s->tray_locked) { 845a07c7dcdSPaolo Bonzini buf[14] |= 1 << 1; 846a0a7573bSMarkus Armbruster } 847a07c7dcdSPaolo Bonzini buf[15] = 0x00; /* No volume & mute control, no changer */ 848a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[16], 704); /* 4x read speed */ 849a07c7dcdSPaolo Bonzini buf[18] = 0; /* Two volume levels */ 85033231e0eSKevin Wolf buf[19] = 2; 851a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[20], 512); /* 512k buffer */ 852a07c7dcdSPaolo Bonzini cpu_to_ube16(&buf[22], 704); /* 4x read speed current */ 85333231e0eSKevin Wolf buf[24] = 0; 85433231e0eSKevin Wolf buf[25] = 0; 85533231e0eSKevin Wolf buf[26] = 0; 85633231e0eSKevin Wolf buf[27] = 0; 8572c20ae11SPaolo Bonzini buf[28] = 0; 8582c20ae11SPaolo Bonzini buf[29] = 0; 8592c20ae11SPaolo Bonzini ide_atapi_cmd_reply(s, 30, max_len); 86033231e0eSKevin Wolf break; 86133231e0eSKevin Wolf default: 86233231e0eSKevin Wolf goto error_cmd; 86333231e0eSKevin Wolf } 86433231e0eSKevin Wolf break; 86533231e0eSKevin Wolf case 1: /* changeable values */ 86633231e0eSKevin Wolf goto error_cmd; 86733231e0eSKevin Wolf case 2: /* default values */ 86833231e0eSKevin Wolf goto error_cmd; 86933231e0eSKevin Wolf default: 87033231e0eSKevin Wolf case 3: /* saved values */ 87167cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 87233231e0eSKevin Wolf ASC_SAVING_PARAMETERS_NOT_SUPPORTED); 87333231e0eSKevin Wolf break; 87433231e0eSKevin Wolf } 875a60cf7e7SKevin Wolf return; 876a60cf7e7SKevin Wolf 877a60cf7e7SKevin Wolf error_cmd: 87867cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET); 87933231e0eSKevin Wolf } 880a60cf7e7SKevin Wolf 881a60cf7e7SKevin Wolf static void cmd_test_unit_ready(IDEState *s, uint8_t *buf) 882a60cf7e7SKevin Wolf { 8837a2c4b82SKevin Wolf /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we 8847a2c4b82SKevin Wolf * come here, we know that it's ready. */ 88533231e0eSKevin Wolf ide_atapi_cmd_ok(s); 886a60cf7e7SKevin Wolf } 887a60cf7e7SKevin Wolf 888a60cf7e7SKevin Wolf static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf) 889a60cf7e7SKevin Wolf { 890a0a7573bSMarkus Armbruster s->tray_locked = buf[4] & 1; 8914be74634SMarkus Armbruster blk_lock_medium(s->blk, buf[4] & 1); 892a60cf7e7SKevin Wolf ide_atapi_cmd_ok(s); 893a60cf7e7SKevin Wolf } 894a60cf7e7SKevin Wolf 895a60cf7e7SKevin Wolf static void cmd_read(IDEState *s, uint8_t* buf) 89633231e0eSKevin Wolf { 89733231e0eSKevin Wolf int nb_sectors, lba; 89833231e0eSKevin Wolf 899a60cf7e7SKevin Wolf if (buf[0] == GPCMD_READ_10) { 900a60cf7e7SKevin Wolf nb_sectors = ube16_to_cpu(buf + 7); 901a60cf7e7SKevin Wolf } else { 902a60cf7e7SKevin Wolf nb_sectors = ube32_to_cpu(buf + 6); 903a60cf7e7SKevin Wolf } 904a60cf7e7SKevin Wolf 905a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 90633231e0eSKevin Wolf if (nb_sectors == 0) { 90733231e0eSKevin Wolf ide_atapi_cmd_ok(s); 908a60cf7e7SKevin Wolf return; 90933231e0eSKevin Wolf } 910a60cf7e7SKevin Wolf 91133231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2048); 91233231e0eSKevin Wolf } 913a60cf7e7SKevin Wolf 914a60cf7e7SKevin Wolf static void cmd_read_cd(IDEState *s, uint8_t* buf) 91533231e0eSKevin Wolf { 91633231e0eSKevin Wolf int nb_sectors, lba, transfer_request; 91733231e0eSKevin Wolf 918a60cf7e7SKevin Wolf nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8]; 919a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 920a60cf7e7SKevin Wolf 92133231e0eSKevin Wolf if (nb_sectors == 0) { 92233231e0eSKevin Wolf ide_atapi_cmd_ok(s); 923a60cf7e7SKevin Wolf return; 92433231e0eSKevin Wolf } 925a60cf7e7SKevin Wolf 926a60cf7e7SKevin Wolf transfer_request = buf[9]; 92733231e0eSKevin Wolf switch(transfer_request & 0xf8) { 92833231e0eSKevin Wolf case 0x00: 92933231e0eSKevin Wolf /* nothing */ 93033231e0eSKevin Wolf ide_atapi_cmd_ok(s); 93133231e0eSKevin Wolf break; 93233231e0eSKevin Wolf case 0x10: 93333231e0eSKevin Wolf /* normal read */ 93433231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2048); 93533231e0eSKevin Wolf break; 93633231e0eSKevin Wolf case 0xf8: 93733231e0eSKevin Wolf /* read all data */ 93833231e0eSKevin Wolf ide_atapi_cmd_read(s, lba, nb_sectors, 2352); 93933231e0eSKevin Wolf break; 94033231e0eSKevin Wolf default: 94167cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 94233231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 94333231e0eSKevin Wolf break; 94433231e0eSKevin Wolf } 94533231e0eSKevin Wolf } 946a60cf7e7SKevin Wolf 947a60cf7e7SKevin Wolf static void cmd_seek(IDEState *s, uint8_t* buf) 94833231e0eSKevin Wolf { 94933231e0eSKevin Wolf unsigned int lba; 950e119bcacSKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 95133231e0eSKevin Wolf 952a60cf7e7SKevin Wolf lba = ube32_to_cpu(buf + 2); 95333231e0eSKevin Wolf if (lba >= total_sectors) { 95467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR); 955a60cf7e7SKevin Wolf return; 95633231e0eSKevin Wolf } 957a60cf7e7SKevin Wolf 95833231e0eSKevin Wolf ide_atapi_cmd_ok(s); 95933231e0eSKevin Wolf } 960a60cf7e7SKevin Wolf 961a60cf7e7SKevin Wolf static void cmd_start_stop_unit(IDEState *s, uint8_t* buf) 96233231e0eSKevin Wolf { 963fdec4404SMarkus Armbruster int sense; 964f0776564SMarkus Armbruster bool start = buf[4] & 1; 965f0776564SMarkus Armbruster bool loej = buf[4] & 2; /* load on start, eject on !start */ 966ce560dcfSRonnie Sahlberg int pwrcnd = buf[4] & 0xf0; 967ce560dcfSRonnie Sahlberg 968ce560dcfSRonnie Sahlberg if (pwrcnd) { 969ce560dcfSRonnie Sahlberg /* eject/load only happens for power condition == 0 */ 970ce560dcfSRonnie Sahlberg return; 971ce560dcfSRonnie Sahlberg } 97233231e0eSKevin Wolf 973f0776564SMarkus Armbruster if (loej) { 97448f65b3fSMarkus Armbruster if (!start && !s->tray_open && s->tray_locked) { 9754be74634SMarkus Armbruster sense = blk_is_inserted(s->blk) 97667cc61e4SPaolo Bonzini ? NOT_READY : ILLEGAL_REQUEST; 977a60cf7e7SKevin Wolf ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED); 978fdec4404SMarkus Armbruster return; 97933231e0eSKevin Wolf } 980d88b1819SLuiz Capitulino 981d88b1819SLuiz Capitulino if (s->tray_open != !start) { 9824be74634SMarkus Armbruster blk_eject(s->blk, !start); 983dd063333SMarkus Armbruster s->tray_open = !start; 984dd063333SMarkus Armbruster } 985d88b1819SLuiz Capitulino } 986fdec4404SMarkus Armbruster 987fdec4404SMarkus Armbruster ide_atapi_cmd_ok(s); 98833231e0eSKevin Wolf } 989a60cf7e7SKevin Wolf 990a60cf7e7SKevin Wolf static void cmd_mechanism_status(IDEState *s, uint8_t* buf) 99133231e0eSKevin Wolf { 992a60cf7e7SKevin Wolf int max_len = ube16_to_cpu(buf + 8); 993a60cf7e7SKevin Wolf 99433231e0eSKevin Wolf cpu_to_ube16(buf, 0); 99533231e0eSKevin Wolf /* no current LBA */ 99633231e0eSKevin Wolf buf[2] = 0; 99733231e0eSKevin Wolf buf[3] = 0; 99833231e0eSKevin Wolf buf[4] = 0; 99933231e0eSKevin Wolf buf[5] = 1; 100033231e0eSKevin Wolf cpu_to_ube16(buf + 6, 0); 100133231e0eSKevin Wolf ide_atapi_cmd_reply(s, 8, max_len); 100233231e0eSKevin Wolf } 1003a60cf7e7SKevin Wolf 1004a60cf7e7SKevin Wolf static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf) 100533231e0eSKevin Wolf { 100633231e0eSKevin Wolf int format, msf, start_track, len; 1007a60cf7e7SKevin Wolf int max_len; 10087a2c4b82SKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 1009a60cf7e7SKevin Wolf 1010a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 7); 1011a60cf7e7SKevin Wolf format = buf[9] >> 6; 1012a60cf7e7SKevin Wolf msf = (buf[1] >> 1) & 1; 1013a60cf7e7SKevin Wolf start_track = buf[6]; 1014a60cf7e7SKevin Wolf 101533231e0eSKevin Wolf switch(format) { 101633231e0eSKevin Wolf case 0: 101733231e0eSKevin Wolf len = cdrom_read_toc(total_sectors, buf, msf, start_track); 101833231e0eSKevin Wolf if (len < 0) 101933231e0eSKevin Wolf goto error_cmd; 102033231e0eSKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 102133231e0eSKevin Wolf break; 102233231e0eSKevin Wolf case 1: 102333231e0eSKevin Wolf /* multi session : only a single session defined */ 102433231e0eSKevin Wolf memset(buf, 0, 12); 102533231e0eSKevin Wolf buf[1] = 0x0a; 102633231e0eSKevin Wolf buf[2] = 0x01; 102733231e0eSKevin Wolf buf[3] = 0x01; 102833231e0eSKevin Wolf ide_atapi_cmd_reply(s, 12, max_len); 102933231e0eSKevin Wolf break; 103033231e0eSKevin Wolf case 2: 103133231e0eSKevin Wolf len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track); 103233231e0eSKevin Wolf if (len < 0) 103333231e0eSKevin Wolf goto error_cmd; 103433231e0eSKevin Wolf ide_atapi_cmd_reply(s, len, max_len); 103533231e0eSKevin Wolf break; 103633231e0eSKevin Wolf default: 103733231e0eSKevin Wolf error_cmd: 103867cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 103933231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 104033231e0eSKevin Wolf } 104133231e0eSKevin Wolf } 1042a60cf7e7SKevin Wolf 1043a60cf7e7SKevin Wolf static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf) 104433231e0eSKevin Wolf { 1045e119bcacSKevin Wolf uint64_t total_sectors = s->nb_sectors >> 2; 104633231e0eSKevin Wolf 104733231e0eSKevin Wolf /* NOTE: it is really the number of sectors minus 1 */ 104833231e0eSKevin Wolf cpu_to_ube32(buf, total_sectors - 1); 104933231e0eSKevin Wolf cpu_to_ube32(buf + 4, 2048); 105033231e0eSKevin Wolf ide_atapi_cmd_reply(s, 8, 8); 105133231e0eSKevin Wolf } 1052a60cf7e7SKevin Wolf 105355042b95SPaolo Bonzini static void cmd_read_disc_information(IDEState *s, uint8_t* buf) 105455042b95SPaolo Bonzini { 105555042b95SPaolo Bonzini uint8_t type = buf[1] & 7; 105655042b95SPaolo Bonzini uint32_t max_len = ube16_to_cpu(buf + 7); 105755042b95SPaolo Bonzini 105855042b95SPaolo Bonzini /* Types 1/2 are only defined for Blu-Ray. */ 105955042b95SPaolo Bonzini if (type != 0) { 106055042b95SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 106155042b95SPaolo Bonzini ASC_INV_FIELD_IN_CMD_PACKET); 106255042b95SPaolo Bonzini return; 106355042b95SPaolo Bonzini } 106455042b95SPaolo Bonzini 106555042b95SPaolo Bonzini memset(buf, 0, 34); 106655042b95SPaolo Bonzini buf[1] = 32; 106755042b95SPaolo Bonzini buf[2] = 0xe; /* last session complete, disc finalized */ 106855042b95SPaolo Bonzini buf[3] = 1; /* first track on disc */ 106955042b95SPaolo Bonzini buf[4] = 1; /* # of sessions */ 107055042b95SPaolo Bonzini buf[5] = 1; /* first track of last session */ 107155042b95SPaolo Bonzini buf[6] = 1; /* last track of last session */ 107255042b95SPaolo Bonzini buf[7] = 0x20; /* unrestricted use */ 107355042b95SPaolo Bonzini buf[8] = 0x00; /* CD-ROM or DVD-ROM */ 107455042b95SPaolo Bonzini /* 9-10-11: most significant byte corresponding bytes 4-5-6 */ 107555042b95SPaolo Bonzini /* 12-23: not meaningful for CD-ROM or DVD-ROM */ 107655042b95SPaolo Bonzini /* 24-31: disc bar code */ 107755042b95SPaolo Bonzini /* 32: disc application code */ 107855042b95SPaolo Bonzini /* 33: number of OPC tables */ 107955042b95SPaolo Bonzini 108055042b95SPaolo Bonzini ide_atapi_cmd_reply(s, 34, max_len); 108155042b95SPaolo Bonzini } 108255042b95SPaolo Bonzini 1083a60cf7e7SKevin Wolf static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf) 108433231e0eSKevin Wolf { 1085a60cf7e7SKevin Wolf int max_len; 1086a60cf7e7SKevin Wolf int media = buf[1]; 1087a60cf7e7SKevin Wolf int format = buf[7]; 108833231e0eSKevin Wolf int ret; 108933231e0eSKevin Wolf 1090a60cf7e7SKevin Wolf max_len = ube16_to_cpu(buf + 8); 109133231e0eSKevin Wolf 109233231e0eSKevin Wolf if (format < 0xff) { 109333231e0eSKevin Wolf if (media_is_cd(s)) { 109467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 109533231e0eSKevin Wolf ASC_INCOMPATIBLE_FORMAT); 1096a60cf7e7SKevin Wolf return; 109733231e0eSKevin Wolf } else if (!media_present(s)) { 109867cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 109933231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 1100a60cf7e7SKevin Wolf return; 110133231e0eSKevin Wolf } 110233231e0eSKevin Wolf } 110333231e0eSKevin Wolf 110433231e0eSKevin Wolf memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ? 110533231e0eSKevin Wolf IDE_DMA_BUF_SECTORS * 512 + 4 : max_len); 110633231e0eSKevin Wolf 110733231e0eSKevin Wolf switch (format) { 110833231e0eSKevin Wolf case 0x00 ... 0x7f: 110933231e0eSKevin Wolf case 0xff: 111033231e0eSKevin Wolf if (media == 0) { 1111a60cf7e7SKevin Wolf ret = ide_dvd_read_structure(s, format, buf, buf); 111233231e0eSKevin Wolf 1113a60cf7e7SKevin Wolf if (ret < 0) { 111467cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret); 1115a60cf7e7SKevin Wolf } else { 111633231e0eSKevin Wolf ide_atapi_cmd_reply(s, ret, max_len); 1117a60cf7e7SKevin Wolf } 111833231e0eSKevin Wolf 111933231e0eSKevin Wolf break; 112033231e0eSKevin Wolf } 112133231e0eSKevin Wolf /* TODO: BD support, fall through for now */ 112233231e0eSKevin Wolf 112333231e0eSKevin Wolf /* Generic disk structures */ 112433231e0eSKevin Wolf case 0x80: /* TODO: AACS volume identifier */ 112533231e0eSKevin Wolf case 0x81: /* TODO: AACS media serial number */ 112633231e0eSKevin Wolf case 0x82: /* TODO: AACS media identifier */ 112733231e0eSKevin Wolf case 0x83: /* TODO: AACS media key block */ 112833231e0eSKevin Wolf case 0x90: /* TODO: List of recognized format layers */ 112933231e0eSKevin Wolf case 0xc0: /* TODO: Write protection status */ 113033231e0eSKevin Wolf default: 113167cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, 113233231e0eSKevin Wolf ASC_INV_FIELD_IN_CMD_PACKET); 113333231e0eSKevin Wolf break; 113433231e0eSKevin Wolf } 113533231e0eSKevin Wolf } 1136a60cf7e7SKevin Wolf 1137a60cf7e7SKevin Wolf static void cmd_set_speed(IDEState *s, uint8_t* buf) 1138a60cf7e7SKevin Wolf { 1139a60cf7e7SKevin Wolf ide_atapi_cmd_ok(s); 1140a60cf7e7SKevin Wolf } 1141a60cf7e7SKevin Wolf 1142e1a064f9SKevin Wolf enum { 1143e1a064f9SKevin Wolf /* 1144e1a064f9SKevin Wolf * Only commands flagged as ALLOW_UA are allowed to run under a 1145e1a064f9SKevin Wolf * unit attention condition. (See MMC-5, section 4.1.6.1) 1146e1a064f9SKevin Wolf */ 1147e1a064f9SKevin Wolf ALLOW_UA = 0x01, 11487a2c4b82SKevin Wolf 11497a2c4b82SKevin Wolf /* 11507a2c4b82SKevin Wolf * Commands flagged with CHECK_READY can only execute if a medium is present. 11517a2c4b82SKevin Wolf * Otherwise they report the Not Ready Condition. (See MMC-5, section 11527a2c4b82SKevin Wolf * 4.1.8) 11537a2c4b82SKevin Wolf */ 11547a2c4b82SKevin Wolf CHECK_READY = 0x02, 1155e1a064f9SKevin Wolf }; 1156e1a064f9SKevin Wolf 1157e1a064f9SKevin Wolf static const struct { 1158e1a064f9SKevin Wolf void (*handler)(IDEState *s, uint8_t *buf); 1159e1a064f9SKevin Wolf int flags; 1160e1a064f9SKevin Wolf } atapi_cmd_table[0x100] = { 11617a2c4b82SKevin Wolf [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY }, 1162e1a064f9SKevin Wolf [ 0x03 ] = { cmd_request_sense, ALLOW_UA }, 1163e1a064f9SKevin Wolf [ 0x12 ] = { cmd_inquiry, ALLOW_UA }, 1164a1aff5bfSMarkus Armbruster [ 0x1b ] = { cmd_start_stop_unit, 0 }, /* [1] */ 1165e1a064f9SKevin Wolf [ 0x1e ] = { cmd_prevent_allow_medium_removal, 0 }, 11667a2c4b82SKevin Wolf [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY }, 1167a1aff5bfSMarkus Armbruster [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY }, 11687a2c4b82SKevin Wolf [ 0x2b ] = { cmd_seek, CHECK_READY }, 11697a2c4b82SKevin Wolf [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY }, 1170e1a064f9SKevin Wolf [ 0x46 ] = { cmd_get_configuration, ALLOW_UA }, 1171e1a064f9SKevin Wolf [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA }, 117255042b95SPaolo Bonzini [ 0x51 ] = { cmd_read_disc_information, CHECK_READY }, 1173e1a064f9SKevin Wolf [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 }, 1174a1aff5bfSMarkus Armbruster [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY }, 1175a1aff5bfSMarkus Armbruster [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY }, 1176e1a064f9SKevin Wolf [ 0xbb ] = { cmd_set_speed, 0 }, 1177e1a064f9SKevin Wolf [ 0xbd ] = { cmd_mechanism_status, 0 }, 1178a1aff5bfSMarkus Armbruster [ 0xbe ] = { cmd_read_cd, CHECK_READY }, 1179a1aff5bfSMarkus Armbruster /* [1] handler detects and reports not ready condition itself */ 1180e1a064f9SKevin Wolf }; 1181e1a064f9SKevin Wolf 1182a60cf7e7SKevin Wolf void ide_atapi_cmd(IDEState *s) 1183a60cf7e7SKevin Wolf { 1184a60cf7e7SKevin Wolf uint8_t *buf; 1185a60cf7e7SKevin Wolf 1186a60cf7e7SKevin Wolf buf = s->io_buffer; 1187a60cf7e7SKevin Wolf #ifdef DEBUG_IDE_ATAPI 1188a60cf7e7SKevin Wolf { 1189a60cf7e7SKevin Wolf int i; 1190a60cf7e7SKevin Wolf printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8)); 1191a60cf7e7SKevin Wolf for(i = 0; i < ATAPI_PACKET_SIZE; i++) { 1192ab719827SAlon Levy printf(" %02x", buf[i]); 1193a60cf7e7SKevin Wolf } 1194a60cf7e7SKevin Wolf printf("\n"); 1195a60cf7e7SKevin Wolf } 1196a60cf7e7SKevin Wolf #endif 1197a60cf7e7SKevin Wolf /* 1198e1a064f9SKevin Wolf * If there's a UNIT_ATTENTION condition pending, only command flagged with 1199e1a064f9SKevin Wolf * ALLOW_UA are allowed to complete. with other commands getting a CHECK 1200e1a064f9SKevin Wolf * condition response unless a higher priority status, defined by the drive 1201a60cf7e7SKevin Wolf * here, is pending. 1202a60cf7e7SKevin Wolf */ 120367cc61e4SPaolo Bonzini if (s->sense_key == UNIT_ATTENTION && 1204e1a064f9SKevin Wolf !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) { 1205a60cf7e7SKevin Wolf ide_atapi_cmd_check_status(s); 1206a60cf7e7SKevin Wolf return; 1207a60cf7e7SKevin Wolf } 12084a737d14SAmit Shah /* 12094a737d14SAmit Shah * When a CD gets changed, we have to report an ejected state and 12104a737d14SAmit Shah * then a loaded state to guests so that they detect tray 12114a737d14SAmit Shah * open/close and media change events. Guests that do not use 12124a737d14SAmit Shah * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close 12134a737d14SAmit Shah * states rely on this behavior. 12144a737d14SAmit Shah */ 12150c6f08b0SPavel Hrdina if (!(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA) && 12164be74634SMarkus Armbruster !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) { 1217a60cf7e7SKevin Wolf 12180c6f08b0SPavel Hrdina if (s->cdrom_changed == 1) { 12190c6f08b0SPavel Hrdina ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT); 12200c6f08b0SPavel Hrdina s->cdrom_changed = 2; 12210c6f08b0SPavel Hrdina } else { 12220c6f08b0SPavel Hrdina ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED); 1223a60cf7e7SKevin Wolf s->cdrom_changed = 0; 12240c6f08b0SPavel Hrdina } 12250c6f08b0SPavel Hrdina 1226a60cf7e7SKevin Wolf return; 1227a60cf7e7SKevin Wolf } 1228e1a064f9SKevin Wolf 12297a2c4b82SKevin Wolf /* Report a Not Ready condition if appropriate for the command */ 12307a2c4b82SKevin Wolf if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) && 12314be74634SMarkus Armbruster (!media_present(s) || !blk_is_inserted(s->blk))) 12327a2c4b82SKevin Wolf { 123367cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT); 12347a2c4b82SKevin Wolf return; 12357a2c4b82SKevin Wolf } 12367a2c4b82SKevin Wolf 1237e1a064f9SKevin Wolf /* Execute the command */ 1238e1a064f9SKevin Wolf if (atapi_cmd_table[s->io_buffer[0]].handler) { 1239e1a064f9SKevin Wolf atapi_cmd_table[s->io_buffer[0]].handler(s, buf); 1240e1a064f9SKevin Wolf return; 124133231e0eSKevin Wolf } 1242e1a064f9SKevin Wolf 124367cc61e4SPaolo Bonzini ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE); 124433231e0eSKevin Wolf } 1245