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

#include <stddef.h>             /* offsetof */
37
#include <stdbool.h>
P
pbrook 已提交
38 39 40
#include "hw.h"
#include "pci.h"
#include "net.h"
T
ths 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
#include "eeprom93xx.h"

/* Common declarations for all PCI devices. */

#define PCI_CONFIG_8(offset, value) \
    (pci_conf[offset] = (value))
#define PCI_CONFIG_16(offset, value) \
    (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
#define PCI_CONFIG_32(offset, value) \
    (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))

#define KiB 1024

54
/* Debug EEPRO100 card. */
55 56 57
#if 0
# define DEBUG_EEPRO100
#endif
T
ths 已提交
58 59

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

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

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

74
#define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
T
ths 已提交
75 76 77 78

#define MAX_ETH_FRAME_SIZE 1514

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

92
/* Use 64 word EEPROM. TODO: could be a runtime option. */
T
ths 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#define EEPROM_SIZE     64

#define PCI_MEM_SIZE            (4 * KiB)
#define PCI_IO_SIZE             64
#define PCI_FLASH_SIZE          (128 * KiB)

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

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

#define  RU_NOP         0x0000
#define  RX_START       0x0001
#define  RX_RESUME      0x0002
#define  RX_ABORT       0x0004
#define  RX_ADDR_LOAD   0x0006
#define  RX_RESUMENR    0x0007
#define INT_MASK        0x0100
#define DRVR_INT        0x0200  /* Driver generated interrupt. */

/* Offsets to the various registers.
   All accesses need not be longword aligned. */
enum speedo_offsets {
124
    SCBStatus = 0,              /* Status Word. */
T
ths 已提交
125 126 127 128 129
    SCBAck = 1,
    SCBCmd = 2,                 /* Rx/Command Unit command and status. */
    SCBIntmask = 3,
    SCBPointer = 4,             /* General purpose pointer. */
    SCBPort = 8,                /* Misc. commands and operands.  */
130 131
    SCBflash = 12,              /* Flash memory control. */
    SCBeeprom = 14,             /* EEPROM control. */
T
ths 已提交
132 133
    SCBCtrlMDI = 16,            /* MDI interface control. */
    SCBEarlyRx = 20,            /* Early receive byte count. */
134 135 136 137
    SCBFlow = 24,               /* Flow Control. */
    SCBpmdr = 27,               /* Power Management Driver. */
    SCBgctrl = 28,              /* General Control. */
    SCBgstat = 29,              /* General Status. */
T
ths 已提交
138 139 140 141 142 143 144
};

/* A speedo3 transmit buffer descriptor with two buffers... */
typedef struct {
    uint16_t status;
    uint16_t command;
    uint32_t link;              /* void * */
S
Stefan Weil 已提交
145
    uint32_t tbd_array_addr;    /* transmit buffer descriptor array address. */
T
ths 已提交
146 147 148 149 150 151 152 153
    uint16_t tcb_bytes;         /* transmit command block byte count (in lower 14 bits */
    uint8_t tx_threshold;       /* transmit threshold */
    uint8_t tbd_count;          /* TBD number */
    //~ /* This constitutes two "TBD" entries: hdr and data */
    //~ uint32_t tx_buf_addr0;  /* void *, header of frame to be transmitted.  */
    //~ int32_t  tx_buf_size0;  /* Length of Tx hdr. */
    //~ uint32_t tx_buf_addr1;  /* void *, data to be transmitted.  */
    //~ int32_t  tx_buf_size1;  /* Length of Tx data. */
A
Anthony Liguori 已提交
154
} eepro100_tx_t;
T
ths 已提交
155 156 157 158 159 160 161 162 163 164

/* Receive frame descriptor. */
typedef struct {
    int16_t status;
    uint16_t command;
    uint32_t link;              /* struct RxFD * */
    uint32_t rx_buf_addr;       /* void * */
    uint16_t count;
    uint16_t size;
    char packet[MAX_ETH_FRAME_SIZE + 4];
A
Anthony Liguori 已提交
165
} eepro100_rx_t;
T
ths 已提交
166 167 168 169 170 171 172 173 174 175

typedef struct {
    uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
        tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
        tx_multiple_collisions, tx_total_collisions;
    uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
        rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
        rx_short_frame_errors;
    uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
    uint16_t xmt_tco_frames, rcv_tco_frames;
176 177
    /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
    uint32_t reserved[4];
A
Anthony Liguori 已提交
178
} eepro100_stats_t;
T
ths 已提交
179 180 181 182 183 184 185

typedef enum {
    cu_idle = 0,
    cu_suspended = 1,
    cu_active = 2,
    cu_lpq_active = 2,
    cu_hqp_active = 3
A
Anthony Liguori 已提交
186
} cu_state_t;
T
ths 已提交
187 188 189 190 191 192

typedef enum {
    ru_idle = 0,
    ru_suspended = 1,
    ru_no_resources = 2,
    ru_ready = 4
A
Anthony Liguori 已提交
193
} ru_state_t;
T
ths 已提交
194 195

typedef struct {
196
    PCIDevice dev;
T
ths 已提交
197 198
    uint8_t mult[8];            /* multicast mask array */
    int mmio_index;
199
    NICState *nic;
200
    NICConf conf;
T
ths 已提交
201 202
    uint8_t scb_stat;           /* SCB stat/ack byte */
    uint8_t int_stat;           /* PCI interrupt status */
203
    /* region must not be saved by nic_save. */
T
ths 已提交
204 205
    uint32_t region[3];         /* PCI region addresses */
    uint16_t mdimem[32];
A
Anthony Liguori 已提交
206
    eeprom_t *eeprom;
T
ths 已提交
207 208 209 210 211 212 213 214
    uint32_t device;            /* device variant */
    uint32_t pointer;
    /* (cu_base + cu_offset) address the next command block in the command block list. */
    uint32_t cu_base;           /* CU base address */
    uint32_t cu_offset;         /* CU address offset */
    /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
    uint32_t ru_base;           /* RU base address */
    uint32_t ru_offset;         /* RU address offset */
A
Anthony Liguori 已提交
215
    uint32_t statsaddr;         /* pointer to eepro100_stats_t */
216

217 218 219 220 221
    /* Temporary status information (no need to save these values),
     * used while processing CU commands. */
    eepro100_tx_t tx;           /* transmit buffer descriptor */
    uint32_t cb_address;        /* = cu_base + cu_offset */

222 223 224
    /* Statistical counters. Also used for wake-up packet (i82559). */
    eepro100_stats_t statistics;

T
ths 已提交
225 226 227 228 229 230 231 232 233
#if 0
    uint16_t status;
#endif

    /* Configuration bytes. */
    uint8_t configuration[22];

    /* Data in mem is always in the byte order of the controller (le). */
    uint8_t mem[PCI_MEM_SIZE];
J
Juan Quintela 已提交
234 235
    /* vmstate for each particular nic */
    VMStateDescription *vmstate;
236 237 238 239

    /* Quasi static device properties (no need to save them). */
    uint16_t stats_size;
    bool has_extended_tcb_support;
T
ths 已提交
240 241
} EEPRO100State;

S
Stefan Weil 已提交
242 243 244 245 246 247 248 249 250 251 252
/* Word indices in EEPROM. */
typedef enum {
    EEPROM_CNFG_MDIX  = 0x03,
    EEPROM_ID         = 0x05,
    EEPROM_PHY_ID     = 0x06,
    EEPROM_VENDOR_ID  = 0x0c,
    EEPROM_CONFIG_ASF = 0x0d,
    EEPROM_DEVICE_ID  = 0x23,
    EEPROM_SMBUS_ADDR = 0x90,
} EEPROMOffset;

T
ths 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/* Default values for MDI (PHY) registers */
static const uint16_t eepro100_mdi_default[] = {
    /* MDI Registers 0 - 6, 7 */
    0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
    /* MDI Registers 8 - 15 */
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
    /* MDI Registers 16 - 31 */
    0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
};

/* Readonly mask for MDI (PHY) registers */
static const uint16_t eepro100_mdi_mask[] = {
    0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
    0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
    0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
};

272 273 274 275 276 277 278
/* XXX: optimize */
static void stl_le_phys(target_phys_addr_t addr, uint32_t val)
{
    val = cpu_to_le32(val);
    cpu_physical_memory_write(addr, (const uint8_t *)&val, sizeof(val));
}

T
ths 已提交
279 280 281 282
#define POLYNOMIAL 0x04c11db6

/* From FreeBSD */
/* XXX: optimize */
S
Stefan Weil 已提交
283
static unsigned compute_mcast_idx(const uint8_t * ep)
T
ths 已提交
284 285 286 287 288 289 290 291 292 293 294 295
{
    uint32_t crc;
    int carry, i, j;
    uint8_t b;

    crc = 0xffffffff;
    for (i = 0; i < 6; i++) {
        b = *ep++;
        for (j = 0; j < 8; j++) {
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
            crc <<= 1;
            b >>= 1;
296
            if (carry) {
T
ths 已提交
297
                crc = ((crc ^ POLYNOMIAL) | carry);
298
            }
T
ths 已提交
299 300
        }
    }
S
Stefan Weil 已提交
301
    return (crc & BITS(7, 2)) >> 2;
T
ths 已提交
302 303 304 305 306 307 308
}

#if defined(DEBUG_EEPRO100)
static const char *nic_dump(const uint8_t * buf, unsigned size)
{
    static char dump[3 * 16 + 1];
    char *p = &dump[0];
309
    if (size > 16) {
T
ths 已提交
310
        size = 16;
311
    }
T
ths 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    while (size-- > 0) {
        p += sprintf(p, " %02x", *buf++);
    }
    return dump;
}
#endif                          /* DEBUG_EEPRO100 */

enum scb_stat_ack {
    stat_ack_not_ours = 0x00,
    stat_ack_sw_gen = 0x04,
    stat_ack_rnr = 0x10,
    stat_ack_cu_idle = 0x20,
    stat_ack_frame_rx = 0x40,
    stat_ack_cu_cmd_done = 0x80,
    stat_ack_not_present = 0xFF,
    stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
    stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
};

static void disable_interrupt(EEPRO100State * s)
{
    if (s->int_stat) {
334
        TRACE(INT, logout("interrupt disabled\n"));
335
        qemu_irq_lower(s->dev.irq[0]);
T
ths 已提交
336 337 338 339 340 341 342
        s->int_stat = 0;
    }
}

static void enable_interrupt(EEPRO100State * s)
{
    if (!s->int_stat) {
343
        TRACE(INT, logout("interrupt enabled\n"));
344
        qemu_irq_raise(s->dev.irq[0]);
T
ths 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        s->int_stat = 1;
    }
}

static void eepro100_acknowledge(EEPRO100State * s)
{
    s->scb_stat &= ~s->mem[SCBAck];
    s->mem[SCBAck] = s->scb_stat;
    if (s->scb_stat == 0) {
        disable_interrupt(s);
    }
}

static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
{
    uint8_t mask = ~s->mem[SCBIntmask];
    s->mem[SCBAck] |= stat;
    stat = s->scb_stat = s->mem[SCBAck];
    stat &= (mask | 0x0f);
    //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
    if (stat && (mask & 0x01)) {
        /* SCB mask and SCB Bit M do not disable interrupt. */
        enable_interrupt(s);
    } else if (s->int_stat) {
        disable_interrupt(s);
    }
}

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

static void eepro100_cna_interrupt(EEPRO100State * s)
{
    /* CU left the active state. */
    eepro100_interrupt(s, 0x20);
}

static void eepro100_fr_interrupt(EEPRO100State * s)
{
    /* RU received a complete frame. */
    eepro100_interrupt(s, 0x40);
}

#if 0
static void eepro100_rnr_interrupt(EEPRO100State * s)
{
    /* RU is not ready. */
    eepro100_interrupt(s, 0x10);
}
#endif

static void eepro100_mdi_interrupt(EEPRO100State * s)
{
    /* MDI completed read or write cycle. */
    eepro100_interrupt(s, 0x08);
}

static void eepro100_swi_interrupt(EEPRO100State * s)
{
    /* Software has requested an interrupt. */
    eepro100_interrupt(s, 0x04);
}

#if 0
static void eepro100_fcp_interrupt(EEPRO100State * s)
{
    /* Flow control pause interrupt (82558 and later). */
    eepro100_interrupt(s, 0x01);
}
#endif

static void pci_reset(EEPRO100State * s)
{
    uint32_t device = s->device;
423
    uint8_t *pci_conf = s->dev.config;
424
    bool power_management = 1;
T
ths 已提交
425

426
    TRACE(OTHER, logout("%p\n", s));
T
ths 已提交
427 428

    /* PCI Vendor ID */
429
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
430
    /* PCI Device ID depends on device and is set below. */
T
ths 已提交
431
    /* PCI Command */
432
    /* TODO: this is the default, do not override. */
T
ths 已提交
433 434
    PCI_CONFIG_16(PCI_COMMAND, 0x0000);
    /* PCI Status */
435
    /* TODO: Value at RST# should be 0. */
436
    PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM | PCI_STATUS_FAST_BACK);
T
ths 已提交
437 438
    /* PCI Revision ID */
    PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
439
    /* TODO: this is the default, do not override. */
T
ths 已提交
440
    /* PCI Class Code */
441
    PCI_CONFIG_8(PCI_CLASS_PROG, 0x00);
442
    pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
T
ths 已提交
443 444 445 446
    /* PCI Cache Line Size */
    /* check cache line size!!! */
    //~ PCI_CONFIG_8(0x0c, 0x00);
    /* PCI Latency Timer */
447
    PCI_CONFIG_8(PCI_LATENCY_TIMER, 0x20);   // latency timer = 32 clocks
T
ths 已提交
448 449 450 451
    /* PCI Header Type */
    /* BIST (built-in self test) */
#if defined(TARGET_I386)
// !!! workaround for buggy bios
452
//~ #define PCI_BASE_ADDRESS_MEM_PREFETCH 0
T
ths 已提交
453 454 455 456 457
#endif
#if 0
    /* PCI Base Address Registers */
    /* CSR Memory Mapped Base Address */
    PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
458 459
                  PCI_BASE_ADDRESS_SPACE_MEMORY |
                  PCI_BASE_ADDRESS_MEM_PREFETCH);
T
ths 已提交
460
    /* CSR I/O Mapped Base Address */
461
    PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_SPACE_IO);
T
ths 已提交
462 463
#if 0
    /* Flash Memory Mapped Base Address */
464 465
    PCI_CONFIG_32(PCI_BASE_ADDRESS_2,
                  0xfffe0000 | PCI_BASE_ADDRESS_SPACE_MEMORY);
T
ths 已提交
466 467 468
#endif
#endif
    /* Expansion ROM Base Address (depends on boot disable!!!) */
469 470
    /* TODO: not needed, set when BAR is registered */
    PCI_CONFIG_32(PCI_ROM_ADDRESS, PCI_BASE_ADDRESS_SPACE_MEMORY);
T
ths 已提交
471
    /* Capability Pointer */
472 473 474
    /* TODO: revisions with power_management 1 use this but
     * do not set new capability list bit in status register. */
    PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0xdc);
475
    /* Interrupt Line */
T
ths 已提交
476
    /* Interrupt Pin */
477 478
    /* TODO: RST# value should be 0 */
    PCI_CONFIG_8(PCI_INTERRUPT_PIN, 1);      // interrupt pin 0
T
ths 已提交
479
    /* Minimum Grant */
480
    PCI_CONFIG_8(PCI_MIN_GNT, 0x08);
T
ths 已提交
481
    /* Maximum Latency */
482
    PCI_CONFIG_8(PCI_MAX_LAT, 0x18);
T
ths 已提交
483 484

    switch (device) {
485 486 487 488 489 490 491 492 493 494
    case i82550:
        // TODO: check device id.
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
        /* Revision ID: 0x0c, 0x0d, 0x0e. */
        PCI_CONFIG_8(PCI_REVISION_ID, 0x0e);
        // TODO: check size of statistical counters.
        s->stats_size = 80;
        // TODO: check extended tcb support.
        s->has_extended_tcb_support = 1;
        break;
T
ths 已提交
495
    case i82551:
496
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
497
        /* Revision ID: 0x0f, 0x10. */
T
ths 已提交
498
        PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
499 500 501 502 503 504 505
        // TODO: check size of statistical counters.
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
        break;
    case i82557A:
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
        PCI_CONFIG_8(PCI_REVISION_ID, 0x01);
506
        PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
507
        power_management = 0;
T
ths 已提交
508 509
        break;
    case i82557B:
510
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
T
ths 已提交
511
        PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
512
        PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
513
        power_management = 0;
T
ths 已提交
514 515
        break;
    case i82557C:
516
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
T
ths 已提交
517
        PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
518
        PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
519 520 521 522
        power_management = 0;
        break;
    case i82558A:
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
523 524
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
525 526 527
        PCI_CONFIG_8(PCI_REVISION_ID, 0x04);
        s->stats_size = 76;
        s->has_extended_tcb_support = 1;
T
ths 已提交
528 529
        break;
    case i82558B:
530
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
531 532
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
T
ths 已提交
533
        PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
534 535 536 537 538
        s->stats_size = 76;
        s->has_extended_tcb_support = 1;
        break;
    case i82559A:
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
539 540
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
541 542 543 544 545 546
        PCI_CONFIG_8(PCI_REVISION_ID, 0x06);
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
        break;
    case i82559B:
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
547 548
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
549 550 551
        PCI_CONFIG_8(PCI_REVISION_ID, 0x07);
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
T
ths 已提交
552 553
        break;
    case i82559C:
554
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
555 556
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
557 558 559 560 561 562 563 564 565
        PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
        // TODO: Windows wants revision id 0x0c.
        PCI_CONFIG_8(PCI_REVISION_ID, 0x0c);
#if EEPROM_SIZE > 0
        PCI_CONFIG_16(PCI_SUBSYSTEM_VENDOR_ID, 0x8086);
        PCI_CONFIG_16(PCI_SUBSYSTEM_ID, 0x0040);
#endif
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
T
ths 已提交
566 567
        break;
    case i82559ER:
568
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
569 570
        PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                  PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
T
ths 已提交
571
        PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
572 573 574 575 576 577 578 579 580 581
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
        break;
    case i82562:
        // TODO: check device id.
        pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
        /* TODO: wrong revision id. */
        PCI_CONFIG_8(PCI_REVISION_ID, 0x0e);
        s->stats_size = 80;
        s->has_extended_tcb_support = 1;
T
ths 已提交
582 583 584 585 586
        break;
    default:
        logout("Device %X is undefined!\n", device);
    }

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    s->configuration[6] |= BIT(5);

    if (s->stats_size == 80) {
        /* TODO: check TCO Statistical Counters bit. Documentation not clear. */
        if (s->configuration[6] & BIT(2)) {
            /* TCO statistical counters. */
            assert(s->configuration[6] & BIT(5));
        } else {
            if (s->configuration[6] & BIT(5)) {
                /* No extended statistical counters, i82557 compatible. */
                s->stats_size = 64;
            } else {
                /* i82558 compatible. */
                s->stats_size = 76;
            }
        }
    } else {
        if (s->configuration[6] & BIT(5)) {
            /* No extended statistical counters. */
            s->stats_size = 64;
        }
    }
    assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics));

    if (power_management) {
        /* Power Management Capabilities */
        PCI_CONFIG_8(0xdc, 0x01);
        /* Next Item Pointer */
        /* Capability ID */
        PCI_CONFIG_16(0xde, 0x7e21);
        /* TODO: Power Management Control / Status. */
        /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
    }

#if EEPROM_SIZE > 0
T
ths 已提交
622
    if (device == i82557C || device == i82558B || device == i82559C) {
623 624 625 626 627 628 629 630
        // TODO: get vendor id from EEPROM for i82557C or later.
        // TODO: get device id from EEPROM for i82557C or later.
        // TODO: status bit 4 can be disabled by EEPROM for i82558, i82559.
        // TODO: header type is determined by EEPROM for i82559.
        // TODO: get subsystem id from EEPROM for i82557C or later.
        // TODO: get subsystem vendor id from EEPROM for i82557C or later.
        // TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later.
        // TODO: capability pointer depends on EEPROM for i82558.
T
ths 已提交
631 632
        logout("Get device id and revision from EEPROM!!!\n");
    }
633
#endif /* EEPROM_SIZE > 0 */
T
ths 已提交
634 635 636 637 638 639 640
}

static void nic_selective_reset(EEPRO100State * s)
{
    size_t i;
    uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
    //~ eeprom93xx_reset(s->eeprom);
641
    memcpy(eeprom_contents, s->conf.macaddr.a, 6);
S
Stefan Weil 已提交
642
    eeprom_contents[EEPROM_ID] = 0x4000;
643 644
    if (s->device == i82557B || s->device == i82557C)
        eeprom_contents[5] = 0x0100;
S
Stefan Weil 已提交
645
    eeprom_contents[EEPROM_PHY_ID] = 1;
T
ths 已提交
646 647 648 649 650
    uint16_t sum = 0;
    for (i = 0; i < EEPROM_SIZE - 1; i++) {
        sum += eeprom_contents[i];
    }
    eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
651
    TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
T
ths 已提交
652 653 654 655 656 657 658 659 660 661 662

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

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

static void nic_reset(void *opaque)
{
663
    EEPRO100State *s = opaque;
664
    TRACE(OTHER, logout("%p\n", s));
S
Stefan Weil 已提交
665 666
    /* TODO: Clearing of multicast table for selective reset, too? */
    memset(&s->mult[0], 0, sizeof(s->mult));
T
ths 已提交
667 668 669 670
    nic_selective_reset(s);
}

#if defined(DEBUG_EEPRO100)
S
Stefan Weil 已提交
671
static const char * const e100_reg[PCI_IO_SIZE / 4] = {
T
ths 已提交
672 673 674 675 676 677
    "Command/Status",
    "General Pointer",
    "Port",
    "EEPROM/Flash Control",
    "MDI Control",
    "Receive DMA Byte Count",
S
Stefan Weil 已提交
678
    "Flow Control",
T
ths 已提交
679 680 681 682 683
    "General Status/Control"
};

static char *regname(uint32_t addr)
{
684
    static char buf[32];
T
ths 已提交
685
    if (addr < PCI_IO_SIZE) {
S
Stefan Weil 已提交
686
        const char *r = e100_reg[addr / 4];
T
ths 已提交
687
        if (r != 0) {
688
            snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
T
ths 已提交
689
        } else {
690
            snprintf(buf, sizeof(buf), "0x%02x", addr);
T
ths 已提交
691 692
        }
    } else {
693
        snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
T
ths 已提交
694 695 696 697 698 699 700 701 702
    }
    return buf;
}
#endif                          /* DEBUG_EEPRO100 */

#if 0
static uint16_t eepro100_read_status(EEPRO100State * s)
{
    uint16_t val = s->status;
703
    TRACE(OTHER, logout("val=0x%04x\n", val));
T
ths 已提交
704 705 706 707 708
    return val;
}

static void eepro100_write_status(EEPRO100State * s, uint16_t val)
{
709
    TRACE(OTHER, logout("val=0x%04x\n", val));
T
ths 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723
    s->status = val;
}
#endif

/*****************************************************************************
 *
 * Command emulation.
 *
 ****************************************************************************/

#if 0
static uint16_t eepro100_read_command(EEPRO100State * s)
{
    uint16_t val = 0xffff;
724
    //~ TRACE(OTHER, logout("val=0x%04x\n", val));
T
ths 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    return val;
}
#endif

/* Commands that can be put in a command list entry. */
enum commands {
    CmdNOp = 0,
    CmdIASetup = 1,
    CmdConfigure = 2,
    CmdMulticastList = 3,
    CmdTx = 4,
    CmdTDR = 5,                 /* load microcode */
    CmdDump = 6,
    CmdDiagnose = 7,

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

A
Anthony Liguori 已提交
746
static cu_state_t get_cu_state(EEPRO100State * s)
T
ths 已提交
747 748 749 750
{
    return ((s->mem[SCBStatus] >> 6) & 0x03);
}

A
Anthony Liguori 已提交
751
static void set_cu_state(EEPRO100State * s, cu_state_t state)
T
ths 已提交
752 753 754 755
{
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
}

A
Anthony Liguori 已提交
756
static ru_state_t get_ru_state(EEPRO100State * s)
T
ths 已提交
757 758 759 760
{
    return ((s->mem[SCBStatus] >> 2) & 0x0f);
}

A
Anthony Liguori 已提交
761
static void set_ru_state(EEPRO100State * s, ru_state_t state)
T
ths 已提交
762 763 764 765 766 767 768 769 770 771 772
{
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
}

static void dump_statistics(EEPRO100State * s)
{
    /* Dump statistical data. Most data is never changed by the emulation
     * and always 0, so we first just copy the whole block and then those
     * values which really matter.
     * Number of data should check configuration!!!
     */
773 774 775 776 777 778 779 780
    cpu_physical_memory_write(s->statsaddr,
                              (uint8_t *) & s->statistics, s->stats_size);
    stl_le_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
    stl_le_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
    stl_le_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
    stl_le_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
    //~ stw_le_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
    //~ stw_le_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
T
ths 已提交
781 782 783
    //~ missing("CU dump statistical counters");
}

784 785
static void tx_command(EEPRO100State *s)
{
S
Stefan Weil 已提交
786
    uint32_t tbd_array = le32_to_cpu(s->tx.tbd_array_addr);
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    uint16_t tcb_bytes = (le16_to_cpu(s->tx.tcb_bytes) & 0x3fff);
    /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */
    uint8_t buf[2600];
    uint16_t size = 0;
    uint32_t tbd_address = s->cb_address + 0x10;
    TRACE(RXTX, logout
        ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
         tbd_array, tcb_bytes, s->tx.tbd_count));

    if (tcb_bytes > 2600) {
        logout("TCB byte count too large, using 2600\n");
        tcb_bytes = 2600;
    }
    if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
        logout
            ("illegal values of TBD array address and TCB byte count!\n");
    }
    assert(tcb_bytes <= sizeof(buf));
    while (size < tcb_bytes) {
        uint32_t tx_buffer_address = ldl_phys(tbd_address);
        uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
        //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
        tbd_address += 8;
        TRACE(RXTX, logout
            ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
             tx_buffer_address, tx_buffer_size));
        tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
        cpu_physical_memory_read(tx_buffer_address, &buf[size],
                                 tx_buffer_size);
        size += tx_buffer_size;
    }
    if (tbd_array == 0xffffffff) {
        /* Simplified mode. Was already handled by code above. */
    } else {
        /* Flexible mode. */
        uint8_t tbd_count = 0;
        if (s->has_extended_tcb_support && !(s->configuration[6] & BIT(4))) {
            /* Extended Flexible TCB. */
            for (; tbd_count < 2; tbd_count++) {
                uint32_t tx_buffer_address = ldl_phys(tbd_address);
                uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
                uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
                tbd_address += 8;
                TRACE(RXTX, logout
                    ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
                     tx_buffer_address, tx_buffer_size));
                tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
                cpu_physical_memory_read(tx_buffer_address, &buf[size],
                                         tx_buffer_size);
                size += tx_buffer_size;
                if (tx_buffer_el & 1) {
                    break;
                }
            }
        }
        tbd_address = tbd_array;
        for (; tbd_count < s->tx.tbd_count; tbd_count++) {
            uint32_t tx_buffer_address = ldl_phys(tbd_address);
            uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
            uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
            tbd_address += 8;
            TRACE(RXTX, logout
                ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
                 tx_buffer_address, tx_buffer_size));
            tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
            cpu_physical_memory_read(tx_buffer_address, &buf[size],
                                     tx_buffer_size);
            size += tx_buffer_size;
            if (tx_buffer_el & 1) {
                break;
            }
        }
    }
    TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
    qemu_send_packet(&s->nic->nc, buf, size);
    s->statistics.tx_good_frames++;
    /* Transmit with bad status would raise an CX/TNO interrupt.
     * (82557 only). Emulation never has bad status. */
    //~ eepro100_cx_interrupt(s);
}

S
Stefan Weil 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
static void set_multicast_list(EEPRO100State *s)
{
    uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0);
    uint16_t i;
    memset(&s->mult[0], 0, sizeof(s->mult));
    TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count));
    for (i = 0; i < multicast_count; i += 6) {
        uint8_t multicast_addr[6];
        cpu_physical_memory_read(s->cb_address + 10 + i, multicast_addr, 6);
        TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6)));
        unsigned mcast_idx = compute_mcast_idx(multicast_addr);
        assert(mcast_idx < 64);
        s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
    }
}

S
Stefan Weil 已提交
884
static void action_command(EEPRO100State *s)
T
ths 已提交
885
{
S
Stefan Weil 已提交
886
    for (;;) {
887 888 889 890
        s->cb_address = s->cu_base + s->cu_offset;
        cpu_physical_memory_read(s->cb_address, (uint8_t *)&s->tx, sizeof(s->tx));
        uint16_t status = le16_to_cpu(s->tx.status);
        uint16_t command = le16_to_cpu(s->tx.command);
891 892
        logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
               status, command, s->tx.link);
T
ths 已提交
893 894 895 896
        bool bit_el = ((command & 0x8000) != 0);
        bool bit_s = ((command & 0x4000) != 0);
        bool bit_i = ((command & 0x2000) != 0);
        bool bit_nc = ((command & 0x0010) != 0);
897
        bool success = true;
T
ths 已提交
898 899
        //~ bool bit_sf = ((command & 0x0008) != 0);
        uint16_t cmd = command & 0x0007;
900
        s->cu_offset = le32_to_cpu(s->tx.link);
T
ths 已提交
901 902 903 904 905
        switch (cmd) {
        case CmdNOp:
            /* Do nothing. */
            break;
        case CmdIASetup:
906
            cpu_physical_memory_read(s->cb_address + 8, &s->conf.macaddr.a[0], 6);
907
            TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)));
T
ths 已提交
908 909
            break;
        case CmdConfigure:
910
            cpu_physical_memory_read(s->cb_address + 8, &s->configuration[0],
T
ths 已提交
911
                                     sizeof(s->configuration));
912
            TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
T
ths 已提交
913 914
            break;
        case CmdMulticastList:
S
Stefan Weil 已提交
915
            set_multicast_list(s);
T
ths 已提交
916 917
            break;
        case CmdTx:
918 919 920 921 922
            if (bit_nc) {
                missing("CmdTx: NC = 0");
                success = false;
                break;
            }
923
            tx_command(s);
T
ths 已提交
924 925
            break;
        case CmdTDR:
926
            TRACE(OTHER, logout("load microcode\n"));
T
ths 已提交
927 928 929 930 931
            /* Starting with offset 8, the command contains
             * 64 dwords microcode which we just ignore here. */
            break;
        default:
            missing("undefined command");
932 933
            success = false;
            break;
T
ths 已提交
934
        }
935
        /* Write new status. */
936
        stw_phys(s->cb_address, status | 0x8000 | (success ? 0x2000 : 0));
T
ths 已提交
937 938 939 940 941
        if (bit_i) {
            /* CU completed action. */
            eepro100_cx_interrupt(s);
        }
        if (bit_el) {
942
            /* CU becomes idle. Terminate command loop. */
T
ths 已提交
943 944
            set_cu_state(s, cu_idle);
            eepro100_cna_interrupt(s);
S
Stefan Weil 已提交
945
            break;
T
ths 已提交
946
        } else if (bit_s) {
S
Stefan Weil 已提交
947
            /* CU becomes suspended. Terminate command loop. */
T
ths 已提交
948 949
            set_cu_state(s, cu_suspended);
            eepro100_cna_interrupt(s);
S
Stefan Weil 已提交
950
            break;
T
ths 已提交
951 952
        } else {
            /* More entries in list. */
953
            TRACE(OTHER, logout("CU list with at least one more entry\n"));
T
ths 已提交
954
        }
S
Stefan Weil 已提交
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
    }
    TRACE(OTHER, logout("CU list empty\n"));
    /* List is empty. Now CU is idle or suspended. */
}

static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
{
    switch (val) {
    case CU_NOP:
        /* No operation. */
        break;
    case CU_START:
        if (get_cu_state(s) != cu_idle) {
            /* Intel documentation says that CU must be idle for the CU
             * start command. Intel driver for Linux also starts the CU
             * from suspended state. */
            logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
            //~ assert(!"wrong CU state");
        }
        set_cu_state(s, cu_active);
        s->cu_offset = s->pointer;
        action_command(s);
T
ths 已提交
977 978 979 980 981 982 983 984 985 986
        break;
    case CU_RESUME:
        if (get_cu_state(s) != cu_suspended) {
            logout("bad CU resume from CU state %u\n", get_cu_state(s));
            /* Workaround for bad Linux eepro100 driver which resumes
             * from idle state. */
            //~ missing("cu resume");
            set_cu_state(s, cu_suspended);
        }
        if (get_cu_state(s) == cu_suspended) {
987
            TRACE(OTHER, logout("CU resuming\n"));
T
ths 已提交
988
            set_cu_state(s, cu_active);
S
Stefan Weil 已提交
989
            action_command(s);
T
ths 已提交
990 991 992 993 994
        }
        break;
    case CU_STATSADDR:
        /* Load dump counters address. */
        s->statsaddr = s->pointer;
995
        TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
T
ths 已提交
996 997 998
        break;
    case CU_SHOWSTATS:
        /* Dump statistical counters. */
999
        TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
T
ths 已提交
1000
        dump_statistics(s);
1001
        stl_le_phys(s->statsaddr + s->stats_size, 0xa005);
T
ths 已提交
1002 1003 1004
        break;
    case CU_CMD_BASE:
        /* Load CU base. */
1005
        TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
T
ths 已提交
1006 1007 1008 1009
        s->cu_base = s->pointer;
        break;
    case CU_DUMPSTATS:
        /* Dump and reset statistical counters. */
1010
        TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
T
ths 已提交
1011
        dump_statistics(s);
1012
        stl_le_phys(s->statsaddr + s->stats_size, 0xa007);
T
ths 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        memset(&s->statistics, 0, sizeof(s->statistics));
        break;
    case CU_SRESUME:
        /* CU static resume. */
        missing("CU static resume");
        break;
    default:
        missing("Undefined CU command");
    }
}

static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
{
    switch (val) {
    case RU_NOP:
        /* No operation. */
        break;
    case RX_START:
        /* RU start. */
        if (get_ru_state(s) != ru_idle) {
            logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
            //~ assert(!"wrong RU state");
        }
        set_ru_state(s, ru_ready);
        s->ru_offset = s->pointer;
1038
        TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
T
ths 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
        break;
    case RX_RESUME:
        /* Restart RU. */
        if (get_ru_state(s) != ru_suspended) {
            logout("RU state is %u, should be %u\n", get_ru_state(s),
                   ru_suspended);
            //~ assert(!"wrong RU state");
        }
        set_ru_state(s, ru_ready);
        break;
    case RX_ADDR_LOAD:
        /* Load RU base. */
1051
        TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
T
ths 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
        s->ru_base = s->pointer;
        break;
    default:
        logout("val=0x%02x (undefined RU command)\n", val);
        missing("Undefined SU command");
    }
}

static void eepro100_write_command(EEPRO100State * s, uint8_t val)
{
    eepro100_ru_command(s, val & 0x0f);
    eepro100_cu_command(s, val & 0xf0);
    if ((val) == 0) {
1065
        TRACE(OTHER, logout("val=0x%02x\n", val));
T
ths 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    }
    /* Clear command byte after command was accepted. */
    s->mem[SCBCmd] = 0;
}

/*****************************************************************************
 *
 * EEPROM emulation.
 *
 ****************************************************************************/

#define EEPROM_CS       0x02
#define EEPROM_SK       0x01
#define EEPROM_DI       0x04
#define EEPROM_DO       0x08

static uint16_t eepro100_read_eeprom(EEPRO100State * s)
{
    uint16_t val;
    memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
    if (eeprom93xx_read(s->eeprom)) {
        val |= EEPROM_DO;
    } else {
        val &= ~EEPROM_DO;
    }
1091
    TRACE(EEPROM, logout("val=0x%04x\n", val));
T
ths 已提交
1092 1093 1094
    return val;
}

A
Anthony Liguori 已提交
1095
static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
T
ths 已提交
1096
{
1097
    TRACE(EEPROM, logout("val=0x%02x\n", val));
T
ths 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

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

    int eecs = ((val & EEPROM_CS) != 0);
    int eesk = ((val & EEPROM_SK) != 0);
    int eedi = ((val & EEPROM_DI) != 0);
    eeprom93xx_write(eeprom, eecs, eesk, eedi);
}

static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
{
    s->pointer = le32_to_cpu(val);
1111
    TRACE(OTHER, logout("val=0x%08x\n", val));
T
ths 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120
}

/*****************************************************************************
 *
 * MDI emulation.
 *
 ****************************************************************************/

#if defined(DEBUG_EEPRO100)
1121
static const char * const mdi_op_name[] = {
T
ths 已提交
1122 1123 1124 1125 1126 1127
    "opcode 0",
    "write",
    "read",
    "opcode 3"
};

1128
static const char * const mdi_reg_name[] = {
T
ths 已提交
1129 1130 1131 1132 1133 1134 1135 1136
    "Control",
    "Status",
    "PHY Identification (Word 1)",
    "PHY Identification (Word 2)",
    "Auto-Negotiation Advertisement",
    "Auto-Negotiation Link Partner Ability",
    "Auto-Negotiation Expansion"
};
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

static const char *reg2name(uint8_t reg)
{
    static char buffer[10];
    const char *p = buffer;
    if (reg < ARRAY_SIZE(mdi_reg_name)) {
        p = mdi_reg_name[reg];
    } else {
        snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
    }
    return p;
}
T
ths 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
#endif                          /* DEBUG_EEPRO100 */

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

#ifdef DEBUG_EEPRO100
    uint8_t raiseint = (val & BIT(29)) >> 29;
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
    uint8_t phy = (val & BITS(25, 21)) >> 21;
    uint8_t reg = (val & BITS(20, 16)) >> 16;
    uint16_t data = (val & BITS(15, 0));
#endif
    /* Emulation takes no time to finish MDI transaction. */
    val |= BIT(28);
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
                      val, raiseint, mdi_op_name[opcode], phy,
1167
                      reg2name(reg), data));
T
ths 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    return val;
}

static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
{
    uint8_t raiseint = (val & BIT(29)) >> 29;
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
    uint8_t phy = (val & BITS(25, 21)) >> 21;
    uint8_t reg = (val & BITS(20, 16)) >> 16;
    uint16_t data = (val & BITS(15, 0));
1178 1179
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
          val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
T
ths 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    if (phy != 1) {
        /* Unsupported PHY address. */
        //~ logout("phy must be 1 but is %u\n", phy);
        data = 0;
    } else if (opcode != 1 && opcode != 2) {
        /* Unsupported opcode. */
        logout("opcode must be 1 or 2 but is %u\n", opcode);
        data = 0;
    } else if (reg > 6) {
        /* Unsupported register. */
        logout("register must be 0...6 but is %u\n", reg);
        data = 0;
    } else {
        TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
                          val, raiseint, mdi_op_name[opcode], phy,
1195
                          reg2name(reg), data));
T
ths 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
        if (opcode == 1) {
            /* MDI write */
            switch (reg) {
            case 0:            /* Control Register */
                if (data & 0x8000) {
                    /* Reset status and control registers to default. */
                    s->mdimem[0] = eepro100_mdi_default[0];
                    s->mdimem[1] = eepro100_mdi_default[1];
                    data = s->mdimem[reg];
                } else {
                    /* Restart Auto Configuration = Normal Operation */
                    data &= ~0x0200;
                }
                break;
            case 1:            /* Status Register */
                missing("not writable");
                data = s->mdimem[reg];
                break;
            case 2:            /* PHY Identification Register (Word 1) */
            case 3:            /* PHY Identification Register (Word 2) */
                missing("not implemented");
                break;
            case 4:            /* Auto-Negotiation Advertisement Register */
            case 5:            /* Auto-Negotiation Link Partner Ability Register */
                break;
            case 6:            /* Auto-Negotiation Expansion Register */
            default:
                missing("not implemented");
            }
            s->mdimem[reg] = data;
        } else if (opcode == 2) {
            /* MDI read */
            switch (reg) {
            case 0:            /* Control Register */
                if (data & 0x8000) {
                    /* Reset status and control registers to default. */
                    s->mdimem[0] = eepro100_mdi_default[0];
                    s->mdimem[1] = eepro100_mdi_default[1];
                }
                break;
            case 1:            /* Status Register */
                s->mdimem[reg] |= 0x0020;
                break;
            case 2:            /* PHY Identification Register (Word 1) */
            case 3:            /* PHY Identification Register (Word 2) */
            case 4:            /* Auto-Negotiation Advertisement Register */
                break;
            case 5:            /* Auto-Negotiation Link Partner Ability Register */
                s->mdimem[reg] = 0x41fe;
                break;
            case 6:            /* Auto-Negotiation Expansion Register */
                s->mdimem[reg] = 0x0001;
                break;
            }
            data = s->mdimem[reg];
        }
        /* Emulation takes no time to finish MDI transaction.
         * Set MDI bit in SCB status register. */
        s->mem[SCBAck] |= 0x08;
        val |= BIT(28);
        if (raiseint) {
            eepro100_mdi_interrupt(s);
        }
    }
    val = (val & 0xffff0000) + data;
    memcpy(&s->mem[0x10], &val, sizeof(val));
}

/*****************************************************************************
 *
 * Port emulation.
 *
 ****************************************************************************/

#define PORT_SOFTWARE_RESET     0
#define PORT_SELFTEST           1
#define PORT_SELECTIVE_RESET    2
#define PORT_DUMP               3
#define PORT_SELECTION_MASK     3

typedef struct {
    uint32_t st_sign;           /* Self Test Signature */
    uint32_t st_result;         /* Self Test Results */
A
Anthony Liguori 已提交
1279
} eepro100_selftest_t;
T
ths 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

static uint32_t eepro100_read_port(EEPRO100State * s)
{
    return 0;
}

static void eepro100_write_port(EEPRO100State * s, uint32_t val)
{
    val = le32_to_cpu(val);
    uint32_t address = (val & ~PORT_SELECTION_MASK);
    uint8_t selection = (val & PORT_SELECTION_MASK);
    switch (selection) {
    case PORT_SOFTWARE_RESET:
        nic_reset(s);
        break;
    case PORT_SELFTEST:
1296
        TRACE(OTHER, logout("selftest address=0x%08x\n", address));
A
Anthony Liguori 已提交
1297
        eepro100_selftest_t data;
T
ths 已提交
1298 1299 1300 1301 1302 1303
        cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
        data.st_sign = 0xffffffff;
        data.st_result = 0;
        cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
        break;
    case PORT_SELECTIVE_RESET:
1304
        TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
T
ths 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        nic_selective_reset(s);
        break;
    default:
        logout("val=0x%08x\n", val);
        missing("unknown port selection");
    }
}

/*****************************************************************************
 *
 * General hardware emulation.
 *
 ****************************************************************************/

static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
{
    uint8_t val;
    if (addr <= sizeof(s->mem) - sizeof(val)) {
        memcpy(&val, &s->mem[addr], sizeof(val));
    }

    switch (addr) {
    case SCBStatus:
        //~ val = eepro100_read_status(s);
1329
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1330 1331 1332
        break;
    case SCBAck:
        //~ val = eepro100_read_status(s);
1333
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1334 1335
        break;
    case SCBCmd:
1336
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1337 1338 1339
        //~ val = eepro100_read_command(s);
        break;
    case SCBIntmask:
1340
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1341 1342
        break;
    case SCBPort + 3:
1343
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1344 1345 1346 1347
        break;
    case SCBeeprom:
        val = eepro100_read_eeprom(s);
        break;
1348
    case SCBpmdr:       /* Power Management Driver Register */
T
ths 已提交
1349
        val = 0;
1350
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1351
        break;
1352
    case SCBgstat:      /* General Status Register */
T
ths 已提交
1353 1354
        /* 100 Mbps full duplex, valid link */
        val = 0x07;
1355
        TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
T
ths 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        break;
    default:
        logout("addr=%s val=0x%02x\n", regname(addr), val);
        missing("unknown byte read");
    }
    return val;
}

static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
{
    uint16_t val;
    if (addr <= sizeof(s->mem) - sizeof(val)) {
        memcpy(&val, &s->mem[addr], sizeof(val));
    }

    switch (addr) {
    case SCBStatus:
        //~ val = eepro100_read_status(s);
1374
    case SCBCmd:
1375
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1376 1377 1378
        break;
    case SCBeeprom:
        val = eepro100_read_eeprom(s);
1379
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
        break;
    default:
        logout("addr=%s val=0x%04x\n", regname(addr), val);
        missing("unknown word read");
    }
    return val;
}

static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
{
    uint32_t val;
    if (addr <= sizeof(s->mem) - sizeof(val)) {
        memcpy(&val, &s->mem[addr], sizeof(val));
    }

    switch (addr) {
    case SCBStatus:
        //~ val = eepro100_read_status(s);
1398
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1399 1400 1401
        break;
    case SCBPointer:
        //~ val = eepro100_read_pointer(s);
1402
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1403 1404 1405
        break;
    case SCBPort:
        val = eepro100_read_port(s);
1406
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
        break;
    case SCBCtrlMDI:
        val = eepro100_read_mdi(s);
        break;
    default:
        logout("addr=%s val=0x%08x\n", regname(addr), val);
        missing("unknown longword read");
    }
    return val;
}

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

1424
    TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442

    switch (addr) {
    case SCBStatus:
        //~ eepro100_write_status(s, val);
        break;
    case SCBAck:
        eepro100_acknowledge(s);
        break;
    case SCBCmd:
        eepro100_write_command(s, val);
        break;
    case SCBIntmask:
        if (val & BIT(1)) {
            eepro100_swi_interrupt(s);
        }
        eepro100_interrupt(s, 0);
        break;
    case SCBPort + 3:
1443
    case SCBFlow:       /* does not exist on 82557 */
T
ths 已提交
1444 1445
    case SCBFlow + 1:
    case SCBFlow + 2:
1446
    case SCBpmdr:       /* does not exist on 82557 */
1447
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
        break;
    case SCBeeprom:
        eepro100_write_eeprom(s->eeprom, val);
        break;
    default:
        logout("addr=%s val=0x%02x\n", regname(addr), val);
        missing("unknown byte write");
    }
}

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

1464
    TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

    switch (addr) {
    case SCBStatus:
        //~ eepro100_write_status(s, val);
        eepro100_acknowledge(s);
        break;
    case SCBCmd:
        eepro100_write_command(s, val);
        eepro100_write1(s, SCBIntmask, val >> 8);
        break;
    case SCBeeprom:
        eepro100_write_eeprom(s->eeprom, val);
        break;
    default:
        logout("addr=%s val=0x%04x\n", regname(addr), val);
        missing("unknown word write");
    }
}

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

    switch (addr) {
    case SCBPointer:
        eepro100_write_pointer(s, val);
        break;
    case SCBPort:
1495
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
        eepro100_write_port(s, val);
        break;
    case SCBCtrlMDI:
        eepro100_write_mdi(s, val);
        break;
    default:
        logout("addr=%s val=0x%08x\n", regname(addr), val);
        missing("unknown longword write");
    }
}

1507 1508 1509 1510 1511 1512
/*****************************************************************************
 *
 * Port mapped I/O.
 *
 ****************************************************************************/

T
ths 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
static uint32_t ioport_read1(void *opaque, uint32_t addr)
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s\n", regname(addr));
    return eepro100_read1(s, addr - s->region[1]);
}

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

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

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

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

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

/***********************************************************/
/* PCI EEPRO100 definitions */

static void pci_map(PCIDevice * pci_dev, int region_num,
1555
                    pcibus_t addr, pcibus_t size, int type)
T
ths 已提交
1556
{
1557
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
T
ths 已提交
1558

1559 1560
    TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
          "size=0x%08"FMT_PCIBUS", type=%d\n",
1561
          region_num, addr, size, type));
T
ths 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

    assert(region_num == 1);
    register_ioport_write(addr, size, 1, ioport_write1, s);
    register_ioport_read(addr, size, 1, ioport_read1, s);
    register_ioport_write(addr, size, 2, ioport_write2, s);
    register_ioport_read(addr, size, 2, ioport_read2, s);
    register_ioport_write(addr, size, 4, ioport_write4, s);
    register_ioport_read(addr, size, 4, ioport_read4, s);

    s->region[region_num] = addr;
}

1574 1575 1576 1577 1578 1579
/*****************************************************************************
 *
 * Memory mapped I/O.
 *
 ****************************************************************************/

A
Anthony Liguori 已提交
1580
static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
T
ths 已提交
1581 1582 1583 1584 1585 1586
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
    eepro100_write1(s, addr, val);
}

A
Anthony Liguori 已提交
1587
static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
T
ths 已提交
1588 1589 1590 1591 1592 1593
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
    eepro100_write2(s, addr, val);
}

A
Anthony Liguori 已提交
1594
static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
T
ths 已提交
1595 1596 1597 1598 1599 1600
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
    eepro100_write4(s, addr, val);
}

A
Anthony Liguori 已提交
1601
static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
T
ths 已提交
1602 1603 1604 1605 1606 1607
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s\n", regname(addr));
    return eepro100_read1(s, addr);
}

A
Anthony Liguori 已提交
1608
static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
T
ths 已提交
1609 1610 1611 1612 1613 1614
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s\n", regname(addr));
    return eepro100_read2(s, addr);
}

A
Anthony Liguori 已提交
1615
static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
T
ths 已提交
1616 1617 1618 1619 1620 1621
{
    EEPRO100State *s = opaque;
    //~ logout("addr=%s\n", regname(addr));
    return eepro100_read4(s, addr);
}

1622
static CPUWriteMemoryFunc * const pci_mmio_write[] = {
T
ths 已提交
1623 1624 1625 1626 1627
    pci_mmio_writeb,
    pci_mmio_writew,
    pci_mmio_writel
};

1628
static CPUReadMemoryFunc * const pci_mmio_read[] = {
T
ths 已提交
1629 1630 1631 1632 1633 1634
    pci_mmio_readb,
    pci_mmio_readw,
    pci_mmio_readl
};

static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1635
                         pcibus_t addr, pcibus_t size, int type)
T
ths 已提交
1636
{
1637
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
T
ths 已提交
1638

1639 1640
    TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
          "size=0x%08"FMT_PCIBUS", type=%d\n",
1641
          region_num, addr, size, type));
T
ths 已提交
1642 1643 1644

    if (region_num == 0) {
        /* Map control / status registers. */
1645 1646
        cpu_register_physical_memory(addr, size, s->mmio_index);
        s->region[region_num] = addr;
T
ths 已提交
1647 1648 1649
    }
}

1650
static int nic_can_receive(VLANClientState *nc)
T
ths 已提交
1651
{
1652
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1653
    TRACE(RXTX, logout("%p\n", s));
T
ths 已提交
1654 1655 1656 1657
    return get_ru_state(s) == ru_ready;
    //~ return !eepro100_buffer_full(s);
}

1658
static ssize_t nic_receive(VLANClientState *nc, const uint8_t * buf, size_t size)
T
ths 已提交
1659 1660 1661 1662 1663
{
    /* TODO:
     * - Magic packets should set bit 30 in power management driver register.
     * - Interesting packets should set bit 29 in power management driver register.
     */
1664
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
T
ths 已提交
1665 1666 1667 1668 1669
    uint16_t rfd_status = 0xa000;
    static const uint8_t broadcast_macaddr[6] =
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

    /* TODO: check multiple IA bit. */
1670 1671 1672 1673
    if (s->configuration[20] & BIT(6)) {
        missing("Multiple IA bit");
        return -1;
    }
T
ths 已提交
1674 1675 1676 1677

    if (s->configuration[8] & 0x80) {
        /* CSMA is disabled. */
        logout("%p received while CSMA is disabled\n", s);
1678
        return -1;
T
ths 已提交
1679 1680 1681
    } else if (size < 64 && (s->configuration[7] & 1)) {
        /* Short frame and configuration byte 7/0 (discard short receive) set:
         * Short frame is discarded */
1682
        logout("%p received short frame (%zu byte)\n", s, size);
T
ths 已提交
1683
        s->statistics.rx_short_frame_errors++;
1684
        //~ return -1;
T
ths 已提交
1685 1686 1687
    } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
        /* Long frame and configuration byte 18/3 (long receive ok) not set:
         * Long frames are discarded. */
1688
        logout("%p received long frame (%zu byte), ignored\n", s, size);
1689
        return -1;
1690
    } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) {       // !!!
T
ths 已提交
1691 1692
        /* Frame matches individual address. */
        /* TODO: check configuration byte 15/4 (ignore U/L). */
1693
        TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
T
ths 已提交
1694 1695
    } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
        /* Broadcast frame. */
1696
        TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
T
ths 已提交
1697
        rfd_status |= 0x0002;
S
Stefan Weil 已提交
1698
    } else if (buf[0] & 0x01) {
T
ths 已提交
1699
        /* Multicast frame. */
S
Stefan Weil 已提交
1700
        TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size)));
1701
        if (s->configuration[21] & BIT(3)) {
S
Stefan Weil 已提交
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
          /* Multicast all bit is set, receive all multicast frames. */
        } else {
          unsigned mcast_idx = compute_mcast_idx(buf);
          assert(mcast_idx < 64);
          if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
            /* Multicast frame is allowed in hash table. */
          } else if (s->configuration[15] & 1) {
              /* Promiscuous: receive all. */
              rfd_status |= 0x0004;
          } else {
              TRACE(RXTX, logout("%p multicast ignored\n", s));
              return -1;
          }
T
ths 已提交
1715
        }
S
Stefan Weil 已提交
1716
        /* TODO: Next not for promiscuous mode? */
T
ths 已提交
1717 1718 1719
        rfd_status |= 0x0002;
    } else if (s->configuration[15] & 1) {
        /* Promiscuous: receive all. */
1720
        TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
T
ths 已提交
1721 1722
        rfd_status |= 0x0004;
    } else {
1723
        TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1724
              nic_dump(buf, size)));
1725
        return size;
T
ths 已提交
1726 1727 1728
    }

    if (get_ru_state(s) != ru_ready) {
1729 1730
        /* No resources available. */
        logout("no resources, state=%u\n", get_ru_state(s));
T
ths 已提交
1731
        s->statistics.rx_resource_errors++;
1732
        //~ assert(!"no resources");
1733
        return -1;
T
ths 已提交
1734 1735 1736
    }
    //~ !!!
//~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
A
Anthony Liguori 已提交
1737
    eepro100_rx_t rx;
T
ths 已提交
1738
    cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
A
Anthony Liguori 已提交
1739
                             offsetof(eepro100_rx_t, packet));
T
ths 已提交
1740 1741
    uint16_t rfd_command = le16_to_cpu(rx.command);
    uint16_t rfd_size = le16_to_cpu(rx.size);
1742 1743 1744 1745 1746 1747

    if (size > rfd_size) {
        logout("Receive buffer (%" PRId16 " bytes) too small for data "
            "(%zu bytes); data truncated\n", rfd_size, size);
        size = rfd_size;
    }
T
ths 已提交
1748 1749 1750
    if (size < 64) {
        rfd_status |= 0x0080;
    }
1751 1752
    TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
          rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
A
Anthony Liguori 已提交
1753
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
T
ths 已提交
1754
             rfd_status);
A
Anthony Liguori 已提交
1755
    stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
T
ths 已提交
1756 1757 1758
    /* Early receive interrupt not supported. */
    //~ eepro100_er_interrupt(s);
    /* Receive CRC Transfer not supported. */
1759 1760 1761 1762
    if (s->configuration[18] & 4) {
        missing("Receive CRC Transfer");
        return -1;
    }
T
ths 已提交
1763 1764 1765
    /* TODO: check stripping enable bit. */
    //~ assert(!(s->configuration[17] & 1));
    cpu_physical_memory_write(s->ru_base + s->ru_offset +
A
Anthony Liguori 已提交
1766
                              offsetof(eepro100_rx_t, packet), buf, size);
T
ths 已提交
1767 1768 1769 1770 1771
    s->statistics.rx_good_frames++;
    eepro100_fr_interrupt(s);
    s->ru_offset = le32_to_cpu(rx.link);
    if (rfd_command & 0x8000) {
        /* EL bit is set, so this was the last frame. */
1772 1773
        logout("receive: Running out of frames\n");
        set_ru_state(s, ru_suspended);
T
ths 已提交
1774 1775 1776 1777 1778
    }
    if (rfd_command & 0x4000) {
        /* S bit is set. */
        set_ru_state(s, ru_suspended);
    }
1779
    return size;
T
ths 已提交
1780 1781
}

J
Juan Quintela 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
static const VMStateDescription vmstate_eepro100 = {
    .version_id = 3,
    .minimum_version_id = 2,
    .minimum_version_id_old = 2,
    .fields      = (VMStateField []) {
        VMSTATE_PCI_DEVICE(dev, EEPRO100State),
        VMSTATE_UNUSED(32),
        VMSTATE_BUFFER(mult, EEPRO100State),
        VMSTATE_BUFFER(mem, EEPRO100State),
        /* Save all members of struct between scb_stat and mem. */
        VMSTATE_UINT8(scb_stat, EEPRO100State),
        VMSTATE_UINT8(int_stat, EEPRO100State),
        VMSTATE_UNUSED(3*4),
        VMSTATE_MACADDR(conf.macaddr, EEPRO100State),
        VMSTATE_UNUSED(19*4),
        VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32),
        /* The eeprom should be saved and restored by its own routines. */
        VMSTATE_UINT32(device, EEPRO100State),
        /* TODO check device. */
        VMSTATE_UINT32(pointer, EEPRO100State),
        VMSTATE_UINT32(cu_base, EEPRO100State),
        VMSTATE_UINT32(cu_offset, EEPRO100State),
        VMSTATE_UINT32(ru_base, EEPRO100State),
        VMSTATE_UINT32(ru_offset, EEPRO100State),
        VMSTATE_UINT32(statsaddr, EEPRO100State),
1807
        /* Save eepro100_stats_t statistics. */
J
Juan Quintela 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
        VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State),
        VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State),
        VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State),
        VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State),
        VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State),
        VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State),
        VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State),
1829
#if 0
J
Juan Quintela 已提交
1830
        VMSTATE_UINT16(status, EEPRO100State),
1831
#endif
J
Juan Quintela 已提交
1832 1833 1834
        /* Configuration bytes. */
        VMSTATE_BUFFER(configuration, EEPRO100State),
        VMSTATE_END_OF_LIST()
1835
    }
J
Juan Quintela 已提交
1836
};
T
ths 已提交
1837

1838
static void nic_cleanup(VLANClientState *nc)
1839
{
1840
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1841

1842
    s->nic = NULL;
1843 1844
}

S
Stefan Weil 已提交
1845
static int pci_nic_uninit(PCIDevice *pci_dev)
1846
{
S
Stefan Weil 已提交
1847
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1848 1849

    cpu_unregister_io_memory(s->mmio_index);
J
Juan Quintela 已提交
1850
    vmstate_unregister(s->vmstate, s);
1851
    eeprom93xx_free(s->eeprom);
1852
    qemu_del_vlan_client(&s->nic->nc);
1853 1854 1855
    return 0;
}

1856 1857 1858 1859 1860 1861 1862 1863
static NetClientInfo net_eepro100_info = {
    .type = NET_CLIENT_TYPE_NIC,
    .size = sizeof(NICState),
    .can_receive = nic_can_receive,
    .receive = nic_receive,
    .cleanup = nic_cleanup,
};

1864
static int nic_init(PCIDevice *pci_dev, uint32_t device)
T
ths 已提交
1865
{
1866
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
T
ths 已提交
1867

1868
    TRACE(OTHER, logout("\n"));
T
ths 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878

    s->device = device;

    pci_reset(s);

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

    /* Handler for memory-mapped I/O */
1879
    s->mmio_index =
1880
        cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
T
ths 已提交
1881

1882
    pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1883 1884 1885
                           PCI_BASE_ADDRESS_SPACE_MEMORY |
                           PCI_BASE_ADDRESS_MEM_PREFETCH, pci_mmio_map);
    pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_BASE_ADDRESS_SPACE_IO,
T
ths 已提交
1886
                           pci_map);
1887
    pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_BASE_ADDRESS_SPACE_MEMORY,
T
ths 已提交
1888 1889
                           pci_mmio_map);

1890
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
1891
    logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
T
ths 已提交
1892 1893 1894 1895
    assert(s->region[1] == 0);

    nic_reset(s);

1896 1897
    s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
                          pci_dev->qdev.info->name, pci_dev->qdev.id, s);
T
ths 已提交
1898

1899 1900
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
    TRACE(OTHER, logout("%s\n", s->nic->nc.info_str));
T
ths 已提交
1901

1902
    qemu_register_reset(nic_reset, s);
T
ths 已提交
1903

J
Juan Quintela 已提交
1904 1905
    s->vmstate = qemu_malloc(sizeof(vmstate_eepro100));
    memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
1906
    s->vmstate->name = s->nic->nc.model;
J
Juan Quintela 已提交
1907
    vmstate_register(-1, s->vmstate, s);
S
Stefan Weil 已提交
1908

1909
    return 0;
T
ths 已提交
1910 1911
}

S
Stefan Weil 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
static int pci_i82550_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82550);
}

static int pci_i82551_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82551);
}

static int pci_i82557a_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82557A);
}

static int pci_i82557b_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82557B);
}

static int pci_i82557c_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82557C);
}

static int pci_i82558a_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82558A);
}

static int pci_i82558b_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82558B);
}

static int pci_i82559a_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82559A);
}

static int pci_i82559b_init(PCIDevice *pci_dev)
{
    return nic_init(pci_dev, i82559B);
}

static int pci_i82559c_init(PCIDevice *pci_dev)
P
Paul Brook 已提交
1958
{
S
Stefan Weil 已提交
1959
    return nic_init(pci_dev, i82559C);
P
Paul Brook 已提交
1960 1961
}

S
Stefan Weil 已提交
1962
static int pci_i82559er_init(PCIDevice *pci_dev)
T
ths 已提交
1963
{
S
Stefan Weil 已提交
1964
    return nic_init(pci_dev, i82559ER);
T
ths 已提交
1965 1966
}

S
Stefan Weil 已提交
1967
static int pci_i82562_init(PCIDevice *pci_dev)
T
ths 已提交
1968
{
S
Stefan Weil 已提交
1969
    return nic_init(pci_dev, i82562);
T
ths 已提交
1970 1971
}

1972 1973
static PCIDeviceInfo eepro100_info[] = {
    {
S
Stefan Weil 已提交
1974 1975 1976
        .qdev.name = "i82550",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82550_init,
1977
        .exit      = pci_nic_uninit,
1978
        .romfile   = "gpxe-eepro100-80861209.rom",
1979 1980 1981 1982
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
1983
    },{
1984
        .qdev.name = "i82551",
1985
        .qdev.size = sizeof(EEPRO100State),
1986
        .init      = pci_i82551_init,
1987
        .exit      = pci_nic_uninit,
1988
        .romfile   = "gpxe-eepro100-80861209.rom",
1989 1990 1991 1992
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
1993 1994 1995 1996
    },{
        .qdev.name = "i82557a",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82557a_init,
1997
        .exit      = pci_nic_uninit,
1998
        .romfile   = "gpxe-eepro100-80861229.rom",
1999 2000 2001 2002
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
2003 2004
    },{
        .qdev.name = "i82557b",
2005
        .qdev.size = sizeof(EEPRO100State),
2006
        .init      = pci_i82557b_init,
2007
        .exit      = pci_nic_uninit,
2008
        .romfile   = "gpxe-eepro100-80861229.rom",
2009 2010 2011 2012
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2013 2014 2015 2016
    },{
        .qdev.name = "i82557c",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82557c_init,
2017
        .exit      = pci_nic_uninit,
2018
        .romfile   = "gpxe-eepro100-80861229.rom",
2019 2020 2021 2022
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2023 2024 2025 2026
    },{
        .qdev.name = "i82558a",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82558a_init,
2027
        .exit      = pci_nic_uninit,
2028
        .romfile   = "gpxe-eepro100-80861229.rom",
2029 2030 2031 2032
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2033 2034 2035 2036
    },{
        .qdev.name = "i82558b",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82558b_init,
2037
        .exit      = pci_nic_uninit,
2038
        .romfile   = "gpxe-eepro100-80861229.rom",
2039 2040 2041 2042
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2043 2044 2045 2046
    },{
        .qdev.name = "i82559a",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82559a_init,
2047
        .exit      = pci_nic_uninit,
2048
        .romfile   = "gpxe-eepro100-80861229.rom",
2049 2050 2051 2052
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2053 2054 2055 2056
    },{
        .qdev.name = "i82559b",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82559b_init,
2057
        .exit      = pci_nic_uninit,
2058
        .romfile   = "gpxe-eepro100-80861229.rom",
2059 2060 2061 2062
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2063 2064 2065 2066
    },{
        .qdev.name = "i82559c",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82559c_init,
2067
        .exit      = pci_nic_uninit,
2068
        .romfile   = "gpxe-eepro100-80861229.rom",
2069 2070 2071 2072
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
2073 2074
    },{
        .qdev.name = "i82559er",
2075
        .qdev.size = sizeof(EEPRO100State),
2076
        .init      = pci_i82559er_init,
2077
        .exit      = pci_nic_uninit,
2078
        .romfile   = "gpxe-eepro100-80861209.rom",
2079 2080 2081 2082
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
S
Stefan Weil 已提交
2083 2084 2085 2086
    },{
        .qdev.name = "i82562",
        .qdev.size = sizeof(EEPRO100State),
        .init      = pci_i82562_init,
2087
        .exit      = pci_nic_uninit,
2088
        .romfile   = "gpxe-eepro100-80861209.rom",
2089 2090 2091 2092
        .qdev.props = (Property[]) {
            DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
            DEFINE_PROP_END_OF_LIST(),
        },
2093 2094 2095 2096 2097
    },{
        /* end of list */
    }
};

P
Paul Brook 已提交
2098
static void eepro100_register_devices(void)
T
ths 已提交
2099
{
2100
    pci_qdev_register_many(eepro100_info);
T
ths 已提交
2101 2102
}

P
Paul Brook 已提交
2103
device_init(eepro100_register_devices)