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