Statistics
| Branch: | Revision:

root / hw / eepro100.c @ f4e94dfe

History | View | Annotate | Download (62.1 kB)

1
/*
2
 * QEMU i8255x (PRO100) emulation
3
 *
4
 * Copyright (c) 2006-2007 Stefan Weil
5
 *
6
 * Portions of the code are copies from grub / etherboot eepro100.c
7
 * and linux e100.c.
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * Tested features (i82559):
23
 *      PXE boot (i386) no valid link
24
 *      Linux networking (i386) ok
25
 *
26
 * Untested:
27
 *      non-i386 platforms
28
 *      Windows networking
29
 *
30
 * References:
31
 *
32
 * Intel 8255x 10/100 Mbps Ethernet Controller Family
33
 * Open Source Software Developer Manual
34
 */
35

    
36
#if defined(TARGET_I386)
37
# warning "PXE boot still not working!"
38
#endif
39

    
40
#include <stddef.h>             /* offsetof */
41
#include <stdbool.h>
42
#include "hw.h"
43
#include "pci.h"
44
#include "net.h"
45
#include "eeprom93xx.h"
46

    
47
/* Common declarations for all PCI devices. */
48

    
49
#define PCI_CONFIG_8(offset, value) \
50
    (pci_conf[offset] = (value))
51
#define PCI_CONFIG_16(offset, value) \
52
    (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
53
#define PCI_CONFIG_32(offset, value) \
54
    (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
55

    
56
#define KiB 1024
57

    
58
/* Debug EEPRO100 card. */
59
//~ #define DEBUG_EEPRO100
60

    
61
#ifdef DEBUG_EEPRO100
62
#define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
63
#else
64
#define logout(fmt, ...) ((void)0)
65
#endif
66

    
67
/* Set flags to 0 to disable debug output. */
68
#define INT     1       /* interrupt related actions */
69
#define MDI     1       /* mdi related actions */
70
#define OTHER   1
71
#define RXTX    1
72
#define EEPROM  1       /* eeprom related actions */
73

    
74
#define TRACE(flag, command) ((flag) ? (command) : (void)0)
75

    
76
#define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
77

    
78
#define MAX_ETH_FRAME_SIZE 1514
79

    
80
/* This driver supports several different devices which are declared here. */
81
#define i82550          0x82550
82
#define i82551          0x82551
83
#define i82557A         0x82557a
84
#define i82557B         0x82557b
85
#define i82557C         0x82557c
86
#define i82558A         0x82558a
87
#define i82558B         0x82558b
88
#define i82559A         0x82559a
89
#define i82559B         0x82559b
90
#define i82559C         0x82559c
91
#define i82559ER        0x82559e
92
#define i82562          0x82562
93

    
94
/* Use 64 word EEPROM. TODO: could be a runtime option. */
95
#define EEPROM_SIZE     64
96

    
97
#define PCI_MEM_SIZE            (4 * KiB)
98
#define PCI_IO_SIZE             64
99
#define PCI_FLASH_SIZE          (128 * KiB)
100

    
101
#define BIT(n) (1 << (n))
102
#define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
103

    
104
/* The SCB accepts the following controls for the Tx and Rx units: */
105
#define  CU_NOP         0x0000  /* No operation. */
106
#define  CU_START       0x0010  /* CU start. */
107
#define  CU_RESUME      0x0020  /* CU resume. */
108
#define  CU_STATSADDR   0x0040  /* Load dump counters address. */
109
#define  CU_SHOWSTATS   0x0050  /* Dump statistical counters. */
110
#define  CU_CMD_BASE    0x0060  /* Load CU base address. */
111
#define  CU_DUMPSTATS   0x0070  /* Dump and reset statistical counters. */
112
#define  CU_SRESUME     0x00a0  /* CU static resume. */
113

    
114
#define  RU_NOP         0x0000
115
#define  RX_START       0x0001
116
#define  RX_RESUME      0x0002
117
#define  RX_ABORT       0x0004
118
#define  RX_ADDR_LOAD   0x0006
119
#define  RX_RESUMENR    0x0007
120
#define INT_MASK        0x0100
121
#define DRVR_INT        0x0200  /* Driver generated interrupt. */
122

    
123
/* Offsets to the various registers.
124
   All accesses need not be longword aligned. */
125
enum speedo_offsets {
126
    SCBStatus = 0,
127
    SCBAck = 1,
128
    SCBCmd = 2,                 /* Rx/Command Unit command and status. */
129
    SCBIntmask = 3,
130
    SCBPointer = 4,             /* General purpose pointer. */
131
    SCBPort = 8,                /* Misc. commands and operands.  */
132
    SCBflash = 12, SCBeeprom = 14,      /* EEPROM and flash memory control. */
133
    SCBCtrlMDI = 16,            /* MDI interface control. */
134
    SCBEarlyRx = 20,            /* Early receive byte count. */
135
    SCBFlow = 24,
136
};
137

    
138
/* A speedo3 transmit buffer descriptor with two buffers... */
139
typedef struct {
140
    uint16_t status;
141
    uint16_t command;
142
    uint32_t link;              /* void * */
143
    uint32_t tx_desc_addr;      /* transmit buffer decsriptor array address. */
144
    uint16_t tcb_bytes;         /* transmit command block byte count (in lower 14 bits */
145
    uint8_t tx_threshold;       /* transmit threshold */
146
    uint8_t tbd_count;          /* TBD number */
147
    //~ /* This constitutes two "TBD" entries: hdr and data */
148
    //~ uint32_t tx_buf_addr0;  /* void *, header of frame to be transmitted.  */
149
    //~ int32_t  tx_buf_size0;  /* Length of Tx hdr. */
150
    //~ uint32_t tx_buf_addr1;  /* void *, data to be transmitted.  */
151
    //~ int32_t  tx_buf_size1;  /* Length of Tx data. */
152
} eepro100_tx_t;
153

    
154
/* Receive frame descriptor. */
155
typedef struct {
156
    int16_t status;
157
    uint16_t command;
158
    uint32_t link;              /* struct RxFD * */
159
    uint32_t rx_buf_addr;       /* void * */
160
    uint16_t count;
161
    uint16_t size;
162
    char packet[MAX_ETH_FRAME_SIZE + 4];
163
} eepro100_rx_t;
164

    
165
typedef struct {
166
    uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
167
        tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
168
        tx_multiple_collisions, tx_total_collisions;
169
    uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
170
        rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
171
        rx_short_frame_errors;
172
    uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
173
    uint16_t xmt_tco_frames, rcv_tco_frames;
174
    uint32_t complete;
175
} eepro100_stats_t;
176

    
177
typedef enum {
178
    cu_idle = 0,
179
    cu_suspended = 1,
180
    cu_active = 2,
181
    cu_lpq_active = 2,
182
    cu_hqp_active = 3
183
} cu_state_t;
184

    
185
typedef enum {
186
    ru_idle = 0,
187
    ru_suspended = 1,
188
    ru_no_resources = 2,
189
    ru_ready = 4
190
} ru_state_t;
191

    
192
typedef struct {
193
    PCIDevice dev;
194
#if 1
195
    uint8_t cmd;
196
    uint32_t start;
197
    uint32_t stop;
198
    uint8_t boundary;
199
    uint8_t tsr;
200
    uint8_t tpsr;
201
    uint16_t tcnt;
202
    uint16_t rcnt;
203
    uint32_t rsar;
204
    uint8_t rsr;
205
    uint8_t rxcr;
206
    uint8_t isr;
207
    uint8_t dcfg;
208
    uint8_t imr;
209
    uint8_t phys[6];            /* mac address */
210
    uint8_t curpag;
211
    uint8_t mult[8];            /* multicast mask array */
212
    int mmio_index;
213
    VLANClientState *vc;
214
#endif
215
    uint8_t scb_stat;           /* SCB stat/ack byte */
216
    uint8_t int_stat;           /* PCI interrupt status */
217
    uint32_t region[3];         /* PCI region addresses */
218
    uint8_t macaddr[6];
219
    uint32_t statcounter[19];
220
    uint16_t mdimem[32];
221
    eeprom_t *eeprom;
222
    uint32_t device;            /* device variant */
223
    uint32_t pointer;
224
    /* (cu_base + cu_offset) address the next command block in the command block list. */
225
    uint32_t cu_base;           /* CU base address */
226
    uint32_t cu_offset;         /* CU address offset */
227
    /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
228
    uint32_t ru_base;           /* RU base address */
229
    uint32_t ru_offset;         /* RU address offset */
230
    uint32_t statsaddr;         /* pointer to eepro100_stats_t */
231
    eepro100_stats_t statistics;        /* statistical counters */
232
#if 0
233
    uint16_t status;
234
#endif
235

    
236
    /* Configuration bytes. */
237
    uint8_t configuration[22];
238

    
239
    /* Data in mem is always in the byte order of the controller (le). */
240
    uint8_t mem[PCI_MEM_SIZE];
241
} EEPRO100State;
242

    
243
/* Default values for MDI (PHY) registers */
244
static const uint16_t eepro100_mdi_default[] = {
245
    /* MDI Registers 0 - 6, 7 */
246
    0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
247
    /* MDI Registers 8 - 15 */
248
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
249
    /* MDI Registers 16 - 31 */
250
    0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
251
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
252
};
253

    
254
/* Readonly mask for MDI (PHY) registers */
255
static const uint16_t eepro100_mdi_mask[] = {
256
    0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
257
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
258
    0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
259
    0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
260
};
261

    
262
#define POLYNOMIAL 0x04c11db6
263

    
264
/* From FreeBSD */
265
/* XXX: optimize */
266
static int compute_mcast_idx(const uint8_t * ep)
267
{
268
    uint32_t crc;
269
    int carry, i, j;
270
    uint8_t b;
271

    
272
    crc = 0xffffffff;
273
    for (i = 0; i < 6; i++) {
274
        b = *ep++;
275
        for (j = 0; j < 8; j++) {
276
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
277
            crc <<= 1;
278
            b >>= 1;
279
            if (carry) {
280
                crc = ((crc ^ POLYNOMIAL) | carry);
281
            }
282
        }
283
    }
284
    return (crc >> 26);
285
}
286

    
287
#if defined(DEBUG_EEPRO100)
288
static const char *nic_dump(const uint8_t * buf, unsigned size)
289
{
290
    static char dump[3 * 16 + 1];
291
    char *p = &dump[0];
292
    if (size > 16) {
293
        size = 16;
294
    }
295
    while (size-- > 0) {
296
        p += sprintf(p, " %02x", *buf++);
297
    }
298
    return dump;
299
}
300
#endif                          /* DEBUG_EEPRO100 */
301

    
302
enum scb_stat_ack {
303
    stat_ack_not_ours = 0x00,
304
    stat_ack_sw_gen = 0x04,
305
    stat_ack_rnr = 0x10,
306
    stat_ack_cu_idle = 0x20,
307
    stat_ack_frame_rx = 0x40,
308
    stat_ack_cu_cmd_done = 0x80,
309
    stat_ack_not_present = 0xFF,
310
    stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
311
    stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
312
};
313

    
314
static void disable_interrupt(EEPRO100State * s)
315
{
316
    if (s->int_stat) {
317
        TRACE(INT, logout("interrupt disabled\n"));
318
        qemu_irq_lower(s->dev.irq[0]);
319
        s->int_stat = 0;
320
    }
321
}
322

    
323
static void enable_interrupt(EEPRO100State * s)
324
{
325
    if (!s->int_stat) {
326
        TRACE(INT, logout("interrupt enabled\n"));
327
        qemu_irq_raise(s->dev.irq[0]);
328
        s->int_stat = 1;
329
    }
330
}
331

    
332
static void eepro100_acknowledge(EEPRO100State * s)
333
{
334
    s->scb_stat &= ~s->mem[SCBAck];
335
    s->mem[SCBAck] = s->scb_stat;
336
    if (s->scb_stat == 0) {
337
        disable_interrupt(s);
338
    }
339
}
340

    
341
static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
342
{
343
    uint8_t mask = ~s->mem[SCBIntmask];
344
    s->mem[SCBAck] |= stat;
345
    stat = s->scb_stat = s->mem[SCBAck];
346
    stat &= (mask | 0x0f);
347
    //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
348
    if (stat && (mask & 0x01)) {
349
        /* SCB mask and SCB Bit M do not disable interrupt. */
350
        enable_interrupt(s);
351
    } else if (s->int_stat) {
352
        disable_interrupt(s);
353
    }
354
}
355

    
356
static void eepro100_cx_interrupt(EEPRO100State * s)
357
{
358
    /* CU completed action command. */
359
    /* Transmit not ok (82557 only, not in emulation). */
360
    eepro100_interrupt(s, 0x80);
361
}
362

    
363
static void eepro100_cna_interrupt(EEPRO100State * s)
364
{
365
    /* CU left the active state. */
366
    eepro100_interrupt(s, 0x20);
367
}
368

    
369
static void eepro100_fr_interrupt(EEPRO100State * s)
370
{
371
    /* RU received a complete frame. */
372
    eepro100_interrupt(s, 0x40);
373
}
374

    
375
#if 0
376
static void eepro100_rnr_interrupt(EEPRO100State * s)
377
{
378
    /* RU is not ready. */
379
    eepro100_interrupt(s, 0x10);
380
}
381
#endif
382

    
383
static void eepro100_mdi_interrupt(EEPRO100State * s)
384
{
385
    /* MDI completed read or write cycle. */
386
    eepro100_interrupt(s, 0x08);
387
}
388

    
389
static void eepro100_swi_interrupt(EEPRO100State * s)
390
{
391
    /* Software has requested an interrupt. */
392
    eepro100_interrupt(s, 0x04);
393
}
394

    
395
#if 0
396
static void eepro100_fcp_interrupt(EEPRO100State * s)
397
{
398
    /* Flow control pause interrupt (82558 and later). */
399
    eepro100_interrupt(s, 0x01);
400
}
401
#endif
402

    
403
static void pci_reset(EEPRO100State * s)
404
{
405
    uint32_t device = s->device;
406
    uint8_t *pci_conf = s->dev.config;
407

    
408
    TRACE(OTHER, logout("%p\n", s));
409

    
410
    /* PCI Vendor ID */
411
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
412
    /* PCI Device ID depends on device and is set below. */
413
    /* PCI Command */
414
    PCI_CONFIG_16(PCI_COMMAND, 0x0000);
415
    /* PCI Status */
416
    PCI_CONFIG_16(PCI_STATUS, 0x2800);
417
    /* PCI Revision ID */
418
    PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
419
    /* PCI Class Code */
420
    PCI_CONFIG_8(0x09, 0x00);
421
    pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
422
    /* PCI Cache Line Size */
423
    /* check cache line size!!! */
424
    //~ PCI_CONFIG_8(0x0c, 0x00);
425
    /* PCI Latency Timer */
426
    PCI_CONFIG_8(0x0d, 0x20);   // latency timer = 32 clocks
427
    /* PCI Header Type */
428
    /* BIST (built-in self test) */
429
#if defined(TARGET_I386)
430
// !!! workaround for buggy bios
431
//~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
432
#endif
433
#if 0
434
    /* PCI Base Address Registers */
435
    /* CSR Memory Mapped Base Address */
436
    PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
437
                  PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_MEM_PREFETCH);
438
    /* CSR I/O Mapped Base Address */
439
    PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_ADDRESS_SPACE_IO);
440
#if 0
441
    /* Flash Memory Mapped Base Address */
442
    PCI_CONFIG_32(PCI_BASE_ADDRESS_2, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM);
443
#endif
444
#endif
445
    /* Expansion ROM Base Address (depends on boot disable!!!) */
446
    PCI_CONFIG_32(0x30, 0x00000000);
447
    /* Capability Pointer */
448
    PCI_CONFIG_8(0x34, 0xdc);
449
    /* Interrupt Line */
450
    /* Interrupt Pin */
451
    PCI_CONFIG_8(0x3d, 1);      // interrupt pin 0
452
    /* Minimum Grant */
453
    PCI_CONFIG_8(0x3e, 0x08);
454
    /* Maximum Latency */
455
    PCI_CONFIG_8(0x3f, 0x18);
456
    /* Power Management Capabilities / Next Item Pointer / Capability ID */
457
    PCI_CONFIG_32(0xdc, 0x7e210001);
458

    
459
    switch (device) {
460
    case i82551:
461
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
462
        PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
463
        break;
464
    case i82557B:
465
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
466
        PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
467
        break;
468
    case i82557C:
469
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
470
        PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
471
        break;
472
    case i82558B:
473
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
474
        PCI_CONFIG_16(PCI_STATUS, 0x2810);
475
        PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
476
        break;
477
    case i82559C:
478
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
479
        PCI_CONFIG_16(PCI_STATUS, 0x2810);
480
        //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
481
        break;
482
    case i82559ER:
483
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
484
        PCI_CONFIG_16(PCI_STATUS, 0x2810);
485
        PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
486
        break;
487
    //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
488
    //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030);       /* 82559 InBusiness 10/100 */
489
    default:
490
        logout("Device %X is undefined!\n", device);
491
    }
492

    
493
    if (device == i82557C || device == i82558B || device == i82559C) {
494
        logout("Get device id and revision from EEPROM!!!\n");
495
    }
496
}
497

    
498
static void nic_selective_reset(EEPRO100State * s)
499
{
500
    size_t i;
501
    uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
502
    //~ eeprom93xx_reset(s->eeprom);
503
    memcpy(eeprom_contents, s->macaddr, 6);
504
    eeprom_contents[0xa] = 0x4000;
505
    if (s->device == i82557B || s->device == i82557C)
506
        eeprom_contents[5] = 0x0100;
507
    uint16_t sum = 0;
508
    for (i = 0; i < EEPROM_SIZE - 1; i++) {
509
        sum += eeprom_contents[i];
510
    }
511
    eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
512
    TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
513

    
514
    memset(s->mem, 0, sizeof(s->mem));
515
    uint32_t val = BIT(21);
516
    memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
517

    
518
    assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
519
    memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
520
}
521

    
522
static void nic_reset(void *opaque)
523
{
524
    EEPRO100State *s = opaque;
525
    TRACE(OTHER, logout("%p\n", s));
526
    nic_selective_reset(s);
527
}
528

    
529
#if defined(DEBUG_EEPRO100)
530
static const char * const reg[PCI_IO_SIZE / 4] = {
531
    "Command/Status",
532
    "General Pointer",
533
    "Port",
534
    "EEPROM/Flash Control",
535
    "MDI Control",
536
    "Receive DMA Byte Count",
537
    "Flow control",
538
    "General Status/Control"
539
};
540

    
541
static char *regname(uint32_t addr)
542
{
543
    static char buf[16];
544
    if (addr < PCI_IO_SIZE) {
545
        const char *r = reg[addr / 4];
546
        if (r != 0) {
547
            snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
548
        } else {
549
            snprintf(buf, sizeof(buf), "0x%02x", addr);
550
        }
551
    } else {
552
        snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
553
    }
554
    return buf;
555
}
556
#endif                          /* DEBUG_EEPRO100 */
557

    
558
#if 0
559
static uint16_t eepro100_read_status(EEPRO100State * s)
560
{
561
    uint16_t val = s->status;
562
    TRACE(OTHER, logout("val=0x%04x\n", val));
563
    return val;
564
}
565

566
static void eepro100_write_status(EEPRO100State * s, uint16_t val)
567
{
568
    TRACE(OTHER, logout("val=0x%04x\n", val));
569
    s->status = val;
570
}
571
#endif
572

    
573
/*****************************************************************************
574
 *
575
 * Command emulation.
576
 *
577
 ****************************************************************************/
578

    
579
#if 0
580
static uint16_t eepro100_read_command(EEPRO100State * s)
581
{
582
    uint16_t val = 0xffff;
583
    //~ TRACE(OTHER, logout("val=0x%04x\n", val));
584
    return val;
585
}
586
#endif
587

    
588
static bool device_supports_eTxCB(EEPRO100State * s)
589
{
590
    return (s->device != i82557B && s->device != i82557C);
591
}
592

    
593
/* Commands that can be put in a command list entry. */
594
enum commands {
595
    CmdNOp = 0,
596
    CmdIASetup = 1,
597
    CmdConfigure = 2,
598
    CmdMulticastList = 3,
599
    CmdTx = 4,
600
    CmdTDR = 5,                 /* load microcode */
601
    CmdDump = 6,
602
    CmdDiagnose = 7,
603

    
604
    /* And some extra flags: */
605
    CmdSuspend = 0x4000,        /* Suspend after completion. */
606
    CmdIntr = 0x2000,           /* Interrupt after completion. */
607
    CmdTxFlex = 0x0008,         /* Use "Flexible mode" for CmdTx command. */
608
};
609

    
610
static cu_state_t get_cu_state(EEPRO100State * s)
611
{
612
    return ((s->mem[SCBStatus] >> 6) & 0x03);
613
}
614

    
615
static void set_cu_state(EEPRO100State * s, cu_state_t state)
616
{
617
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
618
}
619

    
620
static ru_state_t get_ru_state(EEPRO100State * s)
621
{
622
    return ((s->mem[SCBStatus] >> 2) & 0x0f);
623
}
624

    
625
static void set_ru_state(EEPRO100State * s, ru_state_t state)
626
{
627
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
628
}
629

    
630
static void dump_statistics(EEPRO100State * s)
631
{
632
    /* Dump statistical data. Most data is never changed by the emulation
633
     * and always 0, so we first just copy the whole block and then those
634
     * values which really matter.
635
     * Number of data should check configuration!!!
636
     */
637
    cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
638
    stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
639
    stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
640
    stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
641
    stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
642
    //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
643
    //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
644
    //~ missing("CU dump statistical counters");
645
}
646

    
647
static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
648
{
649
    eepro100_tx_t tx;
650
    uint32_t cb_address;
651
    switch (val) {
652
    case CU_NOP:
653
        /* No operation. */
654
        break;
655
    case CU_START:
656
        if (get_cu_state(s) != cu_idle) {
657
            /* Intel documentation says that CU must be idle for the CU
658
             * start command. Intel driver for Linux also starts the CU
659
             * from suspended state. */
660
            logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
661
            //~ assert(!"wrong CU state");
662
        }
663
        set_cu_state(s, cu_active);
664
        s->cu_offset = s->pointer;
665
      next_command:
666
        cb_address = s->cu_base + s->cu_offset;
667
        cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
668
        uint16_t status = le16_to_cpu(tx.status);
669
        uint16_t command = le16_to_cpu(tx.command);
670
        logout
671
            ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
672
             val, status, command, tx.link);
673
        bool bit_el = ((command & 0x8000) != 0);
674
        bool bit_s = ((command & 0x4000) != 0);
675
        bool bit_i = ((command & 0x2000) != 0);
676
        bool bit_nc = ((command & 0x0010) != 0);
677
        bool success = true;
678
        //~ bool bit_sf = ((command & 0x0008) != 0);
679
        uint16_t cmd = command & 0x0007;
680
        s->cu_offset = le32_to_cpu(tx.link);
681
        switch (cmd) {
682
        case CmdNOp:
683
            /* Do nothing. */
684
            break;
685
        case CmdIASetup:
686
            cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
687
            TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6)));
688
            break;
689
        case CmdConfigure:
690
            cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
691
                                     sizeof(s->configuration));
692
            TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
693
            break;
694
        case CmdMulticastList:
695
            //~ missing("multicast list");
696
            break;
697
        case CmdTx:
698
            (void)0;
699
            uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
700
            uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
701
            TRACE(RXTX, logout
702
                ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
703
                 tbd_array, tcb_bytes, tx.tbd_count));
704

    
705
            if (bit_nc) {
706
                missing("CmdTx: NC = 0");
707
                success = false;
708
                break;
709
            }
710
            //~ assert(!bit_sf);
711
            if (tcb_bytes > 2600) {
712
                logout("TCB byte count too large, using 2600\n");
713
                tcb_bytes = 2600;
714
            }
715
            /* Next assertion fails for local configuration. */
716
            //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
717
            if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
718
                logout
719
                    ("illegal values of TBD array address and TCB byte count!\n");
720
            }
721
            // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
722
            uint8_t buf[2600];
723
            uint16_t size = 0;
724
            uint32_t tbd_address = cb_address + 0x10;
725
            assert(tcb_bytes <= sizeof(buf));
726
            while (size < tcb_bytes) {
727
                uint32_t tx_buffer_address = ldl_phys(tbd_address);
728
                uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
729
                //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
730
                tbd_address += 8;
731
                TRACE(RXTX, logout
732
                    ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
733
                     tx_buffer_address, tx_buffer_size));
734
                tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
735
                cpu_physical_memory_read(tx_buffer_address, &buf[size],
736
                                         tx_buffer_size);
737
                size += tx_buffer_size;
738
            }
739
            if (tbd_array == 0xffffffff) {
740
                /* Simplified mode. Was already handled by code above. */
741
            } else {
742
                /* Flexible mode. */
743
                uint8_t tbd_count = 0;
744
                if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
745
                    /* Extended Flexible TCB. */
746
                    for (; tbd_count < 2; tbd_count++) {
747
                        uint32_t tx_buffer_address = ldl_phys(tbd_address);
748
                        uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
749
                        uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
750
                        tbd_address += 8;
751
                        TRACE(RXTX, logout
752
                            ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
753
                             tx_buffer_address, tx_buffer_size));
754
                        tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
755
                        cpu_physical_memory_read(tx_buffer_address, &buf[size],
756
                                                 tx_buffer_size);
757
                        size += tx_buffer_size;
758
                        if (tx_buffer_el & 1) {
759
                            break;
760
                        }
761
                    }
762
                }
763
                tbd_address = tbd_array;
764
                for (; tbd_count < tx.tbd_count; tbd_count++) {
765
                    uint32_t tx_buffer_address = ldl_phys(tbd_address);
766
                    uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
767
                    uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
768
                    tbd_address += 8;
769
                    TRACE(RXTX, logout
770
                        ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
771
                         tx_buffer_address, tx_buffer_size));
772
                    tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
773
                    cpu_physical_memory_read(tx_buffer_address, &buf[size],
774
                                             tx_buffer_size);
775
                    size += tx_buffer_size;
776
                    if (tx_buffer_el & 1) {
777
                        break;
778
                    }
779
                }
780
            }
781
            TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
782
            qemu_send_packet(s->vc, buf, size);
783
            s->statistics.tx_good_frames++;
784
            /* Transmit with bad status would raise an CX/TNO interrupt.
785
             * (82557 only). Emulation never has bad status. */
786
            //~ eepro100_cx_interrupt(s);
787
            break;
788
        case CmdTDR:
789
            TRACE(OTHER, logout("load microcode\n"));
790
            /* Starting with offset 8, the command contains
791
             * 64 dwords microcode which we just ignore here. */
792
            break;
793
        default:
794
            missing("undefined command");
795
            success = false;
796
            break;
797
        }
798
        /* Write new status. */
799
        stw_phys(cb_address, status | 0x8000 | (success ? 0x2000 : 0));
800
        if (bit_i) {
801
            /* CU completed action. */
802
            eepro100_cx_interrupt(s);
803
        }
804
        if (bit_el) {
805
            /* CU becomes idle. Terminate command loop. */
806
            set_cu_state(s, cu_idle);
807
            eepro100_cna_interrupt(s);
808
        } else if (bit_s) {
809
            /* CU becomes suspended. */
810
            set_cu_state(s, cu_suspended);
811
            eepro100_cna_interrupt(s);
812
        } else {
813
            /* More entries in list. */
814
            TRACE(OTHER, logout("CU list with at least one more entry\n"));
815
            goto next_command;
816
        }
817
        TRACE(OTHER, logout("CU list empty\n"));
818
        /* List is empty. Now CU is idle or suspended. */
819
        break;
820
    case CU_RESUME:
821
        if (get_cu_state(s) != cu_suspended) {
822
            logout("bad CU resume from CU state %u\n", get_cu_state(s));
823
            /* Workaround for bad Linux eepro100 driver which resumes
824
             * from idle state. */
825
            //~ missing("cu resume");
826
            set_cu_state(s, cu_suspended);
827
        }
828
        if (get_cu_state(s) == cu_suspended) {
829
            TRACE(OTHER, logout("CU resuming\n"));
830
            set_cu_state(s, cu_active);
831
            goto next_command;
832
        }
833
        break;
834
    case CU_STATSADDR:
835
        /* Load dump counters address. */
836
        s->statsaddr = s->pointer;
837
        TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
838
        break;
839
    case CU_SHOWSTATS:
840
        /* Dump statistical counters. */
841
        TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
842
        dump_statistics(s);
843
        break;
844
    case CU_CMD_BASE:
845
        /* Load CU base. */
846
        TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
847
        s->cu_base = s->pointer;
848
        break;
849
    case CU_DUMPSTATS:
850
        /* Dump and reset statistical counters. */
851
        TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
852
        dump_statistics(s);
853
        memset(&s->statistics, 0, sizeof(s->statistics));
854
        break;
855
    case CU_SRESUME:
856
        /* CU static resume. */
857
        missing("CU static resume");
858
        break;
859
    default:
860
        missing("Undefined CU command");
861
    }
862
}
863

    
864
static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
865
{
866
    switch (val) {
867
    case RU_NOP:
868
        /* No operation. */
869
        break;
870
    case RX_START:
871
        /* RU start. */
872
        if (get_ru_state(s) != ru_idle) {
873
            logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
874
            //~ assert(!"wrong RU state");
875
        }
876
        set_ru_state(s, ru_ready);
877
        s->ru_offset = s->pointer;
878
        TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
879
        break;
880
    case RX_RESUME:
881
        /* Restart RU. */
882
        if (get_ru_state(s) != ru_suspended) {
883
            logout("RU state is %u, should be %u\n", get_ru_state(s),
884
                   ru_suspended);
885
            //~ assert(!"wrong RU state");
886
        }
887
        set_ru_state(s, ru_ready);
888
        break;
889
    case RX_ADDR_LOAD:
890
        /* Load RU base. */
891
        TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
892
        s->ru_base = s->pointer;
893
        break;
894
    default:
895
        logout("val=0x%02x (undefined RU command)\n", val);
896
        missing("Undefined SU command");
897
    }
898
}
899

    
900
static void eepro100_write_command(EEPRO100State * s, uint8_t val)
901
{
902
    eepro100_ru_command(s, val & 0x0f);
903
    eepro100_cu_command(s, val & 0xf0);
904
    if ((val) == 0) {
905
        TRACE(OTHER, logout("val=0x%02x\n", val));
906
    }
907
    /* Clear command byte after command was accepted. */
908
    s->mem[SCBCmd] = 0;
909
}
910

    
911
/*****************************************************************************
912
 *
913
 * EEPROM emulation.
914
 *
915
 ****************************************************************************/
916

    
917
#define EEPROM_CS       0x02
918
#define EEPROM_SK       0x01
919
#define EEPROM_DI       0x04
920
#define EEPROM_DO       0x08
921

    
922
static uint16_t eepro100_read_eeprom(EEPRO100State * s)
923
{
924
    uint16_t val;
925
    memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
926
    if (eeprom93xx_read(s->eeprom)) {
927
        val |= EEPROM_DO;
928
    } else {
929
        val &= ~EEPROM_DO;
930
    }
931
    TRACE(EEPROM, logout("val=0x%04x\n", val));
932
    return val;
933
}
934

    
935
static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
936
{
937
    TRACE(EEPROM, logout("val=0x%02x\n", val));
938

    
939
    /* mask unwriteable bits */
940
    //~ val = SET_MASKED(val, 0x31, eeprom->value);
941

    
942
    int eecs = ((val & EEPROM_CS) != 0);
943
    int eesk = ((val & EEPROM_SK) != 0);
944
    int eedi = ((val & EEPROM_DI) != 0);
945
    eeprom93xx_write(eeprom, eecs, eesk, eedi);
946
}
947

    
948
static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
949
{
950
    s->pointer = le32_to_cpu(val);
951
    TRACE(OTHER, logout("val=0x%08x\n", val));
952
}
953

    
954
/*****************************************************************************
955
 *
956
 * MDI emulation.
957
 *
958
 ****************************************************************************/
959

    
960
#if defined(DEBUG_EEPRO100)
961
static const char * const mdi_op_name[] = {
962
    "opcode 0",
963
    "write",
964
    "read",
965
    "opcode 3"
966
};
967

    
968
static const char * const mdi_reg_name[] = {
969
    "Control",
970
    "Status",
971
    "PHY Identification (Word 1)",
972
    "PHY Identification (Word 2)",
973
    "Auto-Negotiation Advertisement",
974
    "Auto-Negotiation Link Partner Ability",
975
    "Auto-Negotiation Expansion"
976
};
977

    
978
static const char *reg2name(uint8_t reg)
979
{
980
    static char buffer[10];
981
    const char *p = buffer;
982
    if (reg < ARRAY_SIZE(mdi_reg_name)) {
983
        p = mdi_reg_name[reg];
984
    } else {
985
        snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
986
    }
987
    return p;
988
}
989
#endif                          /* DEBUG_EEPRO100 */
990

    
991
static uint32_t eepro100_read_mdi(EEPRO100State * s)
992
{
993
    uint32_t val;
994
    memcpy(&val, &s->mem[0x10], sizeof(val));
995

    
996
#ifdef DEBUG_EEPRO100
997
    uint8_t raiseint = (val & BIT(29)) >> 29;
998
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
999
    uint8_t phy = (val & BITS(25, 21)) >> 21;
1000
    uint8_t reg = (val & BITS(20, 16)) >> 16;
1001
    uint16_t data = (val & BITS(15, 0));
1002
#endif
1003
    /* Emulation takes no time to finish MDI transaction. */
1004
    val |= BIT(28);
1005
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1006
                      val, raiseint, mdi_op_name[opcode], phy,
1007
                      reg2name(reg), data));
1008
    return val;
1009
}
1010

    
1011
static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
1012
{
1013
    uint8_t raiseint = (val & BIT(29)) >> 29;
1014
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
1015
    uint8_t phy = (val & BITS(25, 21)) >> 21;
1016
    uint8_t reg = (val & BITS(20, 16)) >> 16;
1017
    uint16_t data = (val & BITS(15, 0));
1018
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1019
          val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
1020
    if (phy != 1) {
1021
        /* Unsupported PHY address. */
1022
        //~ logout("phy must be 1 but is %u\n", phy);
1023
        data = 0;
1024
    } else if (opcode != 1 && opcode != 2) {
1025
        /* Unsupported opcode. */
1026
        logout("opcode must be 1 or 2 but is %u\n", opcode);
1027
        data = 0;
1028
    } else if (reg > 6) {
1029
        /* Unsupported register. */
1030
        logout("register must be 0...6 but is %u\n", reg);
1031
        data = 0;
1032
    } else {
1033
        TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1034
                          val, raiseint, mdi_op_name[opcode], phy,
1035
                          reg2name(reg), data));
1036
        if (opcode == 1) {
1037
            /* MDI write */
1038
            switch (reg) {
1039
            case 0:            /* Control Register */
1040
                if (data & 0x8000) {
1041
                    /* Reset status and control registers to default. */
1042
                    s->mdimem[0] = eepro100_mdi_default[0];
1043
                    s->mdimem[1] = eepro100_mdi_default[1];
1044
                    data = s->mdimem[reg];
1045
                } else {
1046
                    /* Restart Auto Configuration = Normal Operation */
1047
                    data &= ~0x0200;
1048
                }
1049
                break;
1050
            case 1:            /* Status Register */
1051
                missing("not writable");
1052
                data = s->mdimem[reg];
1053
                break;
1054
            case 2:            /* PHY Identification Register (Word 1) */
1055
            case 3:            /* PHY Identification Register (Word 2) */
1056
                missing("not implemented");
1057
                break;
1058
            case 4:            /* Auto-Negotiation Advertisement Register */
1059
            case 5:            /* Auto-Negotiation Link Partner Ability Register */
1060
                break;
1061
            case 6:            /* Auto-Negotiation Expansion Register */
1062
            default:
1063
                missing("not implemented");
1064
            }
1065
            s->mdimem[reg] = data;
1066
        } else if (opcode == 2) {
1067
            /* MDI read */
1068
            switch (reg) {
1069
            case 0:            /* Control Register */
1070
                if (data & 0x8000) {
1071
                    /* Reset status and control registers to default. */
1072
                    s->mdimem[0] = eepro100_mdi_default[0];
1073
                    s->mdimem[1] = eepro100_mdi_default[1];
1074
                }
1075
                break;
1076
            case 1:            /* Status Register */
1077
                s->mdimem[reg] |= 0x0020;
1078
                break;
1079
            case 2:            /* PHY Identification Register (Word 1) */
1080
            case 3:            /* PHY Identification Register (Word 2) */
1081
            case 4:            /* Auto-Negotiation Advertisement Register */
1082
                break;
1083
            case 5:            /* Auto-Negotiation Link Partner Ability Register */
1084
                s->mdimem[reg] = 0x41fe;
1085
                break;
1086
            case 6:            /* Auto-Negotiation Expansion Register */
1087
                s->mdimem[reg] = 0x0001;
1088
                break;
1089
            }
1090
            data = s->mdimem[reg];
1091
        }
1092
        /* Emulation takes no time to finish MDI transaction.
1093
         * Set MDI bit in SCB status register. */
1094
        s->mem[SCBAck] |= 0x08;
1095
        val |= BIT(28);
1096
        if (raiseint) {
1097
            eepro100_mdi_interrupt(s);
1098
        }
1099
    }
1100
    val = (val & 0xffff0000) + data;
1101
    memcpy(&s->mem[0x10], &val, sizeof(val));
1102
}
1103

    
1104
/*****************************************************************************
1105
 *
1106
 * Port emulation.
1107
 *
1108
 ****************************************************************************/
1109

    
1110
#define PORT_SOFTWARE_RESET     0
1111
#define PORT_SELFTEST           1
1112
#define PORT_SELECTIVE_RESET    2
1113
#define PORT_DUMP               3
1114
#define PORT_SELECTION_MASK     3
1115

    
1116
typedef struct {
1117
    uint32_t st_sign;           /* Self Test Signature */
1118
    uint32_t st_result;         /* Self Test Results */
1119
} eepro100_selftest_t;
1120

    
1121
static uint32_t eepro100_read_port(EEPRO100State * s)
1122
{
1123
    return 0;
1124
}
1125

    
1126
static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1127
{
1128
    val = le32_to_cpu(val);
1129
    uint32_t address = (val & ~PORT_SELECTION_MASK);
1130
    uint8_t selection = (val & PORT_SELECTION_MASK);
1131
    switch (selection) {
1132
    case PORT_SOFTWARE_RESET:
1133
        nic_reset(s);
1134
        break;
1135
    case PORT_SELFTEST:
1136
        TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1137
        eepro100_selftest_t data;
1138
        cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1139
        data.st_sign = 0xffffffff;
1140
        data.st_result = 0;
1141
        cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1142
        break;
1143
    case PORT_SELECTIVE_RESET:
1144
        TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1145
        nic_selective_reset(s);
1146
        break;
1147
    default:
1148
        logout("val=0x%08x\n", val);
1149
        missing("unknown port selection");
1150
    }
1151
}
1152

    
1153
/*****************************************************************************
1154
 *
1155
 * General hardware emulation.
1156
 *
1157
 ****************************************************************************/
1158

    
1159
static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1160
{
1161
    uint8_t val;
1162
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1163
        memcpy(&val, &s->mem[addr], sizeof(val));
1164
    }
1165

    
1166
    switch (addr) {
1167
    case SCBStatus:
1168
        //~ val = eepro100_read_status(s);
1169
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1170
        break;
1171
    case SCBAck:
1172
        //~ val = eepro100_read_status(s);
1173
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1174
        break;
1175
    case SCBCmd:
1176
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1177
        //~ val = eepro100_read_command(s);
1178
        break;
1179
    case SCBIntmask:
1180
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1181
        break;
1182
    case SCBPort + 3:
1183
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1184
        break;
1185
    case SCBeeprom:
1186
        val = eepro100_read_eeprom(s);
1187
        break;
1188
    case 0x1b:                 /* PMDR (power management driver register) */
1189
        val = 0;
1190
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1191
        break;
1192
    case 0x1d:                 /* general status register */
1193
        /* 100 Mbps full duplex, valid link */
1194
        val = 0x07;
1195
        TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1196
        break;
1197
    default:
1198
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1199
        missing("unknown byte read");
1200
    }
1201
    return val;
1202
}
1203

    
1204
static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1205
{
1206
    uint16_t val;
1207
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1208
        memcpy(&val, &s->mem[addr], sizeof(val));
1209
    }
1210

    
1211
    switch (addr) {
1212
    case SCBStatus:
1213
        //~ val = eepro100_read_status(s);
1214
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1215
        break;
1216
    case SCBeeprom:
1217
        val = eepro100_read_eeprom(s);
1218
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1219
        break;
1220
    default:
1221
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1222
        missing("unknown word read");
1223
    }
1224
    return val;
1225
}
1226

    
1227
static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1228
{
1229
    uint32_t val;
1230
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1231
        memcpy(&val, &s->mem[addr], sizeof(val));
1232
    }
1233

    
1234
    switch (addr) {
1235
    case SCBStatus:
1236
        //~ val = eepro100_read_status(s);
1237
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1238
        break;
1239
    case SCBPointer:
1240
        //~ val = eepro100_read_pointer(s);
1241
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1242
        break;
1243
    case SCBPort:
1244
        val = eepro100_read_port(s);
1245
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1246
        break;
1247
    case SCBCtrlMDI:
1248
        val = eepro100_read_mdi(s);
1249
        break;
1250
    default:
1251
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1252
        missing("unknown longword read");
1253
    }
1254
    return val;
1255
}
1256

    
1257
static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1258
{
1259
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1260
        memcpy(&s->mem[addr], &val, sizeof(val));
1261
    }
1262

    
1263
    TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1264

    
1265
    switch (addr) {
1266
    case SCBStatus:
1267
        //~ eepro100_write_status(s, val);
1268
        break;
1269
    case SCBAck:
1270
        eepro100_acknowledge(s);
1271
        break;
1272
    case SCBCmd:
1273
        eepro100_write_command(s, val);
1274
        break;
1275
    case SCBIntmask:
1276
        if (val & BIT(1)) {
1277
            eepro100_swi_interrupt(s);
1278
        }
1279
        eepro100_interrupt(s, 0);
1280
        break;
1281
    case SCBPort + 3:
1282
    case SCBFlow:       /* does not exist on 82557 */
1283
    case SCBFlow + 1:
1284
    case SCBFlow + 2:
1285
    case SCBFlow + 3:
1286
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1287
        break;
1288
    case SCBeeprom:
1289
        eepro100_write_eeprom(s->eeprom, val);
1290
        break;
1291
    default:
1292
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1293
        missing("unknown byte write");
1294
    }
1295
}
1296

    
1297
static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1298
{
1299
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1300
        memcpy(&s->mem[addr], &val, sizeof(val));
1301
    }
1302

    
1303
    TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1304

    
1305
    switch (addr) {
1306
    case SCBStatus:
1307
        //~ eepro100_write_status(s, val);
1308
        eepro100_acknowledge(s);
1309
        break;
1310
    case SCBCmd:
1311
        eepro100_write_command(s, val);
1312
        eepro100_write1(s, SCBIntmask, val >> 8);
1313
        break;
1314
    case SCBeeprom:
1315
        eepro100_write_eeprom(s->eeprom, val);
1316
        break;
1317
    default:
1318
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1319
        missing("unknown word write");
1320
    }
1321
}
1322

    
1323
static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1324
{
1325
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1326
        memcpy(&s->mem[addr], &val, sizeof(val));
1327
    }
1328

    
1329
    switch (addr) {
1330
    case SCBPointer:
1331
        eepro100_write_pointer(s, val);
1332
        break;
1333
    case SCBPort:
1334
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1335
        eepro100_write_port(s, val);
1336
        break;
1337
    case SCBCtrlMDI:
1338
        eepro100_write_mdi(s, val);
1339
        break;
1340
    default:
1341
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1342
        missing("unknown longword write");
1343
    }
1344
}
1345

    
1346
/*****************************************************************************
1347
 *
1348
 * Port mapped I/O.
1349
 *
1350
 ****************************************************************************/
1351

    
1352
static uint32_t ioport_read1(void *opaque, uint32_t addr)
1353
{
1354
    EEPRO100State *s = opaque;
1355
    //~ logout("addr=%s\n", regname(addr));
1356
    return eepro100_read1(s, addr - s->region[1]);
1357
}
1358

    
1359
static uint32_t ioport_read2(void *opaque, uint32_t addr)
1360
{
1361
    EEPRO100State *s = opaque;
1362
    return eepro100_read2(s, addr - s->region[1]);
1363
}
1364

    
1365
static uint32_t ioport_read4(void *opaque, uint32_t addr)
1366
{
1367
    EEPRO100State *s = opaque;
1368
    return eepro100_read4(s, addr - s->region[1]);
1369
}
1370

    
1371
static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1372
{
1373
    EEPRO100State *s = opaque;
1374
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1375
    eepro100_write1(s, addr - s->region[1], val);
1376
}
1377

    
1378
static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1379
{
1380
    EEPRO100State *s = opaque;
1381
    eepro100_write2(s, addr - s->region[1], val);
1382
}
1383

    
1384
static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1385
{
1386
    EEPRO100State *s = opaque;
1387
    eepro100_write4(s, addr - s->region[1], val);
1388
}
1389

    
1390
/***********************************************************/
1391
/* PCI EEPRO100 definitions */
1392

    
1393
static void pci_map(PCIDevice * pci_dev, int region_num,
1394
                    uint32_t addr, uint32_t size, int type)
1395
{
1396
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1397

    
1398
    TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1399
          region_num, addr, size, type));
1400

    
1401
    assert(region_num == 1);
1402
    register_ioport_write(addr, size, 1, ioport_write1, s);
1403
    register_ioport_read(addr, size, 1, ioport_read1, s);
1404
    register_ioport_write(addr, size, 2, ioport_write2, s);
1405
    register_ioport_read(addr, size, 2, ioport_read2, s);
1406
    register_ioport_write(addr, size, 4, ioport_write4, s);
1407
    register_ioport_read(addr, size, 4, ioport_read4, s);
1408

    
1409
    s->region[region_num] = addr;
1410
}
1411

    
1412
/*****************************************************************************
1413
 *
1414
 * Memory mapped I/O.
1415
 *
1416
 ****************************************************************************/
1417

    
1418
static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1419
{
1420
    EEPRO100State *s = opaque;
1421
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1422
    eepro100_write1(s, addr, val);
1423
}
1424

    
1425
static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1426
{
1427
    EEPRO100State *s = opaque;
1428
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1429
    eepro100_write2(s, addr, val);
1430
}
1431

    
1432
static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1433
{
1434
    EEPRO100State *s = opaque;
1435
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1436
    eepro100_write4(s, addr, val);
1437
}
1438

    
1439
static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1440
{
1441
    EEPRO100State *s = opaque;
1442
    //~ logout("addr=%s\n", regname(addr));
1443
    return eepro100_read1(s, addr);
1444
}
1445

    
1446
static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1447
{
1448
    EEPRO100State *s = opaque;
1449
    //~ logout("addr=%s\n", regname(addr));
1450
    return eepro100_read2(s, addr);
1451
}
1452

    
1453
static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1454
{
1455
    EEPRO100State *s = opaque;
1456
    //~ logout("addr=%s\n", regname(addr));
1457
    return eepro100_read4(s, addr);
1458
}
1459

    
1460
static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1461
    pci_mmio_writeb,
1462
    pci_mmio_writew,
1463
    pci_mmio_writel
1464
};
1465

    
1466
static CPUReadMemoryFunc * const pci_mmio_read[] = {
1467
    pci_mmio_readb,
1468
    pci_mmio_readw,
1469
    pci_mmio_readl
1470
};
1471

    
1472
static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1473
                         uint32_t addr, uint32_t size, int type)
1474
{
1475
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1476

    
1477
    TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1478
          region_num, addr, size, type));
1479

    
1480
    if (region_num == 0) {
1481
        /* Map control / status registers. */
1482
        cpu_register_physical_memory(addr, size, s->mmio_index);
1483
        s->region[region_num] = addr;
1484
    }
1485
}
1486

    
1487
static int nic_can_receive(VLANClientState *vc)
1488
{
1489
    EEPRO100State *s = vc->opaque;
1490
    TRACE(RXTX, logout("%p\n", s));
1491
    return get_ru_state(s) == ru_ready;
1492
    //~ return !eepro100_buffer_full(s);
1493
}
1494

    
1495
static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
1496
{
1497
    /* TODO:
1498
     * - Magic packets should set bit 30 in power management driver register.
1499
     * - Interesting packets should set bit 29 in power management driver register.
1500
     */
1501
    EEPRO100State *s = vc->opaque;
1502
    uint16_t rfd_status = 0xa000;
1503
    static const uint8_t broadcast_macaddr[6] =
1504
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1505

    
1506
    /* TODO: check multiple IA bit. */
1507
    if (s->configuration[20] & BIT(6)) {
1508
        missing("Multiple IA bit");
1509
        return -1;
1510
    }
1511

    
1512
    if (s->configuration[8] & 0x80) {
1513
        /* CSMA is disabled. */
1514
        logout("%p received while CSMA is disabled\n", s);
1515
        return -1;
1516
    } else if (size < 64 && (s->configuration[7] & 1)) {
1517
        /* Short frame and configuration byte 7/0 (discard short receive) set:
1518
         * Short frame is discarded */
1519
        logout("%p received short frame (%zu byte)\n", s, size);
1520
        s->statistics.rx_short_frame_errors++;
1521
        //~ return -1;
1522
    } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
1523
        /* Long frame and configuration byte 18/3 (long receive ok) not set:
1524
         * Long frames are discarded. */
1525
        logout("%p received long frame (%zu byte), ignored\n", s, size);
1526
        return -1;
1527
    } else if (memcmp(buf, s->macaddr, 6) == 0) {       // !!!
1528
        /* Frame matches individual address. */
1529
        /* TODO: check configuration byte 15/4 (ignore U/L). */
1530
        TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
1531
    } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1532
        /* Broadcast frame. */
1533
        TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
1534
        rfd_status |= 0x0002;
1535
    } else if (buf[0] & 0x01) { // !!!
1536
        /* Multicast frame. */
1537
        TRACE(RXTX, logout("%p received multicast, len=%zu\n", s, size));
1538
        /* TODO: check multicast all bit. */
1539
        if (s->configuration[21] & BIT(3)) {
1540
            missing("Multicast All bit");
1541
        }
1542
        int mcast_idx = compute_mcast_idx(buf);
1543
        if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
1544
            return size;
1545
        }
1546
        rfd_status |= 0x0002;
1547
    } else if (s->configuration[15] & 1) {
1548
        /* Promiscuous: receive all. */
1549
        TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
1550
        rfd_status |= 0x0004;
1551
    } else {
1552
        TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1553
              nic_dump(buf, size)));
1554
        return size;
1555
    }
1556

    
1557
    if (get_ru_state(s) != ru_ready) {
1558
        /* No resources available. */
1559
        logout("no resources, state=%u\n", get_ru_state(s));
1560
        s->statistics.rx_resource_errors++;
1561
        //~ assert(!"no resources");
1562
        return -1;
1563
    }
1564
    //~ !!!
1565
//~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1566
    eepro100_rx_t rx;
1567
    cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1568
                             offsetof(eepro100_rx_t, packet));
1569
    uint16_t rfd_command = le16_to_cpu(rx.command);
1570
    uint16_t rfd_size = le16_to_cpu(rx.size);
1571

    
1572
    if (size > rfd_size) {
1573
        logout("Receive buffer (%" PRId16 " bytes) too small for data "
1574
            "(%zu bytes); data truncated\n", rfd_size, size);
1575
        size = rfd_size;
1576
    }
1577
    if (size < 64) {
1578
        rfd_status |= 0x0080;
1579
    }
1580
    TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1581
          rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
1582
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1583
             rfd_status);
1584
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1585
    /* Early receive interrupt not supported. */
1586
    //~ eepro100_er_interrupt(s);
1587
    /* Receive CRC Transfer not supported. */
1588
    if (s->configuration[18] & 4) {
1589
        missing("Receive CRC Transfer");
1590
        return -1;
1591
    }
1592
    /* TODO: check stripping enable bit. */
1593
    //~ assert(!(s->configuration[17] & 1));
1594
    cpu_physical_memory_write(s->ru_base + s->ru_offset +
1595
                              offsetof(eepro100_rx_t, packet), buf, size);
1596
    s->statistics.rx_good_frames++;
1597
    eepro100_fr_interrupt(s);
1598
    s->ru_offset = le32_to_cpu(rx.link);
1599
    if (rfd_command & 0x8000) {
1600
        /* EL bit is set, so this was the last frame. */
1601
        logout("receive: Running out of frames\n");
1602
        set_ru_state(s, ru_suspended);
1603
    }
1604
    if (rfd_command & 0x4000) {
1605
        /* S bit is set. */
1606
        set_ru_state(s, ru_suspended);
1607
    }
1608
    return size;
1609
}
1610

    
1611
static int nic_load(QEMUFile * f, void *opaque, int version_id)
1612
{
1613
    EEPRO100State *s = opaque;
1614
    int i;
1615
    int ret;
1616

    
1617
    if (version_id > 3)
1618
        return -EINVAL;
1619

    
1620
    if (version_id >= 3) {
1621
        ret = pci_device_load(&s->dev, f);
1622
        if (ret < 0)
1623
            return ret;
1624
    }
1625

    
1626
    if (version_id >= 2) {
1627
        qemu_get_8s(f, &s->rxcr);
1628
    } else {
1629
        s->rxcr = 0x0c;
1630
    }
1631

    
1632
    qemu_get_8s(f, &s->cmd);
1633
    qemu_get_be32s(f, &s->start);
1634
    qemu_get_be32s(f, &s->stop);
1635
    qemu_get_8s(f, &s->boundary);
1636
    qemu_get_8s(f, &s->tsr);
1637
    qemu_get_8s(f, &s->tpsr);
1638
    qemu_get_be16s(f, &s->tcnt);
1639
    qemu_get_be16s(f, &s->rcnt);
1640
    qemu_get_be32s(f, &s->rsar);
1641
    qemu_get_8s(f, &s->rsr);
1642
    qemu_get_8s(f, &s->isr);
1643
    qemu_get_8s(f, &s->dcfg);
1644
    qemu_get_8s(f, &s->imr);
1645
    qemu_get_buffer(f, s->phys, 6);
1646
    qemu_get_8s(f, &s->curpag);
1647
    qemu_get_buffer(f, s->mult, 8);
1648
    qemu_get_buffer(f, s->mem, sizeof(s->mem));
1649

    
1650
    /* Restore all members of struct between scv_stat and mem. */
1651
    qemu_get_8s(f, &s->scb_stat);
1652
    qemu_get_8s(f, &s->int_stat);
1653
    for (i = 0; i < 3; i++) {
1654
        qemu_get_be32s(f, &s->region[i]);
1655
    }
1656
    qemu_get_buffer(f, s->macaddr, 6);
1657
    for (i = 0; i < 19; i++) {
1658
        qemu_get_be32s(f, &s->statcounter[i]);
1659
    }
1660
    for (i = 0; i < 32; i++) {
1661
        qemu_get_be16s(f, &s->mdimem[i]);
1662
    }
1663
    /* The eeprom should be saved and restored by its own routines. */
1664
    qemu_get_be32s(f, &s->device);
1665
    qemu_get_be32s(f, &s->pointer);
1666
    qemu_get_be32s(f, &s->cu_base);
1667
    qemu_get_be32s(f, &s->cu_offset);
1668
    qemu_get_be32s(f, &s->ru_base);
1669
    qemu_get_be32s(f, &s->ru_offset);
1670
    qemu_get_be32s(f, &s->statsaddr);
1671
    /* Restore epro100_stats_t statistics. */
1672
    qemu_get_be32s(f, &s->statistics.tx_good_frames);
1673
    qemu_get_be32s(f, &s->statistics.tx_max_collisions);
1674
    qemu_get_be32s(f, &s->statistics.tx_late_collisions);
1675
    qemu_get_be32s(f, &s->statistics.tx_underruns);
1676
    qemu_get_be32s(f, &s->statistics.tx_lost_crs);
1677
    qemu_get_be32s(f, &s->statistics.tx_deferred);
1678
    qemu_get_be32s(f, &s->statistics.tx_single_collisions);
1679
    qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
1680
    qemu_get_be32s(f, &s->statistics.tx_total_collisions);
1681
    qemu_get_be32s(f, &s->statistics.rx_good_frames);
1682
    qemu_get_be32s(f, &s->statistics.rx_crc_errors);
1683
    qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
1684
    qemu_get_be32s(f, &s->statistics.rx_resource_errors);
1685
    qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
1686
    qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
1687
    qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
1688
    qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
1689
    qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
1690
    qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
1691
    qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
1692
    qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
1693
    qemu_get_be32s(f, &s->statistics.complete);
1694
#if 0
1695
    qemu_get_be16s(f, &s->status);
1696
#endif
1697

    
1698
    /* Configuration bytes. */
1699
    qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
1700

    
1701
    return 0;
1702
}
1703

    
1704
static void nic_save(QEMUFile * f, void *opaque)
1705
{
1706
    EEPRO100State *s = opaque;
1707
    int i;
1708

    
1709
    pci_device_save(&s->dev, f);
1710

    
1711
    qemu_put_8s(f, &s->rxcr);
1712

    
1713
    qemu_put_8s(f, &s->cmd);
1714
    qemu_put_be32s(f, &s->start);
1715
    qemu_put_be32s(f, &s->stop);
1716
    qemu_put_8s(f, &s->boundary);
1717
    qemu_put_8s(f, &s->tsr);
1718
    qemu_put_8s(f, &s->tpsr);
1719
    qemu_put_be16s(f, &s->tcnt);
1720
    qemu_put_be16s(f, &s->rcnt);
1721
    qemu_put_be32s(f, &s->rsar);
1722
    qemu_put_8s(f, &s->rsr);
1723
    qemu_put_8s(f, &s->isr);
1724
    qemu_put_8s(f, &s->dcfg);
1725
    qemu_put_8s(f, &s->imr);
1726
    qemu_put_buffer(f, s->phys, 6);
1727
    qemu_put_8s(f, &s->curpag);
1728
    qemu_put_buffer(f, s->mult, 8);
1729
    qemu_put_buffer(f, s->mem, sizeof(s->mem));
1730

    
1731
    /* Save all members of struct between scv_stat and mem. */
1732
    qemu_put_8s(f, &s->scb_stat);
1733
    qemu_put_8s(f, &s->int_stat);
1734
    for (i = 0; i < 3; i++) {
1735
        qemu_put_be32s(f, &s->region[i]);
1736
    }
1737
    qemu_put_buffer(f, s->macaddr, 6);
1738
    for (i = 0; i < 19; i++) {
1739
        qemu_put_be32s(f, &s->statcounter[i]);
1740
    }
1741
    for (i = 0; i < 32; i++) {
1742
        qemu_put_be16s(f, &s->mdimem[i]);
1743
    }
1744
    /* The eeprom should be saved and restored by its own routines. */
1745
    qemu_put_be32s(f, &s->device);
1746
    qemu_put_be32s(f, &s->pointer);
1747
    qemu_put_be32s(f, &s->cu_base);
1748
    qemu_put_be32s(f, &s->cu_offset);
1749
    qemu_put_be32s(f, &s->ru_base);
1750
    qemu_put_be32s(f, &s->ru_offset);
1751
    qemu_put_be32s(f, &s->statsaddr);
1752
    /* Save epro100_stats_t statistics. */
1753
    qemu_put_be32s(f, &s->statistics.tx_good_frames);
1754
    qemu_put_be32s(f, &s->statistics.tx_max_collisions);
1755
    qemu_put_be32s(f, &s->statistics.tx_late_collisions);
1756
    qemu_put_be32s(f, &s->statistics.tx_underruns);
1757
    qemu_put_be32s(f, &s->statistics.tx_lost_crs);
1758
    qemu_put_be32s(f, &s->statistics.tx_deferred);
1759
    qemu_put_be32s(f, &s->statistics.tx_single_collisions);
1760
    qemu_put_be32s(f, &s->statistics.tx_multiple_collisions);
1761
    qemu_put_be32s(f, &s->statistics.tx_total_collisions);
1762
    qemu_put_be32s(f, &s->statistics.rx_good_frames);
1763
    qemu_put_be32s(f, &s->statistics.rx_crc_errors);
1764
    qemu_put_be32s(f, &s->statistics.rx_alignment_errors);
1765
    qemu_put_be32s(f, &s->statistics.rx_resource_errors);
1766
    qemu_put_be32s(f, &s->statistics.rx_overrun_errors);
1767
    qemu_put_be32s(f, &s->statistics.rx_cdt_errors);
1768
    qemu_put_be32s(f, &s->statistics.rx_short_frame_errors);
1769
    qemu_put_be32s(f, &s->statistics.fc_xmt_pause);
1770
    qemu_put_be32s(f, &s->statistics.fc_rcv_pause);
1771
    qemu_put_be32s(f, &s->statistics.fc_rcv_unsupported);
1772
    qemu_put_be16s(f, &s->statistics.xmt_tco_frames);
1773
    qemu_put_be16s(f, &s->statistics.rcv_tco_frames);
1774
    qemu_put_be32s(f, &s->statistics.complete);
1775
#if 0
1776
    qemu_put_be16s(f, &s->status);
1777
#endif
1778

    
1779
    /* Configuration bytes. */
1780
    qemu_put_buffer(f, s->configuration, sizeof(s->configuration));
1781
}
1782

    
1783
static void nic_cleanup(VLANClientState *vc)
1784
{
1785
    EEPRO100State *s = vc->opaque;
1786

    
1787
    unregister_savevm(vc->model, s);
1788

    
1789
    eeprom93xx_free(s->eeprom);
1790
}
1791

    
1792
static int pci_nic_uninit(PCIDevice *pci_dev)
1793
{
1794
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1795

    
1796
    cpu_unregister_io_memory(s->mmio_index);
1797

    
1798
    return 0;
1799
}
1800

    
1801
static int nic_init(PCIDevice *pci_dev, uint32_t device)
1802
{
1803
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1804

    
1805
    TRACE(OTHER, logout("\n"));
1806

    
1807
    s->device = device;
1808

    
1809
    pci_reset(s);
1810

    
1811
    /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1812
     * i82559 and later support 64 or 256 word EEPROM. */
1813
    s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1814

    
1815
    /* Handler for memory-mapped I/O */
1816
    s->mmio_index =
1817
        cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1818

    
1819
    pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1820
                           PCI_ADDRESS_SPACE_MEM |
1821
                           PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
1822
    pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
1823
                           pci_map);
1824
    pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
1825
                           pci_mmio_map);
1826

    
1827
    qdev_get_macaddr(&s->dev.qdev, s->macaddr);
1828
    logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1829
    assert(s->region[1] == 0);
1830

    
1831
    nic_reset(s);
1832

    
1833
    s->vc = qdev_get_vlan_client(&s->dev.qdev,
1834
                                 nic_can_receive, nic_receive, NULL,
1835
                                 nic_cleanup, s);
1836

    
1837
    qemu_format_nic_info_str(s->vc, s->macaddr);
1838
    TRACE(OTHER, logout("%s\n", s->vc->info_str));
1839

    
1840
    qemu_register_reset(nic_reset, s);
1841

    
1842
    register_savevm(s->vc->model, -1, 3, nic_save, nic_load, s);
1843
    return 0;
1844
}
1845

    
1846
static int pci_i82550_init(PCIDevice *pci_dev)
1847
{
1848
    return nic_init(pci_dev, i82550);
1849
}
1850

    
1851
static int pci_i82551_init(PCIDevice *pci_dev)
1852
{
1853
    return nic_init(pci_dev, i82551);
1854
}
1855

    
1856
static int pci_i82557a_init(PCIDevice *pci_dev)
1857
{
1858
    return nic_init(pci_dev, i82557A);
1859
}
1860

    
1861
static int pci_i82557b_init(PCIDevice *pci_dev)
1862
{
1863
    return nic_init(pci_dev, i82557B);
1864
}
1865

    
1866
static int pci_i82557c_init(PCIDevice *pci_dev)
1867
{
1868
    return nic_init(pci_dev, i82557C);
1869
}
1870

    
1871
static int pci_i82558a_init(PCIDevice *pci_dev)
1872
{
1873
    return nic_init(pci_dev, i82558A);
1874
}
1875

    
1876
static int pci_i82558b_init(PCIDevice *pci_dev)
1877
{
1878
    return nic_init(pci_dev, i82558B);
1879
}
1880

    
1881
static int pci_i82559a_init(PCIDevice *pci_dev)
1882
{
1883
    return nic_init(pci_dev, i82559A);
1884
}
1885

    
1886
static int pci_i82559b_init(PCIDevice *pci_dev)
1887
{
1888
    return nic_init(pci_dev, i82559B);
1889
}
1890

    
1891
static int pci_i82559c_init(PCIDevice *pci_dev)
1892
{
1893
    return nic_init(pci_dev, i82559C);
1894
}
1895

    
1896
static int pci_i82559er_init(PCIDevice *pci_dev)
1897
{
1898
    return nic_init(pci_dev, i82559ER);
1899
}
1900

    
1901
static int pci_i82562_init(PCIDevice *pci_dev)
1902
{
1903
    return nic_init(pci_dev, i82562);
1904
}
1905

    
1906
static PCIDeviceInfo eepro100_info[] = {
1907
    {
1908
        .qdev.name = "i82550",
1909
        .qdev.size = sizeof(EEPRO100State),
1910
        .init      = pci_i82550_init,
1911
    },{
1912
        .qdev.name = "i82551",
1913
        .qdev.size = sizeof(EEPRO100State),
1914
        .init      = pci_i82551_init,
1915
        .exit      = pci_nic_uninit,
1916
    },{
1917
        .qdev.name = "i82557a",
1918
        .qdev.size = sizeof(EEPRO100State),
1919
        .init      = pci_i82557a_init,
1920
    },{
1921
        .qdev.name = "i82557b",
1922
        .qdev.size = sizeof(EEPRO100State),
1923
        .init      = pci_i82557b_init,
1924
        .exit      = pci_nic_uninit,
1925
    },{
1926
        .qdev.name = "i82557c",
1927
        .qdev.size = sizeof(EEPRO100State),
1928
        .init      = pci_i82557c_init,
1929
    },{
1930
        .qdev.name = "i82558a",
1931
        .qdev.size = sizeof(EEPRO100State),
1932
        .init      = pci_i82558a_init,
1933
    },{
1934
        .qdev.name = "i82558b",
1935
        .qdev.size = sizeof(EEPRO100State),
1936
        .init      = pci_i82558b_init,
1937
    },{
1938
        .qdev.name = "i82559a",
1939
        .qdev.size = sizeof(EEPRO100State),
1940
        .init      = pci_i82559a_init,
1941
    },{
1942
        .qdev.name = "i82559b",
1943
        .qdev.size = sizeof(EEPRO100State),
1944
        .init      = pci_i82559b_init,
1945
    },{
1946
        .qdev.name = "i82559c",
1947
        .qdev.size = sizeof(EEPRO100State),
1948
        .init      = pci_i82559c_init,
1949
    },{
1950
        .qdev.name = "i82559er",
1951
        .qdev.size = sizeof(EEPRO100State),
1952
        .init      = pci_i82559er_init,
1953
        .exit      = pci_nic_uninit,
1954
    },{
1955
        .qdev.name = "i82562",
1956
        .qdev.size = sizeof(EEPRO100State),
1957
        .init      = pci_i82562_init,
1958
    },{
1959
        /* end of list */
1960
    }
1961
};
1962

    
1963
static void eepro100_register_devices(void)
1964
{
1965
    pci_qdev_register_many(eepro100_info);
1966
}
1967

    
1968
device_init(eepro100_register_devices)