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