2 * QEMU IDE disk and CD/DVD-ROM Emulator
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
29 #include "qemu-error.h"
30 #include "qemu-timer.h"
35 #include <hw/ide/internal.h>
37 static const int smart_attributes[][5] = {
38 /* id, flags, val, wrst, thrsh */
39 { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
40 { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
41 { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
42 { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
43 { 0x00, 0x00, 0x00, 0x00, 0x00}
46 /* XXX: DVDs that could fit on a CD will be reported as a CD */
47 static inline int media_present(IDEState *s)
49 return (s->nb_sectors > 0);
52 static inline int media_is_dvd(IDEState *s)
54 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
57 static inline int media_is_cd(IDEState *s)
59 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
62 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
63 static int ide_handle_rw_error(IDEState *s, int error, int op);
65 static void padstr(char *str, const char *src, int len)
68 for(i = 0; i < len; i++) {
77 static void padstr8(uint8_t *buf, int buf_size, const char *src)
80 for(i = 0; i < buf_size; i++) {
88 static void put_le16(uint16_t *p, unsigned int v)
93 static void ide_identify(IDEState *s)
99 if (s->identify_set) {
100 memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
104 memset(s->io_buffer, 0, 512);
105 p = (uint16_t *)s->io_buffer;
106 put_le16(p + 0, 0x0040);
107 put_le16(p + 1, s->cylinders);
108 put_le16(p + 3, s->heads);
109 put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
110 put_le16(p + 5, 512); /* XXX: retired, remove ? */
111 put_le16(p + 6, s->sectors);
112 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
113 put_le16(p + 20, 3); /* XXX: retired, remove ? */
114 put_le16(p + 21, 512); /* cache size in sectors */
115 put_le16(p + 22, 4); /* ecc bytes */
116 padstr((char *)(p + 23), s->version, 8); /* firmware version */
117 padstr((char *)(p + 27), "QEMU HARDDISK", 40); /* model */
118 #if MAX_MULT_SECTORS > 1
119 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
121 put_le16(p + 48, 1); /* dword I/O */
122 put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
123 put_le16(p + 51, 0x200); /* PIO transfer cycle */
124 put_le16(p + 52, 0x200); /* DMA transfer cycle */
125 put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
126 put_le16(p + 54, s->cylinders);
127 put_le16(p + 55, s->heads);
128 put_le16(p + 56, s->sectors);
129 oldsize = s->cylinders * s->heads * s->sectors;
130 put_le16(p + 57, oldsize);
131 put_le16(p + 58, oldsize >> 16);
133 put_le16(p + 59, 0x100 | s->mult_sectors);
134 put_le16(p + 60, s->nb_sectors);
135 put_le16(p + 61, s->nb_sectors >> 16);
136 put_le16(p + 62, 0x07); /* single word dma0-2 supported */
137 put_le16(p + 63, 0x07); /* mdma0-2 supported */
138 put_le16(p + 64, 0x03); /* pio3-4 supported */
139 put_le16(p + 65, 120);
140 put_le16(p + 66, 120);
141 put_le16(p + 67, 120);
142 put_le16(p + 68, 120);
145 put_le16(p + 75, s->ncq_queues - 1);
147 put_le16(p + 76, (1 << 8));
150 put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
151 put_le16(p + 81, 0x16); /* conforms to ata5 */
152 /* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */
153 put_le16(p + 82, (1 << 14) | (1 << 5) | 1);
154 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
155 put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
156 /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
157 put_le16(p + 84, (1 << 14) | 0);
158 /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
159 if (bdrv_enable_write_cache(s->bs))
160 put_le16(p + 85, (1 << 14) | (1 << 5) | 1);
162 put_le16(p + 85, (1 << 14) | 1);
163 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
164 put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
165 /* 14=set to 1, 1=smart self test, 0=smart error logging */
166 put_le16(p + 87, (1 << 14) | 0);
167 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
168 put_le16(p + 93, 1 | (1 << 14) | 0x2000);
169 put_le16(p + 100, s->nb_sectors);
170 put_le16(p + 101, s->nb_sectors >> 16);
171 put_le16(p + 102, s->nb_sectors >> 32);
172 put_le16(p + 103, s->nb_sectors >> 48);
173 dev = s->unit ? s->bus->slave : s->bus->master;
174 if (dev && dev->conf.physical_block_size)
175 put_le16(p + 106, 0x6000 | get_physical_block_exp(&dev->conf));
177 memcpy(s->identify_data, p, sizeof(s->identify_data));
181 static void ide_atapi_identify(IDEState *s)
185 if (s->identify_set) {
186 memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
190 memset(s->io_buffer, 0, 512);
191 p = (uint16_t *)s->io_buffer;
192 /* Removable CDROM, 50us response, 12 byte packets */
193 put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
194 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
195 put_le16(p + 20, 3); /* buffer type */
196 put_le16(p + 21, 512); /* cache size in sectors */
197 put_le16(p + 22, 4); /* ecc bytes */
198 padstr((char *)(p + 23), s->version, 8); /* firmware version */
199 padstr((char *)(p + 27), "QEMU DVD-ROM", 40); /* model */
200 put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
202 put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
203 put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
204 put_le16(p + 62, 7); /* single word dma0-2 supported */
205 put_le16(p + 63, 7); /* mdma0-2 supported */
207 put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
208 put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
209 put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
211 put_le16(p + 64, 3); /* pio3-4 supported */
212 put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
213 put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
214 put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
215 put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
217 put_le16(p + 71, 30); /* in ns */
218 put_le16(p + 72, 30); /* in ns */
221 put_le16(p + 75, s->ncq_queues - 1);
223 put_le16(p + 76, (1 << 8));
226 put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
228 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
230 memcpy(s->identify_data, p, sizeof(s->identify_data));
234 static void ide_cfata_identify(IDEState *s)
239 p = (uint16_t *) s->identify_data;
243 memset(p, 0, sizeof(s->identify_data));
245 cur_sec = s->cylinders * s->heads * s->sectors;
247 put_le16(p + 0, 0x848a); /* CF Storage Card signature */
248 put_le16(p + 1, s->cylinders); /* Default cylinders */
249 put_le16(p + 3, s->heads); /* Default heads */
250 put_le16(p + 6, s->sectors); /* Default sectors per track */
251 put_le16(p + 7, s->nb_sectors >> 16); /* Sectors per card */
252 put_le16(p + 8, s->nb_sectors); /* Sectors per card */
253 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
254 put_le16(p + 22, 0x0004); /* ECC bytes */
255 padstr((char *) (p + 23), s->version, 8); /* Firmware Revision */
256 padstr((char *) (p + 27), "QEMU MICRODRIVE", 40);/* Model number */
257 #if MAX_MULT_SECTORS > 1
258 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
260 put_le16(p + 47, 0x0000);
262 put_le16(p + 49, 0x0f00); /* Capabilities */
263 put_le16(p + 51, 0x0002); /* PIO cycle timing mode */
264 put_le16(p + 52, 0x0001); /* DMA cycle timing mode */
265 put_le16(p + 53, 0x0003); /* Translation params valid */
266 put_le16(p + 54, s->cylinders); /* Current cylinders */
267 put_le16(p + 55, s->heads); /* Current heads */
268 put_le16(p + 56, s->sectors); /* Current sectors */
269 put_le16(p + 57, cur_sec); /* Current capacity */
270 put_le16(p + 58, cur_sec >> 16); /* Current capacity */
271 if (s->mult_sectors) /* Multiple sector setting */
272 put_le16(p + 59, 0x100 | s->mult_sectors);
273 put_le16(p + 60, s->nb_sectors); /* Total LBA sectors */
274 put_le16(p + 61, s->nb_sectors >> 16); /* Total LBA sectors */
275 put_le16(p + 63, 0x0203); /* Multiword DMA capability */
276 put_le16(p + 64, 0x0001); /* Flow Control PIO support */
277 put_le16(p + 65, 0x0096); /* Min. Multiword DMA cycle */
278 put_le16(p + 66, 0x0096); /* Rec. Multiword DMA cycle */
279 put_le16(p + 68, 0x00b4); /* Min. PIO cycle time */
280 put_le16(p + 82, 0x400c); /* Command Set supported */
281 put_le16(p + 83, 0x7068); /* Command Set supported */
282 put_le16(p + 84, 0x4000); /* Features supported */
283 put_le16(p + 85, 0x000c); /* Command Set enabled */
284 put_le16(p + 86, 0x7044); /* Command Set enabled */
285 put_le16(p + 87, 0x4000); /* Features enabled */
286 put_le16(p + 91, 0x4060); /* Current APM level */
287 put_le16(p + 129, 0x0002); /* Current features option */
288 put_le16(p + 130, 0x0005); /* Reassigned sectors */
289 put_le16(p + 131, 0x0001); /* Initial power mode */
290 put_le16(p + 132, 0x0000); /* User signature */
291 put_le16(p + 160, 0x8100); /* Power requirement */
292 put_le16(p + 161, 0x8001); /* CF command set */
297 memcpy(s->io_buffer, p, sizeof(s->identify_data));
300 static void ide_set_signature(IDEState *s)
302 s->select &= 0xf0; /* clear head */
306 if (s->drive_kind == IDE_CD) {
318 static inline void ide_abort_command(IDEState *s)
320 s->status = READY_STAT | ERR_STAT;
324 static inline void ide_dma_submit_check(IDEState *s,
325 BlockDriverCompletionFunc *dma_cb)
327 if (s->bus->dma->aiocb)
332 /* prepare data transfer and tell what to do after */
333 static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
334 EndTransferFunc *end_transfer_func)
336 s->end_transfer_func = end_transfer_func;
338 s->data_end = buf + size;
339 if (!(s->status & ERR_STAT)) {
340 s->status |= DRQ_STAT;
342 s->bus->dma->ops->start_transfer(s->bus->dma);
345 static void ide_transfer_stop(IDEState *s)
347 s->end_transfer_func = ide_transfer_stop;
348 s->data_ptr = s->io_buffer;
349 s->data_end = s->io_buffer;
350 s->status &= ~DRQ_STAT;
353 int64_t ide_get_sector(IDEState *s)
356 if (s->select & 0x40) {
359 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
360 (s->lcyl << 8) | s->sector;
362 sector_num = ((int64_t)s->hob_hcyl << 40) |
363 ((int64_t) s->hob_lcyl << 32) |
364 ((int64_t) s->hob_sector << 24) |
365 ((int64_t) s->hcyl << 16) |
366 ((int64_t) s->lcyl << 8) | s->sector;
369 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
370 (s->select & 0x0f) * s->sectors + (s->sector - 1);
375 void ide_set_sector(IDEState *s, int64_t sector_num)
378 if (s->select & 0x40) {
380 s->select = (s->select & 0xf0) | (sector_num >> 24);
381 s->hcyl = (sector_num >> 16);
382 s->lcyl = (sector_num >> 8);
383 s->sector = (sector_num);
385 s->sector = sector_num;
386 s->lcyl = sector_num >> 8;
387 s->hcyl = sector_num >> 16;
388 s->hob_sector = sector_num >> 24;
389 s->hob_lcyl = sector_num >> 32;
390 s->hob_hcyl = sector_num >> 40;
393 cyl = sector_num / (s->heads * s->sectors);
394 r = sector_num % (s->heads * s->sectors);
397 s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
398 s->sector = (r % s->sectors) + 1;
402 static void ide_rw_error(IDEState *s) {
403 ide_abort_command(s);
407 void ide_sector_read(IDEState *s)
412 s->status = READY_STAT | SEEK_STAT;
413 s->error = 0; /* not needed by IDE spec, but needed by Windows */
414 sector_num = ide_get_sector(s);
417 /* no more sector to read from disk */
418 ide_transfer_stop(s);
420 #if defined(DEBUG_IDE)
421 printf("read sector=%" PRId64 "\n", sector_num);
423 if (n > s->req_nb_sectors)
424 n = s->req_nb_sectors;
425 ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
427 if (ide_handle_rw_error(s, -ret,
428 BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ))
433 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
435 ide_set_sector(s, sector_num + n);
440 static void dma_buf_commit(IDEState *s, int is_write)
442 qemu_sglist_destroy(&s->sg);
445 static void ide_set_inactive(IDEState *s)
447 s->bus->dma->aiocb = NULL;
448 s->bus->dma->ops->set_inactive(s->bus->dma);
451 void ide_dma_error(IDEState *s)
453 ide_transfer_stop(s);
455 s->status = READY_STAT | ERR_STAT;
457 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
461 static int ide_handle_rw_error(IDEState *s, int error, int op)
463 int is_read = (op & BM_STATUS_RETRY_READ);
464 BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
466 if (action == BLOCK_ERR_IGNORE) {
467 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
471 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
472 || action == BLOCK_ERR_STOP_ANY) {
473 s->bus->dma->ops->set_unit(s->bus->dma, s->unit);
474 s->bus->dma->ops->add_status(s->bus->dma, op);
475 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
478 if (op & BM_STATUS_DMA_RETRY) {
479 dma_buf_commit(s, 0);
484 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
490 void ide_read_dma_cb(void *opaque, int ret)
492 IDEState *s = opaque;
497 if (ide_handle_rw_error(s, -ret,
498 BM_STATUS_DMA_RETRY | BM_STATUS_RETRY_READ))
504 n = s->io_buffer_size >> 9;
505 sector_num = ide_get_sector(s);
507 dma_buf_commit(s, 1);
509 ide_set_sector(s, sector_num);
513 /* end of transfer ? */
514 if (s->nsector == 0) {
515 s->status = READY_STAT | SEEK_STAT;
518 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
523 /* launch next transfer */
525 s->io_buffer_index = 0;
526 s->io_buffer_size = n * 512;
527 if (s->bus->dma->ops->prepare_buf(s->bus->dma, 1) == 0)
530 printf("aio_read: sector_num=%" PRId64 " n=%d\n", sector_num, n);
532 s->bus->dma->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num, ide_read_dma_cb, s);
533 ide_dma_submit_check(s, ide_read_dma_cb);
536 static void ide_sector_read_dma(IDEState *s)
538 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
539 s->io_buffer_index = 0;
540 s->io_buffer_size = 0;
542 s->bus->dma->ops->start_dma(s->bus->dma, s, ide_read_dma_cb);
545 static void ide_sector_write_timer_cb(void *opaque)
547 IDEState *s = opaque;
551 void ide_sector_write(IDEState *s)
556 s->status = READY_STAT | SEEK_STAT;
557 sector_num = ide_get_sector(s);
558 #if defined(DEBUG_IDE)
559 printf("write sector=%" PRId64 "\n", sector_num);
562 if (n > s->req_nb_sectors)
563 n = s->req_nb_sectors;
564 ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
567 if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY))
572 if (s->nsector == 0) {
573 /* no more sectors to write */
574 ide_transfer_stop(s);
577 if (n1 > s->req_nb_sectors)
578 n1 = s->req_nb_sectors;
579 ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
581 ide_set_sector(s, sector_num + n);
583 if (win2k_install_hack && ((++s->irq_count % 16) == 0)) {
584 /* It seems there is a bug in the Windows 2000 installer HDD
585 IDE driver which fills the disk with empty logs when the
586 IDE write IRQ comes too early. This hack tries to correct
587 that at the expense of slower write performances. Use this
588 option _only_ to install Windows 2000. You must disable it
590 qemu_mod_timer(s->sector_write_timer,
591 qemu_get_clock(vm_clock) + (get_ticks_per_sec() / 1000));
597 void ide_write_dma_cb(void *opaque, int ret)
599 IDEState *s = opaque;
604 if (ide_handle_rw_error(s, -ret, BM_STATUS_DMA_RETRY))
608 n = s->io_buffer_size >> 9;
609 sector_num = ide_get_sector(s);
611 dma_buf_commit(s, 0);
613 ide_set_sector(s, sector_num);
617 /* end of transfer ? */
618 if (s->nsector == 0) {
619 s->status = READY_STAT | SEEK_STAT;
622 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
628 s->io_buffer_size = n * 512;
629 /* launch next transfer */
630 if (s->bus->dma->ops->prepare_buf(s->bus->dma, 0) == 0)
633 printf("aio_write: sector_num=%" PRId64 " n=%d\n", sector_num, n);
635 s->bus->dma->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num, ide_write_dma_cb, s);
636 ide_dma_submit_check(s, ide_write_dma_cb);
639 static void ide_sector_write_dma(IDEState *s)
641 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
642 s->io_buffer_index = 0;
643 s->io_buffer_size = 0;
645 s->bus->dma->ops->start_dma(s->bus->dma, s, ide_write_dma_cb);
648 void ide_atapi_cmd_ok(IDEState *s)
651 s->status = READY_STAT | SEEK_STAT;
652 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
656 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
658 #ifdef DEBUG_IDE_ATAPI
659 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
661 s->error = sense_key << 4;
662 s->status = READY_STAT | ERR_STAT;
663 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
664 s->sense_key = sense_key;
669 static void ide_atapi_cmd_check_status(IDEState *s)
671 #ifdef DEBUG_IDE_ATAPI
672 printf("atapi_cmd_check_status\n");
674 s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
675 s->status = ERR_STAT;
680 static void ide_flush_cb(void *opaque, int ret)
682 IDEState *s = opaque;
685 /* XXX: What sector number to set here? */
686 if (ide_handle_rw_error(s, -ret, BM_STATUS_RETRY_FLUSH)) {
691 s->status = READY_STAT | SEEK_STAT;
695 void ide_flush_cache(IDEState *s)
697 BlockDriverAIOCB *acb;
704 acb = bdrv_aio_flush(s->bs, ide_flush_cb, s);
706 ide_flush_cb(s, -EIO);
710 static inline void cpu_to_ube16(uint8_t *buf, int val)
716 static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
724 static inline int ube16_to_cpu(const uint8_t *buf)
726 return (buf[0] << 8) | buf[1];
729 static inline int ube32_to_cpu(const uint8_t *buf)
731 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
734 static void lba_to_msf(uint8_t *buf, int lba)
737 buf[0] = (lba / 75) / 60;
738 buf[1] = (lba / 75) % 60;
742 static void cd_data_to_raw(uint8_t *buf, int lba)
746 memset(buf + 1, 0xff, 10);
750 lba_to_msf(buf, lba);
751 buf[3] = 0x01; /* mode 1 data */
755 /* XXX: ECC not computed */
759 static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
764 switch(sector_size) {
766 ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
769 ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
772 cd_data_to_raw(buf, lba);
781 void ide_atapi_io_error(IDEState *s, int ret)
783 /* XXX: handle more errors */
784 if (ret == -ENOMEDIUM) {
785 ide_atapi_cmd_error(s, SENSE_NOT_READY,
786 ASC_MEDIUM_NOT_PRESENT);
788 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
789 ASC_LOGICAL_BLOCK_OOR);
793 /* The whole ATAPI transfer logic is handled in this function */
794 static void ide_atapi_cmd_reply_end(IDEState *s)
796 int byte_count_limit, size, ret;
797 #ifdef DEBUG_IDE_ATAPI
798 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
799 s->packet_transfer_size,
800 s->elementary_transfer_size,
803 if (s->packet_transfer_size <= 0) {
804 /* end of transfer */
805 ide_transfer_stop(s);
806 s->status = READY_STAT | SEEK_STAT;
807 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
809 #ifdef DEBUG_IDE_ATAPI
810 printf("status=0x%x\n", s->status);
813 /* see if a new sector must be read */
814 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
815 ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
817 ide_transfer_stop(s);
818 ide_atapi_io_error(s, ret);
822 s->io_buffer_index = 0;
824 if (s->elementary_transfer_size > 0) {
825 /* there are some data left to transmit in this elementary
827 size = s->cd_sector_size - s->io_buffer_index;
828 if (size > s->elementary_transfer_size)
829 size = s->elementary_transfer_size;
830 s->packet_transfer_size -= size;
831 s->elementary_transfer_size -= size;
832 s->io_buffer_index += size;
833 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
834 size, ide_atapi_cmd_reply_end);
836 /* a new transfer is needed */
837 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
838 byte_count_limit = s->lcyl | (s->hcyl << 8);
839 #ifdef DEBUG_IDE_ATAPI
840 printf("byte_count_limit=%d\n", byte_count_limit);
842 if (byte_count_limit == 0xffff)
844 size = s->packet_transfer_size;
845 if (size > byte_count_limit) {
846 /* byte count limit must be even if this case */
847 if (byte_count_limit & 1)
849 size = byte_count_limit;
853 s->elementary_transfer_size = size;
854 /* we cannot transmit more than one sector at a time */
856 if (size > (s->cd_sector_size - s->io_buffer_index))
857 size = (s->cd_sector_size - s->io_buffer_index);
859 s->packet_transfer_size -= size;
860 s->elementary_transfer_size -= size;
861 s->io_buffer_index += size;
862 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
863 size, ide_atapi_cmd_reply_end);
865 #ifdef DEBUG_IDE_ATAPI
866 printf("status=0x%x\n", s->status);
872 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
873 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
877 s->lba = -1; /* no sector read */
878 s->packet_transfer_size = size;
879 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
880 s->elementary_transfer_size = 0;
881 s->io_buffer_index = 0;
884 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
885 s->bus->dma->ops->start_dma(s->bus->dma, s,
886 ide_atapi_cmd_read_dma_cb);
888 s->status = READY_STAT | SEEK_STAT;
889 ide_atapi_cmd_reply_end(s);
893 /* start a CD-CDROM read command */
894 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
898 s->packet_transfer_size = nb_sectors * sector_size;
899 s->elementary_transfer_size = 0;
900 s->io_buffer_index = sector_size;
901 s->cd_sector_size = sector_size;
903 s->status = READY_STAT | SEEK_STAT;
904 ide_atapi_cmd_reply_end(s);
907 /* ATAPI DMA support */
909 /* XXX: handle read errors */
910 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
912 IDEState *s = opaque;
916 ide_atapi_io_error(s, ret);
920 if (s->io_buffer_size > 0) {
922 * For a cdrom read sector command (s->lba != -1),
923 * adjust the lba for the next s->io_buffer_size chunk
924 * and dma the current chunk.
925 * For a command != read (s->lba == -1), just transfer
929 if (s->cd_sector_size == 2352) {
931 cd_data_to_raw(s->io_buffer, s->lba);
933 n = s->io_buffer_size >> 11;
937 s->packet_transfer_size -= s->io_buffer_size;
938 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
942 if (s->packet_transfer_size <= 0) {
943 s->status = READY_STAT | SEEK_STAT;
944 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
947 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
952 s->io_buffer_index = 0;
953 if (s->cd_sector_size == 2352) {
955 s->io_buffer_size = s->cd_sector_size;
958 n = s->packet_transfer_size >> 11;
959 if (n > (IDE_DMA_BUF_SECTORS / 4))
960 n = (IDE_DMA_BUF_SECTORS / 4);
961 s->io_buffer_size = n * 2048;
965 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
967 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
968 s->bus->dma->iov.iov_len = n * 4 * 512;
969 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
970 s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
971 &s->bus->dma->qiov, n * 4,
972 ide_atapi_cmd_read_dma_cb, s);
973 if (!s->bus->dma->aiocb) {
974 /* Note: media not present is the most likely case */
975 ide_atapi_cmd_error(s, SENSE_NOT_READY,
976 ASC_MEDIUM_NOT_PRESENT);
981 /* start a CD-CDROM read command with DMA */
982 /* XXX: test if DMA is available */
983 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
987 s->packet_transfer_size = nb_sectors * sector_size;
988 s->io_buffer_index = 0;
989 s->io_buffer_size = 0;
990 s->cd_sector_size = sector_size;
992 /* XXX: check if BUSY_STAT should be set */
993 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
994 s->bus->dma->ops->start_dma(s->bus->dma, s,
995 ide_atapi_cmd_read_dma_cb);
998 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
1001 #ifdef DEBUG_IDE_ATAPI
1002 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
1006 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
1008 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
1012 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
1015 uint8_t *buf_profile = buf + 12; /* start of profiles */
1017 buf_profile += ((*index) * 4); /* start of indexed profile */
1018 cpu_to_ube16 (buf_profile, profile);
1019 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
1021 /* each profile adds 4 bytes to the response */
1023 buf[11] += 4; /* Additional Length */
1028 static int ide_dvd_read_structure(IDEState *s, int format,
1029 const uint8_t *packet, uint8_t *buf)
1032 case 0x0: /* Physical format information */
1034 int layer = packet[6];
1035 uint64_t total_sectors;
1038 return -ASC_INV_FIELD_IN_CMD_PACKET;
1040 bdrv_get_geometry(s->bs, &total_sectors);
1041 total_sectors >>= 2;
1042 if (total_sectors == 0)
1043 return -ASC_MEDIUM_NOT_PRESENT;
1045 buf[4] = 1; /* DVD-ROM, part version 1 */
1046 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1047 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1048 buf[7] = 0; /* default densities */
1050 /* FIXME: 0x30000 per spec? */
1051 cpu_to_ube32(buf + 8, 0); /* start sector */
1052 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
1053 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
1055 /* Size of buffer, not including 2 byte size field */
1056 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
1058 /* 2k data + 4 byte header */
1062 case 0x01: /* DVD copyright information */
1063 buf[4] = 0; /* no copyright data */
1064 buf[5] = 0; /* no region restrictions */
1066 /* Size of buffer, not including 2 byte size field */
1067 cpu_to_be16wu((uint16_t *)buf, 4 + 2);
1069 /* 4 byte header + 4 byte data */
1072 case 0x03: /* BCA information - invalid field for no BCA info */
1073 return -ASC_INV_FIELD_IN_CMD_PACKET;
1075 case 0x04: /* DVD disc manufacturing information */
1076 /* Size of buffer, not including 2 byte size field */
1077 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
1079 /* 2k data + 4 byte header */
1084 * This lists all the command capabilities above. Add new ones
1085 * in order and update the length and buffer return values.
1088 buf[4] = 0x00; /* Physical format */
1089 buf[5] = 0x40; /* Not writable, is readable */
1090 cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
1092 buf[8] = 0x01; /* Copyright info */
1093 buf[9] = 0x40; /* Not writable, is readable */
1094 cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
1096 buf[12] = 0x03; /* BCA info */
1097 buf[13] = 0x40; /* Not writable, is readable */
1098 cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
1100 buf[16] = 0x04; /* Manufacturing info */
1101 buf[17] = 0x40; /* Not writable, is readable */
1102 cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
1104 /* Size of buffer, not including 2 byte size field */
1105 cpu_to_be16wu((uint16_t *)buf, 16 + 2);
1107 /* data written + 4 byte header */
1110 default: /* TODO: formats beyond DVD-ROM requires */
1111 return -ASC_INV_FIELD_IN_CMD_PACKET;
1115 static void ide_atapi_cmd(IDEState *s)
1117 const uint8_t *packet;
1121 packet = s->io_buffer;
1123 #ifdef DEBUG_IDE_ATAPI
1126 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1127 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1128 printf(" %02x", packet[i]);
1133 /* If there's a UNIT_ATTENTION condition pending, only
1134 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1135 if (s->sense_key == SENSE_UNIT_ATTENTION &&
1136 s->io_buffer[0] != GPCMD_REQUEST_SENSE &&
1137 s->io_buffer[0] != GPCMD_INQUIRY) {
1138 ide_atapi_cmd_check_status(s);
1141 switch(s->io_buffer[0]) {
1142 case GPCMD_TEST_UNIT_READY:
1143 if (bdrv_is_inserted(s->bs) && !s->cdrom_changed) {
1144 ide_atapi_cmd_ok(s);
1146 s->cdrom_changed = 0;
1147 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1148 ASC_MEDIUM_NOT_PRESENT);
1151 case GPCMD_MODE_SENSE_6:
1152 case GPCMD_MODE_SENSE_10:
1155 if (packet[0] == GPCMD_MODE_SENSE_10)
1156 max_len = ube16_to_cpu(packet + 7);
1158 max_len = packet[4];
1159 action = packet[2] >> 6;
1160 code = packet[2] & 0x3f;
1162 case 0: /* current values */
1164 case GPMODE_R_W_ERROR_PAGE: /* error recovery */
1165 cpu_to_ube16(&buf[0], 16 + 6);
1181 ide_atapi_cmd_reply(s, 16, max_len);
1183 case GPMODE_AUDIO_CTL_PAGE:
1184 cpu_to_ube16(&buf[0], 24 + 6);
1192 /* Fill with CDROM audio volume */
1198 ide_atapi_cmd_reply(s, 24, max_len);
1200 case GPMODE_CAPABILITIES_PAGE:
1201 cpu_to_ube16(&buf[0], 28 + 6);
1214 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1215 code checks for this to automount media. */
1218 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
1219 if (bdrv_is_locked(s->bs))
1222 cpu_to_ube16(&buf[16], 706);
1225 cpu_to_ube16(&buf[20], 512);
1226 cpu_to_ube16(&buf[22], 706);
1231 ide_atapi_cmd_reply(s, 28, max_len);
1237 case 1: /* changeable values */
1239 case 2: /* default values */
1242 case 3: /* saved values */
1243 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1244 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1249 case GPCMD_REQUEST_SENSE:
1250 max_len = packet[4];
1252 buf[0] = 0x70 | (1 << 7);
1253 buf[2] = s->sense_key;
1256 if (s->sense_key == SENSE_UNIT_ATTENTION)
1257 s->sense_key = SENSE_NONE;
1258 ide_atapi_cmd_reply(s, 18, max_len);
1260 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
1261 if (bdrv_is_inserted(s->bs)) {
1262 bdrv_set_locked(s->bs, packet[4] & 1);
1263 ide_atapi_cmd_ok(s);
1265 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1266 ASC_MEDIUM_NOT_PRESENT);
1272 int nb_sectors, lba;
1274 if (packet[0] == GPCMD_READ_10)
1275 nb_sectors = ube16_to_cpu(packet + 7);
1277 nb_sectors = ube32_to_cpu(packet + 6);
1278 lba = ube32_to_cpu(packet + 2);
1279 if (nb_sectors == 0) {
1280 ide_atapi_cmd_ok(s);
1283 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1288 int nb_sectors, lba, transfer_request;
1290 nb_sectors = (packet[6] << 16) | (packet[7] << 8) | packet[8];
1291 lba = ube32_to_cpu(packet + 2);
1292 if (nb_sectors == 0) {
1293 ide_atapi_cmd_ok(s);
1296 transfer_request = packet[9];
1297 switch(transfer_request & 0xf8) {
1300 ide_atapi_cmd_ok(s);
1304 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1308 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1311 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1312 ASC_INV_FIELD_IN_CMD_PACKET);
1320 uint64_t total_sectors;
1322 bdrv_get_geometry(s->bs, &total_sectors);
1323 total_sectors >>= 2;
1324 if (total_sectors == 0) {
1325 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1326 ASC_MEDIUM_NOT_PRESENT);
1329 lba = ube32_to_cpu(packet + 2);
1330 if (lba >= total_sectors) {
1331 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1332 ASC_LOGICAL_BLOCK_OOR);
1335 ide_atapi_cmd_ok(s);
1338 case GPCMD_START_STOP_UNIT:
1340 int start, eject, err = 0;
1341 start = packet[4] & 1;
1342 eject = (packet[4] >> 1) & 1;
1345 err = bdrv_eject(s->bs, !start);
1350 ide_atapi_cmd_ok(s);
1353 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1354 ASC_MEDIA_REMOVAL_PREVENTED);
1357 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1358 ASC_MEDIUM_NOT_PRESENT);
1363 case GPCMD_MECHANISM_STATUS:
1365 max_len = ube16_to_cpu(packet + 8);
1366 cpu_to_ube16(buf, 0);
1367 /* no current LBA */
1372 cpu_to_ube16(buf + 6, 0);
1373 ide_atapi_cmd_reply(s, 8, max_len);
1376 case GPCMD_READ_TOC_PMA_ATIP:
1378 int format, msf, start_track, len;
1379 uint64_t total_sectors;
1381 bdrv_get_geometry(s->bs, &total_sectors);
1382 total_sectors >>= 2;
1383 if (total_sectors == 0) {
1384 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1385 ASC_MEDIUM_NOT_PRESENT);
1388 max_len = ube16_to_cpu(packet + 7);
1389 format = packet[9] >> 6;
1390 msf = (packet[1] >> 1) & 1;
1391 start_track = packet[6];
1394 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1397 ide_atapi_cmd_reply(s, len, max_len);
1400 /* multi session : only a single session defined */
1405 ide_atapi_cmd_reply(s, 12, max_len);
1408 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1411 ide_atapi_cmd_reply(s, len, max_len);
1415 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1416 ASC_INV_FIELD_IN_CMD_PACKET);
1421 case GPCMD_READ_CDVD_CAPACITY:
1423 uint64_t total_sectors;
1425 bdrv_get_geometry(s->bs, &total_sectors);
1426 total_sectors >>= 2;
1427 if (total_sectors == 0) {
1428 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1429 ASC_MEDIUM_NOT_PRESENT);
1432 /* NOTE: it is really the number of sectors minus 1 */
1433 cpu_to_ube32(buf, total_sectors - 1);
1434 cpu_to_ube32(buf + 4, 2048);
1435 ide_atapi_cmd_reply(s, 8, 8);
1438 case GPCMD_READ_DVD_STRUCTURE:
1440 int media = packet[1];
1441 int format = packet[7];
1444 max_len = ube16_to_cpu(packet + 8);
1446 if (format < 0xff) {
1447 if (media_is_cd(s)) {
1448 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1449 ASC_INCOMPATIBLE_FORMAT);
1451 } else if (!media_present(s)) {
1452 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1453 ASC_INV_FIELD_IN_CMD_PACKET);
1458 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1459 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1465 ret = ide_dvd_read_structure(s, format, packet, buf);
1468 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1470 ide_atapi_cmd_reply(s, ret, max_len);
1474 /* TODO: BD support, fall through for now */
1476 /* Generic disk structures */
1477 case 0x80: /* TODO: AACS volume identifier */
1478 case 0x81: /* TODO: AACS media serial number */
1479 case 0x82: /* TODO: AACS media identifier */
1480 case 0x83: /* TODO: AACS media key block */
1481 case 0x90: /* TODO: List of recognized format layers */
1482 case 0xc0: /* TODO: Write protection status */
1484 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1485 ASC_INV_FIELD_IN_CMD_PACKET);
1490 case GPCMD_SET_SPEED:
1491 ide_atapi_cmd_ok(s);
1494 max_len = packet[4];
1495 buf[0] = 0x05; /* CD-ROM */
1496 buf[1] = 0x80; /* removable */
1497 buf[2] = 0x00; /* ISO */
1498 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1499 buf[4] = 31; /* additional length */
1500 buf[5] = 0; /* reserved */
1501 buf[6] = 0; /* reserved */
1502 buf[7] = 0; /* reserved */
1503 padstr8(buf + 8, 8, "QEMU");
1504 padstr8(buf + 16, 16, "QEMU DVD-ROM");
1505 padstr8(buf + 32, 4, s->version);
1506 ide_atapi_cmd_reply(s, 36, max_len);
1508 case GPCMD_GET_CONFIGURATION:
1513 /* only feature 0 is supported */
1514 if (packet[2] != 0 || packet[3] != 0) {
1515 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1516 ASC_INV_FIELD_IN_CMD_PACKET);
1520 /* XXX: could result in alignment problems in some architectures */
1521 max_len = ube16_to_cpu(packet + 7);
1524 * XXX: avoid overflow for io_buffer if max_len is bigger than
1525 * the size of that buffer (dimensioned to max number of
1526 * sectors to transfer at once)
1528 * Only a problem if the feature/profiles grow.
1530 if (max_len > 512) /* XXX: assume 1 sector */
1533 memset(buf, 0, max_len);
1535 * the number of sectors from the media tells us which profile
1536 * to use as current. 0 means there is no media
1538 if (media_is_dvd(s))
1539 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
1540 else if (media_is_cd(s))
1541 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
1543 buf[10] = 0x02 | 0x01; /* persistent and current */
1544 len = 12; /* headers: 8 + 4 */
1545 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
1546 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
1547 cpu_to_ube32(buf, len - 4); /* data length */
1549 ide_atapi_cmd_reply(s, len, max_len);
1552 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
1553 max_len = ube16_to_cpu(packet + 7);
1555 if (packet[1] & 0x01) { /* polling */
1556 /* We don't support any event class (yet). */
1557 cpu_to_ube16(buf, 0x00); /* No event descriptor returned */
1558 buf[2] = 0x80; /* No Event Available (NEA) */
1559 buf[3] = 0x00; /* Empty supported event classes */
1560 ide_atapi_cmd_reply(s, 4, max_len);
1561 } else { /* asynchronous mode */
1562 /* Only polling is supported, asynchronous mode is not. */
1563 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1564 ASC_INV_FIELD_IN_CMD_PACKET);
1568 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1569 ASC_ILLEGAL_OPCODE);
1574 static void ide_cfata_metadata_inquiry(IDEState *s)
1579 p = (uint16_t *) s->io_buffer;
1580 memset(p, 0, 0x200);
1581 spd = ((s->mdata_size - 1) >> 9) + 1;
1583 put_le16(p + 0, 0x0001); /* Data format revision */
1584 put_le16(p + 1, 0x0000); /* Media property: silicon */
1585 put_le16(p + 2, s->media_changed); /* Media status */
1586 put_le16(p + 3, s->mdata_size & 0xffff); /* Capacity in bytes (low) */
1587 put_le16(p + 4, s->mdata_size >> 16); /* Capacity in bytes (high) */
1588 put_le16(p + 5, spd & 0xffff); /* Sectors per device (low) */
1589 put_le16(p + 6, spd >> 16); /* Sectors per device (high) */
1592 static void ide_cfata_metadata_read(IDEState *s)
1596 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1597 s->status = ERR_STAT;
1598 s->error = ABRT_ERR;
1602 p = (uint16_t *) s->io_buffer;
1603 memset(p, 0, 0x200);
1605 put_le16(p + 0, s->media_changed); /* Media status */
1606 memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1607 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1608 s->nsector << 9), 0x200 - 2));
1611 static void ide_cfata_metadata_write(IDEState *s)
1613 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1614 s->status = ERR_STAT;
1615 s->error = ABRT_ERR;
1619 s->media_changed = 0;
1621 memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1623 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1624 s->nsector << 9), 0x200 - 2));
1627 /* called when the inserted state of the media has changed */
1628 static void cdrom_change_cb(void *opaque)
1630 IDEState *s = opaque;
1631 uint64_t nb_sectors;
1633 bdrv_get_geometry(s->bs, &nb_sectors);
1634 s->nb_sectors = nb_sectors;
1636 s->sense_key = SENSE_UNIT_ATTENTION;
1637 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1638 s->cdrom_changed = 1;
1639 ide_set_irq(s->bus);
1642 static void ide_cmd_lba48_transform(IDEState *s, int lba48)
1646 /* handle the 'magic' 0 nsector count conversion here. to avoid
1647 * fiddling with the rest of the read logic, we just store the
1648 * full sector count in ->nsector and ignore ->hob_nsector from now
1654 if (!s->nsector && !s->hob_nsector)
1657 int lo = s->nsector;
1658 int hi = s->hob_nsector;
1660 s->nsector = (hi << 8) | lo;
1665 static void ide_clear_hob(IDEBus *bus)
1667 /* any write clears HOB high bit of device control register */
1668 bus->ifs[0].select &= ~(1 << 7);
1669 bus->ifs[1].select &= ~(1 << 7);
1672 void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1674 IDEBus *bus = opaque;
1677 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1682 /* ignore writes to command block while busy with previous command */
1683 if (addr != 7 && (idebus_active_if(bus)->status & (BUSY_STAT|DRQ_STAT)))
1691 /* NOTE: data is written to the two drives */
1692 bus->ifs[0].hob_feature = bus->ifs[0].feature;
1693 bus->ifs[1].hob_feature = bus->ifs[1].feature;
1694 bus->ifs[0].feature = val;
1695 bus->ifs[1].feature = val;
1699 bus->ifs[0].hob_nsector = bus->ifs[0].nsector;
1700 bus->ifs[1].hob_nsector = bus->ifs[1].nsector;
1701 bus->ifs[0].nsector = val;
1702 bus->ifs[1].nsector = val;
1706 bus->ifs[0].hob_sector = bus->ifs[0].sector;
1707 bus->ifs[1].hob_sector = bus->ifs[1].sector;
1708 bus->ifs[0].sector = val;
1709 bus->ifs[1].sector = val;
1713 bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl;
1714 bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl;
1715 bus->ifs[0].lcyl = val;
1716 bus->ifs[1].lcyl = val;
1720 bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl;
1721 bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl;
1722 bus->ifs[0].hcyl = val;
1723 bus->ifs[1].hcyl = val;
1726 /* FIXME: HOB readback uses bit 7 */
1727 bus->ifs[0].select = (val & ~0x10) | 0xa0;
1728 bus->ifs[1].select = (val | 0x10) | 0xa0;
1730 bus->unit = (val >> 4) & 1;
1735 ide_exec_cmd(bus, val);
1741 void ide_exec_cmd(IDEBus *bus, uint32_t val)
1747 #if defined(DEBUG_IDE)
1748 printf("ide: CMD=%02x\n", val);
1750 s = idebus_active_if(bus);
1751 /* ignore commands to non existant slave */
1752 if (s != bus->ifs && !s->bs)
1755 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1756 if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
1761 if (s->bs && s->drive_kind != IDE_CD) {
1762 if (s->drive_kind != IDE_CFATA)
1765 ide_cfata_identify(s);
1766 s->status = READY_STAT | SEEK_STAT;
1767 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1769 if (s->drive_kind == IDE_CD) {
1770 ide_set_signature(s);
1772 ide_abort_command(s);
1774 ide_set_irq(s->bus);
1779 s->status = READY_STAT | SEEK_STAT;
1780 ide_set_irq(s->bus);
1783 if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
1784 /* Disable Read and Write Multiple */
1785 s->mult_sectors = 0;
1786 s->status = READY_STAT | SEEK_STAT;
1787 } else if ((s->nsector & 0xff) != 0 &&
1788 ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1789 (s->nsector & (s->nsector - 1)) != 0)) {
1790 ide_abort_command(s);
1792 s->mult_sectors = s->nsector & 0xff;
1793 s->status = READY_STAT | SEEK_STAT;
1795 ide_set_irq(s->bus);
1797 case WIN_VERIFY_EXT:
1800 case WIN_VERIFY_ONCE:
1801 /* do sector number check ? */
1802 ide_cmd_lba48_transform(s, lba48);
1803 s->status = READY_STAT | SEEK_STAT;
1804 ide_set_irq(s->bus);
1812 ide_cmd_lba48_transform(s, lba48);
1813 s->req_nb_sectors = 1;
1819 case WIN_WRITE_ONCE:
1820 case CFA_WRITE_SECT_WO_ERASE:
1821 case WIN_WRITE_VERIFY:
1822 ide_cmd_lba48_transform(s, lba48);
1824 s->status = SEEK_STAT | READY_STAT;
1825 s->req_nb_sectors = 1;
1826 ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1827 s->media_changed = 1;
1829 case WIN_MULTREAD_EXT:
1832 if (!s->mult_sectors)
1834 ide_cmd_lba48_transform(s, lba48);
1835 s->req_nb_sectors = s->mult_sectors;
1838 case WIN_MULTWRITE_EXT:
1841 case CFA_WRITE_MULTI_WO_ERASE:
1842 if (!s->mult_sectors)
1844 ide_cmd_lba48_transform(s, lba48);
1846 s->status = SEEK_STAT | READY_STAT;
1847 s->req_nb_sectors = s->mult_sectors;
1849 if (n > s->req_nb_sectors)
1850 n = s->req_nb_sectors;
1851 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1852 s->media_changed = 1;
1854 case WIN_READDMA_EXT:
1857 case WIN_READDMA_ONCE:
1860 ide_cmd_lba48_transform(s, lba48);
1861 ide_sector_read_dma(s);
1863 case WIN_WRITEDMA_EXT:
1866 case WIN_WRITEDMA_ONCE:
1869 ide_cmd_lba48_transform(s, lba48);
1870 ide_sector_write_dma(s);
1871 s->media_changed = 1;
1873 case WIN_READ_NATIVE_MAX_EXT:
1875 case WIN_READ_NATIVE_MAX:
1876 ide_cmd_lba48_transform(s, lba48);
1877 ide_set_sector(s, s->nb_sectors - 1);
1878 s->status = READY_STAT | SEEK_STAT;
1879 ide_set_irq(s->bus);
1881 case WIN_CHECKPOWERMODE1:
1882 case WIN_CHECKPOWERMODE2:
1883 s->nsector = 0xff; /* device active or idle */
1884 s->status = READY_STAT | SEEK_STAT;
1885 ide_set_irq(s->bus);
1887 case WIN_SETFEATURES:
1890 /* XXX: valid for CDROM ? */
1891 switch(s->feature) {
1892 case 0xcc: /* reverting to power-on defaults enable */
1893 case 0x66: /* reverting to power-on defaults disable */
1894 case 0x02: /* write cache enable */
1895 case 0x82: /* write cache disable */
1896 case 0xaa: /* read look-ahead enable */
1897 case 0x55: /* read look-ahead disable */
1898 case 0x05: /* set advanced power management mode */
1899 case 0x85: /* disable advanced power management mode */
1900 case 0x69: /* NOP */
1901 case 0x67: /* NOP */
1902 case 0x96: /* NOP */
1903 case 0x9a: /* NOP */
1904 case 0x42: /* enable Automatic Acoustic Mode */
1905 case 0xc2: /* disable Automatic Acoustic Mode */
1906 s->status = READY_STAT | SEEK_STAT;
1907 ide_set_irq(s->bus);
1909 case 0x03: { /* set transfer mode */
1910 uint8_t val = s->nsector & 0x07;
1911 uint16_t *identify_data = (uint16_t *)s->identify_data;
1913 switch (s->nsector >> 3) {
1914 case 0x00: /* pio default */
1915 case 0x01: /* pio mode */
1916 put_le16(identify_data + 62,0x07);
1917 put_le16(identify_data + 63,0x07);
1918 put_le16(identify_data + 88,0x3f);
1920 case 0x02: /* sigle word dma mode*/
1921 put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
1922 put_le16(identify_data + 63,0x07);
1923 put_le16(identify_data + 88,0x3f);
1925 case 0x04: /* mdma mode */
1926 put_le16(identify_data + 62,0x07);
1927 put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
1928 put_le16(identify_data + 88,0x3f);
1930 case 0x08: /* udma mode */
1931 put_le16(identify_data + 62,0x07);
1932 put_le16(identify_data + 63,0x07);
1933 put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
1938 s->status = READY_STAT | SEEK_STAT;
1939 ide_set_irq(s->bus);
1946 case WIN_FLUSH_CACHE:
1947 case WIN_FLUSH_CACHE_EXT:
1952 case WIN_STANDBYNOW1:
1953 case WIN_STANDBYNOW2:
1954 case WIN_IDLEIMMEDIATE:
1955 case CFA_IDLEIMMEDIATE:
1960 s->status = READY_STAT;
1961 ide_set_irq(s->bus);
1964 if(s->drive_kind == IDE_CD)
1966 /* XXX: Check that seek is within bounds */
1967 s->status = READY_STAT | SEEK_STAT;
1968 ide_set_irq(s->bus);
1970 /* ATAPI commands */
1972 if (s->drive_kind == IDE_CD) {
1973 ide_atapi_identify(s);
1974 s->status = READY_STAT | SEEK_STAT;
1975 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1977 ide_abort_command(s);
1979 ide_set_irq(s->bus);
1982 ide_set_signature(s);
1983 if (s->drive_kind == IDE_CD)
1984 s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
1985 * devices to return a clear status register
1986 * with READY_STAT *not* set. */
1988 s->status = READY_STAT | SEEK_STAT;
1989 s->error = 0x01; /* Device 0 passed, Device 1 passed or not
1992 ide_set_irq(s->bus);
1995 if (s->drive_kind != IDE_CD)
1997 ide_set_signature(s);
1998 s->status = 0x00; /* NOTE: READY is _not_ set */
2002 if (s->drive_kind != IDE_CD)
2004 /* overlapping commands not supported */
2005 if (s->feature & 0x02)
2007 s->status = READY_STAT | SEEK_STAT;
2008 s->atapi_dma = s->feature & 1;
2010 ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
2013 /* CF-ATA commands */
2014 case CFA_REQ_EXT_ERROR_CODE:
2015 if (s->drive_kind != IDE_CFATA)
2017 s->error = 0x09; /* miscellaneous error */
2018 s->status = READY_STAT | SEEK_STAT;
2019 ide_set_irq(s->bus);
2021 case CFA_ERASE_SECTORS:
2022 case CFA_WEAR_LEVEL:
2023 if (s->drive_kind != IDE_CFATA)
2025 if (val == CFA_WEAR_LEVEL)
2027 if (val == CFA_ERASE_SECTORS)
2028 s->media_changed = 1;
2030 s->status = READY_STAT | SEEK_STAT;
2031 ide_set_irq(s->bus);
2033 case CFA_TRANSLATE_SECTOR:
2034 if (s->drive_kind != IDE_CFATA)
2037 s->status = READY_STAT | SEEK_STAT;
2038 memset(s->io_buffer, 0, 0x200);
2039 s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
2040 s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
2041 s->io_buffer[0x02] = s->select; /* Head */
2042 s->io_buffer[0x03] = s->sector; /* Sector */
2043 s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
2044 s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
2045 s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
2046 s->io_buffer[0x13] = 0x00; /* Erase flag */
2047 s->io_buffer[0x18] = 0x00; /* Hot count */
2048 s->io_buffer[0x19] = 0x00; /* Hot count */
2049 s->io_buffer[0x1a] = 0x01; /* Hot count */
2050 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2051 ide_set_irq(s->bus);
2053 case CFA_ACCESS_METADATA_STORAGE:
2054 if (s->drive_kind != IDE_CFATA)
2056 switch (s->feature) {
2057 case 0x02: /* Inquiry Metadata Storage */
2058 ide_cfata_metadata_inquiry(s);
2060 case 0x03: /* Read Metadata Storage */
2061 ide_cfata_metadata_read(s);
2063 case 0x04: /* Write Metadata Storage */
2064 ide_cfata_metadata_write(s);
2069 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2070 s->status = 0x00; /* NOTE: READY is _not_ set */
2071 ide_set_irq(s->bus);
2073 case IBM_SENSE_CONDITION:
2074 if (s->drive_kind != IDE_CFATA)
2076 switch (s->feature) {
2077 case 0x01: /* sense temperature in device */
2078 s->nsector = 0x50; /* +20 C */
2083 s->status = READY_STAT | SEEK_STAT;
2084 ide_set_irq(s->bus);
2088 if (s->drive_kind == IDE_CD)
2090 if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
2092 if (!s->smart_enabled && s->feature != SMART_ENABLE)
2094 switch (s->feature) {
2096 s->smart_enabled = 0;
2097 s->status = READY_STAT | SEEK_STAT;
2098 ide_set_irq(s->bus);
2101 s->smart_enabled = 1;
2102 s->status = READY_STAT | SEEK_STAT;
2103 ide_set_irq(s->bus);
2105 case SMART_ATTR_AUTOSAVE:
2106 switch (s->sector) {
2108 s->smart_autosave = 0;
2111 s->smart_autosave = 1;
2116 s->status = READY_STAT | SEEK_STAT;
2117 ide_set_irq(s->bus);
2120 if (!s->smart_errors) {
2127 s->status = READY_STAT | SEEK_STAT;
2128 ide_set_irq(s->bus);
2130 case SMART_READ_THRESH:
2131 memset(s->io_buffer, 0, 0x200);
2132 s->io_buffer[0] = 0x01; /* smart struct version */
2133 for (n=0; n<30; n++) {
2134 if (smart_attributes[n][0] == 0)
2136 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2137 s->io_buffer[2+1+(n*12)] = smart_attributes[n][4];
2139 for (n=0; n<511; n++) /* checksum */
2140 s->io_buffer[511] += s->io_buffer[n];
2141 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2142 s->status = READY_STAT | SEEK_STAT;
2143 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2144 ide_set_irq(s->bus);
2146 case SMART_READ_DATA:
2147 memset(s->io_buffer, 0, 0x200);
2148 s->io_buffer[0] = 0x01; /* smart struct version */
2149 for (n=0; n<30; n++) {
2150 if (smart_attributes[n][0] == 0)
2152 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2153 s->io_buffer[2+1+(n*12)] = smart_attributes[n][1];
2154 s->io_buffer[2+3+(n*12)] = smart_attributes[n][2];
2155 s->io_buffer[2+4+(n*12)] = smart_attributes[n][3];
2157 s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
2158 if (s->smart_selftest_count == 0) {
2159 s->io_buffer[363] = 0;
2162 s->smart_selftest_data[3 +
2163 (s->smart_selftest_count - 1) *
2166 s->io_buffer[364] = 0x20;
2167 s->io_buffer[365] = 0x01;
2168 /* offline data collection capacity: execute + self-test*/
2169 s->io_buffer[367] = (1<<4 | 1<<3 | 1);
2170 s->io_buffer[368] = 0x03; /* smart capability (1) */
2171 s->io_buffer[369] = 0x00; /* smart capability (2) */
2172 s->io_buffer[370] = 0x01; /* error logging supported */
2173 s->io_buffer[372] = 0x02; /* minutes for poll short test */
2174 s->io_buffer[373] = 0x36; /* minutes for poll ext test */
2175 s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
2177 for (n=0; n<511; n++)
2178 s->io_buffer[511] += s->io_buffer[n];
2179 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2180 s->status = READY_STAT | SEEK_STAT;
2181 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2182 ide_set_irq(s->bus);
2184 case SMART_READ_LOG:
2185 switch (s->sector) {
2186 case 0x01: /* summary smart error log */
2187 memset(s->io_buffer, 0, 0x200);
2188 s->io_buffer[0] = 0x01;
2189 s->io_buffer[1] = 0x00; /* no error entries */
2190 s->io_buffer[452] = s->smart_errors & 0xff;
2191 s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
2193 for (n=0; n<511; n++)
2194 s->io_buffer[511] += s->io_buffer[n];
2195 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2197 case 0x06: /* smart self test log */
2198 memset(s->io_buffer, 0, 0x200);
2199 s->io_buffer[0] = 0x01;
2200 if (s->smart_selftest_count == 0) {
2201 s->io_buffer[508] = 0;
2203 s->io_buffer[508] = s->smart_selftest_count;
2204 for (n=2; n<506; n++)
2205 s->io_buffer[n] = s->smart_selftest_data[n];
2207 for (n=0; n<511; n++)
2208 s->io_buffer[511] += s->io_buffer[n];
2209 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2214 s->status = READY_STAT | SEEK_STAT;
2215 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2216 ide_set_irq(s->bus);
2218 case SMART_EXECUTE_OFFLINE:
2219 switch (s->sector) {
2220 case 0: /* off-line routine */
2221 case 1: /* short self test */
2222 case 2: /* extended self test */
2223 s->smart_selftest_count++;
2224 if(s->smart_selftest_count > 21)
2225 s->smart_selftest_count = 0;
2226 n = 2 + (s->smart_selftest_count - 1) * 24;
2227 s->smart_selftest_data[n] = s->sector;
2228 s->smart_selftest_data[n+1] = 0x00; /* OK and finished */
2229 s->smart_selftest_data[n+2] = 0x34; /* hour count lsb */
2230 s->smart_selftest_data[n+3] = 0x12; /* hour count msb */
2231 s->status = READY_STAT | SEEK_STAT;
2232 ide_set_irq(s->bus);
2244 ide_abort_command(s);
2245 ide_set_irq(s->bus);
2250 uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
2252 IDEBus *bus = opaque;
2253 IDEState *s = idebus_active_if(bus);
2258 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2259 //hob = s->select & (1 << 7);
2266 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2267 (s != bus->ifs && !s->bs))
2272 ret = s->hob_feature;
2275 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2278 ret = s->nsector & 0xff;
2280 ret = s->hob_nsector;
2283 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2288 ret = s->hob_sector;
2291 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2299 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2307 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2314 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2315 (s != bus->ifs && !s->bs))
2319 qemu_irq_lower(bus->irq);
2323 printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
2328 uint32_t ide_status_read(void *opaque, uint32_t addr)
2330 IDEBus *bus = opaque;
2331 IDEState *s = idebus_active_if(bus);
2334 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2335 (s != bus->ifs && !s->bs))
2340 printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
2345 void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
2347 IDEBus *bus = opaque;
2352 printf("ide: write control addr=0x%x val=%02x\n", addr, val);
2354 /* common for both drives */
2355 if (!(bus->cmd & IDE_CMD_RESET) &&
2356 (val & IDE_CMD_RESET)) {
2357 /* reset low to high */
2358 for(i = 0;i < 2; i++) {
2360 s->status = BUSY_STAT | SEEK_STAT;
2363 } else if ((bus->cmd & IDE_CMD_RESET) &&
2364 !(val & IDE_CMD_RESET)) {
2366 for(i = 0;i < 2; i++) {
2368 if (s->drive_kind == IDE_CD)
2369 s->status = 0x00; /* NOTE: READY is _not_ set */
2371 s->status = READY_STAT | SEEK_STAT;
2372 ide_set_signature(s);
2379 void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
2381 IDEBus *bus = opaque;
2382 IDEState *s = idebus_active_if(bus);
2385 /* PIO data access allowed only when DRQ bit is set */
2386 if (!(s->status & DRQ_STAT))
2390 *(uint16_t *)p = le16_to_cpu(val);
2393 if (p >= s->data_end)
2394 s->end_transfer_func(s);
2397 uint32_t ide_data_readw(void *opaque, uint32_t addr)
2399 IDEBus *bus = opaque;
2400 IDEState *s = idebus_active_if(bus);
2404 /* PIO data access allowed only when DRQ bit is set */
2405 if (!(s->status & DRQ_STAT))
2409 ret = cpu_to_le16(*(uint16_t *)p);
2412 if (p >= s->data_end)
2413 s->end_transfer_func(s);
2417 void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
2419 IDEBus *bus = opaque;
2420 IDEState *s = idebus_active_if(bus);
2423 /* PIO data access allowed only when DRQ bit is set */
2424 if (!(s->status & DRQ_STAT))
2428 *(uint32_t *)p = le32_to_cpu(val);
2431 if (p >= s->data_end)
2432 s->end_transfer_func(s);
2435 uint32_t ide_data_readl(void *opaque, uint32_t addr)
2437 IDEBus *bus = opaque;
2438 IDEState *s = idebus_active_if(bus);
2442 /* PIO data access allowed only when DRQ bit is set */
2443 if (!(s->status & DRQ_STAT))
2447 ret = cpu_to_le32(*(uint32_t *)p);
2450 if (p >= s->data_end)
2451 s->end_transfer_func(s);
2455 static void ide_dummy_transfer_stop(IDEState *s)
2457 s->data_ptr = s->io_buffer;
2458 s->data_end = s->io_buffer;
2459 s->io_buffer[0] = 0xff;
2460 s->io_buffer[1] = 0xff;
2461 s->io_buffer[2] = 0xff;
2462 s->io_buffer[3] = 0xff;
2465 static void ide_reset(IDEState *s)
2468 printf("ide: reset\n");
2470 if (s->drive_kind == IDE_CFATA)
2471 s->mult_sectors = 0;
2473 s->mult_sectors = MAX_MULT_SECTORS;
2490 s->status = READY_STAT | SEEK_STAT;
2494 /* ATAPI specific */
2497 s->cdrom_changed = 0;
2498 s->packet_transfer_size = 0;
2499 s->elementary_transfer_size = 0;
2500 s->io_buffer_index = 0;
2501 s->cd_sector_size = 0;
2504 s->io_buffer_size = 0;
2505 s->req_nb_sectors = 0;
2507 ide_set_signature(s);
2508 /* init the transfer handler so that 0xffff is returned on data
2510 s->end_transfer_func = ide_dummy_transfer_stop;
2511 ide_dummy_transfer_stop(s);
2512 s->media_changed = 0;
2515 void ide_bus_reset(IDEBus *bus)
2519 ide_reset(&bus->ifs[0]);
2520 ide_reset(&bus->ifs[1]);
2523 /* pending async DMA */
2524 if (bus->dma->aiocb) {
2526 printf("aio_cancel\n");
2528 bdrv_aio_cancel(bus->dma->aiocb);
2529 bus->dma->aiocb = NULL;
2532 /* reset dma provider too */
2533 bus->dma->ops->reset(bus->dma);
2536 int ide_init_drive(IDEState *s, BlockDriverState *bs,
2537 const char *version, const char *serial)
2539 int cylinders, heads, secs;
2540 uint64_t nb_sectors;
2543 bdrv_get_geometry(bs, &nb_sectors);
2544 bdrv_guess_geometry(bs, &cylinders, &heads, &secs);
2545 if (cylinders < 1 || cylinders > 16383) {
2546 error_report("cyls must be between 1 and 16383");
2549 if (heads < 1 || heads > 16) {
2550 error_report("heads must be between 1 and 16");
2553 if (secs < 1 || secs > 63) {
2554 error_report("secs must be between 1 and 63");
2557 s->cylinders = cylinders;
2560 s->nb_sectors = nb_sectors;
2561 /* The SMART values should be preserved across power cycles
2563 s->smart_enabled = 1;
2564 s->smart_autosave = 1;
2565 s->smart_errors = 0;
2566 s->smart_selftest_count = 0;
2567 if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
2568 s->drive_kind = IDE_CD;
2569 bdrv_set_change_cb(bs, cdrom_change_cb, s);
2570 bs->buffer_alignment = 2048;
2572 if (!bdrv_is_inserted(s->bs)) {
2573 error_report("Device needs media, but drive is empty");
2576 if (bdrv_is_read_only(bs)) {
2577 error_report("Can't use a read-only drive");
2582 strncpy(s->drive_serial_str, serial, sizeof(s->drive_serial_str));
2584 snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
2585 "QM%05d", s->drive_serial);
2588 pstrcpy(s->version, sizeof(s->version), version);
2590 pstrcpy(s->version, sizeof(s->version), QEMU_VERSION);
2594 bdrv_set_removable(bs, s->drive_kind == IDE_CD);
2598 static void ide_init1(IDEBus *bus, int unit)
2600 static int drive_serial = 1;
2601 IDEState *s = &bus->ifs[unit];
2605 s->drive_serial = drive_serial++;
2606 /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
2607 s->io_buffer = qemu_memalign(2048, IDE_DMA_BUF_SECTORS*512 + 4);
2608 s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4;
2609 s->smart_selftest_data = qemu_blockalign(s->bs, 512);
2610 s->sector_write_timer = qemu_new_timer(vm_clock,
2611 ide_sector_write_timer_cb, s);
2614 static void ide_nop_start(IDEDMA *dma, IDEState *s,
2615 BlockDriverCompletionFunc *cb)
2619 static int ide_nop(IDEDMA *dma)
2624 static int ide_nop_int(IDEDMA *dma, int x)
2629 static void ide_nop_restart(void *opaque, int x, int y)
2633 static const IDEDMAOps ide_dma_nop_ops = {
2634 .start_dma = ide_nop_start,
2635 .start_transfer = ide_nop,
2636 .prepare_buf = ide_nop_int,
2637 .rw_buf = ide_nop_int,
2638 .set_unit = ide_nop_int,
2639 .add_status = ide_nop_int,
2640 .set_inactive = ide_nop,
2641 .restart_cb = ide_nop_restart,
2645 static IDEDMA ide_dma_nop = {
2646 .ops = &ide_dma_nop_ops,
2650 void ide_init2(IDEBus *bus, qemu_irq irq)
2654 for(i = 0; i < 2; i++) {
2656 ide_reset(&bus->ifs[i]);
2659 bus->dma = &ide_dma_nop;
2662 /* TODO convert users to qdev and remove */
2663 void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
2664 DriveInfo *hd1, qemu_irq irq)
2669 for(i = 0; i < 2; i++) {
2670 dinfo = i == 0 ? hd0 : hd1;
2673 if (ide_init_drive(&bus->ifs[i], dinfo->bdrv, NULL,
2674 *dinfo->serial ? dinfo->serial : NULL) < 0) {
2675 error_report("Can't set up IDE drive %s", dinfo->id);
2679 ide_reset(&bus->ifs[i]);
2683 bus->dma = &ide_dma_nop;
2686 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2)
2688 register_ioport_write(iobase, 8, 1, ide_ioport_write, bus);
2689 register_ioport_read(iobase, 8, 1, ide_ioport_read, bus);
2691 register_ioport_read(iobase2, 1, 1, ide_status_read, bus);
2692 register_ioport_write(iobase2, 1, 1, ide_cmd_write, bus);
2696 register_ioport_write(iobase, 2, 2, ide_data_writew, bus);
2697 register_ioport_read(iobase, 2, 2, ide_data_readw, bus);
2698 register_ioport_write(iobase, 4, 4, ide_data_writel, bus);
2699 register_ioport_read(iobase, 4, 4, ide_data_readl, bus);
2702 static bool is_identify_set(void *opaque, int version_id)
2704 IDEState *s = opaque;
2706 return s->identify_set != 0;
2709 static EndTransferFunc* transfer_end_table[] = {
2713 ide_atapi_cmd_reply_end,
2715 ide_dummy_transfer_stop,
2718 static int transfer_end_table_idx(EndTransferFunc *fn)
2722 for (i = 0; i < ARRAY_SIZE(transfer_end_table); i++)
2723 if (transfer_end_table[i] == fn)
2729 static int ide_drive_post_load(void *opaque, int version_id)
2731 IDEState *s = opaque;
2733 if (version_id < 3) {
2734 if (s->sense_key == SENSE_UNIT_ATTENTION &&
2735 s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED) {
2736 s->cdrom_changed = 1;
2742 static int ide_drive_pio_post_load(void *opaque, int version_id)
2744 IDEState *s = opaque;
2746 if (s->end_transfer_fn_idx > ARRAY_SIZE(transfer_end_table)) {
2749 s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
2750 s->data_ptr = s->io_buffer + s->cur_io_buffer_offset;
2751 s->data_end = s->data_ptr + s->cur_io_buffer_len;
2756 static void ide_drive_pio_pre_save(void *opaque)
2758 IDEState *s = opaque;
2761 s->cur_io_buffer_offset = s->data_ptr - s->io_buffer;
2762 s->cur_io_buffer_len = s->data_end - s->data_ptr;
2764 idx = transfer_end_table_idx(s->end_transfer_func);
2766 fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n",
2768 s->end_transfer_fn_idx = 2;
2770 s->end_transfer_fn_idx = idx;
2774 static bool ide_drive_pio_state_needed(void *opaque)
2776 IDEState *s = opaque;
2778 return (s->status & DRQ_STAT) != 0;
2781 const VMStateDescription vmstate_ide_drive_pio_state = {
2782 .name = "ide_drive/pio_state",
2784 .minimum_version_id = 1,
2785 .minimum_version_id_old = 1,
2786 .pre_save = ide_drive_pio_pre_save,
2787 .post_load = ide_drive_pio_post_load,
2788 .fields = (VMStateField []) {
2789 VMSTATE_INT32(req_nb_sectors, IDEState),
2790 VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
2791 vmstate_info_uint8, uint8_t),
2792 VMSTATE_INT32(cur_io_buffer_offset, IDEState),
2793 VMSTATE_INT32(cur_io_buffer_len, IDEState),
2794 VMSTATE_UINT8(end_transfer_fn_idx, IDEState),
2795 VMSTATE_INT32(elementary_transfer_size, IDEState),
2796 VMSTATE_INT32(packet_transfer_size, IDEState),
2797 VMSTATE_END_OF_LIST()
2801 const VMStateDescription vmstate_ide_drive = {
2802 .name = "ide_drive",
2804 .minimum_version_id = 0,
2805 .minimum_version_id_old = 0,
2806 .post_load = ide_drive_post_load,
2807 .fields = (VMStateField []) {
2808 VMSTATE_INT32(mult_sectors, IDEState),
2809 VMSTATE_INT32(identify_set, IDEState),
2810 VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set),
2811 VMSTATE_UINT8(feature, IDEState),
2812 VMSTATE_UINT8(error, IDEState),
2813 VMSTATE_UINT32(nsector, IDEState),
2814 VMSTATE_UINT8(sector, IDEState),
2815 VMSTATE_UINT8(lcyl, IDEState),
2816 VMSTATE_UINT8(hcyl, IDEState),
2817 VMSTATE_UINT8(hob_feature, IDEState),
2818 VMSTATE_UINT8(hob_sector, IDEState),
2819 VMSTATE_UINT8(hob_nsector, IDEState),
2820 VMSTATE_UINT8(hob_lcyl, IDEState),
2821 VMSTATE_UINT8(hob_hcyl, IDEState),
2822 VMSTATE_UINT8(select, IDEState),
2823 VMSTATE_UINT8(status, IDEState),
2824 VMSTATE_UINT8(lba48, IDEState),
2825 VMSTATE_UINT8(sense_key, IDEState),
2826 VMSTATE_UINT8(asc, IDEState),
2827 VMSTATE_UINT8_V(cdrom_changed, IDEState, 3),
2828 VMSTATE_END_OF_LIST()
2830 .subsections = (VMStateSubsection []) {
2832 .vmsd = &vmstate_ide_drive_pio_state,
2833 .needed = ide_drive_pio_state_needed,
2840 const VMStateDescription vmstate_ide_bus = {
2843 .minimum_version_id = 1,
2844 .minimum_version_id_old = 1,
2845 .fields = (VMStateField []) {
2846 VMSTATE_UINT8(cmd, IDEBus),
2847 VMSTATE_UINT8(unit, IDEBus),
2848 VMSTATE_END_OF_LIST()