usb: Fix compilation for Windows
[qemu/ar7.git] / hw / net / eepro100.c
blobed49ebd850dbdcafabea33e435376597a817711b
1 /*
2 * QEMU i8255x (PRO100) emulation
4 * Copyright (C) 2006-2011 Stefan Weil
6 * Portions of the code are copies from grub / etherboot eepro100.c
7 * and linux e100.c.
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.
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.
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/>.
22 * Tested features (i82559c):
23 * PXE boot (i386 guest, i386 / ppc host) ok
24 * Linux networking (i386 guest, i386 / ppc / ppc64 host) ok
25 * Linux networking e100 driver (mips / mipsel guest, i386 host) ok
26 * Linux networking eepro100 driver (mipsel guest) not ok
27 * Windows networking (Vista) not ok
29 * References:
31 * Intel 8255x 10/100 Mbps Ethernet Controller Family
32 * Open Source Software Developer Manual
34 * TODO:
35 * * PHY emulation should be separated from nic emulation.
36 * Most nic emulations could share the same phy code.
37 * * i82550 is untested. It is programmed like the i82559.
38 * * i82562 is untested. It is programmed like the i82559.
39 * * Power management (i82558 and later) is not implemented.
40 * * Wake-on-LAN is not implemented.
43 #include "qemu/osdep.h"
44 #include "hw/hw.h"
45 #include "hw/pci/pci.h"
46 #include "net/net.h"
47 #include "hw/nvram/eeprom93xx.h"
48 #include "sysemu/sysemu.h"
49 #include "sysemu/dma.h"
50 #include "qemu/bitops.h"
52 /* QEMU sends frames smaller than 60 bytes to ethernet nics.
53 * This should be fixed in the networking code because normally
54 * such frames are rejected by real nics and their emulations.
55 * To avoid this behaviour, other nic emulations pad received
56 * frames. The following definition enables this workaround for
57 * eepro100, too. */
58 #define CONFIG_PAD_RECEIVED_FRAMES
60 #define KiB 1024
62 /* Debug EEPRO100 card. */
63 #if 0
64 # define DEBUG_EEPRO100
65 #endif
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
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 */
80 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
82 #define UNEXPECTED() logout("%s:%u unexpected\n", __FILE__, __LINE__)
84 #if 0
85 #define missing(text) assert(!"feature is missing in this emulation: " text)
86 #else
87 #define missing(text) logout("feature is missing in this emulation: " text "\n")
88 #endif
90 #define MAX_ETH_FRAME_SIZE 1514
92 /* This driver supports several different devices which are declared here. */
93 #define i82550 0x82550
94 #define i82551 0x82551
95 #define i82557A 0x82557a
96 #define i82557B 0x82557b
97 #define i82557C 0x82557c
98 #define i82558A 0x82558a
99 #define i82558B 0x82558b
100 #define i82559A 0x82559a
101 #define i82559B 0x82559b
102 #define i82559C 0x82559c
103 #define i82559ER 0x82559e
104 #define i82562 0x82562
105 #define i82801 0x82801
107 /* Use 64 word EEPROM. TODO: could be a runtime option. */
108 #define EEPROM_SIZE 64
110 #define PCI_MEM_SIZE (4 * KiB)
111 #define PCI_IO_SIZE 64
112 #define PCI_FLASH_SIZE (128 * KiB)
114 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
116 /* The SCB accepts the following controls for the Tx and Rx units: */
117 #define CU_NOP 0x0000 /* No operation. */
118 #define CU_START 0x0010 /* CU start. */
119 #define CU_RESUME 0x0020 /* CU resume. */
120 #define CU_STATSADDR 0x0040 /* Load dump counters address. */
121 #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
122 #define CU_CMD_BASE 0x0060 /* Load CU base address. */
123 #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
124 #define CU_SRESUME 0x00a0 /* CU static resume. */
126 #define RU_NOP 0x0000
127 #define RX_START 0x0001
128 #define RX_RESUME 0x0002
129 #define RU_ABORT 0x0004
130 #define RX_ADDR_LOAD 0x0006
131 #define RX_RESUMENR 0x0007
132 #define INT_MASK 0x0100
133 #define DRVR_INT 0x0200 /* Driver generated interrupt. */
135 typedef struct {
136 const char *name;
137 const char *desc;
138 uint16_t device_id;
139 uint8_t revision;
140 uint16_t subsystem_vendor_id;
141 uint16_t subsystem_id;
143 uint32_t device;
144 uint8_t stats_size;
145 bool has_extended_tcb_support;
146 bool power_management;
147 } E100PCIDeviceInfo;
149 /* Offsets to the various registers.
150 All accesses need not be longword aligned. */
151 typedef enum {
152 SCBStatus = 0, /* Status Word. */
153 SCBAck = 1,
154 SCBCmd = 2, /* Rx/Command Unit command and status. */
155 SCBIntmask = 3,
156 SCBPointer = 4, /* General purpose pointer. */
157 SCBPort = 8, /* Misc. commands and operands. */
158 SCBflash = 12, /* Flash memory control. */
159 SCBeeprom = 14, /* EEPROM control. */
160 SCBCtrlMDI = 16, /* MDI interface control. */
161 SCBEarlyRx = 20, /* Early receive byte count. */
162 SCBFlow = 24, /* Flow Control. */
163 SCBpmdr = 27, /* Power Management Driver. */
164 SCBgctrl = 28, /* General Control. */
165 SCBgstat = 29, /* General Status. */
166 } E100RegisterOffset;
168 /* A speedo3 transmit buffer descriptor with two buffers... */
169 typedef struct {
170 uint16_t status;
171 uint16_t command;
172 uint32_t link; /* void * */
173 uint32_t tbd_array_addr; /* transmit buffer descriptor array address. */
174 uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
175 uint8_t tx_threshold; /* transmit threshold */
176 uint8_t tbd_count; /* TBD number */
177 #if 0
178 /* This constitutes two "TBD" entries: hdr and data */
179 uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
180 int32_t tx_buf_size0; /* Length of Tx hdr. */
181 uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
182 int32_t tx_buf_size1; /* Length of Tx data. */
183 #endif
184 } eepro100_tx_t;
186 /* Receive frame descriptor. */
187 typedef struct {
188 int16_t status;
189 uint16_t command;
190 uint32_t link; /* struct RxFD * */
191 uint32_t rx_buf_addr; /* void * */
192 uint16_t count;
193 uint16_t size;
194 /* Ethernet frame data follows. */
195 } eepro100_rx_t;
197 typedef enum {
198 COMMAND_EL = BIT(15),
199 COMMAND_S = BIT(14),
200 COMMAND_I = BIT(13),
201 COMMAND_NC = BIT(4),
202 COMMAND_SF = BIT(3),
203 COMMAND_CMD = BITS(2, 0),
204 } scb_command_bit;
206 typedef enum {
207 STATUS_C = BIT(15),
208 STATUS_OK = BIT(13),
209 } scb_status_bit;
211 typedef struct {
212 uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
213 tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
214 tx_multiple_collisions, tx_total_collisions;
215 uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
216 rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
217 rx_short_frame_errors;
218 uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
219 uint16_t xmt_tco_frames, rcv_tco_frames;
220 /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
221 uint32_t reserved[4];
222 } eepro100_stats_t;
224 typedef enum {
225 cu_idle = 0,
226 cu_suspended = 1,
227 cu_active = 2,
228 cu_lpq_active = 2,
229 cu_hqp_active = 3
230 } cu_state_t;
232 typedef enum {
233 ru_idle = 0,
234 ru_suspended = 1,
235 ru_no_resources = 2,
236 ru_ready = 4
237 } ru_state_t;
239 typedef struct {
240 PCIDevice dev;
241 /* Hash register (multicast mask array, multiple individual addresses). */
242 uint8_t mult[8];
243 MemoryRegion mmio_bar;
244 MemoryRegion io_bar;
245 MemoryRegion flash_bar;
246 NICState *nic;
247 NICConf conf;
248 uint8_t scb_stat; /* SCB stat/ack byte */
249 uint8_t int_stat; /* PCI interrupt status */
250 /* region must not be saved by nic_save. */
251 uint16_t mdimem[32];
252 eeprom_t *eeprom;
253 uint32_t device; /* device variant */
254 /* (cu_base + cu_offset) address the next command block in the command block list. */
255 uint32_t cu_base; /* CU base address */
256 uint32_t cu_offset; /* CU address offset */
257 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
258 uint32_t ru_base; /* RU base address */
259 uint32_t ru_offset; /* RU address offset */
260 uint32_t statsaddr; /* pointer to eepro100_stats_t */
262 /* Temporary status information (no need to save these values),
263 * used while processing CU commands. */
264 eepro100_tx_t tx; /* transmit buffer descriptor */
265 uint32_t cb_address; /* = cu_base + cu_offset */
267 /* Statistical counters. Also used for wake-up packet (i82559). */
268 eepro100_stats_t statistics;
270 /* Data in mem is always in the byte order of the controller (le).
271 * It must be dword aligned to allow direct access to 32 bit values. */
272 uint8_t mem[PCI_MEM_SIZE] __attribute__((aligned(8)));
274 /* Configuration bytes. */
275 uint8_t configuration[22];
277 /* vmstate for each particular nic */
278 VMStateDescription *vmstate;
280 /* Quasi static device properties (no need to save them). */
281 uint16_t stats_size;
282 bool has_extended_tcb_support;
283 } EEPRO100State;
285 /* Word indices in EEPROM. */
286 typedef enum {
287 EEPROM_CNFG_MDIX = 0x03,
288 EEPROM_ID = 0x05,
289 EEPROM_PHY_ID = 0x06,
290 EEPROM_VENDOR_ID = 0x0c,
291 EEPROM_CONFIG_ASF = 0x0d,
292 EEPROM_DEVICE_ID = 0x23,
293 EEPROM_SMBUS_ADDR = 0x90,
294 } EEPROMOffset;
296 /* Bit values for EEPROM ID word (offset 0x0a). */
297 typedef enum {
298 EEPROM_ID_MDM = BIT(0), /* Modem */
299 EEPROM_ID_STB = BIT(1), /* Standby Enable */
300 EEPROM_ID_WMR = BIT(2), /* ??? */
301 EEPROM_ID_WOL = BIT(5), /* Wake on LAN */
302 EEPROM_ID_DPD = BIT(6), /* Deep Power Down */
303 EEPROM_ID_ALT = BIT(7), /* */
304 /* BITS(10, 8) device revision */
305 EEPROM_ID_BD = BIT(11), /* boot disable */
306 EEPROM_ID_ID = BIT(13), /* id bit */
307 /* BITS(15, 14) signature */
308 EEPROM_ID_VALID = BIT(14), /* signature for valid eeprom */
309 } eeprom_id_bit;
311 /* Default values for MDI (PHY) registers */
312 static const uint16_t eepro100_mdi_default[] = {
313 /* MDI Registers 0 - 6, 7 */
314 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
315 /* MDI Registers 8 - 15 */
316 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
317 /* MDI Registers 16 - 31 */
318 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
319 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
322 /* Readonly mask for MDI (PHY) registers */
323 static const uint16_t eepro100_mdi_mask[] = {
324 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
325 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
326 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
327 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
330 #define POLYNOMIAL 0x04c11db6
332 static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s);
334 /* From FreeBSD (locally modified). */
335 static unsigned e100_compute_mcast_idx(const uint8_t *ep)
337 uint32_t crc;
338 int carry, i, j;
339 uint8_t b;
341 crc = 0xffffffff;
342 for (i = 0; i < 6; i++) {
343 b = *ep++;
344 for (j = 0; j < 8; j++) {
345 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
346 crc <<= 1;
347 b >>= 1;
348 if (carry) {
349 crc = ((crc ^ POLYNOMIAL) | carry);
353 return (crc & BITS(7, 2)) >> 2;
356 /* Read a 16 bit control/status (CSR) register. */
357 static uint16_t e100_read_reg2(EEPRO100State *s, E100RegisterOffset addr)
359 assert(!((uintptr_t)&s->mem[addr] & 1));
360 return le16_to_cpup((uint16_t *)&s->mem[addr]);
363 /* Read a 32 bit control/status (CSR) register. */
364 static uint32_t e100_read_reg4(EEPRO100State *s, E100RegisterOffset addr)
366 assert(!((uintptr_t)&s->mem[addr] & 3));
367 return le32_to_cpup((uint32_t *)&s->mem[addr]);
370 /* Write a 16 bit control/status (CSR) register. */
371 static void e100_write_reg2(EEPRO100State *s, E100RegisterOffset addr,
372 uint16_t val)
374 assert(!((uintptr_t)&s->mem[addr] & 1));
375 cpu_to_le16w((uint16_t *)&s->mem[addr], val);
378 /* Read a 32 bit control/status (CSR) register. */
379 static void e100_write_reg4(EEPRO100State *s, E100RegisterOffset addr,
380 uint32_t val)
382 assert(!((uintptr_t)&s->mem[addr] & 3));
383 cpu_to_le32w((uint32_t *)&s->mem[addr], val);
386 #if defined(DEBUG_EEPRO100)
387 static const char *nic_dump(const uint8_t * buf, unsigned size)
389 static char dump[3 * 16 + 1];
390 char *p = &dump[0];
391 if (size > 16) {
392 size = 16;
394 while (size-- > 0) {
395 p += sprintf(p, " %02x", *buf++);
397 return dump;
399 #endif /* DEBUG_EEPRO100 */
401 #if 0 /* TODO */
402 enum scb_stat_ack {
403 stat_ack_not_ours = 0x00,
404 stat_ack_sw_gen = 0x04,
405 stat_ack_rnr = 0x10,
406 stat_ack_cu_idle = 0x20,
407 stat_ack_frame_rx = 0x40,
408 stat_ack_cu_cmd_done = 0x80,
409 stat_ack_not_present = 0xFF,
410 stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
411 stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
413 #endif
415 static void disable_interrupt(EEPRO100State * s)
417 if (s->int_stat) {
418 TRACE(INT, logout("interrupt disabled\n"));
419 pci_irq_deassert(&s->dev);
420 s->int_stat = 0;
424 static void enable_interrupt(EEPRO100State * s)
426 if (!s->int_stat) {
427 TRACE(INT, logout("interrupt enabled\n"));
428 pci_irq_assert(&s->dev);
429 s->int_stat = 1;
433 static void eepro100_acknowledge(EEPRO100State * s)
435 s->scb_stat &= ~s->mem[SCBAck];
436 s->mem[SCBAck] = s->scb_stat;
437 if (s->scb_stat == 0) {
438 disable_interrupt(s);
442 static void eepro100_interrupt(EEPRO100State * s, uint8_t status)
444 uint8_t mask = ~s->mem[SCBIntmask];
445 s->mem[SCBAck] |= status;
446 status = s->scb_stat = s->mem[SCBAck];
447 status &= (mask | 0x0f);
448 #if 0
449 status &= (~s->mem[SCBIntmask] | 0x0xf);
450 #endif
451 if (status && (mask & 0x01)) {
452 /* SCB mask and SCB Bit M do not disable interrupt. */
453 enable_interrupt(s);
454 } else if (s->int_stat) {
455 disable_interrupt(s);
459 static void eepro100_cx_interrupt(EEPRO100State * s)
461 /* CU completed action command. */
462 /* Transmit not ok (82557 only, not in emulation). */
463 eepro100_interrupt(s, 0x80);
466 static void eepro100_cna_interrupt(EEPRO100State * s)
468 /* CU left the active state. */
469 eepro100_interrupt(s, 0x20);
472 static void eepro100_fr_interrupt(EEPRO100State * s)
474 /* RU received a complete frame. */
475 eepro100_interrupt(s, 0x40);
478 static void eepro100_rnr_interrupt(EEPRO100State * s)
480 /* RU is not ready. */
481 eepro100_interrupt(s, 0x10);
484 static void eepro100_mdi_interrupt(EEPRO100State * s)
486 /* MDI completed read or write cycle. */
487 eepro100_interrupt(s, 0x08);
490 static void eepro100_swi_interrupt(EEPRO100State * s)
492 /* Software has requested an interrupt. */
493 eepro100_interrupt(s, 0x04);
496 #if 0
497 static void eepro100_fcp_interrupt(EEPRO100State * s)
499 /* Flow control pause interrupt (82558 and later). */
500 eepro100_interrupt(s, 0x01);
502 #endif
504 static void e100_pci_reset(EEPRO100State * s)
506 E100PCIDeviceInfo *info = eepro100_get_class(s);
507 uint32_t device = s->device;
508 uint8_t *pci_conf = s->dev.config;
510 TRACE(OTHER, logout("%p\n", s));
512 /* PCI Status */
513 pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
514 PCI_STATUS_FAST_BACK);
515 /* PCI Latency Timer */
516 pci_set_byte(pci_conf + PCI_LATENCY_TIMER, 0x20); /* latency timer = 32 clocks */
517 /* Capability Pointer is set by PCI framework. */
518 /* Interrupt Line */
519 /* Interrupt Pin */
520 pci_set_byte(pci_conf + PCI_INTERRUPT_PIN, 1); /* interrupt pin A */
521 /* Minimum Grant */
522 pci_set_byte(pci_conf + PCI_MIN_GNT, 0x08);
523 /* Maximum Latency */
524 pci_set_byte(pci_conf + PCI_MAX_LAT, 0x18);
526 s->stats_size = info->stats_size;
527 s->has_extended_tcb_support = info->has_extended_tcb_support;
529 switch (device) {
530 case i82550:
531 case i82551:
532 case i82557A:
533 case i82557B:
534 case i82557C:
535 case i82558A:
536 case i82558B:
537 case i82559A:
538 case i82559B:
539 case i82559ER:
540 case i82562:
541 case i82801:
542 case i82559C:
543 break;
544 default:
545 logout("Device %X is undefined!\n", device);
548 /* Standard TxCB. */
549 s->configuration[6] |= BIT(4);
551 /* Standard statistical counters. */
552 s->configuration[6] |= BIT(5);
554 if (s->stats_size == 80) {
555 /* TODO: check TCO Statistical Counters bit. Documentation not clear. */
556 if (s->configuration[6] & BIT(2)) {
557 /* TCO statistical counters. */
558 assert(s->configuration[6] & BIT(5));
559 } else {
560 if (s->configuration[6] & BIT(5)) {
561 /* No extended statistical counters, i82557 compatible. */
562 s->stats_size = 64;
563 } else {
564 /* i82558 compatible. */
565 s->stats_size = 76;
568 } else {
569 if (s->configuration[6] & BIT(5)) {
570 /* No extended statistical counters. */
571 s->stats_size = 64;
574 assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics));
576 if (info->power_management) {
577 /* Power Management Capabilities */
578 int cfg_offset = 0xdc;
579 int r = pci_add_capability(&s->dev, PCI_CAP_ID_PM,
580 cfg_offset, PCI_PM_SIZEOF);
581 assert(r >= 0);
582 pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21);
583 #if 0 /* TODO: replace dummy code for power management emulation. */
584 /* TODO: Power Management Control / Status. */
585 pci_set_word(pci_conf + cfg_offset + PCI_PM_CTRL, 0x0000);
586 /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
587 pci_set_byte(pci_conf + cfg_offset + PCI_PM_PPB_EXTENSIONS, 0x0000);
588 #endif
591 #if EEPROM_SIZE > 0
592 if (device == i82557C || device == i82558B || device == i82559C) {
594 TODO: get vendor id from EEPROM for i82557C or later.
595 TODO: get device id from EEPROM for i82557C or later.
596 TODO: status bit 4 can be disabled by EEPROM for i82558, i82559.
597 TODO: header type is determined by EEPROM for i82559.
598 TODO: get subsystem id from EEPROM for i82557C or later.
599 TODO: get subsystem vendor id from EEPROM for i82557C or later.
600 TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later.
601 TODO: capability pointer depends on EEPROM for i82558.
603 logout("Get device id and revision from EEPROM!!!\n");
605 #endif /* EEPROM_SIZE > 0 */
608 static void nic_selective_reset(EEPRO100State * s)
610 #if EEPROM_SIZE > 0
611 static const uint16_t eeprom_i82559[] = {
612 /* 0x0000 */ 0x0000, 0x0000, 0x0000, 0x020b,
613 /* 0x0008 */ 0xffff, 0x0201, 0x4701, 0xffff,
614 /* 0x0010 */ 0x7517, 0x6704, 0x50a2, 0x0040,
615 /* 0x0018 */ 0x8086, 0x0064, 0xffff, 0xffff,
616 /* 0x0020 */ 0xffff, 0xffff, 0xffff, 0xffff,
617 /* 0x0028 */ 0xffff, 0xffff, 0xffff, 0xffff,
618 /* 0x0030 */ 0xffff, 0xffff, 0xffff, 0xffff,
619 /* 0x0038 */ 0xffff, 0xffff, 0xffff, 0xffff,
620 /* 0x0040 */ 0xffff, 0xffff, 0xffff, 0x1229,
621 /* 0x0048 */ 0xffff, 0xffff, 0xffff, 0xffff,
622 /* 0x0050 */ 0xffff, 0xffff, 0xffff, 0xffff,
623 /* 0x0058 */ 0xffff, 0xffff, 0xffff, 0xffff,
624 /* 0x0060 */ 0x002c, 0x4000, 0x3003, 0xffff,
625 /* 0x0068 */ 0xffff, 0xffff, 0xffff, 0xffff,
626 /* 0x0070 */ 0xffff, 0xffff, 0xffff, 0xffff,
627 /* 0x0078 */ 0xffff, 0xffff, 0xffff, 0xffff,
629 size_t i;
630 uint8_t *pci_conf = s->dev.config;
631 uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
632 #if 0
633 eeprom93xx_reset(s->eeprom);
634 #endif
635 memcpy(eeprom_contents, eeprom_i82559, EEPROM_SIZE * 2);
636 memcpy(eeprom_contents, s->conf.macaddr.a, 6);
637 #if defined(WORDS_BIGENDIAN)
638 bswap16s(&eeprom_contents[0]);
639 bswap16s(&eeprom_contents[1]);
640 bswap16s(&eeprom_contents[2]);
641 #endif
642 #if 0
643 /* Only needed to set a different vendor id. */
644 memcpy(eeprom_contents + eeprom_vendor_id, pci_conf + PCI_VENDOR_ID, 2);
645 #if defined(WORDS_BIGENDIAN)
646 bswap16s(&eeprom_contents[eeprom_vendor_id]);
647 #endif
648 #endif /* EEPROM_SIZE > 0 */
649 memcpy(eeprom_contents + EEPROM_DEVICE_ID, pci_conf + PCI_DEVICE_ID, 2);
650 #if defined(WORDS_BIGENDIAN)
651 bswap16s(&eeprom_contents[EEPROM_DEVICE_ID]);
652 #endif
653 #if 0
654 /* We might change the phy id here. */
655 eeprom_contents[EEPROM_PHY_ID] =
656 (eeprom_contents[EEPROM_PHY_ID] & 0xff00) + 1;
657 #endif
658 /* TODO: eeprom_id_alt for i82559 */
659 eeprom_contents[EEPROM_ID] |= EEPROM_ID_VALID;
660 if (s->device >= i82557A && s->device <= i82557C) {
661 /* Set revision. */
662 eeprom_contents[EEPROM_ID] |= BIT(8);
664 /* TODO: source of next statement? */
665 eeprom_contents[0xa] = 0x4000;
666 if (s->device == i82557B || s->device == i82557C)
667 eeprom_contents[5] = 0x0100;
668 eeprom_contents[EEPROM_PHY_ID] = 1;
669 uint16_t sum = 0;
670 for (i = 0; i < EEPROM_SIZE - 1; i++) {
671 sum += eeprom_contents[i];
673 eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
674 TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
675 #endif
677 memset(s->mem, 0, sizeof(s->mem));
678 e100_write_reg4(s, SCBCtrlMDI, BIT(21));
680 assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
681 memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
684 static void nic_reset(void *opaque)
686 EEPRO100State *s = opaque;
687 TRACE(OTHER, logout("%p\n", s));
688 /* TODO: Clearing of hash register for selective reset, too? */
689 memset(&s->mult[0], 0, sizeof(s->mult));
690 nic_selective_reset(s);
693 #if defined(DEBUG_EEPRO100)
694 static const char * const e100_reg[PCI_IO_SIZE / 4] = {
695 "Command/Status",
696 "General Pointer",
697 "Port",
698 "EEPROM/Flash Control",
699 "MDI Control",
700 "Receive DMA Byte Count",
701 "Flow Control",
702 "General Status/Control"
705 static char *regname(uint32_t addr)
707 static char buf[32];
708 if (addr < PCI_IO_SIZE) {
709 const char *r = e100_reg[addr / 4];
710 if (r != 0) {
711 snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
712 } else {
713 snprintf(buf, sizeof(buf), "0x%02x", addr);
715 } else {
716 snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
718 return buf;
720 #endif /* DEBUG_EEPRO100 */
722 /*****************************************************************************
724 * Command emulation.
726 ****************************************************************************/
728 #if 0
729 static uint16_t eepro100_read_command(EEPRO100State * s)
731 uint16_t val = 0xffff;
732 TRACE(OTHER, logout("val=0x%04x\n", val));
733 return val;
735 #endif
737 /* Commands that can be put in a command list entry. */
738 enum commands {
739 CmdNOp = 0,
740 CmdIASetup = 1,
741 CmdConfigure = 2,
742 CmdMulticastList = 3,
743 CmdTx = 4,
744 CmdTDR = 5, /* load microcode */
745 CmdDump = 6,
746 CmdDiagnose = 7,
748 /* And some extra flags: */
749 CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
752 static cu_state_t get_cu_state(EEPRO100State * s)
754 return ((s->mem[SCBStatus] & BITS(7, 6)) >> 6);
757 static void set_cu_state(EEPRO100State * s, cu_state_t state)
759 s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(7, 6)) + (state << 6);
762 static ru_state_t get_ru_state(EEPRO100State * s)
764 return ((s->mem[SCBStatus] & BITS(5, 2)) >> 2);
767 static void set_ru_state(EEPRO100State * s, ru_state_t state)
769 s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(5, 2)) + (state << 2);
772 static void dump_statistics(EEPRO100State * s)
774 /* Dump statistical data. Most data is never changed by the emulation
775 * and always 0, so we first just copy the whole block and then those
776 * values which really matter.
777 * Number of data should check configuration!!!
779 pci_dma_write(&s->dev, s->statsaddr, &s->statistics, s->stats_size);
780 stl_le_pci_dma(&s->dev, s->statsaddr + 0, s->statistics.tx_good_frames);
781 stl_le_pci_dma(&s->dev, s->statsaddr + 36, s->statistics.rx_good_frames);
782 stl_le_pci_dma(&s->dev, s->statsaddr + 48,
783 s->statistics.rx_resource_errors);
784 stl_le_pci_dma(&s->dev, s->statsaddr + 60,
785 s->statistics.rx_short_frame_errors);
786 #if 0
787 stw_le_pci_dma(&s->dev, s->statsaddr + 76, s->statistics.xmt_tco_frames);
788 stw_le_pci_dma(&s->dev, s->statsaddr + 78, s->statistics.rcv_tco_frames);
789 missing("CU dump statistical counters");
790 #endif
793 static void read_cb(EEPRO100State *s)
795 pci_dma_read(&s->dev, s->cb_address, &s->tx, sizeof(s->tx));
796 s->tx.status = le16_to_cpu(s->tx.status);
797 s->tx.command = le16_to_cpu(s->tx.command);
798 s->tx.link = le32_to_cpu(s->tx.link);
799 s->tx.tbd_array_addr = le32_to_cpu(s->tx.tbd_array_addr);
800 s->tx.tcb_bytes = le16_to_cpu(s->tx.tcb_bytes);
803 static void tx_command(EEPRO100State *s)
805 uint32_t tbd_array = le32_to_cpu(s->tx.tbd_array_addr);
806 uint16_t tcb_bytes = (le16_to_cpu(s->tx.tcb_bytes) & 0x3fff);
807 /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */
808 uint8_t buf[2600];
809 uint16_t size = 0;
810 uint32_t tbd_address = s->cb_address + 0x10;
811 TRACE(RXTX, logout
812 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
813 tbd_array, tcb_bytes, s->tx.tbd_count));
814 assert(!(s->tx.command & COMMAND_NC));
815 assert(tcb_bytes <= sizeof(buf));
816 if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
817 logout
818 ("illegal values of TBD array address and TCB byte count!\n");
820 if (s->tx.command & COMMAND_SF) {
821 /* No simplified mode. TODO: check code in this block. */
822 for (size = 0; size < tcb_bytes; ) {
823 uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
824 uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
825 #if 0
826 uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6);
827 #endif
828 if (tx_buffer_size == 0) {
829 /* Prevent an endless loop. */
830 logout("loop in %s:%u\n", __FILE__, __LINE__);
831 break;
833 tbd_address += 8;
834 TRACE(RXTX, logout
835 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
836 tx_buffer_address, tx_buffer_size));
837 if (size + tx_buffer_size > sizeof(buf)) {
838 logout("bad simple TCB with size 0x%04x\n", tx_buffer_size);
839 } else {
840 pci_dma_read(&s->dev, tx_buffer_address, &buf[size],
841 tx_buffer_size);
843 size += tx_buffer_size;
846 if (!(s->tx.command & COMMAND_SF)) {
847 /* Simplified mode. */
848 if (tcb_bytes > sizeof(buf)) {
849 logout("bad TCB byte count 0x%04x (simplified mode)\n", tcb_bytes);
850 } else if (tbd_array != 0xffffffff) {
851 logout("bad TCB array address 0x%04x (simplified mode)\n", tbd_array);
852 UNEXPECTED();
853 } else {
854 cpu_physical_memory_read(tbd_address, &buf[0], tcb_bytes);
856 } else {
857 /* Flexible mode. */
858 uint8_t tbd_count = 0;
859 if (!(s->configuration[6] & BIT(4))) {
860 /* Extended TxCB. */
861 assert(tcb_bytes == 0);
862 for (; tbd_count < 2 && tbd_count < s->tx.tbd_count; tbd_count++) {
863 uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
864 uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
865 uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6);
866 tbd_address += 8;
867 TRACE(RXTX, logout
868 ("TBD (extended mode): buffer address 0x%08x, size 0x%04x\n",
869 tx_buffer_address, tx_buffer_size));
870 if (size + tx_buffer_size > sizeof(buf)) {
871 logout("bad extended TCB with size 0x%04x\n", tx_buffer_size);
872 } else if (tx_buffer_size > 0) {
873 assert(tx_buffer_address != 0);
874 pci_dma_read(&s->dev, tx_buffer_address, &buf[size],
875 tx_buffer_size);
876 size += tx_buffer_size;
878 if (tx_buffer_el & 1) {
879 break;
883 tbd_address = tbd_array;
884 for (; tbd_count < s->tx.tbd_count; tbd_count++) {
885 uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
886 uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
887 uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, 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 if (size + tx_buffer_size > sizeof(buf)) {
893 logout("bad flexible TCB with size 0x%04x\n", tx_buffer_size);
894 } else {
895 pci_dma_read(&s->dev, tx_buffer_address, &buf[size],
896 tx_buffer_size);
897 size += tx_buffer_size;
899 if (tx_buffer_el & 1) {
900 break;
904 TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
905 assert(size <= sizeof(buf));
906 qemu_send_packet(qemu_get_queue(s->nic), buf, size);
907 s->statistics.tx_good_frames++;
908 /* Transmit with bad status would raise an CX/TNO interrupt.
909 * (82557 only). Emulation never has bad status. */
910 #if 0
911 eepro100_cx_interrupt(s);
912 #endif
915 static void set_multicast_list(EEPRO100State *s)
917 uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0);
918 uint16_t i;
919 memset(&s->mult[0], 0, sizeof(s->mult));
920 TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count));
921 for (i = 0; i < multicast_count; i += 6) {
922 uint8_t multicast_addr[6];
923 pci_dma_read(&s->dev, s->cb_address + 10 + i, multicast_addr, 6);
924 TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6)));
925 unsigned mcast_idx = e100_compute_mcast_idx(multicast_addr);
926 assert(mcast_idx < 64);
927 s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
931 static void action_command(EEPRO100State *s)
933 /* The loop below won't stop if it gets special handcrafted data.
934 Therefore we limit the number of iterations. */
935 unsigned max_loop_count = 16;
937 for (;;) {
938 bool bit_el;
939 bool bit_s;
940 bool bit_i;
941 bool bit_nc;
942 uint16_t ok_status = STATUS_OK;
943 s->cb_address = s->cu_base + s->cu_offset;
944 read_cb(s);
945 bit_el = ((s->tx.command & COMMAND_EL) != 0);
946 bit_s = ((s->tx.command & COMMAND_S) != 0);
947 bit_i = ((s->tx.command & COMMAND_I) != 0);
948 bit_nc = ((s->tx.command & COMMAND_NC) != 0);
950 if (max_loop_count-- == 0) {
951 /* Prevent an endless loop. */
952 logout("loop in %s:%u\n", __FILE__, __LINE__);
953 break;
956 s->cu_offset = s->tx.link;
957 TRACE(OTHER, logout
958 ("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
959 s->tx.status, s->tx.command, s->cu_offset));
960 switch (s->tx.command & COMMAND_CMD) {
961 case CmdNOp:
962 /* Do nothing. */
963 break;
964 case CmdIASetup:
965 pci_dma_read(&s->dev, s->cb_address + 8, &s->conf.macaddr.a[0], 6);
966 TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)));
967 /* TODO: missing code. */
968 break;
969 case CmdConfigure:
970 pci_dma_read(&s->dev, s->cb_address + 8, &s->configuration[0],
971 sizeof(s->configuration));
972 if (!s->has_extended_tcb_support) {
973 /* Force standard TxCB. */
974 s->configuration[6] |= BIT(4);
976 TRACE(OTHER, logout("configuration: %s\n",
977 nic_dump(&s->configuration[0], 16)));
978 TRACE(OTHER, logout("configuration: %s\n",
979 nic_dump(&s->configuration[16],
980 ARRAY_SIZE(s->configuration) - 16)));
981 if (s->configuration[20] & BIT(6)) {
982 TRACE(OTHER, logout("Multiple IA bit\n"));
984 break;
985 case CmdMulticastList:
986 set_multicast_list(s);
987 break;
988 case CmdTx:
989 if (bit_nc) {
990 missing("CmdTx: NC = 0");
991 ok_status = 0;
992 break;
994 tx_command(s);
995 break;
996 case CmdTDR:
997 TRACE(OTHER, logout("load microcode\n"));
998 /* Starting with offset 8, the command contains
999 * 64 dwords microcode which we just ignore here. */
1000 break;
1001 case CmdDiagnose:
1002 TRACE(OTHER, logout("diagnose\n"));
1003 /* Make sure error flag is not set. */
1004 s->tx.status = 0;
1005 break;
1006 default:
1007 missing("undefined command");
1008 ok_status = 0;
1009 break;
1011 /* Write new status. */
1012 stw_le_pci_dma(&s->dev, s->cb_address,
1013 s->tx.status | ok_status | STATUS_C);
1014 if (bit_i) {
1015 /* CU completed action. */
1016 eepro100_cx_interrupt(s);
1018 if (bit_el) {
1019 /* CU becomes idle. Terminate command loop. */
1020 set_cu_state(s, cu_idle);
1021 eepro100_cna_interrupt(s);
1022 break;
1023 } else if (bit_s) {
1024 /* CU becomes suspended. Terminate command loop. */
1025 set_cu_state(s, cu_suspended);
1026 eepro100_cna_interrupt(s);
1027 break;
1028 } else {
1029 /* More entries in list. */
1030 TRACE(OTHER, logout("CU list with at least one more entry\n"));
1033 TRACE(OTHER, logout("CU list empty\n"));
1034 /* List is empty. Now CU is idle or suspended. */
1037 static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
1039 cu_state_t cu_state;
1040 switch (val) {
1041 case CU_NOP:
1042 /* No operation. */
1043 break;
1044 case CU_START:
1045 cu_state = get_cu_state(s);
1046 if (cu_state != cu_idle && cu_state != cu_suspended) {
1047 /* Intel documentation says that CU must be idle or suspended
1048 * for the CU start command. */
1049 logout("unexpected CU state is %u\n", cu_state);
1051 set_cu_state(s, cu_active);
1052 s->cu_offset = e100_read_reg4(s, SCBPointer);
1053 action_command(s);
1054 break;
1055 case CU_RESUME:
1056 if (get_cu_state(s) != cu_suspended) {
1057 logout("bad CU resume from CU state %u\n", get_cu_state(s));
1058 /* Workaround for bad Linux eepro100 driver which resumes
1059 * from idle state. */
1060 #if 0
1061 missing("cu resume");
1062 #endif
1063 set_cu_state(s, cu_suspended);
1065 if (get_cu_state(s) == cu_suspended) {
1066 TRACE(OTHER, logout("CU resuming\n"));
1067 set_cu_state(s, cu_active);
1068 action_command(s);
1070 break;
1071 case CU_STATSADDR:
1072 /* Load dump counters address. */
1073 s->statsaddr = e100_read_reg4(s, SCBPointer);
1074 TRACE(OTHER, logout("val=0x%02x (dump counters address)\n", val));
1075 if (s->statsaddr & 3) {
1076 /* Memory must be Dword aligned. */
1077 logout("unaligned dump counters address\n");
1078 /* Handling of misaligned addresses is undefined.
1079 * Here we align the address by ignoring the lower bits. */
1080 /* TODO: Test unaligned dump counter address on real hardware. */
1081 s->statsaddr &= ~3;
1083 break;
1084 case CU_SHOWSTATS:
1085 /* Dump statistical counters. */
1086 TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
1087 dump_statistics(s);
1088 stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa005);
1089 break;
1090 case CU_CMD_BASE:
1091 /* Load CU base. */
1092 TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
1093 s->cu_base = e100_read_reg4(s, SCBPointer);
1094 break;
1095 case CU_DUMPSTATS:
1096 /* Dump and reset statistical counters. */
1097 TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
1098 dump_statistics(s);
1099 stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa007);
1100 memset(&s->statistics, 0, sizeof(s->statistics));
1101 break;
1102 case CU_SRESUME:
1103 /* CU static resume. */
1104 missing("CU static resume");
1105 break;
1106 default:
1107 missing("Undefined CU command");
1111 static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
1113 switch (val) {
1114 case RU_NOP:
1115 /* No operation. */
1116 break;
1117 case RX_START:
1118 /* RU start. */
1119 if (get_ru_state(s) != ru_idle) {
1120 logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
1121 #if 0
1122 assert(!"wrong RU state");
1123 #endif
1125 set_ru_state(s, ru_ready);
1126 s->ru_offset = e100_read_reg4(s, SCBPointer);
1127 qemu_flush_queued_packets(qemu_get_queue(s->nic));
1128 TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
1129 break;
1130 case RX_RESUME:
1131 /* Restart RU. */
1132 if (get_ru_state(s) != ru_suspended) {
1133 logout("RU state is %u, should be %u\n", get_ru_state(s),
1134 ru_suspended);
1135 #if 0
1136 assert(!"wrong RU state");
1137 #endif
1139 set_ru_state(s, ru_ready);
1140 break;
1141 case RU_ABORT:
1142 /* RU abort. */
1143 if (get_ru_state(s) == ru_ready) {
1144 eepro100_rnr_interrupt(s);
1146 set_ru_state(s, ru_idle);
1147 break;
1148 case RX_ADDR_LOAD:
1149 /* Load RU base. */
1150 TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
1151 s->ru_base = e100_read_reg4(s, SCBPointer);
1152 break;
1153 default:
1154 logout("val=0x%02x (undefined RU command)\n", val);
1155 missing("Undefined RU command");
1159 static void eepro100_write_command(EEPRO100State * s, uint8_t val)
1161 eepro100_ru_command(s, val & 0x0f);
1162 eepro100_cu_command(s, val & 0xf0);
1163 if ((val) == 0) {
1164 TRACE(OTHER, logout("val=0x%02x\n", val));
1166 /* Clear command byte after command was accepted. */
1167 s->mem[SCBCmd] = 0;
1170 /*****************************************************************************
1172 * EEPROM emulation.
1174 ****************************************************************************/
1176 #define EEPROM_CS 0x02
1177 #define EEPROM_SK 0x01
1178 #define EEPROM_DI 0x04
1179 #define EEPROM_DO 0x08
1181 static uint16_t eepro100_read_eeprom(EEPRO100State * s)
1183 uint16_t val = e100_read_reg2(s, SCBeeprom);
1184 if (eeprom93xx_read(s->eeprom)) {
1185 val |= EEPROM_DO;
1186 } else {
1187 val &= ~EEPROM_DO;
1189 TRACE(EEPROM, logout("val=0x%04x\n", val));
1190 return val;
1193 static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
1195 TRACE(EEPROM, logout("val=0x%02x\n", val));
1197 /* mask unwritable bits */
1198 #if 0
1199 val = SET_MASKED(val, 0x31, eeprom->value);
1200 #endif
1202 int eecs = ((val & EEPROM_CS) != 0);
1203 int eesk = ((val & EEPROM_SK) != 0);
1204 int eedi = ((val & EEPROM_DI) != 0);
1205 eeprom93xx_write(eeprom, eecs, eesk, eedi);
1208 /*****************************************************************************
1210 * MDI emulation.
1212 ****************************************************************************/
1214 #if defined(DEBUG_EEPRO100)
1215 static const char * const mdi_op_name[] = {
1216 "opcode 0",
1217 "write",
1218 "read",
1219 "opcode 3"
1222 static const char * const mdi_reg_name[] = {
1223 "Control",
1224 "Status",
1225 "PHY Identification (Word 1)",
1226 "PHY Identification (Word 2)",
1227 "Auto-Negotiation Advertisement",
1228 "Auto-Negotiation Link Partner Ability",
1229 "Auto-Negotiation Expansion"
1232 static const char *reg2name(uint8_t reg)
1234 static char buffer[10];
1235 const char *p = buffer;
1236 if (reg < ARRAY_SIZE(mdi_reg_name)) {
1237 p = mdi_reg_name[reg];
1238 } else {
1239 snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
1241 return p;
1243 #endif /* DEBUG_EEPRO100 */
1245 static uint32_t eepro100_read_mdi(EEPRO100State * s)
1247 uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
1249 #ifdef DEBUG_EEPRO100
1250 uint8_t raiseint = (val & BIT(29)) >> 29;
1251 uint8_t opcode = (val & BITS(27, 26)) >> 26;
1252 uint8_t phy = (val & BITS(25, 21)) >> 21;
1253 uint8_t reg = (val & BITS(20, 16)) >> 16;
1254 uint16_t data = (val & BITS(15, 0));
1255 #endif
1256 /* Emulation takes no time to finish MDI transaction. */
1257 val |= BIT(28);
1258 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1259 val, raiseint, mdi_op_name[opcode], phy,
1260 reg2name(reg), data));
1261 return val;
1264 static void eepro100_write_mdi(EEPRO100State *s)
1266 uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
1267 uint8_t raiseint = (val & BIT(29)) >> 29;
1268 uint8_t opcode = (val & BITS(27, 26)) >> 26;
1269 uint8_t phy = (val & BITS(25, 21)) >> 21;
1270 uint8_t reg = (val & BITS(20, 16)) >> 16;
1271 uint16_t data = (val & BITS(15, 0));
1272 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1273 val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
1274 if (phy != 1) {
1275 /* Unsupported PHY address. */
1276 #if 0
1277 logout("phy must be 1 but is %u\n", phy);
1278 #endif
1279 data = 0;
1280 } else if (opcode != 1 && opcode != 2) {
1281 /* Unsupported opcode. */
1282 logout("opcode must be 1 or 2 but is %u\n", opcode);
1283 data = 0;
1284 } else if (reg > 6) {
1285 /* Unsupported register. */
1286 logout("register must be 0...6 but is %u\n", reg);
1287 data = 0;
1288 } else {
1289 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1290 val, raiseint, mdi_op_name[opcode], phy,
1291 reg2name(reg), data));
1292 if (opcode == 1) {
1293 /* MDI write */
1294 switch (reg) {
1295 case 0: /* Control Register */
1296 if (data & 0x8000) {
1297 /* Reset status and control registers to default. */
1298 s->mdimem[0] = eepro100_mdi_default[0];
1299 s->mdimem[1] = eepro100_mdi_default[1];
1300 data = s->mdimem[reg];
1301 } else {
1302 /* Restart Auto Configuration = Normal Operation */
1303 data &= ~0x0200;
1305 break;
1306 case 1: /* Status Register */
1307 missing("not writable");
1308 break;
1309 case 2: /* PHY Identification Register (Word 1) */
1310 case 3: /* PHY Identification Register (Word 2) */
1311 missing("not implemented");
1312 break;
1313 case 4: /* Auto-Negotiation Advertisement Register */
1314 case 5: /* Auto-Negotiation Link Partner Ability Register */
1315 break;
1316 case 6: /* Auto-Negotiation Expansion Register */
1317 default:
1318 missing("not implemented");
1320 s->mdimem[reg] &= eepro100_mdi_mask[reg];
1321 s->mdimem[reg] |= data & ~eepro100_mdi_mask[reg];
1322 } else if (opcode == 2) {
1323 /* MDI read */
1324 switch (reg) {
1325 case 0: /* Control Register */
1326 if (data & 0x8000) {
1327 /* Reset status and control registers to default. */
1328 s->mdimem[0] = eepro100_mdi_default[0];
1329 s->mdimem[1] = eepro100_mdi_default[1];
1331 break;
1332 case 1: /* Status Register */
1333 s->mdimem[reg] |= 0x0020;
1334 break;
1335 case 2: /* PHY Identification Register (Word 1) */
1336 case 3: /* PHY Identification Register (Word 2) */
1337 case 4: /* Auto-Negotiation Advertisement Register */
1338 break;
1339 case 5: /* Auto-Negotiation Link Partner Ability Register */
1340 s->mdimem[reg] = 0x41fe;
1341 break;
1342 case 6: /* Auto-Negotiation Expansion Register */
1343 s->mdimem[reg] = 0x0001;
1344 break;
1346 data = s->mdimem[reg];
1348 /* Emulation takes no time to finish MDI transaction.
1349 * Set MDI bit in SCB status register. */
1350 s->mem[SCBAck] |= 0x08;
1351 val |= BIT(28);
1352 if (raiseint) {
1353 eepro100_mdi_interrupt(s);
1356 val = (val & 0xffff0000) + data;
1357 e100_write_reg4(s, SCBCtrlMDI, val);
1360 /*****************************************************************************
1362 * Port emulation.
1364 ****************************************************************************/
1366 #define PORT_SOFTWARE_RESET 0
1367 #define PORT_SELFTEST 1
1368 #define PORT_SELECTIVE_RESET 2
1369 #define PORT_DUMP 3
1370 #define PORT_SELECTION_MASK 3
1372 typedef struct {
1373 uint32_t st_sign; /* Self Test Signature */
1374 uint32_t st_result; /* Self Test Results */
1375 } eepro100_selftest_t;
1377 static uint32_t eepro100_read_port(EEPRO100State * s)
1379 return 0;
1382 static void eepro100_write_port(EEPRO100State *s)
1384 uint32_t val = e100_read_reg4(s, SCBPort);
1385 uint32_t address = (val & ~PORT_SELECTION_MASK);
1386 uint8_t selection = (val & PORT_SELECTION_MASK);
1387 switch (selection) {
1388 case PORT_SOFTWARE_RESET:
1389 nic_reset(s);
1390 break;
1391 case PORT_SELFTEST:
1392 TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1393 eepro100_selftest_t data;
1394 pci_dma_read(&s->dev, address, (uint8_t *) &data, sizeof(data));
1395 data.st_sign = 0xffffffff;
1396 data.st_result = 0;
1397 pci_dma_write(&s->dev, address, (uint8_t *) &data, sizeof(data));
1398 break;
1399 case PORT_SELECTIVE_RESET:
1400 TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1401 nic_selective_reset(s);
1402 break;
1403 default:
1404 logout("val=0x%08x\n", val);
1405 missing("unknown port selection");
1409 /*****************************************************************************
1411 * General hardware emulation.
1413 ****************************************************************************/
1415 static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1417 uint8_t val = 0;
1418 assert(addr <= sizeof(s->mem) - sizeof(val));
1419 if (addr <= sizeof(s->mem) - sizeof(val)) {
1420 val = s->mem[addr];
1423 switch (addr) {
1424 case SCBStatus:
1425 case SCBAck:
1426 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1427 break;
1428 case SCBCmd:
1429 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1430 #if 0
1431 val = eepro100_read_command(s);
1432 #endif
1433 break;
1434 case SCBIntmask:
1435 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1436 break;
1437 case SCBPort + 3:
1438 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1439 break;
1440 case SCBeeprom:
1441 val = eepro100_read_eeprom(s);
1442 break;
1443 case SCBCtrlMDI:
1444 case SCBCtrlMDI + 1:
1445 case SCBCtrlMDI + 2:
1446 case SCBCtrlMDI + 3:
1447 val = (uint8_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
1448 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1449 break;
1450 case SCBpmdr: /* Power Management Driver Register */
1451 val = 0;
1452 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1453 break;
1454 case SCBgctrl: /* General Control Register */
1455 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1456 break;
1457 case SCBgstat: /* General Status Register */
1458 /* 100 Mbps full duplex, valid link */
1459 val = 0x07;
1460 TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1461 break;
1462 default:
1463 logout("addr=%s val=0x%02x\n", regname(addr), val);
1464 missing("unknown byte read");
1466 return val;
1469 static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1471 uint16_t val = 0;
1472 assert(addr <= sizeof(s->mem) - sizeof(val));
1473 if (addr <= sizeof(s->mem) - sizeof(val)) {
1474 val = e100_read_reg2(s, addr);
1477 switch (addr) {
1478 case SCBStatus:
1479 case SCBCmd:
1480 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1481 break;
1482 case SCBeeprom:
1483 val = eepro100_read_eeprom(s);
1484 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1485 break;
1486 case SCBCtrlMDI:
1487 case SCBCtrlMDI + 2:
1488 val = (uint16_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
1489 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1490 break;
1491 default:
1492 logout("addr=%s val=0x%04x\n", regname(addr), val);
1493 missing("unknown word read");
1495 return val;
1498 static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1500 uint32_t val = 0;
1501 assert(addr <= sizeof(s->mem) - sizeof(val));
1502 if (addr <= sizeof(s->mem) - sizeof(val)) {
1503 val = e100_read_reg4(s, addr);
1506 switch (addr) {
1507 case SCBStatus:
1508 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1509 break;
1510 case SCBPointer:
1511 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1512 break;
1513 case SCBPort:
1514 val = eepro100_read_port(s);
1515 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1516 break;
1517 case SCBflash:
1518 val = eepro100_read_eeprom(s);
1519 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1520 break;
1521 case SCBCtrlMDI:
1522 val = eepro100_read_mdi(s);
1523 break;
1524 default:
1525 logout("addr=%s val=0x%08x\n", regname(addr), val);
1526 missing("unknown longword read");
1528 return val;
1531 static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1533 assert(addr <= sizeof(s->mem) - sizeof(val));
1534 /* SCBStatus is readonly. */
1535 if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
1536 s->mem[addr] = val;
1539 switch (addr) {
1540 case SCBStatus:
1541 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1542 break;
1543 case SCBAck:
1544 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1545 eepro100_acknowledge(s);
1546 break;
1547 case SCBCmd:
1548 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1549 eepro100_write_command(s, val);
1550 break;
1551 case SCBIntmask:
1552 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1553 if (val & BIT(1)) {
1554 eepro100_swi_interrupt(s);
1556 eepro100_interrupt(s, 0);
1557 break;
1558 case SCBPointer:
1559 case SCBPointer + 1:
1560 case SCBPointer + 2:
1561 case SCBPointer + 3:
1562 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1563 break;
1564 case SCBPort:
1565 case SCBPort + 1:
1566 case SCBPort + 2:
1567 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1568 break;
1569 case SCBPort + 3:
1570 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1571 eepro100_write_port(s);
1572 break;
1573 case SCBFlow: /* does not exist on 82557 */
1574 case SCBFlow + 1:
1575 case SCBFlow + 2:
1576 case SCBpmdr: /* does not exist on 82557 */
1577 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1578 break;
1579 case SCBeeprom:
1580 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1581 eepro100_write_eeprom(s->eeprom, val);
1582 break;
1583 case SCBCtrlMDI:
1584 case SCBCtrlMDI + 1:
1585 case SCBCtrlMDI + 2:
1586 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1587 break;
1588 case SCBCtrlMDI + 3:
1589 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1590 eepro100_write_mdi(s);
1591 break;
1592 default:
1593 logout("addr=%s val=0x%02x\n", regname(addr), val);
1594 missing("unknown byte write");
1598 static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1600 assert(addr <= sizeof(s->mem) - sizeof(val));
1601 /* SCBStatus is readonly. */
1602 if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
1603 e100_write_reg2(s, addr, val);
1606 switch (addr) {
1607 case SCBStatus:
1608 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1609 s->mem[SCBAck] = (val >> 8);
1610 eepro100_acknowledge(s);
1611 break;
1612 case SCBCmd:
1613 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1614 eepro100_write_command(s, val);
1615 eepro100_write1(s, SCBIntmask, val >> 8);
1616 break;
1617 case SCBPointer:
1618 case SCBPointer + 2:
1619 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1620 break;
1621 case SCBPort:
1622 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1623 break;
1624 case SCBPort + 2:
1625 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1626 eepro100_write_port(s);
1627 break;
1628 case SCBeeprom:
1629 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1630 eepro100_write_eeprom(s->eeprom, val);
1631 break;
1632 case SCBCtrlMDI:
1633 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1634 break;
1635 case SCBCtrlMDI + 2:
1636 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1637 eepro100_write_mdi(s);
1638 break;
1639 default:
1640 logout("addr=%s val=0x%04x\n", regname(addr), val);
1641 missing("unknown word write");
1645 static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1647 assert(addr <= sizeof(s->mem) - sizeof(val));
1648 if (addr <= sizeof(s->mem) - sizeof(val)) {
1649 e100_write_reg4(s, addr, val);
1652 switch (addr) {
1653 case SCBPointer:
1654 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1655 break;
1656 case SCBPort:
1657 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1658 eepro100_write_port(s);
1659 break;
1660 case SCBflash:
1661 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1662 val = val >> 16;
1663 eepro100_write_eeprom(s->eeprom, val);
1664 break;
1665 case SCBCtrlMDI:
1666 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1667 eepro100_write_mdi(s);
1668 break;
1669 default:
1670 logout("addr=%s val=0x%08x\n", regname(addr), val);
1671 missing("unknown longword write");
1675 static uint64_t eepro100_read(void *opaque, hwaddr addr,
1676 unsigned size)
1678 EEPRO100State *s = opaque;
1680 switch (size) {
1681 case 1: return eepro100_read1(s, addr);
1682 case 2: return eepro100_read2(s, addr);
1683 case 4: return eepro100_read4(s, addr);
1684 default: abort();
1688 static void eepro100_write(void *opaque, hwaddr addr,
1689 uint64_t data, unsigned size)
1691 EEPRO100State *s = opaque;
1693 switch (size) {
1694 case 1:
1695 eepro100_write1(s, addr, data);
1696 break;
1697 case 2:
1698 eepro100_write2(s, addr, data);
1699 break;
1700 case 4:
1701 eepro100_write4(s, addr, data);
1702 break;
1703 default:
1704 abort();
1708 static const MemoryRegionOps eepro100_ops = {
1709 .read = eepro100_read,
1710 .write = eepro100_write,
1711 .endianness = DEVICE_LITTLE_ENDIAN,
1714 static ssize_t nic_receive(NetClientState *nc, const uint8_t * buf, size_t size)
1716 /* TODO:
1717 * - Magic packets should set bit 30 in power management driver register.
1718 * - Interesting packets should set bit 29 in power management driver register.
1720 EEPRO100State *s = qemu_get_nic_opaque(nc);
1721 uint16_t rfd_status = 0xa000;
1722 #if defined(CONFIG_PAD_RECEIVED_FRAMES)
1723 uint8_t min_buf[60];
1724 #endif
1725 static const uint8_t broadcast_macaddr[6] =
1726 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1728 #if defined(CONFIG_PAD_RECEIVED_FRAMES)
1729 /* Pad to minimum Ethernet frame length */
1730 if (size < sizeof(min_buf)) {
1731 memcpy(min_buf, buf, size);
1732 memset(&min_buf[size], 0, sizeof(min_buf) - size);
1733 buf = min_buf;
1734 size = sizeof(min_buf);
1736 #endif
1738 if (s->configuration[8] & 0x80) {
1739 /* CSMA is disabled. */
1740 logout("%p received while CSMA is disabled\n", s);
1741 return -1;
1742 #if !defined(CONFIG_PAD_RECEIVED_FRAMES)
1743 } else if (size < 64 && (s->configuration[7] & BIT(0))) {
1744 /* Short frame and configuration byte 7/0 (discard short receive) set:
1745 * Short frame is discarded */
1746 logout("%p received short frame (%zu byte)\n", s, size);
1747 s->statistics.rx_short_frame_errors++;
1748 return -1;
1749 #endif
1750 } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & BIT(3))) {
1751 /* Long frame and configuration byte 18/3 (long receive ok) not set:
1752 * Long frames are discarded. */
1753 logout("%p received long frame (%zu byte), ignored\n", s, size);
1754 return -1;
1755 } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) { /* !!! */
1756 /* Frame matches individual address. */
1757 /* TODO: check configuration byte 15/4 (ignore U/L). */
1758 TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
1759 } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1760 /* Broadcast frame. */
1761 TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
1762 rfd_status |= 0x0002;
1763 } else if (buf[0] & 0x01) {
1764 /* Multicast frame. */
1765 TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size)));
1766 if (s->configuration[21] & BIT(3)) {
1767 /* Multicast all bit is set, receive all multicast frames. */
1768 } else {
1769 unsigned mcast_idx = e100_compute_mcast_idx(buf);
1770 assert(mcast_idx < 64);
1771 if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
1772 /* Multicast frame is allowed in hash table. */
1773 } else if (s->configuration[15] & BIT(0)) {
1774 /* Promiscuous: receive all. */
1775 rfd_status |= 0x0004;
1776 } else {
1777 TRACE(RXTX, logout("%p multicast ignored\n", s));
1778 return -1;
1781 /* TODO: Next not for promiscuous mode? */
1782 rfd_status |= 0x0002;
1783 } else if (s->configuration[15] & BIT(0)) {
1784 /* Promiscuous: receive all. */
1785 TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
1786 rfd_status |= 0x0004;
1787 } else if (s->configuration[20] & BIT(6)) {
1788 /* Multiple IA bit set. */
1789 unsigned mcast_idx = compute_mcast_idx(buf);
1790 assert(mcast_idx < 64);
1791 if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
1792 TRACE(RXTX, logout("%p accepted, multiple IA bit set\n", s));
1793 } else {
1794 TRACE(RXTX, logout("%p frame ignored, multiple IA bit set\n", s));
1795 return -1;
1797 } else {
1798 TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1799 nic_dump(buf, size)));
1800 return size;
1803 if (get_ru_state(s) != ru_ready) {
1804 /* No resources available. */
1805 logout("no resources, state=%u\n", get_ru_state(s));
1806 /* TODO: RNR interrupt only at first failed frame? */
1807 eepro100_rnr_interrupt(s);
1808 s->statistics.rx_resource_errors++;
1809 #if 0
1810 assert(!"no resources");
1811 #endif
1812 return -1;
1814 /* !!! */
1815 eepro100_rx_t rx;
1816 pci_dma_read(&s->dev, s->ru_base + s->ru_offset, &rx,
1817 sizeof(eepro100_rx_t));
1818 /* !!! */
1819 uint16_t rfd_command = le16_to_cpu(rx.command);
1820 uint16_t rfd_size = le16_to_cpu(rx.size);
1822 if (size > rfd_size) {
1823 /* TODO: does real hardware truncate, too? */
1824 logout("received frame with %zu > %u\n", size, rfd_size);
1825 UNEXPECTED();
1826 size = rfd_size;
1828 #if !defined(CONFIG_PAD_RECEIVED_FRAMES)
1829 if (size < 64) {
1830 rfd_status |= 0x0080;
1832 #endif
1833 TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1834 rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
1835 stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
1836 offsetof(eepro100_rx_t, status), rfd_status);
1837 stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
1838 offsetof(eepro100_rx_t, count), size);
1839 /* Early receive interrupt not supported. */
1840 #if 0
1841 eepro100_er_interrupt(s);
1842 #endif
1843 /* Receive CRC Transfer not supported. */
1844 if (s->configuration[18] & BIT(2)) {
1845 missing("Receive CRC Transfer");
1846 return -1;
1848 /* TODO: check stripping enable bit. */
1849 #if 0
1850 assert(!(s->configuration[17] & BIT(0)));
1851 #endif
1852 pci_dma_write(&s->dev, s->ru_base + s->ru_offset +
1853 sizeof(eepro100_rx_t), buf, size);
1854 s->statistics.rx_good_frames++;
1855 eepro100_fr_interrupt(s);
1856 s->ru_offset = le32_to_cpu(rx.link);
1857 if (rfd_command & COMMAND_EL) {
1858 /* EL bit is set, so this was the last frame. */
1859 set_ru_state(s, ru_idle);
1860 #if 1
1861 logout("receive: Running out of frames\n");
1862 // TODO: do we need ru_suspended here?
1863 set_ru_state(s, ru_suspended);
1864 #endif
1865 set_ru_state(s, ru_no_resources);
1866 eepro100_rnr_interrupt(s);
1868 if (rfd_command & COMMAND_S) {
1869 /* S bit is set. */
1870 set_ru_state(s, ru_suspended);
1872 return size;
1875 static const VMStateDescription vmstate_eepro100 = {
1876 .version_id = 3,
1877 .minimum_version_id = 2,
1878 .fields = (VMStateField[]) {
1879 VMSTATE_PCI_DEVICE(dev, EEPRO100State),
1880 VMSTATE_UNUSED(32),
1881 VMSTATE_BUFFER(mult, EEPRO100State),
1882 VMSTATE_BUFFER(mem, EEPRO100State),
1883 /* Save all members of struct between scb_stat and mem. */
1884 VMSTATE_UINT8(scb_stat, EEPRO100State),
1885 VMSTATE_UINT8(int_stat, EEPRO100State),
1886 VMSTATE_UNUSED(3*4),
1887 VMSTATE_MACADDR(conf.macaddr, EEPRO100State),
1888 VMSTATE_UNUSED(19*4),
1889 VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32),
1890 /* The eeprom should be saved and restored by its own routines. */
1891 VMSTATE_UINT32(device, EEPRO100State),
1892 /* TODO check device. */
1893 VMSTATE_UINT32(cu_base, EEPRO100State),
1894 VMSTATE_UINT32(cu_offset, EEPRO100State),
1895 VMSTATE_UINT32(ru_base, EEPRO100State),
1896 VMSTATE_UINT32(ru_offset, EEPRO100State),
1897 VMSTATE_UINT32(statsaddr, EEPRO100State),
1898 /* Save eepro100_stats_t statistics. */
1899 VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State),
1900 VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State),
1901 VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State),
1902 VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State),
1903 VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State),
1904 VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State),
1905 VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State),
1906 VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State),
1907 VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State),
1908 VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State),
1909 VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State),
1910 VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State),
1911 VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State),
1912 VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State),
1913 VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State),
1914 VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State),
1915 VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State),
1916 VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State),
1917 VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State),
1918 VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State),
1919 VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State),
1920 /* Configuration bytes. */
1921 VMSTATE_BUFFER(configuration, EEPRO100State),
1922 VMSTATE_END_OF_LIST()
1926 static void pci_nic_uninit(PCIDevice *pci_dev)
1928 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1930 vmstate_unregister(&pci_dev->qdev, s->vmstate, s);
1931 eeprom93xx_free(&pci_dev->qdev, s->eeprom);
1932 qemu_del_nic(s->nic);
1935 static NetClientInfo net_eepro100_info = {
1936 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1937 .size = sizeof(NICState),
1938 .receive = nic_receive,
1941 static void e100_nic_realize(PCIDevice *pci_dev, Error **errp)
1943 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1944 E100PCIDeviceInfo *info = eepro100_get_class(s);
1946 TRACE(OTHER, logout("\n"));
1948 s->device = info->device;
1950 e100_pci_reset(s);
1952 #if EEPROM_SIZE > 0
1953 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1954 * i82559 and later support 64 or 256 word EEPROM. */
1955 s->eeprom = eeprom93xx_new(&pci_dev->qdev, EEPROM_SIZE);
1956 #endif
1958 /* Handler for memory-mapped I/O */
1959 memory_region_init_io(&s->mmio_bar, OBJECT(s), &eepro100_ops, s,
1960 "eepro100-mmio", PCI_MEM_SIZE);
1961 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->mmio_bar);
1962 memory_region_init_io(&s->io_bar, OBJECT(s), &eepro100_ops, s,
1963 "eepro100-io", PCI_IO_SIZE);
1964 pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1965 /* FIXME: flash aliases to mmio?! */
1966 memory_region_init_io(&s->flash_bar, OBJECT(s), &eepro100_ops, s,
1967 "eepro100-flash", PCI_FLASH_SIZE);
1968 pci_register_bar(&s->dev, 2, 0, &s->flash_bar);
1970 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1971 logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
1973 nic_reset(s);
1975 s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
1976 object_get_typename(OBJECT(pci_dev)), pci_dev->qdev.id, s);
1978 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
1979 TRACE(OTHER, logout("%s\n", qemu_get_queue(s->nic)->info_str));
1981 qemu_register_reset(nic_reset, s);
1983 s->vmstate = g_malloc(sizeof(vmstate_eepro100));
1984 memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
1985 s->vmstate->name = qemu_get_queue(s->nic)->model;
1986 vmstate_register(&pci_dev->qdev, -1, s->vmstate, s);
1989 static void eepro100_instance_init(Object *obj)
1991 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, PCI_DEVICE(obj));
1992 device_add_bootindex_property(obj, &s->conf.bootindex,
1993 "bootindex", "/ethernet-phy@0",
1994 DEVICE(s), NULL);
1997 static E100PCIDeviceInfo e100_devices[] = {
1999 .name = "i82550",
2000 .desc = "Intel i82550 Ethernet",
2001 .device = i82550,
2002 /* TODO: check device id. */
2003 .device_id = PCI_DEVICE_ID_INTEL_82551IT,
2004 /* Revision ID: 0x0c, 0x0d, 0x0e. */
2005 .revision = 0x0e,
2006 /* TODO: check size of statistical counters. */
2007 .stats_size = 80,
2008 /* TODO: check extended tcb support. */
2009 .has_extended_tcb_support = true,
2010 .power_management = true,
2012 .name = "i82551",
2013 .desc = "Intel i82551 Ethernet",
2014 .device = i82551,
2015 .device_id = PCI_DEVICE_ID_INTEL_82551IT,
2016 /* Revision ID: 0x0f, 0x10. */
2017 .revision = 0x0f,
2018 /* TODO: check size of statistical counters. */
2019 .stats_size = 80,
2020 /* TODO: check extended tcb support. */
2021 .has_extended_tcb_support = true,
2022 .power_management = true,
2024 .name = "i82557a",
2025 .desc = "Intel i82557A Ethernet",
2026 .device = i82557A,
2027 .device_id = PCI_DEVICE_ID_INTEL_82557,
2028 .revision = 0x01,
2029 .power_management = false,
2031 .name = "i82557b",
2032 .desc = "Intel i82557B Ethernet",
2033 .device = i82557B,
2034 .device_id = PCI_DEVICE_ID_INTEL_82557,
2035 .revision = 0x02,
2036 .power_management = false,
2038 .name = "i82557c",
2039 .desc = "Intel i82557C Ethernet",
2040 .device = i82557C,
2041 .device_id = PCI_DEVICE_ID_INTEL_82557,
2042 .revision = 0x03,
2043 .power_management = false,
2045 .name = "i82558a",
2046 .desc = "Intel i82558A Ethernet",
2047 .device = i82558A,
2048 .device_id = PCI_DEVICE_ID_INTEL_82557,
2049 .revision = 0x04,
2050 .stats_size = 76,
2051 .has_extended_tcb_support = true,
2052 .power_management = true,
2054 .name = "i82558b",
2055 .desc = "Intel i82558B Ethernet",
2056 .device = i82558B,
2057 .device_id = PCI_DEVICE_ID_INTEL_82557,
2058 .revision = 0x05,
2059 .stats_size = 76,
2060 .has_extended_tcb_support = true,
2061 .power_management = true,
2063 .name = "i82559a",
2064 .desc = "Intel i82559A Ethernet",
2065 .device = i82559A,
2066 .device_id = PCI_DEVICE_ID_INTEL_82557,
2067 .revision = 0x06,
2068 .stats_size = 80,
2069 .has_extended_tcb_support = true,
2070 .power_management = true,
2072 .name = "i82559b",
2073 .desc = "Intel i82559B Ethernet",
2074 .device = i82559B,
2075 .device_id = PCI_DEVICE_ID_INTEL_82557,
2076 .revision = 0x07,
2077 .stats_size = 80,
2078 .has_extended_tcb_support = true,
2079 .power_management = true,
2081 .name = "i82559c",
2082 .desc = "Intel i82559C Ethernet",
2083 .device = i82559C,
2084 .device_id = PCI_DEVICE_ID_INTEL_82557,
2085 #if 0
2086 .revision = 0x08,
2087 #endif
2088 /* TODO: Windows wants revision id 0x0c. */
2089 .revision = 0x0c,
2090 #if EEPROM_SIZE > 0
2091 .subsystem_vendor_id = PCI_VENDOR_ID_INTEL,
2092 .subsystem_id = 0x0040,
2093 #endif
2094 .stats_size = 80,
2095 .has_extended_tcb_support = true,
2096 .power_management = true,
2098 .name = "i82559er",
2099 .desc = "Intel i82559ER Ethernet",
2100 .device = i82559ER,
2101 .device_id = PCI_DEVICE_ID_INTEL_82551IT,
2102 .revision = 0x09,
2103 .stats_size = 80,
2104 .has_extended_tcb_support = true,
2105 .power_management = true,
2107 .name = "i82562",
2108 .desc = "Intel i82562 Ethernet",
2109 .device = i82562,
2110 /* TODO: check device id. */
2111 .device_id = PCI_DEVICE_ID_INTEL_82551IT,
2112 /* TODO: wrong revision id. */
2113 .revision = 0x0e,
2114 .stats_size = 80,
2115 .has_extended_tcb_support = true,
2116 .power_management = true,
2118 /* Toshiba Tecra 8200. */
2119 .name = "i82801",
2120 .desc = "Intel i82801 Ethernet",
2121 .device = i82801,
2122 .device_id = 0x2449,
2123 .revision = 0x03,
2124 .stats_size = 80,
2125 .has_extended_tcb_support = true,
2126 .power_management = true,
2130 static E100PCIDeviceInfo *eepro100_get_class_by_name(const char *typename)
2132 E100PCIDeviceInfo *info = NULL;
2133 int i;
2135 /* This is admittedly awkward but also temporary. QOM allows for
2136 * parameterized typing and for subclassing both of which would suitable
2137 * handle what's going on here. But class_data is already being used as
2138 * a stop-gap hack to allow incremental qdev conversion so we cannot use it
2139 * right now. Once we merge the final QOM series, we can come back here and
2140 * do this in a much more elegant fashion.
2142 for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
2143 if (strcmp(e100_devices[i].name, typename) == 0) {
2144 info = &e100_devices[i];
2145 break;
2148 assert(info != NULL);
2150 return info;
2153 static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s)
2155 return eepro100_get_class_by_name(object_get_typename(OBJECT(s)));
2158 static Property e100_properties[] = {
2159 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
2160 DEFINE_PROP_END_OF_LIST(),
2163 static void eepro100_class_init(ObjectClass *klass, void *data)
2165 DeviceClass *dc = DEVICE_CLASS(klass);
2166 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2167 E100PCIDeviceInfo *info;
2169 info = eepro100_get_class_by_name(object_class_get_name(klass));
2171 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2172 dc->props = e100_properties;
2173 dc->desc = info->desc;
2174 k->vendor_id = PCI_VENDOR_ID_INTEL;
2175 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2176 k->romfile = "pxe-eepro100.rom";
2177 k->realize = e100_nic_realize;
2178 k->exit = pci_nic_uninit;
2179 k->device_id = info->device_id;
2180 k->revision = info->revision;
2181 k->subsystem_vendor_id = info->subsystem_vendor_id;
2182 k->subsystem_id = info->subsystem_id;
2185 static void eepro100_register_types(void)
2187 size_t i;
2188 for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
2189 TypeInfo type_info = {};
2190 E100PCIDeviceInfo *info = &e100_devices[i];
2192 type_info.name = info->name;
2193 type_info.parent = TYPE_PCI_DEVICE;
2194 type_info.class_init = eepro100_class_init;
2195 type_info.instance_size = sizeof(EEPRO100State);
2196 type_info.instance_init = eepro100_instance_init;
2198 type_register(&type_info);
2202 type_init(eepro100_register_types)
2204 /* eof */