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