]> Git Repo - qemu.git/blob - hw/eepro100.c
eepro100: Remove C++ comments
[qemu.git] / hw / eepro100.c
1 /*
2  * QEMU i8255x (PRO100) emulation
3  *
4  * Copyright (C) 2006-2010 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) version 3 or 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) ok
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  * TODO:
36  *      * PHY emulation should be separated from nic emulation.
37  *        Most nic emulations could share the same phy code.
38  *      * i82550 is untested. It is programmed like the i82559.
39  *      * i82562 is untested. It is programmed like the i82559.
40  *      * Power management (i82558 and later) is not implemented.
41  *      * Wake-on-LAN is not implemented.
42  */
43
44 #include <stddef.h>             /* offsetof */
45 #include <stdbool.h>
46 #include "hw.h"
47 #include "pci.h"
48 #include "net.h"
49 #include "eeprom93xx.h"
50
51 /* Common declarations for all PCI devices. */
52
53 #define PCI_CONFIG_8(offset, value) \
54     (pci_conf[offset] = (value))
55 #define PCI_CONFIG_16(offset, value) \
56     (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
57 #define PCI_CONFIG_32(offset, value) \
58     (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
59
60 #define KiB 1024
61
62 /* Debug EEPRO100 card. */
63 #if 0
64 # define DEBUG_EEPRO100
65 #endif
66
67 #ifdef DEBUG_EEPRO100
68 #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
69 #else
70 #define logout(fmt, ...) ((void)0)
71 #endif
72
73 /* Set flags to 0 to disable debug output. */
74 #define INT     1       /* interrupt related actions */
75 #define MDI     1       /* mdi related actions */
76 #define OTHER   1
77 #define RXTX    1
78 #define EEPROM  1       /* eeprom related actions */
79
80 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
81
82 #define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
83
84 #define MAX_ETH_FRAME_SIZE 1514
85
86 /* This driver supports several different devices which are declared here. */
87 #define i82550          0x82550
88 #define i82551          0x82551
89 #define i82557A         0x82557a
90 #define i82557B         0x82557b
91 #define i82557C         0x82557c
92 #define i82558A         0x82558a
93 #define i82558B         0x82558b
94 #define i82559A         0x82559a
95 #define i82559B         0x82559b
96 #define i82559C         0x82559c
97 #define i82559ER        0x82559e
98 #define i82562          0x82562
99
100 /* Use 64 word EEPROM. TODO: could be a runtime option. */
101 #define EEPROM_SIZE     64
102
103 #define PCI_MEM_SIZE            (4 * KiB)
104 #define PCI_IO_SIZE             64
105 #define PCI_FLASH_SIZE          (128 * KiB)
106
107 #define BIT(n) (1 << (n))
108 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
109
110 /* The SCB accepts the following controls for the Tx and Rx units: */
111 #define  CU_NOP         0x0000  /* No operation. */
112 #define  CU_START       0x0010  /* CU start. */
113 #define  CU_RESUME      0x0020  /* CU resume. */
114 #define  CU_STATSADDR   0x0040  /* Load dump counters address. */
115 #define  CU_SHOWSTATS   0x0050  /* Dump statistical counters. */
116 #define  CU_CMD_BASE    0x0060  /* Load CU base address. */
117 #define  CU_DUMPSTATS   0x0070  /* Dump and reset statistical counters. */
118 #define  CU_SRESUME     0x00a0  /* CU static resume. */
119
120 #define  RU_NOP         0x0000
121 #define  RX_START       0x0001
122 #define  RX_RESUME      0x0002
123 #define  RU_ABORT       0x0004
124 #define  RX_ADDR_LOAD   0x0006
125 #define  RX_RESUMENR    0x0007
126 #define INT_MASK        0x0100
127 #define DRVR_INT        0x0200  /* Driver generated interrupt. */
128
129 /* Offsets to the various registers.
130    All accesses need not be longword aligned. */
131 enum speedo_offsets {
132     SCBStatus = 0,              /* Status Word. */
133     SCBAck = 1,
134     SCBCmd = 2,                 /* Rx/Command Unit command and status. */
135     SCBIntmask = 3,
136     SCBPointer = 4,             /* General purpose pointer. */
137     SCBPort = 8,                /* Misc. commands and operands.  */
138     SCBflash = 12,              /* Flash memory control. */
139     SCBeeprom = 14,             /* EEPROM control. */
140     SCBCtrlMDI = 16,            /* MDI interface control. */
141     SCBEarlyRx = 20,            /* Early receive byte count. */
142     SCBFlow = 24,               /* Flow Control. */
143     SCBpmdr = 27,               /* Power Management Driver. */
144     SCBgctrl = 28,              /* General Control. */
145     SCBgstat = 29,              /* General Status. */
146 };
147
148 /* A speedo3 transmit buffer descriptor with two buffers... */
149 typedef struct {
150     uint16_t status;
151     uint16_t command;
152     uint32_t link;              /* void * */
153     uint32_t tbd_array_addr;    /* transmit buffer descriptor array address. */
154     uint16_t tcb_bytes;         /* transmit command block byte count (in lower 14 bits */
155     uint8_t tx_threshold;       /* transmit threshold */
156     uint8_t tbd_count;          /* TBD number */
157 #if 0
158     /* This constitutes two "TBD" entries: hdr and data */
159     uint32_t tx_buf_addr0;  /* void *, header of frame to be transmitted.  */
160     int32_t  tx_buf_size0;  /* Length of Tx hdr. */
161     uint32_t tx_buf_addr1;  /* void *, data to be transmitted.  */
162     int32_t  tx_buf_size1;  /* Length of Tx data. */
163 #endif
164 } eepro100_tx_t;
165
166 /* Receive frame descriptor. */
167 typedef struct {
168     int16_t status;
169     uint16_t command;
170     uint32_t link;              /* struct RxFD * */
171     uint32_t rx_buf_addr;       /* void * */
172     uint16_t count;
173     uint16_t size;
174     char packet[MAX_ETH_FRAME_SIZE + 4];
175 } eepro100_rx_t;
176
177 typedef enum {
178     COMMAND_EL = BIT(15),
179     COMMAND_S = BIT(14),
180     COMMAND_I = BIT(13),
181     COMMAND_NC = BIT(4),
182     COMMAND_SF = BIT(3),
183     COMMAND_CMD = BITS(2, 0),
184 } scb_command_bit;
185
186 typedef enum {
187     STATUS_C = BIT(15),
188     STATUS_OK = BIT(13),
189 } scb_status_bit;
190
191 typedef struct {
192     uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
193              tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
194              tx_multiple_collisions, tx_total_collisions;
195     uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
196              rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
197              rx_short_frame_errors;
198     uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
199     uint16_t xmt_tco_frames, rcv_tco_frames;
200     /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
201     uint32_t reserved[4];
202 } eepro100_stats_t;
203
204 typedef enum {
205     cu_idle = 0,
206     cu_suspended = 1,
207     cu_active = 2,
208     cu_lpq_active = 2,
209     cu_hqp_active = 3
210 } cu_state_t;
211
212 typedef enum {
213     ru_idle = 0,
214     ru_suspended = 1,
215     ru_no_resources = 2,
216     ru_ready = 4
217 } ru_state_t;
218
219 typedef struct {
220     PCIDevice dev;
221     uint8_t mult[8];            /* multicast mask array */
222     int mmio_index;
223     NICState *nic;
224     NICConf conf;
225     uint8_t scb_stat;           /* SCB stat/ack byte */
226     uint8_t int_stat;           /* PCI interrupt status */
227     /* region must not be saved by nic_save. */
228     uint32_t region[3];         /* PCI region addresses */
229     uint16_t mdimem[32];
230     eeprom_t *eeprom;
231     uint32_t device;            /* device variant */
232     uint32_t pointer;
233     /* (cu_base + cu_offset) address the next command block in the command block list. */
234     uint32_t cu_base;           /* CU base address */
235     uint32_t cu_offset;         /* CU address offset */
236     /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
237     uint32_t ru_base;           /* RU base address */
238     uint32_t ru_offset;         /* RU address offset */
239     uint32_t statsaddr;         /* pointer to eepro100_stats_t */
240
241     /* Temporary status information (no need to save these values),
242      * used while processing CU commands. */
243     eepro100_tx_t tx;           /* transmit buffer descriptor */
244     uint32_t cb_address;        /* = cu_base + cu_offset */
245
246     /* Statistical counters. Also used for wake-up packet (i82559). */
247     eepro100_stats_t statistics;
248
249 #if 0
250     uint16_t status;
251 #endif
252
253     /* Configuration bytes. */
254     uint8_t configuration[22];
255
256     /* Data in mem is always in the byte order of the controller (le). */
257     uint8_t mem[PCI_MEM_SIZE];
258     /* vmstate for each particular nic */
259     VMStateDescription *vmstate;
260
261     /* Quasi static device properties (no need to save them). */
262     uint16_t stats_size;
263     bool has_extended_tcb_support;
264 } EEPRO100State;
265
266 /* Word indices in EEPROM. */
267 typedef enum {
268     EEPROM_CNFG_MDIX  = 0x03,
269     EEPROM_ID         = 0x05,
270     EEPROM_PHY_ID     = 0x06,
271     EEPROM_VENDOR_ID  = 0x0c,
272     EEPROM_CONFIG_ASF = 0x0d,
273     EEPROM_DEVICE_ID  = 0x23,
274     EEPROM_SMBUS_ADDR = 0x90,
275 } EEPROMOffset;
276
277 /* Bit values for EEPROM ID word. */
278 typedef enum {
279     EEPROM_ID_MDM = BIT(0),     /* Modem */
280     EEPROM_ID_STB = BIT(1),     /* Standby Enable */
281     EEPROM_ID_WMR = BIT(2),     /* ??? */
282     EEPROM_ID_WOL = BIT(5),     /* Wake on LAN */
283     EEPROM_ID_DPD = BIT(6),     /* Deep Power Down */
284     EEPROM_ID_ALT = BIT(7),     /* */
285     /* BITS(10, 8) device revision */
286     EEPROM_ID_BD = BIT(11),     /* boot disable */
287     EEPROM_ID_ID = BIT(13),     /* id bit */
288     /* BITS(15, 14) signature */
289     EEPROM_ID_VALID = BIT(14),  /* signature for valid eeprom */
290 } eeprom_id_bit;
291
292 /* Default values for MDI (PHY) registers */
293 static const uint16_t eepro100_mdi_default[] = {
294     /* MDI Registers 0 - 6, 7 */
295     0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
296     /* MDI Registers 8 - 15 */
297     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
298     /* MDI Registers 16 - 31 */
299     0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
300     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
301 };
302
303 /* Readonly mask for MDI (PHY) registers */
304 static const uint16_t eepro100_mdi_mask[] = {
305     0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
306     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
307     0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
308     0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
309 };
310
311 /* XXX: optimize */
312 static void stl_le_phys(target_phys_addr_t addr, uint32_t val)
313 {
314     val = cpu_to_le32(val);
315     cpu_physical_memory_write(addr, (const uint8_t *)&val, sizeof(val));
316 }
317
318 #define POLYNOMIAL 0x04c11db6
319
320 /* From FreeBSD */
321 /* XXX: optimize */
322 static unsigned compute_mcast_idx(const uint8_t * ep)
323 {
324     uint32_t crc;
325     int carry, i, j;
326     uint8_t b;
327
328     crc = 0xffffffff;
329     for (i = 0; i < 6; i++) {
330         b = *ep++;
331         for (j = 0; j < 8; j++) {
332             carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
333             crc <<= 1;
334             b >>= 1;
335             if (carry) {
336                 crc = ((crc ^ POLYNOMIAL) | carry);
337             }
338         }
339     }
340     return (crc & BITS(7, 2)) >> 2;
341 }
342
343 #if defined(DEBUG_EEPRO100)
344 static const char *nic_dump(const uint8_t * buf, unsigned size)
345 {
346     static char dump[3 * 16 + 1];
347     char *p = &dump[0];
348     if (size > 16) {
349         size = 16;
350     }
351     while (size-- > 0) {
352         p += sprintf(p, " %02x", *buf++);
353     }
354     return dump;
355 }
356 #endif                          /* DEBUG_EEPRO100 */
357
358 enum scb_stat_ack {
359     stat_ack_not_ours = 0x00,
360     stat_ack_sw_gen = 0x04,
361     stat_ack_rnr = 0x10,
362     stat_ack_cu_idle = 0x20,
363     stat_ack_frame_rx = 0x40,
364     stat_ack_cu_cmd_done = 0x80,
365     stat_ack_not_present = 0xFF,
366     stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
367     stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
368 };
369
370 static void disable_interrupt(EEPRO100State * s)
371 {
372     if (s->int_stat) {
373         TRACE(INT, logout("interrupt disabled\n"));
374         qemu_irq_lower(s->dev.irq[0]);
375         s->int_stat = 0;
376     }
377 }
378
379 static void enable_interrupt(EEPRO100State * s)
380 {
381     if (!s->int_stat) {
382         TRACE(INT, logout("interrupt enabled\n"));
383         qemu_irq_raise(s->dev.irq[0]);
384         s->int_stat = 1;
385     }
386 }
387
388 static void eepro100_acknowledge(EEPRO100State * s)
389 {
390     s->scb_stat &= ~s->mem[SCBAck];
391     s->mem[SCBAck] = s->scb_stat;
392     if (s->scb_stat == 0) {
393         disable_interrupt(s);
394     }
395 }
396
397 static void eepro100_interrupt(EEPRO100State * s, uint8_t status)
398 {
399     uint8_t mask = ~s->mem[SCBIntmask];
400     s->mem[SCBAck] |= status;
401     status = s->scb_stat = s->mem[SCBAck];
402     status &= (mask | 0x0f);
403 #if 0
404     status &= (~s->mem[SCBIntmask] | 0x0xf);
405 #endif
406     if (status && (mask & 0x01)) {
407         /* SCB mask and SCB Bit M do not disable interrupt. */
408         enable_interrupt(s);
409     } else if (s->int_stat) {
410         disable_interrupt(s);
411     }
412 }
413
414 static void eepro100_cx_interrupt(EEPRO100State * s)
415 {
416     /* CU completed action command. */
417     /* Transmit not ok (82557 only, not in emulation). */
418     eepro100_interrupt(s, 0x80);
419 }
420
421 static void eepro100_cna_interrupt(EEPRO100State * s)
422 {
423     /* CU left the active state. */
424     eepro100_interrupt(s, 0x20);
425 }
426
427 static void eepro100_fr_interrupt(EEPRO100State * s)
428 {
429     /* RU received a complete frame. */
430     eepro100_interrupt(s, 0x40);
431 }
432
433 static void eepro100_rnr_interrupt(EEPRO100State * s)
434 {
435     /* RU is not ready. */
436     eepro100_interrupt(s, 0x10);
437 }
438
439 static void eepro100_mdi_interrupt(EEPRO100State * s)
440 {
441     /* MDI completed read or write cycle. */
442     eepro100_interrupt(s, 0x08);
443 }
444
445 static void eepro100_swi_interrupt(EEPRO100State * s)
446 {
447     /* Software has requested an interrupt. */
448     eepro100_interrupt(s, 0x04);
449 }
450
451 #if 0
452 static void eepro100_fcp_interrupt(EEPRO100State * s)
453 {
454     /* Flow control pause interrupt (82558 and later). */
455     eepro100_interrupt(s, 0x01);
456 }
457 #endif
458
459 static void pci_reset(EEPRO100State * s)
460 {
461     uint32_t device = s->device;
462     uint8_t *pci_conf = s->dev.config;
463     bool power_management = 1;
464
465     TRACE(OTHER, logout("%p\n", s));
466
467     /* PCI Vendor ID */
468     pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
469     /* PCI Device ID depends on device and is set below. */
470     /* PCI Command */
471     /* TODO: this is the default, do not override. */
472     PCI_CONFIG_16(PCI_COMMAND, 0x0000);
473     /* PCI Status */
474     /* TODO: Value at RST# should be 0. */
475     PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM | PCI_STATUS_FAST_BACK);
476     /* PCI Revision ID */
477     PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
478     /* TODO: this is the default, do not override. */
479     /* PCI Class Code */
480     PCI_CONFIG_8(PCI_CLASS_PROG, 0x00);
481     pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
482     /* PCI Cache Line Size */
483     /* check cache line size!!! */
484 #if 0
485     PCI_CONFIG_8(0x0c, 0x00);
486 #endif
487     /* PCI Latency Timer */
488     PCI_CONFIG_8(PCI_LATENCY_TIMER, 0x20);   /* latency timer = 32 clocks */
489     /* PCI Header Type */
490     /* BIST (built-in self test) */
491     /* Expansion ROM Base Address (depends on boot disable!!!) */
492     /* TODO: not needed, set when BAR is registered */
493     PCI_CONFIG_32(PCI_ROM_ADDRESS, PCI_BASE_ADDRESS_SPACE_MEMORY);
494     /* Capability Pointer */
495     /* TODO: revisions with power_management 1 use this but
496      * do not set new capability list bit in status register. */
497     PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0xdc);
498     /* Interrupt Line */
499     /* Interrupt Pin */
500     /* TODO: RST# value should be 0 */
501     PCI_CONFIG_8(PCI_INTERRUPT_PIN, 1);      /* interrupt pin 0 */
502     /* Minimum Grant */
503     PCI_CONFIG_8(PCI_MIN_GNT, 0x08);
504     /* Maximum Latency */
505     PCI_CONFIG_8(PCI_MAX_LAT, 0x18);
506
507     switch (device) {
508     case i82550:
509         /* TODO: check device id. */
510         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
511         /* Revision ID: 0x0c, 0x0d, 0x0e. */
512         PCI_CONFIG_8(PCI_REVISION_ID, 0x0e);
513         /* TODO: check size of statistical counters. */
514         s->stats_size = 80;
515         /* TODO: check extended tcb support. */
516         s->has_extended_tcb_support = 1;
517         break;
518     case i82551:
519         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
520         /* Revision ID: 0x0f, 0x10. */
521         PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
522         /* TODO: check size of statistical counters. */
523         s->stats_size = 80;
524         s->has_extended_tcb_support = 1;
525         break;
526     case i82557A:
527         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
528         PCI_CONFIG_8(PCI_REVISION_ID, 0x01);
529         PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
530         power_management = 0;
531         break;
532     case i82557B:
533         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
534         PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
535         PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
536         power_management = 0;
537         break;
538     case i82557C:
539         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
540         PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
541         PCI_CONFIG_8(PCI_CAPABILITY_LIST, 0x00);
542         power_management = 0;
543         break;
544     case i82558A:
545         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
546         PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
547                                   PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
548         PCI_CONFIG_8(PCI_REVISION_ID, 0x04);
549         s->stats_size = 76;
550         s->has_extended_tcb_support = 1;
551         break;
552     case i82558B:
553         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
554         PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
555                                   PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
556         PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
557         s->stats_size = 76;
558         s->has_extended_tcb_support = 1;
559         break;
560     case i82559A:
561         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
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, 0x06);
565         s->stats_size = 80;
566         s->has_extended_tcb_support = 1;
567         break;
568     case i82559B:
569         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
570         PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
571                                   PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
572         PCI_CONFIG_8(PCI_REVISION_ID, 0x07);
573         s->stats_size = 80;
574         s->has_extended_tcb_support = 1;
575         break;
576     case i82559C:
577         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
578         PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
579                                   PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
580         PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
581         /* TODO: Windows wants revision id 0x0c. */
582         PCI_CONFIG_8(PCI_REVISION_ID, 0x0c);
583 #if EEPROM_SIZE > 0
584         PCI_CONFIG_16(PCI_SUBSYSTEM_VENDOR_ID, 0x8086);
585         PCI_CONFIG_16(PCI_SUBSYSTEM_ID, 0x0040);
586 #endif
587         s->stats_size = 80;
588         s->has_extended_tcb_support = 1;
589         break;
590     case i82559ER:
591         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
592         PCI_CONFIG_16(PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
593                                   PCI_STATUS_FAST_BACK | PCI_STATUS_CAP_LIST);
594         PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
595         s->stats_size = 80;
596         s->has_extended_tcb_support = 1;
597         break;
598     case i82562:
599         /* TODO: check device id. */
600         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
601         /* TODO: wrong revision id. */
602         PCI_CONFIG_8(PCI_REVISION_ID, 0x0e);
603         s->stats_size = 80;
604         s->has_extended_tcb_support = 1;
605         break;
606     default:
607         logout("Device %X is undefined!\n", device);
608     }
609
610     s->configuration[6] |= BIT(5);
611
612     if (s->stats_size == 80) {
613         /* TODO: check TCO Statistical Counters bit. Documentation not clear. */
614         if (s->configuration[6] & BIT(2)) {
615             /* TCO statistical counters. */
616             assert(s->configuration[6] & BIT(5));
617         } else {
618             if (s->configuration[6] & BIT(5)) {
619                 /* No extended statistical counters, i82557 compatible. */
620                 s->stats_size = 64;
621             } else {
622                 /* i82558 compatible. */
623                 s->stats_size = 76;
624             }
625         }
626     } else {
627         if (s->configuration[6] & BIT(5)) {
628             /* No extended statistical counters. */
629             s->stats_size = 64;
630         }
631     }
632     assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics));
633
634     if (power_management) {
635         /* Power Management Capabilities */
636         PCI_CONFIG_8(0xdc, 0x01);
637         /* Next Item Pointer */
638         /* Capability ID */
639         PCI_CONFIG_16(0xde, 0x7e21);
640         /* TODO: Power Management Control / Status. */
641         /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
642     }
643
644 #if EEPROM_SIZE > 0
645     if (device == i82557C || device == i82558B || device == i82559C) {
646         /*
647         TODO: get vendor id from EEPROM for i82557C or later.
648         TODO: get device id from EEPROM for i82557C or later.
649         TODO: status bit 4 can be disabled by EEPROM for i82558, i82559.
650         TODO: header type is determined by EEPROM for i82559.
651         TODO: get subsystem id from EEPROM for i82557C or later.
652         TODO: get subsystem vendor id from EEPROM for i82557C or later.
653         TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later.
654         TODO: capability pointer depends on EEPROM for i82558.
655         */
656         logout("Get device id and revision from EEPROM!!!\n");
657     }
658 #endif /* EEPROM_SIZE > 0 */
659 }
660
661 static void nic_selective_reset(EEPRO100State * s)
662 {
663     size_t i;
664     uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
665 #if 0
666     eeprom93xx_reset(s->eeprom);
667 #endif
668     memcpy(eeprom_contents, s->conf.macaddr.a, 6);
669     eeprom_contents[EEPROM_ID] = EEPROM_ID_VALID;
670     if (s->device == i82557B || s->device == i82557C)
671         eeprom_contents[5] = 0x0100;
672     eeprom_contents[EEPROM_PHY_ID] = 1;
673     uint16_t sum = 0;
674     for (i = 0; i < EEPROM_SIZE - 1; i++) {
675         sum += eeprom_contents[i];
676     }
677     eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
678     TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
679
680     memset(s->mem, 0, sizeof(s->mem));
681     uint32_t val = BIT(21);
682     memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
683
684     assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
685     memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
686 }
687
688 static void nic_reset(void *opaque)
689 {
690     EEPRO100State *s = opaque;
691     TRACE(OTHER, logout("%p\n", s));
692     /* TODO: Clearing of multicast table for selective reset, too? */
693     memset(&s->mult[0], 0, sizeof(s->mult));
694     nic_selective_reset(s);
695 }
696
697 #if defined(DEBUG_EEPRO100)
698 static const char * const e100_reg[PCI_IO_SIZE / 4] = {
699     "Command/Status",
700     "General Pointer",
701     "Port",
702     "EEPROM/Flash Control",
703     "MDI Control",
704     "Receive DMA Byte Count",
705     "Flow Control",
706     "General Status/Control"
707 };
708
709 static char *regname(uint32_t addr)
710 {
711     static char buf[32];
712     if (addr < PCI_IO_SIZE) {
713         const char *r = e100_reg[addr / 4];
714         if (r != 0) {
715             snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
716         } else {
717             snprintf(buf, sizeof(buf), "0x%02x", addr);
718         }
719     } else {
720         snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
721     }
722     return buf;
723 }
724 #endif                          /* DEBUG_EEPRO100 */
725
726 #if 0
727 static uint16_t eepro100_read_status(EEPRO100State * s)
728 {
729     uint16_t val = s->status;
730     TRACE(OTHER, logout("val=0x%04x\n", val));
731     return val;
732 }
733
734 static void eepro100_write_status(EEPRO100State * s, uint16_t val)
735 {
736     TRACE(OTHER, logout("val=0x%04x\n", val));
737     s->status = val;
738 }
739 #endif
740
741 /*****************************************************************************
742  *
743  * Command emulation.
744  *
745  ****************************************************************************/
746
747 #if 0
748 static uint16_t eepro100_read_command(EEPRO100State * s)
749 {
750     uint16_t val = 0xffff;
751     TRACE(OTHER, logout("val=0x%04x\n", val));
752     return val;
753 }
754 #endif
755
756 /* Commands that can be put in a command list entry. */
757 enum commands {
758     CmdNOp = 0,
759     CmdIASetup = 1,
760     CmdConfigure = 2,
761     CmdMulticastList = 3,
762     CmdTx = 4,
763     CmdTDR = 5,                 /* load microcode */
764     CmdDump = 6,
765     CmdDiagnose = 7,
766
767     /* And some extra flags: */
768     CmdSuspend = 0x4000,        /* Suspend after completion. */
769     CmdIntr = 0x2000,           /* Interrupt after completion. */
770     CmdTxFlex = 0x0008,         /* Use "Flexible mode" for CmdTx command. */
771 };
772
773 static cu_state_t get_cu_state(EEPRO100State * s)
774 {
775     return ((s->mem[SCBStatus] & BITS(7, 6)) >> 6);
776 }
777
778 static void set_cu_state(EEPRO100State * s, cu_state_t state)
779 {
780     s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(7, 6)) + (state << 6);
781 }
782
783 static ru_state_t get_ru_state(EEPRO100State * s)
784 {
785     return ((s->mem[SCBStatus] & BITS(5, 2)) >> 2);
786 }
787
788 static void set_ru_state(EEPRO100State * s, ru_state_t state)
789 {
790     s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(5, 2)) + (state << 2);
791 }
792
793 static void dump_statistics(EEPRO100State * s)
794 {
795     /* Dump statistical data. Most data is never changed by the emulation
796      * and always 0, so we first just copy the whole block and then those
797      * values which really matter.
798      * Number of data should check configuration!!!
799      */
800     cpu_physical_memory_write(s->statsaddr,
801                               (uint8_t *) & s->statistics, s->stats_size);
802     stl_le_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
803     stl_le_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
804     stl_le_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
805     stl_le_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
806 #if 0
807     stw_le_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
808     stw_le_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
809     missing("CU dump statistical counters");
810 #endif
811 }
812
813 static void read_cb(EEPRO100State *s)
814 {
815     cpu_physical_memory_read(s->cb_address, (uint8_t *) &s->tx, sizeof(s->tx));
816     s->tx.status = le16_to_cpu(s->tx.status);
817     s->tx.command = le16_to_cpu(s->tx.command);
818     s->tx.link = le32_to_cpu(s->tx.link);
819     s->tx.tbd_array_addr = le32_to_cpu(s->tx.tbd_array_addr);
820     s->tx.tcb_bytes = le16_to_cpu(s->tx.tcb_bytes);
821 }
822
823 static void tx_command(EEPRO100State *s)
824 {
825     uint32_t tbd_array = le32_to_cpu(s->tx.tbd_array_addr);
826     uint16_t tcb_bytes = (le16_to_cpu(s->tx.tcb_bytes) & 0x3fff);
827     /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */
828     uint8_t buf[2600];
829     uint16_t size = 0;
830     uint32_t tbd_address = s->cb_address + 0x10;
831     TRACE(RXTX, logout
832         ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
833          tbd_array, tcb_bytes, s->tx.tbd_count));
834
835     if (tcb_bytes > 2600) {
836         logout("TCB byte count too large, using 2600\n");
837         tcb_bytes = 2600;
838     }
839     if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
840         logout
841             ("illegal values of TBD array address and TCB byte count!\n");
842     }
843     assert(tcb_bytes <= sizeof(buf));
844     while (size < tcb_bytes) {
845         uint32_t tx_buffer_address = ldl_phys(tbd_address);
846         uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
847 #if 0
848         uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
849 #endif
850         tbd_address += 8;
851         TRACE(RXTX, logout
852             ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
853              tx_buffer_address, tx_buffer_size));
854         tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
855         cpu_physical_memory_read(tx_buffer_address, &buf[size],
856                                  tx_buffer_size);
857         size += tx_buffer_size;
858     }
859     if (tbd_array == 0xffffffff) {
860         /* Simplified mode. Was already handled by code above. */
861     } else {
862         /* Flexible mode. */
863         uint8_t tbd_count = 0;
864         if (s->has_extended_tcb_support && !(s->configuration[6] & BIT(4))) {
865             /* Extended Flexible TCB. */
866             for (; tbd_count < 2; tbd_count++) {
867                 uint32_t tx_buffer_address = ldl_phys(tbd_address);
868                 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
869                 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
870                 tbd_address += 8;
871                 TRACE(RXTX, logout
872                     ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
873                      tx_buffer_address, tx_buffer_size));
874                 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
875                 cpu_physical_memory_read(tx_buffer_address, &buf[size],
876                                          tx_buffer_size);
877                 size += tx_buffer_size;
878                 if (tx_buffer_el & 1) {
879                     break;
880                 }
881             }
882         }
883         tbd_address = tbd_array;
884         for (; tbd_count < s->tx.tbd_count; tbd_count++) {
885             uint32_t tx_buffer_address = ldl_phys(tbd_address);
886             uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
887             uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
888             tbd_address += 8;
889             TRACE(RXTX, logout
890                 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
891                  tx_buffer_address, tx_buffer_size));
892             tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
893             cpu_physical_memory_read(tx_buffer_address, &buf[size],
894                                      tx_buffer_size);
895             size += tx_buffer_size;
896             if (tx_buffer_el & 1) {
897                 break;
898             }
899         }
900     }
901     TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
902     qemu_send_packet(&s->nic->nc, buf, size);
903     s->statistics.tx_good_frames++;
904     /* Transmit with bad status would raise an CX/TNO interrupt.
905      * (82557 only). Emulation never has bad status. */
906 #if 0
907     eepro100_cx_interrupt(s);
908 #endif
909 }
910
911 static void set_multicast_list(EEPRO100State *s)
912 {
913     uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0);
914     uint16_t i;
915     memset(&s->mult[0], 0, sizeof(s->mult));
916     TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count));
917     for (i = 0; i < multicast_count; i += 6) {
918         uint8_t multicast_addr[6];
919         cpu_physical_memory_read(s->cb_address + 10 + i, multicast_addr, 6);
920         TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6)));
921         unsigned mcast_idx = compute_mcast_idx(multicast_addr);
922         assert(mcast_idx < 64);
923         s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
924     }
925 }
926
927 static void action_command(EEPRO100State *s)
928 {
929     for (;;) {
930         bool bit_el;
931         bool bit_s;
932         bool bit_i;
933         bool bit_nc;
934         bool success = true;
935         s->cb_address = s->cu_base + s->cu_offset;
936         read_cb(s);
937         bit_el = ((s->tx.command & COMMAND_EL) != 0);
938         bit_s = ((s->tx.command & COMMAND_S) != 0);
939         bit_i = ((s->tx.command & COMMAND_I) != 0);
940         bit_nc = ((s->tx.command & COMMAND_NC) != 0);
941 #if 0
942         bool bit_sf = ((s->tx.command & COMMAND_SF) != 0);
943 #endif
944         s->cu_offset = s->tx.link;
945         TRACE(OTHER,
946               logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
947                      s->tx.status, s->tx.command, s->tx.link));
948         switch (s->tx.command & COMMAND_CMD) {
949         case CmdNOp:
950             /* Do nothing. */
951             break;
952         case CmdIASetup:
953             cpu_physical_memory_read(s->cb_address + 8, &s->conf.macaddr.a[0], 6);
954             TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)));
955             break;
956         case CmdConfigure:
957             cpu_physical_memory_read(s->cb_address + 8, &s->configuration[0],
958                                      sizeof(s->configuration));
959             TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
960             break;
961         case CmdMulticastList:
962             set_multicast_list(s);
963             break;
964         case CmdTx:
965             if (bit_nc) {
966                 missing("CmdTx: NC = 0");
967                 success = false;
968                 break;
969             }
970             tx_command(s);
971             break;
972         case CmdTDR:
973             TRACE(OTHER, logout("load microcode\n"));
974             /* Starting with offset 8, the command contains
975              * 64 dwords microcode which we just ignore here. */
976             break;
977         case CmdDiagnose:
978             TRACE(OTHER, logout("diagnose\n"));
979             /* Make sure error flag is not set. */
980             s->tx.status = 0;
981             break;
982         default:
983             missing("undefined command");
984             success = false;
985             break;
986         }
987         /* Write new status. */
988         stw_phys(s->cb_address, s->tx.status | STATUS_C | (success ? STATUS_OK : 0));
989         if (bit_i) {
990             /* CU completed action. */
991             eepro100_cx_interrupt(s);
992         }
993         if (bit_el) {
994             /* CU becomes idle. Terminate command loop. */
995             set_cu_state(s, cu_idle);
996             eepro100_cna_interrupt(s);
997             break;
998         } else if (bit_s) {
999             /* CU becomes suspended. Terminate command loop. */
1000             set_cu_state(s, cu_suspended);
1001             eepro100_cna_interrupt(s);
1002             break;
1003         } else {
1004             /* More entries in list. */
1005             TRACE(OTHER, logout("CU list with at least one more entry\n"));
1006         }
1007     }
1008     TRACE(OTHER, logout("CU list empty\n"));
1009     /* List is empty. Now CU is idle or suspended. */
1010 }
1011
1012 static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
1013 {
1014     cu_state_t cu_state;
1015     switch (val) {
1016     case CU_NOP:
1017         /* No operation. */
1018         break;
1019     case CU_START:
1020         cu_state = get_cu_state(s);
1021         if (cu_state != cu_idle && cu_state != cu_suspended) {
1022             /* Intel documentation says that CU must be idle or suspended
1023              * for the CU start command. */
1024             logout("unexpected CU state is %u\n", cu_state);
1025         }
1026         set_cu_state(s, cu_active);
1027         s->cu_offset = s->pointer;
1028         action_command(s);
1029         break;
1030     case CU_RESUME:
1031         if (get_cu_state(s) != cu_suspended) {
1032             logout("bad CU resume from CU state %u\n", get_cu_state(s));
1033             /* Workaround for bad Linux eepro100 driver which resumes
1034              * from idle state. */
1035 #if 0
1036             missing("cu resume");
1037 #endif
1038             set_cu_state(s, cu_suspended);
1039         }
1040         if (get_cu_state(s) == cu_suspended) {
1041             TRACE(OTHER, logout("CU resuming\n"));
1042             set_cu_state(s, cu_active);
1043             action_command(s);
1044         }
1045         break;
1046     case CU_STATSADDR:
1047         /* Load dump counters address. */
1048         s->statsaddr = s->pointer;
1049         TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
1050         break;
1051     case CU_SHOWSTATS:
1052         /* Dump statistical counters. */
1053         TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
1054         dump_statistics(s);
1055         stl_le_phys(s->statsaddr + s->stats_size, 0xa005);
1056         break;
1057     case CU_CMD_BASE:
1058         /* Load CU base. */
1059         TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
1060         s->cu_base = s->pointer;
1061         break;
1062     case CU_DUMPSTATS:
1063         /* Dump and reset statistical counters. */
1064         TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
1065         dump_statistics(s);
1066         stl_le_phys(s->statsaddr + s->stats_size, 0xa007);
1067         memset(&s->statistics, 0, sizeof(s->statistics));
1068         break;
1069     case CU_SRESUME:
1070         /* CU static resume. */
1071         missing("CU static resume");
1072         break;
1073     default:
1074         missing("Undefined CU command");
1075     }
1076 }
1077
1078 static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
1079 {
1080     switch (val) {
1081     case RU_NOP:
1082         /* No operation. */
1083         break;
1084     case RX_START:
1085         /* RU start. */
1086         if (get_ru_state(s) != ru_idle) {
1087             logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
1088 #if 0
1089             assert(!"wrong RU state");
1090 #endif
1091         }
1092         set_ru_state(s, ru_ready);
1093         s->ru_offset = s->pointer;
1094         TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
1095         break;
1096     case RX_RESUME:
1097         /* Restart RU. */
1098         if (get_ru_state(s) != ru_suspended) {
1099             logout("RU state is %u, should be %u\n", get_ru_state(s),
1100                    ru_suspended);
1101 #if 0
1102             assert(!"wrong RU state");
1103 #endif
1104         }
1105         set_ru_state(s, ru_ready);
1106         break;
1107     case RU_ABORT:
1108         /* RU abort. */
1109         if (get_ru_state(s) == ru_ready) {
1110             eepro100_rnr_interrupt(s);
1111         }
1112         set_ru_state(s, ru_idle);
1113         break;
1114     case RX_ADDR_LOAD:
1115         /* Load RU base. */
1116         TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
1117         s->ru_base = s->pointer;
1118         break;
1119     default:
1120         logout("val=0x%02x (undefined RU command)\n", val);
1121         missing("Undefined SU command");
1122     }
1123 }
1124
1125 static void eepro100_write_command(EEPRO100State * s, uint8_t val)
1126 {
1127     eepro100_ru_command(s, val & 0x0f);
1128     eepro100_cu_command(s, val & 0xf0);
1129     if ((val) == 0) {
1130         TRACE(OTHER, logout("val=0x%02x\n", val));
1131     }
1132     /* Clear command byte after command was accepted. */
1133     s->mem[SCBCmd] = 0;
1134 }
1135
1136 /*****************************************************************************
1137  *
1138  * EEPROM emulation.
1139  *
1140  ****************************************************************************/
1141
1142 #define EEPROM_CS       0x02
1143 #define EEPROM_SK       0x01
1144 #define EEPROM_DI       0x04
1145 #define EEPROM_DO       0x08
1146
1147 static uint16_t eepro100_read_eeprom(EEPRO100State * s)
1148 {
1149     uint16_t val;
1150     memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
1151     if (eeprom93xx_read(s->eeprom)) {
1152         val |= EEPROM_DO;
1153     } else {
1154         val &= ~EEPROM_DO;
1155     }
1156     TRACE(EEPROM, logout("val=0x%04x\n", val));
1157     return val;
1158 }
1159
1160 static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
1161 {
1162     TRACE(EEPROM, logout("val=0x%02x\n", val));
1163
1164     /* mask unwriteable bits */
1165 #if 0
1166     val = SET_MASKED(val, 0x31, eeprom->value);
1167 #endif
1168
1169     int eecs = ((val & EEPROM_CS) != 0);
1170     int eesk = ((val & EEPROM_SK) != 0);
1171     int eedi = ((val & EEPROM_DI) != 0);
1172     eeprom93xx_write(eeprom, eecs, eesk, eedi);
1173 }
1174
1175 static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
1176 {
1177     s->pointer = le32_to_cpu(val);
1178     TRACE(OTHER, logout("val=0x%08x\n", val));
1179 }
1180
1181 /*****************************************************************************
1182  *
1183  * MDI emulation.
1184  *
1185  ****************************************************************************/
1186
1187 #if defined(DEBUG_EEPRO100)
1188 static const char * const mdi_op_name[] = {
1189     "opcode 0",
1190     "write",
1191     "read",
1192     "opcode 3"
1193 };
1194
1195 static const char * const mdi_reg_name[] = {
1196     "Control",
1197     "Status",
1198     "PHY Identification (Word 1)",
1199     "PHY Identification (Word 2)",
1200     "Auto-Negotiation Advertisement",
1201     "Auto-Negotiation Link Partner Ability",
1202     "Auto-Negotiation Expansion"
1203 };
1204
1205 static const char *reg2name(uint8_t reg)
1206 {
1207     static char buffer[10];
1208     const char *p = buffer;
1209     if (reg < ARRAY_SIZE(mdi_reg_name)) {
1210         p = mdi_reg_name[reg];
1211     } else {
1212         snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
1213     }
1214     return p;
1215 }
1216 #endif                          /* DEBUG_EEPRO100 */
1217
1218 static uint32_t eepro100_read_mdi(EEPRO100State * s)
1219 {
1220     uint32_t val;
1221     memcpy(&val, &s->mem[0x10], sizeof(val));
1222
1223 #ifdef DEBUG_EEPRO100
1224     uint8_t raiseint = (val & BIT(29)) >> 29;
1225     uint8_t opcode = (val & BITS(27, 26)) >> 26;
1226     uint8_t phy = (val & BITS(25, 21)) >> 21;
1227     uint8_t reg = (val & BITS(20, 16)) >> 16;
1228     uint16_t data = (val & BITS(15, 0));
1229 #endif
1230     /* Emulation takes no time to finish MDI transaction. */
1231     val |= BIT(28);
1232     TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1233                       val, raiseint, mdi_op_name[opcode], phy,
1234                       reg2name(reg), data));
1235     return val;
1236 }
1237
1238 static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
1239 {
1240     uint8_t raiseint = (val & BIT(29)) >> 29;
1241     uint8_t opcode = (val & BITS(27, 26)) >> 26;
1242     uint8_t phy = (val & BITS(25, 21)) >> 21;
1243     uint8_t reg = (val & BITS(20, 16)) >> 16;
1244     uint16_t data = (val & BITS(15, 0));
1245     TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1246           val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
1247     if (phy != 1) {
1248         /* Unsupported PHY address. */
1249 #if 0
1250         logout("phy must be 1 but is %u\n", phy);
1251 #endif
1252         data = 0;
1253     } else if (opcode != 1 && opcode != 2) {
1254         /* Unsupported opcode. */
1255         logout("opcode must be 1 or 2 but is %u\n", opcode);
1256         data = 0;
1257     } else if (reg > 6) {
1258         /* Unsupported register. */
1259         logout("register must be 0...6 but is %u\n", reg);
1260         data = 0;
1261     } else {
1262         TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1263                           val, raiseint, mdi_op_name[opcode], phy,
1264                           reg2name(reg), data));
1265         if (opcode == 1) {
1266             /* MDI write */
1267             switch (reg) {
1268             case 0:            /* Control Register */
1269                 if (data & 0x8000) {
1270                     /* Reset status and control registers to default. */
1271                     s->mdimem[0] = eepro100_mdi_default[0];
1272                     s->mdimem[1] = eepro100_mdi_default[1];
1273                     data = s->mdimem[reg];
1274                 } else {
1275                     /* Restart Auto Configuration = Normal Operation */
1276                     data &= ~0x0200;
1277                 }
1278                 break;
1279             case 1:            /* Status Register */
1280                 missing("not writable");
1281                 data = s->mdimem[reg];
1282                 break;
1283             case 2:            /* PHY Identification Register (Word 1) */
1284             case 3:            /* PHY Identification Register (Word 2) */
1285                 missing("not implemented");
1286                 break;
1287             case 4:            /* Auto-Negotiation Advertisement Register */
1288             case 5:            /* Auto-Negotiation Link Partner Ability Register */
1289                 break;
1290             case 6:            /* Auto-Negotiation Expansion Register */
1291             default:
1292                 missing("not implemented");
1293             }
1294             s->mdimem[reg] = data;
1295         } else if (opcode == 2) {
1296             /* MDI read */
1297             switch (reg) {
1298             case 0:            /* Control Register */
1299                 if (data & 0x8000) {
1300                     /* Reset status and control registers to default. */
1301                     s->mdimem[0] = eepro100_mdi_default[0];
1302                     s->mdimem[1] = eepro100_mdi_default[1];
1303                 }
1304                 break;
1305             case 1:            /* Status Register */
1306                 s->mdimem[reg] |= 0x0020;
1307                 break;
1308             case 2:            /* PHY Identification Register (Word 1) */
1309             case 3:            /* PHY Identification Register (Word 2) */
1310             case 4:            /* Auto-Negotiation Advertisement Register */
1311                 break;
1312             case 5:            /* Auto-Negotiation Link Partner Ability Register */
1313                 s->mdimem[reg] = 0x41fe;
1314                 break;
1315             case 6:            /* Auto-Negotiation Expansion Register */
1316                 s->mdimem[reg] = 0x0001;
1317                 break;
1318             }
1319             data = s->mdimem[reg];
1320         }
1321         /* Emulation takes no time to finish MDI transaction.
1322          * Set MDI bit in SCB status register. */
1323         s->mem[SCBAck] |= 0x08;
1324         val |= BIT(28);
1325         if (raiseint) {
1326             eepro100_mdi_interrupt(s);
1327         }
1328     }
1329     val = (val & 0xffff0000) + data;
1330     memcpy(&s->mem[0x10], &val, sizeof(val));
1331 }
1332
1333 /*****************************************************************************
1334  *
1335  * Port emulation.
1336  *
1337  ****************************************************************************/
1338
1339 #define PORT_SOFTWARE_RESET     0
1340 #define PORT_SELFTEST           1
1341 #define PORT_SELECTIVE_RESET    2
1342 #define PORT_DUMP               3
1343 #define PORT_SELECTION_MASK     3
1344
1345 typedef struct {
1346     uint32_t st_sign;           /* Self Test Signature */
1347     uint32_t st_result;         /* Self Test Results */
1348 } eepro100_selftest_t;
1349
1350 static uint32_t eepro100_read_port(EEPRO100State * s)
1351 {
1352     return 0;
1353 }
1354
1355 static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1356 {
1357     val = le32_to_cpu(val);
1358     uint32_t address = (val & ~PORT_SELECTION_MASK);
1359     uint8_t selection = (val & PORT_SELECTION_MASK);
1360     switch (selection) {
1361     case PORT_SOFTWARE_RESET:
1362         nic_reset(s);
1363         break;
1364     case PORT_SELFTEST:
1365         TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1366         eepro100_selftest_t data;
1367         cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1368         data.st_sign = 0xffffffff;
1369         data.st_result = 0;
1370         cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1371         break;
1372     case PORT_SELECTIVE_RESET:
1373         TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1374         nic_selective_reset(s);
1375         break;
1376     default:
1377         logout("val=0x%08x\n", val);
1378         missing("unknown port selection");
1379     }
1380 }
1381
1382 /*****************************************************************************
1383  *
1384  * General hardware emulation.
1385  *
1386  ****************************************************************************/
1387
1388 static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1389 {
1390     uint8_t val;
1391     if (addr <= sizeof(s->mem) - sizeof(val)) {
1392         memcpy(&val, &s->mem[addr], sizeof(val));
1393     }
1394
1395     switch (addr) {
1396     case SCBStatus:
1397 #if 0
1398         val = eepro100_read_status(s);
1399 #endif
1400         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1401         break;
1402     case SCBAck:
1403 #if 0
1404         val = eepro100_read_status(s);
1405 #endif
1406         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1407         break;
1408     case SCBCmd:
1409         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1410 #if 0
1411         val = eepro100_read_command(s);
1412 #endif
1413         break;
1414     case SCBIntmask:
1415         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1416         break;
1417     case SCBPort + 3:
1418         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1419         break;
1420     case SCBeeprom:
1421         val = eepro100_read_eeprom(s);
1422         break;
1423     case SCBpmdr:       /* Power Management Driver Register */
1424         val = 0;
1425         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1426         break;
1427     case SCBgstat:      /* General Status Register */
1428         /* 100 Mbps full duplex, valid link */
1429         val = 0x07;
1430         TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1431         break;
1432     default:
1433         logout("addr=%s val=0x%02x\n", regname(addr), val);
1434         missing("unknown byte read");
1435     }
1436     return val;
1437 }
1438
1439 static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1440 {
1441     uint16_t val;
1442     if (addr <= sizeof(s->mem) - sizeof(val)) {
1443         memcpy(&val, &s->mem[addr], sizeof(val));
1444     }
1445
1446     switch (addr) {
1447     case SCBStatus:
1448 #if 0
1449         val = eepro100_read_status(s);
1450 #endif
1451     case SCBCmd:
1452         TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1453         break;
1454     case SCBeeprom:
1455         val = eepro100_read_eeprom(s);
1456         TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1457         break;
1458     default:
1459         logout("addr=%s val=0x%04x\n", regname(addr), val);
1460         missing("unknown word read");
1461     }
1462     return val;
1463 }
1464
1465 static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1466 {
1467     uint32_t val;
1468     if (addr <= sizeof(s->mem) - sizeof(val)) {
1469         memcpy(&val, &s->mem[addr], sizeof(val));
1470     }
1471
1472     switch (addr) {
1473     case SCBStatus:
1474 #if 0
1475         val = eepro100_read_status(s);
1476 #endif
1477         TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1478         break;
1479     case SCBPointer:
1480 #if 0
1481         val = eepro100_read_pointer(s);
1482 #endif
1483         TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1484         break;
1485     case SCBPort:
1486         val = eepro100_read_port(s);
1487         TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1488         break;
1489     case SCBCtrlMDI:
1490         val = eepro100_read_mdi(s);
1491         break;
1492     default:
1493         logout("addr=%s val=0x%08x\n", regname(addr), val);
1494         missing("unknown longword read");
1495     }
1496     return val;
1497 }
1498
1499 static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1500 {
1501     if (addr <= sizeof(s->mem) - sizeof(val)) {
1502         memcpy(&s->mem[addr], &val, sizeof(val));
1503     }
1504
1505     TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1506
1507     switch (addr) {
1508     case SCBStatus:
1509 #if 0
1510         eepro100_write_status(s, val);
1511 #endif
1512         break;
1513     case SCBAck:
1514         eepro100_acknowledge(s);
1515         break;
1516     case SCBCmd:
1517         eepro100_write_command(s, val);
1518         break;
1519     case SCBIntmask:
1520         if (val & BIT(1)) {
1521             eepro100_swi_interrupt(s);
1522         }
1523         eepro100_interrupt(s, 0);
1524         break;
1525     case SCBPort + 3:
1526     case SCBFlow:       /* does not exist on 82557 */
1527     case SCBFlow + 1:
1528     case SCBFlow + 2:
1529     case SCBpmdr:       /* does not exist on 82557 */
1530         TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1531         break;
1532     case SCBeeprom:
1533         eepro100_write_eeprom(s->eeprom, val);
1534         break;
1535     default:
1536         logout("addr=%s val=0x%02x\n", regname(addr), val);
1537         missing("unknown byte write");
1538     }
1539 }
1540
1541 static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1542 {
1543     if (addr <= sizeof(s->mem) - sizeof(val)) {
1544         memcpy(&s->mem[addr], &val, sizeof(val));
1545     }
1546
1547     TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1548
1549     switch (addr) {
1550     case SCBStatus:
1551 #if 0
1552         eepro100_write_status(s, val);
1553 #endif
1554         eepro100_acknowledge(s);
1555         break;
1556     case SCBCmd:
1557         eepro100_write_command(s, val);
1558         eepro100_write1(s, SCBIntmask, val >> 8);
1559         break;
1560     case SCBeeprom:
1561         eepro100_write_eeprom(s->eeprom, val);
1562         break;
1563     default:
1564         logout("addr=%s val=0x%04x\n", regname(addr), val);
1565         missing("unknown word write");
1566     }
1567 }
1568
1569 static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1570 {
1571     if (addr <= sizeof(s->mem) - sizeof(val)) {
1572         memcpy(&s->mem[addr], &val, sizeof(val));
1573     }
1574
1575     switch (addr) {
1576     case SCBPointer:
1577         eepro100_write_pointer(s, val);
1578         break;
1579     case SCBPort:
1580         TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1581         eepro100_write_port(s, val);
1582         break;
1583     case SCBCtrlMDI:
1584         eepro100_write_mdi(s, val);
1585         break;
1586     default:
1587         logout("addr=%s val=0x%08x\n", regname(addr), val);
1588         missing("unknown longword write");
1589     }
1590 }
1591
1592 /*****************************************************************************
1593  *
1594  * Port mapped I/O.
1595  *
1596  ****************************************************************************/
1597
1598 static uint32_t ioport_read1(void *opaque, uint32_t addr)
1599 {
1600     EEPRO100State *s = opaque;
1601 #if 0
1602     logout("addr=%s\n", regname(addr));
1603 #endif
1604     return eepro100_read1(s, addr - s->region[1]);
1605 }
1606
1607 static uint32_t ioport_read2(void *opaque, uint32_t addr)
1608 {
1609     EEPRO100State *s = opaque;
1610     return eepro100_read2(s, addr - s->region[1]);
1611 }
1612
1613 static uint32_t ioport_read4(void *opaque, uint32_t addr)
1614 {
1615     EEPRO100State *s = opaque;
1616     return eepro100_read4(s, addr - s->region[1]);
1617 }
1618
1619 static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1620 {
1621     EEPRO100State *s = opaque;
1622 #if 0
1623     logout("addr=%s val=0x%02x\n", regname(addr), val);
1624 #endif
1625     eepro100_write1(s, addr - s->region[1], val);
1626 }
1627
1628 static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1629 {
1630     EEPRO100State *s = opaque;
1631     eepro100_write2(s, addr - s->region[1], val);
1632 }
1633
1634 static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1635 {
1636     EEPRO100State *s = opaque;
1637     eepro100_write4(s, addr - s->region[1], val);
1638 }
1639
1640 /***********************************************************/
1641 /* PCI EEPRO100 definitions */
1642
1643 static void pci_map(PCIDevice * pci_dev, int region_num,
1644                     pcibus_t addr, pcibus_t size, int type)
1645 {
1646     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1647
1648     TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
1649           "size=0x%08"FMT_PCIBUS", type=%d\n",
1650           region_num, addr, size, type));
1651
1652     assert(region_num == 1);
1653     register_ioport_write(addr, size, 1, ioport_write1, s);
1654     register_ioport_read(addr, size, 1, ioport_read1, s);
1655     register_ioport_write(addr, size, 2, ioport_write2, s);
1656     register_ioport_read(addr, size, 2, ioport_read2, s);
1657     register_ioport_write(addr, size, 4, ioport_write4, s);
1658     register_ioport_read(addr, size, 4, ioport_read4, s);
1659
1660     s->region[region_num] = addr;
1661 }
1662
1663 /*****************************************************************************
1664  *
1665  * Memory mapped I/O.
1666  *
1667  ****************************************************************************/
1668
1669 static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1670 {
1671     EEPRO100State *s = opaque;
1672 #if 0
1673     logout("addr=%s val=0x%02x\n", regname(addr), val);
1674 #endif
1675     eepro100_write1(s, addr, val);
1676 }
1677
1678 static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1679 {
1680     EEPRO100State *s = opaque;
1681 #if 0
1682     logout("addr=%s val=0x%02x\n", regname(addr), val);
1683 #endif
1684     eepro100_write2(s, addr, val);
1685 }
1686
1687 static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1688 {
1689     EEPRO100State *s = opaque;
1690 #if 0
1691     logout("addr=%s val=0x%02x\n", regname(addr), val);
1692 #endif
1693     eepro100_write4(s, addr, val);
1694 }
1695
1696 static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1697 {
1698     EEPRO100State *s = opaque;
1699 #if 0
1700     logout("addr=%s\n", regname(addr));
1701 #endif
1702     return eepro100_read1(s, addr);
1703 }
1704
1705 static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1706 {
1707     EEPRO100State *s = opaque;
1708 #if 0
1709     logout("addr=%s\n", regname(addr));
1710 #endif
1711     return eepro100_read2(s, addr);
1712 }
1713
1714 static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1715 {
1716     EEPRO100State *s = opaque;
1717 #if 0
1718     logout("addr=%s\n", regname(addr));
1719 #endif
1720     return eepro100_read4(s, addr);
1721 }
1722
1723 static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1724     pci_mmio_writeb,
1725     pci_mmio_writew,
1726     pci_mmio_writel
1727 };
1728
1729 static CPUReadMemoryFunc * const pci_mmio_read[] = {
1730     pci_mmio_readb,
1731     pci_mmio_readw,
1732     pci_mmio_readl
1733 };
1734
1735 static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1736                          pcibus_t addr, pcibus_t size, int type)
1737 {
1738     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1739
1740     TRACE(OTHER, logout("region %d, addr=0x%08"FMT_PCIBUS", "
1741           "size=0x%08"FMT_PCIBUS", type=%d\n",
1742           region_num, addr, size, type));
1743
1744     if (region_num == 0) {
1745         /* Map control / status registers. */
1746         cpu_register_physical_memory(addr, size, s->mmio_index);
1747         s->region[region_num] = addr;
1748     }
1749 }
1750
1751 static int nic_can_receive(VLANClientState *nc)
1752 {
1753     EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1754     TRACE(RXTX, logout("%p\n", s));
1755     return get_ru_state(s) == ru_ready;
1756 #if 0
1757     return !eepro100_buffer_full(s);
1758 #endif
1759 }
1760
1761 static ssize_t nic_receive(VLANClientState *nc, const uint8_t * buf, size_t size)
1762 {
1763     /* TODO:
1764      * - Magic packets should set bit 30 in power management driver register.
1765      * - Interesting packets should set bit 29 in power management driver register.
1766      */
1767     EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1768     uint16_t rfd_status = 0xa000;
1769     static const uint8_t broadcast_macaddr[6] =
1770         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1771
1772     /* TODO: check multiple IA bit. */
1773     if (s->configuration[20] & BIT(6)) {
1774         missing("Multiple IA bit");
1775         return -1;
1776     }
1777
1778     if (s->configuration[8] & 0x80) {
1779         /* CSMA is disabled. */
1780         logout("%p received while CSMA is disabled\n", s);
1781         return -1;
1782     } else if (size < 64 && (s->configuration[7] & BIT(0))) {
1783         /* Short frame and configuration byte 7/0 (discard short receive) set:
1784          * Short frame is discarded */
1785         logout("%p received short frame (%zu byte)\n", s, size);
1786         s->statistics.rx_short_frame_errors++;
1787 #if 0
1788         return -1;
1789 #endif
1790     } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & BIT(3))) {
1791         /* Long frame and configuration byte 18/3 (long receive ok) not set:
1792          * Long frames are discarded. */
1793         logout("%p received long frame (%zu byte), ignored\n", s, size);
1794         return -1;
1795     } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) {       /* !!! */
1796         /* Frame matches individual address. */
1797         /* TODO: check configuration byte 15/4 (ignore U/L). */
1798         TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
1799     } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1800         /* Broadcast frame. */
1801         TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
1802         rfd_status |= 0x0002;
1803     } else if (buf[0] & 0x01) {
1804         /* Multicast frame. */
1805         TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size)));
1806         if (s->configuration[21] & BIT(3)) {
1807           /* Multicast all bit is set, receive all multicast frames. */
1808         } else {
1809           unsigned mcast_idx = compute_mcast_idx(buf);
1810           assert(mcast_idx < 64);
1811           if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
1812             /* Multicast frame is allowed in hash table. */
1813           } else if (s->configuration[15] & BIT(0)) {
1814               /* Promiscuous: receive all. */
1815               rfd_status |= 0x0004;
1816           } else {
1817               TRACE(RXTX, logout("%p multicast ignored\n", s));
1818               return -1;
1819           }
1820         }
1821         /* TODO: Next not for promiscuous mode? */
1822         rfd_status |= 0x0002;
1823     } else if (s->configuration[15] & BIT(0)) {
1824         /* Promiscuous: receive all. */
1825         TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
1826         rfd_status |= 0x0004;
1827     } else {
1828         TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1829               nic_dump(buf, size)));
1830         return size;
1831     }
1832
1833     if (get_ru_state(s) != ru_ready) {
1834         /* No resources available. */
1835         logout("no resources, state=%u\n", get_ru_state(s));
1836         /* TODO: RNR interrupt only at first failed frame? */
1837         eepro100_rnr_interrupt(s);
1838         s->statistics.rx_resource_errors++;
1839 #if 0
1840         assert(!"no resources");
1841 #endif
1842         return -1;
1843     }
1844     /* !!! */
1845     eepro100_rx_t rx;
1846     cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1847                              offsetof(eepro100_rx_t, packet));
1848     uint16_t rfd_command = le16_to_cpu(rx.command);
1849     uint16_t rfd_size = le16_to_cpu(rx.size);
1850
1851     if (size > rfd_size) {
1852         logout("Receive buffer (%" PRId16 " bytes) too small for data "
1853             "(%zu bytes); data truncated\n", rfd_size, size);
1854         size = rfd_size;
1855     }
1856     if (size < 64) {
1857         rfd_status |= 0x0080;
1858     }
1859     TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1860           rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
1861     stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1862              rfd_status);
1863     stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1864     /* Early receive interrupt not supported. */
1865 #if 0
1866     eepro100_er_interrupt(s);
1867 #endif
1868     /* Receive CRC Transfer not supported. */
1869     if (s->configuration[18] & BIT(2)) {
1870         missing("Receive CRC Transfer");
1871         return -1;
1872     }
1873     /* TODO: check stripping enable bit. */
1874 #if 0
1875     assert(!(s->configuration[17] & BIT(0)));
1876 #endif
1877     cpu_physical_memory_write(s->ru_base + s->ru_offset +
1878                               offsetof(eepro100_rx_t, packet), buf, size);
1879     s->statistics.rx_good_frames++;
1880     eepro100_fr_interrupt(s);
1881     s->ru_offset = le32_to_cpu(rx.link);
1882     if (rfd_command & COMMAND_EL) {
1883         /* EL bit is set, so this was the last frame. */
1884         logout("receive: Running out of frames\n");
1885         set_ru_state(s, ru_suspended);
1886     }
1887     if (rfd_command & COMMAND_S) {
1888         /* S bit is set. */
1889         set_ru_state(s, ru_suspended);
1890     }
1891     return size;
1892 }
1893
1894 static const VMStateDescription vmstate_eepro100 = {
1895     .version_id = 3,
1896     .minimum_version_id = 2,
1897     .minimum_version_id_old = 2,
1898     .fields      = (VMStateField []) {
1899         VMSTATE_PCI_DEVICE(dev, EEPRO100State),
1900         VMSTATE_UNUSED(32),
1901         VMSTATE_BUFFER(mult, EEPRO100State),
1902         VMSTATE_BUFFER(mem, EEPRO100State),
1903         /* Save all members of struct between scb_stat and mem. */
1904         VMSTATE_UINT8(scb_stat, EEPRO100State),
1905         VMSTATE_UINT8(int_stat, EEPRO100State),
1906         VMSTATE_UNUSED(3*4),
1907         VMSTATE_MACADDR(conf.macaddr, EEPRO100State),
1908         VMSTATE_UNUSED(19*4),
1909         VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32),
1910         /* The eeprom should be saved and restored by its own routines. */
1911         VMSTATE_UINT32(device, EEPRO100State),
1912         /* TODO check device. */
1913         VMSTATE_UINT32(pointer, EEPRO100State),
1914         VMSTATE_UINT32(cu_base, EEPRO100State),
1915         VMSTATE_UINT32(cu_offset, EEPRO100State),
1916         VMSTATE_UINT32(ru_base, EEPRO100State),
1917         VMSTATE_UINT32(ru_offset, EEPRO100State),
1918         VMSTATE_UINT32(statsaddr, EEPRO100State),
1919         /* Save eepro100_stats_t statistics. */
1920         VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State),
1921         VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State),
1922         VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State),
1923         VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State),
1924         VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State),
1925         VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State),
1926         VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State),
1927         VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State),
1928         VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State),
1929         VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State),
1930         VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State),
1931         VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State),
1932         VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State),
1933         VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State),
1934         VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State),
1935         VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State),
1936         VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State),
1937         VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State),
1938         VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State),
1939         VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State),
1940         VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State),
1941 #if 0
1942         VMSTATE_UINT16(status, EEPRO100State),
1943 #endif
1944         /* Configuration bytes. */
1945         VMSTATE_BUFFER(configuration, EEPRO100State),
1946         VMSTATE_END_OF_LIST()
1947     }
1948 };
1949
1950 static void nic_cleanup(VLANClientState *nc)
1951 {
1952     EEPRO100State *s = DO_UPCAST(NICState, nc, nc)->opaque;
1953
1954     s->nic = NULL;
1955 }
1956
1957 static int pci_nic_uninit(PCIDevice *pci_dev)
1958 {
1959     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1960
1961     cpu_unregister_io_memory(s->mmio_index);
1962     vmstate_unregister(s->vmstate, s);
1963     eeprom93xx_free(s->eeprom);
1964     qemu_del_vlan_client(&s->nic->nc);
1965     return 0;
1966 }
1967
1968 static NetClientInfo net_eepro100_info = {
1969     .type = NET_CLIENT_TYPE_NIC,
1970     .size = sizeof(NICState),
1971     .can_receive = nic_can_receive,
1972     .receive = nic_receive,
1973     .cleanup = nic_cleanup,
1974 };
1975
1976 static int nic_init(PCIDevice *pci_dev, uint32_t device)
1977 {
1978     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1979
1980     TRACE(OTHER, logout("\n"));
1981
1982     s->device = device;
1983
1984     pci_reset(s);
1985
1986     /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1987      * i82559 and later support 64 or 256 word EEPROM. */
1988     s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1989
1990     /* Handler for memory-mapped I/O */
1991     s->mmio_index =
1992         cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1993
1994     pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1995                            PCI_BASE_ADDRESS_SPACE_MEMORY |
1996                            PCI_BASE_ADDRESS_MEM_PREFETCH, pci_mmio_map);
1997     pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_BASE_ADDRESS_SPACE_IO,
1998                            pci_map);
1999     pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_BASE_ADDRESS_SPACE_MEMORY,
2000                            pci_mmio_map);
2001
2002     qemu_macaddr_default_if_unset(&s->conf.macaddr);
2003     logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
2004     assert(s->region[1] == 0);
2005
2006     nic_reset(s);
2007
2008     s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
2009                           pci_dev->qdev.info->name, pci_dev->qdev.id, s);
2010
2011     qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
2012     TRACE(OTHER, logout("%s\n", s->nic->nc.info_str));
2013
2014     qemu_register_reset(nic_reset, s);
2015
2016     s->vmstate = qemu_malloc(sizeof(vmstate_eepro100));
2017     memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
2018     s->vmstate->name = s->nic->nc.model;
2019     vmstate_register(-1, s->vmstate, s);
2020
2021     return 0;
2022 }
2023
2024 static int pci_i82550_init(PCIDevice *pci_dev)
2025 {
2026     return nic_init(pci_dev, i82550);
2027 }
2028
2029 static int pci_i82551_init(PCIDevice *pci_dev)
2030 {
2031     return nic_init(pci_dev, i82551);
2032 }
2033
2034 static int pci_i82557a_init(PCIDevice *pci_dev)
2035 {
2036     return nic_init(pci_dev, i82557A);
2037 }
2038
2039 static int pci_i82557b_init(PCIDevice *pci_dev)
2040 {
2041     return nic_init(pci_dev, i82557B);
2042 }
2043
2044 static int pci_i82557c_init(PCIDevice *pci_dev)
2045 {
2046     return nic_init(pci_dev, i82557C);
2047 }
2048
2049 static int pci_i82558a_init(PCIDevice *pci_dev)
2050 {
2051     return nic_init(pci_dev, i82558A);
2052 }
2053
2054 static int pci_i82558b_init(PCIDevice *pci_dev)
2055 {
2056     return nic_init(pci_dev, i82558B);
2057 }
2058
2059 static int pci_i82559a_init(PCIDevice *pci_dev)
2060 {
2061     return nic_init(pci_dev, i82559A);
2062 }
2063
2064 static int pci_i82559b_init(PCIDevice *pci_dev)
2065 {
2066     return nic_init(pci_dev, i82559B);
2067 }
2068
2069 static int pci_i82559c_init(PCIDevice *pci_dev)
2070 {
2071     return nic_init(pci_dev, i82559C);
2072 }
2073
2074 static int pci_i82559er_init(PCIDevice *pci_dev)
2075 {
2076     return nic_init(pci_dev, i82559ER);
2077 }
2078
2079 static int pci_i82562_init(PCIDevice *pci_dev)
2080 {
2081     return nic_init(pci_dev, i82562);
2082 }
2083
2084 static PCIDeviceInfo eepro100_info[] = {
2085     {
2086         .qdev.name = "i82550",
2087         .qdev.desc = "Intel i82550 Ethernet",
2088         .qdev.size = sizeof(EEPRO100State),
2089         .init      = pci_i82550_init,
2090         .exit      = pci_nic_uninit,
2091         .romfile   = "gpxe-eepro100-80861209.rom",
2092         .qdev.props = (Property[]) {
2093             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2094             DEFINE_PROP_END_OF_LIST(),
2095         },
2096     },{
2097         .qdev.name = "i82551",
2098         .qdev.desc = "Intel i82551 Ethernet",
2099         .qdev.size = sizeof(EEPRO100State),
2100         .init      = pci_i82551_init,
2101         .exit      = pci_nic_uninit,
2102         .romfile   = "gpxe-eepro100-80861209.rom",
2103         .qdev.props = (Property[]) {
2104             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2105             DEFINE_PROP_END_OF_LIST(),
2106         },
2107     },{
2108         .qdev.name = "i82557a",
2109         .qdev.desc = "Intel i82557A Ethernet",
2110         .qdev.size = sizeof(EEPRO100State),
2111         .init      = pci_i82557a_init,
2112         .exit      = pci_nic_uninit,
2113         .romfile   = "gpxe-eepro100-80861229.rom",
2114         .qdev.props = (Property[]) {
2115             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2116             DEFINE_PROP_END_OF_LIST(),
2117         },
2118     },{
2119         .qdev.name = "i82557b",
2120         .qdev.desc = "Intel i82557B Ethernet",
2121         .qdev.size = sizeof(EEPRO100State),
2122         .init      = pci_i82557b_init,
2123         .exit      = pci_nic_uninit,
2124         .romfile   = "gpxe-eepro100-80861229.rom",
2125         .qdev.props = (Property[]) {
2126             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2127             DEFINE_PROP_END_OF_LIST(),
2128         },
2129     },{
2130         .qdev.name = "i82557c",
2131         .qdev.desc = "Intel i82557C Ethernet",
2132         .qdev.size = sizeof(EEPRO100State),
2133         .init      = pci_i82557c_init,
2134         .exit      = pci_nic_uninit,
2135         .romfile   = "gpxe-eepro100-80861229.rom",
2136         .qdev.props = (Property[]) {
2137             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2138             DEFINE_PROP_END_OF_LIST(),
2139         },
2140     },{
2141         .qdev.name = "i82558a",
2142         .qdev.desc = "Intel i82558A Ethernet",
2143         .qdev.size = sizeof(EEPRO100State),
2144         .init      = pci_i82558a_init,
2145         .exit      = pci_nic_uninit,
2146         .romfile   = "gpxe-eepro100-80861229.rom",
2147         .qdev.props = (Property[]) {
2148             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2149             DEFINE_PROP_END_OF_LIST(),
2150         },
2151     },{
2152         .qdev.name = "i82558b",
2153         .qdev.desc = "Intel i82558B Ethernet",
2154         .qdev.size = sizeof(EEPRO100State),
2155         .init      = pci_i82558b_init,
2156         .exit      = pci_nic_uninit,
2157         .romfile   = "gpxe-eepro100-80861229.rom",
2158         .qdev.props = (Property[]) {
2159             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2160             DEFINE_PROP_END_OF_LIST(),
2161         },
2162     },{
2163         .qdev.name = "i82559a",
2164         .qdev.desc = "Intel i82559A Ethernet",
2165         .qdev.size = sizeof(EEPRO100State),
2166         .init      = pci_i82559a_init,
2167         .exit      = pci_nic_uninit,
2168         .romfile   = "gpxe-eepro100-80861229.rom",
2169         .qdev.props = (Property[]) {
2170             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2171             DEFINE_PROP_END_OF_LIST(),
2172         },
2173     },{
2174         .qdev.name = "i82559b",
2175         .qdev.desc = "Intel i82559B Ethernet",
2176         .qdev.size = sizeof(EEPRO100State),
2177         .init      = pci_i82559b_init,
2178         .exit      = pci_nic_uninit,
2179         .romfile   = "gpxe-eepro100-80861229.rom",
2180         .qdev.props = (Property[]) {
2181             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2182             DEFINE_PROP_END_OF_LIST(),
2183         },
2184     },{
2185         .qdev.name = "i82559c",
2186         .qdev.desc = "Intel i82559C Ethernet",
2187         .qdev.size = sizeof(EEPRO100State),
2188         .init      = pci_i82559c_init,
2189         .exit      = pci_nic_uninit,
2190         .romfile   = "gpxe-eepro100-80861229.rom",
2191         .qdev.props = (Property[]) {
2192             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2193             DEFINE_PROP_END_OF_LIST(),
2194         },
2195     },{
2196         .qdev.name = "i82559er",
2197         .qdev.desc = "Intel i82559ER Ethernet",
2198         .qdev.size = sizeof(EEPRO100State),
2199         .init      = pci_i82559er_init,
2200         .exit      = pci_nic_uninit,
2201         .romfile   = "gpxe-eepro100-80861209.rom",
2202         .qdev.props = (Property[]) {
2203             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2204             DEFINE_PROP_END_OF_LIST(),
2205         },
2206     },{
2207         .qdev.name = "i82562",
2208         .qdev.desc = "Intel i82562 Ethernet",
2209         .qdev.size = sizeof(EEPRO100State),
2210         .init      = pci_i82562_init,
2211         .exit      = pci_nic_uninit,
2212         .romfile   = "gpxe-eepro100-80861209.rom",
2213         .qdev.props = (Property[]) {
2214             DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2215             DEFINE_PROP_END_OF_LIST(),
2216         },
2217     },{
2218         /* end of list */
2219     }
2220 };
2221
2222 static void eepro100_register_devices(void)
2223 {
2224     pci_qdev_register_many(eepro100_info);
2225 }
2226
2227 device_init(eepro100_register_devices)
This page took 0.149271 seconds and 4 git commands to generate.