2 * QEMU IDE disk and CD/DVD-ROM Emulator
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu-error.h"
30 #include "qemu-timer.h"
35 #include <hw/ide/internal.h>
37 static const int smart_attributes
[][5] = {
38 /* id, flags, val, wrst, thrsh */
39 { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
40 { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
41 { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
42 { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
43 { 0x00, 0x00, 0x00, 0x00, 0x00}
46 /* XXX: DVDs that could fit on a CD will be reported as a CD */
47 static inline int media_present(IDEState
*s
)
49 return (s
->nb_sectors
> 0);
52 static inline int media_is_dvd(IDEState
*s
)
54 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
57 static inline int media_is_cd(IDEState
*s
)
59 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
62 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
63 static int ide_handle_rw_error(IDEState
*s
, int error
, int op
);
65 static void padstr(char *str
, const char *src
, int len
)
68 for(i
= 0; i
< len
; i
++) {
77 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
80 for(i
= 0; i
< buf_size
; i
++) {
88 static void put_le16(uint16_t *p
, unsigned int v
)
93 static void ide_identify(IDEState
*s
)
99 if (s
->identify_set
) {
100 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
104 memset(s
->io_buffer
, 0, 512);
105 p
= (uint16_t *)s
->io_buffer
;
106 put_le16(p
+ 0, 0x0040);
107 put_le16(p
+ 1, s
->cylinders
);
108 put_le16(p
+ 3, s
->heads
);
109 put_le16(p
+ 4, 512 * s
->sectors
); /* XXX: retired, remove ? */
110 put_le16(p
+ 5, 512); /* XXX: retired, remove ? */
111 put_le16(p
+ 6, s
->sectors
);
112 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
113 put_le16(p
+ 20, 3); /* XXX: retired, remove ? */
114 put_le16(p
+ 21, 512); /* cache size in sectors */
115 put_le16(p
+ 22, 4); /* ecc bytes */
116 padstr((char *)(p
+ 23), s
->version
, 8); /* firmware version */
117 padstr((char *)(p
+ 27), "QEMU HARDDISK", 40); /* model */
118 #if MAX_MULT_SECTORS > 1
119 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
121 put_le16(p
+ 48, 1); /* dword I/O */
122 put_le16(p
+ 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
123 put_le16(p
+ 51, 0x200); /* PIO transfer cycle */
124 put_le16(p
+ 52, 0x200); /* DMA transfer cycle */
125 put_le16(p
+ 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
126 put_le16(p
+ 54, s
->cylinders
);
127 put_le16(p
+ 55, s
->heads
);
128 put_le16(p
+ 56, s
->sectors
);
129 oldsize
= s
->cylinders
* s
->heads
* s
->sectors
;
130 put_le16(p
+ 57, oldsize
);
131 put_le16(p
+ 58, oldsize
>> 16);
133 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
134 put_le16(p
+ 60, s
->nb_sectors
);
135 put_le16(p
+ 61, s
->nb_sectors
>> 16);
136 put_le16(p
+ 62, 0x07); /* single word dma0-2 supported */
137 put_le16(p
+ 63, 0x07); /* mdma0-2 supported */
138 put_le16(p
+ 64, 0x03); /* pio3-4 supported */
139 put_le16(p
+ 65, 120);
140 put_le16(p
+ 66, 120);
141 put_le16(p
+ 67, 120);
142 put_le16(p
+ 68, 120);
145 put_le16(p
+ 75, s
->ncq_queues
- 1);
147 put_le16(p
+ 76, (1 << 8));
150 put_le16(p
+ 80, 0xf0); /* ata3 -> ata6 supported */
151 put_le16(p
+ 81, 0x16); /* conforms to ata5 */
152 /* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */
153 put_le16(p
+ 82, (1 << 14) | (1 << 5) | 1);
154 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
155 put_le16(p
+ 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
156 /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
157 put_le16(p
+ 84, (1 << 14) | 0);
158 /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
159 if (bdrv_enable_write_cache(s
->bs
))
160 put_le16(p
+ 85, (1 << 14) | (1 << 5) | 1);
162 put_le16(p
+ 85, (1 << 14) | 1);
163 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
164 put_le16(p
+ 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
165 /* 14=set to 1, 1=smart self test, 0=smart error logging */
166 put_le16(p
+ 87, (1 << 14) | 0);
167 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
168 put_le16(p
+ 93, 1 | (1 << 14) | 0x2000);
169 put_le16(p
+ 100, s
->nb_sectors
);
170 put_le16(p
+ 101, s
->nb_sectors
>> 16);
171 put_le16(p
+ 102, s
->nb_sectors
>> 32);
172 put_le16(p
+ 103, s
->nb_sectors
>> 48);
173 dev
= s
->unit
? s
->bus
->slave
: s
->bus
->master
;
174 if (dev
&& dev
->conf
.physical_block_size
)
175 put_le16(p
+ 106, 0x6000 | get_physical_block_exp(&dev
->conf
));
177 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
181 static void ide_atapi_identify(IDEState
*s
)
185 if (s
->identify_set
) {
186 memcpy(s
->io_buffer
, s
->identify_data
, sizeof(s
->identify_data
));
190 memset(s
->io_buffer
, 0, 512);
191 p
= (uint16_t *)s
->io_buffer
;
192 /* Removable CDROM, 50us response, 12 byte packets */
193 put_le16(p
+ 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
194 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
195 put_le16(p
+ 20, 3); /* buffer type */
196 put_le16(p
+ 21, 512); /* cache size in sectors */
197 put_le16(p
+ 22, 4); /* ecc bytes */
198 padstr((char *)(p
+ 23), s
->version
, 8); /* firmware version */
199 padstr((char *)(p
+ 27), "QEMU DVD-ROM", 40); /* model */
200 put_le16(p
+ 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
202 put_le16(p
+ 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
203 put_le16(p
+ 53, 7); /* words 64-70, 54-58, 88 valid */
204 put_le16(p
+ 62, 7); /* single word dma0-2 supported */
205 put_le16(p
+ 63, 7); /* mdma0-2 supported */
207 put_le16(p
+ 49, 1 << 9); /* LBA supported, no DMA */
208 put_le16(p
+ 53, 3); /* words 64-70, 54-58 valid */
209 put_le16(p
+ 63, 0x103); /* DMA modes XXX: may be incorrect */
211 put_le16(p
+ 64, 3); /* pio3-4 supported */
212 put_le16(p
+ 65, 0xb4); /* minimum DMA multiword tx cycle time */
213 put_le16(p
+ 66, 0xb4); /* recommended DMA multiword tx cycle time */
214 put_le16(p
+ 67, 0x12c); /* minimum PIO cycle time without flow control */
215 put_le16(p
+ 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
217 put_le16(p
+ 71, 30); /* in ns */
218 put_le16(p
+ 72, 30); /* in ns */
221 put_le16(p
+ 75, s
->ncq_queues
- 1);
223 put_le16(p
+ 76, (1 << 8));
226 put_le16(p
+ 80, 0x1e); /* support up to ATA/ATAPI-4 */
228 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
230 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
234 static void ide_cfata_identify(IDEState
*s
)
239 p
= (uint16_t *) s
->identify_data
;
243 memset(p
, 0, sizeof(s
->identify_data
));
245 cur_sec
= s
->cylinders
* s
->heads
* s
->sectors
;
247 put_le16(p
+ 0, 0x848a); /* CF Storage Card signature */
248 put_le16(p
+ 1, s
->cylinders
); /* Default cylinders */
249 put_le16(p
+ 3, s
->heads
); /* Default heads */
250 put_le16(p
+ 6, s
->sectors
); /* Default sectors per track */
251 put_le16(p
+ 7, s
->nb_sectors
>> 16); /* Sectors per card */
252 put_le16(p
+ 8, s
->nb_sectors
); /* Sectors per card */
253 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
254 put_le16(p
+ 22, 0x0004); /* ECC bytes */
255 padstr((char *) (p
+ 23), s
->version
, 8); /* Firmware Revision */
256 padstr((char *) (p
+ 27), "QEMU MICRODRIVE", 40);/* Model number */
257 #if MAX_MULT_SECTORS > 1
258 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
260 put_le16(p
+ 47, 0x0000);
262 put_le16(p
+ 49, 0x0f00); /* Capabilities */
263 put_le16(p
+ 51, 0x0002); /* PIO cycle timing mode */
264 put_le16(p
+ 52, 0x0001); /* DMA cycle timing mode */
265 put_le16(p
+ 53, 0x0003); /* Translation params valid */
266 put_le16(p
+ 54, s
->cylinders
); /* Current cylinders */
267 put_le16(p
+ 55, s
->heads
); /* Current heads */
268 put_le16(p
+ 56, s
->sectors
); /* Current sectors */
269 put_le16(p
+ 57, cur_sec
); /* Current capacity */
270 put_le16(p
+ 58, cur_sec
>> 16); /* Current capacity */
271 if (s
->mult_sectors
) /* Multiple sector setting */
272 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
273 put_le16(p
+ 60, s
->nb_sectors
); /* Total LBA sectors */
274 put_le16(p
+ 61, s
->nb_sectors
>> 16); /* Total LBA sectors */
275 put_le16(p
+ 63, 0x0203); /* Multiword DMA capability */
276 put_le16(p
+ 64, 0x0001); /* Flow Control PIO support */
277 put_le16(p
+ 65, 0x0096); /* Min. Multiword DMA cycle */
278 put_le16(p
+ 66, 0x0096); /* Rec. Multiword DMA cycle */
279 put_le16(p
+ 68, 0x00b4); /* Min. PIO cycle time */
280 put_le16(p
+ 82, 0x400c); /* Command Set supported */
281 put_le16(p
+ 83, 0x7068); /* Command Set supported */
282 put_le16(p
+ 84, 0x4000); /* Features supported */
283 put_le16(p
+ 85, 0x000c); /* Command Set enabled */
284 put_le16(p
+ 86, 0x7044); /* Command Set enabled */
285 put_le16(p
+ 87, 0x4000); /* Features enabled */
286 put_le16(p
+ 91, 0x4060); /* Current APM level */
287 put_le16(p
+ 129, 0x0002); /* Current features option */
288 put_le16(p
+ 130, 0x0005); /* Reassigned sectors */
289 put_le16(p
+ 131, 0x0001); /* Initial power mode */
290 put_le16(p
+ 132, 0x0000); /* User signature */
291 put_le16(p
+ 160, 0x8100); /* Power requirement */
292 put_le16(p
+ 161, 0x8001); /* CF command set */
297 memcpy(s
->io_buffer
, p
, sizeof(s
->identify_data
));
300 static void ide_set_signature(IDEState
*s
)
302 s
->select
&= 0xf0; /* clear head */
306 if (s
->drive_kind
== IDE_CD
) {
318 static inline void ide_abort_command(IDEState
*s
)
320 s
->status
= READY_STAT
| ERR_STAT
;
324 /* prepare data transfer and tell what to do after */
325 static void ide_transfer_start(IDEState
*s
, uint8_t *buf
, int size
,
326 EndTransferFunc
*end_transfer_func
)
328 s
->end_transfer_func
= end_transfer_func
;
330 s
->data_end
= buf
+ size
;
331 if (!(s
->status
& ERR_STAT
)) {
332 s
->status
|= DRQ_STAT
;
334 s
->bus
->dma
->ops
->start_transfer(s
->bus
->dma
);
337 static void ide_transfer_stop(IDEState
*s
)
339 s
->end_transfer_func
= ide_transfer_stop
;
340 s
->data_ptr
= s
->io_buffer
;
341 s
->data_end
= s
->io_buffer
;
342 s
->status
&= ~DRQ_STAT
;
345 int64_t ide_get_sector(IDEState
*s
)
348 if (s
->select
& 0x40) {
351 sector_num
= ((s
->select
& 0x0f) << 24) | (s
->hcyl
<< 16) |
352 (s
->lcyl
<< 8) | s
->sector
;
354 sector_num
= ((int64_t)s
->hob_hcyl
<< 40) |
355 ((int64_t) s
->hob_lcyl
<< 32) |
356 ((int64_t) s
->hob_sector
<< 24) |
357 ((int64_t) s
->hcyl
<< 16) |
358 ((int64_t) s
->lcyl
<< 8) | s
->sector
;
361 sector_num
= ((s
->hcyl
<< 8) | s
->lcyl
) * s
->heads
* s
->sectors
+
362 (s
->select
& 0x0f) * s
->sectors
+ (s
->sector
- 1);
367 void ide_set_sector(IDEState
*s
, int64_t sector_num
)
370 if (s
->select
& 0x40) {
372 s
->select
= (s
->select
& 0xf0) | (sector_num
>> 24);
373 s
->hcyl
= (sector_num
>> 16);
374 s
->lcyl
= (sector_num
>> 8);
375 s
->sector
= (sector_num
);
377 s
->sector
= sector_num
;
378 s
->lcyl
= sector_num
>> 8;
379 s
->hcyl
= sector_num
>> 16;
380 s
->hob_sector
= sector_num
>> 24;
381 s
->hob_lcyl
= sector_num
>> 32;
382 s
->hob_hcyl
= sector_num
>> 40;
385 cyl
= sector_num
/ (s
->heads
* s
->sectors
);
386 r
= sector_num
% (s
->heads
* s
->sectors
);
389 s
->select
= (s
->select
& 0xf0) | ((r
/ s
->sectors
) & 0x0f);
390 s
->sector
= (r
% s
->sectors
) + 1;
394 static void ide_rw_error(IDEState
*s
) {
395 ide_abort_command(s
);
399 void ide_sector_read(IDEState
*s
)
404 s
->status
= READY_STAT
| SEEK_STAT
;
405 s
->error
= 0; /* not needed by IDE spec, but needed by Windows */
406 sector_num
= ide_get_sector(s
);
409 /* no more sector to read from disk */
410 ide_transfer_stop(s
);
412 #if defined(DEBUG_IDE)
413 printf("read sector=%" PRId64
"\n", sector_num
);
415 if (n
> s
->req_nb_sectors
)
416 n
= s
->req_nb_sectors
;
417 ret
= bdrv_read(s
->bs
, sector_num
, s
->io_buffer
, n
);
419 if (ide_handle_rw_error(s
, -ret
,
420 BM_STATUS_PIO_RETRY
| BM_STATUS_RETRY_READ
))
425 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_read
);
427 ide_set_sector(s
, sector_num
+ n
);
432 static void dma_buf_commit(IDEState
*s
, int is_write
)
434 qemu_sglist_destroy(&s
->sg
);
437 static void ide_set_inactive(IDEState
*s
)
439 s
->bus
->dma
->aiocb
= NULL
;
440 s
->bus
->dma
->ops
->set_inactive(s
->bus
->dma
);
443 void ide_dma_error(IDEState
*s
)
445 ide_transfer_stop(s
);
447 s
->status
= READY_STAT
| ERR_STAT
;
449 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
453 static int ide_handle_rw_error(IDEState
*s
, int error
, int op
)
455 int is_read
= (op
& BM_STATUS_RETRY_READ
);
456 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
458 if (action
== BLOCK_ERR_IGNORE
) {
459 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
463 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
464 || action
== BLOCK_ERR_STOP_ANY
) {
465 s
->bus
->dma
->ops
->set_unit(s
->bus
->dma
, s
->unit
);
466 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, op
);
467 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
468 vm_stop(VMSTOP_DISKFULL
);
470 if (op
& BM_STATUS_DMA_RETRY
) {
471 dma_buf_commit(s
, 0);
476 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
482 void ide_dma_cb(void *opaque
, int ret
)
484 IDEState
*s
= opaque
;
490 int op
= BM_STATUS_DMA_RETRY
;
493 op
|= BM_STATUS_RETRY_READ
;
494 if (ide_handle_rw_error(s
, -ret
, op
)) {
499 n
= s
->io_buffer_size
>> 9;
500 sector_num
= ide_get_sector(s
);
502 dma_buf_commit(s
, s
->is_read
);
504 ide_set_sector(s
, sector_num
);
508 /* end of transfer ? */
509 if (s
->nsector
== 0) {
510 s
->status
= READY_STAT
| SEEK_STAT
;
515 /* launch next transfer */
517 s
->io_buffer_index
= 0;
518 s
->io_buffer_size
= n
* 512;
519 if (s
->bus
->dma
->ops
->prepare_buf(s
->bus
->dma
, s
->is_read
) == 0)
523 printf("ide_dma_cb: sector_num=%" PRId64
" n=%d, is_read=%d\n",
524 sector_num
, n
, s
->is_read
);
528 s
->bus
->dma
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
,
531 s
->bus
->dma
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
,
535 if (!s
->bus
->dma
->aiocb
) {
537 goto handle_rw_error
;
542 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
546 static void ide_sector_start_dma(IDEState
*s
, int is_read
)
548 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
549 s
->io_buffer_index
= 0;
550 s
->io_buffer_size
= 0;
551 s
->is_read
= is_read
;
552 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
, ide_dma_cb
);
555 static void ide_sector_write_timer_cb(void *opaque
)
557 IDEState
*s
= opaque
;
561 void ide_sector_write(IDEState
*s
)
566 s
->status
= READY_STAT
| SEEK_STAT
;
567 sector_num
= ide_get_sector(s
);
568 #if defined(DEBUG_IDE)
569 printf("write sector=%" PRId64
"\n", sector_num
);
572 if (n
> s
->req_nb_sectors
)
573 n
= s
->req_nb_sectors
;
574 ret
= bdrv_write(s
->bs
, sector_num
, s
->io_buffer
, n
);
577 if (ide_handle_rw_error(s
, -ret
, BM_STATUS_PIO_RETRY
))
582 if (s
->nsector
== 0) {
583 /* no more sectors to write */
584 ide_transfer_stop(s
);
587 if (n1
> s
->req_nb_sectors
)
588 n1
= s
->req_nb_sectors
;
589 ide_transfer_start(s
, s
->io_buffer
, 512 * n1
, ide_sector_write
);
591 ide_set_sector(s
, sector_num
+ n
);
593 if (win2k_install_hack
&& ((++s
->irq_count
% 16) == 0)) {
594 /* It seems there is a bug in the Windows 2000 installer HDD
595 IDE driver which fills the disk with empty logs when the
596 IDE write IRQ comes too early. This hack tries to correct
597 that at the expense of slower write performances. Use this
598 option _only_ to install Windows 2000. You must disable it
600 qemu_mod_timer(s
->sector_write_timer
,
601 qemu_get_clock(vm_clock
) + (get_ticks_per_sec() / 1000));
607 void ide_atapi_cmd_ok(IDEState
*s
)
610 s
->status
= READY_STAT
| SEEK_STAT
;
611 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
615 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
617 #ifdef DEBUG_IDE_ATAPI
618 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
620 s
->error
= sense_key
<< 4;
621 s
->status
= READY_STAT
| ERR_STAT
;
622 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
623 s
->sense_key
= sense_key
;
628 static void ide_atapi_cmd_check_status(IDEState
*s
)
630 #ifdef DEBUG_IDE_ATAPI
631 printf("atapi_cmd_check_status\n");
633 s
->error
= MC_ERR
| (SENSE_UNIT_ATTENTION
<< 4);
634 s
->status
= ERR_STAT
;
639 static void ide_flush_cb(void *opaque
, int ret
)
641 IDEState
*s
= opaque
;
644 /* XXX: What sector number to set here? */
645 if (ide_handle_rw_error(s
, -ret
, BM_STATUS_RETRY_FLUSH
)) {
650 s
->status
= READY_STAT
| SEEK_STAT
;
654 void ide_flush_cache(IDEState
*s
)
656 BlockDriverAIOCB
*acb
;
663 acb
= bdrv_aio_flush(s
->bs
, ide_flush_cb
, s
);
665 ide_flush_cb(s
, -EIO
);
669 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
675 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
683 static inline int ube16_to_cpu(const uint8_t *buf
)
685 return (buf
[0] << 8) | buf
[1];
688 static inline int ube32_to_cpu(const uint8_t *buf
)
690 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
693 static void lba_to_msf(uint8_t *buf
, int lba
)
696 buf
[0] = (lba
/ 75) / 60;
697 buf
[1] = (lba
/ 75) % 60;
701 static void cd_data_to_raw(uint8_t *buf
, int lba
)
705 memset(buf
+ 1, 0xff, 10);
709 lba_to_msf(buf
, lba
);
710 buf
[3] = 0x01; /* mode 1 data */
714 /* XXX: ECC not computed */
718 static int cd_read_sector(BlockDriverState
*bs
, int lba
, uint8_t *buf
,
723 switch(sector_size
) {
725 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
, 4);
728 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
+ 16, 4);
731 cd_data_to_raw(buf
, lba
);
740 void ide_atapi_io_error(IDEState
*s
, int ret
)
742 /* XXX: handle more errors */
743 if (ret
== -ENOMEDIUM
) {
744 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
745 ASC_MEDIUM_NOT_PRESENT
);
747 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
748 ASC_LOGICAL_BLOCK_OOR
);
752 /* The whole ATAPI transfer logic is handled in this function */
753 static void ide_atapi_cmd_reply_end(IDEState
*s
)
755 int byte_count_limit
, size
, ret
;
756 #ifdef DEBUG_IDE_ATAPI
757 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
758 s
->packet_transfer_size
,
759 s
->elementary_transfer_size
,
762 if (s
->packet_transfer_size
<= 0) {
763 /* end of transfer */
764 ide_transfer_stop(s
);
765 s
->status
= READY_STAT
| SEEK_STAT
;
766 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
768 #ifdef DEBUG_IDE_ATAPI
769 printf("status=0x%x\n", s
->status
);
772 /* see if a new sector must be read */
773 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
774 ret
= cd_read_sector(s
->bs
, s
->lba
, s
->io_buffer
, s
->cd_sector_size
);
776 ide_transfer_stop(s
);
777 ide_atapi_io_error(s
, ret
);
781 s
->io_buffer_index
= 0;
783 if (s
->elementary_transfer_size
> 0) {
784 /* there are some data left to transmit in this elementary
786 size
= s
->cd_sector_size
- s
->io_buffer_index
;
787 if (size
> s
->elementary_transfer_size
)
788 size
= s
->elementary_transfer_size
;
789 s
->packet_transfer_size
-= size
;
790 s
->elementary_transfer_size
-= size
;
791 s
->io_buffer_index
+= size
;
792 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
793 size
, ide_atapi_cmd_reply_end
);
795 /* a new transfer is needed */
796 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
797 byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
798 #ifdef DEBUG_IDE_ATAPI
799 printf("byte_count_limit=%d\n", byte_count_limit
);
801 if (byte_count_limit
== 0xffff)
803 size
= s
->packet_transfer_size
;
804 if (size
> byte_count_limit
) {
805 /* byte count limit must be even if this case */
806 if (byte_count_limit
& 1)
808 size
= byte_count_limit
;
812 s
->elementary_transfer_size
= size
;
813 /* we cannot transmit more than one sector at a time */
815 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
816 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
818 s
->packet_transfer_size
-= size
;
819 s
->elementary_transfer_size
-= size
;
820 s
->io_buffer_index
+= size
;
821 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
822 size
, ide_atapi_cmd_reply_end
);
824 #ifdef DEBUG_IDE_ATAPI
825 printf("status=0x%x\n", s
->status
);
831 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
832 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
836 s
->lba
= -1; /* no sector read */
837 s
->packet_transfer_size
= size
;
838 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
839 s
->elementary_transfer_size
= 0;
840 s
->io_buffer_index
= 0;
843 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
844 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
,
845 ide_atapi_cmd_read_dma_cb
);
847 s
->status
= READY_STAT
| SEEK_STAT
;
848 ide_atapi_cmd_reply_end(s
);
852 /* start a CD-CDROM read command */
853 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
857 s
->packet_transfer_size
= nb_sectors
* sector_size
;
858 s
->elementary_transfer_size
= 0;
859 s
->io_buffer_index
= sector_size
;
860 s
->cd_sector_size
= sector_size
;
862 s
->status
= READY_STAT
| SEEK_STAT
;
863 ide_atapi_cmd_reply_end(s
);
866 /* ATAPI DMA support */
868 /* XXX: handle read errors */
869 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
871 IDEState
*s
= opaque
;
875 ide_atapi_io_error(s
, ret
);
879 if (s
->io_buffer_size
> 0) {
881 * For a cdrom read sector command (s->lba != -1),
882 * adjust the lba for the next s->io_buffer_size chunk
883 * and dma the current chunk.
884 * For a command != read (s->lba == -1), just transfer
888 if (s
->cd_sector_size
== 2352) {
890 cd_data_to_raw(s
->io_buffer
, s
->lba
);
892 n
= s
->io_buffer_size
>> 11;
896 s
->packet_transfer_size
-= s
->io_buffer_size
;
897 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
901 if (s
->packet_transfer_size
<= 0) {
902 s
->status
= READY_STAT
| SEEK_STAT
;
903 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
906 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
911 s
->io_buffer_index
= 0;
912 if (s
->cd_sector_size
== 2352) {
914 s
->io_buffer_size
= s
->cd_sector_size
;
917 n
= s
->packet_transfer_size
>> 11;
918 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
919 n
= (IDE_DMA_BUF_SECTORS
/ 4);
920 s
->io_buffer_size
= n
* 2048;
924 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
926 s
->bus
->dma
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
927 s
->bus
->dma
->iov
.iov_len
= n
* 4 * 512;
928 qemu_iovec_init_external(&s
->bus
->dma
->qiov
, &s
->bus
->dma
->iov
, 1);
929 s
->bus
->dma
->aiocb
= bdrv_aio_readv(s
->bs
, (int64_t)s
->lba
<< 2,
930 &s
->bus
->dma
->qiov
, n
* 4,
931 ide_atapi_cmd_read_dma_cb
, s
);
932 if (!s
->bus
->dma
->aiocb
) {
933 /* Note: media not present is the most likely case */
934 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
935 ASC_MEDIUM_NOT_PRESENT
);
940 /* start a CD-CDROM read command with DMA */
941 /* XXX: test if DMA is available */
942 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
946 s
->packet_transfer_size
= nb_sectors
* sector_size
;
947 s
->io_buffer_index
= 0;
948 s
->io_buffer_size
= 0;
949 s
->cd_sector_size
= sector_size
;
951 /* XXX: check if BUSY_STAT should be set */
952 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
953 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
,
954 ide_atapi_cmd_read_dma_cb
);
957 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
960 #ifdef DEBUG_IDE_ATAPI
961 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
965 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
967 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
971 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
974 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
976 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
977 cpu_to_ube16 (buf_profile
, profile
);
978 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
980 /* each profile adds 4 bytes to the response */
982 buf
[11] += 4; /* Additional Length */
987 static int ide_dvd_read_structure(IDEState
*s
, int format
,
988 const uint8_t *packet
, uint8_t *buf
)
991 case 0x0: /* Physical format information */
993 int layer
= packet
[6];
994 uint64_t total_sectors
;
997 return -ASC_INV_FIELD_IN_CMD_PACKET
;
999 bdrv_get_geometry(s
->bs
, &total_sectors
);
1000 total_sectors
>>= 2;
1001 if (total_sectors
== 0)
1002 return -ASC_MEDIUM_NOT_PRESENT
;
1004 buf
[4] = 1; /* DVD-ROM, part version 1 */
1005 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1006 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1007 buf
[7] = 0; /* default densities */
1009 /* FIXME: 0x30000 per spec? */
1010 cpu_to_ube32(buf
+ 8, 0); /* start sector */
1011 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
1012 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
1014 /* Size of buffer, not including 2 byte size field */
1015 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1017 /* 2k data + 4 byte header */
1021 case 0x01: /* DVD copyright information */
1022 buf
[4] = 0; /* no copyright data */
1023 buf
[5] = 0; /* no region restrictions */
1025 /* Size of buffer, not including 2 byte size field */
1026 cpu_to_be16wu((uint16_t *)buf
, 4 + 2);
1028 /* 4 byte header + 4 byte data */
1031 case 0x03: /* BCA information - invalid field for no BCA info */
1032 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1034 case 0x04: /* DVD disc manufacturing information */
1035 /* Size of buffer, not including 2 byte size field */
1036 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1038 /* 2k data + 4 byte header */
1043 * This lists all the command capabilities above. Add new ones
1044 * in order and update the length and buffer return values.
1047 buf
[4] = 0x00; /* Physical format */
1048 buf
[5] = 0x40; /* Not writable, is readable */
1049 cpu_to_be16wu((uint16_t *)(buf
+ 6), 2048 + 4);
1051 buf
[8] = 0x01; /* Copyright info */
1052 buf
[9] = 0x40; /* Not writable, is readable */
1053 cpu_to_be16wu((uint16_t *)(buf
+ 10), 4 + 4);
1055 buf
[12] = 0x03; /* BCA info */
1056 buf
[13] = 0x40; /* Not writable, is readable */
1057 cpu_to_be16wu((uint16_t *)(buf
+ 14), 188 + 4);
1059 buf
[16] = 0x04; /* Manufacturing info */
1060 buf
[17] = 0x40; /* Not writable, is readable */
1061 cpu_to_be16wu((uint16_t *)(buf
+ 18), 2048 + 4);
1063 /* Size of buffer, not including 2 byte size field */
1064 cpu_to_be16wu((uint16_t *)buf
, 16 + 2);
1066 /* data written + 4 byte header */
1069 default: /* TODO: formats beyond DVD-ROM requires */
1070 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1074 static void ide_atapi_cmd(IDEState
*s
)
1076 const uint8_t *packet
;
1080 packet
= s
->io_buffer
;
1082 #ifdef DEBUG_IDE_ATAPI
1085 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1086 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1087 printf(" %02x", packet
[i
]);
1092 /* If there's a UNIT_ATTENTION condition pending, only
1093 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1094 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
1095 s
->io_buffer
[0] != GPCMD_REQUEST_SENSE
&&
1096 s
->io_buffer
[0] != GPCMD_INQUIRY
) {
1097 ide_atapi_cmd_check_status(s
);
1100 switch(s
->io_buffer
[0]) {
1101 case GPCMD_TEST_UNIT_READY
:
1102 if (bdrv_is_inserted(s
->bs
) && !s
->cdrom_changed
) {
1103 ide_atapi_cmd_ok(s
);
1105 s
->cdrom_changed
= 0;
1106 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1107 ASC_MEDIUM_NOT_PRESENT
);
1110 case GPCMD_MODE_SENSE_6
:
1111 case GPCMD_MODE_SENSE_10
:
1114 if (packet
[0] == GPCMD_MODE_SENSE_10
)
1115 max_len
= ube16_to_cpu(packet
+ 7);
1117 max_len
= packet
[4];
1118 action
= packet
[2] >> 6;
1119 code
= packet
[2] & 0x3f;
1121 case 0: /* current values */
1123 case GPMODE_R_W_ERROR_PAGE
: /* error recovery */
1124 cpu_to_ube16(&buf
[0], 16 + 6);
1140 ide_atapi_cmd_reply(s
, 16, max_len
);
1142 case GPMODE_AUDIO_CTL_PAGE
:
1143 cpu_to_ube16(&buf
[0], 24 + 6);
1151 /* Fill with CDROM audio volume */
1157 ide_atapi_cmd_reply(s
, 24, max_len
);
1159 case GPMODE_CAPABILITIES_PAGE
:
1160 cpu_to_ube16(&buf
[0], 28 + 6);
1173 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1174 code checks for this to automount media. */
1177 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
1178 if (bdrv_is_locked(s
->bs
))
1181 cpu_to_ube16(&buf
[16], 706);
1184 cpu_to_ube16(&buf
[20], 512);
1185 cpu_to_ube16(&buf
[22], 706);
1190 ide_atapi_cmd_reply(s
, 28, max_len
);
1196 case 1: /* changeable values */
1198 case 2: /* default values */
1201 case 3: /* saved values */
1202 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1203 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
1208 case GPCMD_REQUEST_SENSE
:
1209 max_len
= packet
[4];
1211 buf
[0] = 0x70 | (1 << 7);
1212 buf
[2] = s
->sense_key
;
1215 if (s
->sense_key
== SENSE_UNIT_ATTENTION
)
1216 s
->sense_key
= SENSE_NONE
;
1217 ide_atapi_cmd_reply(s
, 18, max_len
);
1219 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
:
1220 if (bdrv_is_inserted(s
->bs
)) {
1221 bdrv_set_locked(s
->bs
, packet
[4] & 1);
1222 ide_atapi_cmd_ok(s
);
1224 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1225 ASC_MEDIUM_NOT_PRESENT
);
1231 int nb_sectors
, lba
;
1233 if (packet
[0] == GPCMD_READ_10
)
1234 nb_sectors
= ube16_to_cpu(packet
+ 7);
1236 nb_sectors
= ube32_to_cpu(packet
+ 6);
1237 lba
= ube32_to_cpu(packet
+ 2);
1238 if (nb_sectors
== 0) {
1239 ide_atapi_cmd_ok(s
);
1242 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1247 int nb_sectors
, lba
, transfer_request
;
1249 nb_sectors
= (packet
[6] << 16) | (packet
[7] << 8) | packet
[8];
1250 lba
= ube32_to_cpu(packet
+ 2);
1251 if (nb_sectors
== 0) {
1252 ide_atapi_cmd_ok(s
);
1255 transfer_request
= packet
[9];
1256 switch(transfer_request
& 0xf8) {
1259 ide_atapi_cmd_ok(s
);
1263 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1267 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1270 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1271 ASC_INV_FIELD_IN_CMD_PACKET
);
1279 uint64_t total_sectors
;
1281 bdrv_get_geometry(s
->bs
, &total_sectors
);
1282 total_sectors
>>= 2;
1283 if (total_sectors
== 0) {
1284 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1285 ASC_MEDIUM_NOT_PRESENT
);
1288 lba
= ube32_to_cpu(packet
+ 2);
1289 if (lba
>= total_sectors
) {
1290 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1291 ASC_LOGICAL_BLOCK_OOR
);
1294 ide_atapi_cmd_ok(s
);
1297 case GPCMD_START_STOP_UNIT
:
1299 int start
, eject
, err
= 0;
1300 start
= packet
[4] & 1;
1301 eject
= (packet
[4] >> 1) & 1;
1304 err
= bdrv_eject(s
->bs
, !start
);
1309 ide_atapi_cmd_ok(s
);
1312 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1313 ASC_MEDIA_REMOVAL_PREVENTED
);
1316 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1317 ASC_MEDIUM_NOT_PRESENT
);
1322 case GPCMD_MECHANISM_STATUS
:
1324 max_len
= ube16_to_cpu(packet
+ 8);
1325 cpu_to_ube16(buf
, 0);
1326 /* no current LBA */
1331 cpu_to_ube16(buf
+ 6, 0);
1332 ide_atapi_cmd_reply(s
, 8, max_len
);
1335 case GPCMD_READ_TOC_PMA_ATIP
:
1337 int format
, msf
, start_track
, len
;
1338 uint64_t total_sectors
;
1340 bdrv_get_geometry(s
->bs
, &total_sectors
);
1341 total_sectors
>>= 2;
1342 if (total_sectors
== 0) {
1343 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1344 ASC_MEDIUM_NOT_PRESENT
);
1347 max_len
= ube16_to_cpu(packet
+ 7);
1348 format
= packet
[9] >> 6;
1349 msf
= (packet
[1] >> 1) & 1;
1350 start_track
= packet
[6];
1353 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1356 ide_atapi_cmd_reply(s
, len
, max_len
);
1359 /* multi session : only a single session defined */
1364 ide_atapi_cmd_reply(s
, 12, max_len
);
1367 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1370 ide_atapi_cmd_reply(s
, len
, max_len
);
1374 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1375 ASC_INV_FIELD_IN_CMD_PACKET
);
1380 case GPCMD_READ_CDVD_CAPACITY
:
1382 uint64_t total_sectors
;
1384 bdrv_get_geometry(s
->bs
, &total_sectors
);
1385 total_sectors
>>= 2;
1386 if (total_sectors
== 0) {
1387 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1388 ASC_MEDIUM_NOT_PRESENT
);
1391 /* NOTE: it is really the number of sectors minus 1 */
1392 cpu_to_ube32(buf
, total_sectors
- 1);
1393 cpu_to_ube32(buf
+ 4, 2048);
1394 ide_atapi_cmd_reply(s
, 8, 8);
1397 case GPCMD_READ_DVD_STRUCTURE
:
1399 int media
= packet
[1];
1400 int format
= packet
[7];
1403 max_len
= ube16_to_cpu(packet
+ 8);
1405 if (format
< 0xff) {
1406 if (media_is_cd(s
)) {
1407 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1408 ASC_INCOMPATIBLE_FORMAT
);
1410 } else if (!media_present(s
)) {
1411 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1412 ASC_INV_FIELD_IN_CMD_PACKET
);
1417 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1418 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1424 ret
= ide_dvd_read_structure(s
, format
, packet
, buf
);
1427 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
, -ret
);
1429 ide_atapi_cmd_reply(s
, ret
, max_len
);
1433 /* TODO: BD support, fall through for now */
1435 /* Generic disk structures */
1436 case 0x80: /* TODO: AACS volume identifier */
1437 case 0x81: /* TODO: AACS media serial number */
1438 case 0x82: /* TODO: AACS media identifier */
1439 case 0x83: /* TODO: AACS media key block */
1440 case 0x90: /* TODO: List of recognized format layers */
1441 case 0xc0: /* TODO: Write protection status */
1443 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1444 ASC_INV_FIELD_IN_CMD_PACKET
);
1449 case GPCMD_SET_SPEED
:
1450 ide_atapi_cmd_ok(s
);
1453 max_len
= packet
[4];
1454 buf
[0] = 0x05; /* CD-ROM */
1455 buf
[1] = 0x80; /* removable */
1456 buf
[2] = 0x00; /* ISO */
1457 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1458 buf
[4] = 31; /* additional length */
1459 buf
[5] = 0; /* reserved */
1460 buf
[6] = 0; /* reserved */
1461 buf
[7] = 0; /* reserved */
1462 padstr8(buf
+ 8, 8, "QEMU");
1463 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
1464 padstr8(buf
+ 32, 4, s
->version
);
1465 ide_atapi_cmd_reply(s
, 36, max_len
);
1467 case GPCMD_GET_CONFIGURATION
:
1472 /* only feature 0 is supported */
1473 if (packet
[2] != 0 || packet
[3] != 0) {
1474 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1475 ASC_INV_FIELD_IN_CMD_PACKET
);
1479 /* XXX: could result in alignment problems in some architectures */
1480 max_len
= ube16_to_cpu(packet
+ 7);
1483 * XXX: avoid overflow for io_buffer if max_len is bigger than
1484 * the size of that buffer (dimensioned to max number of
1485 * sectors to transfer at once)
1487 * Only a problem if the feature/profiles grow.
1489 if (max_len
> 512) /* XXX: assume 1 sector */
1492 memset(buf
, 0, max_len
);
1494 * the number of sectors from the media tells us which profile
1495 * to use as current. 0 means there is no media
1497 if (media_is_dvd(s
))
1498 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
1499 else if (media_is_cd(s
))
1500 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
1502 buf
[10] = 0x02 | 0x01; /* persistent and current */
1503 len
= 12; /* headers: 8 + 4 */
1504 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
1505 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
1506 cpu_to_ube32(buf
, len
- 4); /* data length */
1508 ide_atapi_cmd_reply(s
, len
, max_len
);
1511 case GPCMD_GET_EVENT_STATUS_NOTIFICATION
:
1512 max_len
= ube16_to_cpu(packet
+ 7);
1514 if (packet
[1] & 0x01) { /* polling */
1515 /* We don't support any event class (yet). */
1516 cpu_to_ube16(buf
, 0x00); /* No event descriptor returned */
1517 buf
[2] = 0x80; /* No Event Available (NEA) */
1518 buf
[3] = 0x00; /* Empty supported event classes */
1519 ide_atapi_cmd_reply(s
, 4, max_len
);
1520 } else { /* asynchronous mode */
1521 /* Only polling is supported, asynchronous mode is not. */
1522 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1523 ASC_INV_FIELD_IN_CMD_PACKET
);
1527 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1528 ASC_ILLEGAL_OPCODE
);
1533 static void ide_cfata_metadata_inquiry(IDEState
*s
)
1538 p
= (uint16_t *) s
->io_buffer
;
1539 memset(p
, 0, 0x200);
1540 spd
= ((s
->mdata_size
- 1) >> 9) + 1;
1542 put_le16(p
+ 0, 0x0001); /* Data format revision */
1543 put_le16(p
+ 1, 0x0000); /* Media property: silicon */
1544 put_le16(p
+ 2, s
->media_changed
); /* Media status */
1545 put_le16(p
+ 3, s
->mdata_size
& 0xffff); /* Capacity in bytes (low) */
1546 put_le16(p
+ 4, s
->mdata_size
>> 16); /* Capacity in bytes (high) */
1547 put_le16(p
+ 5, spd
& 0xffff); /* Sectors per device (low) */
1548 put_le16(p
+ 6, spd
>> 16); /* Sectors per device (high) */
1551 static void ide_cfata_metadata_read(IDEState
*s
)
1555 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1556 s
->status
= ERR_STAT
;
1557 s
->error
= ABRT_ERR
;
1561 p
= (uint16_t *) s
->io_buffer
;
1562 memset(p
, 0, 0x200);
1564 put_le16(p
+ 0, s
->media_changed
); /* Media status */
1565 memcpy(p
+ 1, s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1566 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1567 s
->nsector
<< 9), 0x200 - 2));
1570 static void ide_cfata_metadata_write(IDEState
*s
)
1572 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1573 s
->status
= ERR_STAT
;
1574 s
->error
= ABRT_ERR
;
1578 s
->media_changed
= 0;
1580 memcpy(s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1582 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1583 s
->nsector
<< 9), 0x200 - 2));
1586 /* called when the inserted state of the media has changed */
1587 static void cdrom_change_cb(void *opaque
, int reason
)
1589 IDEState
*s
= opaque
;
1590 uint64_t nb_sectors
;
1592 if (!(reason
& CHANGE_MEDIA
)) {
1596 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1597 s
->nb_sectors
= nb_sectors
;
1599 s
->sense_key
= SENSE_UNIT_ATTENTION
;
1600 s
->asc
= ASC_MEDIUM_MAY_HAVE_CHANGED
;
1601 s
->cdrom_changed
= 1;
1602 ide_set_irq(s
->bus
);
1605 static void ide_cmd_lba48_transform(IDEState
*s
, int lba48
)
1609 /* handle the 'magic' 0 nsector count conversion here. to avoid
1610 * fiddling with the rest of the read logic, we just store the
1611 * full sector count in ->nsector and ignore ->hob_nsector from now
1617 if (!s
->nsector
&& !s
->hob_nsector
)
1620 int lo
= s
->nsector
;
1621 int hi
= s
->hob_nsector
;
1623 s
->nsector
= (hi
<< 8) | lo
;
1628 static void ide_clear_hob(IDEBus
*bus
)
1630 /* any write clears HOB high bit of device control register */
1631 bus
->ifs
[0].select
&= ~(1 << 7);
1632 bus
->ifs
[1].select
&= ~(1 << 7);
1635 void ide_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1637 IDEBus
*bus
= opaque
;
1640 printf("IDE: write addr=0x%x val=0x%02x\n", addr
, val
);
1645 /* ignore writes to command block while busy with previous command */
1646 if (addr
!= 7 && (idebus_active_if(bus
)->status
& (BUSY_STAT
|DRQ_STAT
)))
1654 /* NOTE: data is written to the two drives */
1655 bus
->ifs
[0].hob_feature
= bus
->ifs
[0].feature
;
1656 bus
->ifs
[1].hob_feature
= bus
->ifs
[1].feature
;
1657 bus
->ifs
[0].feature
= val
;
1658 bus
->ifs
[1].feature
= val
;
1662 bus
->ifs
[0].hob_nsector
= bus
->ifs
[0].nsector
;
1663 bus
->ifs
[1].hob_nsector
= bus
->ifs
[1].nsector
;
1664 bus
->ifs
[0].nsector
= val
;
1665 bus
->ifs
[1].nsector
= val
;
1669 bus
->ifs
[0].hob_sector
= bus
->ifs
[0].sector
;
1670 bus
->ifs
[1].hob_sector
= bus
->ifs
[1].sector
;
1671 bus
->ifs
[0].sector
= val
;
1672 bus
->ifs
[1].sector
= val
;
1676 bus
->ifs
[0].hob_lcyl
= bus
->ifs
[0].lcyl
;
1677 bus
->ifs
[1].hob_lcyl
= bus
->ifs
[1].lcyl
;
1678 bus
->ifs
[0].lcyl
= val
;
1679 bus
->ifs
[1].lcyl
= val
;
1683 bus
->ifs
[0].hob_hcyl
= bus
->ifs
[0].hcyl
;
1684 bus
->ifs
[1].hob_hcyl
= bus
->ifs
[1].hcyl
;
1685 bus
->ifs
[0].hcyl
= val
;
1686 bus
->ifs
[1].hcyl
= val
;
1689 /* FIXME: HOB readback uses bit 7 */
1690 bus
->ifs
[0].select
= (val
& ~0x10) | 0xa0;
1691 bus
->ifs
[1].select
= (val
| 0x10) | 0xa0;
1693 bus
->unit
= (val
>> 4) & 1;
1698 ide_exec_cmd(bus
, val
);
1704 void ide_exec_cmd(IDEBus
*bus
, uint32_t val
)
1710 #if defined(DEBUG_IDE)
1711 printf("ide: CMD=%02x\n", val
);
1713 s
= idebus_active_if(bus
);
1714 /* ignore commands to non existant slave */
1715 if (s
!= bus
->ifs
&& !s
->bs
)
1718 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1719 if ((s
->status
& (BUSY_STAT
|DRQ_STAT
)) && val
!= WIN_DEVICE_RESET
)
1724 if (s
->bs
&& s
->drive_kind
!= IDE_CD
) {
1725 if (s
->drive_kind
!= IDE_CFATA
)
1728 ide_cfata_identify(s
);
1729 s
->status
= READY_STAT
| SEEK_STAT
;
1730 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1732 if (s
->drive_kind
== IDE_CD
) {
1733 ide_set_signature(s
);
1735 ide_abort_command(s
);
1737 ide_set_irq(s
->bus
);
1742 s
->status
= READY_STAT
| SEEK_STAT
;
1743 ide_set_irq(s
->bus
);
1746 if (s
->drive_kind
== IDE_CFATA
&& s
->nsector
== 0) {
1747 /* Disable Read and Write Multiple */
1748 s
->mult_sectors
= 0;
1749 s
->status
= READY_STAT
| SEEK_STAT
;
1750 } else if ((s
->nsector
& 0xff) != 0 &&
1751 ((s
->nsector
& 0xff) > MAX_MULT_SECTORS
||
1752 (s
->nsector
& (s
->nsector
- 1)) != 0)) {
1753 ide_abort_command(s
);
1755 s
->mult_sectors
= s
->nsector
& 0xff;
1756 s
->status
= READY_STAT
| SEEK_STAT
;
1758 ide_set_irq(s
->bus
);
1760 case WIN_VERIFY_EXT
:
1763 case WIN_VERIFY_ONCE
:
1764 /* do sector number check ? */
1765 ide_cmd_lba48_transform(s
, lba48
);
1766 s
->status
= READY_STAT
| SEEK_STAT
;
1767 ide_set_irq(s
->bus
);
1775 ide_cmd_lba48_transform(s
, lba48
);
1776 s
->req_nb_sectors
= 1;
1782 case WIN_WRITE_ONCE
:
1783 case CFA_WRITE_SECT_WO_ERASE
:
1784 case WIN_WRITE_VERIFY
:
1785 ide_cmd_lba48_transform(s
, lba48
);
1787 s
->status
= SEEK_STAT
| READY_STAT
;
1788 s
->req_nb_sectors
= 1;
1789 ide_transfer_start(s
, s
->io_buffer
, 512, ide_sector_write
);
1790 s
->media_changed
= 1;
1792 case WIN_MULTREAD_EXT
:
1795 if (!s
->mult_sectors
)
1797 ide_cmd_lba48_transform(s
, lba48
);
1798 s
->req_nb_sectors
= s
->mult_sectors
;
1801 case WIN_MULTWRITE_EXT
:
1804 case CFA_WRITE_MULTI_WO_ERASE
:
1805 if (!s
->mult_sectors
)
1807 ide_cmd_lba48_transform(s
, lba48
);
1809 s
->status
= SEEK_STAT
| READY_STAT
;
1810 s
->req_nb_sectors
= s
->mult_sectors
;
1812 if (n
> s
->req_nb_sectors
)
1813 n
= s
->req_nb_sectors
;
1814 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_write
);
1815 s
->media_changed
= 1;
1817 case WIN_READDMA_EXT
:
1820 case WIN_READDMA_ONCE
:
1823 ide_cmd_lba48_transform(s
, lba48
);
1824 ide_sector_start_dma(s
, 1);
1826 case WIN_WRITEDMA_EXT
:
1829 case WIN_WRITEDMA_ONCE
:
1832 ide_cmd_lba48_transform(s
, lba48
);
1833 ide_sector_start_dma(s
, 0);
1834 s
->media_changed
= 1;
1836 case WIN_READ_NATIVE_MAX_EXT
:
1838 case WIN_READ_NATIVE_MAX
:
1839 ide_cmd_lba48_transform(s
, lba48
);
1840 ide_set_sector(s
, s
->nb_sectors
- 1);
1841 s
->status
= READY_STAT
| SEEK_STAT
;
1842 ide_set_irq(s
->bus
);
1844 case WIN_CHECKPOWERMODE1
:
1845 case WIN_CHECKPOWERMODE2
:
1846 s
->nsector
= 0xff; /* device active or idle */
1847 s
->status
= READY_STAT
| SEEK_STAT
;
1848 ide_set_irq(s
->bus
);
1850 case WIN_SETFEATURES
:
1853 /* XXX: valid for CDROM ? */
1854 switch(s
->feature
) {
1855 case 0xcc: /* reverting to power-on defaults enable */
1856 case 0x66: /* reverting to power-on defaults disable */
1857 case 0x02: /* write cache enable */
1858 case 0x82: /* write cache disable */
1859 case 0xaa: /* read look-ahead enable */
1860 case 0x55: /* read look-ahead disable */
1861 case 0x05: /* set advanced power management mode */
1862 case 0x85: /* disable advanced power management mode */
1863 case 0x69: /* NOP */
1864 case 0x67: /* NOP */
1865 case 0x96: /* NOP */
1866 case 0x9a: /* NOP */
1867 case 0x42: /* enable Automatic Acoustic Mode */
1868 case 0xc2: /* disable Automatic Acoustic Mode */
1869 s
->status
= READY_STAT
| SEEK_STAT
;
1870 ide_set_irq(s
->bus
);
1872 case 0x03: { /* set transfer mode */
1873 uint8_t val
= s
->nsector
& 0x07;
1874 uint16_t *identify_data
= (uint16_t *)s
->identify_data
;
1876 switch (s
->nsector
>> 3) {
1877 case 0x00: /* pio default */
1878 case 0x01: /* pio mode */
1879 put_le16(identify_data
+ 62,0x07);
1880 put_le16(identify_data
+ 63,0x07);
1881 put_le16(identify_data
+ 88,0x3f);
1883 case 0x02: /* sigle word dma mode*/
1884 put_le16(identify_data
+ 62,0x07 | (1 << (val
+ 8)));
1885 put_le16(identify_data
+ 63,0x07);
1886 put_le16(identify_data
+ 88,0x3f);
1888 case 0x04: /* mdma mode */
1889 put_le16(identify_data
+ 62,0x07);
1890 put_le16(identify_data
+ 63,0x07 | (1 << (val
+ 8)));
1891 put_le16(identify_data
+ 88,0x3f);
1893 case 0x08: /* udma mode */
1894 put_le16(identify_data
+ 62,0x07);
1895 put_le16(identify_data
+ 63,0x07);
1896 put_le16(identify_data
+ 88,0x3f | (1 << (val
+ 8)));
1901 s
->status
= READY_STAT
| SEEK_STAT
;
1902 ide_set_irq(s
->bus
);
1909 case WIN_FLUSH_CACHE
:
1910 case WIN_FLUSH_CACHE_EXT
:
1915 case WIN_STANDBYNOW1
:
1916 case WIN_STANDBYNOW2
:
1917 case WIN_IDLEIMMEDIATE
:
1918 case CFA_IDLEIMMEDIATE
:
1923 s
->status
= READY_STAT
;
1924 ide_set_irq(s
->bus
);
1927 if(s
->drive_kind
== IDE_CD
)
1929 /* XXX: Check that seek is within bounds */
1930 s
->status
= READY_STAT
| SEEK_STAT
;
1931 ide_set_irq(s
->bus
);
1933 /* ATAPI commands */
1935 if (s
->drive_kind
== IDE_CD
) {
1936 ide_atapi_identify(s
);
1937 s
->status
= READY_STAT
| SEEK_STAT
;
1938 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1940 ide_abort_command(s
);
1942 ide_set_irq(s
->bus
);
1945 ide_set_signature(s
);
1946 if (s
->drive_kind
== IDE_CD
)
1947 s
->status
= 0; /* ATAPI spec (v6) section 9.10 defines packet
1948 * devices to return a clear status register
1949 * with READY_STAT *not* set. */
1951 s
->status
= READY_STAT
| SEEK_STAT
;
1952 s
->error
= 0x01; /* Device 0 passed, Device 1 passed or not
1955 ide_set_irq(s
->bus
);
1958 if (s
->drive_kind
!= IDE_CD
)
1960 ide_set_signature(s
);
1961 s
->status
= 0x00; /* NOTE: READY is _not_ set */
1965 if (s
->drive_kind
!= IDE_CD
)
1967 /* overlapping commands not supported */
1968 if (s
->feature
& 0x02)
1970 s
->status
= READY_STAT
| SEEK_STAT
;
1971 s
->atapi_dma
= s
->feature
& 1;
1973 ide_transfer_start(s
, s
->io_buffer
, ATAPI_PACKET_SIZE
,
1976 /* CF-ATA commands */
1977 case CFA_REQ_EXT_ERROR_CODE
:
1978 if (s
->drive_kind
!= IDE_CFATA
)
1980 s
->error
= 0x09; /* miscellaneous error */
1981 s
->status
= READY_STAT
| SEEK_STAT
;
1982 ide_set_irq(s
->bus
);
1984 case CFA_ERASE_SECTORS
:
1985 case CFA_WEAR_LEVEL
:
1986 if (s
->drive_kind
!= IDE_CFATA
)
1988 if (val
== CFA_WEAR_LEVEL
)
1990 if (val
== CFA_ERASE_SECTORS
)
1991 s
->media_changed
= 1;
1993 s
->status
= READY_STAT
| SEEK_STAT
;
1994 ide_set_irq(s
->bus
);
1996 case CFA_TRANSLATE_SECTOR
:
1997 if (s
->drive_kind
!= IDE_CFATA
)
2000 s
->status
= READY_STAT
| SEEK_STAT
;
2001 memset(s
->io_buffer
, 0, 0x200);
2002 s
->io_buffer
[0x00] = s
->hcyl
; /* Cyl MSB */
2003 s
->io_buffer
[0x01] = s
->lcyl
; /* Cyl LSB */
2004 s
->io_buffer
[0x02] = s
->select
; /* Head */
2005 s
->io_buffer
[0x03] = s
->sector
; /* Sector */
2006 s
->io_buffer
[0x04] = ide_get_sector(s
) >> 16; /* LBA MSB */
2007 s
->io_buffer
[0x05] = ide_get_sector(s
) >> 8; /* LBA */
2008 s
->io_buffer
[0x06] = ide_get_sector(s
) >> 0; /* LBA LSB */
2009 s
->io_buffer
[0x13] = 0x00; /* Erase flag */
2010 s
->io_buffer
[0x18] = 0x00; /* Hot count */
2011 s
->io_buffer
[0x19] = 0x00; /* Hot count */
2012 s
->io_buffer
[0x1a] = 0x01; /* Hot count */
2013 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2014 ide_set_irq(s
->bus
);
2016 case CFA_ACCESS_METADATA_STORAGE
:
2017 if (s
->drive_kind
!= IDE_CFATA
)
2019 switch (s
->feature
) {
2020 case 0x02: /* Inquiry Metadata Storage */
2021 ide_cfata_metadata_inquiry(s
);
2023 case 0x03: /* Read Metadata Storage */
2024 ide_cfata_metadata_read(s
);
2026 case 0x04: /* Write Metadata Storage */
2027 ide_cfata_metadata_write(s
);
2032 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2033 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2034 ide_set_irq(s
->bus
);
2036 case IBM_SENSE_CONDITION
:
2037 if (s
->drive_kind
!= IDE_CFATA
)
2039 switch (s
->feature
) {
2040 case 0x01: /* sense temperature in device */
2041 s
->nsector
= 0x50; /* +20 C */
2046 s
->status
= READY_STAT
| SEEK_STAT
;
2047 ide_set_irq(s
->bus
);
2051 if (s
->drive_kind
== IDE_CD
)
2053 if (s
->hcyl
!= 0xc2 || s
->lcyl
!= 0x4f)
2055 if (!s
->smart_enabled
&& s
->feature
!= SMART_ENABLE
)
2057 switch (s
->feature
) {
2059 s
->smart_enabled
= 0;
2060 s
->status
= READY_STAT
| SEEK_STAT
;
2061 ide_set_irq(s
->bus
);
2064 s
->smart_enabled
= 1;
2065 s
->status
= READY_STAT
| SEEK_STAT
;
2066 ide_set_irq(s
->bus
);
2068 case SMART_ATTR_AUTOSAVE
:
2069 switch (s
->sector
) {
2071 s
->smart_autosave
= 0;
2074 s
->smart_autosave
= 1;
2079 s
->status
= READY_STAT
| SEEK_STAT
;
2080 ide_set_irq(s
->bus
);
2083 if (!s
->smart_errors
) {
2090 s
->status
= READY_STAT
| SEEK_STAT
;
2091 ide_set_irq(s
->bus
);
2093 case SMART_READ_THRESH
:
2094 memset(s
->io_buffer
, 0, 0x200);
2095 s
->io_buffer
[0] = 0x01; /* smart struct version */
2096 for (n
=0; n
<30; n
++) {
2097 if (smart_attributes
[n
][0] == 0)
2099 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2100 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][4];
2102 for (n
=0; n
<511; n
++) /* checksum */
2103 s
->io_buffer
[511] += s
->io_buffer
[n
];
2104 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2105 s
->status
= READY_STAT
| SEEK_STAT
;
2106 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2107 ide_set_irq(s
->bus
);
2109 case SMART_READ_DATA
:
2110 memset(s
->io_buffer
, 0, 0x200);
2111 s
->io_buffer
[0] = 0x01; /* smart struct version */
2112 for (n
=0; n
<30; n
++) {
2113 if (smart_attributes
[n
][0] == 0)
2115 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2116 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][1];
2117 s
->io_buffer
[2+3+(n
*12)] = smart_attributes
[n
][2];
2118 s
->io_buffer
[2+4+(n
*12)] = smart_attributes
[n
][3];
2120 s
->io_buffer
[362] = 0x02 | (s
->smart_autosave
?0x80:0x00);
2121 if (s
->smart_selftest_count
== 0) {
2122 s
->io_buffer
[363] = 0;
2125 s
->smart_selftest_data
[3 +
2126 (s
->smart_selftest_count
- 1) *
2129 s
->io_buffer
[364] = 0x20;
2130 s
->io_buffer
[365] = 0x01;
2131 /* offline data collection capacity: execute + self-test*/
2132 s
->io_buffer
[367] = (1<<4 | 1<<3 | 1);
2133 s
->io_buffer
[368] = 0x03; /* smart capability (1) */
2134 s
->io_buffer
[369] = 0x00; /* smart capability (2) */
2135 s
->io_buffer
[370] = 0x01; /* error logging supported */
2136 s
->io_buffer
[372] = 0x02; /* minutes for poll short test */
2137 s
->io_buffer
[373] = 0x36; /* minutes for poll ext test */
2138 s
->io_buffer
[374] = 0x01; /* minutes for poll conveyance */
2140 for (n
=0; n
<511; n
++)
2141 s
->io_buffer
[511] += s
->io_buffer
[n
];
2142 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2143 s
->status
= READY_STAT
| SEEK_STAT
;
2144 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2145 ide_set_irq(s
->bus
);
2147 case SMART_READ_LOG
:
2148 switch (s
->sector
) {
2149 case 0x01: /* summary smart error log */
2150 memset(s
->io_buffer
, 0, 0x200);
2151 s
->io_buffer
[0] = 0x01;
2152 s
->io_buffer
[1] = 0x00; /* no error entries */
2153 s
->io_buffer
[452] = s
->smart_errors
& 0xff;
2154 s
->io_buffer
[453] = (s
->smart_errors
& 0xff00) >> 8;
2156 for (n
=0; n
<511; n
++)
2157 s
->io_buffer
[511] += s
->io_buffer
[n
];
2158 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2160 case 0x06: /* smart self test log */
2161 memset(s
->io_buffer
, 0, 0x200);
2162 s
->io_buffer
[0] = 0x01;
2163 if (s
->smart_selftest_count
== 0) {
2164 s
->io_buffer
[508] = 0;
2166 s
->io_buffer
[508] = s
->smart_selftest_count
;
2167 for (n
=2; n
<506; n
++)
2168 s
->io_buffer
[n
] = s
->smart_selftest_data
[n
];
2170 for (n
=0; n
<511; n
++)
2171 s
->io_buffer
[511] += s
->io_buffer
[n
];
2172 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2177 s
->status
= READY_STAT
| SEEK_STAT
;
2178 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2179 ide_set_irq(s
->bus
);
2181 case SMART_EXECUTE_OFFLINE
:
2182 switch (s
->sector
) {
2183 case 0: /* off-line routine */
2184 case 1: /* short self test */
2185 case 2: /* extended self test */
2186 s
->smart_selftest_count
++;
2187 if(s
->smart_selftest_count
> 21)
2188 s
->smart_selftest_count
= 0;
2189 n
= 2 + (s
->smart_selftest_count
- 1) * 24;
2190 s
->smart_selftest_data
[n
] = s
->sector
;
2191 s
->smart_selftest_data
[n
+1] = 0x00; /* OK and finished */
2192 s
->smart_selftest_data
[n
+2] = 0x34; /* hour count lsb */
2193 s
->smart_selftest_data
[n
+3] = 0x12; /* hour count msb */
2194 s
->status
= READY_STAT
| SEEK_STAT
;
2195 ide_set_irq(s
->bus
);
2207 ide_abort_command(s
);
2208 ide_set_irq(s
->bus
);
2213 uint32_t ide_ioport_read(void *opaque
, uint32_t addr1
)
2215 IDEBus
*bus
= opaque
;
2216 IDEState
*s
= idebus_active_if(bus
);
2221 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2222 //hob = s->select & (1 << 7);
2229 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2230 (s
!= bus
->ifs
&& !s
->bs
))
2235 ret
= s
->hob_feature
;
2238 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2241 ret
= s
->nsector
& 0xff;
2243 ret
= s
->hob_nsector
;
2246 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2251 ret
= s
->hob_sector
;
2254 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2262 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2270 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2277 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2278 (s
!= bus
->ifs
&& !s
->bs
))
2282 qemu_irq_lower(bus
->irq
);
2286 printf("ide: read addr=0x%x val=%02x\n", addr1
, ret
);
2291 uint32_t ide_status_read(void *opaque
, uint32_t addr
)
2293 IDEBus
*bus
= opaque
;
2294 IDEState
*s
= idebus_active_if(bus
);
2297 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2298 (s
!= bus
->ifs
&& !s
->bs
))
2303 printf("ide: read status addr=0x%x val=%02x\n", addr
, ret
);
2308 void ide_cmd_write(void *opaque
, uint32_t addr
, uint32_t val
)
2310 IDEBus
*bus
= opaque
;
2315 printf("ide: write control addr=0x%x val=%02x\n", addr
, val
);
2317 /* common for both drives */
2318 if (!(bus
->cmd
& IDE_CMD_RESET
) &&
2319 (val
& IDE_CMD_RESET
)) {
2320 /* reset low to high */
2321 for(i
= 0;i
< 2; i
++) {
2323 s
->status
= BUSY_STAT
| SEEK_STAT
;
2326 } else if ((bus
->cmd
& IDE_CMD_RESET
) &&
2327 !(val
& IDE_CMD_RESET
)) {
2329 for(i
= 0;i
< 2; i
++) {
2331 if (s
->drive_kind
== IDE_CD
)
2332 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2334 s
->status
= READY_STAT
| SEEK_STAT
;
2335 ide_set_signature(s
);
2342 void ide_data_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2344 IDEBus
*bus
= opaque
;
2345 IDEState
*s
= idebus_active_if(bus
);
2348 /* PIO data access allowed only when DRQ bit is set */
2349 if (!(s
->status
& DRQ_STAT
))
2353 *(uint16_t *)p
= le16_to_cpu(val
);
2356 if (p
>= s
->data_end
)
2357 s
->end_transfer_func(s
);
2360 uint32_t ide_data_readw(void *opaque
, uint32_t addr
)
2362 IDEBus
*bus
= opaque
;
2363 IDEState
*s
= idebus_active_if(bus
);
2367 /* PIO data access allowed only when DRQ bit is set */
2368 if (!(s
->status
& DRQ_STAT
))
2372 ret
= cpu_to_le16(*(uint16_t *)p
);
2375 if (p
>= s
->data_end
)
2376 s
->end_transfer_func(s
);
2380 void ide_data_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2382 IDEBus
*bus
= opaque
;
2383 IDEState
*s
= idebus_active_if(bus
);
2386 /* PIO data access allowed only when DRQ bit is set */
2387 if (!(s
->status
& DRQ_STAT
))
2391 *(uint32_t *)p
= le32_to_cpu(val
);
2394 if (p
>= s
->data_end
)
2395 s
->end_transfer_func(s
);
2398 uint32_t ide_data_readl(void *opaque
, uint32_t addr
)
2400 IDEBus
*bus
= opaque
;
2401 IDEState
*s
= idebus_active_if(bus
);
2405 /* PIO data access allowed only when DRQ bit is set */
2406 if (!(s
->status
& DRQ_STAT
))
2410 ret
= cpu_to_le32(*(uint32_t *)p
);
2413 if (p
>= s
->data_end
)
2414 s
->end_transfer_func(s
);
2418 static void ide_dummy_transfer_stop(IDEState
*s
)
2420 s
->data_ptr
= s
->io_buffer
;
2421 s
->data_end
= s
->io_buffer
;
2422 s
->io_buffer
[0] = 0xff;
2423 s
->io_buffer
[1] = 0xff;
2424 s
->io_buffer
[2] = 0xff;
2425 s
->io_buffer
[3] = 0xff;
2428 static void ide_reset(IDEState
*s
)
2431 printf("ide: reset\n");
2433 if (s
->drive_kind
== IDE_CFATA
)
2434 s
->mult_sectors
= 0;
2436 s
->mult_sectors
= MAX_MULT_SECTORS
;
2453 s
->status
= READY_STAT
| SEEK_STAT
;
2457 /* ATAPI specific */
2460 s
->cdrom_changed
= 0;
2461 s
->packet_transfer_size
= 0;
2462 s
->elementary_transfer_size
= 0;
2463 s
->io_buffer_index
= 0;
2464 s
->cd_sector_size
= 0;
2467 s
->io_buffer_size
= 0;
2468 s
->req_nb_sectors
= 0;
2470 ide_set_signature(s
);
2471 /* init the transfer handler so that 0xffff is returned on data
2473 s
->end_transfer_func
= ide_dummy_transfer_stop
;
2474 ide_dummy_transfer_stop(s
);
2475 s
->media_changed
= 0;
2478 void ide_bus_reset(IDEBus
*bus
)
2482 ide_reset(&bus
->ifs
[0]);
2483 ide_reset(&bus
->ifs
[1]);
2486 /* pending async DMA */
2487 if (bus
->dma
->aiocb
) {
2489 printf("aio_cancel\n");
2491 bdrv_aio_cancel(bus
->dma
->aiocb
);
2492 bus
->dma
->aiocb
= NULL
;
2495 /* reset dma provider too */
2496 bus
->dma
->ops
->reset(bus
->dma
);
2499 int ide_init_drive(IDEState
*s
, BlockDriverState
*bs
,
2500 const char *version
, const char *serial
)
2502 int cylinders
, heads
, secs
;
2503 uint64_t nb_sectors
;
2506 bdrv_get_geometry(bs
, &nb_sectors
);
2507 bdrv_guess_geometry(bs
, &cylinders
, &heads
, &secs
);
2508 if (cylinders
< 1 || cylinders
> 16383) {
2509 error_report("cyls must be between 1 and 16383");
2512 if (heads
< 1 || heads
> 16) {
2513 error_report("heads must be between 1 and 16");
2516 if (secs
< 1 || secs
> 63) {
2517 error_report("secs must be between 1 and 63");
2520 s
->cylinders
= cylinders
;
2523 s
->nb_sectors
= nb_sectors
;
2524 /* The SMART values should be preserved across power cycles
2526 s
->smart_enabled
= 1;
2527 s
->smart_autosave
= 1;
2528 s
->smart_errors
= 0;
2529 s
->smart_selftest_count
= 0;
2530 if (bdrv_get_type_hint(bs
) == BDRV_TYPE_CDROM
) {
2531 s
->drive_kind
= IDE_CD
;
2532 bdrv_set_change_cb(bs
, cdrom_change_cb
, s
);
2533 bs
->buffer_alignment
= 2048;
2535 if (!bdrv_is_inserted(s
->bs
)) {
2536 error_report("Device needs media, but drive is empty");
2539 if (bdrv_is_read_only(bs
)) {
2540 error_report("Can't use a read-only drive");
2545 strncpy(s
->drive_serial_str
, serial
, sizeof(s
->drive_serial_str
));
2547 snprintf(s
->drive_serial_str
, sizeof(s
->drive_serial_str
),
2548 "QM%05d", s
->drive_serial
);
2551 pstrcpy(s
->version
, sizeof(s
->version
), version
);
2553 pstrcpy(s
->version
, sizeof(s
->version
), QEMU_VERSION
);
2557 bdrv_set_removable(bs
, s
->drive_kind
== IDE_CD
);
2561 static void ide_init1(IDEBus
*bus
, int unit
)
2563 static int drive_serial
= 1;
2564 IDEState
*s
= &bus
->ifs
[unit
];
2568 s
->drive_serial
= drive_serial
++;
2569 /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
2570 s
->io_buffer
= qemu_memalign(2048, IDE_DMA_BUF_SECTORS
*512 + 4);
2571 s
->io_buffer_total_len
= IDE_DMA_BUF_SECTORS
*512 + 4;
2572 s
->smart_selftest_data
= qemu_blockalign(s
->bs
, 512);
2573 s
->sector_write_timer
= qemu_new_timer(vm_clock
,
2574 ide_sector_write_timer_cb
, s
);
2577 static void ide_nop_start(IDEDMA
*dma
, IDEState
*s
,
2578 BlockDriverCompletionFunc
*cb
)
2582 static int ide_nop(IDEDMA
*dma
)
2587 static int ide_nop_int(IDEDMA
*dma
, int x
)
2592 static void ide_nop_restart(void *opaque
, int x
, int y
)
2596 static const IDEDMAOps ide_dma_nop_ops
= {
2597 .start_dma
= ide_nop_start
,
2598 .start_transfer
= ide_nop
,
2599 .prepare_buf
= ide_nop_int
,
2600 .rw_buf
= ide_nop_int
,
2601 .set_unit
= ide_nop_int
,
2602 .add_status
= ide_nop_int
,
2603 .set_inactive
= ide_nop
,
2604 .restart_cb
= ide_nop_restart
,
2608 static IDEDMA ide_dma_nop
= {
2609 .ops
= &ide_dma_nop_ops
,
2613 void ide_init2(IDEBus
*bus
, qemu_irq irq
)
2617 for(i
= 0; i
< 2; i
++) {
2619 ide_reset(&bus
->ifs
[i
]);
2622 bus
->dma
= &ide_dma_nop
;
2625 /* TODO convert users to qdev and remove */
2626 void ide_init2_with_non_qdev_drives(IDEBus
*bus
, DriveInfo
*hd0
,
2627 DriveInfo
*hd1
, qemu_irq irq
)
2632 for(i
= 0; i
< 2; i
++) {
2633 dinfo
= i
== 0 ? hd0
: hd1
;
2636 if (ide_init_drive(&bus
->ifs
[i
], dinfo
->bdrv
, NULL
,
2637 *dinfo
->serial
? dinfo
->serial
: NULL
) < 0) {
2638 error_report("Can't set up IDE drive %s", dinfo
->id
);
2642 ide_reset(&bus
->ifs
[i
]);
2646 bus
->dma
= &ide_dma_nop
;
2649 void ide_init_ioport(IDEBus
*bus
, int iobase
, int iobase2
)
2651 register_ioport_write(iobase
, 8, 1, ide_ioport_write
, bus
);
2652 register_ioport_read(iobase
, 8, 1, ide_ioport_read
, bus
);
2654 register_ioport_read(iobase2
, 1, 1, ide_status_read
, bus
);
2655 register_ioport_write(iobase2
, 1, 1, ide_cmd_write
, bus
);
2659 register_ioport_write(iobase
, 2, 2, ide_data_writew
, bus
);
2660 register_ioport_read(iobase
, 2, 2, ide_data_readw
, bus
);
2661 register_ioport_write(iobase
, 4, 4, ide_data_writel
, bus
);
2662 register_ioport_read(iobase
, 4, 4, ide_data_readl
, bus
);
2665 static bool is_identify_set(void *opaque
, int version_id
)
2667 IDEState
*s
= opaque
;
2669 return s
->identify_set
!= 0;
2672 static EndTransferFunc
* transfer_end_table
[] = {
2676 ide_atapi_cmd_reply_end
,
2678 ide_dummy_transfer_stop
,
2681 static int transfer_end_table_idx(EndTransferFunc
*fn
)
2685 for (i
= 0; i
< ARRAY_SIZE(transfer_end_table
); i
++)
2686 if (transfer_end_table
[i
] == fn
)
2692 static int ide_drive_post_load(void *opaque
, int version_id
)
2694 IDEState
*s
= opaque
;
2696 if (version_id
< 3) {
2697 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
2698 s
->asc
== ASC_MEDIUM_MAY_HAVE_CHANGED
) {
2699 s
->cdrom_changed
= 1;
2705 static int ide_drive_pio_post_load(void *opaque
, int version_id
)
2707 IDEState
*s
= opaque
;
2709 if (s
->end_transfer_fn_idx
> ARRAY_SIZE(transfer_end_table
)) {
2712 s
->end_transfer_func
= transfer_end_table
[s
->end_transfer_fn_idx
];
2713 s
->data_ptr
= s
->io_buffer
+ s
->cur_io_buffer_offset
;
2714 s
->data_end
= s
->data_ptr
+ s
->cur_io_buffer_len
;
2719 static void ide_drive_pio_pre_save(void *opaque
)
2721 IDEState
*s
= opaque
;
2724 s
->cur_io_buffer_offset
= s
->data_ptr
- s
->io_buffer
;
2725 s
->cur_io_buffer_len
= s
->data_end
- s
->data_ptr
;
2727 idx
= transfer_end_table_idx(s
->end_transfer_func
);
2729 fprintf(stderr
, "%s: invalid end_transfer_func for DRQ_STAT\n",
2731 s
->end_transfer_fn_idx
= 2;
2733 s
->end_transfer_fn_idx
= idx
;
2737 static bool ide_drive_pio_state_needed(void *opaque
)
2739 IDEState
*s
= opaque
;
2741 return (s
->status
& DRQ_STAT
) != 0;
2744 const VMStateDescription vmstate_ide_drive_pio_state
= {
2745 .name
= "ide_drive/pio_state",
2747 .minimum_version_id
= 1,
2748 .minimum_version_id_old
= 1,
2749 .pre_save
= ide_drive_pio_pre_save
,
2750 .post_load
= ide_drive_pio_post_load
,
2751 .fields
= (VMStateField
[]) {
2752 VMSTATE_INT32(req_nb_sectors
, IDEState
),
2753 VMSTATE_VARRAY_INT32(io_buffer
, IDEState
, io_buffer_total_len
, 1,
2754 vmstate_info_uint8
, uint8_t),
2755 VMSTATE_INT32(cur_io_buffer_offset
, IDEState
),
2756 VMSTATE_INT32(cur_io_buffer_len
, IDEState
),
2757 VMSTATE_UINT8(end_transfer_fn_idx
, IDEState
),
2758 VMSTATE_INT32(elementary_transfer_size
, IDEState
),
2759 VMSTATE_INT32(packet_transfer_size
, IDEState
),
2760 VMSTATE_END_OF_LIST()
2764 const VMStateDescription vmstate_ide_drive
= {
2765 .name
= "ide_drive",
2767 .minimum_version_id
= 0,
2768 .minimum_version_id_old
= 0,
2769 .post_load
= ide_drive_post_load
,
2770 .fields
= (VMStateField
[]) {
2771 VMSTATE_INT32(mult_sectors
, IDEState
),
2772 VMSTATE_INT32(identify_set
, IDEState
),
2773 VMSTATE_BUFFER_TEST(identify_data
, IDEState
, is_identify_set
),
2774 VMSTATE_UINT8(feature
, IDEState
),
2775 VMSTATE_UINT8(error
, IDEState
),
2776 VMSTATE_UINT32(nsector
, IDEState
),
2777 VMSTATE_UINT8(sector
, IDEState
),
2778 VMSTATE_UINT8(lcyl
, IDEState
),
2779 VMSTATE_UINT8(hcyl
, IDEState
),
2780 VMSTATE_UINT8(hob_feature
, IDEState
),
2781 VMSTATE_UINT8(hob_sector
, IDEState
),
2782 VMSTATE_UINT8(hob_nsector
, IDEState
),
2783 VMSTATE_UINT8(hob_lcyl
, IDEState
),
2784 VMSTATE_UINT8(hob_hcyl
, IDEState
),
2785 VMSTATE_UINT8(select
, IDEState
),
2786 VMSTATE_UINT8(status
, IDEState
),
2787 VMSTATE_UINT8(lba48
, IDEState
),
2788 VMSTATE_UINT8(sense_key
, IDEState
),
2789 VMSTATE_UINT8(asc
, IDEState
),
2790 VMSTATE_UINT8_V(cdrom_changed
, IDEState
, 3),
2791 VMSTATE_END_OF_LIST()
2793 .subsections
= (VMStateSubsection
[]) {
2795 .vmsd
= &vmstate_ide_drive_pio_state
,
2796 .needed
= ide_drive_pio_state_needed
,
2803 const VMStateDescription vmstate_ide_bus
= {
2806 .minimum_version_id
= 1,
2807 .minimum_version_id_old
= 1,
2808 .fields
= (VMStateField
[]) {
2809 VMSTATE_UINT8(cmd
, IDEBus
),
2810 VMSTATE_UINT8(unit
, IDEBus
),
2811 VMSTATE_END_OF_LIST()