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