Statistics
| Branch: | Revision:

root / hw / eepro100.c @ bc578fe0

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
    case SCBCmd:
1215
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1216
        break;
1217
    case SCBeeprom:
1218
        val = eepro100_read_eeprom(s);
1219
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1220
        break;
1221
    default:
1222
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1223
        missing("unknown word read");
1224
    }
1225
    return val;
1226
}
1227

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1391
/***********************************************************/
1392
/* PCI EEPRO100 definitions */
1393

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1702
    return 0;
1703
}
1704

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

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

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

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

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

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

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

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

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

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

    
1797
    cpu_unregister_io_memory(s->mmio_index);
1798

    
1799
    return 0;
1800
}
1801

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

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

    
1808
    s->device = device;
1809

    
1810
    pci_reset(s);
1811

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

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

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

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

    
1832
    nic_reset(s);
1833

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

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

    
1841
    qemu_register_reset(nic_reset, s);
1842

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1969
device_init(eepro100_register_devices)