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
28 #include "scsi-disk.h"
31 #include "block_int.h"
32 #include "qemu-timer.h"
35 #include "mac_dbdma.h"
38 #include "ide-internal.h"
40 static int smart_attributes
[][5] = {
41 /* id, flags, val, wrst, thrsh */
42 { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
43 { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
44 { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
45 { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
46 { 0x00, 0x00, 0x00, 0x00, 0x00}
49 /* XXX: DVDs that could fit on a CD will be reported as a CD */
50 static inline int media_present(IDEState
*s
)
52 return (s
->nb_sectors
> 0);
55 static inline int media_is_dvd(IDEState
*s
)
57 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
60 static inline int media_is_cd(IDEState
*s
)
62 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
65 #define IDE_TYPE_PIIX3 0
66 #define IDE_TYPE_CMD646 1
67 #define IDE_TYPE_PIIX4 2
71 #define MRDMODE_INTR_CH0 0x04
72 #define MRDMODE_INTR_CH1 0x08
73 #define MRDMODE_BLK_CH0 0x10
74 #define MRDMODE_BLK_CH1 0x20
75 #define UDIDETCR0 0x73
76 #define UDIDETCR1 0x7B
78 typedef struct PCIIDEState
{
82 int type
; /* see IDE_TYPE_xxx */
85 static void ide_dma_start(IDEState
*s
, BlockDriverCompletionFunc
*dma_cb
);
86 static void ide_dma_restart(IDEState
*s
);
87 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
89 static void padstr(char *str
, const char *src
, int len
)
92 for(i
= 0; i
< len
; i
++) {
101 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
104 for(i
= 0; i
< buf_size
; i
++) {
112 static void put_le16(uint16_t *p
, unsigned int v
)
117 static void ide_identify(IDEState
*s
)
120 unsigned int oldsize
;
122 if (s
->identify_set
) {
123 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
127 memset(s
->io_buffer
, 0, 512);
128 p
= (uint16_t *)s
->io_buffer
;
129 put_le16(p
+ 0, 0x0040);
130 put_le16(p
+ 1, s
->cylinders
);
131 put_le16(p
+ 3, s
->heads
);
132 put_le16(p
+ 4, 512 * s
->sectors
); /* XXX: retired, remove ? */
133 put_le16(p
+ 5, 512); /* XXX: retired, remove ? */
134 put_le16(p
+ 6, s
->sectors
);
135 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
136 put_le16(p
+ 20, 3); /* XXX: retired, remove ? */
137 put_le16(p
+ 21, 512); /* cache size in sectors */
138 put_le16(p
+ 22, 4); /* ecc bytes */
139 padstr((char *)(p
+ 23), QEMU_VERSION
, 8); /* firmware version */
140 padstr((char *)(p
+ 27), "QEMU HARDDISK", 40); /* model */
141 #if MAX_MULT_SECTORS > 1
142 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
144 put_le16(p
+ 48, 1); /* dword I/O */
145 put_le16(p
+ 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
146 put_le16(p
+ 51, 0x200); /* PIO transfer cycle */
147 put_le16(p
+ 52, 0x200); /* DMA transfer cycle */
148 put_le16(p
+ 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
149 put_le16(p
+ 54, s
->cylinders
);
150 put_le16(p
+ 55, s
->heads
);
151 put_le16(p
+ 56, s
->sectors
);
152 oldsize
= s
->cylinders
* s
->heads
* s
->sectors
;
153 put_le16(p
+ 57, oldsize
);
154 put_le16(p
+ 58, oldsize
>> 16);
156 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
157 put_le16(p
+ 60, s
->nb_sectors
);
158 put_le16(p
+ 61, s
->nb_sectors
>> 16);
159 put_le16(p
+ 62, 0x07); /* single word dma0-2 supported */
160 put_le16(p
+ 63, 0x07); /* mdma0-2 supported */
161 put_le16(p
+ 65, 120);
162 put_le16(p
+ 66, 120);
163 put_le16(p
+ 67, 120);
164 put_le16(p
+ 68, 120);
165 put_le16(p
+ 80, 0xf0); /* ata3 -> ata6 supported */
166 put_le16(p
+ 81, 0x16); /* conforms to ata5 */
167 /* 14=NOP supported, 0=SMART supported */
168 put_le16(p
+ 82, (1 << 14) | 1);
169 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
170 put_le16(p
+ 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
171 /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
172 put_le16(p
+ 84, (1 << 14) | 0);
173 /* 14 = NOP supported, 0=SMART feature set enabled */
174 put_le16(p
+ 85, (1 << 14) | 1);
175 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
176 put_le16(p
+ 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
177 /* 14=set to 1, 1=smart self test, 0=smart error logging */
178 put_le16(p
+ 87, (1 << 14) | 0);
179 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
180 put_le16(p
+ 93, 1 | (1 << 14) | 0x2000);
181 put_le16(p
+ 100, s
->nb_sectors
);
182 put_le16(p
+ 101, s
->nb_sectors
>> 16);
183 put_le16(p
+ 102, s
->nb_sectors
>> 32);
184 put_le16(p
+ 103, s
->nb_sectors
>> 48);
186 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
190 static void ide_atapi_identify(IDEState
*s
)
194 if (s
->identify_set
) {
195 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
199 memset(s
->io_buffer
, 0, 512);
200 p
= (uint16_t *)s
->io_buffer
;
201 /* Removable CDROM, 50us response, 12 byte packets */
202 put_le16(p
+ 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
203 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
204 put_le16(p
+ 20, 3); /* buffer type */
205 put_le16(p
+ 21, 512); /* cache size in sectors */
206 put_le16(p
+ 22, 4); /* ecc bytes */
207 padstr((char *)(p
+ 23), QEMU_VERSION
, 8); /* firmware version */
208 padstr((char *)(p
+ 27), "QEMU DVD-ROM", 40); /* model */
209 put_le16(p
+ 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
211 put_le16(p
+ 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
212 put_le16(p
+ 53, 7); /* words 64-70, 54-58, 88 valid */
213 put_le16(p
+ 62, 7); /* single word dma0-2 supported */
214 put_le16(p
+ 63, 7); /* mdma0-2 supported */
215 put_le16(p
+ 64, 0x3f); /* PIO modes supported */
217 put_le16(p
+ 49, 1 << 9); /* LBA supported, no DMA */
218 put_le16(p
+ 53, 3); /* words 64-70, 54-58 valid */
219 put_le16(p
+ 63, 0x103); /* DMA modes XXX: may be incorrect */
220 put_le16(p
+ 64, 1); /* PIO modes */
222 put_le16(p
+ 65, 0xb4); /* minimum DMA multiword tx cycle time */
223 put_le16(p
+ 66, 0xb4); /* recommended DMA multiword tx cycle time */
224 put_le16(p
+ 67, 0x12c); /* minimum PIO cycle time without flow control */
225 put_le16(p
+ 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
227 put_le16(p
+ 71, 30); /* in ns */
228 put_le16(p
+ 72, 30); /* in ns */
230 put_le16(p
+ 80, 0x1e); /* support up to ATA/ATAPI-4 */
232 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
234 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
238 static void ide_cfata_identify(IDEState
*s
)
243 p
= (uint16_t *) s
->identify_data
;
247 memset(p
, 0, sizeof(s
->identify_data
));
249 cur_sec
= s
->cylinders
* s
->heads
* s
->sectors
;
251 put_le16(p
+ 0, 0x848a); /* CF Storage Card signature */
252 put_le16(p
+ 1, s
->cylinders
); /* Default cylinders */
253 put_le16(p
+ 3, s
->heads
); /* Default heads */
254 put_le16(p
+ 6, s
->sectors
); /* Default sectors per track */
255 put_le16(p
+ 7, s
->nb_sectors
>> 16); /* Sectors per card */
256 put_le16(p
+ 8, s
->nb_sectors
); /* Sectors per card */
257 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
258 put_le16(p
+ 22, 0x0004); /* ECC bytes */
259 padstr((char *) (p
+ 23), QEMU_VERSION
, 8); /* Firmware Revision */
260 padstr((char *) (p
+ 27), "QEMU MICRODRIVE", 40);/* Model number */
261 #if MAX_MULT_SECTORS > 1
262 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
264 put_le16(p
+ 47, 0x0000);
266 put_le16(p
+ 49, 0x0f00); /* Capabilities */
267 put_le16(p
+ 51, 0x0002); /* PIO cycle timing mode */
268 put_le16(p
+ 52, 0x0001); /* DMA cycle timing mode */
269 put_le16(p
+ 53, 0x0003); /* Translation params valid */
270 put_le16(p
+ 54, s
->cylinders
); /* Current cylinders */
271 put_le16(p
+ 55, s
->heads
); /* Current heads */
272 put_le16(p
+ 56, s
->sectors
); /* Current sectors */
273 put_le16(p
+ 57, cur_sec
); /* Current capacity */
274 put_le16(p
+ 58, cur_sec
>> 16); /* Current capacity */
275 if (s
->mult_sectors
) /* Multiple sector setting */
276 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
277 put_le16(p
+ 60, s
->nb_sectors
); /* Total LBA sectors */
278 put_le16(p
+ 61, s
->nb_sectors
>> 16); /* Total LBA sectors */
279 put_le16(p
+ 63, 0x0203); /* Multiword DMA capability */
280 put_le16(p
+ 64, 0x0001); /* Flow Control PIO support */
281 put_le16(p
+ 65, 0x0096); /* Min. Multiword DMA cycle */
282 put_le16(p
+ 66, 0x0096); /* Rec. Multiword DMA cycle */
283 put_le16(p
+ 68, 0x00b4); /* Min. PIO cycle time */
284 put_le16(p
+ 82, 0x400c); /* Command Set supported */
285 put_le16(p
+ 83, 0x7068); /* Command Set supported */
286 put_le16(p
+ 84, 0x4000); /* Features supported */
287 put_le16(p
+ 85, 0x000c); /* Command Set enabled */
288 put_le16(p
+ 86, 0x7044); /* Command Set enabled */
289 put_le16(p
+ 87, 0x4000); /* Features enabled */
290 put_le16(p
+ 91, 0x4060); /* Current APM level */
291 put_le16(p
+ 129, 0x0002); /* Current features option */
292 put_le16(p
+ 130, 0x0005); /* Reassigned sectors */
293 put_le16(p
+ 131, 0x0001); /* Initial power mode */
294 put_le16(p
+ 132, 0x0000); /* User signature */
295 put_le16(p
+ 160, 0x8100); /* Power requirement */
296 put_le16(p
+ 161, 0x8001); /* CF command set */
301 memcpy(s
->io_buffer
, p
, sizeof(s
->identify_data
));
304 static void ide_set_signature(IDEState
*s
)
306 s
->select
&= 0xf0; /* clear head */
322 static inline void ide_abort_command(IDEState
*s
)
324 s
->status
= READY_STAT
| ERR_STAT
;
328 static inline void ide_dma_submit_check(IDEState
*s
,
329 BlockDriverCompletionFunc
*dma_cb
, BMDMAState
*bm
)
336 /* prepare data transfer and tell what to do after */
337 static void ide_transfer_start(IDEState
*s
, uint8_t *buf
, int size
,
338 EndTransferFunc
*end_transfer_func
)
340 s
->end_transfer_func
= end_transfer_func
;
342 s
->data_end
= buf
+ size
;
343 if (!(s
->status
& ERR_STAT
))
344 s
->status
|= DRQ_STAT
;
347 static void ide_transfer_stop(IDEState
*s
)
349 s
->end_transfer_func
= ide_transfer_stop
;
350 s
->data_ptr
= s
->io_buffer
;
351 s
->data_end
= s
->io_buffer
;
352 s
->status
&= ~DRQ_STAT
;
355 int64_t ide_get_sector(IDEState
*s
)
358 if (s
->select
& 0x40) {
361 sector_num
= ((s
->select
& 0x0f) << 24) | (s
->hcyl
<< 16) |
362 (s
->lcyl
<< 8) | s
->sector
;
364 sector_num
= ((int64_t)s
->hob_hcyl
<< 40) |
365 ((int64_t) s
->hob_lcyl
<< 32) |
366 ((int64_t) s
->hob_sector
<< 24) |
367 ((int64_t) s
->hcyl
<< 16) |
368 ((int64_t) s
->lcyl
<< 8) | s
->sector
;
371 sector_num
= ((s
->hcyl
<< 8) | s
->lcyl
) * s
->heads
* s
->sectors
+
372 (s
->select
& 0x0f) * s
->sectors
+ (s
->sector
- 1);
377 void ide_set_sector(IDEState
*s
, int64_t sector_num
)
380 if (s
->select
& 0x40) {
382 s
->select
= (s
->select
& 0xf0) | (sector_num
>> 24);
383 s
->hcyl
= (sector_num
>> 16);
384 s
->lcyl
= (sector_num
>> 8);
385 s
->sector
= (sector_num
);
387 s
->sector
= sector_num
;
388 s
->lcyl
= sector_num
>> 8;
389 s
->hcyl
= sector_num
>> 16;
390 s
->hob_sector
= sector_num
>> 24;
391 s
->hob_lcyl
= sector_num
>> 32;
392 s
->hob_hcyl
= sector_num
>> 40;
395 cyl
= sector_num
/ (s
->heads
* s
->sectors
);
396 r
= sector_num
% (s
->heads
* s
->sectors
);
399 s
->select
= (s
->select
& 0xf0) | ((r
/ s
->sectors
) & 0x0f);
400 s
->sector
= (r
% s
->sectors
) + 1;
404 static void ide_rw_error(IDEState
*s
) {
405 ide_abort_command(s
);
409 static void ide_sector_read(IDEState
*s
)
414 s
->status
= READY_STAT
| SEEK_STAT
;
415 s
->error
= 0; /* not needed by IDE spec, but needed by Windows */
416 sector_num
= ide_get_sector(s
);
419 /* no more sector to read from disk */
420 ide_transfer_stop(s
);
422 #if defined(DEBUG_IDE)
423 printf("read sector=%" PRId64
"\n", sector_num
);
425 if (n
> s
->req_nb_sectors
)
426 n
= s
->req_nb_sectors
;
427 ret
= bdrv_read(s
->bs
, sector_num
, s
->io_buffer
, n
);
432 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_read
);
434 ide_set_sector(s
, sector_num
+ n
);
440 /* return 0 if buffer completed */
441 static int dma_buf_prepare(BMDMAState
*bm
, int is_write
)
443 IDEState
*s
= bmdma_active_if(bm
);
450 qemu_sglist_init(&s
->sg
, s
->nsector
/ (TARGET_PAGE_SIZE
/512) + 1);
451 s
->io_buffer_size
= 0;
453 if (bm
->cur_prd_len
== 0) {
454 /* end of table (with a fail safe of one page) */
455 if (bm
->cur_prd_last
||
456 (bm
->cur_addr
- bm
->addr
) >= 4096)
457 return s
->io_buffer_size
!= 0;
458 cpu_physical_memory_read(bm
->cur_addr
, (uint8_t *)&prd
, 8);
460 prd
.addr
= le32_to_cpu(prd
.addr
);
461 prd
.size
= le32_to_cpu(prd
.size
);
462 len
= prd
.size
& 0xfffe;
465 bm
->cur_prd_len
= len
;
466 bm
->cur_prd_addr
= prd
.addr
;
467 bm
->cur_prd_last
= (prd
.size
& 0x80000000);
471 qemu_sglist_add(&s
->sg
, bm
->cur_prd_addr
, l
);
472 bm
->cur_prd_addr
+= l
;
473 bm
->cur_prd_len
-= l
;
474 s
->io_buffer_size
+= l
;
480 static void dma_buf_commit(IDEState
*s
, int is_write
)
482 qemu_sglist_destroy(&s
->sg
);
485 void ide_dma_error(IDEState
*s
)
487 ide_transfer_stop(s
);
489 s
->status
= READY_STAT
| ERR_STAT
;
493 static int ide_handle_write_error(IDEState
*s
, int error
, int op
)
495 BlockInterfaceErrorAction action
= drive_get_onerror(s
->bs
);
497 if (action
== BLOCK_ERR_IGNORE
)
500 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
501 || action
== BLOCK_ERR_STOP_ANY
) {
502 s
->bus
->bmdma
->unit
= s
->unit
;
503 s
->bus
->bmdma
->status
|= op
;
506 if (op
== BM_STATUS_DMA_RETRY
) {
507 dma_buf_commit(s
, 0);
517 /* return 0 if buffer completed */
518 static int dma_buf_rw(BMDMAState
*bm
, int is_write
)
520 IDEState
*s
= bmdma_active_if(bm
);
528 l
= s
->io_buffer_size
- s
->io_buffer_index
;
531 if (bm
->cur_prd_len
== 0) {
532 /* end of table (with a fail safe of one page) */
533 if (bm
->cur_prd_last
||
534 (bm
->cur_addr
- bm
->addr
) >= 4096)
536 cpu_physical_memory_read(bm
->cur_addr
, (uint8_t *)&prd
, 8);
538 prd
.addr
= le32_to_cpu(prd
.addr
);
539 prd
.size
= le32_to_cpu(prd
.size
);
540 len
= prd
.size
& 0xfffe;
543 bm
->cur_prd_len
= len
;
544 bm
->cur_prd_addr
= prd
.addr
;
545 bm
->cur_prd_last
= (prd
.size
& 0x80000000);
547 if (l
> bm
->cur_prd_len
)
551 cpu_physical_memory_write(bm
->cur_prd_addr
,
552 s
->io_buffer
+ s
->io_buffer_index
, l
);
554 cpu_physical_memory_read(bm
->cur_prd_addr
,
555 s
->io_buffer
+ s
->io_buffer_index
, l
);
557 bm
->cur_prd_addr
+= l
;
558 bm
->cur_prd_len
-= l
;
559 s
->io_buffer_index
+= l
;
565 static void ide_read_dma_cb(void *opaque
, int ret
)
567 BMDMAState
*bm
= opaque
;
568 IDEState
*s
= bmdma_active_if(bm
);
573 dma_buf_commit(s
, 1);
578 n
= s
->io_buffer_size
>> 9;
579 sector_num
= ide_get_sector(s
);
581 dma_buf_commit(s
, 1);
583 ide_set_sector(s
, sector_num
);
587 /* end of transfer ? */
588 if (s
->nsector
== 0) {
589 s
->status
= READY_STAT
| SEEK_STAT
;
592 bm
->status
&= ~BM_STATUS_DMAING
;
593 bm
->status
|= BM_STATUS_INT
;
600 /* launch next transfer */
602 s
->io_buffer_index
= 0;
603 s
->io_buffer_size
= n
* 512;
604 if (dma_buf_prepare(bm
, 1) == 0)
607 printf("aio_read: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
609 bm
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
, ide_read_dma_cb
, bm
);
610 ide_dma_submit_check(s
, ide_read_dma_cb
, bm
);
613 static void ide_sector_read_dma(IDEState
*s
)
615 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
616 s
->io_buffer_index
= 0;
617 s
->io_buffer_size
= 0;
619 ide_dma_start(s
, ide_read_dma_cb
);
622 static void ide_sector_write_timer_cb(void *opaque
)
624 IDEState
*s
= opaque
;
628 static void ide_sector_write(IDEState
*s
)
633 s
->status
= READY_STAT
| SEEK_STAT
;
634 sector_num
= ide_get_sector(s
);
635 #if defined(DEBUG_IDE)
636 printf("write sector=%" PRId64
"\n", sector_num
);
639 if (n
> s
->req_nb_sectors
)
640 n
= s
->req_nb_sectors
;
641 ret
= bdrv_write(s
->bs
, sector_num
, s
->io_buffer
, n
);
644 if (ide_handle_write_error(s
, -ret
, BM_STATUS_PIO_RETRY
))
649 if (s
->nsector
== 0) {
650 /* no more sectors to write */
651 ide_transfer_stop(s
);
654 if (n1
> s
->req_nb_sectors
)
655 n1
= s
->req_nb_sectors
;
656 ide_transfer_start(s
, s
->io_buffer
, 512 * n1
, ide_sector_write
);
658 ide_set_sector(s
, sector_num
+ n
);
661 if (win2k_install_hack
&& ((++s
->irq_count
% 16) == 0)) {
662 /* It seems there is a bug in the Windows 2000 installer HDD
663 IDE driver which fills the disk with empty logs when the
664 IDE write IRQ comes too early. This hack tries to correct
665 that at the expense of slower write performances. Use this
666 option _only_ to install Windows 2000. You must disable it
668 qemu_mod_timer(s
->sector_write_timer
,
669 qemu_get_clock(vm_clock
) + (ticks_per_sec
/ 1000));
677 static void ide_dma_restart_bh(void *opaque
)
679 BMDMAState
*bm
= opaque
;
681 qemu_bh_delete(bm
->bh
);
684 if (bm
->status
& BM_STATUS_DMA_RETRY
) {
685 bm
->status
&= ~BM_STATUS_DMA_RETRY
;
686 ide_dma_restart(bmdma_active_if(bm
));
687 } else if (bm
->status
& BM_STATUS_PIO_RETRY
) {
688 bm
->status
&= ~BM_STATUS_PIO_RETRY
;
689 ide_sector_write(bmdma_active_if(bm
));
693 void ide_dma_restart_cb(void *opaque
, int running
, int reason
)
695 BMDMAState
*bm
= opaque
;
701 bm
->bh
= qemu_bh_new(ide_dma_restart_bh
, bm
);
702 qemu_bh_schedule(bm
->bh
);
706 static void ide_write_dma_cb(void *opaque
, int ret
)
708 BMDMAState
*bm
= opaque
;
709 IDEState
*s
= bmdma_active_if(bm
);
714 if (ide_handle_write_error(s
, -ret
, BM_STATUS_DMA_RETRY
))
718 n
= s
->io_buffer_size
>> 9;
719 sector_num
= ide_get_sector(s
);
721 dma_buf_commit(s
, 0);
723 ide_set_sector(s
, sector_num
);
727 /* end of transfer ? */
728 if (s
->nsector
== 0) {
729 s
->status
= READY_STAT
| SEEK_STAT
;
732 bm
->status
&= ~BM_STATUS_DMAING
;
733 bm
->status
|= BM_STATUS_INT
;
741 s
->io_buffer_size
= n
* 512;
742 /* launch next transfer */
743 if (dma_buf_prepare(bm
, 0) == 0)
746 printf("aio_write: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
748 bm
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
, ide_write_dma_cb
, bm
);
749 ide_dma_submit_check(s
, ide_write_dma_cb
, bm
);
752 static void ide_sector_write_dma(IDEState
*s
)
754 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
755 s
->io_buffer_index
= 0;
756 s
->io_buffer_size
= 0;
758 ide_dma_start(s
, ide_write_dma_cb
);
761 void ide_atapi_cmd_ok(IDEState
*s
)
764 s
->status
= READY_STAT
| SEEK_STAT
;
765 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
769 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
771 #ifdef DEBUG_IDE_ATAPI
772 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
774 s
->error
= sense_key
<< 4;
775 s
->status
= READY_STAT
| ERR_STAT
;
776 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
777 s
->sense_key
= sense_key
;
782 static void ide_atapi_cmd_check_status(IDEState
*s
)
784 #ifdef DEBUG_IDE_ATAPI
785 printf("atapi_cmd_check_status\n");
787 s
->error
= MC_ERR
| (SENSE_UNIT_ATTENTION
<< 4);
788 s
->status
= ERR_STAT
;
793 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
799 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
807 static inline int ube16_to_cpu(const uint8_t *buf
)
809 return (buf
[0] << 8) | buf
[1];
812 static inline int ube32_to_cpu(const uint8_t *buf
)
814 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
817 static void lba_to_msf(uint8_t *buf
, int lba
)
820 buf
[0] = (lba
/ 75) / 60;
821 buf
[1] = (lba
/ 75) % 60;
825 static void cd_data_to_raw(uint8_t *buf
, int lba
)
829 memset(buf
+ 1, 0xff, 10);
833 lba_to_msf(buf
, lba
);
834 buf
[3] = 0x01; /* mode 1 data */
838 /* XXX: ECC not computed */
842 static int cd_read_sector(BlockDriverState
*bs
, int lba
, uint8_t *buf
,
847 switch(sector_size
) {
849 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
, 4);
852 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
+ 16, 4);
855 cd_data_to_raw(buf
, lba
);
864 void ide_atapi_io_error(IDEState
*s
, int ret
)
866 /* XXX: handle more errors */
867 if (ret
== -ENOMEDIUM
) {
868 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
869 ASC_MEDIUM_NOT_PRESENT
);
871 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
872 ASC_LOGICAL_BLOCK_OOR
);
876 /* The whole ATAPI transfer logic is handled in this function */
877 static void ide_atapi_cmd_reply_end(IDEState
*s
)
879 int byte_count_limit
, size
, ret
;
880 #ifdef DEBUG_IDE_ATAPI
881 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
882 s
->packet_transfer_size
,
883 s
->elementary_transfer_size
,
886 if (s
->packet_transfer_size
<= 0) {
887 /* end of transfer */
888 ide_transfer_stop(s
);
889 s
->status
= READY_STAT
| SEEK_STAT
;
890 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
892 #ifdef DEBUG_IDE_ATAPI
893 printf("status=0x%x\n", s
->status
);
896 /* see if a new sector must be read */
897 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
898 ret
= cd_read_sector(s
->bs
, s
->lba
, s
->io_buffer
, s
->cd_sector_size
);
900 ide_transfer_stop(s
);
901 ide_atapi_io_error(s
, ret
);
905 s
->io_buffer_index
= 0;
907 if (s
->elementary_transfer_size
> 0) {
908 /* there are some data left to transmit in this elementary
910 size
= s
->cd_sector_size
- s
->io_buffer_index
;
911 if (size
> s
->elementary_transfer_size
)
912 size
= s
->elementary_transfer_size
;
913 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
,
914 size
, ide_atapi_cmd_reply_end
);
915 s
->packet_transfer_size
-= size
;
916 s
->elementary_transfer_size
-= size
;
917 s
->io_buffer_index
+= size
;
919 /* a new transfer is needed */
920 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
921 byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
922 #ifdef DEBUG_IDE_ATAPI
923 printf("byte_count_limit=%d\n", byte_count_limit
);
925 if (byte_count_limit
== 0xffff)
927 size
= s
->packet_transfer_size
;
928 if (size
> byte_count_limit
) {
929 /* byte count limit must be even if this case */
930 if (byte_count_limit
& 1)
932 size
= byte_count_limit
;
936 s
->elementary_transfer_size
= size
;
937 /* we cannot transmit more than one sector at a time */
939 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
940 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
942 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
,
943 size
, ide_atapi_cmd_reply_end
);
944 s
->packet_transfer_size
-= size
;
945 s
->elementary_transfer_size
-= size
;
946 s
->io_buffer_index
+= size
;
948 #ifdef DEBUG_IDE_ATAPI
949 printf("status=0x%x\n", s
->status
);
955 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
956 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
960 s
->lba
= -1; /* no sector read */
961 s
->packet_transfer_size
= size
;
962 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
963 s
->elementary_transfer_size
= 0;
964 s
->io_buffer_index
= 0;
967 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
968 ide_dma_start(s
, ide_atapi_cmd_read_dma_cb
);
970 s
->status
= READY_STAT
| SEEK_STAT
;
971 ide_atapi_cmd_reply_end(s
);
975 /* start a CD-CDROM read command */
976 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
980 s
->packet_transfer_size
= nb_sectors
* sector_size
;
981 s
->elementary_transfer_size
= 0;
982 s
->io_buffer_index
= sector_size
;
983 s
->cd_sector_size
= sector_size
;
985 s
->status
= READY_STAT
| SEEK_STAT
;
986 ide_atapi_cmd_reply_end(s
);
989 /* ATAPI DMA support */
991 /* XXX: handle read errors */
992 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
994 BMDMAState
*bm
= opaque
;
995 IDEState
*s
= bmdma_active_if(bm
);
999 ide_atapi_io_error(s
, ret
);
1003 if (s
->io_buffer_size
> 0) {
1005 * For a cdrom read sector command (s->lba != -1),
1006 * adjust the lba for the next s->io_buffer_size chunk
1007 * and dma the current chunk.
1008 * For a command != read (s->lba == -1), just transfer
1012 if (s
->cd_sector_size
== 2352) {
1014 cd_data_to_raw(s
->io_buffer
, s
->lba
);
1016 n
= s
->io_buffer_size
>> 11;
1020 s
->packet_transfer_size
-= s
->io_buffer_size
;
1021 if (dma_buf_rw(bm
, 1) == 0)
1025 if (s
->packet_transfer_size
<= 0) {
1026 s
->status
= READY_STAT
| SEEK_STAT
;
1027 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
1030 bm
->status
&= ~BM_STATUS_DMAING
;
1031 bm
->status
|= BM_STATUS_INT
;
1038 s
->io_buffer_index
= 0;
1039 if (s
->cd_sector_size
== 2352) {
1041 s
->io_buffer_size
= s
->cd_sector_size
;
1044 n
= s
->packet_transfer_size
>> 11;
1045 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
1046 n
= (IDE_DMA_BUF_SECTORS
/ 4);
1047 s
->io_buffer_size
= n
* 2048;
1051 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
1053 bm
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
1054 bm
->iov
.iov_len
= n
* 4 * 512;
1055 qemu_iovec_init_external(&bm
->qiov
, &bm
->iov
, 1);
1056 bm
->aiocb
= bdrv_aio_readv(s
->bs
, (int64_t)s
->lba
<< 2, &bm
->qiov
,
1057 n
* 4, ide_atapi_cmd_read_dma_cb
, bm
);
1059 /* Note: media not present is the most likely case */
1060 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1061 ASC_MEDIUM_NOT_PRESENT
);
1066 /* start a CD-CDROM read command with DMA */
1067 /* XXX: test if DMA is available */
1068 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
1072 s
->packet_transfer_size
= nb_sectors
* sector_size
;
1073 s
->io_buffer_index
= 0;
1074 s
->io_buffer_size
= 0;
1075 s
->cd_sector_size
= sector_size
;
1077 /* XXX: check if BUSY_STAT should be set */
1078 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
1079 ide_dma_start(s
, ide_atapi_cmd_read_dma_cb
);
1082 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
1085 #ifdef DEBUG_IDE_ATAPI
1086 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
1090 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
1092 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
1096 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
1099 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
1101 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
1102 cpu_to_ube16 (buf_profile
, profile
);
1103 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
1105 /* each profile adds 4 bytes to the response */
1107 buf
[11] += 4; /* Additional Length */
1112 static int ide_dvd_read_structure(IDEState
*s
, int format
,
1113 const uint8_t *packet
, uint8_t *buf
)
1116 case 0x0: /* Physical format information */
1118 int layer
= packet
[6];
1119 uint64_t total_sectors
;
1122 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1124 bdrv_get_geometry(s
->bs
, &total_sectors
);
1125 total_sectors
>>= 2;
1126 if (total_sectors
== 0)
1127 return -ASC_MEDIUM_NOT_PRESENT
;
1129 buf
[4] = 1; /* DVD-ROM, part version 1 */
1130 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1131 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1132 buf
[7] = 0; /* default densities */
1134 /* FIXME: 0x30000 per spec? */
1135 cpu_to_ube32(buf
+ 8, 0); /* start sector */
1136 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
1137 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
1139 /* Size of buffer, not including 2 byte size field */
1140 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1142 /* 2k data + 4 byte header */
1146 case 0x01: /* DVD copyright information */
1147 buf
[4] = 0; /* no copyright data */
1148 buf
[5] = 0; /* no region restrictions */
1150 /* Size of buffer, not including 2 byte size field */
1151 cpu_to_be16wu((uint16_t *)buf
, 4 + 2);
1153 /* 4 byte header + 4 byte data */
1156 case 0x03: /* BCA information - invalid field for no BCA info */
1157 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1159 case 0x04: /* DVD disc manufacturing information */
1160 /* Size of buffer, not including 2 byte size field */
1161 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1163 /* 2k data + 4 byte header */
1168 * This lists all the command capabilities above. Add new ones
1169 * in order and update the length and buffer return values.
1172 buf
[4] = 0x00; /* Physical format */
1173 buf
[5] = 0x40; /* Not writable, is readable */
1174 cpu_to_be16wu((uint16_t *)(buf
+ 6), 2048 + 4);
1176 buf
[8] = 0x01; /* Copyright info */
1177 buf
[9] = 0x40; /* Not writable, is readable */
1178 cpu_to_be16wu((uint16_t *)(buf
+ 10), 4 + 4);
1180 buf
[12] = 0x03; /* BCA info */
1181 buf
[13] = 0x40; /* Not writable, is readable */
1182 cpu_to_be16wu((uint16_t *)(buf
+ 14), 188 + 4);
1184 buf
[16] = 0x04; /* Manufacturing info */
1185 buf
[17] = 0x40; /* Not writable, is readable */
1186 cpu_to_be16wu((uint16_t *)(buf
+ 18), 2048 + 4);
1188 /* Size of buffer, not including 2 byte size field */
1189 cpu_to_be16wu((uint16_t *)buf
, 16 + 2);
1191 /* data written + 4 byte header */
1194 default: /* TODO: formats beyond DVD-ROM requires */
1195 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1199 static void ide_atapi_cmd(IDEState
*s
)
1201 const uint8_t *packet
;
1205 packet
= s
->io_buffer
;
1207 #ifdef DEBUG_IDE_ATAPI
1210 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1211 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1212 printf(" %02x", packet
[i
]);
1217 /* If there's a UNIT_ATTENTION condition pending, only
1218 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1219 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
1220 s
->io_buffer
[0] != GPCMD_REQUEST_SENSE
&&
1221 s
->io_buffer
[0] != GPCMD_INQUIRY
) {
1222 ide_atapi_cmd_check_status(s
);
1225 switch(s
->io_buffer
[0]) {
1226 case GPCMD_TEST_UNIT_READY
:
1227 if (bdrv_is_inserted(s
->bs
) && !s
->cdrom_changed
) {
1228 ide_atapi_cmd_ok(s
);
1230 s
->cdrom_changed
= 0;
1231 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1232 ASC_MEDIUM_NOT_PRESENT
);
1235 case GPCMD_MODE_SENSE_6
:
1236 case GPCMD_MODE_SENSE_10
:
1239 if (packet
[0] == GPCMD_MODE_SENSE_10
)
1240 max_len
= ube16_to_cpu(packet
+ 7);
1242 max_len
= packet
[4];
1243 action
= packet
[2] >> 6;
1244 code
= packet
[2] & 0x3f;
1246 case 0: /* current values */
1248 case 0x01: /* error recovery */
1249 cpu_to_ube16(&buf
[0], 16 + 6);
1265 ide_atapi_cmd_reply(s
, 16, max_len
);
1268 cpu_to_ube16(&buf
[0], 28 + 6);
1281 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1282 code checks for this to automount media. */
1285 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
1286 if (bdrv_is_locked(s
->bs
))
1289 cpu_to_ube16(&buf
[16], 706);
1292 cpu_to_ube16(&buf
[20], 512);
1293 cpu_to_ube16(&buf
[22], 706);
1298 ide_atapi_cmd_reply(s
, 28, max_len
);
1304 case 1: /* changeable values */
1306 case 2: /* default values */
1309 case 3: /* saved values */
1310 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1311 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
1316 case GPCMD_REQUEST_SENSE
:
1317 max_len
= packet
[4];
1319 buf
[0] = 0x70 | (1 << 7);
1320 buf
[2] = s
->sense_key
;
1323 if (s
->sense_key
== SENSE_UNIT_ATTENTION
)
1324 s
->sense_key
= SENSE_NONE
;
1325 ide_atapi_cmd_reply(s
, 18, max_len
);
1327 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
:
1328 if (bdrv_is_inserted(s
->bs
)) {
1329 bdrv_set_locked(s
->bs
, packet
[4] & 1);
1330 ide_atapi_cmd_ok(s
);
1332 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1333 ASC_MEDIUM_NOT_PRESENT
);
1339 int nb_sectors
, lba
;
1341 if (packet
[0] == GPCMD_READ_10
)
1342 nb_sectors
= ube16_to_cpu(packet
+ 7);
1344 nb_sectors
= ube32_to_cpu(packet
+ 6);
1345 lba
= ube32_to_cpu(packet
+ 2);
1346 if (nb_sectors
== 0) {
1347 ide_atapi_cmd_ok(s
);
1350 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1355 int nb_sectors
, lba
, transfer_request
;
1357 nb_sectors
= (packet
[6] << 16) | (packet
[7] << 8) | packet
[8];
1358 lba
= ube32_to_cpu(packet
+ 2);
1359 if (nb_sectors
== 0) {
1360 ide_atapi_cmd_ok(s
);
1363 transfer_request
= packet
[9];
1364 switch(transfer_request
& 0xf8) {
1367 ide_atapi_cmd_ok(s
);
1371 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1375 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1378 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1379 ASC_INV_FIELD_IN_CMD_PACKET
);
1387 uint64_t total_sectors
;
1389 bdrv_get_geometry(s
->bs
, &total_sectors
);
1390 total_sectors
>>= 2;
1391 if (total_sectors
== 0) {
1392 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1393 ASC_MEDIUM_NOT_PRESENT
);
1396 lba
= ube32_to_cpu(packet
+ 2);
1397 if (lba
>= total_sectors
) {
1398 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1399 ASC_LOGICAL_BLOCK_OOR
);
1402 ide_atapi_cmd_ok(s
);
1405 case GPCMD_START_STOP_UNIT
:
1407 int start
, eject
, err
= 0;
1408 start
= packet
[4] & 1;
1409 eject
= (packet
[4] >> 1) & 1;
1412 err
= bdrv_eject(s
->bs
, !start
);
1417 ide_atapi_cmd_ok(s
);
1420 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1421 ASC_MEDIA_REMOVAL_PREVENTED
);
1424 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1425 ASC_MEDIUM_NOT_PRESENT
);
1430 case GPCMD_MECHANISM_STATUS
:
1432 max_len
= ube16_to_cpu(packet
+ 8);
1433 cpu_to_ube16(buf
, 0);
1434 /* no current LBA */
1439 cpu_to_ube16(buf
+ 6, 0);
1440 ide_atapi_cmd_reply(s
, 8, max_len
);
1443 case GPCMD_READ_TOC_PMA_ATIP
:
1445 int format
, msf
, start_track
, len
;
1446 uint64_t total_sectors
;
1448 bdrv_get_geometry(s
->bs
, &total_sectors
);
1449 total_sectors
>>= 2;
1450 if (total_sectors
== 0) {
1451 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1452 ASC_MEDIUM_NOT_PRESENT
);
1455 max_len
= ube16_to_cpu(packet
+ 7);
1456 format
= packet
[9] >> 6;
1457 msf
= (packet
[1] >> 1) & 1;
1458 start_track
= packet
[6];
1461 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1464 ide_atapi_cmd_reply(s
, len
, max_len
);
1467 /* multi session : only a single session defined */
1472 ide_atapi_cmd_reply(s
, 12, max_len
);
1475 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1478 ide_atapi_cmd_reply(s
, len
, max_len
);
1482 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1483 ASC_INV_FIELD_IN_CMD_PACKET
);
1488 case GPCMD_READ_CDVD_CAPACITY
:
1490 uint64_t total_sectors
;
1492 bdrv_get_geometry(s
->bs
, &total_sectors
);
1493 total_sectors
>>= 2;
1494 if (total_sectors
== 0) {
1495 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1496 ASC_MEDIUM_NOT_PRESENT
);
1499 /* NOTE: it is really the number of sectors minus 1 */
1500 cpu_to_ube32(buf
, total_sectors
- 1);
1501 cpu_to_ube32(buf
+ 4, 2048);
1502 ide_atapi_cmd_reply(s
, 8, 8);
1505 case GPCMD_READ_DVD_STRUCTURE
:
1507 int media
= packet
[1];
1508 int format
= packet
[7];
1511 max_len
= ube16_to_cpu(packet
+ 8);
1513 if (format
< 0xff) {
1514 if (media_is_cd(s
)) {
1515 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1516 ASC_INCOMPATIBLE_FORMAT
);
1518 } else if (!media_present(s
)) {
1519 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1520 ASC_INV_FIELD_IN_CMD_PACKET
);
1525 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1526 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1532 ret
= ide_dvd_read_structure(s
, format
, packet
, buf
);
1535 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
, -ret
);
1537 ide_atapi_cmd_reply(s
, ret
, max_len
);
1541 /* TODO: BD support, fall through for now */
1543 /* Generic disk structures */
1544 case 0x80: /* TODO: AACS volume identifier */
1545 case 0x81: /* TODO: AACS media serial number */
1546 case 0x82: /* TODO: AACS media identifier */
1547 case 0x83: /* TODO: AACS media key block */
1548 case 0x90: /* TODO: List of recognized format layers */
1549 case 0xc0: /* TODO: Write protection status */
1551 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1552 ASC_INV_FIELD_IN_CMD_PACKET
);
1557 case GPCMD_SET_SPEED
:
1558 ide_atapi_cmd_ok(s
);
1561 max_len
= packet
[4];
1562 buf
[0] = 0x05; /* CD-ROM */
1563 buf
[1] = 0x80; /* removable */
1564 buf
[2] = 0x00; /* ISO */
1565 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1566 buf
[4] = 31; /* additional length */
1567 buf
[5] = 0; /* reserved */
1568 buf
[6] = 0; /* reserved */
1569 buf
[7] = 0; /* reserved */
1570 padstr8(buf
+ 8, 8, "QEMU");
1571 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
1572 padstr8(buf
+ 32, 4, QEMU_VERSION
);
1573 ide_atapi_cmd_reply(s
, 36, max_len
);
1575 case GPCMD_GET_CONFIGURATION
:
1580 /* only feature 0 is supported */
1581 if (packet
[2] != 0 || packet
[3] != 0) {
1582 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1583 ASC_INV_FIELD_IN_CMD_PACKET
);
1587 /* XXX: could result in alignment problems in some architectures */
1588 max_len
= ube16_to_cpu(packet
+ 7);
1591 * XXX: avoid overflow for io_buffer if max_len is bigger than
1592 * the size of that buffer (dimensioned to max number of
1593 * sectors to transfer at once)
1595 * Only a problem if the feature/profiles grow.
1597 if (max_len
> 512) /* XXX: assume 1 sector */
1600 memset(buf
, 0, max_len
);
1602 * the number of sectors from the media tells us which profile
1603 * to use as current. 0 means there is no media
1605 if (media_is_dvd(s
))
1606 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
1607 else if (media_is_cd(s
))
1608 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
1610 buf
[10] = 0x02 | 0x01; /* persistent and current */
1611 len
= 12; /* headers: 8 + 4 */
1612 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
1613 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
1614 cpu_to_ube32(buf
, len
- 4); /* data length */
1616 ide_atapi_cmd_reply(s
, len
, max_len
);
1620 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1621 ASC_ILLEGAL_OPCODE
);
1626 static void ide_cfata_metadata_inquiry(IDEState
*s
)
1631 p
= (uint16_t *) s
->io_buffer
;
1632 memset(p
, 0, 0x200);
1633 spd
= ((s
->mdata_size
- 1) >> 9) + 1;
1635 put_le16(p
+ 0, 0x0001); /* Data format revision */
1636 put_le16(p
+ 1, 0x0000); /* Media property: silicon */
1637 put_le16(p
+ 2, s
->media_changed
); /* Media status */
1638 put_le16(p
+ 3, s
->mdata_size
& 0xffff); /* Capacity in bytes (low) */
1639 put_le16(p
+ 4, s
->mdata_size
>> 16); /* Capacity in bytes (high) */
1640 put_le16(p
+ 5, spd
& 0xffff); /* Sectors per device (low) */
1641 put_le16(p
+ 6, spd
>> 16); /* Sectors per device (high) */
1644 static void ide_cfata_metadata_read(IDEState
*s
)
1648 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1649 s
->status
= ERR_STAT
;
1650 s
->error
= ABRT_ERR
;
1654 p
= (uint16_t *) s
->io_buffer
;
1655 memset(p
, 0, 0x200);
1657 put_le16(p
+ 0, s
->media_changed
); /* Media status */
1658 memcpy(p
+ 1, s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1659 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1660 s
->nsector
<< 9), 0x200 - 2));
1663 static void ide_cfata_metadata_write(IDEState
*s
)
1665 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1666 s
->status
= ERR_STAT
;
1667 s
->error
= ABRT_ERR
;
1671 s
->media_changed
= 0;
1673 memcpy(s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1675 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1676 s
->nsector
<< 9), 0x200 - 2));
1679 /* called when the inserted state of the media has changed */
1680 static void cdrom_change_cb(void *opaque
)
1682 IDEState
*s
= opaque
;
1683 uint64_t nb_sectors
;
1685 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1686 s
->nb_sectors
= nb_sectors
;
1688 s
->sense_key
= SENSE_UNIT_ATTENTION
;
1689 s
->asc
= ASC_MEDIUM_MAY_HAVE_CHANGED
;
1690 s
->cdrom_changed
= 1;
1694 static void ide_cmd_lba48_transform(IDEState
*s
, int lba48
)
1698 /* handle the 'magic' 0 nsector count conversion here. to avoid
1699 * fiddling with the rest of the read logic, we just store the
1700 * full sector count in ->nsector and ignore ->hob_nsector from now
1706 if (!s
->nsector
&& !s
->hob_nsector
)
1709 int lo
= s
->nsector
;
1710 int hi
= s
->hob_nsector
;
1712 s
->nsector
= (hi
<< 8) | lo
;
1717 static void ide_clear_hob(IDEBus
*bus
)
1719 /* any write clears HOB high bit of device control register */
1720 bus
->ifs
[0].select
&= ~(1 << 7);
1721 bus
->ifs
[1].select
&= ~(1 << 7);
1724 void ide_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1726 IDEBus
*bus
= opaque
;
1732 printf("IDE: write addr=0x%x val=0x%02x\n", addr
, val
);
1737 /* ignore writes to command block while busy with previous command */
1738 if (addr
!= 7 && (idebus_active_if(bus
)->status
& (BUSY_STAT
|DRQ_STAT
)))
1746 /* NOTE: data is written to the two drives */
1747 bus
->ifs
[0].hob_feature
= bus
->ifs
[0].feature
;
1748 bus
->ifs
[1].hob_feature
= bus
->ifs
[1].feature
;
1749 bus
->ifs
[0].feature
= val
;
1750 bus
->ifs
[1].feature
= val
;
1754 bus
->ifs
[0].hob_nsector
= bus
->ifs
[0].nsector
;
1755 bus
->ifs
[1].hob_nsector
= bus
->ifs
[1].nsector
;
1756 bus
->ifs
[0].nsector
= val
;
1757 bus
->ifs
[1].nsector
= val
;
1761 bus
->ifs
[0].hob_sector
= bus
->ifs
[0].sector
;
1762 bus
->ifs
[1].hob_sector
= bus
->ifs
[1].sector
;
1763 bus
->ifs
[0].sector
= val
;
1764 bus
->ifs
[1].sector
= val
;
1768 bus
->ifs
[0].hob_lcyl
= bus
->ifs
[0].lcyl
;
1769 bus
->ifs
[1].hob_lcyl
= bus
->ifs
[1].lcyl
;
1770 bus
->ifs
[0].lcyl
= val
;
1771 bus
->ifs
[1].lcyl
= val
;
1775 bus
->ifs
[0].hob_hcyl
= bus
->ifs
[0].hcyl
;
1776 bus
->ifs
[1].hob_hcyl
= bus
->ifs
[1].hcyl
;
1777 bus
->ifs
[0].hcyl
= val
;
1778 bus
->ifs
[1].hcyl
= val
;
1781 /* FIXME: HOB readback uses bit 7 */
1782 bus
->ifs
[0].select
= (val
& ~0x10) | 0xa0;
1783 bus
->ifs
[1].select
= (val
| 0x10) | 0xa0;
1785 bus
->unit
= (val
>> 4) & 1;
1790 #if defined(DEBUG_IDE)
1791 printf("ide: CMD=%02x\n", val
);
1793 s
= idebus_active_if(bus
);
1794 /* ignore commands to non existant slave */
1795 if (s
!= bus
->ifs
&& !s
->bs
)
1798 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1799 if ((s
->status
& (BUSY_STAT
|DRQ_STAT
)) && val
!= WIN_DEVICE_RESET
)
1804 if (s
->bs
&& !s
->is_cdrom
) {
1808 ide_cfata_identify(s
);
1809 s
->status
= READY_STAT
| SEEK_STAT
;
1810 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1813 ide_set_signature(s
);
1815 ide_abort_command(s
);
1822 s
->status
= READY_STAT
| SEEK_STAT
;
1826 if (s
->is_cf
&& s
->nsector
== 0) {
1827 /* Disable Read and Write Multiple */
1828 s
->mult_sectors
= 0;
1829 s
->status
= READY_STAT
| SEEK_STAT
;
1830 } else if ((s
->nsector
& 0xff) != 0 &&
1831 ((s
->nsector
& 0xff) > MAX_MULT_SECTORS
||
1832 (s
->nsector
& (s
->nsector
- 1)) != 0)) {
1833 ide_abort_command(s
);
1835 s
->mult_sectors
= s
->nsector
& 0xff;
1836 s
->status
= READY_STAT
| SEEK_STAT
;
1840 case WIN_VERIFY_EXT
:
1843 case WIN_VERIFY_ONCE
:
1844 /* do sector number check ? */
1845 ide_cmd_lba48_transform(s
, lba48
);
1846 s
->status
= READY_STAT
| SEEK_STAT
;
1855 ide_cmd_lba48_transform(s
, lba48
);
1856 s
->req_nb_sectors
= 1;
1862 case WIN_WRITE_ONCE
:
1863 case CFA_WRITE_SECT_WO_ERASE
:
1864 case WIN_WRITE_VERIFY
:
1865 ide_cmd_lba48_transform(s
, lba48
);
1867 s
->status
= SEEK_STAT
| READY_STAT
;
1868 s
->req_nb_sectors
= 1;
1869 ide_transfer_start(s
, s
->io_buffer
, 512, ide_sector_write
);
1870 s
->media_changed
= 1;
1872 case WIN_MULTREAD_EXT
:
1875 if (!s
->mult_sectors
)
1877 ide_cmd_lba48_transform(s
, lba48
);
1878 s
->req_nb_sectors
= s
->mult_sectors
;
1881 case WIN_MULTWRITE_EXT
:
1884 case CFA_WRITE_MULTI_WO_ERASE
:
1885 if (!s
->mult_sectors
)
1887 ide_cmd_lba48_transform(s
, lba48
);
1889 s
->status
= SEEK_STAT
| READY_STAT
;
1890 s
->req_nb_sectors
= s
->mult_sectors
;
1892 if (n
> s
->req_nb_sectors
)
1893 n
= s
->req_nb_sectors
;
1894 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_write
);
1895 s
->media_changed
= 1;
1897 case WIN_READDMA_EXT
:
1900 case WIN_READDMA_ONCE
:
1903 ide_cmd_lba48_transform(s
, lba48
);
1904 ide_sector_read_dma(s
);
1906 case WIN_WRITEDMA_EXT
:
1909 case WIN_WRITEDMA_ONCE
:
1912 ide_cmd_lba48_transform(s
, lba48
);
1913 ide_sector_write_dma(s
);
1914 s
->media_changed
= 1;
1916 case WIN_READ_NATIVE_MAX_EXT
:
1918 case WIN_READ_NATIVE_MAX
:
1919 ide_cmd_lba48_transform(s
, lba48
);
1920 ide_set_sector(s
, s
->nb_sectors
- 1);
1921 s
->status
= READY_STAT
| SEEK_STAT
;
1924 case WIN_CHECKPOWERMODE1
:
1925 case WIN_CHECKPOWERMODE2
:
1926 s
->nsector
= 0xff; /* device active or idle */
1927 s
->status
= READY_STAT
| SEEK_STAT
;
1930 case WIN_SETFEATURES
:
1933 /* XXX: valid for CDROM ? */
1934 switch(s
->feature
) {
1935 case 0xcc: /* reverting to power-on defaults enable */
1936 case 0x66: /* reverting to power-on defaults disable */
1937 case 0x02: /* write cache enable */
1938 case 0x82: /* write cache disable */
1939 case 0xaa: /* read look-ahead enable */
1940 case 0x55: /* read look-ahead disable */
1941 case 0x05: /* set advanced power management mode */
1942 case 0x85: /* disable advanced power management mode */
1943 case 0x69: /* NOP */
1944 case 0x67: /* NOP */
1945 case 0x96: /* NOP */
1946 case 0x9a: /* NOP */
1947 case 0x42: /* enable Automatic Acoustic Mode */
1948 case 0xc2: /* disable Automatic Acoustic Mode */
1949 s
->status
= READY_STAT
| SEEK_STAT
;
1952 case 0x03: { /* set transfer mode */
1953 uint8_t val
= s
->nsector
& 0x07;
1955 switch (s
->nsector
>> 3) {
1956 case 0x00: /* pio default */
1957 case 0x01: /* pio mode */
1958 put_le16(s
->identify_data
+ 62,0x07);
1959 put_le16(s
->identify_data
+ 63,0x07);
1960 put_le16(s
->identify_data
+ 88,0x3f);
1962 case 0x02: /* sigle word dma mode*/
1963 put_le16(s
->identify_data
+ 62,0x07 | (1 << (val
+ 8)));
1964 put_le16(s
->identify_data
+ 63,0x07);
1965 put_le16(s
->identify_data
+ 88,0x3f);
1967 case 0x04: /* mdma mode */
1968 put_le16(s
->identify_data
+ 62,0x07);
1969 put_le16(s
->identify_data
+ 63,0x07 | (1 << (val
+ 8)));
1970 put_le16(s
->identify_data
+ 88,0x3f);
1972 case 0x08: /* udma mode */
1973 put_le16(s
->identify_data
+ 62,0x07);
1974 put_le16(s
->identify_data
+ 63,0x07);
1975 put_le16(s
->identify_data
+ 88,0x3f | (1 << (val
+ 8)));
1980 s
->status
= READY_STAT
| SEEK_STAT
;
1988 case WIN_FLUSH_CACHE
:
1989 case WIN_FLUSH_CACHE_EXT
:
1992 s
->status
= READY_STAT
| SEEK_STAT
;
1997 case WIN_STANDBYNOW1
:
1998 case WIN_STANDBYNOW2
:
1999 case WIN_IDLEIMMEDIATE
:
2000 case CFA_IDLEIMMEDIATE
:
2005 s
->status
= READY_STAT
;
2011 /* XXX: Check that seek is within bounds */
2012 s
->status
= READY_STAT
| SEEK_STAT
;
2015 /* ATAPI commands */
2018 ide_atapi_identify(s
);
2019 s
->status
= READY_STAT
| SEEK_STAT
;
2020 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
2022 ide_abort_command(s
);
2027 ide_set_signature(s
);
2029 s
->status
= 0; /* ATAPI spec (v6) section 9.10 defines packet
2030 * devices to return a clear status register
2031 * with READY_STAT *not* set. */
2033 s
->status
= READY_STAT
| SEEK_STAT
;
2034 s
->error
= 0x01; /* Device 0 passed, Device 1 passed or not
2042 ide_set_signature(s
);
2043 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2049 /* overlapping commands not supported */
2050 if (s
->feature
& 0x02)
2052 s
->status
= READY_STAT
| SEEK_STAT
;
2053 s
->atapi_dma
= s
->feature
& 1;
2055 ide_transfer_start(s
, s
->io_buffer
, ATAPI_PACKET_SIZE
,
2058 /* CF-ATA commands */
2059 case CFA_REQ_EXT_ERROR_CODE
:
2062 s
->error
= 0x09; /* miscellaneous error */
2063 s
->status
= READY_STAT
| SEEK_STAT
;
2066 case CFA_ERASE_SECTORS
:
2067 case CFA_WEAR_LEVEL
:
2070 if (val
== CFA_WEAR_LEVEL
)
2072 if (val
== CFA_ERASE_SECTORS
)
2073 s
->media_changed
= 1;
2075 s
->status
= READY_STAT
| SEEK_STAT
;
2078 case CFA_TRANSLATE_SECTOR
:
2082 s
->status
= READY_STAT
| SEEK_STAT
;
2083 memset(s
->io_buffer
, 0, 0x200);
2084 s
->io_buffer
[0x00] = s
->hcyl
; /* Cyl MSB */
2085 s
->io_buffer
[0x01] = s
->lcyl
; /* Cyl LSB */
2086 s
->io_buffer
[0x02] = s
->select
; /* Head */
2087 s
->io_buffer
[0x03] = s
->sector
; /* Sector */
2088 s
->io_buffer
[0x04] = ide_get_sector(s
) >> 16; /* LBA MSB */
2089 s
->io_buffer
[0x05] = ide_get_sector(s
) >> 8; /* LBA */
2090 s
->io_buffer
[0x06] = ide_get_sector(s
) >> 0; /* LBA LSB */
2091 s
->io_buffer
[0x13] = 0x00; /* Erase flag */
2092 s
->io_buffer
[0x18] = 0x00; /* Hot count */
2093 s
->io_buffer
[0x19] = 0x00; /* Hot count */
2094 s
->io_buffer
[0x1a] = 0x01; /* Hot count */
2095 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2098 case CFA_ACCESS_METADATA_STORAGE
:
2101 switch (s
->feature
) {
2102 case 0x02: /* Inquiry Metadata Storage */
2103 ide_cfata_metadata_inquiry(s
);
2105 case 0x03: /* Read Metadata Storage */
2106 ide_cfata_metadata_read(s
);
2108 case 0x04: /* Write Metadata Storage */
2109 ide_cfata_metadata_write(s
);
2114 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2115 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2118 case IBM_SENSE_CONDITION
:
2121 switch (s
->feature
) {
2122 case 0x01: /* sense temperature in device */
2123 s
->nsector
= 0x50; /* +20 C */
2128 s
->status
= READY_STAT
| SEEK_STAT
;
2135 if (s
->hcyl
!= 0xc2 || s
->lcyl
!= 0x4f)
2137 if (!s
->smart_enabled
&& s
->feature
!= SMART_ENABLE
)
2139 switch (s
->feature
) {
2141 s
->smart_enabled
= 0;
2142 s
->status
= READY_STAT
| SEEK_STAT
;
2146 s
->smart_enabled
= 1;
2147 s
->status
= READY_STAT
| SEEK_STAT
;
2150 case SMART_ATTR_AUTOSAVE
:
2151 switch (s
->sector
) {
2153 s
->smart_autosave
= 0;
2156 s
->smart_autosave
= 1;
2161 s
->status
= READY_STAT
| SEEK_STAT
;
2165 if (!s
->smart_errors
) {
2172 s
->status
= READY_STAT
| SEEK_STAT
;
2175 case SMART_READ_THRESH
:
2176 memset(s
->io_buffer
, 0, 0x200);
2177 s
->io_buffer
[0] = 0x01; /* smart struct version */
2178 for (n
=0; n
<30; n
++) {
2179 if (smart_attributes
[n
][0] == 0)
2181 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2182 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][4];
2184 for (n
=0; n
<511; n
++) /* checksum */
2185 s
->io_buffer
[511] += s
->io_buffer
[n
];
2186 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2187 s
->status
= READY_STAT
| SEEK_STAT
;
2188 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2191 case SMART_READ_DATA
:
2192 memset(s
->io_buffer
, 0, 0x200);
2193 s
->io_buffer
[0] = 0x01; /* smart struct version */
2194 for (n
=0; n
<30; n
++) {
2195 if (smart_attributes
[n
][0] == 0)
2197 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2198 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][1];
2199 s
->io_buffer
[2+3+(n
*12)] = smart_attributes
[n
][2];
2200 s
->io_buffer
[2+4+(n
*12)] = smart_attributes
[n
][3];
2202 s
->io_buffer
[362] = 0x02 | (s
->smart_autosave
?0x80:0x00);
2203 if (s
->smart_selftest_count
== 0) {
2204 s
->io_buffer
[363] = 0;
2207 s
->smart_selftest_data
[3 +
2208 (s
->smart_selftest_count
- 1) *
2211 s
->io_buffer
[364] = 0x20;
2212 s
->io_buffer
[365] = 0x01;
2213 /* offline data collection capacity: execute + self-test*/
2214 s
->io_buffer
[367] = (1<<4 | 1<<3 | 1);
2215 s
->io_buffer
[368] = 0x03; /* smart capability (1) */
2216 s
->io_buffer
[369] = 0x00; /* smart capability (2) */
2217 s
->io_buffer
[370] = 0x01; /* error logging supported */
2218 s
->io_buffer
[372] = 0x02; /* minutes for poll short test */
2219 s
->io_buffer
[373] = 0x36; /* minutes for poll ext test */
2220 s
->io_buffer
[374] = 0x01; /* minutes for poll conveyance */
2222 for (n
=0; n
<511; n
++)
2223 s
->io_buffer
[511] += s
->io_buffer
[n
];
2224 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2225 s
->status
= READY_STAT
| SEEK_STAT
;
2226 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2229 case SMART_READ_LOG
:
2230 switch (s
->sector
) {
2231 case 0x01: /* summary smart error log */
2232 memset(s
->io_buffer
, 0, 0x200);
2233 s
->io_buffer
[0] = 0x01;
2234 s
->io_buffer
[1] = 0x00; /* no error entries */
2235 s
->io_buffer
[452] = s
->smart_errors
& 0xff;
2236 s
->io_buffer
[453] = (s
->smart_errors
& 0xff00) >> 8;
2238 for (n
=0; n
<511; n
++)
2239 s
->io_buffer
[511] += s
->io_buffer
[n
];
2240 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2242 case 0x06: /* smart self test log */
2243 memset(s
->io_buffer
, 0, 0x200);
2244 s
->io_buffer
[0] = 0x01;
2245 if (s
->smart_selftest_count
== 0) {
2246 s
->io_buffer
[508] = 0;
2248 s
->io_buffer
[508] = s
->smart_selftest_count
;
2249 for (n
=2; n
<506; n
++)
2250 s
->io_buffer
[n
] = s
->smart_selftest_data
[n
];
2252 for (n
=0; n
<511; n
++)
2253 s
->io_buffer
[511] += s
->io_buffer
[n
];
2254 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2259 s
->status
= READY_STAT
| SEEK_STAT
;
2260 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2263 case SMART_EXECUTE_OFFLINE
:
2264 switch (s
->sector
) {
2265 case 0: /* off-line routine */
2266 case 1: /* short self test */
2267 case 2: /* extended self test */
2268 s
->smart_selftest_count
++;
2269 if(s
->smart_selftest_count
> 21)
2270 s
->smart_selftest_count
= 0;
2271 n
= 2 + (s
->smart_selftest_count
- 1) * 24;
2272 s
->smart_selftest_data
[n
] = s
->sector
;
2273 s
->smart_selftest_data
[n
+1] = 0x00; /* OK and finished */
2274 s
->smart_selftest_data
[n
+2] = 0x34; /* hour count lsb */
2275 s
->smart_selftest_data
[n
+3] = 0x12; /* hour count msb */
2276 s
->status
= READY_STAT
| SEEK_STAT
;
2289 ide_abort_command(s
);
2296 uint32_t ide_ioport_read(void *opaque
, uint32_t addr1
)
2298 IDEBus
*bus
= opaque
;
2299 IDEState
*s
= idebus_active_if(bus
);
2304 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2305 //hob = s->select & (1 << 7);
2312 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2313 (s
!= bus
->ifs
&& !s
->bs
))
2318 ret
= s
->hob_feature
;
2321 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2324 ret
= s
->nsector
& 0xff;
2326 ret
= s
->hob_nsector
;
2329 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2334 ret
= s
->hob_sector
;
2337 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2345 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2353 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2360 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2361 (s
!= bus
->ifs
&& !s
->bs
))
2365 qemu_irq_lower(s
->irq
);
2369 printf("ide: read addr=0x%x val=%02x\n", addr1
, ret
);
2374 uint32_t ide_status_read(void *opaque
, uint32_t addr
)
2376 IDEBus
*bus
= opaque
;
2377 IDEState
*s
= idebus_active_if(bus
);
2380 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2381 (s
!= bus
->ifs
&& !s
->bs
))
2386 printf("ide: read status addr=0x%x val=%02x\n", addr
, ret
);
2391 void ide_cmd_write(void *opaque
, uint32_t addr
, uint32_t val
)
2393 IDEBus
*bus
= opaque
;
2398 printf("ide: write control addr=0x%x val=%02x\n", addr
, val
);
2400 /* common for both drives */
2401 if (!(bus
->ifs
[0].cmd
& IDE_CMD_RESET
) &&
2402 (val
& IDE_CMD_RESET
)) {
2403 /* reset low to high */
2404 for(i
= 0;i
< 2; i
++) {
2406 s
->status
= BUSY_STAT
| SEEK_STAT
;
2409 } else if ((bus
->ifs
[0].cmd
& IDE_CMD_RESET
) &&
2410 !(val
& IDE_CMD_RESET
)) {
2412 for(i
= 0;i
< 2; i
++) {
2415 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2417 s
->status
= READY_STAT
| SEEK_STAT
;
2418 ide_set_signature(s
);
2422 bus
->ifs
[0].cmd
= val
;
2423 bus
->ifs
[1].cmd
= val
;
2426 void ide_data_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2428 IDEBus
*bus
= opaque
;
2429 IDEState
*s
= idebus_active_if(bus
);
2432 /* PIO data access allowed only when DRQ bit is set */
2433 if (!(s
->status
& DRQ_STAT
))
2437 *(uint16_t *)p
= le16_to_cpu(val
);
2440 if (p
>= s
->data_end
)
2441 s
->end_transfer_func(s
);
2444 uint32_t ide_data_readw(void *opaque
, uint32_t addr
)
2446 IDEBus
*bus
= opaque
;
2447 IDEState
*s
= idebus_active_if(bus
);
2451 /* PIO data access allowed only when DRQ bit is set */
2452 if (!(s
->status
& DRQ_STAT
))
2456 ret
= cpu_to_le16(*(uint16_t *)p
);
2459 if (p
>= s
->data_end
)
2460 s
->end_transfer_func(s
);
2464 void ide_data_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2466 IDEBus
*bus
= opaque
;
2467 IDEState
*s
= idebus_active_if(bus
);
2470 /* PIO data access allowed only when DRQ bit is set */
2471 if (!(s
->status
& DRQ_STAT
))
2475 *(uint32_t *)p
= le32_to_cpu(val
);
2478 if (p
>= s
->data_end
)
2479 s
->end_transfer_func(s
);
2482 uint32_t ide_data_readl(void *opaque
, uint32_t addr
)
2484 IDEBus
*bus
= opaque
;
2485 IDEState
*s
= idebus_active_if(bus
);
2489 /* PIO data access allowed only when DRQ bit is set */
2490 if (!(s
->status
& DRQ_STAT
))
2494 ret
= cpu_to_le32(*(uint32_t *)p
);
2497 if (p
>= s
->data_end
)
2498 s
->end_transfer_func(s
);
2502 static void ide_dummy_transfer_stop(IDEState
*s
)
2504 s
->data_ptr
= s
->io_buffer
;
2505 s
->data_end
= s
->io_buffer
;
2506 s
->io_buffer
[0] = 0xff;
2507 s
->io_buffer
[1] = 0xff;
2508 s
->io_buffer
[2] = 0xff;
2509 s
->io_buffer
[3] = 0xff;
2512 void ide_reset(IDEState
*s
)
2514 IDEBus
*bus
= s
->bus
;
2517 s
->mult_sectors
= 0;
2519 s
->mult_sectors
= MAX_MULT_SECTORS
;
2520 bus
->unit
= s
->unit
;
2522 s
->status
= READY_STAT
| SEEK_STAT
;
2523 ide_set_signature(s
);
2524 /* init the transfer handler so that 0xffff is returned on data
2526 s
->end_transfer_func
= ide_dummy_transfer_stop
;
2527 ide_dummy_transfer_stop(s
);
2528 s
->media_changed
= 0;
2531 void ide_init2(IDEBus
*bus
, BlockDriverState
*hd0
, BlockDriverState
*hd1
,
2535 static int drive_serial
= 1;
2536 int i
, cylinders
, heads
, secs
;
2537 uint64_t nb_sectors
;
2539 for(i
= 0; i
< 2; i
++) {
2543 s
->bs
= (i
== 0) ? hd0
: hd1
;
2544 s
->io_buffer
= qemu_blockalign(s
->bs
, IDE_DMA_BUF_SECTORS
*512 + 4);
2546 bdrv_get_geometry(s
->bs
, &nb_sectors
);
2547 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
2548 s
->cylinders
= cylinders
;
2551 s
->nb_sectors
= nb_sectors
;
2552 /* The SMART values should be preserved across power cycles
2554 s
->smart_enabled
= 1;
2555 s
->smart_autosave
= 1;
2556 s
->smart_errors
= 0;
2557 s
->smart_selftest_count
= 0;
2558 s
->smart_selftest_data
= qemu_blockalign(s
->bs
, 512);
2559 if (bdrv_get_type_hint(s
->bs
) == BDRV_TYPE_CDROM
) {
2561 bdrv_set_change_cb(s
->bs
, cdrom_change_cb
, s
);
2564 s
->drive_serial
= drive_serial
++;
2565 strncpy(s
->drive_serial_str
, drive_get_serial(s
->bs
),
2566 sizeof(s
->drive_serial_str
));
2567 if (strlen(s
->drive_serial_str
) == 0)
2568 snprintf(s
->drive_serial_str
, sizeof(s
->drive_serial_str
),
2569 "QM%05d", s
->drive_serial
);
2571 s
->sector_write_timer
= qemu_new_timer(vm_clock
,
2572 ide_sector_write_timer_cb
, s
);
2577 void ide_init_ioport(IDEBus
*bus
, int iobase
, int iobase2
)
2579 register_ioport_write(iobase
, 8, 1, ide_ioport_write
, bus
);
2580 register_ioport_read(iobase
, 8, 1, ide_ioport_read
, bus
);
2582 register_ioport_read(iobase2
, 1, 1, ide_status_read
, bus
);
2583 register_ioport_write(iobase2
, 1, 1, ide_cmd_write
, bus
);
2587 register_ioport_write(iobase
, 2, 2, ide_data_writew
, bus
);
2588 register_ioport_read(iobase
, 2, 2, ide_data_readw
, bus
);
2589 register_ioport_write(iobase
, 4, 4, ide_data_writel
, bus
);
2590 register_ioport_read(iobase
, 4, 4, ide_data_readl
, bus
);
2593 /* save per IDE drive data */
2594 void ide_save(QEMUFile
* f
, IDEState
*s
)
2596 qemu_put_be32(f
, s
->mult_sectors
);
2597 qemu_put_be32(f
, s
->identify_set
);
2598 if (s
->identify_set
) {
2599 qemu_put_buffer(f
, (const uint8_t *)s
->identify_data
, 512);
2601 qemu_put_8s(f
, &s
->feature
);
2602 qemu_put_8s(f
, &s
->error
);
2603 qemu_put_be32s(f
, &s
->nsector
);
2604 qemu_put_8s(f
, &s
->sector
);
2605 qemu_put_8s(f
, &s
->lcyl
);
2606 qemu_put_8s(f
, &s
->hcyl
);
2607 qemu_put_8s(f
, &s
->hob_feature
);
2608 qemu_put_8s(f
, &s
->hob_nsector
);
2609 qemu_put_8s(f
, &s
->hob_sector
);
2610 qemu_put_8s(f
, &s
->hob_lcyl
);
2611 qemu_put_8s(f
, &s
->hob_hcyl
);
2612 qemu_put_8s(f
, &s
->select
);
2613 qemu_put_8s(f
, &s
->status
);
2614 qemu_put_8s(f
, &s
->lba48
);
2616 qemu_put_8s(f
, &s
->sense_key
);
2617 qemu_put_8s(f
, &s
->asc
);
2618 qemu_put_8s(f
, &s
->cdrom_changed
);
2619 /* XXX: if a transfer is pending, we do not save it yet */
2622 /* load per IDE drive data */
2623 void ide_load(QEMUFile
* f
, IDEState
*s
, int version_id
)
2625 s
->mult_sectors
=qemu_get_be32(f
);
2626 s
->identify_set
=qemu_get_be32(f
);
2627 if (s
->identify_set
) {
2628 qemu_get_buffer(f
, (uint8_t *)s
->identify_data
, 512);
2630 qemu_get_8s(f
, &s
->feature
);
2631 qemu_get_8s(f
, &s
->error
);
2632 qemu_get_be32s(f
, &s
->nsector
);
2633 qemu_get_8s(f
, &s
->sector
);
2634 qemu_get_8s(f
, &s
->lcyl
);
2635 qemu_get_8s(f
, &s
->hcyl
);
2636 qemu_get_8s(f
, &s
->hob_feature
);
2637 qemu_get_8s(f
, &s
->hob_nsector
);
2638 qemu_get_8s(f
, &s
->hob_sector
);
2639 qemu_get_8s(f
, &s
->hob_lcyl
);
2640 qemu_get_8s(f
, &s
->hob_hcyl
);
2641 qemu_get_8s(f
, &s
->select
);
2642 qemu_get_8s(f
, &s
->status
);
2643 qemu_get_8s(f
, &s
->lba48
);
2645 qemu_get_8s(f
, &s
->sense_key
);
2646 qemu_get_8s(f
, &s
->asc
);
2647 if (version_id
== 3) {
2648 qemu_get_8s(f
, &s
->cdrom_changed
);
2650 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
2651 s
->asc
== ASC_MEDIUM_MAY_HAVE_CHANGED
)
2652 s
->cdrom_changed
= 1;
2654 /* XXX: if a transfer is pending, we do not save it yet */
2657 void idebus_save(QEMUFile
* f
, IDEBus
*bus
)
2659 IDEState
*s
= idebus_active_if(bus
);
2660 qemu_put_8s(f
, &s
->cmd
);
2661 qemu_put_8s(f
, &bus
->unit
);
2664 void idebus_load(QEMUFile
* f
, IDEBus
*bus
, int version_id
)
2669 qemu_get_8s(f
, &cmd
);
2670 qemu_get_8s(f
, &bus
->unit
);
2671 s
= idebus_active_if(bus
);
2675 /***********************************************************/
2676 /* ISA IDE definitions */
2678 void isa_ide_init(int iobase
, int iobase2
, qemu_irq irq
,
2679 BlockDriverState
*hd0
, BlockDriverState
*hd1
)
2683 bus
= qemu_mallocz(sizeof(*bus
));
2685 ide_init2(bus
, hd0
, hd1
, irq
);
2686 ide_init_ioport(bus
, iobase
, iobase2
);
2689 /***********************************************************/
2690 /* PCI IDE definitions */
2692 static void cmd646_update_irq(PCIIDEState
*d
);
2694 static void ide_map(PCIDevice
*pci_dev
, int region_num
,
2695 uint32_t addr
, uint32_t size
, int type
)
2697 PCIIDEState
*d
= (PCIIDEState
*)pci_dev
;
2700 if (region_num
<= 3) {
2701 bus
= &d
->bus
[(region_num
>> 1)];
2702 if (region_num
& 1) {
2703 register_ioport_read(addr
+ 2, 1, 1, ide_status_read
, bus
);
2704 register_ioport_write(addr
+ 2, 1, 1, ide_cmd_write
, bus
);
2706 register_ioport_write(addr
, 8, 1, ide_ioport_write
, bus
);
2707 register_ioport_read(addr
, 8, 1, ide_ioport_read
, bus
);
2710 register_ioport_write(addr
, 2, 2, ide_data_writew
, bus
);
2711 register_ioport_read(addr
, 2, 2, ide_data_readw
, bus
);
2712 register_ioport_write(addr
, 4, 4, ide_data_writel
, bus
);
2713 register_ioport_read(addr
, 4, 4, ide_data_readl
, bus
);
2718 static void ide_dma_start(IDEState
*s
, BlockDriverCompletionFunc
*dma_cb
)
2720 BMDMAState
*bm
= s
->bus
->bmdma
;
2724 bm
->dma_cb
= dma_cb
;
2725 bm
->cur_prd_last
= 0;
2726 bm
->cur_prd_addr
= 0;
2727 bm
->cur_prd_len
= 0;
2728 bm
->sector_num
= ide_get_sector(s
);
2729 bm
->nsector
= s
->nsector
;
2730 if (bm
->status
& BM_STATUS_DMAING
) {
2735 static void ide_dma_restart(IDEState
*s
)
2737 BMDMAState
*bm
= s
->bus
->bmdma
;
2738 ide_set_sector(s
, bm
->sector_num
);
2739 s
->io_buffer_index
= 0;
2740 s
->io_buffer_size
= 0;
2741 s
->nsector
= bm
->nsector
;
2742 bm
->cur_addr
= bm
->addr
;
2743 bm
->dma_cb
= ide_write_dma_cb
;
2744 ide_dma_start(s
, bm
->dma_cb
);
2747 void ide_dma_cancel(BMDMAState
*bm
)
2749 if (bm
->status
& BM_STATUS_DMAING
) {
2750 bm
->status
&= ~BM_STATUS_DMAING
;
2751 /* cancel DMA request */
2756 printf("aio_cancel\n");
2758 bdrv_aio_cancel(bm
->aiocb
);
2764 static void bmdma_cmd_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
2766 BMDMAState
*bm
= opaque
;
2768 printf("%s: 0x%08x\n", __func__
, val
);
2770 if (!(val
& BM_CMD_START
)) {
2771 /* XXX: do it better */
2773 bm
->cmd
= val
& 0x09;
2775 if (!(bm
->status
& BM_STATUS_DMAING
)) {
2776 bm
->status
|= BM_STATUS_DMAING
;
2777 /* start dma transfer if possible */
2781 bm
->cmd
= val
& 0x09;
2785 static uint32_t bmdma_readb(void *opaque
, uint32_t addr
)
2787 BMDMAState
*bm
= opaque
;
2788 PCIIDEState
*pci_dev
;
2796 pci_dev
= bm
->pci_dev
;
2797 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
2798 val
= pci_dev
->dev
.config
[MRDMODE
];
2807 pci_dev
= bm
->pci_dev
;
2808 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
2809 if (bm
== &pci_dev
->bmdma
[0])
2810 val
= pci_dev
->dev
.config
[UDIDETCR0
];
2812 val
= pci_dev
->dev
.config
[UDIDETCR1
];
2822 printf("bmdma: readb 0x%02x : 0x%02x\n", addr
, val
);
2827 static void bmdma_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
2829 BMDMAState
*bm
= opaque
;
2830 PCIIDEState
*pci_dev
;
2832 printf("bmdma: writeb 0x%02x : 0x%02x\n", addr
, val
);
2836 pci_dev
= bm
->pci_dev
;
2837 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
2838 pci_dev
->dev
.config
[MRDMODE
] =
2839 (pci_dev
->dev
.config
[MRDMODE
] & ~0x30) | (val
& 0x30);
2840 cmd646_update_irq(pci_dev
);
2844 bm
->status
= (val
& 0x60) | (bm
->status
& 1) | (bm
->status
& ~val
& 0x06);
2847 pci_dev
= bm
->pci_dev
;
2848 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
2849 if (bm
== &pci_dev
->bmdma
[0])
2850 pci_dev
->dev
.config
[UDIDETCR0
] = val
;
2852 pci_dev
->dev
.config
[UDIDETCR1
] = val
;
2858 static uint32_t bmdma_addr_readb(void *opaque
, uint32_t addr
)
2860 BMDMAState
*bm
= opaque
;
2862 val
= (bm
->addr
>> ((addr
& 3) * 8)) & 0xff;
2864 printf("%s: 0x%08x\n", __func__
, val
);
2869 static void bmdma_addr_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
2871 BMDMAState
*bm
= opaque
;
2872 int shift
= (addr
& 3) * 8;
2874 printf("%s: 0x%08x\n", __func__
, val
);
2876 bm
->addr
&= ~(0xFF << shift
);
2877 bm
->addr
|= ((val
& 0xFF) << shift
) & ~3;
2878 bm
->cur_addr
= bm
->addr
;
2881 static uint32_t bmdma_addr_readw(void *opaque
, uint32_t addr
)
2883 BMDMAState
*bm
= opaque
;
2885 val
= (bm
->addr
>> ((addr
& 3) * 8)) & 0xffff;
2887 printf("%s: 0x%08x\n", __func__
, val
);
2892 static void bmdma_addr_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2894 BMDMAState
*bm
= opaque
;
2895 int shift
= (addr
& 3) * 8;
2897 printf("%s: 0x%08x\n", __func__
, val
);
2899 bm
->addr
&= ~(0xFFFF << shift
);
2900 bm
->addr
|= ((val
& 0xFFFF) << shift
) & ~3;
2901 bm
->cur_addr
= bm
->addr
;
2904 static uint32_t bmdma_addr_readl(void *opaque
, uint32_t addr
)
2906 BMDMAState
*bm
= opaque
;
2910 printf("%s: 0x%08x\n", __func__
, val
);
2915 static void bmdma_addr_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2917 BMDMAState
*bm
= opaque
;
2919 printf("%s: 0x%08x\n", __func__
, val
);
2921 bm
->addr
= val
& ~3;
2922 bm
->cur_addr
= bm
->addr
;
2925 static void bmdma_map(PCIDevice
*pci_dev
, int region_num
,
2926 uint32_t addr
, uint32_t size
, int type
)
2928 PCIIDEState
*d
= (PCIIDEState
*)pci_dev
;
2931 for(i
= 0;i
< 2; i
++) {
2932 BMDMAState
*bm
= &d
->bmdma
[i
];
2933 d
->bus
[i
].bmdma
= bm
;
2934 bm
->pci_dev
= DO_UPCAST(PCIIDEState
, dev
, pci_dev
);
2936 qemu_add_vm_change_state_handler(ide_dma_restart_cb
, bm
);
2938 register_ioport_write(addr
, 1, 1, bmdma_cmd_writeb
, bm
);
2940 register_ioport_write(addr
+ 1, 3, 1, bmdma_writeb
, bm
);
2941 register_ioport_read(addr
, 4, 1, bmdma_readb
, bm
);
2943 register_ioport_write(addr
+ 4, 4, 1, bmdma_addr_writeb
, bm
);
2944 register_ioport_read(addr
+ 4, 4, 1, bmdma_addr_readb
, bm
);
2945 register_ioport_write(addr
+ 4, 4, 2, bmdma_addr_writew
, bm
);
2946 register_ioport_read(addr
+ 4, 4, 2, bmdma_addr_readw
, bm
);
2947 register_ioport_write(addr
+ 4, 4, 4, bmdma_addr_writel
, bm
);
2948 register_ioport_read(addr
+ 4, 4, 4, bmdma_addr_readl
, bm
);
2953 static void pci_ide_save(QEMUFile
* f
, void *opaque
)
2955 PCIIDEState
*d
= opaque
;
2958 pci_device_save(&d
->dev
, f
);
2960 for(i
= 0; i
< 2; i
++) {
2961 BMDMAState
*bm
= &d
->bmdma
[i
];
2963 qemu_put_8s(f
, &bm
->cmd
);
2964 qemu_put_8s(f
, &bm
->status
);
2965 qemu_put_be32s(f
, &bm
->addr
);
2966 qemu_put_sbe64s(f
, &bm
->sector_num
);
2967 qemu_put_be32s(f
, &bm
->nsector
);
2968 ifidx
= bm
->unit
+ 2*i
;
2969 qemu_put_8s(f
, &ifidx
);
2970 /* XXX: if a transfer is pending, we do not save it yet */
2973 /* per IDE interface data */
2974 for(i
= 0; i
< 2; i
++) {
2975 idebus_save(f
, &d
->bus
[i
]);
2978 /* per IDE drive data */
2979 for(i
= 0; i
< 2; i
++) {
2980 ide_save(f
, &d
->bus
[i
].ifs
[0]);
2981 ide_save(f
, &d
->bus
[i
].ifs
[1]);
2985 static int pci_ide_load(QEMUFile
* f
, void *opaque
, int version_id
)
2987 PCIIDEState
*d
= opaque
;
2990 if (version_id
!= 2 && version_id
!= 3)
2992 ret
= pci_device_load(&d
->dev
, f
);
2996 for(i
= 0; i
< 2; i
++) {
2997 BMDMAState
*bm
= &d
->bmdma
[i
];
2999 qemu_get_8s(f
, &bm
->cmd
);
3000 qemu_get_8s(f
, &bm
->status
);
3001 qemu_get_be32s(f
, &bm
->addr
);
3002 qemu_get_sbe64s(f
, &bm
->sector_num
);
3003 qemu_get_be32s(f
, &bm
->nsector
);
3004 qemu_get_8s(f
, &ifidx
);
3005 bm
->unit
= ifidx
& 1;
3006 /* XXX: if a transfer is pending, we do not save it yet */
3009 /* per IDE interface data */
3010 for(i
= 0; i
< 2; i
++) {
3011 idebus_load(f
, &d
->bus
[i
], version_id
);
3014 /* per IDE drive data */
3015 for(i
= 0; i
< 2; i
++) {
3016 ide_load(f
, &d
->bus
[i
].ifs
[0], version_id
);
3017 ide_load(f
, &d
->bus
[i
].ifs
[1], version_id
);
3022 /* XXX: call it also when the MRDMODE is changed from the PCI config
3024 static void cmd646_update_irq(PCIIDEState
*d
)
3027 pci_level
= ((d
->dev
.config
[MRDMODE
] & MRDMODE_INTR_CH0
) &&
3028 !(d
->dev
.config
[MRDMODE
] & MRDMODE_BLK_CH0
)) ||
3029 ((d
->dev
.config
[MRDMODE
] & MRDMODE_INTR_CH1
) &&
3030 !(d
->dev
.config
[MRDMODE
] & MRDMODE_BLK_CH1
));
3031 qemu_set_irq(d
->dev
.irq
[0], pci_level
);
3034 /* the PCI irq level is the logical OR of the two channels */
3035 static void cmd646_set_irq(void *opaque
, int channel
, int level
)
3037 PCIIDEState
*d
= opaque
;
3040 irq_mask
= MRDMODE_INTR_CH0
<< channel
;
3042 d
->dev
.config
[MRDMODE
] |= irq_mask
;
3044 d
->dev
.config
[MRDMODE
] &= ~irq_mask
;
3045 cmd646_update_irq(d
);
3048 static void cmd646_reset(void *opaque
)
3050 PCIIDEState
*d
= opaque
;
3053 for (i
= 0; i
< 2; i
++)
3054 ide_dma_cancel(&d
->bmdma
[i
]);
3057 /* CMD646 PCI IDE controller */
3058 void pci_cmd646_ide_init(PCIBus
*bus
, BlockDriverState
**hd_table
,
3059 int secondary_ide_enabled
)
3065 d
= (PCIIDEState
*)pci_register_device(bus
, "CMD646 IDE",
3066 sizeof(PCIIDEState
),
3069 d
->type
= IDE_TYPE_CMD646
;
3070 pci_conf
= d
->dev
.config
;
3071 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_CMD
);
3072 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_CMD_646
);
3074 pci_conf
[0x08] = 0x07; // IDE controller revision
3075 pci_conf
[0x09] = 0x8f;
3077 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
3078 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
3080 pci_conf
[0x51] = 0x04; // enable IDE0
3081 if (secondary_ide_enabled
) {
3082 /* XXX: if not enabled, really disable the seconday IDE controller */
3083 pci_conf
[0x51] |= 0x08; /* enable IDE1 */
3086 pci_register_bar((PCIDevice
*)d
, 0, 0x8,
3087 PCI_ADDRESS_SPACE_IO
, ide_map
);
3088 pci_register_bar((PCIDevice
*)d
, 1, 0x4,
3089 PCI_ADDRESS_SPACE_IO
, ide_map
);
3090 pci_register_bar((PCIDevice
*)d
, 2, 0x8,
3091 PCI_ADDRESS_SPACE_IO
, ide_map
);
3092 pci_register_bar((PCIDevice
*)d
, 3, 0x4,
3093 PCI_ADDRESS_SPACE_IO
, ide_map
);
3094 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
3095 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
3097 pci_conf
[0x3d] = 0x01; // interrupt on pin 1
3099 irq
= qemu_allocate_irqs(cmd646_set_irq
, d
, 2);
3100 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], irq
[0]);
3101 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], irq
[1]);
3103 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);
3104 qemu_register_reset(cmd646_reset
, d
);
3108 static void piix3_reset(void *opaque
)
3110 PCIIDEState
*d
= opaque
;
3111 uint8_t *pci_conf
= d
->dev
.config
;
3114 for (i
= 0; i
< 2; i
++)
3115 ide_dma_cancel(&d
->bmdma
[i
]);
3117 pci_conf
[0x04] = 0x00;
3118 pci_conf
[0x05] = 0x00;
3119 pci_conf
[0x06] = 0x80; /* FBC */
3120 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
3121 pci_conf
[0x20] = 0x01; /* BMIBA: 20-23h */
3124 /* hd_table must contain 4 block drivers */
3125 /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
3126 void pci_piix3_ide_init(PCIBus
*bus
, BlockDriverState
**hd_table
, int devfn
,
3133 /* register a function 1 of PIIX3 */
3134 d
= (PCIIDEState
*)pci_register_device(bus
, "PIIX3 IDE",
3135 sizeof(PCIIDEState
),
3138 d
->type
= IDE_TYPE_PIIX3
;
3140 pci_conf
= d
->dev
.config
;
3141 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
3142 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371SB_1
);
3143 pci_conf
[0x09] = 0x80; // legacy ATA mode
3144 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
3145 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
3147 qemu_register_reset(piix3_reset
, d
);
3150 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
3151 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
3153 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], isa_reserve_irq(14));
3154 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], isa_reserve_irq(15));
3155 ide_init_ioport(&d
->bus
[0], 0x1f0, 0x3f6);
3156 ide_init_ioport(&d
->bus
[1], 0x170, 0x376);
3158 for (i
= 0; i
< 4; i
++)
3160 hd_table
[i
]->private = &d
->dev
;
3162 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);
3165 /* hd_table must contain 4 block drivers */
3166 /* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
3167 void pci_piix4_ide_init(PCIBus
*bus
, BlockDriverState
**hd_table
, int devfn
,
3173 /* register a function 1 of PIIX4 */
3174 d
= (PCIIDEState
*)pci_register_device(bus
, "PIIX4 IDE",
3175 sizeof(PCIIDEState
),
3178 d
->type
= IDE_TYPE_PIIX4
;
3180 pci_conf
= d
->dev
.config
;
3181 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
3182 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB
);
3183 pci_conf
[0x09] = 0x80; // legacy ATA mode
3184 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
3185 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
3187 qemu_register_reset(piix3_reset
, d
);
3190 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
3191 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
3194 * These should call isa_reserve_irq() instead when MIPS supports it
3196 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], pic
[14]);
3197 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], pic
[15]);
3198 ide_init_ioport(&d
->bus
[0], 0x1f0, 0x3f6);
3199 ide_init_ioport(&d
->bus
[1], 0x170, 0x376);
3201 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);
3204 #if defined(TARGET_PPC)
3205 /***********************************************************/
3206 /* MacIO based PowerPC IDE */
3208 typedef struct MACIOIDEState
{
3210 BlockDriverAIOCB
*aiocb
;
3213 static void pmac_ide_atapi_transfer_cb(void *opaque
, int ret
)
3215 DBDMA_io
*io
= opaque
;
3216 MACIOIDEState
*m
= io
->opaque
;
3217 IDEState
*s
= idebus_active_if(&m
->bus
);
3221 qemu_sglist_destroy(&s
->sg
);
3222 ide_atapi_io_error(s
, ret
);
3223 io
->dma_end(opaque
);
3227 if (s
->io_buffer_size
> 0) {
3229 qemu_sglist_destroy(&s
->sg
);
3231 s
->packet_transfer_size
-= s
->io_buffer_size
;
3233 s
->io_buffer_index
+= s
->io_buffer_size
;
3234 s
->lba
+= s
->io_buffer_index
>> 11;
3235 s
->io_buffer_index
&= 0x7ff;
3238 if (s
->packet_transfer_size
<= 0)
3239 ide_atapi_cmd_ok(s
);
3242 io
->dma_end(opaque
);
3246 /* launch next transfer */
3248 s
->io_buffer_size
= io
->len
;
3250 qemu_sglist_init(&s
->sg
, io
->len
/ TARGET_PAGE_SIZE
+ 1);
3251 qemu_sglist_add(&s
->sg
, io
->addr
, io
->len
);
3252 io
->addr
+= io
->len
;
3255 m
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
,
3256 (int64_t)(s
->lba
<< 2) + (s
->io_buffer_index
>> 9),
3257 pmac_ide_atapi_transfer_cb
, io
);
3259 qemu_sglist_destroy(&s
->sg
);
3260 /* Note: media not present is the most likely case */
3261 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
3262 ASC_MEDIUM_NOT_PRESENT
);
3263 io
->dma_end(opaque
);
3268 static void pmac_ide_transfer_cb(void *opaque
, int ret
)
3270 DBDMA_io
*io
= opaque
;
3271 MACIOIDEState
*m
= io
->opaque
;
3272 IDEState
*s
= idebus_active_if(&m
->bus
);
3278 qemu_sglist_destroy(&s
->sg
);
3284 sector_num
= ide_get_sector(s
);
3285 if (s
->io_buffer_size
> 0) {
3287 qemu_sglist_destroy(&s
->sg
);
3288 n
= (s
->io_buffer_size
+ 0x1ff) >> 9;
3290 ide_set_sector(s
, sector_num
);
3294 /* end of transfer ? */
3295 if (s
->nsector
== 0) {
3296 s
->status
= READY_STAT
| SEEK_STAT
;
3307 /* launch next transfer */
3309 s
->io_buffer_index
= 0;
3310 s
->io_buffer_size
= io
->len
;
3312 qemu_sglist_init(&s
->sg
, io
->len
/ TARGET_PAGE_SIZE
+ 1);
3313 qemu_sglist_add(&s
->sg
, io
->addr
, io
->len
);
3314 io
->addr
+= io
->len
;
3318 m
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
,
3319 pmac_ide_transfer_cb
, io
);
3321 m
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
,
3322 pmac_ide_transfer_cb
, io
);
3324 pmac_ide_transfer_cb(io
, -1);
3327 static void pmac_ide_transfer(DBDMA_io
*io
)
3329 MACIOIDEState
*m
= io
->opaque
;
3330 IDEState
*s
= idebus_active_if(&m
->bus
);
3332 s
->io_buffer_size
= 0;
3334 pmac_ide_atapi_transfer_cb(io
, 0);
3338 pmac_ide_transfer_cb(io
, 0);
3341 static void pmac_ide_flush(DBDMA_io
*io
)
3343 MACIOIDEState
*m
= io
->opaque
;
3349 /* PowerMac IDE memory IO */
3350 static void pmac_ide_writeb (void *opaque
,
3351 target_phys_addr_t addr
, uint32_t val
)
3353 MACIOIDEState
*d
= opaque
;
3355 addr
= (addr
& 0xFFF) >> 4;
3358 ide_ioport_write(&d
->bus
, addr
, val
);
3362 ide_cmd_write(&d
->bus
, 0, val
);
3369 static uint32_t pmac_ide_readb (void *opaque
,target_phys_addr_t addr
)
3372 MACIOIDEState
*d
= opaque
;
3374 addr
= (addr
& 0xFFF) >> 4;
3377 retval
= ide_ioport_read(&d
->bus
, addr
);
3381 retval
= ide_status_read(&d
->bus
, 0);
3390 static void pmac_ide_writew (void *opaque
,
3391 target_phys_addr_t addr
, uint32_t val
)
3393 MACIOIDEState
*d
= opaque
;
3395 addr
= (addr
& 0xFFF) >> 4;
3396 #ifdef TARGET_WORDS_BIGENDIAN
3400 ide_data_writew(&d
->bus
, 0, val
);
3404 static uint32_t pmac_ide_readw (void *opaque
,target_phys_addr_t addr
)
3407 MACIOIDEState
*d
= opaque
;
3409 addr
= (addr
& 0xFFF) >> 4;
3411 retval
= ide_data_readw(&d
->bus
, 0);
3415 #ifdef TARGET_WORDS_BIGENDIAN
3416 retval
= bswap16(retval
);
3421 static void pmac_ide_writel (void *opaque
,
3422 target_phys_addr_t addr
, uint32_t val
)
3424 MACIOIDEState
*d
= opaque
;
3426 addr
= (addr
& 0xFFF) >> 4;
3427 #ifdef TARGET_WORDS_BIGENDIAN
3431 ide_data_writel(&d
->bus
, 0, val
);
3435 static uint32_t pmac_ide_readl (void *opaque
,target_phys_addr_t addr
)
3438 MACIOIDEState
*d
= opaque
;
3440 addr
= (addr
& 0xFFF) >> 4;
3442 retval
= ide_data_readl(&d
->bus
, 0);
3444 retval
= 0xFFFFFFFF;
3446 #ifdef TARGET_WORDS_BIGENDIAN
3447 retval
= bswap32(retval
);
3452 static CPUWriteMemoryFunc
* const pmac_ide_write
[] = {
3458 static CPUReadMemoryFunc
* const pmac_ide_read
[] = {
3464 static void pmac_ide_save(QEMUFile
*f
, void *opaque
)
3466 MACIOIDEState
*d
= opaque
;
3469 /* per IDE interface data */
3470 idebus_save(f
, &d
->bus
);
3472 /* per IDE drive data */
3473 for(i
= 0; i
< 2; i
++) {
3474 ide_save(f
, &d
->bus
.ifs
[i
]);
3478 static int pmac_ide_load(QEMUFile
*f
, void *opaque
, int version_id
)
3480 MACIOIDEState
*d
= opaque
;
3483 if (version_id
!= 1 && version_id
!= 3)
3486 /* per IDE interface data */
3487 idebus_load(f
, &d
->bus
, version_id
);
3489 /* per IDE drive data */
3490 for(i
= 0; i
< 2; i
++) {
3491 ide_load(f
, &d
->bus
.ifs
[i
], version_id
);
3496 static void pmac_ide_reset(void *opaque
)
3498 MACIOIDEState
*d
= opaque
;
3500 ide_reset(d
->bus
.ifs
+0);
3501 ide_reset(d
->bus
.ifs
+1);
3504 /* hd_table must contain 4 block drivers */
3505 /* PowerMac uses memory mapped registers, not I/O. Return the memory
3506 I/O index to access the ide. */
3507 int pmac_ide_init (BlockDriverState
**hd_table
, qemu_irq irq
,
3508 void *dbdma
, int channel
, qemu_irq dma_irq
)
3511 int pmac_ide_memory
;
3513 d
= qemu_mallocz(sizeof(MACIOIDEState
));
3514 ide_init2(&d
->bus
, hd_table
[0], hd_table
[1], irq
);
3517 DBDMA_register_channel(dbdma
, channel
, dma_irq
, pmac_ide_transfer
, pmac_ide_flush
, d
);
3519 pmac_ide_memory
= cpu_register_io_memory(pmac_ide_read
,
3521 register_savevm("ide", 0, 3, pmac_ide_save
, pmac_ide_load
, d
);
3522 qemu_register_reset(pmac_ide_reset
, d
);
3525 return pmac_ide_memory
;
3527 #endif /* TARGET_PPC */
3529 /***********************************************************/
3530 /* MMIO based ide port
3531 * This emulates IDE device connected directly to the CPU bus without
3532 * dedicated ide controller, which is often seen on embedded boards.
3540 static uint32_t mmio_ide_read (void *opaque
, target_phys_addr_t addr
)
3542 MMIOState
*s
= (MMIOState
*)opaque
;
3543 IDEBus
*bus
= s
->bus
;
3546 return ide_ioport_read(bus
, addr
);
3548 return ide_data_readw(bus
, 0);
3551 static void mmio_ide_write (void *opaque
, target_phys_addr_t addr
,
3554 MMIOState
*s
= (MMIOState
*)opaque
;
3555 IDEBus
*bus
= s
->bus
;
3558 ide_ioport_write(bus
, addr
, val
);
3560 ide_data_writew(bus
, 0, val
);
3563 static CPUReadMemoryFunc
* const mmio_ide_reads
[] = {
3569 static CPUWriteMemoryFunc
* const mmio_ide_writes
[] = {
3575 static uint32_t mmio_ide_status_read (void *opaque
, target_phys_addr_t addr
)
3577 MMIOState
*s
= (MMIOState
*)opaque
;
3578 IDEBus
*bus
= s
->bus
;
3579 return ide_status_read(bus
, 0);
3582 static void mmio_ide_cmd_write (void *opaque
, target_phys_addr_t addr
,
3585 MMIOState
*s
= (MMIOState
*)opaque
;
3586 IDEBus
*bus
= s
->bus
;
3587 ide_cmd_write(bus
, 0, val
);
3590 static CPUReadMemoryFunc
* const mmio_ide_status
[] = {
3591 mmio_ide_status_read
,
3592 mmio_ide_status_read
,
3593 mmio_ide_status_read
,
3596 static CPUWriteMemoryFunc
* const mmio_ide_cmd
[] = {
3602 void mmio_ide_init (target_phys_addr_t membase
, target_phys_addr_t membase2
,
3603 qemu_irq irq
, int shift
,
3604 BlockDriverState
*hd0
, BlockDriverState
*hd1
)
3606 MMIOState
*s
= qemu_mallocz(sizeof(MMIOState
));
3607 IDEBus
*bus
= qemu_mallocz(sizeof(*bus
));
3610 ide_init2(bus
, hd0
, hd1
, irq
);
3615 mem1
= cpu_register_io_memory(mmio_ide_reads
, mmio_ide_writes
, s
);
3616 mem2
= cpu_register_io_memory(mmio_ide_status
, mmio_ide_cmd
, s
);
3617 cpu_register_physical_memory(membase
, 16 << shift
, mem1
);
3618 cpu_register_physical_memory(membase2
, 2 << shift
, mem2
);
3621 /***********************************************************/
3622 /* CF-ATA Microdrive */
3624 #define METADATA_SIZE 0x20
3626 /* DSCM-1XXXX Microdrive hard disk with CF+ II / PCMCIA interface. */
3629 PCMCIACardState card
;
3643 /* Register bitfields */
3646 OPT_MODE_IOMAP16
= 1,
3647 OPT_MODE_IOMAP1
= 2,
3648 OPT_MODE_IOMAP2
= 3,
3659 STAT_CHANGED
= 0x80,
3670 static inline void md_interrupt_update(MicroDriveState
*s
)
3675 qemu_set_irq(s
->card
.slot
->irq
,
3676 !(s
->stat
& STAT_INT
) && /* Inverted */
3677 !(s
->ctrl
& (CTRL_IEN
| CTRL_SRST
)) &&
3678 !(s
->opt
& OPT_SRESET
));
3681 static void md_set_irq(void *opaque
, int irq
, int level
)
3683 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3685 s
->stat
|= STAT_INT
;
3687 s
->stat
&= ~STAT_INT
;
3689 md_interrupt_update(s
);
3692 static void md_reset(MicroDriveState
*s
)
3694 s
->opt
= OPT_MODE_MMAP
;
3699 ide_reset(s
->bus
.ifs
);
3702 static uint8_t md_attr_read(void *opaque
, uint32_t at
)
3704 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3705 if (at
< s
->attr_base
) {
3706 if (at
< s
->card
.cis_len
)
3707 return s
->card
.cis
[at
];
3715 case 0x00: /* Configuration Option Register */
3717 case 0x02: /* Card Configuration Status Register */
3718 if (s
->ctrl
& CTRL_IEN
)
3719 return s
->stat
& ~STAT_INT
;
3722 case 0x04: /* Pin Replacement Register */
3723 return (s
->pins
& PINS_CRDY
) | 0x0c;
3724 case 0x06: /* Socket and Copy Register */
3728 printf("%s: Bad attribute space register %02x\n", __FUNCTION__
, at
);
3735 static void md_attr_write(void *opaque
, uint32_t at
, uint8_t value
)
3737 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3741 case 0x00: /* Configuration Option Register */
3742 s
->opt
= value
& 0xcf;
3743 if (value
& OPT_SRESET
)
3745 md_interrupt_update(s
);
3747 case 0x02: /* Card Configuration Status Register */
3748 if ((s
->stat
^ value
) & STAT_PWRDWN
)
3749 s
->pins
|= PINS_CRDY
;
3751 s
->stat
|= value
& 0x74;
3752 md_interrupt_update(s
);
3753 /* Word 170 in Identify Device must be equal to STAT_XE */
3755 case 0x04: /* Pin Replacement Register */
3756 s
->pins
&= PINS_CRDY
;
3757 s
->pins
|= value
& PINS_MRDY
;
3759 case 0x06: /* Socket and Copy Register */
3762 printf("%s: Bad attribute space register %02x\n", __FUNCTION__
, at
);
3766 static uint16_t md_common_read(void *opaque
, uint32_t at
)
3768 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3773 switch (s
->opt
& OPT_MODE
) {
3775 if ((at
& ~0x3ff) == 0x400)
3778 case OPT_MODE_IOMAP16
:
3781 case OPT_MODE_IOMAP1
:
3782 if ((at
& ~0xf) == 0x3f0)
3784 else if ((at
& ~0xf) == 0x1f0)
3787 case OPT_MODE_IOMAP2
:
3788 if ((at
& ~0xf) == 0x370)
3790 else if ((at
& ~0xf) == 0x170)
3795 case 0x0: /* Even RD Data */
3797 return ide_data_readw(&s
->bus
, 0);
3799 /* TODO: 8-bit accesses */
3803 s
->io
= ide_data_readw(&s
->bus
, 0);
3806 s
->cycle
= !s
->cycle
;
3808 case 0x9: /* Odd RD Data */
3810 case 0xd: /* Error */
3811 return ide_ioport_read(&s
->bus
, 0x1);
3812 case 0xe: /* Alternate Status */
3813 ifs
= idebus_active_if(&s
->bus
);
3818 case 0xf: /* Device Address */
3819 ifs
= idebus_active_if(&s
->bus
);
3820 return 0xc2 | ((~ifs
->select
<< 2) & 0x3c);
3822 return ide_ioport_read(&s
->bus
, at
);
3828 static void md_common_write(void *opaque
, uint32_t at
, uint16_t value
)
3830 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3833 switch (s
->opt
& OPT_MODE
) {
3835 if ((at
& ~0x3ff) == 0x400)
3838 case OPT_MODE_IOMAP16
:
3841 case OPT_MODE_IOMAP1
:
3842 if ((at
& ~0xf) == 0x3f0)
3844 else if ((at
& ~0xf) == 0x1f0)
3847 case OPT_MODE_IOMAP2
:
3848 if ((at
& ~0xf) == 0x370)
3850 else if ((at
& ~0xf) == 0x170)
3855 case 0x0: /* Even WR Data */
3857 ide_data_writew(&s
->bus
, 0, value
);
3860 /* TODO: 8-bit accesses */
3862 ide_data_writew(&s
->bus
, 0, s
->io
| (value
<< 8));
3864 s
->io
= value
& 0xff;
3865 s
->cycle
= !s
->cycle
;
3868 s
->io
= value
& 0xff;
3869 s
->cycle
= !s
->cycle
;
3871 case 0xd: /* Features */
3872 ide_ioport_write(&s
->bus
, 0x1, value
);
3874 case 0xe: /* Device Control */
3876 if (value
& CTRL_SRST
)
3878 md_interrupt_update(s
);
3881 if (s
->stat
& STAT_PWRDWN
) {
3882 s
->pins
|= PINS_CRDY
;
3883 s
->stat
&= ~STAT_PWRDWN
;
3885 ide_ioport_write(&s
->bus
, at
, value
);
3889 static void md_save(QEMUFile
*f
, void *opaque
)
3891 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3894 qemu_put_8s(f
, &s
->opt
);
3895 qemu_put_8s(f
, &s
->stat
);
3896 qemu_put_8s(f
, &s
->pins
);
3898 qemu_put_8s(f
, &s
->ctrl
);
3899 qemu_put_be16s(f
, &s
->io
);
3900 qemu_put_byte(f
, s
->cycle
);
3902 idebus_save(f
, &s
->bus
);
3904 for (i
= 0; i
< 2; i
++)
3905 ide_save(f
, &s
->bus
.ifs
[i
]);
3908 static int md_load(QEMUFile
*f
, void *opaque
, int version_id
)
3910 MicroDriveState
*s
= (MicroDriveState
*) opaque
;
3913 if (version_id
!= 0 && version_id
!= 3)
3916 qemu_get_8s(f
, &s
->opt
);
3917 qemu_get_8s(f
, &s
->stat
);
3918 qemu_get_8s(f
, &s
->pins
);
3920 qemu_get_8s(f
, &s
->ctrl
);
3921 qemu_get_be16s(f
, &s
->io
);
3922 s
->cycle
= qemu_get_byte(f
);
3924 idebus_load(f
, &s
->bus
, version_id
);
3926 for (i
= 0; i
< 2; i
++)
3927 ide_load(f
, &s
->bus
.ifs
[i
], version_id
);
3932 static const uint8_t dscm1xxxx_cis
[0x14a] = {
3933 [0x000] = CISTPL_DEVICE
, /* 5V Device Information */
3934 [0x002] = 0x03, /* Tuple length = 4 bytes */
3935 [0x004] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
3936 [0x006] = 0x01, /* Size = 2K bytes */
3937 [0x008] = CISTPL_ENDMARK
,
3939 [0x00a] = CISTPL_DEVICE_OC
, /* Additional Device Information */
3940 [0x00c] = 0x04, /* Tuple length = 4 byest */
3941 [0x00e] = 0x03, /* Conditions: Ext = 0, Vcc 3.3V, MWAIT = 1 */
3942 [0x010] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
3943 [0x012] = 0x01, /* Size = 2K bytes */
3944 [0x014] = CISTPL_ENDMARK
,
3946 [0x016] = CISTPL_JEDEC_C
, /* JEDEC ID */
3947 [0x018] = 0x02, /* Tuple length = 2 bytes */
3948 [0x01a] = 0xdf, /* PC Card ATA with no Vpp required */
3951 [0x01e] = CISTPL_MANFID
, /* Manufacture ID */
3952 [0x020] = 0x04, /* Tuple length = 4 bytes */
3953 [0x022] = 0xa4, /* TPLMID_MANF = 00a4 (IBM) */
3955 [0x026] = 0x00, /* PLMID_CARD = 0000 */
3958 [0x02a] = CISTPL_VERS_1
, /* Level 1 Version */
3959 [0x02c] = 0x12, /* Tuple length = 23 bytes */
3960 [0x02e] = 0x04, /* Major Version = JEIDA 4.2 / PCMCIA 2.1 */
3961 [0x030] = 0x01, /* Minor Version = 1 */
3977 [0x050] = CISTPL_ENDMARK
,
3979 [0x052] = CISTPL_FUNCID
, /* Function ID */
3980 [0x054] = 0x02, /* Tuple length = 2 bytes */
3981 [0x056] = 0x04, /* TPLFID_FUNCTION = Fixed Disk */
3982 [0x058] = 0x01, /* TPLFID_SYSINIT: POST = 1, ROM = 0 */
3984 [0x05a] = CISTPL_FUNCE
, /* Function Extension */
3985 [0x05c] = 0x02, /* Tuple length = 2 bytes */
3986 [0x05e] = 0x01, /* TPLFE_TYPE = Disk Device Interface */
3987 [0x060] = 0x01, /* TPLFE_DATA = PC Card ATA Interface */
3989 [0x062] = CISTPL_FUNCE
, /* Function Extension */
3990 [0x064] = 0x03, /* Tuple length = 3 bytes */
3991 [0x066] = 0x02, /* TPLFE_TYPE = Basic PC Card ATA Interface */
3992 [0x068] = 0x08, /* TPLFE_DATA: Rotating, Unique, Single */
3993 [0x06a] = 0x0f, /* TPLFE_DATA: Sleep, Standby, Idle, Auto */
3995 [0x06c] = CISTPL_CONFIG
, /* Configuration */
3996 [0x06e] = 0x05, /* Tuple length = 5 bytes */
3997 [0x070] = 0x01, /* TPCC_RASZ = 2 bytes, TPCC_RMSZ = 1 byte */
3998 [0x072] = 0x07, /* TPCC_LAST = 7 */
3999 [0x074] = 0x00, /* TPCC_RADR = 0200 */
4001 [0x078] = 0x0f, /* TPCC_RMSK = 200, 202, 204, 206 */
4003 [0x07a] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4004 [0x07c] = 0x0b, /* Tuple length = 11 bytes */
4005 [0x07e] = 0xc0, /* TPCE_INDX = Memory Mode, Default, Iface */
4006 [0x080] = 0xc0, /* TPCE_IF = Memory, no BVDs, no WP, READY */
4007 [0x082] = 0xa1, /* TPCE_FS = Vcc only, no I/O, Memory, Misc */
4008 [0x084] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
4009 [0x086] = 0x55, /* NomV: 5.0 V */
4010 [0x088] = 0x4d, /* MinV: 4.5 V */
4011 [0x08a] = 0x5d, /* MaxV: 5.5 V */
4012 [0x08c] = 0x4e, /* Peakl: 450 mA */
4013 [0x08e] = 0x08, /* TPCE_MS = 1 window, 1 byte, Host address */
4014 [0x090] = 0x00, /* Window descriptor: Window length = 0 */
4015 [0x092] = 0x20, /* TPCE_MI: support power down mode, RW */
4017 [0x094] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4018 [0x096] = 0x06, /* Tuple length = 6 bytes */
4019 [0x098] = 0x00, /* TPCE_INDX = Memory Mode, no Default */
4020 [0x09a] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
4021 [0x09c] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
4022 [0x09e] = 0xb5, /* NomV: 3.3 V */
4024 [0x0a2] = 0x3e, /* Peakl: 350 mA */
4026 [0x0a4] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4027 [0x0a6] = 0x0d, /* Tuple length = 13 bytes */
4028 [0x0a8] = 0xc1, /* TPCE_INDX = I/O and Memory Mode, Default */
4029 [0x0aa] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
4030 [0x0ac] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
4031 [0x0ae] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
4032 [0x0b0] = 0x55, /* NomV: 5.0 V */
4033 [0x0b2] = 0x4d, /* MinV: 4.5 V */
4034 [0x0b4] = 0x5d, /* MaxV: 5.5 V */
4035 [0x0b6] = 0x4e, /* Peakl: 450 mA */
4036 [0x0b8] = 0x64, /* TPCE_IO = 16-byte boundary, 16/8 accesses */
4037 [0x0ba] = 0xf0, /* TPCE_IR = MASK, Level, Pulse, Share */
4038 [0x0bc] = 0xff, /* IRQ0..IRQ7 supported */
4039 [0x0be] = 0xff, /* IRQ8..IRQ15 supported */
4040 [0x0c0] = 0x20, /* TPCE_MI = support power down mode */
4042 [0x0c2] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4043 [0x0c4] = 0x06, /* Tuple length = 6 bytes */
4044 [0x0c6] = 0x01, /* TPCE_INDX = I/O and Memory Mode */
4045 [0x0c8] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
4046 [0x0ca] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
4047 [0x0cc] = 0xb5, /* NomV: 3.3 V */
4049 [0x0d0] = 0x3e, /* Peakl: 350 mA */
4051 [0x0d2] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4052 [0x0d4] = 0x12, /* Tuple length = 18 bytes */
4053 [0x0d6] = 0xc2, /* TPCE_INDX = I/O Primary Mode */
4054 [0x0d8] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
4055 [0x0da] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
4056 [0x0dc] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
4057 [0x0de] = 0x55, /* NomV: 5.0 V */
4058 [0x0e0] = 0x4d, /* MinV: 4.5 V */
4059 [0x0e2] = 0x5d, /* MaxV: 5.5 V */
4060 [0x0e4] = 0x4e, /* Peakl: 450 mA */
4061 [0x0e6] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
4062 [0x0e8] = 0x61, /* Range: 2 fields, 2 bytes addr, 1 byte len */
4063 [0x0ea] = 0xf0, /* Field 1 address = 0x01f0 */
4065 [0x0ee] = 0x07, /* Address block length = 8 */
4066 [0x0f0] = 0xf6, /* Field 2 address = 0x03f6 */
4068 [0x0f4] = 0x01, /* Address block length = 2 */
4069 [0x0f6] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
4070 [0x0f8] = 0x20, /* TPCE_MI = support power down mode */
4072 [0x0fa] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4073 [0x0fc] = 0x06, /* Tuple length = 6 bytes */
4074 [0x0fe] = 0x02, /* TPCE_INDX = I/O Primary Mode, no Default */
4075 [0x100] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
4076 [0x102] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
4077 [0x104] = 0xb5, /* NomV: 3.3 V */
4079 [0x108] = 0x3e, /* Peakl: 350 mA */
4081 [0x10a] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4082 [0x10c] = 0x12, /* Tuple length = 18 bytes */
4083 [0x10e] = 0xc3, /* TPCE_INDX = I/O Secondary Mode, Default */
4084 [0x110] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
4085 [0x112] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
4086 [0x114] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
4087 [0x116] = 0x55, /* NomV: 5.0 V */
4088 [0x118] = 0x4d, /* MinV: 4.5 V */
4089 [0x11a] = 0x5d, /* MaxV: 5.5 V */
4090 [0x11c] = 0x4e, /* Peakl: 450 mA */
4091 [0x11e] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
4092 [0x120] = 0x61, /* Range: 2 fields, 2 byte addr, 1 byte len */
4093 [0x122] = 0x70, /* Field 1 address = 0x0170 */
4095 [0x126] = 0x07, /* Address block length = 8 */
4096 [0x128] = 0x76, /* Field 2 address = 0x0376 */
4098 [0x12c] = 0x01, /* Address block length = 2 */
4099 [0x12e] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
4100 [0x130] = 0x20, /* TPCE_MI = support power down mode */
4102 [0x132] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
4103 [0x134] = 0x06, /* Tuple length = 6 bytes */
4104 [0x136] = 0x03, /* TPCE_INDX = I/O Secondary Mode */
4105 [0x138] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
4106 [0x13a] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
4107 [0x13c] = 0xb5, /* NomV: 3.3 V */
4109 [0x140] = 0x3e, /* Peakl: 350 mA */
4111 [0x142] = CISTPL_NO_LINK
, /* No Link */
4112 [0x144] = 0x00, /* Tuple length = 0 bytes */
4114 [0x146] = CISTPL_END
, /* Tuple End */
4117 static int dscm1xxxx_attach(void *opaque
)
4119 MicroDriveState
*md
= (MicroDriveState
*) opaque
;
4120 md
->card
.attr_read
= md_attr_read
;
4121 md
->card
.attr_write
= md_attr_write
;
4122 md
->card
.common_read
= md_common_read
;
4123 md
->card
.common_write
= md_common_write
;
4124 md
->card
.io_read
= md_common_read
;
4125 md
->card
.io_write
= md_common_write
;
4127 md
->attr_base
= md
->card
.cis
[0x74] | (md
->card
.cis
[0x76] << 8);
4131 md_interrupt_update(md
);
4133 md
->card
.slot
->card_string
= "DSCM-1xxxx Hitachi Microdrive";
4137 static int dscm1xxxx_detach(void *opaque
)
4139 MicroDriveState
*md
= (MicroDriveState
*) opaque
;
4144 PCMCIACardState
*dscm1xxxx_init(BlockDriverState
*bdrv
)
4146 MicroDriveState
*md
= (MicroDriveState
*) qemu_mallocz(sizeof(MicroDriveState
));
4147 md
->card
.state
= md
;
4148 md
->card
.attach
= dscm1xxxx_attach
;
4149 md
->card
.detach
= dscm1xxxx_detach
;
4150 md
->card
.cis
= dscm1xxxx_cis
;
4151 md
->card
.cis_len
= sizeof(dscm1xxxx_cis
);
4153 ide_init2(&md
->bus
, bdrv
, NULL
, qemu_allocate_irqs(md_set_irq
, md
, 1)[0]);
4154 md
->bus
.ifs
[0].is_cf
= 1;
4155 md
->bus
.ifs
[0].mdata_size
= METADATA_SIZE
;
4156 md
->bus
.ifs
[0].mdata_storage
= (uint8_t *) qemu_mallocz(METADATA_SIZE
);
4158 register_savevm("microdrive", -1, 3, md_save
, md_load
, md
);