core.c 56.0 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU IDE disk and CD/DVD-ROM Emulator
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003 Fabrice Bellard
5
 * Copyright (c) 2006 Openedhand Ltd.
6
 *
B
bellard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
G
Gerd Hoffmann 已提交
25 26 27
#include <hw/hw.h>
#include <hw/pc.h>
#include <hw/pci.h>
28
#include "qemu-error.h"
P
pbrook 已提交
29 30
#include "qemu-timer.h"
#include "sysemu.h"
31
#include "dma.h"
B
Blue Swirl 已提交
32
#include "blockdev.h"
G
Gerd Hoffmann 已提交
33 34

#include <hw/ide/internal.h>
B
Brian Wheeler 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* These values were based on a Seagate ST3500418AS but have been modified
   to make more sense in QEMU */
static const int smart_attributes[][12] = {
    /* id,  flags, hflags, val, wrst, raw (6 bytes), threshold */
    /* raw read error rate*/
    { 0x01, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06},
    /* spin up */
    { 0x03, 0x03, 0x00, 0x64, 0x64, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    /* start stop count */
    { 0x04, 0x02, 0x00, 0x64, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14},
    /* remapped sectors */
    { 0x05, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24},
    /* power on hours */
    { 0x09, 0x03, 0x00, 0x64, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    /* power cycle count */
    { 0x0c, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    /* airflow-temperature-celsius */
    { 190,  0x03, 0x00, 0x45, 0x45, 0x1f, 0x00, 0x1f, 0x1f, 0x00, 0x00, 0x32},
    /* end of list */
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
B
Brian Wheeler 已提交
56 57
};

K
Kevin Wolf 已提交
58
static int ide_handle_rw_error(IDEState *s, int error, int op);
B
bellard 已提交
59

B
bellard 已提交
60 61 62 63 64 65 66 67
static void padstr(char *str, const char *src, int len)
{
    int i, v;
    for(i = 0; i < len; i++) {
        if (*src)
            v = *src++;
        else
            v = ' ';
68
        str[i^1] = v;
B
bellard 已提交
69 70 71
    }
}

B
bellard 已提交
72 73
static void put_le16(uint16_t *p, unsigned int v)
{
B
bellard 已提交
74
    *p = cpu_to_le16(v);
B
bellard 已提交
75 76
}

B
bellard 已提交
77 78 79 80
static void ide_identify(IDEState *s)
{
    uint16_t *p;
    unsigned int oldsize;
81
    IDEDevice *dev;
B
bellard 已提交
82

83 84 85 86 87
    if (s->identify_set) {
	memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
	return;
    }

B
bellard 已提交
88 89
    memset(s->io_buffer, 0, 512);
    p = (uint16_t *)s->io_buffer;
B
bellard 已提交
90
    put_le16(p + 0, 0x0040);
91
    put_le16(p + 1, s->cylinders);
B
bellard 已提交
92 93 94
    put_le16(p + 3, s->heads);
    put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
    put_le16(p + 5, 512); /* XXX: retired, remove ? */
95
    put_le16(p + 6, s->sectors);
96
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
B
bellard 已提交
97 98 99
    put_le16(p + 20, 3); /* XXX: retired, remove ? */
    put_le16(p + 21, 512); /* cache size in sectors */
    put_le16(p + 22, 4); /* ecc bytes */
G
Gerd Hoffmann 已提交
100
    padstr((char *)(p + 23), s->version, 8); /* firmware version */
T
ths 已提交
101
    padstr((char *)(p + 27), "QEMU HARDDISK", 40); /* model */
102
#if MAX_MULT_SECTORS > 1
B
bellard 已提交
103
    put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
B
bellard 已提交
104
#endif
B
bellard 已提交
105
    put_le16(p + 48, 1); /* dword I/O */
106
    put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
B
bellard 已提交
107 108
    put_le16(p + 51, 0x200); /* PIO transfer cycle */
    put_le16(p + 52, 0x200); /* DMA transfer cycle */
109
    put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
B
bellard 已提交
110 111 112
    put_le16(p + 54, s->cylinders);
    put_le16(p + 55, s->heads);
    put_le16(p + 56, s->sectors);
B
bellard 已提交
113
    oldsize = s->cylinders * s->heads * s->sectors;
B
bellard 已提交
114 115
    put_le16(p + 57, oldsize);
    put_le16(p + 58, oldsize >> 16);
B
bellard 已提交
116
    if (s->mult_sectors)
B
bellard 已提交
117 118 119
        put_le16(p + 59, 0x100 | s->mult_sectors);
    put_le16(p + 60, s->nb_sectors);
    put_le16(p + 61, s->nb_sectors >> 16);
120
    put_le16(p + 62, 0x07); /* single word dma0-2 supported */
121
    put_le16(p + 63, 0x07); /* mdma0-2 supported */
122
    put_le16(p + 64, 0x03); /* pio3-4 supported */
123 124 125 126
    put_le16(p + 65, 120);
    put_le16(p + 66, 120);
    put_le16(p + 67, 120);
    put_le16(p + 68, 120);
127 128 129 130 131 132 133

    if (s->ncq_queues) {
        put_le16(p + 75, s->ncq_queues - 1);
        /* NCQ supported */
        put_le16(p + 76, (1 << 8));
    }

134 135
    put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
    put_le16(p + 81, 0x16); /* conforms to ata5 */
136 137
    /* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */
    put_le16(p + 82, (1 << 14) | (1 << 5) | 1);
B
bellard 已提交
138 139
    /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
    put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
B
Brian Wheeler 已提交
140 141
    /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
    put_le16(p + 84, (1 << 14) | 0);
142 143 144 145 146
    /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
    if (bdrv_enable_write_cache(s->bs))
         put_le16(p + 85, (1 << 14) | (1 << 5) | 1);
    else
         put_le16(p + 85, (1 << 14) | 1);
B
bellard 已提交
147 148
    /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
    put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
B
Brian Wheeler 已提交
149 150
    /* 14=set to 1, 1=smart self test, 0=smart error logging */
    put_le16(p + 87, (1 << 14) | 0);
151 152
    put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
    put_le16(p + 93, 1 | (1 << 14) | 0x2000);
B
bellard 已提交
153 154 155 156
    put_le16(p + 100, s->nb_sectors);
    put_le16(p + 101, s->nb_sectors >> 16);
    put_le16(p + 102, s->nb_sectors >> 32);
    put_le16(p + 103, s->nb_sectors >> 48);
157 158 159
    dev = s->unit ? s->bus->slave : s->bus->master;
    if (dev && dev->conf.physical_block_size)
        put_le16(p + 106, 0x6000 | get_physical_block_exp(&dev->conf));
160 161 162

    memcpy(s->identify_data, p, sizeof(s->identify_data));
    s->identify_set = 1;
B
bellard 已提交
163 164 165 166 167 168
}

static void ide_atapi_identify(IDEState *s)
{
    uint16_t *p;

169 170 171 172 173
    if (s->identify_set) {
	memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
	return;
    }

B
bellard 已提交
174 175 176
    memset(s->io_buffer, 0, 512);
    p = (uint16_t *)s->io_buffer;
    /* Removable CDROM, 50us response, 12 byte packets */
B
bellard 已提交
177
    put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
178
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
B
bellard 已提交
179 180 181
    put_le16(p + 20, 3); /* buffer type */
    put_le16(p + 21, 512); /* cache size in sectors */
    put_le16(p + 22, 4); /* ecc bytes */
G
Gerd Hoffmann 已提交
182
    padstr((char *)(p + 23), s->version, 8); /* firmware version */
183
    padstr((char *)(p + 27), "QEMU DVD-ROM", 40); /* model */
B
bellard 已提交
184
    put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
185 186 187
#ifdef USE_DMA_CDROM
    put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
    put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
188
    put_le16(p + 62, 7);  /* single word dma0-2 supported */
189 190
    put_le16(p + 63, 7);  /* mdma0-2 supported */
#else
B
bellard 已提交
191 192 193
    put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
    put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
    put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
194
#endif
195
    put_le16(p + 64, 3); /* pio3-4 supported */
B
bellard 已提交
196 197 198 199
    put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
    put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
    put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
    put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
200

B
bellard 已提交
201 202
    put_le16(p + 71, 30); /* in ns */
    put_le16(p + 72, 30); /* in ns */
B
bellard 已提交
203

A
Alexander Graf 已提交
204 205 206 207 208 209
    if (s->ncq_queues) {
        put_le16(p + 75, s->ncq_queues - 1);
        /* NCQ supported */
        put_le16(p + 76, (1 << 8));
    }

B
bellard 已提交
210
    put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
211 212 213
#ifdef USE_DMA_CDROM
    put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
#endif
214 215
    memcpy(s->identify_data, p, sizeof(s->identify_data));
    s->identify_set = 1;
B
bellard 已提交
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static void ide_cfata_identify(IDEState *s)
{
    uint16_t *p;
    uint32_t cur_sec;

    p = (uint16_t *) s->identify_data;
    if (s->identify_set)
        goto fill_buffer;

    memset(p, 0, sizeof(s->identify_data));

    cur_sec = s->cylinders * s->heads * s->sectors;

    put_le16(p + 0, 0x848a);			/* CF Storage Card signature */
    put_le16(p + 1, s->cylinders);		/* Default cylinders */
    put_le16(p + 3, s->heads);			/* Default heads */
    put_le16(p + 6, s->sectors);		/* Default sectors per track */
    put_le16(p + 7, s->nb_sectors >> 16);	/* Sectors per card */
    put_le16(p + 8, s->nb_sectors);		/* Sectors per card */
237
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
238
    put_le16(p + 22, 0x0004);			/* ECC bytes */
G
Gerd Hoffmann 已提交
239
    padstr((char *) (p + 23), s->version, 8);	/* Firmware Revision */
T
ths 已提交
240
    padstr((char *) (p + 27), "QEMU MICRODRIVE", 40);/* Model number */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#if MAX_MULT_SECTORS > 1
    put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
#else
    put_le16(p + 47, 0x0000);
#endif
    put_le16(p + 49, 0x0f00);			/* Capabilities */
    put_le16(p + 51, 0x0002);			/* PIO cycle timing mode */
    put_le16(p + 52, 0x0001);			/* DMA cycle timing mode */
    put_le16(p + 53, 0x0003);			/* Translation params valid */
    put_le16(p + 54, s->cylinders);		/* Current cylinders */
    put_le16(p + 55, s->heads);			/* Current heads */
    put_le16(p + 56, s->sectors);		/* Current sectors */
    put_le16(p + 57, cur_sec);			/* Current capacity */
    put_le16(p + 58, cur_sec >> 16);		/* Current capacity */
    if (s->mult_sectors)			/* Multiple sector setting */
        put_le16(p + 59, 0x100 | s->mult_sectors);
    put_le16(p + 60, s->nb_sectors);		/* Total LBA sectors */
    put_le16(p + 61, s->nb_sectors >> 16);	/* Total LBA sectors */
    put_le16(p + 63, 0x0203);			/* Multiword DMA capability */
    put_le16(p + 64, 0x0001);			/* Flow Control PIO support */
    put_le16(p + 65, 0x0096);			/* Min. Multiword DMA cycle */
    put_le16(p + 66, 0x0096);			/* Rec. Multiword DMA cycle */
    put_le16(p + 68, 0x00b4);			/* Min. PIO cycle time */
    put_le16(p + 82, 0x400c);			/* Command Set supported */
    put_le16(p + 83, 0x7068);			/* Command Set supported */
    put_le16(p + 84, 0x4000);			/* Features supported */
    put_le16(p + 85, 0x000c);			/* Command Set enabled */
    put_le16(p + 86, 0x7044);			/* Command Set enabled */
    put_le16(p + 87, 0x4000);			/* Features enabled */
    put_le16(p + 91, 0x4060);			/* Current APM level */
    put_le16(p + 129, 0x0002);			/* Current features option */
    put_le16(p + 130, 0x0005);			/* Reassigned sectors */
    put_le16(p + 131, 0x0001);			/* Initial power mode */
    put_le16(p + 132, 0x0000);			/* User signature */
    put_le16(p + 160, 0x8100);			/* Power requirement */
    put_le16(p + 161, 0x8001);			/* CF command set */

    s->identify_set = 1;

fill_buffer:
    memcpy(s->io_buffer, p, sizeof(s->identify_data));
}

B
bellard 已提交
284 285 286 287 288 289
static void ide_set_signature(IDEState *s)
{
    s->select &= 0xf0; /* clear head */
    /* put signature */
    s->nsector = 1;
    s->sector = 1;
290
    if (s->drive_kind == IDE_CD) {
B
bellard 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        s->lcyl = 0x14;
        s->hcyl = 0xeb;
    } else if (s->bs) {
        s->lcyl = 0;
        s->hcyl = 0;
    } else {
        s->lcyl = 0xff;
        s->hcyl = 0xff;
    }
}

static inline void ide_abort_command(IDEState *s)
{
    s->status = READY_STAT | ERR_STAT;
    s->error = ABRT_ERR;
}

/* prepare data transfer and tell what to do after */
K
Kevin Wolf 已提交
309 310
void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                        EndTransferFunc *end_transfer_func)
B
bellard 已提交
311 312 313 314
{
    s->end_transfer_func = end_transfer_func;
    s->data_ptr = buf;
    s->data_end = buf + size;
315
    if (!(s->status & ERR_STAT)) {
316
        s->status |= DRQ_STAT;
317 318
    }
    s->bus->dma->ops->start_transfer(s->bus->dma);
B
bellard 已提交
319 320
}

K
Kevin Wolf 已提交
321
void ide_transfer_stop(IDEState *s)
B
bellard 已提交
322 323 324 325 326 327 328
{
    s->end_transfer_func = ide_transfer_stop;
    s->data_ptr = s->io_buffer;
    s->data_end = s->io_buffer;
    s->status &= ~DRQ_STAT;
}

G
Gerd Hoffmann 已提交
329
int64_t ide_get_sector(IDEState *s)
B
bellard 已提交
330 331 332 333
{
    int64_t sector_num;
    if (s->select & 0x40) {
        /* lba */
B
bellard 已提交
334 335 336 337 338 339 340 341 342 343
	if (!s->lba48) {
	    sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
		(s->lcyl << 8) | s->sector;
	} else {
	    sector_num = ((int64_t)s->hob_hcyl << 40) |
		((int64_t) s->hob_lcyl << 32) |
		((int64_t) s->hob_sector << 24) |
		((int64_t) s->hcyl << 16) |
		((int64_t) s->lcyl << 8) | s->sector;
	}
B
bellard 已提交
344 345
    } else {
        sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
B
bellard 已提交
346
            (s->select & 0x0f) * s->sectors + (s->sector - 1);
B
bellard 已提交
347 348 349 350
    }
    return sector_num;
}

G
Gerd Hoffmann 已提交
351
void ide_set_sector(IDEState *s, int64_t sector_num)
B
bellard 已提交
352 353 354
{
    unsigned int cyl, r;
    if (s->select & 0x40) {
B
bellard 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367
	if (!s->lba48) {
            s->select = (s->select & 0xf0) | (sector_num >> 24);
            s->hcyl = (sector_num >> 16);
            s->lcyl = (sector_num >> 8);
            s->sector = (sector_num);
	} else {
	    s->sector = sector_num;
	    s->lcyl = sector_num >> 8;
	    s->hcyl = sector_num >> 16;
	    s->hob_sector = sector_num >> 24;
	    s->hob_lcyl = sector_num >> 32;
	    s->hob_hcyl = sector_num >> 40;
	}
B
bellard 已提交
368 369 370 371 372
    } else {
        cyl = sector_num / (s->heads * s->sectors);
        r = sector_num % (s->heads * s->sectors);
        s->hcyl = cyl >> 8;
        s->lcyl = cyl;
373
        s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
B
bellard 已提交
374 375 376 377
        s->sector = (r % s->sectors) + 1;
    }
}

378 379
static void ide_rw_error(IDEState *s) {
    ide_abort_command(s);
380
    ide_set_irq(s->bus);
381 382
}

383
void ide_sector_read(IDEState *s)
B
bellard 已提交
384 385 386 387 388
{
    int64_t sector_num;
    int ret, n;

    s->status = READY_STAT | SEEK_STAT;
389
    s->error = 0; /* not needed by IDE spec, but needed by Windows */
B
bellard 已提交
390 391 392 393 394 395 396
    sector_num = ide_get_sector(s);
    n = s->nsector;
    if (n == 0) {
        /* no more sector to read from disk */
        ide_transfer_stop(s);
    } else {
#if defined(DEBUG_IDE)
397
        printf("read sector=%" PRId64 "\n", sector_num);
B
bellard 已提交
398 399 400 401
#endif
        if (n > s->req_nb_sectors)
            n = s->req_nb_sectors;
        ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
402
        if (ret != 0) {
K
Kevin Wolf 已提交
403 404 405 406 407
            if (ide_handle_rw_error(s, -ret,
                BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ))
            {
                return;
            }
408
        }
B
bellard 已提交
409
        ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
410
        ide_set_irq(s->bus);
B
bellard 已提交
411 412 413 414 415
        ide_set_sector(s, sector_num + n);
        s->nsector -= n;
    }
}

416 417
static void dma_buf_commit(IDEState *s, int is_write)
{
418
    qemu_sglist_destroy(&s->sg);
419 420
}

K
Kevin Wolf 已提交
421
void ide_set_inactive(IDEState *s)
K
Kevin Wolf 已提交
422
{
423 424
    s->bus->dma->aiocb = NULL;
    s->bus->dma->ops->set_inactive(s->bus->dma);
K
Kevin Wolf 已提交
425 426
}

G
Gerd Hoffmann 已提交
427
void ide_dma_error(IDEState *s)
428 429 430 431
{
    ide_transfer_stop(s);
    s->error = ABRT_ERR;
    s->status = READY_STAT | ERR_STAT;
432 433
    ide_set_inactive(s);
    s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
434
    ide_set_irq(s->bus);
435 436
}

K
Kevin Wolf 已提交
437
static int ide_handle_rw_error(IDEState *s, int error, int op)
438
{
K
Kevin Wolf 已提交
439
    int is_read = (op & BM_STATUS_RETRY_READ);
440
    BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
441

442 443
    if (action == BLOCK_ERR_IGNORE) {
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
444
        return 0;
445
    }
446 447 448

    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
            || action == BLOCK_ERR_STOP_ANY) {
449 450
        s->bus->dma->ops->set_unit(s->bus->dma, s->unit);
        s->bus->dma->ops->add_status(s->bus->dma, op);
451
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
J
Jan Kiszka 已提交
452
        vm_stop(VMSTOP_DISKFULL);
453
    } else {
K
Kevin Wolf 已提交
454
        if (op & BM_STATUS_DMA_RETRY) {
455
            dma_buf_commit(s, 0);
456
            ide_dma_error(s);
457
        } else {
458
            ide_rw_error(s);
459
        }
460
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
461 462 463 464 465
    }

    return 1;
}

466
void ide_dma_cb(void *opaque, int ret)
B
bellard 已提交
467
{
468
    IDEState *s = opaque;
469 470 471
    int n;
    int64_t sector_num;

472
handle_rw_error:
473
    if (ret < 0) {
474 475 476 477 478
        int op = BM_STATUS_DMA_RETRY;

        if (s->is_read)
            op |= BM_STATUS_RETRY_READ;
        if (ide_handle_rw_error(s, -ret, op)) {
K
Kevin Wolf 已提交
479 480
            return;
        }
481 482
    }

483 484 485
    n = s->io_buffer_size >> 9;
    sector_num = ide_get_sector(s);
    if (n > 0) {
486
        dma_buf_commit(s, s->is_read);
487 488 489 490 491 492 493
        sector_num += n;
        ide_set_sector(s, sector_num);
        s->nsector -= n;
    }

    /* end of transfer ? */
    if (s->nsector == 0) {
B
bellard 已提交
494
        s->status = READY_STAT | SEEK_STAT;
495
        ide_set_irq(s->bus);
496
        goto eot;
B
bellard 已提交
497
    }
498 499 500

    /* launch next transfer */
    n = s->nsector;
501
    s->io_buffer_index = 0;
502
    s->io_buffer_size = n * 512;
503
    if (s->bus->dma->ops->prepare_buf(s->bus->dma, s->is_read) == 0)
504
        goto eot;
505

506
#ifdef DEBUG_AIO
507 508
    printf("ide_dma_cb: sector_num=%" PRId64 " n=%d, is_read=%d\n",
           sector_num, n, s->is_read);
509
#endif
510 511 512 513 514 515 516 517

    if (s->is_read) {
        s->bus->dma->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num,
                                           ide_dma_cb, s);
    } else {
        s->bus->dma->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num,
                                            ide_dma_cb, s);
    }
518 519 520 521 522

    if (!s->bus->dma->aiocb) {
        ret = -1;
        goto handle_rw_error;
    }
523 524 525 526 527
    return;

eot:
   s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
   ide_set_inactive(s);
B
bellard 已提交
528 529
}

530
static void ide_sector_start_dma(IDEState *s, int is_read)
B
bellard 已提交
531
{
532
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
B
bellard 已提交
533 534
    s->io_buffer_index = 0;
    s->io_buffer_size = 0;
535 536
    s->is_read = is_read;
    s->bus->dma->ops->start_dma(s->bus->dma, s, ide_dma_cb);
B
bellard 已提交
537 538
}

539 540 541
static void ide_sector_write_timer_cb(void *opaque)
{
    IDEState *s = opaque;
542
    ide_set_irq(s->bus);
543 544
}

545
void ide_sector_write(IDEState *s)
B
bellard 已提交
546 547
{
    int64_t sector_num;
548
    int ret, n, n1;
B
bellard 已提交
549 550 551 552

    s->status = READY_STAT | SEEK_STAT;
    sector_num = ide_get_sector(s);
#if defined(DEBUG_IDE)
553
    printf("write sector=%" PRId64 "\n", sector_num);
B
bellard 已提交
554 555 556 557
#endif
    n = s->nsector;
    if (n > s->req_nb_sectors)
        n = s->req_nb_sectors;
558
    ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
559

560
    if (ret != 0) {
K
Kevin Wolf 已提交
561
        if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY))
562
            return;
563 564
    }

B
bellard 已提交
565 566
    s->nsector -= n;
    if (s->nsector == 0) {
T
ths 已提交
567
        /* no more sectors to write */
B
bellard 已提交
568 569 570 571 572 573 574 575
        ide_transfer_stop(s);
    } else {
        n1 = s->nsector;
        if (n1 > s->req_nb_sectors)
            n1 = s->req_nb_sectors;
        ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
    }
    ide_set_sector(s, sector_num + n);
576

577 578 579 580 581 582 583
    if (win2k_install_hack && ((++s->irq_count % 16) == 0)) {
        /* It seems there is a bug in the Windows 2000 installer HDD
           IDE driver which fills the disk with empty logs when the
           IDE write IRQ comes too early. This hack tries to correct
           that at the expense of slower write performances. Use this
           option _only_ to install Windows 2000. You must disable it
           for normal use. */
B
Blue Swirl 已提交
584
        qemu_mod_timer(s->sector_write_timer,
585
                       qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 1000));
B
Blue Swirl 已提交
586
    } else {
587
        ide_set_irq(s->bus);
588
    }
B
bellard 已提交
589 590
}

C
Christoph Hellwig 已提交
591 592 593 594
static void ide_flush_cb(void *opaque, int ret)
{
    IDEState *s = opaque;

K
Kevin Wolf 已提交
595 596 597 598 599 600
    if (ret < 0) {
        /* XXX: What sector number to set here? */
        if (ide_handle_rw_error(s, -ret, BM_STATUS_RETRY_FLUSH)) {
            return;
        }
    }
C
Christoph Hellwig 已提交
601 602 603 604 605

    s->status = READY_STAT | SEEK_STAT;
    ide_set_irq(s->bus);
}

606
void ide_flush_cache(IDEState *s)
K
Kevin Wolf 已提交
607
{
608 609 610
    BlockDriverAIOCB *acb;

    if (s->bs == NULL) {
K
Kevin Wolf 已提交
611
        ide_flush_cb(s, 0);
612 613 614 615 616 617
        return;
    }

    acb = bdrv_aio_flush(s->bs, ide_flush_cb, s);
    if (acb == NULL) {
        ide_flush_cb(s, -EIO);
K
Kevin Wolf 已提交
618 619 620
    }
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static void ide_cfata_metadata_inquiry(IDEState *s)
{
    uint16_t *p;
    uint32_t spd;

    p = (uint16_t *) s->io_buffer;
    memset(p, 0, 0x200);
    spd = ((s->mdata_size - 1) >> 9) + 1;

    put_le16(p + 0, 0x0001);			/* Data format revision */
    put_le16(p + 1, 0x0000);			/* Media property: silicon */
    put_le16(p + 2, s->media_changed);		/* Media status */
    put_le16(p + 3, s->mdata_size & 0xffff);	/* Capacity in bytes (low) */
    put_le16(p + 4, s->mdata_size >> 16);	/* Capacity in bytes (high) */
    put_le16(p + 5, spd & 0xffff);		/* Sectors per device (low) */
    put_le16(p + 6, spd >> 16);			/* Sectors per device (high) */
}

static void ide_cfata_metadata_read(IDEState *s)
{
    uint16_t *p;

    if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
        s->status = ERR_STAT;
        s->error = ABRT_ERR;
        return;
    }

    p = (uint16_t *) s->io_buffer;
    memset(p, 0, 0x200);

    put_le16(p + 0, s->media_changed);		/* Media status */
    memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
                    MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
                                    s->nsector << 9), 0x200 - 2));
}

static void ide_cfata_metadata_write(IDEState *s)
{
    if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
        s->status = ERR_STAT;
        s->error = ABRT_ERR;
        return;
    }

    s->media_changed = 0;

    memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
                    s->io_buffer + 2,
                    MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
                                    s->nsector << 9), 0x200 - 2));
}

T
ths 已提交
674
/* called when the inserted state of the media has changed */
675
static void cdrom_change_cb(void *opaque, int reason)
T
ths 已提交
676 677
{
    IDEState *s = opaque;
678
    uint64_t nb_sectors;
T
ths 已提交
679

680 681 682 683
    if (!(reason & CHANGE_MEDIA)) {
        return;
    }

T
ths 已提交
684 685
    bdrv_get_geometry(s->bs, &nb_sectors);
    s->nb_sectors = nb_sectors;
686

687 688 689 690 691 692 693
    /*
     * First indicate to the guest that a CD has been removed.  That's
     * done on the next command the guest sends us.
     *
     * Then we set SENSE_UNIT_ATTENTION, by which the guest will
     * detect a new CD in the drive.  See ide_atapi_cmd() for details.
     */
G
Gleb Natapov 已提交
694
    s->cdrom_changed = 1;
695
    s->events.new_media = true;
696
    ide_set_irq(s->bus);
T
ths 已提交
697 698
}

B
bellard 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static void ide_cmd_lba48_transform(IDEState *s, int lba48)
{
    s->lba48 = lba48;

    /* handle the 'magic' 0 nsector count conversion here. to avoid
     * fiddling with the rest of the read logic, we just store the
     * full sector count in ->nsector and ignore ->hob_nsector from now
     */
    if (!s->lba48) {
	if (!s->nsector)
	    s->nsector = 256;
    } else {
	if (!s->nsector && !s->hob_nsector)
	    s->nsector = 65536;
	else {
	    int lo = s->nsector;
	    int hi = s->hob_nsector;

	    s->nsector = (hi << 8) | lo;
	}
    }
}

G
Gerd Hoffmann 已提交
722
static void ide_clear_hob(IDEBus *bus)
B
bellard 已提交
723 724
{
    /* any write clears HOB high bit of device control register */
G
Gerd Hoffmann 已提交
725 726
    bus->ifs[0].select &= ~(1 << 7);
    bus->ifs[1].select &= ~(1 << 7);
B
bellard 已提交
727 728
}

G
Gerd Hoffmann 已提交
729
void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
730
{
G
Gerd Hoffmann 已提交
731
    IDEBus *bus = opaque;
B
bellard 已提交
732 733 734 735

#ifdef DEBUG_IDE
    printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
#endif
B
bellard 已提交
736

B
bellard 已提交
737
    addr &= 7;
738 739

    /* ignore writes to command block while busy with previous command */
G
Gerd Hoffmann 已提交
740
    if (addr != 7 && (idebus_active_if(bus)->status & (BUSY_STAT|DRQ_STAT)))
741 742
        return;

B
bellard 已提交
743 744 745 746
    switch(addr) {
    case 0:
        break;
    case 1:
G
Gerd Hoffmann 已提交
747
	ide_clear_hob(bus);
748
        /* NOTE: data is written to the two drives */
G
Gerd Hoffmann 已提交
749 750 751 752
	bus->ifs[0].hob_feature = bus->ifs[0].feature;
	bus->ifs[1].hob_feature = bus->ifs[1].feature;
        bus->ifs[0].feature = val;
        bus->ifs[1].feature = val;
B
bellard 已提交
753 754
        break;
    case 2:
G
Gerd Hoffmann 已提交
755 756 757 758 759
	ide_clear_hob(bus);
	bus->ifs[0].hob_nsector = bus->ifs[0].nsector;
	bus->ifs[1].hob_nsector = bus->ifs[1].nsector;
        bus->ifs[0].nsector = val;
        bus->ifs[1].nsector = val;
B
bellard 已提交
760 761
        break;
    case 3:
G
Gerd Hoffmann 已提交
762 763 764 765 766
	ide_clear_hob(bus);
	bus->ifs[0].hob_sector = bus->ifs[0].sector;
	bus->ifs[1].hob_sector = bus->ifs[1].sector;
        bus->ifs[0].sector = val;
        bus->ifs[1].sector = val;
B
bellard 已提交
767 768
        break;
    case 4:
G
Gerd Hoffmann 已提交
769 770 771 772 773
	ide_clear_hob(bus);
	bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl;
	bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl;
        bus->ifs[0].lcyl = val;
        bus->ifs[1].lcyl = val;
B
bellard 已提交
774 775
        break;
    case 5:
G
Gerd Hoffmann 已提交
776 777 778 779 780
	ide_clear_hob(bus);
	bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl;
	bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl;
        bus->ifs[0].hcyl = val;
        bus->ifs[1].hcyl = val;
B
bellard 已提交
781 782
        break;
    case 6:
B
bellard 已提交
783
	/* FIXME: HOB readback uses bit 7 */
G
Gerd Hoffmann 已提交
784 785
        bus->ifs[0].select = (val & ~0x10) | 0xa0;
        bus->ifs[1].select = (val | 0x10) | 0xa0;
B
bellard 已提交
786
        /* select drive */
G
Gerd Hoffmann 已提交
787
        bus->unit = (val >> 4) & 1;
B
bellard 已提交
788 789 790 791
        break;
    default:
    case 7:
        /* command */
792 793 794 795 796 797 798 799 800 801 802 803
        ide_exec_cmd(bus, val);
        break;
    }
}


void ide_exec_cmd(IDEBus *bus, uint32_t val)
{
    IDEState *s;
    int n;
    int lba48 = 0;

B
bellard 已提交
804
#if defined(DEBUG_IDE)
805
    printf("ide: CMD=%02x\n", val);
B
bellard 已提交
806
#endif
807 808 809 810
    s = idebus_active_if(bus);
    /* ignore commands to non existant slave */
    if (s != bus->ifs && !s->bs)
        return;
B
bellard 已提交
811

812 813 814
    /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
    if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
        return;
815

816 817 818 819 820 821 822
    switch(val) {
    case WIN_IDENTIFY:
        if (s->bs && s->drive_kind != IDE_CD) {
            if (s->drive_kind != IDE_CFATA)
                ide_identify(s);
            else
                ide_cfata_identify(s);
B
bellard 已提交
823
            s->status = READY_STAT | SEEK_STAT;
824 825 826 827
            ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
        } else {
            if (s->drive_kind == IDE_CD) {
                ide_set_signature(s);
B
bellard 已提交
828
            }
829 830 831 832 833 834 835 836 837 838 839 840 841 842
            ide_abort_command(s);
        }
        ide_set_irq(s->bus);
        break;
    case WIN_SPECIFY:
    case WIN_RECAL:
        s->error = 0;
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
    case WIN_SETMULT:
        if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
            /* Disable Read and Write Multiple */
            s->mult_sectors = 0;
A
aliguori 已提交
843
            s->status = READY_STAT | SEEK_STAT;
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        } else if ((s->nsector & 0xff) != 0 &&
            ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
             (s->nsector & (s->nsector - 1)) != 0)) {
            ide_abort_command(s);
        } else {
            s->mult_sectors = s->nsector & 0xff;
            s->status = READY_STAT | SEEK_STAT;
        }
        ide_set_irq(s->bus);
        break;
    case WIN_VERIFY_EXT:
	lba48 = 1;
    case WIN_VERIFY:
    case WIN_VERIFY_ONCE:
        /* do sector number check ? */
	ide_cmd_lba48_transform(s, lba48);
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
B
bellard 已提交
863
	case WIN_READ_EXT:
864 865 866 867 868 869 870 871 872
	lba48 = 1;
    case WIN_READ:
    case WIN_READ_ONCE:
        if (!s->bs)
            goto abort_cmd;
	ide_cmd_lba48_transform(s, lba48);
        s->req_nb_sectors = 1;
        ide_sector_read(s);
        break;
B
bellard 已提交
873
	case WIN_WRITE_EXT:
874 875 876 877 878 879 880 881 882 883 884 885
	lba48 = 1;
    case WIN_WRITE:
    case WIN_WRITE_ONCE:
    case CFA_WRITE_SECT_WO_ERASE:
    case WIN_WRITE_VERIFY:
	ide_cmd_lba48_transform(s, lba48);
        s->error = 0;
        s->status = SEEK_STAT | READY_STAT;
        s->req_nb_sectors = 1;
        ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
        s->media_changed = 1;
        break;
B
bellard 已提交
886
	case WIN_MULTREAD_EXT:
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
	lba48 = 1;
    case WIN_MULTREAD:
        if (!s->mult_sectors)
            goto abort_cmd;
	ide_cmd_lba48_transform(s, lba48);
        s->req_nb_sectors = s->mult_sectors;
        ide_sector_read(s);
        break;
    case WIN_MULTWRITE_EXT:
	lba48 = 1;
    case WIN_MULTWRITE:
    case CFA_WRITE_MULTI_WO_ERASE:
        if (!s->mult_sectors)
            goto abort_cmd;
	ide_cmd_lba48_transform(s, lba48);
        s->error = 0;
        s->status = SEEK_STAT | READY_STAT;
        s->req_nb_sectors = s->mult_sectors;
        n = s->nsector;
        if (n > s->req_nb_sectors)
            n = s->req_nb_sectors;
        ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
        s->media_changed = 1;
        break;
B
bellard 已提交
911
	case WIN_READDMA_EXT:
912 913 914 915 916 917
	lba48 = 1;
    case WIN_READDMA:
    case WIN_READDMA_ONCE:
        if (!s->bs)
            goto abort_cmd;
	ide_cmd_lba48_transform(s, lba48);
918
        ide_sector_start_dma(s, 1);
919
        break;
B
bellard 已提交
920
	case WIN_WRITEDMA_EXT:
921 922 923 924 925 926
	lba48 = 1;
    case WIN_WRITEDMA:
    case WIN_WRITEDMA_ONCE:
        if (!s->bs)
            goto abort_cmd;
	ide_cmd_lba48_transform(s, lba48);
927
        ide_sector_start_dma(s, 0);
928 929 930 931 932 933 934 935 936 937 938 939
        s->media_changed = 1;
        break;
    case WIN_READ_NATIVE_MAX_EXT:
	lba48 = 1;
    case WIN_READ_NATIVE_MAX:
	ide_cmd_lba48_transform(s, lba48);
        ide_set_sector(s, s->nb_sectors - 1);
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
    case WIN_CHECKPOWERMODE1:
    case WIN_CHECKPOWERMODE2:
940
        s->error = 0;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
        s->nsector = 0xff; /* device active or idle */
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
    case WIN_SETFEATURES:
        if (!s->bs)
            goto abort_cmd;
        /* XXX: valid for CDROM ? */
        switch(s->feature) {
        case 0xcc: /* reverting to power-on defaults enable */
        case 0x66: /* reverting to power-on defaults disable */
        case 0x02: /* write cache enable */
        case 0x82: /* write cache disable */
        case 0xaa: /* read look-ahead enable */
        case 0x55: /* read look-ahead disable */
        case 0x05: /* set advanced power management mode */
        case 0x85: /* disable advanced power management mode */
        case 0x69: /* NOP */
        case 0x67: /* NOP */
        case 0x96: /* NOP */
        case 0x9a: /* NOP */
        case 0x42: /* enable Automatic Acoustic Mode */
        case 0xc2: /* disable Automatic Acoustic Mode */
A
aliguori 已提交
964
            s->status = READY_STAT | SEEK_STAT;
965
            ide_set_irq(s->bus);
966
            break;
967
        case 0x03: { /* set transfer mode */
968
		uint8_t val = s->nsector & 0x07;
969
            uint16_t *identify_data = (uint16_t *)s->identify_data;
970 971

		switch (s->nsector >> 3) {
972 973
		case 0x00: /* pio default */
		case 0x01: /* pio mode */
974 975 976
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f);
977
			break;
978
                case 0x02: /* sigle word dma mode*/
979 980 981
			put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f);
982
			break;
983
		case 0x04: /* mdma mode */
984 985 986
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
			put_le16(identify_data + 88,0x3f);
987
			break;
988
		case 0x08: /* udma mode */
989 990 991
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
992
			break;
993
		default:
994 995
			goto abort_cmd;
		}
A
aurel32 已提交
996
            s->status = READY_STAT | SEEK_STAT;
997
            ide_set_irq(s->bus);
A
aurel32 已提交
998
            break;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	}
        default:
            goto abort_cmd;
        }
        break;
    case WIN_FLUSH_CACHE:
    case WIN_FLUSH_CACHE_EXT:
        ide_flush_cache(s);
        break;
    case WIN_STANDBY:
    case WIN_STANDBY2:
    case WIN_STANDBYNOW1:
    case WIN_STANDBYNOW2:
    case WIN_IDLEIMMEDIATE:
    case CFA_IDLEIMMEDIATE:
    case WIN_SETIDLE1:
    case WIN_SETIDLE2:
    case WIN_SLEEPNOW1:
    case WIN_SLEEPNOW2:
        s->status = READY_STAT;
        ide_set_irq(s->bus);
        break;
    case WIN_SEEK:
        if(s->drive_kind == IDE_CD)
            goto abort_cmd;
        /* XXX: Check that seek is within bounds */
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
        /* ATAPI commands */
    case WIN_PIDENTIFY:
        if (s->drive_kind == IDE_CD) {
            ide_atapi_identify(s);
A
aliguori 已提交
1032
            s->status = READY_STAT | SEEK_STAT;
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
        } else {
            ide_abort_command(s);
        }
        ide_set_irq(s->bus);
        break;
    case WIN_DIAGNOSE:
        ide_set_signature(s);
        if (s->drive_kind == IDE_CD)
            s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
                            * devices to return a clear status register
                            * with READY_STAT *not* set. */
        else
A
aliguori 已提交
1046
            s->status = READY_STAT | SEEK_STAT;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
        s->error = 0x01; /* Device 0 passed, Device 1 passed or not
                          * present.
                          */
        ide_set_irq(s->bus);
        break;
    case WIN_SRST:
        if (s->drive_kind != IDE_CD)
            goto abort_cmd;
        ide_set_signature(s);
        s->status = 0x00; /* NOTE: READY is _not_ set */
        s->error = 0x01;
        break;
    case WIN_PACKETCMD:
        if (s->drive_kind != IDE_CD)
            goto abort_cmd;
        /* overlapping commands not supported */
        if (s->feature & 0x02)
            goto abort_cmd;
        s->status = READY_STAT | SEEK_STAT;
        s->atapi_dma = s->feature & 1;
        s->nsector = 1;
        ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
                           ide_atapi_cmd);
        break;
    /* CF-ATA commands */
    case CFA_REQ_EXT_ERROR_CODE:
        if (s->drive_kind != IDE_CFATA)
            goto abort_cmd;
        s->error = 0x09;    /* miscellaneous error */
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
    case CFA_ERASE_SECTORS:
    case CFA_WEAR_LEVEL:
        if (s->drive_kind != IDE_CFATA)
            goto abort_cmd;
        if (val == CFA_WEAR_LEVEL)
            s->nsector = 0;
        if (val == CFA_ERASE_SECTORS)
            s->media_changed = 1;
        s->error = 0x00;
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
    case CFA_TRANSLATE_SECTOR:
        if (s->drive_kind != IDE_CFATA)
            goto abort_cmd;
        s->error = 0x00;
        s->status = READY_STAT | SEEK_STAT;
        memset(s->io_buffer, 0, 0x200);
        s->io_buffer[0x00] = s->hcyl;			/* Cyl MSB */
        s->io_buffer[0x01] = s->lcyl;			/* Cyl LSB */
        s->io_buffer[0x02] = s->select;			/* Head */
        s->io_buffer[0x03] = s->sector;			/* Sector */
        s->io_buffer[0x04] = ide_get_sector(s) >> 16;	/* LBA MSB */
        s->io_buffer[0x05] = ide_get_sector(s) >> 8;	/* LBA */
        s->io_buffer[0x06] = ide_get_sector(s) >> 0;	/* LBA LSB */
        s->io_buffer[0x13] = 0x00;				/* Erase flag */
        s->io_buffer[0x18] = 0x00;				/* Hot count */
        s->io_buffer[0x19] = 0x00;				/* Hot count */
        s->io_buffer[0x1a] = 0x01;				/* Hot count */
        ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
        ide_set_irq(s->bus);
        break;
    case CFA_ACCESS_METADATA_STORAGE:
        if (s->drive_kind != IDE_CFATA)
            goto abort_cmd;
        switch (s->feature) {
        case 0x02:	/* Inquiry Metadata Storage */
            ide_cfata_metadata_inquiry(s);
1117
            break;
1118 1119
        case 0x03:	/* Read Metadata Storage */
            ide_cfata_metadata_read(s);
1120
            break;
1121 1122
        case 0x04:	/* Write Metadata Storage */
            ide_cfata_metadata_write(s);
1123
            break;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        default:
            goto abort_cmd;
        }
        ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
        s->status = 0x00; /* NOTE: READY is _not_ set */
        ide_set_irq(s->bus);
        break;
    case IBM_SENSE_CONDITION:
        if (s->drive_kind != IDE_CFATA)
            goto abort_cmd;
        switch (s->feature) {
        case 0x01:  /* sense temperature in device */
            s->nsector = 0x50;      /* +20 C */
1137
            break;
1138 1139 1140 1141 1142 1143
        default:
            goto abort_cmd;
        }
        s->status = READY_STAT | SEEK_STAT;
        ide_set_irq(s->bus);
        break;
B
Brian Wheeler 已提交
1144 1145

	case WIN_SMART:
1146
	if (s->drive_kind == IDE_CD)
B
Brian Wheeler 已提交
1147
		goto abort_cmd;
1148
	if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
B
Brian Wheeler 已提交
1149
		goto abort_cmd;
1150
	if (!s->smart_enabled && s->feature != SMART_ENABLE)
B
Brian Wheeler 已提交
1151
		goto abort_cmd;
1152 1153
	switch (s->feature) {
	case SMART_DISABLE:
B
Brian Wheeler 已提交
1154 1155
		s->smart_enabled = 0;
		s->status = READY_STAT | SEEK_STAT;
1156
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1157
		break;
1158
	case SMART_ENABLE:
B
Brian Wheeler 已提交
1159 1160
		s->smart_enabled = 1;
		s->status = READY_STAT | SEEK_STAT;
1161
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1162
		break;
1163
	case SMART_ATTR_AUTOSAVE:
B
Brian Wheeler 已提交
1164 1165
		switch (s->sector) {
		case 0x00:
1166 1167
		s->smart_autosave = 0;
		break;
B
Brian Wheeler 已提交
1168
		case 0xf1:
1169 1170
		s->smart_autosave = 1;
		break;
B
Brian Wheeler 已提交
1171
		default:
1172
		goto abort_cmd;
B
Brian Wheeler 已提交
1173 1174
		}
		s->status = READY_STAT | SEEK_STAT;
1175
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1176
		break;
1177
	case SMART_STATUS:
B
Brian Wheeler 已提交
1178
		if (!s->smart_errors) {
1179 1180
		s->hcyl = 0xc2;
		s->lcyl = 0x4f;
B
Brian Wheeler 已提交
1181
		} else {
1182 1183
		s->hcyl = 0x2c;
		s->lcyl = 0xf4;
B
Brian Wheeler 已提交
1184 1185
		}
		s->status = READY_STAT | SEEK_STAT;
1186
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1187
		break;
1188
	case SMART_READ_THRESH:
B
Brian Wheeler 已提交
1189 1190 1191
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01; /* smart struct version */
		for (n=0; n<30; n++) {
1192
		if (smart_attributes[n][0] == 0)
B
Brian Wheeler 已提交
1193
			break;
1194
		s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
1195
		s->io_buffer[2+1+(n*12)] = smart_attributes[n][11];
B
Brian Wheeler 已提交
1196 1197
		}
		for (n=0; n<511; n++) /* checksum */
1198
		s->io_buffer[511] += s->io_buffer[n];
B
Brian Wheeler 已提交
1199 1200 1201
		s->io_buffer[511] = 0x100 - s->io_buffer[511];
		s->status = READY_STAT | SEEK_STAT;
		ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1202
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1203
		break;
1204
	case SMART_READ_DATA:
B
Brian Wheeler 已提交
1205 1206 1207
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01; /* smart struct version */
		for (n=0; n<30; n++) {
1208
		    if (smart_attributes[n][0] == 0) {
B
Brian Wheeler 已提交
1209
			break;
1210 1211 1212 1213 1214
		    }
		    int i;
		    for(i = 0; i < 11; i++) {
			s->io_buffer[2+i+(n*12)] = smart_attributes[n][i];
		    }
B
Brian Wheeler 已提交
1215 1216 1217
		}
		s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
		if (s->smart_selftest_count == 0) {
1218
		s->io_buffer[363] = 0;
B
Brian Wheeler 已提交
1219
		} else {
1220
		s->io_buffer[363] =
B
Brian Wheeler 已提交
1221
			s->smart_selftest_data[3 + 
1222 1223
					   (s->smart_selftest_count - 1) *
					   24];
B
Brian Wheeler 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
		}
		s->io_buffer[364] = 0x20; 
		s->io_buffer[365] = 0x01; 
		/* offline data collection capacity: execute + self-test*/
		s->io_buffer[367] = (1<<4 | 1<<3 | 1); 
		s->io_buffer[368] = 0x03; /* smart capability (1) */
		s->io_buffer[369] = 0x00; /* smart capability (2) */
		s->io_buffer[370] = 0x01; /* error logging supported */
		s->io_buffer[372] = 0x02; /* minutes for poll short test */
		s->io_buffer[373] = 0x36; /* minutes for poll ext test */
		s->io_buffer[374] = 0x01; /* minutes for poll conveyance */

		for (n=0; n<511; n++) 
1237
		s->io_buffer[511] += s->io_buffer[n];
B
Brian Wheeler 已提交
1238 1239 1240
		s->io_buffer[511] = 0x100 - s->io_buffer[511];
		s->status = READY_STAT | SEEK_STAT;
		ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1241
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1242
		break;
1243
	case SMART_READ_LOG:
B
Brian Wheeler 已提交
1244 1245
		switch (s->sector) {
		case 0x01: /* summary smart error log */
1246 1247 1248 1249 1250
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01;
		s->io_buffer[1] = 0x00; /* no error entries */
		s->io_buffer[452] = s->smart_errors & 0xff;
		s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
B
Brian Wheeler 已提交
1251

1252
		for (n=0; n<511; n++)
B
Brian Wheeler 已提交
1253
			s->io_buffer[511] += s->io_buffer[n];
1254 1255
		s->io_buffer[511] = 0x100 - s->io_buffer[511];
		break;
B
Brian Wheeler 已提交
1256
		case 0x06: /* smart self test log */
1257 1258 1259
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01;
		if (s->smart_selftest_count == 0) {
B
Brian Wheeler 已提交
1260
			s->io_buffer[508] = 0;
1261
		} else {
B
Brian Wheeler 已提交
1262 1263
			s->io_buffer[508] = s->smart_selftest_count;
			for (n=2; n<506; n++) 
1264 1265 1266
			s->io_buffer[n] = s->smart_selftest_data[n];
		}
		for (n=0; n<511; n++)
B
Brian Wheeler 已提交
1267
			s->io_buffer[511] += s->io_buffer[n];
1268 1269
		s->io_buffer[511] = 0x100 - s->io_buffer[511];
		break;
B
Brian Wheeler 已提交
1270
		default:
1271
		goto abort_cmd;
B
Brian Wheeler 已提交
1272 1273 1274
		}
		s->status = READY_STAT | SEEK_STAT;
		ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1275
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
1276
		break;
1277
	case SMART_EXECUTE_OFFLINE:
B
Brian Wheeler 已提交
1278 1279 1280 1281
		switch (s->sector) {
		case 0: /* off-line routine */
		case 1: /* short self test */
		case 2: /* extended self test */
1282 1283
		s->smart_selftest_count++;
		if(s->smart_selftest_count > 21)
B
Brian Wheeler 已提交
1284
			s->smart_selftest_count = 0;
1285 1286 1287 1288 1289 1290 1291 1292
		n = 2 + (s->smart_selftest_count - 1) * 24;
		s->smart_selftest_data[n] = s->sector;
		s->smart_selftest_data[n+1] = 0x00; /* OK and finished */
		s->smart_selftest_data[n+2] = 0x34; /* hour count lsb */
		s->smart_selftest_data[n+3] = 0x12; /* hour count msb */
		s->status = READY_STAT | SEEK_STAT;
		ide_set_irq(s->bus);
		break;
B
Brian Wheeler 已提交
1293
		default:
1294
		goto abort_cmd;
B
Brian Wheeler 已提交
1295 1296
		}
		break;
1297
	default:
B
Brian Wheeler 已提交
1298
		goto abort_cmd;
1299 1300 1301 1302 1303 1304 1305 1306
	}
	break;
    default:
    abort_cmd:
        ide_abort_command(s);
        ide_set_irq(s->bus);
        break;
    }
B
bellard 已提交
1307 1308
}

G
Gerd Hoffmann 已提交
1309
uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
B
bellard 已提交
1310
{
G
Gerd Hoffmann 已提交
1311 1312
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1313
    uint32_t addr;
B
bellard 已提交
1314
    int ret, hob;
B
bellard 已提交
1315 1316

    addr = addr1 & 7;
B
bellard 已提交
1317 1318 1319
    /* FIXME: HOB readback uses bit 7, but it's always set right now */
    //hob = s->select & (1 << 7);
    hob = 0;
B
bellard 已提交
1320 1321 1322 1323 1324
    switch(addr) {
    case 0:
        ret = 0xff;
        break;
    case 1:
G
Gerd Hoffmann 已提交
1325 1326
        if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
            (s != bus->ifs && !s->bs))
1327
            ret = 0;
B
bellard 已提交
1328
        else if (!hob)
1329
            ret = s->error;
B
bellard 已提交
1330 1331
	else
	    ret = s->hob_feature;
B
bellard 已提交
1332 1333
        break;
    case 2:
G
Gerd Hoffmann 已提交
1334
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
1335
            ret = 0;
B
bellard 已提交
1336
        else if (!hob)
1337
            ret = s->nsector & 0xff;
B
bellard 已提交
1338 1339
	else
	    ret = s->hob_nsector;
B
bellard 已提交
1340 1341
        break;
    case 3:
G
Gerd Hoffmann 已提交
1342
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
1343
            ret = 0;
B
bellard 已提交
1344
        else if (!hob)
1345
            ret = s->sector;
B
bellard 已提交
1346 1347
	else
	    ret = s->hob_sector;
B
bellard 已提交
1348 1349
        break;
    case 4:
G
Gerd Hoffmann 已提交
1350
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
1351
            ret = 0;
B
bellard 已提交
1352
        else if (!hob)
1353
            ret = s->lcyl;
B
bellard 已提交
1354 1355
	else
	    ret = s->hob_lcyl;
B
bellard 已提交
1356 1357
        break;
    case 5:
G
Gerd Hoffmann 已提交
1358
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
1359
            ret = 0;
B
bellard 已提交
1360
        else if (!hob)
1361
            ret = s->hcyl;
B
bellard 已提交
1362 1363
	else
	    ret = s->hob_hcyl;
B
bellard 已提交
1364 1365
        break;
    case 6:
G
Gerd Hoffmann 已提交
1366
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
1367 1368
            ret = 0;
        else
B
bellard 已提交
1369
            ret = s->select;
B
bellard 已提交
1370 1371 1372
        break;
    default:
    case 7:
G
Gerd Hoffmann 已提交
1373 1374
        if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
            (s != bus->ifs && !s->bs))
1375 1376 1377
            ret = 0;
        else
            ret = s->status;
1378
        qemu_irq_lower(bus->irq);
B
bellard 已提交
1379 1380 1381 1382 1383 1384 1385 1386
        break;
    }
#ifdef DEBUG_IDE
    printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
#endif
    return ret;
}

G
Gerd Hoffmann 已提交
1387
uint32_t ide_status_read(void *opaque, uint32_t addr)
B
bellard 已提交
1388
{
G
Gerd Hoffmann 已提交
1389 1390
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1391
    int ret;
B
bellard 已提交
1392

G
Gerd Hoffmann 已提交
1393 1394
    if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
        (s != bus->ifs && !s->bs))
B
bellard 已提交
1395 1396 1397
        ret = 0;
    else
        ret = s->status;
B
bellard 已提交
1398 1399 1400 1401 1402 1403
#ifdef DEBUG_IDE
    printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
#endif
    return ret;
}

G
Gerd Hoffmann 已提交
1404
void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
1405
{
G
Gerd Hoffmann 已提交
1406
    IDEBus *bus = opaque;
B
bellard 已提交
1407 1408 1409 1410 1411 1412 1413
    IDEState *s;
    int i;

#ifdef DEBUG_IDE
    printf("ide: write control addr=0x%x val=%02x\n", addr, val);
#endif
    /* common for both drives */
1414
    if (!(bus->cmd & IDE_CMD_RESET) &&
B
bellard 已提交
1415 1416 1417
        (val & IDE_CMD_RESET)) {
        /* reset low to high */
        for(i = 0;i < 2; i++) {
G
Gerd Hoffmann 已提交
1418
            s = &bus->ifs[i];
B
bellard 已提交
1419 1420 1421
            s->status = BUSY_STAT | SEEK_STAT;
            s->error = 0x01;
        }
1422
    } else if ((bus->cmd & IDE_CMD_RESET) &&
B
bellard 已提交
1423 1424 1425
               !(val & IDE_CMD_RESET)) {
        /* high to low */
        for(i = 0;i < 2; i++) {
G
Gerd Hoffmann 已提交
1426
            s = &bus->ifs[i];
1427
            if (s->drive_kind == IDE_CD)
B
bellard 已提交
1428 1429
                s->status = 0x00; /* NOTE: READY is _not_ set */
            else
1430
                s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
1431 1432 1433 1434
            ide_set_signature(s);
        }
    }

1435
    bus->cmd = val;
B
bellard 已提交
1436 1437
}

G
Gerd Hoffmann 已提交
1438
void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
1439
{
G
Gerd Hoffmann 已提交
1440 1441
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1442 1443
    uint8_t *p;

1444 1445 1446 1447
    /* PIO data access allowed only when DRQ bit is set */
    if (!(s->status & DRQ_STAT))
        return;

B
bellard 已提交
1448
    p = s->data_ptr;
B
bellard 已提交
1449
    *(uint16_t *)p = le16_to_cpu(val);
B
bellard 已提交
1450 1451 1452 1453 1454 1455
    p += 2;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
}

G
Gerd Hoffmann 已提交
1456
uint32_t ide_data_readw(void *opaque, uint32_t addr)
B
bellard 已提交
1457
{
G
Gerd Hoffmann 已提交
1458 1459
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1460 1461
    uint8_t *p;
    int ret;
1462 1463 1464 1465 1466

    /* PIO data access allowed only when DRQ bit is set */
    if (!(s->status & DRQ_STAT))
        return 0;

B
bellard 已提交
1467
    p = s->data_ptr;
B
bellard 已提交
1468
    ret = cpu_to_le16(*(uint16_t *)p);
B
bellard 已提交
1469 1470 1471 1472 1473 1474 1475
    p += 2;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
    return ret;
}

G
Gerd Hoffmann 已提交
1476
void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
1477
{
G
Gerd Hoffmann 已提交
1478 1479
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1480 1481
    uint8_t *p;

1482 1483 1484 1485
    /* PIO data access allowed only when DRQ bit is set */
    if (!(s->status & DRQ_STAT))
        return;

B
bellard 已提交
1486
    p = s->data_ptr;
B
bellard 已提交
1487
    *(uint32_t *)p = le32_to_cpu(val);
B
bellard 已提交
1488 1489 1490 1491 1492 1493
    p += 4;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
}

G
Gerd Hoffmann 已提交
1494
uint32_t ide_data_readl(void *opaque, uint32_t addr)
B
bellard 已提交
1495
{
G
Gerd Hoffmann 已提交
1496 1497
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
1498 1499
    uint8_t *p;
    int ret;
1500

1501 1502 1503 1504
    /* PIO data access allowed only when DRQ bit is set */
    if (!(s->status & DRQ_STAT))
        return 0;

B
bellard 已提交
1505
    p = s->data_ptr;
B
bellard 已提交
1506
    ret = cpu_to_le32(*(uint32_t *)p);
B
bellard 已提交
1507 1508 1509 1510 1511 1512 1513
    p += 4;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
    return ret;
}

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
static void ide_dummy_transfer_stop(IDEState *s)
{
    s->data_ptr = s->io_buffer;
    s->data_end = s->io_buffer;
    s->io_buffer[0] = 0xff;
    s->io_buffer[1] = 0xff;
    s->io_buffer[2] = 0xff;
    s->io_buffer[3] = 0xff;
}

B
Blue Swirl 已提交
1524
static void ide_reset(IDEState *s)
B
bellard 已提交
1525
{
B
Blue Swirl 已提交
1526 1527 1528
#ifdef DEBUG_IDE
    printf("ide: reset\n");
#endif
1529
    if (s->drive_kind == IDE_CFATA)
1530 1531 1532
        s->mult_sectors = 0;
    else
        s->mult_sectors = MAX_MULT_SECTORS;
B
Blue Swirl 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
    /* ide regs */
    s->feature = 0;
    s->error = 0;
    s->nsector = 0;
    s->sector = 0;
    s->lcyl = 0;
    s->hcyl = 0;

    /* lba48 */
    s->hob_feature = 0;
    s->hob_sector = 0;
    s->hob_nsector = 0;
    s->hob_lcyl = 0;
    s->hob_hcyl = 0;

B
bellard 已提交
1548
    s->select = 0xa0;
A
aliguori 已提交
1549
    s->status = READY_STAT | SEEK_STAT;
B
Blue Swirl 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565

    s->lba48 = 0;

    /* ATAPI specific */
    s->sense_key = 0;
    s->asc = 0;
    s->cdrom_changed = 0;
    s->packet_transfer_size = 0;
    s->elementary_transfer_size = 0;
    s->io_buffer_index = 0;
    s->cd_sector_size = 0;
    s->atapi_dma = 0;
    /* ATA DMA state */
    s->io_buffer_size = 0;
    s->req_nb_sectors = 0;

B
bellard 已提交
1566
    ide_set_signature(s);
1567 1568 1569 1570
    /* init the transfer handler so that 0xffff is returned on data
       accesses */
    s->end_transfer_func = ide_dummy_transfer_stop;
    ide_dummy_transfer_stop(s);
1571
    s->media_changed = 0;
B
bellard 已提交
1572 1573
}

B
Blue Swirl 已提交
1574 1575 1576 1577 1578 1579 1580
void ide_bus_reset(IDEBus *bus)
{
    bus->unit = 0;
    bus->cmd = 0;
    ide_reset(&bus->ifs[0]);
    ide_reset(&bus->ifs[1]);
    ide_clear_hob(bus);
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592

    /* pending async DMA */
    if (bus->dma->aiocb) {
#ifdef DEBUG_AIO
        printf("aio_cancel\n");
#endif
        bdrv_aio_cancel(bus->dma->aiocb);
        bus->dma->aiocb = NULL;
    }

    /* reset dma provider too */
    bus->dma->ops->reset(bus->dma);
B
Blue Swirl 已提交
1593 1594
}

1595
int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
1596
                   const char *version, const char *serial)
1597 1598 1599 1600
{
    int cylinders, heads, secs;
    uint64_t nb_sectors;

1601
    s->bs = bs;
1602 1603
    s->drive_kind = kind;

1604 1605
    bdrv_get_geometry(bs, &nb_sectors);
    bdrv_guess_geometry(bs, &cylinders, &heads, &secs);
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
    if (cylinders < 1 || cylinders > 16383) {
        error_report("cyls must be between 1 and 16383");
        return -1;
    }
    if (heads < 1 || heads > 16) {
        error_report("heads must be between 1 and 16");
        return -1;
    }
    if (secs < 1 || secs > 63) {
        error_report("secs must be between 1 and 63");
        return -1;
    }
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    s->cylinders = cylinders;
    s->heads = heads;
    s->sectors = secs;
    s->nb_sectors = nb_sectors;
    /* The SMART values should be preserved across power cycles
       but they aren't.  */
    s->smart_enabled = 1;
    s->smart_autosave = 1;
    s->smart_errors = 0;
    s->smart_selftest_count = 0;
1628
    if (kind == IDE_CD) {
1629
        bdrv_set_change_cb(bs, cdrom_change_cb, s);
1630
        bs->buffer_alignment = 2048;
1631
    } else {
1632 1633 1634 1635
        if (!bdrv_is_inserted(s->bs)) {
            error_report("Device needs media, but drive is empty");
            return -1;
        }
1636 1637 1638 1639
        if (bdrv_is_read_only(bs)) {
            error_report("Can't use a read-only drive");
            return -1;
        }
1640
    }
1641
    if (serial) {
1642 1643
        strncpy(s->drive_serial_str, serial, sizeof(s->drive_serial_str));
    } else {
1644 1645
        snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
                 "QM%05d", s->drive_serial);
1646
    }
G
Gerd Hoffmann 已提交
1647 1648 1649 1650 1651
    if (version) {
        pstrcpy(s->version, sizeof(s->version), version);
    } else {
        pstrcpy(s->version, sizeof(s->version), QEMU_VERSION);
    }
1652

1653
    ide_reset(s);
1654
    bdrv_set_removable(bs, s->drive_kind == IDE_CD);
1655
    return 0;
1656 1657
}

1658
static void ide_init1(IDEBus *bus, int unit)
1659 1660 1661 1662 1663 1664 1665
{
    static int drive_serial = 1;
    IDEState *s = &bus->ifs[unit];

    s->bus = bus;
    s->unit = unit;
    s->drive_serial = drive_serial++;
1666 1667
    /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
    s->io_buffer = qemu_memalign(2048, IDE_DMA_BUF_SECTORS*512 + 4);
1668
    s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4;
1669
    s->smart_selftest_data = qemu_blockalign(s->bs, 512);
1670
    s->sector_write_timer = qemu_new_timer_ns(vm_clock,
1671
                                           ide_sector_write_timer_cb, s);
1672 1673
}

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
static void ide_nop_start(IDEDMA *dma, IDEState *s,
                          BlockDriverCompletionFunc *cb)
{
}

static int ide_nop(IDEDMA *dma)
{
    return 0;
}

static int ide_nop_int(IDEDMA *dma, int x)
{
    return 0;
}

static void ide_nop_restart(void *opaque, int x, int y)
{
}

static const IDEDMAOps ide_dma_nop_ops = {
    .start_dma      = ide_nop_start,
    .start_transfer = ide_nop,
    .prepare_buf    = ide_nop_int,
    .rw_buf         = ide_nop_int,
    .set_unit       = ide_nop_int,
    .add_status     = ide_nop_int,
    .set_inactive   = ide_nop,
    .restart_cb     = ide_nop_restart,
    .reset          = ide_nop,
};

static IDEDMA ide_dma_nop = {
    .ops = &ide_dma_nop_ops,
    .aiocb = NULL,
};

1710 1711 1712 1713 1714 1715 1716
void ide_init2(IDEBus *bus, qemu_irq irq)
{
    int i;

    for(i = 0; i < 2; i++) {
        ide_init1(bus, i);
        ide_reset(&bus->ifs[i]);
1717
    }
1718
    bus->irq = irq;
1719
    bus->dma = &ide_dma_nop;
1720 1721
}

1722 1723 1724
/* TODO convert users to qdev and remove */
void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
                                    DriveInfo *hd1, qemu_irq irq)
B
bellard 已提交
1725
{
1726
    int i;
1727
    DriveInfo *dinfo;
B
bellard 已提交
1728

1729
    for(i = 0; i < 2; i++) {
1730 1731 1732
        dinfo = i == 0 ? hd0 : hd1;
        ide_init1(bus, i);
        if (dinfo) {
1733 1734 1735
            if (ide_init_drive(&bus->ifs[i], dinfo->bdrv,
                               bdrv_get_type_hint(dinfo->bdrv) == BDRV_TYPE_CDROM ? IDE_CD : IDE_HD,
                               NULL,
1736 1737 1738 1739
                               *dinfo->serial ? dinfo->serial : NULL) < 0) {
                error_report("Can't set up IDE drive %s", dinfo->id);
                exit(1);
            }
1740 1741 1742
        } else {
            ide_reset(&bus->ifs[i]);
        }
B
bellard 已提交
1743
    }
1744
    bus->irq = irq;
1745
    bus->dma = &ide_dma_nop;
B
bellard 已提交
1746 1747
}

G
Gerd Hoffmann 已提交
1748
void ide_init_ioport(IDEBus *bus, int iobase, int iobase2)
B
bellard 已提交
1749
{
G
Gerd Hoffmann 已提交
1750 1751
    register_ioport_write(iobase, 8, 1, ide_ioport_write, bus);
    register_ioport_read(iobase, 8, 1, ide_ioport_read, bus);
1752
    if (iobase2) {
G
Gerd Hoffmann 已提交
1753 1754
        register_ioport_read(iobase2, 1, 1, ide_status_read, bus);
        register_ioport_write(iobase2, 1, 1, ide_cmd_write, bus);
B
bellard 已提交
1755
    }
1756

1757
    /* data ports */
G
Gerd Hoffmann 已提交
1758 1759 1760 1761
    register_ioport_write(iobase, 2, 2, ide_data_writew, bus);
    register_ioport_read(iobase, 2, 2, ide_data_readw, bus);
    register_ioport_write(iobase, 4, 4, ide_data_writel, bus);
    register_ioport_read(iobase, 4, 4, ide_data_readl, bus);
B
bellard 已提交
1762
}
B
bellard 已提交
1763

J
Juan Quintela 已提交
1764
static bool is_identify_set(void *opaque, int version_id)
1765
{
J
Juan Quintela 已提交
1766 1767 1768 1769 1770
    IDEState *s = opaque;

    return s->identify_set != 0;
}

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
static EndTransferFunc* transfer_end_table[] = {
        ide_sector_read,
        ide_sector_write,
        ide_transfer_stop,
        ide_atapi_cmd_reply_end,
        ide_atapi_cmd,
        ide_dummy_transfer_stop,
};

static int transfer_end_table_idx(EndTransferFunc *fn)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(transfer_end_table); i++)
        if (transfer_end_table[i] == fn)
            return i;

    return -1;
}

J
Juan Quintela 已提交
1791
static int ide_drive_post_load(void *opaque, int version_id)
1792
{
J
Juan Quintela 已提交
1793 1794 1795
    IDEState *s = opaque;

    if (version_id < 3) {
G
Gleb Natapov 已提交
1796
        if (s->sense_key == SENSE_UNIT_ATTENTION &&
J
Juan Quintela 已提交
1797
            s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED) {
G
Gleb Natapov 已提交
1798
            s->cdrom_changed = 1;
J
Juan Quintela 已提交
1799
        }
G
Gleb Natapov 已提交
1800
    }
J
Juan Quintela 已提交
1801
    return 0;
1802 1803
}

1804 1805 1806 1807
static int ide_drive_pio_post_load(void *opaque, int version_id)
{
    IDEState *s = opaque;

1808
    if (s->end_transfer_fn_idx > ARRAY_SIZE(transfer_end_table)) {
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
        return -EINVAL;
    }
    s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
    s->data_ptr = s->io_buffer + s->cur_io_buffer_offset;
    s->data_end = s->data_ptr + s->cur_io_buffer_len;

    return 0;
}

static void ide_drive_pio_pre_save(void *opaque)
{
    IDEState *s = opaque;
    int idx;

    s->cur_io_buffer_offset = s->data_ptr - s->io_buffer;
    s->cur_io_buffer_len = s->data_end - s->data_ptr;

    idx = transfer_end_table_idx(s->end_transfer_func);
    if (idx == -1) {
        fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n",
                        __func__);
        s->end_transfer_fn_idx = 2;
    } else {
        s->end_transfer_fn_idx = idx;
    }
}

static bool ide_drive_pio_state_needed(void *opaque)
{
    IDEState *s = opaque;

    return (s->status & DRQ_STAT) != 0;
}

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
static bool ide_atapi_gesn_needed(void *opaque)
{
    IDEState *s = opaque;

    return s->events.new_media || s->events.eject_request;
}

/* Fields for GET_EVENT_STATUS_NOTIFICATION ATAPI command */
const VMStateDescription vmstate_ide_atapi_gesn_state = {
    .name ="ide_drive/atapi/gesn_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields = (VMStateField []) {
        VMSTATE_BOOL(events.new_media, IDEState),
        VMSTATE_BOOL(events.eject_request, IDEState),
    }
};

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
const VMStateDescription vmstate_ide_drive_pio_state = {
    .name = "ide_drive/pio_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .pre_save = ide_drive_pio_pre_save,
    .post_load = ide_drive_pio_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_INT32(req_nb_sectors, IDEState),
        VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
			     vmstate_info_uint8, uint8_t),
        VMSTATE_INT32(cur_io_buffer_offset, IDEState),
        VMSTATE_INT32(cur_io_buffer_len, IDEState),
        VMSTATE_UINT8(end_transfer_fn_idx, IDEState),
        VMSTATE_INT32(elementary_transfer_size, IDEState),
        VMSTATE_INT32(packet_transfer_size, IDEState),
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
1882 1883
const VMStateDescription vmstate_ide_drive = {
    .name = "ide_drive",
1884
    .version_id = 3,
J
Juan Quintela 已提交
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .post_load = ide_drive_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_INT32(mult_sectors, IDEState),
        VMSTATE_INT32(identify_set, IDEState),
        VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set),
        VMSTATE_UINT8(feature, IDEState),
        VMSTATE_UINT8(error, IDEState),
        VMSTATE_UINT32(nsector, IDEState),
        VMSTATE_UINT8(sector, IDEState),
        VMSTATE_UINT8(lcyl, IDEState),
        VMSTATE_UINT8(hcyl, IDEState),
        VMSTATE_UINT8(hob_feature, IDEState),
        VMSTATE_UINT8(hob_sector, IDEState),
        VMSTATE_UINT8(hob_nsector, IDEState),
        VMSTATE_UINT8(hob_lcyl, IDEState),
        VMSTATE_UINT8(hob_hcyl, IDEState),
        VMSTATE_UINT8(select, IDEState),
        VMSTATE_UINT8(status, IDEState),
        VMSTATE_UINT8(lba48, IDEState),
        VMSTATE_UINT8(sense_key, IDEState),
        VMSTATE_UINT8(asc, IDEState),
        VMSTATE_UINT8_V(cdrom_changed, IDEState, 3),
        VMSTATE_END_OF_LIST()
1910 1911 1912 1913 1914
    },
    .subsections = (VMStateSubsection []) {
        {
            .vmsd = &vmstate_ide_drive_pio_state,
            .needed = ide_drive_pio_state_needed,
1915 1916 1917
        }, {
            .vmsd = &vmstate_ide_atapi_gesn_state,
            .needed = ide_atapi_gesn_needed,
1918 1919 1920
        }, {
            /* empty */
        }
J
Juan Quintela 已提交
1921 1922 1923
    }
};

J
Juan Quintela 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
const VMStateDescription vmstate_ide_bus = {
    .name = "ide_bus",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT8(cmd, IDEBus),
        VMSTATE_UINT8(unit, IDEBus),
        VMSTATE_END_OF_LIST()
    }
};
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

void ide_drive_get(DriveInfo **hd, int max_bus)
{
    int i;

    if (drive_get_max_bus(IF_IDE) >= max_bus) {
        fprintf(stderr, "qemu: too many IDE bus: %d\n", max_bus);
        exit(1);
    }

    for(i = 0; i < max_bus * MAX_IDE_DEVS; i++) {
        hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
    }
}