]> Git Repo - qemu.git/blame - hw/ide/core.c
block: add block_resize monitor command
[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);
554a310b 468 vm_stop(0);
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
TS
1586/* called when the inserted state of the media has changed */
1587static void cdrom_change_cb(void *opaque)
1588{
1589 IDEState *s = opaque;
96b8f136 1590 uint64_t nb_sectors;
bd491d6a 1591
bd491d6a
TS
1592 bdrv_get_geometry(s->bs, &nb_sectors);
1593 s->nb_sectors = nb_sectors;
9118e7f0
AL
1594
1595 s->sense_key = SENSE_UNIT_ATTENTION;
1596 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
93c8cfd9 1597 s->cdrom_changed = 1;
9cdd03a7 1598 ide_set_irq(s->bus);
bd491d6a
TS
1599}
1600
c2ff060f
FB
1601static void ide_cmd_lba48_transform(IDEState *s, int lba48)
1602{
1603 s->lba48 = lba48;
1604
1605 /* handle the 'magic' 0 nsector count conversion here. to avoid
1606 * fiddling with the rest of the read logic, we just store the
1607 * full sector count in ->nsector and ignore ->hob_nsector from now
1608 */
1609 if (!s->lba48) {
1610 if (!s->nsector)
1611 s->nsector = 256;
1612 } else {
1613 if (!s->nsector && !s->hob_nsector)
1614 s->nsector = 65536;
1615 else {
1616 int lo = s->nsector;
1617 int hi = s->hob_nsector;
1618
1619 s->nsector = (hi << 8) | lo;
1620 }
1621 }
1622}
1623
bcbdc4d3 1624static void ide_clear_hob(IDEBus *bus)
c2ff060f
FB
1625{
1626 /* any write clears HOB high bit of device control register */
bcbdc4d3
GH
1627 bus->ifs[0].select &= ~(1 << 7);
1628 bus->ifs[1].select &= ~(1 << 7);
c2ff060f
FB
1629}
1630
356721ae 1631void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
caed8802 1632{
bcbdc4d3 1633 IDEBus *bus = opaque;
5391d806
FB
1634
1635#ifdef DEBUG_IDE
1636 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1637#endif
c2ff060f 1638
5391d806 1639 addr &= 7;
fcdd25ab
AL
1640
1641 /* ignore writes to command block while busy with previous command */
bcbdc4d3 1642 if (addr != 7 && (idebus_active_if(bus)->status & (BUSY_STAT|DRQ_STAT)))
fcdd25ab
AL
1643 return;
1644
5391d806
FB
1645 switch(addr) {
1646 case 0:
1647 break;
1648 case 1:
bcbdc4d3 1649 ide_clear_hob(bus);
c45c3d00 1650 /* NOTE: data is written to the two drives */
bcbdc4d3
GH
1651 bus->ifs[0].hob_feature = bus->ifs[0].feature;
1652 bus->ifs[1].hob_feature = bus->ifs[1].feature;
1653 bus->ifs[0].feature = val;
1654 bus->ifs[1].feature = val;
5391d806
FB
1655 break;
1656 case 2:
bcbdc4d3
GH
1657 ide_clear_hob(bus);
1658 bus->ifs[0].hob_nsector = bus->ifs[0].nsector;
1659 bus->ifs[1].hob_nsector = bus->ifs[1].nsector;
1660 bus->ifs[0].nsector = val;
1661 bus->ifs[1].nsector = val;
5391d806
FB
1662 break;
1663 case 3:
bcbdc4d3
GH
1664 ide_clear_hob(bus);
1665 bus->ifs[0].hob_sector = bus->ifs[0].sector;
1666 bus->ifs[1].hob_sector = bus->ifs[1].sector;
1667 bus->ifs[0].sector = val;
1668 bus->ifs[1].sector = val;
5391d806
FB
1669 break;
1670 case 4:
bcbdc4d3
GH
1671 ide_clear_hob(bus);
1672 bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl;
1673 bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl;
1674 bus->ifs[0].lcyl = val;
1675 bus->ifs[1].lcyl = val;
5391d806
FB
1676 break;
1677 case 5:
bcbdc4d3
GH
1678 ide_clear_hob(bus);
1679 bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl;
1680 bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl;
1681 bus->ifs[0].hcyl = val;
1682 bus->ifs[1].hcyl = val;
5391d806
FB
1683 break;
1684 case 6:
c2ff060f 1685 /* FIXME: HOB readback uses bit 7 */
bcbdc4d3
GH
1686 bus->ifs[0].select = (val & ~0x10) | 0xa0;
1687 bus->ifs[1].select = (val | 0x10) | 0xa0;
5391d806 1688 /* select drive */
bcbdc4d3 1689 bus->unit = (val >> 4) & 1;
5391d806
FB
1690 break;
1691 default:
1692 case 7:
1693 /* command */
7cff87ff
AG
1694 ide_exec_cmd(bus, val);
1695 break;
1696 }
1697}
1698
1699
1700void ide_exec_cmd(IDEBus *bus, uint32_t val)
1701{
1702 IDEState *s;
1703 int n;
1704 int lba48 = 0;
1705
5391d806 1706#if defined(DEBUG_IDE)
6ef2ba5e 1707 printf("ide: CMD=%02x\n", val);
5391d806 1708#endif
6ef2ba5e
AG
1709 s = idebus_active_if(bus);
1710 /* ignore commands to non existant slave */
1711 if (s != bus->ifs && !s->bs)
1712 return;
c2ff060f 1713
6ef2ba5e
AG
1714 /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
1715 if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
1716 return;
fcdd25ab 1717
6ef2ba5e
AG
1718 switch(val) {
1719 case WIN_IDENTIFY:
1720 if (s->bs && s->drive_kind != IDE_CD) {
1721 if (s->drive_kind != IDE_CFATA)
1722 ide_identify(s);
1723 else
1724 ide_cfata_identify(s);
769bec72 1725 s->status = READY_STAT | SEEK_STAT;
6ef2ba5e
AG
1726 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1727 } else {
1728 if (s->drive_kind == IDE_CD) {
1729 ide_set_signature(s);
5391d806 1730 }
6ef2ba5e
AG
1731 ide_abort_command(s);
1732 }
1733 ide_set_irq(s->bus);
1734 break;
1735 case WIN_SPECIFY:
1736 case WIN_RECAL:
1737 s->error = 0;
1738 s->status = READY_STAT | SEEK_STAT;
1739 ide_set_irq(s->bus);
1740 break;
1741 case WIN_SETMULT:
1742 if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
1743 /* Disable Read and Write Multiple */
1744 s->mult_sectors = 0;
41a2b959 1745 s->status = READY_STAT | SEEK_STAT;
6ef2ba5e
AG
1746 } else if ((s->nsector & 0xff) != 0 &&
1747 ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1748 (s->nsector & (s->nsector - 1)) != 0)) {
1749 ide_abort_command(s);
1750 } else {
1751 s->mult_sectors = s->nsector & 0xff;
1752 s->status = READY_STAT | SEEK_STAT;
1753 }
1754 ide_set_irq(s->bus);
1755 break;
1756 case WIN_VERIFY_EXT:
1757 lba48 = 1;
1758 case WIN_VERIFY:
1759 case WIN_VERIFY_ONCE:
1760 /* do sector number check ? */
1761 ide_cmd_lba48_transform(s, lba48);
1762 s->status = READY_STAT | SEEK_STAT;
1763 ide_set_irq(s->bus);
1764 break;
c2ff060f 1765 case WIN_READ_EXT:
6ef2ba5e
AG
1766 lba48 = 1;
1767 case WIN_READ:
1768 case WIN_READ_ONCE:
1769 if (!s->bs)
1770 goto abort_cmd;
1771 ide_cmd_lba48_transform(s, lba48);
1772 s->req_nb_sectors = 1;
1773 ide_sector_read(s);
1774 break;
c2ff060f 1775 case WIN_WRITE_EXT:
6ef2ba5e
AG
1776 lba48 = 1;
1777 case WIN_WRITE:
1778 case WIN_WRITE_ONCE:
1779 case CFA_WRITE_SECT_WO_ERASE:
1780 case WIN_WRITE_VERIFY:
1781 ide_cmd_lba48_transform(s, lba48);
1782 s->error = 0;
1783 s->status = SEEK_STAT | READY_STAT;
1784 s->req_nb_sectors = 1;
1785 ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1786 s->media_changed = 1;
1787 break;
c2ff060f 1788 case WIN_MULTREAD_EXT:
6ef2ba5e
AG
1789 lba48 = 1;
1790 case WIN_MULTREAD:
1791 if (!s->mult_sectors)
1792 goto abort_cmd;
1793 ide_cmd_lba48_transform(s, lba48);
1794 s->req_nb_sectors = s->mult_sectors;
1795 ide_sector_read(s);
1796 break;
1797 case WIN_MULTWRITE_EXT:
1798 lba48 = 1;
1799 case WIN_MULTWRITE:
1800 case CFA_WRITE_MULTI_WO_ERASE:
1801 if (!s->mult_sectors)
1802 goto abort_cmd;
1803 ide_cmd_lba48_transform(s, lba48);
1804 s->error = 0;
1805 s->status = SEEK_STAT | READY_STAT;
1806 s->req_nb_sectors = s->mult_sectors;
1807 n = s->nsector;
1808 if (n > s->req_nb_sectors)
1809 n = s->req_nb_sectors;
1810 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1811 s->media_changed = 1;
1812 break;
c2ff060f 1813 case WIN_READDMA_EXT:
6ef2ba5e
AG
1814 lba48 = 1;
1815 case WIN_READDMA:
1816 case WIN_READDMA_ONCE:
1817 if (!s->bs)
1818 goto abort_cmd;
1819 ide_cmd_lba48_transform(s, lba48);
cd369c46 1820 ide_sector_start_dma(s, 1);
6ef2ba5e 1821 break;
c2ff060f 1822 case WIN_WRITEDMA_EXT:
6ef2ba5e
AG
1823 lba48 = 1;
1824 case WIN_WRITEDMA:
1825 case WIN_WRITEDMA_ONCE:
1826 if (!s->bs)
1827 goto abort_cmd;
1828 ide_cmd_lba48_transform(s, lba48);
cd369c46 1829 ide_sector_start_dma(s, 0);
6ef2ba5e
AG
1830 s->media_changed = 1;
1831 break;
1832 case WIN_READ_NATIVE_MAX_EXT:
1833 lba48 = 1;
1834 case WIN_READ_NATIVE_MAX:
1835 ide_cmd_lba48_transform(s, lba48);
1836 ide_set_sector(s, s->nb_sectors - 1);
1837 s->status = READY_STAT | SEEK_STAT;
1838 ide_set_irq(s->bus);
1839 break;
1840 case WIN_CHECKPOWERMODE1:
1841 case WIN_CHECKPOWERMODE2:
1842 s->nsector = 0xff; /* device active or idle */
1843 s->status = READY_STAT | SEEK_STAT;
1844 ide_set_irq(s->bus);
1845 break;
1846 case WIN_SETFEATURES:
1847 if (!s->bs)
1848 goto abort_cmd;
1849 /* XXX: valid for CDROM ? */
1850 switch(s->feature) {
1851 case 0xcc: /* reverting to power-on defaults enable */
1852 case 0x66: /* reverting to power-on defaults disable */
1853 case 0x02: /* write cache enable */
1854 case 0x82: /* write cache disable */
1855 case 0xaa: /* read look-ahead enable */
1856 case 0x55: /* read look-ahead disable */
1857 case 0x05: /* set advanced power management mode */
1858 case 0x85: /* disable advanced power management mode */
1859 case 0x69: /* NOP */
1860 case 0x67: /* NOP */
1861 case 0x96: /* NOP */
1862 case 0x9a: /* NOP */
1863 case 0x42: /* enable Automatic Acoustic Mode */
1864 case 0xc2: /* disable Automatic Acoustic Mode */
41a2b959 1865 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 1866 ide_set_irq(s->bus);
a136e5a8 1867 break;
6ef2ba5e 1868 case 0x03: { /* set transfer mode */
94458802 1869 uint8_t val = s->nsector & 0x07;
6ef2ba5e 1870 uint16_t *identify_data = (uint16_t *)s->identify_data;
94458802
FB
1871
1872 switch (s->nsector >> 3) {
6ef2ba5e
AG
1873 case 0x00: /* pio default */
1874 case 0x01: /* pio mode */
96c35ceb
JQ
1875 put_le16(identify_data + 62,0x07);
1876 put_le16(identify_data + 63,0x07);
1877 put_le16(identify_data + 88,0x3f);
d1b5c20d 1878 break;
6ef2ba5e 1879 case 0x02: /* sigle word dma mode*/
96c35ceb
JQ
1880 put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
1881 put_le16(identify_data + 63,0x07);
1882 put_le16(identify_data + 88,0x3f);
94458802 1883 break;
6ef2ba5e 1884 case 0x04: /* mdma mode */
96c35ceb
JQ
1885 put_le16(identify_data + 62,0x07);
1886 put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
1887 put_le16(identify_data + 88,0x3f);
94458802 1888 break;
6ef2ba5e 1889 case 0x08: /* udma mode */
96c35ceb
JQ
1890 put_le16(identify_data + 62,0x07);
1891 put_le16(identify_data + 63,0x07);
1892 put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
94458802 1893 break;
6ef2ba5e 1894 default:
94458802
FB
1895 goto abort_cmd;
1896 }
4fbfcd6d 1897 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 1898 ide_set_irq(s->bus);
4fbfcd6d 1899 break;
6ef2ba5e
AG
1900 }
1901 default:
1902 goto abort_cmd;
1903 }
1904 break;
1905 case WIN_FLUSH_CACHE:
1906 case WIN_FLUSH_CACHE_EXT:
1907 ide_flush_cache(s);
1908 break;
1909 case WIN_STANDBY:
1910 case WIN_STANDBY2:
1911 case WIN_STANDBYNOW1:
1912 case WIN_STANDBYNOW2:
1913 case WIN_IDLEIMMEDIATE:
1914 case CFA_IDLEIMMEDIATE:
1915 case WIN_SETIDLE1:
1916 case WIN_SETIDLE2:
1917 case WIN_SLEEPNOW1:
1918 case WIN_SLEEPNOW2:
1919 s->status = READY_STAT;
1920 ide_set_irq(s->bus);
1921 break;
1922 case WIN_SEEK:
1923 if(s->drive_kind == IDE_CD)
1924 goto abort_cmd;
1925 /* XXX: Check that seek is within bounds */
1926 s->status = READY_STAT | SEEK_STAT;
1927 ide_set_irq(s->bus);
1928 break;
1929 /* ATAPI commands */
1930 case WIN_PIDENTIFY:
1931 if (s->drive_kind == IDE_CD) {
1932 ide_atapi_identify(s);
41a2b959 1933 s->status = READY_STAT | SEEK_STAT;
6ef2ba5e
AG
1934 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1935 } else {
1936 ide_abort_command(s);
1937 }
1938 ide_set_irq(s->bus);
1939 break;
1940 case WIN_DIAGNOSE:
1941 ide_set_signature(s);
1942 if (s->drive_kind == IDE_CD)
1943 s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
1944 * devices to return a clear status register
1945 * with READY_STAT *not* set. */
1946 else
41a2b959 1947 s->status = READY_STAT | SEEK_STAT;
6ef2ba5e
AG
1948 s->error = 0x01; /* Device 0 passed, Device 1 passed or not
1949 * present.
1950 */
1951 ide_set_irq(s->bus);
1952 break;
1953 case WIN_SRST:
1954 if (s->drive_kind != IDE_CD)
1955 goto abort_cmd;
1956 ide_set_signature(s);
1957 s->status = 0x00; /* NOTE: READY is _not_ set */
1958 s->error = 0x01;
1959 break;
1960 case WIN_PACKETCMD:
1961 if (s->drive_kind != IDE_CD)
1962 goto abort_cmd;
1963 /* overlapping commands not supported */
1964 if (s->feature & 0x02)
1965 goto abort_cmd;
1966 s->status = READY_STAT | SEEK_STAT;
1967 s->atapi_dma = s->feature & 1;
1968 s->nsector = 1;
1969 ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1970 ide_atapi_cmd);
1971 break;
1972 /* CF-ATA commands */
1973 case CFA_REQ_EXT_ERROR_CODE:
1974 if (s->drive_kind != IDE_CFATA)
1975 goto abort_cmd;
1976 s->error = 0x09; /* miscellaneous error */
1977 s->status = READY_STAT | SEEK_STAT;
1978 ide_set_irq(s->bus);
1979 break;
1980 case CFA_ERASE_SECTORS:
1981 case CFA_WEAR_LEVEL:
1982 if (s->drive_kind != IDE_CFATA)
1983 goto abort_cmd;
1984 if (val == CFA_WEAR_LEVEL)
1985 s->nsector = 0;
1986 if (val == CFA_ERASE_SECTORS)
1987 s->media_changed = 1;
1988 s->error = 0x00;
1989 s->status = READY_STAT | SEEK_STAT;
1990 ide_set_irq(s->bus);
1991 break;
1992 case CFA_TRANSLATE_SECTOR:
1993 if (s->drive_kind != IDE_CFATA)
1994 goto abort_cmd;
1995 s->error = 0x00;
1996 s->status = READY_STAT | SEEK_STAT;
1997 memset(s->io_buffer, 0, 0x200);
1998 s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
1999 s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
2000 s->io_buffer[0x02] = s->select; /* Head */
2001 s->io_buffer[0x03] = s->sector; /* Sector */
2002 s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
2003 s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
2004 s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
2005 s->io_buffer[0x13] = 0x00; /* Erase flag */
2006 s->io_buffer[0x18] = 0x00; /* Hot count */
2007 s->io_buffer[0x19] = 0x00; /* Hot count */
2008 s->io_buffer[0x1a] = 0x01; /* Hot count */
2009 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2010 ide_set_irq(s->bus);
2011 break;
2012 case CFA_ACCESS_METADATA_STORAGE:
2013 if (s->drive_kind != IDE_CFATA)
2014 goto abort_cmd;
2015 switch (s->feature) {
2016 case 0x02: /* Inquiry Metadata Storage */
2017 ide_cfata_metadata_inquiry(s);
201a51fc 2018 break;
6ef2ba5e
AG
2019 case 0x03: /* Read Metadata Storage */
2020 ide_cfata_metadata_read(s);
201a51fc 2021 break;
6ef2ba5e
AG
2022 case 0x04: /* Write Metadata Storage */
2023 ide_cfata_metadata_write(s);
201a51fc 2024 break;
6ef2ba5e
AG
2025 default:
2026 goto abort_cmd;
2027 }
2028 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2029 s->status = 0x00; /* NOTE: READY is _not_ set */
2030 ide_set_irq(s->bus);
2031 break;
2032 case IBM_SENSE_CONDITION:
2033 if (s->drive_kind != IDE_CFATA)
2034 goto abort_cmd;
2035 switch (s->feature) {
2036 case 0x01: /* sense temperature in device */
2037 s->nsector = 0x50; /* +20 C */
201a51fc 2038 break;
6ef2ba5e
AG
2039 default:
2040 goto abort_cmd;
2041 }
2042 s->status = READY_STAT | SEEK_STAT;
2043 ide_set_irq(s->bus);
2044 break;
e8b54394
BW
2045
2046 case WIN_SMART:
6ef2ba5e 2047 if (s->drive_kind == IDE_CD)
e8b54394 2048 goto abort_cmd;
6ef2ba5e 2049 if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
e8b54394 2050 goto abort_cmd;
6ef2ba5e 2051 if (!s->smart_enabled && s->feature != SMART_ENABLE)
e8b54394 2052 goto abort_cmd;
6ef2ba5e
AG
2053 switch (s->feature) {
2054 case SMART_DISABLE:
e8b54394
BW
2055 s->smart_enabled = 0;
2056 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 2057 ide_set_irq(s->bus);
e8b54394 2058 break;
6ef2ba5e 2059 case SMART_ENABLE:
e8b54394
BW
2060 s->smart_enabled = 1;
2061 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 2062 ide_set_irq(s->bus);
e8b54394 2063 break;
6ef2ba5e 2064 case SMART_ATTR_AUTOSAVE:
e8b54394
BW
2065 switch (s->sector) {
2066 case 0x00:
6ef2ba5e
AG
2067 s->smart_autosave = 0;
2068 break;
e8b54394 2069 case 0xf1:
6ef2ba5e
AG
2070 s->smart_autosave = 1;
2071 break;
e8b54394 2072 default:
6ef2ba5e 2073 goto abort_cmd;
e8b54394
BW
2074 }
2075 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 2076 ide_set_irq(s->bus);
e8b54394 2077 break;
6ef2ba5e 2078 case SMART_STATUS:
e8b54394 2079 if (!s->smart_errors) {
6ef2ba5e
AG
2080 s->hcyl = 0xc2;
2081 s->lcyl = 0x4f;
e8b54394 2082 } else {
6ef2ba5e
AG
2083 s->hcyl = 0x2c;
2084 s->lcyl = 0xf4;
e8b54394
BW
2085 }
2086 s->status = READY_STAT | SEEK_STAT;
9cdd03a7 2087 ide_set_irq(s->bus);
e8b54394 2088 break;
6ef2ba5e 2089 case SMART_READ_THRESH:
e8b54394
BW
2090 memset(s->io_buffer, 0, 0x200);
2091 s->io_buffer[0] = 0x01; /* smart struct version */
2092 for (n=0; n<30; n++) {
6ef2ba5e 2093 if (smart_attributes[n][0] == 0)
e8b54394 2094 break;
6ef2ba5e
AG
2095 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2096 s->io_buffer[2+1+(n*12)] = smart_attributes[n][4];
e8b54394
BW
2097 }
2098 for (n=0; n<511; n++) /* checksum */
6ef2ba5e 2099 s->io_buffer[511] += s->io_buffer[n];
e8b54394
BW
2100 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2101 s->status = READY_STAT | SEEK_STAT;
2102 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
9cdd03a7 2103 ide_set_irq(s->bus);
e8b54394 2104 break;
6ef2ba5e 2105 case SMART_READ_DATA:
e8b54394
BW
2106 memset(s->io_buffer, 0, 0x200);
2107 s->io_buffer[0] = 0x01; /* smart struct version */
2108 for (n=0; n<30; n++) {
6ef2ba5e 2109 if (smart_attributes[n][0] == 0)
e8b54394 2110 break;
6ef2ba5e
AG
2111 s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
2112 s->io_buffer[2+1+(n*12)] = smart_attributes[n][1];
2113 s->io_buffer[2+3+(n*12)] = smart_attributes[n][2];
2114 s->io_buffer[2+4+(n*12)] = smart_attributes[n][3];
e8b54394
BW
2115 }
2116 s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
2117 if (s->smart_selftest_count == 0) {
6ef2ba5e 2118 s->io_buffer[363] = 0;
e8b54394 2119 } else {
6ef2ba5e 2120 s->io_buffer[363] =
e8b54394 2121 s->smart_selftest_data[3 +
6ef2ba5e
AG
2122 (s->smart_selftest_count - 1) *
2123 24];
e8b54394
BW
2124 }
2125 s->io_buffer[364] = 0x20;
2126 s->io_buffer[365] = 0x01;
2127 /* offline data collection capacity: execute + self-test*/
2128 s->io_buffer[367] = (1<<4 | 1<<3 | 1);
2129 s->io_buffer[368] = 0x03; /* smart capability (1) */
2130 s->io_buffer[369] = 0x00; /* smart capability (2) */
2131 s->io_buffer[370] = 0x01; /* error logging supported */
2132 s->io_buffer[372] = 0x02; /* minutes for poll short test */
2133 s->io_buffer[373] = 0x36; /* minutes for poll ext test */
2134 s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
2135
2136 for (n=0; n<511; n++)
6ef2ba5e 2137 s->io_buffer[511] += s->io_buffer[n];
e8b54394
BW
2138 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2139 s->status = READY_STAT | SEEK_STAT;
2140 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
9cdd03a7 2141 ide_set_irq(s->bus);
e8b54394 2142 break;
6ef2ba5e 2143 case SMART_READ_LOG:
e8b54394
BW
2144 switch (s->sector) {
2145 case 0x01: /* summary smart error log */
6ef2ba5e
AG
2146 memset(s->io_buffer, 0, 0x200);
2147 s->io_buffer[0] = 0x01;
2148 s->io_buffer[1] = 0x00; /* no error entries */
2149 s->io_buffer[452] = s->smart_errors & 0xff;
2150 s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
e8b54394 2151
6ef2ba5e 2152 for (n=0; n<511; n++)
e8b54394 2153 s->io_buffer[511] += s->io_buffer[n];
6ef2ba5e
AG
2154 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2155 break;
e8b54394 2156 case 0x06: /* smart self test log */
6ef2ba5e
AG
2157 memset(s->io_buffer, 0, 0x200);
2158 s->io_buffer[0] = 0x01;
2159 if (s->smart_selftest_count == 0) {
e8b54394 2160 s->io_buffer[508] = 0;
6ef2ba5e 2161 } else {
e8b54394
BW
2162 s->io_buffer[508] = s->smart_selftest_count;
2163 for (n=2; n<506; n++)
6ef2ba5e
AG
2164 s->io_buffer[n] = s->smart_selftest_data[n];
2165 }
2166 for (n=0; n<511; n++)
e8b54394 2167 s->io_buffer[511] += s->io_buffer[n];
6ef2ba5e
AG
2168 s->io_buffer[511] = 0x100 - s->io_buffer[511];
2169 break;
e8b54394 2170 default:
6ef2ba5e 2171 goto abort_cmd;
e8b54394
BW
2172 }
2173 s->status = READY_STAT | SEEK_STAT;
2174 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
9cdd03a7 2175 ide_set_irq(s->bus);
e8b54394 2176 break;
6ef2ba5e 2177 case SMART_EXECUTE_OFFLINE:
e8b54394
BW
2178 switch (s->sector) {
2179 case 0: /* off-line routine */
2180 case 1: /* short self test */
2181 case 2: /* extended self test */
6ef2ba5e
AG
2182 s->smart_selftest_count++;
2183 if(s->smart_selftest_count > 21)
e8b54394 2184 s->smart_selftest_count = 0;
6ef2ba5e
AG
2185 n = 2 + (s->smart_selftest_count - 1) * 24;
2186 s->smart_selftest_data[n] = s->sector;
2187 s->smart_selftest_data[n+1] = 0x00; /* OK and finished */
2188 s->smart_selftest_data[n+2] = 0x34; /* hour count lsb */
2189 s->smart_selftest_data[n+3] = 0x12; /* hour count msb */
2190 s->status = READY_STAT | SEEK_STAT;
2191 ide_set_irq(s->bus);
2192 break;
e8b54394 2193 default:
6ef2ba5e 2194 goto abort_cmd;
e8b54394
BW
2195 }
2196 break;
6ef2ba5e 2197 default:
e8b54394 2198 goto abort_cmd;
6ef2ba5e
AG
2199 }
2200 break;
2201 default:
2202 abort_cmd:
2203 ide_abort_command(s);
2204 ide_set_irq(s->bus);
2205 break;
2206 }
5391d806
FB
2207}
2208
356721ae 2209uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
5391d806 2210{
bcbdc4d3
GH
2211 IDEBus *bus = opaque;
2212 IDEState *s = idebus_active_if(bus);
5391d806 2213 uint32_t addr;
c2ff060f 2214 int ret, hob;
5391d806
FB
2215
2216 addr = addr1 & 7;
c2ff060f
FB
2217 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2218 //hob = s->select & (1 << 7);
2219 hob = 0;
5391d806
FB
2220 switch(addr) {
2221 case 0:
2222 ret = 0xff;
2223 break;
2224 case 1:
bcbdc4d3
GH
2225 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2226 (s != bus->ifs && !s->bs))
c45c3d00 2227 ret = 0;
c2ff060f 2228 else if (!hob)
c45c3d00 2229 ret = s->error;
c2ff060f
FB
2230 else
2231 ret = s->hob_feature;
5391d806
FB
2232 break;
2233 case 2:
bcbdc4d3 2234 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
c45c3d00 2235 ret = 0;
c2ff060f 2236 else if (!hob)
c45c3d00 2237 ret = s->nsector & 0xff;
c2ff060f
FB
2238 else
2239 ret = s->hob_nsector;
5391d806
FB
2240 break;
2241 case 3:
bcbdc4d3 2242 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
c45c3d00 2243 ret = 0;
c2ff060f 2244 else if (!hob)
c45c3d00 2245 ret = s->sector;
c2ff060f
FB
2246 else
2247 ret = s->hob_sector;
5391d806
FB
2248 break;
2249 case 4:
bcbdc4d3 2250 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
c45c3d00 2251 ret = 0;
c2ff060f 2252 else if (!hob)
c45c3d00 2253 ret = s->lcyl;
c2ff060f
FB
2254 else
2255 ret = s->hob_lcyl;
5391d806
FB
2256 break;
2257 case 5:
bcbdc4d3 2258 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
c45c3d00 2259 ret = 0;
c2ff060f 2260 else if (!hob)
c45c3d00 2261 ret = s->hcyl;
c2ff060f
FB
2262 else
2263 ret = s->hob_hcyl;
5391d806
FB
2264 break;
2265 case 6:
bcbdc4d3 2266 if (!bus->ifs[0].bs && !bus->ifs[1].bs)
c45c3d00
FB
2267 ret = 0;
2268 else
7ae98627 2269 ret = s->select;
5391d806
FB
2270 break;
2271 default:
2272 case 7:
bcbdc4d3
GH
2273 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2274 (s != bus->ifs && !s->bs))
c45c3d00
FB
2275 ret = 0;
2276 else
2277 ret = s->status;
9cdd03a7 2278 qemu_irq_lower(bus->irq);
5391d806
FB
2279 break;
2280 }
2281#ifdef DEBUG_IDE
2282 printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
2283#endif
2284 return ret;
2285}
2286
356721ae 2287uint32_t ide_status_read(void *opaque, uint32_t addr)
5391d806 2288{
bcbdc4d3
GH
2289 IDEBus *bus = opaque;
2290 IDEState *s = idebus_active_if(bus);
5391d806 2291 int ret;
7ae98627 2292
bcbdc4d3
GH
2293 if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
2294 (s != bus->ifs && !s->bs))
7ae98627
FB
2295 ret = 0;
2296 else
2297 ret = s->status;
5391d806
FB
2298#ifdef DEBUG_IDE
2299 printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
2300#endif
2301 return ret;
2302}
2303
356721ae 2304void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
5391d806 2305{
bcbdc4d3 2306 IDEBus *bus = opaque;
5391d806
FB
2307 IDEState *s;
2308 int i;
2309
2310#ifdef DEBUG_IDE
2311 printf("ide: write control addr=0x%x val=%02x\n", addr, val);
2312#endif
2313 /* common for both drives */
9cdd03a7 2314 if (!(bus->cmd & IDE_CMD_RESET) &&
5391d806
FB
2315 (val & IDE_CMD_RESET)) {
2316 /* reset low to high */
2317 for(i = 0;i < 2; i++) {
bcbdc4d3 2318 s = &bus->ifs[i];
5391d806
FB
2319 s->status = BUSY_STAT | SEEK_STAT;
2320 s->error = 0x01;
2321 }
9cdd03a7 2322 } else if ((bus->cmd & IDE_CMD_RESET) &&
5391d806
FB
2323 !(val & IDE_CMD_RESET)) {
2324 /* high to low */
2325 for(i = 0;i < 2; i++) {
bcbdc4d3 2326 s = &bus->ifs[i];
cd8722bb 2327 if (s->drive_kind == IDE_CD)
6b136f9e
FB
2328 s->status = 0x00; /* NOTE: READY is _not_ set */
2329 else
56bf1d37 2330 s->status = READY_STAT | SEEK_STAT;
5391d806
FB
2331 ide_set_signature(s);
2332 }
2333 }
2334
9cdd03a7 2335 bus->cmd = val;
5391d806
FB
2336}
2337
356721ae 2338void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
5391d806 2339{
bcbdc4d3
GH
2340 IDEBus *bus = opaque;
2341 IDEState *s = idebus_active_if(bus);
5391d806
FB
2342 uint8_t *p;
2343
fcdd25ab
AL
2344 /* PIO data access allowed only when DRQ bit is set */
2345 if (!(s->status & DRQ_STAT))
2346 return;
2347
5391d806 2348 p = s->data_ptr;
0c4ad8dc 2349 *(uint16_t *)p = le16_to_cpu(val);
5391d806
FB
2350 p += 2;
2351 s->data_ptr = p;
2352 if (p >= s->data_end)
2353 s->end_transfer_func(s);
2354}
2355
356721ae 2356uint32_t ide_data_readw(void *opaque, uint32_t addr)
5391d806 2357{
bcbdc4d3
GH
2358 IDEBus *bus = opaque;
2359 IDEState *s = idebus_active_if(bus);
5391d806
FB
2360 uint8_t *p;
2361 int ret;
fcdd25ab
AL
2362
2363 /* PIO data access allowed only when DRQ bit is set */
2364 if (!(s->status & DRQ_STAT))
2365 return 0;
2366
5391d806 2367 p = s->data_ptr;
0c4ad8dc 2368 ret = cpu_to_le16(*(uint16_t *)p);
5391d806
FB
2369 p += 2;
2370 s->data_ptr = p;
2371 if (p >= s->data_end)
2372 s->end_transfer_func(s);
2373 return ret;
2374}
2375
356721ae 2376void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
5391d806 2377{
bcbdc4d3
GH
2378 IDEBus *bus = opaque;
2379 IDEState *s = idebus_active_if(bus);
5391d806
FB
2380 uint8_t *p;
2381
fcdd25ab
AL
2382 /* PIO data access allowed only when DRQ bit is set */
2383 if (!(s->status & DRQ_STAT))
2384 return;
2385
5391d806 2386 p = s->data_ptr;
0c4ad8dc 2387 *(uint32_t *)p = le32_to_cpu(val);
5391d806
FB
2388 p += 4;
2389 s->data_ptr = p;
2390 if (p >= s->data_end)
2391 s->end_transfer_func(s);
2392}
2393
356721ae 2394uint32_t ide_data_readl(void *opaque, uint32_t addr)
5391d806 2395{
bcbdc4d3
GH
2396 IDEBus *bus = opaque;
2397 IDEState *s = idebus_active_if(bus);
5391d806
FB
2398 uint8_t *p;
2399 int ret;
3b46e624 2400
fcdd25ab
AL
2401 /* PIO data access allowed only when DRQ bit is set */
2402 if (!(s->status & DRQ_STAT))
2403 return 0;
2404
5391d806 2405 p = s->data_ptr;
0c4ad8dc 2406 ret = cpu_to_le32(*(uint32_t *)p);
5391d806
FB
2407 p += 4;
2408 s->data_ptr = p;
2409 if (p >= s->data_end)
2410 s->end_transfer_func(s);
2411 return ret;
2412}
2413
a7dfe172
FB
2414static void ide_dummy_transfer_stop(IDEState *s)
2415{
2416 s->data_ptr = s->io_buffer;
2417 s->data_end = s->io_buffer;
2418 s->io_buffer[0] = 0xff;
2419 s->io_buffer[1] = 0xff;
2420 s->io_buffer[2] = 0xff;
2421 s->io_buffer[3] = 0xff;
2422}
2423
4a643563 2424static void ide_reset(IDEState *s)
5391d806 2425{
4a643563
BS
2426#ifdef DEBUG_IDE
2427 printf("ide: reset\n");
2428#endif
cd8722bb 2429 if (s->drive_kind == IDE_CFATA)
201a51fc
AZ
2430 s->mult_sectors = 0;
2431 else
2432 s->mult_sectors = MAX_MULT_SECTORS;
4a643563
BS
2433 /* ide regs */
2434 s->feature = 0;
2435 s->error = 0;
2436 s->nsector = 0;
2437 s->sector = 0;
2438 s->lcyl = 0;
2439 s->hcyl = 0;
2440
2441 /* lba48 */
2442 s->hob_feature = 0;
2443 s->hob_sector = 0;
2444 s->hob_nsector = 0;
2445 s->hob_lcyl = 0;
2446 s->hob_hcyl = 0;
2447
5391d806 2448 s->select = 0xa0;
41a2b959 2449 s->status = READY_STAT | SEEK_STAT;
4a643563
BS
2450
2451 s->lba48 = 0;
2452
2453 /* ATAPI specific */
2454 s->sense_key = 0;
2455 s->asc = 0;
2456 s->cdrom_changed = 0;
2457 s->packet_transfer_size = 0;
2458 s->elementary_transfer_size = 0;
2459 s->io_buffer_index = 0;
2460 s->cd_sector_size = 0;
2461 s->atapi_dma = 0;
2462 /* ATA DMA state */
2463 s->io_buffer_size = 0;
2464 s->req_nb_sectors = 0;
2465
5391d806 2466 ide_set_signature(s);
a7dfe172
FB
2467 /* init the transfer handler so that 0xffff is returned on data
2468 accesses */
2469 s->end_transfer_func = ide_dummy_transfer_stop;
2470 ide_dummy_transfer_stop(s);
201a51fc 2471 s->media_changed = 0;
5391d806
FB
2472}
2473
4a643563
BS
2474void ide_bus_reset(IDEBus *bus)
2475{
2476 bus->unit = 0;
2477 bus->cmd = 0;
2478 ide_reset(&bus->ifs[0]);
2479 ide_reset(&bus->ifs[1]);
2480 ide_clear_hob(bus);
40a6238a
AG
2481
2482 /* pending async DMA */
2483 if (bus->dma->aiocb) {
2484#ifdef DEBUG_AIO
2485 printf("aio_cancel\n");
2486#endif
2487 bdrv_aio_cancel(bus->dma->aiocb);
2488 bus->dma->aiocb = NULL;
2489 }
2490
2491 /* reset dma provider too */
2492 bus->dma->ops->reset(bus->dma);
4a643563
BS
2493}
2494
c4d74df7
MA
2495int ide_init_drive(IDEState *s, BlockDriverState *bs,
2496 const char *version, const char *serial)
88804180
GH
2497{
2498 int cylinders, heads, secs;
2499 uint64_t nb_sectors;
2500
f8b6cc00
MA
2501 s->bs = bs;
2502 bdrv_get_geometry(bs, &nb_sectors);
2503 bdrv_guess_geometry(bs, &cylinders, &heads, &secs);
dce9e928
MA
2504 if (cylinders < 1 || cylinders > 16383) {
2505 error_report("cyls must be between 1 and 16383");
2506 return -1;
2507 }
2508 if (heads < 1 || heads > 16) {
2509 error_report("heads must be between 1 and 16");
2510 return -1;
2511 }
2512 if (secs < 1 || secs > 63) {
2513 error_report("secs must be between 1 and 63");
2514 return -1;
2515 }
870111c8
MA
2516 s->cylinders = cylinders;
2517 s->heads = heads;
2518 s->sectors = secs;
2519 s->nb_sectors = nb_sectors;
2520 /* The SMART values should be preserved across power cycles
2521 but they aren't. */
2522 s->smart_enabled = 1;
2523 s->smart_autosave = 1;
2524 s->smart_errors = 0;
2525 s->smart_selftest_count = 0;
f8b6cc00 2526 if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
cd8722bb 2527 s->drive_kind = IDE_CD;
f8b6cc00 2528 bdrv_set_change_cb(bs, cdrom_change_cb, s);
1b2adf28 2529 bs->buffer_alignment = 2048;
7aa9c811 2530 } else {
98f28ad7
MA
2531 if (!bdrv_is_inserted(s->bs)) {
2532 error_report("Device needs media, but drive is empty");
2533 return -1;
2534 }
7aa9c811
MA
2535 if (bdrv_is_read_only(bs)) {
2536 error_report("Can't use a read-only drive");
2537 return -1;
2538 }
88804180 2539 }
f8b6cc00 2540 if (serial) {
6ced55a5
MA
2541 strncpy(s->drive_serial_str, serial, sizeof(s->drive_serial_str));
2542 } else {
88804180
GH
2543 snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
2544 "QM%05d", s->drive_serial);
870111c8 2545 }
47c06340
GH
2546 if (version) {
2547 pstrcpy(s->version, sizeof(s->version), version);
2548 } else {
2549 pstrcpy(s->version, sizeof(s->version), QEMU_VERSION);
2550 }
40a6238a 2551
88804180 2552 ide_reset(s);
cd8722bb 2553 bdrv_set_removable(bs, s->drive_kind == IDE_CD);
c4d74df7 2554 return 0;
88804180
GH
2555}
2556
57234ee4 2557static void ide_init1(IDEBus *bus, int unit)
d459da0e
MA
2558{
2559 static int drive_serial = 1;
2560 IDEState *s = &bus->ifs[unit];
2561
2562 s->bus = bus;
2563 s->unit = unit;
2564 s->drive_serial = drive_serial++;
1b2adf28
CH
2565 /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
2566 s->io_buffer = qemu_memalign(2048, IDE_DMA_BUF_SECTORS*512 + 4);
50641c5c 2567 s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4;
d459da0e
MA
2568 s->smart_selftest_data = qemu_blockalign(s->bs, 512);
2569 s->sector_write_timer = qemu_new_timer(vm_clock,
2570 ide_sector_write_timer_cb, s);
57234ee4
MA
2571}
2572
40a6238a
AG
2573static void ide_nop_start(IDEDMA *dma, IDEState *s,
2574 BlockDriverCompletionFunc *cb)
2575{
2576}
2577
2578static int ide_nop(IDEDMA *dma)
2579{
2580 return 0;
2581}
2582
2583static int ide_nop_int(IDEDMA *dma, int x)
2584{
2585 return 0;
2586}
2587
2588static void ide_nop_restart(void *opaque, int x, int y)
2589{
2590}
2591
2592static const IDEDMAOps ide_dma_nop_ops = {
2593 .start_dma = ide_nop_start,
2594 .start_transfer = ide_nop,
2595 .prepare_buf = ide_nop_int,
2596 .rw_buf = ide_nop_int,
2597 .set_unit = ide_nop_int,
2598 .add_status = ide_nop_int,
2599 .set_inactive = ide_nop,
2600 .restart_cb = ide_nop_restart,
2601 .reset = ide_nop,
2602};
2603
2604static IDEDMA ide_dma_nop = {
2605 .ops = &ide_dma_nop_ops,
2606 .aiocb = NULL,
2607};
2608
57234ee4
MA
2609void ide_init2(IDEBus *bus, qemu_irq irq)
2610{
2611 int i;
2612
2613 for(i = 0; i < 2; i++) {
2614 ide_init1(bus, i);
2615 ide_reset(&bus->ifs[i]);
870111c8 2616 }
57234ee4 2617 bus->irq = irq;
40a6238a 2618 bus->dma = &ide_dma_nop;
d459da0e
MA
2619}
2620
57234ee4
MA
2621/* TODO convert users to qdev and remove */
2622void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
2623 DriveInfo *hd1, qemu_irq irq)
5391d806 2624{
88804180 2625 int i;
57234ee4 2626 DriveInfo *dinfo;
5391d806 2627
caed8802 2628 for(i = 0; i < 2; i++) {
57234ee4
MA
2629 dinfo = i == 0 ? hd0 : hd1;
2630 ide_init1(bus, i);
2631 if (dinfo) {
c4d74df7
MA
2632 if (ide_init_drive(&bus->ifs[i], dinfo->bdrv, NULL,
2633 *dinfo->serial ? dinfo->serial : NULL) < 0) {
2634 error_report("Can't set up IDE drive %s", dinfo->id);
2635 exit(1);
2636 }
57234ee4
MA
2637 } else {
2638 ide_reset(&bus->ifs[i]);
2639 }
5391d806 2640 }
9cdd03a7 2641 bus->irq = irq;
40a6238a 2642 bus->dma = &ide_dma_nop;
69b91039
FB
2643}
2644
356721ae 2645void ide_init_ioport(IDEBus *bus, int iobase, int iobase2)
69b91039 2646{
bcbdc4d3
GH
2647 register_ioport_write(iobase, 8, 1, ide_ioport_write, bus);
2648 register_ioport_read(iobase, 8, 1, ide_ioport_read, bus);
caed8802 2649 if (iobase2) {
bcbdc4d3
GH
2650 register_ioport_read(iobase2, 1, 1, ide_status_read, bus);
2651 register_ioport_write(iobase2, 1, 1, ide_cmd_write, bus);
5391d806 2652 }
3b46e624 2653
caed8802 2654 /* data ports */
bcbdc4d3
GH
2655 register_ioport_write(iobase, 2, 2, ide_data_writew, bus);
2656 register_ioport_read(iobase, 2, 2, ide_data_readw, bus);
2657 register_ioport_write(iobase, 4, 4, ide_data_writel, bus);
2658 register_ioport_read(iobase, 4, 4, ide_data_readl, bus);
5391d806 2659}
69b91039 2660
37159f13 2661static bool is_identify_set(void *opaque, int version_id)
aa941b94 2662{
37159f13
JQ
2663 IDEState *s = opaque;
2664
2665 return s->identify_set != 0;
2666}
2667
50641c5c
JQ
2668static EndTransferFunc* transfer_end_table[] = {
2669 ide_sector_read,
2670 ide_sector_write,
2671 ide_transfer_stop,
2672 ide_atapi_cmd_reply_end,
2673 ide_atapi_cmd,
2674 ide_dummy_transfer_stop,
2675};
2676
2677static int transfer_end_table_idx(EndTransferFunc *fn)
2678{
2679 int i;
2680
2681 for (i = 0; i < ARRAY_SIZE(transfer_end_table); i++)
2682 if (transfer_end_table[i] == fn)
2683 return i;
2684
2685 return -1;
2686}
2687
37159f13 2688static int ide_drive_post_load(void *opaque, int version_id)
aa941b94 2689{
37159f13
JQ
2690 IDEState *s = opaque;
2691
2692 if (version_id < 3) {
93c8cfd9 2693 if (s->sense_key == SENSE_UNIT_ATTENTION &&
37159f13 2694 s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED) {
93c8cfd9 2695 s->cdrom_changed = 1;
37159f13 2696 }
93c8cfd9 2697 }
37159f13 2698 return 0;
aa941b94
AZ
2699}
2700
50641c5c
JQ
2701static int ide_drive_pio_post_load(void *opaque, int version_id)
2702{
2703 IDEState *s = opaque;
2704
7bccf573 2705 if (s->end_transfer_fn_idx > ARRAY_SIZE(transfer_end_table)) {
50641c5c
JQ
2706 return -EINVAL;
2707 }
2708 s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
2709 s->data_ptr = s->io_buffer + s->cur_io_buffer_offset;
2710 s->data_end = s->data_ptr + s->cur_io_buffer_len;
2711
2712 return 0;
2713}
2714
2715static void ide_drive_pio_pre_save(void *opaque)
2716{
2717 IDEState *s = opaque;
2718 int idx;
2719
2720 s->cur_io_buffer_offset = s->data_ptr - s->io_buffer;
2721 s->cur_io_buffer_len = s->data_end - s->data_ptr;
2722
2723 idx = transfer_end_table_idx(s->end_transfer_func);
2724 if (idx == -1) {
2725 fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n",
2726 __func__);
2727 s->end_transfer_fn_idx = 2;
2728 } else {
2729 s->end_transfer_fn_idx = idx;
2730 }
2731}
2732
2733static bool ide_drive_pio_state_needed(void *opaque)
2734{
2735 IDEState *s = opaque;
2736
2737 return (s->status & DRQ_STAT) != 0;
2738}
2739
2740const VMStateDescription vmstate_ide_drive_pio_state = {
2741 .name = "ide_drive/pio_state",
2742 .version_id = 1,
2743 .minimum_version_id = 1,
2744 .minimum_version_id_old = 1,
2745 .pre_save = ide_drive_pio_pre_save,
2746 .post_load = ide_drive_pio_post_load,
2747 .fields = (VMStateField []) {
2748 VMSTATE_INT32(req_nb_sectors, IDEState),
2749 VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
2750 vmstate_info_uint8, uint8_t),
2751 VMSTATE_INT32(cur_io_buffer_offset, IDEState),
2752 VMSTATE_INT32(cur_io_buffer_len, IDEState),
2753 VMSTATE_UINT8(end_transfer_fn_idx, IDEState),
2754 VMSTATE_INT32(elementary_transfer_size, IDEState),
2755 VMSTATE_INT32(packet_transfer_size, IDEState),
2756 VMSTATE_END_OF_LIST()
2757 }
2758};
2759
37159f13
JQ
2760const VMStateDescription vmstate_ide_drive = {
2761 .name = "ide_drive",
3abb6260 2762 .version_id = 3,
37159f13
JQ
2763 .minimum_version_id = 0,
2764 .minimum_version_id_old = 0,
2765 .post_load = ide_drive_post_load,
2766 .fields = (VMStateField []) {
2767 VMSTATE_INT32(mult_sectors, IDEState),
2768 VMSTATE_INT32(identify_set, IDEState),
2769 VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set),
2770 VMSTATE_UINT8(feature, IDEState),
2771 VMSTATE_UINT8(error, IDEState),
2772 VMSTATE_UINT32(nsector, IDEState),
2773 VMSTATE_UINT8(sector, IDEState),
2774 VMSTATE_UINT8(lcyl, IDEState),
2775 VMSTATE_UINT8(hcyl, IDEState),
2776 VMSTATE_UINT8(hob_feature, IDEState),
2777 VMSTATE_UINT8(hob_sector, IDEState),
2778 VMSTATE_UINT8(hob_nsector, IDEState),
2779 VMSTATE_UINT8(hob_lcyl, IDEState),
2780 VMSTATE_UINT8(hob_hcyl, IDEState),
2781 VMSTATE_UINT8(select, IDEState),
2782 VMSTATE_UINT8(status, IDEState),
2783 VMSTATE_UINT8(lba48, IDEState),
2784 VMSTATE_UINT8(sense_key, IDEState),
2785 VMSTATE_UINT8(asc, IDEState),
2786 VMSTATE_UINT8_V(cdrom_changed, IDEState, 3),
37159f13 2787 VMSTATE_END_OF_LIST()
50641c5c
JQ
2788 },
2789 .subsections = (VMStateSubsection []) {
2790 {
2791 .vmsd = &vmstate_ide_drive_pio_state,
2792 .needed = ide_drive_pio_state_needed,
2793 }, {
2794 /* empty */
2795 }
37159f13
JQ
2796 }
2797};
2798
6521dc62
JQ
2799const VMStateDescription vmstate_ide_bus = {
2800 .name = "ide_bus",
2801 .version_id = 1,
2802 .minimum_version_id = 1,
2803 .minimum_version_id_old = 1,
2804 .fields = (VMStateField []) {
2805 VMSTATE_UINT8(cmd, IDEBus),
2806 VMSTATE_UINT8(unit, IDEBus),
2807 VMSTATE_END_OF_LIST()
2808 }
2809};
This page took 0.968985 seconds and 4 git commands to generate.