eepro100.c 68.4 KB
Newer Older
T
ths 已提交
1 2 3
/*
 * QEMU i8255x (PRO100) emulation
 *
4
 * Copyright (C) 2006-2011 Stefan Weil
T
ths 已提交
5 6 7 8
 *
 * Portions of the code are copies from grub / etherboot eepro100.c
 * and linux e100.c.
 *
S
Stefan Weil 已提交
9
 * This program is free software: you can redistribute it and/or modify
T
ths 已提交
10
 * it under the terms of the GNU General Public License as published by
S
Stefan Weil 已提交
11 12
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) version 3 or any later version.
T
ths 已提交
13 14 15 16 17 18 19
 *
 * 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
S
Stefan Weil 已提交
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 guest, i386 / mips / mipsel / ppc host) ok
T
ths 已提交
24 25 26 27 28 29 30 31 32
 *      Linux networking (i386) ok
 *
 * Untested:
 *      Windows networking
 *
 * References:
 *
 * Intel 8255x 10/100 Mbps Ethernet Controller Family
 * Open Source Software Developer Manual
S
Stefan Weil 已提交
33 34 35 36 37 38 39 40
 *
 * TODO:
 *      * PHY emulation should be separated from nic emulation.
 *        Most nic emulations could share the same phy code.
 *      * i82550 is untested. It is programmed like the i82559.
 *      * i82562 is untested. It is programmed like the i82559.
 *      * Power management (i82558 and later) is not implemented.
 *      * Wake-on-LAN is not implemented.
T
ths 已提交
41 42 43
 */

#include <stddef.h>             /* offsetof */
P
pbrook 已提交
44 45 46
#include "hw.h"
#include "pci.h"
#include "net.h"
T
ths 已提交
47
#include "eeprom93xx.h"
48
#include "sysemu.h"
49
#include "dma.h"
T
ths 已提交
50

51 52 53 54 55 56 57 58 59
/* QEMU sends frames smaller than 60 bytes to ethernet nics.
 * Such frames are rejected by real nics and their emulations.
 * To avoid this behaviour, other nic emulations pad received
 * frames. The following definition enables this padding for
 * eepro100, too. We keep the define around in case it might
 * become useful the future if the core networking is ever
 * changed to pad short packets itself. */
#define CONFIG_PAD_RECEIVED_FRAMES

T
ths 已提交
60 61
#define KiB 1024

62
/* Debug EEPRO100 card. */
63 64 65
#if 0
# define DEBUG_EEPRO100
#endif
T
ths 已提交
66 67

#ifdef DEBUG_EEPRO100
68
#define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
T
ths 已提交
69
#else
70
#define logout(fmt, ...) ((void)0)
T
ths 已提交
71 72 73
#endif

/* Set flags to 0 to disable debug output. */
74 75 76 77 78
#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 已提交
79 80 81

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

82
#define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
T
ths 已提交
83 84 85 86

#define MAX_ETH_FRAME_SIZE 1514

/* This driver supports several different devices which are declared here. */
S
Stefan Weil 已提交
87
#define i82550          0x82550
T
ths 已提交
88
#define i82551          0x82551
S
Stefan Weil 已提交
89
#define i82557A         0x82557a
T
ths 已提交
90 91
#define i82557B         0x82557b
#define i82557C         0x82557c
S
Stefan Weil 已提交
92
#define i82558A         0x82558a
T
ths 已提交
93
#define i82558B         0x82558b
S
Stefan Weil 已提交
94 95
#define i82559A         0x82559a
#define i82559B         0x82559b
T
ths 已提交
96 97 98
#define i82559C         0x82559c
#define i82559ER        0x82559e
#define i82562          0x82562
99
#define i82801          0x82801
T
ths 已提交
100

101
/* Use 64 word EEPROM. TODO: could be a runtime option. */
T
ths 已提交
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
S
Stefan Weil 已提交
124
#define  RU_ABORT       0x0004
T
ths 已提交
125 126 127 128 129
#define  RX_ADDR_LOAD   0x0006
#define  RX_RESUMENR    0x0007
#define INT_MASK        0x0100
#define DRVR_INT        0x0200  /* Driver generated interrupt. */

130
typedef struct {
131 132
    const char *name;
    const char *desc;
133 134 135 136 137
    uint16_t device_id;
    uint8_t revision;
    uint16_t subsystem_vendor_id;
    uint16_t subsystem_id;

138 139 140 141 142 143
    uint32_t device;
    uint8_t stats_size;
    bool has_extended_tcb_support;
    bool power_management;
} E100PCIDeviceInfo;

T
ths 已提交
144 145
/* Offsets to the various registers.
   All accesses need not be longword aligned. */
S
Stefan Weil 已提交
146
typedef enum {
147
    SCBStatus = 0,              /* Status Word. */
T
ths 已提交
148 149 150 151 152
    SCBAck = 1,
    SCBCmd = 2,                 /* Rx/Command Unit command and status. */
    SCBIntmask = 3,
    SCBPointer = 4,             /* General purpose pointer. */
    SCBPort = 8,                /* Misc. commands and operands.  */
153 154
    SCBflash = 12,              /* Flash memory control. */
    SCBeeprom = 14,             /* EEPROM control. */
T
ths 已提交
155 156
    SCBCtrlMDI = 16,            /* MDI interface control. */
    SCBEarlyRx = 20,            /* Early receive byte count. */
157 158 159 160
    SCBFlow = 24,               /* Flow Control. */
    SCBpmdr = 27,               /* Power Management Driver. */
    SCBgctrl = 28,              /* General Control. */
    SCBgstat = 29,              /* General Status. */
S
Stefan Weil 已提交
161
} E100RegisterOffset;
T
ths 已提交
162 163 164 165 166 167

/* A speedo3 transmit buffer descriptor with two buffers... */
typedef struct {
    uint16_t status;
    uint16_t command;
    uint32_t link;              /* void * */
S
Stefan Weil 已提交
168
    uint32_t tbd_array_addr;    /* transmit buffer descriptor array address. */
T
ths 已提交
169 170 171
    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 */
S
Stefan Weil 已提交
172 173 174 175 176 177 178
#if 0
    /* 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. */
#endif
A
Anthony Liguori 已提交
179
} eepro100_tx_t;
T
ths 已提交
180 181 182 183 184 185 186 187 188

/* 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;
189
    /* Ethernet frame data follows. */
A
Anthony Liguori 已提交
190
} eepro100_rx_t;
T
ths 已提交
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205
typedef enum {
    COMMAND_EL = BIT(15),
    COMMAND_S = BIT(14),
    COMMAND_I = BIT(13),
    COMMAND_NC = BIT(4),
    COMMAND_SF = BIT(3),
    COMMAND_CMD = BITS(2, 0),
} scb_command_bit;

typedef enum {
    STATUS_C = BIT(15),
    STATUS_OK = BIT(13),
} scb_status_bit;

T
ths 已提交
206 207
typedef struct {
    uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
208 209
             tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
             tx_multiple_collisions, tx_total_collisions;
T
ths 已提交
210
    uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
211 212
             rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
             rx_short_frame_errors;
T
ths 已提交
213 214
    uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
    uint16_t xmt_tco_frames, rcv_tco_frames;
215 216
    /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
    uint32_t reserved[4];
A
Anthony Liguori 已提交
217
} eepro100_stats_t;
T
ths 已提交
218 219 220 221 222 223 224

typedef enum {
    cu_idle = 0,
    cu_suspended = 1,
    cu_active = 2,
    cu_lpq_active = 2,
    cu_hqp_active = 3
A
Anthony Liguori 已提交
225
} cu_state_t;
T
ths 已提交
226 227 228 229 230 231

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

typedef struct {
235
    PCIDevice dev;
236 237
    /* Hash register (multicast mask array, multiple individual addresses). */
    uint8_t mult[8];
A
Avi Kivity 已提交
238 239 240
    MemoryRegion mmio_bar;
    MemoryRegion io_bar;
    MemoryRegion flash_bar;
241
    NICState *nic;
242
    NICConf conf;
T
ths 已提交
243 244
    uint8_t scb_stat;           /* SCB stat/ack byte */
    uint8_t int_stat;           /* PCI interrupt status */
245
    /* region must not be saved by nic_save. */
T
ths 已提交
246
    uint16_t mdimem[32];
A
Anthony Liguori 已提交
247
    eeprom_t *eeprom;
T
ths 已提交
248 249 250 251 252 253 254
    uint32_t device;            /* device variant */
    /* (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 已提交
255
    uint32_t statsaddr;         /* pointer to eepro100_stats_t */
256

257 258 259 260 261
    /* 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 */

262 263 264
    /* Statistical counters. Also used for wake-up packet (i82559). */
    eepro100_stats_t statistics;

S
Stefan Weil 已提交
265 266
    /* Data in mem is always in the byte order of the controller (le).
     * It must be dword aligned to allow direct access to 32 bit values. */
267
    uint8_t mem[PCI_MEM_SIZE] __attribute__((aligned(8)));
S
Stefan Weil 已提交
268

T
ths 已提交
269 270 271
    /* Configuration bytes. */
    uint8_t configuration[22];

J
Juan Quintela 已提交
272 273
    /* vmstate for each particular nic */
    VMStateDescription *vmstate;
274 275 276 277

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

S
Stefan Weil 已提交
280 281 282 283 284 285 286 287 288 289 290
/* 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;

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
/* Bit values for EEPROM ID word. */
typedef enum {
    EEPROM_ID_MDM = BIT(0),     /* Modem */
    EEPROM_ID_STB = BIT(1),     /* Standby Enable */
    EEPROM_ID_WMR = BIT(2),     /* ??? */
    EEPROM_ID_WOL = BIT(5),     /* Wake on LAN */
    EEPROM_ID_DPD = BIT(6),     /* Deep Power Down */
    EEPROM_ID_ALT = BIT(7),     /* */
    /* BITS(10, 8) device revision */
    EEPROM_ID_BD = BIT(11),     /* boot disable */
    EEPROM_ID_ID = BIT(13),     /* id bit */
    /* BITS(15, 14) signature */
    EEPROM_ID_VALID = BIT(14),  /* signature for valid eeprom */
} eeprom_id_bit;

T
ths 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/* 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,
};

325 326
static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s);

S
Stefan Weil 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/* Read a 16 bit control/status (CSR) register. */
static uint16_t e100_read_reg2(EEPRO100State *s, E100RegisterOffset addr)
{
    assert(!((uintptr_t)&s->mem[addr] & 1));
    return le16_to_cpup((uint16_t *)&s->mem[addr]);
}

/* Read a 32 bit control/status (CSR) register. */
static uint32_t e100_read_reg4(EEPRO100State *s, E100RegisterOffset addr)
{
    assert(!((uintptr_t)&s->mem[addr] & 3));
    return le32_to_cpup((uint32_t *)&s->mem[addr]);
}

/* Write a 16 bit control/status (CSR) register. */
static void e100_write_reg2(EEPRO100State *s, E100RegisterOffset addr,
                            uint16_t val)
{
    assert(!((uintptr_t)&s->mem[addr] & 1));
    cpu_to_le16w((uint16_t *)&s->mem[addr], val);
}

/* Read a 32 bit control/status (CSR) register. */
static void e100_write_reg4(EEPRO100State *s, E100RegisterOffset addr,
                            uint32_t val)
{
    assert(!((uintptr_t)&s->mem[addr] & 3));
    cpu_to_le32w((uint32_t *)&s->mem[addr], val);
}

T
ths 已提交
357 358 359 360 361
#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];
362
    if (size > 16) {
T
ths 已提交
363
        size = 16;
364
    }
T
ths 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    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) {
387
        TRACE(INT, logout("interrupt disabled\n"));
388
        qemu_irq_lower(s->dev.irq[0]);
T
ths 已提交
389 390 391 392 393 394 395
        s->int_stat = 0;
    }
}

static void enable_interrupt(EEPRO100State * s)
{
    if (!s->int_stat) {
396
        TRACE(INT, logout("interrupt enabled\n"));
397
        qemu_irq_raise(s->dev.irq[0]);
T
ths 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
        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);
    }
}

411
static void eepro100_interrupt(EEPRO100State * s, uint8_t status)
T
ths 已提交
412 413
{
    uint8_t mask = ~s->mem[SCBIntmask];
414 415 416
    s->mem[SCBAck] |= status;
    status = s->scb_stat = s->mem[SCBAck];
    status &= (mask | 0x0f);
S
Stefan Weil 已提交
417 418 419
#if 0
    status &= (~s->mem[SCBIntmask] | 0x0xf);
#endif
420
    if (status && (mask & 0x01)) {
T
ths 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        /* 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);
}

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

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

473
static void e100_pci_reset(EEPRO100State * s)
T
ths 已提交
474
{
475
    E100PCIDeviceInfo *info = eepro100_get_class(s);
T
ths 已提交
476
    uint32_t device = s->device;
477
    uint8_t *pci_conf = s->dev.config;
T
ths 已提交
478

479
    TRACE(OTHER, logout("%p\n", s));
T
ths 已提交
480 481

    /* PCI Status */
482 483
    pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
                                        PCI_STATUS_FAST_BACK);
T
ths 已提交
484
    /* PCI Latency Timer */
485
    pci_set_byte(pci_conf + PCI_LATENCY_TIMER, 0x20);   /* latency timer = 32 clocks */
486
    /* Capability Pointer is set by PCI framework. */
487 488 489
    /* Interrupt Line */
    /* Interrupt Pin */
    pci_set_byte(pci_conf + PCI_INTERRUPT_PIN, 1);      /* interrupt pin A */
T
ths 已提交
490
    /* Minimum Grant */
491
    pci_set_byte(pci_conf + PCI_MIN_GNT, 0x08);
T
ths 已提交
492
    /* Maximum Latency */
493
    pci_set_byte(pci_conf + PCI_MAX_LAT, 0x18);
T
ths 已提交
494

495 496
    s->stats_size = info->stats_size;
    s->has_extended_tcb_support = info->has_extended_tcb_support;
497

T
ths 已提交
498
    switch (device) {
499
    case i82550:
T
ths 已提交
500
    case i82551:
501
    case i82557A:
T
ths 已提交
502 503
    case i82557B:
    case i82557C:
504
    case i82558A:
T
ths 已提交
505
    case i82558B:
506 507
    case i82559A:
    case i82559B:
508 509
    case i82559ER:
    case i82562:
510
    case i82801:
T
ths 已提交
511 512 513 514 515 516
    case i82559C:
        break;
    default:
        logout("Device %X is undefined!\n", device);
    }

517 518 519
    /* Standard TxCB. */
    s->configuration[6] |= BIT(4);

520
    /* Standard statistical counters. */
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    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));

545
    if (info->power_management) {
546
        /* Power Management Capabilities */
547
        int cfg_offset = 0xdc;
548 549
        int r = pci_add_capability(&s->dev, PCI_CAP_ID_PM,
                                   cfg_offset, PCI_PM_SIZEOF);
550 551
        assert(r >= 0);
        pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21);
552
#if 0 /* TODO: replace dummy code for power management emulation. */
553 554 555 556
        /* TODO: Power Management Control / Status. */
        pci_set_word(pci_conf + cfg_offset + PCI_PM_CTRL, 0x0000);
        /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
        pci_set_byte(pci_conf + cfg_offset + PCI_PM_PPB_EXTENSIONS, 0x0000);
557
#endif
558 559 560
    }

#if EEPROM_SIZE > 0
T
ths 已提交
561
    if (device == i82557C || device == i82558B || device == i82559C) {
S
Stefan Weil 已提交
562 563 564 565 566 567 568 569 570 571
        /*
        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 已提交
572 573
        logout("Get device id and revision from EEPROM!!!\n");
    }
574
#endif /* EEPROM_SIZE > 0 */
T
ths 已提交
575 576 577 578 579 580
}

static void nic_selective_reset(EEPRO100State * s)
{
    size_t i;
    uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
S
Stefan Weil 已提交
581 582 583
#if 0
    eeprom93xx_reset(s->eeprom);
#endif
584
    memcpy(eeprom_contents, s->conf.macaddr.a, 6);
585
    eeprom_contents[EEPROM_ID] = EEPROM_ID_VALID;
586 587
    if (s->device == i82557B || s->device == i82557C)
        eeprom_contents[5] = 0x0100;
S
Stefan Weil 已提交
588
    eeprom_contents[EEPROM_PHY_ID] = 1;
T
ths 已提交
589 590 591 592 593
    uint16_t sum = 0;
    for (i = 0; i < EEPROM_SIZE - 1; i++) {
        sum += eeprom_contents[i];
    }
    eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
594
    TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
T
ths 已提交
595 596

    memset(s->mem, 0, sizeof(s->mem));
S
Stefan Weil 已提交
597
    e100_write_reg4(s, SCBCtrlMDI, BIT(21));
T
ths 已提交
598 599 600 601 602 603 604

    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)
{
605
    EEPRO100State *s = opaque;
606
    TRACE(OTHER, logout("%p\n", s));
607
    /* TODO: Clearing of hash register for selective reset, too? */
S
Stefan Weil 已提交
608
    memset(&s->mult[0], 0, sizeof(s->mult));
T
ths 已提交
609 610 611 612
    nic_selective_reset(s);
}

#if defined(DEBUG_EEPRO100)
S
Stefan Weil 已提交
613
static const char * const e100_reg[PCI_IO_SIZE / 4] = {
T
ths 已提交
614 615 616 617 618 619
    "Command/Status",
    "General Pointer",
    "Port",
    "EEPROM/Flash Control",
    "MDI Control",
    "Receive DMA Byte Count",
S
Stefan Weil 已提交
620
    "Flow Control",
T
ths 已提交
621 622 623 624 625
    "General Status/Control"
};

static char *regname(uint32_t addr)
{
626
    static char buf[32];
T
ths 已提交
627
    if (addr < PCI_IO_SIZE) {
S
Stefan Weil 已提交
628
        const char *r = e100_reg[addr / 4];
T
ths 已提交
629
        if (r != 0) {
630
            snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
T
ths 已提交
631
        } else {
632
            snprintf(buf, sizeof(buf), "0x%02x", addr);
T
ths 已提交
633 634
        }
    } else {
635
        snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
T
ths 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    }
    return buf;
}
#endif                          /* DEBUG_EEPRO100 */

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

#if 0
static uint16_t eepro100_read_command(EEPRO100State * s)
{
    uint16_t val = 0xffff;
S
Stefan Weil 已提交
651
    TRACE(OTHER, logout("val=0x%04x\n", val));
T
ths 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    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 已提交
673
static cu_state_t get_cu_state(EEPRO100State * s)
T
ths 已提交
674
{
675
    return ((s->mem[SCBStatus] & BITS(7, 6)) >> 6);
T
ths 已提交
676 677
}

A
Anthony Liguori 已提交
678
static void set_cu_state(EEPRO100State * s, cu_state_t state)
T
ths 已提交
679
{
680
    s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(7, 6)) + (state << 6);
T
ths 已提交
681 682
}

A
Anthony Liguori 已提交
683
static ru_state_t get_ru_state(EEPRO100State * s)
T
ths 已提交
684
{
685
    return ((s->mem[SCBStatus] & BITS(5, 2)) >> 2);
T
ths 已提交
686 687
}

A
Anthony Liguori 已提交
688
static void set_ru_state(EEPRO100State * s, ru_state_t state)
T
ths 已提交
689
{
690
    s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(5, 2)) + (state << 2);
T
ths 已提交
691 692 693 694 695 696 697 698 699
}

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!!!
     */
700
    pci_dma_write(&s->dev, s->statsaddr, &s->statistics, s->stats_size);
701 702 703 704 705 706 707 708
    stl_le_pci_dma(&s->dev, s->statsaddr + 0,
                   s->statistics.tx_good_frames);
    stl_le_pci_dma(&s->dev, s->statsaddr + 36,
                   s->statistics.rx_good_frames);
    stl_le_pci_dma(&s->dev, s->statsaddr + 48,
                   s->statistics.rx_resource_errors);
    stl_le_pci_dma(&s->dev, s->statsaddr + 60,
                   s->statistics.rx_short_frame_errors);
S
Stefan Weil 已提交
709
#if 0
710 711
    stw_le_pci_dma(&s->dev, s->statsaddr + 76, s->statistics.xmt_tco_frames);
    stw_le_pci_dma(&s->dev, s->statsaddr + 78, s->statistics.rcv_tco_frames);
S
Stefan Weil 已提交
712 713
    missing("CU dump statistical counters");
#endif
T
ths 已提交
714 715
}

716 717
static void read_cb(EEPRO100State *s)
{
718
    pci_dma_read(&s->dev, s->cb_address, &s->tx, sizeof(s->tx));
719 720 721 722 723 724 725
    s->tx.status = le16_to_cpu(s->tx.status);
    s->tx.command = le16_to_cpu(s->tx.command);
    s->tx.link = le32_to_cpu(s->tx.link);
    s->tx.tbd_array_addr = le32_to_cpu(s->tx.tbd_array_addr);
    s->tx.tcb_bytes = le16_to_cpu(s->tx.tcb_bytes);
}

726 727
static void tx_command(EEPRO100State *s)
{
S
Stefan Weil 已提交
728
    uint32_t tbd_array = le32_to_cpu(s->tx.tbd_array_addr);
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    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) {
748 749
        uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
        uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
S
Stefan Weil 已提交
750
#if 0
751
        uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6);
S
Stefan Weil 已提交
752
#endif
753 754 755 756 757
        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);
758
        pci_dma_read(&s->dev, tx_buffer_address, &buf[size], tx_buffer_size);
759 760 761 762 763 764 765 766 767 768
        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++) {
769 770 771 772 773 774
                uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev,
                                                            tbd_address);
                uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev,
                                                          tbd_address + 4);
                uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev,
                                                        tbd_address + 6);
775 776 777 778 779
                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);
780 781
                pci_dma_read(&s->dev, tx_buffer_address,
                             &buf[size], tx_buffer_size);
782 783 784 785 786 787 788 789
                size += tx_buffer_size;
                if (tx_buffer_el & 1) {
                    break;
                }
            }
        }
        tbd_address = tbd_array;
        for (; tbd_count < s->tx.tbd_count; tbd_count++) {
790 791 792
            uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
            uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
            uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6);
793 794 795 796 797
            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);
798 799
            pci_dma_read(&s->dev, tx_buffer_address,
                         &buf[size], tx_buffer_size);
800 801 802 803 804 805 806 807 808 809 810
            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. */
S
Stefan Weil 已提交
811 812 813
#if 0
    eepro100_cx_interrupt(s);
#endif
814 815
}

S
Stefan Weil 已提交
816 817 818 819 820 821 822 823
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];
824
        pci_dma_read(&s->dev, s->cb_address + 10 + i, multicast_addr, 6);
S
Stefan Weil 已提交
825 826 827 828 829 830 831
        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 已提交
832
static void action_command(EEPRO100State *s)
T
ths 已提交
833
{
S
Stefan Weil 已提交
834
    for (;;) {
835 836 837 838
        bool bit_el;
        bool bit_s;
        bool bit_i;
        bool bit_nc;
S
Stefan Weil 已提交
839
        uint16_t ok_status = STATUS_OK;
840 841 842 843 844 845 846 847 848 849 850 851 852 853
        s->cb_address = s->cu_base + s->cu_offset;
        read_cb(s);
        bit_el = ((s->tx.command & COMMAND_EL) != 0);
        bit_s = ((s->tx.command & COMMAND_S) != 0);
        bit_i = ((s->tx.command & COMMAND_I) != 0);
        bit_nc = ((s->tx.command & COMMAND_NC) != 0);
#if 0
        bool bit_sf = ((s->tx.command & COMMAND_SF) != 0);
#endif
        s->cu_offset = s->tx.link;
        TRACE(OTHER,
              logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
                     s->tx.status, s->tx.command, s->tx.link));
        switch (s->tx.command & COMMAND_CMD) {
T
ths 已提交
854 855 856 857
        case CmdNOp:
            /* Do nothing. */
            break;
        case CmdIASetup:
858
            pci_dma_read(&s->dev, s->cb_address + 8, &s->conf.macaddr.a[0], 6);
859
            TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)));
T
ths 已提交
860 861
            break;
        case CmdConfigure:
862 863
            pci_dma_read(&s->dev, s->cb_address + 8,
                         &s->configuration[0], sizeof(s->configuration));
864 865 866 867 868 869 870 871
            TRACE(OTHER, logout("configuration: %s\n",
                                nic_dump(&s->configuration[0], 16)));
            TRACE(OTHER, logout("configuration: %s\n",
                                nic_dump(&s->configuration[16],
                                ARRAY_SIZE(s->configuration) - 16)));
            if (s->configuration[20] & BIT(6)) {
                TRACE(OTHER, logout("Multiple IA bit\n"));
            }
T
ths 已提交
872 873
            break;
        case CmdMulticastList:
S
Stefan Weil 已提交
874
            set_multicast_list(s);
T
ths 已提交
875 876
            break;
        case CmdTx:
877 878
            if (bit_nc) {
                missing("CmdTx: NC = 0");
S
Stefan Weil 已提交
879
                ok_status = 0;
880 881
                break;
            }
882
            tx_command(s);
T
ths 已提交
883 884
            break;
        case CmdTDR:
885
            TRACE(OTHER, logout("load microcode\n"));
T
ths 已提交
886 887 888
            /* Starting with offset 8, the command contains
             * 64 dwords microcode which we just ignore here. */
            break;
S
Stefan Weil 已提交
889 890 891 892 893
        case CmdDiagnose:
            TRACE(OTHER, logout("diagnose\n"));
            /* Make sure error flag is not set. */
            s->tx.status = 0;
            break;
T
ths 已提交
894 895
        default:
            missing("undefined command");
S
Stefan Weil 已提交
896
            ok_status = 0;
897
            break;
T
ths 已提交
898
        }
899
        /* Write new status. */
900 901
        stw_le_pci_dma(&s->dev, s->cb_address,
                       s->tx.status | ok_status | STATUS_C);
T
ths 已提交
902 903 904 905 906
        if (bit_i) {
            /* CU completed action. */
            eepro100_cx_interrupt(s);
        }
        if (bit_el) {
907
            /* CU becomes idle. Terminate command loop. */
T
ths 已提交
908 909
            set_cu_state(s, cu_idle);
            eepro100_cna_interrupt(s);
S
Stefan Weil 已提交
910
            break;
T
ths 已提交
911
        } else if (bit_s) {
S
Stefan Weil 已提交
912
            /* CU becomes suspended. Terminate command loop. */
T
ths 已提交
913 914
            set_cu_state(s, cu_suspended);
            eepro100_cna_interrupt(s);
S
Stefan Weil 已提交
915
            break;
T
ths 已提交
916 917
        } else {
            /* More entries in list. */
918
            TRACE(OTHER, logout("CU list with at least one more entry\n"));
T
ths 已提交
919
        }
S
Stefan Weil 已提交
920 921 922 923 924 925 926
    }
    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)
{
S
Stefan Weil 已提交
927
    cu_state_t cu_state;
S
Stefan Weil 已提交
928 929 930 931 932
    switch (val) {
    case CU_NOP:
        /* No operation. */
        break;
    case CU_START:
S
Stefan Weil 已提交
933 934 935 936 937
        cu_state = get_cu_state(s);
        if (cu_state != cu_idle && cu_state != cu_suspended) {
            /* Intel documentation says that CU must be idle or suspended
             * for the CU start command. */
            logout("unexpected CU state is %u\n", cu_state);
S
Stefan Weil 已提交
938 939
        }
        set_cu_state(s, cu_active);
940
        s->cu_offset = e100_read_reg4(s, SCBPointer);
S
Stefan Weil 已提交
941
        action_command(s);
T
ths 已提交
942 943 944 945 946 947
        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. */
S
Stefan Weil 已提交
948 949 950
#if 0
            missing("cu resume");
#endif
T
ths 已提交
951 952 953
            set_cu_state(s, cu_suspended);
        }
        if (get_cu_state(s) == cu_suspended) {
954
            TRACE(OTHER, logout("CU resuming\n"));
T
ths 已提交
955
            set_cu_state(s, cu_active);
S
Stefan Weil 已提交
956
            action_command(s);
T
ths 已提交
957 958 959 960
        }
        break;
    case CU_STATSADDR:
        /* Load dump counters address. */
961
        s->statsaddr = e100_read_reg4(s, SCBPointer);
962 963 964 965 966 967 968 969 970
        TRACE(OTHER, logout("val=0x%02x (dump counters address)\n", val));
        if (s->statsaddr & 3) {
            /* Memory must be Dword aligned. */
            logout("unaligned dump counters address\n");
            /* Handling of misaligned addresses is undefined.
             * Here we align the address by ignoring the lower bits. */
            /* TODO: Test unaligned dump counter address on real hardware. */
            s->statsaddr &= ~3;
        }
T
ths 已提交
971 972 973
        break;
    case CU_SHOWSTATS:
        /* Dump statistical counters. */
974
        TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
T
ths 已提交
975
        dump_statistics(s);
976
        stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa005);
T
ths 已提交
977 978 979
        break;
    case CU_CMD_BASE:
        /* Load CU base. */
980
        TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
981
        s->cu_base = e100_read_reg4(s, SCBPointer);
T
ths 已提交
982 983 984
        break;
    case CU_DUMPSTATS:
        /* Dump and reset statistical counters. */
985
        TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
T
ths 已提交
986
        dump_statistics(s);
987
        stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa007);
T
ths 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
        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);
S
Stefan Weil 已提交
1009 1010 1011
#if 0
            assert(!"wrong RU state");
#endif
T
ths 已提交
1012 1013
        }
        set_ru_state(s, ru_ready);
1014
        s->ru_offset = e100_read_reg4(s, SCBPointer);
1015
        TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
T
ths 已提交
1016 1017 1018 1019 1020 1021
        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);
S
Stefan Weil 已提交
1022 1023 1024
#if 0
            assert(!"wrong RU state");
#endif
T
ths 已提交
1025 1026 1027
        }
        set_ru_state(s, ru_ready);
        break;
S
Stefan Weil 已提交
1028 1029 1030 1031 1032 1033 1034
    case RU_ABORT:
        /* RU abort. */
        if (get_ru_state(s) == ru_ready) {
            eepro100_rnr_interrupt(s);
        }
        set_ru_state(s, ru_idle);
        break;
T
ths 已提交
1035 1036
    case RX_ADDR_LOAD:
        /* Load RU base. */
1037
        TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
1038
        s->ru_base = e100_read_reg4(s, SCBPointer);
T
ths 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
        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) {
1051
        TRACE(OTHER, logout("val=0x%02x\n", val));
T
ths 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    }
    /* 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)
{
S
Stefan Weil 已提交
1070
    uint16_t val = e100_read_reg2(s, SCBeeprom);
T
ths 已提交
1071 1072 1073 1074 1075
    if (eeprom93xx_read(s->eeprom)) {
        val |= EEPROM_DO;
    } else {
        val &= ~EEPROM_DO;
    }
1076
    TRACE(EEPROM, logout("val=0x%04x\n", val));
T
ths 已提交
1077 1078 1079
    return val;
}

A
Anthony Liguori 已提交
1080
static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
T
ths 已提交
1081
{
1082
    TRACE(EEPROM, logout("val=0x%02x\n", val));
T
ths 已提交
1083

S
Stefan Weil 已提交
1084
    /* mask unwritable bits */
S
Stefan Weil 已提交
1085 1086 1087
#if 0
    val = SET_MASKED(val, 0x31, eeprom->value);
#endif
T
ths 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

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

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

#if defined(DEBUG_EEPRO100)
1102
static const char * const mdi_op_name[] = {
T
ths 已提交
1103 1104 1105 1106 1107 1108
    "opcode 0",
    "write",
    "read",
    "opcode 3"
};

1109
static const char * const mdi_reg_name[] = {
T
ths 已提交
1110 1111 1112 1113 1114 1115 1116 1117
    "Control",
    "Status",
    "PHY Identification (Word 1)",
    "PHY Identification (Word 2)",
    "Auto-Negotiation Advertisement",
    "Auto-Negotiation Link Partner Ability",
    "Auto-Negotiation Expansion"
};
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

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 已提交
1130 1131 1132 1133
#endif                          /* DEBUG_EEPRO100 */

static uint32_t eepro100_read_mdi(EEPRO100State * s)
{
S
Stefan Weil 已提交
1134
    uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
T
ths 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

#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,
1147
                      reg2name(reg), data));
T
ths 已提交
1148 1149 1150
    return val;
}

1151
static void eepro100_write_mdi(EEPRO100State *s)
T
ths 已提交
1152
{
1153
    uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
T
ths 已提交
1154 1155 1156 1157 1158
    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));
1159 1160
    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 已提交
1161 1162
    if (phy != 1) {
        /* Unsupported PHY address. */
S
Stefan Weil 已提交
1163 1164 1165
#if 0
        logout("phy must be 1 but is %u\n", phy);
#endif
T
ths 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        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,
1178
                          reg2name(reg), data));
T
ths 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 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
        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;
S
Stefan Weil 已提交
1244
    e100_write_reg4(s, SCBCtrlMDI, val);
T
ths 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
}

/*****************************************************************************
 *
 * 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 已提交
1262
} eepro100_selftest_t;
T
ths 已提交
1263 1264 1265 1266 1267 1268

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

1269
static void eepro100_write_port(EEPRO100State *s)
T
ths 已提交
1270
{
1271
    uint32_t val = e100_read_reg4(s, SCBPort);
T
ths 已提交
1272 1273 1274 1275 1276 1277 1278
    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:
1279
        TRACE(OTHER, logout("selftest address=0x%08x\n", address));
A
Anthony Liguori 已提交
1280
        eepro100_selftest_t data;
1281
        pci_dma_read(&s->dev, address, (uint8_t *) &data, sizeof(data));
T
ths 已提交
1282 1283
        data.st_sign = 0xffffffff;
        data.st_result = 0;
1284
        pci_dma_write(&s->dev, address, (uint8_t *) &data, sizeof(data));
T
ths 已提交
1285 1286
        break;
    case PORT_SELECTIVE_RESET:
1287
        TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
T
ths 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
        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)
{
1304
    uint8_t val = 0;
T
ths 已提交
1305
    if (addr <= sizeof(s->mem) - sizeof(val)) {
S
Stefan Weil 已提交
1306
        val = s->mem[addr];
T
ths 已提交
1307 1308 1309 1310 1311
    }

    switch (addr) {
    case SCBStatus:
    case SCBAck:
1312
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1313 1314
        break;
    case SCBCmd:
1315
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
S
Stefan Weil 已提交
1316 1317 1318
#if 0
        val = eepro100_read_command(s);
#endif
T
ths 已提交
1319 1320
        break;
    case SCBIntmask:
1321
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1322 1323
        break;
    case SCBPort + 3:
1324
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1325 1326 1327 1328
        break;
    case SCBeeprom:
        val = eepro100_read_eeprom(s);
        break;
1329 1330 1331 1332 1333 1334 1335
    case SCBCtrlMDI:
    case SCBCtrlMDI + 1:
    case SCBCtrlMDI + 2:
    case SCBCtrlMDI + 3:
        val = (uint8_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        break;
1336
    case SCBpmdr:       /* Power Management Driver Register */
T
ths 已提交
1337
        val = 0;
1338
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1339
        break;
1340 1341 1342
    case SCBgctrl:      /* General Control Register */
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        break;
1343
    case SCBgstat:      /* General Status Register */
T
ths 已提交
1344 1345
        /* 100 Mbps full duplex, valid link */
        val = 0x07;
1346
        TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
T
ths 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
        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)
{
1357
    uint16_t val = 0;
T
ths 已提交
1358
    if (addr <= sizeof(s->mem) - sizeof(val)) {
S
Stefan Weil 已提交
1359
        val = e100_read_reg2(s, addr);
T
ths 已提交
1360 1361 1362 1363
    }

    switch (addr) {
    case SCBStatus:
1364
    case SCBCmd:
1365
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1366 1367 1368
        break;
    case SCBeeprom:
        val = eepro100_read_eeprom(s);
1369
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1370
        break;
1371 1372 1373 1374 1375
    case SCBCtrlMDI:
    case SCBCtrlMDI + 2:
        val = (uint16_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        break;
T
ths 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384
    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)
{
1385
    uint32_t val = 0;
T
ths 已提交
1386
    if (addr <= sizeof(s->mem) - sizeof(val)) {
S
Stefan Weil 已提交
1387
        val = e100_read_reg4(s, addr);
T
ths 已提交
1388 1389 1390 1391
    }

    switch (addr) {
    case SCBStatus:
1392
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1393 1394
        break;
    case SCBPointer:
1395
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1396 1397 1398
        break;
    case SCBPort:
        val = eepro100_read_port(s);
1399
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1400
        break;
1401 1402 1403 1404
    case SCBflash:
        val = eepro100_read_eeprom(s);
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
        break;
T
ths 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    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)
{
1417 1418
    /* SCBStatus is readonly. */
    if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
S
Stefan Weil 已提交
1419
        s->mem[addr] = val;
T
ths 已提交
1420 1421 1422 1423
    }

    switch (addr) {
    case SCBStatus:
1424
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1425 1426
        break;
    case SCBAck:
1427
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1428 1429 1430
        eepro100_acknowledge(s);
        break;
    case SCBCmd:
1431
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1432 1433 1434
        eepro100_write_command(s, val);
        break;
    case SCBIntmask:
1435
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1436 1437 1438 1439 1440
        if (val & BIT(1)) {
            eepro100_swi_interrupt(s);
        }
        eepro100_interrupt(s, 0);
        break;
1441 1442 1443 1444 1445 1446
    case SCBPointer:
    case SCBPointer + 1:
    case SCBPointer + 2:
    case SCBPointer + 3:
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        break;
1447 1448 1449 1450 1451
    case SCBPort:
    case SCBPort + 1:
    case SCBPort + 2:
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        break;
T
ths 已提交
1452
    case SCBPort + 3:
1453 1454 1455
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        eepro100_write_port(s);
        break;
1456
    case SCBFlow:       /* does not exist on 82557 */
T
ths 已提交
1457 1458
    case SCBFlow + 1:
    case SCBFlow + 2:
1459
    case SCBpmdr:       /* does not exist on 82557 */
1460
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1461 1462
        break;
    case SCBeeprom:
1463
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
T
ths 已提交
1464 1465
        eepro100_write_eeprom(s->eeprom, val);
        break;
1466 1467 1468 1469 1470 1471 1472 1473 1474
    case SCBCtrlMDI:
    case SCBCtrlMDI + 1:
    case SCBCtrlMDI + 2:
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        break;
    case SCBCtrlMDI + 3:
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
        eepro100_write_mdi(s);
        break;
T
ths 已提交
1475 1476 1477 1478 1479 1480 1481 1482
    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)
{
1483 1484
    /* SCBStatus is readonly. */
    if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
S
Stefan Weil 已提交
1485
        e100_write_reg2(s, addr, val);
T
ths 已提交
1486 1487 1488 1489
    }

    switch (addr) {
    case SCBStatus:
1490
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1491
        s->mem[SCBAck] = (val >> 8);
T
ths 已提交
1492 1493 1494
        eepro100_acknowledge(s);
        break;
    case SCBCmd:
1495
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1496 1497 1498
        eepro100_write_command(s, val);
        eepro100_write1(s, SCBIntmask, val >> 8);
        break;
1499 1500 1501 1502
    case SCBPointer:
    case SCBPointer + 2:
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        break;
1503 1504 1505 1506 1507 1508 1509
    case SCBPort:
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        break;
    case SCBPort + 2:
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        eepro100_write_port(s);
        break;
T
ths 已提交
1510
    case SCBeeprom:
1511
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
T
ths 已提交
1512 1513
        eepro100_write_eeprom(s->eeprom, val);
        break;
1514 1515 1516 1517 1518 1519 1520
    case SCBCtrlMDI:
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        break;
    case SCBCtrlMDI + 2:
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
        eepro100_write_mdi(s);
        break;
T
ths 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529
    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)) {
S
Stefan Weil 已提交
1530
        e100_write_reg4(s, addr, val);
T
ths 已提交
1531 1532 1533 1534
    }

    switch (addr) {
    case SCBPointer:
1535
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
T
ths 已提交
1536 1537
        break;
    case SCBPort:
1538
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1539
        eepro100_write_port(s);
T
ths 已提交
1540
        break;
1541 1542 1543 1544 1545
    case SCBflash:
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
        val = val >> 16;
        eepro100_write_eeprom(s->eeprom, val);
        break;
T
ths 已提交
1546
    case SCBCtrlMDI:
1547 1548
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
        eepro100_write_mdi(s);
T
ths 已提交
1549 1550 1551 1552 1553 1554 1555
        break;
    default:
        logout("addr=%s val=0x%08x\n", regname(addr), val);
        missing("unknown longword write");
    }
}

A
Avi Kivity 已提交
1556 1557
static uint64_t eepro100_read(void *opaque, target_phys_addr_t addr,
                              unsigned size)
T
ths 已提交
1558 1559 1560
{
    EEPRO100State *s = opaque;

A
Avi Kivity 已提交
1561 1562 1563 1564 1565 1566
    switch (size) {
    case 1: return eepro100_read1(s, addr);
    case 2: return eepro100_read2(s, addr);
    case 4: return eepro100_read4(s, addr);
    default: abort();
    }
T
ths 已提交
1567 1568
}

A
Avi Kivity 已提交
1569 1570
static void eepro100_write(void *opaque, target_phys_addr_t addr,
                           uint64_t data, unsigned size)
T
ths 已提交
1571 1572 1573
{
    EEPRO100State *s = opaque;

A
Avi Kivity 已提交
1574 1575 1576 1577 1578 1579
    switch (size) {
    case 1: return eepro100_write1(s, addr, data);
    case 2: return eepro100_write2(s, addr, data);
    case 4: return eepro100_write4(s, addr, data);
    default: abort();
    }
T
ths 已提交
1580 1581
}

A
Avi Kivity 已提交
1582 1583 1584 1585
static const MemoryRegionOps eepro100_ops = {
    .read = eepro100_read,
    .write = eepro100_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
T
ths 已提交
1586 1587
};

1588
static int nic_can_receive(VLANClientState *nc)
T
ths 已提交
1589
{
1590
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1591
    TRACE(RXTX, logout("%p\n", s));
T
ths 已提交
1592
    return get_ru_state(s) == ru_ready;
S
Stefan Weil 已提交
1593 1594 1595
#if 0
    return !eepro100_buffer_full(s);
#endif
T
ths 已提交
1596 1597
}

1598
static ssize_t nic_receive(VLANClientState *nc, const uint8_t * buf, size_t size)
T
ths 已提交
1599 1600 1601 1602 1603
{
    /* TODO:
     * - Magic packets should set bit 30 in power management driver register.
     * - Interesting packets should set bit 29 in power management driver register.
     */
1604
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
T
ths 已提交
1605
    uint16_t rfd_status = 0xa000;
1606 1607 1608
#if defined(CONFIG_PAD_RECEIVED_FRAMES)
    uint8_t min_buf[60];
#endif
T
ths 已提交
1609 1610 1611
    static const uint8_t broadcast_macaddr[6] =
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
#if defined(CONFIG_PAD_RECEIVED_FRAMES)
    /* Pad to minimum Ethernet frame length */
    if (size < sizeof(min_buf)) {
        memcpy(min_buf, buf, size);
        memset(&min_buf[size], 0, sizeof(min_buf) - size);
        buf = min_buf;
        size = sizeof(min_buf);
    }
#endif

T
ths 已提交
1622 1623 1624
    if (s->configuration[8] & 0x80) {
        /* CSMA is disabled. */
        logout("%p received while CSMA is disabled\n", s);
1625
        return -1;
1626
#if !defined(CONFIG_PAD_RECEIVED_FRAMES)
1627
    } else if (size < 64 && (s->configuration[7] & BIT(0))) {
T
ths 已提交
1628 1629
        /* Short frame and configuration byte 7/0 (discard short receive) set:
         * Short frame is discarded */
1630
        logout("%p received short frame (%zu byte)\n", s, size);
T
ths 已提交
1631
        s->statistics.rx_short_frame_errors++;
S
Stefan Weil 已提交
1632 1633
        return -1;
#endif
1634
    } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & BIT(3))) {
T
ths 已提交
1635 1636
        /* Long frame and configuration byte 18/3 (long receive ok) not set:
         * Long frames are discarded. */
1637
        logout("%p received long frame (%zu byte), ignored\n", s, size);
1638
        return -1;
S
Stefan Weil 已提交
1639
    } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) {       /* !!! */
T
ths 已提交
1640 1641
        /* Frame matches individual address. */
        /* TODO: check configuration byte 15/4 (ignore U/L). */
1642
        TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
T
ths 已提交
1643 1644
    } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
        /* Broadcast frame. */
1645
        TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
T
ths 已提交
1646
        rfd_status |= 0x0002;
S
Stefan Weil 已提交
1647
    } else if (buf[0] & 0x01) {
T
ths 已提交
1648
        /* Multicast frame. */
S
Stefan Weil 已提交
1649
        TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size)));
1650
        if (s->configuration[21] & BIT(3)) {
S
Stefan Weil 已提交
1651 1652 1653 1654 1655 1656
          /* 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. */
1657
          } else if (s->configuration[15] & BIT(0)) {
S
Stefan Weil 已提交
1658 1659 1660 1661 1662 1663
              /* Promiscuous: receive all. */
              rfd_status |= 0x0004;
          } else {
              TRACE(RXTX, logout("%p multicast ignored\n", s));
              return -1;
          }
T
ths 已提交
1664
        }
S
Stefan Weil 已提交
1665
        /* TODO: Next not for promiscuous mode? */
T
ths 已提交
1666
        rfd_status |= 0x0002;
1667
    } else if (s->configuration[15] & BIT(0)) {
T
ths 已提交
1668
        /* Promiscuous: receive all. */
1669
        TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
T
ths 已提交
1670
        rfd_status |= 0x0004;
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    } else if (s->configuration[20] & BIT(6)) {
        /* Multiple IA bit set. */
        unsigned mcast_idx = compute_mcast_idx(buf);
        assert(mcast_idx < 64);
        if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
            TRACE(RXTX, logout("%p accepted, multiple IA bit set\n", s));
        } else {
            TRACE(RXTX, logout("%p frame ignored, multiple IA bit set\n", s));
            return -1;
        }
T
ths 已提交
1681
    } else {
1682
        TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1683
              nic_dump(buf, size)));
1684
        return size;
T
ths 已提交
1685 1686 1687
    }

    if (get_ru_state(s) != ru_ready) {
1688 1689
        /* No resources available. */
        logout("no resources, state=%u\n", get_ru_state(s));
S
Stefan Weil 已提交
1690 1691
        /* TODO: RNR interrupt only at first failed frame? */
        eepro100_rnr_interrupt(s);
T
ths 已提交
1692
        s->statistics.rx_resource_errors++;
S
Stefan Weil 已提交
1693 1694 1695
#if 0
        assert(!"no resources");
#endif
1696
        return -1;
T
ths 已提交
1697
    }
S
Stefan Weil 已提交
1698
    /* !!! */
A
Anthony Liguori 已提交
1699
    eepro100_rx_t rx;
1700
    pci_dma_read(&s->dev, s->ru_base + s->ru_offset,
1701
                 &rx, sizeof(eepro100_rx_t));
T
ths 已提交
1702 1703
    uint16_t rfd_command = le16_to_cpu(rx.command);
    uint16_t rfd_size = le16_to_cpu(rx.size);
1704 1705 1706 1707 1708 1709

    if (size > rfd_size) {
        logout("Receive buffer (%" PRId16 " bytes) too small for data "
            "(%zu bytes); data truncated\n", rfd_size, size);
        size = rfd_size;
    }
1710
#if !defined(CONFIG_PAD_RECEIVED_FRAMES)
T
ths 已提交
1711 1712 1713
    if (size < 64) {
        rfd_status |= 0x0080;
    }
1714
#endif
1715 1716
    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));
1717 1718 1719 1720
    stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
                offsetof(eepro100_rx_t, status), rfd_status);
    stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
                offsetof(eepro100_rx_t, count), size);
T
ths 已提交
1721
    /* Early receive interrupt not supported. */
S
Stefan Weil 已提交
1722 1723 1724
#if 0
    eepro100_er_interrupt(s);
#endif
T
ths 已提交
1725
    /* Receive CRC Transfer not supported. */
1726
    if (s->configuration[18] & BIT(2)) {
1727 1728 1729
        missing("Receive CRC Transfer");
        return -1;
    }
T
ths 已提交
1730
    /* TODO: check stripping enable bit. */
S
Stefan Weil 已提交
1731 1732 1733
#if 0
    assert(!(s->configuration[17] & BIT(0)));
#endif
1734 1735
    pci_dma_write(&s->dev, s->ru_base + s->ru_offset +
                  sizeof(eepro100_rx_t), buf, size);
T
ths 已提交
1736 1737 1738
    s->statistics.rx_good_frames++;
    eepro100_fr_interrupt(s);
    s->ru_offset = le32_to_cpu(rx.link);
1739
    if (rfd_command & COMMAND_EL) {
T
ths 已提交
1740
        /* EL bit is set, so this was the last frame. */
1741 1742
        logout("receive: Running out of frames\n");
        set_ru_state(s, ru_suspended);
T
ths 已提交
1743
    }
1744
    if (rfd_command & COMMAND_S) {
T
ths 已提交
1745 1746 1747
        /* S bit is set. */
        set_ru_state(s, ru_suspended);
    }
1748
    return size;
T
ths 已提交
1749 1750
}

J
Juan Quintela 已提交
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
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(cu_base, EEPRO100State),
        VMSTATE_UINT32(cu_offset, EEPRO100State),
        VMSTATE_UINT32(ru_base, EEPRO100State),
        VMSTATE_UINT32(ru_offset, EEPRO100State),
        VMSTATE_UINT32(statsaddr, EEPRO100State),
1775
        /* Save eepro100_stats_t statistics. */
J
Juan Quintela 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
        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),
        /* Configuration bytes. */
        VMSTATE_BUFFER(configuration, EEPRO100State),
        VMSTATE_END_OF_LIST()
1800
    }
J
Juan Quintela 已提交
1801
};
T
ths 已提交
1802

1803
static void nic_cleanup(VLANClientState *nc)
1804
{
1805
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1806

1807
    s->nic = NULL;
1808 1809
}

S
Stefan Weil 已提交
1810
static int pci_nic_uninit(PCIDevice *pci_dev)
1811
{
S
Stefan Weil 已提交
1812
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1813

A
Avi Kivity 已提交
1814 1815 1816
    memory_region_destroy(&s->mmio_bar);
    memory_region_destroy(&s->io_bar);
    memory_region_destroy(&s->flash_bar);
A
Alex Williamson 已提交
1817
    vmstate_unregister(&pci_dev->qdev, s->vmstate, s);
1818
    eeprom93xx_free(&pci_dev->qdev, s->eeprom);
1819
    qemu_del_vlan_client(&s->nic->nc);
1820 1821 1822
    return 0;
}

1823 1824 1825 1826 1827 1828 1829 1830
static NetClientInfo net_eepro100_info = {
    .type = NET_CLIENT_TYPE_NIC,
    .size = sizeof(NICState),
    .can_receive = nic_can_receive,
    .receive = nic_receive,
    .cleanup = nic_cleanup,
};

1831
static int e100_nic_init(PCIDevice *pci_dev)
T
ths 已提交
1832
{
1833
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1834
    E100PCIDeviceInfo *info = eepro100_get_class(s);
T
ths 已提交
1835

1836
    TRACE(OTHER, logout("\n"));
T
ths 已提交
1837

1838
    s->device = info->device;
T
ths 已提交
1839

1840
    e100_pci_reset(s);
T
ths 已提交
1841 1842 1843

    /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
     * i82559 and later support 64 or 256 word EEPROM. */
1844
    s->eeprom = eeprom93xx_new(&pci_dev->qdev, EEPROM_SIZE);
T
ths 已提交
1845 1846

    /* Handler for memory-mapped I/O */
A
Avi Kivity 已提交
1847 1848
    memory_region_init_io(&s->mmio_bar, &eepro100_ops, s, "eepro100-mmio",
                          PCI_MEM_SIZE);
1849
    pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->mmio_bar);
A
Avi Kivity 已提交
1850 1851
    memory_region_init_io(&s->io_bar, &eepro100_ops, s, "eepro100-io",
                          PCI_IO_SIZE);
1852
    pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
A
Avi Kivity 已提交
1853 1854 1855
    /* FIXME: flash aliases to mmio?! */
    memory_region_init_io(&s->flash_bar, &eepro100_ops, s, "eepro100-flash",
                          PCI_FLASH_SIZE);
1856
    pci_register_bar(&s->dev, 2, 0, &s->flash_bar);
T
ths 已提交
1857

1858
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
1859
    logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
T
ths 已提交
1860 1861 1862

    nic_reset(s);

1863
    s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
1864
                          object_get_typename(OBJECT(pci_dev)), pci_dev->qdev.id, s);
T
ths 已提交
1865

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

1869
    qemu_register_reset(nic_reset, s);
T
ths 已提交
1870

1871
    s->vmstate = g_malloc(sizeof(vmstate_eepro100));
J
Juan Quintela 已提交
1872
    memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
1873
    s->vmstate->name = s->nic->nc.model;
A
Alex Williamson 已提交
1874
    vmstate_register(&pci_dev->qdev, -1, s->vmstate, s);
S
Stefan Weil 已提交
1875

1876 1877
    add_boot_device_path(s->conf.bootindex, &pci_dev->qdev, "/ethernet-phy@0");

1878
    return 0;
T
ths 已提交
1879 1880
}

1881
static E100PCIDeviceInfo e100_devices[] = {
1882
    {
1883 1884
        .name = "i82550",
        .desc = "Intel i82550 Ethernet",
1885 1886
        .device = i82550,
        /* TODO: check device id. */
1887
        .device_id = PCI_DEVICE_ID_INTEL_82551IT,
1888
        /* Revision ID: 0x0c, 0x0d, 0x0e. */
1889
        .revision = 0x0e,
1890 1891 1892 1893 1894
        /* TODO: check size of statistical counters. */
        .stats_size = 80,
        /* TODO: check extended tcb support. */
        .has_extended_tcb_support = true,
        .power_management = true,
S
Stefan Weil 已提交
1895
    },{
1896 1897
        .name = "i82551",
        .desc = "Intel i82551 Ethernet",
1898
        .device = i82551,
1899
        .device_id = PCI_DEVICE_ID_INTEL_82551IT,
1900
        /* Revision ID: 0x0f, 0x10. */
1901
        .revision = 0x0f,
1902 1903 1904 1905
        /* TODO: check size of statistical counters. */
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
1906
    },{
1907 1908
        .name = "i82557a",
        .desc = "Intel i82557A Ethernet",
1909
        .device = i82557A,
1910 1911
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x01,
1912
        .power_management = false,
S
Stefan Weil 已提交
1913
    },{
1914 1915
        .name = "i82557b",
        .desc = "Intel i82557B Ethernet",
1916
        .device = i82557B,
1917 1918
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x02,
1919
        .power_management = false,
S
Stefan Weil 已提交
1920
    },{
1921 1922
        .name = "i82557c",
        .desc = "Intel i82557C Ethernet",
1923
        .device = i82557C,
1924 1925
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x03,
1926
        .power_management = false,
S
Stefan Weil 已提交
1927
    },{
1928 1929
        .name = "i82558a",
        .desc = "Intel i82558A Ethernet",
1930
        .device = i82558A,
1931 1932
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x04,
1933 1934 1935
        .stats_size = 76,
        .has_extended_tcb_support = true,
        .power_management = true,
S
Stefan Weil 已提交
1936
    },{
1937 1938
        .name = "i82558b",
        .desc = "Intel i82558B Ethernet",
1939
        .device = i82558B,
1940 1941
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x05,
1942 1943 1944
        .stats_size = 76,
        .has_extended_tcb_support = true,
        .power_management = true,
S
Stefan Weil 已提交
1945
    },{
1946 1947
        .name = "i82559a",
        .desc = "Intel i82559A Ethernet",
1948
        .device = i82559A,
1949 1950
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x06,
1951 1952 1953
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
S
Stefan Weil 已提交
1954
    },{
1955 1956
        .name = "i82559b",
        .desc = "Intel i82559B Ethernet",
1957
        .device = i82559B,
1958 1959
        .device_id = PCI_DEVICE_ID_INTEL_82557,
        .revision = 0x07,
1960 1961 1962
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
1963
    },{
1964 1965
        .name = "i82559c",
        .desc = "Intel i82559C Ethernet",
1966
        .device = i82559C,
1967
        .device_id = PCI_DEVICE_ID_INTEL_82557,
1968
#if 0
1969
        .revision = 0x08,
1970 1971
#endif
        /* TODO: Windows wants revision id 0x0c. */
1972
        .revision = 0x0c,
1973
#if EEPROM_SIZE > 0
1974 1975
        .subsystem_vendor_id = PCI_VENDOR_ID_INTEL,
        .subsystem_id = 0x0040,
1976
#endif
1977 1978 1979
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
S
Stefan Weil 已提交
1980
    },{
1981 1982
        .name = "i82559er",
        .desc = "Intel i82559ER Ethernet",
1983
        .device = i82559ER,
1984 1985
        .device_id = PCI_DEVICE_ID_INTEL_82551IT,
        .revision = 0x09,
1986 1987 1988
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
1989
    },{
1990 1991
        .name = "i82562",
        .desc = "Intel i82562 Ethernet",
1992 1993
        .device = i82562,
        /* TODO: check device id. */
1994
        .device_id = PCI_DEVICE_ID_INTEL_82551IT,
1995
        /* TODO: wrong revision id. */
1996
        .revision = 0x0e,
1997 1998 1999
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
2000 2001
    },{
        /* Toshiba Tecra 8200. */
2002 2003
        .name = "i82801",
        .desc = "Intel i82801 Ethernet",
2004
        .device = i82801,
2005 2006
        .device_id = 0x2449,
        .revision = 0x03,
2007 2008 2009
        .stats_size = 80,
        .has_extended_tcb_support = true,
        .power_management = true,
2010 2011 2012
    }
};

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
static E100PCIDeviceInfo *eepro100_get_class_by_name(const char *typename)
{
    E100PCIDeviceInfo *info = NULL;
    int i;

    /* This is admittedly awkward but also temporary.  QOM allows for
     * parameterized typing and for subclassing both of which would suitable
     * handle what's going on here.  But class_data is already being used as
     * a stop-gap hack to allow incremental qdev conversion so we cannot use it
     * right now.  Once we merge the final QOM series, we can come back here and
     * do this in a much more elegant fashion.
     */
    for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
2026
        if (strcmp(e100_devices[i].name, typename) == 0) {
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
            info = &e100_devices[i];
            break;
        }
    }
    assert(info != NULL);

    return info;
}

static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s)
{
    return eepro100_get_class_by_name(object_get_typename(OBJECT(s)));
}

2041 2042 2043 2044 2045
static Property e100_properties[] = {
    DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
    DEFINE_PROP_END_OF_LIST(),
};

2046 2047
static void eepro100_class_init(ObjectClass *klass, void *data)
{
2048
    DeviceClass *dc = DEVICE_CLASS(klass);
2049 2050 2051 2052 2053
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
    E100PCIDeviceInfo *info;

    info = eepro100_get_class_by_name(object_class_get_name(klass));

2054 2055
    dc->props = e100_properties;
    dc->desc = info->desc;
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
    k->vendor_id = PCI_VENDOR_ID_INTEL;
    k->class_id = PCI_CLASS_NETWORK_ETHERNET;
    k->romfile = "pxe-eepro100.rom";
    k->init = e100_nic_init;
    k->exit = pci_nic_uninit;
    k->device_id = info->device_id;
    k->revision = info->revision;
    k->subsystem_vendor_id = info->subsystem_vendor_id;
    k->subsystem_id = info->subsystem_id;
}

A
Andreas Färber 已提交
2067
static void eepro100_register_types(void)
T
ths 已提交
2068
{
2069 2070
    size_t i;
    for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
2071 2072
        TypeInfo type_info = {};
        E100PCIDeviceInfo *info = &e100_devices[i];
2073

2074 2075 2076 2077
        type_info.name = info->name;
        type_info.parent = TYPE_PCI_DEVICE;
        type_info.class_init = eepro100_class_init;
        type_info.instance_size = sizeof(EEPRO100State);
2078
        
2079
        type_register(&type_info);
2080
    }
T
ths 已提交
2081 2082
}

A
Andreas Färber 已提交
2083
type_init(eepro100_register_types)