]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ide/atapi.c
hw/mips: Express dependencies of the r4k platform with Kconfig
[mirror_qemu.git] / hw / ide / atapi.c
CommitLineData
33231e0e
KW
1/*
2 * QEMU ATAPI Emulation
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
53239262 26#include "qemu/osdep.h"
33231e0e 27#include "hw/ide/internal.h"
0d09e41a 28#include "hw/scsi/scsi.h"
4be74634 29#include "sysemu/block-backend.h"
82a13ff8 30#include "trace.h"
33231e0e 31
26a122d3
EB
32#define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
33#define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
34
33231e0e
KW
35static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
36
37static void padstr8(uint8_t *buf, int buf_size, const char *src)
38{
39 int i;
40 for(i = 0; i < buf_size; i++) {
41 if (*src)
42 buf[i] = *src++;
43 else
44 buf[i] = ' ';
45 }
46}
47
48static inline void cpu_to_ube16(uint8_t *buf, int val)
49{
50 buf[0] = val >> 8;
51 buf[1] = val & 0xff;
52}
53
54static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
55{
56 buf[0] = val >> 24;
57 buf[1] = val >> 16;
58 buf[2] = val >> 8;
59 buf[3] = val & 0xff;
60}
61
62static inline int ube16_to_cpu(const uint8_t *buf)
63{
64 return (buf[0] << 8) | buf[1];
65}
66
67static inline int ube32_to_cpu(const uint8_t *buf)
68{
69 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
70}
71
72static void lba_to_msf(uint8_t *buf, int lba)
73{
74 lba += 150;
75 buf[0] = (lba / 75) / 60;
76 buf[1] = (lba / 75) % 60;
77 buf[2] = lba % 75;
78}
79
33231e0e
KW
80static inline int media_present(IDEState *s)
81{
a1aff5bf 82 return !s->tray_open && s->nb_sectors > 0;
33231e0e
KW
83}
84
a7acf552 85/* XXX: DVDs that could fit on a CD will be reported as a CD */
33231e0e
KW
86static inline int media_is_dvd(IDEState *s)
87{
88 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
89}
90
91static inline int media_is_cd(IDEState *s)
92{
93 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
94}
95
96static void cd_data_to_raw(uint8_t *buf, int lba)
97{
98 /* sync bytes */
99 buf[0] = 0x00;
100 memset(buf + 1, 0xff, 10);
101 buf[11] = 0x00;
102 buf += 12;
103 /* MSF */
104 lba_to_msf(buf, lba);
105 buf[3] = 0x01; /* mode 1 data */
106 buf += 4;
107 /* data */
108 buf += 2048;
109 /* XXX: ECC not computed */
110 memset(buf, 0, 288);
111}
112
5f81724d
PL
113static int
114cd_read_sector_sync(IDEState *s)
33231e0e
KW
115{
116 int ret;
ece2d05e 117 block_acct_start(blk_get_stats(s->blk), &s->acct,
26a122d3 118 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
33231e0e 119
82a13ff8 120 trace_cd_read_sector_sync(s->lba);
5f81724d
PL
121
122 switch (s->cd_sector_size) {
33231e0e 123 case 2048:
26a122d3
EB
124 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
125 s->io_buffer, ATAPI_SECTOR_SIZE);
33231e0e
KW
126 break;
127 case 2352:
26a122d3
EB
128 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
129 s->io_buffer + 16, ATAPI_SECTOR_SIZE);
ece2d05e 130 if (ret >= 0) {
5f81724d 131 cd_data_to_raw(s->io_buffer, s->lba);
ece2d05e 132 }
33231e0e
KW
133 break;
134 default:
ece2d05e
AG
135 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
136 return -EIO;
137 }
138
139 if (ret < 0) {
140 block_acct_failed(blk_get_stats(s->blk), &s->acct);
141 } else {
142 block_acct_done(blk_get_stats(s->blk), &s->acct);
5f81724d
PL
143 s->lba++;
144 s->io_buffer_index = 0;
33231e0e 145 }
ece2d05e 146
33231e0e
KW
147 return ret;
148}
149
5f81724d
PL
150static void cd_read_sector_cb(void *opaque, int ret)
151{
152 IDEState *s = opaque;
153
82a13ff8 154 trace_cd_read_sector_cb(s->lba, ret);
5f81724d
PL
155
156 if (ret < 0) {
36be0929 157 block_acct_failed(blk_get_stats(s->blk), &s->acct);
5f81724d
PL
158 ide_atapi_io_error(s, ret);
159 return;
160 }
161
36be0929
AG
162 block_acct_done(blk_get_stats(s->blk), &s->acct);
163
5f81724d
PL
164 if (s->cd_sector_size == 2352) {
165 cd_data_to_raw(s->io_buffer, s->lba);
166 }
167
168 s->lba++;
169 s->io_buffer_index = 0;
170 s->status &= ~BUSY_STAT;
171
172 ide_atapi_cmd_reply_end(s);
173}
174
175static int cd_read_sector(IDEState *s)
176{
e5863d49
VSO
177 void *buf;
178
5f81724d 179 if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
36be0929 180 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
5f81724d
PL
181 return -EINVAL;
182 }
183
e5863d49
VSO
184 buf = (s->cd_sector_size == 2352) ? s->io_buffer + 16 : s->io_buffer;
185 qemu_iovec_init_buf(&s->qiov, buf, ATAPI_SECTOR_SIZE);
5f81724d 186
82a13ff8 187 trace_cd_read_sector(s->lba);
5f81724d
PL
188
189 block_acct_start(blk_get_stats(s->blk), &s->acct,
26a122d3 190 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
5f81724d 191
02506b20
PL
192 ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
193 cd_read_sector_cb, s);
5f81724d
PL
194
195 s->status |= BUSY_STAT;
196 return 0;
197}
198
33231e0e
KW
199void ide_atapi_cmd_ok(IDEState *s)
200{
201 s->error = 0;
202 s->status = READY_STAT | SEEK_STAT;
203 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
d735b620 204 ide_transfer_stop(s);
33231e0e
KW
205 ide_set_irq(s->bus);
206}
207
208void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
209{
82a13ff8 210 trace_ide_atapi_cmd_error(s, sense_key, asc);
33231e0e
KW
211 s->error = sense_key << 4;
212 s->status = READY_STAT | ERR_STAT;
213 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
214 s->sense_key = sense_key;
215 s->asc = asc;
d735b620 216 ide_transfer_stop(s);
33231e0e
KW
217 ide_set_irq(s->bus);
218}
219
220void ide_atapi_io_error(IDEState *s, int ret)
221{
222 /* XXX: handle more errors */
223 if (ret == -ENOMEDIUM) {
67cc61e4 224 ide_atapi_cmd_error(s, NOT_READY,
33231e0e
KW
225 ASC_MEDIUM_NOT_PRESENT);
226 } else {
67cc61e4 227 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
33231e0e
KW
228 ASC_LOGICAL_BLOCK_OOR);
229 }
230}
231
af0e00db
JS
232static uint16_t atapi_byte_count_limit(IDEState *s)
233{
234 uint16_t bcl;
235
236 bcl = s->lcyl | (s->hcyl << 8);
237 if (bcl == 0xffff) {
238 return 0xfffe;
239 }
240 return bcl;
241}
242
33231e0e
KW
243/* The whole ATAPI transfer logic is handled in this function */
244void ide_atapi_cmd_reply_end(IDEState *s)
245{
246 int byte_count_limit, size, ret;
c173723f
PB
247 while (s->packet_transfer_size > 0) {
248 trace_ide_atapi_cmd_reply_end(s, s->packet_transfer_size,
249 s->elementary_transfer_size,
250 s->io_buffer_index);
251
33231e0e
KW
252 /* see if a new sector must be read */
253 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
5f81724d
PL
254 if (!s->elementary_transfer_size) {
255 ret = cd_read_sector(s);
256 if (ret < 0) {
257 ide_atapi_io_error(s, ret);
258 }
33231e0e 259 return;
5f81724d
PL
260 } else {
261 /* rebuffering within an elementary transfer is
262 * only possible with a sync request because we
263 * end up with a race condition otherwise */
264 ret = cd_read_sector_sync(s);
265 if (ret < 0) {
266 ide_atapi_io_error(s, ret);
267 return;
268 }
33231e0e 269 }
33231e0e
KW
270 }
271 if (s->elementary_transfer_size > 0) {
272 /* there are some data left to transmit in this elementary
273 transfer */
274 size = s->cd_sector_size - s->io_buffer_index;
275 if (size > s->elementary_transfer_size)
276 size = s->elementary_transfer_size;
33231e0e
KW
277 } else {
278 /* a new transfer is needed */
279 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
d02cea64 280 ide_set_irq(s->bus);
af0e00db 281 byte_count_limit = atapi_byte_count_limit(s);
82a13ff8 282 trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
33231e0e
KW
283 size = s->packet_transfer_size;
284 if (size > byte_count_limit) {
285 /* byte count limit must be even if this case */
286 if (byte_count_limit & 1)
287 byte_count_limit--;
288 size = byte_count_limit;
289 }
290 s->lcyl = size;
291 s->hcyl = size >> 8;
292 s->elementary_transfer_size = size;
293 /* we cannot transmit more than one sector at a time */
294 if (s->lba != -1) {
295 if (size > (s->cd_sector_size - s->io_buffer_index))
296 size = (s->cd_sector_size - s->io_buffer_index);
297 }
d02cea64 298 trace_ide_atapi_cmd_reply_end_new(s, s->status);
c173723f
PB
299 }
300 s->packet_transfer_size -= size;
301 s->elementary_transfer_size -= size;
302 s->io_buffer_index += size;
303
304 /* Some adapters process PIO data right away. In that case, we need
305 * to avoid mutual recursion between ide_transfer_start
306 * and ide_atapi_cmd_reply_end.
307 */
308 if (!ide_transfer_start_norecurse(s,
309 s->io_buffer + s->io_buffer_index - size,
310 size, ide_atapi_cmd_reply_end)) {
311 return;
33231e0e
KW
312 }
313 }
c173723f
PB
314
315 /* end of transfer */
316 trace_ide_atapi_cmd_reply_end_eot(s, s->status);
317 ide_atapi_cmd_ok(s);
318 ide_set_irq(s->bus);
33231e0e
KW
319}
320
321/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
322static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
323{
324 if (size > max_size)
325 size = max_size;
326 s->lba = -1; /* no sector read */
327 s->packet_transfer_size = size;
328 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
329 s->elementary_transfer_size = 0;
33231e0e
KW
330
331 if (s->atapi_dma) {
4be74634 332 block_acct_start(blk_get_stats(s->blk), &s->acct, size,
5366d0c8 333 BLOCK_ACCT_READ);
33231e0e 334 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
4855b576 335 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
33231e0e
KW
336 } else {
337 s->status = READY_STAT | SEEK_STAT;
c71c06d4 338 s->io_buffer_index = 0;
33231e0e
KW
339 ide_atapi_cmd_reply_end(s);
340 }
341}
342
343/* start a CD-CDROM read command */
344static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
345 int sector_size)
346{
347 s->lba = lba;
348 s->packet_transfer_size = nb_sectors * sector_size;
349 s->elementary_transfer_size = 0;
350 s->io_buffer_index = sector_size;
351 s->cd_sector_size = sector_size;
352
33231e0e
KW
353 ide_atapi_cmd_reply_end(s);
354}
355
356static void ide_atapi_cmd_check_status(IDEState *s)
357{
82a13ff8 358 trace_ide_atapi_cmd_check_status(s);
67cc61e4 359 s->error = MC_ERR | (UNIT_ATTENTION << 4);
33231e0e
KW
360 s->status = ERR_STAT;
361 s->nsector = 0;
362 ide_set_irq(s->bus);
363}
364/* ATAPI DMA support */
365
33231e0e
KW
366static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
367{
368 IDEState *s = opaque;
369 int data_offset, n;
370
371 if (ret < 0) {
502356ee
PB
372 if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
373 if (s->bus->error_status) {
7f951b2d 374 s->bus->dma->aiocb = NULL;
502356ee
PB
375 return;
376 }
377 goto eot;
378 }
33231e0e
KW
379 }
380
381 if (s->io_buffer_size > 0) {
382 /*
383 * For a cdrom read sector command (s->lba != -1),
384 * adjust the lba for the next s->io_buffer_size chunk
385 * and dma the current chunk.
386 * For a command != read (s->lba == -1), just transfer
387 * the reply data.
388 */
389 if (s->lba != -1) {
390 if (s->cd_sector_size == 2352) {
391 n = 1;
392 cd_data_to_raw(s->io_buffer, s->lba);
393 } else {
394 n = s->io_buffer_size >> 11;
395 }
396 s->lba += n;
397 }
398 s->packet_transfer_size -= s->io_buffer_size;
399 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
400 goto eot;
401 }
402
403 if (s->packet_transfer_size <= 0) {
404 s->status = READY_STAT | SEEK_STAT;
405 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
406 ide_set_irq(s->bus);
a597e79c 407 goto eot;
33231e0e
KW
408 }
409
410 s->io_buffer_index = 0;
411 if (s->cd_sector_size == 2352) {
412 n = 1;
413 s->io_buffer_size = s->cd_sector_size;
414 data_offset = 16;
415 } else {
416 n = s->packet_transfer_size >> 11;
417 if (n > (IDE_DMA_BUF_SECTORS / 4))
418 n = (IDE_DMA_BUF_SECTORS / 4);
419 s->io_buffer_size = n * 2048;
420 data_offset = 0;
421 }
0e168d35 422 trace_ide_atapi_cmd_read_dma_cb_aio(s, s->lba, n);
9942586b
VSO
423 qemu_iovec_init_buf(&s->bus->dma->qiov, s->io_buffer + data_offset,
424 n * ATAPI_SECTOR_SIZE);
a597e79c 425
02506b20
PL
426 s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
427 &s->bus->dma->qiov, n * 4,
428 ide_atapi_cmd_read_dma_cb, s);
a597e79c 429 return;
ad54ae80 430
a597e79c 431eot:
ece2d05e
AG
432 if (ret < 0) {
433 block_acct_failed(blk_get_stats(s->blk), &s->acct);
434 } else {
73a27d9a 435 block_acct_done(blk_get_stats(s->blk), &s->acct);
ece2d05e 436 }
0e7ce54c 437 ide_set_inactive(s, false);
33231e0e
KW
438}
439
440/* start a CD-CDROM read command with DMA */
441/* XXX: test if DMA is available */
442static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
443 int sector_size)
444{
445 s->lba = lba;
446 s->packet_transfer_size = nb_sectors * sector_size;
33231e0e
KW
447 s->io_buffer_size = 0;
448 s->cd_sector_size = sector_size;
449
4be74634 450 block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
5366d0c8 451 BLOCK_ACCT_READ);
a597e79c 452
33231e0e
KW
453 /* XXX: check if BUSY_STAT should be set */
454 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
4855b576 455 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
33231e0e
KW
456}
457
458static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
459 int sector_size)
460{
82a13ff8
JS
461 trace_ide_atapi_cmd_read(s, s->atapi_dma ? "dma" : "pio",
462 lba, nb_sectors);
33231e0e
KW
463 if (s->atapi_dma) {
464 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
465 } else {
466 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
467 }
468}
469
a71754e5
DDAG
470void ide_atapi_dma_restart(IDEState *s)
471{
472 /*
9a41826f
PB
473 * At this point we can just re-evaluate the packet command and start over.
474 * The presence of ->dma_cb callback in the pre_save ensures that the packet
475 * command has been completely sent and we can safely restart command.
a71754e5 476 */
9a41826f
PB
477 s->unit = s->bus->retry_unit;
478 s->bus->dma->ops->restart_dma(s->bus->dma);
479 ide_atapi_cmd(s);
a71754e5
DDAG
480}
481
33231e0e
KW
482static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
483 uint16_t profile)
484{
485 uint8_t *buf_profile = buf + 12; /* start of profiles */
486
487 buf_profile += ((*index) * 4); /* start of indexed profile */
488 cpu_to_ube16 (buf_profile, profile);
489 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
490
491 /* each profile adds 4 bytes to the response */
492 (*index)++;
493 buf[11] += 4; /* Additional Length */
494
495 return 4;
496}
497
498static int ide_dvd_read_structure(IDEState *s, int format,
499 const uint8_t *packet, uint8_t *buf)
500{
501 switch (format) {
502 case 0x0: /* Physical format information */
503 {
504 int layer = packet[6];
505 uint64_t total_sectors;
506
507 if (layer != 0)
508 return -ASC_INV_FIELD_IN_CMD_PACKET;
509
e119bcac
KW
510 total_sectors = s->nb_sectors >> 2;
511 if (total_sectors == 0) {
33231e0e 512 return -ASC_MEDIUM_NOT_PRESENT;
e119bcac 513 }
33231e0e
KW
514
515 buf[4] = 1; /* DVD-ROM, part version 1 */
516 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
517 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
518 buf[7] = 0; /* default densities */
519
520 /* FIXME: 0x30000 per spec? */
521 cpu_to_ube32(buf + 8, 0); /* start sector */
522 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
523 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
524
525 /* Size of buffer, not including 2 byte size field */
d8ee2591 526 stw_be_p(buf, 2048 + 2);
33231e0e
KW
527
528 /* 2k data + 4 byte header */
529 return (2048 + 4);
530 }
531
532 case 0x01: /* DVD copyright information */
533 buf[4] = 0; /* no copyright data */
534 buf[5] = 0; /* no region restrictions */
535
536 /* Size of buffer, not including 2 byte size field */
d8ee2591 537 stw_be_p(buf, 4 + 2);
33231e0e
KW
538
539 /* 4 byte header + 4 byte data */
540 return (4 + 4);
541
542 case 0x03: /* BCA information - invalid field for no BCA info */
543 return -ASC_INV_FIELD_IN_CMD_PACKET;
544
545 case 0x04: /* DVD disc manufacturing information */
546 /* Size of buffer, not including 2 byte size field */
d8ee2591 547 stw_be_p(buf, 2048 + 2);
33231e0e
KW
548
549 /* 2k data + 4 byte header */
550 return (2048 + 4);
551
552 case 0xff:
553 /*
554 * This lists all the command capabilities above. Add new ones
555 * in order and update the length and buffer return values.
556 */
557
558 buf[4] = 0x00; /* Physical format */
559 buf[5] = 0x40; /* Not writable, is readable */
d8ee2591 560 stw_be_p(buf + 6, 2048 + 4);
33231e0e
KW
561
562 buf[8] = 0x01; /* Copyright info */
563 buf[9] = 0x40; /* Not writable, is readable */
d8ee2591 564 stw_be_p(buf + 10, 4 + 4);
33231e0e
KW
565
566 buf[12] = 0x03; /* BCA info */
567 buf[13] = 0x40; /* Not writable, is readable */
d8ee2591 568 stw_be_p(buf + 14, 188 + 4);
33231e0e
KW
569
570 buf[16] = 0x04; /* Manufacturing info */
571 buf[17] = 0x40; /* Not writable, is readable */
d8ee2591 572 stw_be_p(buf + 18, 2048 + 4);
33231e0e
KW
573
574 /* Size of buffer, not including 2 byte size field */
d8ee2591 575 stw_be_p(buf, 16 + 2);
33231e0e
KW
576
577 /* data written + 4 byte header */
578 return (16 + 4);
579
580 default: /* TODO: formats beyond DVD-ROM requires */
581 return -ASC_INV_FIELD_IN_CMD_PACKET;
582 }
583}
584
585static unsigned int event_status_media(IDEState *s,
586 uint8_t *buf)
587{
33231e0e
KW
588 uint8_t event_code, media_status;
589
590 media_status = 0;
dd063333 591 if (s->tray_open) {
33231e0e 592 media_status = MS_TRAY_OPEN;
4be74634 593 } else if (blk_is_inserted(s->blk)) {
33231e0e
KW
594 media_status = MS_MEDIA_PRESENT;
595 }
596
597 /* Event notification descriptor */
598 event_code = MEC_NO_CHANGE;
2df0a3a3
PB
599 if (media_status != MS_TRAY_OPEN) {
600 if (s->events.new_media) {
601 event_code = MEC_NEW_MEDIA;
602 s->events.new_media = false;
603 } else if (s->events.eject_request) {
604 event_code = MEC_EJECT_REQUESTED;
605 s->events.eject_request = false;
606 }
33231e0e
KW
607 }
608
609 buf[4] = event_code;
610 buf[5] = media_status;
611
612 /* These fields are reserved, just clear them. */
613 buf[6] = 0;
614 buf[7] = 0;
615
616 return 8; /* We wrote to 4 extra bytes from the header */
617}
618
e7bd708e
JS
619/*
620 * Before transferring data or otherwise signalling acceptance of a command
621 * marked CONDDATA, we must check the validity of the byte_count_limit.
622 */
623static bool validate_bcl(IDEState *s)
624{
625 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
626 if (s->atapi_dma || atapi_byte_count_limit(s)) {
627 return true;
628 }
629
630 /* TODO: Move abort back into core.c and introduce proper error flow between
631 * ATAPI layer and IDE core layer */
632 ide_abort_command(s);
633 return false;
634}
635
e1a064f9
KW
636static void cmd_get_event_status_notification(IDEState *s,
637 uint8_t *buf)
33231e0e 638{
e1a064f9
KW
639 const uint8_t *packet = buf;
640
33231e0e
KW
641 struct {
642 uint8_t opcode;
643 uint8_t polled; /* lsb bit is polled; others are reserved */
644 uint8_t reserved2[2];
645 uint8_t class;
646 uint8_t reserved3[2];
647 uint16_t len;
648 uint8_t control;
541dc0d4 649 } QEMU_PACKED *gesn_cdb;
33231e0e
KW
650
651 struct {
652 uint16_t len;
653 uint8_t notification_class;
654 uint8_t supported_events;
541dc0d4 655 } QEMU_PACKED *gesn_event_header;
33231e0e
KW
656 unsigned int max_len, used_len;
657
658 gesn_cdb = (void *)packet;
659 gesn_event_header = (void *)buf;
660
661 max_len = be16_to_cpu(gesn_cdb->len);
662
663 /* It is fine by the MMC spec to not support async mode operations */
664 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
665 /* Only polling is supported, asynchronous mode is not. */
67cc61e4 666 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
33231e0e
KW
667 ASC_INV_FIELD_IN_CMD_PACKET);
668 return;
669 }
670
671 /* polling mode operation */
672
673 /*
674 * These are the supported events.
675 *
676 * We currently only support requests of the 'media' type.
f0f992e6
PB
677 * Notification class requests and supported event classes are bitmasks,
678 * but they are build from the same values as the "notification class"
679 * field.
33231e0e 680 */
f0f992e6 681 gesn_event_header->supported_events = 1 << GESN_MEDIA;
33231e0e
KW
682
683 /*
684 * We use |= below to set the class field; other bits in this byte
685 * are reserved now but this is useful to do if we have to use the
686 * reserved fields later.
687 */
688 gesn_event_header->notification_class = 0;
689
690 /*
691 * Responses to requests are to be based on request priority. The
692 * notification_class_request_type enum above specifies the
693 * priority: upper elements are higher prio than lower ones.
694 */
f0f992e6
PB
695 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
696 gesn_event_header->notification_class |= GESN_MEDIA;
33231e0e
KW
697 used_len = event_status_media(s, buf);
698 } else {
699 gesn_event_header->notification_class = 0x80; /* No event available */
700 used_len = sizeof(*gesn_event_header);
701 }
702 gesn_event_header->len = cpu_to_be16(used_len
703 - sizeof(*gesn_event_header));
704 ide_atapi_cmd_reply(s, used_len, max_len);
705}
706
a60cf7e7
KW
707static void cmd_request_sense(IDEState *s, uint8_t *buf)
708{
709 int max_len = buf[4];
710
711 memset(buf, 0, 18);
712 buf[0] = 0x70 | (1 << 7);
713 buf[2] = s->sense_key;
714 buf[7] = 10;
715 buf[12] = s->asc;
716
67cc61e4
PB
717 if (s->sense_key == UNIT_ATTENTION) {
718 s->sense_key = NO_SENSE;
a60cf7e7
KW
719 }
720
721 ide_atapi_cmd_reply(s, 18, max_len);
722}
723
724static void cmd_inquiry(IDEState *s, uint8_t *buf)
725{
9a502563 726 uint8_t page_code = buf[2];
a60cf7e7
KW
727 int max_len = buf[4];
728
9a502563
JS
729 unsigned idx = 0;
730 unsigned size_idx;
731 unsigned preamble_len;
732
733 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
734 * we are being asked for a specific page of info indicated by byte 2. */
735 if (buf[1] & 0x01) {
736 preamble_len = 4;
737 size_idx = 3;
738
739 buf[idx++] = 0x05; /* CD-ROM */
740 buf[idx++] = page_code; /* Page Code */
741 buf[idx++] = 0x00; /* reserved */
742 idx++; /* length (set later) */
743
744 switch (page_code) {
745 case 0x00:
746 /* Supported Pages: List of supported VPD responses. */
747 buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
748 buf[idx++] = 0x83; /* 0x83: Device Identification. */
749 break;
750
751 case 0x83:
752 /* Device Identification. Each entry is optional, but the entries
753 * included here are modeled after libata's VPD responses.
754 * If the response is given, at least one entry must be present. */
755
756 /* Entry 1: Serial */
757 if (idx + 24 > max_len) {
758 /* Not enough room for even the first entry: */
759 /* 4 byte header + 20 byte string */
760 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
761 ASC_DATA_PHASE_ERROR);
762 return;
763 }
764 buf[idx++] = 0x02; /* Ascii */
765 buf[idx++] = 0x00; /* Vendor Specific */
766 buf[idx++] = 0x00;
767 buf[idx++] = 20; /* Remaining length */
768 padstr8(buf + idx, 20, s->drive_serial_str);
769 idx += 20;
770
771 /* Entry 2: Drive Model and Serial */
772 if (idx + 72 > max_len) {
773 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
774 goto out;
775 }
776 buf[idx++] = 0x02; /* Ascii */
777 buf[idx++] = 0x01; /* T10 Vendor */
778 buf[idx++] = 0x00;
779 buf[idx++] = 68;
780 padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
781 idx += 8;
782 padstr8(buf + idx, 40, s->drive_model_str);
783 idx += 40;
784 padstr8(buf + idx, 20, s->drive_serial_str);
785 idx += 20;
786
787 /* Entry 3: WWN */
788 if (s->wwn && (idx + 12 <= max_len)) {
789 /* 4 byte header + 8 byte wwn */
790 buf[idx++] = 0x01; /* Binary */
791 buf[idx++] = 0x03; /* NAA */
792 buf[idx++] = 0x00;
793 buf[idx++] = 0x08;
794 stq_be_p(&buf[idx], s->wwn);
795 idx += 8;
796 }
797 break;
798
799 default:
800 /* SPC-3, revision 23 sec. 6.4 */
801 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
802 ASC_INV_FIELD_IN_CMD_PACKET);
803 return;
804 }
805 } else {
806 preamble_len = 5;
807 size_idx = 4;
808
809 buf[0] = 0x05; /* CD-ROM */
810 buf[1] = 0x80; /* removable */
811 buf[2] = 0x00; /* ISO */
812 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
813 /* buf[size_idx] set below. */
814 buf[5] = 0; /* reserved */
815 buf[6] = 0; /* reserved */
816 buf[7] = 0; /* reserved */
817 padstr8(buf + 8, 8, "QEMU");
818 padstr8(buf + 16, 16, "QEMU DVD-ROM");
819 padstr8(buf + 32, 4, s->version);
820 idx = 36;
821 }
822
823 out:
824 buf[size_idx] = idx - preamble_len;
825 ide_atapi_cmd_reply(s, idx, max_len);
a60cf7e7
KW
826}
827
828static void cmd_get_configuration(IDEState *s, uint8_t *buf)
829{
830 uint32_t len;
831 uint8_t index = 0;
832 int max_len;
833
834 /* only feature 0 is supported */
835 if (buf[2] != 0 || buf[3] != 0) {
67cc61e4 836 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
837 ASC_INV_FIELD_IN_CMD_PACKET);
838 return;
839 }
840
841 /* XXX: could result in alignment problems in some architectures */
842 max_len = ube16_to_cpu(buf + 7);
843
844 /*
845 * XXX: avoid overflow for io_buffer if max_len is bigger than
846 * the size of that buffer (dimensioned to max number of
847 * sectors to transfer at once)
848 *
849 * Only a problem if the feature/profiles grow.
850 */
851 if (max_len > 512) {
852 /* XXX: assume 1 sector */
853 max_len = 512;
854 }
855
856 memset(buf, 0, max_len);
857 /*
858 * the number of sectors from the media tells us which profile
859 * to use as current. 0 means there is no media
860 */
861 if (media_is_dvd(s)) {
862 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
863 } else if (media_is_cd(s)) {
864 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
865 }
866
867 buf[10] = 0x02 | 0x01; /* persistent and current */
868 len = 12; /* headers: 8 + 4 */
869 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
870 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
871 cpu_to_ube32(buf, len - 4); /* data length */
872
873 ide_atapi_cmd_reply(s, len, max_len);
874}
875
876static void cmd_mode_sense(IDEState *s, uint8_t *buf)
877{
878 int action, code;
879 int max_len;
880
2c20ae11 881 max_len = ube16_to_cpu(buf + 7);
a60cf7e7
KW
882 action = buf[2] >> 6;
883 code = buf[2] & 0x3f;
884
885 switch(action) {
886 case 0: /* current values */
887 switch(code) {
67cc61e4 888 case MODE_PAGE_R_W_ERROR: /* error recovery */
2c20ae11 889 cpu_to_ube16(&buf[0], 16 - 2);
a60cf7e7
KW
890 buf[2] = 0x70;
891 buf[3] = 0;
892 buf[4] = 0;
893 buf[5] = 0;
894 buf[6] = 0;
895 buf[7] = 0;
896
af0e1ea2
PB
897 buf[8] = MODE_PAGE_R_W_ERROR;
898 buf[9] = 16 - 10;
a60cf7e7
KW
899 buf[10] = 0x00;
900 buf[11] = 0x05;
901 buf[12] = 0x00;
902 buf[13] = 0x00;
903 buf[14] = 0x00;
904 buf[15] = 0x00;
905 ide_atapi_cmd_reply(s, 16, max_len);
906 break;
67cc61e4 907 case MODE_PAGE_AUDIO_CTL:
2c20ae11 908 cpu_to_ube16(&buf[0], 24 - 2);
a60cf7e7
KW
909 buf[2] = 0x70;
910 buf[3] = 0;
911 buf[4] = 0;
912 buf[5] = 0;
913 buf[6] = 0;
914 buf[7] = 0;
915
af0e1ea2
PB
916 buf[8] = MODE_PAGE_AUDIO_CTL;
917 buf[9] = 24 - 10;
a60cf7e7
KW
918 /* Fill with CDROM audio volume */
919 buf[17] = 0;
920 buf[19] = 0;
921 buf[21] = 0;
922 buf[23] = 0;
923
924 ide_atapi_cmd_reply(s, 24, max_len);
925 break;
67cc61e4 926 case MODE_PAGE_CAPABILITIES:
2c20ae11 927 cpu_to_ube16(&buf[0], 30 - 2);
a60cf7e7
KW
928 buf[2] = 0x70;
929 buf[3] = 0;
930 buf[4] = 0;
931 buf[5] = 0;
932 buf[6] = 0;
933 buf[7] = 0;
934
af0e1ea2 935 buf[8] = MODE_PAGE_CAPABILITIES;
2c20ae11 936 buf[9] = 30 - 10;
a07c7dcd 937 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
a60cf7e7
KW
938 buf[11] = 0x00;
939
940 /* Claim PLAY_AUDIO capability (0x01) since some Linux
941 code checks for this to automount media. */
942 buf[12] = 0x71;
943 buf[13] = 3 << 5;
944 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
a0a7573b 945 if (s->tray_locked) {
a07c7dcd 946 buf[14] |= 1 << 1;
a0a7573b 947 }
a07c7dcd
PB
948 buf[15] = 0x00; /* No volume & mute control, no changer */
949 cpu_to_ube16(&buf[16], 704); /* 4x read speed */
950 buf[18] = 0; /* Two volume levels */
a60cf7e7 951 buf[19] = 2;
a07c7dcd
PB
952 cpu_to_ube16(&buf[20], 512); /* 512k buffer */
953 cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
a60cf7e7
KW
954 buf[24] = 0;
955 buf[25] = 0;
956 buf[26] = 0;
957 buf[27] = 0;
2c20ae11
PB
958 buf[28] = 0;
959 buf[29] = 0;
960 ide_atapi_cmd_reply(s, 30, max_len);
a60cf7e7
KW
961 break;
962 default:
963 goto error_cmd;
964 }
965 break;
966 case 1: /* changeable values */
967 goto error_cmd;
968 case 2: /* default values */
969 goto error_cmd;
970 default:
971 case 3: /* saved values */
67cc61e4 972 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
973 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
974 break;
975 }
976 return;
977
978error_cmd:
67cc61e4 979 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
a60cf7e7
KW
980}
981
982static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
983{
7a2c4b82
KW
984 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
985 * come here, we know that it's ready. */
986 ide_atapi_cmd_ok(s);
a60cf7e7
KW
987}
988
989static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
990{
a0a7573b 991 s->tray_locked = buf[4] & 1;
4be74634 992 blk_lock_medium(s->blk, buf[4] & 1);
a60cf7e7
KW
993 ide_atapi_cmd_ok(s);
994}
995
996static void cmd_read(IDEState *s, uint8_t* buf)
997{
998 int nb_sectors, lba;
999
1000 if (buf[0] == GPCMD_READ_10) {
1001 nb_sectors = ube16_to_cpu(buf + 7);
1002 } else {
1003 nb_sectors = ube32_to_cpu(buf + 6);
1004 }
1005
1006 lba = ube32_to_cpu(buf + 2);
1007 if (nb_sectors == 0) {
1008 ide_atapi_cmd_ok(s);
1009 return;
1010 }
1011
1012 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1013}
1014
1015static void cmd_read_cd(IDEState *s, uint8_t* buf)
1016{
1017 int nb_sectors, lba, transfer_request;
1018
1019 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
1020 lba = ube32_to_cpu(buf + 2);
1021
1022 if (nb_sectors == 0) {
1023 ide_atapi_cmd_ok(s);
1024 return;
1025 }
1026
e7bd708e
JS
1027 transfer_request = buf[9] & 0xf8;
1028 if (transfer_request == 0x00) {
a60cf7e7
KW
1029 /* nothing */
1030 ide_atapi_cmd_ok(s);
e7bd708e
JS
1031 return;
1032 }
1033
1034 /* Check validity of BCL before transferring data */
1035 if (!validate_bcl(s)) {
1036 return;
1037 }
1038
1039 switch (transfer_request) {
a60cf7e7
KW
1040 case 0x10:
1041 /* normal read */
1042 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1043 break;
1044 case 0xf8:
1045 /* read all data */
1046 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1047 break;
1048 default:
67cc61e4 1049 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1050 ASC_INV_FIELD_IN_CMD_PACKET);
1051 break;
1052 }
1053}
1054
1055static void cmd_seek(IDEState *s, uint8_t* buf)
1056{
1057 unsigned int lba;
e119bcac 1058 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7 1059
a60cf7e7
KW
1060 lba = ube32_to_cpu(buf + 2);
1061 if (lba >= total_sectors) {
67cc61e4 1062 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
a60cf7e7
KW
1063 return;
1064 }
1065
1066 ide_atapi_cmd_ok(s);
1067}
1068
1069static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
1070{
fdec4404 1071 int sense;
f0776564
MA
1072 bool start = buf[4] & 1;
1073 bool loej = buf[4] & 2; /* load on start, eject on !start */
ce560dcf
RS
1074 int pwrcnd = buf[4] & 0xf0;
1075
1076 if (pwrcnd) {
1077 /* eject/load only happens for power condition == 0 */
03441c3a 1078 ide_atapi_cmd_ok(s);
ce560dcf
RS
1079 return;
1080 }
a60cf7e7 1081
f0776564 1082 if (loej) {
48f65b3f 1083 if (!start && !s->tray_open && s->tray_locked) {
4be74634 1084 sense = blk_is_inserted(s->blk)
67cc61e4 1085 ? NOT_READY : ILLEGAL_REQUEST;
fdec4404
MA
1086 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1087 return;
a60cf7e7 1088 }
d88b1819
LC
1089
1090 if (s->tray_open != !start) {
4be74634 1091 blk_eject(s->blk, !start);
d88b1819
LC
1092 s->tray_open = !start;
1093 }
dd063333 1094 }
fdec4404
MA
1095
1096 ide_atapi_cmd_ok(s);
a60cf7e7
KW
1097}
1098
1099static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
1100{
1101 int max_len = ube16_to_cpu(buf + 8);
1102
1103 cpu_to_ube16(buf, 0);
1104 /* no current LBA */
1105 buf[2] = 0;
1106 buf[3] = 0;
1107 buf[4] = 0;
1108 buf[5] = 1;
1109 cpu_to_ube16(buf + 6, 0);
1110 ide_atapi_cmd_reply(s, 8, max_len);
1111}
1112
1113static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
1114{
1115 int format, msf, start_track, len;
a60cf7e7 1116 int max_len;
7a2c4b82 1117 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7
KW
1118
1119 max_len = ube16_to_cpu(buf + 7);
1120 format = buf[9] >> 6;
1121 msf = (buf[1] >> 1) & 1;
1122 start_track = buf[6];
1123
1124 switch(format) {
1125 case 0:
1126 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1127 if (len < 0)
1128 goto error_cmd;
1129 ide_atapi_cmd_reply(s, len, max_len);
1130 break;
1131 case 1:
1132 /* multi session : only a single session defined */
1133 memset(buf, 0, 12);
1134 buf[1] = 0x0a;
1135 buf[2] = 0x01;
1136 buf[3] = 0x01;
1137 ide_atapi_cmd_reply(s, 12, max_len);
1138 break;
1139 case 2:
1140 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1141 if (len < 0)
1142 goto error_cmd;
1143 ide_atapi_cmd_reply(s, len, max_len);
1144 break;
1145 default:
1146 error_cmd:
67cc61e4 1147 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1148 ASC_INV_FIELD_IN_CMD_PACKET);
1149 }
1150}
1151
1152static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
1153{
e119bcac 1154 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7 1155
a60cf7e7
KW
1156 /* NOTE: it is really the number of sectors minus 1 */
1157 cpu_to_ube32(buf, total_sectors - 1);
1158 cpu_to_ube32(buf + 4, 2048);
1159 ide_atapi_cmd_reply(s, 8, 8);
1160}
1161
55042b95
PB
1162static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
1163{
1164 uint8_t type = buf[1] & 7;
1165 uint32_t max_len = ube16_to_cpu(buf + 7);
1166
1167 /* Types 1/2 are only defined for Blu-Ray. */
1168 if (type != 0) {
1169 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1170 ASC_INV_FIELD_IN_CMD_PACKET);
1171 return;
1172 }
1173
1174 memset(buf, 0, 34);
1175 buf[1] = 32;
1176 buf[2] = 0xe; /* last session complete, disc finalized */
1177 buf[3] = 1; /* first track on disc */
1178 buf[4] = 1; /* # of sessions */
1179 buf[5] = 1; /* first track of last session */
1180 buf[6] = 1; /* last track of last session */
1181 buf[7] = 0x20; /* unrestricted use */
1182 buf[8] = 0x00; /* CD-ROM or DVD-ROM */
1183 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1184 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1185 /* 24-31: disc bar code */
1186 /* 32: disc application code */
1187 /* 33: number of OPC tables */
1188
1189 ide_atapi_cmd_reply(s, 34, max_len);
1190}
1191
a60cf7e7
KW
1192static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1193{
1194 int max_len;
1195 int media = buf[1];
1196 int format = buf[7];
1197 int ret;
1198
1199 max_len = ube16_to_cpu(buf + 8);
1200
1201 if (format < 0xff) {
1202 if (media_is_cd(s)) {
67cc61e4 1203 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1204 ASC_INCOMPATIBLE_FORMAT);
1205 return;
1206 } else if (!media_present(s)) {
67cc61e4 1207 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1208 ASC_INV_FIELD_IN_CMD_PACKET);
1209 return;
1210 }
1211 }
1212
1213 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1214 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1215
1216 switch (format) {
1217 case 0x00 ... 0x7f:
1218 case 0xff:
1219 if (media == 0) {
1220 ret = ide_dvd_read_structure(s, format, buf, buf);
1221
1222 if (ret < 0) {
67cc61e4 1223 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
a60cf7e7
KW
1224 } else {
1225 ide_atapi_cmd_reply(s, ret, max_len);
1226 }
1227
1228 break;
1229 }
1230 /* TODO: BD support, fall through for now */
1231
1232 /* Generic disk structures */
1233 case 0x80: /* TODO: AACS volume identifier */
1234 case 0x81: /* TODO: AACS media serial number */
1235 case 0x82: /* TODO: AACS media identifier */
1236 case 0x83: /* TODO: AACS media key block */
1237 case 0x90: /* TODO: List of recognized format layers */
1238 case 0xc0: /* TODO: Write protection status */
1239 default:
67cc61e4 1240 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1241 ASC_INV_FIELD_IN_CMD_PACKET);
1242 break;
1243 }
1244}
1245
1246static void cmd_set_speed(IDEState *s, uint8_t* buf)
1247{
1248 ide_atapi_cmd_ok(s);
1249}
1250
e1a064f9
KW
1251enum {
1252 /*
1253 * Only commands flagged as ALLOW_UA are allowed to run under a
1254 * unit attention condition. (See MMC-5, section 4.1.6.1)
1255 */
1256 ALLOW_UA = 0x01,
7a2c4b82
KW
1257
1258 /*
1259 * Commands flagged with CHECK_READY can only execute if a medium is present.
1260 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1261 * 4.1.8)
1262 */
1263 CHECK_READY = 0x02,
9ef2e93f
JS
1264
1265 /*
1266 * Commands flagged with NONDATA do not in any circumstances return
1267 * any data via ide_atapi_cmd_reply. These commands are exempt from
1268 * the normal byte_count_limit constraints.
1269 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1270 */
1271 NONDATA = 0x04,
e7bd708e
JS
1272
1273 /*
1274 * CONDDATA implies a command that transfers data only conditionally based
1275 * on the presence of suboptions. It should be exempt from the BCL check at
1276 * command validation time, but it needs to be checked at the command
1277 * handler level instead.
1278 */
1279 CONDDATA = 0x08,
e1a064f9
KW
1280};
1281
f36aa12d 1282static const struct AtapiCmd {
e1a064f9
KW
1283 void (*handler)(IDEState *s, uint8_t *buf);
1284 int flags;
1285} atapi_cmd_table[0x100] = {
9ef2e93f 1286 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
e1a064f9
KW
1287 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1288 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
9ef2e93f
JS
1289 [ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */
1290 [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
7a2c4b82 1291 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
a1aff5bf 1292 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
9ef2e93f 1293 [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
7a2c4b82 1294 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
e1a064f9
KW
1295 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1296 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
55042b95 1297 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
e1a064f9 1298 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
a1aff5bf
MA
1299 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1300 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
9ef2e93f 1301 [ 0xbb ] = { cmd_set_speed, NONDATA },
e1a064f9 1302 [ 0xbd ] = { cmd_mechanism_status, 0 },
e7bd708e 1303 [ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA },
a1aff5bf 1304 /* [1] handler detects and reports not ready condition itself */
e1a064f9
KW
1305};
1306
33231e0e
KW
1307void ide_atapi_cmd(IDEState *s)
1308{
f36aa12d
JS
1309 uint8_t *buf = s->io_buffer;
1310 const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
33231e0e 1311
82a13ff8
JS
1312 trace_ide_atapi_cmd(s, s->io_buffer[0]);
1313
1314 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET)) {
1315 /* Each pretty-printed byte needs two bytes and a space; */
1316 char *ppacket = g_malloc(ATAPI_PACKET_SIZE * 3 + 1);
33231e0e 1317 int i;
82a13ff8
JS
1318 for (i = 0; i < ATAPI_PACKET_SIZE; i++) {
1319 sprintf(ppacket + (i * 3), "%02x ", buf[i]);
33231e0e 1320 }
82a13ff8
JS
1321 trace_ide_atapi_cmd_packet(s, s->lcyl | (s->hcyl << 8), ppacket);
1322 g_free(ppacket);
33231e0e 1323 }
f36aa12d 1324
33231e0e 1325 /*
e1a064f9
KW
1326 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1327 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1328 * condition response unless a higher priority status, defined by the drive
33231e0e
KW
1329 * here, is pending.
1330 */
f36aa12d 1331 if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
33231e0e
KW
1332 ide_atapi_cmd_check_status(s);
1333 return;
1334 }
4a737d14
AS
1335 /*
1336 * When a CD gets changed, we have to report an ejected state and
1337 * then a loaded state to guests so that they detect tray
1338 * open/close and media change events. Guests that do not use
1339 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1340 * states rely on this behavior.
1341 */
f36aa12d 1342 if (!(cmd->flags & ALLOW_UA) &&
4be74634 1343 !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
0c6f08b0
PH
1344
1345 if (s->cdrom_changed == 1) {
1346 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1347 s->cdrom_changed = 2;
1348 } else {
1349 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1350 s->cdrom_changed = 0;
1351 }
33231e0e 1352
33231e0e
KW
1353 return;
1354 }
e1a064f9 1355
7a2c4b82 1356 /* Report a Not Ready condition if appropriate for the command */
f36aa12d 1357 if ((cmd->flags & CHECK_READY) &&
4be74634 1358 (!media_present(s) || !blk_is_inserted(s->blk)))
7a2c4b82 1359 {
67cc61e4 1360 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
7a2c4b82
KW
1361 return;
1362 }
1363
e7bd708e 1364 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
9ef2e93f
JS
1365 * If this is a data-transferring PIO command and BCL is 0,
1366 * we abort at the /ATA/ level, not the ATAPI level.
1367 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
e7bd708e
JS
1368 if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
1369 if (!validate_bcl(s)) {
9ef2e93f
JS
1370 return;
1371 }
1372 }
1373
e1a064f9 1374 /* Execute the command */
f36aa12d
JS
1375 if (cmd->handler) {
1376 cmd->handler(s, buf);
e1a064f9 1377 return;
33231e0e 1378 }
e1a064f9 1379
67cc61e4 1380 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
33231e0e 1381}