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