Statistics
| Branch: | Revision:

root / hw / eepro100.c @ c4c270e2

History | View | Annotate | Download (60.2 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)       assert(!"feature is missing in this emulation: " text)
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 mult[8];            /* multicast mask array */
199
    int mmio_index;
200
    VLANClientState *vc;
201
#endif
202
    uint8_t scb_stat;           /* SCB stat/ack byte */
203
    uint8_t int_stat;           /* PCI interrupt status */
204
    uint32_t region[3];         /* PCI region addresses */
205
    uint8_t macaddr[6];
206
    uint16_t mdimem[32];
207
    eeprom_t *eeprom;
208
    uint32_t device;            /* device variant */
209
    uint32_t pointer;
210
    /* (cu_base + cu_offset) address the next command block in the command block list. */
211
    uint32_t cu_base;           /* CU base address */
212
    uint32_t cu_offset;         /* CU address offset */
213
    /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
214
    uint32_t ru_base;           /* RU base address */
215
    uint32_t ru_offset;         /* RU address offset */
216
    uint32_t statsaddr;         /* pointer to eepro100_stats_t */
217
    /* Statistical counters. Also used for wake-up packet (i82559). */
218
    eepro100_stats_t statistics;
219
#if 0
220
    uint16_t status;
221
#endif
222

    
223
    /* Configuration bytes. */
224
    uint8_t configuration[22];
225

    
226
    /* Data in mem is always in the byte order of the controller (le). */
227
    uint8_t mem[PCI_MEM_SIZE];
228
} EEPRO100State;
229

    
230
/* Parameters for nic_save, nic_load. */
231
static const int eepro100_instance = -1;
232
static const int eepro100_version = 20090807;
233

    
234
/* Default values for MDI (PHY) registers */
235
static const uint16_t eepro100_mdi_default[] = {
236
    /* MDI Registers 0 - 6, 7 */
237
    0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
238
    /* MDI Registers 8 - 15 */
239
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
240
    /* MDI Registers 16 - 31 */
241
    0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
242
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
243
};
244

    
245
/* Readonly mask for MDI (PHY) registers */
246
static const uint16_t eepro100_mdi_mask[] = {
247
    0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
248
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
249
    0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
250
    0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
251
};
252

    
253
#define POLYNOMIAL 0x04c11db6
254

    
255
/* From FreeBSD */
256
/* XXX: optimize */
257
static int compute_mcast_idx(const uint8_t * ep)
258
{
259
    uint32_t crc;
260
    int carry, i, j;
261
    uint8_t b;
262

    
263
    crc = 0xffffffff;
264
    for (i = 0; i < 6; i++) {
265
        b = *ep++;
266
        for (j = 0; j < 8; j++) {
267
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
268
            crc <<= 1;
269
            b >>= 1;
270
            if (carry) {
271
                crc = ((crc ^ POLYNOMIAL) | carry);
272
            }
273
        }
274
    }
275
    return (crc >> 26);
276
}
277

    
278
#if defined(DEBUG_EEPRO100)
279
static const char *nic_dump(const uint8_t * buf, unsigned size)
280
{
281
    static char dump[3 * 16 + 1];
282
    char *p = &dump[0];
283
    if (size > 16) {
284
        size = 16;
285
    }
286
    while (size-- > 0) {
287
        p += sprintf(p, " %02x", *buf++);
288
    }
289
    return dump;
290
}
291
#endif                          /* DEBUG_EEPRO100 */
292

    
293
enum scb_stat_ack {
294
    stat_ack_not_ours = 0x00,
295
    stat_ack_sw_gen = 0x04,
296
    stat_ack_rnr = 0x10,
297
    stat_ack_cu_idle = 0x20,
298
    stat_ack_frame_rx = 0x40,
299
    stat_ack_cu_cmd_done = 0x80,
300
    stat_ack_not_present = 0xFF,
301
    stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
302
    stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
303
};
304

    
305
static void disable_interrupt(EEPRO100State * s)
306
{
307
    if (s->int_stat) {
308
        TRACE(INT, logout("interrupt disabled\n"));
309
        qemu_irq_lower(s->dev.irq[0]);
310
        s->int_stat = 0;
311
    }
312
}
313

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

    
323
static void eepro100_acknowledge(EEPRO100State * s)
324
{
325
    s->scb_stat &= ~s->mem[SCBAck];
326
    s->mem[SCBAck] = s->scb_stat;
327
    if (s->scb_stat == 0) {
328
        disable_interrupt(s);
329
    }
330
}
331

    
332
static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
333
{
334
    uint8_t mask = ~s->mem[SCBIntmask];
335
    s->mem[SCBAck] |= stat;
336
    stat = s->scb_stat = s->mem[SCBAck];
337
    stat &= (mask | 0x0f);
338
    //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
339
    if (stat && (mask & 0x01)) {
340
        /* SCB mask and SCB Bit M do not disable interrupt. */
341
        enable_interrupt(s);
342
    } else if (s->int_stat) {
343
        disable_interrupt(s);
344
    }
345
}
346

    
347
static void eepro100_cx_interrupt(EEPRO100State * s)
348
{
349
    /* CU completed action command. */
350
    /* Transmit not ok (82557 only, not in emulation). */
351
    eepro100_interrupt(s, 0x80);
352
}
353

    
354
static void eepro100_cna_interrupt(EEPRO100State * s)
355
{
356
    /* CU left the active state. */
357
    eepro100_interrupt(s, 0x20);
358
}
359

    
360
static void eepro100_fr_interrupt(EEPRO100State * s)
361
{
362
    /* RU received a complete frame. */
363
    eepro100_interrupt(s, 0x40);
364
}
365

    
366
#if 0
367
static void eepro100_rnr_interrupt(EEPRO100State * s)
368
{
369
    /* RU is not ready. */
370
    eepro100_interrupt(s, 0x10);
371
}
372
#endif
373

    
374
static void eepro100_mdi_interrupt(EEPRO100State * s)
375
{
376
    /* MDI completed read or write cycle. */
377
    eepro100_interrupt(s, 0x08);
378
}
379

    
380
static void eepro100_swi_interrupt(EEPRO100State * s)
381
{
382
    /* Software has requested an interrupt. */
383
    eepro100_interrupt(s, 0x04);
384
}
385

    
386
#if 0
387
static void eepro100_fcp_interrupt(EEPRO100State * s)
388
{
389
    /* Flow control pause interrupt (82558 and later). */
390
    eepro100_interrupt(s, 0x01);
391
}
392
#endif
393

    
394
static void pci_reset(EEPRO100State * s)
395
{
396
    uint32_t device = s->device;
397
    uint8_t *pci_conf = s->dev.config;
398

    
399
    TRACE(OTHER, logout("%p\n", s));
400

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

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

    
484
    if (device == i82557C || device == i82558B || device == i82559C) {
485
        logout("Get device id and revision from EEPROM!!!\n");
486
    }
487
}
488

    
489
static void nic_selective_reset(EEPRO100State * s)
490
{
491
    size_t i;
492
    uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
493
    //~ eeprom93xx_reset(s->eeprom);
494
    memcpy(eeprom_contents, s->macaddr, 6);
495
    eeprom_contents[0xa] = 0x4000;
496
    uint16_t sum = 0;
497
    for (i = 0; i < EEPROM_SIZE - 1; i++) {
498
        sum += eeprom_contents[i];
499
    }
500
    eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
501
    TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
502

    
503
    memset(s->mem, 0, sizeof(s->mem));
504
    uint32_t val = BIT(21);
505
    memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
506

    
507
    assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
508
    memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
509
}
510

    
511
static void nic_reset(void *opaque)
512
{
513
    EEPRO100State *s = opaque;
514
    TRACE(OTHER, logout("%p\n", s));
515
    nic_selective_reset(s);
516
}
517

    
518
#if defined(DEBUG_EEPRO100)
519
static const char * const reg[PCI_IO_SIZE / 4] = {
520
    "Command/Status",
521
    "General Pointer",
522
    "Port",
523
    "EEPROM/Flash Control",
524
    "MDI Control",
525
    "Receive DMA Byte Count",
526
    "Flow control",
527
    "General Status/Control"
528
};
529

    
530
static char *regname(uint32_t addr)
531
{
532
    static char buf[16];
533
    if (addr < PCI_IO_SIZE) {
534
        const char *r = reg[addr / 4];
535
        if (r != 0) {
536
            snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
537
        } else {
538
            snprintf(buf, sizeof(buf), "0x%02x", addr);
539
        }
540
    } else {
541
        snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
542
    }
543
    return buf;
544
}
545
#endif                          /* DEBUG_EEPRO100 */
546

    
547
#if 0
548
static uint16_t eepro100_read_status(EEPRO100State * s)
549
{
550
    uint16_t val = s->status;
551
    TRACE(OTHER, logout("val=0x%04x\n", val));
552
    return val;
553
}
554

555
static void eepro100_write_status(EEPRO100State * s, uint16_t val)
556
{
557
    TRACE(OTHER, logout("val=0x%04x\n", val));
558
    s->status = val;
559
}
560
#endif
561

    
562
/*****************************************************************************
563
 *
564
 * Command emulation.
565
 *
566
 ****************************************************************************/
567

    
568
#if 0
569
static uint16_t eepro100_read_command(EEPRO100State * s)
570
{
571
    uint16_t val = 0xffff;
572
    //~ TRACE(OTHER, logout("val=0x%04x\n", val));
573
    return val;
574
}
575
#endif
576

    
577
static bool device_supports_eTxCB(EEPRO100State * s)
578
{
579
    return (s->device != i82557B && s->device != i82557C);
580
}
581

    
582
/* Commands that can be put in a command list entry. */
583
enum commands {
584
    CmdNOp = 0,
585
    CmdIASetup = 1,
586
    CmdConfigure = 2,
587
    CmdMulticastList = 3,
588
    CmdTx = 4,
589
    CmdTDR = 5,                 /* load microcode */
590
    CmdDump = 6,
591
    CmdDiagnose = 7,
592

    
593
    /* And some extra flags: */
594
    CmdSuspend = 0x4000,        /* Suspend after completion. */
595
    CmdIntr = 0x2000,           /* Interrupt after completion. */
596
    CmdTxFlex = 0x0008,         /* Use "Flexible mode" for CmdTx command. */
597
};
598

    
599
static cu_state_t get_cu_state(EEPRO100State * s)
600
{
601
    return ((s->mem[SCBStatus] >> 6) & 0x03);
602
}
603

    
604
static void set_cu_state(EEPRO100State * s, cu_state_t state)
605
{
606
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
607
}
608

    
609
static ru_state_t get_ru_state(EEPRO100State * s)
610
{
611
    return ((s->mem[SCBStatus] >> 2) & 0x0f);
612
}
613

    
614
static void set_ru_state(EEPRO100State * s, ru_state_t state)
615
{
616
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
617
}
618

    
619
static void dump_statistics(EEPRO100State * s)
620
{
621
    /* Dump statistical data. Most data is never changed by the emulation
622
     * and always 0, so we first just copy the whole block and then those
623
     * values which really matter.
624
     * Number of data should check configuration!!!
625
     */
626
    cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
627
    stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
628
    stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
629
    stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
630
    stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
631
    //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
632
    //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
633
    //~ missing("CU dump statistical counters");
634
}
635

    
636
static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
637
{
638
    eepro100_tx_t tx;
639
    uint32_t cb_address;
640
    switch (val) {
641
    case CU_NOP:
642
        /* No operation. */
643
        break;
644
    case CU_START:
645
        if (get_cu_state(s) != cu_idle) {
646
            /* Intel documentation says that CU must be idle for the CU
647
             * start command. Intel driver for Linux also starts the CU
648
             * from suspended state. */
649
            logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
650
            //~ assert(!"wrong CU state");
651
        }
652
        set_cu_state(s, cu_active);
653
        s->cu_offset = s->pointer;
654
      next_command:
655
        cb_address = s->cu_base + s->cu_offset;
656
        cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
657
        uint16_t status = le16_to_cpu(tx.status);
658
        uint16_t command = le16_to_cpu(tx.command);
659
        logout
660
            ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
661
             val, status, command, tx.link);
662
        bool bit_el = ((command & 0x8000) != 0);
663
        bool bit_s = ((command & 0x4000) != 0);
664
        bool bit_i = ((command & 0x2000) != 0);
665
        bool bit_nc = ((command & 0x0010) != 0);
666
        //~ bool bit_sf = ((command & 0x0008) != 0);
667
        uint16_t cmd = command & 0x0007;
668
        s->cu_offset = le32_to_cpu(tx.link);
669
        switch (cmd) {
670
        case CmdNOp:
671
            /* Do nothing. */
672
            break;
673
        case CmdIASetup:
674
            cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
675
            TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6)));
676
            break;
677
        case CmdConfigure:
678
            cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
679
                                     sizeof(s->configuration));
680
            TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
681
            break;
682
        case CmdMulticastList:
683
            //~ missing("multicast list");
684
            break;
685
        case CmdTx:
686
            (void)0;
687
            uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
688
            uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
689
            TRACE(RXTX, logout
690
                ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
691
                 tbd_array, tcb_bytes, tx.tbd_count));
692
            assert(!bit_nc);
693
            //~ assert(!bit_sf);
694
            assert(tcb_bytes <= 2600);
695
            /* Next assertion fails for local configuration. */
696
            //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
697
            if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
698
                logout
699
                    ("illegal values of TBD array address and TCB byte count!\n");
700
            }
701
            // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
702
            uint8_t buf[2600];
703
            uint16_t size = 0;
704
            uint32_t tbd_address = cb_address + 0x10;
705
            assert(tcb_bytes <= sizeof(buf));
706
            while (size < tcb_bytes) {
707
                uint32_t tx_buffer_address = ldl_phys(tbd_address);
708
                uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
709
                //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
710
                tbd_address += 8;
711
                TRACE(RXTX, logout
712
                    ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
713
                     tx_buffer_address, tx_buffer_size));
714
                tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
715
                cpu_physical_memory_read(tx_buffer_address, &buf[size],
716
                                         tx_buffer_size);
717
                size += tx_buffer_size;
718
            }
719
            if (tbd_array == 0xffffffff) {
720
                /* Simplified mode. Was already handled by code above. */
721
            } else {
722
                /* Flexible mode. */
723
                uint8_t tbd_count = 0;
724
                if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
725
                    /* Extended Flexible TCB. */
726
                    assert(tcb_bytes == 0);
727
                    for (; tbd_count < 2; tbd_count++) {
728
                        uint32_t tx_buffer_address = ldl_phys(tbd_address);
729
                        uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
730
                        uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
731
                        tbd_address += 8;
732
                        TRACE(RXTX, logout
733
                            ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
734
                             tx_buffer_address, tx_buffer_size));
735
                        tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
736
                        cpu_physical_memory_read(tx_buffer_address, &buf[size],
737
                                                 tx_buffer_size);
738
                        size += tx_buffer_size;
739
                        if (tx_buffer_el & 1) {
740
                            break;
741
                        }
742
                    }
743
                }
744
                tbd_address = tbd_array;
745
                for (; tbd_count < tx.tbd_count; tbd_count++) {
746
                    uint32_t tx_buffer_address = ldl_phys(tbd_address);
747
                    uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
748
                    uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
749
                    tbd_address += 8;
750
                    TRACE(RXTX, logout
751
                        ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
752
                         tx_buffer_address, tx_buffer_size));
753
                    tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
754
                    cpu_physical_memory_read(tx_buffer_address, &buf[size],
755
                                             tx_buffer_size);
756
                    size += tx_buffer_size;
757
                    if (tx_buffer_el & 1) {
758
                        break;
759
                    }
760
                }
761
            }
762
            TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
763
            qemu_send_packet(s->vc, buf, size);
764
            s->statistics.tx_good_frames++;
765
            /* Transmit with bad status would raise an CX/TNO interrupt.
766
             * (82557 only). Emulation never has bad status. */
767
            //~ eepro100_cx_interrupt(s);
768
            break;
769
        case CmdTDR:
770
            TRACE(OTHER, logout("load microcode\n"));
771
            /* Starting with offset 8, the command contains
772
             * 64 dwords microcode which we just ignore here. */
773
            break;
774
        default:
775
            missing("undefined command");
776
        }
777
        /* Write new status (success). */
778
        stw_phys(cb_address, status | 0x8000 | 0x2000);
779
        if (bit_i) {
780
            /* CU completed action. */
781
            eepro100_cx_interrupt(s);
782
        }
783
        if (bit_el) {
784
            /* CU becomes idle. Terminate command loop. */
785
            set_cu_state(s, cu_idle);
786
            eepro100_cna_interrupt(s);
787
        } else if (bit_s) {
788
            /* CU becomes suspended. */
789
            set_cu_state(s, cu_suspended);
790
            eepro100_cna_interrupt(s);
791
        } else {
792
            /* More entries in list. */
793
            TRACE(OTHER, logout("CU list with at least one more entry\n"));
794
            goto next_command;
795
        }
796
        TRACE(OTHER, logout("CU list empty\n"));
797
        /* List is empty. Now CU is idle or suspended. */
798
        break;
799
    case CU_RESUME:
800
        if (get_cu_state(s) != cu_suspended) {
801
            logout("bad CU resume from CU state %u\n", get_cu_state(s));
802
            /* Workaround for bad Linux eepro100 driver which resumes
803
             * from idle state. */
804
            //~ missing("cu resume");
805
            set_cu_state(s, cu_suspended);
806
        }
807
        if (get_cu_state(s) == cu_suspended) {
808
            TRACE(OTHER, logout("CU resuming\n"));
809
            set_cu_state(s, cu_active);
810
            goto next_command;
811
        }
812
        break;
813
    case CU_STATSADDR:
814
        /* Load dump counters address. */
815
        s->statsaddr = s->pointer;
816
        TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
817
        break;
818
    case CU_SHOWSTATS:
819
        /* Dump statistical counters. */
820
        TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
821
        dump_statistics(s);
822
        break;
823
    case CU_CMD_BASE:
824
        /* Load CU base. */
825
        TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
826
        s->cu_base = s->pointer;
827
        break;
828
    case CU_DUMPSTATS:
829
        /* Dump and reset statistical counters. */
830
        TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
831
        dump_statistics(s);
832
        memset(&s->statistics, 0, sizeof(s->statistics));
833
        break;
834
    case CU_SRESUME:
835
        /* CU static resume. */
836
        missing("CU static resume");
837
        break;
838
    default:
839
        missing("Undefined CU command");
840
    }
841
}
842

    
843
static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
844
{
845
    switch (val) {
846
    case RU_NOP:
847
        /* No operation. */
848
        break;
849
    case RX_START:
850
        /* RU start. */
851
        if (get_ru_state(s) != ru_idle) {
852
            logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
853
            //~ assert(!"wrong RU state");
854
        }
855
        set_ru_state(s, ru_ready);
856
        s->ru_offset = s->pointer;
857
        TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
858
        break;
859
    case RX_RESUME:
860
        /* Restart RU. */
861
        if (get_ru_state(s) != ru_suspended) {
862
            logout("RU state is %u, should be %u\n", get_ru_state(s),
863
                   ru_suspended);
864
            //~ assert(!"wrong RU state");
865
        }
866
        set_ru_state(s, ru_ready);
867
        break;
868
    case RX_ADDR_LOAD:
869
        /* Load RU base. */
870
        TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
871
        s->ru_base = s->pointer;
872
        break;
873
    default:
874
        logout("val=0x%02x (undefined RU command)\n", val);
875
        missing("Undefined SU command");
876
    }
877
}
878

    
879
static void eepro100_write_command(EEPRO100State * s, uint8_t val)
880
{
881
    eepro100_ru_command(s, val & 0x0f);
882
    eepro100_cu_command(s, val & 0xf0);
883
    if ((val) == 0) {
884
        TRACE(OTHER, logout("val=0x%02x\n", val));
885
    }
886
    /* Clear command byte after command was accepted. */
887
    s->mem[SCBCmd] = 0;
888
}
889

    
890
/*****************************************************************************
891
 *
892
 * EEPROM emulation.
893
 *
894
 ****************************************************************************/
895

    
896
#define EEPROM_CS       0x02
897
#define EEPROM_SK       0x01
898
#define EEPROM_DI       0x04
899
#define EEPROM_DO       0x08
900

    
901
static uint16_t eepro100_read_eeprom(EEPRO100State * s)
902
{
903
    uint16_t val;
904
    memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
905
    if (eeprom93xx_read(s->eeprom)) {
906
        val |= EEPROM_DO;
907
    } else {
908
        val &= ~EEPROM_DO;
909
    }
910
    TRACE(EEPROM, logout("val=0x%04x\n", val));
911
    return val;
912
}
913

    
914
static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
915
{
916
    TRACE(EEPROM, logout("val=0x%02x\n", val));
917

    
918
    /* mask unwriteable bits */
919
    //~ val = SET_MASKED(val, 0x31, eeprom->value);
920

    
921
    int eecs = ((val & EEPROM_CS) != 0);
922
    int eesk = ((val & EEPROM_SK) != 0);
923
    int eedi = ((val & EEPROM_DI) != 0);
924
    eeprom93xx_write(eeprom, eecs, eesk, eedi);
925
}
926

    
927
static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
928
{
929
    s->pointer = le32_to_cpu(val);
930
    TRACE(OTHER, logout("val=0x%08x\n", val));
931
}
932

    
933
/*****************************************************************************
934
 *
935
 * MDI emulation.
936
 *
937
 ****************************************************************************/
938

    
939
#if defined(DEBUG_EEPRO100)
940
static const char * const mdi_op_name[] = {
941
    "opcode 0",
942
    "write",
943
    "read",
944
    "opcode 3"
945
};
946

    
947
static const char * const mdi_reg_name[] = {
948
    "Control",
949
    "Status",
950
    "PHY Identification (Word 1)",
951
    "PHY Identification (Word 2)",
952
    "Auto-Negotiation Advertisement",
953
    "Auto-Negotiation Link Partner Ability",
954
    "Auto-Negotiation Expansion"
955
};
956

    
957
static const char *reg2name(uint8_t reg)
958
{
959
    static char buffer[10];
960
    const char *p = buffer;
961
    if (reg < ARRAY_SIZE(mdi_reg_name)) {
962
        p = mdi_reg_name[reg];
963
    } else {
964
        snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
965
    }
966
    return p;
967
}
968
#endif                          /* DEBUG_EEPRO100 */
969

    
970
static uint32_t eepro100_read_mdi(EEPRO100State * s)
971
{
972
    uint32_t val;
973
    memcpy(&val, &s->mem[0x10], sizeof(val));
974

    
975
#ifdef DEBUG_EEPRO100
976
    uint8_t raiseint = (val & BIT(29)) >> 29;
977
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
978
    uint8_t phy = (val & BITS(25, 21)) >> 21;
979
    uint8_t reg = (val & BITS(20, 16)) >> 16;
980
    uint16_t data = (val & BITS(15, 0));
981
#endif
982
    /* Emulation takes no time to finish MDI transaction. */
983
    val |= BIT(28);
984
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
985
                      val, raiseint, mdi_op_name[opcode], phy,
986
                      reg2name(reg), data));
987
    return val;
988
}
989

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

    
1083
/*****************************************************************************
1084
 *
1085
 * Port emulation.
1086
 *
1087
 ****************************************************************************/
1088

    
1089
#define PORT_SOFTWARE_RESET     0
1090
#define PORT_SELFTEST           1
1091
#define PORT_SELECTIVE_RESET    2
1092
#define PORT_DUMP               3
1093
#define PORT_SELECTION_MASK     3
1094

    
1095
typedef struct {
1096
    uint32_t st_sign;           /* Self Test Signature */
1097
    uint32_t st_result;         /* Self Test Results */
1098
} eepro100_selftest_t;
1099

    
1100
static uint32_t eepro100_read_port(EEPRO100State * s)
1101
{
1102
    return 0;
1103
}
1104

    
1105
static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1106
{
1107
    val = le32_to_cpu(val);
1108
    uint32_t address = (val & ~PORT_SELECTION_MASK);
1109
    uint8_t selection = (val & PORT_SELECTION_MASK);
1110
    switch (selection) {
1111
    case PORT_SOFTWARE_RESET:
1112
        nic_reset(s);
1113
        break;
1114
    case PORT_SELFTEST:
1115
        TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1116
        eepro100_selftest_t data;
1117
        cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1118
        data.st_sign = 0xffffffff;
1119
        data.st_result = 0;
1120
        cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1121
        break;
1122
    case PORT_SELECTIVE_RESET:
1123
        TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1124
        nic_selective_reset(s);
1125
        break;
1126
    default:
1127
        logout("val=0x%08x\n", val);
1128
        missing("unknown port selection");
1129
    }
1130
}
1131

    
1132
/*****************************************************************************
1133
 *
1134
 * General hardware emulation.
1135
 *
1136
 ****************************************************************************/
1137

    
1138
static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1139
{
1140
    uint8_t val;
1141
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1142
        memcpy(&val, &s->mem[addr], sizeof(val));
1143
    }
1144

    
1145
    switch (addr) {
1146
    case SCBStatus:
1147
        //~ val = eepro100_read_status(s);
1148
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1149
        break;
1150
    case SCBAck:
1151
        //~ val = eepro100_read_status(s);
1152
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1153
        break;
1154
    case SCBCmd:
1155
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1156
        //~ val = eepro100_read_command(s);
1157
        break;
1158
    case SCBIntmask:
1159
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1160
        break;
1161
    case SCBPort + 3:
1162
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1163
        break;
1164
    case SCBeeprom:
1165
        val = eepro100_read_eeprom(s);
1166
        break;
1167
    case 0x1b:                 /* PMDR (power management driver register) */
1168
        val = 0;
1169
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1170
        break;
1171
    case 0x1d:                 /* general status register */
1172
        /* 100 Mbps full duplex, valid link */
1173
        val = 0x07;
1174
        TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1175
        break;
1176
    default:
1177
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1178
        missing("unknown byte read");
1179
    }
1180
    return val;
1181
}
1182

    
1183
static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1184
{
1185
    uint16_t val;
1186
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1187
        memcpy(&val, &s->mem[addr], sizeof(val));
1188
    }
1189

    
1190
    switch (addr) {
1191
    case SCBStatus:
1192
        //~ val = eepro100_read_status(s);
1193
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1194
        break;
1195
    case SCBeeprom:
1196
        val = eepro100_read_eeprom(s);
1197
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1198
        break;
1199
    default:
1200
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1201
        missing("unknown word read");
1202
    }
1203
    return val;
1204
}
1205

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

    
1213
    switch (addr) {
1214
    case SCBStatus:
1215
        //~ val = eepro100_read_status(s);
1216
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1217
        break;
1218
    case SCBPointer:
1219
        //~ val = eepro100_read_pointer(s);
1220
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1221
        break;
1222
    case SCBPort:
1223
        val = eepro100_read_port(s);
1224
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1225
        break;
1226
    case SCBCtrlMDI:
1227
        val = eepro100_read_mdi(s);
1228
        break;
1229
    default:
1230
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1231
        missing("unknown longword read");
1232
    }
1233
    return val;
1234
}
1235

    
1236
static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1237
{
1238
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1239
        memcpy(&s->mem[addr], &val, sizeof(val));
1240
    }
1241

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

    
1244
    switch (addr) {
1245
    case SCBStatus:
1246
        //~ eepro100_write_status(s, val);
1247
        break;
1248
    case SCBAck:
1249
        eepro100_acknowledge(s);
1250
        break;
1251
    case SCBCmd:
1252
        eepro100_write_command(s, val);
1253
        break;
1254
    case SCBIntmask:
1255
        if (val & BIT(1)) {
1256
            eepro100_swi_interrupt(s);
1257
        }
1258
        eepro100_interrupt(s, 0);
1259
        break;
1260
    case SCBPort + 3:
1261
    case SCBFlow:       /* does not exist on 82557 */
1262
    case SCBFlow + 1:
1263
    case SCBFlow + 2:
1264
    case SCBFlow + 3:
1265
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1266
        break;
1267
    case SCBeeprom:
1268
        eepro100_write_eeprom(s->eeprom, val);
1269
        break;
1270
    default:
1271
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1272
        missing("unknown byte write");
1273
    }
1274
}
1275

    
1276
static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1277
{
1278
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1279
        memcpy(&s->mem[addr], &val, sizeof(val));
1280
    }
1281

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

    
1284
    switch (addr) {
1285
    case SCBStatus:
1286
        //~ eepro100_write_status(s, val);
1287
        eepro100_acknowledge(s);
1288
        break;
1289
    case SCBCmd:
1290
        eepro100_write_command(s, val);
1291
        eepro100_write1(s, SCBIntmask, val >> 8);
1292
        break;
1293
    case SCBeeprom:
1294
        eepro100_write_eeprom(s->eeprom, val);
1295
        break;
1296
    default:
1297
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1298
        missing("unknown word write");
1299
    }
1300
}
1301

    
1302
static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1303
{
1304
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1305
        memcpy(&s->mem[addr], &val, sizeof(val));
1306
    }
1307

    
1308
    switch (addr) {
1309
    case SCBPointer:
1310
        eepro100_write_pointer(s, val);
1311
        break;
1312
    case SCBPort:
1313
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1314
        eepro100_write_port(s, val);
1315
        break;
1316
    case SCBCtrlMDI:
1317
        eepro100_write_mdi(s, val);
1318
        break;
1319
    default:
1320
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1321
        missing("unknown longword write");
1322
    }
1323
}
1324

    
1325
/*****************************************************************************
1326
 *
1327
 * Port mapped I/O.
1328
 *
1329
 ****************************************************************************/
1330

    
1331
static uint32_t ioport_read1(void *opaque, uint32_t addr)
1332
{
1333
    EEPRO100State *s = opaque;
1334
    //~ logout("addr=%s\n", regname(addr));
1335
    return eepro100_read1(s, addr - s->region[1]);
1336
}
1337

    
1338
static uint32_t ioport_read2(void *opaque, uint32_t addr)
1339
{
1340
    EEPRO100State *s = opaque;
1341
    return eepro100_read2(s, addr - s->region[1]);
1342
}
1343

    
1344
static uint32_t ioport_read4(void *opaque, uint32_t addr)
1345
{
1346
    EEPRO100State *s = opaque;
1347
    return eepro100_read4(s, addr - s->region[1]);
1348
}
1349

    
1350
static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1351
{
1352
    EEPRO100State *s = opaque;
1353
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1354
    eepro100_write1(s, addr - s->region[1], val);
1355
}
1356

    
1357
static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1358
{
1359
    EEPRO100State *s = opaque;
1360
    eepro100_write2(s, addr - s->region[1], val);
1361
}
1362

    
1363
static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1364
{
1365
    EEPRO100State *s = opaque;
1366
    eepro100_write4(s, addr - s->region[1], val);
1367
}
1368

    
1369
/***********************************************************/
1370
/* PCI EEPRO100 definitions */
1371

    
1372
static void pci_map(PCIDevice * pci_dev, int region_num,
1373
                    uint32_t addr, uint32_t size, int type)
1374
{
1375
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1376

    
1377
    TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1378
          region_num, addr, size, type));
1379

    
1380
    assert(region_num == 1);
1381
    register_ioport_write(addr, size, 1, ioport_write1, s);
1382
    register_ioport_read(addr, size, 1, ioport_read1, s);
1383
    register_ioport_write(addr, size, 2, ioport_write2, s);
1384
    register_ioport_read(addr, size, 2, ioport_read2, s);
1385
    register_ioport_write(addr, size, 4, ioport_write4, s);
1386
    register_ioport_read(addr, size, 4, ioport_read4, s);
1387

    
1388
    s->region[region_num] = addr;
1389
}
1390

    
1391
/*****************************************************************************
1392
 *
1393
 * Memory mapped I/O.
1394
 *
1395
 ****************************************************************************/
1396

    
1397
static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1398
{
1399
    EEPRO100State *s = opaque;
1400
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1401
    eepro100_write1(s, addr, val);
1402
}
1403

    
1404
static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1405
{
1406
    EEPRO100State *s = opaque;
1407
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1408
    eepro100_write2(s, addr, val);
1409
}
1410

    
1411
static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1412
{
1413
    EEPRO100State *s = opaque;
1414
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1415
    eepro100_write4(s, addr, val);
1416
}
1417

    
1418
static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1419
{
1420
    EEPRO100State *s = opaque;
1421
    //~ logout("addr=%s\n", regname(addr));
1422
    return eepro100_read1(s, addr);
1423
}
1424

    
1425
static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1426
{
1427
    EEPRO100State *s = opaque;
1428
    //~ logout("addr=%s\n", regname(addr));
1429
    return eepro100_read2(s, addr);
1430
}
1431

    
1432
static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1433
{
1434
    EEPRO100State *s = opaque;
1435
    //~ logout("addr=%s\n", regname(addr));
1436
    return eepro100_read4(s, addr);
1437
}
1438

    
1439
static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1440
    pci_mmio_writeb,
1441
    pci_mmio_writew,
1442
    pci_mmio_writel
1443
};
1444

    
1445
static CPUReadMemoryFunc * const pci_mmio_read[] = {
1446
    pci_mmio_readb,
1447
    pci_mmio_readw,
1448
    pci_mmio_readl
1449
};
1450

    
1451
static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1452
                         uint32_t addr, uint32_t size, int type)
1453
{
1454
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1455

    
1456
    TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1457
          region_num, addr, size, type));
1458

    
1459
    if (region_num == 0) {
1460
        /* Map control / status registers. */
1461
        cpu_register_physical_memory(addr, size, s->mmio_index);
1462
        s->region[region_num] = addr;
1463
    }
1464
}
1465

    
1466
static int nic_can_receive(VLANClientState *vc)
1467
{
1468
    EEPRO100State *s = vc->opaque;
1469
    TRACE(RXTX, logout("%p\n", s));
1470
    return get_ru_state(s) == ru_ready;
1471
    //~ return !eepro100_buffer_full(s);
1472
}
1473

    
1474
static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
1475
{
1476
    /* TODO:
1477
     * - Magic packets should set bit 30 in power management driver register.
1478
     * - Interesting packets should set bit 29 in power management driver register.
1479
     */
1480
    EEPRO100State *s = vc->opaque;
1481
    uint16_t rfd_status = 0xa000;
1482
    static const uint8_t broadcast_macaddr[6] =
1483
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1484

    
1485
    /* TODO: check multiple IA bit. */
1486
    assert(!(s->configuration[20] & BIT(6)));
1487

    
1488
    if (s->configuration[8] & 0x80) {
1489
        /* CSMA is disabled. */
1490
        logout("%p received while CSMA is disabled\n", s);
1491
        return -1;
1492
    } else if (size < 64 && (s->configuration[7] & 1)) {
1493
        /* Short frame and configuration byte 7/0 (discard short receive) set:
1494
         * Short frame is discarded */
1495
        logout("%p received short frame (%zu byte)\n", s, size);
1496
        s->statistics.rx_short_frame_errors++;
1497
        //~ return -1;
1498
    } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
1499
        /* Long frame and configuration byte 18/3 (long receive ok) not set:
1500
         * Long frames are discarded. */
1501
        logout("%p received long frame (%zu byte), ignored\n", s, size);
1502
        return -1;
1503
    } else if (memcmp(buf, s->macaddr, 6) == 0) {       // !!!
1504
        /* Frame matches individual address. */
1505
        /* TODO: check configuration byte 15/4 (ignore U/L). */
1506
        TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
1507
    } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1508
        /* Broadcast frame. */
1509
        TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
1510
        rfd_status |= 0x0002;
1511
    } else if (buf[0] & 0x01) { // !!!
1512
        /* Multicast frame. */
1513
        TRACE(RXTX, logout("%p received multicast, len=%zu\n", s, size));
1514
        /* TODO: check multicast all bit. */
1515
        assert(!(s->configuration[21] & BIT(3)));
1516
        int mcast_idx = compute_mcast_idx(buf);
1517
        if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
1518
            return size;
1519
        }
1520
        rfd_status |= 0x0002;
1521
    } else if (s->configuration[15] & 1) {
1522
        /* Promiscuous: receive all. */
1523
        TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
1524
        rfd_status |= 0x0004;
1525
    } else {
1526
        TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1527
              nic_dump(buf, size)));
1528
        return size;
1529
    }
1530

    
1531
    if (get_ru_state(s) != ru_ready) {
1532
        /* No resources available. */
1533
        logout("no resources, state=%u\n", get_ru_state(s));
1534
        s->statistics.rx_resource_errors++;
1535
        //~ assert(!"no resources");
1536
        return -1;
1537
    }
1538
    //~ !!!
1539
//~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1540
    eepro100_rx_t rx;
1541
    cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1542
                             offsetof(eepro100_rx_t, packet));
1543
    uint16_t rfd_command = le16_to_cpu(rx.command);
1544
    uint16_t rfd_size = le16_to_cpu(rx.size);
1545
    assert(size <= rfd_size);
1546
    if (size < 64) {
1547
        rfd_status |= 0x0080;
1548
    }
1549
    TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1550
          rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
1551
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1552
             rfd_status);
1553
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1554
    /* Early receive interrupt not supported. */
1555
    //~ eepro100_er_interrupt(s);
1556
    /* Receive CRC Transfer not supported. */
1557
    assert(!(s->configuration[18] & 4));
1558
    /* TODO: check stripping enable bit. */
1559
    //~ assert(!(s->configuration[17] & 1));
1560
    cpu_physical_memory_write(s->ru_base + s->ru_offset +
1561
                              offsetof(eepro100_rx_t, packet), buf, size);
1562
    s->statistics.rx_good_frames++;
1563
    eepro100_fr_interrupt(s);
1564
    s->ru_offset = le32_to_cpu(rx.link);
1565
    if (rfd_command & 0x8000) {
1566
        /* EL bit is set, so this was the last frame. */
1567
        assert(0);
1568
    }
1569
    if (rfd_command & 0x4000) {
1570
        /* S bit is set. */
1571
        set_ru_state(s, ru_suspended);
1572
    }
1573
    return size;
1574
}
1575

    
1576
static int nic_load(QEMUFile * f, void *opaque, int version_id)
1577
{
1578
    EEPRO100State *s = opaque;
1579
    int i;
1580
    int ret;
1581

    
1582
    if (version_id != eepro100_version) {
1583
        return -EINVAL;
1584
    }
1585

    
1586
    ret = pci_device_load(&s->dev, f);
1587
    if (ret < 0) {
1588
        return ret;
1589
    }
1590

    
1591
    qemu_get_8s(f, &s->cmd);
1592
    qemu_get_be32s(f, &s->start);
1593
    qemu_get_be32s(f, &s->stop);
1594
    qemu_get_buffer(f, s->mult, 8);
1595
    qemu_get_buffer(f, s->mem, sizeof(s->mem));
1596

    
1597
    /* Restore all members of struct between scv_stat and mem. */
1598
    qemu_get_8s(f, &s->scb_stat);
1599
    qemu_get_8s(f, &s->int_stat);
1600
    for (i = 0; i < ARRAY_SIZE(s->region); i++) {
1601
        qemu_get_be32s(f, &s->region[i]);
1602
    }
1603
    qemu_get_buffer(f, s->macaddr, 6);
1604
    for (i = 0; i < ARRAY_SIZE(s->mdimem); i++) {
1605
        qemu_get_be16s(f, &s->mdimem[i]);
1606
    }
1607
    /* The eeprom should be saved and restored by its own routines. */
1608
    qemu_get_be32s(f, &s->device);
1609
    qemu_get_be32s(f, &s->pointer);
1610
    qemu_get_be32s(f, &s->cu_base);
1611
    qemu_get_be32s(f, &s->cu_offset);
1612
    qemu_get_be32s(f, &s->ru_base);
1613
    qemu_get_be32s(f, &s->ru_offset);
1614
    qemu_get_be32s(f, &s->statsaddr);
1615
    /* Restore epro100_stats_t statistics. */
1616
    qemu_get_be32s(f, &s->statistics.tx_good_frames);
1617
    qemu_get_be32s(f, &s->statistics.tx_max_collisions);
1618
    qemu_get_be32s(f, &s->statistics.tx_late_collisions);
1619
    qemu_get_be32s(f, &s->statistics.tx_underruns);
1620
    qemu_get_be32s(f, &s->statistics.tx_lost_crs);
1621
    qemu_get_be32s(f, &s->statistics.tx_deferred);
1622
    qemu_get_be32s(f, &s->statistics.tx_single_collisions);
1623
    qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
1624
    qemu_get_be32s(f, &s->statistics.tx_total_collisions);
1625
    qemu_get_be32s(f, &s->statistics.rx_good_frames);
1626
    qemu_get_be32s(f, &s->statistics.rx_crc_errors);
1627
    qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
1628
    qemu_get_be32s(f, &s->statistics.rx_resource_errors);
1629
    qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
1630
    qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
1631
    qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
1632
    qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
1633
    qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
1634
    qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
1635
    qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
1636
    qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
1637
    qemu_get_be32s(f, &s->statistics.complete);
1638
#if 0
1639
    qemu_get_be16s(f, &s->status);
1640
#endif
1641

    
1642
    /* Configuration bytes. */
1643
    qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
1644

    
1645
    return 0;
1646
}
1647

    
1648
static void nic_save(QEMUFile * f, void *opaque)
1649
{
1650
    EEPRO100State *s = opaque;
1651
    int i;
1652

    
1653
    pci_device_save(&s->dev, f);
1654

    
1655
    qemu_put_8s(f, &s->cmd);
1656
    qemu_put_be32s(f, &s->start);
1657
    qemu_put_be32s(f, &s->stop);
1658
    qemu_put_buffer(f, s->mult, 8);
1659
    qemu_put_buffer(f, s->mem, sizeof(s->mem));
1660

    
1661
    /* Save all members of struct between scv_stat and mem. */
1662
    qemu_put_8s(f, &s->scb_stat);
1663
    qemu_put_8s(f, &s->int_stat);
1664
    for (i = 0; i < ARRAY_SIZE(s->region); i++) {
1665
        qemu_put_be32s(f, &s->region[i]);
1666
    }
1667
    qemu_put_buffer(f, s->macaddr, 6);
1668
    for (i = 0; i < ARRAY_SIZE(s->mdimem); i++) {
1669
        qemu_put_be16s(f, &s->mdimem[i]);
1670
    }
1671
    /* The eeprom should be saved and restored by its own routines. */
1672
    qemu_put_be32s(f, &s->device);
1673
    qemu_put_be32s(f, &s->pointer);
1674
    qemu_put_be32s(f, &s->cu_base);
1675
    qemu_put_be32s(f, &s->cu_offset);
1676
    qemu_put_be32s(f, &s->ru_base);
1677
    qemu_put_be32s(f, &s->ru_offset);
1678
    qemu_put_be32s(f, &s->statsaddr);
1679
    /* Save epro100_stats_t statistics. */
1680
    qemu_put_be32s(f, &s->statistics.tx_good_frames);
1681
    qemu_put_be32s(f, &s->statistics.tx_max_collisions);
1682
    qemu_put_be32s(f, &s->statistics.tx_late_collisions);
1683
    qemu_put_be32s(f, &s->statistics.tx_underruns);
1684
    qemu_put_be32s(f, &s->statistics.tx_lost_crs);
1685
    qemu_put_be32s(f, &s->statistics.tx_deferred);
1686
    qemu_put_be32s(f, &s->statistics.tx_single_collisions);
1687
    qemu_put_be32s(f, &s->statistics.tx_multiple_collisions);
1688
    qemu_put_be32s(f, &s->statistics.tx_total_collisions);
1689
    qemu_put_be32s(f, &s->statistics.rx_good_frames);
1690
    qemu_put_be32s(f, &s->statistics.rx_crc_errors);
1691
    qemu_put_be32s(f, &s->statistics.rx_alignment_errors);
1692
    qemu_put_be32s(f, &s->statistics.rx_resource_errors);
1693
    qemu_put_be32s(f, &s->statistics.rx_overrun_errors);
1694
    qemu_put_be32s(f, &s->statistics.rx_cdt_errors);
1695
    qemu_put_be32s(f, &s->statistics.rx_short_frame_errors);
1696
    qemu_put_be32s(f, &s->statistics.fc_xmt_pause);
1697
    qemu_put_be32s(f, &s->statistics.fc_rcv_pause);
1698
    qemu_put_be32s(f, &s->statistics.fc_rcv_unsupported);
1699
    qemu_put_be16s(f, &s->statistics.xmt_tco_frames);
1700
    qemu_put_be16s(f, &s->statistics.rcv_tco_frames);
1701
    qemu_put_be32s(f, &s->statistics.complete);
1702
#if 0
1703
    qemu_put_be16s(f, &s->status);
1704
#endif
1705

    
1706
    /* Configuration bytes. */
1707
    qemu_put_buffer(f, s->configuration, sizeof(s->configuration));
1708
}
1709

    
1710
static void nic_cleanup(VLANClientState *vc)
1711
{
1712
    EEPRO100State *s = vc->opaque;
1713

    
1714
    unregister_savevm(vc->model, s);
1715

    
1716
    eeprom93xx_free(s->eeprom);
1717
}
1718

    
1719
static int pci_nic_uninit(PCIDevice *pci_dev)
1720
{
1721
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1722

    
1723
    cpu_unregister_io_memory(s->mmio_index);
1724

    
1725
    return 0;
1726
}
1727

    
1728
static int nic_init(PCIDevice *pci_dev, uint32_t device)
1729
{
1730
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1731

    
1732
    TRACE(OTHER, logout("\n"));
1733

    
1734
    s->dev.unregister = pci_nic_uninit;
1735

    
1736
    s->device = device;
1737

    
1738
    pci_reset(s);
1739

    
1740
    /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1741
     * i82559 and later support 64 or 256 word EEPROM. */
1742
    s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1743

    
1744
    /* Handler for memory-mapped I/O */
1745
    s->mmio_index =
1746
        cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1747

    
1748
    pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1749
                           PCI_ADDRESS_SPACE_MEM |
1750
                           PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
1751
    pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
1752
                           pci_map);
1753
    pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
1754
                           pci_mmio_map);
1755

    
1756
    qdev_get_macaddr(&s->dev.qdev, s->macaddr);
1757
    logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1758
    assert(s->region[1] == 0);
1759

    
1760
    nic_reset(s);
1761

    
1762
    s->vc = qdev_get_vlan_client(&s->dev.qdev,
1763
                                 nic_can_receive, nic_receive, NULL,
1764
                                 nic_cleanup, s);
1765

    
1766
    qemu_format_nic_info_str(s->vc, s->macaddr);
1767
    TRACE(OTHER, logout("%s\n", s->vc->info_str));
1768

    
1769
    qemu_register_reset(nic_reset, s);
1770

    
1771
    register_savevm(s->vc->model, eepro100_instance, eepro100_version,
1772
                    nic_save, nic_load, s);
1773
    return 0;
1774
}
1775

    
1776
static int pci_i82550_init(PCIDevice *pci_dev)
1777
{
1778
    return nic_init(pci_dev, i82550);
1779
}
1780

    
1781
static int pci_i82551_init(PCIDevice *pci_dev)
1782
{
1783
    return nic_init(pci_dev, i82551);
1784
}
1785

    
1786
static int pci_i82557a_init(PCIDevice *pci_dev)
1787
{
1788
    return nic_init(pci_dev, i82557A);
1789
}
1790

    
1791
static int pci_i82557b_init(PCIDevice *pci_dev)
1792
{
1793
    return nic_init(pci_dev, i82557B);
1794
}
1795

    
1796
static int pci_i82557c_init(PCIDevice *pci_dev)
1797
{
1798
    return nic_init(pci_dev, i82557C);
1799
}
1800

    
1801
static int pci_i82558a_init(PCIDevice *pci_dev)
1802
{
1803
    return nic_init(pci_dev, i82558A);
1804
}
1805

    
1806
static int pci_i82558b_init(PCIDevice *pci_dev)
1807
{
1808
    return nic_init(pci_dev, i82558B);
1809
}
1810

    
1811
static int pci_i82559a_init(PCIDevice *pci_dev)
1812
{
1813
    return nic_init(pci_dev, i82559A);
1814
}
1815

    
1816
static int pci_i82559b_init(PCIDevice *pci_dev)
1817
{
1818
    return nic_init(pci_dev, i82559B);
1819
}
1820

    
1821
static int pci_i82559c_init(PCIDevice *pci_dev)
1822
{
1823
    return nic_init(pci_dev, i82559C);
1824
}
1825

    
1826
static int pci_i82559er_init(PCIDevice *pci_dev)
1827
{
1828
    return nic_init(pci_dev, i82559ER);
1829
}
1830

    
1831
static int pci_i82562_init(PCIDevice *pci_dev)
1832
{
1833
    return nic_init(pci_dev, i82562);
1834
}
1835

    
1836
static PCIDeviceInfo eepro100_info[] = {
1837
    {
1838
        .qdev.name = "i82550",
1839
        .qdev.size = sizeof(EEPRO100State),
1840
        .init      = pci_i82550_init,
1841
    },{
1842
        .qdev.name = "i82551",
1843
        .qdev.size = sizeof(EEPRO100State),
1844
        .init      = pci_i82551_init,
1845
    },{
1846
        .qdev.name = "i82557a",
1847
        .qdev.size = sizeof(EEPRO100State),
1848
        .init      = pci_i82557a_init,
1849
    },{
1850
        .qdev.name = "i82557b",
1851
        .qdev.size = sizeof(EEPRO100State),
1852
        .init      = pci_i82557b_init,
1853
    },{
1854
        .qdev.name = "i82557c",
1855
        .qdev.size = sizeof(EEPRO100State),
1856
        .init      = pci_i82557c_init,
1857
    },{
1858
        .qdev.name = "i82558a",
1859
        .qdev.size = sizeof(EEPRO100State),
1860
        .init      = pci_i82558a_init,
1861
    },{
1862
        .qdev.name = "i82558b",
1863
        .qdev.size = sizeof(EEPRO100State),
1864
        .init      = pci_i82558b_init,
1865
    },{
1866
        .qdev.name = "i82559a",
1867
        .qdev.size = sizeof(EEPRO100State),
1868
        .init      = pci_i82559a_init,
1869
    },{
1870
        .qdev.name = "i82559b",
1871
        .qdev.size = sizeof(EEPRO100State),
1872
        .init      = pci_i82559b_init,
1873
    },{
1874
        .qdev.name = "i82559c",
1875
        .qdev.size = sizeof(EEPRO100State),
1876
        .init      = pci_i82559c_init,
1877
    },{
1878
        .qdev.name = "i82559er",
1879
        .qdev.size = sizeof(EEPRO100State),
1880
        .init      = pci_i82559er_init,
1881
    },{
1882
        .qdev.name = "i82562",
1883
        .qdev.size = sizeof(EEPRO100State),
1884
        .init      = pci_i82562_init,
1885
    },{
1886
        /* end of list */
1887
    }
1888
};
1889

    
1890
static void eepro100_register_devices(void)
1891
{
1892
    pci_qdev_register_many(eepro100_info);
1893
}
1894

    
1895
device_init(eepro100_register_devices)