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