Statistics
| Branch: | Revision:

root / hw / eepro100.c @ 0908bba1

History | View | Annotate | Download (67.9 kB)

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

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

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

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

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

    
56
#define KiB 1024
57

    
58
/* Debug EEPRO100 card. */
59
#if 0
60
# define DEBUG_EEPRO100
61
#endif
62

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

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

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

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

    
80
#define MAX_ETH_FRAME_SIZE 1514
81

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

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

    
99
#define PCI_MEM_SIZE            (4 * KiB)
100
#define PCI_IO_SIZE             64
101
#define PCI_FLASH_SIZE          (128 * KiB)
102

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

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

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

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

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

    
160
/* Receive frame descriptor. */
161
typedef struct {
162
    int16_t status;
163
    uint16_t command;
164
    uint32_t link;              /* struct RxFD * */
165
    uint32_t rx_buf_addr;       /* void * */
166
    uint16_t count;
167
    uint16_t size;
168
    char packet[MAX_ETH_FRAME_SIZE + 4];
169
} eepro100_rx_t;
170

    
171
typedef struct {
172
    uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
173
        tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
174
        tx_multiple_collisions, tx_total_collisions;
175
    uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
176
        rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
177
        rx_short_frame_errors;
178
    uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
179
    uint16_t xmt_tco_frames, rcv_tco_frames;
180
    /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
181
    uint32_t reserved[4];
182
} eepro100_stats_t;
183

    
184
typedef enum {
185
    cu_idle = 0,
186
    cu_suspended = 1,
187
    cu_active = 2,
188
    cu_lpq_active = 2,
189
    cu_hqp_active = 3
190
} cu_state_t;
191

    
192
typedef enum {
193
    ru_idle = 0,
194
    ru_suspended = 1,
195
    ru_no_resources = 2,
196
    ru_ready = 4
197
} ru_state_t;
198

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

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

    
226
    /* Statistical counters. Also used for wake-up packet (i82559). */
227
    eepro100_stats_t statistics;
228

    
229
#if 0
230
    uint16_t status;
231
#endif
232

    
233
    /* Configuration bytes. */
234
    uint8_t configuration[22];
235

    
236
    /* Data in mem is always in the byte order of the controller (le). */
237
    uint8_t mem[PCI_MEM_SIZE];
238
    /* vmstate for each particular nic */
239
    VMStateDescription *vmstate;
240

    
241
    /* Quasi static device properties (no need to save them). */
242
    uint16_t stats_size;
243
    bool has_extended_tcb_support;
244
} EEPRO100State;
245

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

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

    
265
/* XXX: optimize */
266
static void stl_le_phys(target_phys_addr_t addr, uint32_t val)
267
{
268
    val = cpu_to_le32(val);
269
    cpu_physical_memory_write(addr, (const uint8_t *)&val, sizeof(val));
270
}
271

    
272
#define POLYNOMIAL 0x04c11db6
273

    
274
/* From FreeBSD */
275
/* XXX: optimize */
276
static unsigned compute_mcast_idx(const uint8_t * ep)
277
{
278
    uint32_t crc;
279
    int carry, i, j;
280
    uint8_t b;
281

    
282
    crc = 0xffffffff;
283
    for (i = 0; i < 6; i++) {
284
        b = *ep++;
285
        for (j = 0; j < 8; j++) {
286
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
287
            crc <<= 1;
288
            b >>= 1;
289
            if (carry) {
290
                crc = ((crc ^ POLYNOMIAL) | carry);
291
            }
292
        }
293
    }
294
    return (crc & BITS(7, 2)) >> 2;
295
}
296

    
297
#if defined(DEBUG_EEPRO100)
298
static const char *nic_dump(const uint8_t * buf, unsigned size)
299
{
300
    static char dump[3 * 16 + 1];
301
    char *p = &dump[0];
302
    if (size > 16) {
303
        size = 16;
304
    }
305
    while (size-- > 0) {
306
        p += sprintf(p, " %02x", *buf++);
307
    }
308
    return dump;
309
}
310
#endif                          /* DEBUG_EEPRO100 */
311

    
312
enum scb_stat_ack {
313
    stat_ack_not_ours = 0x00,
314
    stat_ack_sw_gen = 0x04,
315
    stat_ack_rnr = 0x10,
316
    stat_ack_cu_idle = 0x20,
317
    stat_ack_frame_rx = 0x40,
318
    stat_ack_cu_cmd_done = 0x80,
319
    stat_ack_not_present = 0xFF,
320
    stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
321
    stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
322
};
323

    
324
static void disable_interrupt(EEPRO100State * s)
325
{
326
    if (s->int_stat) {
327
        TRACE(INT, logout("interrupt disabled\n"));
328
        qemu_irq_lower(s->dev.irq[0]);
329
        s->int_stat = 0;
330
    }
331
}
332

    
333
static void enable_interrupt(EEPRO100State * s)
334
{
335
    if (!s->int_stat) {
336
        TRACE(INT, logout("interrupt enabled\n"));
337
        qemu_irq_raise(s->dev.irq[0]);
338
        s->int_stat = 1;
339
    }
340
}
341

    
342
static void eepro100_acknowledge(EEPRO100State * s)
343
{
344
    s->scb_stat &= ~s->mem[SCBAck];
345
    s->mem[SCBAck] = s->scb_stat;
346
    if (s->scb_stat == 0) {
347
        disable_interrupt(s);
348
    }
349
}
350

    
351
static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
352
{
353
    uint8_t mask = ~s->mem[SCBIntmask];
354
    s->mem[SCBAck] |= stat;
355
    stat = s->scb_stat = s->mem[SCBAck];
356
    stat &= (mask | 0x0f);
357
    //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
358
    if (stat && (mask & 0x01)) {
359
        /* SCB mask and SCB Bit M do not disable interrupt. */
360
        enable_interrupt(s);
361
    } else if (s->int_stat) {
362
        disable_interrupt(s);
363
    }
364
}
365

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

    
373
static void eepro100_cna_interrupt(EEPRO100State * s)
374
{
375
    /* CU left the active state. */
376
    eepro100_interrupt(s, 0x20);
377
}
378

    
379
static void eepro100_fr_interrupt(EEPRO100State * s)
380
{
381
    /* RU received a complete frame. */
382
    eepro100_interrupt(s, 0x40);
383
}
384

    
385
#if 0
386
static void eepro100_rnr_interrupt(EEPRO100State * s)
387
{
388
    /* RU is not ready. */
389
    eepro100_interrupt(s, 0x10);
390
}
391
#endif
392

    
393
static void eepro100_mdi_interrupt(EEPRO100State * s)
394
{
395
    /* MDI completed read or write cycle. */
396
    eepro100_interrupt(s, 0x08);
397
}
398

    
399
static void eepro100_swi_interrupt(EEPRO100State * s)
400
{
401
    /* Software has requested an interrupt. */
402
    eepro100_interrupt(s, 0x04);
403
}
404

    
405
#if 0
406
static void eepro100_fcp_interrupt(EEPRO100State * s)
407
{
408
    /* Flow control pause interrupt (82558 and later). */
409
    eepro100_interrupt(s, 0x01);
410
}
411
#endif
412

    
413
static void pci_reset(EEPRO100State * s)
414
{
415
    uint32_t device = s->device;
416
    uint8_t *pci_conf = s->dev.config;
417
    bool power_management = 1;
418

    
419
    TRACE(OTHER, logout("%p\n", s));
420

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

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

    
580
    s->configuration[6] |= BIT(5);
581

    
582
    if (s->stats_size == 80) {
583
        /* TODO: check TCO Statistical Counters bit. Documentation not clear. */
584
        if (s->configuration[6] & BIT(2)) {
585
            /* TCO statistical counters. */
586
            assert(s->configuration[6] & BIT(5));
587
        } else {
588
            if (s->configuration[6] & BIT(5)) {
589
                /* No extended statistical counters, i82557 compatible. */
590
                s->stats_size = 64;
591
            } else {
592
                /* i82558 compatible. */
593
                s->stats_size = 76;
594
            }
595
        }
596
    } else {
597
        if (s->configuration[6] & BIT(5)) {
598
            /* No extended statistical counters. */
599
            s->stats_size = 64;
600
        }
601
    }
602
    assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics));
603

    
604
    if (power_management) {
605
        /* Power Management Capabilities */
606
        PCI_CONFIG_8(0xdc, 0x01);
607
        /* Next Item Pointer */
608
        /* Capability ID */
609
        PCI_CONFIG_16(0xde, 0x7e21);
610
        /* TODO: Power Management Control / Status. */
611
        /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
612
    }
613

    
614
#if EEPROM_SIZE > 0
615
    if (device == i82557C || device == i82558B || device == i82559C) {
616
        // TODO: get vendor id from EEPROM for i82557C or later.
617
        // TODO: get device id from EEPROM for i82557C or later.
618
        // TODO: status bit 4 can be disabled by EEPROM for i82558, i82559.
619
        // TODO: header type is determined by EEPROM for i82559.
620
        // TODO: get subsystem id from EEPROM for i82557C or later.
621
        // TODO: get subsystem vendor id from EEPROM for i82557C or later.
622
        // TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later.
623
        // TODO: capability pointer depends on EEPROM for i82558.
624
        logout("Get device id and revision from EEPROM!!!\n");
625
    }
626
#endif /* EEPROM_SIZE > 0 */
627
}
628

    
629
static void nic_selective_reset(EEPRO100State * s)
630
{
631
    size_t i;
632
    uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
633
    //~ eeprom93xx_reset(s->eeprom);
634
    memcpy(eeprom_contents, s->conf.macaddr.a, 6);
635
    eeprom_contents[0xa] = 0x4000;
636
    if (s->device == i82557B || s->device == i82557C)
637
        eeprom_contents[5] = 0x0100;
638
    uint16_t sum = 0;
639
    for (i = 0; i < EEPROM_SIZE - 1; i++) {
640
        sum += eeprom_contents[i];
641
    }
642
    eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
643
    TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
644

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

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

    
653
static void nic_reset(void *opaque)
654
{
655
    EEPRO100State *s = opaque;
656
    TRACE(OTHER, logout("%p\n", s));
657
    /* TODO: Clearing of multicast table for selective reset, too? */
658
    memset(&s->mult[0], 0, sizeof(s->mult));
659
    nic_selective_reset(s);
660
}
661

    
662
#if defined(DEBUG_EEPRO100)
663
static const char * const e100_reg[PCI_IO_SIZE / 4] = {
664
    "Command/Status",
665
    "General Pointer",
666
    "Port",
667
    "EEPROM/Flash Control",
668
    "MDI Control",
669
    "Receive DMA Byte Count",
670
    "Flow Control",
671
    "General Status/Control"
672
};
673

    
674
static char *regname(uint32_t addr)
675
{
676
    static char buf[32];
677
    if (addr < PCI_IO_SIZE) {
678
        const char *r = e100_reg[addr / 4];
679
        if (r != 0) {
680
            snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
681
        } else {
682
            snprintf(buf, sizeof(buf), "0x%02x", addr);
683
        }
684
    } else {
685
        snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
686
    }
687
    return buf;
688
}
689
#endif                          /* DEBUG_EEPRO100 */
690

    
691
#if 0
692
static uint16_t eepro100_read_status(EEPRO100State * s)
693
{
694
    uint16_t val = s->status;
695
    TRACE(OTHER, logout("val=0x%04x\n", val));
696
    return val;
697
}
698

699
static void eepro100_write_status(EEPRO100State * s, uint16_t val)
700
{
701
    TRACE(OTHER, logout("val=0x%04x\n", val));
702
    s->status = val;
703
}
704
#endif
705

    
706
/*****************************************************************************
707
 *
708
 * Command emulation.
709
 *
710
 ****************************************************************************/
711

    
712
#if 0
713
static uint16_t eepro100_read_command(EEPRO100State * s)
714
{
715
    uint16_t val = 0xffff;
716
    //~ TRACE(OTHER, logout("val=0x%04x\n", val));
717
    return val;
718
}
719
#endif
720

    
721
/* Commands that can be put in a command list entry. */
722
enum commands {
723
    CmdNOp = 0,
724
    CmdIASetup = 1,
725
    CmdConfigure = 2,
726
    CmdMulticastList = 3,
727
    CmdTx = 4,
728
    CmdTDR = 5,                 /* load microcode */
729
    CmdDump = 6,
730
    CmdDiagnose = 7,
731

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

    
738
static cu_state_t get_cu_state(EEPRO100State * s)
739
{
740
    return ((s->mem[SCBStatus] >> 6) & 0x03);
741
}
742

    
743
static void set_cu_state(EEPRO100State * s, cu_state_t state)
744
{
745
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
746
}
747

    
748
static ru_state_t get_ru_state(EEPRO100State * s)
749
{
750
    return ((s->mem[SCBStatus] >> 2) & 0x0f);
751
}
752

    
753
static void set_ru_state(EEPRO100State * s, ru_state_t state)
754
{
755
    s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
756
}
757

    
758
static void dump_statistics(EEPRO100State * s)
759
{
760
    /* Dump statistical data. Most data is never changed by the emulation
761
     * and always 0, so we first just copy the whole block and then those
762
     * values which really matter.
763
     * Number of data should check configuration!!!
764
     */
765
    cpu_physical_memory_write(s->statsaddr,
766
                              (uint8_t *) & s->statistics, s->stats_size);
767
    stl_le_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
768
    stl_le_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
769
    stl_le_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
770
    stl_le_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
771
    //~ stw_le_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
772
    //~ stw_le_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
773
    //~ missing("CU dump statistical counters");
774
}
775

    
776
static void tx_command(EEPRO100State *s)
777
{
778
    uint32_t tbd_array = le32_to_cpu(s->tx.tbd_array_addr);
779
    uint16_t tcb_bytes = (le16_to_cpu(s->tx.tcb_bytes) & 0x3fff);
780
    /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */
781
    uint8_t buf[2600];
782
    uint16_t size = 0;
783
    uint32_t tbd_address = s->cb_address + 0x10;
784
    TRACE(RXTX, logout
785
        ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
786
         tbd_array, tcb_bytes, s->tx.tbd_count));
787

    
788
    if (tcb_bytes > 2600) {
789
        logout("TCB byte count too large, using 2600\n");
790
        tcb_bytes = 2600;
791
    }
792
    if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
793
        logout
794
            ("illegal values of TBD array address and TCB byte count!\n");
795
    }
796
    assert(tcb_bytes <= sizeof(buf));
797
    while (size < tcb_bytes) {
798
        uint32_t tx_buffer_address = ldl_phys(tbd_address);
799
        uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
800
        //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
801
        tbd_address += 8;
802
        TRACE(RXTX, logout
803
            ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
804
             tx_buffer_address, tx_buffer_size));
805
        tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
806
        cpu_physical_memory_read(tx_buffer_address, &buf[size],
807
                                 tx_buffer_size);
808
        size += tx_buffer_size;
809
    }
810
    if (tbd_array == 0xffffffff) {
811
        /* Simplified mode. Was already handled by code above. */
812
    } else {
813
        /* Flexible mode. */
814
        uint8_t tbd_count = 0;
815
        if (s->has_extended_tcb_support && !(s->configuration[6] & BIT(4))) {
816
            /* Extended Flexible TCB. */
817
            for (; tbd_count < 2; tbd_count++) {
818
                uint32_t tx_buffer_address = ldl_phys(tbd_address);
819
                uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
820
                uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
821
                tbd_address += 8;
822
                TRACE(RXTX, logout
823
                    ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
824
                     tx_buffer_address, tx_buffer_size));
825
                tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
826
                cpu_physical_memory_read(tx_buffer_address, &buf[size],
827
                                         tx_buffer_size);
828
                size += tx_buffer_size;
829
                if (tx_buffer_el & 1) {
830
                    break;
831
                }
832
            }
833
        }
834
        tbd_address = tbd_array;
835
        for (; tbd_count < s->tx.tbd_count; tbd_count++) {
836
            uint32_t tx_buffer_address = ldl_phys(tbd_address);
837
            uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
838
            uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
839
            tbd_address += 8;
840
            TRACE(RXTX, logout
841
                ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
842
                 tx_buffer_address, tx_buffer_size));
843
            tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
844
            cpu_physical_memory_read(tx_buffer_address, &buf[size],
845
                                     tx_buffer_size);
846
            size += tx_buffer_size;
847
            if (tx_buffer_el & 1) {
848
                break;
849
            }
850
        }
851
    }
852
    TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
853
    qemu_send_packet(&s->nic->nc, buf, size);
854
    s->statistics.tx_good_frames++;
855
    /* Transmit with bad status would raise an CX/TNO interrupt.
856
     * (82557 only). Emulation never has bad status. */
857
    //~ eepro100_cx_interrupt(s);
858
}
859

    
860
static void set_multicast_list(EEPRO100State *s)
861
{
862
    uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0);
863
    uint16_t i;
864
    memset(&s->mult[0], 0, sizeof(s->mult));
865
    TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count));
866
    for (i = 0; i < multicast_count; i += 6) {
867
        uint8_t multicast_addr[6];
868
        cpu_physical_memory_read(s->cb_address + 10 + i, multicast_addr, 6);
869
        TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6)));
870
        unsigned mcast_idx = compute_mcast_idx(multicast_addr);
871
        assert(mcast_idx < 64);
872
        s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
873
    }
874
}
875

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

    
952
static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
953
{
954
    switch (val) {
955
    case CU_NOP:
956
        /* No operation. */
957
        break;
958
    case CU_START:
959
        if (get_cu_state(s) != cu_idle) {
960
            /* Intel documentation says that CU must be idle for the CU
961
             * start command. Intel driver for Linux also starts the CU
962
             * from suspended state. */
963
            logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
964
            //~ assert(!"wrong CU state");
965
        }
966
        set_cu_state(s, cu_active);
967
        s->cu_offset = s->pointer;
968
        action_command(s);
969
        break;
970
    case CU_RESUME:
971
        if (get_cu_state(s) != cu_suspended) {
972
            logout("bad CU resume from CU state %u\n", get_cu_state(s));
973
            /* Workaround for bad Linux eepro100 driver which resumes
974
             * from idle state. */
975
            //~ missing("cu resume");
976
            set_cu_state(s, cu_suspended);
977
        }
978
        if (get_cu_state(s) == cu_suspended) {
979
            TRACE(OTHER, logout("CU resuming\n"));
980
            set_cu_state(s, cu_active);
981
            action_command(s);
982
        }
983
        break;
984
    case CU_STATSADDR:
985
        /* Load dump counters address. */
986
        s->statsaddr = s->pointer;
987
        TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
988
        break;
989
    case CU_SHOWSTATS:
990
        /* Dump statistical counters. */
991
        TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
992
        dump_statistics(s);
993
        stl_le_phys(s->statsaddr + s->stats_size, 0xa005);
994
        break;
995
    case CU_CMD_BASE:
996
        /* Load CU base. */
997
        TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
998
        s->cu_base = s->pointer;
999
        break;
1000
    case CU_DUMPSTATS:
1001
        /* Dump and reset statistical counters. */
1002
        TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
1003
        dump_statistics(s);
1004
        stl_le_phys(s->statsaddr + s->stats_size, 0xa007);
1005
        memset(&s->statistics, 0, sizeof(s->statistics));
1006
        break;
1007
    case CU_SRESUME:
1008
        /* CU static resume. */
1009
        missing("CU static resume");
1010
        break;
1011
    default:
1012
        missing("Undefined CU command");
1013
    }
1014
}
1015

    
1016
static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
1017
{
1018
    switch (val) {
1019
    case RU_NOP:
1020
        /* No operation. */
1021
        break;
1022
    case RX_START:
1023
        /* RU start. */
1024
        if (get_ru_state(s) != ru_idle) {
1025
            logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
1026
            //~ assert(!"wrong RU state");
1027
        }
1028
        set_ru_state(s, ru_ready);
1029
        s->ru_offset = s->pointer;
1030
        TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
1031
        break;
1032
    case RX_RESUME:
1033
        /* Restart RU. */
1034
        if (get_ru_state(s) != ru_suspended) {
1035
            logout("RU state is %u, should be %u\n", get_ru_state(s),
1036
                   ru_suspended);
1037
            //~ assert(!"wrong RU state");
1038
        }
1039
        set_ru_state(s, ru_ready);
1040
        break;
1041
    case RX_ADDR_LOAD:
1042
        /* Load RU base. */
1043
        TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
1044
        s->ru_base = s->pointer;
1045
        break;
1046
    default:
1047
        logout("val=0x%02x (undefined RU command)\n", val);
1048
        missing("Undefined SU command");
1049
    }
1050
}
1051

    
1052
static void eepro100_write_command(EEPRO100State * s, uint8_t val)
1053
{
1054
    eepro100_ru_command(s, val & 0x0f);
1055
    eepro100_cu_command(s, val & 0xf0);
1056
    if ((val) == 0) {
1057
        TRACE(OTHER, logout("val=0x%02x\n", val));
1058
    }
1059
    /* Clear command byte after command was accepted. */
1060
    s->mem[SCBCmd] = 0;
1061
}
1062

    
1063
/*****************************************************************************
1064
 *
1065
 * EEPROM emulation.
1066
 *
1067
 ****************************************************************************/
1068

    
1069
#define EEPROM_CS       0x02
1070
#define EEPROM_SK       0x01
1071
#define EEPROM_DI       0x04
1072
#define EEPROM_DO       0x08
1073

    
1074
static uint16_t eepro100_read_eeprom(EEPRO100State * s)
1075
{
1076
    uint16_t val;
1077
    memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
1078
    if (eeprom93xx_read(s->eeprom)) {
1079
        val |= EEPROM_DO;
1080
    } else {
1081
        val &= ~EEPROM_DO;
1082
    }
1083
    TRACE(EEPROM, logout("val=0x%04x\n", val));
1084
    return val;
1085
}
1086

    
1087
static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
1088
{
1089
    TRACE(EEPROM, logout("val=0x%02x\n", val));
1090

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

    
1094
    int eecs = ((val & EEPROM_CS) != 0);
1095
    int eesk = ((val & EEPROM_SK) != 0);
1096
    int eedi = ((val & EEPROM_DI) != 0);
1097
    eeprom93xx_write(eeprom, eecs, eesk, eedi);
1098
}
1099

    
1100
static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
1101
{
1102
    s->pointer = le32_to_cpu(val);
1103
    TRACE(OTHER, logout("val=0x%08x\n", val));
1104
}
1105

    
1106
/*****************************************************************************
1107
 *
1108
 * MDI emulation.
1109
 *
1110
 ****************************************************************************/
1111

    
1112
#if defined(DEBUG_EEPRO100)
1113
static const char * const mdi_op_name[] = {
1114
    "opcode 0",
1115
    "write",
1116
    "read",
1117
    "opcode 3"
1118
};
1119

    
1120
static const char * const mdi_reg_name[] = {
1121
    "Control",
1122
    "Status",
1123
    "PHY Identification (Word 1)",
1124
    "PHY Identification (Word 2)",
1125
    "Auto-Negotiation Advertisement",
1126
    "Auto-Negotiation Link Partner Ability",
1127
    "Auto-Negotiation Expansion"
1128
};
1129

    
1130
static const char *reg2name(uint8_t reg)
1131
{
1132
    static char buffer[10];
1133
    const char *p = buffer;
1134
    if (reg < ARRAY_SIZE(mdi_reg_name)) {
1135
        p = mdi_reg_name[reg];
1136
    } else {
1137
        snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
1138
    }
1139
    return p;
1140
}
1141
#endif                          /* DEBUG_EEPRO100 */
1142

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

    
1148
#ifdef DEBUG_EEPRO100
1149
    uint8_t raiseint = (val & BIT(29)) >> 29;
1150
    uint8_t opcode = (val & BITS(27, 26)) >> 26;
1151
    uint8_t phy = (val & BITS(25, 21)) >> 21;
1152
    uint8_t reg = (val & BITS(20, 16)) >> 16;
1153
    uint16_t data = (val & BITS(15, 0));
1154
#endif
1155
    /* Emulation takes no time to finish MDI transaction. */
1156
    val |= BIT(28);
1157
    TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1158
                      val, raiseint, mdi_op_name[opcode], phy,
1159
                      reg2name(reg), data));
1160
    return val;
1161
}
1162

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

    
1256
/*****************************************************************************
1257
 *
1258
 * Port emulation.
1259
 *
1260
 ****************************************************************************/
1261

    
1262
#define PORT_SOFTWARE_RESET     0
1263
#define PORT_SELFTEST           1
1264
#define PORT_SELECTIVE_RESET    2
1265
#define PORT_DUMP               3
1266
#define PORT_SELECTION_MASK     3
1267

    
1268
typedef struct {
1269
    uint32_t st_sign;           /* Self Test Signature */
1270
    uint32_t st_result;         /* Self Test Results */
1271
} eepro100_selftest_t;
1272

    
1273
static uint32_t eepro100_read_port(EEPRO100State * s)
1274
{
1275
    return 0;
1276
}
1277

    
1278
static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1279
{
1280
    val = le32_to_cpu(val);
1281
    uint32_t address = (val & ~PORT_SELECTION_MASK);
1282
    uint8_t selection = (val & PORT_SELECTION_MASK);
1283
    switch (selection) {
1284
    case PORT_SOFTWARE_RESET:
1285
        nic_reset(s);
1286
        break;
1287
    case PORT_SELFTEST:
1288
        TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1289
        eepro100_selftest_t data;
1290
        cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1291
        data.st_sign = 0xffffffff;
1292
        data.st_result = 0;
1293
        cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1294
        break;
1295
    case PORT_SELECTIVE_RESET:
1296
        TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1297
        nic_selective_reset(s);
1298
        break;
1299
    default:
1300
        logout("val=0x%08x\n", val);
1301
        missing("unknown port selection");
1302
    }
1303
}
1304

    
1305
/*****************************************************************************
1306
 *
1307
 * General hardware emulation.
1308
 *
1309
 ****************************************************************************/
1310

    
1311
static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1312
{
1313
    uint8_t val;
1314
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1315
        memcpy(&val, &s->mem[addr], sizeof(val));
1316
    }
1317

    
1318
    switch (addr) {
1319
    case SCBStatus:
1320
        //~ val = eepro100_read_status(s);
1321
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1322
        break;
1323
    case SCBAck:
1324
        //~ val = eepro100_read_status(s);
1325
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1326
        break;
1327
    case SCBCmd:
1328
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1329
        //~ val = eepro100_read_command(s);
1330
        break;
1331
    case SCBIntmask:
1332
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1333
        break;
1334
    case SCBPort + 3:
1335
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1336
        break;
1337
    case SCBeeprom:
1338
        val = eepro100_read_eeprom(s);
1339
        break;
1340
    case SCBpmdr:       /* Power Management Driver Register */
1341
        val = 0;
1342
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1343
        break;
1344
    case SCBgstat:      /* General Status Register */
1345
        /* 100 Mbps full duplex, valid link */
1346
        val = 0x07;
1347
        TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1348
        break;
1349
    default:
1350
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1351
        missing("unknown byte read");
1352
    }
1353
    return val;
1354
}
1355

    
1356
static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1357
{
1358
    uint16_t val;
1359
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1360
        memcpy(&val, &s->mem[addr], sizeof(val));
1361
    }
1362

    
1363
    switch (addr) {
1364
    case SCBStatus:
1365
        //~ val = eepro100_read_status(s);
1366
    case SCBCmd:
1367
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1368
        break;
1369
    case SCBeeprom:
1370
        val = eepro100_read_eeprom(s);
1371
        TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1372
        break;
1373
    default:
1374
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1375
        missing("unknown word read");
1376
    }
1377
    return val;
1378
}
1379

    
1380
static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1381
{
1382
    uint32_t val;
1383
    if (addr <= sizeof(s->mem) - sizeof(val)) {
1384
        memcpy(&val, &s->mem[addr], sizeof(val));
1385
    }
1386

    
1387
    switch (addr) {
1388
    case SCBStatus:
1389
        //~ val = eepro100_read_status(s);
1390
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1391
        break;
1392
    case SCBPointer:
1393
        //~ val = eepro100_read_pointer(s);
1394
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1395
        break;
1396
    case SCBPort:
1397
        val = eepro100_read_port(s);
1398
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1399
        break;
1400
    case SCBCtrlMDI:
1401
        val = eepro100_read_mdi(s);
1402
        break;
1403
    default:
1404
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1405
        missing("unknown longword read");
1406
    }
1407
    return val;
1408
}
1409

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

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

    
1418
    switch (addr) {
1419
    case SCBStatus:
1420
        //~ eepro100_write_status(s, val);
1421
        break;
1422
    case SCBAck:
1423
        eepro100_acknowledge(s);
1424
        break;
1425
    case SCBCmd:
1426
        eepro100_write_command(s, val);
1427
        break;
1428
    case SCBIntmask:
1429
        if (val & BIT(1)) {
1430
            eepro100_swi_interrupt(s);
1431
        }
1432
        eepro100_interrupt(s, 0);
1433
        break;
1434
    case SCBPort + 3:
1435
    case SCBFlow:       /* does not exist on 82557 */
1436
    case SCBFlow + 1:
1437
    case SCBFlow + 2:
1438
    case SCBpmdr:       /* does not exist on 82557 */
1439
        TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1440
        break;
1441
    case SCBeeprom:
1442
        eepro100_write_eeprom(s->eeprom, val);
1443
        break;
1444
    default:
1445
        logout("addr=%s val=0x%02x\n", regname(addr), val);
1446
        missing("unknown byte write");
1447
    }
1448
}
1449

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

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

    
1458
    switch (addr) {
1459
    case SCBStatus:
1460
        //~ eepro100_write_status(s, val);
1461
        eepro100_acknowledge(s);
1462
        break;
1463
    case SCBCmd:
1464
        eepro100_write_command(s, val);
1465
        eepro100_write1(s, SCBIntmask, val >> 8);
1466
        break;
1467
    case SCBeeprom:
1468
        eepro100_write_eeprom(s->eeprom, val);
1469
        break;
1470
    default:
1471
        logout("addr=%s val=0x%04x\n", regname(addr), val);
1472
        missing("unknown word write");
1473
    }
1474
}
1475

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

    
1482
    switch (addr) {
1483
    case SCBPointer:
1484
        eepro100_write_pointer(s, val);
1485
        break;
1486
    case SCBPort:
1487
        TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1488
        eepro100_write_port(s, val);
1489
        break;
1490
    case SCBCtrlMDI:
1491
        eepro100_write_mdi(s, val);
1492
        break;
1493
    default:
1494
        logout("addr=%s val=0x%08x\n", regname(addr), val);
1495
        missing("unknown longword write");
1496
    }
1497
}
1498

    
1499
/*****************************************************************************
1500
 *
1501
 * Port mapped I/O.
1502
 *
1503
 ****************************************************************************/
1504

    
1505
static uint32_t ioport_read1(void *opaque, uint32_t addr)
1506
{
1507
    EEPRO100State *s = opaque;
1508
    //~ logout("addr=%s\n", regname(addr));
1509
    return eepro100_read1(s, addr - s->region[1]);
1510
}
1511

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

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

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

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

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

    
1543
/***********************************************************/
1544
/* PCI EEPRO100 definitions */
1545

    
1546
static void pci_map(PCIDevice * pci_dev, int region_num,
1547
                    pcibus_t addr, pcibus_t size, int type)
1548
{
1549
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1550

    
1551
    TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
1552
          "size=0x%08"FMT_PCIBUS", type=%d\n",
1553
          region_num, addr, size, type));
1554

    
1555
    assert(region_num == 1);
1556
    register_ioport_write(addr, size, 1, ioport_write1, s);
1557
    register_ioport_read(addr, size, 1, ioport_read1, s);
1558
    register_ioport_write(addr, size, 2, ioport_write2, s);
1559
    register_ioport_read(addr, size, 2, ioport_read2, s);
1560
    register_ioport_write(addr, size, 4, ioport_write4, s);
1561
    register_ioport_read(addr, size, 4, ioport_read4, s);
1562

    
1563
    s->region[region_num] = addr;
1564
}
1565

    
1566
/*****************************************************************************
1567
 *
1568
 * Memory mapped I/O.
1569
 *
1570
 ****************************************************************************/
1571

    
1572
static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1573
{
1574
    EEPRO100State *s = opaque;
1575
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1576
    eepro100_write1(s, addr, val);
1577
}
1578

    
1579
static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1580
{
1581
    EEPRO100State *s = opaque;
1582
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1583
    eepro100_write2(s, addr, val);
1584
}
1585

    
1586
static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1587
{
1588
    EEPRO100State *s = opaque;
1589
    //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1590
    eepro100_write4(s, addr, val);
1591
}
1592

    
1593
static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1594
{
1595
    EEPRO100State *s = opaque;
1596
    //~ logout("addr=%s\n", regname(addr));
1597
    return eepro100_read1(s, addr);
1598
}
1599

    
1600
static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1601
{
1602
    EEPRO100State *s = opaque;
1603
    //~ logout("addr=%s\n", regname(addr));
1604
    return eepro100_read2(s, addr);
1605
}
1606

    
1607
static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1608
{
1609
    EEPRO100State *s = opaque;
1610
    //~ logout("addr=%s\n", regname(addr));
1611
    return eepro100_read4(s, addr);
1612
}
1613

    
1614
static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1615
    pci_mmio_writeb,
1616
    pci_mmio_writew,
1617
    pci_mmio_writel
1618
};
1619

    
1620
static CPUReadMemoryFunc * const pci_mmio_read[] = {
1621
    pci_mmio_readb,
1622
    pci_mmio_readw,
1623
    pci_mmio_readl
1624
};
1625

    
1626
static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1627
                         pcibus_t addr, pcibus_t size, int type)
1628
{
1629
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1630

    
1631
    TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
1632
          "size=0x%08"FMT_PCIBUS", type=%d\n",
1633
          region_num, addr, size, type));
1634

    
1635
    if (region_num == 0) {
1636
        /* Map control / status registers. */
1637
        cpu_register_physical_memory(addr, size, s->mmio_index);
1638
        s->region[region_num] = addr;
1639
    }
1640
}
1641

    
1642
static int nic_can_receive(VLANClientState *nc)
1643
{
1644
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1645
    TRACE(RXTX, logout("%p\n", s));
1646
    return get_ru_state(s) == ru_ready;
1647
    //~ return !eepro100_buffer_full(s);
1648
}
1649

    
1650
static ssize_t nic_receive(VLANClientState *nc, const uint8_t * buf, size_t size)
1651
{
1652
    /* TODO:
1653
     * - Magic packets should set bit 30 in power management driver register.
1654
     * - Interesting packets should set bit 29 in power management driver register.
1655
     */
1656
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1657
    uint16_t rfd_status = 0xa000;
1658
    static const uint8_t broadcast_macaddr[6] =
1659
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1660

    
1661
    /* TODO: check multiple IA bit. */
1662
    if (s->configuration[20] & BIT(6)) {
1663
        missing("Multiple IA bit");
1664
        return -1;
1665
    }
1666

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

    
1720
    if (get_ru_state(s) != ru_ready) {
1721
        /* No resources available. */
1722
        logout("no resources, state=%u\n", get_ru_state(s));
1723
        s->statistics.rx_resource_errors++;
1724
        //~ assert(!"no resources");
1725
        return -1;
1726
    }
1727
    //~ !!!
1728
//~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1729
    eepro100_rx_t rx;
1730
    cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1731
                             offsetof(eepro100_rx_t, packet));
1732
    uint16_t rfd_command = le16_to_cpu(rx.command);
1733
    uint16_t rfd_size = le16_to_cpu(rx.size);
1734

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

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

    
1830
static void nic_cleanup(VLANClientState *nc)
1831
{
1832
    EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1833

    
1834
    s->nic = NULL;
1835
}
1836

    
1837
static int pci_nic_uninit(PCIDevice *pci_dev)
1838
{
1839
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1840

    
1841
    cpu_unregister_io_memory(s->mmio_index);
1842
    vmstate_unregister(s->vmstate, s);
1843
    eeprom93xx_free(s->eeprom);
1844
    qemu_del_vlan_client(&s->nic->nc);
1845
    return 0;
1846
}
1847

    
1848
static NetClientInfo net_eepro100_info = {
1849
    .type = NET_CLIENT_TYPE_NIC,
1850
    .size = sizeof(NICState),
1851
    .can_receive = nic_can_receive,
1852
    .receive = nic_receive,
1853
    .cleanup = nic_cleanup,
1854
};
1855

    
1856
static int nic_init(PCIDevice *pci_dev, uint32_t device)
1857
{
1858
    EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1859

    
1860
    TRACE(OTHER, logout("\n"));
1861

    
1862
    s->device = device;
1863

    
1864
    pci_reset(s);
1865

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

    
1870
    /* Handler for memory-mapped I/O */
1871
    s->mmio_index =
1872
        cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1873

    
1874
    pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1875
                           PCI_BASE_ADDRESS_SPACE_MEMORY |
1876
                           PCI_BASE_ADDRESS_MEM_PREFETCH, pci_mmio_map);
1877
    pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_BASE_ADDRESS_SPACE_IO,
1878
                           pci_map);
1879
    pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_BASE_ADDRESS_SPACE_MEMORY,
1880
                           pci_mmio_map);
1881

    
1882
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
1883
    logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
1884
    assert(s->region[1] == 0);
1885

    
1886
    nic_reset(s);
1887

    
1888
    s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
1889
                          pci_dev->qdev.info->name, pci_dev->qdev.id, s);
1890

    
1891
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
1892
    TRACE(OTHER, logout("%s\n", s->nic->nc.info_str));
1893

    
1894
    qemu_register_reset(nic_reset, s);
1895

    
1896
    s->vmstate = qemu_malloc(sizeof(vmstate_eepro100));
1897
    memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
1898
    s->vmstate->name = s->nic->nc.model;
1899
    vmstate_register(-1, s->vmstate, s);
1900

    
1901
    return 0;
1902
}
1903

    
1904
static int pci_i82550_init(PCIDevice *pci_dev)
1905
{
1906
    return nic_init(pci_dev, i82550);
1907
}
1908

    
1909
static int pci_i82551_init(PCIDevice *pci_dev)
1910
{
1911
    return nic_init(pci_dev, i82551);
1912
}
1913

    
1914
static int pci_i82557a_init(PCIDevice *pci_dev)
1915
{
1916
    return nic_init(pci_dev, i82557A);
1917
}
1918

    
1919
static int pci_i82557b_init(PCIDevice *pci_dev)
1920
{
1921
    return nic_init(pci_dev, i82557B);
1922
}
1923

    
1924
static int pci_i82557c_init(PCIDevice *pci_dev)
1925
{
1926
    return nic_init(pci_dev, i82557C);
1927
}
1928

    
1929
static int pci_i82558a_init(PCIDevice *pci_dev)
1930
{
1931
    return nic_init(pci_dev, i82558A);
1932
}
1933

    
1934
static int pci_i82558b_init(PCIDevice *pci_dev)
1935
{
1936
    return nic_init(pci_dev, i82558B);
1937
}
1938

    
1939
static int pci_i82559a_init(PCIDevice *pci_dev)
1940
{
1941
    return nic_init(pci_dev, i82559A);
1942
}
1943

    
1944
static int pci_i82559b_init(PCIDevice *pci_dev)
1945
{
1946
    return nic_init(pci_dev, i82559B);
1947
}
1948

    
1949
static int pci_i82559c_init(PCIDevice *pci_dev)
1950
{
1951
    return nic_init(pci_dev, i82559C);
1952
}
1953

    
1954
static int pci_i82559er_init(PCIDevice *pci_dev)
1955
{
1956
    return nic_init(pci_dev, i82559ER);
1957
}
1958

    
1959
static int pci_i82562_init(PCIDevice *pci_dev)
1960
{
1961
    return nic_init(pci_dev, i82562);
1962
}
1963

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

    
2079
static void eepro100_register_devices(void)
2080
{
2081
    pci_qdev_register_many(eepro100_info);
2082
}
2083

    
2084
device_init(eepro100_register_devices)