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