Statistics
| Branch: | Revision:

root / hw / ide / atapi.c @ 69c38b8f

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