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