2 * QEMU i8255x (PRO100) emulation
4 * Copyright (c) 2006-2007 Stefan Weil
6 * Portions of the code are copies from grub / etherboot eepro100.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, see <http://www.gnu.org/licenses/>.
22 * Tested features (i82559):
23 * PXE boot (i386) no valid link
24 * Linux networking (i386) ok
32 * Intel 8255x 10/100 Mbps Ethernet Controller Family
33 * Open Source Software Developer Manual
36 #if defined(TARGET_I386)
37 # warning "PXE boot still not working!"
40 #include <stddef.h> /* offsetof */
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))
58 /* debug EEPRO100 card */
59 //~ #define DEBUG_EEPRO100
62 #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
64 #define logout(fmt, ...) ((void)0)
67 /* Set flags to 0 to disable debug output. */
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 /* Offsets to the various registers.
114 All accesses need not be longword aligned. */
115 enum speedo_offsets {
118 SCBCmd = 2, /* Rx/Command Unit command and status. */
120 SCBPointer = 4, /* General purpose pointer. */
121 SCBPort = 8, /* Misc. commands and operands. */
122 SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
123 SCBCtrlMDI = 16, /* MDI interface control. */
124 SCBEarlyRx = 20, /* Early receive byte count. */
128 /* A speedo3 transmit buffer descriptor with two buffers... */
132 uint32_t link; /* void * */
133 uint32_t tx_desc_addr; /* transmit buffer decsriptor array address. */
134 uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
135 uint8_t tx_threshold; /* transmit threshold */
136 uint8_t tbd_count; /* TBD number */
137 //~ /* This constitutes two "TBD" entries: hdr and data */
138 //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
139 //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
140 //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
141 //~ int32_t tx_buf_size1; /* Length of Tx data. */
144 /* Receive frame descriptor. */
148 uint32_t link; /* struct RxFD * */
149 uint32_t rx_buf_addr; /* void * */
152 char packet[MAX_ETH_FRAME_SIZE + 4];
156 uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
157 tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
158 tx_multiple_collisions, tx_total_collisions;
159 uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
160 rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
161 rx_short_frame_errors;
162 uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
163 uint16_t xmt_tco_frames, rcv_tco_frames;
199 uint8_t phys[6]; /* mac address */
201 uint8_t mult[8]; /* multicast mask array */
205 uint8_t scb_stat; /* SCB stat/ack byte */
206 uint8_t int_stat; /* PCI interrupt status */
207 uint32_t region[3]; /* PCI region addresses */
209 uint32_t statcounter[19];
212 uint32_t device; /* device variant */
214 /* (cu_base + cu_offset) address the next command block in the command block list. */
215 uint32_t cu_base; /* CU base address */
216 uint32_t cu_offset; /* CU address offset */
217 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
218 uint32_t ru_base; /* RU base address */
219 uint32_t ru_offset; /* RU address offset */
220 uint32_t statsaddr; /* pointer to eepro100_stats_t */
221 eepro100_stats_t statistics; /* statistical counters */
226 /* Configuration bytes. */
227 uint8_t configuration[22];
229 /* Data in mem is always in the byte order of the controller (le). */
230 uint8_t mem[PCI_MEM_SIZE];
233 /* Default values for MDI (PHY) registers */
234 static const uint16_t eepro100_mdi_default[] = {
235 /* MDI Registers 0 - 6, 7 */
236 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
237 /* MDI Registers 8 - 15 */
238 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
239 /* MDI Registers 16 - 31 */
240 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
241 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
244 /* Readonly mask for MDI (PHY) registers */
245 static const uint16_t eepro100_mdi_mask[] = {
246 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
247 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
248 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
249 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
252 #define POLYNOMIAL 0x04c11db6
256 static int compute_mcast_idx(const uint8_t * ep)
263 for (i = 0; i < 6; i++) {
265 for (j = 0; j < 8; j++) {
266 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
270 crc = ((crc ^ POLYNOMIAL) | carry);
276 #if defined(DEBUG_EEPRO100)
277 static const char *nic_dump(const uint8_t * buf, unsigned size)
279 static char dump[3 * 16 + 1];
284 p += sprintf(p, " %02x", *buf++);
288 #endif /* DEBUG_EEPRO100 */
291 stat_ack_not_ours = 0x00,
292 stat_ack_sw_gen = 0x04,
294 stat_ack_cu_idle = 0x20,
295 stat_ack_frame_rx = 0x40,
296 stat_ack_cu_cmd_done = 0x80,
297 stat_ack_not_present = 0xFF,
298 stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
299 stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
302 static void disable_interrupt(EEPRO100State * s)
305 logout("interrupt disabled\n");
306 qemu_irq_lower(s->dev.irq[0]);
311 static void enable_interrupt(EEPRO100State * s)
314 logout("interrupt enabled\n");
315 qemu_irq_raise(s->dev.irq[0]);
320 static void eepro100_acknowledge(EEPRO100State * s)
322 s->scb_stat &= ~s->mem[SCBAck];
323 s->mem[SCBAck] = s->scb_stat;
324 if (s->scb_stat == 0) {
325 disable_interrupt(s);
329 static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
331 uint8_t mask = ~s->mem[SCBIntmask];
332 s->mem[SCBAck] |= stat;
333 stat = s->scb_stat = s->mem[SCBAck];
334 stat &= (mask | 0x0f);
335 //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
336 if (stat && (mask & 0x01)) {
337 /* SCB mask and SCB Bit M do not disable interrupt. */
339 } else if (s->int_stat) {
340 disable_interrupt(s);
344 static void eepro100_cx_interrupt(EEPRO100State * s)
346 /* CU completed action command. */
347 /* Transmit not ok (82557 only, not in emulation). */
348 eepro100_interrupt(s, 0x80);
351 static void eepro100_cna_interrupt(EEPRO100State * s)
353 /* CU left the active state. */
354 eepro100_interrupt(s, 0x20);
357 static void eepro100_fr_interrupt(EEPRO100State * s)
359 /* RU received a complete frame. */
360 eepro100_interrupt(s, 0x40);
364 static void eepro100_rnr_interrupt(EEPRO100State * s)
366 /* RU is not ready. */
367 eepro100_interrupt(s, 0x10);
371 static void eepro100_mdi_interrupt(EEPRO100State * s)
373 /* MDI completed read or write cycle. */
374 eepro100_interrupt(s, 0x08);
377 static void eepro100_swi_interrupt(EEPRO100State * s)
379 /* Software has requested an interrupt. */
380 eepro100_interrupt(s, 0x04);
384 static void eepro100_fcp_interrupt(EEPRO100State * s)
386 /* Flow control pause interrupt (82558 and later). */
387 eepro100_interrupt(s, 0x01);
391 static void pci_reset(EEPRO100State * s)
393 uint32_t device = s->device;
394 uint8_t *pci_conf = s->dev.config;
399 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
400 /* PCI Device ID depends on device and is set below. */
402 PCI_CONFIG_16(PCI_COMMAND, 0x0000);
404 PCI_CONFIG_16(PCI_STATUS, 0x2800);
405 /* PCI Revision ID */
406 PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
408 PCI_CONFIG_8(0x09, 0x00);
409 pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
410 /* PCI Cache Line Size */
411 /* check cache line size!!! */
412 //~ PCI_CONFIG_8(0x0c, 0x00);
413 /* PCI Latency Timer */
414 PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
415 /* PCI Header Type */
416 /* BIST (built-in self test) */
417 #if defined(TARGET_I386)
418 // !!! workaround for buggy bios
419 //~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
422 /* PCI Base Address Registers */
423 /* CSR Memory Mapped Base Address */
424 PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
425 PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_MEM_PREFETCH);
426 /* CSR I/O Mapped Base Address */
427 PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_ADDRESS_SPACE_IO);
429 /* Flash Memory Mapped Base Address */
430 PCI_CONFIG_32(PCI_BASE_ADDRESS_2, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM);
433 /* Expansion ROM Base Address (depends on boot disable!!!) */
434 PCI_CONFIG_32(0x30, 0x00000000);
435 /* Capability Pointer */
436 PCI_CONFIG_8(0x34, 0xdc);
438 PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
440 PCI_CONFIG_8(0x3e, 0x08);
441 /* Maximum Latency */
442 PCI_CONFIG_8(0x3f, 0x18);
443 /* Power Management Capabilities / Next Item Pointer / Capability ID */
444 PCI_CONFIG_32(0xdc, 0x7e210001);
448 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
449 PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
452 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
453 PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
456 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
457 PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
460 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
461 PCI_CONFIG_16(PCI_STATUS, 0x2810);
462 PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
465 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
466 PCI_CONFIG_16(PCI_STATUS, 0x2810);
467 //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
470 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
471 PCI_CONFIG_16(PCI_STATUS, 0x2810);
472 PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
474 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
475 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
477 logout("Device %X is undefined!\n", device);
480 if (device == i82557C || device == i82558B || device == i82559C) {
481 logout("Get device id and revision from EEPROM!!!\n");
485 static void nic_selective_reset(EEPRO100State * s)
488 uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
489 //~ eeprom93xx_reset(s->eeprom);
490 memcpy(eeprom_contents, s->macaddr, 6);
491 eeprom_contents[0xa] = 0x4000;
493 for (i = 0; i < EEPROM_SIZE - 1; i++) {
494 sum += eeprom_contents[i];
496 eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
498 memset(s->mem, 0, sizeof(s->mem));
499 uint32_t val = BIT(21);
500 memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
502 assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
503 memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
506 static void nic_reset(void *opaque)
508 EEPRO100State *s = opaque;
514 nic_selective_reset(s);
517 #if defined(DEBUG_EEPRO100)
518 static const char *reg[PCI_IO_SIZE / 4] = {
522 "EEPROM/Flash Control",
524 "Receive DMA Byte Count",
525 "Flow control register",
526 "General Status/Control"
529 static char *regname(uint32_t addr)
532 if (addr < PCI_IO_SIZE) {
533 const char *r = reg[addr / 4];
535 sprintf(buf, "%s+%u", r, addr % 4);
537 sprintf(buf, "0x%02x", addr);
540 sprintf(buf, "??? 0x%08x", addr);
544 #endif /* DEBUG_EEPRO100 */
547 static uint16_t eepro100_read_status(EEPRO100State * s)
549 uint16_t val = s->status;
550 logout("val=0x%04x\n", val);
554 static void eepro100_write_status(EEPRO100State * s, uint16_t val)
556 logout("val=0x%04x\n", val);
561 /*****************************************************************************
565 ****************************************************************************/
568 static uint16_t eepro100_read_command(EEPRO100State * s)
570 uint16_t val = 0xffff;
571 //~ logout("val=0x%04x\n", val);
576 static bool device_supports_eTxCB(EEPRO100State * s)
578 return (s->device != i82557B && s->device != i82557C);
581 /* Commands that can be put in a command list entry. */
586 CmdMulticastList = 3,
588 CmdTDR = 5, /* load microcode */
592 /* And some extra flags: */
593 CmdSuspend = 0x4000, /* Suspend after completion. */
594 CmdIntr = 0x2000, /* Interrupt after completion. */
595 CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
598 static cu_state_t get_cu_state(EEPRO100State * s)
600 return ((s->mem[SCBStatus] >> 6) & 0x03);
603 static void set_cu_state(EEPRO100State * s, cu_state_t state)
605 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
608 static ru_state_t get_ru_state(EEPRO100State * s)
610 return ((s->mem[SCBStatus] >> 2) & 0x0f);
613 static void set_ru_state(EEPRO100State * s, ru_state_t state)
615 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
618 static void dump_statistics(EEPRO100State * s)
620 /* Dump statistical data. Most data is never changed by the emulation
621 * and always 0, so we first just copy the whole block and then those
622 * values which really matter.
623 * Number of data should check configuration!!!
625 cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
626 stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
627 stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
628 stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
629 stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
630 //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
631 //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
632 //~ missing("CU dump statistical counters");
635 static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
644 if (get_cu_state(s) != cu_idle) {
645 /* Intel documentation says that CU must be idle for the CU
646 * start command. Intel driver for Linux also starts the CU
647 * from suspended state. */
648 logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
649 //~ assert(!"wrong CU state");
651 set_cu_state(s, cu_active);
652 s->cu_offset = s->pointer;
654 cb_address = s->cu_base + s->cu_offset;
655 cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
656 uint16_t status = le16_to_cpu(tx.status);
657 uint16_t command = le16_to_cpu(tx.command);
659 ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
660 val, status, command, tx.link);
661 bool bit_el = ((command & 0x8000) != 0);
662 bool bit_s = ((command & 0x4000) != 0);
663 bool bit_i = ((command & 0x2000) != 0);
664 bool bit_nc = ((command & 0x0010) != 0);
665 //~ bool bit_sf = ((command & 0x0008) != 0);
666 uint16_t cmd = command & 0x0007;
667 s->cu_offset = le32_to_cpu(tx.link);
673 cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
674 logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
677 cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
678 sizeof(s->configuration));
679 logout("configuration: %s\n", nic_dump(&s->configuration[0], 16));
681 case CmdMulticastList:
682 //~ missing("multicast list");
686 uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
687 uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
689 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
690 tbd_array, tcb_bytes, tx.tbd_count);
693 assert(tcb_bytes <= 2600);
694 /* Next assertion fails for local configuration. */
695 //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
696 if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
698 ("illegal values of TBD array address and TCB byte count!\n");
700 // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
703 uint32_t tbd_address = cb_address + 0x10;
704 assert(tcb_bytes <= sizeof(buf));
705 while (size < tcb_bytes) {
706 uint32_t tx_buffer_address = ldl_phys(tbd_address);
707 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
708 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
711 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
712 tx_buffer_address, tx_buffer_size);
713 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
714 cpu_physical_memory_read(tx_buffer_address, &buf[size],
716 size += tx_buffer_size;
718 if (tbd_array == 0xffffffff) {
719 /* Simplified mode. Was already handled by code above. */
722 uint8_t tbd_count = 0;
723 if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
724 /* Extended Flexible TCB. */
725 assert(tcb_bytes == 0);
726 for (; tbd_count < 2; tbd_count++) {
727 uint32_t tx_buffer_address = ldl_phys(tbd_address);
728 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
729 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
732 ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
733 tx_buffer_address, tx_buffer_size);
734 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
735 cpu_physical_memory_read(tx_buffer_address, &buf[size],
737 size += tx_buffer_size;
738 if (tx_buffer_el & 1) {
743 tbd_address = tbd_array;
744 for (; tbd_count < tx.tbd_count; tbd_count++) {
745 uint32_t tx_buffer_address = ldl_phys(tbd_address);
746 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
747 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
750 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
751 tx_buffer_address, tx_buffer_size);
752 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
753 cpu_physical_memory_read(tx_buffer_address, &buf[size],
755 size += tx_buffer_size;
756 if (tx_buffer_el & 1) {
761 qemu_send_packet(s->vc, buf, size);
762 s->statistics.tx_good_frames++;
763 /* Transmit with bad status would raise an CX/TNO interrupt.
764 * (82557 only). Emulation never has bad status. */
765 //~ eepro100_cx_interrupt(s);
768 logout("load microcode\n");
769 /* Starting with offset 8, the command contains
770 * 64 dwords microcode which we just ignore here. */
773 missing("undefined command");
775 /* Write new status (success). */
776 stw_phys(cb_address, status | 0x8000 | 0x2000);
778 /* CU completed action. */
779 eepro100_cx_interrupt(s);
782 /* CU becomes idle. */
783 set_cu_state(s, cu_idle);
784 eepro100_cna_interrupt(s);
786 /* CU becomes suspended. */
787 set_cu_state(s, cu_suspended);
788 eepro100_cna_interrupt(s);
790 /* More entries in list. */
791 logout("CU list with at least one more entry\n");
794 logout("CU list empty\n");
795 /* List is empty. Now CU is idle or suspended. */
798 if (get_cu_state(s) != cu_suspended) {
799 logout("bad CU resume from CU state %u\n", get_cu_state(s));
800 /* Workaround for bad Linux eepro100 driver which resumes
801 * from idle state. */
802 //~ missing("cu resume");
803 set_cu_state(s, cu_suspended);
805 if (get_cu_state(s) == cu_suspended) {
806 logout("CU resuming\n");
807 set_cu_state(s, cu_active);
812 /* Load dump counters address. */
813 s->statsaddr = s->pointer;
814 logout("val=0x%02x (status address)\n", val);
817 /* Dump statistical counters. */
822 logout("val=0x%02x (CU base address)\n", val);
823 s->cu_base = s->pointer;
826 /* Dump and reset statistical counters. */
828 memset(&s->statistics, 0, sizeof(s->statistics));
831 /* CU static resume. */
832 missing("CU static resume");
835 missing("Undefined CU command");
839 static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
847 if (get_ru_state(s) != ru_idle) {
848 logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
849 //~ assert(!"wrong RU state");
851 set_ru_state(s, ru_ready);
852 s->ru_offset = s->pointer;
853 logout("val=0x%02x (rx start)\n", val);
857 if (get_ru_state(s) != ru_suspended) {
858 logout("RU state is %u, should be %u\n", get_ru_state(s),
860 //~ assert(!"wrong RU state");
862 set_ru_state(s, ru_ready);
866 logout("val=0x%02x (RU base address)\n", val);
867 s->ru_base = s->pointer;
870 logout("val=0x%02x (undefined RU command)\n", val);
871 missing("Undefined SU command");
875 static void eepro100_write_command(EEPRO100State * s, uint8_t val)
877 eepro100_ru_command(s, val & 0x0f);
878 eepro100_cu_command(s, val & 0xf0);
880 logout("val=0x%02x\n", val);
882 /* Clear command byte after command was accepted. */
886 /*****************************************************************************
890 ****************************************************************************/
892 #define EEPROM_CS 0x02
893 #define EEPROM_SK 0x01
894 #define EEPROM_DI 0x04
895 #define EEPROM_DO 0x08
897 static uint16_t eepro100_read_eeprom(EEPRO100State * s)
900 memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
901 if (eeprom93xx_read(s->eeprom)) {
909 static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
911 logout("write val=0x%02x\n", val);
913 /* mask unwriteable bits */
914 //~ val = SET_MASKED(val, 0x31, eeprom->value);
916 int eecs = ((val & EEPROM_CS) != 0);
917 int eesk = ((val & EEPROM_SK) != 0);
918 int eedi = ((val & EEPROM_DI) != 0);
919 eeprom93xx_write(eeprom, eecs, eesk, eedi);
922 static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
924 s->pointer = le32_to_cpu(val);
925 logout("val=0x%08x\n", val);
928 /*****************************************************************************
932 ****************************************************************************/
934 #if defined(DEBUG_EEPRO100)
935 static const char *mdi_op_name[] = {
942 static const char *mdi_reg_name[] = {
945 "PHY Identification (Word 1)",
946 "PHY Identification (Word 2)",
947 "Auto-Negotiation Advertisement",
948 "Auto-Negotiation Link Partner Ability",
949 "Auto-Negotiation Expansion"
951 #endif /* DEBUG_EEPRO100 */
953 static uint32_t eepro100_read_mdi(EEPRO100State * s)
956 memcpy(&val, &s->mem[0x10], sizeof(val));
958 #ifdef DEBUG_EEPRO100
959 uint8_t raiseint = (val & BIT(29)) >> 29;
960 uint8_t opcode = (val & BITS(27, 26)) >> 26;
961 uint8_t phy = (val & BITS(25, 21)) >> 21;
962 uint8_t reg = (val & BITS(20, 16)) >> 16;
963 uint16_t data = (val & BITS(15, 0));
965 /* Emulation takes no time to finish MDI transaction. */
967 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
968 val, raiseint, mdi_op_name[opcode], phy,
969 mdi_reg_name[reg], data));
973 //~ #define BITS(val, upper, lower) (val & ???)
974 static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
976 uint8_t raiseint = (val & BIT(29)) >> 29;
977 uint8_t opcode = (val & BITS(27, 26)) >> 26;
978 uint8_t phy = (val & BITS(25, 21)) >> 21;
979 uint8_t reg = (val & BITS(20, 16)) >> 16;
980 uint16_t data = (val & BITS(15, 0));
982 /* Unsupported PHY address. */
983 //~ logout("phy must be 1 but is %u\n", phy);
985 } else if (opcode != 1 && opcode != 2) {
986 /* Unsupported opcode. */
987 logout("opcode must be 1 or 2 but is %u\n", opcode);
989 } else if (reg > 6) {
990 /* Unsupported register. */
991 logout("register must be 0...6 but is %u\n", reg);
994 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
995 val, raiseint, mdi_op_name[opcode], phy,
996 mdi_reg_name[reg], data));
1000 case 0: /* Control Register */
1001 if (data & 0x8000) {
1002 /* Reset status and control registers to default. */
1003 s->mdimem[0] = eepro100_mdi_default[0];
1004 s->mdimem[1] = eepro100_mdi_default[1];
1005 data = s->mdimem[reg];
1007 /* Restart Auto Configuration = Normal Operation */
1011 case 1: /* Status Register */
1012 missing("not writable");
1013 data = s->mdimem[reg];
1015 case 2: /* PHY Identification Register (Word 1) */
1016 case 3: /* PHY Identification Register (Word 2) */
1017 missing("not implemented");
1019 case 4: /* Auto-Negotiation Advertisement Register */
1020 case 5: /* Auto-Negotiation Link Partner Ability Register */
1022 case 6: /* Auto-Negotiation Expansion Register */
1024 missing("not implemented");
1026 s->mdimem[reg] = data;
1027 } else if (opcode == 2) {
1030 case 0: /* Control Register */
1031 if (data & 0x8000) {
1032 /* Reset status and control registers to default. */
1033 s->mdimem[0] = eepro100_mdi_default[0];
1034 s->mdimem[1] = eepro100_mdi_default[1];
1037 case 1: /* Status Register */
1038 s->mdimem[reg] |= 0x0020;
1040 case 2: /* PHY Identification Register (Word 1) */
1041 case 3: /* PHY Identification Register (Word 2) */
1042 case 4: /* Auto-Negotiation Advertisement Register */
1044 case 5: /* Auto-Negotiation Link Partner Ability Register */
1045 s->mdimem[reg] = 0x41fe;
1047 case 6: /* Auto-Negotiation Expansion Register */
1048 s->mdimem[reg] = 0x0001;
1051 data = s->mdimem[reg];
1053 /* Emulation takes no time to finish MDI transaction.
1054 * Set MDI bit in SCB status register. */
1055 s->mem[SCBAck] |= 0x08;
1058 eepro100_mdi_interrupt(s);
1061 val = (val & 0xffff0000) + data;
1062 memcpy(&s->mem[0x10], &val, sizeof(val));
1065 /*****************************************************************************
1069 ****************************************************************************/
1071 #define PORT_SOFTWARE_RESET 0
1072 #define PORT_SELFTEST 1
1073 #define PORT_SELECTIVE_RESET 2
1075 #define PORT_SELECTION_MASK 3
1078 uint32_t st_sign; /* Self Test Signature */
1079 uint32_t st_result; /* Self Test Results */
1080 } eepro100_selftest_t;
1082 static uint32_t eepro100_read_port(EEPRO100State * s)
1087 static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1089 val = le32_to_cpu(val);
1090 uint32_t address = (val & ~PORT_SELECTION_MASK);
1091 uint8_t selection = (val & PORT_SELECTION_MASK);
1092 switch (selection) {
1093 case PORT_SOFTWARE_RESET:
1097 logout("selftest address=0x%08x\n", address);
1098 eepro100_selftest_t data;
1099 cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1100 data.st_sign = 0xffffffff;
1102 cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1104 case PORT_SELECTIVE_RESET:
1105 logout("selective reset, selftest address=0x%08x\n", address);
1106 nic_selective_reset(s);
1109 logout("val=0x%08x\n", val);
1110 missing("unknown port selection");
1114 /*****************************************************************************
1116 * General hardware emulation.
1118 ****************************************************************************/
1120 static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1123 if (addr <= sizeof(s->mem) - sizeof(val)) {
1124 memcpy(&val, &s->mem[addr], sizeof(val));
1129 //~ val = eepro100_read_status(s);
1130 logout("addr=%s val=0x%02x\n", regname(addr), val);
1133 //~ val = eepro100_read_status(s);
1134 logout("addr=%s val=0x%02x\n", regname(addr), val);
1137 logout("addr=%s val=0x%02x\n", regname(addr), val);
1138 //~ val = eepro100_read_command(s);
1141 logout("addr=%s val=0x%02x\n", regname(addr), val);
1144 logout("addr=%s val=0x%02x\n", regname(addr), val);
1147 val = eepro100_read_eeprom(s);
1149 case 0x1b: /* PMDR (power management driver register) */
1151 logout("addr=%s val=0x%02x\n", regname(addr), val);
1153 case 0x1d: /* general status register */
1154 /* 100 Mbps full duplex, valid link */
1156 logout("addr=General Status val=%02x\n", val);
1159 logout("addr=%s val=0x%02x\n", regname(addr), val);
1160 missing("unknown byte read");
1165 static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1168 if (addr <= sizeof(s->mem) - sizeof(val)) {
1169 memcpy(&val, &s->mem[addr], sizeof(val));
1172 logout("addr=%s val=0x%04x\n", regname(addr), val);
1176 //~ val = eepro100_read_status(s);
1179 val = eepro100_read_eeprom(s);
1182 logout("addr=%s val=0x%04x\n", regname(addr), val);
1183 missing("unknown word read");
1188 static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1191 if (addr <= sizeof(s->mem) - sizeof(val)) {
1192 memcpy(&val, &s->mem[addr], sizeof(val));
1197 //~ val = eepro100_read_status(s);
1198 logout("addr=%s val=0x%08x\n", regname(addr), val);
1201 //~ val = eepro100_read_pointer(s);
1202 logout("addr=%s val=0x%08x\n", regname(addr), val);
1205 val = eepro100_read_port(s);
1206 logout("addr=%s val=0x%08x\n", regname(addr), val);
1209 val = eepro100_read_mdi(s);
1212 logout("addr=%s val=0x%08x\n", regname(addr), val);
1213 missing("unknown longword read");
1218 static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1220 if (addr <= sizeof(s->mem) - sizeof(val)) {
1221 memcpy(&s->mem[addr], &val, sizeof(val));
1224 logout("addr=%s val=0x%02x\n", regname(addr), val);
1228 //~ eepro100_write_status(s, val);
1231 eepro100_acknowledge(s);
1234 eepro100_write_command(s, val);
1238 eepro100_swi_interrupt(s);
1240 eepro100_interrupt(s, 0);
1247 logout("addr=%s val=0x%02x\n", regname(addr), val);
1250 eepro100_write_eeprom(s->eeprom, val);
1253 logout("addr=%s val=0x%02x\n", regname(addr), val);
1254 missing("unknown byte write");
1258 static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1260 if (addr <= sizeof(s->mem) - sizeof(val)) {
1261 memcpy(&s->mem[addr], &val, sizeof(val));
1264 logout("addr=%s val=0x%04x\n", regname(addr), val);
1268 //~ eepro100_write_status(s, val);
1269 eepro100_acknowledge(s);
1272 eepro100_write_command(s, val);
1273 eepro100_write1(s, SCBIntmask, val >> 8);
1276 eepro100_write_eeprom(s->eeprom, val);
1279 logout("addr=%s val=0x%04x\n", regname(addr), val);
1280 missing("unknown word write");
1284 static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1286 if (addr <= sizeof(s->mem) - sizeof(val)) {
1287 memcpy(&s->mem[addr], &val, sizeof(val));
1292 eepro100_write_pointer(s, val);
1295 logout("addr=%s val=0x%08x\n", regname(addr), val);
1296 eepro100_write_port(s, val);
1299 eepro100_write_mdi(s, val);
1302 logout("addr=%s val=0x%08x\n", regname(addr), val);
1303 missing("unknown longword write");
1307 static uint32_t ioport_read1(void *opaque, uint32_t addr)
1309 EEPRO100State *s = opaque;
1310 //~ logout("addr=%s\n", regname(addr));
1311 return eepro100_read1(s, addr - s->region[1]);
1314 static uint32_t ioport_read2(void *opaque, uint32_t addr)
1316 EEPRO100State *s = opaque;
1317 return eepro100_read2(s, addr - s->region[1]);
1320 static uint32_t ioport_read4(void *opaque, uint32_t addr)
1322 EEPRO100State *s = opaque;
1323 return eepro100_read4(s, addr - s->region[1]);
1326 static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1328 EEPRO100State *s = opaque;
1329 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1330 eepro100_write1(s, addr - s->region[1], val);
1333 static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1335 EEPRO100State *s = opaque;
1336 eepro100_write2(s, addr - s->region[1], val);
1339 static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1341 EEPRO100State *s = opaque;
1342 eepro100_write4(s, addr - s->region[1], val);
1345 /***********************************************************/
1346 /* PCI EEPRO100 definitions */
1348 static void pci_map(PCIDevice * pci_dev, int region_num,
1349 uint32_t addr, uint32_t size, int type)
1351 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
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 * const pci_mmio_write[] = {
1415 static CPUReadMemoryFunc * const pci_mmio_read[] = {
1421 static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1422 uint32_t addr, uint32_t size, int type)
1424 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, 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, s->mmio_index);
1432 s->region[region_num] = addr;
1436 static int nic_can_receive(VLANClientState *vc)
1438 EEPRO100State *s = vc->opaque;
1440 return get_ru_state(s) == ru_ready;
1441 //~ return !eepro100_buffer_full(s);
1444 static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
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 = vc->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);
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++;
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);
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)))) {
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;
1496 logout("%p received frame, ignored, len=%d,%s\n", s, size,
1497 nic_dump(buf, size));
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");
1509 //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
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);
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),
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. */
1539 if (rfd_command & 0x4000) {
1541 set_ru_state(s, ru_suspended);
1546 static int nic_load(QEMUFile * f, void *opaque, int version_id)
1548 EEPRO100State *s = opaque;
1555 if (version_id >= 3) {
1556 ret = pci_device_load(&s->dev, f);
1561 if (version_id >= 2) {
1562 qemu_get_8s(f, &s->rxcr);
1567 qemu_get_8s(f, &s->cmd);
1568 qemu_get_be32s(f, &s->start);
1569 qemu_get_be32s(f, &s->stop);
1570 qemu_get_8s(f, &s->boundary);
1571 qemu_get_8s(f, &s->tsr);
1572 qemu_get_8s(f, &s->tpsr);
1573 qemu_get_be16s(f, &s->tcnt);
1574 qemu_get_be16s(f, &s->rcnt);
1575 qemu_get_be32s(f, &s->rsar);
1576 qemu_get_8s(f, &s->rsr);
1577 qemu_get_8s(f, &s->isr);
1578 qemu_get_8s(f, &s->dcfg);
1579 qemu_get_8s(f, &s->imr);
1580 qemu_get_buffer(f, s->phys, 6);
1581 qemu_get_8s(f, &s->curpag);
1582 qemu_get_buffer(f, s->mult, 8);
1583 qemu_get_buffer(f, s->mem, sizeof(s->mem));
1585 /* Restore all members of struct between scv_stat and mem */
1586 qemu_get_8s(f, &s->scb_stat);
1587 qemu_get_8s(f, &s->int_stat);
1588 for (i = 0; i < 3; i++)
1589 qemu_get_be32s(f, &s->region[i]);
1590 qemu_get_buffer(f, s->macaddr, 6);
1591 for (i = 0; i < 19; i++)
1592 qemu_get_be32s(f, &s->statcounter[i]);
1593 for (i = 0; i < 32; i++)
1594 qemu_get_be16s(f, &s->mdimem[i]);
1595 /* The eeprom should be saved and restored by its own routines */
1596 qemu_get_be32s(f, &s->device);
1597 qemu_get_be32s(f, &s->pointer);
1598 qemu_get_be32s(f, &s->cu_base);
1599 qemu_get_be32s(f, &s->cu_offset);
1600 qemu_get_be32s(f, &s->ru_base);
1601 qemu_get_be32s(f, &s->ru_offset);
1602 qemu_get_be32s(f, &s->statsaddr);
1603 /* Restore epro100_stats_t statistics */
1604 qemu_get_be32s(f, &s->statistics.tx_good_frames);
1605 qemu_get_be32s(f, &s->statistics.tx_max_collisions);
1606 qemu_get_be32s(f, &s->statistics.tx_late_collisions);
1607 qemu_get_be32s(f, &s->statistics.tx_underruns);
1608 qemu_get_be32s(f, &s->statistics.tx_lost_crs);
1609 qemu_get_be32s(f, &s->statistics.tx_deferred);
1610 qemu_get_be32s(f, &s->statistics.tx_single_collisions);
1611 qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
1612 qemu_get_be32s(f, &s->statistics.tx_total_collisions);
1613 qemu_get_be32s(f, &s->statistics.rx_good_frames);
1614 qemu_get_be32s(f, &s->statistics.rx_crc_errors);
1615 qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
1616 qemu_get_be32s(f, &s->statistics.rx_resource_errors);
1617 qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
1618 qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
1619 qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
1620 qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
1621 qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
1622 qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
1623 qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
1624 qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
1625 qemu_get_be32s(f, &s->statistics.complete);
1627 qemu_get_be16s(f, &s->status);
1630 /* Configuration bytes. */
1631 qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
1636 static void nic_save(QEMUFile * f, void *opaque)
1638 EEPRO100State *s = opaque;
1641 pci_device_save(&s->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);
1705 qemu_put_be16s(f, &s->status);
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 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, dev);
1725 cpu_unregister_io_memory(s->mmio_index);
1730 static int nic_init(PCIDevice *pci_dev, uint32_t device)
1732 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1736 s->dev.unregister = pci_nic_uninit;
1742 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1743 * i82559 and later support 64 or 256 word EEPROM. */
1744 s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1746 /* Handler for memory-mapped I/O */
1748 cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1750 pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1751 PCI_ADDRESS_SPACE_MEM |
1752 PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
1753 pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
1755 pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
1758 qdev_get_macaddr(&s->dev.qdev, s->macaddr);
1759 logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1760 assert(s->region[1] == 0);
1764 s->vc = qdev_get_vlan_client(&s->dev.qdev,
1765 nic_can_receive, nic_receive, NULL,
1768 qemu_format_nic_info_str(s->vc, s->macaddr);
1770 qemu_register_reset(nic_reset, s);
1772 register_savevm(s->vc->model, -1, 3, nic_save, nic_load, s);
1776 static int pci_i82551_init(PCIDevice *dev)
1778 return nic_init(dev, i82551);
1781 static int pci_i82557b_init(PCIDevice *dev)
1783 return nic_init(dev, i82557B);
1786 static int pci_i82559er_init(PCIDevice *dev)
1788 return nic_init(dev, i82559ER);
1791 static PCIDeviceInfo eepro100_info[] = {
1793 .qdev.name = "i82551",
1794 .qdev.size = sizeof(EEPRO100State),
1795 .init = pci_i82551_init,
1797 .qdev.name = "i82557b",
1798 .qdev.size = sizeof(EEPRO100State),
1799 .init = pci_i82557b_init,
1801 .qdev.name = "i82559er",
1802 .qdev.size = sizeof(EEPRO100State),
1803 .init = pci_i82559er_init,
1809 static void eepro100_register_devices(void)
1811 pci_qdev_register_many(eepro100_info);
1814 device_init(eepro100_register_devices)