esp.c 15.1 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU ESP/NCR53C9x emulation
3
 *
P
pbrook 已提交
4
 * Copyright (c) 2005-2006 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 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.
 */
#include "vl.h"

/* debug ESP card */
B
bellard 已提交
27
//#define DEBUG_ESP
B
bellard 已提交
28

29 30 31 32 33 34 35 36
/*
 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O), also
 * produced as NCR89C100. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
 * and
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
 */

B
bellard 已提交
37 38 39 40 41 42 43
#ifdef DEBUG_ESP
#define DPRINTF(fmt, args...) \
do { printf("ESP: " fmt , ##args); } while (0)
#else
#define DPRINTF(fmt, args...)
#endif

B
blueswir1 已提交
44 45 46
#define ESP_MASK 0x3f
#define ESP_REGS 16
#define ESP_SIZE (ESP_REGS * 4)
P
pbrook 已提交
47
#define TI_BUFSZ 32
T
ths 已提交
48 49
/* The HBA is ID 7, so for simplicitly limit to 7 devices.  */
#define ESP_MAX_DEVS      7
50

P
pbrook 已提交
51
typedef struct ESPState ESPState;
B
bellard 已提交
52

P
pbrook 已提交
53
struct ESPState {
54
    qemu_irq irq;
B
bellard 已提交
55
    BlockDriverState **bd;
B
blueswir1 已提交
56 57
    uint8_t rregs[ESP_REGS];
    uint8_t wregs[ESP_REGS];
58
    int32_t ti_size;
59 60
    uint32_t ti_rptr, ti_wptr;
    uint8_t ti_buf[TI_BUFSZ];
P
pbrook 已提交
61
    int sense;
62
    int dma;
P
pbrook 已提交
63 64
    SCSIDevice *scsi_dev[MAX_DISKS];
    SCSIDevice *current_dev;
P
pbrook 已提交
65 66 67
    uint8_t cmdbuf[TI_BUFSZ];
    int cmdlen;
    int do_cmd;
P
pbrook 已提交
68

P
pbrook 已提交
69
    /* The amount of data left in the current DMA transfer.  */
P
pbrook 已提交
70
    uint32_t dma_left;
P
pbrook 已提交
71 72 73
    /* The size of the current DMA transfer.  Zero if no transfer is in
       progress.  */
    uint32_t dma_counter;
P
pbrook 已提交
74
    uint8_t *async_buf;
P
pbrook 已提交
75
    uint32_t async_len;
76
    void *dma_opaque;
P
pbrook 已提交
77
};
B
bellard 已提交
78

B
bellard 已提交
79 80 81 82 83 84 85 86
#define STAT_DO 0x00
#define STAT_DI 0x01
#define STAT_CD 0x02
#define STAT_ST 0x03
#define STAT_MI 0x06
#define STAT_MO 0x07

#define STAT_TC 0x10
P
pbrook 已提交
87 88
#define STAT_PE 0x20
#define STAT_GE 0x40
B
bellard 已提交
89 90 91 92 93
#define STAT_IN 0x80

#define INTR_FC 0x08
#define INTR_BS 0x10
#define INTR_DC 0x20
B
bellard 已提交
94
#define INTR_RST 0x80
B
bellard 已提交
95 96 97 98

#define SEQ_0 0x0
#define SEQ_CD 0x4

P
pbrook 已提交
99
static int get_cmd(ESPState *s, uint8_t *buf)
B
bellard 已提交
100
{
P
pbrook 已提交
101
    uint32_t dmalen;
B
bellard 已提交
102 103
    int target;

P
pbrook 已提交
104
    dmalen = s->rregs[0] | (s->rregs[1] << 8);
105
    target = s->wregs[4] & 7;
P
pbrook 已提交
106
    DPRINTF("get_cmd: len %d target %d\n", dmalen, target);
107
    if (s->dma) {
108
        espdma_memory_read(s->dma_opaque, buf, dmalen);
109 110 111 112 113
    } else {
	buf[0] = 0;
	memcpy(&buf[1], s->ti_buf, dmalen);
	dmalen++;
    }
P
pbrook 已提交
114

B
bellard 已提交
115
    s->ti_size = 0;
116 117
    s->ti_rptr = 0;
    s->ti_wptr = 0;
B
bellard 已提交
118

P
pbrook 已提交
119 120 121 122 123 124
    if (s->current_dev) {
        /* Started a new command before the old one finished.  Cancel it.  */
        scsi_cancel_io(s->current_dev, 0);
        s->async_len = 0;
    }

125
    if (target >= MAX_DISKS || !s->scsi_dev[target]) {
P
pbrook 已提交
126
        // No such drive
B
bellard 已提交
127 128 129
	s->rregs[4] = STAT_IN;
	s->rregs[5] = INTR_DC;
	s->rregs[6] = SEQ_0;
130
	qemu_irq_raise(s->irq);
P
pbrook 已提交
131
	return 0;
B
bellard 已提交
132
    }
P
pbrook 已提交
133
    s->current_dev = s->scsi_dev[target];
P
pbrook 已提交
134 135 136 137 138 139 140 141 142 143
    return dmalen;
}

static void do_cmd(ESPState *s, uint8_t *buf)
{
    int32_t datalen;
    int lun;

    DPRINTF("do_cmd: busid 0x%x\n", buf[0]);
    lun = buf[0] & 7;
P
pbrook 已提交
144
    datalen = scsi_send_command(s->current_dev, 0, &buf[1], lun);
145 146
    s->ti_size = datalen;
    if (datalen != 0) {
P
pbrook 已提交
147
        s->rregs[4] = STAT_IN | STAT_TC;
P
pbrook 已提交
148
        s->dma_left = 0;
P
pbrook 已提交
149
        s->dma_counter = 0;
P
pbrook 已提交
150 151
        if (datalen > 0) {
            s->rregs[4] |= STAT_DI;
P
pbrook 已提交
152
            scsi_read_data(s->current_dev, 0);
P
pbrook 已提交
153 154
        } else {
            s->rregs[4] |= STAT_DO;
P
pbrook 已提交
155
            scsi_write_data(s->current_dev, 0);
B
bellard 已提交
156
        }
B
bellard 已提交
157 158 159
    }
    s->rregs[5] = INTR_BS | INTR_FC;
    s->rregs[6] = SEQ_CD;
160
    qemu_irq_raise(s->irq);
B
bellard 已提交
161 162
}

P
pbrook 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static void handle_satn(ESPState *s)
{
    uint8_t buf[32];
    int len;

    len = get_cmd(s, buf);
    if (len)
        do_cmd(s, buf);
}

static void handle_satn_stop(ESPState *s)
{
    s->cmdlen = get_cmd(s, s->cmdbuf);
    if (s->cmdlen) {
        DPRINTF("Set ATN & Stop: cmdlen %d\n", s->cmdlen);
        s->do_cmd = 1;
        s->rregs[4] = STAT_IN | STAT_TC | STAT_CD;
        s->rregs[5] = INTR_BS | INTR_FC;
        s->rregs[6] = SEQ_CD;
182
        qemu_irq_raise(s->irq);
P
pbrook 已提交
183 184 185
    }
}

P
pbrook 已提交
186
static void write_response(ESPState *s)
B
bellard 已提交
187
{
P
pbrook 已提交
188 189 190
    DPRINTF("Transfer status (sense=%d)\n", s->sense);
    s->ti_buf[0] = s->sense;
    s->ti_buf[1] = 0;
191
    if (s->dma) {
192
        espdma_memory_write(s->dma_opaque, s->ti_buf, 2);
193 194 195 196
	s->rregs[4] = STAT_IN | STAT_TC | STAT_ST;
	s->rregs[5] = INTR_BS | INTR_FC;
	s->rregs[6] = SEQ_CD;
    } else {
P
pbrook 已提交
197
	s->ti_size = 2;
198 199
	s->ti_rptr = 0;
	s->ti_wptr = 0;
P
pbrook 已提交
200
	s->rregs[7] = 2;
201
    }
202
    qemu_irq_raise(s->irq);
B
bellard 已提交
203
}
204

P
pbrook 已提交
205 206 207 208 209 210
static void esp_dma_done(ESPState *s)
{
    s->rregs[4] |= STAT_IN | STAT_TC;
    s->rregs[5] = INTR_BS;
    s->rregs[6] = 0;
    s->rregs[7] = 0;
P
pbrook 已提交
211 212
    s->rregs[0] = 0;
    s->rregs[1] = 0;
213
    qemu_irq_raise(s->irq);
P
pbrook 已提交
214 215
}

P
pbrook 已提交
216 217
static void esp_do_dma(ESPState *s)
{
218
    uint32_t len;
P
pbrook 已提交
219
    int to_device;
P
pbrook 已提交
220

221
    to_device = (s->ti_size < 0);
P
pbrook 已提交
222
    len = s->dma_left;
P
pbrook 已提交
223 224
    if (s->do_cmd) {
        DPRINTF("command len %d + %d\n", s->cmdlen, len);
225
        espdma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
P
pbrook 已提交
226 227 228 229 230
        s->ti_size = 0;
        s->cmdlen = 0;
        s->do_cmd = 0;
        do_cmd(s, s->cmdbuf);
        return;
P
pbrook 已提交
231 232 233 234 235 236 237 238 239
    }
    if (s->async_len == 0) {
        /* Defer until data is available.  */
        return;
    }
    if (len > s->async_len) {
        len = s->async_len;
    }
    if (to_device) {
240
        espdma_memory_read(s->dma_opaque, s->async_buf, len);
P
pbrook 已提交
241
    } else {
242
        espdma_memory_write(s->dma_opaque, s->async_buf, len);
P
pbrook 已提交
243 244 245 246
    }
    s->dma_left -= len;
    s->async_buf += len;
    s->async_len -= len;
P
pbrook 已提交
247 248 249 250
    if (to_device)
        s->ti_size += len;
    else
        s->ti_size -= len;
P
pbrook 已提交
251
    if (s->async_len == 0) {
P
pbrook 已提交
252
        if (to_device) {
253
            // ti_size is negative
P
pbrook 已提交
254
            scsi_write_data(s->current_dev, 0);
P
pbrook 已提交
255
        } else {
P
pbrook 已提交
256
            scsi_read_data(s->current_dev, 0);
P
pbrook 已提交
257 258 259 260 261 262
            /* If there is still data to be read from the device then
               complete the DMA operation immeriately.  Otherwise defer
               until the scsi layer has completed.  */
            if (s->dma_left == 0 && s->ti_size > 0) {
                esp_dma_done(s);
            }
P
pbrook 已提交
263
        }
P
pbrook 已提交
264 265
    } else {
        /* Partially filled a scsi buffer. Complete immediately.  */
P
pbrook 已提交
266 267
        esp_dma_done(s);
    }
P
pbrook 已提交
268 269
}

P
pbrook 已提交
270 271
static void esp_command_complete(void *opaque, int reason, uint32_t tag,
                                 uint32_t arg)
P
pbrook 已提交
272 273 274
{
    ESPState *s = (ESPState *)opaque;

P
pbrook 已提交
275 276 277 278 279
    if (reason == SCSI_REASON_DONE) {
        DPRINTF("SCSI Command complete\n");
        if (s->ti_size != 0)
            DPRINTF("SCSI command completed unexpectedly\n");
        s->ti_size = 0;
P
pbrook 已提交
280 281 282
        s->dma_left = 0;
        s->async_len = 0;
        if (arg)
P
pbrook 已提交
283
            DPRINTF("Command failed\n");
P
pbrook 已提交
284 285 286 287
        s->sense = arg;
        s->rregs[4] = STAT_ST;
        esp_dma_done(s);
        s->current_dev = NULL;
P
pbrook 已提交
288 289
    } else {
        DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
P
pbrook 已提交
290 291
        s->async_len = arg;
        s->async_buf = scsi_get_buf(s->current_dev, 0);
P
pbrook 已提交
292
        if (s->dma_left) {
P
pbrook 已提交
293
            esp_do_dma(s);
P
pbrook 已提交
294 295 296 297 298
        } else if (s->dma_counter != 0 && s->ti_size <= 0) {
            /* If this was the last part of a DMA transfer then the
               completion interrupt is deferred to here.  */
            esp_dma_done(s);
        }
P
pbrook 已提交
299
    }
P
pbrook 已提交
300 301
}

B
bellard 已提交
302 303
static void handle_ti(ESPState *s)
{
P
pbrook 已提交
304
    uint32_t dmalen, minlen;
B
bellard 已提交
305

P
pbrook 已提交
306
    dmalen = s->rregs[0] | (s->rregs[1] << 8);
P
pbrook 已提交
307 308 309
    if (dmalen==0) {
      dmalen=0x10000;
    }
P
pbrook 已提交
310
    s->dma_counter = dmalen;
P
pbrook 已提交
311

P
pbrook 已提交
312 313
    if (s->do_cmd)
        minlen = (dmalen < 32) ? dmalen : 32;
314 315
    else if (s->ti_size < 0)
        minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
P
pbrook 已提交
316 317
    else
        minlen = (dmalen < s->ti_size) ? dmalen : s->ti_size;
P
pbrook 已提交
318
    DPRINTF("Transfer Information len %d\n", minlen);
319
    if (s->dma) {
P
pbrook 已提交
320 321 322
        s->dma_left = minlen;
        s->rregs[4] &= ~STAT_TC;
        esp_do_dma(s);
P
pbrook 已提交
323 324 325 326 327 328 329 330
    } else if (s->do_cmd) {
        DPRINTF("command len %d\n", s->cmdlen);
        s->ti_size = 0;
        s->cmdlen = 0;
        s->do_cmd = 0;
        do_cmd(s, s->cmdbuf);
        return;
    }
B
bellard 已提交
331 332
}

B
blueswir1 已提交
333
static void esp_reset(void *opaque)
B
bellard 已提交
334 335
{
    ESPState *s = opaque;
336

B
blueswir1 已提交
337 338
    memset(s->rregs, 0, ESP_REGS);
    memset(s->wregs, 0, ESP_REGS);
B
bellard 已提交
339
    s->rregs[0x0e] = 0x4; // Indicate fas100a
P
pbrook 已提交
340 341 342 343
    s->ti_size = 0;
    s->ti_rptr = 0;
    s->ti_wptr = 0;
    s->dma = 0;
P
pbrook 已提交
344
    s->do_cmd = 0;
B
bellard 已提交
345 346
}

347 348 349 350 351 352
static void parent_esp_reset(void *opaque, int irq, int level)
{
    if (level)
        esp_reset(opaque);
}

B
bellard 已提交
353 354 355 356 357
static uint32_t esp_mem_readb(void *opaque, target_phys_addr_t addr)
{
    ESPState *s = opaque;
    uint32_t saddr;

B
blueswir1 已提交
358
    saddr = (addr & ESP_MASK) >> 2;
B
bellard 已提交
359
    DPRINTF("read reg[%d]: 0x%2.2x\n", saddr, s->rregs[saddr]);
B
bellard 已提交
360
    switch (saddr) {
361 362 363 364
    case 2:
	// FIFO
	if (s->ti_size > 0) {
	    s->ti_size--;
P
pbrook 已提交
365 366
            if ((s->rregs[4] & 6) == 0) {
                /* Data in/out.  */
P
pbrook 已提交
367 368
                fprintf(stderr, "esp: PIO data read not implemented\n");
                s->rregs[2] = 0;
P
pbrook 已提交
369 370 371
            } else {
                s->rregs[2] = s->ti_buf[s->ti_rptr++];
            }
372
            qemu_irq_raise(s->irq);
373 374 375 376 377 378
	}
	if (s->ti_size == 0) {
            s->ti_rptr = 0;
            s->ti_wptr = 0;
        }
	break;
B
bellard 已提交
379 380
    case 5:
        // interrupt
P
pbrook 已提交
381 382
        // Clear interrupt/error status bits
        s->rregs[4] &= ~(STAT_IN | STAT_GE | STAT_PE);
383
	qemu_irq_lower(s->irq);
B
bellard 已提交
384
        break;
B
bellard 已提交
385 386 387
    default:
	break;
    }
B
bellard 已提交
388
    return s->rregs[saddr];
B
bellard 已提交
389 390 391 392 393 394 395
}

static void esp_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
{
    ESPState *s = opaque;
    uint32_t saddr;

B
blueswir1 已提交
396
    saddr = (addr & ESP_MASK) >> 2;
B
bellard 已提交
397
    DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr, s->wregs[saddr], val);
B
bellard 已提交
398
    switch (saddr) {
399 400
    case 0:
    case 1:
P
pbrook 已提交
401
        s->rregs[4] &= ~STAT_TC;
402 403 404
        break;
    case 2:
	// FIFO
P
pbrook 已提交
405 406 407
        if (s->do_cmd) {
            s->cmdbuf[s->cmdlen++] = val & 0xff;
        } else if ((s->rregs[4] & 6) == 0) {
P
pbrook 已提交
408 409 410
            uint8_t buf;
            buf = val & 0xff;
            s->ti_size--;
P
pbrook 已提交
411
            fprintf(stderr, "esp: PIO data write not implemented\n");
P
pbrook 已提交
412 413 414 415
        } else {
            s->ti_size++;
            s->ti_buf[s->ti_wptr++] = val & 0xff;
        }
416
	break;
B
bellard 已提交
417
    case 3:
418
        s->rregs[saddr] = val;
B
bellard 已提交
419
	// Command
420 421
	if (val & 0x80) {
	    s->dma = 1;
P
pbrook 已提交
422 423 424
            /* Reload DMA counter.  */
            s->rregs[0] = s->wregs[0];
            s->rregs[1] = s->wregs[1];
425 426 427
	} else {
	    s->dma = 0;
	}
B
bellard 已提交
428 429
	switch(val & 0x7f) {
	case 0:
B
bellard 已提交
430 431 432 433
	    DPRINTF("NOP (%2.2x)\n", val);
	    break;
	case 1:
	    DPRINTF("Flush FIFO (%2.2x)\n", val);
B
bellard 已提交
434
            //s->ti_size = 0;
B
bellard 已提交
435
	    s->rregs[5] = INTR_FC;
B
bellard 已提交
436
	    s->rregs[6] = 0;
B
bellard 已提交
437 438
	    break;
	case 2:
B
bellard 已提交
439
	    DPRINTF("Chip reset (%2.2x)\n", val);
B
bellard 已提交
440 441 442
	    esp_reset(s);
	    break;
	case 3:
B
bellard 已提交
443
	    DPRINTF("Bus reset (%2.2x)\n", val);
B
bellard 已提交
444 445
	    s->rregs[5] = INTR_RST;
            if (!(s->wregs[8] & 0x40)) {
446
                qemu_irq_raise(s->irq);
B
bellard 已提交
447
            }
B
bellard 已提交
448 449 450 451 452 453
	    break;
	case 0x10:
	    handle_ti(s);
	    break;
	case 0x11:
	    DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val);
P
pbrook 已提交
454
	    write_response(s);
B
bellard 已提交
455 456 457
	    break;
	case 0x12:
	    DPRINTF("Message Accepted (%2.2x)\n", val);
P
pbrook 已提交
458
	    write_response(s);
B
bellard 已提交
459 460
	    s->rregs[5] = INTR_DC;
	    s->rregs[6] = 0;
B
bellard 已提交
461 462
	    break;
	case 0x1a:
B
bellard 已提交
463
	    DPRINTF("Set ATN (%2.2x)\n", val);
B
bellard 已提交
464 465
	    break;
	case 0x42:
P
pbrook 已提交
466
	    DPRINTF("Set ATN (%2.2x)\n", val);
B
bellard 已提交
467 468 469 470
	    handle_satn(s);
	    break;
	case 0x43:
	    DPRINTF("Set ATN & stop (%2.2x)\n", val);
P
pbrook 已提交
471
	    handle_satn_stop(s);
B
bellard 已提交
472
	    break;
B
blueswir1 已提交
473 474 475
        case 0x44:
            DPRINTF("Enable selection (%2.2x)\n", val);
            break;
B
bellard 已提交
476
	default:
477
	    DPRINTF("Unhandled ESP command (%2.2x)\n", val);
B
bellard 已提交
478 479 480 481 482
	    break;
	}
	break;
    case 4 ... 7:
	break;
483 484 485 486 487
    case 8:
        s->rregs[saddr] = val;
        break;
    case 9 ... 10:
        break;
B
bellard 已提交
488 489 490 491
    case 11:
        s->rregs[saddr] = val & 0x15;
        break;
    case 12 ... 15:
492 493
        s->rregs[saddr] = val;
        break;
B
bellard 已提交
494 495 496
    default:
	break;
    }
B
bellard 已提交
497
    s->wregs[saddr] = val;
B
bellard 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
}

static CPUReadMemoryFunc *esp_mem_read[3] = {
    esp_mem_readb,
    esp_mem_readb,
    esp_mem_readb,
};

static CPUWriteMemoryFunc *esp_mem_write[3] = {
    esp_mem_writeb,
    esp_mem_writeb,
    esp_mem_writeb,
};

static void esp_save(QEMUFile *f, void *opaque)
{
    ESPState *s = opaque;
B
bellard 已提交
515

B
blueswir1 已提交
516 517
    qemu_put_buffer(f, s->rregs, ESP_REGS);
    qemu_put_buffer(f, s->wregs, ESP_REGS);
518 519 520 521
    qemu_put_be32s(f, &s->ti_size);
    qemu_put_be32s(f, &s->ti_rptr);
    qemu_put_be32s(f, &s->ti_wptr);
    qemu_put_buffer(f, s->ti_buf, TI_BUFSZ);
B
blueswir1 已提交
522
    qemu_put_be32s(f, &s->sense);
523
    qemu_put_be32s(f, &s->dma);
B
blueswir1 已提交
524 525 526 527 528
    qemu_put_buffer(f, s->cmdbuf, TI_BUFSZ);
    qemu_put_be32s(f, &s->cmdlen);
    qemu_put_be32s(f, &s->do_cmd);
    qemu_put_be32s(f, &s->dma_left);
    // There should be no transfers in progress, so dma_counter is not saved
B
bellard 已提交
529 530 531 532 533
}

static int esp_load(QEMUFile *f, void *opaque, int version_id)
{
    ESPState *s = opaque;
534

B
blueswir1 已提交
535 536
    if (version_id != 3)
        return -EINVAL; // Cannot emulate 2
B
bellard 已提交
537

B
blueswir1 已提交
538 539
    qemu_get_buffer(f, s->rregs, ESP_REGS);
    qemu_get_buffer(f, s->wregs, ESP_REGS);
540 541 542 543
    qemu_get_be32s(f, &s->ti_size);
    qemu_get_be32s(f, &s->ti_rptr);
    qemu_get_be32s(f, &s->ti_wptr);
    qemu_get_buffer(f, s->ti_buf, TI_BUFSZ);
B
blueswir1 已提交
544
    qemu_get_be32s(f, &s->sense);
545
    qemu_get_be32s(f, &s->dma);
B
blueswir1 已提交
546 547 548 549
    qemu_get_buffer(f, s->cmdbuf, TI_BUFSZ);
    qemu_get_be32s(f, &s->cmdlen);
    qemu_get_be32s(f, &s->do_cmd);
    qemu_get_be32s(f, &s->dma_left);
B
bellard 已提交
550

B
bellard 已提交
551 552 553
    return 0;
}

T
ths 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
void esp_scsi_attach(void *opaque, BlockDriverState *bd, int id)
{
    ESPState *s = (ESPState *)opaque;

    if (id < 0) {
        for (id = 0; id < ESP_MAX_DEVS; id++) {
            if (s->scsi_dev[id] == NULL)
                break;
        }
    }
    if (id >= ESP_MAX_DEVS) {
        DPRINTF("Bad Device ID %d\n", id);
        return;
    }
    if (s->scsi_dev[id]) {
        DPRINTF("Destroying device %d\n", id);
        scsi_disk_destroy(s->scsi_dev[id]);
    }
    DPRINTF("Attaching block device %d\n", id);
    /* Command queueing is not implemented.  */
    s->scsi_dev[id] = scsi_disk_init(bd, 0, esp_command_complete, s);
}

577
void *esp_init(BlockDriverState **bd, target_phys_addr_t espaddr,
578
               void *dma_opaque, qemu_irq irq, qemu_irq *reset)
B
bellard 已提交
579 580
{
    ESPState *s;
581
    int esp_io_memory;
B
bellard 已提交
582 583 584

    s = qemu_mallocz(sizeof(ESPState));
    if (!s)
585
        return NULL;
B
bellard 已提交
586 587

    s->bd = bd;
588
    s->irq = irq;
589
    s->dma_opaque = dma_opaque;
B
bellard 已提交
590 591

    esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
B
blueswir1 已提交
592
    cpu_register_physical_memory(espaddr, ESP_SIZE, esp_io_memory);
B
bellard 已提交
593 594 595

    esp_reset(s);

B
blueswir1 已提交
596
    register_savevm("esp", espaddr, 3, esp_save, esp_load, s);
B
bellard 已提交
597 598
    qemu_register_reset(esp_reset, s);

599 600
    *reset = *qemu_allocate_irqs(parent_esp_reset, s, 1);

601 602
    return s;
}