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 */
220 put_le16(p
+ 80, 0x1e); /* support up to ATA/ATAPI-4 */
222 put_le16(p
+ 88, 0x3f | (1 << 13)); /* udma5 set and supported */
224 memcpy(s
->identify_data
, p
, sizeof(s
->identify_data
));
228 static void ide_cfata_identify(IDEState
*s
)
233 p
= (uint16_t *) s
->identify_data
;
237 memset(p
, 0, sizeof(s
->identify_data
));
239 cur_sec
= s
->cylinders
* s
->heads
* s
->sectors
;
241 put_le16(p
+ 0, 0x848a); /* CF Storage Card signature */
242 put_le16(p
+ 1, s
->cylinders
); /* Default cylinders */
243 put_le16(p
+ 3, s
->heads
); /* Default heads */
244 put_le16(p
+ 6, s
->sectors
); /* Default sectors per track */
245 put_le16(p
+ 7, s
->nb_sectors
>> 16); /* Sectors per card */
246 put_le16(p
+ 8, s
->nb_sectors
); /* Sectors per card */
247 padstr((char *)(p
+ 10), s
->drive_serial_str
, 20); /* serial number */
248 put_le16(p
+ 22, 0x0004); /* ECC bytes */
249 padstr((char *) (p
+ 23), s
->version
, 8); /* Firmware Revision */
250 padstr((char *) (p
+ 27), "QEMU MICRODRIVE", 40);/* Model number */
251 #if MAX_MULT_SECTORS > 1
252 put_le16(p
+ 47, 0x8000 | MAX_MULT_SECTORS
);
254 put_le16(p
+ 47, 0x0000);
256 put_le16(p
+ 49, 0x0f00); /* Capabilities */
257 put_le16(p
+ 51, 0x0002); /* PIO cycle timing mode */
258 put_le16(p
+ 52, 0x0001); /* DMA cycle timing mode */
259 put_le16(p
+ 53, 0x0003); /* Translation params valid */
260 put_le16(p
+ 54, s
->cylinders
); /* Current cylinders */
261 put_le16(p
+ 55, s
->heads
); /* Current heads */
262 put_le16(p
+ 56, s
->sectors
); /* Current sectors */
263 put_le16(p
+ 57, cur_sec
); /* Current capacity */
264 put_le16(p
+ 58, cur_sec
>> 16); /* Current capacity */
265 if (s
->mult_sectors
) /* Multiple sector setting */
266 put_le16(p
+ 59, 0x100 | s
->mult_sectors
);
267 put_le16(p
+ 60, s
->nb_sectors
); /* Total LBA sectors */
268 put_le16(p
+ 61, s
->nb_sectors
>> 16); /* Total LBA sectors */
269 put_le16(p
+ 63, 0x0203); /* Multiword DMA capability */
270 put_le16(p
+ 64, 0x0001); /* Flow Control PIO support */
271 put_le16(p
+ 65, 0x0096); /* Min. Multiword DMA cycle */
272 put_le16(p
+ 66, 0x0096); /* Rec. Multiword DMA cycle */
273 put_le16(p
+ 68, 0x00b4); /* Min. PIO cycle time */
274 put_le16(p
+ 82, 0x400c); /* Command Set supported */
275 put_le16(p
+ 83, 0x7068); /* Command Set supported */
276 put_le16(p
+ 84, 0x4000); /* Features supported */
277 put_le16(p
+ 85, 0x000c); /* Command Set enabled */
278 put_le16(p
+ 86, 0x7044); /* Command Set enabled */
279 put_le16(p
+ 87, 0x4000); /* Features enabled */
280 put_le16(p
+ 91, 0x4060); /* Current APM level */
281 put_le16(p
+ 129, 0x0002); /* Current features option */
282 put_le16(p
+ 130, 0x0005); /* Reassigned sectors */
283 put_le16(p
+ 131, 0x0001); /* Initial power mode */
284 put_le16(p
+ 132, 0x0000); /* User signature */
285 put_le16(p
+ 160, 0x8100); /* Power requirement */
286 put_le16(p
+ 161, 0x8001); /* CF command set */
291 memcpy(s
->io_buffer
, p
, sizeof(s
->identify_data
));
294 static void ide_set_signature(IDEState
*s
)
296 s
->select
&= 0xf0; /* clear head */
300 if (s
->drive_kind
== IDE_CD
) {
312 static inline void ide_abort_command(IDEState
*s
)
314 s
->status
= READY_STAT
| ERR_STAT
;
318 static inline void ide_dma_submit_check(IDEState
*s
,
319 BlockDriverCompletionFunc
*dma_cb
)
321 if (s
->bus
->dma
->aiocb
)
326 /* prepare data transfer and tell what to do after */
327 static void ide_transfer_start(IDEState
*s
, uint8_t *buf
, int size
,
328 EndTransferFunc
*end_transfer_func
)
330 s
->end_transfer_func
= end_transfer_func
;
332 s
->data_end
= buf
+ size
;
333 if (!(s
->status
& ERR_STAT
)) {
334 s
->status
|= DRQ_STAT
;
336 s
->bus
->dma
->ops
->start_transfer(s
->bus
->dma
);
339 static void ide_transfer_stop(IDEState
*s
)
341 s
->end_transfer_func
= ide_transfer_stop
;
342 s
->data_ptr
= s
->io_buffer
;
343 s
->data_end
= s
->io_buffer
;
344 s
->status
&= ~DRQ_STAT
;
347 int64_t ide_get_sector(IDEState
*s
)
350 if (s
->select
& 0x40) {
353 sector_num
= ((s
->select
& 0x0f) << 24) | (s
->hcyl
<< 16) |
354 (s
->lcyl
<< 8) | s
->sector
;
356 sector_num
= ((int64_t)s
->hob_hcyl
<< 40) |
357 ((int64_t) s
->hob_lcyl
<< 32) |
358 ((int64_t) s
->hob_sector
<< 24) |
359 ((int64_t) s
->hcyl
<< 16) |
360 ((int64_t) s
->lcyl
<< 8) | s
->sector
;
363 sector_num
= ((s
->hcyl
<< 8) | s
->lcyl
) * s
->heads
* s
->sectors
+
364 (s
->select
& 0x0f) * s
->sectors
+ (s
->sector
- 1);
369 void ide_set_sector(IDEState
*s
, int64_t sector_num
)
372 if (s
->select
& 0x40) {
374 s
->select
= (s
->select
& 0xf0) | (sector_num
>> 24);
375 s
->hcyl
= (sector_num
>> 16);
376 s
->lcyl
= (sector_num
>> 8);
377 s
->sector
= (sector_num
);
379 s
->sector
= sector_num
;
380 s
->lcyl
= sector_num
>> 8;
381 s
->hcyl
= sector_num
>> 16;
382 s
->hob_sector
= sector_num
>> 24;
383 s
->hob_lcyl
= sector_num
>> 32;
384 s
->hob_hcyl
= sector_num
>> 40;
387 cyl
= sector_num
/ (s
->heads
* s
->sectors
);
388 r
= sector_num
% (s
->heads
* s
->sectors
);
391 s
->select
= (s
->select
& 0xf0) | ((r
/ s
->sectors
) & 0x0f);
392 s
->sector
= (r
% s
->sectors
) + 1;
396 static void ide_rw_error(IDEState
*s
) {
397 ide_abort_command(s
);
401 void ide_sector_read(IDEState
*s
)
406 s
->status
= READY_STAT
| SEEK_STAT
;
407 s
->error
= 0; /* not needed by IDE spec, but needed by Windows */
408 sector_num
= ide_get_sector(s
);
411 /* no more sector to read from disk */
412 ide_transfer_stop(s
);
414 #if defined(DEBUG_IDE)
415 printf("read sector=%" PRId64
"\n", sector_num
);
417 if (n
> s
->req_nb_sectors
)
418 n
= s
->req_nb_sectors
;
419 ret
= bdrv_read(s
->bs
, sector_num
, s
->io_buffer
, n
);
421 if (ide_handle_rw_error(s
, -ret
,
422 BM_STATUS_PIO_RETRY
| BM_STATUS_RETRY_READ
))
427 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_read
);
429 ide_set_sector(s
, sector_num
+ n
);
434 static void dma_buf_commit(IDEState
*s
, int is_write
)
436 qemu_sglist_destroy(&s
->sg
);
439 static void ide_set_inactive(IDEState
*s
)
441 s
->bus
->dma
->aiocb
= NULL
;
442 s
->bus
->dma
->ops
->set_inactive(s
->bus
->dma
);
445 void ide_dma_error(IDEState
*s
)
447 ide_transfer_stop(s
);
449 s
->status
= READY_STAT
| ERR_STAT
;
451 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
455 static int ide_handle_rw_error(IDEState
*s
, int error
, int op
)
457 int is_read
= (op
& BM_STATUS_RETRY_READ
);
458 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
460 if (action
== BLOCK_ERR_IGNORE
) {
461 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
465 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
466 || action
== BLOCK_ERR_STOP_ANY
) {
467 s
->bus
->dma
->ops
->set_unit(s
->bus
->dma
, s
->unit
);
468 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, op
);
469 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
472 if (op
& BM_STATUS_DMA_RETRY
) {
473 dma_buf_commit(s
, 0);
478 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
484 void ide_read_dma_cb(void *opaque
, int ret
)
486 IDEState
*s
= opaque
;
491 if (ide_handle_rw_error(s
, -ret
,
492 BM_STATUS_DMA_RETRY
| BM_STATUS_RETRY_READ
))
498 n
= s
->io_buffer_size
>> 9;
499 sector_num
= ide_get_sector(s
);
501 dma_buf_commit(s
, 1);
503 ide_set_sector(s
, sector_num
);
507 /* end of transfer ? */
508 if (s
->nsector
== 0) {
509 s
->status
= READY_STAT
| SEEK_STAT
;
512 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
517 /* launch next transfer */
519 s
->io_buffer_index
= 0;
520 s
->io_buffer_size
= n
* 512;
521 if (s
->bus
->dma
->ops
->prepare_buf(s
->bus
->dma
, 1) == 0)
524 printf("aio_read: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
526 s
->bus
->dma
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
, ide_read_dma_cb
, s
);
527 ide_dma_submit_check(s
, ide_read_dma_cb
);
530 static void ide_sector_read_dma(IDEState
*s
)
532 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
533 s
->io_buffer_index
= 0;
534 s
->io_buffer_size
= 0;
536 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
, ide_read_dma_cb
);
539 static void ide_sector_write_timer_cb(void *opaque
)
541 IDEState
*s
= opaque
;
545 void ide_sector_write(IDEState
*s
)
550 s
->status
= READY_STAT
| SEEK_STAT
;
551 sector_num
= ide_get_sector(s
);
552 #if defined(DEBUG_IDE)
553 printf("write sector=%" PRId64
"\n", sector_num
);
556 if (n
> s
->req_nb_sectors
)
557 n
= s
->req_nb_sectors
;
558 ret
= bdrv_write(s
->bs
, sector_num
, s
->io_buffer
, n
);
561 if (ide_handle_rw_error(s
, -ret
, BM_STATUS_PIO_RETRY
))
566 if (s
->nsector
== 0) {
567 /* no more sectors to write */
568 ide_transfer_stop(s
);
571 if (n1
> s
->req_nb_sectors
)
572 n1
= s
->req_nb_sectors
;
573 ide_transfer_start(s
, s
->io_buffer
, 512 * n1
, ide_sector_write
);
575 ide_set_sector(s
, sector_num
+ n
);
577 if (win2k_install_hack
&& ((++s
->irq_count
% 16) == 0)) {
578 /* It seems there is a bug in the Windows 2000 installer HDD
579 IDE driver which fills the disk with empty logs when the
580 IDE write IRQ comes too early. This hack tries to correct
581 that at the expense of slower write performances. Use this
582 option _only_ to install Windows 2000. You must disable it
584 qemu_mod_timer(s
->sector_write_timer
,
585 qemu_get_clock(vm_clock
) + (get_ticks_per_sec() / 1000));
591 void ide_write_dma_cb(void *opaque
, int ret
)
593 IDEState
*s
= opaque
;
598 if (ide_handle_rw_error(s
, -ret
, BM_STATUS_DMA_RETRY
))
602 n
= s
->io_buffer_size
>> 9;
603 sector_num
= ide_get_sector(s
);
605 dma_buf_commit(s
, 0);
607 ide_set_sector(s
, sector_num
);
611 /* end of transfer ? */
612 if (s
->nsector
== 0) {
613 s
->status
= READY_STAT
| SEEK_STAT
;
616 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
622 s
->io_buffer_size
= n
* 512;
623 /* launch next transfer */
624 if (s
->bus
->dma
->ops
->prepare_buf(s
->bus
->dma
, 0) == 0)
627 printf("aio_write: sector_num=%" PRId64
" n=%d\n", sector_num
, n
);
629 s
->bus
->dma
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
, ide_write_dma_cb
, s
);
630 ide_dma_submit_check(s
, ide_write_dma_cb
);
633 static void ide_sector_write_dma(IDEState
*s
)
635 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
636 s
->io_buffer_index
= 0;
637 s
->io_buffer_size
= 0;
639 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
, ide_write_dma_cb
);
642 void ide_atapi_cmd_ok(IDEState
*s
)
645 s
->status
= READY_STAT
| SEEK_STAT
;
646 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
650 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
652 #ifdef DEBUG_IDE_ATAPI
653 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
655 s
->error
= sense_key
<< 4;
656 s
->status
= READY_STAT
| ERR_STAT
;
657 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
658 s
->sense_key
= sense_key
;
663 static void ide_atapi_cmd_check_status(IDEState
*s
)
665 #ifdef DEBUG_IDE_ATAPI
666 printf("atapi_cmd_check_status\n");
668 s
->error
= MC_ERR
| (SENSE_UNIT_ATTENTION
<< 4);
669 s
->status
= ERR_STAT
;
674 static void ide_flush_cb(void *opaque
, int ret
)
676 IDEState
*s
= opaque
;
679 /* XXX: What sector number to set here? */
680 if (ide_handle_rw_error(s
, -ret
, BM_STATUS_RETRY_FLUSH
)) {
685 s
->status
= READY_STAT
| SEEK_STAT
;
689 void ide_flush_cache(IDEState
*s
)
691 BlockDriverAIOCB
*acb
;
698 acb
= bdrv_aio_flush(s
->bs
, ide_flush_cb
, s
);
700 ide_flush_cb(s
, -EIO
);
704 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
710 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
718 static inline int ube16_to_cpu(const uint8_t *buf
)
720 return (buf
[0] << 8) | buf
[1];
723 static inline int ube32_to_cpu(const uint8_t *buf
)
725 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
728 static void lba_to_msf(uint8_t *buf
, int lba
)
731 buf
[0] = (lba
/ 75) / 60;
732 buf
[1] = (lba
/ 75) % 60;
736 static void cd_data_to_raw(uint8_t *buf
, int lba
)
740 memset(buf
+ 1, 0xff, 10);
744 lba_to_msf(buf
, lba
);
745 buf
[3] = 0x01; /* mode 1 data */
749 /* XXX: ECC not computed */
753 static int cd_read_sector(BlockDriverState
*bs
, int lba
, uint8_t *buf
,
758 switch(sector_size
) {
760 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
, 4);
763 ret
= bdrv_read(bs
, (int64_t)lba
<< 2, buf
+ 16, 4);
766 cd_data_to_raw(buf
, lba
);
775 void ide_atapi_io_error(IDEState
*s
, int ret
)
777 /* XXX: handle more errors */
778 if (ret
== -ENOMEDIUM
) {
779 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
780 ASC_MEDIUM_NOT_PRESENT
);
782 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
783 ASC_LOGICAL_BLOCK_OOR
);
787 /* The whole ATAPI transfer logic is handled in this function */
788 static void ide_atapi_cmd_reply_end(IDEState
*s
)
790 int byte_count_limit
, size
, ret
;
791 #ifdef DEBUG_IDE_ATAPI
792 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
793 s
->packet_transfer_size
,
794 s
->elementary_transfer_size
,
797 if (s
->packet_transfer_size
<= 0) {
798 /* end of transfer */
799 ide_transfer_stop(s
);
800 s
->status
= READY_STAT
| SEEK_STAT
;
801 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
803 #ifdef DEBUG_IDE_ATAPI
804 printf("status=0x%x\n", s
->status
);
807 /* see if a new sector must be read */
808 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
809 ret
= cd_read_sector(s
->bs
, s
->lba
, s
->io_buffer
, s
->cd_sector_size
);
811 ide_transfer_stop(s
);
812 ide_atapi_io_error(s
, ret
);
816 s
->io_buffer_index
= 0;
818 if (s
->elementary_transfer_size
> 0) {
819 /* there are some data left to transmit in this elementary
821 size
= s
->cd_sector_size
- s
->io_buffer_index
;
822 if (size
> s
->elementary_transfer_size
)
823 size
= s
->elementary_transfer_size
;
824 s
->packet_transfer_size
-= size
;
825 s
->elementary_transfer_size
-= size
;
826 s
->io_buffer_index
+= size
;
827 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
828 size
, ide_atapi_cmd_reply_end
);
830 /* a new transfer is needed */
831 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
832 byte_count_limit
= s
->lcyl
| (s
->hcyl
<< 8);
833 #ifdef DEBUG_IDE_ATAPI
834 printf("byte_count_limit=%d\n", byte_count_limit
);
836 if (byte_count_limit
== 0xffff)
838 size
= s
->packet_transfer_size
;
839 if (size
> byte_count_limit
) {
840 /* byte count limit must be even if this case */
841 if (byte_count_limit
& 1)
843 size
= byte_count_limit
;
847 s
->elementary_transfer_size
= size
;
848 /* we cannot transmit more than one sector at a time */
850 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
851 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
853 s
->packet_transfer_size
-= size
;
854 s
->elementary_transfer_size
-= size
;
855 s
->io_buffer_index
+= size
;
856 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
857 size
, ide_atapi_cmd_reply_end
);
859 #ifdef DEBUG_IDE_ATAPI
860 printf("status=0x%x\n", s
->status
);
866 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
867 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
871 s
->lba
= -1; /* no sector read */
872 s
->packet_transfer_size
= size
;
873 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
874 s
->elementary_transfer_size
= 0;
875 s
->io_buffer_index
= 0;
878 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
879 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
,
880 ide_atapi_cmd_read_dma_cb
);
882 s
->status
= READY_STAT
| SEEK_STAT
;
883 ide_atapi_cmd_reply_end(s
);
887 /* start a CD-CDROM read command */
888 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
892 s
->packet_transfer_size
= nb_sectors
* sector_size
;
893 s
->elementary_transfer_size
= 0;
894 s
->io_buffer_index
= sector_size
;
895 s
->cd_sector_size
= sector_size
;
897 s
->status
= READY_STAT
| SEEK_STAT
;
898 ide_atapi_cmd_reply_end(s
);
901 /* ATAPI DMA support */
903 /* XXX: handle read errors */
904 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
906 IDEState
*s
= opaque
;
910 ide_atapi_io_error(s
, ret
);
914 if (s
->io_buffer_size
> 0) {
916 * For a cdrom read sector command (s->lba != -1),
917 * adjust the lba for the next s->io_buffer_size chunk
918 * and dma the current chunk.
919 * For a command != read (s->lba == -1), just transfer
923 if (s
->cd_sector_size
== 2352) {
925 cd_data_to_raw(s
->io_buffer
, s
->lba
);
927 n
= s
->io_buffer_size
>> 11;
931 s
->packet_transfer_size
-= s
->io_buffer_size
;
932 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
936 if (s
->packet_transfer_size
<= 0) {
937 s
->status
= READY_STAT
| SEEK_STAT
;
938 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
941 s
->bus
->dma
->ops
->add_status(s
->bus
->dma
, BM_STATUS_INT
);
946 s
->io_buffer_index
= 0;
947 if (s
->cd_sector_size
== 2352) {
949 s
->io_buffer_size
= s
->cd_sector_size
;
952 n
= s
->packet_transfer_size
>> 11;
953 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
954 n
= (IDE_DMA_BUF_SECTORS
/ 4);
955 s
->io_buffer_size
= n
* 2048;
959 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
961 s
->bus
->dma
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
962 s
->bus
->dma
->iov
.iov_len
= n
* 4 * 512;
963 qemu_iovec_init_external(&s
->bus
->dma
->qiov
, &s
->bus
->dma
->iov
, 1);
964 s
->bus
->dma
->aiocb
= bdrv_aio_readv(s
->bs
, (int64_t)s
->lba
<< 2,
965 &s
->bus
->dma
->qiov
, n
* 4,
966 ide_atapi_cmd_read_dma_cb
, s
);
967 if (!s
->bus
->dma
->aiocb
) {
968 /* Note: media not present is the most likely case */
969 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
970 ASC_MEDIUM_NOT_PRESENT
);
975 /* start a CD-CDROM read command with DMA */
976 /* XXX: test if DMA is available */
977 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
981 s
->packet_transfer_size
= nb_sectors
* sector_size
;
982 s
->io_buffer_index
= 0;
983 s
->io_buffer_size
= 0;
984 s
->cd_sector_size
= sector_size
;
986 /* XXX: check if BUSY_STAT should be set */
987 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
988 s
->bus
->dma
->ops
->start_dma(s
->bus
->dma
, s
,
989 ide_atapi_cmd_read_dma_cb
);
992 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
995 #ifdef DEBUG_IDE_ATAPI
996 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
1000 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
1002 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
1006 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
1009 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
1011 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
1012 cpu_to_ube16 (buf_profile
, profile
);
1013 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
1015 /* each profile adds 4 bytes to the response */
1017 buf
[11] += 4; /* Additional Length */
1022 static int ide_dvd_read_structure(IDEState
*s
, int format
,
1023 const uint8_t *packet
, uint8_t *buf
)
1026 case 0x0: /* Physical format information */
1028 int layer
= packet
[6];
1029 uint64_t total_sectors
;
1032 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1034 bdrv_get_geometry(s
->bs
, &total_sectors
);
1035 total_sectors
>>= 2;
1036 if (total_sectors
== 0)
1037 return -ASC_MEDIUM_NOT_PRESENT
;
1039 buf
[4] = 1; /* DVD-ROM, part version 1 */
1040 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1041 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1042 buf
[7] = 0; /* default densities */
1044 /* FIXME: 0x30000 per spec? */
1045 cpu_to_ube32(buf
+ 8, 0); /* start sector */
1046 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
1047 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
1049 /* Size of buffer, not including 2 byte size field */
1050 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1052 /* 2k data + 4 byte header */
1056 case 0x01: /* DVD copyright information */
1057 buf
[4] = 0; /* no copyright data */
1058 buf
[5] = 0; /* no region restrictions */
1060 /* Size of buffer, not including 2 byte size field */
1061 cpu_to_be16wu((uint16_t *)buf
, 4 + 2);
1063 /* 4 byte header + 4 byte data */
1066 case 0x03: /* BCA information - invalid field for no BCA info */
1067 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1069 case 0x04: /* DVD disc manufacturing information */
1070 /* Size of buffer, not including 2 byte size field */
1071 cpu_to_be16wu((uint16_t *)buf
, 2048 + 2);
1073 /* 2k data + 4 byte header */
1078 * This lists all the command capabilities above. Add new ones
1079 * in order and update the length and buffer return values.
1082 buf
[4] = 0x00; /* Physical format */
1083 buf
[5] = 0x40; /* Not writable, is readable */
1084 cpu_to_be16wu((uint16_t *)(buf
+ 6), 2048 + 4);
1086 buf
[8] = 0x01; /* Copyright info */
1087 buf
[9] = 0x40; /* Not writable, is readable */
1088 cpu_to_be16wu((uint16_t *)(buf
+ 10), 4 + 4);
1090 buf
[12] = 0x03; /* BCA info */
1091 buf
[13] = 0x40; /* Not writable, is readable */
1092 cpu_to_be16wu((uint16_t *)(buf
+ 14), 188 + 4);
1094 buf
[16] = 0x04; /* Manufacturing info */
1095 buf
[17] = 0x40; /* Not writable, is readable */
1096 cpu_to_be16wu((uint16_t *)(buf
+ 18), 2048 + 4);
1098 /* Size of buffer, not including 2 byte size field */
1099 cpu_to_be16wu((uint16_t *)buf
, 16 + 2);
1101 /* data written + 4 byte header */
1104 default: /* TODO: formats beyond DVD-ROM requires */
1105 return -ASC_INV_FIELD_IN_CMD_PACKET
;
1109 static void ide_atapi_cmd(IDEState
*s
)
1111 const uint8_t *packet
;
1115 packet
= s
->io_buffer
;
1117 #ifdef DEBUG_IDE_ATAPI
1120 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1121 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1122 printf(" %02x", packet
[i
]);
1127 /* If there's a UNIT_ATTENTION condition pending, only
1128 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1129 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
1130 s
->io_buffer
[0] != GPCMD_REQUEST_SENSE
&&
1131 s
->io_buffer
[0] != GPCMD_INQUIRY
) {
1132 ide_atapi_cmd_check_status(s
);
1135 switch(s
->io_buffer
[0]) {
1136 case GPCMD_TEST_UNIT_READY
:
1137 if (bdrv_is_inserted(s
->bs
) && !s
->cdrom_changed
) {
1138 ide_atapi_cmd_ok(s
);
1140 s
->cdrom_changed
= 0;
1141 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1142 ASC_MEDIUM_NOT_PRESENT
);
1145 case GPCMD_MODE_SENSE_6
:
1146 case GPCMD_MODE_SENSE_10
:
1149 if (packet
[0] == GPCMD_MODE_SENSE_10
)
1150 max_len
= ube16_to_cpu(packet
+ 7);
1152 max_len
= packet
[4];
1153 action
= packet
[2] >> 6;
1154 code
= packet
[2] & 0x3f;
1156 case 0: /* current values */
1158 case GPMODE_R_W_ERROR_PAGE
: /* error recovery */
1159 cpu_to_ube16(&buf
[0], 16 + 6);
1175 ide_atapi_cmd_reply(s
, 16, max_len
);
1177 case GPMODE_AUDIO_CTL_PAGE
:
1178 cpu_to_ube16(&buf
[0], 24 + 6);
1186 /* Fill with CDROM audio volume */
1192 ide_atapi_cmd_reply(s
, 24, max_len
);
1194 case GPMODE_CAPABILITIES_PAGE
:
1195 cpu_to_ube16(&buf
[0], 28 + 6);
1208 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1209 code checks for this to automount media. */
1212 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
1213 if (bdrv_is_locked(s
->bs
))
1216 cpu_to_ube16(&buf
[16], 706);
1219 cpu_to_ube16(&buf
[20], 512);
1220 cpu_to_ube16(&buf
[22], 706);
1225 ide_atapi_cmd_reply(s
, 28, max_len
);
1231 case 1: /* changeable values */
1233 case 2: /* default values */
1236 case 3: /* saved values */
1237 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1238 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
1243 case GPCMD_REQUEST_SENSE
:
1244 max_len
= packet
[4];
1246 buf
[0] = 0x70 | (1 << 7);
1247 buf
[2] = s
->sense_key
;
1250 if (s
->sense_key
== SENSE_UNIT_ATTENTION
)
1251 s
->sense_key
= SENSE_NONE
;
1252 ide_atapi_cmd_reply(s
, 18, max_len
);
1254 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
:
1255 if (bdrv_is_inserted(s
->bs
)) {
1256 bdrv_set_locked(s
->bs
, packet
[4] & 1);
1257 ide_atapi_cmd_ok(s
);
1259 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1260 ASC_MEDIUM_NOT_PRESENT
);
1266 int nb_sectors
, lba
;
1268 if (packet
[0] == GPCMD_READ_10
)
1269 nb_sectors
= ube16_to_cpu(packet
+ 7);
1271 nb_sectors
= ube32_to_cpu(packet
+ 6);
1272 lba
= ube32_to_cpu(packet
+ 2);
1273 if (nb_sectors
== 0) {
1274 ide_atapi_cmd_ok(s
);
1277 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1282 int nb_sectors
, lba
, transfer_request
;
1284 nb_sectors
= (packet
[6] << 16) | (packet
[7] << 8) | packet
[8];
1285 lba
= ube32_to_cpu(packet
+ 2);
1286 if (nb_sectors
== 0) {
1287 ide_atapi_cmd_ok(s
);
1290 transfer_request
= packet
[9];
1291 switch(transfer_request
& 0xf8) {
1294 ide_atapi_cmd_ok(s
);
1298 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1302 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1305 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1306 ASC_INV_FIELD_IN_CMD_PACKET
);
1314 uint64_t total_sectors
;
1316 bdrv_get_geometry(s
->bs
, &total_sectors
);
1317 total_sectors
>>= 2;
1318 if (total_sectors
== 0) {
1319 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1320 ASC_MEDIUM_NOT_PRESENT
);
1323 lba
= ube32_to_cpu(packet
+ 2);
1324 if (lba
>= total_sectors
) {
1325 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1326 ASC_LOGICAL_BLOCK_OOR
);
1329 ide_atapi_cmd_ok(s
);
1332 case GPCMD_START_STOP_UNIT
:
1334 int start
, eject
, err
= 0;
1335 start
= packet
[4] & 1;
1336 eject
= (packet
[4] >> 1) & 1;
1339 err
= bdrv_eject(s
->bs
, !start
);
1344 ide_atapi_cmd_ok(s
);
1347 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1348 ASC_MEDIA_REMOVAL_PREVENTED
);
1351 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1352 ASC_MEDIUM_NOT_PRESENT
);
1357 case GPCMD_MECHANISM_STATUS
:
1359 max_len
= ube16_to_cpu(packet
+ 8);
1360 cpu_to_ube16(buf
, 0);
1361 /* no current LBA */
1366 cpu_to_ube16(buf
+ 6, 0);
1367 ide_atapi_cmd_reply(s
, 8, max_len
);
1370 case GPCMD_READ_TOC_PMA_ATIP
:
1372 int format
, msf
, start_track
, len
;
1373 uint64_t total_sectors
;
1375 bdrv_get_geometry(s
->bs
, &total_sectors
);
1376 total_sectors
>>= 2;
1377 if (total_sectors
== 0) {
1378 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1379 ASC_MEDIUM_NOT_PRESENT
);
1382 max_len
= ube16_to_cpu(packet
+ 7);
1383 format
= packet
[9] >> 6;
1384 msf
= (packet
[1] >> 1) & 1;
1385 start_track
= packet
[6];
1388 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1391 ide_atapi_cmd_reply(s
, len
, max_len
);
1394 /* multi session : only a single session defined */
1399 ide_atapi_cmd_reply(s
, 12, max_len
);
1402 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1405 ide_atapi_cmd_reply(s
, len
, max_len
);
1409 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1410 ASC_INV_FIELD_IN_CMD_PACKET
);
1415 case GPCMD_READ_CDVD_CAPACITY
:
1417 uint64_t total_sectors
;
1419 bdrv_get_geometry(s
->bs
, &total_sectors
);
1420 total_sectors
>>= 2;
1421 if (total_sectors
== 0) {
1422 ide_atapi_cmd_error(s
, SENSE_NOT_READY
,
1423 ASC_MEDIUM_NOT_PRESENT
);
1426 /* NOTE: it is really the number of sectors minus 1 */
1427 cpu_to_ube32(buf
, total_sectors
- 1);
1428 cpu_to_ube32(buf
+ 4, 2048);
1429 ide_atapi_cmd_reply(s
, 8, 8);
1432 case GPCMD_READ_DVD_STRUCTURE
:
1434 int media
= packet
[1];
1435 int format
= packet
[7];
1438 max_len
= ube16_to_cpu(packet
+ 8);
1440 if (format
< 0xff) {
1441 if (media_is_cd(s
)) {
1442 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1443 ASC_INCOMPATIBLE_FORMAT
);
1445 } else if (!media_present(s
)) {
1446 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1447 ASC_INV_FIELD_IN_CMD_PACKET
);
1452 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1453 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1459 ret
= ide_dvd_read_structure(s
, format
, packet
, buf
);
1462 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
, -ret
);
1464 ide_atapi_cmd_reply(s
, ret
, max_len
);
1468 /* TODO: BD support, fall through for now */
1470 /* Generic disk structures */
1471 case 0x80: /* TODO: AACS volume identifier */
1472 case 0x81: /* TODO: AACS media serial number */
1473 case 0x82: /* TODO: AACS media identifier */
1474 case 0x83: /* TODO: AACS media key block */
1475 case 0x90: /* TODO: List of recognized format layers */
1476 case 0xc0: /* TODO: Write protection status */
1478 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1479 ASC_INV_FIELD_IN_CMD_PACKET
);
1484 case GPCMD_SET_SPEED
:
1485 ide_atapi_cmd_ok(s
);
1488 max_len
= packet
[4];
1489 buf
[0] = 0x05; /* CD-ROM */
1490 buf
[1] = 0x80; /* removable */
1491 buf
[2] = 0x00; /* ISO */
1492 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1493 buf
[4] = 31; /* additional length */
1494 buf
[5] = 0; /* reserved */
1495 buf
[6] = 0; /* reserved */
1496 buf
[7] = 0; /* reserved */
1497 padstr8(buf
+ 8, 8, "QEMU");
1498 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
1499 padstr8(buf
+ 32, 4, s
->version
);
1500 ide_atapi_cmd_reply(s
, 36, max_len
);
1502 case GPCMD_GET_CONFIGURATION
:
1507 /* only feature 0 is supported */
1508 if (packet
[2] != 0 || packet
[3] != 0) {
1509 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1510 ASC_INV_FIELD_IN_CMD_PACKET
);
1514 /* XXX: could result in alignment problems in some architectures */
1515 max_len
= ube16_to_cpu(packet
+ 7);
1518 * XXX: avoid overflow for io_buffer if max_len is bigger than
1519 * the size of that buffer (dimensioned to max number of
1520 * sectors to transfer at once)
1522 * Only a problem if the feature/profiles grow.
1524 if (max_len
> 512) /* XXX: assume 1 sector */
1527 memset(buf
, 0, max_len
);
1529 * the number of sectors from the media tells us which profile
1530 * to use as current. 0 means there is no media
1532 if (media_is_dvd(s
))
1533 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
1534 else if (media_is_cd(s
))
1535 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
1537 buf
[10] = 0x02 | 0x01; /* persistent and current */
1538 len
= 12; /* headers: 8 + 4 */
1539 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
1540 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
1541 cpu_to_ube32(buf
, len
- 4); /* data length */
1543 ide_atapi_cmd_reply(s
, len
, max_len
);
1546 case GPCMD_GET_EVENT_STATUS_NOTIFICATION
:
1547 max_len
= ube16_to_cpu(packet
+ 7);
1549 if (packet
[1] & 0x01) { /* polling */
1550 /* We don't support any event class (yet). */
1551 cpu_to_ube16(buf
, 0x00); /* No event descriptor returned */
1552 buf
[2] = 0x80; /* No Event Available (NEA) */
1553 buf
[3] = 0x00; /* Empty supported event classes */
1554 ide_atapi_cmd_reply(s
, 4, max_len
);
1555 } else { /* asynchronous mode */
1556 /* Only polling is supported, asynchronous mode is not. */
1557 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1558 ASC_INV_FIELD_IN_CMD_PACKET
);
1562 ide_atapi_cmd_error(s
, SENSE_ILLEGAL_REQUEST
,
1563 ASC_ILLEGAL_OPCODE
);
1568 static void ide_cfata_metadata_inquiry(IDEState
*s
)
1573 p
= (uint16_t *) s
->io_buffer
;
1574 memset(p
, 0, 0x200);
1575 spd
= ((s
->mdata_size
- 1) >> 9) + 1;
1577 put_le16(p
+ 0, 0x0001); /* Data format revision */
1578 put_le16(p
+ 1, 0x0000); /* Media property: silicon */
1579 put_le16(p
+ 2, s
->media_changed
); /* Media status */
1580 put_le16(p
+ 3, s
->mdata_size
& 0xffff); /* Capacity in bytes (low) */
1581 put_le16(p
+ 4, s
->mdata_size
>> 16); /* Capacity in bytes (high) */
1582 put_le16(p
+ 5, spd
& 0xffff); /* Sectors per device (low) */
1583 put_le16(p
+ 6, spd
>> 16); /* Sectors per device (high) */
1586 static void ide_cfata_metadata_read(IDEState
*s
)
1590 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1591 s
->status
= ERR_STAT
;
1592 s
->error
= ABRT_ERR
;
1596 p
= (uint16_t *) s
->io_buffer
;
1597 memset(p
, 0, 0x200);
1599 put_le16(p
+ 0, s
->media_changed
); /* Media status */
1600 memcpy(p
+ 1, s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1601 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1602 s
->nsector
<< 9), 0x200 - 2));
1605 static void ide_cfata_metadata_write(IDEState
*s
)
1607 if (((s
->hcyl
<< 16) | s
->lcyl
) << 9 > s
->mdata_size
+ 2) {
1608 s
->status
= ERR_STAT
;
1609 s
->error
= ABRT_ERR
;
1613 s
->media_changed
= 0;
1615 memcpy(s
->mdata_storage
+ (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1617 MIN(MIN(s
->mdata_size
- (((s
->hcyl
<< 16) | s
->lcyl
) << 9),
1618 s
->nsector
<< 9), 0x200 - 2));
1621 /* called when the inserted state of the media has changed */
1622 static void cdrom_change_cb(void *opaque
)
1624 IDEState
*s
= opaque
;
1625 uint64_t nb_sectors
;
1627 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1628 s
->nb_sectors
= nb_sectors
;
1630 s
->sense_key
= SENSE_UNIT_ATTENTION
;
1631 s
->asc
= ASC_MEDIUM_MAY_HAVE_CHANGED
;
1632 s
->cdrom_changed
= 1;
1633 ide_set_irq(s
->bus
);
1636 static void ide_cmd_lba48_transform(IDEState
*s
, int lba48
)
1640 /* handle the 'magic' 0 nsector count conversion here. to avoid
1641 * fiddling with the rest of the read logic, we just store the
1642 * full sector count in ->nsector and ignore ->hob_nsector from now
1648 if (!s
->nsector
&& !s
->hob_nsector
)
1651 int lo
= s
->nsector
;
1652 int hi
= s
->hob_nsector
;
1654 s
->nsector
= (hi
<< 8) | lo
;
1659 static void ide_clear_hob(IDEBus
*bus
)
1661 /* any write clears HOB high bit of device control register */
1662 bus
->ifs
[0].select
&= ~(1 << 7);
1663 bus
->ifs
[1].select
&= ~(1 << 7);
1666 void ide_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1668 IDEBus
*bus
= opaque
;
1671 printf("IDE: write addr=0x%x val=0x%02x\n", addr
, val
);
1676 /* ignore writes to command block while busy with previous command */
1677 if (addr
!= 7 && (idebus_active_if(bus
)->status
& (BUSY_STAT
|DRQ_STAT
)))
1685 /* NOTE: data is written to the two drives */
1686 bus
->ifs
[0].hob_feature
= bus
->ifs
[0].feature
;
1687 bus
->ifs
[1].hob_feature
= bus
->ifs
[1].feature
;
1688 bus
->ifs
[0].feature
= val
;
1689 bus
->ifs
[1].feature
= val
;
1693 bus
->ifs
[0].hob_nsector
= bus
->ifs
[0].nsector
;
1694 bus
->ifs
[1].hob_nsector
= bus
->ifs
[1].nsector
;
1695 bus
->ifs
[0].nsector
= val
;
1696 bus
->ifs
[1].nsector
= val
;
1700 bus
->ifs
[0].hob_sector
= bus
->ifs
[0].sector
;
1701 bus
->ifs
[1].hob_sector
= bus
->ifs
[1].sector
;
1702 bus
->ifs
[0].sector
= val
;
1703 bus
->ifs
[1].sector
= val
;
1707 bus
->ifs
[0].hob_lcyl
= bus
->ifs
[0].lcyl
;
1708 bus
->ifs
[1].hob_lcyl
= bus
->ifs
[1].lcyl
;
1709 bus
->ifs
[0].lcyl
= val
;
1710 bus
->ifs
[1].lcyl
= val
;
1714 bus
->ifs
[0].hob_hcyl
= bus
->ifs
[0].hcyl
;
1715 bus
->ifs
[1].hob_hcyl
= bus
->ifs
[1].hcyl
;
1716 bus
->ifs
[0].hcyl
= val
;
1717 bus
->ifs
[1].hcyl
= val
;
1720 /* FIXME: HOB readback uses bit 7 */
1721 bus
->ifs
[0].select
= (val
& ~0x10) | 0xa0;
1722 bus
->ifs
[1].select
= (val
| 0x10) | 0xa0;
1724 bus
->unit
= (val
>> 4) & 1;
1729 ide_exec_cmd(bus
, val
);
1735 void ide_exec_cmd(IDEBus
*bus
, uint32_t val
)
1741 #if defined(DEBUG_IDE)
1742 printf("ide: CMD=%02x\n", val
);
1744 s
= idebus_active_if(bus
);
1745 /* ignore commands to non existant slave */
1746 if (s
!= bus
->ifs
&& !s
->bs
)
1749 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1750 if ((s
->status
& (BUSY_STAT
|DRQ_STAT
)) && val
!= WIN_DEVICE_RESET
)
1755 if (s
->bs
&& s
->drive_kind
!= IDE_CD
) {
1756 if (s
->drive_kind
!= IDE_CFATA
)
1759 ide_cfata_identify(s
);
1760 s
->status
= READY_STAT
| SEEK_STAT
;
1761 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1763 if (s
->drive_kind
== IDE_CD
) {
1764 ide_set_signature(s
);
1766 ide_abort_command(s
);
1768 ide_set_irq(s
->bus
);
1773 s
->status
= READY_STAT
| SEEK_STAT
;
1774 ide_set_irq(s
->bus
);
1777 if (s
->drive_kind
== IDE_CFATA
&& s
->nsector
== 0) {
1778 /* Disable Read and Write Multiple */
1779 s
->mult_sectors
= 0;
1780 s
->status
= READY_STAT
| SEEK_STAT
;
1781 } else if ((s
->nsector
& 0xff) != 0 &&
1782 ((s
->nsector
& 0xff) > MAX_MULT_SECTORS
||
1783 (s
->nsector
& (s
->nsector
- 1)) != 0)) {
1784 ide_abort_command(s
);
1786 s
->mult_sectors
= s
->nsector
& 0xff;
1787 s
->status
= READY_STAT
| SEEK_STAT
;
1789 ide_set_irq(s
->bus
);
1791 case WIN_VERIFY_EXT
:
1794 case WIN_VERIFY_ONCE
:
1795 /* do sector number check ? */
1796 ide_cmd_lba48_transform(s
, lba48
);
1797 s
->status
= READY_STAT
| SEEK_STAT
;
1798 ide_set_irq(s
->bus
);
1806 ide_cmd_lba48_transform(s
, lba48
);
1807 s
->req_nb_sectors
= 1;
1813 case WIN_WRITE_ONCE
:
1814 case CFA_WRITE_SECT_WO_ERASE
:
1815 case WIN_WRITE_VERIFY
:
1816 ide_cmd_lba48_transform(s
, lba48
);
1818 s
->status
= SEEK_STAT
| READY_STAT
;
1819 s
->req_nb_sectors
= 1;
1820 ide_transfer_start(s
, s
->io_buffer
, 512, ide_sector_write
);
1821 s
->media_changed
= 1;
1823 case WIN_MULTREAD_EXT
:
1826 if (!s
->mult_sectors
)
1828 ide_cmd_lba48_transform(s
, lba48
);
1829 s
->req_nb_sectors
= s
->mult_sectors
;
1832 case WIN_MULTWRITE_EXT
:
1835 case CFA_WRITE_MULTI_WO_ERASE
:
1836 if (!s
->mult_sectors
)
1838 ide_cmd_lba48_transform(s
, lba48
);
1840 s
->status
= SEEK_STAT
| READY_STAT
;
1841 s
->req_nb_sectors
= s
->mult_sectors
;
1843 if (n
> s
->req_nb_sectors
)
1844 n
= s
->req_nb_sectors
;
1845 ide_transfer_start(s
, s
->io_buffer
, 512 * n
, ide_sector_write
);
1846 s
->media_changed
= 1;
1848 case WIN_READDMA_EXT
:
1851 case WIN_READDMA_ONCE
:
1854 ide_cmd_lba48_transform(s
, lba48
);
1855 ide_sector_read_dma(s
);
1857 case WIN_WRITEDMA_EXT
:
1860 case WIN_WRITEDMA_ONCE
:
1863 ide_cmd_lba48_transform(s
, lba48
);
1864 ide_sector_write_dma(s
);
1865 s
->media_changed
= 1;
1867 case WIN_READ_NATIVE_MAX_EXT
:
1869 case WIN_READ_NATIVE_MAX
:
1870 ide_cmd_lba48_transform(s
, lba48
);
1871 ide_set_sector(s
, s
->nb_sectors
- 1);
1872 s
->status
= READY_STAT
| SEEK_STAT
;
1873 ide_set_irq(s
->bus
);
1875 case WIN_CHECKPOWERMODE1
:
1876 case WIN_CHECKPOWERMODE2
:
1877 s
->nsector
= 0xff; /* device active or idle */
1878 s
->status
= READY_STAT
| SEEK_STAT
;
1879 ide_set_irq(s
->bus
);
1881 case WIN_SETFEATURES
:
1884 /* XXX: valid for CDROM ? */
1885 switch(s
->feature
) {
1886 case 0xcc: /* reverting to power-on defaults enable */
1887 case 0x66: /* reverting to power-on defaults disable */
1888 case 0x02: /* write cache enable */
1889 case 0x82: /* write cache disable */
1890 case 0xaa: /* read look-ahead enable */
1891 case 0x55: /* read look-ahead disable */
1892 case 0x05: /* set advanced power management mode */
1893 case 0x85: /* disable advanced power management mode */
1894 case 0x69: /* NOP */
1895 case 0x67: /* NOP */
1896 case 0x96: /* NOP */
1897 case 0x9a: /* NOP */
1898 case 0x42: /* enable Automatic Acoustic Mode */
1899 case 0xc2: /* disable Automatic Acoustic Mode */
1900 s
->status
= READY_STAT
| SEEK_STAT
;
1901 ide_set_irq(s
->bus
);
1903 case 0x03: { /* set transfer mode */
1904 uint8_t val
= s
->nsector
& 0x07;
1905 uint16_t *identify_data
= (uint16_t *)s
->identify_data
;
1907 switch (s
->nsector
>> 3) {
1908 case 0x00: /* pio default */
1909 case 0x01: /* pio mode */
1910 put_le16(identify_data
+ 62,0x07);
1911 put_le16(identify_data
+ 63,0x07);
1912 put_le16(identify_data
+ 88,0x3f);
1914 case 0x02: /* sigle word dma mode*/
1915 put_le16(identify_data
+ 62,0x07 | (1 << (val
+ 8)));
1916 put_le16(identify_data
+ 63,0x07);
1917 put_le16(identify_data
+ 88,0x3f);
1919 case 0x04: /* mdma mode */
1920 put_le16(identify_data
+ 62,0x07);
1921 put_le16(identify_data
+ 63,0x07 | (1 << (val
+ 8)));
1922 put_le16(identify_data
+ 88,0x3f);
1924 case 0x08: /* udma mode */
1925 put_le16(identify_data
+ 62,0x07);
1926 put_le16(identify_data
+ 63,0x07);
1927 put_le16(identify_data
+ 88,0x3f | (1 << (val
+ 8)));
1932 s
->status
= READY_STAT
| SEEK_STAT
;
1933 ide_set_irq(s
->bus
);
1940 case WIN_FLUSH_CACHE
:
1941 case WIN_FLUSH_CACHE_EXT
:
1946 case WIN_STANDBYNOW1
:
1947 case WIN_STANDBYNOW2
:
1948 case WIN_IDLEIMMEDIATE
:
1949 case CFA_IDLEIMMEDIATE
:
1954 s
->status
= READY_STAT
;
1955 ide_set_irq(s
->bus
);
1958 if(s
->drive_kind
== IDE_CD
)
1960 /* XXX: Check that seek is within bounds */
1961 s
->status
= READY_STAT
| SEEK_STAT
;
1962 ide_set_irq(s
->bus
);
1964 /* ATAPI commands */
1966 if (s
->drive_kind
== IDE_CD
) {
1967 ide_atapi_identify(s
);
1968 s
->status
= READY_STAT
| SEEK_STAT
;
1969 ide_transfer_start(s
, s
->io_buffer
, 512, ide_transfer_stop
);
1971 ide_abort_command(s
);
1973 ide_set_irq(s
->bus
);
1976 ide_set_signature(s
);
1977 if (s
->drive_kind
== IDE_CD
)
1978 s
->status
= 0; /* ATAPI spec (v6) section 9.10 defines packet
1979 * devices to return a clear status register
1980 * with READY_STAT *not* set. */
1982 s
->status
= READY_STAT
| SEEK_STAT
;
1983 s
->error
= 0x01; /* Device 0 passed, Device 1 passed or not
1986 ide_set_irq(s
->bus
);
1989 if (s
->drive_kind
!= IDE_CD
)
1991 ide_set_signature(s
);
1992 s
->status
= 0x00; /* NOTE: READY is _not_ set */
1996 if (s
->drive_kind
!= IDE_CD
)
1998 /* overlapping commands not supported */
1999 if (s
->feature
& 0x02)
2001 s
->status
= READY_STAT
| SEEK_STAT
;
2002 s
->atapi_dma
= s
->feature
& 1;
2004 ide_transfer_start(s
, s
->io_buffer
, ATAPI_PACKET_SIZE
,
2007 /* CF-ATA commands */
2008 case CFA_REQ_EXT_ERROR_CODE
:
2009 if (s
->drive_kind
!= IDE_CFATA
)
2011 s
->error
= 0x09; /* miscellaneous error */
2012 s
->status
= READY_STAT
| SEEK_STAT
;
2013 ide_set_irq(s
->bus
);
2015 case CFA_ERASE_SECTORS
:
2016 case CFA_WEAR_LEVEL
:
2017 if (s
->drive_kind
!= IDE_CFATA
)
2019 if (val
== CFA_WEAR_LEVEL
)
2021 if (val
== CFA_ERASE_SECTORS
)
2022 s
->media_changed
= 1;
2024 s
->status
= READY_STAT
| SEEK_STAT
;
2025 ide_set_irq(s
->bus
);
2027 case CFA_TRANSLATE_SECTOR
:
2028 if (s
->drive_kind
!= IDE_CFATA
)
2031 s
->status
= READY_STAT
| SEEK_STAT
;
2032 memset(s
->io_buffer
, 0, 0x200);
2033 s
->io_buffer
[0x00] = s
->hcyl
; /* Cyl MSB */
2034 s
->io_buffer
[0x01] = s
->lcyl
; /* Cyl LSB */
2035 s
->io_buffer
[0x02] = s
->select
; /* Head */
2036 s
->io_buffer
[0x03] = s
->sector
; /* Sector */
2037 s
->io_buffer
[0x04] = ide_get_sector(s
) >> 16; /* LBA MSB */
2038 s
->io_buffer
[0x05] = ide_get_sector(s
) >> 8; /* LBA */
2039 s
->io_buffer
[0x06] = ide_get_sector(s
) >> 0; /* LBA LSB */
2040 s
->io_buffer
[0x13] = 0x00; /* Erase flag */
2041 s
->io_buffer
[0x18] = 0x00; /* Hot count */
2042 s
->io_buffer
[0x19] = 0x00; /* Hot count */
2043 s
->io_buffer
[0x1a] = 0x01; /* Hot count */
2044 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2045 ide_set_irq(s
->bus
);
2047 case CFA_ACCESS_METADATA_STORAGE
:
2048 if (s
->drive_kind
!= IDE_CFATA
)
2050 switch (s
->feature
) {
2051 case 0x02: /* Inquiry Metadata Storage */
2052 ide_cfata_metadata_inquiry(s
);
2054 case 0x03: /* Read Metadata Storage */
2055 ide_cfata_metadata_read(s
);
2057 case 0x04: /* Write Metadata Storage */
2058 ide_cfata_metadata_write(s
);
2063 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2064 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2065 ide_set_irq(s
->bus
);
2067 case IBM_SENSE_CONDITION
:
2068 if (s
->drive_kind
!= IDE_CFATA
)
2070 switch (s
->feature
) {
2071 case 0x01: /* sense temperature in device */
2072 s
->nsector
= 0x50; /* +20 C */
2077 s
->status
= READY_STAT
| SEEK_STAT
;
2078 ide_set_irq(s
->bus
);
2082 if (s
->drive_kind
== IDE_CD
)
2084 if (s
->hcyl
!= 0xc2 || s
->lcyl
!= 0x4f)
2086 if (!s
->smart_enabled
&& s
->feature
!= SMART_ENABLE
)
2088 switch (s
->feature
) {
2090 s
->smart_enabled
= 0;
2091 s
->status
= READY_STAT
| SEEK_STAT
;
2092 ide_set_irq(s
->bus
);
2095 s
->smart_enabled
= 1;
2096 s
->status
= READY_STAT
| SEEK_STAT
;
2097 ide_set_irq(s
->bus
);
2099 case SMART_ATTR_AUTOSAVE
:
2100 switch (s
->sector
) {
2102 s
->smart_autosave
= 0;
2105 s
->smart_autosave
= 1;
2110 s
->status
= READY_STAT
| SEEK_STAT
;
2111 ide_set_irq(s
->bus
);
2114 if (!s
->smart_errors
) {
2121 s
->status
= READY_STAT
| SEEK_STAT
;
2122 ide_set_irq(s
->bus
);
2124 case SMART_READ_THRESH
:
2125 memset(s
->io_buffer
, 0, 0x200);
2126 s
->io_buffer
[0] = 0x01; /* smart struct version */
2127 for (n
=0; n
<30; n
++) {
2128 if (smart_attributes
[n
][0] == 0)
2130 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2131 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][4];
2133 for (n
=0; n
<511; n
++) /* checksum */
2134 s
->io_buffer
[511] += s
->io_buffer
[n
];
2135 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2136 s
->status
= READY_STAT
| SEEK_STAT
;
2137 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2138 ide_set_irq(s
->bus
);
2140 case SMART_READ_DATA
:
2141 memset(s
->io_buffer
, 0, 0x200);
2142 s
->io_buffer
[0] = 0x01; /* smart struct version */
2143 for (n
=0; n
<30; n
++) {
2144 if (smart_attributes
[n
][0] == 0)
2146 s
->io_buffer
[2+0+(n
*12)] = smart_attributes
[n
][0];
2147 s
->io_buffer
[2+1+(n
*12)] = smart_attributes
[n
][1];
2148 s
->io_buffer
[2+3+(n
*12)] = smart_attributes
[n
][2];
2149 s
->io_buffer
[2+4+(n
*12)] = smart_attributes
[n
][3];
2151 s
->io_buffer
[362] = 0x02 | (s
->smart_autosave
?0x80:0x00);
2152 if (s
->smart_selftest_count
== 0) {
2153 s
->io_buffer
[363] = 0;
2156 s
->smart_selftest_data
[3 +
2157 (s
->smart_selftest_count
- 1) *
2160 s
->io_buffer
[364] = 0x20;
2161 s
->io_buffer
[365] = 0x01;
2162 /* offline data collection capacity: execute + self-test*/
2163 s
->io_buffer
[367] = (1<<4 | 1<<3 | 1);
2164 s
->io_buffer
[368] = 0x03; /* smart capability (1) */
2165 s
->io_buffer
[369] = 0x00; /* smart capability (2) */
2166 s
->io_buffer
[370] = 0x01; /* error logging supported */
2167 s
->io_buffer
[372] = 0x02; /* minutes for poll short test */
2168 s
->io_buffer
[373] = 0x36; /* minutes for poll ext test */
2169 s
->io_buffer
[374] = 0x01; /* minutes for poll conveyance */
2171 for (n
=0; n
<511; n
++)
2172 s
->io_buffer
[511] += s
->io_buffer
[n
];
2173 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2174 s
->status
= READY_STAT
| SEEK_STAT
;
2175 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2176 ide_set_irq(s
->bus
);
2178 case SMART_READ_LOG
:
2179 switch (s
->sector
) {
2180 case 0x01: /* summary smart error log */
2181 memset(s
->io_buffer
, 0, 0x200);
2182 s
->io_buffer
[0] = 0x01;
2183 s
->io_buffer
[1] = 0x00; /* no error entries */
2184 s
->io_buffer
[452] = s
->smart_errors
& 0xff;
2185 s
->io_buffer
[453] = (s
->smart_errors
& 0xff00) >> 8;
2187 for (n
=0; n
<511; n
++)
2188 s
->io_buffer
[511] += s
->io_buffer
[n
];
2189 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2191 case 0x06: /* smart self test log */
2192 memset(s
->io_buffer
, 0, 0x200);
2193 s
->io_buffer
[0] = 0x01;
2194 if (s
->smart_selftest_count
== 0) {
2195 s
->io_buffer
[508] = 0;
2197 s
->io_buffer
[508] = s
->smart_selftest_count
;
2198 for (n
=2; n
<506; n
++)
2199 s
->io_buffer
[n
] = s
->smart_selftest_data
[n
];
2201 for (n
=0; n
<511; n
++)
2202 s
->io_buffer
[511] += s
->io_buffer
[n
];
2203 s
->io_buffer
[511] = 0x100 - s
->io_buffer
[511];
2208 s
->status
= READY_STAT
| SEEK_STAT
;
2209 ide_transfer_start(s
, s
->io_buffer
, 0x200, ide_transfer_stop
);
2210 ide_set_irq(s
->bus
);
2212 case SMART_EXECUTE_OFFLINE
:
2213 switch (s
->sector
) {
2214 case 0: /* off-line routine */
2215 case 1: /* short self test */
2216 case 2: /* extended self test */
2217 s
->smart_selftest_count
++;
2218 if(s
->smart_selftest_count
> 21)
2219 s
->smart_selftest_count
= 0;
2220 n
= 2 + (s
->smart_selftest_count
- 1) * 24;
2221 s
->smart_selftest_data
[n
] = s
->sector
;
2222 s
->smart_selftest_data
[n
+1] = 0x00; /* OK and finished */
2223 s
->smart_selftest_data
[n
+2] = 0x34; /* hour count lsb */
2224 s
->smart_selftest_data
[n
+3] = 0x12; /* hour count msb */
2225 s
->status
= READY_STAT
| SEEK_STAT
;
2226 ide_set_irq(s
->bus
);
2238 ide_abort_command(s
);
2239 ide_set_irq(s
->bus
);
2244 uint32_t ide_ioport_read(void *opaque
, uint32_t addr1
)
2246 IDEBus
*bus
= opaque
;
2247 IDEState
*s
= idebus_active_if(bus
);
2252 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2253 //hob = s->select & (1 << 7);
2260 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2261 (s
!= bus
->ifs
&& !s
->bs
))
2266 ret
= s
->hob_feature
;
2269 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2272 ret
= s
->nsector
& 0xff;
2274 ret
= s
->hob_nsector
;
2277 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2282 ret
= s
->hob_sector
;
2285 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2293 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2301 if (!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
)
2308 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2309 (s
!= bus
->ifs
&& !s
->bs
))
2313 qemu_irq_lower(bus
->irq
);
2317 printf("ide: read addr=0x%x val=%02x\n", addr1
, ret
);
2322 uint32_t ide_status_read(void *opaque
, uint32_t addr
)
2324 IDEBus
*bus
= opaque
;
2325 IDEState
*s
= idebus_active_if(bus
);
2328 if ((!bus
->ifs
[0].bs
&& !bus
->ifs
[1].bs
) ||
2329 (s
!= bus
->ifs
&& !s
->bs
))
2334 printf("ide: read status addr=0x%x val=%02x\n", addr
, ret
);
2339 void ide_cmd_write(void *opaque
, uint32_t addr
, uint32_t val
)
2341 IDEBus
*bus
= opaque
;
2346 printf("ide: write control addr=0x%x val=%02x\n", addr
, val
);
2348 /* common for both drives */
2349 if (!(bus
->cmd
& IDE_CMD_RESET
) &&
2350 (val
& IDE_CMD_RESET
)) {
2351 /* reset low to high */
2352 for(i
= 0;i
< 2; i
++) {
2354 s
->status
= BUSY_STAT
| SEEK_STAT
;
2357 } else if ((bus
->cmd
& IDE_CMD_RESET
) &&
2358 !(val
& IDE_CMD_RESET
)) {
2360 for(i
= 0;i
< 2; i
++) {
2362 if (s
->drive_kind
== IDE_CD
)
2363 s
->status
= 0x00; /* NOTE: READY is _not_ set */
2365 s
->status
= READY_STAT
| SEEK_STAT
;
2366 ide_set_signature(s
);
2373 void ide_data_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2375 IDEBus
*bus
= opaque
;
2376 IDEState
*s
= idebus_active_if(bus
);
2379 /* PIO data access allowed only when DRQ bit is set */
2380 if (!(s
->status
& DRQ_STAT
))
2384 *(uint16_t *)p
= le16_to_cpu(val
);
2387 if (p
>= s
->data_end
)
2388 s
->end_transfer_func(s
);
2391 uint32_t ide_data_readw(void *opaque
, uint32_t addr
)
2393 IDEBus
*bus
= opaque
;
2394 IDEState
*s
= idebus_active_if(bus
);
2398 /* PIO data access allowed only when DRQ bit is set */
2399 if (!(s
->status
& DRQ_STAT
))
2403 ret
= cpu_to_le16(*(uint16_t *)p
);
2406 if (p
>= s
->data_end
)
2407 s
->end_transfer_func(s
);
2411 void ide_data_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2413 IDEBus
*bus
= opaque
;
2414 IDEState
*s
= idebus_active_if(bus
);
2417 /* PIO data access allowed only when DRQ bit is set */
2418 if (!(s
->status
& DRQ_STAT
))
2422 *(uint32_t *)p
= le32_to_cpu(val
);
2425 if (p
>= s
->data_end
)
2426 s
->end_transfer_func(s
);
2429 uint32_t ide_data_readl(void *opaque
, uint32_t addr
)
2431 IDEBus
*bus
= opaque
;
2432 IDEState
*s
= idebus_active_if(bus
);
2436 /* PIO data access allowed only when DRQ bit is set */
2437 if (!(s
->status
& DRQ_STAT
))
2441 ret
= cpu_to_le32(*(uint32_t *)p
);
2444 if (p
>= s
->data_end
)
2445 s
->end_transfer_func(s
);
2449 static void ide_dummy_transfer_stop(IDEState
*s
)
2451 s
->data_ptr
= s
->io_buffer
;
2452 s
->data_end
= s
->io_buffer
;
2453 s
->io_buffer
[0] = 0xff;
2454 s
->io_buffer
[1] = 0xff;
2455 s
->io_buffer
[2] = 0xff;
2456 s
->io_buffer
[3] = 0xff;
2459 static void ide_reset(IDEState
*s
)
2462 printf("ide: reset\n");
2464 if (s
->drive_kind
== IDE_CFATA
)
2465 s
->mult_sectors
= 0;
2467 s
->mult_sectors
= MAX_MULT_SECTORS
;
2484 s
->status
= READY_STAT
| SEEK_STAT
;
2488 /* ATAPI specific */
2491 s
->cdrom_changed
= 0;
2492 s
->packet_transfer_size
= 0;
2493 s
->elementary_transfer_size
= 0;
2494 s
->io_buffer_index
= 0;
2495 s
->cd_sector_size
= 0;
2498 s
->io_buffer_size
= 0;
2499 s
->req_nb_sectors
= 0;
2501 ide_set_signature(s
);
2502 /* init the transfer handler so that 0xffff is returned on data
2504 s
->end_transfer_func
= ide_dummy_transfer_stop
;
2505 ide_dummy_transfer_stop(s
);
2506 s
->media_changed
= 0;
2509 void ide_bus_reset(IDEBus
*bus
)
2513 ide_reset(&bus
->ifs
[0]);
2514 ide_reset(&bus
->ifs
[1]);
2517 /* pending async DMA */
2518 if (bus
->dma
->aiocb
) {
2520 printf("aio_cancel\n");
2522 bdrv_aio_cancel(bus
->dma
->aiocb
);
2523 bus
->dma
->aiocb
= NULL
;
2526 /* reset dma provider too */
2527 bus
->dma
->ops
->reset(bus
->dma
);
2530 int ide_init_drive(IDEState
*s
, BlockDriverState
*bs
,
2531 const char *version
, const char *serial
)
2533 int cylinders
, heads
, secs
;
2534 uint64_t nb_sectors
;
2537 bdrv_get_geometry(bs
, &nb_sectors
);
2538 bdrv_guess_geometry(bs
, &cylinders
, &heads
, &secs
);
2539 if (cylinders
< 1 || cylinders
> 16383) {
2540 error_report("cyls must be between 1 and 16383");
2543 if (heads
< 1 || heads
> 16) {
2544 error_report("heads must be between 1 and 16");
2547 if (secs
< 1 || secs
> 63) {
2548 error_report("secs must be between 1 and 63");
2551 s
->cylinders
= cylinders
;
2554 s
->nb_sectors
= nb_sectors
;
2555 /* The SMART values should be preserved across power cycles
2557 s
->smart_enabled
= 1;
2558 s
->smart_autosave
= 1;
2559 s
->smart_errors
= 0;
2560 s
->smart_selftest_count
= 0;
2561 if (bdrv_get_type_hint(bs
) == BDRV_TYPE_CDROM
) {
2562 s
->drive_kind
= IDE_CD
;
2563 bdrv_set_change_cb(bs
, cdrom_change_cb
, s
);
2564 bs
->buffer_alignment
= 2048;
2566 if (!bdrv_is_inserted(s
->bs
)) {
2567 error_report("Device needs media, but drive is empty");
2570 if (bdrv_is_read_only(bs
)) {
2571 error_report("Can't use a read-only drive");
2576 strncpy(s
->drive_serial_str
, serial
, sizeof(s
->drive_serial_str
));
2578 snprintf(s
->drive_serial_str
, sizeof(s
->drive_serial_str
),
2579 "QM%05d", s
->drive_serial
);
2582 pstrcpy(s
->version
, sizeof(s
->version
), version
);
2584 pstrcpy(s
->version
, sizeof(s
->version
), QEMU_VERSION
);
2588 bdrv_set_removable(bs
, s
->drive_kind
== IDE_CD
);
2592 static void ide_init1(IDEBus
*bus
, int unit
)
2594 static int drive_serial
= 1;
2595 IDEState
*s
= &bus
->ifs
[unit
];
2599 s
->drive_serial
= drive_serial
++;
2600 /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
2601 s
->io_buffer
= qemu_memalign(2048, IDE_DMA_BUF_SECTORS
*512 + 4);
2602 s
->io_buffer_total_len
= IDE_DMA_BUF_SECTORS
*512 + 4;
2603 s
->smart_selftest_data
= qemu_blockalign(s
->bs
, 512);
2604 s
->sector_write_timer
= qemu_new_timer(vm_clock
,
2605 ide_sector_write_timer_cb
, s
);
2608 static void ide_nop_start(IDEDMA
*dma
, IDEState
*s
,
2609 BlockDriverCompletionFunc
*cb
)
2613 static int ide_nop(IDEDMA
*dma
)
2618 static int ide_nop_int(IDEDMA
*dma
, int x
)
2623 static void ide_nop_restart(void *opaque
, int x
, int y
)
2627 static const IDEDMAOps ide_dma_nop_ops
= {
2628 .start_dma
= ide_nop_start
,
2629 .start_transfer
= ide_nop
,
2630 .prepare_buf
= ide_nop_int
,
2631 .rw_buf
= ide_nop_int
,
2632 .set_unit
= ide_nop_int
,
2633 .add_status
= ide_nop_int
,
2634 .set_inactive
= ide_nop
,
2635 .restart_cb
= ide_nop_restart
,
2639 static IDEDMA ide_dma_nop
= {
2640 .ops
= &ide_dma_nop_ops
,
2644 void ide_init2(IDEBus
*bus
, qemu_irq irq
)
2648 for(i
= 0; i
< 2; i
++) {
2650 ide_reset(&bus
->ifs
[i
]);
2653 bus
->dma
= &ide_dma_nop
;
2656 /* TODO convert users to qdev and remove */
2657 void ide_init2_with_non_qdev_drives(IDEBus
*bus
, DriveInfo
*hd0
,
2658 DriveInfo
*hd1
, qemu_irq irq
)
2663 for(i
= 0; i
< 2; i
++) {
2664 dinfo
= i
== 0 ? hd0
: hd1
;
2667 if (ide_init_drive(&bus
->ifs
[i
], dinfo
->bdrv
, NULL
,
2668 *dinfo
->serial
? dinfo
->serial
: NULL
) < 0) {
2669 error_report("Can't set up IDE drive %s", dinfo
->id
);
2673 ide_reset(&bus
->ifs
[i
]);
2677 bus
->dma
= &ide_dma_nop
;
2680 void ide_init_ioport(IDEBus
*bus
, int iobase
, int iobase2
)
2682 register_ioport_write(iobase
, 8, 1, ide_ioport_write
, bus
);
2683 register_ioport_read(iobase
, 8, 1, ide_ioport_read
, bus
);
2685 register_ioport_read(iobase2
, 1, 1, ide_status_read
, bus
);
2686 register_ioport_write(iobase2
, 1, 1, ide_cmd_write
, bus
);
2690 register_ioport_write(iobase
, 2, 2, ide_data_writew
, bus
);
2691 register_ioport_read(iobase
, 2, 2, ide_data_readw
, bus
);
2692 register_ioport_write(iobase
, 4, 4, ide_data_writel
, bus
);
2693 register_ioport_read(iobase
, 4, 4, ide_data_readl
, bus
);
2696 static bool is_identify_set(void *opaque
, int version_id
)
2698 IDEState
*s
= opaque
;
2700 return s
->identify_set
!= 0;
2703 static EndTransferFunc
* transfer_end_table
[] = {
2707 ide_atapi_cmd_reply_end
,
2709 ide_dummy_transfer_stop
,
2712 static int transfer_end_table_idx(EndTransferFunc
*fn
)
2716 for (i
= 0; i
< ARRAY_SIZE(transfer_end_table
); i
++)
2717 if (transfer_end_table
[i
] == fn
)
2723 static int ide_drive_post_load(void *opaque
, int version_id
)
2725 IDEState
*s
= opaque
;
2727 if (version_id
< 3) {
2728 if (s
->sense_key
== SENSE_UNIT_ATTENTION
&&
2729 s
->asc
== ASC_MEDIUM_MAY_HAVE_CHANGED
) {
2730 s
->cdrom_changed
= 1;
2736 static int ide_drive_pio_post_load(void *opaque
, int version_id
)
2738 IDEState
*s
= opaque
;
2740 if (s
->end_transfer_fn_idx
> ARRAY_SIZE(transfer_end_table
)) {
2743 s
->end_transfer_func
= transfer_end_table
[s
->end_transfer_fn_idx
];
2744 s
->data_ptr
= s
->io_buffer
+ s
->cur_io_buffer_offset
;
2745 s
->data_end
= s
->data_ptr
+ s
->cur_io_buffer_len
;
2750 static void ide_drive_pio_pre_save(void *opaque
)
2752 IDEState
*s
= opaque
;
2755 s
->cur_io_buffer_offset
= s
->data_ptr
- s
->io_buffer
;
2756 s
->cur_io_buffer_len
= s
->data_end
- s
->data_ptr
;
2758 idx
= transfer_end_table_idx(s
->end_transfer_func
);
2760 fprintf(stderr
, "%s: invalid end_transfer_func for DRQ_STAT\n",
2762 s
->end_transfer_fn_idx
= 2;
2764 s
->end_transfer_fn_idx
= idx
;
2768 static bool ide_drive_pio_state_needed(void *opaque
)
2770 IDEState
*s
= opaque
;
2772 return (s
->status
& DRQ_STAT
) != 0;
2775 const VMStateDescription vmstate_ide_drive_pio_state
= {
2776 .name
= "ide_drive/pio_state",
2778 .minimum_version_id
= 1,
2779 .minimum_version_id_old
= 1,
2780 .pre_save
= ide_drive_pio_pre_save
,
2781 .post_load
= ide_drive_pio_post_load
,
2782 .fields
= (VMStateField
[]) {
2783 VMSTATE_INT32(req_nb_sectors
, IDEState
),
2784 VMSTATE_VARRAY_INT32(io_buffer
, IDEState
, io_buffer_total_len
, 1,
2785 vmstate_info_uint8
, uint8_t),
2786 VMSTATE_INT32(cur_io_buffer_offset
, IDEState
),
2787 VMSTATE_INT32(cur_io_buffer_len
, IDEState
),
2788 VMSTATE_UINT8(end_transfer_fn_idx
, IDEState
),
2789 VMSTATE_INT32(elementary_transfer_size
, IDEState
),
2790 VMSTATE_INT32(packet_transfer_size
, IDEState
),
2791 VMSTATE_END_OF_LIST()
2795 const VMStateDescription vmstate_ide_drive
= {
2796 .name
= "ide_drive",
2798 .minimum_version_id
= 0,
2799 .minimum_version_id_old
= 0,
2800 .post_load
= ide_drive_post_load
,
2801 .fields
= (VMStateField
[]) {
2802 VMSTATE_INT32(mult_sectors
, IDEState
),
2803 VMSTATE_INT32(identify_set
, IDEState
),
2804 VMSTATE_BUFFER_TEST(identify_data
, IDEState
, is_identify_set
),
2805 VMSTATE_UINT8(feature
, IDEState
),
2806 VMSTATE_UINT8(error
, IDEState
),
2807 VMSTATE_UINT32(nsector
, IDEState
),
2808 VMSTATE_UINT8(sector
, IDEState
),
2809 VMSTATE_UINT8(lcyl
, IDEState
),
2810 VMSTATE_UINT8(hcyl
, IDEState
),
2811 VMSTATE_UINT8(hob_feature
, IDEState
),
2812 VMSTATE_UINT8(hob_sector
, IDEState
),
2813 VMSTATE_UINT8(hob_nsector
, IDEState
),
2814 VMSTATE_UINT8(hob_lcyl
, IDEState
),
2815 VMSTATE_UINT8(hob_hcyl
, IDEState
),
2816 VMSTATE_UINT8(select
, IDEState
),
2817 VMSTATE_UINT8(status
, IDEState
),
2818 VMSTATE_UINT8(lba48
, IDEState
),
2819 VMSTATE_UINT8(sense_key
, IDEState
),
2820 VMSTATE_UINT8(asc
, IDEState
),
2821 VMSTATE_UINT8_V(cdrom_changed
, IDEState
, 3),
2822 VMSTATE_END_OF_LIST()
2824 .subsections
= (VMStateSubsection
[]) {
2826 .vmsd
= &vmstate_ide_drive_pio_state
,
2827 .needed
= ide_drive_pio_state_needed
,
2834 const VMStateDescription vmstate_ide_bus
= {
2837 .minimum_version_id
= 1,
2838 .minimum_version_id_old
= 1,
2839 .fields
= (VMStateField
[]) {
2840 VMSTATE_UINT8(cmd
, IDEBus
),
2841 VMSTATE_UINT8(unit
, IDEBus
),
2842 VMSTATE_END_OF_LIST()