esp.c 14.9 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 {
B
bellard 已提交
54
    BlockDriverState **bd;
B
blueswir1 已提交
55 56
    uint8_t rregs[ESP_REGS];
    uint8_t wregs[ESP_REGS];
57
    int32_t ti_size;
58 59
    uint32_t ti_rptr, ti_wptr;
    uint8_t ti_buf[TI_BUFSZ];
P
pbrook 已提交
60
    int sense;
61
    int dma;
P
pbrook 已提交
62 63
    SCSIDevice *scsi_dev[MAX_DISKS];
    SCSIDevice *current_dev;
P
pbrook 已提交
64 65 66
    uint8_t cmdbuf[TI_BUFSZ];
    int cmdlen;
    int do_cmd;
P
pbrook 已提交
67

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

B
bellard 已提交
78 79 80 81 82 83 84 85
#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 已提交
86 87
#define STAT_PE 0x20
#define STAT_GE 0x40
B
bellard 已提交
88 89 90 91 92
#define STAT_IN 0x80

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

#define SEQ_0 0x0
#define SEQ_CD 0x4

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

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

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

P
pbrook 已提交
118 119 120 121 122 123
    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;
    }

124
    if (target >= MAX_DISKS || !s->scsi_dev[target]) {
P
pbrook 已提交
125
        // No such drive
B
bellard 已提交
126 127 128
	s->rregs[4] = STAT_IN;
	s->rregs[5] = INTR_DC;
	s->rregs[6] = SEQ_0;
129
	espdma_raise_irq(s->dma_opaque);
P
pbrook 已提交
130
	return 0;
B
bellard 已提交
131
    }
P
pbrook 已提交
132
    s->current_dev = s->scsi_dev[target];
P
pbrook 已提交
133 134 135 136 137 138 139 140 141 142
    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 已提交
143
    datalen = scsi_send_command(s->current_dev, 0, &buf[1], lun);
144 145
    s->ti_size = datalen;
    if (datalen != 0) {
P
pbrook 已提交
146
        s->rregs[4] = STAT_IN | STAT_TC;
P
pbrook 已提交
147
        s->dma_left = 0;
P
pbrook 已提交
148
        s->dma_counter = 0;
P
pbrook 已提交
149 150
        if (datalen > 0) {
            s->rregs[4] |= STAT_DI;
P
pbrook 已提交
151
            scsi_read_data(s->current_dev, 0);
P
pbrook 已提交
152 153
        } else {
            s->rregs[4] |= STAT_DO;
P
pbrook 已提交
154
            scsi_write_data(s->current_dev, 0);
B
bellard 已提交
155
        }
B
bellard 已提交
156 157 158
    }
    s->rregs[5] = INTR_BS | INTR_FC;
    s->rregs[6] = SEQ_CD;
159
    espdma_raise_irq(s->dma_opaque);
B
bellard 已提交
160 161
}

P
pbrook 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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;
181
        espdma_raise_irq(s->dma_opaque);
P
pbrook 已提交
182 183 184
    }
}

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

P
pbrook 已提交
204 205 206 207 208 209
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 已提交
210 211
    s->rregs[0] = 0;
    s->rregs[1] = 0;
212
    espdma_raise_irq(s->dma_opaque);
P
pbrook 已提交
213 214
}

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

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

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

P
pbrook 已提交
274 275 276 277 278
    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 已提交
279 280 281
        s->dma_left = 0;
        s->async_len = 0;
        if (arg)
P
pbrook 已提交
282
            DPRINTF("Command failed\n");
P
pbrook 已提交
283 284 285 286
        s->sense = arg;
        s->rregs[4] = STAT_ST;
        esp_dma_done(s);
        s->current_dev = NULL;
P
pbrook 已提交
287 288
    } else {
        DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
P
pbrook 已提交
289 290
        s->async_len = arg;
        s->async_buf = scsi_get_buf(s->current_dev, 0);
P
pbrook 已提交
291
        if (s->dma_left) {
P
pbrook 已提交
292
            esp_do_dma(s);
P
pbrook 已提交
293 294 295 296 297
        } 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 已提交
298
    }
P
pbrook 已提交
299 300
}

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

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

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

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

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

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

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

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

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

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

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

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

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

B
bellard 已提交
541 542 543
    return 0;
}

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

567 568
void *esp_init(BlockDriverState **bd, target_phys_addr_t espaddr,
               void *dma_opaque)
B
bellard 已提交
569 570
{
    ESPState *s;
571
    int esp_io_memory;
B
bellard 已提交
572 573 574

    s = qemu_mallocz(sizeof(ESPState));
    if (!s)
575
        return NULL;
B
bellard 已提交
576 577

    s->bd = bd;
578
    s->dma_opaque = dma_opaque;
B
blueswir1 已提交
579
    sparc32_dma_set_reset_data(dma_opaque, esp_reset, s);
B
bellard 已提交
580 581

    esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
B
blueswir1 已提交
582
    cpu_register_physical_memory(espaddr, ESP_SIZE, esp_io_memory);
B
bellard 已提交
583 584 585

    esp_reset(s);

B
blueswir1 已提交
586
    register_savevm("esp", espaddr, 3, esp_save, esp_load, s);
B
bellard 已提交
587 588
    qemu_register_reset(esp_reset, s);

589 590
    return s;
}