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 <hw/scsi-disk.h>
31 #include "block_int.h"
32 #include "qemu-timer.h"
36 #include <hw/ide/internal.h>
38 static int smart_attributes
[][5] = {
39 /* id, flags, val, wrst, thrsh */
40 { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
41 { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
42 { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
43 { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
44 { 0x00, 0x00, 0x00, 0x00, 0x00}
47 /* XXX: DVDs that could fit on a CD will be reported as a CD */
48 static inline int media_present(IDEState
*s
)
50 return (s
->nb_sectors
> 0);
53 static inline int media_is_dvd(IDEState
*s
)
55 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
58 static inline int media_is_cd(IDEState
*s
)
60 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
63 static void ide_dma_start(IDEState
*s
, BlockDriverCompletionFunc
*dma_cb
);
64 static void ide_dma_restart(IDEState
*s
);
65 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
67 static void padstr(char *str
, const char *src
, int len
)
70 for(i
= 0; i
< len
; i
++) {
79 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
82 for(i
= 0; i
< buf_size
; i
++) {
90 static void put_le16(uint16_t *p
, unsigned int v
)
95 static void ide_identify(IDEState
*s
)
100 if (s
->identify_set
) {
101 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
105 memset(s
->io_buffer
, 0, 512);
106 p
= (uint16_t *)s
->io_buffer
;
107 put_le16(p
+ 0, 0x0040);
108 put_le16(p
+ 1, s
->cylinders
);
109 put_le16(p
+ 3, s
->heads
);
110 put_le16(p
+ 4, 512 * s
->sectors
); /* XXX: retired, remove ? */
111 put_le16(p
+ 5, 512); /* XXX: retired, remove ? */
112 put_le16(p
+ 6, s
->sectors
);
113 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
114 put_le16(p
+ 20, 3); /* XXX: retired, remove ? */
115 put_le16(p
+ 21, 512); /* cache size in sectors */
116 put_le16(p
+ 22, 4); /* ecc bytes */
117 padstr((char *)(p
+ 23), QEMU_VERSION
, 8); /* firmware version */
118 padstr((char *)(p
+ 27), "QEMU HARDDISK", 40); /* model */
119 #if MAX_MULT_SECTORS > 1
120 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
122 put_le16(p
+ 48, 1); /* dword I/O */
123 put_le16(p
+ 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
124 put_le16(p
+ 51, 0x200); /* PIO transfer cycle */
125 put_le16(p
+ 52, 0x200); /* DMA transfer cycle */
126 put_le16(p
+ 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
127 put_le16(p
+ 54, s
->cylinders
);
128 put_le16(p
+ 55, s
->heads
);
129 put_le16(p
+ 56, s
->sectors
);
130 oldsize
= s
->cylinders
* s
->heads
* s
->sectors
;
131 put_le16(p
+ 57, oldsize
);
132 put_le16(p
+ 58, oldsize
>> 16);
134 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
135 put_le16(p
+ 60, s
->nb_sectors
);
136 put_le16(p
+ 61, s
->nb_sectors
>> 16);
137 put_le16(p
+ 62, 0x07); /* single word dma0-2 supported */
138 put_le16(p
+ 63, 0x07); /* mdma0-2 supported */
139 put_le16(p
+ 65, 120);
140 put_le16(p
+ 66, 120);
141 put_le16(p
+ 67, 120);
142 put_le16(p
+ 68, 120);
143 put_le16(p
+ 80, 0xf0); /* ata3 -> ata6 supported */
144 put_le16(p
+ 81, 0x16); /* conforms to ata5 */
145 /* 14=NOP supported, 0=SMART supported */
146 put_le16(p
+ 82, (1 << 14) | 1);
147 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
148 put_le16(p
+ 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
149 /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
150 put_le16(p
+ 84, (1 << 14) | 0);
151 /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
152 if (bdrv_enable_write_cache(s
->bs
))
153 put_le16(p
+ 85, (1 << 14) | (1 << 5) | 1);
155 put_le16(p
+ 85, (1 << 14) | 1);
156 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
157 put_le16(p
+ 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
158 /* 14=set to 1, 1=smart self test, 0=smart error logging */
159 put_le16(p
+ 87, (1 << 14) | 0);
160 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
161 put_le16(p
+ 93, 1 | (1 << 14) | 0x2000);
162 put_le16(p
+ 100, s
->nb_sectors
);
163 put_le16(p
+ 101, s
->nb_sectors
>> 16);
164 put_le16(p
+ 102, s
->nb_sectors
>> 32);
165 put_le16(p
+ 103, s
->nb_sectors
>> 48);
167 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
171 static void ide_atapi_identify(IDEState
*s
)
175 if (s
->identify_set
) {
176 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
180 memset(s
->io_buffer
, 0, 512);
181 p
= (uint16_t *)s
->io_buffer
;
182 /* Removable CDROM, 50us response, 12 byte packets */
183 put_le16(p
+ 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
184 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
185 put_le16(p
+ 20, 3); /* buffer type */
186 put_le16(p
+ 21, 512); /* cache size in sectors */
187 put_le16(p
+ 22, 4); /* ecc bytes */
188 padstr((char *)(p
+ 23), QEMU_VERSION
, 8); /* firmware version */
189 padstr((char *)(p
+ 27), "QEMU DVD-ROM", 40); /* model */
190 put_le16(p
+ 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
192 put_le16(p
+ 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
193 put_le16(p
+ 53, 7); /* words 64-70, 54-58, 88 valid */
194 put_le16(p
+ 62, 7); /* single word dma0-2 supported */
195 put_le16(p
+ 63, 7); /* mdma0-2 supported */
196 put_le16(p
+ 64, 0x3f); /* PIO modes supported */
198 put_le16(p
+ 49, 1 << 9); /* LBA supported, no DMA */
199 put_le16(p
+ 53, 3); /* words 64-70, 54-58 valid */
200 put_le16(p
+ 63, 0x103); /* DMA modes XXX: may be incorrect */
201 put_le16(p
+ 64, 1); /* PIO modes */
203 put_le16(p
+ 65, 0xb4); /* minimum DMA multiword tx cycle time */
204 put_le16(p
+ 66, 0xb4); /* recommended DMA multiword tx cycle time */
205 put_le16(p
+ 67, 0x12c); /* minimum PIO cycle time without flow control */
206 put_le16(p
+ 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
208 put_le16(p
+ 71, 30); /* in ns */
209 put_le16(p
+ 72, 30); /* in ns */
211 put_le16(p
+ 80, 0x1e); /* support up to ATA/ATAPI-4 */
213 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
215 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
219 static void ide_cfata_identify(IDEState
*s
)
224 p
= (uint16_t *) s
->identify_data
;
228 memset(p
, 0, sizeof(s
->identify_data
));
230 cur_sec
= s
->cylinders
* s
->heads
* s
->sectors
;
232 put_le16(p
+ 0, 0x848a); /* CF Storage Card signature */
233 put_le16(p
+ 1, s
->cylinders
); /* Default cylinders */
234 put_le16(p
+ 3, s
->heads
); /* Default heads */
235 put_le16(p
+ 6, s
->sectors
); /* Default sectors per track */
236 put_le16(p
+ 7, s
->nb_sectors
>> 16); /* Sectors per card */
237 put_le16(p
+ 8, s
->nb_sectors
); /* Sectors per card */
238 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
239 put_le16(p
+ 22, 0x0004); /* ECC bytes */
240 padstr((char *) (p
+ 23), QEMU_VERSION
, 8); /* Firmware Revision */
241 padstr((char *) (p
+ 27), "QEMU MICRODRIVE", 40);/* Model number */
242 #if MAX_MULT_SECTORS > 1
243 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
245 put_le16(p
+ 47, 0x0000);
247 put_le16(p
+ 49, 0x0f00); /* Capabilities */
248 put_le16(p
+ 51, 0x0002); /* PIO cycle timing mode */
249 put_le16(p
+ 52, 0x0001); /* DMA cycle timing mode */
250 put_le16(p
+ 53, 0x0003); /* Translation params valid */
251 put_le16(p
+ 54, s
->cylinders
); /* Current cylinders */
252 put_le16(p
+ 55, s
->heads
); /* Current heads */
253 put_le16(p
+ 56, s
->sectors
); /* Current sectors */
254 put_le16(p
+ 57, cur_sec
); /* Current capacity */
255 put_le16(p
+ 58, cur_sec
>> 16); /* Current capacity */
256 if (s
->mult_sectors
) /* Multiple sector setting */
257 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
258 put_le16(p
+ 60, s
->nb_sectors
); /* Total LBA sectors */
259 put_le16(p
+ 61, s
->nb_sectors
>> 16); /* Total LBA sectors */
260 put_le16(p
+ 63, 0x0203); /* Multiword DMA capability */
261 put_le16(p
+ 64, 0x0001); /* Flow Control PIO support */
262 put_le16(p
+ 65, 0x0096); /* Min. Multiword DMA cycle */
263 put_le16(p
+ 66, 0x0096); /* Rec. Multiword DMA cycle */
264 put_le16(p
+ 68, 0x00b4); /* Min. PIO cycle time */
265 put_le16(p
+ 82, 0x400c); /* Command Set supported */
266 put_le16(p
+ 83, 0x7068); /* Command Set supported */
267 put_le16(p
+ 84, 0x4000); /* Features supported */
268 put_le16(p
+ 85, 0x000c); /* Command Set enabled */
269 put_le16(p
+ 86, 0x7044); /* Command Set enabled */
270 put_le16(p
+ 87, 0x4000); /* Features enabled */
271 put_le16(p
+ 91, 0x4060); /* Current APM level */
272 put_le16(p
+ 129, 0x0002); /* Current features option */
273 put_le16(p
+ 130, 0x0005); /* Reassigned sectors */
274 put_le16(p
+ 131, 0x0001); /* Initial power mode */
275 put_le16(p
+ 132, 0x0000); /* User signature */
276 put_le16(p
+ 160, 0x8100); /* Power requirement */
277 put_le16(p
+ 161, 0x8001); /* CF command set */
282 memcpy(s
->io_buffer
, p
, sizeof(s
->identify_data
));
285 static void ide_set_signature(IDEState
*s
)
287 s
->select
&= 0xf0; /* clear head */
303 static inline void ide_abort_command(IDEState
*s
)
305 s
->status
= READY_STAT
| ERR_STAT
;
309 static inline void ide_dma_submit_check(IDEState
*s
,
310 BlockDriverCompletionFunc
*dma_cb
, BMDMAState
*bm
)
317 /* prepare data transfer and tell what to do after */
318 static void ide_transfer_start(IDEState
*s
, uint8_t *buf
, int size
,
319 EndTransferFunc
*end_transfer_func
)
321 s
->end_transfer_func
= end_transfer_func
;
323 s
->data_end
= buf
+ size
;
324 if (!(s
->status
& ERR_STAT
))
325 s
->status
|= DRQ_STAT
;
328 static void ide_transfer_stop(IDEState
*s
)
330 s
->end_transfer_func
= ide_transfer_stop
;
331 s
->data_ptr
= s
->io_buffer
;
332 s
->data_end
= s
->io_buffer
;
333 s
->status
&= ~DRQ_STAT
;
336 int64_t ide_get_sector(IDEState
*s
)
339 if (s
->select
& 0x40) {
342 sector_num
= ((s
->select
& 0x0f) << 24) | (s
->hcyl
<< 16) |
343 (s
->lcyl
<< 8) | s
->sector
;
345 sector_num
= ((int64_t)s
->hob_hcyl
<< 40) |
346 ((int64_t) s
->hob_lcyl
<< 32) |
347 ((int64_t) s
->hob_sector
<< 24) |
348 ((int64_t) s
->hcyl
<< 16) |
349 ((int64_t) s
->lcyl
<< 8) | s
->sector
;
352 sector_num
= ((s
->hcyl
<< 8) | s
->lcyl
) * s
->heads
* s
->sectors
+
353 (s
->select
& 0x0f) * s
->sectors
+ (s
->sector
- 1);
358 void ide_set_sector(IDEState
*s
, int64_t sector_num
)
361 if (s
->select
& 0x40) {
363 s
->select
= (s
->select
& 0xf0) | (sector_num
>> 24);
364 s
->hcyl
= (sector_num
>> 16);
365 s
->lcyl
= (sector_num
>> 8);
366 s
->sector
= (sector_num
);
368 s
->sector
= sector_num
;
369 s
->lcyl
= sector_num
>> 8;
370 s
->hcyl
= sector_num
>> 16;
371 s
->hob_sector
= sector_num
>> 24;
372 s
->hob_lcyl
= sector_num
>> 32;
373 s
->hob_hcyl
= sector_num
>> 40;
376 cyl
= sector_num
/ (s
->heads
* s
->sectors
);
377 r
= sector_num
% (s
->heads
* s
->sectors
);
380 s
->select
= (s
->select
& 0xf0) | ((r
/ s
->sectors
) & 0x0f);
381 s
->sector
= (r
% s
->sectors
) + 1;
385 static void ide_rw_error(IDEState
*s
) {
386 ide_abort_command(s
);
390 static void ide_sector_read(IDEState
*s
)
395 s
->status
= READY_STAT
| SEEK_STAT
;
396 s
->error
= 0; /* not needed by IDE spec, but needed by Windows */
397 sector_num
= ide_get_sector(s
);
400 /* no more sector to read from disk */
401 ide_transfer_stop(s
);
403 #if defined(DEBUG_IDE)
404 printf("read sector=%" PRId64
"\n", sector_num
);
406 if (n
> s
->req_nb_sectors
)
407 n
= s
->req_nb_sectors
;
408 ret
= bdrv_read(s
->bs
, sector_num
, s
->io_buffer
, n
);
413 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_read
);
415 ide_set_sector(s
, sector_num
+ n
);
421 /* return 0 if buffer completed */
422 static int dma_buf_prepare(BMDMAState
*bm
, int is_write
)
424 IDEState
*s
= bmdma_active_if(bm
);
431 qemu_sglist_init(&s
->sg
, s
->nsector
/ (TARGET_PAGE_SIZE
/512) + 1);
432 s
->io_buffer_size
= 0;
434 if (bm
->cur_prd_len
== 0) {
435 /* end of table (with a fail safe of one page) */
436 if (bm
->cur_prd_last
||
437 (bm
->cur_addr
- bm
->addr
) >= 4096)
438 return s
->io_buffer_size
!= 0;
439 cpu_physical_memory_read(bm
->cur_addr
, (uint8_t *)&prd
, 8);
441 prd
.addr
= le32_to_cpu(prd
.addr
);
442 prd
.size
= le32_to_cpu(prd
.size
);
443 len
= prd
.size
& 0xfffe;
446 bm
->cur_prd_len
= len
;
447 bm
->cur_prd_addr
= prd
.addr
;
448 bm
->cur_prd_last
= (prd
.size
& 0x80000000);
452 qemu_sglist_add(&s
->sg
, bm
->cur_prd_addr
, l
);
453 bm
->cur_prd_addr
+= l
;
454 bm
->cur_prd_len
-= l
;
455 s
->io_buffer_size
+= l
;
461 static void dma_buf_commit(IDEState
*s
, int is_write
)
463 qemu_sglist_destroy(&s
->sg
);
466 void ide_dma_error(IDEState
*s
)
468 ide_transfer_stop(s
);
470 s
->status
= READY_STAT
| ERR_STAT
;
474 static int ide_handle_write_error(IDEState
*s
, int error
, int op
)
476 BlockInterfaceErrorAction action
= drive_get_onerror(s
->bs
);
478 if (action
== BLOCK_ERR_IGNORE
)
481 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
482 || action
== BLOCK_ERR_STOP_ANY
) {
483 s
->bus
->bmdma
->unit
= s
->unit
;
484 s
->bus
->bmdma
->status
|= op
;
487 if (op
== BM_STATUS_DMA_RETRY
) {
488 dma_buf_commit(s
, 0);
498 /* return 0 if buffer completed */
499 static int dma_buf_rw(BMDMAState
*bm
, int is_write
)
501 IDEState
*s
= bmdma_active_if(bm
);
509 l
= s
->io_buffer_size
- s
->io_buffer_index
;
512 if (bm
->cur_prd_len
== 0) {
513 /* end of table (with a fail safe of one page) */
514 if (bm
->cur_prd_last
||
515 (bm
->cur_addr
- bm
->addr
) >= 4096)
517 cpu_physical_memory_read(bm
->cur_addr
, (uint8_t *)&prd
, 8);
519 prd
.addr
= le32_to_cpu(prd
.addr
);
520 prd
.size
= le32_to_cpu(prd
.size
);
521 len
= prd
.size
& 0xfffe;
524 bm
->cur_prd_len
= len
;
525 bm
->cur_prd_addr
= prd
.addr
;
526 bm
->cur_prd_last
= (prd
.size
& 0x80000000);
528 if (l
> bm
->cur_prd_len
)
532 cpu_physical_memory_write(bm
->cur_prd_addr
,
533 s
->io_buffer
+ s
->io_buffer_index
, l
);
535 cpu_physical_memory_read(bm
->cur_prd_addr
,
536 s
->io_buffer
+ s
->io_buffer_index
, l
);
538 bm
->cur_prd_addr
+= l
;
539 bm
->cur_prd_len
-= l
;
540 s
->io_buffer_index
+= l
;
546 static void ide_read_dma_cb(void *opaque
, int ret
)
548 BMDMAState
*bm
= opaque
;
549 IDEState
*s
= bmdma_active_if(bm
);
554 dma_buf_commit(s
, 1);
559 n
= s
->io_buffer_size
>> 9;
560 sector_num
= ide_get_sector(s
);
562 dma_buf_commit(s
, 1);
564 ide_set_sector(s
, sector_num
);
568 /* end of transfer ? */
569 if (s
->nsector
== 0) {
570 s
->status
= READY_STAT
| SEEK_STAT
;
573 bm
->status
&= ~BM_STATUS_DMAING
;
574 bm
->status
|= BM_STATUS_INT
;
581 /* launch next transfer */
583 s
->io_buffer_index
= 0;
584 s
->io_buffer_size
= n
* 512;
585 if (dma_buf_prepare(bm
, 1) == 0)
588 printf("aio_read: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
590 bm
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
, ide_read_dma_cb
, bm
);
591 ide_dma_submit_check(s
, ide_read_dma_cb
, bm
);
594 static void ide_sector_read_dma(IDEState
*s
)
596 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
597 s
->io_buffer_index
= 0;
598 s
->io_buffer_size
= 0;
600 ide_dma_start(s
, ide_read_dma_cb
);
603 static void ide_sector_write_timer_cb(void *opaque
)
605 IDEState
*s
= opaque
;
609 static void ide_sector_write(IDEState
*s
)
614 s
->status
= READY_STAT
| SEEK_STAT
;
615 sector_num
= ide_get_sector(s
);
616 #if defined(DEBUG_IDE)
617 printf("write sector=%" PRId64
"\n", sector_num
);
620 if (n
> s
->req_nb_sectors
)
621 n
= s
->req_nb_sectors
;
622 ret
= bdrv_write(s
->bs
, sector_num
, s
->io_buffer
, n
);
625 if (ide_handle_write_error(s
, -ret
, BM_STATUS_PIO_RETRY
))
630 if (s
->nsector
== 0) {
631 /* no more sectors to write */
632 ide_transfer_stop(s
);
635 if (n1
> s
->req_nb_sectors
)
636 n1
= s
->req_nb_sectors
;
637 ide_transfer_start(s
, s
->io_buffer
, 512 * n1
, ide_sector_write
);
639 ide_set_sector(s
, sector_num
+ n
);
642 if (win2k_install_hack
&& ((++s
->irq_count
% 16) == 0)) {
643 /* It seems there is a bug in the Windows 2000 installer HDD
644 IDE driver which fills the disk with empty logs when the
645 IDE write IRQ comes too early. This hack tries to correct
646 that at the expense of slower write performances. Use this
647 option _only_ to install Windows 2000. You must disable it
649 qemu_mod_timer(s
->sector_write_timer
,
650 qemu_get_clock(vm_clock
) + (get_ticks_per_sec() / 1000));
658 static void ide_dma_restart_bh(void *opaque
)
660 BMDMAState
*bm
= opaque
;
662 qemu_bh_delete(bm
->bh
);
665 if (bm
->status
& BM_STATUS_DMA_RETRY
) {
666 bm
->status
&= ~BM_STATUS_DMA_RETRY
;
667 ide_dma_restart(bmdma_active_if(bm
));
668 } else if (bm
->status
& BM_STATUS_PIO_RETRY
) {
669 bm
->status
&= ~BM_STATUS_PIO_RETRY
;
670 ide_sector_write(bmdma_active_if(bm
));
674 void ide_dma_restart_cb(void *opaque
, int running
, int reason
)
676 BMDMAState
*bm
= opaque
;
682 bm
->bh
= qemu_bh_new(ide_dma_restart_bh
, bm
);
683 qemu_bh_schedule(bm
->bh
);
687 static void ide_write_dma_cb(void *opaque
, int ret
)
689 BMDMAState
*bm
= opaque
;
690 IDEState
*s
= bmdma_active_if(bm
);
695 if (ide_handle_write_error(s
, -ret
, BM_STATUS_DMA_RETRY
))
699 n
= s
->io_buffer_size
>> 9;
700 sector_num
= ide_get_sector(s
);
702 dma_buf_commit(s
, 0);
704 ide_set_sector(s
, sector_num
);
708 /* end of transfer ? */
709 if (s
->nsector
== 0) {
710 s
->status
= READY_STAT
| SEEK_STAT
;
713 bm
->status
&= ~BM_STATUS_DMAING
;
714 bm
->status
|= BM_STATUS_INT
;
722 s
->io_buffer_size
= n
* 512;
723 /* launch next transfer */
724 if (dma_buf_prepare(bm
, 0) == 0)
727 printf("aio_write: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
729 bm
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
, ide_write_dma_cb
, bm
);
730 ide_dma_submit_check(s
, ide_write_dma_cb
, bm
);
733 static void ide_sector_write_dma(IDEState
*s
)
735 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
736 s
->io_buffer_index
= 0;
737 s
->io_buffer_size
= 0;
739 ide_dma_start(s
, ide_write_dma_cb
);
742 void ide_atapi_cmd_ok(IDEState
*s
)
745 s
->status
= READY_STAT
| SEEK_STAT
;
746 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
750 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
752 #ifdef DEBUG_IDE_ATAPI
753 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
755 s
->error
= sense_key
<< 4;
756 s
->status
= READY_STAT
| ERR_STAT
;
757 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
758 s
->sense_key
= sense_key
;
763 static void ide_atapi_cmd_check_status(IDEState
*s
)
765 #ifdef DEBUG_IDE_ATAPI
766 printf("atapi_cmd_check_status\n");
768 s
->error
= MC_ERR
| (SENSE_UNIT_ATTENTION
<< 4);
769 s
->status
= ERR_STAT
;
774 static void ide_flush_cb(void *opaque
, int ret
)
776 IDEState
*s
= opaque
;
778 /* XXX: how do we signal I/O errors here? */
780 s
->status
= READY_STAT
| SEEK_STAT
;
784 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
790 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
798 static inline int ube16_to_cpu(const uint8_t *buf
)
800 return (buf
[0] << 8) | buf
[1];
803 static inline int ube32_to_cpu(const uint8_t *buf
)
805 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
808 static void lba_to_msf(uint8_t *buf
, int lba
)
811 buf
[0] = (lba
/ 75) / 60;
812 buf
[1] = (lba
/ 75) % 60;
816 static void cd_data_to_raw(uint8_t *buf
, int lba
)
820 memset(buf
+ 1, 0xff, 10);
824 lba_to_msf(buf
, lba
);
825 buf
[3] = 0x01; /* mode 1 data */
829 /* XXX: ECC not computed */
833 static int cd_read_sector(BlockDriverState
*bs
, int lba
, uint8_t *buf
,
838 switch(sector_size
) {
840 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
, 4);
843 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
+ 16, 4);
846 cd_data_to_raw(buf
, lba
);
855 void ide_atapi_io_error(IDEState
*s
, int ret
)
857 /* XXX: handle more errors */
858 if (ret
== -ENOMEDIUM
) {
859 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
860 ASC_MEDIUM_NOT_PRESENT
);
862 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
863 ASC_LOGICAL_BLOCK_OOR
);
867 /* The whole ATAPI transfer logic is handled in this function */
868 static void ide_atapi_cmd_reply_end(IDEState
*s
)
870 int byte_count_limit
, size
, ret
;
871 #ifdef DEBUG_IDE_ATAPI
872 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
873 s
->packet_transfer_size
,
874 s
->elementary_transfer_size
,
877 if (s
->packet_transfer_size
<= 0) {
878 /* end of transfer */
879 ide_transfer_stop(s
);
880 s
->status
= READY_STAT
| SEEK_STAT
;
881 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
883 #ifdef DEBUG_IDE_ATAPI
884 printf("status=0x%x\n", s
->status
);
887 /* see if a new sector must be read */
888 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
889 ret
= cd_read_sector(s
->bs
, s
->lba
, s
->io_buffer
, s
->cd_sector_size
);
891 ide_transfer_stop(s
);
892 ide_atapi_io_error(s
, ret
);
896 s
->io_buffer_index
= 0;
898 if (s
->elementary_transfer_size
> 0) {
899 /* there are some data left to transmit in this elementary
901 size
= s
->cd_sector_size
- s
->io_buffer_index
;
902 if (size
> s
->elementary_transfer_size
)
903 size
= s
->elementary_transfer_size
;
904 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
,
905 size
, ide_atapi_cmd_reply_end
);
906 s
->packet_transfer_size
-= size
;
907 s
->elementary_transfer_size
-= size
;
908 s
->io_buffer_index
+= size
;
910 /* a new transfer is needed */
911 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
912 byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
913 #ifdef DEBUG_IDE_ATAPI
914 printf("byte_count_limit=%d\n", byte_count_limit
);
916 if (byte_count_limit
== 0xffff)
918 size
= s
->packet_transfer_size
;
919 if (size
> byte_count_limit
) {
920 /* byte count limit must be even if this case */
921 if (byte_count_limit
& 1)
923 size
= byte_count_limit
;
927 s
->elementary_transfer_size
= size
;
928 /* we cannot transmit more than one sector at a time */
930 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
931 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
933 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
,
934 size
, ide_atapi_cmd_reply_end
);
935 s
->packet_transfer_size
-= size
;
936 s
->elementary_transfer_size
-= size
;
937 s
->io_buffer_index
+= size
;
939 #ifdef DEBUG_IDE_ATAPI
940 printf("status=0x%x\n", s
->status
);
946 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
947 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
951 s
->lba
= -1; /* no sector read */
952 s
->packet_transfer_size
= size
;
953 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
954 s
->elementary_transfer_size
= 0;
955 s
->io_buffer_index
= 0;
958 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
959 ide_dma_start(s
, ide_atapi_cmd_read_dma_cb
);
961 s
->status
= READY_STAT
| SEEK_STAT
;
962 ide_atapi_cmd_reply_end(s
);
966 /* start a CD-CDROM read command */
967 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
971 s
->packet_transfer_size
= nb_sectors
* sector_size
;
972 s
->elementary_transfer_size
= 0;
973 s
->io_buffer_index
= sector_size
;
974 s
->cd_sector_size
= sector_size
;
976 s
->status
= READY_STAT
| SEEK_STAT
;
977 ide_atapi_cmd_reply_end(s
);
980 /* ATAPI DMA support */
982 /* XXX: handle read errors */
983 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
985 BMDMAState
*bm
= opaque
;
986 IDEState
*s
= bmdma_active_if(bm
);
990 ide_atapi_io_error(s
, ret
);
994 if (s
->io_buffer_size
> 0) {
996 * For a cdrom read sector command (s->lba != -1),
997 * adjust the lba for the next s->io_buffer_size chunk
998 * and dma the current chunk.
999 * For a command != read (s->lba == -1), just transfer
1003 if (s
->cd_sector_size
== 2352) {
1005 cd_data_to_raw(s
->io_buffer
, s
->lba
);
1007 n
= s
->io_buffer_size
>> 11;
1011 s
->packet_transfer_size
-= s
->io_buffer_size
;
1012 if (dma_buf_rw(bm
, 1) == 0)
1016 if (s
->packet_transfer_size
<= 0) {
1017 s
->status
= READY_STAT
| SEEK_STAT
;
1018 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
1019 ide_set_irq(s
->bus
);
1021 bm
->status
&= ~BM_STATUS_DMAING
;
1022 bm
->status
|= BM_STATUS_INT
;
1029 s
->io_buffer_index
= 0;
1030 if (s
->cd_sector_size
== 2352) {
1032 s
->io_buffer_size
= s
->cd_sector_size
;
1035 n
= s
->packet_transfer_size
>> 11;
1036 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
1037 n
= (IDE_DMA_BUF_SECTORS
/ 4);
1038 s
->io_buffer_size
= n
* 2048;
1042 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
1044 bm
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
1045 bm
->iov
.iov_len
= n
* 4 * 512;
1046 qemu_iovec_init_external(&bm
->qiov
, &bm
->iov
, 1);
1047 bm
->aiocb
= bdrv_aio_readv(s
->bs
, (int64_t)s
->lba
<< 2, &bm
->qiov
,
1048 n
* 4, ide_atapi_cmd_read_dma_cb
, bm
);
1050 /* Note: media not present is the most likely case */
1051 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1052 ASC_MEDIUM_NOT_PRESENT
);
1057 /* start a CD-CDROM read command with DMA */
1058 /* XXX: test if DMA is available */
1059 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
1063 s
->packet_transfer_size
= nb_sectors
* sector_size
;
1064 s
->io_buffer_index
= 0;
1065 s
->io_buffer_size
= 0;
1066 s
->cd_sector_size
= sector_size
;
1068 /* XXX: check if BUSY_STAT should be set */
1069 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
1070 ide_dma_start(s
, ide_atapi_cmd_read_dma_cb
);
1073 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
1076 #ifdef DEBUG_IDE_ATAPI
1077 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
1081 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
1083 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
1087 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
1090 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
1092 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
1093 cpu_to_ube16 (buf_profile
, profile
);
1094 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
1096 /* each profile adds 4 bytes to the response */
1098 buf
[11] += 4; /* Additional Length */
1103 static int ide_dvd_read_structure(IDEState
*s
, int format
,
1104 const uint8_t *packet
, uint8_t *buf
)
1107 case 0x0: /* Physical format information */
1109 int layer
= packet
[6];
1110 uint64_t total_sectors
;
1113 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1115 bdrv_get_geometry(s
->bs
, &total_sectors
);
1116 total_sectors
>>= 2;
1117 if (total_sectors
== 0)
1118 return -ASC_MEDIUM_NOT_PRESENT
;
1120 buf
[4] = 1; /* DVD-ROM, part version 1 */
1121 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1122 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1123 buf
[7] = 0; /* default densities */
1125 /* FIXME: 0x30000 per spec? */
1126 cpu_to_ube32(buf
+ 8, 0); /* start sector */
1127 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
1128 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
1130 /* Size of buffer, not including 2 byte size field */
1131 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1133 /* 2k data + 4 byte header */
1137 case 0x01: /* DVD copyright information */
1138 buf
[4] = 0; /* no copyright data */
1139 buf
[5] = 0; /* no region restrictions */
1141 /* Size of buffer, not including 2 byte size field */
1142 cpu_to_be16wu((uint16_t *)buf
, 4 + 2);
1144 /* 4 byte header + 4 byte data */
1147 case 0x03: /* BCA information - invalid field for no BCA info */
1148 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1150 case 0x04: /* DVD disc manufacturing information */
1151 /* Size of buffer, not including 2 byte size field */
1152 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1154 /* 2k data + 4 byte header */
1159 * This lists all the command capabilities above. Add new ones
1160 * in order and update the length and buffer return values.
1163 buf
[4] = 0x00; /* Physical format */
1164 buf
[5] = 0x40; /* Not writable, is readable */
1165 cpu_to_be16wu((uint16_t *)(buf
+ 6), 2048 + 4);
1167 buf
[8] = 0x01; /* Copyright info */
1168 buf
[9] = 0x40; /* Not writable, is readable */
1169 cpu_to_be16wu((uint16_t *)(buf
+ 10), 4 + 4);
1171 buf
[12] = 0x03; /* BCA info */
1172 buf
[13] = 0x40; /* Not writable, is readable */
1173 cpu_to_be16wu((uint16_t *)(buf
+ 14), 188 + 4);
1175 buf
[16] = 0x04; /* Manufacturing info */
1176 buf
[17] = 0x40; /* Not writable, is readable */
1177 cpu_to_be16wu((uint16_t *)(buf
+ 18), 2048 + 4);
1179 /* Size of buffer, not including 2 byte size field */
1180 cpu_to_be16wu((uint16_t *)buf
, 16 + 2);
1182 /* data written + 4 byte header */
1185 default: /* TODO: formats beyond DVD-ROM requires */
1186 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1190 static void ide_atapi_cmd(IDEState
*s
)
1192 const uint8_t *packet
;
1196 packet
= s
->io_buffer
;
1198 #ifdef DEBUG_IDE_ATAPI
1201 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1202 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1203 printf(" %02x", packet
[i
]);
1208 /* If there's a UNIT_ATTENTION condition pending, only
1209 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1210 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
1211 s
->io_buffer
[0] != GPCMD_REQUEST_SENSE
&&
1212 s
->io_buffer
[0] != GPCMD_INQUIRY
) {
1213 ide_atapi_cmd_check_status(s
);
1216 switch(s
->io_buffer
[0]) {
1217 case GPCMD_TEST_UNIT_READY
:
1218 if (bdrv_is_inserted(s
->bs
) && !s
->cdrom_changed
) {
1219 ide_atapi_cmd_ok(s
);
1221 s
->cdrom_changed
= 0;
1222 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1223 ASC_MEDIUM_NOT_PRESENT
);
1226 case GPCMD_MODE_SENSE_6
:
1227 case GPCMD_MODE_SENSE_10
:
1230 if (packet
[0] == GPCMD_MODE_SENSE_10
)
1231 max_len
= ube16_to_cpu(packet
+ 7);
1233 max_len
= packet
[4];
1234 action
= packet
[2] >> 6;
1235 code
= packet
[2] & 0x3f;
1237 case 0: /* current values */
1239 case 0x01: /* error recovery */
1240 cpu_to_ube16(&buf
[0], 16 + 6);
1256 ide_atapi_cmd_reply(s
, 16, max_len
);
1259 cpu_to_ube16(&buf
[0], 28 + 6);
1272 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1273 code checks for this to automount media. */
1276 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
1277 if (bdrv_is_locked(s
->bs
))
1280 cpu_to_ube16(&buf
[16], 706);
1283 cpu_to_ube16(&buf
[20], 512);
1284 cpu_to_ube16(&buf
[22], 706);
1289 ide_atapi_cmd_reply(s
, 28, max_len
);
1295 case 1: /* changeable values */
1297 case 2: /* default values */
1300 case 3: /* saved values */
1301 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1302 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
1307 case GPCMD_REQUEST_SENSE
:
1308 max_len
= packet
[4];
1310 buf
[0] = 0x70 | (1 << 7);
1311 buf
[2] = s
->sense_key
;
1314 if (s
->sense_key
== SENSE_UNIT_ATTENTION
)
1315 s
->sense_key
= SENSE_NONE
;
1316 ide_atapi_cmd_reply(s
, 18, max_len
);
1318 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
:
1319 if (bdrv_is_inserted(s
->bs
)) {
1320 bdrv_set_locked(s
->bs
, packet
[4] & 1);
1321 ide_atapi_cmd_ok(s
);
1323 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1324 ASC_MEDIUM_NOT_PRESENT
);
1330 int nb_sectors
, lba
;
1332 if (packet
[0] == GPCMD_READ_10
)
1333 nb_sectors
= ube16_to_cpu(packet
+ 7);
1335 nb_sectors
= ube32_to_cpu(packet
+ 6);
1336 lba
= ube32_to_cpu(packet
+ 2);
1337 if (nb_sectors
== 0) {
1338 ide_atapi_cmd_ok(s
);
1341 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1346 int nb_sectors
, lba
, transfer_request
;
1348 nb_sectors
= (packet
[6] << 16) | (packet
[7] << 8) | packet
[8];
1349 lba
= ube32_to_cpu(packet
+ 2);
1350 if (nb_sectors
== 0) {
1351 ide_atapi_cmd_ok(s
);
1354 transfer_request
= packet
[9];
1355 switch(transfer_request
& 0xf8) {
1358 ide_atapi_cmd_ok(s
);
1362 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1366 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1369 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1370 ASC_INV_FIELD_IN_CMD_PACKET
);
1378 uint64_t total_sectors
;
1380 bdrv_get_geometry(s
->bs
, &total_sectors
);
1381 total_sectors
>>= 2;
1382 if (total_sectors
== 0) {
1383 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1384 ASC_MEDIUM_NOT_PRESENT
);
1387 lba
= ube32_to_cpu(packet
+ 2);
1388 if (lba
>= total_sectors
) {
1389 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1390 ASC_LOGICAL_BLOCK_OOR
);
1393 ide_atapi_cmd_ok(s
);
1396 case GPCMD_START_STOP_UNIT
:
1398 int start
, eject
, err
= 0;
1399 start
= packet
[4] & 1;
1400 eject
= (packet
[4] >> 1) & 1;
1403 err
= bdrv_eject(s
->bs
, !start
);
1408 ide_atapi_cmd_ok(s
);
1411 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1412 ASC_MEDIA_REMOVAL_PREVENTED
);
1415 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1416 ASC_MEDIUM_NOT_PRESENT
);
1421 case GPCMD_MECHANISM_STATUS
:
1423 max_len
= ube16_to_cpu(packet
+ 8);
1424 cpu_to_ube16(buf
, 0);
1425 /* no current LBA */
1430 cpu_to_ube16(buf
+ 6, 0);
1431 ide_atapi_cmd_reply(s
, 8, max_len
);
1434 case GPCMD_READ_TOC_PMA_ATIP
:
1436 int format
, msf
, start_track
, len
;
1437 uint64_t total_sectors
;
1439 bdrv_get_geometry(s
->bs
, &total_sectors
);
1440 total_sectors
>>= 2;
1441 if (total_sectors
== 0) {
1442 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1443 ASC_MEDIUM_NOT_PRESENT
);
1446 max_len
= ube16_to_cpu(packet
+ 7);
1447 format
= packet
[9] >> 6;
1448 msf
= (packet
[1] >> 1) & 1;
1449 start_track
= packet
[6];
1452 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1455 ide_atapi_cmd_reply(s
, len
, max_len
);
1458 /* multi session : only a single session defined */
1463 ide_atapi_cmd_reply(s
, 12, max_len
);
1466 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1469 ide_atapi_cmd_reply(s
, len
, max_len
);
1473 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1474 ASC_INV_FIELD_IN_CMD_PACKET
);
1479 case GPCMD_READ_CDVD_CAPACITY
:
1481 uint64_t total_sectors
;
1483 bdrv_get_geometry(s
->bs
, &total_sectors
);
1484 total_sectors
>>= 2;
1485 if (total_sectors
== 0) {
1486 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1487 ASC_MEDIUM_NOT_PRESENT
);
1490 /* NOTE: it is really the number of sectors minus 1 */
1491 cpu_to_ube32(buf
, total_sectors
- 1);
1492 cpu_to_ube32(buf
+ 4, 2048);
1493 ide_atapi_cmd_reply(s
, 8, 8);
1496 case GPCMD_READ_DVD_STRUCTURE
:
1498 int media
= packet
[1];
1499 int format
= packet
[7];
1502 max_len
= ube16_to_cpu(packet
+ 8);
1504 if (format
< 0xff) {
1505 if (media_is_cd(s
)) {
1506 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1507 ASC_INCOMPATIBLE_FORMAT
);
1509 } else if (!media_present(s
)) {
1510 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1511 ASC_INV_FIELD_IN_CMD_PACKET
);
1516 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1517 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1523 ret
= ide_dvd_read_structure(s
, format
, packet
, buf
);
1526 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
, -ret
);
1528 ide_atapi_cmd_reply(s
, ret
, max_len
);
1532 /* TODO: BD support, fall through for now */
1534 /* Generic disk structures */
1535 case 0x80: /* TODO: AACS volume identifier */
1536 case 0x81: /* TODO: AACS media serial number */
1537 case 0x82: /* TODO: AACS media identifier */
1538 case 0x83: /* TODO: AACS media key block */
1539 case 0x90: /* TODO: List of recognized format layers */
1540 case 0xc0: /* TODO: Write protection status */
1542 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1543 ASC_INV_FIELD_IN_CMD_PACKET
);
1548 case GPCMD_SET_SPEED
:
1549 ide_atapi_cmd_ok(s
);
1552 max_len
= packet
[4];
1553 buf
[0] = 0x05; /* CD-ROM */
1554 buf
[1] = 0x80; /* removable */
1555 buf
[2] = 0x00; /* ISO */
1556 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1557 buf
[4] = 31; /* additional length */
1558 buf
[5] = 0; /* reserved */
1559 buf
[6] = 0; /* reserved */
1560 buf
[7] = 0; /* reserved */
1561 padstr8(buf
+ 8, 8, "QEMU");
1562 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
1563 padstr8(buf
+ 32, 4, QEMU_VERSION
);
1564 ide_atapi_cmd_reply(s
, 36, max_len
);
1566 case GPCMD_GET_CONFIGURATION
:
1571 /* only feature 0 is supported */
1572 if (packet
[2] != 0 || packet
[3] != 0) {
1573 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1574 ASC_INV_FIELD_IN_CMD_PACKET
);
1578 /* XXX: could result in alignment problems in some architectures */
1579 max_len
= ube16_to_cpu(packet
+ 7);
1582 * XXX: avoid overflow for io_buffer if max_len is bigger than
1583 * the size of that buffer (dimensioned to max number of
1584 * sectors to transfer at once)
1586 * Only a problem if the feature/profiles grow.
1588 if (max_len
> 512) /* XXX: assume 1 sector */
1591 memset(buf
, 0, max_len
);
1593 * the number of sectors from the media tells us which profile
1594 * to use as current. 0 means there is no media
1596 if (media_is_dvd(s
))
1597 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
1598 else if (media_is_cd(s
))
1599 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
1601 buf
[10] = 0x02 | 0x01; /* persistent and current */
1602 len
= 12; /* headers: 8 + 4 */
1603 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
1604 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
1605 cpu_to_ube32(buf
, len
- 4); /* data length */
1607 ide_atapi_cmd_reply(s
, len
, max_len
);
1611 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1612 ASC_ILLEGAL_OPCODE
);
1617 static void ide_cfata_metadata_inquiry(IDEState
*s
)
1622 p
= (uint16_t *) s
->io_buffer
;
1623 memset(p
, 0, 0x200);
1624 spd
= ((s
->mdata_size
- 1) >> 9) + 1;
1626 put_le16(p
+ 0, 0x0001); /* Data format revision */
1627 put_le16(p
+ 1, 0x0000); /* Media property: silicon */
1628 put_le16(p
+ 2, s
->media_changed
); /* Media status */
1629 put_le16(p
+ 3, s
->mdata_size
& 0xffff); /* Capacity in bytes (low) */
1630 put_le16(p
+ 4, s
->mdata_size
>> 16); /* Capacity in bytes (high) */
1631 put_le16(p
+ 5, spd
& 0xffff); /* Sectors per device (low) */
1632 put_le16(p
+ 6, spd
>> 16); /* Sectors per device (high) */
1635 static void ide_cfata_metadata_read(IDEState
*s
)
1639 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1640 s
->status
= ERR_STAT
;
1641 s
->error
= ABRT_ERR
;
1645 p
= (uint16_t *) s
->io_buffer
;
1646 memset(p
, 0, 0x200);
1648 put_le16(p
+ 0, s
->media_changed
); /* Media status */
1649 memcpy(p
+ 1, s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1650 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1651 s
->nsector
<< 9), 0x200 - 2));
1654 static void ide_cfata_metadata_write(IDEState
*s
)
1656 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1657 s
->status
= ERR_STAT
;
1658 s
->error
= ABRT_ERR
;
1662 s
->media_changed
= 0;
1664 memcpy(s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1666 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1667 s
->nsector
<< 9), 0x200 - 2));
1670 /* called when the inserted state of the media has changed */
1671 static void cdrom_change_cb(void *opaque
)
1673 IDEState
*s
= opaque
;
1674 uint64_t nb_sectors
;
1676 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1677 s
->nb_sectors
= nb_sectors
;
1679 s
->sense_key
= SENSE_UNIT_ATTENTION
;
1680 s
->asc
= ASC_MEDIUM_MAY_HAVE_CHANGED
;
1681 s
->cdrom_changed
= 1;
1682 ide_set_irq(s
->bus
);
1685 static void ide_cmd_lba48_transform(IDEState
*s
, int lba48
)
1689 /* handle the 'magic' 0 nsector count conversion here. to avoid
1690 * fiddling with the rest of the read logic, we just store the
1691 * full sector count in ->nsector and ignore ->hob_nsector from now
1697 if (!s
->nsector
&& !s
->hob_nsector
)
1700 int lo
= s
->nsector
;
1701 int hi
= s
->hob_nsector
;
1703 s
->nsector
= (hi
<< 8) | lo
;
1708 static void ide_clear_hob(IDEBus
*bus
)
1710 /* any write clears HOB high bit of device control register */
1711 bus
->ifs
[0].select
&= ~(1 << 7);
1712 bus
->ifs
[1].select
&= ~(1 << 7);
1715 void ide_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1717 IDEBus
*bus
= opaque
;
1723 printf("IDE: write addr=0x%x val=0x%02x\n", addr
, val
);
1728 /* ignore writes to command block while busy with previous command */
1729 if (addr
!= 7 && (idebus_active_if(bus
)->status
& (BUSY_STAT
|DRQ_STAT
)))
1737 /* NOTE: data is written to the two drives */
1738 bus
->ifs
[0].hob_feature
= bus
->ifs
[0].feature
;
1739 bus
->ifs
[1].hob_feature
= bus
->ifs
[1].feature
;
1740 bus
->ifs
[0].feature
= val
;
1741 bus
->ifs
[1].feature
= val
;
1745 bus
->ifs
[0].hob_nsector
= bus
->ifs
[0].nsector
;
1746 bus
->ifs
[1].hob_nsector
= bus
->ifs
[1].nsector
;
1747 bus
->ifs
[0].nsector
= val
;
1748 bus
->ifs
[1].nsector
= val
;
1752 bus
->ifs
[0].hob_sector
= bus
->ifs
[0].sector
;
1753 bus
->ifs
[1].hob_sector
= bus
->ifs
[1].sector
;
1754 bus
->ifs
[0].sector
= val
;
1755 bus
->ifs
[1].sector
= val
;
1759 bus
->ifs
[0].hob_lcyl
= bus
->ifs
[0].lcyl
;
1760 bus
->ifs
[1].hob_lcyl
= bus
->ifs
[1].lcyl
;
1761 bus
->ifs
[0].lcyl
= val
;
1762 bus
->ifs
[1].lcyl
= val
;
1766 bus
->ifs
[0].hob_hcyl
= bus
->ifs
[0].hcyl
;
1767 bus
->ifs
[1].hob_hcyl
= bus
->ifs
[1].hcyl
;
1768 bus
->ifs
[0].hcyl
= val
;
1769 bus
->ifs
[1].hcyl
= val
;
1772 /* FIXME: HOB readback uses bit 7 */
1773 bus
->ifs
[0].select
= (val
& ~0x10) | 0xa0;
1774 bus
->ifs
[1].select
= (val
| 0x10) | 0xa0;
1776 bus
->unit
= (val
>> 4) & 1;
1781 #if defined(DEBUG_IDE)
1782 printf("ide: CMD=%02x\n", val
);
1784 s
= idebus_active_if(bus
);
1785 /* ignore commands to non existant slave */
1786 if (s
!= bus
->ifs
&& !s
->bs
)
1789 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1790 if ((s
->status
& (BUSY_STAT
|DRQ_STAT
)) && val
!= WIN_DEVICE_RESET
)
1795 if (s
->bs
&& !s
->is_cdrom
) {
1799 ide_cfata_identify(s
);
1800 s
->status
= READY_STAT
| SEEK_STAT
;
1801 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1804 ide_set_signature(s
);
1806 ide_abort_command(s
);
1808 ide_set_irq(s
->bus
);
1813 s
->status
= READY_STAT
| SEEK_STAT
;
1814 ide_set_irq(s
->bus
);
1817 if (s
->is_cf
&& s
->nsector
== 0) {
1818 /* Disable Read and Write Multiple */
1819 s
->mult_sectors
= 0;
1820 s
->status
= READY_STAT
| SEEK_STAT
;
1821 } else if ((s
->nsector
& 0xff) != 0 &&
1822 ((s
->nsector
& 0xff) > MAX_MULT_SECTORS
||
1823 (s
->nsector
& (s
->nsector
- 1)) != 0)) {
1824 ide_abort_command(s
);
1826 s
->mult_sectors
= s
->nsector
& 0xff;
1827 s
->status
= READY_STAT
| SEEK_STAT
;
1829 ide_set_irq(s
->bus
);
1831 case WIN_VERIFY_EXT
:
1834 case WIN_VERIFY_ONCE
:
1835 /* do sector number check ? */
1836 ide_cmd_lba48_transform(s
, lba48
);
1837 s
->status
= READY_STAT
| SEEK_STAT
;
1838 ide_set_irq(s
->bus
);
1846 ide_cmd_lba48_transform(s
, lba48
);
1847 s
->req_nb_sectors
= 1;
1853 case WIN_WRITE_ONCE
:
1854 case CFA_WRITE_SECT_WO_ERASE
:
1855 case WIN_WRITE_VERIFY
:
1856 ide_cmd_lba48_transform(s
, lba48
);
1858 s
->status
= SEEK_STAT
| READY_STAT
;
1859 s
->req_nb_sectors
= 1;
1860 ide_transfer_start(s
, s
->io_buffer
, 512, ide_sector_write
);
1861 s
->media_changed
= 1;
1863 case WIN_MULTREAD_EXT
:
1866 if (!s
->mult_sectors
)
1868 ide_cmd_lba48_transform(s
, lba48
);
1869 s
->req_nb_sectors
= s
->mult_sectors
;
1872 case WIN_MULTWRITE_EXT
:
1875 case CFA_WRITE_MULTI_WO_ERASE
:
1876 if (!s
->mult_sectors
)
1878 ide_cmd_lba48_transform(s
, lba48
);
1880 s
->status
= SEEK_STAT
| READY_STAT
;
1881 s
->req_nb_sectors
= s
->mult_sectors
;
1883 if (n
> s
->req_nb_sectors
)
1884 n
= s
->req_nb_sectors
;
1885 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_write
);
1886 s
->media_changed
= 1;
1888 case WIN_READDMA_EXT
:
1891 case WIN_READDMA_ONCE
:
1894 ide_cmd_lba48_transform(s
, lba48
);
1895 ide_sector_read_dma(s
);
1897 case WIN_WRITEDMA_EXT
:
1900 case WIN_WRITEDMA_ONCE
:
1903 ide_cmd_lba48_transform(s
, lba48
);
1904 ide_sector_write_dma(s
);
1905 s
->media_changed
= 1;
1907 case WIN_READ_NATIVE_MAX_EXT
:
1909 case WIN_READ_NATIVE_MAX
:
1910 ide_cmd_lba48_transform(s
, lba48
);
1911 ide_set_sector(s
, s
->nb_sectors
- 1);
1912 s
->status
= READY_STAT
| SEEK_STAT
;
1913 ide_set_irq(s
->bus
);
1915 case WIN_CHECKPOWERMODE1
:
1916 case WIN_CHECKPOWERMODE2
:
1917 s
->nsector
= 0xff; /* device active or idle */
1918 s
->status
= READY_STAT
| SEEK_STAT
;
1919 ide_set_irq(s
->bus
);
1921 case WIN_SETFEATURES
:
1924 /* XXX: valid for CDROM ? */
1925 switch(s
->feature
) {
1926 case 0xcc: /* reverting to power-on defaults enable */
1927 case 0x66: /* reverting to power-on defaults disable */
1928 case 0x02: /* write cache enable */
1929 case 0x82: /* write cache disable */
1930 case 0xaa: /* read look-ahead enable */
1931 case 0x55: /* read look-ahead disable */
1932 case 0x05: /* set advanced power management mode */
1933 case 0x85: /* disable advanced power management mode */
1934 case 0x69: /* NOP */
1935 case 0x67: /* NOP */
1936 case 0x96: /* NOP */
1937 case 0x9a: /* NOP */
1938 case 0x42: /* enable Automatic Acoustic Mode */
1939 case 0xc2: /* disable Automatic Acoustic Mode */
1940 s
->status
= READY_STAT
| SEEK_STAT
;
1941 ide_set_irq(s
->bus
);
1943 case 0x03: { /* set transfer mode */
1944 uint8_t val
= s
->nsector
& 0x07;
1945 uint16_t *identify_data
= (uint16_t *)s
->identify_data
;
1947 switch (s
->nsector
>> 3) {
1948 case 0x00: /* pio default */
1949 case 0x01: /* pio mode */
1950 put_le16(identify_data
+ 62,0x07);
1951 put_le16(identify_data
+ 63,0x07);
1952 put_le16(identify_data
+ 88,0x3f);
1954 case 0x02: /* sigle word dma mode*/
1955 put_le16(identify_data
+ 62,0x07 | (1 << (val
+ 8)));
1956 put_le16(identify_data
+ 63,0x07);
1957 put_le16(identify_data
+ 88,0x3f);
1959 case 0x04: /* mdma mode */
1960 put_le16(identify_data
+ 62,0x07);
1961 put_le16(identify_data
+ 63,0x07 | (1 << (val
+ 8)));
1962 put_le16(identify_data
+ 88,0x3f);
1964 case 0x08: /* udma mode */
1965 put_le16(identify_data
+ 62,0x07);
1966 put_le16(identify_data
+ 63,0x07);
1967 put_le16(identify_data
+ 88,0x3f | (1 << (val
+ 8)));
1972 s
->status
= READY_STAT
| SEEK_STAT
;
1973 ide_set_irq(s
->bus
);
1980 case WIN_FLUSH_CACHE
:
1981 case WIN_FLUSH_CACHE_EXT
:
1983 bdrv_aio_flush(s
->bs
, ide_flush_cb
, s
);
1989 case WIN_STANDBYNOW1
:
1990 case WIN_STANDBYNOW2
:
1991 case WIN_IDLEIMMEDIATE
:
1992 case CFA_IDLEIMMEDIATE
:
1997 s
->status
= READY_STAT
;
1998 ide_set_irq(s
->bus
);
2003 /* XXX: Check that seek is within bounds */
2004 s
->status
= READY_STAT
| SEEK_STAT
;
2005 ide_set_irq(s
->bus
);
2007 /* ATAPI commands */
2010 ide_atapi_identify(s
);
2011 s
->status
= READY_STAT
| SEEK_STAT
;
2012 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
2014 ide_abort_command(s
);
2016 ide_set_irq(s
->bus
);
2019 ide_set_signature(s
);
2021 s
->status
= 0; /* ATAPI spec (v6) section 9.10 defines packet
2022 * devices to return a clear status register
2023 * with READY_STAT *not* set. */
2025 s
->status
= READY_STAT
| SEEK_STAT
;
2026 s
->error
= 0x01; /* Device 0 passed, Device 1 passed or not
2029 ide_set_irq(s
->bus
);
2034 ide_set_signature(s
);
2035 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2041 /* overlapping commands not supported */
2042 if (s
->feature
& 0x02)
2044 s
->status
= READY_STAT
| SEEK_STAT
;
2045 s
->atapi_dma
= s
->feature
& 1;
2047 ide_transfer_start(s
, s
->io_buffer
, ATAPI_PACKET_SIZE
,
2050 /* CF-ATA commands */
2051 case CFA_REQ_EXT_ERROR_CODE
:
2054 s
->error
= 0x09; /* miscellaneous error */
2055 s
->status
= READY_STAT
| SEEK_STAT
;
2056 ide_set_irq(s
->bus
);
2058 case CFA_ERASE_SECTORS
:
2059 case CFA_WEAR_LEVEL
:
2062 if (val
== CFA_WEAR_LEVEL
)
2064 if (val
== CFA_ERASE_SECTORS
)
2065 s
->media_changed
= 1;
2067 s
->status
= READY_STAT
| SEEK_STAT
;
2068 ide_set_irq(s
->bus
);
2070 case CFA_TRANSLATE_SECTOR
:
2074 s
->status
= READY_STAT
| SEEK_STAT
;
2075 memset(s
->io_buffer
, 0, 0x200);
2076 s
->io_buffer
[0x00] = s
->hcyl
; /* Cyl MSB */
2077 s
->io_buffer
[0x01] = s
->lcyl
; /* Cyl LSB */
2078 s
->io_buffer
[0x02] = s
->select
; /* Head */
2079 s
->io_buffer
[0x03] = s
->sector
; /* Sector */
2080 s
->io_buffer
[0x04] = ide_get_sector(s
) >> 16; /* LBA MSB */
2081 s
->io_buffer
[0x05] = ide_get_sector(s
) >> 8; /* LBA */
2082 s
->io_buffer
[0x06] = ide_get_sector(s
) >> 0; /* LBA LSB */
2083 s
->io_buffer
[0x13] = 0x00; /* Erase flag */
2084 s
->io_buffer
[0x18] = 0x00; /* Hot count */
2085 s
->io_buffer
[0x19] = 0x00; /* Hot count */
2086 s
->io_buffer
[0x1a] = 0x01; /* Hot count */
2087 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2088 ide_set_irq(s
->bus
);
2090 case CFA_ACCESS_METADATA_STORAGE
:
2093 switch (s
->feature
) {
2094 case 0x02: /* Inquiry Metadata Storage */
2095 ide_cfata_metadata_inquiry(s
);
2097 case 0x03: /* Read Metadata Storage */
2098 ide_cfata_metadata_read(s
);
2100 case 0x04: /* Write Metadata Storage */
2101 ide_cfata_metadata_write(s
);
2106 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2107 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2108 ide_set_irq(s
->bus
);
2110 case IBM_SENSE_CONDITION
:
2113 switch (s
->feature
) {
2114 case 0x01: /* sense temperature in device */
2115 s
->nsector
= 0x50; /* +20 C */
2120 s
->status
= READY_STAT
| SEEK_STAT
;
2121 ide_set_irq(s
->bus
);
2127 if (s
->hcyl
!= 0xc2 || s
->lcyl
!= 0x4f)
2129 if (!s
->smart_enabled
&& s
->feature
!= SMART_ENABLE
)
2131 switch (s
->feature
) {
2133 s
->smart_enabled
= 0;
2134 s
->status
= READY_STAT
| SEEK_STAT
;
2135 ide_set_irq(s
->bus
);
2138 s
->smart_enabled
= 1;
2139 s
->status
= READY_STAT
| SEEK_STAT
;
2140 ide_set_irq(s
->bus
);
2142 case SMART_ATTR_AUTOSAVE
:
2143 switch (s
->sector
) {
2145 s
->smart_autosave
= 0;
2148 s
->smart_autosave
= 1;
2153 s
->status
= READY_STAT
| SEEK_STAT
;
2154 ide_set_irq(s
->bus
);
2157 if (!s
->smart_errors
) {
2164 s
->status
= READY_STAT
| SEEK_STAT
;
2165 ide_set_irq(s
->bus
);
2167 case SMART_READ_THRESH
:
2168 memset(s
->io_buffer
, 0, 0x200);
2169 s
->io_buffer
[0] = 0x01; /* smart struct version */
2170 for (n
=0; n
<30; n
++) {
2171 if (smart_attributes
[n
][0] == 0)
2173 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2174 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][4];
2176 for (n
=0; n
<511; n
++) /* checksum */
2177 s
->io_buffer
[511] += s
->io_buffer
[n
];
2178 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2179 s
->status
= READY_STAT
| SEEK_STAT
;
2180 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2181 ide_set_irq(s
->bus
);
2183 case SMART_READ_DATA
:
2184 memset(s
->io_buffer
, 0, 0x200);
2185 s
->io_buffer
[0] = 0x01; /* smart struct version */
2186 for (n
=0; n
<30; n
++) {
2187 if (smart_attributes
[n
][0] == 0)
2189 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2190 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][1];
2191 s
->io_buffer
[2+3+(n
*12)] = smart_attributes
[n
][2];
2192 s
->io_buffer
[2+4+(n
*12)] = smart_attributes
[n
][3];
2194 s
->io_buffer
[362] = 0x02 | (s
->smart_autosave
?0x80:0x00);
2195 if (s
->smart_selftest_count
== 0) {
2196 s
->io_buffer
[363] = 0;
2199 s
->smart_selftest_data
[3 +
2200 (s
->smart_selftest_count
- 1) *
2203 s
->io_buffer
[364] = 0x20;
2204 s
->io_buffer
[365] = 0x01;
2205 /* offline data collection capacity: execute + self-test*/
2206 s
->io_buffer
[367] = (1<<4 | 1<<3 | 1);
2207 s
->io_buffer
[368] = 0x03; /* smart capability (1) */
2208 s
->io_buffer
[369] = 0x00; /* smart capability (2) */
2209 s
->io_buffer
[370] = 0x01; /* error logging supported */
2210 s
->io_buffer
[372] = 0x02; /* minutes for poll short test */
2211 s
->io_buffer
[373] = 0x36; /* minutes for poll ext test */
2212 s
->io_buffer
[374] = 0x01; /* minutes for poll conveyance */
2214 for (n
=0; n
<511; n
++)
2215 s
->io_buffer
[511] += s
->io_buffer
[n
];
2216 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2217 s
->status
= READY_STAT
| SEEK_STAT
;
2218 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2219 ide_set_irq(s
->bus
);
2221 case SMART_READ_LOG
:
2222 switch (s
->sector
) {
2223 case 0x01: /* summary smart error log */
2224 memset(s
->io_buffer
, 0, 0x200);
2225 s
->io_buffer
[0] = 0x01;
2226 s
->io_buffer
[1] = 0x00; /* no error entries */
2227 s
->io_buffer
[452] = s
->smart_errors
& 0xff;
2228 s
->io_buffer
[453] = (s
->smart_errors
& 0xff00) >> 8;
2230 for (n
=0; n
<511; n
++)
2231 s
->io_buffer
[511] += s
->io_buffer
[n
];
2232 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2234 case 0x06: /* smart self test log */
2235 memset(s
->io_buffer
, 0, 0x200);
2236 s
->io_buffer
[0] = 0x01;
2237 if (s
->smart_selftest_count
== 0) {
2238 s
->io_buffer
[508] = 0;
2240 s
->io_buffer
[508] = s
->smart_selftest_count
;
2241 for (n
=2; n
<506; n
++)
2242 s
->io_buffer
[n
] = s
->smart_selftest_data
[n
];
2244 for (n
=0; n
<511; n
++)
2245 s
->io_buffer
[511] += s
->io_buffer
[n
];
2246 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2251 s
->status
= READY_STAT
| SEEK_STAT
;
2252 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2253 ide_set_irq(s
->bus
);
2255 case SMART_EXECUTE_OFFLINE
:
2256 switch (s
->sector
) {
2257 case 0: /* off-line routine */
2258 case 1: /* short self test */
2259 case 2: /* extended self test */
2260 s
->smart_selftest_count
++;
2261 if(s
->smart_selftest_count
> 21)
2262 s
->smart_selftest_count
= 0;
2263 n
= 2 + (s
->smart_selftest_count
- 1) * 24;
2264 s
->smart_selftest_data
[n
] = s
->sector
;
2265 s
->smart_selftest_data
[n
+1] = 0x00; /* OK and finished */
2266 s
->smart_selftest_data
[n
+2] = 0x34; /* hour count lsb */
2267 s
->smart_selftest_data
[n
+3] = 0x12; /* hour count msb */
2268 s
->status
= READY_STAT
| SEEK_STAT
;
2269 ide_set_irq(s
->bus
);
2281 ide_abort_command(s
);
2282 ide_set_irq(s
->bus
);
2288 uint32_t ide_ioport_read(void *opaque
, uint32_t addr1
)
2290 IDEBus
*bus
= opaque
;
2291 IDEState
*s
= idebus_active_if(bus
);
2296 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2297 //hob = s->select & (1 << 7);
2304 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2305 (s
!= bus
->ifs
&& !s
->bs
))
2310 ret
= s
->hob_feature
;
2313 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2316 ret
= s
->nsector
& 0xff;
2318 ret
= s
->hob_nsector
;
2321 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2326 ret
= s
->hob_sector
;
2329 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2337 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2345 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2352 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2353 (s
!= bus
->ifs
&& !s
->bs
))
2357 qemu_irq_lower(bus
->irq
);
2361 printf("ide: read addr=0x%x val=%02x\n", addr1
, ret
);
2366 uint32_t ide_status_read(void *opaque
, uint32_t addr
)
2368 IDEBus
*bus
= opaque
;
2369 IDEState
*s
= idebus_active_if(bus
);
2372 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2373 (s
!= bus
->ifs
&& !s
->bs
))
2378 printf("ide: read status addr=0x%x val=%02x\n", addr
, ret
);
2383 void ide_cmd_write(void *opaque
, uint32_t addr
, uint32_t val
)
2385 IDEBus
*bus
= opaque
;
2390 printf("ide: write control addr=0x%x val=%02x\n", addr
, val
);
2392 /* common for both drives */
2393 if (!(bus
->cmd
& IDE_CMD_RESET
) &&
2394 (val
& IDE_CMD_RESET
)) {
2395 /* reset low to high */
2396 for(i
= 0;i
< 2; i
++) {
2398 s
->status
= BUSY_STAT
| SEEK_STAT
;
2401 } else if ((bus
->cmd
& IDE_CMD_RESET
) &&
2402 !(val
& IDE_CMD_RESET
)) {
2404 for(i
= 0;i
< 2; i
++) {
2407 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2409 s
->status
= READY_STAT
| SEEK_STAT
;
2410 ide_set_signature(s
);
2417 void ide_data_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2419 IDEBus
*bus
= opaque
;
2420 IDEState
*s
= idebus_active_if(bus
);
2423 /* PIO data access allowed only when DRQ bit is set */
2424 if (!(s
->status
& DRQ_STAT
))
2428 *(uint16_t *)p
= le16_to_cpu(val
);
2431 if (p
>= s
->data_end
)
2432 s
->end_transfer_func(s
);
2435 uint32_t ide_data_readw(void *opaque
, uint32_t addr
)
2437 IDEBus
*bus
= opaque
;
2438 IDEState
*s
= idebus_active_if(bus
);
2442 /* PIO data access allowed only when DRQ bit is set */
2443 if (!(s
->status
& DRQ_STAT
))
2447 ret
= cpu_to_le16(*(uint16_t *)p
);
2450 if (p
>= s
->data_end
)
2451 s
->end_transfer_func(s
);
2455 void ide_data_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2457 IDEBus
*bus
= opaque
;
2458 IDEState
*s
= idebus_active_if(bus
);
2461 /* PIO data access allowed only when DRQ bit is set */
2462 if (!(s
->status
& DRQ_STAT
))
2466 *(uint32_t *)p
= le32_to_cpu(val
);
2469 if (p
>= s
->data_end
)
2470 s
->end_transfer_func(s
);
2473 uint32_t ide_data_readl(void *opaque
, uint32_t addr
)
2475 IDEBus
*bus
= opaque
;
2476 IDEState
*s
= idebus_active_if(bus
);
2480 /* PIO data access allowed only when DRQ bit is set */
2481 if (!(s
->status
& DRQ_STAT
))
2485 ret
= cpu_to_le32(*(uint32_t *)p
);
2488 if (p
>= s
->data_end
)
2489 s
->end_transfer_func(s
);
2493 static void ide_dummy_transfer_stop(IDEState
*s
)
2495 s
->data_ptr
= s
->io_buffer
;
2496 s
->data_end
= s
->io_buffer
;
2497 s
->io_buffer
[0] = 0xff;
2498 s
->io_buffer
[1] = 0xff;
2499 s
->io_buffer
[2] = 0xff;
2500 s
->io_buffer
[3] = 0xff;
2503 void ide_reset(IDEState
*s
)
2505 IDEBus
*bus
= s
->bus
;
2508 s
->mult_sectors
= 0;
2510 s
->mult_sectors
= MAX_MULT_SECTORS
;
2511 bus
->unit
= s
->unit
;
2513 s
->status
= READY_STAT
| SEEK_STAT
;
2514 ide_set_signature(s
);
2515 /* init the transfer handler so that 0xffff is returned on data
2517 s
->end_transfer_func
= ide_dummy_transfer_stop
;
2518 ide_dummy_transfer_stop(s
);
2519 s
->media_changed
= 0;
2522 void ide_init_drive(IDEState
*s
, DriveInfo
*dinfo
)
2524 int cylinders
, heads
, secs
;
2525 uint64_t nb_sectors
;
2527 if (dinfo
&& dinfo
->bdrv
) {
2528 s
->bs
= dinfo
->bdrv
;
2529 bdrv_get_geometry(s
->bs
, &nb_sectors
);
2530 bdrv_guess_geometry(s
->bs
, &cylinders
, &heads
, &secs
);
2531 s
->cylinders
= cylinders
;
2534 s
->nb_sectors
= nb_sectors
;
2535 /* The SMART values should be preserved across power cycles
2537 s
->smart_enabled
= 1;
2538 s
->smart_autosave
= 1;
2539 s
->smart_errors
= 0;
2540 s
->smart_selftest_count
= 0;
2541 if (bdrv_get_type_hint(s
->bs
) == BDRV_TYPE_CDROM
) {
2543 bdrv_set_change_cb(s
->bs
, cdrom_change_cb
, s
);
2545 strncpy(s
->drive_serial_str
, drive_get_serial(s
->bs
),
2546 sizeof(s
->drive_serial_str
));
2548 if (strlen(s
->drive_serial_str
) == 0)
2549 snprintf(s
->drive_serial_str
, sizeof(s
->drive_serial_str
),
2550 "QM%05d", s
->drive_serial
);
2554 void ide_init2(IDEBus
*bus
, DriveInfo
*hd0
, DriveInfo
*hd1
,
2558 static int drive_serial
= 1;
2561 for(i
= 0; i
< 2; i
++) {
2565 s
->drive_serial
= drive_serial
++;
2566 s
->io_buffer
= qemu_blockalign(s
->bs
, IDE_DMA_BUF_SECTORS
*512 + 4);
2567 s
->smart_selftest_data
= qemu_blockalign(s
->bs
, 512);
2568 s
->sector_write_timer
= qemu_new_timer(vm_clock
,
2569 ide_sector_write_timer_cb
, s
);
2571 ide_init_drive(s
, hd0
);
2573 ide_init_drive(s
, hd1
);
2578 void ide_init_ioport(IDEBus
*bus
, int iobase
, int iobase2
)
2580 register_ioport_write(iobase
, 8, 1, ide_ioport_write
, bus
);
2581 register_ioport_read(iobase
, 8, 1, ide_ioport_read
, bus
);
2583 register_ioport_read(iobase2
, 1, 1, ide_status_read
, bus
);
2584 register_ioport_write(iobase2
, 1, 1, ide_cmd_write
, bus
);
2588 register_ioport_write(iobase
, 2, 2, ide_data_writew
, bus
);
2589 register_ioport_read(iobase
, 2, 2, ide_data_readw
, bus
);
2590 register_ioport_write(iobase
, 4, 4, ide_data_writel
, bus
);
2591 register_ioport_read(iobase
, 4, 4, ide_data_readl
, bus
);
2594 static bool is_identify_set(void *opaque
, int version_id
)
2596 IDEState
*s
= opaque
;
2598 return s
->identify_set
!= 0;
2601 static int ide_drive_post_load(void *opaque
, int version_id
)
2603 IDEState
*s
= opaque
;
2605 if (version_id
< 3) {
2606 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
2607 s
->asc
== ASC_MEDIUM_MAY_HAVE_CHANGED
) {
2608 s
->cdrom_changed
= 1;
2614 const VMStateDescription vmstate_ide_drive
= {
2615 .name
= "ide_drive",
2617 .minimum_version_id
= 0,
2618 .minimum_version_id_old
= 0,
2619 .post_load
= ide_drive_post_load
,
2620 .fields
= (VMStateField
[]) {
2621 VMSTATE_INT32(mult_sectors
, IDEState
),
2622 VMSTATE_INT32(identify_set
, IDEState
),
2623 VMSTATE_BUFFER_TEST(identify_data
, IDEState
, is_identify_set
),
2624 VMSTATE_UINT8(feature
, IDEState
),
2625 VMSTATE_UINT8(error
, IDEState
),
2626 VMSTATE_UINT32(nsector
, IDEState
),
2627 VMSTATE_UINT8(sector
, IDEState
),
2628 VMSTATE_UINT8(lcyl
, IDEState
),
2629 VMSTATE_UINT8(hcyl
, IDEState
),
2630 VMSTATE_UINT8(hob_feature
, IDEState
),
2631 VMSTATE_UINT8(hob_sector
, IDEState
),
2632 VMSTATE_UINT8(hob_nsector
, IDEState
),
2633 VMSTATE_UINT8(hob_lcyl
, IDEState
),
2634 VMSTATE_UINT8(hob_hcyl
, IDEState
),
2635 VMSTATE_UINT8(select
, IDEState
),
2636 VMSTATE_UINT8(status
, IDEState
),
2637 VMSTATE_UINT8(lba48
, IDEState
),
2638 VMSTATE_UINT8(sense_key
, IDEState
),
2639 VMSTATE_UINT8(asc
, IDEState
),
2640 VMSTATE_UINT8_V(cdrom_changed
, IDEState
, 3),
2641 /* XXX: if a transfer is pending, we do not save it yet */
2642 VMSTATE_END_OF_LIST()
2646 const VMStateDescription vmstate_ide_bus
= {
2649 .minimum_version_id
= 1,
2650 .minimum_version_id_old
= 1,
2651 .fields
= (VMStateField
[]) {
2652 VMSTATE_UINT8(cmd
, IDEBus
),
2653 VMSTATE_UINT8(unit
, IDEBus
),
2654 VMSTATE_END_OF_LIST()
2658 /***********************************************************/
2659 /* PCI IDE definitions */
2661 static void ide_dma_start(IDEState
*s
, BlockDriverCompletionFunc
*dma_cb
)
2663 BMDMAState
*bm
= s
->bus
->bmdma
;
2667 bm
->dma_cb
= dma_cb
;
2668 bm
->cur_prd_last
= 0;
2669 bm
->cur_prd_addr
= 0;
2670 bm
->cur_prd_len
= 0;
2671 bm
->sector_num
= ide_get_sector(s
);
2672 bm
->nsector
= s
->nsector
;
2673 if (bm
->status
& BM_STATUS_DMAING
) {
2678 static void ide_dma_restart(IDEState
*s
)
2680 BMDMAState
*bm
= s
->bus
->bmdma
;
2681 ide_set_sector(s
, bm
->sector_num
);
2682 s
->io_buffer_index
= 0;
2683 s
->io_buffer_size
= 0;
2684 s
->nsector
= bm
->nsector
;
2685 bm
->cur_addr
= bm
->addr
;
2686 bm
->dma_cb
= ide_write_dma_cb
;
2687 ide_dma_start(s
, bm
->dma_cb
);
2690 void ide_dma_cancel(BMDMAState
*bm
)
2692 if (bm
->status
& BM_STATUS_DMAING
) {
2693 bm
->status
&= ~BM_STATUS_DMAING
;
2694 /* cancel DMA request */
2699 printf("aio_cancel\n");
2701 bdrv_aio_cancel(bm
->aiocb
);