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