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