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