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