core.c 86.8 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>
G
Gerd Hoffmann 已提交
28
#include <hw/scsi.h>
P
pbrook 已提交
29 30
#include "qemu-timer.h"
#include "sysemu.h"
31
#include "dma.h"
G
Gerd Hoffmann 已提交
32 33

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

B
Blue Swirl 已提交
35 36
#define IDE_PAGE_SIZE 4096

B
Blue Swirl 已提交
37
static const int smart_attributes[][5] = {
B
Brian Wheeler 已提交
38 39 40 41 42 43 44 45
    /* id,  flags, val, wrst, thrsh */
    { 0x01, 0x03, 0x64, 0x64, 0x06}, /* raw read */
    { 0x03, 0x03, 0x64, 0x64, 0x46}, /* spin up */
    { 0x04, 0x02, 0x64, 0x64, 0x14}, /* start stop count */
    { 0x05, 0x03, 0x64, 0x64, 0x36}, /* remapped sectors */
    { 0x00, 0x00, 0x00, 0x00, 0x00}
};

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/* XXX: DVDs that could fit on a CD will be reported as a CD */
static inline int media_present(IDEState *s)
{
    return (s->nb_sectors > 0);
}

static inline int media_is_dvd(IDEState *s)
{
    return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
}

static inline int media_is_cd(IDEState *s)
{
    return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
}

62
static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb);
K
Kevin Wolf 已提交
63
static void ide_dma_restart(IDEState *s, int is_read);
64
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
K
Kevin Wolf 已提交
65
static int ide_handle_rw_error(IDEState *s, int error, int op);
B
bellard 已提交
66

B
bellard 已提交
67 68 69 70 71 72 73 74
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 = ' ';
75
        str[i^1] = v;
B
bellard 已提交
76 77 78
    }
}

B
bellard 已提交
79 80 81 82 83 84 85 86 87 88 89
static void padstr8(uint8_t *buf, int buf_size, const char *src)
{
    int i;
    for(i = 0; i < buf_size; i++) {
        if (*src)
            buf[i] = *src++;
        else
            buf[i] = ' ';
    }
}

B
bellard 已提交
90 91
static void put_le16(uint16_t *p, unsigned int v)
{
B
bellard 已提交
92
    *p = cpu_to_le16(v);
B
bellard 已提交
93 94
}

B
bellard 已提交
95 96 97 98
static void ide_identify(IDEState *s)
{
    uint16_t *p;
    unsigned int oldsize;
99
    IDEDevice *dev;
B
bellard 已提交
100

101 102 103 104 105
    if (s->identify_set) {
	memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
	return;
    }

B
bellard 已提交
106 107
    memset(s->io_buffer, 0, 512);
    p = (uint16_t *)s->io_buffer;
B
bellard 已提交
108
    put_le16(p + 0, 0x0040);
109
    put_le16(p + 1, s->cylinders);
B
bellard 已提交
110 111 112
    put_le16(p + 3, s->heads);
    put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
    put_le16(p + 5, 512); /* XXX: retired, remove ? */
113
    put_le16(p + 6, s->sectors);
114
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
B
bellard 已提交
115 116 117
    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 已提交
118
    padstr((char *)(p + 23), s->version, 8); /* firmware version */
T
ths 已提交
119
    padstr((char *)(p + 27), "QEMU HARDDISK", 40); /* model */
120
#if MAX_MULT_SECTORS > 1
B
bellard 已提交
121
    put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
B
bellard 已提交
122
#endif
B
bellard 已提交
123
    put_le16(p + 48, 1); /* dword I/O */
124
    put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
B
bellard 已提交
125 126
    put_le16(p + 51, 0x200); /* PIO transfer cycle */
    put_le16(p + 52, 0x200); /* DMA transfer cycle */
127
    put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
B
bellard 已提交
128 129 130
    put_le16(p + 54, s->cylinders);
    put_le16(p + 55, s->heads);
    put_le16(p + 56, s->sectors);
B
bellard 已提交
131
    oldsize = s->cylinders * s->heads * s->sectors;
B
bellard 已提交
132 133
    put_le16(p + 57, oldsize);
    put_le16(p + 58, oldsize >> 16);
B
bellard 已提交
134
    if (s->mult_sectors)
B
bellard 已提交
135 136 137
        put_le16(p + 59, 0x100 | s->mult_sectors);
    put_le16(p + 60, s->nb_sectors);
    put_le16(p + 61, s->nb_sectors >> 16);
138
    put_le16(p + 62, 0x07); /* single word dma0-2 supported */
139 140 141 142 143 144 145
    put_le16(p + 63, 0x07); /* mdma0-2 supported */
    put_le16(p + 65, 120);
    put_le16(p + 66, 120);
    put_le16(p + 67, 120);
    put_le16(p + 68, 120);
    put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
    put_le16(p + 81, 0x16); /* conforms to ata5 */
B
Brian Wheeler 已提交
146 147
    /* 14=NOP supported, 0=SMART supported */
    put_le16(p + 82, (1 << 14) | 1);
B
bellard 已提交
148 149
    /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
    put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
B
Brian Wheeler 已提交
150 151
    /* 14=set to 1, 1=SMART self test, 0=SMART error logging */
    put_le16(p + 84, (1 << 14) | 0);
152 153 154 155 156
    /* 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 已提交
157 158
    /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
    put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
B
Brian Wheeler 已提交
159 160
    /* 14=set to 1, 1=smart self test, 0=smart error logging */
    put_le16(p + 87, (1 << 14) | 0);
161 162
    put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
    put_le16(p + 93, 1 | (1 << 14) | 0x2000);
B
bellard 已提交
163 164 165 166
    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);
167 168 169
    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));
170 171 172

    memcpy(s->identify_data, p, sizeof(s->identify_data));
    s->identify_set = 1;
B
bellard 已提交
173 174 175 176 177 178
}

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

179 180 181 182 183
    if (s->identify_set) {
	memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
	return;
    }

B
bellard 已提交
184 185 186
    memset(s->io_buffer, 0, 512);
    p = (uint16_t *)s->io_buffer;
    /* Removable CDROM, 50us response, 12 byte packets */
B
bellard 已提交
187
    put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
188
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
B
bellard 已提交
189 190 191
    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 已提交
192
    padstr((char *)(p + 23), s->version, 8); /* firmware version */
193
    padstr((char *)(p + 27), "QEMU DVD-ROM", 40); /* model */
B
bellard 已提交
194
    put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
195 196 197
#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 */
198
    put_le16(p + 62, 7);  /* single word dma0-2 supported */
199 200 201
    put_le16(p + 63, 7);  /* mdma0-2 supported */
    put_le16(p + 64, 0x3f); /* PIO modes supported */
#else
B
bellard 已提交
202 203 204 205
    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 */
    put_le16(p + 64, 1); /* PIO modes */
206
#endif
B
bellard 已提交
207 208 209 210
    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 */
211

B
bellard 已提交
212 213
    put_le16(p + 71, 30); /* in ns */
    put_le16(p + 72, 30); /* in ns */
B
bellard 已提交
214

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

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
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 */
242
    padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
243
    put_le16(p + 22, 0x0004);			/* ECC bytes */
G
Gerd Hoffmann 已提交
244
    padstr((char *) (p + 23), s->version, 8);	/* Firmware Revision */
T
ths 已提交
245
    padstr((char *) (p + 27), "QEMU MICRODRIVE", 40);/* Model number */
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 284 285 286 287 288
#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 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
static void ide_set_signature(IDEState *s)
{
    s->select &= 0xf0; /* clear head */
    /* put signature */
    s->nsector = 1;
    s->sector = 1;
    if (s->is_cdrom) {
        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;
}

313 314 315 316 317 318 319 320
static inline void ide_dma_submit_check(IDEState *s,
          BlockDriverCompletionFunc *dma_cb, BMDMAState *bm)
{
    if (bm->aiocb)
	return;
    dma_cb(bm, -1);
}

B
bellard 已提交
321
/* prepare data transfer and tell what to do after */
322
static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
B
bellard 已提交
323 324 325 326 327
                               EndTransferFunc *end_transfer_func)
{
    s->end_transfer_func = end_transfer_func;
    s->data_ptr = buf;
    s->data_end = buf + size;
328 329
    if (!(s->status & ERR_STAT))
        s->status |= DRQ_STAT;
B
bellard 已提交
330 331 332 333 334 335 336 337 338 339
}

static void ide_transfer_stop(IDEState *s)
{
    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 已提交
340
int64_t ide_get_sector(IDEState *s)
B
bellard 已提交
341 342 343 344
{
    int64_t sector_num;
    if (s->select & 0x40) {
        /* lba */
B
bellard 已提交
345 346 347 348 349 350 351 352 353 354
	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 已提交
355 356
    } else {
        sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
B
bellard 已提交
357
            (s->select & 0x0f) * s->sectors + (s->sector - 1);
B
bellard 已提交
358 359 360 361
    }
    return sector_num;
}

G
Gerd Hoffmann 已提交
362
void ide_set_sector(IDEState *s, int64_t sector_num)
B
bellard 已提交
363 364 365
{
    unsigned int cyl, r;
    if (s->select & 0x40) {
B
bellard 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378
	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 已提交
379 380 381 382 383
    } else {
        cyl = sector_num / (s->heads * s->sectors);
        r = sector_num % (s->heads * s->sectors);
        s->hcyl = cyl >> 8;
        s->lcyl = cyl;
384
        s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
B
bellard 已提交
385 386 387 388
        s->sector = (r % s->sectors) + 1;
    }
}

389 390
static void ide_rw_error(IDEState *s) {
    ide_abort_command(s);
391
    ide_set_irq(s->bus);
392 393
}

B
bellard 已提交
394 395 396 397 398 399
static void ide_sector_read(IDEState *s)
{
    int64_t sector_num;
    int ret, n;

    s->status = READY_STAT | SEEK_STAT;
400
    s->error = 0; /* not needed by IDE spec, but needed by Windows */
B
bellard 已提交
401 402 403 404 405 406 407
    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)
408
        printf("read sector=%" PRId64 "\n", sector_num);
B
bellard 已提交
409 410 411 412
#endif
        if (n > s->req_nb_sectors)
            n = s->req_nb_sectors;
        ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
413
        if (ret != 0) {
K
Kevin Wolf 已提交
414 415 416 417 418
            if (ide_handle_rw_error(s, -ret,
                BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ))
            {
                return;
            }
419
        }
B
bellard 已提交
420
        ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
421
        ide_set_irq(s->bus);
B
bellard 已提交
422 423 424 425 426
        ide_set_sector(s, sector_num + n);
        s->nsector -= n;
    }
}

427 428 429 430

/* return 0 if buffer completed */
static int dma_buf_prepare(BMDMAState *bm, int is_write)
{
G
Gerd Hoffmann 已提交
431
    IDEState *s = bmdma_active_if(bm);
432 433 434 435 436 437
    struct {
        uint32_t addr;
        uint32_t size;
    } prd;
    int l, len;

B
Blue Swirl 已提交
438
    qemu_sglist_init(&s->sg, s->nsector / (IDE_PAGE_SIZE / 512) + 1);
439 440 441 442 443
    s->io_buffer_size = 0;
    for(;;) {
        if (bm->cur_prd_len == 0) {
            /* end of table (with a fail safe of one page) */
            if (bm->cur_prd_last ||
B
Blue Swirl 已提交
444
                (bm->cur_addr - bm->addr) >= IDE_PAGE_SIZE)
445 446 447 448 449 450 451 452 453 454 455 456 457 458
                return s->io_buffer_size != 0;
            cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
            bm->cur_addr += 8;
            prd.addr = le32_to_cpu(prd.addr);
            prd.size = le32_to_cpu(prd.size);
            len = prd.size & 0xfffe;
            if (len == 0)
                len = 0x10000;
            bm->cur_prd_len = len;
            bm->cur_prd_addr = prd.addr;
            bm->cur_prd_last = (prd.size & 0x80000000);
        }
        l = bm->cur_prd_len;
        if (l > 0) {
459 460 461 462
            qemu_sglist_add(&s->sg, bm->cur_prd_addr, l);
            bm->cur_prd_addr += l;
            bm->cur_prd_len -= l;
            s->io_buffer_size += l;
463 464 465 466 467 468 469
        }
    }
    return 1;
}

static void dma_buf_commit(IDEState *s, int is_write)
{
470
    qemu_sglist_destroy(&s->sg);
471 472
}

G
Gerd Hoffmann 已提交
473
void ide_dma_error(IDEState *s)
474 475 476 477
{
    ide_transfer_stop(s);
    s->error = ABRT_ERR;
    s->status = READY_STAT | ERR_STAT;
478
    ide_set_irq(s->bus);
479 480
}

K
Kevin Wolf 已提交
481
static int ide_handle_rw_error(IDEState *s, int error, int op)
482
{
K
Kevin Wolf 已提交
483
    int is_read = (op & BM_STATUS_RETRY_READ);
484
    BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
485

486 487
    if (action == BLOCK_ERR_IGNORE) {
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
488
        return 0;
489
    }
490 491 492

    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
            || action == BLOCK_ERR_STOP_ANY) {
G
Gerd Hoffmann 已提交
493 494
        s->bus->bmdma->unit = s->unit;
        s->bus->bmdma->status |= op;
495
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
496
        vm_stop(0);
497
    } else {
K
Kevin Wolf 已提交
498
        if (op & BM_STATUS_DMA_RETRY) {
499
            dma_buf_commit(s, 0);
500
            ide_dma_error(s);
501
        } else {
502
            ide_rw_error(s);
503
        }
504
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
505 506 507 508 509
    }

    return 1;
}

510 511
/* return 0 if buffer completed */
static int dma_buf_rw(BMDMAState *bm, int is_write)
B
bellard 已提交
512
{
G
Gerd Hoffmann 已提交
513
    IDEState *s = bmdma_active_if(bm);
514 515 516 517 518
    struct {
        uint32_t addr;
        uint32_t size;
    } prd;
    int l, len;
B
bellard 已提交
519

520 521
    for(;;) {
        l = s->io_buffer_size - s->io_buffer_index;
522
        if (l <= 0)
523 524 525 526
            break;
        if (bm->cur_prd_len == 0) {
            /* end of table (with a fail safe of one page) */
            if (bm->cur_prd_last ||
B
Blue Swirl 已提交
527
                (bm->cur_addr - bm->addr) >= IDE_PAGE_SIZE)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                return 0;
            cpu_physical_memory_read(bm->cur_addr, (uint8_t *)&prd, 8);
            bm->cur_addr += 8;
            prd.addr = le32_to_cpu(prd.addr);
            prd.size = le32_to_cpu(prd.size);
            len = prd.size & 0xfffe;
            if (len == 0)
                len = 0x10000;
            bm->cur_prd_len = len;
            bm->cur_prd_addr = prd.addr;
            bm->cur_prd_last = (prd.size & 0x80000000);
        }
        if (l > bm->cur_prd_len)
            l = bm->cur_prd_len;
        if (l > 0) {
            if (is_write) {
544
                cpu_physical_memory_write(bm->cur_prd_addr,
545 546
                                          s->io_buffer + s->io_buffer_index, l);
            } else {
547
                cpu_physical_memory_read(bm->cur_prd_addr,
548 549 550 551 552
                                          s->io_buffer + s->io_buffer_index, l);
            }
            bm->cur_prd_addr += l;
            bm->cur_prd_len -= l;
            s->io_buffer_index += l;
B
bellard 已提交
553 554
        }
    }
555 556 557 558 559 560
    return 1;
}

static void ide_read_dma_cb(void *opaque, int ret)
{
    BMDMAState *bm = opaque;
G
Gerd Hoffmann 已提交
561
    IDEState *s = bmdma_active_if(bm);
562 563 564
    int n;
    int64_t sector_num;

565
    if (ret < 0) {
K
Kevin Wolf 已提交
566 567 568 569 570
        if (ide_handle_rw_error(s, -ret,
            BM_STATUS_DMA_RETRY | BM_STATUS_RETRY_READ))
        {
            return;
        }
571 572
    }

573 574 575
    n = s->io_buffer_size >> 9;
    sector_num = ide_get_sector(s);
    if (n > 0) {
576
        dma_buf_commit(s, 1);
577 578 579 580 581 582 583
        sector_num += n;
        ide_set_sector(s, sector_num);
        s->nsector -= n;
    }

    /* end of transfer ? */
    if (s->nsector == 0) {
B
bellard 已提交
584
        s->status = READY_STAT | SEEK_STAT;
585
        ide_set_irq(s->bus);
586 587 588 589
    eot:
        bm->status &= ~BM_STATUS_DMAING;
        bm->status |= BM_STATUS_INT;
        bm->dma_cb = NULL;
G
Gerd Hoffmann 已提交
590
        bm->unit = -1;
591 592
        bm->aiocb = NULL;
        return;
B
bellard 已提交
593
    }
594 595 596 597 598

    /* launch next transfer */
    n = s->nsector;
    s->io_buffer_index = 0;
    s->io_buffer_size = n * 512;
599 600
    if (dma_buf_prepare(bm, 1) == 0)
        goto eot;
601
#ifdef DEBUG_AIO
B
blueswir1 已提交
602
    printf("aio_read: sector_num=%" PRId64 " n=%d\n", sector_num, n);
603
#endif
604
    bm->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num, ide_read_dma_cb, bm);
605
    ide_dma_submit_check(s, ide_read_dma_cb, bm);
B
bellard 已提交
606 607 608 609
}

static void ide_sector_read_dma(IDEState *s)
{
610
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
B
bellard 已提交
611 612
    s->io_buffer_index = 0;
    s->io_buffer_size = 0;
A
aurel32 已提交
613
    s->is_read = 1;
B
bellard 已提交
614 615 616
    ide_dma_start(s, ide_read_dma_cb);
}

617 618 619
static void ide_sector_write_timer_cb(void *opaque)
{
    IDEState *s = opaque;
620
    ide_set_irq(s->bus);
621 622
}

B
bellard 已提交
623 624 625
static void ide_sector_write(IDEState *s)
{
    int64_t sector_num;
626
    int ret, n, n1;
B
bellard 已提交
627 628 629 630

    s->status = READY_STAT | SEEK_STAT;
    sector_num = ide_get_sector(s);
#if defined(DEBUG_IDE)
631
    printf("write sector=%" PRId64 "\n", sector_num);
B
bellard 已提交
632 633 634 635
#endif
    n = s->nsector;
    if (n > s->req_nb_sectors)
        n = s->req_nb_sectors;
636
    ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
637

638
    if (ret != 0) {
K
Kevin Wolf 已提交
639
        if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY))
640
            return;
641 642
    }

B
bellard 已提交
643 644
    s->nsector -= n;
    if (s->nsector == 0) {
T
ths 已提交
645
        /* no more sectors to write */
B
bellard 已提交
646 647 648 649 650 651 652 653
        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);
654

655 656 657 658 659 660 661
    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 已提交
662
        qemu_mod_timer(s->sector_write_timer,
663
                       qemu_get_clock(vm_clock) + (get_ticks_per_sec() / 1000));
B
Blue Swirl 已提交
664
    } else {
665
        ide_set_irq(s->bus);
666
    }
B
bellard 已提交
667 668
}

669
static void ide_dma_restart_bh(void *opaque)
670 671
{
    BMDMAState *bm = opaque;
K
Kevin Wolf 已提交
672
    int is_read;
673 674 675 676

    qemu_bh_delete(bm->bh);
    bm->bh = NULL;

K
Kevin Wolf 已提交
677 678
    is_read = !!(bm->status & BM_STATUS_RETRY_READ);

679
    if (bm->status & BM_STATUS_DMA_RETRY) {
K
Kevin Wolf 已提交
680 681
        bm->status &= ~(BM_STATUS_DMA_RETRY | BM_STATUS_RETRY_READ);
        ide_dma_restart(bmdma_active_if(bm), is_read);
682
    } else if (bm->status & BM_STATUS_PIO_RETRY) {
K
Kevin Wolf 已提交
683 684 685 686 687 688
        bm->status &= ~(BM_STATUS_PIO_RETRY | BM_STATUS_RETRY_READ);
        if (is_read) {
            ide_sector_read(bmdma_active_if(bm));
        } else {
            ide_sector_write(bmdma_active_if(bm));
        }
689 690 691
    }
}

G
Gerd Hoffmann 已提交
692
void ide_dma_restart_cb(void *opaque, int running, int reason)
693 694 695 696 697 698 699 700 701 702 703 704
{
    BMDMAState *bm = opaque;

    if (!running)
        return;

    if (!bm->bh) {
        bm->bh = qemu_bh_new(ide_dma_restart_bh, bm);
        qemu_bh_schedule(bm->bh);
    }
}

705
static void ide_write_dma_cb(void *opaque, int ret)
B
bellard 已提交
706
{
707
    BMDMAState *bm = opaque;
G
Gerd Hoffmann 已提交
708
    IDEState *s = bmdma_active_if(bm);
709
    int n;
B
bellard 已提交
710 711
    int64_t sector_num;

712
    if (ret < 0) {
K
Kevin Wolf 已提交
713
        if (ide_handle_rw_error(s, -ret,  BM_STATUS_DMA_RETRY))
714
            return;
715 716
    }

717 718 719
    n = s->io_buffer_size >> 9;
    sector_num = ide_get_sector(s);
    if (n > 0) {
720
        dma_buf_commit(s, 0);
721 722 723
        sector_num += n;
        ide_set_sector(s, sector_num);
        s->nsector -= n;
B
bellard 已提交
724 725
    }

726 727 728
    /* end of transfer ? */
    if (s->nsector == 0) {
        s->status = READY_STAT | SEEK_STAT;
729
        ide_set_irq(s->bus);
730 731 732 733
    eot:
        bm->status &= ~BM_STATUS_DMAING;
        bm->status |= BM_STATUS_INT;
        bm->dma_cb = NULL;
G
Gerd Hoffmann 已提交
734
        bm->unit = -1;
735 736 737 738
        bm->aiocb = NULL;
        return;
    }

B
bellard 已提交
739 740
    n = s->nsector;
    s->io_buffer_size = n * 512;
741 742
    /* launch next transfer */
    if (dma_buf_prepare(bm, 0) == 0)
743 744
        goto eot;
#ifdef DEBUG_AIO
B
blueswir1 已提交
745
    printf("aio_write: sector_num=%" PRId64 " n=%d\n", sector_num, n);
746
#endif
747
    bm->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num, ide_write_dma_cb, bm);
748
    ide_dma_submit_check(s, ide_write_dma_cb, bm);
749 750 751 752 753 754 755
}

static void ide_sector_write_dma(IDEState *s)
{
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
    s->io_buffer_index = 0;
    s->io_buffer_size = 0;
A
aurel32 已提交
756
    s->is_read = 0;
B
bellard 已提交
757 758 759
    ide_dma_start(s, ide_write_dma_cb);
}

G
Gerd Hoffmann 已提交
760
void ide_atapi_cmd_ok(IDEState *s)
B
bellard 已提交
761 762
{
    s->error = 0;
A
aliguori 已提交
763
    s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
764
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
765
    ide_set_irq(s->bus);
B
bellard 已提交
766 767
}

G
Gerd Hoffmann 已提交
768
void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
B
bellard 已提交
769 770 771 772 773 774 775 776 777
{
#ifdef DEBUG_IDE_ATAPI
    printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
#endif
    s->error = sense_key << 4;
    s->status = READY_STAT | ERR_STAT;
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
    s->sense_key = sense_key;
    s->asc = asc;
778
    ide_set_irq(s->bus);
B
bellard 已提交
779 780
}

781 782 783 784 785 786 787 788
static void ide_atapi_cmd_check_status(IDEState *s)
{
#ifdef DEBUG_IDE_ATAPI
    printf("atapi_cmd_check_status\n");
#endif
    s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
    s->status = ERR_STAT;
    s->nsector = 0;
789
    ide_set_irq(s->bus);
790 791
}

C
Christoph Hellwig 已提交
792 793 794 795 796 797 798 799 800 801
static void ide_flush_cb(void *opaque, int ret)
{
    IDEState *s = opaque;

    /* XXX: how do we signal I/O errors here? */

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

B
bellard 已提交
802 803 804
static inline void cpu_to_ube16(uint8_t *buf, int val)
{
    buf[0] = val >> 8;
B
blueswir1 已提交
805
    buf[1] = val & 0xff;
B
bellard 已提交
806 807 808 809 810 811 812
}

static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
{
    buf[0] = val >> 24;
    buf[1] = val >> 16;
    buf[2] = val >> 8;
B
blueswir1 已提交
813
    buf[3] = val & 0xff;
B
bellard 已提交
814 815 816 817 818 819 820 821 822 823 824 825
}

static inline int ube16_to_cpu(const uint8_t *buf)
{
    return (buf[0] << 8) | buf[1];
}

static inline int ube32_to_cpu(const uint8_t *buf)
{
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}

B
bellard 已提交
826 827 828 829 830 831 832 833
static void lba_to_msf(uint8_t *buf, int lba)
{
    lba += 150;
    buf[0] = (lba / 75) / 60;
    buf[1] = (lba / 75) % 60;
    buf[2] = lba % 75;
}

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
static void cd_data_to_raw(uint8_t *buf, int lba)
{
    /* sync bytes */
    buf[0] = 0x00;
    memset(buf + 1, 0xff, 10);
    buf[11] = 0x00;
    buf += 12;
    /* MSF */
    lba_to_msf(buf, lba);
    buf[3] = 0x01; /* mode 1 data */
    buf += 4;
    /* data */
    buf += 2048;
    /* XXX: ECC not computed */
    memset(buf, 0, 288);
}

851
static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
B
bellard 已提交
852 853
                           int sector_size)
{
B
bellard 已提交
854 855
    int ret;

B
bellard 已提交
856 857
    switch(sector_size) {
    case 2048:
B
bellard 已提交
858
        ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
B
bellard 已提交
859 860
        break;
    case 2352:
B
bellard 已提交
861 862 863
        ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
        if (ret < 0)
            return ret;
864
        cd_data_to_raw(buf, lba);
B
bellard 已提交
865 866
        break;
    default:
B
bellard 已提交
867
        ret = -EIO;
B
bellard 已提交
868 869
        break;
    }
B
bellard 已提交
870 871 872
    return ret;
}

G
Gerd Hoffmann 已提交
873
void ide_atapi_io_error(IDEState *s, int ret)
B
bellard 已提交
874 875 876
{
    /* XXX: handle more errors */
    if (ret == -ENOMEDIUM) {
877
        ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
878 879
                            ASC_MEDIUM_NOT_PRESENT);
    } else {
880
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
B
bellard 已提交
881 882
                            ASC_LOGICAL_BLOCK_OOR);
    }
B
bellard 已提交
883 884
}

B
bellard 已提交
885 886 887
/* The whole ATAPI transfer logic is handled in this function */
static void ide_atapi_cmd_reply_end(IDEState *s)
{
B
bellard 已提交
888
    int byte_count_limit, size, ret;
B
bellard 已提交
889
#ifdef DEBUG_IDE_ATAPI
890
    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
B
bellard 已提交
891 892 893 894 895 896 897
           s->packet_transfer_size,
           s->elementary_transfer_size,
           s->io_buffer_index);
#endif
    if (s->packet_transfer_size <= 0) {
        /* end of transfer */
        ide_transfer_stop(s);
A
aliguori 已提交
898
        s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
899
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
900
        ide_set_irq(s->bus);
B
bellard 已提交
901 902 903 904 905
#ifdef DEBUG_IDE_ATAPI
        printf("status=0x%x\n", s->status);
#endif
    } else {
        /* see if a new sector must be read */
B
bellard 已提交
906
        if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
B
bellard 已提交
907 908 909 910 911 912
            ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
            if (ret < 0) {
                ide_transfer_stop(s);
                ide_atapi_io_error(s, ret);
                return;
            }
B
bellard 已提交
913 914 915 916 917 918
            s->lba++;
            s->io_buffer_index = 0;
        }
        if (s->elementary_transfer_size > 0) {
            /* there are some data left to transmit in this elementary
               transfer */
B
bellard 已提交
919
            size = s->cd_sector_size - s->io_buffer_index;
B
bellard 已提交
920 921
            if (size > s->elementary_transfer_size)
                size = s->elementary_transfer_size;
922
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
B
bellard 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
                               size, ide_atapi_cmd_reply_end);
            s->packet_transfer_size -= size;
            s->elementary_transfer_size -= size;
            s->io_buffer_index += size;
        } else {
            /* a new transfer is needed */
            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
            byte_count_limit = s->lcyl | (s->hcyl << 8);
#ifdef DEBUG_IDE_ATAPI
            printf("byte_count_limit=%d\n", byte_count_limit);
#endif
            if (byte_count_limit == 0xffff)
                byte_count_limit--;
            size = s->packet_transfer_size;
            if (size > byte_count_limit) {
                /* byte count limit must be even if this case */
                if (byte_count_limit & 1)
                    byte_count_limit--;
                size = byte_count_limit;
            }
943 944
            s->lcyl = size;
            s->hcyl = size >> 8;
B
bellard 已提交
945 946 947
            s->elementary_transfer_size = size;
            /* we cannot transmit more than one sector at a time */
            if (s->lba != -1) {
B
bellard 已提交
948 949
                if (size > (s->cd_sector_size - s->io_buffer_index))
                    size = (s->cd_sector_size - s->io_buffer_index);
B
bellard 已提交
950
            }
951
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
B
bellard 已提交
952 953 954 955
                               size, ide_atapi_cmd_reply_end);
            s->packet_transfer_size -= size;
            s->elementary_transfer_size -= size;
            s->io_buffer_index += size;
956
            ide_set_irq(s->bus);
B
bellard 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970
#ifdef DEBUG_IDE_ATAPI
            printf("status=0x%x\n", s->status);
#endif
        }
    }
}

/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
{
    if (size > max_size)
        size = max_size;
    s->lba = -1; /* no sector read */
    s->packet_transfer_size = size;
971
    s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
B
bellard 已提交
972 973 974
    s->elementary_transfer_size = 0;
    s->io_buffer_index = 0;

975
    if (s->atapi_dma) {
A
aliguori 已提交
976
    	s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
977 978
	ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
    } else {
A
aliguori 已提交
979
    	s->status = READY_STAT | SEEK_STAT;
980 981
    	ide_atapi_cmd_reply_end(s);
    }
B
bellard 已提交
982 983 984
}

/* start a CD-CDROM read command */
B
bellard 已提交
985 986
static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
                                   int sector_size)
B
bellard 已提交
987 988
{
    s->lba = lba;
B
bellard 已提交
989
    s->packet_transfer_size = nb_sectors * sector_size;
B
bellard 已提交
990
    s->elementary_transfer_size = 0;
B
bellard 已提交
991 992
    s->io_buffer_index = sector_size;
    s->cd_sector_size = sector_size;
B
bellard 已提交
993

A
aliguori 已提交
994
    s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
995 996 997
    ide_atapi_cmd_reply_end(s);
}

B
bellard 已提交
998
/* ATAPI DMA support */
999 1000 1001

/* XXX: handle read errors */
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
B
bellard 已提交
1002
{
1003
    BMDMAState *bm = opaque;
G
Gerd Hoffmann 已提交
1004
    IDEState *s = bmdma_active_if(bm);
1005 1006
    int data_offset, n;

B
bellard 已提交
1007 1008 1009 1010 1011
    if (ret < 0) {
        ide_atapi_io_error(s, ret);
        goto eot;
    }

1012
    if (s->io_buffer_size > 0) {
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	/*
	 * For a cdrom read sector command (s->lba != -1),
	 * adjust the lba for the next s->io_buffer_size chunk
	 * and dma the current chunk.
	 * For a command != read (s->lba == -1), just transfer
	 * the reply data.
	 */
	if (s->lba != -1) {
	    if (s->cd_sector_size == 2352) {
		n = 1;
		cd_data_to_raw(s->io_buffer, s->lba);
	    } else {
		n = s->io_buffer_size >> 11;
	    }
	    s->lba += n;
	}
1029 1030 1031
        s->packet_transfer_size -= s->io_buffer_size;
        if (dma_buf_rw(bm, 1) == 0)
            goto eot;
B
bellard 已提交
1032
    }
1033

B
bellard 已提交
1034
    if (s->packet_transfer_size <= 0) {
A
aliguori 已提交
1035
        s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
1036
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
1037
        ide_set_irq(s->bus);
1038 1039 1040 1041
    eot:
        bm->status &= ~BM_STATUS_DMAING;
        bm->status |= BM_STATUS_INT;
        bm->dma_cb = NULL;
G
Gerd Hoffmann 已提交
1042
        bm->unit = -1;
1043 1044 1045
        bm->aiocb = NULL;
        return;
    }
1046

1047 1048 1049 1050 1051 1052 1053
    s->io_buffer_index = 0;
    if (s->cd_sector_size == 2352) {
        n = 1;
        s->io_buffer_size = s->cd_sector_size;
        data_offset = 16;
    } else {
        n = s->packet_transfer_size >> 11;
1054 1055
        if (n > (IDE_DMA_BUF_SECTORS / 4))
            n = (IDE_DMA_BUF_SECTORS / 4);
1056 1057
        s->io_buffer_size = n * 2048;
        data_offset = 0;
B
bellard 已提交
1058
    }
1059 1060 1061
#ifdef DEBUG_AIO
    printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
#endif
1062
    bm->iov.iov_base = (void *)(s->io_buffer + data_offset);
1063 1064 1065 1066
    bm->iov.iov_len = n * 4 * 512;
    qemu_iovec_init_external(&bm->qiov, &bm->iov, 1);
    bm->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2, &bm->qiov,
                               n * 4, ide_atapi_cmd_read_dma_cb, bm);
B
bellard 已提交
1067 1068
    if (!bm->aiocb) {
        /* Note: media not present is the most likely case */
1069
        ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1070 1071 1072
                            ASC_MEDIUM_NOT_PRESENT);
        goto eot;
    }
B
bellard 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081
}

/* start a CD-CDROM read command with DMA */
/* XXX: test if DMA is available */
static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
                                   int sector_size)
{
    s->lba = lba;
    s->packet_transfer_size = nb_sectors * sector_size;
1082 1083
    s->io_buffer_index = 0;
    s->io_buffer_size = 0;
B
bellard 已提交
1084 1085
    s->cd_sector_size = sector_size;

1086
    /* XXX: check if BUSY_STAT should be set */
A
aliguori 已提交
1087
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
B
bellard 已提交
1088 1089 1090
    ide_dma_start(s, ide_atapi_cmd_read_dma_cb);
}

1091
static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
B
bellard 已提交
1092 1093 1094
                               int sector_size)
{
#ifdef DEBUG_IDE_ATAPI
1095 1096
    printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
	lba, nb_sectors);
B
bellard 已提交
1097 1098 1099 1100 1101 1102 1103 1104
#endif
    if (s->atapi_dma) {
        ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
    } else {
        ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
    }
}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
                                            uint16_t profile)
{
    uint8_t *buf_profile = buf + 12; /* start of profiles */

    buf_profile += ((*index) * 4); /* start of indexed profile */
    cpu_to_ube16 (buf_profile, profile);
    buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));

    /* each profile adds 4 bytes to the response */
    (*index)++;
    buf[11] += 4; /* Additional Length */

    return 4;
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
static int ide_dvd_read_structure(IDEState *s, int format,
                                  const uint8_t *packet, uint8_t *buf)
{
    switch (format) {
        case 0x0: /* Physical format information */
            {
                int layer = packet[6];
                uint64_t total_sectors;

                if (layer != 0)
                    return -ASC_INV_FIELD_IN_CMD_PACKET;

                bdrv_get_geometry(s->bs, &total_sectors);
                total_sectors >>= 2;
                if (total_sectors == 0)
                    return -ASC_MEDIUM_NOT_PRESENT;

                buf[4] = 1;   /* DVD-ROM, part version 1 */
                buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
                buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
                buf[7] = 0;   /* default densities */

                /* FIXME: 0x30000 per spec? */
                cpu_to_ube32(buf + 8, 0); /* start sector */
                cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
                cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */

                /* Size of buffer, not including 2 byte size field */
                cpu_to_be16wu((uint16_t *)buf, 2048 + 2);

                /* 2k data + 4 byte header */
                return (2048 + 4);
            }

        case 0x01: /* DVD copyright information */
            buf[4] = 0; /* no copyright data */
            buf[5] = 0; /* no region restrictions */

            /* Size of buffer, not including 2 byte size field */
            cpu_to_be16wu((uint16_t *)buf, 4 + 2);

            /* 4 byte header + 4 byte data */
            return (4 + 4);

        case 0x03: /* BCA information - invalid field for no BCA info */
            return -ASC_INV_FIELD_IN_CMD_PACKET;

        case 0x04: /* DVD disc manufacturing information */
            /* Size of buffer, not including 2 byte size field */
            cpu_to_be16wu((uint16_t *)buf, 2048 + 2);

            /* 2k data + 4 byte header */
            return (2048 + 4);

        case 0xff:
            /*
             * This lists all the command capabilities above.  Add new ones
             * in order and update the length and buffer return values.
             */

            buf[4] = 0x00; /* Physical format */
            buf[5] = 0x40; /* Not writable, is readable */
            cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);

            buf[8] = 0x01; /* Copyright info */
            buf[9] = 0x40; /* Not writable, is readable */
            cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);

            buf[12] = 0x03; /* BCA info */
            buf[13] = 0x40; /* Not writable, is readable */
            cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);

            buf[16] = 0x04; /* Manufacturing info */
            buf[17] = 0x40; /* Not writable, is readable */
            cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);

            /* Size of buffer, not including 2 byte size field */
            cpu_to_be16wu((uint16_t *)buf, 16 + 2);

            /* data written + 4 byte header */
            return (16 + 4);

        default: /* TODO: formats beyond DVD-ROM requires */
            return -ASC_INV_FIELD_IN_CMD_PACKET;
    }
}

B
bellard 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
static void ide_atapi_cmd(IDEState *s)
{
    const uint8_t *packet;
    uint8_t *buf;
    int max_len;

    packet = s->io_buffer;
    buf = s->io_buffer;
#ifdef DEBUG_IDE_ATAPI
    {
        int i;
        printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
        for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
            printf(" %02x", packet[i]);
        }
        printf("\n");
    }
#endif
1226 1227 1228 1229 1230 1231 1232 1233
    /* If there's a UNIT_ATTENTION condition pending, only
       REQUEST_SENSE and INQUIRY commands are allowed to complete. */
    if (s->sense_key == SENSE_UNIT_ATTENTION &&
	s->io_buffer[0] != GPCMD_REQUEST_SENSE &&
	s->io_buffer[0] != GPCMD_INQUIRY) {
	ide_atapi_cmd_check_status(s);
	return;
    }
B
bellard 已提交
1234 1235
    switch(s->io_buffer[0]) {
    case GPCMD_TEST_UNIT_READY:
G
Gleb Natapov 已提交
1236
        if (bdrv_is_inserted(s->bs) && !s->cdrom_changed) {
B
bellard 已提交
1237 1238
            ide_atapi_cmd_ok(s);
        } else {
G
Gleb Natapov 已提交
1239
            s->cdrom_changed = 0;
1240
            ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1241 1242 1243
                                ASC_MEDIUM_NOT_PRESENT);
        }
        break;
1244
    case GPCMD_MODE_SENSE_6:
B
bellard 已提交
1245 1246 1247
    case GPCMD_MODE_SENSE_10:
        {
            int action, code;
1248 1249 1250 1251
            if (packet[0] == GPCMD_MODE_SENSE_10)
                max_len = ube16_to_cpu(packet + 7);
            else
                max_len = packet[4];
B
bellard 已提交
1252 1253 1254 1255 1256
            action = packet[2] >> 6;
            code = packet[2] & 0x3f;
            switch(action) {
            case 0: /* current values */
                switch(code) {
1257
                case GPMODE_R_W_ERROR_PAGE: /* error recovery */
B
bellard 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
                    cpu_to_ube16(&buf[0], 16 + 6);
                    buf[2] = 0x70;
                    buf[3] = 0;
                    buf[4] = 0;
                    buf[5] = 0;
                    buf[6] = 0;
                    buf[7] = 0;

                    buf[8] = 0x01;
                    buf[9] = 0x06;
                    buf[10] = 0x00;
                    buf[11] = 0x05;
                    buf[12] = 0x00;
                    buf[13] = 0x00;
                    buf[14] = 0x00;
                    buf[15] = 0x00;
                    ide_atapi_cmd_reply(s, 16, max_len);
                    break;
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
                case GPMODE_AUDIO_CTL_PAGE:
                    cpu_to_ube16(&buf[0], 24 + 6);
                    buf[2] = 0x70;
                    buf[3] = 0;
                    buf[4] = 0;
                    buf[5] = 0;
                    buf[6] = 0;
                    buf[7] = 0;

                    /* Fill with CDROM audio volume */
                    buf[17] = 0;
                    buf[19] = 0;
                    buf[21] = 0;
                    buf[23] = 0;

                    ide_atapi_cmd_reply(s, 24, max_len);
                    break;
1293
                case GPMODE_CAPABILITIES_PAGE:
B
bellard 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
                    cpu_to_ube16(&buf[0], 28 + 6);
                    buf[2] = 0x70;
                    buf[3] = 0;
                    buf[4] = 0;
                    buf[5] = 0;
                    buf[6] = 0;
                    buf[7] = 0;

                    buf[8] = 0x2a;
                    buf[9] = 0x12;
1304
                    buf[10] = 0x00;
B
bellard 已提交
1305
                    buf[11] = 0x00;
1306

1307 1308 1309
                    /* Claim PLAY_AUDIO capability (0x01) since some Linux
                       code checks for this to automount media. */
                    buf[12] = 0x71;
B
bellard 已提交
1310 1311
                    buf[13] = 3 << 5;
                    buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
1312
                    if (bdrv_is_locked(s->bs))
B
bellard 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
                        buf[6] |= 1 << 1;
                    buf[15] = 0x00;
                    cpu_to_ube16(&buf[16], 706);
                    buf[18] = 0;
                    buf[19] = 2;
                    cpu_to_ube16(&buf[20], 512);
                    cpu_to_ube16(&buf[22], 706);
                    buf[24] = 0;
                    buf[25] = 0;
                    buf[26] = 0;
                    buf[27] = 0;
                    ide_atapi_cmd_reply(s, 28, max_len);
                    break;
                default:
                    goto error_cmd;
                }
                break;
            case 1: /* changeable values */
                goto error_cmd;
            case 2: /* default values */
                goto error_cmd;
            default:
            case 3: /* saved values */
1336
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
B
bellard 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
                                    ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
                break;
            }
        }
        break;
    case GPCMD_REQUEST_SENSE:
        max_len = packet[4];
        memset(buf, 0, 18);
        buf[0] = 0x70 | (1 << 7);
        buf[2] = s->sense_key;
        buf[7] = 10;
        buf[12] = s->asc;
1349 1350
        if (s->sense_key == SENSE_UNIT_ATTENTION)
            s->sense_key = SENSE_NONE;
B
bellard 已提交
1351 1352 1353
        ide_atapi_cmd_reply(s, 18, max_len);
        break;
    case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
1354 1355
        if (bdrv_is_inserted(s->bs)) {
            bdrv_set_locked(s->bs, packet[4] & 1);
B
bellard 已提交
1356 1357
            ide_atapi_cmd_ok(s);
        } else {
1358
            ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
                                ASC_MEDIUM_NOT_PRESENT);
        }
        break;
    case GPCMD_READ_10:
    case GPCMD_READ_12:
        {
            int nb_sectors, lba;

            if (packet[0] == GPCMD_READ_10)
                nb_sectors = ube16_to_cpu(packet + 7);
            else
                nb_sectors = ube32_to_cpu(packet + 6);
            lba = ube32_to_cpu(packet + 2);
            if (nb_sectors == 0) {
                ide_atapi_cmd_ok(s);
                break;
            }
B
bellard 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
            ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
        }
        break;
    case GPCMD_READ_CD:
        {
            int nb_sectors, lba, transfer_request;

            nb_sectors = (packet[6] << 16) | (packet[7] << 8) | packet[8];
            lba = ube32_to_cpu(packet + 2);
            if (nb_sectors == 0) {
                ide_atapi_cmd_ok(s);
                break;
            }
            transfer_request = packet[9];
            switch(transfer_request & 0xf8) {
            case 0x00:
                /* nothing */
                ide_atapi_cmd_ok(s);
                break;
            case 0x10:
                /* normal read */
                ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
                break;
            case 0xf8:
                /* read all data */
                ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
                break;
            default:
1404
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
B
bellard 已提交
1405 1406 1407
                                    ASC_INV_FIELD_IN_CMD_PACKET);
                break;
            }
B
bellard 已提交
1408 1409 1410 1411
        }
        break;
    case GPCMD_SEEK:
        {
1412 1413
            unsigned int lba;
            uint64_t total_sectors;
B
bellard 已提交
1414 1415 1416

            bdrv_get_geometry(s->bs, &total_sectors);
            total_sectors >>= 2;
1417
            if (total_sectors == 0) {
1418
                ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1419 1420 1421 1422
                                    ASC_MEDIUM_NOT_PRESENT);
                break;
            }
            lba = ube32_to_cpu(packet + 2);
B
bellard 已提交
1423
            if (lba >= total_sectors) {
1424
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
B
bellard 已提交
1425 1426 1427 1428 1429 1430 1431 1432
                                    ASC_LOGICAL_BLOCK_OOR);
                break;
            }
            ide_atapi_cmd_ok(s);
        }
        break;
    case GPCMD_START_STOP_UNIT:
        {
1433
            int start, eject, err = 0;
B
bellard 已提交
1434 1435
            start = packet[4] & 1;
            eject = (packet[4] >> 1) & 1;
1436

1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
            if (eject) {
                err = bdrv_eject(s->bs, !start);
            }

            switch (err) {
            case 0:
                ide_atapi_cmd_ok(s);
                break;
            case -EBUSY:
                ide_atapi_cmd_error(s, SENSE_NOT_READY,
                                    ASC_MEDIA_REMOVAL_PREVENTED);
                break;
            default:
                ide_atapi_cmd_error(s, SENSE_NOT_READY,
                                    ASC_MEDIUM_NOT_PRESENT);
                break;
1453
            }
B
bellard 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
        }
        break;
    case GPCMD_MECHANISM_STATUS:
        {
            max_len = ube16_to_cpu(packet + 8);
            cpu_to_ube16(buf, 0);
            /* no current LBA */
            buf[2] = 0;
            buf[3] = 0;
            buf[4] = 0;
            buf[5] = 1;
            cpu_to_ube16(buf + 6, 0);
            ide_atapi_cmd_reply(s, 8, max_len);
        }
        break;
    case GPCMD_READ_TOC_PMA_ATIP:
        {
            int format, msf, start_track, len;
1472
            uint64_t total_sectors;
B
bellard 已提交
1473

B
bellard 已提交
1474 1475
            bdrv_get_geometry(s->bs, &total_sectors);
            total_sectors >>= 2;
1476
            if (total_sectors == 0) {
1477
                ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486
                                    ASC_MEDIUM_NOT_PRESENT);
                break;
            }
            max_len = ube16_to_cpu(packet + 7);
            format = packet[9] >> 6;
            msf = (packet[1] >> 1) & 1;
            start_track = packet[6];
            switch(format) {
            case 0:
B
bellard 已提交
1487
                len = cdrom_read_toc(total_sectors, buf, msf, start_track);
B
bellard 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
                if (len < 0)
                    goto error_cmd;
                ide_atapi_cmd_reply(s, len, max_len);
                break;
            case 1:
                /* multi session : only a single session defined */
                memset(buf, 0, 12);
                buf[1] = 0x0a;
                buf[2] = 0x01;
                buf[3] = 0x01;
                ide_atapi_cmd_reply(s, 12, max_len);
                break;
B
bellard 已提交
1500
            case 2:
B
bellard 已提交
1501
                len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
B
bellard 已提交
1502 1503 1504 1505
                if (len < 0)
                    goto error_cmd;
                ide_atapi_cmd_reply(s, len, max_len);
                break;
B
bellard 已提交
1506
            default:
1507
            error_cmd:
1508
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1509 1510
                                    ASC_INV_FIELD_IN_CMD_PACKET);
                break;
B
bellard 已提交
1511 1512 1513 1514
            }
        }
        break;
    case GPCMD_READ_CDVD_CAPACITY:
B
bellard 已提交
1515
        {
1516
            uint64_t total_sectors;
B
bellard 已提交
1517 1518 1519

            bdrv_get_geometry(s->bs, &total_sectors);
            total_sectors >>= 2;
1520
            if (total_sectors == 0) {
1521
                ide_atapi_cmd_error(s, SENSE_NOT_READY,
B
bellard 已提交
1522 1523 1524 1525 1526 1527 1528
                                    ASC_MEDIUM_NOT_PRESENT);
                break;
            }
            /* NOTE: it is really the number of sectors minus 1 */
            cpu_to_ube32(buf, total_sectors - 1);
            cpu_to_ube32(buf + 4, 2048);
            ide_atapi_cmd_reply(s, 8, 8);
B
bellard 已提交
1529 1530
        }
        break;
1531 1532 1533
    case GPCMD_READ_DVD_STRUCTURE:
        {
            int media = packet[1];
1534 1535
            int format = packet[7];
            int ret;
1536

1537
            max_len = ube16_to_cpu(packet + 8);
1538

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
            if (format < 0xff) {
                if (media_is_cd(s)) {
                    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
                                        ASC_INCOMPATIBLE_FORMAT);
                    break;
                } else if (!media_present(s)) {
                    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
                                        ASC_INV_FIELD_IN_CMD_PACKET);
                    break;
                }
            }
1550

1551 1552
            memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
                   IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1553

1554 1555 1556 1557 1558
            switch (format) {
                case 0x00 ... 0x7f:
                case 0xff:
                    if (media == 0) {
                        ret = ide_dvd_read_structure(s, format, packet, buf);
1559

1560 1561 1562 1563
                        if (ret < 0)
                            ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
                        else
                            ide_atapi_cmd_reply(s, ret, max_len);
1564

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
                        break;
                    }
                    /* TODO: BD support, fall through for now */

                /* Generic disk structures */
                case 0x80: /* TODO: AACS volume identifier */
                case 0x81: /* TODO: AACS media serial number */
                case 0x82: /* TODO: AACS media identifier */
                case 0x83: /* TODO: AACS media key block */
                case 0x90: /* TODO: List of recognized format layers */
                case 0xc0: /* TODO: Write protection status */
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
                default:
                    ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
                                        ASC_INV_FIELD_IN_CMD_PACKET);
                    break;
            }
        }
        break;
    case GPCMD_SET_SPEED:
        ide_atapi_cmd_ok(s);
        break;
B
bellard 已提交
1586 1587 1588 1589 1590 1591
    case GPCMD_INQUIRY:
        max_len = packet[4];
        buf[0] = 0x05; /* CD-ROM */
        buf[1] = 0x80; /* removable */
        buf[2] = 0x00; /* ISO */
        buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
T
ths 已提交
1592
        buf[4] = 31; /* additional length */
B
bellard 已提交
1593 1594 1595 1596
        buf[5] = 0; /* reserved */
        buf[6] = 0; /* reserved */
        buf[7] = 0; /* reserved */
        padstr8(buf + 8, 8, "QEMU");
1597
        padstr8(buf + 16, 16, "QEMU DVD-ROM");
G
Gerd Hoffmann 已提交
1598
        padstr8(buf + 32, 4, s->version);
B
bellard 已提交
1599 1600
        ide_atapi_cmd_reply(s, 36, max_len);
        break;
1601 1602
    case GPCMD_GET_CONFIGURATION:
        {
1603
            uint32_t len;
1604
            uint8_t index = 0;
1605 1606 1607 1608 1609 1610 1611

            /* only feature 0 is supported */
            if (packet[2] != 0 || packet[3] != 0) {
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
                                    ASC_INV_FIELD_IN_CMD_PACKET);
                break;
            }
1612 1613 1614

            /* XXX: could result in alignment problems in some architectures */
            max_len = ube16_to_cpu(packet + 7);
1615

1616
            /*
1617 1618 1619
             * XXX: avoid overflow for io_buffer if max_len is bigger than
             *      the size of that buffer (dimensioned to max number of
             *      sectors to transfer at once)
1620
             *
1621
             *      Only a problem if the feature/profiles grow.
1622 1623 1624 1625 1626 1627 1628 1629 1630
             */
            if (max_len > 512) /* XXX: assume 1 sector */
                max_len = 512;

            memset(buf, 0, max_len);
            /* 
             * the number of sectors from the media tells us which profile
             * to use as current.  0 means there is no media
             */
1631 1632 1633 1634
            if (media_is_dvd(s))
                cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
            else if (media_is_cd(s))
                cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
1635

1636 1637 1638 1639
            buf[10] = 0x02 | 0x01; /* persistent and current */
            len = 12; /* headers: 8 + 4 */
            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
            len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
1640 1641 1642
            cpu_to_ube32(buf, len - 4); /* data length */

            ide_atapi_cmd_reply(s, len, max_len);
1643 1644
            break;
        }
B
bellard 已提交
1645
    default:
1646
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1647
                            ASC_ILLEGAL_OPCODE);
B
bellard 已提交
1648 1649 1650 1651
        break;
    }
}

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 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
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 已提交
1705 1706 1707 1708
/* called when the inserted state of the media has changed */
static void cdrom_change_cb(void *opaque)
{
    IDEState *s = opaque;
1709
    uint64_t nb_sectors;
T
ths 已提交
1710 1711 1712

    bdrv_get_geometry(s->bs, &nb_sectors);
    s->nb_sectors = nb_sectors;
1713 1714 1715

    s->sense_key = SENSE_UNIT_ATTENTION;
    s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
G
Gleb Natapov 已提交
1716
    s->cdrom_changed = 1;
1717
    ide_set_irq(s->bus);
T
ths 已提交
1718 1719
}

B
bellard 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
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 已提交
1743
static void ide_clear_hob(IDEBus *bus)
B
bellard 已提交
1744 1745
{
    /* any write clears HOB high bit of device control register */
G
Gerd Hoffmann 已提交
1746 1747
    bus->ifs[0].select &= ~(1 << 7);
    bus->ifs[1].select &= ~(1 << 7);
B
bellard 已提交
1748 1749
}

G
Gerd Hoffmann 已提交
1750
void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1751
{
G
Gerd Hoffmann 已提交
1752
    IDEBus *bus = opaque;
1753
    IDEState *s;
G
Gerd Hoffmann 已提交
1754
    int n;
B
bellard 已提交
1755
    int lba48 = 0;
B
bellard 已提交
1756 1757 1758 1759

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

B
bellard 已提交
1761
    addr &= 7;
1762 1763

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

B
bellard 已提交
1767 1768 1769 1770
    switch(addr) {
    case 0:
        break;
    case 1:
G
Gerd Hoffmann 已提交
1771
	ide_clear_hob(bus);
1772
        /* NOTE: data is written to the two drives */
G
Gerd Hoffmann 已提交
1773 1774 1775 1776
	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 已提交
1777 1778
        break;
    case 2:
G
Gerd Hoffmann 已提交
1779 1780 1781 1782 1783
	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 已提交
1784 1785
        break;
    case 3:
G
Gerd Hoffmann 已提交
1786 1787 1788 1789 1790
	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 已提交
1791 1792
        break;
    case 4:
G
Gerd Hoffmann 已提交
1793 1794 1795 1796 1797
	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 已提交
1798 1799
        break;
    case 5:
G
Gerd Hoffmann 已提交
1800 1801 1802 1803 1804
	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 已提交
1805 1806
        break;
    case 6:
B
bellard 已提交
1807
	/* FIXME: HOB readback uses bit 7 */
G
Gerd Hoffmann 已提交
1808 1809
        bus->ifs[0].select = (val & ~0x10) | 0xa0;
        bus->ifs[1].select = (val | 0x10) | 0xa0;
B
bellard 已提交
1810
        /* select drive */
G
Gerd Hoffmann 已提交
1811
        bus->unit = (val >> 4) & 1;
B
bellard 已提交
1812 1813 1814 1815 1816 1817 1818
        break;
    default:
    case 7:
        /* command */
#if defined(DEBUG_IDE)
        printf("ide: CMD=%02x\n", val);
#endif
G
Gerd Hoffmann 已提交
1819
        s = idebus_active_if(bus);
1820
        /* ignore commands to non existant slave */
G
Gerd Hoffmann 已提交
1821
        if (s != bus->ifs && !s->bs)
1822
            break;
B
bellard 已提交
1823

1824 1825 1826 1827
        /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */
        if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
            break;

B
bellard 已提交
1828 1829 1830
        switch(val) {
        case WIN_IDENTIFY:
            if (s->bs && !s->is_cdrom) {
1831 1832 1833 1834
                if (!s->is_cf)
                    ide_identify(s);
                else
                    ide_cfata_identify(s);
1835
                s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
1836 1837 1838 1839 1840 1841 1842
                ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
            } else {
                if (s->is_cdrom) {
                    ide_set_signature(s);
                }
                ide_abort_command(s);
            }
1843
            ide_set_irq(s->bus);
B
bellard 已提交
1844 1845 1846
            break;
        case WIN_SPECIFY:
        case WIN_RECAL:
1847
            s->error = 0;
B
bellard 已提交
1848
            s->status = READY_STAT | SEEK_STAT;
1849
            ide_set_irq(s->bus);
B
bellard 已提交
1850 1851
            break;
        case WIN_SETMULT:
1852 1853 1854
            if (s->is_cf && s->nsector == 0) {
                /* Disable Read and Write Multiple */
                s->mult_sectors = 0;
A
aliguori 已提交
1855
                s->status = READY_STAT | SEEK_STAT;
1856
            } else if ((s->nsector & 0xff) != 0 &&
1857 1858
                ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
                 (s->nsector & (s->nsector - 1)) != 0)) {
B
bellard 已提交
1859 1860
                ide_abort_command(s);
            } else {
T
ths 已提交
1861
                s->mult_sectors = s->nsector & 0xff;
A
aliguori 已提交
1862
                s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
1863
            }
1864
            ide_set_irq(s->bus);
B
bellard 已提交
1865
            break;
B
bellard 已提交
1866 1867
        case WIN_VERIFY_EXT:
	    lba48 = 1;
B
bellard 已提交
1868 1869 1870
        case WIN_VERIFY:
        case WIN_VERIFY_ONCE:
            /* do sector number check ? */
B
bellard 已提交
1871
	    ide_cmd_lba48_transform(s, lba48);
A
aliguori 已提交
1872
            s->status = READY_STAT | SEEK_STAT;
1873
            ide_set_irq(s->bus);
B
bellard 已提交
1874
            break;
B
bellard 已提交
1875 1876
	case WIN_READ_EXT:
	    lba48 = 1;
B
bellard 已提交
1877 1878
        case WIN_READ:
        case WIN_READ_ONCE:
1879
            if (!s->bs)
B
bellard 已提交
1880
                goto abort_cmd;
B
bellard 已提交
1881
	    ide_cmd_lba48_transform(s, lba48);
B
bellard 已提交
1882 1883 1884
            s->req_nb_sectors = 1;
            ide_sector_read(s);
            break;
B
bellard 已提交
1885 1886
	case WIN_WRITE_EXT:
	    lba48 = 1;
B
bellard 已提交
1887 1888
        case WIN_WRITE:
        case WIN_WRITE_ONCE:
1889 1890
        case CFA_WRITE_SECT_WO_ERASE:
        case WIN_WRITE_VERIFY:
B
bellard 已提交
1891
	    ide_cmd_lba48_transform(s, lba48);
1892
            s->error = 0;
1893
            s->status = SEEK_STAT | READY_STAT;
B
bellard 已提交
1894 1895
            s->req_nb_sectors = 1;
            ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1896
            s->media_changed = 1;
B
bellard 已提交
1897
            break;
B
bellard 已提交
1898 1899
	case WIN_MULTREAD_EXT:
	    lba48 = 1;
B
bellard 已提交
1900 1901 1902
        case WIN_MULTREAD:
            if (!s->mult_sectors)
                goto abort_cmd;
B
bellard 已提交
1903
	    ide_cmd_lba48_transform(s, lba48);
B
bellard 已提交
1904 1905 1906
            s->req_nb_sectors = s->mult_sectors;
            ide_sector_read(s);
            break;
B
bellard 已提交
1907 1908
        case WIN_MULTWRITE_EXT:
	    lba48 = 1;
B
bellard 已提交
1909
        case WIN_MULTWRITE:
1910
        case CFA_WRITE_MULTI_WO_ERASE:
B
bellard 已提交
1911 1912
            if (!s->mult_sectors)
                goto abort_cmd;
B
bellard 已提交
1913
	    ide_cmd_lba48_transform(s, lba48);
1914
            s->error = 0;
1915
            s->status = SEEK_STAT | READY_STAT;
B
bellard 已提交
1916 1917 1918 1919 1920
            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);
1921
            s->media_changed = 1;
B
bellard 已提交
1922
            break;
B
bellard 已提交
1923 1924
	case WIN_READDMA_EXT:
	    lba48 = 1;
B
bellard 已提交
1925 1926
        case WIN_READDMA:
        case WIN_READDMA_ONCE:
1927
            if (!s->bs)
B
bellard 已提交
1928
                goto abort_cmd;
B
bellard 已提交
1929
	    ide_cmd_lba48_transform(s, lba48);
B
bellard 已提交
1930 1931
            ide_sector_read_dma(s);
            break;
B
bellard 已提交
1932 1933
	case WIN_WRITEDMA_EXT:
	    lba48 = 1;
B
bellard 已提交
1934 1935
        case WIN_WRITEDMA:
        case WIN_WRITEDMA_ONCE:
1936
            if (!s->bs)
B
bellard 已提交
1937
                goto abort_cmd;
B
bellard 已提交
1938
	    ide_cmd_lba48_transform(s, lba48);
B
bellard 已提交
1939
            ide_sector_write_dma(s);
1940
            s->media_changed = 1;
B
bellard 已提交
1941
            break;
B
bellard 已提交
1942 1943
        case WIN_READ_NATIVE_MAX_EXT:
	    lba48 = 1;
B
bellard 已提交
1944
        case WIN_READ_NATIVE_MAX:
B
bellard 已提交
1945
	    ide_cmd_lba48_transform(s, lba48);
B
bellard 已提交
1946
            ide_set_sector(s, s->nb_sectors - 1);
A
aliguori 已提交
1947
            s->status = READY_STAT | SEEK_STAT;
1948
            ide_set_irq(s->bus);
B
bellard 已提交
1949
            break;
1950
        case WIN_CHECKPOWERMODE1:
1951
        case WIN_CHECKPOWERMODE2:
1952
            s->nsector = 0xff; /* device active or idle */
A
aliguori 已提交
1953
            s->status = READY_STAT | SEEK_STAT;
1954
            ide_set_irq(s->bus);
1955
            break;
1956 1957 1958 1959 1960
        case WIN_SETFEATURES:
            if (!s->bs)
                goto abort_cmd;
            /* XXX: valid for CDROM ? */
            switch(s->feature) {
1961 1962
            case 0xcc: /* reverting to power-on defaults enable */
            case 0x66: /* reverting to power-on defaults disable */
1963 1964 1965 1966
            case 0x02: /* write cache enable */
            case 0x82: /* write cache disable */
            case 0xaa: /* read look-ahead enable */
            case 0x55: /* read look-ahead disable */
1967 1968 1969 1970 1971 1972
            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 */
1973 1974
            case 0x42: /* enable Automatic Acoustic Mode */
            case 0xc2: /* disable Automatic Acoustic Mode */
B
bellard 已提交
1975
                s->status = READY_STAT | SEEK_STAT;
1976
                ide_set_irq(s->bus);
1977
                break;
1978 1979
            case 0x03: { /* set transfer mode */
		uint8_t val = s->nsector & 0x07;
1980
                uint16_t *identify_data = (uint16_t *)s->identify_data;
1981 1982 1983 1984

		switch (s->nsector >> 3) {
		    case 0x00: /* pio default */
		    case 0x01: /* pio mode */
1985 1986 1987
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f);
1988 1989
			break;
                    case 0x02: /* sigle word dma mode*/
1990 1991 1992
			put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f);
1993 1994
			break;
		    case 0x04: /* mdma mode */
1995 1996 1997
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
			put_le16(identify_data + 88,0x3f);
1998 1999
			break;
		    case 0x08: /* udma mode */
2000 2001 2002
			put_le16(identify_data + 62,0x07);
			put_le16(identify_data + 63,0x07);
			put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
2003 2004 2005 2006 2007
			break;
		    default:
			goto abort_cmd;
		}
                s->status = READY_STAT | SEEK_STAT;
2008
                ide_set_irq(s->bus);
2009 2010
                break;
	    }
2011 2012 2013 2014
            default:
                goto abort_cmd;
            }
            break;
B
bellard 已提交
2015 2016
        case WIN_FLUSH_CACHE:
        case WIN_FLUSH_CACHE_EXT:
P
pbrook 已提交
2017
            if (s->bs)
C
Christoph Hellwig 已提交
2018 2019 2020
                bdrv_aio_flush(s->bs, ide_flush_cb, s);
            else
                ide_flush_cb(s, 0);
P
pbrook 已提交
2021
            break;
2022 2023 2024
        case WIN_STANDBY:
        case WIN_STANDBY2:
        case WIN_STANDBYNOW1:
2025
        case WIN_STANDBYNOW2:
2026
        case WIN_IDLEIMMEDIATE:
2027 2028 2029
        case CFA_IDLEIMMEDIATE:
        case WIN_SETIDLE1:
        case WIN_SETIDLE2:
2030 2031 2032
        case WIN_SLEEPNOW1:
        case WIN_SLEEPNOW2:
            s->status = READY_STAT;
2033
            ide_set_irq(s->bus);
2034
            break;
A
aurel32 已提交
2035 2036 2037 2038 2039
        case WIN_SEEK:
            if(s->is_cdrom)
                goto abort_cmd;
            /* XXX: Check that seek is within bounds */
            s->status = READY_STAT | SEEK_STAT;
2040
            ide_set_irq(s->bus);
A
aurel32 已提交
2041
            break;
B
bellard 已提交
2042 2043 2044 2045
            /* ATAPI commands */
        case WIN_PIDENTIFY:
            if (s->is_cdrom) {
                ide_atapi_identify(s);
B
bellard 已提交
2046
                s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
2047 2048 2049 2050
                ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
            } else {
                ide_abort_command(s);
            }
2051
            ide_set_irq(s->bus);
B
bellard 已提交
2052
            break;
2053 2054
        case WIN_DIAGNOSE:
            ide_set_signature(s);
2055 2056 2057 2058 2059 2060 2061 2062 2063
            if (s->is_cdrom)
                s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
                                * devices to return a clear status register
                                * with READY_STAT *not* set. */
            else
                s->status = READY_STAT | SEEK_STAT;
            s->error = 0x01; /* Device 0 passed, Device 1 passed or not
                              * present. 
                              */
2064
            ide_set_irq(s->bus);
2065
            break;
B
bellard 已提交
2066 2067 2068 2069
        case WIN_SRST:
            if (!s->is_cdrom)
                goto abort_cmd;
            ide_set_signature(s);
B
bellard 已提交
2070
            s->status = 0x00; /* NOTE: READY is _not_ set */
B
bellard 已提交
2071 2072 2073 2074 2075
            s->error = 0x01;
            break;
        case WIN_PACKETCMD:
            if (!s->is_cdrom)
                goto abort_cmd;
B
bellard 已提交
2076 2077
            /* overlapping commands not supported */
            if (s->feature & 0x02)
B
bellard 已提交
2078
                goto abort_cmd;
A
aliguori 已提交
2079
            s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
2080
            s->atapi_dma = s->feature & 1;
B
bellard 已提交
2081
            s->nsector = 1;
2082
            ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
B
bellard 已提交
2083 2084
                               ide_atapi_cmd);
            break;
2085 2086 2087 2088 2089
        /* CF-ATA commands */
        case CFA_REQ_EXT_ERROR_CODE:
            if (!s->is_cf)
                goto abort_cmd;
            s->error = 0x09;    /* miscellaneous error */
A
aliguori 已提交
2090
            s->status = READY_STAT | SEEK_STAT;
2091
            ide_set_irq(s->bus);
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
            break;
        case CFA_ERASE_SECTORS:
        case CFA_WEAR_LEVEL:
            if (!s->is_cf)
                goto abort_cmd;
            if (val == CFA_WEAR_LEVEL)
                s->nsector = 0;
            if (val == CFA_ERASE_SECTORS)
                s->media_changed = 1;
            s->error = 0x00;
A
aliguori 已提交
2102
            s->status = READY_STAT | SEEK_STAT;
2103
            ide_set_irq(s->bus);
2104 2105 2106 2107 2108
            break;
        case CFA_TRANSLATE_SECTOR:
            if (!s->is_cf)
                goto abort_cmd;
            s->error = 0x00;
A
aliguori 已提交
2109
            s->status = READY_STAT | SEEK_STAT;
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
            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);
2123
            ide_set_irq(s->bus);
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
            break;
        case CFA_ACCESS_METADATA_STORAGE:
            if (!s->is_cf)
                goto abort_cmd;
            switch (s->feature) {
            case 0x02:	/* Inquiry Metadata Storage */
                ide_cfata_metadata_inquiry(s);
                break;
            case 0x03:	/* Read Metadata Storage */
                ide_cfata_metadata_read(s);
                break;
            case 0x04:	/* Write Metadata Storage */
                ide_cfata_metadata_write(s);
                break;
            default:
                goto abort_cmd;
            }
            ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
            s->status = 0x00; /* NOTE: READY is _not_ set */
2143
            ide_set_irq(s->bus);
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
            break;
        case IBM_SENSE_CONDITION:
            if (!s->is_cf)
                goto abort_cmd;
            switch (s->feature) {
            case 0x01:  /* sense temperature in device */
                s->nsector = 0x50;      /* +20 C */
                break;
            default:
                goto abort_cmd;
            }
A
aliguori 已提交
2155
            s->status = READY_STAT | SEEK_STAT;
2156
            ide_set_irq(s->bus);
2157
            break;
B
Brian Wheeler 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169

	case WIN_SMART:
	    if (s->is_cdrom)
		goto abort_cmd;
	    if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
		goto abort_cmd;
	    if (!s->smart_enabled && s->feature != SMART_ENABLE)
		goto abort_cmd;
	    switch (s->feature) {
	    case SMART_DISABLE:
		s->smart_enabled = 0;
		s->status = READY_STAT | SEEK_STAT;
2170
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2171 2172 2173 2174
		break;
	    case SMART_ENABLE:
		s->smart_enabled = 1;
		s->status = READY_STAT | SEEK_STAT;
2175
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
		break;
	    case SMART_ATTR_AUTOSAVE:
		switch (s->sector) {
		case 0x00:
		    s->smart_autosave = 0;
		    break;
		case 0xf1:
		    s->smart_autosave = 1;
		    break;
		default:
		    goto abort_cmd;
		}
		s->status = READY_STAT | SEEK_STAT;
2189
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
		break;
	    case SMART_STATUS:
		if (!s->smart_errors) {
		    s->hcyl = 0xc2;
		    s->lcyl = 0x4f;
		} else {
		    s->hcyl = 0x2c;
		    s->lcyl = 0xf4;
		}
		s->status = READY_STAT | SEEK_STAT;
2200
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
		break;
	    case SMART_READ_THRESH:
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01; /* smart struct version */
		for (n=0; n<30; n++) {
		    if (smart_attributes[n][0] == 0)
			break;
		    s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
		    s->io_buffer[2+1+(n*12)] = smart_attributes[n][4];
		}
		for (n=0; n<511; n++) /* checksum */
		    s->io_buffer[511] += s->io_buffer[n];
		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);
2216
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
		break;
	    case SMART_READ_DATA:
		memset(s->io_buffer, 0, 0x200);
		s->io_buffer[0] = 0x01; /* smart struct version */
		for (n=0; n<30; n++) {
		    if (smart_attributes[n][0] == 0)
			break;
		    s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
		    s->io_buffer[2+1+(n*12)] = smart_attributes[n][1];
		    s->io_buffer[2+3+(n*12)] = smart_attributes[n][2];
		    s->io_buffer[2+4+(n*12)] = smart_attributes[n][3];
		}
		s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
		if (s->smart_selftest_count == 0) {
		    s->io_buffer[363] = 0;
		} else {
		    s->io_buffer[363] = 
			s->smart_selftest_data[3 + 
					       (s->smart_selftest_count - 1) * 
					       24];
		}
		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++) 
		    s->io_buffer[511] += s->io_buffer[n];
		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);
2254
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
		break;
	    case SMART_READ_LOG:
		switch (s->sector) {
		case 0x01: /* summary smart error log */
		    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;

		    for (n=0; n<511; n++)
			s->io_buffer[511] += s->io_buffer[n];
		    s->io_buffer[511] = 0x100 - s->io_buffer[511];
		    break;
		case 0x06: /* smart self test log */
		    memset(s->io_buffer, 0, 0x200);
		    s->io_buffer[0] = 0x01; 
		    if (s->smart_selftest_count == 0) {
			s->io_buffer[508] = 0;
		    } else {
			s->io_buffer[508] = s->smart_selftest_count;
			for (n=2; n<506; n++) 
			    s->io_buffer[n] = s->smart_selftest_data[n];
		    }		    
		    for (n=0; n<511; n++)
			s->io_buffer[511] += s->io_buffer[n];
		    s->io_buffer[511] = 0x100 - s->io_buffer[511];
		    break;
		default:
		    goto abort_cmd;
		}
		s->status = READY_STAT | SEEK_STAT;
		ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
2288
		ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
		break;
	    case SMART_EXECUTE_OFFLINE:
		switch (s->sector) {
		case 0: /* off-line routine */
		case 1: /* short self test */
		case 2: /* extended self test */
		    s->smart_selftest_count++;
		    if(s->smart_selftest_count > 21)
			s->smart_selftest_count = 0;
		    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;
2304
		    ide_set_irq(s->bus);
B
Brian Wheeler 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313
		    break;
		default:
		    goto abort_cmd;
		}
		break;
	    default:
		goto abort_cmd;
	    }
	    break;
B
bellard 已提交
2314 2315 2316
        default:
        abort_cmd:
            ide_abort_command(s);
2317
            ide_set_irq(s->bus);
B
bellard 已提交
2318 2319 2320 2321 2322
            break;
        }
    }
}

G
Gerd Hoffmann 已提交
2323
uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
B
bellard 已提交
2324
{
G
Gerd Hoffmann 已提交
2325 2326
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2327
    uint32_t addr;
B
bellard 已提交
2328
    int ret, hob;
B
bellard 已提交
2329 2330

    addr = addr1 & 7;
B
bellard 已提交
2331 2332 2333
    /* FIXME: HOB readback uses bit 7, but it's always set right now */
    //hob = s->select & (1 << 7);
    hob = 0;
B
bellard 已提交
2334 2335 2336 2337 2338
    switch(addr) {
    case 0:
        ret = 0xff;
        break;
    case 1:
G
Gerd Hoffmann 已提交
2339 2340
        if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
            (s != bus->ifs && !s->bs))
2341
            ret = 0;
B
bellard 已提交
2342
        else if (!hob)
2343
            ret = s->error;
B
bellard 已提交
2344 2345
	else
	    ret = s->hob_feature;
B
bellard 已提交
2346 2347
        break;
    case 2:
G
Gerd Hoffmann 已提交
2348
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2349
            ret = 0;
B
bellard 已提交
2350
        else if (!hob)
2351
            ret = s->nsector & 0xff;
B
bellard 已提交
2352 2353
	else
	    ret = s->hob_nsector;
B
bellard 已提交
2354 2355
        break;
    case 3:
G
Gerd Hoffmann 已提交
2356
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2357
            ret = 0;
B
bellard 已提交
2358
        else if (!hob)
2359
            ret = s->sector;
B
bellard 已提交
2360 2361
	else
	    ret = s->hob_sector;
B
bellard 已提交
2362 2363
        break;
    case 4:
G
Gerd Hoffmann 已提交
2364
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2365
            ret = 0;
B
bellard 已提交
2366
        else if (!hob)
2367
            ret = s->lcyl;
B
bellard 已提交
2368 2369
	else
	    ret = s->hob_lcyl;
B
bellard 已提交
2370 2371
        break;
    case 5:
G
Gerd Hoffmann 已提交
2372
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2373
            ret = 0;
B
bellard 已提交
2374
        else if (!hob)
2375
            ret = s->hcyl;
B
bellard 已提交
2376 2377
	else
	    ret = s->hob_hcyl;
B
bellard 已提交
2378 2379
        break;
    case 6:
G
Gerd Hoffmann 已提交
2380
        if (!bus->ifs[0].bs && !bus->ifs[1].bs)
2381 2382
            ret = 0;
        else
B
bellard 已提交
2383
            ret = s->select;
B
bellard 已提交
2384 2385 2386
        break;
    default:
    case 7:
G
Gerd Hoffmann 已提交
2387 2388
        if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
            (s != bus->ifs && !s->bs))
2389 2390 2391
            ret = 0;
        else
            ret = s->status;
2392
        qemu_irq_lower(bus->irq);
B
bellard 已提交
2393 2394 2395 2396 2397 2398 2399 2400
        break;
    }
#ifdef DEBUG_IDE
    printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
#endif
    return ret;
}

G
Gerd Hoffmann 已提交
2401
uint32_t ide_status_read(void *opaque, uint32_t addr)
B
bellard 已提交
2402
{
G
Gerd Hoffmann 已提交
2403 2404
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2405
    int ret;
B
bellard 已提交
2406

G
Gerd Hoffmann 已提交
2407 2408
    if ((!bus->ifs[0].bs && !bus->ifs[1].bs) ||
        (s != bus->ifs && !s->bs))
B
bellard 已提交
2409 2410 2411
        ret = 0;
    else
        ret = s->status;
B
bellard 已提交
2412 2413 2414 2415 2416 2417
#ifdef DEBUG_IDE
    printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
#endif
    return ret;
}

G
Gerd Hoffmann 已提交
2418
void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
2419
{
G
Gerd Hoffmann 已提交
2420
    IDEBus *bus = opaque;
B
bellard 已提交
2421 2422 2423 2424 2425 2426 2427
    IDEState *s;
    int i;

#ifdef DEBUG_IDE
    printf("ide: write control addr=0x%x val=%02x\n", addr, val);
#endif
    /* common for both drives */
2428
    if (!(bus->cmd & IDE_CMD_RESET) &&
B
bellard 已提交
2429 2430 2431
        (val & IDE_CMD_RESET)) {
        /* reset low to high */
        for(i = 0;i < 2; i++) {
G
Gerd Hoffmann 已提交
2432
            s = &bus->ifs[i];
B
bellard 已提交
2433 2434 2435
            s->status = BUSY_STAT | SEEK_STAT;
            s->error = 0x01;
        }
2436
    } else if ((bus->cmd & IDE_CMD_RESET) &&
B
bellard 已提交
2437 2438 2439
               !(val & IDE_CMD_RESET)) {
        /* high to low */
        for(i = 0;i < 2; i++) {
G
Gerd Hoffmann 已提交
2440
            s = &bus->ifs[i];
B
bellard 已提交
2441 2442 2443
            if (s->is_cdrom)
                s->status = 0x00; /* NOTE: READY is _not_ set */
            else
2444
                s->status = READY_STAT | SEEK_STAT;
B
bellard 已提交
2445 2446 2447 2448
            ide_set_signature(s);
        }
    }

2449
    bus->cmd = val;
B
bellard 已提交
2450 2451
}

G
Gerd Hoffmann 已提交
2452
void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
2453
{
G
Gerd Hoffmann 已提交
2454 2455
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2456 2457
    uint8_t *p;

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

B
bellard 已提交
2462
    p = s->data_ptr;
B
bellard 已提交
2463
    *(uint16_t *)p = le16_to_cpu(val);
B
bellard 已提交
2464 2465 2466 2467 2468 2469
    p += 2;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
}

G
Gerd Hoffmann 已提交
2470
uint32_t ide_data_readw(void *opaque, uint32_t addr)
B
bellard 已提交
2471
{
G
Gerd Hoffmann 已提交
2472 2473
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2474 2475
    uint8_t *p;
    int ret;
2476 2477 2478 2479 2480

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

B
bellard 已提交
2481
    p = s->data_ptr;
B
bellard 已提交
2482
    ret = cpu_to_le16(*(uint16_t *)p);
B
bellard 已提交
2483 2484 2485 2486 2487 2488 2489
    p += 2;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
    return ret;
}

G
Gerd Hoffmann 已提交
2490
void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
2491
{
G
Gerd Hoffmann 已提交
2492 2493
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2494 2495
    uint8_t *p;

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

B
bellard 已提交
2500
    p = s->data_ptr;
B
bellard 已提交
2501
    *(uint32_t *)p = le32_to_cpu(val);
B
bellard 已提交
2502 2503 2504 2505 2506 2507
    p += 4;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
}

G
Gerd Hoffmann 已提交
2508
uint32_t ide_data_readl(void *opaque, uint32_t addr)
B
bellard 已提交
2509
{
G
Gerd Hoffmann 已提交
2510 2511
    IDEBus *bus = opaque;
    IDEState *s = idebus_active_if(bus);
B
bellard 已提交
2512 2513
    uint8_t *p;
    int ret;
2514

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

B
bellard 已提交
2519
    p = s->data_ptr;
B
bellard 已提交
2520
    ret = cpu_to_le32(*(uint32_t *)p);
B
bellard 已提交
2521 2522 2523 2524 2525 2526 2527
    p += 4;
    s->data_ptr = p;
    if (p >= s->data_end)
        s->end_transfer_func(s);
    return ret;
}

2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
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 已提交
2538
static void ide_reset(IDEState *s)
B
bellard 已提交
2539
{
B
Blue Swirl 已提交
2540 2541 2542
#ifdef DEBUG_IDE
    printf("ide: reset\n");
#endif
2543 2544 2545 2546
    if (s->is_cf)
        s->mult_sectors = 0;
    else
        s->mult_sectors = MAX_MULT_SECTORS;
B
Blue Swirl 已提交
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
    /* 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 已提交
2562
    s->select = 0xa0;
A
aliguori 已提交
2563
    s->status = READY_STAT | SEEK_STAT;
B
Blue Swirl 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579

    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 已提交
2580
    ide_set_signature(s);
2581 2582 2583 2584
    /* 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);
2585
    s->media_changed = 0;
B
bellard 已提交
2586 2587
}

B
Blue Swirl 已提交
2588 2589 2590 2591 2592 2593 2594 2595 2596
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);
}

2597 2598
void ide_init_drive(IDEState *s, DriveInfo *dinfo,
                    const char *version, const char *serial)
2599 2600 2601 2602
{
    int cylinders, heads, secs;
    uint64_t nb_sectors;

2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
    s->bs = dinfo->bdrv;
    bdrv_get_geometry(s->bs, &nb_sectors);
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
    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;
    if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
        s->is_cdrom = 1;
        bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
2619
    }
2620 2621 2622
    if (serial && *serial) {
        strncpy(s->drive_serial_str, serial, sizeof(s->drive_serial_str));
    } else {
2623 2624
        snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
                 "QM%05d", s->drive_serial);
2625
    }
G
Gerd Hoffmann 已提交
2626 2627 2628 2629 2630
    if (version) {
        pstrcpy(s->version, sizeof(s->version), version);
    } else {
        pstrcpy(s->version, sizeof(s->version), QEMU_VERSION);
    }
2631 2632 2633
    ide_reset(s);
}

2634
static void ide_init1(IDEBus *bus, int unit)
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
{
    static int drive_serial = 1;
    IDEState *s = &bus->ifs[unit];

    s->bus = bus;
    s->unit = unit;
    s->drive_serial = drive_serial++;
    s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
    s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4;
    s->smart_selftest_data = qemu_blockalign(s->bs, 512);
    s->sector_write_timer = qemu_new_timer(vm_clock,
                                           ide_sector_write_timer_cb, s);
2647 2648 2649 2650 2651 2652 2653 2654 2655
}

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]);
2656
    }
2657
    bus->irq = irq;
2658 2659
}

2660 2661 2662
/* 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 已提交
2663
{
2664
    int i;
2665
    DriveInfo *dinfo;
B
bellard 已提交
2666

2667
    for(i = 0; i < 2; i++) {
2668 2669 2670
        dinfo = i == 0 ? hd0 : hd1;
        ide_init1(bus, i);
        if (dinfo) {
2671
            ide_init_drive(&bus->ifs[i], dinfo, NULL, dinfo->serial);
2672 2673 2674
        } else {
            ide_reset(&bus->ifs[i]);
        }
B
bellard 已提交
2675
    }
2676
    bus->irq = irq;
B
bellard 已提交
2677 2678
}

G
Gerd Hoffmann 已提交
2679
void ide_init_ioport(IDEBus *bus, int iobase, int iobase2)
B
bellard 已提交
2680
{
G
Gerd Hoffmann 已提交
2681 2682
    register_ioport_write(iobase, 8, 1, ide_ioport_write, bus);
    register_ioport_read(iobase, 8, 1, ide_ioport_read, bus);
2683
    if (iobase2) {
G
Gerd Hoffmann 已提交
2684 2685
        register_ioport_read(iobase2, 1, 1, ide_status_read, bus);
        register_ioport_write(iobase2, 1, 1, ide_cmd_write, bus);
B
bellard 已提交
2686
    }
2687

2688
    /* data ports */
G
Gerd Hoffmann 已提交
2689 2690 2691 2692
    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 已提交
2693
}
B
bellard 已提交
2694

J
Juan Quintela 已提交
2695
static bool is_identify_set(void *opaque, int version_id)
2696
{
J
Juan Quintela 已提交
2697 2698 2699 2700 2701
    IDEState *s = opaque;

    return s->identify_set != 0;
}

2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
static EndTransferFunc* transfer_end_table[] = {
        ide_sector_read,
        ide_sector_write,
        ide_transfer_stop,
        ide_atapi_cmd_reply_end,
        ide_atapi_cmd,
};

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 已提交
2721
static int ide_drive_post_load(void *opaque, int version_id)
2722
{
J
Juan Quintela 已提交
2723 2724 2725
    IDEState *s = opaque;

    if (version_id < 3) {
G
Gleb Natapov 已提交
2726
        if (s->sense_key == SENSE_UNIT_ATTENTION &&
J
Juan Quintela 已提交
2727
            s->asc == ASC_MEDIUM_MAY_HAVE_CHANGED) {
G
Gleb Natapov 已提交
2728
            s->cdrom_changed = 1;
J
Juan Quintela 已提交
2729
        }
G
Gleb Natapov 已提交
2730
    }
2731 2732 2733 2734 2735 2736 2737

    if (s->cur_io_buffer_len) {
        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;
    }
        
J
Juan Quintela 已提交
2738
    return 0;
2739 2740
}

2741 2742 2743
static void ide_drive_pre_save(void *opaque)
{
    IDEState *s = opaque;
B
Blue Swirl 已提交
2744
    int idx;
2745 2746 2747 2748 2749 2750 2751 2752 2753

    s->cur_io_buffer_len = 0;

    if (!(s->status & DRQ_STAT))
        return;

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

B
Blue Swirl 已提交
2754 2755
    idx = transfer_end_table_idx(s->end_transfer_func);
    if (idx == -1) {
2756 2757 2758
        fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n",
                        __func__);
        s->end_transfer_fn_idx = 2;
B
Blue Swirl 已提交
2759 2760
    } else {
        s->end_transfer_fn_idx = idx;
2761 2762 2763
    }
}

J
Juan Quintela 已提交
2764 2765
const VMStateDescription vmstate_ide_drive = {
    .name = "ide_drive",
2766
    .version_id = 4,
J
Juan Quintela 已提交
2767 2768
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
2769
    .pre_save = ide_drive_pre_save,
J
Juan Quintela 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
    .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),
2792 2793 2794 2795 2796 2797 2798 2799
        VMSTATE_INT32_V(req_nb_sectors, IDEState, 4),
        VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 4,
			     vmstate_info_uint8, uint8_t),
        VMSTATE_INT32_V(cur_io_buffer_offset, IDEState, 4),
        VMSTATE_INT32_V(cur_io_buffer_len, IDEState, 4),
        VMSTATE_UINT8_V(end_transfer_fn_idx, IDEState, 4),
        VMSTATE_INT32_V(elementary_transfer_size, IDEState, 4),
        VMSTATE_INT32_V(packet_transfer_size, IDEState, 4),
J
Juan Quintela 已提交
2800 2801 2802 2803
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
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()
    }
};

B
bellard 已提交
2816 2817 2818
/***********************************************************/
/* PCI IDE definitions */

2819
static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb)
B
bellard 已提交
2820
{
G
Gerd Hoffmann 已提交
2821
    BMDMAState *bm = s->bus->bmdma;
B
bellard 已提交
2822 2823
    if(!bm)
        return;
G
Gerd Hoffmann 已提交
2824
    bm->unit = s->unit;
B
bellard 已提交
2825
    bm->dma_cb = dma_cb;
2826 2827 2828
    bm->cur_prd_last = 0;
    bm->cur_prd_addr = 0;
    bm->cur_prd_len = 0;
2829 2830
    bm->sector_num = ide_get_sector(s);
    bm->nsector = s->nsector;
B
bellard 已提交
2831
    if (bm->status & BM_STATUS_DMAING) {
2832
        bm->dma_cb(bm, 0);
B
bellard 已提交
2833 2834 2835
    }
}

K
Kevin Wolf 已提交
2836
static void ide_dma_restart(IDEState *s, int is_read)
2837
{
G
Gerd Hoffmann 已提交
2838
    BMDMAState *bm = s->bus->bmdma;
2839 2840 2841 2842 2843
    ide_set_sector(s, bm->sector_num);
    s->io_buffer_index = 0;
    s->io_buffer_size = 0;
    s->nsector = bm->nsector;
    bm->cur_addr = bm->addr;
K
Kevin Wolf 已提交
2844 2845 2846 2847 2848 2849 2850

    if (is_read) {
        bm->dma_cb = ide_read_dma_cb;
    } else {
        bm->dma_cb = ide_write_dma_cb;
    }

2851 2852 2853
    ide_dma_start(s, bm->dma_cb);
}

G
Gerd Hoffmann 已提交
2854
void ide_dma_cancel(BMDMAState *bm)
2855 2856 2857 2858 2859 2860 2861 2862 2863
{
    if (bm->status & BM_STATUS_DMAING) {
        if (bm->aiocb) {
#ifdef DEBUG_AIO
            printf("aio_cancel\n");
#endif
            bdrv_aio_cancel(bm->aiocb);
            bm->aiocb = NULL;
        }
K
Kevin Wolf 已提交
2864 2865 2866 2867
        bm->status &= ~BM_STATUS_DMAING;
        /* cancel DMA request */
        bm->unit = -1;
        bm->dma_cb = NULL;
2868 2869 2870
    }
}

B
Blue Swirl 已提交
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
void ide_dma_reset(BMDMAState *bm)
{
#ifdef DEBUG_IDE
    printf("ide: dma_reset\n");
#endif
    ide_dma_cancel(bm);
    bm->cmd = 0;
    bm->status = 0;
    bm->addr = 0;
    bm->cur_addr = 0;
    bm->cur_prd_last = 0;
    bm->cur_prd_addr = 0;
    bm->cur_prd_len = 0;
    bm->sector_num = 0;
    bm->nsector = 0;
}