esp.c 15.0 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU ESP/NCR53C9x emulation
B
bellard 已提交
3
 * 
P
pbrook 已提交
4
 * Copyright (c) 2005-2006 Fabrice Bellard
B
bellard 已提交
5 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
}

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

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

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

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

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 已提交
509

B
blueswir1 已提交
510 511
    qemu_put_buffer(f, s->rregs, ESP_REGS);
    qemu_put_buffer(f, s->wregs, ESP_REGS);
512 513 514 515
    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 已提交
516
    qemu_put_be32s(f, &s->sense);
517
    qemu_put_be32s(f, &s->dma);
B
blueswir1 已提交
518 519 520 521 522
    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 已提交
523 524 525 526 527 528
}

static int esp_load(QEMUFile *f, void *opaque, int version_id)
{
    ESPState *s = opaque;
    
B
blueswir1 已提交
529 530
    if (version_id != 3)
        return -EINVAL; // Cannot emulate 2
B
bellard 已提交
531

B
blueswir1 已提交
532 533
    qemu_get_buffer(f, s->rregs, ESP_REGS);
    qemu_get_buffer(f, s->wregs, ESP_REGS);
534 535 536 537
    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 已提交
538
    qemu_get_be32s(f, &s->sense);
539
    qemu_get_be32s(f, &s->dma);
B
blueswir1 已提交
540 541 542 543
    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 已提交
544

B
bellard 已提交
545 546 547
    return 0;
}

T
ths 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
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);
}

571
void *esp_init(BlockDriverState **bd, target_phys_addr_t espaddr,
572
               void *dma_opaque, qemu_irq irq)
B
bellard 已提交
573 574
{
    ESPState *s;
575
    int esp_io_memory;
B
bellard 已提交
576 577 578

    s = qemu_mallocz(sizeof(ESPState));
    if (!s)
579
        return NULL;
B
bellard 已提交
580 581

    s->bd = bd;
582
    s->irq = irq;
583
    s->dma_opaque = dma_opaque;
B
blueswir1 已提交
584
    sparc32_dma_set_reset_data(dma_opaque, esp_reset, s);
B
bellard 已提交
585 586

    esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
B
blueswir1 已提交
587
    cpu_register_physical_memory(espaddr, ESP_SIZE, esp_io_memory);
B
bellard 已提交
588 589 590

    esp_reset(s);

B
blueswir1 已提交
591
    register_savevm("esp", espaddr, 3, esp_save, esp_load, s);
B
bellard 已提交
592 593
    qemu_register_reset(esp_reset, s);

594 595
    return s;
}