]> git.proxmox.com Git - qemu.git/blob - hw/ide/core.c
ide: Use some already defined page macros instead of constants
[qemu.git] / hw / ide / core.c
1 /*
2 * QEMU IDE disk and CD/DVD-ROM Emulator
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
25 #include <hw/hw.h>
26 #include <hw/pc.h>
27 #include <hw/pci.h>
28 #include <hw/scsi.h>
29 #include <hw/sh.h>
30 #include "block.h"
31 #include "block_int.h"
32 #include "qemu-timer.h"
33 #include "sysemu.h"
34 #include "dma.h"
35
36 #include <hw/ide/internal.h>
37
38 static int smart_attributes[][5] = {
39 /* id, flags, val, wrst, thrsh */
40 { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
41 { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
42 { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
43 { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
44 { 0x00, 0x00, 0x00, 0x00, 0x00}
45 };
46
47 /* XXX: DVDs that could fit on a CD will be reported as a CD */
48 static inline int media_present(IDEState *s)
49 {
50 return (s->nb_sectors > 0);
51 }
52
53 static inline int media_is_dvd(IDEState *s)
54 {
55 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
56 }
57
58 static inline int media_is_cd(IDEState *s)
59 {
60 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
61 }
62
63 static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb);
64 static void ide_dma_restart(IDEState *s, int is_read);
65 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
66 static int ide_handle_rw_error(IDEState *s, int error, int op);
67
68 static void padstr(char *str, const char *src, int len)
69 {
70 int i, v;
71 for(i = 0; i < len; i++) {
72 if (*src)
73 v = *src++;
74 else
75 v = ' ';
76 str[i^1] = v;
77 }
78 }
79
80 static void padstr8(uint8_t *buf, int buf_size, const char *src)
81 {
82 int i;
83 for(i = 0; i < buf_size; i++) {
84 if (*src)
85 buf[i] = *src++;
86 else
87 buf[i] = ' ';
88 }
89 }
90
91 static void put_le16(uint16_t *p, unsigned int v)
92 {
93 *p = cpu_to_le16(v);
94 }
95
96 static void ide_identify(IDEState *s)
97 {
98 uint16_t *p;
99 unsigned int oldsize;
100
101 if (s->identify_set) {
102 memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
103 return;
104 }
105
106 memset(s->io_buffer, 0, 512);
107 p = (uint16_t *)s->io_buffer;
108 put_le16(p + 0, 0x0040);
109 put_le16(p + 1, s->cylinders);
110 put_le16(p + 3, s->heads);
111 put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
112 put_le16(p + 5, 512); /* XXX: retired, remove ? */
113 put_le16(p + 6, s->sectors);
114 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
115 put_le16(p + 20, 3); /* XXX: retired, remove ? */
116 put_le16(p + 21, 512); /* cache size in sectors */
117 put_le16(p + 22, 4); /* ecc bytes */
118 padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware version */
119 padstr((char *)(p + 27), "QEMU HARDDISK", 40); /* model */
120 #if MAX_MULT_SECTORS > 1
121 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
122 #endif
123 put_le16(p + 48, 1); /* dword I/O */
124 put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
125 put_le16(p + 51, 0x200); /* PIO transfer cycle */
126 put_le16(p + 52, 0x200); /* DMA transfer cycle */
127 put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
128 put_le16(p + 54, s->cylinders);
129 put_le16(p + 55, s->heads);
130 put_le16(p + 56, s->sectors);
131 oldsize = s->cylinders * s->heads * s->sectors;
132 put_le16(p + 57, oldsize);
133 put_le16(p + 58, oldsize >> 16);
134 if (s->mult_sectors)
135 put_le16(p + 59, 0x100 | s->mult_sectors);
136 put_le16(p + 60, s->nb_sectors);
137 put_le16(p + 61, s->nb_sectors >> 16);
138 put_le16(p + 62, 0x07); /* single word dma0-2 supported */
139 put_le16(p + 63, 0x07); /* mdma0-2 supported */
140 put_le16(p + 65, 120);
141 put_le16(p + 66, 120);
142 put_le16(p + 67, 120);
143 put_le16(p + 68, 120);
144 put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
145 put_le16(p + 81, 0x16); /* conforms to ata5 */
146 /* 14=NOP supported, 0=SMART supported */
147 put_le16(p + 82, (1 << 14) | 1);
148 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
149 put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
150 /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
151 put_le16(p + 84, (1 << 14) | 0);
152 /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
153 if (bdrv_enable_write_cache(s->bs))
154 put_le16(p + 85, (1 << 14) | (1 << 5) | 1);
155 else
156 put_le16(p + 85, (1 << 14) | 1);
157 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
158 put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
159 /* 14=set to 1, 1=smart self test, 0=smart error logging */
160 put_le16(p + 87, (1 << 14) | 0);
161 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
162 put_le16(p + 93, 1 | (1 << 14) | 0x2000);
163 put_le16(p + 100, s->nb_sectors);
164 put_le16(p + 101, s->nb_sectors >> 16);
165 put_le16(p + 102, s->nb_sectors >> 32);
166 put_le16(p + 103, s->nb_sectors >> 48);
167
168 memcpy(s->identify_data, p, sizeof(s->identify_data));
169 s->identify_set = 1;
170 }
171
172 static void ide_atapi_identify(IDEState *s)
173 {
174 uint16_t *p;
175
176 if (s->identify_set) {
177 memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
178 return;
179 }
180
181 memset(s->io_buffer, 0, 512);
182 p = (uint16_t *)s->io_buffer;
183 /* Removable CDROM, 50us response, 12 byte packets */
184 put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
185 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
186 put_le16(p + 20, 3); /* buffer type */
187 put_le16(p + 21, 512); /* cache size in sectors */
188 put_le16(p + 22, 4); /* ecc bytes */
189 padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware version */
190 padstr((char *)(p + 27), "QEMU DVD-ROM", 40); /* model */
191 put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
192 #ifdef USE_DMA_CDROM
193 put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
194 put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
195 put_le16(p + 62, 7); /* single word dma0-2 supported */
196 put_le16(p + 63, 7); /* mdma0-2 supported */
197 put_le16(p + 64, 0x3f); /* PIO modes supported */
198 #else
199 put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
200 put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
201 put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
202 put_le16(p + 64, 1); /* PIO modes */
203 #endif
204 put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
205 put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
206 put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
207 put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
208
209 put_le16(p + 71, 30); /* in ns */
210 put_le16(p + 72, 30); /* in ns */
211
212 put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
213 #ifdef USE_DMA_CDROM
214 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
215 #endif
216 memcpy(s->identify_data, p, sizeof(s->identify_data));
217 s->identify_set = 1;
218 }
219
220 static void ide_cfata_identify(IDEState *s)
221 {
222 uint16_t *p;
223 uint32_t cur_sec;
224
225 p = (uint16_t *) s->identify_data;
226 if (s->identify_set)
227 goto fill_buffer;
228
229 memset(p, 0, sizeof(s->identify_data));
230
231 cur_sec = s->cylinders * s->heads * s->sectors;
232
233 put_le16(p + 0, 0x848a); /* CF Storage Card signature */
234 put_le16(p + 1, s->cylinders); /* Default cylinders */
235 put_le16(p + 3, s->heads); /* Default heads */
236 put_le16(p + 6, s->sectors); /* Default sectors per track */
237 put_le16(p + 7, s->nb_sectors >> 16); /* Sectors per card */
238 put_le16(p + 8, s->nb_sectors); /* Sectors per card */
239 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
240 put_le16(p + 22, 0x0004); /* ECC bytes */
241 padstr((char *) (p + 23), QEMU_VERSION, 8); /* Firmware Revision */
242 padstr((char *) (p + 27), "QEMU MICRODRIVE", 40);/* Model number */
243 #if MAX_MULT_SECTORS > 1
244 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
245 #else
246 put_le16(p + 47, 0x0000);
247 #endif
248 put_le16(p + 49, 0x0f00); /* Capabilities */
249 put_le16(p + 51, 0x0002); /* PIO cycle timing mode */
250 put_le16(p + 52, 0x0001); /* DMA cycle timing mode */
251 put_le16(p + 53, 0x0003); /* Translation params valid */
252 put_le16(p + 54, s->cylinders); /* Current cylinders */
253 put_le16(p + 55, s->heads); /* Current heads */
254 put_le16(p + 56, s->sectors); /* Current sectors */
255 put_le16(p + 57, cur_sec); /* Current capacity */
256 put_le16(p + 58, cur_sec >> 16); /* Current capacity */
257 if (s->mult_sectors) /* Multiple sector setting */
258 put_le16(p + 59, 0x100 | s->mult_sectors);
259 put_le16(p + 60, s->nb_sectors); /* Total LBA sectors */
260 put_le16(p + 61, s->nb_sectors >> 16); /* Total LBA sectors */
261 put_le16(p + 63, 0x0203); /* Multiword DMA capability */
262 put_le16(p + 64, 0x0001); /* Flow Control PIO support */
263 put_le16(p + 65, 0x0096); /* Min. Multiword DMA cycle */
264 put_le16(p + 66, 0x0096); /* Rec. Multiword DMA cycle */
265 put_le16(p + 68, 0x00b4); /* Min. PIO cycle time */
266 put_le16(p + 82, 0x400c); /* Command Set supported */
267 put_le16(p + 83, 0x7068); /* Command Set supported */
268 put_le16(p + 84, 0x4000); /* Features supported */
269 put_le16(p + 85, 0x000c); /* Command Set enabled */
270 put_le16(p + 86, 0x7044); /* Command Set enabled */
271 put_le16(p + 87, 0x4000); /* Features enabled */
272 put_le16(p + 91, 0x4060); /* Current APM level */
273 put_le16(p + 129, 0x0002); /* Current features option */
274 put_le16(p + 130, 0x0005); /* Reassigned sectors */
275 put_le16(p + 131, 0x0001); /* Initial power mode */
276 put_le16(p + 132, 0x0000); /* User signature */
277 put_le16(p + 160, 0x8100); /* Power requirement */
278 put_le16(p + 161, 0x8001); /* CF command set */
279
280 s->identify_set = 1;
281
282 fill_buffer:
283 memcpy(s->io_buffer, p, sizeof(s->identify_data));
284 }
285
286 static void ide_set_signature(IDEState *s)
287 {
288 s->select &= 0xf0; /* clear head */
289 /* put signature */
290 s->nsector = 1;
291 s->sector = 1;
292 if (s->is_cdrom) {
293 s->lcyl = 0x14;
294 s->hcyl = 0xeb;
295 } else if (s->bs) {
296 s->lcyl = 0;
297 s->hcyl = 0;
298 } else {
299 s->lcyl = 0xff;
300 s->hcyl = 0xff;
301 }
302 }
303
304 static inline void ide_abort_command(IDEState *s)
305 {
306 s->status = READY_STAT | ERR_STAT;
307 s->error = ABRT_ERR;
308 }
309
310 static inline void ide_dma_submit_check(IDEState *s,
311 BlockDriverCompletionFunc *dma_cb, BMDMAState *bm)
312 {
313 if (bm->aiocb)
314 return;
315 dma_cb(bm, -1);
316 }
317
318 /* prepare data transfer and tell what to do after */
319 static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
320 EndTransferFunc *end_transfer_func)
321 {
322 s->end_transfer_func = end_transfer_func;
323 s->data_ptr = buf;
324 s->data_end = buf + size;
325 if (!(s->status & ERR_STAT))
326 s->status |= DRQ_STAT;
327 }
328
329 static void ide_transfer_stop(IDEState *s)
330 {
331 s->end_transfer_func = ide_transfer_stop;
332 s->data_ptr = s->io_buffer;
333 s->data_end = s->io_buffer;
334 s->status &= ~DRQ_STAT;
335 }
336
337 int64_t ide_get_sector(IDEState *s)
338 {
339 int64_t sector_num;
340 if (s->select & 0x40) {
341 /* lba */
342 if (!s->lba48) {
343 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
344 (s->lcyl << 8) | s->sector;
345 } else {
346 sector_num = ((int64_t)s->hob_hcyl << 40) |
347 ((int64_t) s->hob_lcyl << 32) |
348 ((int64_t) s->hob_sector << 24) |
349 ((int64_t) s->hcyl << 16) |
350 ((int64_t) s->lcyl << 8) | s->sector;
351 }
352 } else {
353 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
354 (s->select & 0x0f) * s->sectors + (s->sector - 1);
355 }
356 return sector_num;
357 }
358
359 void ide_set_sector(IDEState *s, int64_t sector_num)
360 {
361 unsigned int cyl, r;
362 if (s->select & 0x40) {
363 if (!s->lba48) {
364 s->select = (s->select & 0xf0) | (sector_num >> 24);
365 s->hcyl = (sector_num >> 16);
366 s->lcyl = (sector_num >> 8);
367 s->sector = (sector_num);
368 } else {
369 s->sector = sector_num;
370 s->lcyl = sector_num >> 8;
371 s->hcyl = sector_num >> 16;
372 s->hob_sector = sector_num >> 24;
373 s->hob_lcyl = sector_num >> 32;
374 s->hob_hcyl = sector_num >> 40;
375 }
376 } else {
377 cyl = sector_num / (s->heads * s->sectors);
378 r = sector_num % (s->heads * s->sectors);
379 s->hcyl = cyl >> 8;
380 s->lcyl = cyl;
381 s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
382 s->sector = (r % s->sectors) + 1;
383 }
384 }
385
386 static void ide_rw_error(IDEState *s) {
387 ide_abort_command(s);
388 ide_set_irq(s->bus);
389 }
390
391 static void ide_sector_read(IDEState *s)
392 {
393 int64_t sector_num;
394 int ret, n;
395
396 s->status = READY_STAT | SEEK_STAT;
397 s->error = 0; /* not needed by IDE spec, but needed by Windows */
398 sector_num = ide_get_sector(s);
399 n = s->nsector;
400 if (n == 0) {
401 /* no more sector to read from disk */
402 ide_transfer_stop(s);
403 } else {
404 #if defined(DEBUG_IDE)
405 printf("read sector=%" PRId64 "\n", sector_num);
406 #endif
407 if (n > s->req_nb_sectors)
408 n = s->req_nb_sectors;
409 ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
410 if (ret != 0) {
411 if (ide_handle_rw_error(s, -ret,
412 BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ))
413 {
414 return;
415 }
416 }
417 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
418 ide_set_irq(s->bus);
419 ide_set_sector(s, sector_num + n);
420 s->nsector -= n;
421 }
422 }
423
424
425 /* return 0 if buffer completed */
426 static int dma_buf_prepare(BMDMAState *bm, int is_write)
427 {
428 IDEState *s = bmdma_active_if(bm);
429 struct {
430 uint32_t addr;
431 uint32_t size;
432 } prd;
433 int l, len;
434
435 qemu_sglist_init(&s->sg, s->nsector / (TARGET_PAGE_SIZE/512) + 1);
436 s->io_buffer_size = 0;
437 for(;;) {
438 if (bm->cur_prd_len == 0) {
439 /* end of table (with a fail safe of one page) */
440 if (bm->cur_prd_last ||
441 (bm->cur_addr - bm->addr) >= 4096)
442 return s->io_buffer_size != 0;
443 cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
444 bm->cur_addr += 8;
445 prd.addr = le32_to_cpu(prd.addr);
446 prd.size = le32_to_cpu(prd.size);
447 len = prd.size & 0xfffe;
448 if (len == 0)
449 len = 0x10000;
450 bm->cur_prd_len = len;
451 bm->cur_prd_addr = prd.addr;
452 bm->cur_prd_last = (prd.size & 0x80000000);
453 }
454 l = bm->cur_prd_len;
455 if (l > 0) {
456 qemu_sglist_add(&s->sg, bm->cur_prd_addr, l);
457 bm->cur_prd_addr += l;
458 bm->cur_prd_len -= l;
459 s->io_buffer_size += l;
460 }
461 }
462 return 1;
463 }
464
465 static void dma_buf_commit(IDEState *s, int is_write)
466 {
467 qemu_sglist_destroy(&s->sg);
468 }
469
470 void ide_dma_error(IDEState *s)
471 {
472 ide_transfer_stop(s);
473 s->error = ABRT_ERR;
474 s->status = READY_STAT | ERR_STAT;
475 ide_set_irq(s->bus);
476 }
477
478 static int ide_handle_rw_error(IDEState *s, int error, int op)
479 {
480 int is_read = (op & BM_STATUS_RETRY_READ);
481 BlockInterfaceErrorAction action = drive_get_on_error(s->bs, is_read);
482
483 if (action == BLOCK_ERR_IGNORE)
484 return 0;
485
486 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
487 || action == BLOCK_ERR_STOP_ANY) {
488 s->bus->bmdma->unit = s->unit;
489 s->bus->bmdma->status |= op;
490 vm_stop(0);
491 } else {
492 if (op & BM_STATUS_DMA_RETRY) {
493 dma_buf_commit(s, 0);
494 ide_dma_error(s);
495 } else {
496 ide_rw_error(s);
497 }
498 }
499
500 return 1;
501 }
502
503 /* return 0 if buffer completed */
504 static int dma_buf_rw(BMDMAState *bm, int is_write)
505 {
506 IDEState *s = bmdma_active_if(bm);
507 struct {
508 uint32_t addr;
509 uint32_t size;
510 } prd;
511 int l, len;
512
513 for(;;) {
514 l = s->io_buffer_size - s->io_buffer_index;
515 if (l <= 0)
516 break;
517 if (bm->cur_prd_len == 0) {
518 /* end of table (with a fail safe of one page) */
519 if (bm->cur_prd_last ||
520 (bm->cur_addr - bm->addr) >= 4096)
521 return 0;
522 cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
523 bm->cur_addr += 8;
524 prd.addr = le32_to_cpu(prd.addr);
525 prd.size = le32_to_cpu(prd.size);
526 len = prd.size & 0xfffe;
527 if (len == 0)
528 len = 0x10000;
529 bm->cur_prd_len = len;
530 bm->cur_prd_addr = prd.addr;
531 bm->cur_prd_last = (prd.size & 0x80000000);
532 }
533 if (l > bm->cur_prd_len)
534 l = bm->cur_prd_len;
535 if (l > 0) {
536 if (is_write) {
537 cpu_physical_memory_write(bm->cur_prd_addr,
538 s->io_buffer + s->io_buffer_index, l);
539 } else {
540 cpu_physical_memory_read(bm->cur_prd_addr,
541 s->io_buffer + s->io_buffer_index, l);
542 }
543 bm->cur_prd_addr += l;
544 bm->cur_prd_len -= l;
545 s->io_buffer_index += l;
546 }
547 }
548 return 1;
549 }
550
551 static void ide_read_dma_cb(void *opaque, int ret)
552 {
553 BMDMAState *bm = opaque;
554 IDEState *s = bmdma_active_if(bm);
555 int n;
556 int64_t sector_num;
557
558 if (ret < 0) {
559 if (ide_handle_rw_error(s, -ret,
560 BM_STATUS_DMA_RETRY | BM_STATUS_RETRY_READ))
561 {
562 return;
563 }
564 }
565
566 n = s->io_buffer_size >> 9;
567 sector_num = ide_get_sector(s);
568 if (n > 0) {
569 dma_buf_commit(s, 1);
570 sector_num += n;
571 ide_set_sector(s, sector_num);
572 s->nsector -= n;
573 }
574
575 /* end of transfer ? */
576 if (s->nsector == 0) {
577 s->status = READY_STAT | SEEK_STAT;
578 ide_set_irq(s->bus);
579 eot:
580 bm->status &= ~BM_STATUS_DMAING;
581 bm->status |= BM_STATUS_INT;
582 bm->dma_cb = NULL;
583 bm->unit = -1;
584 bm->aiocb = NULL;
585 return;
586 }
587
588 /* launch next transfer */
589 n = s->nsector;
590 s->io_buffer_index = 0;
591 s->io_buffer_size = n * 512;
592 if (dma_buf_prepare(bm, 1) == 0)
593 goto eot;
594 #ifdef DEBUG_AIO
595 printf("aio_read: sector_num=%" PRId64 " n=%d\n", sector_num, n);
596 #endif
597 bm->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num, ide_read_dma_cb, bm);
598 ide_dma_submit_check(s, ide_read_dma_cb, bm);
599 }
600
601 static void ide_sector_read_dma(IDEState *s)
602 {
603 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
604 s->io_buffer_index = 0;
605 s->io_buffer_size = 0;
606 s->is_read = 1;
607 ide_dma_start(s, ide_read_dma_cb);
608 }
609
610 static void ide_sector_write_timer_cb(void *opaque)
611 {
612 IDEState *s = opaque;
613 ide_set_irq(s->bus);
614 }
615
616 static void ide_sector_write(IDEState *s)
617 {
618 int64_t sector_num;
619 int ret, n, n1;
620
621 s->status = READY_STAT | SEEK_STAT;
622 sector_num = ide_get_sector(s);
623 #if defined(DEBUG_IDE)
624 printf("write sector=%" PRId64 "\n", sector_num);
625 #endif
626 n = s->nsector;
627 if (n > s->req_nb_sectors)
628 n = s->req_nb_sectors;
629 ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
630
631 if (ret != 0) {
632 if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY))
633 return;
634 }
635
636 s->nsector -= n;
637 if (s->nsector == 0) {
638 /* no more sectors to write */
639 ide_transfer_stop(s);
640 } else {
641 n1 = s->nsector;
642 if (n1 > s->req_nb_sectors)
643 n1 = s->req_nb_sectors;
644 ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
645 }
646 ide_set_sector(s, sector_num + n);
647
648 #ifdef TARGET_I386
649 if (win2k_install_hack && ((++s->irq_count % 16) == 0)) {
650 /* It seems there is a bug in the Windows 2000 installer HDD
651 IDE driver which fills the disk with empty logs when the
652 IDE write IRQ comes too early. This hack tries to correct
653 that at the expense of slower write performances. Use this
654 option _only_ to install Windows 2000. You must disable it
655 for normal use. */
656 qemu_mod_timer(s->sector_write_timer,
657 qemu_get_clock(vm_clock) + (get_ticks_per_sec() / 1000));
658 } else
659 #endif
660 {
661 ide_set_irq(s->bus);
662 }
663 }
664
665 static void ide_dma_restart_bh(void *opaque)
666 {
667 BMDMAState *bm = opaque;
668 int is_read;
669
670 qemu_bh_delete(bm->bh);
671 bm->bh = NULL;
672
673 is_read = !!(bm->status & BM_STATUS_RETRY_READ);
674
675 if (bm->status & BM_STATUS_DMA_RETRY) {
676 bm->status &= ~(BM_STATUS_DMA_RETRY | BM_STATUS_RETRY_READ);
677 ide_dma_restart(bmdma_active_if(bm), is_read);
678 } else if (bm->status & BM_STATUS_PIO_RETRY) {
679 bm->status &= ~(BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ);
680 if (is_read) {
681 ide_sector_read(bmdma_active_if(bm));
682 } else {
683 ide_sector_write(bmdma_active_if(bm));
684 }
685 }
686 }
687
688 void ide_dma_restart_cb(void *opaque, int running, int reason)
689 {
690 BMDMAState *bm = opaque;
691
692 if (!running)
693 return;
694
695 if (!bm->bh) {
696 bm->bh = qemu_bh_new(ide_dma_restart_bh, bm);
697 qemu_bh_schedule(bm->bh);
698 }
699 }
700
701 static void ide_write_dma_cb(void *opaque, int ret)
702 {
703 BMDMAState *bm = opaque;
704 IDEState *s = bmdma_active_if(bm);
705 int n;
706 int64_t sector_num;
707
708 if (ret < 0) {
709 if (ide_handle_rw_error(s, -ret, BM_STATUS_DMA_RETRY))
710 return;
711 }
712
713 n = s->io_buffer_size >> 9;
714 sector_num = ide_get_sector(s);
715 if (n > 0) {
716 dma_buf_commit(s, 0);
717 sector_num += n;
718 ide_set_sector(s, sector_num);
719 s->nsector -= n;
720 }
721
722 /* end of transfer ? */
723 if (s->nsector == 0) {
724 s->status = READY_STAT | SEEK_STAT;
725 ide_set_irq(s->bus);
726 eot:
727 bm->status &= ~BM_STATUS_DMAING;
728 bm->status |= BM_STATUS_INT;
729 bm->dma_cb = NULL;
730 bm->unit = -1;
731 bm->aiocb = NULL;
732 return;
733 }
734
735 n = s->nsector;
736 s->io_buffer_size = n * 512;
737 /* launch next transfer */
738 if (dma_buf_prepare(bm, 0) == 0)
739 goto eot;
740 #ifdef DEBUG_AIO
741 printf("aio_write: sector_num=%" PRId64 " n=%d\n", sector_num, n);
742 #endif
743 bm->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num, ide_write_dma_cb, bm);
744 ide_dma_submit_check(s, ide_write_dma_cb, bm);
745 }
746
747 static void ide_sector_write_dma(IDEState *s)
748 {
749 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
750 s->io_buffer_index = 0;
751 s->io_buffer_size = 0;
752 s->is_read = 0;
753 ide_dma_start(s, ide_write_dma_cb);
754 }
755
756 void ide_atapi_cmd_ok(IDEState *s)
757 {
758 s->error = 0;
759 s->status = READY_STAT | SEEK_STAT;
760 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
761 ide_set_irq(s->bus);
762 }
763
764 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
765 {
766 #ifdef DEBUG_IDE_ATAPI
767 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
768 #endif
769 s->error = sense_key << 4;
770 s->status = READY_STAT | ERR_STAT;
771 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
772 s->sense_key = sense_key;
773 s->asc = asc;
774 ide_set_irq(s->bus);
775 }
776
777 static void ide_atapi_cmd_check_status(IDEState *s)
778 {
779 #ifdef DEBUG_IDE_ATAPI
780 printf("atapi_cmd_check_status\n");
781 #endif
782 s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
783 s->status = ERR_STAT;
784 s->nsector = 0;
785 ide_set_irq(s->bus);
786 }
787
788 static void ide_flush_cb(void *opaque, int ret)
789 {
790 IDEState *s = opaque;
791
792 /* XXX: how do we signal I/O errors here? */
793
794 s->status = READY_STAT | SEEK_STAT;
795 ide_set_irq(s->bus);
796 }
797
798 static inline void cpu_to_ube16(uint8_t *buf, int val)
799 {
800 buf[0] = val >> 8;
801 buf[1] = val & 0xff;
802 }
803
804 static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
805 {
806 buf[0] = val >> 24;
807 buf[1] = val >> 16;
808 buf[2] = val >> 8;
809 buf[3] = val & 0xff;
810 }
811
812 static inline int ube16_to_cpu(const uint8_t *buf)
813 {
814 return (buf[0] << 8) | buf[1];
815 }
816
817 static inline int ube32_to_cpu(const uint8_t *buf)
818 {
819 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
820 }
821
822 static void lba_to_msf(uint8_t *buf, int lba)
823 {
824 lba += 150;
825 buf[0] = (lba / 75) / 60;
826 buf[1] = (lba / 75) % 60;
827 buf[2] = lba % 75;
828 }
829
830 static void cd_data_to_raw(uint8_t *buf, int lba)
831 {
832 /* sync bytes */
833 buf[0] = 0x00;
834 memset(buf + 1, 0xff, 10);
835 buf[11] = 0x00;
836 buf += 12;
837 /* MSF */
838 lba_to_msf(buf, lba);
839 buf[3] = 0x01; /* mode 1 data */
840 buf += 4;
841 /* data */
842 buf += 2048;
843 /* XXX: ECC not computed */
844 memset(buf, 0, 288);
845 }
846
847 static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
848 int sector_size)
849 {
850 int ret;
851
852 switch(sector_size) {
853 case 2048:
854 ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
855 break;
856 case 2352:
857 ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
858 if (ret < 0)
859 return ret;
860 cd_data_to_raw(buf, lba);
861 break;
862 default:
863 ret = -EIO;
864 break;
865 }
866 return ret;
867 }
868
869 void ide_atapi_io_error(IDEState *s, int ret)
870 {
871 /* XXX: handle more errors */
872 if (ret == -ENOMEDIUM) {
873 ide_atapi_cmd_error(s, SENSE_NOT_READY,
874 ASC_MEDIUM_NOT_PRESENT);
875 } else {
876 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
877 ASC_LOGICAL_BLOCK_OOR);
878 }
879 }
880
881 /* The whole ATAPI transfer logic is handled in this function */
882 static void ide_atapi_cmd_reply_end(IDEState *s)
883 {
884 int byte_count_limit, size, ret;
885 #ifdef DEBUG_IDE_ATAPI
886 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
887 s->packet_transfer_size,
888 s->elementary_transfer_size,
889 s->io_buffer_index);
890 #endif
891 if (s->packet_transfer_size <= 0) {
892 /* end of transfer */
893 ide_transfer_stop(s);
894 s->status = READY_STAT | SEEK_STAT;
895 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
896 ide_set_irq(s->bus);
897 #ifdef DEBUG_IDE_ATAPI
898 printf("status=0x%x\n", s->status);
899 #endif
900 } else {
901 /* see if a new sector must be read */
902 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
903 ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
904 if (ret < 0) {
905 ide_transfer_stop(s);
906 ide_atapi_io_error(s, ret);
907 return;
908 }
909 s->lba++;
910 s->io_buffer_index = 0;
911 }
912 if (s->elementary_transfer_size > 0) {
913 /* there are some data left to transmit in this elementary
914 transfer */
915 size = s->cd_sector_size - s->io_buffer_index;
916 if (size > s->elementary_transfer_size)
917 size = s->elementary_transfer_size;
918 ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
919 size, ide_atapi_cmd_reply_end);
920 s->packet_transfer_size -= size;
921 s->elementary_transfer_size -= size;
922 s->io_buffer_index += size;
923 } else {
924 /* a new transfer is needed */
925 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
926 byte_count_limit = s->lcyl | (s->hcyl << 8);
927 #ifdef DEBUG_IDE_ATAPI
928 printf("byte_count_limit=%d\n", byte_count_limit);
929 #endif
930 if (byte_count_limit == 0xffff)
931 byte_count_limit--;
932 size = s->packet_transfer_size;
933 if (size > byte_count_limit) {
934 /* byte count limit must be even if this case */
935 if (byte_count_limit & 1)
936 byte_count_limit--;
937 size = byte_count_limit;
938 }
939 s->lcyl = size;
940 s->hcyl = size >> 8;
941 s->elementary_transfer_size = size;
942 /* we cannot transmit more than one sector at a time */
943 if (s->lba != -1) {
944 if (size > (s->cd_sector_size - s->io_buffer_index))
945 size = (s->cd_sector_size - s->io_buffer_index);
946 }
947 ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
948 size, ide_atapi_cmd_reply_end);
949 s->packet_transfer_size -= size;
950 s->elementary_transfer_size -= size;
951 s->io_buffer_index += size;
952 ide_set_irq(s->bus);
953 #ifdef DEBUG_IDE_ATAPI
954 printf("status=0x%x\n", s->status);
955 #endif
956 }
957 }
958 }
959
960 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
961 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
962 {
963 if (size > max_size)
964 size = max_size;
965 s->lba = -1; /* no sector read */
966 s->packet_transfer_size = size;
967 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
968 s->elementary_transfer_size = 0;
969 s->io_buffer_index = 0;
970
971 if (s->atapi_dma) {
972 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
973 ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
974 } else {
975 s->status = READY_STAT | SEEK_STAT;
976 ide_atapi_cmd_reply_end(s);
977 }
978 }
979
980 /* start a CD-CDROM read command */
981 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
982 int sector_size)
983 {
984 s->lba = lba;
985 s->packet_transfer_size = nb_sectors * sector_size;
986 s->elementary_transfer_size = 0;
987 s->io_buffer_index = sector_size;
988 s->cd_sector_size = sector_size;
989
990 s->status = READY_STAT | SEEK_STAT;
991 ide_atapi_cmd_reply_end(s);
992 }
993
994 /* ATAPI DMA support */
995
996 /* XXX: handle read errors */
997 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
998 {
999 BMDMAState *bm = opaque;
1000 IDEState *s = bmdma_active_if(bm);
1001 int data_offset, n;
1002
1003 if (ret < 0) {
1004 ide_atapi_io_error(s, ret);
1005 goto eot;
1006 }
1007
1008 if (s->io_buffer_size > 0) {
1009 /*
1010 * For a cdrom read sector command (s->lba != -1),
1011 * adjust the lba for the next s->io_buffer_size chunk
1012 * and dma the current chunk.
1013 * For a command != read (s->lba == -1), just transfer
1014 * the reply data.
1015 */
1016 if (s->lba != -1) {
1017 if (s->cd_sector_size == 2352) {
1018 n = 1;
1019 cd_data_to_raw(s->io_buffer, s->lba);
1020 } else {
1021 n = s->io_buffer_size >> 11;
1022 }
1023 s->lba += n;
1024 }
1025 s->packet_transfer_size -= s->io_buffer_size;
1026 if (dma_buf_rw(bm, 1) == 0)
1027 goto eot;
1028 }
1029
1030 if (s->packet_transfer_size <= 0) {
1031 s->status = READY_STAT | SEEK_STAT;
1032 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
1033 ide_set_irq(s->bus);
1034 eot:
1035 bm->status &= ~BM_STATUS_DMAING;
1036 bm->status |= BM_STATUS_INT;
1037 bm->dma_cb = NULL;
1038 bm->unit = -1;
1039 bm->aiocb = NULL;
1040 return;
1041 }
1042
1043 s->io_buffer_index = 0;
1044 if (s->cd_sector_size == 2352) {
1045 n = 1;
1046 s->io_buffer_size = s->cd_sector_size;
1047 data_offset = 16;
1048 } else {
1049 n = s->packet_transfer_size >> 11;
1050 if (n > (IDE_DMA_BUF_SECTORS / 4))
1051 n = (IDE_DMA_BUF_SECTORS / 4);
1052 s->io_buffer_size = n * 2048;
1053 data_offset = 0;
1054 }
1055 #ifdef DEBUG_AIO
1056 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
1057 #endif
1058 bm->iov.iov_base = (void *)(s->io_buffer + data_offset);
1059 bm->iov.iov_len = n * 4 * 512;
1060 qemu_iovec_init_external(&bm->qiov, &bm->iov, 1);
1061 bm->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2, &bm->qiov,
1062 n * 4, ide_atapi_cmd_read_dma_cb, bm);
1063 if (!bm->aiocb) {
1064 /* Note: media not present is the most likely case */
1065 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1066 ASC_MEDIUM_NOT_PRESENT);
1067 goto eot;
1068 }
1069 }
1070
1071 /* start a CD-CDROM read command with DMA */
1072 /* XXX: test if DMA is available */
1073 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
1074 int sector_size)
1075 {
1076 s->lba = lba;
1077 s->packet_transfer_size = nb_sectors * sector_size;
1078 s->io_buffer_index = 0;
1079 s->io_buffer_size = 0;
1080 s->cd_sector_size = sector_size;
1081
1082 /* XXX: check if BUSY_STAT should be set */
1083 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
1084 ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
1085 }
1086
1087 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
1088 int sector_size)
1089 {
1090 #ifdef DEBUG_IDE_ATAPI
1091 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
1092 lba, nb_sectors);
1093 #endif
1094 if (s->atapi_dma) {
1095 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
1096 } else {
1097 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
1098 }
1099 }
1100
1101 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
1102 uint16_t profile)
1103 {
1104 uint8_t *buf_profile = buf + 12; /* start of profiles */
1105
1106 buf_profile += ((*index) * 4); /* start of indexed profile */
1107 cpu_to_ube16 (buf_profile, profile);
1108 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
1109
1110 /* each profile adds 4 bytes to the response */
1111 (*index)++;
1112 buf[11] += 4; /* Additional Length */
1113
1114 return 4;
1115 }
1116
1117 static int ide_dvd_read_structure(IDEState *s, int format,
1118 const uint8_t *packet, uint8_t *buf)
1119 {
1120 switch (format) {
1121 case 0x0: /* Physical format information */
1122 {
1123 int layer = packet[6];
1124 uint64_t total_sectors;
1125
1126 if (layer != 0)
1127 return -ASC_INV_FIELD_IN_CMD_PACKET;
1128
1129 bdrv_get_geometry(s->bs, &total_sectors);
1130 total_sectors >>= 2;
1131 if (total_sectors == 0)
1132 return -ASC_MEDIUM_NOT_PRESENT;
1133
1134 buf[4] = 1; /* DVD-ROM, part version 1 */
1135 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
1136 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
1137 buf[7] = 0; /* default densities */
1138
1139 /* FIXME: 0x30000 per spec? */
1140 cpu_to_ube32(buf + 8, 0); /* start sector */
1141 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
1142 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
1143
1144 /* Size of buffer, not including 2 byte size field */
1145 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
1146
1147 /* 2k data + 4 byte header */
1148 return (2048 + 4);
1149 }
1150
1151 case 0x01: /* DVD copyright information */
1152 buf[4] = 0; /* no copyright data */
1153 buf[5] = 0; /* no region restrictions */
1154
1155 /* Size of buffer, not including 2 byte size field */
1156 cpu_to_be16wu((uint16_t *)buf, 4 + 2);
1157
1158 /* 4 byte header + 4 byte data */
1159 return (4 + 4);
1160
1161 case 0x03: /* BCA information - invalid field for no BCA info */
1162 return -ASC_INV_FIELD_IN_CMD_PACKET;
1163
1164 case 0x04: /* DVD disc manufacturing information */
1165 /* Size of buffer, not including 2 byte size field */
1166 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
1167
1168 /* 2k data + 4 byte header */
1169 return (2048 + 4);
1170
1171 case 0xff:
1172 /*
1173 * This lists all the command capabilities above. Add new ones
1174 * in order and update the length and buffer return values.
1175 */
1176
1177 buf[4] = 0x00; /* Physical format */
1178 buf[5] = 0x40; /* Not writable, is readable */
1179 cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
1180
1181 buf[8] = 0x01; /* Copyright info */
1182 buf[9] = 0x40; /* Not writable, is readable */
1183 cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
1184
1185 buf[12] = 0x03; /* BCA info */
1186 buf[13] = 0x40; /* Not writable, is readable */
1187 cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
1188
1189 buf[16] = 0x04; /* Manufacturing info */
1190 buf[17] = 0x40; /* Not writable, is readable */
1191 cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
1192
1193 /* Size of buffer, not including 2 byte size field */
1194 cpu_to_be16wu((uint16_t *)buf, 16 + 2);
1195
1196 /* data written + 4 byte header */
1197 return (16 + 4);
1198
1199 default: /* TODO: formats beyond DVD-ROM requires */
1200 return -ASC_INV_FIELD_IN_CMD_PACKET;
1201 }
1202 }
1203
1204 static void ide_atapi_cmd(IDEState *s)
1205 {
1206 const uint8_t *packet;
1207 uint8_t *buf;
1208 int max_len;
1209
1210 packet = s->io_buffer;
1211 buf = s->io_buffer;
1212 #ifdef DEBUG_IDE_ATAPI
1213 {
1214 int i;
1215 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1216 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1217 printf(" %02x", packet[i]);
1218 }
1219 printf("\n");
1220 }
1221 #endif
1222 /* If there's a UNIT_ATTENTION condition pending, only
1223 REQUEST_SENSE and INQUIRY commands are allowed to complete. */
1224 if (s->sense_key == SENSE_UNIT_ATTENTION &&
1225 s->io_buffer[0] != GPCMD_REQUEST_SENSE &&
1226 s->io_buffer[0] != GPCMD_INQUIRY) {
1227 ide_atapi_cmd_check_status(s);
1228 return;
1229 }
1230 switch(s->io_buffer[0]) {
1231 case GPCMD_TEST_UNIT_READY:
1232 if (bdrv_is_inserted(s->bs) && !s->cdrom_changed) {
1233 ide_atapi_cmd_ok(s);
1234 } else {
1235 s->cdrom_changed = 0;
1236 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1237 ASC_MEDIUM_NOT_PRESENT);
1238 }
1239 break;
1240 case GPCMD_MODE_SENSE_6:
1241 case GPCMD_MODE_SENSE_10:
1242 {
1243 int action, code;
1244 if (packet[0] == GPCMD_MODE_SENSE_10)
1245 max_len = ube16_to_cpu(packet + 7);
1246 else
1247 max_len = packet[4];
1248 action = packet[2] >> 6;
1249 code = packet[2] & 0x3f;
1250 switch(action) {
1251 case 0: /* current values */
1252 switch(code) {
1253 case GPMODE_R_W_ERROR_PAGE: /* error recovery */
1254 cpu_to_ube16(&buf[0], 16 + 6);
1255 buf[2] = 0x70;
1256 buf[3] = 0;
1257 buf[4] = 0;
1258 buf[5] = 0;
1259 buf[6] = 0;
1260 buf[7] = 0;
1261
1262 buf[8] = 0x01;
1263 buf[9] = 0x06;
1264 buf[10] = 0x00;
1265 buf[11] = 0x05;
1266 buf[12] = 0x00;
1267 buf[13] = 0x00;
1268 buf[14] = 0x00;
1269 buf[15] = 0x00;
1270 ide_atapi_cmd_reply(s, 16, max_len);
1271 break;
1272 case GPMODE_CAPABILITIES_PAGE:
1273 cpu_to_ube16(&buf[0], 28 + 6);
1274 buf[2] = 0x70;
1275 buf[3] = 0;
1276 buf[4] = 0;
1277 buf[5] = 0;
1278 buf[6] = 0;
1279 buf[7] = 0;
1280
1281 buf[8] = 0x2a;
1282 buf[9] = 0x12;
1283 buf[10] = 0x00;
1284 buf[11] = 0x00;
1285
1286 /* Claim PLAY_AUDIO capability (0x01) since some Linux
1287 code checks for this to automount media. */
1288 buf[12] = 0x71;
1289 buf[13] = 3 << 5;
1290 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
1291 if (bdrv_is_locked(s->bs))
1292 buf[6] |= 1 << 1;
1293 buf[15] = 0x00;
1294 cpu_to_ube16(&buf[16], 706);
1295 buf[18] = 0;
1296 buf[19] = 2;
1297 cpu_to_ube16(&buf[20], 512);
1298 cpu_to_ube16(&buf[22], 706);
1299 buf[24] = 0;
1300 buf[25] = 0;
1301 buf[26] = 0;
1302 buf[27] = 0;
1303 ide_atapi_cmd_reply(s, 28, max_len);
1304 break;
1305 default:
1306 goto error_cmd;
1307 }
1308 break;
1309 case 1: /* changeable values */
1310 goto error_cmd;
1311 case 2: /* default values */
1312 goto error_cmd;
1313 default:
1314 case 3: /* saved values */
1315 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1316 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1317 break;
1318 }
1319 }
1320 break;
1321 case GPCMD_REQUEST_SENSE:
1322 max_len = packet[4];
1323 memset(buf, 0, 18);
1324 buf[0] = 0x70 | (1 << 7);
1325 buf[2] = s->sense_key;
1326 buf[7] = 10;
1327 buf[12] = s->asc;
1328 if (s->sense_key == SENSE_UNIT_ATTENTION)
1329 s->sense_key = SENSE_NONE;
1330 ide_atapi_cmd_reply(s, 18, max_len);
1331 break;
1332 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
1333 if (bdrv_is_inserted(s->bs)) {
1334 bdrv_set_locked(s->bs, packet[4] & 1);
1335 ide_atapi_cmd_ok(s);
1336 } else {
1337 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1338 ASC_MEDIUM_NOT_PRESENT);
1339 }
1340 break;
1341 case GPCMD_READ_10:
1342 case GPCMD_READ_12:
1343 {
1344 int nb_sectors, lba;
1345
1346 if (packet[0] == GPCMD_READ_10)
1347 nb_sectors = ube16_to_cpu(packet + 7);
1348 else
1349 nb_sectors = ube32_to_cpu(packet + 6);
1350 lba = ube32_to_cpu(packet + 2);
1351 if (nb_sectors == 0) {
1352 ide_atapi_cmd_ok(s);
1353 break;
1354 }
1355 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1356 }
1357 break;
1358 case GPCMD_READ_CD:
1359 {
1360 int nb_sectors, lba, transfer_request;
1361
1362 nb_sectors = (packet[6] << 16) | (packet[7] << 8) | packet[8];
1363 lba = ube32_to_cpu(packet + 2);
1364 if (nb_sectors == 0) {
1365 ide_atapi_cmd_ok(s);
1366 break;
1367 }
1368 transfer_request = packet[9];
1369 switch(transfer_request & 0xf8) {
1370 case 0x00:
1371 /* nothing */
1372 ide_atapi_cmd_ok(s);
1373 break;
1374 case 0x10:
1375 /* normal read */
1376 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1377 break;
1378 case 0xf8:
1379 /* read all data */
1380 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1381 break;
1382 default:
1383 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1384 ASC_INV_FIELD_IN_CMD_PACKET);
1385 break;
1386 }
1387 }
1388 break;
1389 case GPCMD_SEEK:
1390 {
1391 unsigned int lba;
1392 uint64_t total_sectors;
1393
1394 bdrv_get_geometry(s->bs, &total_sectors);
1395 total_sectors >>= 2;
1396 if (total_sectors == 0) {
1397 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1398 ASC_MEDIUM_NOT_PRESENT);
1399 break;
1400 }
1401 lba = ube32_to_cpu(packet + 2);
1402 if (lba >= total_sectors) {
1403 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1404 ASC_LOGICAL_BLOCK_OOR);
1405 break;
1406 }
1407 ide_atapi_cmd_ok(s);
1408 }
1409 break;
1410 case GPCMD_START_STOP_UNIT:
1411 {
1412 int start, eject, err = 0;
1413 start = packet[4] & 1;
1414 eject = (packet[4] >> 1) & 1;
1415
1416 if (eject) {
1417 err = bdrv_eject(s->bs, !start);
1418 }
1419
1420 switch (err) {
1421 case 0:
1422 ide_atapi_cmd_ok(s);
1423 break;
1424 case -EBUSY:
1425 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1426 ASC_MEDIA_REMOVAL_PREVENTED);
1427 break;
1428 default:
1429 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1430 ASC_MEDIUM_NOT_PRESENT);
1431 break;
1432 }
1433 }
1434 break;
1435 case GPCMD_MECHANISM_STATUS:
1436 {
1437 max_len = ube16_to_cpu(packet + 8);
1438 cpu_to_ube16(buf, 0);
1439 /* no current LBA */
1440 buf[2] = 0;
1441 buf[3] = 0;
1442 buf[4] = 0;
1443 buf[5] = 1;
1444 cpu_to_ube16(buf + 6, 0);
1445 ide_atapi_cmd_reply(s, 8, max_len);
1446 }
1447 break;
1448 case GPCMD_READ_TOC_PMA_ATIP:
1449 {
1450 int format, msf, start_track, len;
1451 uint64_t total_sectors;
1452
1453 bdrv_get_geometry(s->bs, &total_sectors);
1454 total_sectors >>= 2;
1455 if (total_sectors == 0) {
1456 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1457 ASC_MEDIUM_NOT_PRESENT);
1458 break;
1459 }
1460 max_len = ube16_to_cpu(packet + 7);
1461 format = packet[9] >> 6;
1462 msf = (packet[1] >> 1) & 1;
1463 start_track = packet[6];
1464 switch(format) {
1465 case 0:
1466 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1467 if (len < 0)
1468 goto error_cmd;
1469 ide_atapi_cmd_reply(s, len, max_len);
1470 break;
1471 case 1:
1472 /* multi session : only a single session defined */
1473 memset(buf, 0, 12);
1474 buf[1] = 0x0a;
1475 buf[2] = 0x01;
1476 buf[3] = 0x01;
1477 ide_atapi_cmd_reply(s, 12, max_len);
1478 break;
1479 case 2:
1480 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1481 if (len < 0)
1482 goto error_cmd;
1483 ide_atapi_cmd_reply(s, len, max_len);
1484 break;
1485 default:
1486 error_cmd:
1487 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1488 ASC_INV_FIELD_IN_CMD_PACKET);
1489 break;
1490 }
1491 }
1492 break;
1493 case GPCMD_READ_CDVD_CAPACITY:
1494 {
1495 uint64_t total_sectors;
1496
1497 bdrv_get_geometry(s->bs, &total_sectors);
1498 total_sectors >>= 2;
1499 if (total_sectors == 0) {
1500 ide_atapi_cmd_error(s, SENSE_NOT_READY,
1501 ASC_MEDIUM_NOT_PRESENT);
1502 break;
1503 }
1504 /* NOTE: it is really the number of sectors minus 1 */
1505 cpu_to_ube32(buf, total_sectors - 1);
1506 cpu_to_ube32(buf + 4, 2048);
1507 ide_atapi_cmd_reply(s, 8, 8);
1508 }
1509 break;
1510 case GPCMD_READ_DVD_STRUCTURE:
1511 {
1512 int media = packet[1];
1513 int format = packet[7];
1514 int ret;
1515
1516 max_len = ube16_to_cpu(packet + 8);
1517
1518 if (format < 0xff) {
1519 if (media_is_cd(s)) {
1520 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1521 ASC_INCOMPATIBLE_FORMAT);
1522 break;
1523 } else if (!media_present(s)) {
1524 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1525 ASC_INV_FIELD_IN_CMD_PACKET);
1526 break;
1527 }
1528 }
1529
1530 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1531 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1532
1533 switch (format) {
1534 case 0x00 ... 0x7f:
1535 case 0xff:
1536 if (media == 0) {
1537 ret = ide_dvd_read_structure(s, format, packet, buf);
1538
1539 if (ret < 0)
1540 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1541 else
1542 ide_atapi_cmd_reply(s, ret, max_len);
1543
1544 break;
1545 }
1546 /* TODO: BD support, fall through for now */
1547
1548 /* Generic disk structures */
1549 case 0x80: /* TODO: AACS volume identifier */
1550 case 0x81: /* TODO: AACS media serial number */
1551 case 0x82: /* TODO: AACS media identifier */
1552 case 0x83: /* TODO: AACS media key block */
1553 case 0x90: /* TODO: List of recognized format layers */
1554 case 0xc0: /* TODO: Write protection status */
1555 default:
1556 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1557 ASC_INV_FIELD_IN_CMD_PACKET);
1558 break;
1559 }
1560 }
1561 break;
1562 case GPCMD_SET_SPEED:
1563 ide_atapi_cmd_ok(s);
1564 break;
1565 case GPCMD_INQUIRY:
1566 max_len = packet[4];
1567 buf[0] = 0x05; /* CD-ROM */
1568 buf[1] = 0x80; /* removable */
1569 buf[2] = 0x00; /* ISO */
1570 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1571 buf[4] = 31; /* additional length */
1572 buf[5] = 0; /* reserved */
1573 buf[6] = 0; /* reserved */
1574 buf[7] = 0; /* reserved */
1575 padstr8(buf + 8, 8, "QEMU");
1576 padstr8(buf + 16, 16, "QEMU DVD-ROM");
1577 padstr8(buf + 32, 4, QEMU_VERSION);
1578 ide_atapi_cmd_reply(s, 36, max_len);
1579 break;
1580 case GPCMD_GET_CONFIGURATION:
1581 {
1582 uint32_t len;
1583 uint8_t index = 0;
1584
1585 /* only feature 0 is supported */
1586 if (packet[2] != 0 || packet[3] != 0) {
1587 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1588 ASC_INV_FIELD_IN_CMD_PACKET);
1589 break;
1590 }
1591
1592 /* XXX: could result in alignment problems in some architectures */
1593 max_len = ube16_to_cpu(packet + 7);
1594
1595 /*
1596 * XXX: avoid overflow for io_buffer if max_len is bigger than
1597 * the size of that buffer (dimensioned to max number of
1598 * sectors to transfer at once)
1599 *
1600 * Only a problem if the feature/profiles grow.
1601 */
1602 if (max_len > 512) /* XXX: assume 1 sector */
1603 max_len = 512;
1604
1605 memset(buf, 0, max_len);
1606 /*
1607 * the number of sectors from the media tells us which profile
1608 * to use as current. 0 means there is no media
1609 */
1610 if (media_is_dvd(s))
1611 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
1612 else if (media_is_cd(s))
1613 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
1614
1615 buf[10] = 0x02 | 0x01; /* persistent and current */
1616 len = 12; /* headers: 8 + 4 */
1617 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
1618 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
1619 cpu_to_ube32(buf, len - 4); /* data length */
1620
1621 ide_atapi_cmd_reply(s, len, max_len);
1622 break;
1623 }
1624 default:
1625 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1626 ASC_ILLEGAL_OPCODE);
1627 break;
1628 }
1629 }
1630
1631 static void ide_cfata_metadata_inquiry(IDEState *s)
1632 {
1633 uint16_t *p;
1634 uint32_t spd;
1635
1636 p = (uint16_t *) s->io_buffer;
1637 memset(p, 0, 0x200);
1638 spd = ((s->mdata_size - 1) >> 9) + 1;
1639
1640 put_le16(p + 0, 0x0001); /* Data format revision */
1641 put_le16(p + 1, 0x0000); /* Media property: silicon */
1642 put_le16(p + 2, s->media_changed); /* Media status */
1643 put_le16(p + 3, s->mdata_size & 0xffff); /* Capacity in bytes (low) */
1644 put_le16(p + 4, s->mdata_size >> 16); /* Capacity in bytes (high) */
1645 put_le16(p + 5, spd & 0xffff); /* Sectors per device (low) */
1646 put_le16(p + 6, spd >> 16); /* Sectors per device (high) */
1647 }
1648
1649 static void ide_cfata_metadata_read(IDEState *s)
1650 {
1651 uint16_t *p;
1652
1653 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1654 s->status = ERR_STAT;
1655 s->error = ABRT_ERR;
1656 return;
1657 }
1658
1659 p = (uint16_t *) s->io_buffer;
1660 memset(p, 0, 0x200);
1661
1662 put_le16(p + 0, s->media_changed); /* Media status */
1663 memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1664 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1665 s->nsector << 9), 0x200 - 2));
1666 }
1667
1668 static void ide_cfata_metadata_write(IDEState *s)
1669 {
1670 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1671 s->status = ERR_STAT;
1672 s->error = ABRT_ERR;
1673 return;
1674 }
1675
1676 s->media_changed = 0;
1677
1678 memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1679 s->io_buffer + 2,
1680 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1681 s->nsector << 9), 0x200 - 2));
1682 }
1683
1684 /* called when the inserted state of the media has changed */
1685 static void cdrom_change_cb(void *opaque)
1686 {
1687 IDEState *s = opaque;
1688 uint64_t nb_sectors;
1689
1690 bdrv_get_geometry(s->bs, &nb_sectors);
1691 s->nb_sectors = nb_sectors;
1692
1693 s->sense_key = SENSE_UNIT_ATTENTION;
1694 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1695 s->cdrom_changed = 1;
1696 ide_set_irq(s->bus);
1697 }
1698
1699 static void ide_cmd_lba48_transform(IDEState *s, int lba48)
1700 {
1701 s->lba48 = lba48;
1702
1703 /* handle the 'magic' 0 nsector count conversion here. to avoid
1704 * fiddling with the rest of the read logic, we just store the
1705 * full sector count in ->nsector and ignore ->hob_nsector from now
1706 */
1707 if (!s->lba48) {
1708 if (!s->nsector)
1709 s->nsector = 256;
1710 } else {
1711 if (!s->nsector && !s->hob_nsector)
1712 s->nsector = 65536;
1713 else {
1714 int lo = s->nsector;
1715 int hi = s->hob_nsector;
1716
1717 s->nsector = (hi << 8) | lo;
1718 }
1719 }
1720 }
1721
1722 static void ide_clear_hob(IDEBus *bus)
1723 {
1724 /* any write clears HOB high bit of device control register */
1725 bus->ifs[0].select &= ~(1 << 7);
1726 bus->ifs[1].select &= ~(1 << 7);
1727 }
1728
1729 void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1730 {
1731 IDEBus *bus = opaque;
1732 IDEState *s;
1733 int n;
1734 int lba48 = 0;
1735
1736 #ifdef DEBUG_IDE
1737 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1738 #endif
1739
1740 addr &= 7;
1741
1742 /* ignore writes to command block while busy with previous command */
1743 if (addr != 7 && (idebus_active_if(bus)->status & (BUSY_STAT|DRQ_STAT)))
1744 return;
1745
1746 switch(addr) {
1747 case 0:
1748 break;
1749 case 1:
1750 ide_clear_hob(bus);
1751 /* NOTE: data is written to the two drives */
1752 bus->ifs[0].hob_feature = bus->ifs[0].feature;
1753 bus->ifs[1].hob_feature = bus->ifs[1].feature;
1754 bus->ifs[0].feature = val;
1755 bus->ifs[1].feature = val;
1756 break;
1757 case 2:
1758 ide_clear_hob(bus);
1759 bus->ifs[0].hob_nsector = bus->ifs[0].nsector;
1760 bus->ifs[1].hob_nsector = bus->ifs[1].nsector;
1761 bus->ifs[0].nsector = val;
1762 bus->ifs[1].nsector = val;
1763 break;
1764 case 3:
1765 ide_clear_hob(bus);
1766 bus->ifs[0].hob_sector = bus->ifs[0].sector;
1767 bus->ifs[1].hob_sector = bus->ifs[1].sector;
1768 bus->ifs[0].sector = val;
1769 bus->ifs[1].sector = val;
1770 break;
1771 case 4:
1772 ide_clear_hob(bus);
1773 bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl;
1774 bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl;
1775 bus->ifs[0].lcyl = val;
1776 bus->ifs[1].lcyl = val;
1777 break;
1778 case 5:
1779 ide_clear_hob(bus);
1780 bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl;
1781 bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl;
1782 bus->ifs[0].hcyl = val;
1783 bus->ifs[1].hcyl = val;
1784 break;
1785 case 6:
1786 /* FIXME: HOB readback uses bit 7 */
1787 bus->ifs[0].select = (val & ~0x10) | 0xa0;
1788 bus->ifs[1].select = (val | 0x10) | 0xa0;
1789 /* select drive */
1790 bus->unit = (val >> 4) & 1;
1791 break;
1792 default:
1793 case 7:
1794 /* command */
1795 #if defined(DEBUG_IDE)
1796 printf("ide: CMD=%02x\n", val);
1797 #endif
1798 s = idebus_active_if(bus);
1799 /* ignore commands to non existant slave */
1800 if (s != bus->ifs && !s->bs)
1801 break;
1802
1803 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1804 if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
1805 break;
1806
1807 switch(val) {
1808 case WIN_IDENTIFY:
1809 if (s->bs && !s->is_cdrom) {
1810 if (!s->is_cf)
1811 ide_identify(s);
1812 else
1813 ide_cfata_identify(s);
1814 s->status = READY_STAT | SEEK_STAT;
1815 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1816 } else {
1817 if (s->is_cdrom) {
1818 ide_set_signature(s);
1819 }
1820 ide_abort_command(s);
1821 }
1822 ide_set_irq(s->bus);
1823 break;
1824 case WIN_SPECIFY:
1825 case WIN_RECAL:
1826 s->error = 0;
1827 s->status = READY_STAT | SEEK_STAT;
1828 ide_set_irq(s->bus);
1829 break;
1830 case WIN_SETMULT:
1831 if (s->is_cf && s->nsector == 0) {
1832 /* Disable Read and Write Multiple */
1833 s->mult_sectors = 0;
1834 s->status = READY_STAT | SEEK_STAT;
1835 } else if ((s->nsector & 0xff) != 0 &&
1836 ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1837 (s->nsector & (s->nsector - 1)) != 0)) {
1838 ide_abort_command(s);
1839 } else {
1840 s->mult_sectors = s->nsector & 0xff;
1841 s->status = READY_STAT | SEEK_STAT;
1842 }
1843 ide_set_irq(s->bus);
1844 break;
1845 case WIN_VERIFY_EXT:
1846 lba48 = 1;
1847 case WIN_VERIFY:
1848 case WIN_VERIFY_ONCE:
1849 /* do sector number check ? */
1850 ide_cmd_lba48_transform(s, lba48);
1851 s->status = READY_STAT | SEEK_STAT;
1852 ide_set_irq(s->bus);
1853 break;
1854 case WIN_READ_EXT:
1855 lba48 = 1;
1856 case WIN_READ:
1857 case WIN_READ_ONCE:
1858 if (!s->bs)
1859 goto abort_cmd;
1860 ide_cmd_lba48_transform(s, lba48);
1861 s->req_nb_sectors = 1;
1862 ide_sector_read(s);
1863 break;
1864 case WIN_WRITE_EXT:
1865 lba48 = 1;
1866 case WIN_WRITE:
1867 case WIN_WRITE_ONCE:
1868 case CFA_WRITE_SECT_WO_ERASE:
1869 case WIN_WRITE_VERIFY:
1870 ide_cmd_lba48_transform(s, lba48);
1871 s->error = 0;
1872 s->status = SEEK_STAT | READY_STAT;
1873 s->req_nb_sectors = 1;
1874 ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1875 s->media_changed = 1;
1876 break;
1877 case WIN_MULTREAD_EXT:
1878 lba48 = 1;
1879 case WIN_MULTREAD:
1880 if (!s->mult_sectors)
1881 goto abort_cmd;
1882 ide_cmd_lba48_transform(s, lba48);
1883 s->req_nb_sectors = s->mult_sectors;
1884 ide_sector_read(s);
1885 break;
1886 case WIN_MULTWRITE_EXT:
1887 lba48 = 1;
1888 case WIN_MULTWRITE:
1889 case CFA_WRITE_MULTI_WO_ERASE:
1890 if (!s->mult_sectors)
1891 goto abort_cmd;
1892 ide_cmd_lba48_transform(s, lba48);
1893 s->error = 0;
1894 s->status = SEEK_STAT | READY_STAT;
1895 s->req_nb_sectors = s->mult_sectors;
1896 n = s->nsector;
1897 if (n > s->req_nb_sectors)
1898 n = s->req_nb_sectors;
1899 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1900 s->media_changed = 1;
1901 break;
1902 case WIN_READDMA_EXT:
1903 lba48 = 1;
1904 case WIN_READDMA:
1905 case WIN_READDMA_ONCE:
1906 if (!s->bs)
1907 goto abort_cmd;
1908 ide_cmd_lba48_transform(s, lba48);
1909 ide_sector_read_dma(s);
1910 break;
1911 case WIN_WRITEDMA_EXT:
1912 lba48 = 1;
1913 case WIN_WRITEDMA:
1914 case WIN_WRITEDMA_ONCE:
1915 if (!s->bs)
1916 goto abort_cmd;
1917 ide_cmd_lba48_transform(s, lba48);
1918 ide_sector_write_dma(s);
1919 s->media_changed = 1;
1920 break;
1921 case WIN_READ_NATIVE_MAX_EXT:
1922 lba48 = 1;
1923 case WIN_READ_NATIVE_MAX:
1924 ide_cmd_lba48_transform(s, lba48);
1925 ide_set_sector(s, s->nb_sectors - 1);
1926 s->status = READY_STAT | SEEK_STAT;
1927 ide_set_irq(s->bus);
1928 break;
1929 case WIN_CHECKPOWERMODE1:
1930 case WIN_CHECKPOWERMODE2:
1931 s->nsector = 0xff; /* device active or idle */
1932 s->status = READY_STAT | SEEK_STAT;
1933 ide_set_irq(s->bus);
1934 break;
1935 case WIN_SETFEATURES:
1936 if (!s->bs)
1937 goto abort_cmd;
1938 /* XXX: valid for CDROM ? */
1939 switch(s->feature) {
1940 case 0xcc: /* reverting to power-on defaults enable */
1941 case 0x66: /* reverting to power-on defaults disable */
1942 case 0x02: /* write cache enable */
1943 case 0x82: /* write cache disable */
1944 case 0xaa: /* read look-ahead enable */
1945 case 0x55: /* read look-ahead disable */
1946 case 0x05: /* set advanced power management mode */
1947 case 0x85: /* disable advanced power management mode */
1948 case 0x69: /* NOP */
1949 case 0x67: /* NOP */
1950 case 0x96: /* NOP */
1951 case 0x9a: /* NOP */
1952 case 0x42: /* enable Automatic Acoustic Mode */
1953 case 0xc2: /* disable Automatic Acoustic Mode */
1954 s->status = READY_STAT | SEEK_STAT;
1955 ide_set_irq(s->bus);
1956 break;
1957 case 0x03: { /* set transfer mode */
1958 uint8_t val = s->nsector & 0x07;
1959 uint16_t *identify_data = (uint16_t *)s->identify_data;
1960
1961 switch (s->nsector >> 3) {
1962 case 0x00: /* pio default */
1963 case 0x01: /* pio mode */
1964 put_le16(identify_data + 62,0x07);
1965 put_le16(identify_data + 63,0x07);
1966 put_le16(identify_data + 88,0x3f);
1967 break;
1968 case 0x02: /* sigle word dma mode*/
1969 put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
1970 put_le16(identify_data + 63,0x07);
1971 put_le16(identify_data + 88,0x3f);
1972 break;
1973 case 0x04: /* mdma mode */
1974 put_le16(identify_data + 62,0x07);
1975 put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
1976 put_le16(identify_data + 88,0x3f);
1977 break;
1978 case 0x08: /* udma mode */
1979 put_le16(identify_data + 62,0x07);
1980 put_le16(identify_data + 63,0x07);
1981 put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
1982 break;
1983 default:
1984 goto abort_cmd;
1985 }
1986 s->status = READY_STAT | SEEK_STAT;
1987 ide_set_irq(s->bus);
1988 break;
1989 }
1990 default:
1991 goto abort_cmd;
1992 }
1993 break;
1994 case WIN_FLUSH_CACHE:
1995 case WIN_FLUSH_CACHE_EXT:
1996 if (s->bs)
1997 bdrv_aio_flush(s->bs, ide_flush_cb, s);
1998 else
1999 ide_flush_cb(s, 0);
2000 break;
2001 case WIN_STANDBY:
2002 case WIN_STANDBY2:
2003 case WIN_STANDBYNOW1:
2004 case WIN_STANDBYNOW2:
2005 case WIN_IDLEIMMEDIATE:
2006 case CFA_IDLEIMMEDIATE:
2007 case WIN_SETIDLE1:
2008 case WIN_SETIDLE2:
2009 case WIN_SLEEPNOW1:
2010 case WIN_SLEEPNOW2:
2011 s->status = READY_STAT;
2012 ide_set_irq(s->bus);
2013 break;
2014 case WIN_SEEK:
2015 if(s->is_cdrom)
2016 goto abort_cmd;
2017 /* XXX: Check that seek is within bounds */
2018 s->status = READY_STAT | SEEK_STAT;
2019 ide_set_irq(s->bus);
2020 break;
2021 /* ATAPI commands */
2022 case WIN_PIDENTIFY:
2023 if (s->is_cdrom) {
2024 ide_atapi_identify(s);
2025 s->status = READY_STAT | SEEK_STAT;
2026 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
2027 } else {
2028 ide_abort_command(s);
2029 }
2030 ide_set_irq(s->bus);
2031 break;
2032 case WIN_DIAGNOSE:
2033 ide_set_signature(s);
2034 if (s->is_cdrom)
2035 s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
2036 * devices to return a clear status register
2037 * with READY_STAT *not* set. */
2038 else
2039 s->status = READY_STAT | SEEK_STAT;
2040 s->error = 0x01; /* Device 0 passed, Device 1 passed or not
2041 * present.
2042 */
2043 ide_set_irq(s->bus);
2044 break;
2045 case WIN_SRST:
2046 if (!s->is_cdrom)
2047 goto abort_cmd;
2048 ide_set_signature(s);
2049 s->status = 0x00; /* NOTE: READY is _not_ set */
2050 s->error = 0x01;
2051 break;
2052 case WIN_PACKETCMD:
2053 if (!s->is_cdrom)
2054 goto abort_cmd;
2055 /* overlapping commands not supported */
2056 if (s->feature & 0x02)
2057 goto abort_cmd;
2058 s->status = READY_STAT | SEEK_STAT;
2059 s->atapi_dma = s->feature & 1;
2060 s->nsector = 1;
2061 ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
2062 ide_atapi_cmd);
2063 break;
2064 /* CF-ATA commands */
2065 case CFA_REQ_EXT_ERROR_CODE:
2066 if (!s->is_cf)
2067 goto abort_cmd;
2068 s->error = 0x09; /* miscellaneous error */
2069 s->status = READY_STAT | SEEK_STAT;
2070 ide_set_irq(s->bus);
2071 break;
2072 case CFA_ERASE_SECTORS:
2073 case CFA_WEAR_LEVEL:
2074 if (!s->is_cf)
2075 goto abort_cmd;
2076 if (val == CFA_WEAR_LEVEL)
2077 s->nsector = 0;
2078 if (val == CFA_ERASE_SECTORS)
2079 s->media_changed = 1;
2080 s->error = 0x00;
2081 s->status = READY_STAT | SEEK_STAT;
2082 ide_set_irq(s->bus);
2083 break;
2084 case CFA_TRANSLATE_SECTOR:
2085 if (!s->is_cf)
2086 goto abort_cmd;
2087 s->error = 0x00;
2088 s->status = READY_STAT | SEEK_STAT;
2089 memset(s->io_buffer, 0, 0x200);
2090 s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
2091 s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
2092 s->io_buffer[0x02] = s->select; /* Head */
2093 s->io_buffer[0x03] = s->sector; /* Sector */
2094 s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
2095 s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
2096 s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
2097 s->io_buffer[0x13] = 0x00; /* Erase flag */
2098 s->io_buffer[0x18] = 0x00; /* Hot count */
2099 s->io_buffer[0x19] = 0x00; /* Hot count */
2100 s->io_buffer[0x1a] = 0x01; /* Hot count */
2101 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2102 ide_set_irq(s->bus);
2103 break;
2104 case CFA_ACCESS_METADATA_STORAGE:
2105 if (!s->is_cf)
2106 goto abort_cmd;
2107 switch (s->feature) {
2108 case 0x02: /* Inquiry Metadata Storage */
2109 ide_cfata_metadata_inquiry(s);
2110 break;
2111 case 0x03: /* Read Metadata Storage */
2112 ide_cfata_metadata_read(s);
2113 break;
2114 case 0x04: /* Write Metadata Storage */
2115 ide_cfata_metadata_write(s);
2116 break;
2117 default:
2118 goto abort_cmd;
2119 }
2120 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2121 s->status = 0x00; /* NOTE: READY is _not_ set */
2122 ide_set_irq(s->bus);
2123 break;
2124 case IBM_SENSE_CONDITION:
2125 if (!s->is_cf)
2126 goto abort_cmd;
2127 switch (s->feature) {
2128 case 0x01: /* sense temperature in device */
2129 s->nsector = 0x50; /* +20 C */
2130 break;
2131 default:
2132 goto abort_cmd;
2133 }
2134 s->status = READY_STAT | SEEK_STAT;
2135 ide_set_irq(s->bus);
2136 break;
2137
2138 case WIN_SMART:
2139 if (s->is_cdrom)
2140 goto abort_cmd;
2141 if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
2142 goto abort_cmd;
2143 if (!s->smart_enabled && s->feature != SMART_ENABLE)
2144 goto abort_cmd;
2145 switch (s->feature) {
2146 case SMART_DISABLE:
2147 s->smart_enabled = 0;
2148 s->status = READY_STAT | SEEK_STAT;
2149 ide_set_irq(s->bus);
2150 break;
2151 case SMART_ENABLE:
2152 s->smart_enabled = 1;
2153 s->status = READY_STAT | SEEK_STAT;
2154 ide_set_irq(s->bus);
2155 break;
2156 case SMART_ATTR_AUTOSAVE:
2157 switch (s->sector) {
2158 case 0x00:
2159 s->smart_autosave = 0;
2160 break;
2161 case 0xf1:
2162 s->smart_autosave = 1;
2163 break;
2164 default:
2165 goto abort_cmd;
2166 }
2167 s->status = READY_STAT | SEEK_STAT;
2168 ide_set_irq(s->bus);
2169 break;
2170 case SMART_STATUS:
2171 if (!s->smart_errors) {
2172 s->hcyl = 0xc2;
2173 s->lcyl = 0x4f;
2174 } else {
2175 s->hcyl = 0x2c;
2176 s->lcyl = 0xf4;
2177 }
2178 s->status = READY_STAT | SEEK_STAT;
2179 ide_set_irq(s->bus);
2180 break;
2181 case SMART_READ_THRESH:
2182 memset(s->io_buffer, 0, 0x200);
2183 s->io_buffer[0] = 0x01; /* smart struct version */
2184 for (n=0; n<30; n++) {
2185 if (smart_attributes[n][0] == 0)
2186 break;
2187 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2188 s->io_buffer[2+1+(n*12)] = smart_attributes[n][4];
2189 }
2190 for (n=0; n<511; n++) /* checksum */
2191 s->io_buffer[511] += s->io_buffer[n];
2192 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2193 s->status = READY_STAT | SEEK_STAT;
2194 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2195 ide_set_irq(s->bus);
2196 break;
2197 case SMART_READ_DATA:
2198 memset(s->io_buffer, 0, 0x200);
2199 s->io_buffer[0] = 0x01; /* smart struct version */
2200 for (n=0; n<30; n++) {
2201 if (smart_attributes[n][0] == 0)
2202 break;
2203 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2204 s->io_buffer[2+1+(n*12)] = smart_attributes[n][1];
2205 s->io_buffer[2+3+(n*12)] = smart_attributes[n][2];
2206 s->io_buffer[2+4+(n*12)] = smart_attributes[n][3];
2207 }
2208 s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
2209 if (s->smart_selftest_count == 0) {
2210 s->io_buffer[363] = 0;
2211 } else {
2212 s->io_buffer[363] =
2213 s->smart_selftest_data[3 +
2214 (s->smart_selftest_count - 1) *
2215 24];
2216 }
2217 s->io_buffer[364] = 0x20;
2218 s->io_buffer[365] = 0x01;
2219 /* offline data collection capacity: execute + self-test*/
2220 s->io_buffer[367] = (1<<4 | 1<<3 | 1);
2221 s->io_buffer[368] = 0x03; /* smart capability (1) */
2222 s->io_buffer[369] = 0x00; /* smart capability (2) */
2223 s->io_buffer[370] = 0x01; /* error logging supported */
2224 s->io_buffer[372] = 0x02; /* minutes for poll short test */
2225 s->io_buffer[373] = 0x36; /* minutes for poll ext test */
2226 s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
2227
2228 for (n=0; n<511; n++)
2229 s->io_buffer[511] += s->io_buffer[n];
2230 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2231 s->status = READY_STAT | SEEK_STAT;
2232 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2233 ide_set_irq(s->bus);
2234 break;
2235 case SMART_READ_LOG:
2236 switch (s->sector) {
2237 case 0x01: /* summary smart error log */
2238 memset(s->io_buffer, 0, 0x200);
2239 s->io_buffer[0] = 0x01;
2240 s->io_buffer[1] = 0x00; /* no error entries */
2241 s->io_buffer[452] = s->smart_errors & 0xff;
2242 s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
2243
2244 for (n=0; n<511; n++)
2245 s->io_buffer[511] += s->io_buffer[n];
2246 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2247 break;
2248 case 0x06: /* smart self test log */
2249 memset(s->io_buffer, 0, 0x200);
2250 s->io_buffer[0] = 0x01;
2251 if (s->smart_selftest_count == 0) {
2252 s->io_buffer[508] = 0;
2253 } else {
2254 s->io_buffer[508] = s->smart_selftest_count;
2255 for (n=2; n<506; n++)
2256 s->io_buffer[n] = s->smart_selftest_data[n];
2257 }
2258 for (n=0; n<511; n++)
2259 s->io_buffer[511] += s->io_buffer[n];
2260 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2261 break;
2262 default:
2263 goto abort_cmd;
2264 }
2265 s->status = READY_STAT | SEEK_STAT;
2266 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2267 ide_set_irq(s->bus);
2268 break;
2269 case SMART_EXECUTE_OFFLINE:
2270 switch (s->sector) {
2271 case 0: /* off-line routine */
2272 case 1: /* short self test */
2273 case 2: /* extended self test */
2274 s->smart_selftest_count++;
2275 if(s->smart_selftest_count > 21)
2276 s->smart_selftest_count = 0;
2277 n = 2 + (s->smart_selftest_count - 1) * 24;
2278 s->smart_selftest_data[n] = s->sector;
2279 s->smart_selftest_data[n+1] = 0x00; /* OK and finished */
2280 s->smart_selftest_data[n+2] = 0x34; /* hour count lsb */
2281 s->smart_selftest_data[n+3] = 0x12; /* hour count msb */
2282 s->status = READY_STAT | SEEK_STAT;
2283 ide_set_irq(s->bus);
2284 break;
2285 default:
2286 goto abort_cmd;
2287 }
2288 break;
2289 default:
2290 goto abort_cmd;
2291 }
2292 break;
2293 default:
2294 abort_cmd:
2295 ide_abort_command(s);
2296 ide_set_irq(s->bus);
2297 break;
2298 }
2299 }
2300 }
2301
2302 uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
2303 {
2304 IDEBus *bus = opaque;
2305 IDEState *s = idebus_active_if(bus);
2306 uint32_t addr;
2307 int ret, hob;
2308
2309 addr = addr1 & 7;
2310 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2311 //hob = s->select & (1 << 7);
2312 hob = 0;
2313 switch(addr) {
2314 case 0:
2315 ret = 0xff;
2316 break;
2317 case 1:
2318 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2319 (s != bus->ifs && !s->bs))
2320 ret = 0;
2321 else if (!hob)
2322 ret = s->error;
2323 else
2324 ret = s->hob_feature;
2325 break;
2326 case 2:
2327 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2328 ret = 0;
2329 else if (!hob)
2330 ret = s->nsector & 0xff;
2331 else
2332 ret = s->hob_nsector;
2333 break;
2334 case 3:
2335 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2336 ret = 0;
2337 else if (!hob)
2338 ret = s->sector;
2339 else
2340 ret = s->hob_sector;
2341 break;
2342 case 4:
2343 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2344 ret = 0;
2345 else if (!hob)
2346 ret = s->lcyl;
2347 else
2348 ret = s->hob_lcyl;
2349 break;
2350 case 5:
2351 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2352 ret = 0;
2353 else if (!hob)
2354 ret = s->hcyl;
2355 else
2356 ret = s->hob_hcyl;
2357 break;
2358 case 6:
2359 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2360 ret = 0;
2361 else
2362 ret = s->select;
2363 break;
2364 default:
2365 case 7:
2366 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2367 (s != bus->ifs && !s->bs))
2368 ret = 0;
2369 else
2370 ret = s->status;
2371 qemu_irq_lower(bus->irq);
2372 break;
2373 }
2374 #ifdef DEBUG_IDE
2375 printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
2376 #endif
2377 return ret;
2378 }
2379
2380 uint32_t ide_status_read(void *opaque, uint32_t addr)
2381 {
2382 IDEBus *bus = opaque;
2383 IDEState *s = idebus_active_if(bus);
2384 int ret;
2385
2386 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2387 (s != bus->ifs && !s->bs))
2388 ret = 0;
2389 else
2390 ret = s->status;
2391 #ifdef DEBUG_IDE
2392 printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
2393 #endif
2394 return ret;
2395 }
2396
2397 void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
2398 {
2399 IDEBus *bus = opaque;
2400 IDEState *s;
2401 int i;
2402
2403 #ifdef DEBUG_IDE
2404 printf("ide: write control addr=0x%x val=%02x\n", addr, val);
2405 #endif
2406 /* common for both drives */
2407 if (!(bus->cmd & IDE_CMD_RESET) &&
2408 (val & IDE_CMD_RESET)) {
2409 /* reset low to high */
2410 for(i = 0;i < 2; i++) {
2411 s = &bus->ifs[i];
2412 s->status = BUSY_STAT | SEEK_STAT;
2413 s->error = 0x01;
2414 }
2415 } else if ((bus->cmd & IDE_CMD_RESET) &&
2416 !(val & IDE_CMD_RESET)) {
2417 /* high to low */
2418 for(i = 0;i < 2; i++) {
2419 s = &bus->ifs[i];
2420 if (s->is_cdrom)
2421 s->status = 0x00; /* NOTE: READY is _not_ set */
2422 else
2423 s->status = READY_STAT | SEEK_STAT;
2424 ide_set_signature(s);
2425 }
2426 }
2427
2428 bus->cmd = val;
2429 }
2430
2431 void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
2432 {
2433 IDEBus *bus = opaque;
2434 IDEState *s = idebus_active_if(bus);
2435 uint8_t *p;
2436
2437 /* PIO data access allowed only when DRQ bit is set */
2438 if (!(s->status & DRQ_STAT))
2439 return;
2440
2441 p = s->data_ptr;
2442 *(uint16_t *)p = le16_to_cpu(val);
2443 p += 2;
2444 s->data_ptr = p;
2445 if (p >= s->data_end)
2446 s->end_transfer_func(s);
2447 }
2448
2449 uint32_t ide_data_readw(void *opaque, uint32_t addr)
2450 {
2451 IDEBus *bus = opaque;
2452 IDEState *s = idebus_active_if(bus);
2453 uint8_t *p;
2454 int ret;
2455
2456 /* PIO data access allowed only when DRQ bit is set */
2457 if (!(s->status & DRQ_STAT))
2458 return 0;
2459
2460 p = s->data_ptr;
2461 ret = cpu_to_le16(*(uint16_t *)p);
2462 p += 2;
2463 s->data_ptr = p;
2464 if (p >= s->data_end)
2465 s->end_transfer_func(s);
2466 return ret;
2467 }
2468
2469 void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
2470 {
2471 IDEBus *bus = opaque;
2472 IDEState *s = idebus_active_if(bus);
2473 uint8_t *p;
2474
2475 /* PIO data access allowed only when DRQ bit is set */
2476 if (!(s->status & DRQ_STAT))
2477 return;
2478
2479 p = s->data_ptr;
2480 *(uint32_t *)p = le32_to_cpu(val);
2481 p += 4;
2482 s->data_ptr = p;
2483 if (p >= s->data_end)
2484 s->end_transfer_func(s);
2485 }
2486
2487 uint32_t ide_data_readl(void *opaque, uint32_t addr)
2488 {
2489 IDEBus *bus = opaque;
2490 IDEState *s = idebus_active_if(bus);
2491 uint8_t *p;
2492 int ret;
2493
2494 /* PIO data access allowed only when DRQ bit is set */
2495 if (!(s->status & DRQ_STAT))
2496 return 0;
2497
2498 p = s->data_ptr;
2499 ret = cpu_to_le32(*(uint32_t *)p);
2500 p += 4;
2501 s->data_ptr = p;
2502 if (p >= s->data_end)
2503 s->end_transfer_func(s);
2504 return ret;
2505 }
2506
2507 static void ide_dummy_transfer_stop(IDEState *s)
2508 {
2509 s->data_ptr = s->io_buffer;
2510 s->data_end = s->io_buffer;
2511 s->io_buffer[0] = 0xff;
2512 s->io_buffer[1] = 0xff;
2513 s->io_buffer[2] = 0xff;
2514 s->io_buffer[3] = 0xff;
2515 }
2516
2517 static void ide_reset(IDEState *s)
2518 {
2519 #ifdef DEBUG_IDE
2520 printf("ide: reset\n");
2521 #endif
2522 if (s->is_cf)
2523 s->mult_sectors = 0;
2524 else
2525 s->mult_sectors = MAX_MULT_SECTORS;
2526 /* ide regs */
2527 s->feature = 0;
2528 s->error = 0;
2529 s->nsector = 0;
2530 s->sector = 0;
2531 s->lcyl = 0;
2532 s->hcyl = 0;
2533
2534 /* lba48 */
2535 s->hob_feature = 0;
2536 s->hob_sector = 0;
2537 s->hob_nsector = 0;
2538 s->hob_lcyl = 0;
2539 s->hob_hcyl = 0;
2540
2541 s->select = 0xa0;
2542 s->status = READY_STAT | SEEK_STAT;
2543
2544 s->lba48 = 0;
2545
2546 /* ATAPI specific */
2547 s->sense_key = 0;
2548 s->asc = 0;
2549 s->cdrom_changed = 0;
2550 s->packet_transfer_size = 0;
2551 s->elementary_transfer_size = 0;
2552 s->io_buffer_index = 0;
2553 s->cd_sector_size = 0;
2554 s->atapi_dma = 0;
2555 /* ATA DMA state */
2556 s->io_buffer_size = 0;
2557 s->req_nb_sectors = 0;
2558
2559 ide_set_signature(s);
2560 /* init the transfer handler so that 0xffff is returned on data
2561 accesses */
2562 s->end_transfer_func = ide_dummy_transfer_stop;
2563 ide_dummy_transfer_stop(s);
2564 s->media_changed = 0;
2565 }
2566
2567 void ide_bus_reset(IDEBus *bus)
2568 {
2569 bus->unit = 0;
2570 bus->cmd = 0;
2571 ide_reset(&bus->ifs[0]);
2572 ide_reset(&bus->ifs[1]);
2573 ide_clear_hob(bus);
2574 }
2575
2576 void ide_init_drive(IDEState *s, DriveInfo *dinfo)
2577 {
2578 int cylinders, heads, secs;
2579 uint64_t nb_sectors;
2580
2581 if (dinfo && dinfo->bdrv) {
2582 s->bs = dinfo->bdrv;
2583 bdrv_get_geometry(s->bs, &nb_sectors);
2584 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
2585 s->cylinders = cylinders;
2586 s->heads = heads;
2587 s->sectors = secs;
2588 s->nb_sectors = nb_sectors;
2589 /* The SMART values should be preserved across power cycles
2590 but they aren't. */
2591 s->smart_enabled = 1;
2592 s->smart_autosave = 1;
2593 s->smart_errors = 0;
2594 s->smart_selftest_count = 0;
2595 if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
2596 s->is_cdrom = 1;
2597 bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
2598 }
2599 strncpy(s->drive_serial_str, drive_get_serial(s->bs),
2600 sizeof(s->drive_serial_str));
2601 }
2602 if (strlen(s->drive_serial_str) == 0)
2603 snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
2604 "QM%05d", s->drive_serial);
2605 ide_reset(s);
2606 }
2607
2608 void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
2609 qemu_irq irq)
2610 {
2611 IDEState *s;
2612 static int drive_serial = 1;
2613 int i;
2614
2615 for(i = 0; i < 2; i++) {
2616 s = bus->ifs + i;
2617 s->bus = bus;
2618 s->unit = i;
2619 s->drive_serial = drive_serial++;
2620 s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
2621 s->smart_selftest_data = qemu_blockalign(s->bs, 512);
2622 s->sector_write_timer = qemu_new_timer(vm_clock,
2623 ide_sector_write_timer_cb, s);
2624 if (i == 0)
2625 ide_init_drive(s, hd0);
2626 if (i == 1)
2627 ide_init_drive(s, hd1);
2628 }
2629 bus->irq = irq;
2630 }
2631
2632 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2)
2633 {
2634 register_ioport_write(iobase, 8, 1, ide_ioport_write, bus);
2635 register_ioport_read(iobase, 8, 1, ide_ioport_read, bus);
2636 if (iobase2) {
2637 register_ioport_read(iobase2, 1, 1, ide_status_read, bus);
2638 register_ioport_write(iobase2, 1, 1, ide_cmd_write, bus);
2639 }
2640
2641 /* data ports */
2642 register_ioport_write(iobase, 2, 2, ide_data_writew, bus);
2643 register_ioport_read(iobase, 2, 2, ide_data_readw, bus);
2644 register_ioport_write(iobase, 4, 4, ide_data_writel, bus);
2645 register_ioport_read(iobase, 4, 4, ide_data_readl, bus);
2646 }
2647
2648 static bool is_identify_set(void *opaque, int version_id)
2649 {
2650 IDEState *s = opaque;
2651
2652 return s->identify_set != 0;
2653 }
2654
2655 static int ide_drive_post_load(void *opaque, int version_id)
2656 {
2657 IDEState *s = opaque;
2658
2659 if (version_id < 3) {
2660 if (s->sense_key == SENSE_UNIT_ATTENTION &&
2661 s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED) {
2662 s->cdrom_changed = 1;
2663 }
2664 }
2665 return 0;
2666 }
2667
2668 const VMStateDescription vmstate_ide_drive = {
2669 .name = "ide_drive",
2670 .version_id = 3,
2671 .minimum_version_id = 0,
2672 .minimum_version_id_old = 0,
2673 .post_load = ide_drive_post_load,
2674 .fields = (VMStateField []) {
2675 VMSTATE_INT32(mult_sectors, IDEState),
2676 VMSTATE_INT32(identify_set, IDEState),
2677 VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set),
2678 VMSTATE_UINT8(feature, IDEState),
2679 VMSTATE_UINT8(error, IDEState),
2680 VMSTATE_UINT32(nsector, IDEState),
2681 VMSTATE_UINT8(sector, IDEState),
2682 VMSTATE_UINT8(lcyl, IDEState),
2683 VMSTATE_UINT8(hcyl, IDEState),
2684 VMSTATE_UINT8(hob_feature, IDEState),
2685 VMSTATE_UINT8(hob_sector, IDEState),
2686 VMSTATE_UINT8(hob_nsector, IDEState),
2687 VMSTATE_UINT8(hob_lcyl, IDEState),
2688 VMSTATE_UINT8(hob_hcyl, IDEState),
2689 VMSTATE_UINT8(select, IDEState),
2690 VMSTATE_UINT8(status, IDEState),
2691 VMSTATE_UINT8(lba48, IDEState),
2692 VMSTATE_UINT8(sense_key, IDEState),
2693 VMSTATE_UINT8(asc, IDEState),
2694 VMSTATE_UINT8_V(cdrom_changed, IDEState, 3),
2695 /* XXX: if a transfer is pending, we do not save it yet */
2696 VMSTATE_END_OF_LIST()
2697 }
2698 };
2699
2700 const VMStateDescription vmstate_ide_bus = {
2701 .name = "ide_bus",
2702 .version_id = 1,
2703 .minimum_version_id = 1,
2704 .minimum_version_id_old = 1,
2705 .fields = (VMStateField []) {
2706 VMSTATE_UINT8(cmd, IDEBus),
2707 VMSTATE_UINT8(unit, IDEBus),
2708 VMSTATE_END_OF_LIST()
2709 }
2710 };
2711
2712 /***********************************************************/
2713 /* PCI IDE definitions */
2714
2715 static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb)
2716 {
2717 BMDMAState *bm = s->bus->bmdma;
2718 if(!bm)
2719 return;
2720 bm->unit = s->unit;
2721 bm->dma_cb = dma_cb;
2722 bm->cur_prd_last = 0;
2723 bm->cur_prd_addr = 0;
2724 bm->cur_prd_len = 0;
2725 bm->sector_num = ide_get_sector(s);
2726 bm->nsector = s->nsector;
2727 if (bm->status & BM_STATUS_DMAING) {
2728 bm->dma_cb(bm, 0);
2729 }
2730 }
2731
2732 static void ide_dma_restart(IDEState *s, int is_read)
2733 {
2734 BMDMAState *bm = s->bus->bmdma;
2735 ide_set_sector(s, bm->sector_num);
2736 s->io_buffer_index = 0;
2737 s->io_buffer_size = 0;
2738 s->nsector = bm->nsector;
2739 bm->cur_addr = bm->addr;
2740
2741 if (is_read) {
2742 bm->dma_cb = ide_read_dma_cb;
2743 } else {
2744 bm->dma_cb = ide_write_dma_cb;
2745 }
2746
2747 ide_dma_start(s, bm->dma_cb);
2748 }
2749
2750 void ide_dma_cancel(BMDMAState *bm)
2751 {
2752 if (bm->status & BM_STATUS_DMAING) {
2753 bm->status &= ~BM_STATUS_DMAING;
2754 /* cancel DMA request */
2755 bm->unit = -1;
2756 bm->dma_cb = NULL;
2757 if (bm->aiocb) {
2758 #ifdef DEBUG_AIO
2759 printf("aio_cancel\n");
2760 #endif
2761 bdrv_aio_cancel(bm->aiocb);
2762 bm->aiocb = NULL;
2763 }
2764 }
2765 }
2766
2767 void ide_dma_reset(BMDMAState *bm)
2768 {
2769 #ifdef DEBUG_IDE
2770 printf("ide: dma_reset\n");
2771 #endif
2772 ide_dma_cancel(bm);
2773 bm->cmd = 0;
2774 bm->status = 0;
2775 bm->addr = 0;
2776 bm->cur_addr = 0;
2777 bm->cur_prd_last = 0;
2778 bm->cur_prd_addr = 0;
2779 bm->cur_prd_len = 0;
2780 bm->sector_num = 0;
2781 bm->nsector = 0;
2782 }