blob: 8af1cfdd7e792e16e92dde67ae8287426964ff3d [file] [log] [blame]
Kevin Wolf33231e02011-04-18 16:45:49 +02001/*
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
Kevin Wolf33231e02011-04-18 16:45:49 +020074static inline int media_present(IDEState *s)
75{
Markus Armbrustera1aff5b2011-09-06 18:58:41 +020076 return !s->tray_open && s->nb_sectors > 0;
Kevin Wolf33231e02011-04-18 16:45:49 +020077}
78
Amit Shaha7acf552011-04-28 20:04:40 +053079/* XXX: DVDs that could fit on a CD will be reported as a CD */
Kevin Wolf33231e02011-04-18 16:45:49 +020080static 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
Christoph Hellwiga597e792011-08-25 08:26:01 +0200107static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
Kevin Wolf33231e02011-04-18 16:45:49 +0200108{
109 int ret;
110
111 switch(sector_size) {
112 case 2048:
Christoph Hellwiga597e792011-08-25 08:26:01 +0200113 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);
Kevin Wolf33231e02011-04-18 16:45:49 +0200116 break;
117 case 2352:
Christoph Hellwiga597e792011-08-25 08:26:01 +0200118 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);
Kevin Wolf33231e02011-04-18 16:45:49 +0200121 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) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200157 ide_atapi_cmd_error(s, NOT_READY,
Kevin Wolf33231e02011-04-18 16:45:49 +0200158 ASC_MEDIUM_NOT_PRESENT);
159 } else {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200160 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolf33231e02011-04-18 16:45:49 +0200161 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) {
Christoph Hellwiga597e792011-08-25 08:26:01 +0200187 ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
Kevin Wolf33231e02011-04-18 16:45:49 +0200188 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) {
Christoph Hellwiga597e792011-08-25 08:26:01 +0200256 bdrv_acct_start(s->bs, &s->acct, size, BDRV_ACCT_READ);
Kevin Wolf33231e02011-04-18 16:45:49 +0200257 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
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200285 s->error = MC_ERR | (UNIT_ATTENTION << 4);
Kevin Wolf33231e02011-04-18 16:45:49 +0200286 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);
Christoph Hellwiga597e792011-08-25 08:26:01 +0200329 goto eot;
Kevin Wolf33231e02011-04-18 16:45:49 +0200330 }
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
Christoph Hellwiga597e792011-08-25 08:26:01 +0200347
Kevin Wolf33231e02011-04-18 16:45:49 +0200348 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);
Christoph Hellwiga597e792011-08-25 08:26:01 +0200351
Kevin Wolf33231e02011-04-18 16:45:49 +0200352 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 */
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200357 ide_atapi_cmd_error(s, NOT_READY,
Kevin Wolf33231e02011-04-18 16:45:49 +0200358 ASC_MEDIUM_NOT_PRESENT);
359 goto eot;
360 }
Christoph Hellwiga597e792011-08-25 08:26:01 +0200361
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);
Kevin Wolf33231e02011-04-18 16:45:49 +0200367}
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
Christoph Hellwiga597e792011-08-25 08:26:01 +0200380 bdrv_acct_start(s->bs, &s->acct, s->packet_transfer_size, BDRV_ACCT_READ);
381
Kevin Wolf33231e02011-04-18 16:45:49 +0200382 /* 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
Kevin Wolfe119bca2011-04-19 13:13:44 +0200430 total_sectors = s->nb_sectors >> 2;
431 if (total_sectors == 0) {
Kevin Wolf33231e02011-04-18 16:45:49 +0200432 return -ASC_MEDIUM_NOT_PRESENT;
Kevin Wolfe119bca2011-04-19 13:13:44 +0200433 }
Kevin Wolf33231e02011-04-18 16:45:49 +0200434
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{
Kevin Wolf33231e02011-04-18 16:45:49 +0200508 uint8_t event_code, media_status;
509
510 media_status = 0;
Markus Armbrusterdd063332011-09-06 18:58:38 +0200511 if (s->tray_open) {
Kevin Wolf33231e02011-04-18 16:45:49 +0200512 media_status = MS_TRAY_OPEN;
513 } else if (bdrv_is_inserted(s->bs)) {
514 media_status = MS_MEDIA_PRESENT;
515 }
516
517 /* Event notification descriptor */
518 event_code = MEC_NO_CHANGE;
Paolo Bonzini2df0a3a2011-10-25 12:53:39 +0200519 if (media_status != MS_TRAY_OPEN) {
520 if (s->events.new_media) {
521 event_code = MEC_NEW_MEDIA;
522 s->events.new_media = false;
523 } else if (s->events.eject_request) {
524 event_code = MEC_EJECT_REQUESTED;
525 s->events.eject_request = false;
526 }
Kevin Wolf33231e02011-04-18 16:45:49 +0200527 }
528
529 buf[4] = event_code;
530 buf[5] = media_status;
531
532 /* These fields are reserved, just clear them. */
533 buf[6] = 0;
534 buf[7] = 0;
535
536 return 8; /* We wrote to 4 extra bytes from the header */
537}
538
Kevin Wolfe1a064f2011-04-18 17:55:08 +0200539static void cmd_get_event_status_notification(IDEState *s,
540 uint8_t *buf)
Kevin Wolf33231e02011-04-18 16:45:49 +0200541{
Kevin Wolfe1a064f2011-04-18 17:55:08 +0200542 const uint8_t *packet = buf;
543
Kevin Wolf33231e02011-04-18 16:45:49 +0200544 struct {
545 uint8_t opcode;
546 uint8_t polled; /* lsb bit is polled; others are reserved */
547 uint8_t reserved2[2];
548 uint8_t class;
549 uint8_t reserved3[2];
550 uint16_t len;
551 uint8_t control;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200552 } QEMU_PACKED *gesn_cdb;
Kevin Wolf33231e02011-04-18 16:45:49 +0200553
554 struct {
555 uint16_t len;
556 uint8_t notification_class;
557 uint8_t supported_events;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200558 } QEMU_PACKED *gesn_event_header;
Kevin Wolf33231e02011-04-18 16:45:49 +0200559 unsigned int max_len, used_len;
560
561 gesn_cdb = (void *)packet;
562 gesn_event_header = (void *)buf;
563
564 max_len = be16_to_cpu(gesn_cdb->len);
565
566 /* It is fine by the MMC spec to not support async mode operations */
567 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
568 /* Only polling is supported, asynchronous mode is not. */
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200569 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolf33231e02011-04-18 16:45:49 +0200570 ASC_INV_FIELD_IN_CMD_PACKET);
571 return;
572 }
573
574 /* polling mode operation */
575
576 /*
577 * These are the supported events.
578 *
579 * We currently only support requests of the 'media' type.
Paolo Bonzinif0f992e2011-09-13 16:00:52 +0200580 * Notification class requests and supported event classes are bitmasks,
581 * but they are build from the same values as the "notification class"
582 * field.
Kevin Wolf33231e02011-04-18 16:45:49 +0200583 */
Paolo Bonzinif0f992e2011-09-13 16:00:52 +0200584 gesn_event_header->supported_events = 1 << GESN_MEDIA;
Kevin Wolf33231e02011-04-18 16:45:49 +0200585
586 /*
587 * We use |= below to set the class field; other bits in this byte
588 * are reserved now but this is useful to do if we have to use the
589 * reserved fields later.
590 */
591 gesn_event_header->notification_class = 0;
592
593 /*
594 * Responses to requests are to be based on request priority. The
595 * notification_class_request_type enum above specifies the
596 * priority: upper elements are higher prio than lower ones.
597 */
Paolo Bonzinif0f992e2011-09-13 16:00:52 +0200598 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
599 gesn_event_header->notification_class |= GESN_MEDIA;
Kevin Wolf33231e02011-04-18 16:45:49 +0200600 used_len = event_status_media(s, buf);
601 } else {
602 gesn_event_header->notification_class = 0x80; /* No event available */
603 used_len = sizeof(*gesn_event_header);
604 }
605 gesn_event_header->len = cpu_to_be16(used_len
606 - sizeof(*gesn_event_header));
607 ide_atapi_cmd_reply(s, used_len, max_len);
608}
609
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200610static void cmd_request_sense(IDEState *s, uint8_t *buf)
611{
612 int max_len = buf[4];
613
614 memset(buf, 0, 18);
615 buf[0] = 0x70 | (1 << 7);
616 buf[2] = s->sense_key;
617 buf[7] = 10;
618 buf[12] = s->asc;
619
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200620 if (s->sense_key == UNIT_ATTENTION) {
621 s->sense_key = NO_SENSE;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200622 }
623
624 ide_atapi_cmd_reply(s, 18, max_len);
625}
626
627static void cmd_inquiry(IDEState *s, uint8_t *buf)
628{
629 int max_len = buf[4];
630
631 buf[0] = 0x05; /* CD-ROM */
632 buf[1] = 0x80; /* removable */
633 buf[2] = 0x00; /* ISO */
634 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
635 buf[4] = 31; /* additional length */
636 buf[5] = 0; /* reserved */
637 buf[6] = 0; /* reserved */
638 buf[7] = 0; /* reserved */
639 padstr8(buf + 8, 8, "QEMU");
640 padstr8(buf + 16, 16, "QEMU DVD-ROM");
641 padstr8(buf + 32, 4, s->version);
642 ide_atapi_cmd_reply(s, 36, max_len);
643}
644
645static void cmd_get_configuration(IDEState *s, uint8_t *buf)
646{
647 uint32_t len;
648 uint8_t index = 0;
649 int max_len;
650
651 /* only feature 0 is supported */
652 if (buf[2] != 0 || buf[3] != 0) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200653 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200654 ASC_INV_FIELD_IN_CMD_PACKET);
655 return;
656 }
657
658 /* XXX: could result in alignment problems in some architectures */
659 max_len = ube16_to_cpu(buf + 7);
660
661 /*
662 * XXX: avoid overflow for io_buffer if max_len is bigger than
663 * the size of that buffer (dimensioned to max number of
664 * sectors to transfer at once)
665 *
666 * Only a problem if the feature/profiles grow.
667 */
668 if (max_len > 512) {
669 /* XXX: assume 1 sector */
670 max_len = 512;
671 }
672
673 memset(buf, 0, max_len);
674 /*
675 * the number of sectors from the media tells us which profile
676 * to use as current. 0 means there is no media
677 */
678 if (media_is_dvd(s)) {
679 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
680 } else if (media_is_cd(s)) {
681 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
682 }
683
684 buf[10] = 0x02 | 0x01; /* persistent and current */
685 len = 12; /* headers: 8 + 4 */
686 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
687 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
688 cpu_to_ube32(buf, len - 4); /* data length */
689
690 ide_atapi_cmd_reply(s, len, max_len);
691}
692
693static void cmd_mode_sense(IDEState *s, uint8_t *buf)
694{
695 int action, code;
696 int max_len;
697
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100698 max_len = ube16_to_cpu(buf + 7);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200699 action = buf[2] >> 6;
700 code = buf[2] & 0x3f;
701
702 switch(action) {
703 case 0: /* current values */
704 switch(code) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200705 case MODE_PAGE_R_W_ERROR: /* error recovery */
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100706 cpu_to_ube16(&buf[0], 16 - 2);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200707 buf[2] = 0x70;
708 buf[3] = 0;
709 buf[4] = 0;
710 buf[5] = 0;
711 buf[6] = 0;
712 buf[7] = 0;
713
Paolo Bonziniaf0e1ea2011-10-17 16:34:59 +0200714 buf[8] = MODE_PAGE_R_W_ERROR;
715 buf[9] = 16 - 10;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200716 buf[10] = 0x00;
717 buf[11] = 0x05;
718 buf[12] = 0x00;
719 buf[13] = 0x00;
720 buf[14] = 0x00;
721 buf[15] = 0x00;
722 ide_atapi_cmd_reply(s, 16, max_len);
723 break;
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200724 case MODE_PAGE_AUDIO_CTL:
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100725 cpu_to_ube16(&buf[0], 24 - 2);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200726 buf[2] = 0x70;
727 buf[3] = 0;
728 buf[4] = 0;
729 buf[5] = 0;
730 buf[6] = 0;
731 buf[7] = 0;
732
Paolo Bonziniaf0e1ea2011-10-17 16:34:59 +0200733 buf[8] = MODE_PAGE_AUDIO_CTL;
734 buf[9] = 24 - 10;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200735 /* Fill with CDROM audio volume */
736 buf[17] = 0;
737 buf[19] = 0;
738 buf[21] = 0;
739 buf[23] = 0;
740
741 ide_atapi_cmd_reply(s, 24, max_len);
742 break;
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200743 case MODE_PAGE_CAPABILITIES:
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100744 cpu_to_ube16(&buf[0], 30 - 2);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200745 buf[2] = 0x70;
746 buf[3] = 0;
747 buf[4] = 0;
748 buf[5] = 0;
749 buf[6] = 0;
750 buf[7] = 0;
751
Paolo Bonziniaf0e1ea2011-10-17 16:34:59 +0200752 buf[8] = MODE_PAGE_CAPABILITIES;
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100753 buf[9] = 30 - 10;
Paolo Bonzinia07c7dc2011-09-13 15:08:22 +0200754 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200755 buf[11] = 0x00;
756
757 /* Claim PLAY_AUDIO capability (0x01) since some Linux
758 code checks for this to automount media. */
759 buf[12] = 0x71;
760 buf[13] = 3 << 5;
761 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
Markus Armbrustera0a75732011-09-06 18:58:43 +0200762 if (s->tray_locked) {
Paolo Bonzinia07c7dc2011-09-13 15:08:22 +0200763 buf[14] |= 1 << 1;
Markus Armbrustera0a75732011-09-06 18:58:43 +0200764 }
Paolo Bonzinia07c7dc2011-09-13 15:08:22 +0200765 buf[15] = 0x00; /* No volume & mute control, no changer */
766 cpu_to_ube16(&buf[16], 704); /* 4x read speed */
767 buf[18] = 0; /* Two volume levels */
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200768 buf[19] = 2;
Paolo Bonzinia07c7dc2011-09-13 15:08:22 +0200769 cpu_to_ube16(&buf[20], 512); /* 512k buffer */
770 cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200771 buf[24] = 0;
772 buf[25] = 0;
773 buf[26] = 0;
774 buf[27] = 0;
Paolo Bonzini2c20ae12011-11-14 14:31:47 +0100775 buf[28] = 0;
776 buf[29] = 0;
777 ide_atapi_cmd_reply(s, 30, max_len);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200778 break;
779 default:
780 goto error_cmd;
781 }
782 break;
783 case 1: /* changeable values */
784 goto error_cmd;
785 case 2: /* default values */
786 goto error_cmd;
787 default:
788 case 3: /* saved values */
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200789 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200790 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
791 break;
792 }
793 return;
794
795error_cmd:
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200796 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200797}
798
799static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
800{
Kevin Wolf7a2c4b82011-04-19 13:15:52 +0200801 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
802 * come here, we know that it's ready. */
803 ide_atapi_cmd_ok(s);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200804}
805
806static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
807{
Markus Armbrustera0a75732011-09-06 18:58:43 +0200808 s->tray_locked = buf[4] & 1;
Markus Armbruster025e8492011-09-06 18:58:47 +0200809 bdrv_lock_medium(s->bs, buf[4] & 1);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200810 ide_atapi_cmd_ok(s);
811}
812
813static void cmd_read(IDEState *s, uint8_t* buf)
814{
815 int nb_sectors, lba;
816
817 if (buf[0] == GPCMD_READ_10) {
818 nb_sectors = ube16_to_cpu(buf + 7);
819 } else {
820 nb_sectors = ube32_to_cpu(buf + 6);
821 }
822
823 lba = ube32_to_cpu(buf + 2);
824 if (nb_sectors == 0) {
825 ide_atapi_cmd_ok(s);
826 return;
827 }
828
829 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
830}
831
832static void cmd_read_cd(IDEState *s, uint8_t* buf)
833{
834 int nb_sectors, lba, transfer_request;
835
836 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
837 lba = ube32_to_cpu(buf + 2);
838
839 if (nb_sectors == 0) {
840 ide_atapi_cmd_ok(s);
841 return;
842 }
843
844 transfer_request = buf[9];
845 switch(transfer_request & 0xf8) {
846 case 0x00:
847 /* nothing */
848 ide_atapi_cmd_ok(s);
849 break;
850 case 0x10:
851 /* normal read */
852 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
853 break;
854 case 0xf8:
855 /* read all data */
856 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
857 break;
858 default:
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200859 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200860 ASC_INV_FIELD_IN_CMD_PACKET);
861 break;
862 }
863}
864
865static void cmd_seek(IDEState *s, uint8_t* buf)
866{
867 unsigned int lba;
Kevin Wolfe119bca2011-04-19 13:13:44 +0200868 uint64_t total_sectors = s->nb_sectors >> 2;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200869
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200870 lba = ube32_to_cpu(buf + 2);
871 if (lba >= total_sectors) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200872 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200873 return;
874 }
875
876 ide_atapi_cmd_ok(s);
877}
878
879static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
880{
Markus Armbrusterfdec4402011-09-06 18:58:45 +0200881 int sense;
Markus Armbrusterf0776562011-09-06 18:58:37 +0200882 bool start = buf[4] & 1;
883 bool loej = buf[4] & 2; /* load on start, eject on !start */
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200884
Markus Armbrusterf0776562011-09-06 18:58:37 +0200885 if (loej) {
Markus Armbruster48f65b32011-09-06 18:58:48 +0200886 if (!start && !s->tray_open && s->tray_locked) {
Markus Armbrusterfdec4402011-09-06 18:58:45 +0200887 sense = bdrv_is_inserted(s->bs)
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200888 ? NOT_READY : ILLEGAL_REQUEST;
Markus Armbrusterfdec4402011-09-06 18:58:45 +0200889 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
890 return;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200891 }
Markus Armbrusterfdec4402011-09-06 18:58:45 +0200892 bdrv_eject(s->bs, !start);
Markus Armbrusterdd063332011-09-06 18:58:38 +0200893 s->tray_open = !start;
894 }
Markus Armbrusterfdec4402011-09-06 18:58:45 +0200895
896 ide_atapi_cmd_ok(s);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200897}
898
899static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
900{
901 int max_len = ube16_to_cpu(buf + 8);
902
903 cpu_to_ube16(buf, 0);
904 /* no current LBA */
905 buf[2] = 0;
906 buf[3] = 0;
907 buf[4] = 0;
908 buf[5] = 1;
909 cpu_to_ube16(buf + 6, 0);
910 ide_atapi_cmd_reply(s, 8, max_len);
911}
912
913static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
914{
915 int format, msf, start_track, len;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200916 int max_len;
Kevin Wolf7a2c4b82011-04-19 13:15:52 +0200917 uint64_t total_sectors = s->nb_sectors >> 2;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200918
919 max_len = ube16_to_cpu(buf + 7);
920 format = buf[9] >> 6;
921 msf = (buf[1] >> 1) & 1;
922 start_track = buf[6];
923
924 switch(format) {
925 case 0:
926 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
927 if (len < 0)
928 goto error_cmd;
929 ide_atapi_cmd_reply(s, len, max_len);
930 break;
931 case 1:
932 /* multi session : only a single session defined */
933 memset(buf, 0, 12);
934 buf[1] = 0x0a;
935 buf[2] = 0x01;
936 buf[3] = 0x01;
937 ide_atapi_cmd_reply(s, 12, max_len);
938 break;
939 case 2:
940 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
941 if (len < 0)
942 goto error_cmd;
943 ide_atapi_cmd_reply(s, len, max_len);
944 break;
945 default:
946 error_cmd:
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200947 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200948 ASC_INV_FIELD_IN_CMD_PACKET);
949 }
950}
951
952static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
953{
Kevin Wolfe119bca2011-04-19 13:13:44 +0200954 uint64_t total_sectors = s->nb_sectors >> 2;
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200955
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200956 /* NOTE: it is really the number of sectors minus 1 */
957 cpu_to_ube32(buf, total_sectors - 1);
958 cpu_to_ube32(buf + 4, 2048);
959 ide_atapi_cmd_reply(s, 8, 8);
960}
961
962static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
963{
964 int max_len;
965 int media = buf[1];
966 int format = buf[7];
967 int ret;
968
969 max_len = ube16_to_cpu(buf + 8);
970
971 if (format < 0xff) {
972 if (media_is_cd(s)) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200973 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200974 ASC_INCOMPATIBLE_FORMAT);
975 return;
976 } else if (!media_present(s)) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200977 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200978 ASC_INV_FIELD_IN_CMD_PACKET);
979 return;
980 }
981 }
982
983 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
984 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
985
986 switch (format) {
987 case 0x00 ... 0x7f:
988 case 0xff:
989 if (media == 0) {
990 ret = ide_dvd_read_structure(s, format, buf, buf);
991
992 if (ret < 0) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +0200993 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
Kevin Wolfa60cf7e2011-04-18 17:55:08 +0200994 } else {
995 ide_atapi_cmd_reply(s, ret, max_len);
996 }
997
998 break;
999 }
1000 /* TODO: BD support, fall through for now */
1001
1002 /* Generic disk structures */
1003 case 0x80: /* TODO: AACS volume identifier */
1004 case 0x81: /* TODO: AACS media serial number */
1005 case 0x82: /* TODO: AACS media identifier */
1006 case 0x83: /* TODO: AACS media key block */
1007 case 0x90: /* TODO: List of recognized format layers */
1008 case 0xc0: /* TODO: Write protection status */
1009 default:
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001010 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
Kevin Wolfa60cf7e2011-04-18 17:55:08 +02001011 ASC_INV_FIELD_IN_CMD_PACKET);
1012 break;
1013 }
1014}
1015
1016static void cmd_set_speed(IDEState *s, uint8_t* buf)
1017{
1018 ide_atapi_cmd_ok(s);
1019}
1020
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001021enum {
1022 /*
1023 * Only commands flagged as ALLOW_UA are allowed to run under a
1024 * unit attention condition. (See MMC-5, section 4.1.6.1)
1025 */
1026 ALLOW_UA = 0x01,
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001027
1028 /*
1029 * Commands flagged with CHECK_READY can only execute if a medium is present.
1030 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1031 * 4.1.8)
1032 */
1033 CHECK_READY = 0x02,
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001034};
1035
1036static const struct {
1037 void (*handler)(IDEState *s, uint8_t *buf);
1038 int flags;
1039} atapi_cmd_table[0x100] = {
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001040 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY },
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001041 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1042 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02001043 [ 0x1b ] = { cmd_start_stop_unit, 0 }, /* [1] */
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001044 [ 0x1e ] = { cmd_prevent_allow_medium_removal, 0 },
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001045 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02001046 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001047 [ 0x2b ] = { cmd_seek, CHECK_READY },
1048 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001049 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1050 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1051 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02001052 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1053 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001054 [ 0xbb ] = { cmd_set_speed, 0 },
1055 [ 0xbd ] = { cmd_mechanism_status, 0 },
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02001056 [ 0xbe ] = { cmd_read_cd, CHECK_READY },
1057 /* [1] handler detects and reports not ready condition itself */
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001058};
1059
Kevin Wolf33231e02011-04-18 16:45:49 +02001060void ide_atapi_cmd(IDEState *s)
1061{
Kevin Wolf33231e02011-04-18 16:45:49 +02001062 uint8_t *buf;
Kevin Wolf33231e02011-04-18 16:45:49 +02001063
Kevin Wolf33231e02011-04-18 16:45:49 +02001064 buf = s->io_buffer;
1065#ifdef DEBUG_IDE_ATAPI
1066 {
1067 int i;
1068 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1069 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
Alon Levyab719822011-04-28 16:34:39 +03001070 printf(" %02x", buf[i]);
Kevin Wolf33231e02011-04-18 16:45:49 +02001071 }
1072 printf("\n");
1073 }
1074#endif
1075 /*
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001076 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1077 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1078 * condition response unless a higher priority status, defined by the drive
Kevin Wolf33231e02011-04-18 16:45:49 +02001079 * here, is pending.
1080 */
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001081 if (s->sense_key == UNIT_ATTENTION &&
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001082 !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
Kevin Wolf33231e02011-04-18 16:45:49 +02001083 ide_atapi_cmd_check_status(s);
1084 return;
1085 }
Amit Shah4a737d12011-04-28 20:04:41 +05301086 /*
1087 * When a CD gets changed, we have to report an ejected state and
1088 * then a loaded state to guests so that they detect tray
1089 * open/close and media change events. Guests that do not use
1090 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1091 * states rely on this behavior.
1092 */
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02001093 if (!s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001094 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
Kevin Wolf33231e02011-04-18 16:45:49 +02001095
1096 s->cdrom_changed = 0;
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001097 s->sense_key = UNIT_ATTENTION;
Kevin Wolf33231e02011-04-18 16:45:49 +02001098 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1099 return;
1100 }
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001101
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001102 /* Report a Not Ready condition if appropriate for the command */
1103 if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
1104 (!media_present(s) || !bdrv_is_inserted(s->bs)))
1105 {
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001106 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
Kevin Wolf7a2c4b82011-04-19 13:15:52 +02001107 return;
1108 }
1109
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001110 /* Execute the command */
1111 if (atapi_cmd_table[s->io_buffer[0]].handler) {
1112 atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1113 return;
Kevin Wolf33231e02011-04-18 16:45:49 +02001114 }
Kevin Wolfe1a064f2011-04-18 17:55:08 +02001115
Paolo Bonzini67cc61e2011-09-13 14:41:56 +02001116 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
Kevin Wolf33231e02011-04-18 16:45:49 +02001117}