]> git.proxmox.com Git - mirror_qemu.git/blame - hw/eepro100.c
eepro100: Replace sprintf by snprintf
[mirror_qemu.git] / hw / eepro100.c
CommitLineData
663e8e51
TS
1/*
2 * QEMU i8255x (PRO100) emulation
3 *
4 * Copyright (c) 2006-2007 Stefan Weil
5 *
6 * Portions of the code are copies from grub / etherboot eepro100.c
7 * and linux e100.c.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
8167ee88 20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
663e8e51
TS
21 *
22 * Tested features (i82559):
23 * PXE boot (i386) no valid link
24 * Linux networking (i386) ok
25 *
26 * Untested:
27 * non-i386 platforms
28 * Windows networking
29 *
30 * References:
31 *
32 * Intel 8255x 10/100 Mbps Ethernet Controller Family
33 * Open Source Software Developer Manual
34 */
35
36#if defined(TARGET_I386)
37# warning "PXE boot still not working!"
38#endif
39
663e8e51 40#include <stddef.h> /* offsetof */
b84a5c6f 41#include <stdbool.h>
87ecb68b
PB
42#include "hw.h"
43#include "pci.h"
44#include "net.h"
663e8e51
TS
45#include "eeprom93xx.h"
46
47/* Common declarations for all PCI devices. */
48
663e8e51
TS
49#define PCI_CONFIG_8(offset, value) \
50 (pci_conf[offset] = (value))
51#define PCI_CONFIG_16(offset, value) \
52 (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
53#define PCI_CONFIG_32(offset, value) \
54 (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
55
56#define KiB 1024
57
aac443e6 58/* Debug EEPRO100 card. */
663e8e51
TS
59//~ #define DEBUG_EEPRO100
60
61#ifdef DEBUG_EEPRO100
001faf32 62#define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
663e8e51 63#else
001faf32 64#define logout(fmt, ...) ((void)0)
663e8e51
TS
65#endif
66
67/* Set flags to 0 to disable debug output. */
aac443e6
SW
68#define INT 1 /* interrupt related actions */
69#define MDI 1 /* mdi related actions */
70#define OTHER 1
71#define RXTX 1
72#define EEPROM 1 /* eeprom related actions */
663e8e51
TS
73
74#define TRACE(flag, command) ((flag) ? (command) : (void)0)
75
76#define missing(text) assert(!"feature is missing in this emulation: " text)
77
78#define MAX_ETH_FRAME_SIZE 1514
79
80/* This driver supports several different devices which are declared here. */
81#define i82551 0x82551
82#define i82557B 0x82557b
83#define i82557C 0x82557c
84#define i82558B 0x82558b
85#define i82559C 0x82559c
86#define i82559ER 0x82559e
87#define i82562 0x82562
88
aac443e6 89/* Use 64 word EEPROM. TODO: could be a runtime option. */
663e8e51
TS
90#define EEPROM_SIZE 64
91
92#define PCI_MEM_SIZE (4 * KiB)
93#define PCI_IO_SIZE 64
94#define PCI_FLASH_SIZE (128 * KiB)
95
96#define BIT(n) (1 << (n))
97#define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
98
99/* The SCB accepts the following controls for the Tx and Rx units: */
100#define CU_NOP 0x0000 /* No operation. */
101#define CU_START 0x0010 /* CU start. */
102#define CU_RESUME 0x0020 /* CU resume. */
103#define CU_STATSADDR 0x0040 /* Load dump counters address. */
104#define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
105#define CU_CMD_BASE 0x0060 /* Load CU base address. */
106#define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
107#define CU_SRESUME 0x00a0 /* CU static resume. */
108
109#define RU_NOP 0x0000
110#define RX_START 0x0001
111#define RX_RESUME 0x0002
112#define RX_ABORT 0x0004
113#define RX_ADDR_LOAD 0x0006
114#define RX_RESUMENR 0x0007
115#define INT_MASK 0x0100
116#define DRVR_INT 0x0200 /* Driver generated interrupt. */
117
663e8e51
TS
118/* Offsets to the various registers.
119 All accesses need not be longword aligned. */
120enum speedo_offsets {
121 SCBStatus = 0,
122 SCBAck = 1,
123 SCBCmd = 2, /* Rx/Command Unit command and status. */
124 SCBIntmask = 3,
125 SCBPointer = 4, /* General purpose pointer. */
126 SCBPort = 8, /* Misc. commands and operands. */
127 SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
128 SCBCtrlMDI = 16, /* MDI interface control. */
129 SCBEarlyRx = 20, /* Early receive byte count. */
3257d2b6 130 SCBFlow = 24,
663e8e51
TS
131};
132
133/* A speedo3 transmit buffer descriptor with two buffers... */
134typedef struct {
135 uint16_t status;
136 uint16_t command;
137 uint32_t link; /* void * */
138 uint32_t tx_desc_addr; /* transmit buffer decsriptor array address. */
139 uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
140 uint8_t tx_threshold; /* transmit threshold */
141 uint8_t tbd_count; /* TBD number */
142 //~ /* This constitutes two "TBD" entries: hdr and data */
143 //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
144 //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
145 //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
146 //~ int32_t tx_buf_size1; /* Length of Tx data. */
147} eepro100_tx_t;
148
149/* Receive frame descriptor. */
150typedef struct {
151 int16_t status;
152 uint16_t command;
153 uint32_t link; /* struct RxFD * */
154 uint32_t rx_buf_addr; /* void * */
155 uint16_t count;
156 uint16_t size;
157 char packet[MAX_ETH_FRAME_SIZE + 4];
158} eepro100_rx_t;
159
160typedef struct {
161 uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
162 tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
163 tx_multiple_collisions, tx_total_collisions;
164 uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
165 rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
166 rx_short_frame_errors;
167 uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
168 uint16_t xmt_tco_frames, rcv_tco_frames;
169 uint32_t complete;
170} eepro100_stats_t;
171
172typedef enum {
173 cu_idle = 0,
174 cu_suspended = 1,
175 cu_active = 2,
176 cu_lpq_active = 2,
177 cu_hqp_active = 3
178} cu_state_t;
179
180typedef enum {
181 ru_idle = 0,
182 ru_suspended = 1,
183 ru_no_resources = 2,
184 ru_ready = 4
185} ru_state_t;
186
663e8e51 187typedef struct {
273a2142 188 PCIDevice dev;
663e8e51
TS
189#if 1
190 uint8_t cmd;
191 uint32_t start;
192 uint32_t stop;
193 uint8_t boundary;
194 uint8_t tsr;
195 uint8_t tpsr;
196 uint16_t tcnt;
197 uint16_t rcnt;
198 uint32_t rsar;
199 uint8_t rsr;
200 uint8_t rxcr;
201 uint8_t isr;
202 uint8_t dcfg;
203 uint8_t imr;
204 uint8_t phys[6]; /* mac address */
205 uint8_t curpag;
206 uint8_t mult[8]; /* multicast mask array */
207 int mmio_index;
663e8e51
TS
208 VLANClientState *vc;
209#endif
210 uint8_t scb_stat; /* SCB stat/ack byte */
211 uint8_t int_stat; /* PCI interrupt status */
212 uint32_t region[3]; /* PCI region addresses */
213 uint8_t macaddr[6];
214 uint32_t statcounter[19];
215 uint16_t mdimem[32];
216 eeprom_t *eeprom;
217 uint32_t device; /* device variant */
218 uint32_t pointer;
219 /* (cu_base + cu_offset) address the next command block in the command block list. */
220 uint32_t cu_base; /* CU base address */
221 uint32_t cu_offset; /* CU address offset */
222 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
223 uint32_t ru_base; /* RU base address */
224 uint32_t ru_offset; /* RU address offset */
225 uint32_t statsaddr; /* pointer to eepro100_stats_t */
226 eepro100_stats_t statistics; /* statistical counters */
227#if 0
228 uint16_t status;
229#endif
230
231 /* Configuration bytes. */
232 uint8_t configuration[22];
233
234 /* Data in mem is always in the byte order of the controller (le). */
235 uint8_t mem[PCI_MEM_SIZE];
236} EEPRO100State;
237
238/* Default values for MDI (PHY) registers */
239static const uint16_t eepro100_mdi_default[] = {
240 /* MDI Registers 0 - 6, 7 */
241 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
242 /* MDI Registers 8 - 15 */
243 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
244 /* MDI Registers 16 - 31 */
245 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
246 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
247};
248
249/* Readonly mask for MDI (PHY) registers */
250static const uint16_t eepro100_mdi_mask[] = {
251 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
252 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
253 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
254 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
255};
256
257#define POLYNOMIAL 0x04c11db6
258
259/* From FreeBSD */
260/* XXX: optimize */
261static int compute_mcast_idx(const uint8_t * ep)
262{
263 uint32_t crc;
264 int carry, i, j;
265 uint8_t b;
266
267 crc = 0xffffffff;
268 for (i = 0; i < 6; i++) {
269 b = *ep++;
270 for (j = 0; j < 8; j++) {
271 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
272 crc <<= 1;
273 b >>= 1;
aac443e6 274 if (carry) {
663e8e51 275 crc = ((crc ^ POLYNOMIAL) | carry);
aac443e6 276 }
663e8e51
TS
277 }
278 }
279 return (crc >> 26);
280}
281
282#if defined(DEBUG_EEPRO100)
283static const char *nic_dump(const uint8_t * buf, unsigned size)
284{
285 static char dump[3 * 16 + 1];
286 char *p = &dump[0];
aac443e6 287 if (size > 16) {
663e8e51 288 size = 16;
aac443e6 289 }
663e8e51
TS
290 while (size-- > 0) {
291 p += sprintf(p, " %02x", *buf++);
292 }
293 return dump;
294}
295#endif /* DEBUG_EEPRO100 */
296
297enum scb_stat_ack {
298 stat_ack_not_ours = 0x00,
299 stat_ack_sw_gen = 0x04,
300 stat_ack_rnr = 0x10,
301 stat_ack_cu_idle = 0x20,
302 stat_ack_frame_rx = 0x40,
303 stat_ack_cu_cmd_done = 0x80,
304 stat_ack_not_present = 0xFF,
305 stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
306 stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
307};
308
309static void disable_interrupt(EEPRO100State * s)
310{
311 if (s->int_stat) {
aac443e6 312 TRACE(INT, logout("interrupt disabled\n"));
273a2142 313 qemu_irq_lower(s->dev.irq[0]);
663e8e51
TS
314 s->int_stat = 0;
315 }
316}
317
318static void enable_interrupt(EEPRO100State * s)
319{
320 if (!s->int_stat) {
aac443e6 321 TRACE(INT, logout("interrupt enabled\n"));
273a2142 322 qemu_irq_raise(s->dev.irq[0]);
663e8e51
TS
323 s->int_stat = 1;
324 }
325}
326
327static void eepro100_acknowledge(EEPRO100State * s)
328{
329 s->scb_stat &= ~s->mem[SCBAck];
330 s->mem[SCBAck] = s->scb_stat;
331 if (s->scb_stat == 0) {
332 disable_interrupt(s);
333 }
334}
335
336static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
337{
338 uint8_t mask = ~s->mem[SCBIntmask];
339 s->mem[SCBAck] |= stat;
340 stat = s->scb_stat = s->mem[SCBAck];
341 stat &= (mask | 0x0f);
342 //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
343 if (stat && (mask & 0x01)) {
344 /* SCB mask and SCB Bit M do not disable interrupt. */
345 enable_interrupt(s);
346 } else if (s->int_stat) {
347 disable_interrupt(s);
348 }
349}
350
351static void eepro100_cx_interrupt(EEPRO100State * s)
352{
353 /* CU completed action command. */
354 /* Transmit not ok (82557 only, not in emulation). */
355 eepro100_interrupt(s, 0x80);
356}
357
358static void eepro100_cna_interrupt(EEPRO100State * s)
359{
360 /* CU left the active state. */
361 eepro100_interrupt(s, 0x20);
362}
363
364static void eepro100_fr_interrupt(EEPRO100State * s)
365{
366 /* RU received a complete frame. */
367 eepro100_interrupt(s, 0x40);
368}
369
370#if 0
371static void eepro100_rnr_interrupt(EEPRO100State * s)
372{
373 /* RU is not ready. */
374 eepro100_interrupt(s, 0x10);
375}
376#endif
377
378static void eepro100_mdi_interrupt(EEPRO100State * s)
379{
380 /* MDI completed read or write cycle. */
381 eepro100_interrupt(s, 0x08);
382}
383
384static void eepro100_swi_interrupt(EEPRO100State * s)
385{
386 /* Software has requested an interrupt. */
387 eepro100_interrupt(s, 0x04);
388}
389
390#if 0
391static void eepro100_fcp_interrupt(EEPRO100State * s)
392{
393 /* Flow control pause interrupt (82558 and later). */
394 eepro100_interrupt(s, 0x01);
395}
396#endif
397
398static void pci_reset(EEPRO100State * s)
399{
400 uint32_t device = s->device;
273a2142 401 uint8_t *pci_conf = s->dev.config;
663e8e51 402
aac443e6 403 TRACE(OTHER, logout("%p\n", s));
663e8e51
TS
404
405 /* PCI Vendor ID */
deb54399 406 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
d6fd1e66 407 /* PCI Device ID depends on device and is set below. */
663e8e51
TS
408 /* PCI Command */
409 PCI_CONFIG_16(PCI_COMMAND, 0x0000);
410 /* PCI Status */
411 PCI_CONFIG_16(PCI_STATUS, 0x2800);
412 /* PCI Revision ID */
413 PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
414 /* PCI Class Code */
415 PCI_CONFIG_8(0x09, 0x00);
173a543b 416 pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
663e8e51
TS
417 /* PCI Cache Line Size */
418 /* check cache line size!!! */
419 //~ PCI_CONFIG_8(0x0c, 0x00);
420 /* PCI Latency Timer */
421 PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
422 /* PCI Header Type */
423 /* BIST (built-in self test) */
424#if defined(TARGET_I386)
425// !!! workaround for buggy bios
426//~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
427#endif
428#if 0
429 /* PCI Base Address Registers */
430 /* CSR Memory Mapped Base Address */
431 PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
432 PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_MEM_PREFETCH);
433 /* CSR I/O Mapped Base Address */
434 PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_ADDRESS_SPACE_IO);
435#if 0
436 /* Flash Memory Mapped Base Address */
437 PCI_CONFIG_32(PCI_BASE_ADDRESS_2, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM);
438#endif
439#endif
440 /* Expansion ROM Base Address (depends on boot disable!!!) */
441 PCI_CONFIG_32(0x30, 0x00000000);
442 /* Capability Pointer */
443 PCI_CONFIG_8(0x34, 0xdc);
aac443e6 444 /* Interrupt Line */
663e8e51
TS
445 /* Interrupt Pin */
446 PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
447 /* Minimum Grant */
448 PCI_CONFIG_8(0x3e, 0x08);
449 /* Maximum Latency */
450 PCI_CONFIG_8(0x3f, 0x18);
451 /* Power Management Capabilities / Next Item Pointer / Capability ID */
452 PCI_CONFIG_32(0xdc, 0x7e210001);
453
454 switch (device) {
455 case i82551:
d6fd1e66 456 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
663e8e51
TS
457 PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
458 break;
459 case i82557B:
d6fd1e66 460 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
663e8e51
TS
461 PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
462 break;
463 case i82557C:
d6fd1e66 464 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
663e8e51
TS
465 PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
466 break;
467 case i82558B:
d6fd1e66 468 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
663e8e51
TS
469 PCI_CONFIG_16(PCI_STATUS, 0x2810);
470 PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
471 break;
472 case i82559C:
d6fd1e66 473 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
663e8e51
TS
474 PCI_CONFIG_16(PCI_STATUS, 0x2810);
475 //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
476 break;
477 case i82559ER:
d6fd1e66 478 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
663e8e51
TS
479 PCI_CONFIG_16(PCI_STATUS, 0x2810);
480 PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
481 break;
482 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
483 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
484 default:
485 logout("Device %X is undefined!\n", device);
486 }
487
488 if (device == i82557C || device == i82558B || device == i82559C) {
489 logout("Get device id and revision from EEPROM!!!\n");
490 }
491}
492
493static void nic_selective_reset(EEPRO100State * s)
494{
495 size_t i;
496 uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
497 //~ eeprom93xx_reset(s->eeprom);
498 memcpy(eeprom_contents, s->macaddr, 6);
499 eeprom_contents[0xa] = 0x4000;
500 uint16_t sum = 0;
501 for (i = 0; i < EEPROM_SIZE - 1; i++) {
502 sum += eeprom_contents[i];
503 }
504 eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
aac443e6 505 TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
663e8e51
TS
506
507 memset(s->mem, 0, sizeof(s->mem));
508 uint32_t val = BIT(21);
509 memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
510
511 assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
512 memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
513}
514
515static void nic_reset(void *opaque)
516{
769cf7a5 517 EEPRO100State *s = opaque;
aac443e6 518 TRACE(OTHER, logout("%p\n", s));
663e8e51
TS
519 nic_selective_reset(s);
520}
521
522#if defined(DEBUG_EEPRO100)
6a0b9cc9 523static const char * const reg[PCI_IO_SIZE / 4] = {
663e8e51
TS
524 "Command/Status",
525 "General Pointer",
526 "Port",
527 "EEPROM/Flash Control",
528 "MDI Control",
529 "Receive DMA Byte Count",
aac443e6 530 "Flow control",
663e8e51
TS
531 "General Status/Control"
532};
533
534static char *regname(uint32_t addr)
535{
536 static char buf[16];
537 if (addr < PCI_IO_SIZE) {
538 const char *r = reg[addr / 4];
539 if (r != 0) {
41cbc23c 540 snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
663e8e51 541 } else {
41cbc23c 542 snprintf(buf, sizeof(buf), "0x%02x", addr);
663e8e51
TS
543 }
544 } else {
41cbc23c 545 snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
663e8e51
TS
546 }
547 return buf;
548}
549#endif /* DEBUG_EEPRO100 */
550
551#if 0
552static uint16_t eepro100_read_status(EEPRO100State * s)
553{
554 uint16_t val = s->status;
aac443e6 555 TRACE(OTHER, logout("val=0x%04x\n", val));
663e8e51
TS
556 return val;
557}
558
559static void eepro100_write_status(EEPRO100State * s, uint16_t val)
560{
aac443e6 561 TRACE(OTHER, logout("val=0x%04x\n", val));
663e8e51
TS
562 s->status = val;
563}
564#endif
565
566/*****************************************************************************
567 *
568 * Command emulation.
569 *
570 ****************************************************************************/
571
572#if 0
573static uint16_t eepro100_read_command(EEPRO100State * s)
574{
575 uint16_t val = 0xffff;
aac443e6 576 //~ TRACE(OTHER, logout("val=0x%04x\n", val));
663e8e51
TS
577 return val;
578}
579#endif
580
0859df68
NS
581static bool device_supports_eTxCB(EEPRO100State * s)
582{
583 return (s->device != i82557B && s->device != i82557C);
584}
585
663e8e51
TS
586/* Commands that can be put in a command list entry. */
587enum commands {
588 CmdNOp = 0,
589 CmdIASetup = 1,
590 CmdConfigure = 2,
591 CmdMulticastList = 3,
592 CmdTx = 4,
593 CmdTDR = 5, /* load microcode */
594 CmdDump = 6,
595 CmdDiagnose = 7,
596
597 /* And some extra flags: */
598 CmdSuspend = 0x4000, /* Suspend after completion. */
599 CmdIntr = 0x2000, /* Interrupt after completion. */
600 CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
601};
602
603static cu_state_t get_cu_state(EEPRO100State * s)
604{
605 return ((s->mem[SCBStatus] >> 6) & 0x03);
606}
607
608static void set_cu_state(EEPRO100State * s, cu_state_t state)
609{
610 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
611}
612
613static ru_state_t get_ru_state(EEPRO100State * s)
614{
615 return ((s->mem[SCBStatus] >> 2) & 0x0f);
616}
617
618static void set_ru_state(EEPRO100State * s, ru_state_t state)
619{
620 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
621}
622
623static void dump_statistics(EEPRO100State * s)
624{
625 /* Dump statistical data. Most data is never changed by the emulation
626 * and always 0, so we first just copy the whole block and then those
627 * values which really matter.
628 * Number of data should check configuration!!!
629 */
630 cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
631 stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
632 stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
633 stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
634 stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
635 //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
636 //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
637 //~ missing("CU dump statistical counters");
638}
639
640static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
641{
642 eepro100_tx_t tx;
643 uint32_t cb_address;
644 switch (val) {
645 case CU_NOP:
646 /* No operation. */
647 break;
648 case CU_START:
649 if (get_cu_state(s) != cu_idle) {
650 /* Intel documentation says that CU must be idle for the CU
651 * start command. Intel driver for Linux also starts the CU
652 * from suspended state. */
653 logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
654 //~ assert(!"wrong CU state");
655 }
656 set_cu_state(s, cu_active);
657 s->cu_offset = s->pointer;
658 next_command:
659 cb_address = s->cu_base + s->cu_offset;
660 cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
661 uint16_t status = le16_to_cpu(tx.status);
662 uint16_t command = le16_to_cpu(tx.command);
663 logout
664 ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
665 val, status, command, tx.link);
666 bool bit_el = ((command & 0x8000) != 0);
667 bool bit_s = ((command & 0x4000) != 0);
668 bool bit_i = ((command & 0x2000) != 0);
669 bool bit_nc = ((command & 0x0010) != 0);
670 //~ bool bit_sf = ((command & 0x0008) != 0);
671 uint16_t cmd = command & 0x0007;
672 s->cu_offset = le32_to_cpu(tx.link);
673 switch (cmd) {
674 case CmdNOp:
675 /* Do nothing. */
676 break;
677 case CmdIASetup:
678 cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
aac443e6 679 TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6)));
663e8e51
TS
680 break;
681 case CmdConfigure:
682 cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
683 sizeof(s->configuration));
aac443e6 684 TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
663e8e51
TS
685 break;
686 case CmdMulticastList:
687 //~ missing("multicast list");
688 break;
689 case CmdTx:
690 (void)0;
691 uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
692 uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
aac443e6 693 TRACE(RXTX, logout
663e8e51 694 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
aac443e6 695 tbd_array, tcb_bytes, tx.tbd_count));
663e8e51
TS
696 assert(!bit_nc);
697 //~ assert(!bit_sf);
698 assert(tcb_bytes <= 2600);
699 /* Next assertion fails for local configuration. */
700 //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
701 if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
702 logout
703 ("illegal values of TBD array address and TCB byte count!\n");
704 }
24e6f355
RD
705 // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
706 uint8_t buf[2600];
663e8e51
TS
707 uint16_t size = 0;
708 uint32_t tbd_address = cb_address + 0x10;
709 assert(tcb_bytes <= sizeof(buf));
710 while (size < tcb_bytes) {
711 uint32_t tx_buffer_address = ldl_phys(tbd_address);
712 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
713 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
714 tbd_address += 8;
aac443e6 715 TRACE(RXTX, logout
663e8e51 716 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
aac443e6 717 tx_buffer_address, tx_buffer_size));
24e6f355 718 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
663e8e51
TS
719 cpu_physical_memory_read(tx_buffer_address, &buf[size],
720 tx_buffer_size);
721 size += tx_buffer_size;
722 }
723 if (tbd_array == 0xffffffff) {
724 /* Simplified mode. Was already handled by code above. */
725 } else {
726 /* Flexible mode. */
727 uint8_t tbd_count = 0;
0859df68 728 if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
3f9cb1c1 729 /* Extended Flexible TCB. */
663e8e51
TS
730 assert(tcb_bytes == 0);
731 for (; tbd_count < 2; tbd_count++) {
732 uint32_t tx_buffer_address = ldl_phys(tbd_address);
733 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
734 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
735 tbd_address += 8;
aac443e6 736 TRACE(RXTX, logout
3f9cb1c1 737 ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
aac443e6 738 tx_buffer_address, tx_buffer_size));
24e6f355 739 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
663e8e51
TS
740 cpu_physical_memory_read(tx_buffer_address, &buf[size],
741 tx_buffer_size);
742 size += tx_buffer_size;
743 if (tx_buffer_el & 1) {
744 break;
745 }
746 }
747 }
748 tbd_address = tbd_array;
749 for (; tbd_count < tx.tbd_count; tbd_count++) {
750 uint32_t tx_buffer_address = ldl_phys(tbd_address);
751 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
752 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
753 tbd_address += 8;
aac443e6 754 TRACE(RXTX, logout
663e8e51 755 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
aac443e6 756 tx_buffer_address, tx_buffer_size));
24e6f355 757 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
663e8e51
TS
758 cpu_physical_memory_read(tx_buffer_address, &buf[size],
759 tx_buffer_size);
760 size += tx_buffer_size;
761 if (tx_buffer_el & 1) {
762 break;
763 }
764 }
765 }
aac443e6 766 TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
663e8e51
TS
767 qemu_send_packet(s->vc, buf, size);
768 s->statistics.tx_good_frames++;
769 /* Transmit with bad status would raise an CX/TNO interrupt.
770 * (82557 only). Emulation never has bad status. */
771 //~ eepro100_cx_interrupt(s);
772 break;
773 case CmdTDR:
aac443e6 774 TRACE(OTHER, logout("load microcode\n"));
663e8e51
TS
775 /* Starting with offset 8, the command contains
776 * 64 dwords microcode which we just ignore here. */
777 break;
778 default:
779 missing("undefined command");
780 }
781 /* Write new status (success). */
782 stw_phys(cb_address, status | 0x8000 | 0x2000);
783 if (bit_i) {
784 /* CU completed action. */
785 eepro100_cx_interrupt(s);
786 }
787 if (bit_el) {
aac443e6 788 /* CU becomes idle. Terminate command loop. */
663e8e51
TS
789 set_cu_state(s, cu_idle);
790 eepro100_cna_interrupt(s);
791 } else if (bit_s) {
792 /* CU becomes suspended. */
793 set_cu_state(s, cu_suspended);
794 eepro100_cna_interrupt(s);
795 } else {
796 /* More entries in list. */
aac443e6 797 TRACE(OTHER, logout("CU list with at least one more entry\n"));
663e8e51
TS
798 goto next_command;
799 }
aac443e6 800 TRACE(OTHER, logout("CU list empty\n"));
663e8e51
TS
801 /* List is empty. Now CU is idle or suspended. */
802 break;
803 case CU_RESUME:
804 if (get_cu_state(s) != cu_suspended) {
805 logout("bad CU resume from CU state %u\n", get_cu_state(s));
806 /* Workaround for bad Linux eepro100 driver which resumes
807 * from idle state. */
808 //~ missing("cu resume");
809 set_cu_state(s, cu_suspended);
810 }
811 if (get_cu_state(s) == cu_suspended) {
aac443e6 812 TRACE(OTHER, logout("CU resuming\n"));
663e8e51
TS
813 set_cu_state(s, cu_active);
814 goto next_command;
815 }
816 break;
817 case CU_STATSADDR:
818 /* Load dump counters address. */
819 s->statsaddr = s->pointer;
aac443e6 820 TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
663e8e51
TS
821 break;
822 case CU_SHOWSTATS:
823 /* Dump statistical counters. */
aac443e6 824 TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
663e8e51
TS
825 dump_statistics(s);
826 break;
827 case CU_CMD_BASE:
828 /* Load CU base. */
aac443e6 829 TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
663e8e51
TS
830 s->cu_base = s->pointer;
831 break;
832 case CU_DUMPSTATS:
833 /* Dump and reset statistical counters. */
aac443e6 834 TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
663e8e51
TS
835 dump_statistics(s);
836 memset(&s->statistics, 0, sizeof(s->statistics));
837 break;
838 case CU_SRESUME:
839 /* CU static resume. */
840 missing("CU static resume");
841 break;
842 default:
843 missing("Undefined CU command");
844 }
845}
846
847static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
848{
849 switch (val) {
850 case RU_NOP:
851 /* No operation. */
852 break;
853 case RX_START:
854 /* RU start. */
855 if (get_ru_state(s) != ru_idle) {
856 logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
857 //~ assert(!"wrong RU state");
858 }
859 set_ru_state(s, ru_ready);
860 s->ru_offset = s->pointer;
aac443e6 861 TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
663e8e51
TS
862 break;
863 case RX_RESUME:
864 /* Restart RU. */
865 if (get_ru_state(s) != ru_suspended) {
866 logout("RU state is %u, should be %u\n", get_ru_state(s),
867 ru_suspended);
868 //~ assert(!"wrong RU state");
869 }
870 set_ru_state(s, ru_ready);
871 break;
872 case RX_ADDR_LOAD:
873 /* Load RU base. */
aac443e6 874 TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
663e8e51
TS
875 s->ru_base = s->pointer;
876 break;
877 default:
878 logout("val=0x%02x (undefined RU command)\n", val);
879 missing("Undefined SU command");
880 }
881}
882
883static void eepro100_write_command(EEPRO100State * s, uint8_t val)
884{
885 eepro100_ru_command(s, val & 0x0f);
886 eepro100_cu_command(s, val & 0xf0);
887 if ((val) == 0) {
aac443e6 888 TRACE(OTHER, logout("val=0x%02x\n", val));
663e8e51
TS
889 }
890 /* Clear command byte after command was accepted. */
891 s->mem[SCBCmd] = 0;
892}
893
894/*****************************************************************************
895 *
896 * EEPROM emulation.
897 *
898 ****************************************************************************/
899
900#define EEPROM_CS 0x02
901#define EEPROM_SK 0x01
902#define EEPROM_DI 0x04
903#define EEPROM_DO 0x08
904
905static uint16_t eepro100_read_eeprom(EEPRO100State * s)
906{
907 uint16_t val;
908 memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
909 if (eeprom93xx_read(s->eeprom)) {
910 val |= EEPROM_DO;
911 } else {
912 val &= ~EEPROM_DO;
913 }
aac443e6 914 TRACE(EEPROM, logout("val=0x%04x\n", val));
663e8e51
TS
915 return val;
916}
917
918static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
919{
aac443e6 920 TRACE(EEPROM, logout("val=0x%02x\n", val));
663e8e51
TS
921
922 /* mask unwriteable bits */
923 //~ val = SET_MASKED(val, 0x31, eeprom->value);
924
925 int eecs = ((val & EEPROM_CS) != 0);
926 int eesk = ((val & EEPROM_SK) != 0);
927 int eedi = ((val & EEPROM_DI) != 0);
928 eeprom93xx_write(eeprom, eecs, eesk, eedi);
929}
930
931static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
932{
933 s->pointer = le32_to_cpu(val);
aac443e6 934 TRACE(OTHER, logout("val=0x%08x\n", val));
663e8e51
TS
935}
936
937/*****************************************************************************
938 *
939 * MDI emulation.
940 *
941 ****************************************************************************/
942
943#if defined(DEBUG_EEPRO100)
6a0b9cc9 944static const char * const mdi_op_name[] = {
663e8e51
TS
945 "opcode 0",
946 "write",
947 "read",
948 "opcode 3"
949};
950
6a0b9cc9 951static const char * const mdi_reg_name[] = {
663e8e51
TS
952 "Control",
953 "Status",
954 "PHY Identification (Word 1)",
955 "PHY Identification (Word 2)",
956 "Auto-Negotiation Advertisement",
957 "Auto-Negotiation Link Partner Ability",
958 "Auto-Negotiation Expansion"
959};
aac443e6
SW
960
961static const char *reg2name(uint8_t reg)
962{
963 static char buffer[10];
964 const char *p = buffer;
965 if (reg < ARRAY_SIZE(mdi_reg_name)) {
966 p = mdi_reg_name[reg];
967 } else {
968 snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
969 }
970 return p;
971}
663e8e51
TS
972#endif /* DEBUG_EEPRO100 */
973
974static uint32_t eepro100_read_mdi(EEPRO100State * s)
975{
976 uint32_t val;
977 memcpy(&val, &s->mem[0x10], sizeof(val));
978
979#ifdef DEBUG_EEPRO100
980 uint8_t raiseint = (val & BIT(29)) >> 29;
981 uint8_t opcode = (val & BITS(27, 26)) >> 26;
982 uint8_t phy = (val & BITS(25, 21)) >> 21;
983 uint8_t reg = (val & BITS(20, 16)) >> 16;
984 uint16_t data = (val & BITS(15, 0));
985#endif
986 /* Emulation takes no time to finish MDI transaction. */
987 val |= BIT(28);
988 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
989 val, raiseint, mdi_op_name[opcode], phy,
aac443e6 990 reg2name(reg), data));
663e8e51
TS
991 return val;
992}
993
663e8e51
TS
994static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
995{
996 uint8_t raiseint = (val & BIT(29)) >> 29;
997 uint8_t opcode = (val & BITS(27, 26)) >> 26;
998 uint8_t phy = (val & BITS(25, 21)) >> 21;
999 uint8_t reg = (val & BITS(20, 16)) >> 16;
1000 uint16_t data = (val & BITS(15, 0));
aac443e6
SW
1001 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1002 val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
663e8e51
TS
1003 if (phy != 1) {
1004 /* Unsupported PHY address. */
1005 //~ logout("phy must be 1 but is %u\n", phy);
1006 data = 0;
1007 } else if (opcode != 1 && opcode != 2) {
1008 /* Unsupported opcode. */
1009 logout("opcode must be 1 or 2 but is %u\n", opcode);
1010 data = 0;
1011 } else if (reg > 6) {
1012 /* Unsupported register. */
1013 logout("register must be 0...6 but is %u\n", reg);
1014 data = 0;
1015 } else {
1016 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1017 val, raiseint, mdi_op_name[opcode], phy,
aac443e6 1018 reg2name(reg), data));
663e8e51
TS
1019 if (opcode == 1) {
1020 /* MDI write */
1021 switch (reg) {
1022 case 0: /* Control Register */
1023 if (data & 0x8000) {
1024 /* Reset status and control registers to default. */
1025 s->mdimem[0] = eepro100_mdi_default[0];
1026 s->mdimem[1] = eepro100_mdi_default[1];
1027 data = s->mdimem[reg];
1028 } else {
1029 /* Restart Auto Configuration = Normal Operation */
1030 data &= ~0x0200;
1031 }
1032 break;
1033 case 1: /* Status Register */
1034 missing("not writable");
1035 data = s->mdimem[reg];
1036 break;
1037 case 2: /* PHY Identification Register (Word 1) */
1038 case 3: /* PHY Identification Register (Word 2) */
1039 missing("not implemented");
1040 break;
1041 case 4: /* Auto-Negotiation Advertisement Register */
1042 case 5: /* Auto-Negotiation Link Partner Ability Register */
1043 break;
1044 case 6: /* Auto-Negotiation Expansion Register */
1045 default:
1046 missing("not implemented");
1047 }
1048 s->mdimem[reg] = data;
1049 } else if (opcode == 2) {
1050 /* MDI read */
1051 switch (reg) {
1052 case 0: /* Control Register */
1053 if (data & 0x8000) {
1054 /* Reset status and control registers to default. */
1055 s->mdimem[0] = eepro100_mdi_default[0];
1056 s->mdimem[1] = eepro100_mdi_default[1];
1057 }
1058 break;
1059 case 1: /* Status Register */
1060 s->mdimem[reg] |= 0x0020;
1061 break;
1062 case 2: /* PHY Identification Register (Word 1) */
1063 case 3: /* PHY Identification Register (Word 2) */
1064 case 4: /* Auto-Negotiation Advertisement Register */
1065 break;
1066 case 5: /* Auto-Negotiation Link Partner Ability Register */
1067 s->mdimem[reg] = 0x41fe;
1068 break;
1069 case 6: /* Auto-Negotiation Expansion Register */
1070 s->mdimem[reg] = 0x0001;
1071 break;
1072 }
1073 data = s->mdimem[reg];
1074 }
1075 /* Emulation takes no time to finish MDI transaction.
1076 * Set MDI bit in SCB status register. */
1077 s->mem[SCBAck] |= 0x08;
1078 val |= BIT(28);
1079 if (raiseint) {
1080 eepro100_mdi_interrupt(s);
1081 }
1082 }
1083 val = (val & 0xffff0000) + data;
1084 memcpy(&s->mem[0x10], &val, sizeof(val));
1085}
1086
1087/*****************************************************************************
1088 *
1089 * Port emulation.
1090 *
1091 ****************************************************************************/
1092
1093#define PORT_SOFTWARE_RESET 0
1094#define PORT_SELFTEST 1
1095#define PORT_SELECTIVE_RESET 2
1096#define PORT_DUMP 3
1097#define PORT_SELECTION_MASK 3
1098
1099typedef struct {
1100 uint32_t st_sign; /* Self Test Signature */
1101 uint32_t st_result; /* Self Test Results */
1102} eepro100_selftest_t;
1103
1104static uint32_t eepro100_read_port(EEPRO100State * s)
1105{
1106 return 0;
1107}
1108
1109static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1110{
1111 val = le32_to_cpu(val);
1112 uint32_t address = (val & ~PORT_SELECTION_MASK);
1113 uint8_t selection = (val & PORT_SELECTION_MASK);
1114 switch (selection) {
1115 case PORT_SOFTWARE_RESET:
1116 nic_reset(s);
1117 break;
1118 case PORT_SELFTEST:
aac443e6 1119 TRACE(OTHER, logout("selftest address=0x%08x\n", address));
663e8e51
TS
1120 eepro100_selftest_t data;
1121 cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1122 data.st_sign = 0xffffffff;
1123 data.st_result = 0;
1124 cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1125 break;
1126 case PORT_SELECTIVE_RESET:
aac443e6 1127 TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
663e8e51
TS
1128 nic_selective_reset(s);
1129 break;
1130 default:
1131 logout("val=0x%08x\n", val);
1132 missing("unknown port selection");
1133 }
1134}
1135
1136/*****************************************************************************
1137 *
1138 * General hardware emulation.
1139 *
1140 ****************************************************************************/
1141
1142static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1143{
1144 uint8_t val;
1145 if (addr <= sizeof(s->mem) - sizeof(val)) {
1146 memcpy(&val, &s->mem[addr], sizeof(val));
1147 }
1148
1149 switch (addr) {
1150 case SCBStatus:
1151 //~ val = eepro100_read_status(s);
aac443e6 1152 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1153 break;
1154 case SCBAck:
1155 //~ val = eepro100_read_status(s);
aac443e6 1156 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1157 break;
1158 case SCBCmd:
aac443e6 1159 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1160 //~ val = eepro100_read_command(s);
1161 break;
1162 case SCBIntmask:
aac443e6 1163 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1164 break;
1165 case SCBPort + 3:
aac443e6 1166 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1167 break;
1168 case SCBeeprom:
1169 val = eepro100_read_eeprom(s);
1170 break;
1171 case 0x1b: /* PMDR (power management driver register) */
1172 val = 0;
aac443e6 1173 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1174 break;
1175 case 0x1d: /* general status register */
1176 /* 100 Mbps full duplex, valid link */
1177 val = 0x07;
aac443e6 1178 TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
663e8e51
TS
1179 break;
1180 default:
1181 logout("addr=%s val=0x%02x\n", regname(addr), val);
1182 missing("unknown byte read");
1183 }
1184 return val;
1185}
1186
1187static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1188{
1189 uint16_t val;
1190 if (addr <= sizeof(s->mem) - sizeof(val)) {
1191 memcpy(&val, &s->mem[addr], sizeof(val));
1192 }
1193
663e8e51
TS
1194 switch (addr) {
1195 case SCBStatus:
1196 //~ val = eepro100_read_status(s);
aac443e6 1197 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
663e8e51
TS
1198 break;
1199 case SCBeeprom:
1200 val = eepro100_read_eeprom(s);
aac443e6 1201 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
663e8e51
TS
1202 break;
1203 default:
1204 logout("addr=%s val=0x%04x\n", regname(addr), val);
1205 missing("unknown word read");
1206 }
1207 return val;
1208}
1209
1210static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1211{
1212 uint32_t val;
1213 if (addr <= sizeof(s->mem) - sizeof(val)) {
1214 memcpy(&val, &s->mem[addr], sizeof(val));
1215 }
1216
1217 switch (addr) {
1218 case SCBStatus:
1219 //~ val = eepro100_read_status(s);
aac443e6 1220 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
663e8e51
TS
1221 break;
1222 case SCBPointer:
1223 //~ val = eepro100_read_pointer(s);
aac443e6 1224 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
663e8e51
TS
1225 break;
1226 case SCBPort:
1227 val = eepro100_read_port(s);
aac443e6 1228 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
663e8e51
TS
1229 break;
1230 case SCBCtrlMDI:
1231 val = eepro100_read_mdi(s);
1232 break;
1233 default:
1234 logout("addr=%s val=0x%08x\n", regname(addr), val);
1235 missing("unknown longword read");
1236 }
1237 return val;
1238}
1239
1240static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1241{
1242 if (addr <= sizeof(s->mem) - sizeof(val)) {
1243 memcpy(&s->mem[addr], &val, sizeof(val));
1244 }
1245
aac443e6 1246 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1247
1248 switch (addr) {
1249 case SCBStatus:
1250 //~ eepro100_write_status(s, val);
1251 break;
1252 case SCBAck:
1253 eepro100_acknowledge(s);
1254 break;
1255 case SCBCmd:
1256 eepro100_write_command(s, val);
1257 break;
1258 case SCBIntmask:
1259 if (val & BIT(1)) {
1260 eepro100_swi_interrupt(s);
1261 }
1262 eepro100_interrupt(s, 0);
1263 break;
1264 case SCBPort + 3:
aac443e6 1265 case SCBFlow: /* does not exist on 82557 */
3257d2b6
TS
1266 case SCBFlow + 1:
1267 case SCBFlow + 2:
1268 case SCBFlow + 3:
aac443e6 1269 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
663e8e51
TS
1270 break;
1271 case SCBeeprom:
1272 eepro100_write_eeprom(s->eeprom, val);
1273 break;
1274 default:
1275 logout("addr=%s val=0x%02x\n", regname(addr), val);
1276 missing("unknown byte write");
1277 }
1278}
1279
1280static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1281{
1282 if (addr <= sizeof(s->mem) - sizeof(val)) {
1283 memcpy(&s->mem[addr], &val, sizeof(val));
1284 }
1285
aac443e6 1286 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
663e8e51
TS
1287
1288 switch (addr) {
1289 case SCBStatus:
1290 //~ eepro100_write_status(s, val);
1291 eepro100_acknowledge(s);
1292 break;
1293 case SCBCmd:
1294 eepro100_write_command(s, val);
1295 eepro100_write1(s, SCBIntmask, val >> 8);
1296 break;
1297 case SCBeeprom:
1298 eepro100_write_eeprom(s->eeprom, val);
1299 break;
1300 default:
1301 logout("addr=%s val=0x%04x\n", regname(addr), val);
1302 missing("unknown word write");
1303 }
1304}
1305
1306static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1307{
1308 if (addr <= sizeof(s->mem) - sizeof(val)) {
1309 memcpy(&s->mem[addr], &val, sizeof(val));
1310 }
1311
1312 switch (addr) {
1313 case SCBPointer:
1314 eepro100_write_pointer(s, val);
1315 break;
1316 case SCBPort:
aac443e6 1317 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
663e8e51
TS
1318 eepro100_write_port(s, val);
1319 break;
1320 case SCBCtrlMDI:
1321 eepro100_write_mdi(s, val);
1322 break;
1323 default:
1324 logout("addr=%s val=0x%08x\n", regname(addr), val);
1325 missing("unknown longword write");
1326 }
1327}
1328
aac443e6
SW
1329/*****************************************************************************
1330 *
1331 * Port mapped I/O.
1332 *
1333 ****************************************************************************/
1334
663e8e51
TS
1335static uint32_t ioport_read1(void *opaque, uint32_t addr)
1336{
1337 EEPRO100State *s = opaque;
1338 //~ logout("addr=%s\n", regname(addr));
1339 return eepro100_read1(s, addr - s->region[1]);
1340}
1341
1342static uint32_t ioport_read2(void *opaque, uint32_t addr)
1343{
1344 EEPRO100State *s = opaque;
1345 return eepro100_read2(s, addr - s->region[1]);
1346}
1347
1348static uint32_t ioport_read4(void *opaque, uint32_t addr)
1349{
1350 EEPRO100State *s = opaque;
1351 return eepro100_read4(s, addr - s->region[1]);
1352}
1353
1354static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1355{
1356 EEPRO100State *s = opaque;
1357 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1358 eepro100_write1(s, addr - s->region[1], val);
1359}
1360
1361static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1362{
1363 EEPRO100State *s = opaque;
1364 eepro100_write2(s, addr - s->region[1], val);
1365}
1366
1367static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1368{
1369 EEPRO100State *s = opaque;
1370 eepro100_write4(s, addr - s->region[1], val);
1371}
1372
1373/***********************************************************/
1374/* PCI EEPRO100 definitions */
1375
663e8e51
TS
1376static void pci_map(PCIDevice * pci_dev, int region_num,
1377 uint32_t addr, uint32_t size, int type)
1378{
273a2142 1379 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
663e8e51 1380
aac443e6
SW
1381 TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1382 region_num, addr, size, type));
663e8e51
TS
1383
1384 assert(region_num == 1);
1385 register_ioport_write(addr, size, 1, ioport_write1, s);
1386 register_ioport_read(addr, size, 1, ioport_read1, s);
1387 register_ioport_write(addr, size, 2, ioport_write2, s);
1388 register_ioport_read(addr, size, 2, ioport_read2, s);
1389 register_ioport_write(addr, size, 4, ioport_write4, s);
1390 register_ioport_read(addr, size, 4, ioport_read4, s);
1391
1392 s->region[region_num] = addr;
1393}
1394
aac443e6
SW
1395/*****************************************************************************
1396 *
1397 * Memory mapped I/O.
1398 *
1399 ****************************************************************************/
1400
663e8e51
TS
1401static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1402{
1403 EEPRO100State *s = opaque;
663e8e51
TS
1404 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1405 eepro100_write1(s, addr, val);
1406}
1407
1408static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1409{
1410 EEPRO100State *s = opaque;
663e8e51
TS
1411 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1412 eepro100_write2(s, addr, val);
1413}
1414
1415static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1416{
1417 EEPRO100State *s = opaque;
663e8e51
TS
1418 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1419 eepro100_write4(s, addr, val);
1420}
1421
1422static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1423{
1424 EEPRO100State *s = opaque;
663e8e51
TS
1425 //~ logout("addr=%s\n", regname(addr));
1426 return eepro100_read1(s, addr);
1427}
1428
1429static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1430{
1431 EEPRO100State *s = opaque;
663e8e51
TS
1432 //~ logout("addr=%s\n", regname(addr));
1433 return eepro100_read2(s, addr);
1434}
1435
1436static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1437{
1438 EEPRO100State *s = opaque;
663e8e51
TS
1439 //~ logout("addr=%s\n", regname(addr));
1440 return eepro100_read4(s, addr);
1441}
1442
d60efc6b 1443static CPUWriteMemoryFunc * const pci_mmio_write[] = {
663e8e51
TS
1444 pci_mmio_writeb,
1445 pci_mmio_writew,
1446 pci_mmio_writel
1447};
1448
d60efc6b 1449static CPUReadMemoryFunc * const pci_mmio_read[] = {
663e8e51
TS
1450 pci_mmio_readb,
1451 pci_mmio_readw,
1452 pci_mmio_readl
1453};
1454
1455static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1456 uint32_t addr, uint32_t size, int type)
1457{
273a2142 1458 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
663e8e51 1459
aac443e6
SW
1460 TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1461 region_num, addr, size, type));
663e8e51
TS
1462
1463 if (region_num == 0) {
1464 /* Map control / status registers. */
273a2142
JQ
1465 cpu_register_physical_memory(addr, size, s->mmio_index);
1466 s->region[region_num] = addr;
663e8e51
TS
1467 }
1468}
1469
e3f5ec2b 1470static int nic_can_receive(VLANClientState *vc)
663e8e51 1471{
e3f5ec2b 1472 EEPRO100State *s = vc->opaque;
aac443e6 1473 TRACE(RXTX, logout("%p\n", s));
663e8e51
TS
1474 return get_ru_state(s) == ru_ready;
1475 //~ return !eepro100_buffer_full(s);
1476}
1477
4f1c942b 1478static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
663e8e51
TS
1479{
1480 /* TODO:
1481 * - Magic packets should set bit 30 in power management driver register.
1482 * - Interesting packets should set bit 29 in power management driver register.
1483 */
e3f5ec2b 1484 EEPRO100State *s = vc->opaque;
663e8e51
TS
1485 uint16_t rfd_status = 0xa000;
1486 static const uint8_t broadcast_macaddr[6] =
1487 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1488
1489 /* TODO: check multiple IA bit. */
1490 assert(!(s->configuration[20] & BIT(6)));
1491
1492 if (s->configuration[8] & 0x80) {
1493 /* CSMA is disabled. */
1494 logout("%p received while CSMA is disabled\n", s);
4f1c942b 1495 return -1;
663e8e51
TS
1496 } else if (size < 64 && (s->configuration[7] & 1)) {
1497 /* Short frame and configuration byte 7/0 (discard short receive) set:
1498 * Short frame is discarded */
1499 logout("%p received short frame (%d byte)\n", s, size);
1500 s->statistics.rx_short_frame_errors++;
4f1c942b 1501 //~ return -1;
663e8e51
TS
1502 } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
1503 /* Long frame and configuration byte 18/3 (long receive ok) not set:
1504 * Long frames are discarded. */
1505 logout("%p received long frame (%d byte), ignored\n", s, size);
4f1c942b 1506 return -1;
663e8e51
TS
1507 } else if (memcmp(buf, s->macaddr, 6) == 0) { // !!!
1508 /* Frame matches individual address. */
1509 /* TODO: check configuration byte 15/4 (ignore U/L). */
aac443e6 1510 TRACE(RXTX, logout("%p received frame for me, len=%d\n", s, size));
663e8e51
TS
1511 } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1512 /* Broadcast frame. */
aac443e6 1513 TRACE(RXTX, logout("%p received broadcast, len=%d\n", s, size));
663e8e51
TS
1514 rfd_status |= 0x0002;
1515 } else if (buf[0] & 0x01) { // !!!
1516 /* Multicast frame. */
aac443e6 1517 TRACE(RXTX, logout("%p received multicast, len=%d\n", s, size));
663e8e51
TS
1518 /* TODO: check multicast all bit. */
1519 assert(!(s->configuration[21] & BIT(3)));
1520 int mcast_idx = compute_mcast_idx(buf);
1521 if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
4f1c942b 1522 return size;
663e8e51
TS
1523 }
1524 rfd_status |= 0x0002;
1525 } else if (s->configuration[15] & 1) {
1526 /* Promiscuous: receive all. */
aac443e6 1527 TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%d\n", s, size));
663e8e51
TS
1528 rfd_status |= 0x0004;
1529 } else {
aac443e6
SW
1530 TRACE(RXTX, logout("%p received frame, ignored, len=%d,%s\n", s, size,
1531 nic_dump(buf, size)));
4f1c942b 1532 return size;
663e8e51
TS
1533 }
1534
1535 if (get_ru_state(s) != ru_ready) {
aac443e6
SW
1536 /* No resources available. */
1537 logout("no resources, state=%u\n", get_ru_state(s));
663e8e51 1538 s->statistics.rx_resource_errors++;
aac443e6 1539 //~ assert(!"no resources");
4f1c942b 1540 return -1;
663e8e51
TS
1541 }
1542 //~ !!!
1543//~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1544 eepro100_rx_t rx;
1545 cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1546 offsetof(eepro100_rx_t, packet));
1547 uint16_t rfd_command = le16_to_cpu(rx.command);
1548 uint16_t rfd_size = le16_to_cpu(rx.size);
1549 assert(size <= rfd_size);
1550 if (size < 64) {
1551 rfd_status |= 0x0080;
1552 }
aac443e6
SW
1553 TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1554 rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
663e8e51
TS
1555 stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1556 rfd_status);
1557 stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1558 /* Early receive interrupt not supported. */
1559 //~ eepro100_er_interrupt(s);
1560 /* Receive CRC Transfer not supported. */
1561 assert(!(s->configuration[18] & 4));
1562 /* TODO: check stripping enable bit. */
1563 //~ assert(!(s->configuration[17] & 1));
1564 cpu_physical_memory_write(s->ru_base + s->ru_offset +
1565 offsetof(eepro100_rx_t, packet), buf, size);
1566 s->statistics.rx_good_frames++;
1567 eepro100_fr_interrupt(s);
1568 s->ru_offset = le32_to_cpu(rx.link);
1569 if (rfd_command & 0x8000) {
1570 /* EL bit is set, so this was the last frame. */
1571 assert(0);
1572 }
1573 if (rfd_command & 0x4000) {
1574 /* S bit is set. */
1575 set_ru_state(s, ru_suspended);
1576 }
4f1c942b 1577 return size;
663e8e51
TS
1578}
1579
1580static int nic_load(QEMUFile * f, void *opaque, int version_id)
1581{
769cf7a5 1582 EEPRO100State *s = opaque;
2657c663 1583 int i;
663e8e51
TS
1584 int ret;
1585
663e8e51
TS
1586 if (version_id > 3)
1587 return -EINVAL;
1588
273a2142
JQ
1589 if (version_id >= 3) {
1590 ret = pci_device_load(&s->dev, f);
663e8e51
TS
1591 if (ret < 0)
1592 return ret;
1593 }
1594
1595 if (version_id >= 2) {
1596 qemu_get_8s(f, &s->rxcr);
1597 } else {
1598 s->rxcr = 0x0c;
1599 }
1600
1601 qemu_get_8s(f, &s->cmd);
1602 qemu_get_be32s(f, &s->start);
1603 qemu_get_be32s(f, &s->stop);
1604 qemu_get_8s(f, &s->boundary);
1605 qemu_get_8s(f, &s->tsr);
1606 qemu_get_8s(f, &s->tpsr);
1607 qemu_get_be16s(f, &s->tcnt);
1608 qemu_get_be16s(f, &s->rcnt);
1609 qemu_get_be32s(f, &s->rsar);
1610 qemu_get_8s(f, &s->rsr);
1611 qemu_get_8s(f, &s->isr);
1612 qemu_get_8s(f, &s->dcfg);
1613 qemu_get_8s(f, &s->imr);
1614 qemu_get_buffer(f, s->phys, 6);
1615 qemu_get_8s(f, &s->curpag);
1616 qemu_get_buffer(f, s->mult, 8);
1617 qemu_get_buffer(f, s->mem, sizeof(s->mem));
1618
aac443e6 1619 /* Restore all members of struct between scv_stat and mem. */
2657c663
AZ
1620 qemu_get_8s(f, &s->scb_stat);
1621 qemu_get_8s(f, &s->int_stat);
aac443e6 1622 for (i = 0; i < 3; i++) {
2657c663 1623 qemu_get_be32s(f, &s->region[i]);
aac443e6 1624 }
2657c663 1625 qemu_get_buffer(f, s->macaddr, 6);
aac443e6 1626 for (i = 0; i < 19; i++) {
2657c663 1627 qemu_get_be32s(f, &s->statcounter[i]);
aac443e6
SW
1628 }
1629 for (i = 0; i < 32; i++) {
2657c663 1630 qemu_get_be16s(f, &s->mdimem[i]);
aac443e6
SW
1631 }
1632 /* The eeprom should be saved and restored by its own routines. */
2657c663
AZ
1633 qemu_get_be32s(f, &s->device);
1634 qemu_get_be32s(f, &s->pointer);
1635 qemu_get_be32s(f, &s->cu_base);
1636 qemu_get_be32s(f, &s->cu_offset);
1637 qemu_get_be32s(f, &s->ru_base);
1638 qemu_get_be32s(f, &s->ru_offset);
1639 qemu_get_be32s(f, &s->statsaddr);
aac443e6 1640 /* Restore epro100_stats_t statistics. */
2657c663
AZ
1641 qemu_get_be32s(f, &s->statistics.tx_good_frames);
1642 qemu_get_be32s(f, &s->statistics.tx_max_collisions);
1643 qemu_get_be32s(f, &s->statistics.tx_late_collisions);
1644 qemu_get_be32s(f, &s->statistics.tx_underruns);
1645 qemu_get_be32s(f, &s->statistics.tx_lost_crs);
1646 qemu_get_be32s(f, &s->statistics.tx_deferred);
1647 qemu_get_be32s(f, &s->statistics.tx_single_collisions);
1648 qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
1649 qemu_get_be32s(f, &s->statistics.tx_total_collisions);
1650 qemu_get_be32s(f, &s->statistics.rx_good_frames);
1651 qemu_get_be32s(f, &s->statistics.rx_crc_errors);
1652 qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
1653 qemu_get_be32s(f, &s->statistics.rx_resource_errors);
1654 qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
1655 qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
1656 qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
1657 qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
1658 qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
1659 qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
1660 qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
1661 qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
1662 qemu_get_be32s(f, &s->statistics.complete);
1663#if 0
1664 qemu_get_be16s(f, &s->status);
1665#endif
1666
1667 /* Configuration bytes. */
1668 qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
1669
663e8e51
TS
1670 return 0;
1671}
1672
1673static void nic_save(QEMUFile * f, void *opaque)
1674{
769cf7a5 1675 EEPRO100State *s = opaque;
2657c663 1676 int i;
663e8e51 1677
273a2142 1678 pci_device_save(&s->dev, f);
663e8e51
TS
1679
1680 qemu_put_8s(f, &s->rxcr);
1681
1682 qemu_put_8s(f, &s->cmd);
1683 qemu_put_be32s(f, &s->start);
1684 qemu_put_be32s(f, &s->stop);
1685 qemu_put_8s(f, &s->boundary);
1686 qemu_put_8s(f, &s->tsr);
1687 qemu_put_8s(f, &s->tpsr);
1688 qemu_put_be16s(f, &s->tcnt);
1689 qemu_put_be16s(f, &s->rcnt);
1690 qemu_put_be32s(f, &s->rsar);
1691 qemu_put_8s(f, &s->rsr);
1692 qemu_put_8s(f, &s->isr);
1693 qemu_put_8s(f, &s->dcfg);
1694 qemu_put_8s(f, &s->imr);
1695 qemu_put_buffer(f, s->phys, 6);
1696 qemu_put_8s(f, &s->curpag);
1697 qemu_put_buffer(f, s->mult, 8);
1698 qemu_put_buffer(f, s->mem, sizeof(s->mem));
2657c663 1699
aac443e6 1700 /* Save all members of struct between scv_stat and mem. */
2657c663
AZ
1701 qemu_put_8s(f, &s->scb_stat);
1702 qemu_put_8s(f, &s->int_stat);
aac443e6 1703 for (i = 0; i < 3; i++) {
2657c663 1704 qemu_put_be32s(f, &s->region[i]);
aac443e6 1705 }
2657c663 1706 qemu_put_buffer(f, s->macaddr, 6);
aac443e6 1707 for (i = 0; i < 19; i++) {
2657c663 1708 qemu_put_be32s(f, &s->statcounter[i]);
aac443e6
SW
1709 }
1710 for (i = 0; i < 32; i++) {
2657c663 1711 qemu_put_be16s(f, &s->mdimem[i]);
aac443e6
SW
1712 }
1713 /* The eeprom should be saved and restored by its own routines. */
2657c663
AZ
1714 qemu_put_be32s(f, &s->device);
1715 qemu_put_be32s(f, &s->pointer);
1716 qemu_put_be32s(f, &s->cu_base);
1717 qemu_put_be32s(f, &s->cu_offset);
1718 qemu_put_be32s(f, &s->ru_base);
1719 qemu_put_be32s(f, &s->ru_offset);
1720 qemu_put_be32s(f, &s->statsaddr);
aac443e6 1721 /* Save epro100_stats_t statistics. */
2657c663
AZ
1722 qemu_put_be32s(f, &s->statistics.tx_good_frames);
1723 qemu_put_be32s(f, &s->statistics.tx_max_collisions);
1724 qemu_put_be32s(f, &s->statistics.tx_late_collisions);
1725 qemu_put_be32s(f, &s->statistics.tx_underruns);
1726 qemu_put_be32s(f, &s->statistics.tx_lost_crs);
1727 qemu_put_be32s(f, &s->statistics.tx_deferred);
1728 qemu_put_be32s(f, &s->statistics.tx_single_collisions);
1729 qemu_put_be32s(f, &s->statistics.tx_multiple_collisions);
1730 qemu_put_be32s(f, &s->statistics.tx_total_collisions);
1731 qemu_put_be32s(f, &s->statistics.rx_good_frames);
1732 qemu_put_be32s(f, &s->statistics.rx_crc_errors);
1733 qemu_put_be32s(f, &s->statistics.rx_alignment_errors);
1734 qemu_put_be32s(f, &s->statistics.rx_resource_errors);
1735 qemu_put_be32s(f, &s->statistics.rx_overrun_errors);
1736 qemu_put_be32s(f, &s->statistics.rx_cdt_errors);
1737 qemu_put_be32s(f, &s->statistics.rx_short_frame_errors);
1738 qemu_put_be32s(f, &s->statistics.fc_xmt_pause);
1739 qemu_put_be32s(f, &s->statistics.fc_rcv_pause);
1740 qemu_put_be32s(f, &s->statistics.fc_rcv_unsupported);
1741 qemu_put_be16s(f, &s->statistics.xmt_tco_frames);
1742 qemu_put_be16s(f, &s->statistics.rcv_tco_frames);
1743 qemu_put_be32s(f, &s->statistics.complete);
1744#if 0
1745 qemu_put_be16s(f, &s->status);
1746#endif
1747
1748 /* Configuration bytes. */
1749 qemu_put_buffer(f, s->configuration, sizeof(s->configuration));
663e8e51
TS
1750}
1751
b946a153
AL
1752static void nic_cleanup(VLANClientState *vc)
1753{
1754 EEPRO100State *s = vc->opaque;
1755
1756 unregister_savevm(vc->model, s);
1757
1758 eeprom93xx_free(s->eeprom);
1759}
1760
1761static int pci_nic_uninit(PCIDevice *dev)
1762{
273a2142 1763 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, dev);
b946a153
AL
1764
1765 cpu_unregister_io_memory(s->mmio_index);
1766
1767 return 0;
1768}
1769
81a322d4 1770static int nic_init(PCIDevice *pci_dev, uint32_t device)
663e8e51 1771{
273a2142 1772 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
663e8e51 1773
aac443e6 1774 TRACE(OTHER, logout("\n"));
663e8e51 1775
273a2142 1776 s->dev.unregister = pci_nic_uninit;
663e8e51 1777
663e8e51 1778 s->device = device;
663e8e51
TS
1779
1780 pci_reset(s);
1781
1782 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1783 * i82559 and later support 64 or 256 word EEPROM. */
1784 s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1785
1786 /* Handler for memory-mapped I/O */
273a2142 1787 s->mmio_index =
1eed09cb 1788 cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
663e8e51 1789
273a2142 1790 pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
663e8e51
TS
1791 PCI_ADDRESS_SPACE_MEM |
1792 PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
273a2142 1793 pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
663e8e51 1794 pci_map);
273a2142 1795 pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
663e8e51
TS
1796 pci_mmio_map);
1797
273a2142 1798 qdev_get_macaddr(&s->dev.qdev, s->macaddr);
663e8e51
TS
1799 logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1800 assert(s->region[1] == 0);
1801
1802 nic_reset(s);
1803
273a2142 1804 s->vc = qdev_get_vlan_client(&s->dev.qdev,
463af534 1805 nic_can_receive, nic_receive, NULL,
b946a153 1806 nic_cleanup, s);
663e8e51 1807
7cb7434b 1808 qemu_format_nic_info_str(s->vc, s->macaddr);
aac443e6 1809 TRACE(OTHER, logout("%s\n", s->vc->info_str));
663e8e51 1810
a08d4367 1811 qemu_register_reset(nic_reset, s);
663e8e51 1812
32a8f6ae 1813 register_savevm(s->vc->model, -1, 3, nic_save, nic_load, s);
81a322d4 1814 return 0;
663e8e51
TS
1815}
1816
81a322d4 1817static int pci_i82551_init(PCIDevice *dev)
9d07d757 1818{
81a322d4 1819 return nic_init(dev, i82551);
9d07d757
PB
1820}
1821
81a322d4 1822static int pci_i82557b_init(PCIDevice *dev)
663e8e51 1823{
81a322d4 1824 return nic_init(dev, i82557B);
663e8e51
TS
1825}
1826
81a322d4 1827static int pci_i82559er_init(PCIDevice *dev)
663e8e51 1828{
81a322d4 1829 return nic_init(dev, i82559ER);
663e8e51
TS
1830}
1831
0aab0d3a
GH
1832static PCIDeviceInfo eepro100_info[] = {
1833 {
1834 .qdev.name = "i82551",
273a2142 1835 .qdev.size = sizeof(EEPRO100State),
0aab0d3a
GH
1836 .init = pci_i82551_init,
1837 },{
1838 .qdev.name = "i82557b",
273a2142 1839 .qdev.size = sizeof(EEPRO100State),
0aab0d3a
GH
1840 .init = pci_i82557b_init,
1841 },{
1842 .qdev.name = "i82559er",
273a2142 1843 .qdev.size = sizeof(EEPRO100State),
0aab0d3a
GH
1844 .init = pci_i82559er_init,
1845 },{
1846 /* end of list */
1847 }
1848};
1849
9d07d757 1850static void eepro100_register_devices(void)
663e8e51 1851{
0aab0d3a 1852 pci_qdev_register_many(eepro100_info);
663e8e51
TS
1853}
1854
9d07d757 1855device_init(eepro100_register_devices)