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