4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "qemu/osdep.h"
27 #include "hw/scsi/scsi.h"
28 #include "sysemu/block-backend.h"
29 #include "scsi/constants.h"
30 #include "ide-internal.h"
33 #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
34 #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
36 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
38 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
41 for(i
= 0; i
< buf_size
; i
++) {
49 static void lba_to_msf(uint8_t *buf
, int lba
)
52 buf
[0] = (lba
/ 75) / 60;
53 buf
[1] = (lba
/ 75) % 60;
57 static inline int media_present(IDEState
*s
)
59 return !s
->tray_open
&& s
->nb_sectors
> 0;
62 /* XXX: DVDs that could fit on a CD will be reported as a CD */
63 static inline int media_is_dvd(IDEState
*s
)
65 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
68 static inline int media_is_cd(IDEState
*s
)
70 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
73 static void cd_data_to_raw(uint8_t *buf
, int lba
)
77 memset(buf
+ 1, 0xff, 10);
82 buf
[3] = 0x01; /* mode 1 data */
86 /* XXX: ECC not computed */
91 cd_read_sector_sync(IDEState
*s
)
94 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
95 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
97 trace_cd_read_sector_sync(s
->lba
);
99 switch (s
->cd_sector_size
) {
101 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
102 ATAPI_SECTOR_SIZE
, s
->io_buffer
, 0);
105 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
106 ATAPI_SECTOR_SIZE
, s
->io_buffer
+ 16, 0);
108 cd_data_to_raw(s
->io_buffer
, s
->lba
);
112 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
117 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
119 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
121 s
->io_buffer_index
= 0;
127 static void cd_read_sector_cb(void *opaque
, int ret
)
129 IDEState
*s
= opaque
;
131 trace_cd_read_sector_cb(s
->lba
, ret
);
134 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
135 ide_atapi_io_error(s
, ret
);
139 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
141 if (s
->cd_sector_size
== 2352) {
142 cd_data_to_raw(s
->io_buffer
, s
->lba
);
146 s
->io_buffer_index
= 0;
147 s
->status
&= ~BUSY_STAT
;
149 ide_atapi_cmd_reply_end(s
);
152 static int cd_read_sector(IDEState
*s
)
156 if (s
->cd_sector_size
!= 2048 && s
->cd_sector_size
!= 2352) {
157 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
161 buf
= (s
->cd_sector_size
== 2352) ? s
->io_buffer
+ 16 : s
->io_buffer
;
162 qemu_iovec_init_buf(&s
->qiov
, buf
, ATAPI_SECTOR_SIZE
);
164 trace_cd_read_sector(s
->lba
);
166 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
167 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
169 ide_buffered_readv(s
, (int64_t)s
->lba
<< 2, &s
->qiov
, 4,
170 cd_read_sector_cb
, s
);
172 s
->status
|= BUSY_STAT
;
176 void ide_atapi_cmd_ok(IDEState
*s
)
179 s
->status
= READY_STAT
| SEEK_STAT
;
180 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
181 ide_transfer_stop(s
);
182 ide_bus_set_irq(s
->bus
);
185 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
187 trace_ide_atapi_cmd_error(s
, sense_key
, asc
);
188 s
->error
= sense_key
<< 4;
189 s
->status
= READY_STAT
| ERR_STAT
;
190 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
191 s
->sense_key
= sense_key
;
193 ide_transfer_stop(s
);
194 ide_bus_set_irq(s
->bus
);
197 void ide_atapi_io_error(IDEState
*s
, int ret
)
199 /* XXX: handle more errors */
200 if (ret
== -ENOMEDIUM
) {
201 ide_atapi_cmd_error(s
, NOT_READY
,
202 ASC_MEDIUM_NOT_PRESENT
);
204 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
205 ASC_LOGICAL_BLOCK_OOR
);
209 static uint16_t atapi_byte_count_limit(IDEState
*s
)
213 bcl
= s
->lcyl
| (s
->hcyl
<< 8);
220 /* The whole ATAPI transfer logic is handled in this function */
221 void ide_atapi_cmd_reply_end(IDEState
*s
)
223 int byte_count_limit
, size
, ret
;
224 while (s
->packet_transfer_size
> 0) {
225 trace_ide_atapi_cmd_reply_end(s
, s
->packet_transfer_size
,
226 s
->elementary_transfer_size
,
229 /* see if a new sector must be read */
230 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
231 if (!s
->elementary_transfer_size
) {
232 ret
= cd_read_sector(s
);
234 ide_atapi_io_error(s
, ret
);
238 /* rebuffering within an elementary transfer is
239 * only possible with a sync request because we
240 * end up with a race condition otherwise */
241 ret
= cd_read_sector_sync(s
);
243 ide_atapi_io_error(s
, ret
);
248 if (s
->elementary_transfer_size
> 0) {
249 /* there are some data left to transmit in this elementary
251 size
= s
->cd_sector_size
- s
->io_buffer_index
;
252 if (size
> s
->elementary_transfer_size
)
253 size
= s
->elementary_transfer_size
;
255 /* a new transfer is needed */
256 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
257 ide_bus_set_irq(s
->bus
);
258 byte_count_limit
= atapi_byte_count_limit(s
);
259 trace_ide_atapi_cmd_reply_end_bcl(s
, byte_count_limit
);
260 size
= s
->packet_transfer_size
;
261 if (size
> byte_count_limit
) {
262 /* byte count limit must be even if this case */
263 if (byte_count_limit
& 1)
265 size
= byte_count_limit
;
269 s
->elementary_transfer_size
= size
;
270 /* we cannot transmit more than one sector at a time */
272 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
273 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
275 trace_ide_atapi_cmd_reply_end_new(s
, s
->status
);
277 s
->packet_transfer_size
-= size
;
278 s
->elementary_transfer_size
-= size
;
279 s
->io_buffer_index
+= size
;
280 assert(size
<= s
->io_buffer_total_len
);
281 assert(s
->io_buffer_index
<= s
->io_buffer_total_len
);
283 /* Some adapters process PIO data right away. In that case, we need
284 * to avoid mutual recursion between ide_transfer_start
285 * and ide_atapi_cmd_reply_end.
287 if (!ide_transfer_start_norecurse(s
,
288 s
->io_buffer
+ s
->io_buffer_index
- size
,
289 size
, ide_atapi_cmd_reply_end
)) {
294 /* end of transfer */
295 trace_ide_atapi_cmd_reply_end_eot(s
, s
->status
);
297 ide_bus_set_irq(s
->bus
);
300 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
301 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
305 s
->lba
= -1; /* no sector read */
306 s
->packet_transfer_size
= size
;
307 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
308 s
->elementary_transfer_size
= 0;
311 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, size
,
313 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
314 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
316 s
->status
= READY_STAT
| SEEK_STAT
;
317 s
->io_buffer_index
= 0;
318 ide_atapi_cmd_reply_end(s
);
322 /* start a CD-ROM read command */
323 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
326 assert(0 <= lba
&& lba
< (s
->nb_sectors
>> 2));
329 s
->packet_transfer_size
= nb_sectors
* sector_size
;
330 s
->elementary_transfer_size
= 0;
331 s
->io_buffer_index
= sector_size
;
332 s
->cd_sector_size
= sector_size
;
334 ide_atapi_cmd_reply_end(s
);
337 static void ide_atapi_cmd_check_status(IDEState
*s
)
339 trace_ide_atapi_cmd_check_status(s
);
340 s
->error
= MC_ERR
| (UNIT_ATTENTION
<< 4);
341 s
->status
= ERR_STAT
;
343 ide_bus_set_irq(s
->bus
);
345 /* ATAPI DMA support */
347 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
349 IDEState
*s
= opaque
;
353 if (ide_handle_rw_error(s
, -ret
, ide_dma_cmd_to_retry(s
->dma_cmd
))) {
354 if (s
->bus
->error_status
) {
355 s
->bus
->dma
->aiocb
= NULL
;
362 if (s
->io_buffer_size
> 0) {
364 * For a cdrom read sector command (s->lba != -1),
365 * adjust the lba for the next s->io_buffer_size chunk
366 * and dma the current chunk.
367 * For a command != read (s->lba == -1), just transfer
371 if (s
->cd_sector_size
== 2352) {
373 cd_data_to_raw(s
->io_buffer
, s
->lba
);
375 n
= s
->io_buffer_size
>> 11;
379 s
->packet_transfer_size
-= s
->io_buffer_size
;
380 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
384 if (s
->packet_transfer_size
<= 0) {
385 s
->status
= READY_STAT
| SEEK_STAT
;
386 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
387 ide_bus_set_irq(s
->bus
);
391 s
->io_buffer_index
= 0;
392 if (s
->cd_sector_size
== 2352) {
394 s
->io_buffer_size
= s
->cd_sector_size
;
397 n
= s
->packet_transfer_size
>> 11;
398 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
399 n
= (IDE_DMA_BUF_SECTORS
/ 4);
400 s
->io_buffer_size
= n
* 2048;
403 trace_ide_atapi_cmd_read_dma_cb_aio(s
, s
->lba
, n
);
404 qemu_iovec_init_buf(&s
->bus
->dma
->qiov
, s
->io_buffer
+ data_offset
,
405 n
* ATAPI_SECTOR_SIZE
);
407 s
->bus
->dma
->aiocb
= ide_buffered_readv(s
, (int64_t)s
->lba
<< 2,
408 &s
->bus
->dma
->qiov
, n
* 4,
409 ide_atapi_cmd_read_dma_cb
, s
);
414 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
416 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
418 ide_set_inactive(s
, false);
421 /* start a CD-ROM read command with DMA */
422 /* XXX: test if DMA is available */
423 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
426 assert(0 <= lba
&& lba
< (s
->nb_sectors
>> 2));
429 s
->packet_transfer_size
= nb_sectors
* sector_size
;
430 s
->io_buffer_size
= 0;
431 s
->cd_sector_size
= sector_size
;
433 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, s
->packet_transfer_size
,
436 /* XXX: check if BUSY_STAT should be set */
437 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
438 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
441 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
444 trace_ide_atapi_cmd_read(s
, s
->atapi_dma
? "dma" : "pio",
447 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
449 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
453 void ide_atapi_dma_restart(IDEState
*s
)
456 * At this point we can just re-evaluate the packet command and start over.
457 * The presence of ->dma_cb callback in the pre_save ensures that the packet
458 * command has been completely sent and we can safely restart command.
460 s
->unit
= s
->bus
->retry_unit
;
461 s
->bus
->dma
->ops
->restart_dma(s
->bus
->dma
);
465 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
468 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
470 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
471 stw_be_p(buf_profile
, profile
);
472 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
474 /* each profile adds 4 bytes to the response */
476 buf
[11] += 4; /* Additional Length */
481 static int ide_dvd_read_structure(IDEState
*s
, int format
,
482 const uint8_t *packet
, uint8_t *buf
)
485 case 0x0: /* Physical format information */
487 int layer
= packet
[6];
488 uint64_t total_sectors
;
491 return -ASC_INV_FIELD_IN_CMD_PACKET
;
493 total_sectors
= s
->nb_sectors
>> 2;
494 if (total_sectors
== 0) {
495 return -ASC_MEDIUM_NOT_PRESENT
;
498 buf
[4] = 1; /* DVD-ROM, part version 1 */
499 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
500 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
501 buf
[7] = 0; /* default densities */
503 /* FIXME: 0x30000 per spec? */
504 stl_be_p(buf
+ 8, 0); /* start sector */
505 stl_be_p(buf
+ 12, total_sectors
- 1); /* end sector */
506 stl_be_p(buf
+ 16, total_sectors
- 1); /* l0 end sector */
508 /* Size of buffer, not including 2 byte size field */
509 stw_be_p(buf
, 2048 + 2);
511 /* 2k data + 4 byte header */
515 case 0x01: /* DVD copyright information */
516 buf
[4] = 0; /* no copyright data */
517 buf
[5] = 0; /* no region restrictions */
519 /* Size of buffer, not including 2 byte size field */
520 stw_be_p(buf
, 4 + 2);
522 /* 4 byte header + 4 byte data */
525 case 0x03: /* BCA information - invalid field for no BCA info */
526 return -ASC_INV_FIELD_IN_CMD_PACKET
;
528 case 0x04: /* DVD disc manufacturing information */
529 /* Size of buffer, not including 2 byte size field */
530 stw_be_p(buf
, 2048 + 2);
532 /* 2k data + 4 byte header */
537 * This lists all the command capabilities above. Add new ones
538 * in order and update the length and buffer return values.
541 buf
[4] = 0x00; /* Physical format */
542 buf
[5] = 0x40; /* Not writable, is readable */
543 stw_be_p(buf
+ 6, 2048 + 4);
545 buf
[8] = 0x01; /* Copyright info */
546 buf
[9] = 0x40; /* Not writable, is readable */
547 stw_be_p(buf
+ 10, 4 + 4);
549 buf
[12] = 0x03; /* BCA info */
550 buf
[13] = 0x40; /* Not writable, is readable */
551 stw_be_p(buf
+ 14, 188 + 4);
553 buf
[16] = 0x04; /* Manufacturing info */
554 buf
[17] = 0x40; /* Not writable, is readable */
555 stw_be_p(buf
+ 18, 2048 + 4);
557 /* Size of buffer, not including 2 byte size field */
558 stw_be_p(buf
, 16 + 2);
560 /* data written + 4 byte header */
563 default: /* TODO: formats beyond DVD-ROM requires */
564 return -ASC_INV_FIELD_IN_CMD_PACKET
;
568 static unsigned int event_status_media(IDEState
*s
,
571 uint8_t event_code
, media_status
;
575 media_status
= MS_TRAY_OPEN
;
576 } else if (blk_is_inserted(s
->blk
)) {
577 media_status
= MS_MEDIA_PRESENT
;
580 /* Event notification descriptor */
581 event_code
= MEC_NO_CHANGE
;
582 if (media_status
!= MS_TRAY_OPEN
) {
583 if (s
->events
.new_media
) {
584 event_code
= MEC_NEW_MEDIA
;
585 s
->events
.new_media
= false;
586 } else if (s
->events
.eject_request
) {
587 event_code
= MEC_EJECT_REQUESTED
;
588 s
->events
.eject_request
= false;
593 buf
[5] = media_status
;
595 /* These fields are reserved, just clear them. */
599 return 8; /* We wrote to 4 extra bytes from the header */
603 * Before transferring data or otherwise signalling acceptance of a command
604 * marked CONDDATA, we must check the validity of the byte_count_limit.
606 static bool validate_bcl(IDEState
*s
)
608 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
609 if (s
->atapi_dma
|| atapi_byte_count_limit(s
)) {
613 /* TODO: Move abort back into core.c and introduce proper error flow between
614 * ATAPI layer and IDE core layer */
615 ide_abort_command(s
);
619 static void cmd_get_event_status_notification(IDEState
*s
,
622 const uint8_t *packet
= buf
;
626 uint8_t polled
; /* lsb bit is polled; others are reserved */
627 uint8_t reserved2
[2];
629 uint8_t reserved3
[2];
632 } QEMU_PACKED
*gesn_cdb
;
636 uint8_t notification_class
;
637 uint8_t supported_events
;
638 } QEMU_PACKED
*gesn_event_header
;
639 unsigned int max_len
, used_len
;
641 gesn_cdb
= (void *)packet
;
642 gesn_event_header
= (void *)buf
;
644 max_len
= be16_to_cpu(gesn_cdb
->len
);
646 /* It is fine by the MMC spec to not support async mode operations */
647 if (!(gesn_cdb
->polled
& 0x01)) { /* asynchronous mode */
648 /* Only polling is supported, asynchronous mode is not. */
649 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
650 ASC_INV_FIELD_IN_CMD_PACKET
);
654 /* polling mode operation */
657 * These are the supported events.
659 * We currently only support requests of the 'media' type.
660 * Notification class requests and supported event classes are bitmasks,
661 * but they are build from the same values as the "notification class"
664 gesn_event_header
->supported_events
= 1 << GESN_MEDIA
;
667 * We use |= below to set the class field; other bits in this byte
668 * are reserved now but this is useful to do if we have to use the
669 * reserved fields later.
671 gesn_event_header
->notification_class
= 0;
674 * Responses to requests are to be based on request priority. The
675 * notification_class_request_type enum above specifies the
676 * priority: upper elements are higher prio than lower ones.
678 if (gesn_cdb
->class & (1 << GESN_MEDIA
)) {
679 gesn_event_header
->notification_class
|= GESN_MEDIA
;
680 used_len
= event_status_media(s
, buf
);
682 gesn_event_header
->notification_class
= 0x80; /* No event available */
683 used_len
= sizeof(*gesn_event_header
);
685 gesn_event_header
->len
= cpu_to_be16(used_len
686 - sizeof(*gesn_event_header
));
687 ide_atapi_cmd_reply(s
, used_len
, max_len
);
690 static void cmd_request_sense(IDEState
*s
, uint8_t *buf
)
692 int max_len
= buf
[4];
695 buf
[0] = 0x70 | (1 << 7);
696 buf
[2] = s
->sense_key
;
700 if (s
->sense_key
== UNIT_ATTENTION
) {
701 s
->sense_key
= NO_SENSE
;
704 ide_atapi_cmd_reply(s
, 18, max_len
);
707 static void cmd_inquiry(IDEState
*s
, uint8_t *buf
)
709 uint8_t page_code
= buf
[2];
710 int max_len
= buf
[4];
714 unsigned preamble_len
;
716 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
717 * we are being asked for a specific page of info indicated by byte 2. */
722 buf
[idx
++] = 0x05; /* CD-ROM */
723 buf
[idx
++] = page_code
; /* Page Code */
724 buf
[idx
++] = 0x00; /* reserved */
725 idx
++; /* length (set later) */
729 /* Supported Pages: List of supported VPD responses. */
730 buf
[idx
++] = 0x00; /* 0x00: Supported Pages, and: */
731 buf
[idx
++] = 0x83; /* 0x83: Device Identification. */
735 /* Device Identification. Each entry is optional, but the entries
736 * included here are modeled after libata's VPD responses.
737 * If the response is given, at least one entry must be present. */
739 /* Entry 1: Serial */
740 if (idx
+ 24 > max_len
) {
741 /* Not enough room for even the first entry: */
742 /* 4 byte header + 20 byte string */
743 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
744 ASC_DATA_PHASE_ERROR
);
747 buf
[idx
++] = 0x02; /* Ascii */
748 buf
[idx
++] = 0x00; /* Vendor Specific */
750 buf
[idx
++] = 20; /* Remaining length */
751 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
754 /* Entry 2: Drive Model and Serial */
755 if (idx
+ 72 > max_len
) {
756 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
759 buf
[idx
++] = 0x02; /* Ascii */
760 buf
[idx
++] = 0x01; /* T10 Vendor */
763 padstr8(buf
+ idx
, 8, "ATA"); /* Generic T10 vendor */
765 padstr8(buf
+ idx
, 40, s
->drive_model_str
);
767 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
771 if (s
->wwn
&& (idx
+ 12 <= max_len
)) {
772 /* 4 byte header + 8 byte wwn */
773 buf
[idx
++] = 0x01; /* Binary */
774 buf
[idx
++] = 0x03; /* NAA */
777 stq_be_p(&buf
[idx
], s
->wwn
);
783 /* SPC-3, revision 23 sec. 6.4 */
784 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
785 ASC_INV_FIELD_IN_CMD_PACKET
);
792 buf
[0] = 0x05; /* CD-ROM */
793 buf
[1] = 0x80; /* removable */
794 buf
[2] = 0x00; /* ISO */
795 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
796 /* buf[size_idx] set below. */
797 buf
[5] = 0; /* reserved */
798 buf
[6] = 0; /* reserved */
799 buf
[7] = 0; /* reserved */
800 padstr8(buf
+ 8, 8, "QEMU");
801 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
802 padstr8(buf
+ 32, 4, s
->version
);
807 buf
[size_idx
] = idx
- preamble_len
;
808 ide_atapi_cmd_reply(s
, idx
, max_len
);
811 static void cmd_get_configuration(IDEState
*s
, uint8_t *buf
)
817 /* only feature 0 is supported */
818 if (buf
[2] != 0 || buf
[3] != 0) {
819 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
820 ASC_INV_FIELD_IN_CMD_PACKET
);
824 /* XXX: could result in alignment problems in some architectures */
825 max_len
= lduw_be_p(buf
+ 7);
828 * XXX: avoid overflow for io_buffer if max_len is bigger than
829 * the size of that buffer (dimensioned to max number of
830 * sectors to transfer at once)
832 * Only a problem if the feature/profiles grow.
834 if (max_len
> BDRV_SECTOR_SIZE
) {
835 /* XXX: assume 1 sector */
836 max_len
= BDRV_SECTOR_SIZE
;
839 memset(buf
, 0, max_len
);
841 * the number of sectors from the media tells us which profile
842 * to use as current. 0 means there is no media
844 if (media_is_dvd(s
)) {
845 stw_be_p(buf
+ 6, MMC_PROFILE_DVD_ROM
);
846 } else if (media_is_cd(s
)) {
847 stw_be_p(buf
+ 6, MMC_PROFILE_CD_ROM
);
850 buf
[10] = 0x02 | 0x01; /* persistent and current */
851 len
= 12; /* headers: 8 + 4 */
852 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
853 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
854 stl_be_p(buf
, len
- 4); /* data length */
856 ide_atapi_cmd_reply(s
, len
, max_len
);
859 static void cmd_mode_sense(IDEState
*s
, uint8_t *buf
)
864 max_len
= lduw_be_p(buf
+ 7);
865 action
= buf
[2] >> 6;
866 code
= buf
[2] & 0x3f;
869 case 0: /* current values */
871 case MODE_PAGE_R_W_ERROR
: /* error recovery */
872 stw_be_p(&buf
[0], 16 - 2);
880 buf
[8] = MODE_PAGE_R_W_ERROR
;
888 ide_atapi_cmd_reply(s
, 16, max_len
);
890 case MODE_PAGE_AUDIO_CTL
:
891 stw_be_p(&buf
[0], 24 - 2);
899 buf
[8] = MODE_PAGE_AUDIO_CTL
;
901 /* Fill with CDROM audio volume */
907 ide_atapi_cmd_reply(s
, 24, max_len
);
909 case MODE_PAGE_CAPABILITIES
:
910 stw_be_p(&buf
[0], 30 - 2);
918 buf
[8] = MODE_PAGE_CAPABILITIES
;
920 buf
[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
923 /* Claim PLAY_AUDIO capability (0x01) since some Linux
924 code checks for this to automount media. */
927 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
928 if (s
->tray_locked
) {
931 buf
[15] = 0x00; /* No volume & mute control, no changer */
932 stw_be_p(&buf
[16], 704); /* 4x read speed */
933 buf
[18] = 0; /* Two volume levels */
935 stw_be_p(&buf
[20], 512); /* 512k buffer */
936 stw_be_p(&buf
[22], 704); /* 4x read speed current */
943 ide_atapi_cmd_reply(s
, 30, max_len
);
949 case 1: /* changeable values */
951 case 2: /* default values */
954 case 3: /* saved values */
955 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
956 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
962 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_INV_FIELD_IN_CMD_PACKET
);
965 static void cmd_test_unit_ready(IDEState
*s
, uint8_t *buf
)
967 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
968 * come here, we know that it's ready. */
972 static void cmd_prevent_allow_medium_removal(IDEState
*s
, uint8_t* buf
)
974 s
->tray_locked
= buf
[4] & 1;
975 blk_lock_medium(s
->blk
, buf
[4] & 1);
979 static void cmd_read(IDEState
*s
, uint8_t* buf
)
981 unsigned int nb_sectors
, lba
;
983 /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
984 uint64_t total_sectors
= s
->nb_sectors
>> 2;
986 if (buf
[0] == GPCMD_READ_10
) {
987 nb_sectors
= lduw_be_p(buf
+ 7);
989 nb_sectors
= ldl_be_p(buf
+ 6);
991 if (nb_sectors
== 0) {
996 lba
= ldl_be_p(buf
+ 2);
997 if (lba
>= total_sectors
|| lba
+ nb_sectors
- 1 >= total_sectors
) {
998 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1002 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1005 static void cmd_read_cd(IDEState
*s
, uint8_t* buf
)
1007 unsigned int nb_sectors
, lba
, transfer_request
;
1009 /* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */
1010 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1012 nb_sectors
= (buf
[6] << 16) | (buf
[7] << 8) | buf
[8];
1013 if (nb_sectors
== 0) {
1014 ide_atapi_cmd_ok(s
);
1018 lba
= ldl_be_p(buf
+ 2);
1019 if (lba
>= total_sectors
|| lba
+ nb_sectors
- 1 >= total_sectors
) {
1020 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1024 transfer_request
= buf
[9] & 0xf8;
1025 if (transfer_request
== 0x00) {
1027 ide_atapi_cmd_ok(s
);
1031 /* Check validity of BCL before transferring data */
1032 if (!validate_bcl(s
)) {
1036 switch (transfer_request
) {
1039 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1043 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1046 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1047 ASC_INV_FIELD_IN_CMD_PACKET
);
1052 static void cmd_seek(IDEState
*s
, uint8_t* buf
)
1055 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1057 lba
= ldl_be_p(buf
+ 2);
1058 if (lba
>= total_sectors
) {
1059 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1063 ide_atapi_cmd_ok(s
);
1066 static void cmd_start_stop_unit(IDEState
*s
, uint8_t* buf
)
1069 bool start
= buf
[4] & 1;
1070 bool loej
= buf
[4] & 2; /* load on start, eject on !start */
1071 int pwrcnd
= buf
[4] & 0xf0;
1074 /* eject/load only happens for power condition == 0 */
1075 ide_atapi_cmd_ok(s
);
1080 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1081 sense
= blk_is_inserted(s
->blk
)
1082 ? NOT_READY
: ILLEGAL_REQUEST
;
1083 ide_atapi_cmd_error(s
, sense
, ASC_MEDIA_REMOVAL_PREVENTED
);
1087 if (s
->tray_open
!= !start
) {
1088 blk_eject(s
->blk
, !start
);
1089 s
->tray_open
= !start
;
1093 ide_atapi_cmd_ok(s
);
1096 static void cmd_mechanism_status(IDEState
*s
, uint8_t* buf
)
1098 int max_len
= lduw_be_p(buf
+ 8);
1101 /* no current LBA */
1106 stw_be_p(buf
+ 6, 0);
1107 ide_atapi_cmd_reply(s
, 8, max_len
);
1110 static void cmd_read_toc_pma_atip(IDEState
*s
, uint8_t* buf
)
1112 int format
, msf
, start_track
, len
;
1114 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1116 max_len
= lduw_be_p(buf
+ 7);
1117 format
= buf
[9] >> 6;
1118 msf
= (buf
[1] >> 1) & 1;
1119 start_track
= buf
[6];
1123 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1126 ide_atapi_cmd_reply(s
, len
, max_len
);
1129 /* multi session : only a single session defined */
1134 ide_atapi_cmd_reply(s
, 12, max_len
);
1137 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1140 ide_atapi_cmd_reply(s
, len
, max_len
);
1144 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1145 ASC_INV_FIELD_IN_CMD_PACKET
);
1149 static void cmd_read_cdvd_capacity(IDEState
*s
, uint8_t* buf
)
1151 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1153 /* NOTE: it is really the number of sectors minus 1 */
1154 stl_be_p(buf
, total_sectors
- 1);
1155 stl_be_p(buf
+ 4, 2048);
1156 ide_atapi_cmd_reply(s
, 8, 8);
1159 static void cmd_read_disc_information(IDEState
*s
, uint8_t* buf
)
1161 uint8_t type
= buf
[1] & 7;
1162 uint32_t max_len
= lduw_be_p(buf
+ 7);
1164 /* Types 1/2 are only defined for Blu-Ray. */
1166 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1167 ASC_INV_FIELD_IN_CMD_PACKET
);
1173 buf
[2] = 0xe; /* last session complete, disc finalized */
1174 buf
[3] = 1; /* first track on disc */
1175 buf
[4] = 1; /* # of sessions */
1176 buf
[5] = 1; /* first track of last session */
1177 buf
[6] = 1; /* last track of last session */
1178 buf
[7] = 0x20; /* unrestricted use */
1179 buf
[8] = 0x00; /* CD-ROM or DVD-ROM */
1180 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1181 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1182 /* 24-31: disc bar code */
1183 /* 32: disc application code */
1184 /* 33: number of OPC tables */
1186 ide_atapi_cmd_reply(s
, 34, max_len
);
1189 static void cmd_read_dvd_structure(IDEState
*s
, uint8_t* buf
)
1193 int format
= buf
[7];
1196 max_len
= lduw_be_p(buf
+ 8);
1198 if (format
< 0xff) {
1199 if (media_is_cd(s
)) {
1200 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1201 ASC_INCOMPATIBLE_FORMAT
);
1203 } else if (!media_present(s
)) {
1204 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1205 ASC_INV_FIELD_IN_CMD_PACKET
);
1210 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* BDRV_SECTOR_SIZE
+ 4 ?
1211 IDE_DMA_BUF_SECTORS
* BDRV_SECTOR_SIZE
+ 4 : max_len
);
1217 ret
= ide_dvd_read_structure(s
, format
, buf
, buf
);
1220 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, -ret
);
1222 ide_atapi_cmd_reply(s
, ret
, max_len
);
1227 /* TODO: BD support, fall through for now */
1229 /* Generic disk structures */
1230 case 0x80: /* TODO: AACS volume identifier */
1231 case 0x81: /* TODO: AACS media serial number */
1232 case 0x82: /* TODO: AACS media identifier */
1233 case 0x83: /* TODO: AACS media key block */
1234 case 0x90: /* TODO: List of recognized format layers */
1235 case 0xc0: /* TODO: Write protection status */
1237 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1238 ASC_INV_FIELD_IN_CMD_PACKET
);
1243 static void cmd_set_speed(IDEState
*s
, uint8_t* buf
)
1245 ide_atapi_cmd_ok(s
);
1250 * Only commands flagged as ALLOW_UA are allowed to run under a
1251 * unit attention condition. (See MMC-5, section 4.1.6.1)
1256 * Commands flagged with CHECK_READY can only execute if a medium is present.
1257 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1263 * Commands flagged with NONDATA do not in any circumstances return
1264 * any data via ide_atapi_cmd_reply. These commands are exempt from
1265 * the normal byte_count_limit constraints.
1266 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1271 * CONDDATA implies a command that transfers data only conditionally based
1272 * on the presence of suboptions. It should be exempt from the BCL check at
1273 * command validation time, but it needs to be checked at the command
1274 * handler level instead.
1279 static const struct AtapiCmd
{
1280 void (*handler
)(IDEState
*s
, uint8_t *buf
);
1282 } atapi_cmd_table
[0x100] = {
1283 [ 0x00 ] = { cmd_test_unit_ready
, CHECK_READY
| NONDATA
},
1284 [ 0x03 ] = { cmd_request_sense
, ALLOW_UA
},
1285 [ 0x12 ] = { cmd_inquiry
, ALLOW_UA
},
1286 [ 0x1b ] = { cmd_start_stop_unit
, NONDATA
}, /* [1] */
1287 [ 0x1e ] = { cmd_prevent_allow_medium_removal
, NONDATA
},
1288 [ 0x25 ] = { cmd_read_cdvd_capacity
, CHECK_READY
},
1289 [ 0x28 ] = { cmd_read
, /* (10) */ CHECK_READY
},
1290 [ 0x2b ] = { cmd_seek
, CHECK_READY
| NONDATA
},
1291 [ 0x43 ] = { cmd_read_toc_pma_atip
, CHECK_READY
},
1292 [ 0x46 ] = { cmd_get_configuration
, ALLOW_UA
},
1293 [ 0x4a ] = { cmd_get_event_status_notification
, ALLOW_UA
},
1294 [ 0x51 ] = { cmd_read_disc_information
, CHECK_READY
},
1295 [ 0x5a ] = { cmd_mode_sense
, /* (10) */ 0 },
1296 [ 0xa8 ] = { cmd_read
, /* (12) */ CHECK_READY
},
1297 [ 0xad ] = { cmd_read_dvd_structure
, CHECK_READY
},
1298 [ 0xbb ] = { cmd_set_speed
, NONDATA
},
1299 [ 0xbd ] = { cmd_mechanism_status
, 0 },
1300 [ 0xbe ] = { cmd_read_cd
, CHECK_READY
| CONDDATA
},
1301 /* [1] handler detects and reports not ready condition itself */
1304 void ide_atapi_cmd(IDEState
*s
)
1306 uint8_t *buf
= s
->io_buffer
;
1307 const struct AtapiCmd
*cmd
= &atapi_cmd_table
[s
->io_buffer
[0]];
1309 trace_ide_atapi_cmd(s
, s
->io_buffer
[0]);
1311 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET
)) {
1312 /* Each pretty-printed byte needs two bytes and a space; */
1313 char *ppacket
= g_malloc(ATAPI_PACKET_SIZE
* 3 + 1);
1315 for (i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1316 sprintf(ppacket
+ (i
* 3), "%02x ", buf
[i
]);
1318 trace_ide_atapi_cmd_packet(s
, s
->lcyl
| (s
->hcyl
<< 8), ppacket
);
1323 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1324 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1325 * condition response unless a higher priority status, defined by the drive
1328 if (s
->sense_key
== UNIT_ATTENTION
&& !(cmd
->flags
& ALLOW_UA
)) {
1329 ide_atapi_cmd_check_status(s
);
1333 * When a CD gets changed, we have to report an ejected state and
1334 * then a loaded state to guests so that they detect tray
1335 * open/close and media change events. Guests that do not use
1336 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1337 * states rely on this behavior.
1339 if (!(cmd
->flags
& ALLOW_UA
) &&
1340 !s
->tray_open
&& blk_is_inserted(s
->blk
) && s
->cdrom_changed
) {
1342 if (s
->cdrom_changed
== 1) {
1343 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1344 s
->cdrom_changed
= 2;
1346 ide_atapi_cmd_error(s
, UNIT_ATTENTION
, ASC_MEDIUM_MAY_HAVE_CHANGED
);
1347 s
->cdrom_changed
= 0;
1353 /* Report a Not Ready condition if appropriate for the command */
1354 if ((cmd
->flags
& CHECK_READY
) &&
1355 (!media_present(s
) || !blk_is_inserted(s
->blk
)))
1357 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1361 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1362 * If this is a data-transferring PIO command and BCL is 0,
1363 * we abort at the /ATA/ level, not the ATAPI level.
1364 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1365 if (cmd
->handler
&& !(cmd
->flags
& (NONDATA
| CONDDATA
))) {
1366 if (!validate_bcl(s
)) {
1371 /* Execute the command */
1373 cmd
->handler(s
, buf
);
1377 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_ILLEGAL_OPCODE
);