hw/isa/vt82c686: Consolidate the use of device_class_set_parent_realize()
[qemu/ar7.git] / hw / ide / atapi.c
blobdcc39df9a44bd667aa9fdb917b48a9e9e0249b0d
1 /*
2 * QEMU ATAPI Emulation
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "qemu/osdep.h"
27 #include "hw/ide/internal.h"
28 #include "hw/scsi/scsi.h"
29 #include "sysemu/block-backend.h"
30 #include "scsi/constants.h"
31 #include "trace.h"
33 #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
34 #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
36 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
38 static void padstr8(uint8_t *buf, int buf_size, const char *src)
40 int i;
41 for(i = 0; i < buf_size; i++) {
42 if (*src)
43 buf[i] = *src++;
44 else
45 buf[i] = ' ';
49 static void lba_to_msf(uint8_t *buf, int lba)
51 lba += 150;
52 buf[0] = (lba / 75) / 60;
53 buf[1] = (lba / 75) % 60;
54 buf[2] = lba % 75;
57 static inline int media_present(IDEState *s)
59 return !s->tray_open && s->nb_sectors > 0;
62 /* XXX: DVDs that could fit on a CD will be reported as a CD */
63 static inline int media_is_dvd(IDEState *s)
65 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
68 static inline int media_is_cd(IDEState *s)
70 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
73 static void cd_data_to_raw(uint8_t *buf, int lba)
75 /* sync bytes */
76 buf[0] = 0x00;
77 memset(buf + 1, 0xff, 10);
78 buf[11] = 0x00;
79 buf += 12;
80 /* MSF */
81 lba_to_msf(buf, lba);
82 buf[3] = 0x01; /* mode 1 data */
83 buf += 4;
84 /* data */
85 buf += 2048;
86 /* XXX: ECC not computed */
87 memset(buf, 0, 288);
90 static int
91 cd_read_sector_sync(IDEState *s)
93 int ret;
94 block_acct_start(blk_get_stats(s->blk), &s->acct,
95 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
97 trace_cd_read_sector_sync(s->lba);
99 switch (s->cd_sector_size) {
100 case 2048:
101 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
102 ATAPI_SECTOR_SIZE, s->io_buffer, 0);
103 break;
104 case 2352:
105 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
106 ATAPI_SECTOR_SIZE, s->io_buffer + 16, 0);
107 if (ret >= 0) {
108 cd_data_to_raw(s->io_buffer, s->lba);
110 break;
111 default:
112 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
113 return -EIO;
116 if (ret < 0) {
117 block_acct_failed(blk_get_stats(s->blk), &s->acct);
118 } else {
119 block_acct_done(blk_get_stats(s->blk), &s->acct);
120 s->lba++;
121 s->io_buffer_index = 0;
124 return ret;
127 static void cd_read_sector_cb(void *opaque, int ret)
129 IDEState *s = opaque;
131 trace_cd_read_sector_cb(s->lba, ret);
133 if (ret < 0) {
134 block_acct_failed(blk_get_stats(s->blk), &s->acct);
135 ide_atapi_io_error(s, ret);
136 return;
139 block_acct_done(blk_get_stats(s->blk), &s->acct);
141 if (s->cd_sector_size == 2352) {
142 cd_data_to_raw(s->io_buffer, s->lba);
145 s->lba++;
146 s->io_buffer_index = 0;
147 s->status &= ~BUSY_STAT;
149 ide_atapi_cmd_reply_end(s);
152 static int cd_read_sector(IDEState *s)
154 void *buf;
156 if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
157 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
158 return -EINVAL;
161 buf = (s->cd_sector_size == 2352) ? s->io_buffer + 16 : s->io_buffer;
162 qemu_iovec_init_buf(&s->qiov, buf, ATAPI_SECTOR_SIZE);
164 trace_cd_read_sector(s->lba);
166 block_acct_start(blk_get_stats(s->blk), &s->acct,
167 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
169 ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
170 cd_read_sector_cb, s);
172 s->status |= BUSY_STAT;
173 return 0;
176 void ide_atapi_cmd_ok(IDEState *s)
178 s->error = 0;
179 s->status = READY_STAT | SEEK_STAT;
180 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
181 ide_transfer_stop(s);
182 ide_bus_set_irq(s->bus);
185 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
187 trace_ide_atapi_cmd_error(s, sense_key, asc);
188 s->error = sense_key << 4;
189 s->status = READY_STAT | ERR_STAT;
190 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
191 s->sense_key = sense_key;
192 s->asc = asc;
193 ide_transfer_stop(s);
194 ide_bus_set_irq(s->bus);
197 void ide_atapi_io_error(IDEState *s, int ret)
199 /* XXX: handle more errors */
200 if (ret == -ENOMEDIUM) {
201 ide_atapi_cmd_error(s, NOT_READY,
202 ASC_MEDIUM_NOT_PRESENT);
203 } else {
204 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
205 ASC_LOGICAL_BLOCK_OOR);
209 static uint16_t atapi_byte_count_limit(IDEState *s)
211 uint16_t bcl;
213 bcl = s->lcyl | (s->hcyl << 8);
214 if (bcl == 0xffff) {
215 return 0xfffe;
217 return bcl;
220 /* The whole ATAPI transfer logic is handled in this function */
221 void ide_atapi_cmd_reply_end(IDEState *s)
223 int byte_count_limit, size, ret;
224 while (s->packet_transfer_size > 0) {
225 trace_ide_atapi_cmd_reply_end(s, s->packet_transfer_size,
226 s->elementary_transfer_size,
227 s->io_buffer_index);
229 /* see if a new sector must be read */
230 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
231 if (!s->elementary_transfer_size) {
232 ret = cd_read_sector(s);
233 if (ret < 0) {
234 ide_atapi_io_error(s, ret);
236 return;
237 } else {
238 /* rebuffering within an elementary transfer is
239 * only possible with a sync request because we
240 * end up with a race condition otherwise */
241 ret = cd_read_sector_sync(s);
242 if (ret < 0) {
243 ide_atapi_io_error(s, ret);
244 return;
248 if (s->elementary_transfer_size > 0) {
249 /* there are some data left to transmit in this elementary
250 transfer */
251 size = s->cd_sector_size - s->io_buffer_index;
252 if (size > s->elementary_transfer_size)
253 size = s->elementary_transfer_size;
254 } else {
255 /* a new transfer is needed */
256 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
257 ide_bus_set_irq(s->bus);
258 byte_count_limit = atapi_byte_count_limit(s);
259 trace_ide_atapi_cmd_reply_end_bcl(s, byte_count_limit);
260 size = s->packet_transfer_size;
261 if (size > byte_count_limit) {
262 /* byte count limit must be even if this case */
263 if (byte_count_limit & 1)
264 byte_count_limit--;
265 size = byte_count_limit;
267 s->lcyl = size;
268 s->hcyl = size >> 8;
269 s->elementary_transfer_size = size;
270 /* we cannot transmit more than one sector at a time */
271 if (s->lba != -1) {
272 if (size > (s->cd_sector_size - s->io_buffer_index))
273 size = (s->cd_sector_size - s->io_buffer_index);
275 trace_ide_atapi_cmd_reply_end_new(s, s->status);
277 s->packet_transfer_size -= size;
278 s->elementary_transfer_size -= size;
279 s->io_buffer_index += size;
280 assert(size <= s->io_buffer_total_len);
281 assert(s->io_buffer_index <= s->io_buffer_total_len);
283 /* Some adapters process PIO data right away. In that case, we need
284 * to avoid mutual recursion between ide_transfer_start
285 * and ide_atapi_cmd_reply_end.
287 if (!ide_transfer_start_norecurse(s,
288 s->io_buffer + s->io_buffer_index - size,
289 size, ide_atapi_cmd_reply_end)) {
290 return;
294 /* end of transfer */
295 trace_ide_atapi_cmd_reply_end_eot(s, s->status);
296 ide_atapi_cmd_ok(s);
297 ide_bus_set_irq(s->bus);
300 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
301 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
303 if (size > max_size)
304 size = max_size;
305 s->lba = -1; /* no sector read */
306 s->packet_transfer_size = size;
307 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
308 s->elementary_transfer_size = 0;
310 if (s->atapi_dma) {
311 block_acct_start(blk_get_stats(s->blk), &s->acct, size,
312 BLOCK_ACCT_READ);
313 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
314 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
315 } else {
316 s->status = READY_STAT | SEEK_STAT;
317 s->io_buffer_index = 0;
318 ide_atapi_cmd_reply_end(s);
322 /* start a CD-ROM read command */
323 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
324 int sector_size)
326 assert(0 <= lba && lba < (s->nb_sectors >> 2));
328 s->lba = lba;
329 s->packet_transfer_size = nb_sectors * sector_size;
330 s->elementary_transfer_size = 0;
331 s->io_buffer_index = sector_size;
332 s->cd_sector_size = sector_size;
334 ide_atapi_cmd_reply_end(s);
337 static void ide_atapi_cmd_check_status(IDEState *s)
339 trace_ide_atapi_cmd_check_status(s);
340 s->error = MC_ERR | (UNIT_ATTENTION << 4);
341 s->status = ERR_STAT;
342 s->nsector = 0;
343 ide_bus_set_irq(s->bus);
345 /* ATAPI DMA support */
347 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
349 IDEState *s = opaque;
350 int data_offset, n;
352 if (ret < 0) {
353 if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
354 if (s->bus->error_status) {
355 s->bus->dma->aiocb = NULL;
356 return;
358 goto eot;
362 if (s->io_buffer_size > 0) {
364 * For a cdrom read sector command (s->lba != -1),
365 * adjust the lba for the next s->io_buffer_size chunk
366 * and dma the current chunk.
367 * For a command != read (s->lba == -1), just transfer
368 * the reply data.
370 if (s->lba != -1) {
371 if (s->cd_sector_size == 2352) {
372 n = 1;
373 cd_data_to_raw(s->io_buffer, s->lba);
374 } else {
375 n = s->io_buffer_size >> 11;
377 s->lba += n;
379 s->packet_transfer_size -= s->io_buffer_size;
380 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
381 goto eot;
384 if (s->packet_transfer_size <= 0) {
385 s->status = READY_STAT | SEEK_STAT;
386 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
387 ide_bus_set_irq(s->bus);
388 goto eot;
391 s->io_buffer_index = 0;
392 if (s->cd_sector_size == 2352) {
393 n = 1;
394 s->io_buffer_size = s->cd_sector_size;
395 data_offset = 16;
396 } else {
397 n = s->packet_transfer_size >> 11;
398 if (n > (IDE_DMA_BUF_SECTORS / 4))
399 n = (IDE_DMA_BUF_SECTORS / 4);
400 s->io_buffer_size = n * 2048;
401 data_offset = 0;
403 trace_ide_atapi_cmd_read_dma_cb_aio(s, s->lba, n);
404 qemu_iovec_init_buf(&s->bus->dma->qiov, s->io_buffer + data_offset,
405 n * ATAPI_SECTOR_SIZE);
407 s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
408 &s->bus->dma->qiov, n * 4,
409 ide_atapi_cmd_read_dma_cb, s);
410 return;
412 eot:
413 if (ret < 0) {
414 block_acct_failed(blk_get_stats(s->blk), &s->acct);
415 } else {
416 block_acct_done(blk_get_stats(s->blk), &s->acct);
418 ide_set_inactive(s, false);
421 /* start a CD-ROM read command with DMA */
422 /* XXX: test if DMA is available */
423 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
424 int sector_size)
426 assert(0 <= lba && lba < (s->nb_sectors >> 2));
428 s->lba = lba;
429 s->packet_transfer_size = nb_sectors * sector_size;
430 s->io_buffer_size = 0;
431 s->cd_sector_size = sector_size;
433 block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
434 BLOCK_ACCT_READ);
436 /* XXX: check if BUSY_STAT should be set */
437 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
438 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
441 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
442 int sector_size)
444 trace_ide_atapi_cmd_read(s, s->atapi_dma ? "dma" : "pio",
445 lba, nb_sectors);
446 if (s->atapi_dma) {
447 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
448 } else {
449 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
453 void ide_atapi_dma_restart(IDEState *s)
456 * At this point we can just re-evaluate the packet command and start over.
457 * The presence of ->dma_cb callback in the pre_save ensures that the packet
458 * command has been completely sent and we can safely restart command.
460 s->unit = s->bus->retry_unit;
461 s->bus->dma->ops->restart_dma(s->bus->dma);
462 ide_atapi_cmd(s);
465 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
466 uint16_t profile)
468 uint8_t *buf_profile = buf + 12; /* start of profiles */
470 buf_profile += ((*index) * 4); /* start of indexed profile */
471 stw_be_p(buf_profile, profile);
472 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
474 /* each profile adds 4 bytes to the response */
475 (*index)++;
476 buf[11] += 4; /* Additional Length */
478 return 4;
481 static int ide_dvd_read_structure(IDEState *s, int format,
482 const uint8_t *packet, uint8_t *buf)
484 switch (format) {
485 case 0x0: /* Physical format information */
487 int layer = packet[6];
488 uint64_t total_sectors;
490 if (layer != 0)
491 return -ASC_INV_FIELD_IN_CMD_PACKET;
493 total_sectors = s->nb_sectors >> 2;
494 if (total_sectors == 0) {
495 return -ASC_MEDIUM_NOT_PRESENT;
498 buf[4] = 1; /* DVD-ROM, part version 1 */
499 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
500 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
501 buf[7] = 0; /* default densities */
503 /* FIXME: 0x30000 per spec? */
504 stl_be_p(buf + 8, 0); /* start sector */
505 stl_be_p(buf + 12, total_sectors - 1); /* end sector */
506 stl_be_p(buf + 16, total_sectors - 1); /* l0 end sector */
508 /* Size of buffer, not including 2 byte size field */
509 stw_be_p(buf, 2048 + 2);
511 /* 2k data + 4 byte header */
512 return (2048 + 4);
515 case 0x01: /* DVD copyright information */
516 buf[4] = 0; /* no copyright data */
517 buf[5] = 0; /* no region restrictions */
519 /* Size of buffer, not including 2 byte size field */
520 stw_be_p(buf, 4 + 2);
522 /* 4 byte header + 4 byte data */
523 return (4 + 4);
525 case 0x03: /* BCA information - invalid field for no BCA info */
526 return -ASC_INV_FIELD_IN_CMD_PACKET;
528 case 0x04: /* DVD disc manufacturing information */
529 /* Size of buffer, not including 2 byte size field */
530 stw_be_p(buf, 2048 + 2);
532 /* 2k data + 4 byte header */
533 return (2048 + 4);
535 case 0xff:
537 * This lists all the command capabilities above. Add new ones
538 * in order and update the length and buffer return values.
541 buf[4] = 0x00; /* Physical format */
542 buf[5] = 0x40; /* Not writable, is readable */
543 stw_be_p(buf + 6, 2048 + 4);
545 buf[8] = 0x01; /* Copyright info */
546 buf[9] = 0x40; /* Not writable, is readable */
547 stw_be_p(buf + 10, 4 + 4);
549 buf[12] = 0x03; /* BCA info */
550 buf[13] = 0x40; /* Not writable, is readable */
551 stw_be_p(buf + 14, 188 + 4);
553 buf[16] = 0x04; /* Manufacturing info */
554 buf[17] = 0x40; /* Not writable, is readable */
555 stw_be_p(buf + 18, 2048 + 4);
557 /* Size of buffer, not including 2 byte size field */
558 stw_be_p(buf, 16 + 2);
560 /* data written + 4 byte header */
561 return (16 + 4);
563 default: /* TODO: formats beyond DVD-ROM requires */
564 return -ASC_INV_FIELD_IN_CMD_PACKET;
568 static unsigned int event_status_media(IDEState *s,
569 uint8_t *buf)
571 uint8_t event_code, media_status;
573 media_status = 0;
574 if (s->tray_open) {
575 media_status = MS_TRAY_OPEN;
576 } else if (blk_is_inserted(s->blk)) {
577 media_status = MS_MEDIA_PRESENT;
580 /* Event notification descriptor */
581 event_code = MEC_NO_CHANGE;
582 if (media_status != MS_TRAY_OPEN) {
583 if (s->events.new_media) {
584 event_code = MEC_NEW_MEDIA;
585 s->events.new_media = false;
586 } else if (s->events.eject_request) {
587 event_code = MEC_EJECT_REQUESTED;
588 s->events.eject_request = false;
592 buf[4] = event_code;
593 buf[5] = media_status;
595 /* These fields are reserved, just clear them. */
596 buf[6] = 0;
597 buf[7] = 0;
599 return 8; /* We wrote to 4 extra bytes from the header */
603 * Before transferring data or otherwise signalling acceptance of a command
604 * marked CONDDATA, we must check the validity of the byte_count_limit.
606 static bool validate_bcl(IDEState *s)
608 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
609 if (s->atapi_dma || atapi_byte_count_limit(s)) {
610 return true;
613 /* TODO: Move abort back into core.c and introduce proper error flow between
614 * ATAPI layer and IDE core layer */
615 ide_abort_command(s);
616 return false;
619 static void cmd_get_event_status_notification(IDEState *s,
620 uint8_t *buf)
622 const uint8_t *packet = buf;
624 struct {
625 uint8_t opcode;
626 uint8_t polled; /* lsb bit is polled; others are reserved */
627 uint8_t reserved2[2];
628 uint8_t class;
629 uint8_t reserved3[2];
630 uint16_t len;
631 uint8_t control;
632 } QEMU_PACKED *gesn_cdb;
634 struct {
635 uint16_t len;
636 uint8_t notification_class;
637 uint8_t supported_events;
638 } QEMU_PACKED *gesn_event_header;
639 unsigned int max_len, used_len;
641 gesn_cdb = (void *)packet;
642 gesn_event_header = (void *)buf;
644 max_len = be16_to_cpu(gesn_cdb->len);
646 /* It is fine by the MMC spec to not support async mode operations */
647 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
648 /* Only polling is supported, asynchronous mode is not. */
649 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
650 ASC_INV_FIELD_IN_CMD_PACKET);
651 return;
654 /* polling mode operation */
657 * These are the supported events.
659 * We currently only support requests of the 'media' type.
660 * Notification class requests and supported event classes are bitmasks,
661 * but they are build from the same values as the "notification class"
662 * field.
664 gesn_event_header->supported_events = 1 << GESN_MEDIA;
667 * We use |= below to set the class field; other bits in this byte
668 * are reserved now but this is useful to do if we have to use the
669 * reserved fields later.
671 gesn_event_header->notification_class = 0;
674 * Responses to requests are to be based on request priority. The
675 * notification_class_request_type enum above specifies the
676 * priority: upper elements are higher prio than lower ones.
678 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
679 gesn_event_header->notification_class |= GESN_MEDIA;
680 used_len = event_status_media(s, buf);
681 } else {
682 gesn_event_header->notification_class = 0x80; /* No event available */
683 used_len = sizeof(*gesn_event_header);
685 gesn_event_header->len = cpu_to_be16(used_len
686 - sizeof(*gesn_event_header));
687 ide_atapi_cmd_reply(s, used_len, max_len);
690 static void cmd_request_sense(IDEState *s, uint8_t *buf)
692 int max_len = buf[4];
694 memset(buf, 0, 18);
695 buf[0] = 0x70 | (1 << 7);
696 buf[2] = s->sense_key;
697 buf[7] = 10;
698 buf[12] = s->asc;
700 if (s->sense_key == UNIT_ATTENTION) {
701 s->sense_key = NO_SENSE;
704 ide_atapi_cmd_reply(s, 18, max_len);
707 static void cmd_inquiry(IDEState *s, uint8_t *buf)
709 uint8_t page_code = buf[2];
710 int max_len = buf[4];
712 unsigned idx = 0;
713 unsigned size_idx;
714 unsigned preamble_len;
716 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
717 * we are being asked for a specific page of info indicated by byte 2. */
718 if (buf[1] & 0x01) {
719 preamble_len = 4;
720 size_idx = 3;
722 buf[idx++] = 0x05; /* CD-ROM */
723 buf[idx++] = page_code; /* Page Code */
724 buf[idx++] = 0x00; /* reserved */
725 idx++; /* length (set later) */
727 switch (page_code) {
728 case 0x00:
729 /* Supported Pages: List of supported VPD responses. */
730 buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
731 buf[idx++] = 0x83; /* 0x83: Device Identification. */
732 break;
734 case 0x83:
735 /* Device Identification. Each entry is optional, but the entries
736 * included here are modeled after libata's VPD responses.
737 * If the response is given, at least one entry must be present. */
739 /* Entry 1: Serial */
740 if (idx + 24 > max_len) {
741 /* Not enough room for even the first entry: */
742 /* 4 byte header + 20 byte string */
743 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
744 ASC_DATA_PHASE_ERROR);
745 return;
747 buf[idx++] = 0x02; /* Ascii */
748 buf[idx++] = 0x00; /* Vendor Specific */
749 buf[idx++] = 0x00;
750 buf[idx++] = 20; /* Remaining length */
751 padstr8(buf + idx, 20, s->drive_serial_str);
752 idx += 20;
754 /* Entry 2: Drive Model and Serial */
755 if (idx + 72 > max_len) {
756 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
757 goto out;
759 buf[idx++] = 0x02; /* Ascii */
760 buf[idx++] = 0x01; /* T10 Vendor */
761 buf[idx++] = 0x00;
762 buf[idx++] = 68;
763 padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
764 idx += 8;
765 padstr8(buf + idx, 40, s->drive_model_str);
766 idx += 40;
767 padstr8(buf + idx, 20, s->drive_serial_str);
768 idx += 20;
770 /* Entry 3: WWN */
771 if (s->wwn && (idx + 12 <= max_len)) {
772 /* 4 byte header + 8 byte wwn */
773 buf[idx++] = 0x01; /* Binary */
774 buf[idx++] = 0x03; /* NAA */
775 buf[idx++] = 0x00;
776 buf[idx++] = 0x08;
777 stq_be_p(&buf[idx], s->wwn);
778 idx += 8;
780 break;
782 default:
783 /* SPC-3, revision 23 sec. 6.4 */
784 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
785 ASC_INV_FIELD_IN_CMD_PACKET);
786 return;
788 } else {
789 preamble_len = 5;
790 size_idx = 4;
792 buf[0] = 0x05; /* CD-ROM */
793 buf[1] = 0x80; /* removable */
794 buf[2] = 0x00; /* ISO */
795 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
796 /* buf[size_idx] set below. */
797 buf[5] = 0; /* reserved */
798 buf[6] = 0; /* reserved */
799 buf[7] = 0; /* reserved */
800 padstr8(buf + 8, 8, "QEMU");
801 padstr8(buf + 16, 16, "QEMU DVD-ROM");
802 padstr8(buf + 32, 4, s->version);
803 idx = 36;
806 out:
807 buf[size_idx] = idx - preamble_len;
808 ide_atapi_cmd_reply(s, idx, max_len);
811 static void cmd_get_configuration(IDEState *s, uint8_t *buf)
813 uint32_t len;
814 uint8_t index = 0;
815 int max_len;
817 /* only feature 0 is supported */
818 if (buf[2] != 0 || buf[3] != 0) {
819 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
820 ASC_INV_FIELD_IN_CMD_PACKET);
821 return;
824 /* XXX: could result in alignment problems in some architectures */
825 max_len = lduw_be_p(buf + 7);
828 * XXX: avoid overflow for io_buffer if max_len is bigger than
829 * the size of that buffer (dimensioned to max number of
830 * sectors to transfer at once)
832 * Only a problem if the feature/profiles grow.
834 if (max_len > BDRV_SECTOR_SIZE) {
835 /* XXX: assume 1 sector */
836 max_len = BDRV_SECTOR_SIZE;
839 memset(buf, 0, max_len);
841 * the number of sectors from the media tells us which profile
842 * to use as current. 0 means there is no media
844 if (media_is_dvd(s)) {
845 stw_be_p(buf + 6, MMC_PROFILE_DVD_ROM);
846 } else if (media_is_cd(s)) {
847 stw_be_p(buf + 6, MMC_PROFILE_CD_ROM);
850 buf[10] = 0x02 | 0x01; /* persistent and current */
851 len = 12; /* headers: 8 + 4 */
852 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
853 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
854 stl_be_p(buf, len - 4); /* data length */
856 ide_atapi_cmd_reply(s, len, max_len);
859 static void cmd_mode_sense(IDEState *s, uint8_t *buf)
861 int action, code;
862 int max_len;
864 max_len = lduw_be_p(buf + 7);
865 action = buf[2] >> 6;
866 code = buf[2] & 0x3f;
868 switch(action) {
869 case 0: /* current values */
870 switch(code) {
871 case MODE_PAGE_R_W_ERROR: /* error recovery */
872 stw_be_p(&buf[0], 16 - 2);
873 buf[2] = 0x70;
874 buf[3] = 0;
875 buf[4] = 0;
876 buf[5] = 0;
877 buf[6] = 0;
878 buf[7] = 0;
880 buf[8] = MODE_PAGE_R_W_ERROR;
881 buf[9] = 16 - 10;
882 buf[10] = 0x00;
883 buf[11] = 0x05;
884 buf[12] = 0x00;
885 buf[13] = 0x00;
886 buf[14] = 0x00;
887 buf[15] = 0x00;
888 ide_atapi_cmd_reply(s, 16, max_len);
889 break;
890 case MODE_PAGE_AUDIO_CTL:
891 stw_be_p(&buf[0], 24 - 2);
892 buf[2] = 0x70;
893 buf[3] = 0;
894 buf[4] = 0;
895 buf[5] = 0;
896 buf[6] = 0;
897 buf[7] = 0;
899 buf[8] = MODE_PAGE_AUDIO_CTL;
900 buf[9] = 24 - 10;
901 /* Fill with CDROM audio volume */
902 buf[17] = 0;
903 buf[19] = 0;
904 buf[21] = 0;
905 buf[23] = 0;
907 ide_atapi_cmd_reply(s, 24, max_len);
908 break;
909 case MODE_PAGE_CAPABILITIES:
910 stw_be_p(&buf[0], 30 - 2);
911 buf[2] = 0x70;
912 buf[3] = 0;
913 buf[4] = 0;
914 buf[5] = 0;
915 buf[6] = 0;
916 buf[7] = 0;
918 buf[8] = MODE_PAGE_CAPABILITIES;
919 buf[9] = 30 - 10;
920 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
921 buf[11] = 0x00;
923 /* Claim PLAY_AUDIO capability (0x01) since some Linux
924 code checks for this to automount media. */
925 buf[12] = 0x71;
926 buf[13] = 3 << 5;
927 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
928 if (s->tray_locked) {
929 buf[14] |= 1 << 1;
931 buf[15] = 0x00; /* No volume & mute control, no changer */
932 stw_be_p(&buf[16], 704); /* 4x read speed */
933 buf[18] = 0; /* Two volume levels */
934 buf[19] = 2;
935 stw_be_p(&buf[20], 512); /* 512k buffer */
936 stw_be_p(&buf[22], 704); /* 4x read speed current */
937 buf[24] = 0;
938 buf[25] = 0;
939 buf[26] = 0;
940 buf[27] = 0;
941 buf[28] = 0;
942 buf[29] = 0;
943 ide_atapi_cmd_reply(s, 30, max_len);
944 break;
945 default:
946 goto error_cmd;
948 break;
949 case 1: /* changeable values */
950 goto error_cmd;
951 case 2: /* default values */
952 goto error_cmd;
953 default:
954 case 3: /* saved values */
955 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
956 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
957 break;
959 return;
961 error_cmd:
962 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
965 static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
967 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
968 * come here, we know that it's ready. */
969 ide_atapi_cmd_ok(s);
972 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
974 s->tray_locked = buf[4] & 1;
975 blk_lock_medium(s->blk, buf[4] & 1);
976 ide_atapi_cmd_ok(s);
979 static void cmd_read(IDEState *s, uint8_t* buf)
981 unsigned int nb_sectors, lba;
983 /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
984 uint64_t total_sectors = s->nb_sectors >> 2;
986 if (buf[0] == GPCMD_READ_10) {
987 nb_sectors = lduw_be_p(buf + 7);
988 } else {
989 nb_sectors = ldl_be_p(buf + 6);
991 if (nb_sectors == 0) {
992 ide_atapi_cmd_ok(s);
993 return;
996 lba = ldl_be_p(buf + 2);
997 if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
998 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
999 return;
1002 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1005 static void cmd_read_cd(IDEState *s, uint8_t* buf)
1007 unsigned int nb_sectors, lba, transfer_request;
1009 /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
1010 uint64_t total_sectors = s->nb_sectors >> 2;
1012 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
1013 if (nb_sectors == 0) {
1014 ide_atapi_cmd_ok(s);
1015 return;
1018 lba = ldl_be_p(buf + 2);
1019 if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
1020 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1021 return;
1024 transfer_request = buf[9] & 0xf8;
1025 if (transfer_request == 0x00) {
1026 /* nothing */
1027 ide_atapi_cmd_ok(s);
1028 return;
1031 /* Check validity of BCL before transferring data */
1032 if (!validate_bcl(s)) {
1033 return;
1036 switch (transfer_request) {
1037 case 0x10:
1038 /* normal read */
1039 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1040 break;
1041 case 0xf8:
1042 /* read all data */
1043 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1044 break;
1045 default:
1046 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1047 ASC_INV_FIELD_IN_CMD_PACKET);
1048 break;
1052 static void cmd_seek(IDEState *s, uint8_t* buf)
1054 unsigned int lba;
1055 uint64_t total_sectors = s->nb_sectors >> 2;
1057 lba = ldl_be_p(buf + 2);
1058 if (lba >= total_sectors) {
1059 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1060 return;
1063 ide_atapi_cmd_ok(s);
1066 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
1068 int sense;
1069 bool start = buf[4] & 1;
1070 bool loej = buf[4] & 2; /* load on start, eject on !start */
1071 int pwrcnd = buf[4] & 0xf0;
1073 if (pwrcnd) {
1074 /* eject/load only happens for power condition == 0 */
1075 ide_atapi_cmd_ok(s);
1076 return;
1079 if (loej) {
1080 if (!start && !s->tray_open && s->tray_locked) {
1081 sense = blk_is_inserted(s->blk)
1082 ? NOT_READY : ILLEGAL_REQUEST;
1083 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1084 return;
1087 if (s->tray_open != !start) {
1088 blk_eject(s->blk, !start);
1089 s->tray_open = !start;
1093 ide_atapi_cmd_ok(s);
1096 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
1098 int max_len = lduw_be_p(buf + 8);
1100 stw_be_p(buf, 0);
1101 /* no current LBA */
1102 buf[2] = 0;
1103 buf[3] = 0;
1104 buf[4] = 0;
1105 buf[5] = 1;
1106 stw_be_p(buf + 6, 0);
1107 ide_atapi_cmd_reply(s, 8, max_len);
1110 static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
1112 int format, msf, start_track, len;
1113 int max_len;
1114 uint64_t total_sectors = s->nb_sectors >> 2;
1116 max_len = lduw_be_p(buf + 7);
1117 format = buf[9] >> 6;
1118 msf = (buf[1] >> 1) & 1;
1119 start_track = buf[6];
1121 switch(format) {
1122 case 0:
1123 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1124 if (len < 0)
1125 goto error_cmd;
1126 ide_atapi_cmd_reply(s, len, max_len);
1127 break;
1128 case 1:
1129 /* multi session : only a single session defined */
1130 memset(buf, 0, 12);
1131 buf[1] = 0x0a;
1132 buf[2] = 0x01;
1133 buf[3] = 0x01;
1134 ide_atapi_cmd_reply(s, 12, max_len);
1135 break;
1136 case 2:
1137 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1138 if (len < 0)
1139 goto error_cmd;
1140 ide_atapi_cmd_reply(s, len, max_len);
1141 break;
1142 default:
1143 error_cmd:
1144 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1145 ASC_INV_FIELD_IN_CMD_PACKET);
1149 static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
1151 uint64_t total_sectors = s->nb_sectors >> 2;
1153 /* NOTE: it is really the number of sectors minus 1 */
1154 stl_be_p(buf, total_sectors - 1);
1155 stl_be_p(buf + 4, 2048);
1156 ide_atapi_cmd_reply(s, 8, 8);
1159 static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
1161 uint8_t type = buf[1] & 7;
1162 uint32_t max_len = lduw_be_p(buf + 7);
1164 /* Types 1/2 are only defined for Blu-Ray. */
1165 if (type != 0) {
1166 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1167 ASC_INV_FIELD_IN_CMD_PACKET);
1168 return;
1171 memset(buf, 0, 34);
1172 buf[1] = 32;
1173 buf[2] = 0xe; /* last session complete, disc finalized */
1174 buf[3] = 1; /* first track on disc */
1175 buf[4] = 1; /* # of sessions */
1176 buf[5] = 1; /* first track of last session */
1177 buf[6] = 1; /* last track of last session */
1178 buf[7] = 0x20; /* unrestricted use */
1179 buf[8] = 0x00; /* CD-ROM or DVD-ROM */
1180 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1181 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1182 /* 24-31: disc bar code */
1183 /* 32: disc application code */
1184 /* 33: number of OPC tables */
1186 ide_atapi_cmd_reply(s, 34, max_len);
1189 static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1191 int max_len;
1192 int media = buf[1];
1193 int format = buf[7];
1194 int ret;
1196 max_len = lduw_be_p(buf + 8);
1198 if (format < 0xff) {
1199 if (media_is_cd(s)) {
1200 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1201 ASC_INCOMPATIBLE_FORMAT);
1202 return;
1203 } else if (!media_present(s)) {
1204 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1205 ASC_INV_FIELD_IN_CMD_PACKET);
1206 return;
1210 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * BDRV_SECTOR_SIZE + 4 ?
1211 IDE_DMA_BUF_SECTORS * BDRV_SECTOR_SIZE + 4 : max_len);
1213 switch (format) {
1214 case 0x00 ... 0x7f:
1215 case 0xff:
1216 if (media == 0) {
1217 ret = ide_dvd_read_structure(s, format, buf, buf);
1219 if (ret < 0) {
1220 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1221 } else {
1222 ide_atapi_cmd_reply(s, ret, max_len);
1225 break;
1227 /* TODO: BD support, fall through for now */
1229 /* Generic disk structures */
1230 case 0x80: /* TODO: AACS volume identifier */
1231 case 0x81: /* TODO: AACS media serial number */
1232 case 0x82: /* TODO: AACS media identifier */
1233 case 0x83: /* TODO: AACS media key block */
1234 case 0x90: /* TODO: List of recognized format layers */
1235 case 0xc0: /* TODO: Write protection status */
1236 default:
1237 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1238 ASC_INV_FIELD_IN_CMD_PACKET);
1239 break;
1243 static void cmd_set_speed(IDEState *s, uint8_t* buf)
1245 ide_atapi_cmd_ok(s);
1248 enum {
1250 * Only commands flagged as ALLOW_UA are allowed to run under a
1251 * unit attention condition. (See MMC-5, section 4.1.6.1)
1253 ALLOW_UA = 0x01,
1256 * Commands flagged with CHECK_READY can only execute if a medium is present.
1257 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1258 * 4.1.8)
1260 CHECK_READY = 0x02,
1263 * Commands flagged with NONDATA do not in any circumstances return
1264 * any data via ide_atapi_cmd_reply. These commands are exempt from
1265 * the normal byte_count_limit constraints.
1266 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1268 NONDATA = 0x04,
1271 * CONDDATA implies a command that transfers data only conditionally based
1272 * on the presence of suboptions. It should be exempt from the BCL check at
1273 * command validation time, but it needs to be checked at the command
1274 * handler level instead.
1276 CONDDATA = 0x08,
1279 static const struct AtapiCmd {
1280 void (*handler)(IDEState *s, uint8_t *buf);
1281 int flags;
1282 } atapi_cmd_table[0x100] = {
1283 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
1284 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1285 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
1286 [ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */
1287 [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
1288 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
1289 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
1290 [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
1291 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
1292 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1293 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1294 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
1295 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
1296 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1297 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
1298 [ 0xbb ] = { cmd_set_speed, NONDATA },
1299 [ 0xbd ] = { cmd_mechanism_status, 0 },
1300 [ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA },
1301 /* [1] handler detects and reports not ready condition itself */
1304 void ide_atapi_cmd(IDEState *s)
1306 uint8_t *buf = s->io_buffer;
1307 const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
1309 trace_ide_atapi_cmd(s, s->io_buffer[0]);
1311 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET)) {
1312 /* Each pretty-printed byte needs two bytes and a space; */
1313 char *ppacket = g_malloc(ATAPI_PACKET_SIZE * 3 + 1);
1314 int i;
1315 for (i = 0; i < ATAPI_PACKET_SIZE; i++) {
1316 sprintf(ppacket + (i * 3), "%02x ", buf[i]);
1318 trace_ide_atapi_cmd_packet(s, s->lcyl | (s->hcyl << 8), ppacket);
1319 g_free(ppacket);
1323 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1324 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1325 * condition response unless a higher priority status, defined by the drive
1326 * here, is pending.
1328 if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
1329 ide_atapi_cmd_check_status(s);
1330 return;
1333 * When a CD gets changed, we have to report an ejected state and
1334 * then a loaded state to guests so that they detect tray
1335 * open/close and media change events. Guests that do not use
1336 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1337 * states rely on this behavior.
1339 if (!(cmd->flags & ALLOW_UA) &&
1340 !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1342 if (s->cdrom_changed == 1) {
1343 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1344 s->cdrom_changed = 2;
1345 } else {
1346 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1347 s->cdrom_changed = 0;
1350 return;
1353 /* Report a Not Ready condition if appropriate for the command */
1354 if ((cmd->flags & CHECK_READY) &&
1355 (!media_present(s) || !blk_is_inserted(s->blk)))
1357 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1358 return;
1361 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1362 * If this is a data-transferring PIO command and BCL is 0,
1363 * we abort at the /ATA/ level, not the ATAPI level.
1364 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1365 if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
1366 if (!validate_bcl(s)) {
1367 return;
1371 /* Execute the command */
1372 if (cmd->handler) {
1373 cmd->handler(s, buf);
1374 return;
1377 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);