Statistics
| Branch: | Revision:

root / hw / ide / atapi.c @ 098d314a

History | View | Annotate | Download (34.2 kB)

1 33231e0e Kevin Wolf
/*
2 33231e0e Kevin Wolf
 * QEMU ATAPI Emulation
3 33231e0e Kevin Wolf
 *
4 33231e0e Kevin Wolf
 * Copyright (c) 2003 Fabrice Bellard
5 33231e0e Kevin Wolf
 * Copyright (c) 2006 Openedhand Ltd.
6 33231e0e Kevin Wolf
 *
7 33231e0e Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 33231e0e Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
9 33231e0e Kevin Wolf
 * in the Software without restriction, including without limitation the rights
10 33231e0e Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 33231e0e Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
12 33231e0e Kevin Wolf
 * furnished to do so, subject to the following conditions:
13 33231e0e Kevin Wolf
 *
14 33231e0e Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
15 33231e0e Kevin Wolf
 * all copies or substantial portions of the Software.
16 33231e0e Kevin Wolf
 *
17 33231e0e Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 33231e0e Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 33231e0e Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 33231e0e Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 33231e0e Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 33231e0e Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 33231e0e Kevin Wolf
 * THE SOFTWARE.
24 33231e0e Kevin Wolf
 */
25 33231e0e Kevin Wolf
26 33231e0e Kevin Wolf
#include "hw/ide/internal.h"
27 33231e0e Kevin Wolf
#include "hw/scsi.h"
28 33231e0e Kevin Wolf
29 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
30 33231e0e Kevin Wolf
31 33231e0e Kevin Wolf
static void padstr8(uint8_t *buf, int buf_size, const char *src)
32 33231e0e Kevin Wolf
{
33 33231e0e Kevin Wolf
    int i;
34 33231e0e Kevin Wolf
    for(i = 0; i < buf_size; i++) {
35 33231e0e Kevin Wolf
        if (*src)
36 33231e0e Kevin Wolf
            buf[i] = *src++;
37 33231e0e Kevin Wolf
        else
38 33231e0e Kevin Wolf
            buf[i] = ' ';
39 33231e0e Kevin Wolf
    }
40 33231e0e Kevin Wolf
}
41 33231e0e Kevin Wolf
42 33231e0e Kevin Wolf
static inline void cpu_to_ube16(uint8_t *buf, int val)
43 33231e0e Kevin Wolf
{
44 33231e0e Kevin Wolf
    buf[0] = val >> 8;
45 33231e0e Kevin Wolf
    buf[1] = val & 0xff;
46 33231e0e Kevin Wolf
}
47 33231e0e Kevin Wolf
48 33231e0e Kevin Wolf
static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
49 33231e0e Kevin Wolf
{
50 33231e0e Kevin Wolf
    buf[0] = val >> 24;
51 33231e0e Kevin Wolf
    buf[1] = val >> 16;
52 33231e0e Kevin Wolf
    buf[2] = val >> 8;
53 33231e0e Kevin Wolf
    buf[3] = val & 0xff;
54 33231e0e Kevin Wolf
}
55 33231e0e Kevin Wolf
56 33231e0e Kevin Wolf
static inline int ube16_to_cpu(const uint8_t *buf)
57 33231e0e Kevin Wolf
{
58 33231e0e Kevin Wolf
    return (buf[0] << 8) | buf[1];
59 33231e0e Kevin Wolf
}
60 33231e0e Kevin Wolf
61 33231e0e Kevin Wolf
static inline int ube32_to_cpu(const uint8_t *buf)
62 33231e0e Kevin Wolf
{
63 33231e0e Kevin Wolf
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
64 33231e0e Kevin Wolf
}
65 33231e0e Kevin Wolf
66 33231e0e Kevin Wolf
static void lba_to_msf(uint8_t *buf, int lba)
67 33231e0e Kevin Wolf
{
68 33231e0e Kevin Wolf
    lba += 150;
69 33231e0e Kevin Wolf
    buf[0] = (lba / 75) / 60;
70 33231e0e Kevin Wolf
    buf[1] = (lba / 75) % 60;
71 33231e0e Kevin Wolf
    buf[2] = lba % 75;
72 33231e0e Kevin Wolf
}
73 33231e0e Kevin Wolf
74 33231e0e Kevin Wolf
static inline int media_present(IDEState *s)
75 33231e0e Kevin Wolf
{
76 a1aff5bf Markus Armbruster
    return !s->tray_open && s->nb_sectors > 0;
77 33231e0e Kevin Wolf
}
78 33231e0e Kevin Wolf
79 a7acf552 Amit Shah
/* XXX: DVDs that could fit on a CD will be reported as a CD */
80 33231e0e Kevin Wolf
static inline int media_is_dvd(IDEState *s)
81 33231e0e Kevin Wolf
{
82 33231e0e Kevin Wolf
    return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
83 33231e0e Kevin Wolf
}
84 33231e0e Kevin Wolf
85 33231e0e Kevin Wolf
static inline int media_is_cd(IDEState *s)
86 33231e0e Kevin Wolf
{
87 33231e0e Kevin Wolf
    return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
88 33231e0e Kevin Wolf
}
89 33231e0e Kevin Wolf
90 33231e0e Kevin Wolf
static void cd_data_to_raw(uint8_t *buf, int lba)
91 33231e0e Kevin Wolf
{
92 33231e0e Kevin Wolf
    /* sync bytes */
93 33231e0e Kevin Wolf
    buf[0] = 0x00;
94 33231e0e Kevin Wolf
    memset(buf + 1, 0xff, 10);
95 33231e0e Kevin Wolf
    buf[11] = 0x00;
96 33231e0e Kevin Wolf
    buf += 12;
97 33231e0e Kevin Wolf
    /* MSF */
98 33231e0e Kevin Wolf
    lba_to_msf(buf, lba);
99 33231e0e Kevin Wolf
    buf[3] = 0x01; /* mode 1 data */
100 33231e0e Kevin Wolf
    buf += 4;
101 33231e0e Kevin Wolf
    /* data */
102 33231e0e Kevin Wolf
    buf += 2048;
103 33231e0e Kevin Wolf
    /* XXX: ECC not computed */
104 33231e0e Kevin Wolf
    memset(buf, 0, 288);
105 33231e0e Kevin Wolf
}
106 33231e0e Kevin Wolf
107 a597e79c Christoph Hellwig
static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
108 33231e0e Kevin Wolf
{
109 33231e0e Kevin Wolf
    int ret;
110 33231e0e Kevin Wolf
111 33231e0e Kevin Wolf
    switch(sector_size) {
112 33231e0e Kevin Wolf
    case 2048:
113 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
114 a597e79c Christoph Hellwig
        ret = bdrv_read(s->bs, (int64_t)lba << 2, buf, 4);
115 a597e79c Christoph Hellwig
        bdrv_acct_done(s->bs, &s->acct);
116 33231e0e Kevin Wolf
        break;
117 33231e0e Kevin Wolf
    case 2352:
118 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
119 a597e79c Christoph Hellwig
        ret = bdrv_read(s->bs, (int64_t)lba << 2, buf + 16, 4);
120 a597e79c Christoph Hellwig
        bdrv_acct_done(s->bs, &s->acct);
121 33231e0e Kevin Wolf
        if (ret < 0)
122 33231e0e Kevin Wolf
            return ret;
123 33231e0e Kevin Wolf
        cd_data_to_raw(buf, lba);
124 33231e0e Kevin Wolf
        break;
125 33231e0e Kevin Wolf
    default:
126 33231e0e Kevin Wolf
        ret = -EIO;
127 33231e0e Kevin Wolf
        break;
128 33231e0e Kevin Wolf
    }
129 33231e0e Kevin Wolf
    return ret;
130 33231e0e Kevin Wolf
}
131 33231e0e Kevin Wolf
132 33231e0e Kevin Wolf
void ide_atapi_cmd_ok(IDEState *s)
133 33231e0e Kevin Wolf
{
134 33231e0e Kevin Wolf
    s->error = 0;
135 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT;
136 33231e0e Kevin Wolf
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
137 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
138 33231e0e Kevin Wolf
}
139 33231e0e Kevin Wolf
140 33231e0e Kevin Wolf
void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
141 33231e0e Kevin Wolf
{
142 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
143 33231e0e Kevin Wolf
    printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
144 33231e0e Kevin Wolf
#endif
145 33231e0e Kevin Wolf
    s->error = sense_key << 4;
146 33231e0e Kevin Wolf
    s->status = READY_STAT | ERR_STAT;
147 33231e0e Kevin Wolf
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
148 33231e0e Kevin Wolf
    s->sense_key = sense_key;
149 33231e0e Kevin Wolf
    s->asc = asc;
150 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
151 33231e0e Kevin Wolf
}
152 33231e0e Kevin Wolf
153 33231e0e Kevin Wolf
void ide_atapi_io_error(IDEState *s, int ret)
154 33231e0e Kevin Wolf
{
155 33231e0e Kevin Wolf
    /* XXX: handle more errors */
156 33231e0e Kevin Wolf
    if (ret == -ENOMEDIUM) {
157 33231e0e Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_NOT_READY,
158 33231e0e Kevin Wolf
                            ASC_MEDIUM_NOT_PRESENT);
159 33231e0e Kevin Wolf
    } else {
160 33231e0e Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
161 33231e0e Kevin Wolf
                            ASC_LOGICAL_BLOCK_OOR);
162 33231e0e Kevin Wolf
    }
163 33231e0e Kevin Wolf
}
164 33231e0e Kevin Wolf
165 33231e0e Kevin Wolf
/* The whole ATAPI transfer logic is handled in this function */
166 33231e0e Kevin Wolf
void ide_atapi_cmd_reply_end(IDEState *s)
167 33231e0e Kevin Wolf
{
168 33231e0e Kevin Wolf
    int byte_count_limit, size, ret;
169 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
170 33231e0e Kevin Wolf
    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
171 33231e0e Kevin Wolf
           s->packet_transfer_size,
172 33231e0e Kevin Wolf
           s->elementary_transfer_size,
173 33231e0e Kevin Wolf
           s->io_buffer_index);
174 33231e0e Kevin Wolf
#endif
175 33231e0e Kevin Wolf
    if (s->packet_transfer_size <= 0) {
176 33231e0e Kevin Wolf
        /* end of transfer */
177 33231e0e Kevin Wolf
        ide_transfer_stop(s);
178 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
179 33231e0e Kevin Wolf
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
180 33231e0e Kevin Wolf
        ide_set_irq(s->bus);
181 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
182 33231e0e Kevin Wolf
        printf("status=0x%x\n", s->status);
183 33231e0e Kevin Wolf
#endif
184 33231e0e Kevin Wolf
    } else {
185 33231e0e Kevin Wolf
        /* see if a new sector must be read */
186 33231e0e Kevin Wolf
        if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
187 a597e79c Christoph Hellwig
            ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
188 33231e0e Kevin Wolf
            if (ret < 0) {
189 33231e0e Kevin Wolf
                ide_transfer_stop(s);
190 33231e0e Kevin Wolf
                ide_atapi_io_error(s, ret);
191 33231e0e Kevin Wolf
                return;
192 33231e0e Kevin Wolf
            }
193 33231e0e Kevin Wolf
            s->lba++;
194 33231e0e Kevin Wolf
            s->io_buffer_index = 0;
195 33231e0e Kevin Wolf
        }
196 33231e0e Kevin Wolf
        if (s->elementary_transfer_size > 0) {
197 33231e0e Kevin Wolf
            /* there are some data left to transmit in this elementary
198 33231e0e Kevin Wolf
               transfer */
199 33231e0e Kevin Wolf
            size = s->cd_sector_size - s->io_buffer_index;
200 33231e0e Kevin Wolf
            if (size > s->elementary_transfer_size)
201 33231e0e Kevin Wolf
                size = s->elementary_transfer_size;
202 33231e0e Kevin Wolf
            s->packet_transfer_size -= size;
203 33231e0e Kevin Wolf
            s->elementary_transfer_size -= size;
204 33231e0e Kevin Wolf
            s->io_buffer_index += size;
205 33231e0e Kevin Wolf
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
206 33231e0e Kevin Wolf
                               size, ide_atapi_cmd_reply_end);
207 33231e0e Kevin Wolf
        } else {
208 33231e0e Kevin Wolf
            /* a new transfer is needed */
209 33231e0e Kevin Wolf
            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
210 33231e0e Kevin Wolf
            byte_count_limit = s->lcyl | (s->hcyl << 8);
211 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
212 33231e0e Kevin Wolf
            printf("byte_count_limit=%d\n", byte_count_limit);
213 33231e0e Kevin Wolf
#endif
214 33231e0e Kevin Wolf
            if (byte_count_limit == 0xffff)
215 33231e0e Kevin Wolf
                byte_count_limit--;
216 33231e0e Kevin Wolf
            size = s->packet_transfer_size;
217 33231e0e Kevin Wolf
            if (size > byte_count_limit) {
218 33231e0e Kevin Wolf
                /* byte count limit must be even if this case */
219 33231e0e Kevin Wolf
                if (byte_count_limit & 1)
220 33231e0e Kevin Wolf
                    byte_count_limit--;
221 33231e0e Kevin Wolf
                size = byte_count_limit;
222 33231e0e Kevin Wolf
            }
223 33231e0e Kevin Wolf
            s->lcyl = size;
224 33231e0e Kevin Wolf
            s->hcyl = size >> 8;
225 33231e0e Kevin Wolf
            s->elementary_transfer_size = size;
226 33231e0e Kevin Wolf
            /* we cannot transmit more than one sector at a time */
227 33231e0e Kevin Wolf
            if (s->lba != -1) {
228 33231e0e Kevin Wolf
                if (size > (s->cd_sector_size - s->io_buffer_index))
229 33231e0e Kevin Wolf
                    size = (s->cd_sector_size - s->io_buffer_index);
230 33231e0e Kevin Wolf
            }
231 33231e0e Kevin Wolf
            s->packet_transfer_size -= size;
232 33231e0e Kevin Wolf
            s->elementary_transfer_size -= size;
233 33231e0e Kevin Wolf
            s->io_buffer_index += size;
234 33231e0e Kevin Wolf
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
235 33231e0e Kevin Wolf
                               size, ide_atapi_cmd_reply_end);
236 33231e0e Kevin Wolf
            ide_set_irq(s->bus);
237 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
238 33231e0e Kevin Wolf
            printf("status=0x%x\n", s->status);
239 33231e0e Kevin Wolf
#endif
240 33231e0e Kevin Wolf
        }
241 33231e0e Kevin Wolf
    }
242 33231e0e Kevin Wolf
}
243 33231e0e Kevin Wolf
244 33231e0e Kevin Wolf
/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
245 33231e0e Kevin Wolf
static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
246 33231e0e Kevin Wolf
{
247 33231e0e Kevin Wolf
    if (size > max_size)
248 33231e0e Kevin Wolf
        size = max_size;
249 33231e0e Kevin Wolf
    s->lba = -1; /* no sector read */
250 33231e0e Kevin Wolf
    s->packet_transfer_size = size;
251 33231e0e Kevin Wolf
    s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
252 33231e0e Kevin Wolf
    s->elementary_transfer_size = 0;
253 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
254 33231e0e Kevin Wolf
255 33231e0e Kevin Wolf
    if (s->atapi_dma) {
256 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, size, BDRV_ACCT_READ);
257 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
258 33231e0e Kevin Wolf
        s->bus->dma->ops->start_dma(s->bus->dma, s,
259 33231e0e Kevin Wolf
                                   ide_atapi_cmd_read_dma_cb);
260 33231e0e Kevin Wolf
    } else {
261 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
262 33231e0e Kevin Wolf
        ide_atapi_cmd_reply_end(s);
263 33231e0e Kevin Wolf
    }
264 33231e0e Kevin Wolf
}
265 33231e0e Kevin Wolf
266 33231e0e Kevin Wolf
/* start a CD-CDROM read command */
267 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
268 33231e0e Kevin Wolf
                                   int sector_size)
269 33231e0e Kevin Wolf
{
270 33231e0e Kevin Wolf
    s->lba = lba;
271 33231e0e Kevin Wolf
    s->packet_transfer_size = nb_sectors * sector_size;
272 33231e0e Kevin Wolf
    s->elementary_transfer_size = 0;
273 33231e0e Kevin Wolf
    s->io_buffer_index = sector_size;
274 33231e0e Kevin Wolf
    s->cd_sector_size = sector_size;
275 33231e0e Kevin Wolf
276 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT;
277 33231e0e Kevin Wolf
    ide_atapi_cmd_reply_end(s);
278 33231e0e Kevin Wolf
}
279 33231e0e Kevin Wolf
280 33231e0e Kevin Wolf
static void ide_atapi_cmd_check_status(IDEState *s)
281 33231e0e Kevin Wolf
{
282 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
283 33231e0e Kevin Wolf
    printf("atapi_cmd_check_status\n");
284 33231e0e Kevin Wolf
#endif
285 33231e0e Kevin Wolf
    s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
286 33231e0e Kevin Wolf
    s->status = ERR_STAT;
287 33231e0e Kevin Wolf
    s->nsector = 0;
288 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
289 33231e0e Kevin Wolf
}
290 33231e0e Kevin Wolf
/* ATAPI DMA support */
291 33231e0e Kevin Wolf
292 33231e0e Kevin Wolf
/* XXX: handle read errors */
293 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
294 33231e0e Kevin Wolf
{
295 33231e0e Kevin Wolf
    IDEState *s = opaque;
296 33231e0e Kevin Wolf
    int data_offset, n;
297 33231e0e Kevin Wolf
298 33231e0e Kevin Wolf
    if (ret < 0) {
299 33231e0e Kevin Wolf
        ide_atapi_io_error(s, ret);
300 33231e0e Kevin Wolf
        goto eot;
301 33231e0e Kevin Wolf
    }
302 33231e0e Kevin Wolf
303 33231e0e Kevin Wolf
    if (s->io_buffer_size > 0) {
304 33231e0e Kevin Wolf
        /*
305 33231e0e Kevin Wolf
         * For a cdrom read sector command (s->lba != -1),
306 33231e0e Kevin Wolf
         * adjust the lba for the next s->io_buffer_size chunk
307 33231e0e Kevin Wolf
         * and dma the current chunk.
308 33231e0e Kevin Wolf
         * For a command != read (s->lba == -1), just transfer
309 33231e0e Kevin Wolf
         * the reply data.
310 33231e0e Kevin Wolf
         */
311 33231e0e Kevin Wolf
        if (s->lba != -1) {
312 33231e0e Kevin Wolf
            if (s->cd_sector_size == 2352) {
313 33231e0e Kevin Wolf
                n = 1;
314 33231e0e Kevin Wolf
                cd_data_to_raw(s->io_buffer, s->lba);
315 33231e0e Kevin Wolf
            } else {
316 33231e0e Kevin Wolf
                n = s->io_buffer_size >> 11;
317 33231e0e Kevin Wolf
            }
318 33231e0e Kevin Wolf
            s->lba += n;
319 33231e0e Kevin Wolf
        }
320 33231e0e Kevin Wolf
        s->packet_transfer_size -= s->io_buffer_size;
321 33231e0e Kevin Wolf
        if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
322 33231e0e Kevin Wolf
            goto eot;
323 33231e0e Kevin Wolf
    }
324 33231e0e Kevin Wolf
325 33231e0e Kevin Wolf
    if (s->packet_transfer_size <= 0) {
326 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
327 33231e0e Kevin Wolf
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
328 33231e0e Kevin Wolf
        ide_set_irq(s->bus);
329 a597e79c Christoph Hellwig
        goto eot;
330 33231e0e Kevin Wolf
    }
331 33231e0e Kevin Wolf
332 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
333 33231e0e Kevin Wolf
    if (s->cd_sector_size == 2352) {
334 33231e0e Kevin Wolf
        n = 1;
335 33231e0e Kevin Wolf
        s->io_buffer_size = s->cd_sector_size;
336 33231e0e Kevin Wolf
        data_offset = 16;
337 33231e0e Kevin Wolf
    } else {
338 33231e0e Kevin Wolf
        n = s->packet_transfer_size >> 11;
339 33231e0e Kevin Wolf
        if (n > (IDE_DMA_BUF_SECTORS / 4))
340 33231e0e Kevin Wolf
            n = (IDE_DMA_BUF_SECTORS / 4);
341 33231e0e Kevin Wolf
        s->io_buffer_size = n * 2048;
342 33231e0e Kevin Wolf
        data_offset = 0;
343 33231e0e Kevin Wolf
    }
344 33231e0e Kevin Wolf
#ifdef DEBUG_AIO
345 33231e0e Kevin Wolf
    printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
346 33231e0e Kevin Wolf
#endif
347 a597e79c Christoph Hellwig
348 33231e0e Kevin Wolf
    s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
349 33231e0e Kevin Wolf
    s->bus->dma->iov.iov_len = n * 4 * 512;
350 33231e0e Kevin Wolf
    qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
351 a597e79c Christoph Hellwig
352 33231e0e Kevin Wolf
    s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
353 33231e0e Kevin Wolf
                                       &s->bus->dma->qiov, n * 4,
354 33231e0e Kevin Wolf
                                       ide_atapi_cmd_read_dma_cb, s);
355 33231e0e Kevin Wolf
    if (!s->bus->dma->aiocb) {
356 33231e0e Kevin Wolf
        /* Note: media not present is the most likely case */
357 33231e0e Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_NOT_READY,
358 33231e0e Kevin Wolf
                            ASC_MEDIUM_NOT_PRESENT);
359 33231e0e Kevin Wolf
        goto eot;
360 33231e0e Kevin Wolf
    }
361 a597e79c Christoph Hellwig
362 a597e79c Christoph Hellwig
    return;
363 a597e79c Christoph Hellwig
eot:
364 a597e79c Christoph Hellwig
    bdrv_acct_done(s->bs, &s->acct);
365 a597e79c Christoph Hellwig
    s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
366 a597e79c Christoph Hellwig
    ide_set_inactive(s);
367 33231e0e Kevin Wolf
}
368 33231e0e Kevin Wolf
369 33231e0e Kevin Wolf
/* start a CD-CDROM read command with DMA */
370 33231e0e Kevin Wolf
/* XXX: test if DMA is available */
371 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
372 33231e0e Kevin Wolf
                                   int sector_size)
373 33231e0e Kevin Wolf
{
374 33231e0e Kevin Wolf
    s->lba = lba;
375 33231e0e Kevin Wolf
    s->packet_transfer_size = nb_sectors * sector_size;
376 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
377 33231e0e Kevin Wolf
    s->io_buffer_size = 0;
378 33231e0e Kevin Wolf
    s->cd_sector_size = sector_size;
379 33231e0e Kevin Wolf
380 a597e79c Christoph Hellwig
    bdrv_acct_start(s->bs, &s->acct, s->packet_transfer_size, BDRV_ACCT_READ);
381 a597e79c Christoph Hellwig
382 33231e0e Kevin Wolf
    /* XXX: check if BUSY_STAT should be set */
383 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
384 33231e0e Kevin Wolf
    s->bus->dma->ops->start_dma(s->bus->dma, s,
385 33231e0e Kevin Wolf
                               ide_atapi_cmd_read_dma_cb);
386 33231e0e Kevin Wolf
}
387 33231e0e Kevin Wolf
388 33231e0e Kevin Wolf
static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
389 33231e0e Kevin Wolf
                               int sector_size)
390 33231e0e Kevin Wolf
{
391 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
392 33231e0e Kevin Wolf
    printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
393 33231e0e Kevin Wolf
        lba, nb_sectors);
394 33231e0e Kevin Wolf
#endif
395 33231e0e Kevin Wolf
    if (s->atapi_dma) {
396 33231e0e Kevin Wolf
        ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
397 33231e0e Kevin Wolf
    } else {
398 33231e0e Kevin Wolf
        ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
399 33231e0e Kevin Wolf
    }
400 33231e0e Kevin Wolf
}
401 33231e0e Kevin Wolf
402 33231e0e Kevin Wolf
static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
403 33231e0e Kevin Wolf
                                            uint16_t profile)
404 33231e0e Kevin Wolf
{
405 33231e0e Kevin Wolf
    uint8_t *buf_profile = buf + 12; /* start of profiles */
406 33231e0e Kevin Wolf
407 33231e0e Kevin Wolf
    buf_profile += ((*index) * 4); /* start of indexed profile */
408 33231e0e Kevin Wolf
    cpu_to_ube16 (buf_profile, profile);
409 33231e0e Kevin Wolf
    buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
410 33231e0e Kevin Wolf
411 33231e0e Kevin Wolf
    /* each profile adds 4 bytes to the response */
412 33231e0e Kevin Wolf
    (*index)++;
413 33231e0e Kevin Wolf
    buf[11] += 4; /* Additional Length */
414 33231e0e Kevin Wolf
415 33231e0e Kevin Wolf
    return 4;
416 33231e0e Kevin Wolf
}
417 33231e0e Kevin Wolf
418 33231e0e Kevin Wolf
static int ide_dvd_read_structure(IDEState *s, int format,
419 33231e0e Kevin Wolf
                                  const uint8_t *packet, uint8_t *buf)
420 33231e0e Kevin Wolf
{
421 33231e0e Kevin Wolf
    switch (format) {
422 33231e0e Kevin Wolf
        case 0x0: /* Physical format information */
423 33231e0e Kevin Wolf
            {
424 33231e0e Kevin Wolf
                int layer = packet[6];
425 33231e0e Kevin Wolf
                uint64_t total_sectors;
426 33231e0e Kevin Wolf
427 33231e0e Kevin Wolf
                if (layer != 0)
428 33231e0e Kevin Wolf
                    return -ASC_INV_FIELD_IN_CMD_PACKET;
429 33231e0e Kevin Wolf
430 e119bcac Kevin Wolf
                total_sectors = s->nb_sectors >> 2;
431 e119bcac Kevin Wolf
                if (total_sectors == 0) {
432 33231e0e Kevin Wolf
                    return -ASC_MEDIUM_NOT_PRESENT;
433 e119bcac Kevin Wolf
                }
434 33231e0e Kevin Wolf
435 33231e0e Kevin Wolf
                buf[4] = 1;   /* DVD-ROM, part version 1 */
436 33231e0e Kevin Wolf
                buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
437 33231e0e Kevin Wolf
                buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
438 33231e0e Kevin Wolf
                buf[7] = 0;   /* default densities */
439 33231e0e Kevin Wolf
440 33231e0e Kevin Wolf
                /* FIXME: 0x30000 per spec? */
441 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 8, 0); /* start sector */
442 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
443 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
444 33231e0e Kevin Wolf
445 33231e0e Kevin Wolf
                /* Size of buffer, not including 2 byte size field */
446 33231e0e Kevin Wolf
                cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
447 33231e0e Kevin Wolf
448 33231e0e Kevin Wolf
                /* 2k data + 4 byte header */
449 33231e0e Kevin Wolf
                return (2048 + 4);
450 33231e0e Kevin Wolf
            }
451 33231e0e Kevin Wolf
452 33231e0e Kevin Wolf
        case 0x01: /* DVD copyright information */
453 33231e0e Kevin Wolf
            buf[4] = 0; /* no copyright data */
454 33231e0e Kevin Wolf
            buf[5] = 0; /* no region restrictions */
455 33231e0e Kevin Wolf
456 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
457 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 4 + 2);
458 33231e0e Kevin Wolf
459 33231e0e Kevin Wolf
            /* 4 byte header + 4 byte data */
460 33231e0e Kevin Wolf
            return (4 + 4);
461 33231e0e Kevin Wolf
462 33231e0e Kevin Wolf
        case 0x03: /* BCA information - invalid field for no BCA info */
463 33231e0e Kevin Wolf
            return -ASC_INV_FIELD_IN_CMD_PACKET;
464 33231e0e Kevin Wolf
465 33231e0e Kevin Wolf
        case 0x04: /* DVD disc manufacturing information */
466 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
467 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
468 33231e0e Kevin Wolf
469 33231e0e Kevin Wolf
            /* 2k data + 4 byte header */
470 33231e0e Kevin Wolf
            return (2048 + 4);
471 33231e0e Kevin Wolf
472 33231e0e Kevin Wolf
        case 0xff:
473 33231e0e Kevin Wolf
            /*
474 33231e0e Kevin Wolf
             * This lists all the command capabilities above.  Add new ones
475 33231e0e Kevin Wolf
             * in order and update the length and buffer return values.
476 33231e0e Kevin Wolf
             */
477 33231e0e Kevin Wolf
478 33231e0e Kevin Wolf
            buf[4] = 0x00; /* Physical format */
479 33231e0e Kevin Wolf
            buf[5] = 0x40; /* Not writable, is readable */
480 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
481 33231e0e Kevin Wolf
482 33231e0e Kevin Wolf
            buf[8] = 0x01; /* Copyright info */
483 33231e0e Kevin Wolf
            buf[9] = 0x40; /* Not writable, is readable */
484 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
485 33231e0e Kevin Wolf
486 33231e0e Kevin Wolf
            buf[12] = 0x03; /* BCA info */
487 33231e0e Kevin Wolf
            buf[13] = 0x40; /* Not writable, is readable */
488 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
489 33231e0e Kevin Wolf
490 33231e0e Kevin Wolf
            buf[16] = 0x04; /* Manufacturing info */
491 33231e0e Kevin Wolf
            buf[17] = 0x40; /* Not writable, is readable */
492 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
493 33231e0e Kevin Wolf
494 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
495 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 16 + 2);
496 33231e0e Kevin Wolf
497 33231e0e Kevin Wolf
            /* data written + 4 byte header */
498 33231e0e Kevin Wolf
            return (16 + 4);
499 33231e0e Kevin Wolf
500 33231e0e Kevin Wolf
        default: /* TODO: formats beyond DVD-ROM requires */
501 33231e0e Kevin Wolf
            return -ASC_INV_FIELD_IN_CMD_PACKET;
502 33231e0e Kevin Wolf
    }
503 33231e0e Kevin Wolf
}
504 33231e0e Kevin Wolf
505 33231e0e Kevin Wolf
static unsigned int event_status_media(IDEState *s,
506 33231e0e Kevin Wolf
                                       uint8_t *buf)
507 33231e0e Kevin Wolf
{
508 33231e0e Kevin Wolf
    enum media_event_code {
509 33231e0e Kevin Wolf
        MEC_NO_CHANGE = 0,       /* Status unchanged */
510 33231e0e Kevin Wolf
        MEC_EJECT_REQUESTED,     /* received a request from user to eject */
511 33231e0e Kevin Wolf
        MEC_NEW_MEDIA,           /* new media inserted and ready for access */
512 33231e0e Kevin Wolf
        MEC_MEDIA_REMOVAL,       /* only for media changers */
513 33231e0e Kevin Wolf
        MEC_MEDIA_CHANGED,       /* only for media changers */
514 33231e0e Kevin Wolf
        MEC_BG_FORMAT_COMPLETED, /* MRW or DVD+RW b/g format completed */
515 33231e0e Kevin Wolf
        MEC_BG_FORMAT_RESTARTED, /* MRW or DVD+RW b/g format restarted */
516 33231e0e Kevin Wolf
    };
517 33231e0e Kevin Wolf
    enum media_status {
518 33231e0e Kevin Wolf
        MS_TRAY_OPEN = 1,
519 33231e0e Kevin Wolf
        MS_MEDIA_PRESENT = 2,
520 33231e0e Kevin Wolf
    };
521 33231e0e Kevin Wolf
    uint8_t event_code, media_status;
522 33231e0e Kevin Wolf
523 33231e0e Kevin Wolf
    media_status = 0;
524 dd063333 Markus Armbruster
    if (s->tray_open) {
525 33231e0e Kevin Wolf
        media_status = MS_TRAY_OPEN;
526 33231e0e Kevin Wolf
    } else if (bdrv_is_inserted(s->bs)) {
527 33231e0e Kevin Wolf
        media_status = MS_MEDIA_PRESENT;
528 33231e0e Kevin Wolf
    }
529 33231e0e Kevin Wolf
530 33231e0e Kevin Wolf
    /* Event notification descriptor */
531 33231e0e Kevin Wolf
    event_code = MEC_NO_CHANGE;
532 33231e0e Kevin Wolf
    if (media_status != MS_TRAY_OPEN && s->events.new_media) {
533 33231e0e Kevin Wolf
        event_code = MEC_NEW_MEDIA;
534 33231e0e Kevin Wolf
        s->events.new_media = false;
535 33231e0e Kevin Wolf
    }
536 33231e0e Kevin Wolf
537 33231e0e Kevin Wolf
    buf[4] = event_code;
538 33231e0e Kevin Wolf
    buf[5] = media_status;
539 33231e0e Kevin Wolf
540 33231e0e Kevin Wolf
    /* These fields are reserved, just clear them. */
541 33231e0e Kevin Wolf
    buf[6] = 0;
542 33231e0e Kevin Wolf
    buf[7] = 0;
543 33231e0e Kevin Wolf
544 33231e0e Kevin Wolf
    return 8; /* We wrote to 4 extra bytes from the header */
545 33231e0e Kevin Wolf
}
546 33231e0e Kevin Wolf
547 e1a064f9 Kevin Wolf
static void cmd_get_event_status_notification(IDEState *s,
548 e1a064f9 Kevin Wolf
                                              uint8_t *buf)
549 33231e0e Kevin Wolf
{
550 e1a064f9 Kevin Wolf
    const uint8_t *packet = buf;
551 e1a064f9 Kevin Wolf
552 33231e0e Kevin Wolf
    struct {
553 33231e0e Kevin Wolf
        uint8_t opcode;
554 33231e0e Kevin Wolf
        uint8_t polled;        /* lsb bit is polled; others are reserved */
555 33231e0e Kevin Wolf
        uint8_t reserved2[2];
556 33231e0e Kevin Wolf
        uint8_t class;
557 33231e0e Kevin Wolf
        uint8_t reserved3[2];
558 33231e0e Kevin Wolf
        uint16_t len;
559 33231e0e Kevin Wolf
        uint8_t control;
560 541dc0d4 Stefan Weil
    } QEMU_PACKED *gesn_cdb;
561 33231e0e Kevin Wolf
562 33231e0e Kevin Wolf
    struct {
563 33231e0e Kevin Wolf
        uint16_t len;
564 33231e0e Kevin Wolf
        uint8_t notification_class;
565 33231e0e Kevin Wolf
        uint8_t supported_events;
566 541dc0d4 Stefan Weil
    } QEMU_PACKED *gesn_event_header;
567 33231e0e Kevin Wolf
568 33231e0e Kevin Wolf
    enum notification_class_request_type {
569 33231e0e Kevin Wolf
        NCR_RESERVED1 = 1 << 0,
570 33231e0e Kevin Wolf
        NCR_OPERATIONAL_CHANGE = 1 << 1,
571 33231e0e Kevin Wolf
        NCR_POWER_MANAGEMENT = 1 << 2,
572 33231e0e Kevin Wolf
        NCR_EXTERNAL_REQUEST = 1 << 3,
573 33231e0e Kevin Wolf
        NCR_MEDIA = 1 << 4,
574 33231e0e Kevin Wolf
        NCR_MULTI_HOST = 1 << 5,
575 33231e0e Kevin Wolf
        NCR_DEVICE_BUSY = 1 << 6,
576 33231e0e Kevin Wolf
        NCR_RESERVED2 = 1 << 7,
577 33231e0e Kevin Wolf
    };
578 33231e0e Kevin Wolf
    enum event_notification_class_field {
579 33231e0e Kevin Wolf
        ENC_NO_EVENTS = 0,
580 33231e0e Kevin Wolf
        ENC_OPERATIONAL_CHANGE,
581 33231e0e Kevin Wolf
        ENC_POWER_MANAGEMENT,
582 33231e0e Kevin Wolf
        ENC_EXTERNAL_REQUEST,
583 33231e0e Kevin Wolf
        ENC_MEDIA,
584 33231e0e Kevin Wolf
        ENC_MULTIPLE_HOSTS,
585 33231e0e Kevin Wolf
        ENC_DEVICE_BUSY,
586 33231e0e Kevin Wolf
        ENC_RESERVED,
587 33231e0e Kevin Wolf
    };
588 33231e0e Kevin Wolf
    unsigned int max_len, used_len;
589 33231e0e Kevin Wolf
590 33231e0e Kevin Wolf
    gesn_cdb = (void *)packet;
591 33231e0e Kevin Wolf
    gesn_event_header = (void *)buf;
592 33231e0e Kevin Wolf
593 33231e0e Kevin Wolf
    max_len = be16_to_cpu(gesn_cdb->len);
594 33231e0e Kevin Wolf
595 33231e0e Kevin Wolf
    /* It is fine by the MMC spec to not support async mode operations */
596 33231e0e Kevin Wolf
    if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
597 33231e0e Kevin Wolf
        /* Only polling is supported, asynchronous mode is not. */
598 33231e0e Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
599 33231e0e Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
600 33231e0e Kevin Wolf
        return;
601 33231e0e Kevin Wolf
    }
602 33231e0e Kevin Wolf
603 33231e0e Kevin Wolf
    /* polling mode operation */
604 33231e0e Kevin Wolf
605 33231e0e Kevin Wolf
    /*
606 33231e0e Kevin Wolf
     * These are the supported events.
607 33231e0e Kevin Wolf
     *
608 33231e0e Kevin Wolf
     * We currently only support requests of the 'media' type.
609 33231e0e Kevin Wolf
     */
610 33231e0e Kevin Wolf
    gesn_event_header->supported_events = NCR_MEDIA;
611 33231e0e Kevin Wolf
612 33231e0e Kevin Wolf
    /*
613 33231e0e Kevin Wolf
     * We use |= below to set the class field; other bits in this byte
614 33231e0e Kevin Wolf
     * are reserved now but this is useful to do if we have to use the
615 33231e0e Kevin Wolf
     * reserved fields later.
616 33231e0e Kevin Wolf
     */
617 33231e0e Kevin Wolf
    gesn_event_header->notification_class = 0;
618 33231e0e Kevin Wolf
619 33231e0e Kevin Wolf
    /*
620 33231e0e Kevin Wolf
     * Responses to requests are to be based on request priority.  The
621 33231e0e Kevin Wolf
     * notification_class_request_type enum above specifies the
622 33231e0e Kevin Wolf
     * priority: upper elements are higher prio than lower ones.
623 33231e0e Kevin Wolf
     */
624 33231e0e Kevin Wolf
    if (gesn_cdb->class & NCR_MEDIA) {
625 33231e0e Kevin Wolf
        gesn_event_header->notification_class |= ENC_MEDIA;
626 33231e0e Kevin Wolf
        used_len = event_status_media(s, buf);
627 33231e0e Kevin Wolf
    } else {
628 33231e0e Kevin Wolf
        gesn_event_header->notification_class = 0x80; /* No event available */
629 33231e0e Kevin Wolf
        used_len = sizeof(*gesn_event_header);
630 33231e0e Kevin Wolf
    }
631 33231e0e Kevin Wolf
    gesn_event_header->len = cpu_to_be16(used_len
632 33231e0e Kevin Wolf
                                         - sizeof(*gesn_event_header));
633 33231e0e Kevin Wolf
    ide_atapi_cmd_reply(s, used_len, max_len);
634 33231e0e Kevin Wolf
}
635 33231e0e Kevin Wolf
636 a60cf7e7 Kevin Wolf
static void cmd_request_sense(IDEState *s, uint8_t *buf)
637 a60cf7e7 Kevin Wolf
{
638 a60cf7e7 Kevin Wolf
    int max_len = buf[4];
639 a60cf7e7 Kevin Wolf
640 a60cf7e7 Kevin Wolf
    memset(buf, 0, 18);
641 a60cf7e7 Kevin Wolf
    buf[0] = 0x70 | (1 << 7);
642 a60cf7e7 Kevin Wolf
    buf[2] = s->sense_key;
643 a60cf7e7 Kevin Wolf
    buf[7] = 10;
644 a60cf7e7 Kevin Wolf
    buf[12] = s->asc;
645 a60cf7e7 Kevin Wolf
646 a60cf7e7 Kevin Wolf
    if (s->sense_key == SENSE_UNIT_ATTENTION) {
647 a60cf7e7 Kevin Wolf
        s->sense_key = SENSE_NONE;
648 a60cf7e7 Kevin Wolf
    }
649 a60cf7e7 Kevin Wolf
650 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 18, max_len);
651 a60cf7e7 Kevin Wolf
}
652 a60cf7e7 Kevin Wolf
653 a60cf7e7 Kevin Wolf
static void cmd_inquiry(IDEState *s, uint8_t *buf)
654 a60cf7e7 Kevin Wolf
{
655 a60cf7e7 Kevin Wolf
    int max_len = buf[4];
656 a60cf7e7 Kevin Wolf
657 a60cf7e7 Kevin Wolf
    buf[0] = 0x05; /* CD-ROM */
658 a60cf7e7 Kevin Wolf
    buf[1] = 0x80; /* removable */
659 a60cf7e7 Kevin Wolf
    buf[2] = 0x00; /* ISO */
660 a60cf7e7 Kevin Wolf
    buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
661 a60cf7e7 Kevin Wolf
    buf[4] = 31; /* additional length */
662 a60cf7e7 Kevin Wolf
    buf[5] = 0; /* reserved */
663 a60cf7e7 Kevin Wolf
    buf[6] = 0; /* reserved */
664 a60cf7e7 Kevin Wolf
    buf[7] = 0; /* reserved */
665 a60cf7e7 Kevin Wolf
    padstr8(buf + 8, 8, "QEMU");
666 a60cf7e7 Kevin Wolf
    padstr8(buf + 16, 16, "QEMU DVD-ROM");
667 a60cf7e7 Kevin Wolf
    padstr8(buf + 32, 4, s->version);
668 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 36, max_len);
669 a60cf7e7 Kevin Wolf
}
670 a60cf7e7 Kevin Wolf
671 a60cf7e7 Kevin Wolf
static void cmd_get_configuration(IDEState *s, uint8_t *buf)
672 a60cf7e7 Kevin Wolf
{
673 a60cf7e7 Kevin Wolf
    uint32_t len;
674 a60cf7e7 Kevin Wolf
    uint8_t index = 0;
675 a60cf7e7 Kevin Wolf
    int max_len;
676 a60cf7e7 Kevin Wolf
677 a60cf7e7 Kevin Wolf
    /* only feature 0 is supported */
678 a60cf7e7 Kevin Wolf
    if (buf[2] != 0 || buf[3] != 0) {
679 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
680 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
681 a60cf7e7 Kevin Wolf
        return;
682 a60cf7e7 Kevin Wolf
    }
683 a60cf7e7 Kevin Wolf
684 a60cf7e7 Kevin Wolf
    /* XXX: could result in alignment problems in some architectures */
685 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 7);
686 a60cf7e7 Kevin Wolf
687 a60cf7e7 Kevin Wolf
    /*
688 a60cf7e7 Kevin Wolf
     * XXX: avoid overflow for io_buffer if max_len is bigger than
689 a60cf7e7 Kevin Wolf
     *      the size of that buffer (dimensioned to max number of
690 a60cf7e7 Kevin Wolf
     *      sectors to transfer at once)
691 a60cf7e7 Kevin Wolf
     *
692 a60cf7e7 Kevin Wolf
     *      Only a problem if the feature/profiles grow.
693 a60cf7e7 Kevin Wolf
     */
694 a60cf7e7 Kevin Wolf
    if (max_len > 512) {
695 a60cf7e7 Kevin Wolf
        /* XXX: assume 1 sector */
696 a60cf7e7 Kevin Wolf
        max_len = 512;
697 a60cf7e7 Kevin Wolf
    }
698 a60cf7e7 Kevin Wolf
699 a60cf7e7 Kevin Wolf
    memset(buf, 0, max_len);
700 a60cf7e7 Kevin Wolf
    /*
701 a60cf7e7 Kevin Wolf
     * the number of sectors from the media tells us which profile
702 a60cf7e7 Kevin Wolf
     * to use as current.  0 means there is no media
703 a60cf7e7 Kevin Wolf
     */
704 a60cf7e7 Kevin Wolf
    if (media_is_dvd(s)) {
705 a60cf7e7 Kevin Wolf
        cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
706 a60cf7e7 Kevin Wolf
    } else if (media_is_cd(s)) {
707 a60cf7e7 Kevin Wolf
        cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
708 a60cf7e7 Kevin Wolf
    }
709 a60cf7e7 Kevin Wolf
710 a60cf7e7 Kevin Wolf
    buf[10] = 0x02 | 0x01; /* persistent and current */
711 a60cf7e7 Kevin Wolf
    len = 12; /* headers: 8 + 4 */
712 a60cf7e7 Kevin Wolf
    len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
713 a60cf7e7 Kevin Wolf
    len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
714 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf, len - 4); /* data length */
715 a60cf7e7 Kevin Wolf
716 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, len, max_len);
717 a60cf7e7 Kevin Wolf
}
718 a60cf7e7 Kevin Wolf
719 a60cf7e7 Kevin Wolf
static void cmd_mode_sense(IDEState *s, uint8_t *buf)
720 a60cf7e7 Kevin Wolf
{
721 a60cf7e7 Kevin Wolf
    int action, code;
722 a60cf7e7 Kevin Wolf
    int max_len;
723 a60cf7e7 Kevin Wolf
724 a60cf7e7 Kevin Wolf
    if (buf[0] == GPCMD_MODE_SENSE_10) {
725 a60cf7e7 Kevin Wolf
        max_len = ube16_to_cpu(buf + 7);
726 a60cf7e7 Kevin Wolf
    } else {
727 a60cf7e7 Kevin Wolf
        max_len = buf[4];
728 a60cf7e7 Kevin Wolf
    }
729 a60cf7e7 Kevin Wolf
730 a60cf7e7 Kevin Wolf
    action = buf[2] >> 6;
731 a60cf7e7 Kevin Wolf
    code = buf[2] & 0x3f;
732 a60cf7e7 Kevin Wolf
733 a60cf7e7 Kevin Wolf
    switch(action) {
734 a60cf7e7 Kevin Wolf
    case 0: /* current values */
735 a60cf7e7 Kevin Wolf
        switch(code) {
736 a60cf7e7 Kevin Wolf
        case GPMODE_R_W_ERROR_PAGE: /* error recovery */
737 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[0], 16 + 6);
738 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
739 a60cf7e7 Kevin Wolf
            buf[3] = 0;
740 a60cf7e7 Kevin Wolf
            buf[4] = 0;
741 a60cf7e7 Kevin Wolf
            buf[5] = 0;
742 a60cf7e7 Kevin Wolf
            buf[6] = 0;
743 a60cf7e7 Kevin Wolf
            buf[7] = 0;
744 a60cf7e7 Kevin Wolf
745 a60cf7e7 Kevin Wolf
            buf[8] = 0x01;
746 a60cf7e7 Kevin Wolf
            buf[9] = 0x06;
747 a60cf7e7 Kevin Wolf
            buf[10] = 0x00;
748 a60cf7e7 Kevin Wolf
            buf[11] = 0x05;
749 a60cf7e7 Kevin Wolf
            buf[12] = 0x00;
750 a60cf7e7 Kevin Wolf
            buf[13] = 0x00;
751 a60cf7e7 Kevin Wolf
            buf[14] = 0x00;
752 a60cf7e7 Kevin Wolf
            buf[15] = 0x00;
753 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_reply(s, 16, max_len);
754 a60cf7e7 Kevin Wolf
            break;
755 a60cf7e7 Kevin Wolf
        case GPMODE_AUDIO_CTL_PAGE:
756 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[0], 24 + 6);
757 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
758 a60cf7e7 Kevin Wolf
            buf[3] = 0;
759 a60cf7e7 Kevin Wolf
            buf[4] = 0;
760 a60cf7e7 Kevin Wolf
            buf[5] = 0;
761 a60cf7e7 Kevin Wolf
            buf[6] = 0;
762 a60cf7e7 Kevin Wolf
            buf[7] = 0;
763 a60cf7e7 Kevin Wolf
764 a60cf7e7 Kevin Wolf
            /* Fill with CDROM audio volume */
765 a60cf7e7 Kevin Wolf
            buf[17] = 0;
766 a60cf7e7 Kevin Wolf
            buf[19] = 0;
767 a60cf7e7 Kevin Wolf
            buf[21] = 0;
768 a60cf7e7 Kevin Wolf
            buf[23] = 0;
769 a60cf7e7 Kevin Wolf
770 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_reply(s, 24, max_len);
771 a60cf7e7 Kevin Wolf
            break;
772 a60cf7e7 Kevin Wolf
        case GPMODE_CAPABILITIES_PAGE:
773 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[0], 28 + 6);
774 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
775 a60cf7e7 Kevin Wolf
            buf[3] = 0;
776 a60cf7e7 Kevin Wolf
            buf[4] = 0;
777 a60cf7e7 Kevin Wolf
            buf[5] = 0;
778 a60cf7e7 Kevin Wolf
            buf[6] = 0;
779 a60cf7e7 Kevin Wolf
            buf[7] = 0;
780 a60cf7e7 Kevin Wolf
781 a60cf7e7 Kevin Wolf
            buf[8] = 0x2a;
782 a60cf7e7 Kevin Wolf
            buf[9] = 0x12;
783 a60cf7e7 Kevin Wolf
            buf[10] = 0x00;
784 a60cf7e7 Kevin Wolf
            buf[11] = 0x00;
785 a60cf7e7 Kevin Wolf
786 a60cf7e7 Kevin Wolf
            /* Claim PLAY_AUDIO capability (0x01) since some Linux
787 a60cf7e7 Kevin Wolf
               code checks for this to automount media. */
788 a60cf7e7 Kevin Wolf
            buf[12] = 0x71;
789 a60cf7e7 Kevin Wolf
            buf[13] = 3 << 5;
790 a60cf7e7 Kevin Wolf
            buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
791 a0a7573b Markus Armbruster
            if (s->tray_locked) {
792 a60cf7e7 Kevin Wolf
                buf[6] |= 1 << 1;
793 a0a7573b Markus Armbruster
            }
794 a60cf7e7 Kevin Wolf
            buf[15] = 0x00;
795 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[16], 706);
796 a60cf7e7 Kevin Wolf
            buf[18] = 0;
797 a60cf7e7 Kevin Wolf
            buf[19] = 2;
798 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[20], 512);
799 a60cf7e7 Kevin Wolf
            cpu_to_ube16(&buf[22], 706);
800 a60cf7e7 Kevin Wolf
            buf[24] = 0;
801 a60cf7e7 Kevin Wolf
            buf[25] = 0;
802 a60cf7e7 Kevin Wolf
            buf[26] = 0;
803 a60cf7e7 Kevin Wolf
            buf[27] = 0;
804 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_reply(s, 28, max_len);
805 a60cf7e7 Kevin Wolf
            break;
806 a60cf7e7 Kevin Wolf
        default:
807 a60cf7e7 Kevin Wolf
            goto error_cmd;
808 a60cf7e7 Kevin Wolf
        }
809 a60cf7e7 Kevin Wolf
        break;
810 a60cf7e7 Kevin Wolf
    case 1: /* changeable values */
811 a60cf7e7 Kevin Wolf
        goto error_cmd;
812 a60cf7e7 Kevin Wolf
    case 2: /* default values */
813 a60cf7e7 Kevin Wolf
        goto error_cmd;
814 a60cf7e7 Kevin Wolf
    default:
815 a60cf7e7 Kevin Wolf
    case 3: /* saved values */
816 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
817 a60cf7e7 Kevin Wolf
                            ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
818 a60cf7e7 Kevin Wolf
        break;
819 a60cf7e7 Kevin Wolf
    }
820 a60cf7e7 Kevin Wolf
    return;
821 a60cf7e7 Kevin Wolf
822 a60cf7e7 Kevin Wolf
error_cmd:
823 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
824 a60cf7e7 Kevin Wolf
}
825 a60cf7e7 Kevin Wolf
826 a60cf7e7 Kevin Wolf
static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
827 a60cf7e7 Kevin Wolf
{
828 7a2c4b82 Kevin Wolf
    /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
829 7a2c4b82 Kevin Wolf
     * come here, we know that it's ready. */
830 7a2c4b82 Kevin Wolf
    ide_atapi_cmd_ok(s);
831 a60cf7e7 Kevin Wolf
}
832 a60cf7e7 Kevin Wolf
833 a60cf7e7 Kevin Wolf
static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
834 a60cf7e7 Kevin Wolf
{
835 a0a7573b Markus Armbruster
    s->tray_locked = buf[4] & 1;
836 025e849a Markus Armbruster
    bdrv_lock_medium(s->bs, buf[4] & 1);
837 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
838 a60cf7e7 Kevin Wolf
}
839 a60cf7e7 Kevin Wolf
840 a60cf7e7 Kevin Wolf
static void cmd_read(IDEState *s, uint8_t* buf)
841 a60cf7e7 Kevin Wolf
{
842 a60cf7e7 Kevin Wolf
    int nb_sectors, lba;
843 a60cf7e7 Kevin Wolf
844 a60cf7e7 Kevin Wolf
    if (buf[0] == GPCMD_READ_10) {
845 a60cf7e7 Kevin Wolf
        nb_sectors = ube16_to_cpu(buf + 7);
846 a60cf7e7 Kevin Wolf
    } else {
847 a60cf7e7 Kevin Wolf
        nb_sectors = ube32_to_cpu(buf + 6);
848 a60cf7e7 Kevin Wolf
    }
849 a60cf7e7 Kevin Wolf
850 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
851 a60cf7e7 Kevin Wolf
    if (nb_sectors == 0) {
852 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
853 a60cf7e7 Kevin Wolf
        return;
854 a60cf7e7 Kevin Wolf
    }
855 a60cf7e7 Kevin Wolf
856 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
857 a60cf7e7 Kevin Wolf
}
858 a60cf7e7 Kevin Wolf
859 a60cf7e7 Kevin Wolf
static void cmd_read_cd(IDEState *s, uint8_t* buf)
860 a60cf7e7 Kevin Wolf
{
861 a60cf7e7 Kevin Wolf
    int nb_sectors, lba, transfer_request;
862 a60cf7e7 Kevin Wolf
863 a60cf7e7 Kevin Wolf
    nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
864 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
865 a60cf7e7 Kevin Wolf
866 a60cf7e7 Kevin Wolf
    if (nb_sectors == 0) {
867 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
868 a60cf7e7 Kevin Wolf
        return;
869 a60cf7e7 Kevin Wolf
    }
870 a60cf7e7 Kevin Wolf
871 a60cf7e7 Kevin Wolf
    transfer_request = buf[9];
872 a60cf7e7 Kevin Wolf
    switch(transfer_request & 0xf8) {
873 a60cf7e7 Kevin Wolf
    case 0x00:
874 a60cf7e7 Kevin Wolf
        /* nothing */
875 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
876 a60cf7e7 Kevin Wolf
        break;
877 a60cf7e7 Kevin Wolf
    case 0x10:
878 a60cf7e7 Kevin Wolf
        /* normal read */
879 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
880 a60cf7e7 Kevin Wolf
        break;
881 a60cf7e7 Kevin Wolf
    case 0xf8:
882 a60cf7e7 Kevin Wolf
        /* read all data */
883 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
884 a60cf7e7 Kevin Wolf
        break;
885 a60cf7e7 Kevin Wolf
    default:
886 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
887 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
888 a60cf7e7 Kevin Wolf
        break;
889 a60cf7e7 Kevin Wolf
    }
890 a60cf7e7 Kevin Wolf
}
891 a60cf7e7 Kevin Wolf
892 a60cf7e7 Kevin Wolf
static void cmd_seek(IDEState *s, uint8_t* buf)
893 a60cf7e7 Kevin Wolf
{
894 a60cf7e7 Kevin Wolf
    unsigned int lba;
895 e119bcac Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
896 a60cf7e7 Kevin Wolf
897 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
898 a60cf7e7 Kevin Wolf
    if (lba >= total_sectors) {
899 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
900 a60cf7e7 Kevin Wolf
        return;
901 a60cf7e7 Kevin Wolf
    }
902 a60cf7e7 Kevin Wolf
903 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
904 a60cf7e7 Kevin Wolf
}
905 a60cf7e7 Kevin Wolf
906 a60cf7e7 Kevin Wolf
static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
907 a60cf7e7 Kevin Wolf
{
908 fdec4404 Markus Armbruster
    int sense;
909 f0776564 Markus Armbruster
    bool start = buf[4] & 1;
910 f0776564 Markus Armbruster
    bool loej = buf[4] & 2;     /* load on start, eject on !start */
911 a60cf7e7 Kevin Wolf
912 f0776564 Markus Armbruster
    if (loej) {
913 48f65b3f Markus Armbruster
        if (!start && !s->tray_open && s->tray_locked) {
914 fdec4404 Markus Armbruster
            sense = bdrv_is_inserted(s->bs)
915 fdec4404 Markus Armbruster
                ? SENSE_NOT_READY : SENSE_ILLEGAL_REQUEST;
916 fdec4404 Markus Armbruster
            ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
917 fdec4404 Markus Armbruster
            return;
918 a60cf7e7 Kevin Wolf
        }
919 fdec4404 Markus Armbruster
        bdrv_eject(s->bs, !start);
920 dd063333 Markus Armbruster
        s->tray_open = !start;
921 dd063333 Markus Armbruster
    }
922 fdec4404 Markus Armbruster
923 fdec4404 Markus Armbruster
    ide_atapi_cmd_ok(s);
924 a60cf7e7 Kevin Wolf
}
925 a60cf7e7 Kevin Wolf
926 a60cf7e7 Kevin Wolf
static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
927 a60cf7e7 Kevin Wolf
{
928 a60cf7e7 Kevin Wolf
    int max_len = ube16_to_cpu(buf + 8);
929 a60cf7e7 Kevin Wolf
930 a60cf7e7 Kevin Wolf
    cpu_to_ube16(buf, 0);
931 a60cf7e7 Kevin Wolf
    /* no current LBA */
932 a60cf7e7 Kevin Wolf
    buf[2] = 0;
933 a60cf7e7 Kevin Wolf
    buf[3] = 0;
934 a60cf7e7 Kevin Wolf
    buf[4] = 0;
935 a60cf7e7 Kevin Wolf
    buf[5] = 1;
936 a60cf7e7 Kevin Wolf
    cpu_to_ube16(buf + 6, 0);
937 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 8, max_len);
938 a60cf7e7 Kevin Wolf
}
939 a60cf7e7 Kevin Wolf
940 a60cf7e7 Kevin Wolf
static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
941 a60cf7e7 Kevin Wolf
{
942 a60cf7e7 Kevin Wolf
    int format, msf, start_track, len;
943 a60cf7e7 Kevin Wolf
    int max_len;
944 7a2c4b82 Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
945 a60cf7e7 Kevin Wolf
946 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 7);
947 a60cf7e7 Kevin Wolf
    format = buf[9] >> 6;
948 a60cf7e7 Kevin Wolf
    msf = (buf[1] >> 1) & 1;
949 a60cf7e7 Kevin Wolf
    start_track = buf[6];
950 a60cf7e7 Kevin Wolf
951 a60cf7e7 Kevin Wolf
    switch(format) {
952 a60cf7e7 Kevin Wolf
    case 0:
953 a60cf7e7 Kevin Wolf
        len = cdrom_read_toc(total_sectors, buf, msf, start_track);
954 a60cf7e7 Kevin Wolf
        if (len < 0)
955 a60cf7e7 Kevin Wolf
            goto error_cmd;
956 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, len, max_len);
957 a60cf7e7 Kevin Wolf
        break;
958 a60cf7e7 Kevin Wolf
    case 1:
959 a60cf7e7 Kevin Wolf
        /* multi session : only a single session defined */
960 a60cf7e7 Kevin Wolf
        memset(buf, 0, 12);
961 a60cf7e7 Kevin Wolf
        buf[1] = 0x0a;
962 a60cf7e7 Kevin Wolf
        buf[2] = 0x01;
963 a60cf7e7 Kevin Wolf
        buf[3] = 0x01;
964 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, 12, max_len);
965 a60cf7e7 Kevin Wolf
        break;
966 a60cf7e7 Kevin Wolf
    case 2:
967 a60cf7e7 Kevin Wolf
        len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
968 a60cf7e7 Kevin Wolf
        if (len < 0)
969 a60cf7e7 Kevin Wolf
            goto error_cmd;
970 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, len, max_len);
971 a60cf7e7 Kevin Wolf
        break;
972 a60cf7e7 Kevin Wolf
    default:
973 a60cf7e7 Kevin Wolf
    error_cmd:
974 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
975 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
976 a60cf7e7 Kevin Wolf
    }
977 a60cf7e7 Kevin Wolf
}
978 a60cf7e7 Kevin Wolf
979 a60cf7e7 Kevin Wolf
static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
980 a60cf7e7 Kevin Wolf
{
981 e119bcac Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
982 a60cf7e7 Kevin Wolf
983 a60cf7e7 Kevin Wolf
    /* NOTE: it is really the number of sectors minus 1 */
984 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf, total_sectors - 1);
985 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf + 4, 2048);
986 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 8, 8);
987 a60cf7e7 Kevin Wolf
}
988 a60cf7e7 Kevin Wolf
989 a60cf7e7 Kevin Wolf
static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
990 a60cf7e7 Kevin Wolf
{
991 a60cf7e7 Kevin Wolf
    int max_len;
992 a60cf7e7 Kevin Wolf
    int media = buf[1];
993 a60cf7e7 Kevin Wolf
    int format = buf[7];
994 a60cf7e7 Kevin Wolf
    int ret;
995 a60cf7e7 Kevin Wolf
996 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 8);
997 a60cf7e7 Kevin Wolf
998 a60cf7e7 Kevin Wolf
    if (format < 0xff) {
999 a60cf7e7 Kevin Wolf
        if (media_is_cd(s)) {
1000 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1001 a60cf7e7 Kevin Wolf
                                ASC_INCOMPATIBLE_FORMAT);
1002 a60cf7e7 Kevin Wolf
            return;
1003 a60cf7e7 Kevin Wolf
        } else if (!media_present(s)) {
1004 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1005 a60cf7e7 Kevin Wolf
                                ASC_INV_FIELD_IN_CMD_PACKET);
1006 a60cf7e7 Kevin Wolf
            return;
1007 a60cf7e7 Kevin Wolf
        }
1008 a60cf7e7 Kevin Wolf
    }
1009 a60cf7e7 Kevin Wolf
1010 a60cf7e7 Kevin Wolf
    memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1011 a60cf7e7 Kevin Wolf
           IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1012 a60cf7e7 Kevin Wolf
1013 a60cf7e7 Kevin Wolf
    switch (format) {
1014 a60cf7e7 Kevin Wolf
        case 0x00 ... 0x7f:
1015 a60cf7e7 Kevin Wolf
        case 0xff:
1016 a60cf7e7 Kevin Wolf
            if (media == 0) {
1017 a60cf7e7 Kevin Wolf
                ret = ide_dvd_read_structure(s, format, buf, buf);
1018 a60cf7e7 Kevin Wolf
1019 a60cf7e7 Kevin Wolf
                if (ret < 0) {
1020 a60cf7e7 Kevin Wolf
                    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1021 a60cf7e7 Kevin Wolf
                } else {
1022 a60cf7e7 Kevin Wolf
                    ide_atapi_cmd_reply(s, ret, max_len);
1023 a60cf7e7 Kevin Wolf
                }
1024 a60cf7e7 Kevin Wolf
1025 a60cf7e7 Kevin Wolf
                break;
1026 a60cf7e7 Kevin Wolf
            }
1027 a60cf7e7 Kevin Wolf
            /* TODO: BD support, fall through for now */
1028 a60cf7e7 Kevin Wolf
1029 a60cf7e7 Kevin Wolf
        /* Generic disk structures */
1030 a60cf7e7 Kevin Wolf
        case 0x80: /* TODO: AACS volume identifier */
1031 a60cf7e7 Kevin Wolf
        case 0x81: /* TODO: AACS media serial number */
1032 a60cf7e7 Kevin Wolf
        case 0x82: /* TODO: AACS media identifier */
1033 a60cf7e7 Kevin Wolf
        case 0x83: /* TODO: AACS media key block */
1034 a60cf7e7 Kevin Wolf
        case 0x90: /* TODO: List of recognized format layers */
1035 a60cf7e7 Kevin Wolf
        case 0xc0: /* TODO: Write protection status */
1036 a60cf7e7 Kevin Wolf
        default:
1037 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1038 a60cf7e7 Kevin Wolf
                                ASC_INV_FIELD_IN_CMD_PACKET);
1039 a60cf7e7 Kevin Wolf
            break;
1040 a60cf7e7 Kevin Wolf
    }
1041 a60cf7e7 Kevin Wolf
}
1042 a60cf7e7 Kevin Wolf
1043 a60cf7e7 Kevin Wolf
static void cmd_set_speed(IDEState *s, uint8_t* buf)
1044 a60cf7e7 Kevin Wolf
{
1045 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
1046 a60cf7e7 Kevin Wolf
}
1047 a60cf7e7 Kevin Wolf
1048 e1a064f9 Kevin Wolf
enum {
1049 e1a064f9 Kevin Wolf
    /*
1050 e1a064f9 Kevin Wolf
     * Only commands flagged as ALLOW_UA are allowed to run under a
1051 e1a064f9 Kevin Wolf
     * unit attention condition. (See MMC-5, section 4.1.6.1)
1052 e1a064f9 Kevin Wolf
     */
1053 e1a064f9 Kevin Wolf
    ALLOW_UA = 0x01,
1054 7a2c4b82 Kevin Wolf
1055 7a2c4b82 Kevin Wolf
    /*
1056 7a2c4b82 Kevin Wolf
     * Commands flagged with CHECK_READY can only execute if a medium is present.
1057 7a2c4b82 Kevin Wolf
     * Otherwise they report the Not Ready Condition. (See MMC-5, section
1058 7a2c4b82 Kevin Wolf
     * 4.1.8)
1059 7a2c4b82 Kevin Wolf
     */
1060 7a2c4b82 Kevin Wolf
    CHECK_READY = 0x02,
1061 e1a064f9 Kevin Wolf
};
1062 e1a064f9 Kevin Wolf
1063 e1a064f9 Kevin Wolf
static const struct {
1064 e1a064f9 Kevin Wolf
    void (*handler)(IDEState *s, uint8_t *buf);
1065 e1a064f9 Kevin Wolf
    int flags;
1066 e1a064f9 Kevin Wolf
} atapi_cmd_table[0x100] = {
1067 7a2c4b82 Kevin Wolf
    [ 0x00 ] = { cmd_test_unit_ready,               CHECK_READY },
1068 e1a064f9 Kevin Wolf
    [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
1069 e1a064f9 Kevin Wolf
    [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
1070 e1a064f9 Kevin Wolf
    [ 0x1a ] = { cmd_mode_sense, /* (6) */          0 },
1071 a1aff5bf Markus Armbruster
    [ 0x1b ] = { cmd_start_stop_unit,               0 }, /* [1] */
1072 e1a064f9 Kevin Wolf
    [ 0x1e ] = { cmd_prevent_allow_medium_removal,  0 },
1073 7a2c4b82 Kevin Wolf
    [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
1074 a1aff5bf Markus Armbruster
    [ 0x28 ] = { cmd_read, /* (10) */               CHECK_READY },
1075 7a2c4b82 Kevin Wolf
    [ 0x2b ] = { cmd_seek,                          CHECK_READY },
1076 7a2c4b82 Kevin Wolf
    [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
1077 e1a064f9 Kevin Wolf
    [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
1078 e1a064f9 Kevin Wolf
    [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1079 e1a064f9 Kevin Wolf
    [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
1080 a1aff5bf Markus Armbruster
    [ 0xa8 ] = { cmd_read, /* (12) */               CHECK_READY },
1081 a1aff5bf Markus Armbruster
    [ 0xad ] = { cmd_read_dvd_structure,            CHECK_READY },
1082 e1a064f9 Kevin Wolf
    [ 0xbb ] = { cmd_set_speed,                     0 },
1083 e1a064f9 Kevin Wolf
    [ 0xbd ] = { cmd_mechanism_status,              0 },
1084 a1aff5bf Markus Armbruster
    [ 0xbe ] = { cmd_read_cd,                       CHECK_READY },
1085 a1aff5bf Markus Armbruster
    /* [1] handler detects and reports not ready condition itself */
1086 e1a064f9 Kevin Wolf
};
1087 e1a064f9 Kevin Wolf
1088 33231e0e Kevin Wolf
void ide_atapi_cmd(IDEState *s)
1089 33231e0e Kevin Wolf
{
1090 33231e0e Kevin Wolf
    uint8_t *buf;
1091 33231e0e Kevin Wolf
1092 33231e0e Kevin Wolf
    buf = s->io_buffer;
1093 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
1094 33231e0e Kevin Wolf
    {
1095 33231e0e Kevin Wolf
        int i;
1096 33231e0e Kevin Wolf
        printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1097 33231e0e Kevin Wolf
        for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1098 ab719827 Alon Levy
            printf(" %02x", buf[i]);
1099 33231e0e Kevin Wolf
        }
1100 33231e0e Kevin Wolf
        printf("\n");
1101 33231e0e Kevin Wolf
    }
1102 33231e0e Kevin Wolf
#endif
1103 33231e0e Kevin Wolf
    /*
1104 e1a064f9 Kevin Wolf
     * If there's a UNIT_ATTENTION condition pending, only command flagged with
1105 e1a064f9 Kevin Wolf
     * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1106 e1a064f9 Kevin Wolf
     * condition response unless a higher priority status, defined by the drive
1107 33231e0e Kevin Wolf
     * here, is pending.
1108 33231e0e Kevin Wolf
     */
1109 33231e0e Kevin Wolf
    if (s->sense_key == SENSE_UNIT_ATTENTION &&
1110 e1a064f9 Kevin Wolf
        !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
1111 33231e0e Kevin Wolf
        ide_atapi_cmd_check_status(s);
1112 33231e0e Kevin Wolf
        return;
1113 33231e0e Kevin Wolf
    }
1114 4a737d14 Amit Shah
    /*
1115 4a737d14 Amit Shah
     * When a CD gets changed, we have to report an ejected state and
1116 4a737d14 Amit Shah
     * then a loaded state to guests so that they detect tray
1117 4a737d14 Amit Shah
     * open/close and media change events.  Guests that do not use
1118 4a737d14 Amit Shah
     * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1119 4a737d14 Amit Shah
     * states rely on this behavior.
1120 4a737d14 Amit Shah
     */
1121 a1aff5bf Markus Armbruster
    if (!s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1122 33231e0e Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1123 33231e0e Kevin Wolf
1124 33231e0e Kevin Wolf
        s->cdrom_changed = 0;
1125 33231e0e Kevin Wolf
        s->sense_key = SENSE_UNIT_ATTENTION;
1126 33231e0e Kevin Wolf
        s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1127 33231e0e Kevin Wolf
        return;
1128 33231e0e Kevin Wolf
    }
1129 e1a064f9 Kevin Wolf
1130 7a2c4b82 Kevin Wolf
    /* Report a Not Ready condition if appropriate for the command */
1131 7a2c4b82 Kevin Wolf
    if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
1132 7a2c4b82 Kevin Wolf
        (!media_present(s) || !bdrv_is_inserted(s->bs)))
1133 7a2c4b82 Kevin Wolf
    {
1134 7a2c4b82 Kevin Wolf
        ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1135 7a2c4b82 Kevin Wolf
        return;
1136 7a2c4b82 Kevin Wolf
    }
1137 7a2c4b82 Kevin Wolf
1138 e1a064f9 Kevin Wolf
    /* Execute the command */
1139 e1a064f9 Kevin Wolf
    if (atapi_cmd_table[s->io_buffer[0]].handler) {
1140 e1a064f9 Kevin Wolf
        atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1141 e1a064f9 Kevin Wolf
        return;
1142 33231e0e Kevin Wolf
    }
1143 e1a064f9 Kevin Wolf
1144 e1a064f9 Kevin Wolf
    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
1145 33231e0e Kevin Wolf
}