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. */
68 #define INT 1 /* interrupt related actions */
69 #define MDI 1 /* mdi related actions */
72 #define EEPROM 1 /* eeprom related actions */
74 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
76 #define missing(text) assert(!"feature is missing in this emulation: " text)
78 #define MAX_ETH_FRAME_SIZE 1514
80 /* This driver supports several different devices which are declared here. */
81 #define i82550 0x82550
82 #define i82551 0x82551
83 #define i82557A 0x82557a
84 #define i82557B 0x82557b
85 #define i82557C 0x82557c
86 #define i82558A 0x82558a
87 #define i82558B 0x82558b
88 #define i82559A 0x82559a
89 #define i82559B 0x82559b
90 #define i82559C 0x82559c
91 #define i82559ER 0x82559e
92 #define i82562 0x82562
94 /* Use 64 word EEPROM. TODO: could be a runtime option. */
95 #define EEPROM_SIZE 64
97 #define PCI_MEM_SIZE (4 * KiB)
98 #define PCI_IO_SIZE 64
99 #define PCI_FLASH_SIZE (128 * KiB)
101 #define BIT(n) (1 << (n))
102 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
104 /* The SCB accepts the following controls for the Tx and Rx units: */
105 #define CU_NOP 0x0000 /* No operation. */
106 #define CU_START 0x0010 /* CU start. */
107 #define CU_RESUME 0x0020 /* CU resume. */
108 #define CU_STATSADDR 0x0040 /* Load dump counters address. */
109 #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
110 #define CU_CMD_BASE 0x0060 /* Load CU base address. */
111 #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
112 #define CU_SRESUME 0x00a0 /* CU static resume. */
114 #define RU_NOP 0x0000
115 #define RX_START 0x0001
116 #define RX_RESUME 0x0002
117 #define RX_ABORT 0x0004
118 #define RX_ADDR_LOAD 0x0006
119 #define RX_RESUMENR 0x0007
120 #define INT_MASK 0x0100
121 #define DRVR_INT 0x0200 /* Driver generated interrupt. */
123 /* Offsets to the various registers.
124 All accesses need not be longword aligned. */
125 enum speedo_offsets
{
128 SCBCmd
= 2, /* Rx/Command Unit command and status. */
130 SCBPointer
= 4, /* General purpose pointer. */
131 SCBPort
= 8, /* Misc. commands and operands. */
132 SCBflash
= 12, SCBeeprom
= 14, /* EEPROM and flash memory control. */
133 SCBCtrlMDI
= 16, /* MDI interface control. */
134 SCBEarlyRx
= 20, /* Early receive byte count. */
138 /* A speedo3 transmit buffer descriptor with two buffers... */
142 uint32_t link
; /* void * */
143 uint32_t tx_desc_addr
; /* transmit buffer decsriptor array address. */
144 uint16_t tcb_bytes
; /* transmit command block byte count (in lower 14 bits */
145 uint8_t tx_threshold
; /* transmit threshold */
146 uint8_t tbd_count
; /* TBD number */
147 //~ /* This constitutes two "TBD" entries: hdr and data */
148 //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
149 //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
150 //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
151 //~ int32_t tx_buf_size1; /* Length of Tx data. */
154 /* Receive frame descriptor. */
158 uint32_t link
; /* struct RxFD * */
159 uint32_t rx_buf_addr
; /* void * */
162 char packet
[MAX_ETH_FRAME_SIZE
+ 4];
166 uint32_t tx_good_frames
, tx_max_collisions
, tx_late_collisions
,
167 tx_underruns
, tx_lost_crs
, tx_deferred
, tx_single_collisions
,
168 tx_multiple_collisions
, tx_total_collisions
;
169 uint32_t rx_good_frames
, rx_crc_errors
, rx_alignment_errors
,
170 rx_resource_errors
, rx_overrun_errors
, rx_cdt_errors
,
171 rx_short_frame_errors
;
172 uint32_t fc_xmt_pause
, fc_rcv_pause
, fc_rcv_unsupported
;
173 uint16_t xmt_tco_frames
, rcv_tco_frames
;
209 uint8_t phys
[6]; /* mac address */
211 uint8_t mult
[8]; /* multicast mask array */
215 uint8_t scb_stat
; /* SCB stat/ack byte */
216 uint8_t int_stat
; /* PCI interrupt status */
217 uint32_t region
[3]; /* PCI region addresses */
219 uint32_t statcounter
[19];
222 uint32_t device
; /* device variant */
224 /* (cu_base + cu_offset) address the next command block in the command block list. */
225 uint32_t cu_base
; /* CU base address */
226 uint32_t cu_offset
; /* CU address offset */
227 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
228 uint32_t ru_base
; /* RU base address */
229 uint32_t ru_offset
; /* RU address offset */
230 uint32_t statsaddr
; /* pointer to eepro100_stats_t */
231 eepro100_stats_t statistics
; /* statistical counters */
236 /* Configuration bytes. */
237 uint8_t configuration
[22];
239 /* Data in mem is always in the byte order of the controller (le). */
240 uint8_t mem
[PCI_MEM_SIZE
];
243 /* Default values for MDI (PHY) registers */
244 static const uint16_t eepro100_mdi_default
[] = {
245 /* MDI Registers 0 - 6, 7 */
246 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
247 /* MDI Registers 8 - 15 */
248 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
249 /* MDI Registers 16 - 31 */
250 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
251 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
254 /* Readonly mask for MDI (PHY) registers */
255 static const uint16_t eepro100_mdi_mask
[] = {
256 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
257 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
258 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
259 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
262 #define POLYNOMIAL 0x04c11db6
266 static int compute_mcast_idx(const uint8_t * ep
)
273 for (i
= 0; i
< 6; i
++) {
275 for (j
= 0; j
< 8; j
++) {
276 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
280 crc
= ((crc
^ POLYNOMIAL
) | carry
);
287 #if defined(DEBUG_EEPRO100)
288 static const char *nic_dump(const uint8_t * buf
, unsigned size
)
290 static char dump
[3 * 16 + 1];
296 p
+= sprintf(p
, " %02x", *buf
++);
300 #endif /* DEBUG_EEPRO100 */
303 stat_ack_not_ours
= 0x00,
304 stat_ack_sw_gen
= 0x04,
306 stat_ack_cu_idle
= 0x20,
307 stat_ack_frame_rx
= 0x40,
308 stat_ack_cu_cmd_done
= 0x80,
309 stat_ack_not_present
= 0xFF,
310 stat_ack_rx
= (stat_ack_sw_gen
| stat_ack_rnr
| stat_ack_frame_rx
),
311 stat_ack_tx
= (stat_ack_cu_idle
| stat_ack_cu_cmd_done
),
314 static void disable_interrupt(EEPRO100State
* s
)
317 TRACE(INT
, logout("interrupt disabled\n"));
318 qemu_irq_lower(s
->dev
.irq
[0]);
323 static void enable_interrupt(EEPRO100State
* s
)
326 TRACE(INT
, logout("interrupt enabled\n"));
327 qemu_irq_raise(s
->dev
.irq
[0]);
332 static void eepro100_acknowledge(EEPRO100State
* s
)
334 s
->scb_stat
&= ~s
->mem
[SCBAck
];
335 s
->mem
[SCBAck
] = s
->scb_stat
;
336 if (s
->scb_stat
== 0) {
337 disable_interrupt(s
);
341 static void eepro100_interrupt(EEPRO100State
* s
, uint8_t stat
)
343 uint8_t mask
= ~s
->mem
[SCBIntmask
];
344 s
->mem
[SCBAck
] |= stat
;
345 stat
= s
->scb_stat
= s
->mem
[SCBAck
];
346 stat
&= (mask
| 0x0f);
347 //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
348 if (stat
&& (mask
& 0x01)) {
349 /* SCB mask and SCB Bit M do not disable interrupt. */
351 } else if (s
->int_stat
) {
352 disable_interrupt(s
);
356 static void eepro100_cx_interrupt(EEPRO100State
* s
)
358 /* CU completed action command. */
359 /* Transmit not ok (82557 only, not in emulation). */
360 eepro100_interrupt(s
, 0x80);
363 static void eepro100_cna_interrupt(EEPRO100State
* s
)
365 /* CU left the active state. */
366 eepro100_interrupt(s
, 0x20);
369 static void eepro100_fr_interrupt(EEPRO100State
* s
)
371 /* RU received a complete frame. */
372 eepro100_interrupt(s
, 0x40);
376 static void eepro100_rnr_interrupt(EEPRO100State
* s
)
378 /* RU is not ready. */
379 eepro100_interrupt(s
, 0x10);
383 static void eepro100_mdi_interrupt(EEPRO100State
* s
)
385 /* MDI completed read or write cycle. */
386 eepro100_interrupt(s
, 0x08);
389 static void eepro100_swi_interrupt(EEPRO100State
* s
)
391 /* Software has requested an interrupt. */
392 eepro100_interrupt(s
, 0x04);
396 static void eepro100_fcp_interrupt(EEPRO100State
* s
)
398 /* Flow control pause interrupt (82558 and later). */
399 eepro100_interrupt(s
, 0x01);
403 static void pci_reset(EEPRO100State
* s
)
405 uint32_t device
= s
->device
;
406 uint8_t *pci_conf
= s
->dev
.config
;
408 TRACE(OTHER
, logout("%p\n", s
));
411 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
412 /* PCI Device ID depends on device and is set below. */
414 PCI_CONFIG_16(PCI_COMMAND
, 0x0000);
416 PCI_CONFIG_16(PCI_STATUS
, 0x2800);
417 /* PCI Revision ID */
418 PCI_CONFIG_8(PCI_REVISION_ID
, 0x08);
420 PCI_CONFIG_8(0x09, 0x00);
421 pci_config_set_class(pci_conf
, PCI_CLASS_NETWORK_ETHERNET
);
422 /* PCI Cache Line Size */
423 /* check cache line size!!! */
424 //~ PCI_CONFIG_8(0x0c, 0x00);
425 /* PCI Latency Timer */
426 PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
427 /* PCI Header Type */
428 /* BIST (built-in self test) */
429 #if defined(TARGET_I386)
430 // !!! workaround for buggy bios
431 //~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
434 /* PCI Base Address Registers */
435 /* CSR Memory Mapped Base Address */
436 PCI_CONFIG_32(PCI_BASE_ADDRESS_0
,
437 PCI_ADDRESS_SPACE_MEM
| PCI_ADDRESS_SPACE_MEM_PREFETCH
);
438 /* CSR I/O Mapped Base Address */
439 PCI_CONFIG_32(PCI_BASE_ADDRESS_1
, PCI_ADDRESS_SPACE_IO
);
441 /* Flash Memory Mapped Base Address */
442 PCI_CONFIG_32(PCI_BASE_ADDRESS_2
, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM
);
445 /* Expansion ROM Base Address (depends on boot disable!!!) */
446 PCI_CONFIG_32(0x30, 0x00000000);
447 /* Capability Pointer */
448 PCI_CONFIG_8(0x34, 0xdc);
451 PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
453 PCI_CONFIG_8(0x3e, 0x08);
454 /* Maximum Latency */
455 PCI_CONFIG_8(0x3f, 0x18);
456 /* Power Management Capabilities / Next Item Pointer / Capability ID */
457 PCI_CONFIG_32(0xdc, 0x7e210001);
461 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82551IT
);
462 PCI_CONFIG_8(PCI_REVISION_ID
, 0x0f);
465 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82557
);
466 PCI_CONFIG_8(PCI_REVISION_ID
, 0x02);
469 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82557
);
470 PCI_CONFIG_8(PCI_REVISION_ID
, 0x03);
473 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82557
);
474 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
475 PCI_CONFIG_8(PCI_REVISION_ID
, 0x05);
478 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82557
);
479 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
480 //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
483 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82551IT
);
484 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
485 PCI_CONFIG_8(PCI_REVISION_ID
, 0x09);
487 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
488 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
490 logout("Device %X is undefined!\n", device
);
493 if (device
== i82557C
|| device
== i82558B
|| device
== i82559C
) {
494 logout("Get device id and revision from EEPROM!!!\n");
498 static void nic_selective_reset(EEPRO100State
* s
)
501 uint16_t *eeprom_contents
= eeprom93xx_data(s
->eeprom
);
502 //~ eeprom93xx_reset(s->eeprom);
503 memcpy(eeprom_contents
, s
->macaddr
, 6);
504 eeprom_contents
[0xa] = 0x4000;
506 for (i
= 0; i
< EEPROM_SIZE
- 1; i
++) {
507 sum
+= eeprom_contents
[i
];
509 eeprom_contents
[EEPROM_SIZE
- 1] = 0xbaba - sum
;
510 TRACE(EEPROM
, logout("checksum=0x%04x\n", eeprom_contents
[EEPROM_SIZE
- 1]));
512 memset(s
->mem
, 0, sizeof(s
->mem
));
513 uint32_t val
= BIT(21);
514 memcpy(&s
->mem
[SCBCtrlMDI
], &val
, sizeof(val
));
516 assert(sizeof(s
->mdimem
) == sizeof(eepro100_mdi_default
));
517 memcpy(&s
->mdimem
[0], &eepro100_mdi_default
[0], sizeof(s
->mdimem
));
520 static void nic_reset(void *opaque
)
522 EEPRO100State
*s
= opaque
;
523 TRACE(OTHER
, logout("%p\n", s
));
524 nic_selective_reset(s
);
527 #if defined(DEBUG_EEPRO100)
528 static const char * const reg
[PCI_IO_SIZE
/ 4] = {
532 "EEPROM/Flash Control",
534 "Receive DMA Byte Count",
536 "General Status/Control"
539 static char *regname(uint32_t addr
)
542 if (addr
< PCI_IO_SIZE
) {
543 const char *r
= reg
[addr
/ 4];
545 snprintf(buf
, sizeof(buf
), "%s+%u", r
, addr
% 4);
547 snprintf(buf
, sizeof(buf
), "0x%02x", addr
);
550 snprintf(buf
, sizeof(buf
), "??? 0x%08x", addr
);
554 #endif /* DEBUG_EEPRO100 */
557 static uint16_t eepro100_read_status(EEPRO100State
* s
)
559 uint16_t val
= s
->status
;
560 TRACE(OTHER
, logout("val=0x%04x\n", val
));
564 static void eepro100_write_status(EEPRO100State
* s
, uint16_t val
)
566 TRACE(OTHER
, logout("val=0x%04x\n", val
));
571 /*****************************************************************************
575 ****************************************************************************/
578 static uint16_t eepro100_read_command(EEPRO100State
* s
)
580 uint16_t val
= 0xffff;
581 //~ TRACE(OTHER, logout("val=0x%04x\n", val));
586 static bool device_supports_eTxCB(EEPRO100State
* s
)
588 return (s
->device
!= i82557B
&& s
->device
!= i82557C
);
591 /* Commands that can be put in a command list entry. */
596 CmdMulticastList
= 3,
598 CmdTDR
= 5, /* load microcode */
602 /* And some extra flags: */
603 CmdSuspend
= 0x4000, /* Suspend after completion. */
604 CmdIntr
= 0x2000, /* Interrupt after completion. */
605 CmdTxFlex
= 0x0008, /* Use "Flexible mode" for CmdTx command. */
608 static cu_state_t
get_cu_state(EEPRO100State
* s
)
610 return ((s
->mem
[SCBStatus
] >> 6) & 0x03);
613 static void set_cu_state(EEPRO100State
* s
, cu_state_t state
)
615 s
->mem
[SCBStatus
] = (s
->mem
[SCBStatus
] & 0x3f) + (state
<< 6);
618 static ru_state_t
get_ru_state(EEPRO100State
* s
)
620 return ((s
->mem
[SCBStatus
] >> 2) & 0x0f);
623 static void set_ru_state(EEPRO100State
* s
, ru_state_t state
)
625 s
->mem
[SCBStatus
] = (s
->mem
[SCBStatus
] & 0xc3) + (state
<< 2);
628 static void dump_statistics(EEPRO100State
* s
)
630 /* Dump statistical data. Most data is never changed by the emulation
631 * and always 0, so we first just copy the whole block and then those
632 * values which really matter.
633 * Number of data should check configuration!!!
635 cpu_physical_memory_write(s
->statsaddr
, (uint8_t *) & s
->statistics
, 64);
636 stl_phys(s
->statsaddr
+ 0, s
->statistics
.tx_good_frames
);
637 stl_phys(s
->statsaddr
+ 36, s
->statistics
.rx_good_frames
);
638 stl_phys(s
->statsaddr
+ 48, s
->statistics
.rx_resource_errors
);
639 stl_phys(s
->statsaddr
+ 60, s
->statistics
.rx_short_frame_errors
);
640 //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
641 //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
642 //~ missing("CU dump statistical counters");
645 static void eepro100_cu_command(EEPRO100State
* s
, uint8_t val
)
654 if (get_cu_state(s
) != cu_idle
) {
655 /* Intel documentation says that CU must be idle for the CU
656 * start command. Intel driver for Linux also starts the CU
657 * from suspended state. */
658 logout("CU state is %u, should be %u\n", get_cu_state(s
), cu_idle
);
659 //~ assert(!"wrong CU state");
661 set_cu_state(s
, cu_active
);
662 s
->cu_offset
= s
->pointer
;
664 cb_address
= s
->cu_base
+ s
->cu_offset
;
665 cpu_physical_memory_read(cb_address
, (uint8_t *) & tx
, sizeof(tx
));
666 uint16_t status
= le16_to_cpu(tx
.status
);
667 uint16_t command
= le16_to_cpu(tx
.command
);
669 ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
670 val
, status
, command
, tx
.link
);
671 bool bit_el
= ((command
& 0x8000) != 0);
672 bool bit_s
= ((command
& 0x4000) != 0);
673 bool bit_i
= ((command
& 0x2000) != 0);
674 bool bit_nc
= ((command
& 0x0010) != 0);
675 //~ bool bit_sf = ((command & 0x0008) != 0);
676 uint16_t cmd
= command
& 0x0007;
677 s
->cu_offset
= le32_to_cpu(tx
.link
);
683 cpu_physical_memory_read(cb_address
+ 8, &s
->macaddr
[0], 6);
684 TRACE(OTHER
, logout("macaddr: %s\n", nic_dump(&s
->macaddr
[0], 6)));
687 cpu_physical_memory_read(cb_address
+ 8, &s
->configuration
[0],
688 sizeof(s
->configuration
));
689 TRACE(OTHER
, logout("configuration: %s\n", nic_dump(&s
->configuration
[0], 16)));
691 case CmdMulticastList
:
692 //~ missing("multicast list");
696 uint32_t tbd_array
= le32_to_cpu(tx
.tx_desc_addr
);
697 uint16_t tcb_bytes
= (le16_to_cpu(tx
.tcb_bytes
) & 0x3fff);
699 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
700 tbd_array
, tcb_bytes
, tx
.tbd_count
));
703 assert(tcb_bytes
<= 2600);
704 /* Next assertion fails for local configuration. */
705 //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
706 if (!((tcb_bytes
> 0) || (tbd_array
!= 0xffffffff))) {
708 ("illegal values of TBD array address and TCB byte count!\n");
710 // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
713 uint32_t tbd_address
= cb_address
+ 0x10;
714 assert(tcb_bytes
<= sizeof(buf
));
715 while (size
< tcb_bytes
) {
716 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
717 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
718 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
721 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
722 tx_buffer_address
, tx_buffer_size
));
723 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
724 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
726 size
+= tx_buffer_size
;
728 if (tbd_array
== 0xffffffff) {
729 /* Simplified mode. Was already handled by code above. */
732 uint8_t tbd_count
= 0;
733 if (device_supports_eTxCB(s
) && !(s
->configuration
[6] & BIT(4))) {
734 /* Extended Flexible TCB. */
735 assert(tcb_bytes
== 0);
736 for (; tbd_count
< 2; tbd_count
++) {
737 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
738 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
739 uint16_t tx_buffer_el
= lduw_phys(tbd_address
+ 6);
742 ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
743 tx_buffer_address
, tx_buffer_size
));
744 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
745 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
747 size
+= tx_buffer_size
;
748 if (tx_buffer_el
& 1) {
753 tbd_address
= tbd_array
;
754 for (; tbd_count
< tx
.tbd_count
; tbd_count
++) {
755 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
756 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
757 uint16_t tx_buffer_el
= lduw_phys(tbd_address
+ 6);
760 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
761 tx_buffer_address
, tx_buffer_size
));
762 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
763 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
765 size
+= tx_buffer_size
;
766 if (tx_buffer_el
& 1) {
771 TRACE(RXTX
, logout("%p sending frame, len=%d,%s\n", s
, size
, nic_dump(buf
, size
)));
772 qemu_send_packet(s
->vc
, buf
, size
);
773 s
->statistics
.tx_good_frames
++;
774 /* Transmit with bad status would raise an CX/TNO interrupt.
775 * (82557 only). Emulation never has bad status. */
776 //~ eepro100_cx_interrupt(s);
779 TRACE(OTHER
, logout("load microcode\n"));
780 /* Starting with offset 8, the command contains
781 * 64 dwords microcode which we just ignore here. */
784 missing("undefined command");
786 /* Write new status (success). */
787 stw_phys(cb_address
, status
| 0x8000 | 0x2000);
789 /* CU completed action. */
790 eepro100_cx_interrupt(s
);
793 /* CU becomes idle. Terminate command loop. */
794 set_cu_state(s
, cu_idle
);
795 eepro100_cna_interrupt(s
);
797 /* CU becomes suspended. */
798 set_cu_state(s
, cu_suspended
);
799 eepro100_cna_interrupt(s
);
801 /* More entries in list. */
802 TRACE(OTHER
, logout("CU list with at least one more entry\n"));
805 TRACE(OTHER
, logout("CU list empty\n"));
806 /* List is empty. Now CU is idle or suspended. */
809 if (get_cu_state(s
) != cu_suspended
) {
810 logout("bad CU resume from CU state %u\n", get_cu_state(s
));
811 /* Workaround for bad Linux eepro100 driver which resumes
812 * from idle state. */
813 //~ missing("cu resume");
814 set_cu_state(s
, cu_suspended
);
816 if (get_cu_state(s
) == cu_suspended
) {
817 TRACE(OTHER
, logout("CU resuming\n"));
818 set_cu_state(s
, cu_active
);
823 /* Load dump counters address. */
824 s
->statsaddr
= s
->pointer
;
825 TRACE(OTHER
, logout("val=0x%02x (status address)\n", val
));
828 /* Dump statistical counters. */
829 TRACE(OTHER
, logout("val=0x%02x (dump stats)\n", val
));
834 TRACE(OTHER
, logout("val=0x%02x (CU base address)\n", val
));
835 s
->cu_base
= s
->pointer
;
838 /* Dump and reset statistical counters. */
839 TRACE(OTHER
, logout("val=0x%02x (dump stats and reset)\n", val
));
841 memset(&s
->statistics
, 0, sizeof(s
->statistics
));
844 /* CU static resume. */
845 missing("CU static resume");
848 missing("Undefined CU command");
852 static void eepro100_ru_command(EEPRO100State
* s
, uint8_t val
)
860 if (get_ru_state(s
) != ru_idle
) {
861 logout("RU state is %u, should be %u\n", get_ru_state(s
), ru_idle
);
862 //~ assert(!"wrong RU state");
864 set_ru_state(s
, ru_ready
);
865 s
->ru_offset
= s
->pointer
;
866 TRACE(OTHER
, logout("val=0x%02x (rx start)\n", val
));
870 if (get_ru_state(s
) != ru_suspended
) {
871 logout("RU state is %u, should be %u\n", get_ru_state(s
),
873 //~ assert(!"wrong RU state");
875 set_ru_state(s
, ru_ready
);
879 TRACE(OTHER
, logout("val=0x%02x (RU base address)\n", val
));
880 s
->ru_base
= s
->pointer
;
883 logout("val=0x%02x (undefined RU command)\n", val
);
884 missing("Undefined SU command");
888 static void eepro100_write_command(EEPRO100State
* s
, uint8_t val
)
890 eepro100_ru_command(s
, val
& 0x0f);
891 eepro100_cu_command(s
, val
& 0xf0);
893 TRACE(OTHER
, logout("val=0x%02x\n", val
));
895 /* Clear command byte after command was accepted. */
899 /*****************************************************************************
903 ****************************************************************************/
905 #define EEPROM_CS 0x02
906 #define EEPROM_SK 0x01
907 #define EEPROM_DI 0x04
908 #define EEPROM_DO 0x08
910 static uint16_t eepro100_read_eeprom(EEPRO100State
* s
)
913 memcpy(&val
, &s
->mem
[SCBeeprom
], sizeof(val
));
914 if (eeprom93xx_read(s
->eeprom
)) {
919 TRACE(EEPROM
, logout("val=0x%04x\n", val
));
923 static void eepro100_write_eeprom(eeprom_t
* eeprom
, uint8_t val
)
925 TRACE(EEPROM
, logout("val=0x%02x\n", val
));
927 /* mask unwriteable bits */
928 //~ val = SET_MASKED(val, 0x31, eeprom->value);
930 int eecs
= ((val
& EEPROM_CS
) != 0);
931 int eesk
= ((val
& EEPROM_SK
) != 0);
932 int eedi
= ((val
& EEPROM_DI
) != 0);
933 eeprom93xx_write(eeprom
, eecs
, eesk
, eedi
);
936 static void eepro100_write_pointer(EEPRO100State
* s
, uint32_t val
)
938 s
->pointer
= le32_to_cpu(val
);
939 TRACE(OTHER
, logout("val=0x%08x\n", val
));
942 /*****************************************************************************
946 ****************************************************************************/
948 #if defined(DEBUG_EEPRO100)
949 static const char * const mdi_op_name
[] = {
956 static const char * const mdi_reg_name
[] = {
959 "PHY Identification (Word 1)",
960 "PHY Identification (Word 2)",
961 "Auto-Negotiation Advertisement",
962 "Auto-Negotiation Link Partner Ability",
963 "Auto-Negotiation Expansion"
966 static const char *reg2name(uint8_t reg
)
968 static char buffer
[10];
969 const char *p
= buffer
;
970 if (reg
< ARRAY_SIZE(mdi_reg_name
)) {
971 p
= mdi_reg_name
[reg
];
973 snprintf(buffer
, sizeof(buffer
), "reg=0x%02x", reg
);
977 #endif /* DEBUG_EEPRO100 */
979 static uint32_t eepro100_read_mdi(EEPRO100State
* s
)
982 memcpy(&val
, &s
->mem
[0x10], sizeof(val
));
984 #ifdef DEBUG_EEPRO100
985 uint8_t raiseint
= (val
& BIT(29)) >> 29;
986 uint8_t opcode
= (val
& BITS(27, 26)) >> 26;
987 uint8_t phy
= (val
& BITS(25, 21)) >> 21;
988 uint8_t reg
= (val
& BITS(20, 16)) >> 16;
989 uint16_t data
= (val
& BITS(15, 0));
991 /* Emulation takes no time to finish MDI transaction. */
993 TRACE(MDI
, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
994 val
, raiseint
, mdi_op_name
[opcode
], phy
,
995 reg2name(reg
), data
));
999 static void eepro100_write_mdi(EEPRO100State
* s
, uint32_t val
)
1001 uint8_t raiseint
= (val
& BIT(29)) >> 29;
1002 uint8_t opcode
= (val
& BITS(27, 26)) >> 26;
1003 uint8_t phy
= (val
& BITS(25, 21)) >> 21;
1004 uint8_t reg
= (val
& BITS(20, 16)) >> 16;
1005 uint16_t data
= (val
& BITS(15, 0));
1006 TRACE(MDI
, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1007 val
, raiseint
, mdi_op_name
[opcode
], phy
, reg2name(reg
), data
));
1009 /* Unsupported PHY address. */
1010 //~ logout("phy must be 1 but is %u\n", phy);
1012 } else if (opcode
!= 1 && opcode
!= 2) {
1013 /* Unsupported opcode. */
1014 logout("opcode must be 1 or 2 but is %u\n", opcode
);
1016 } else if (reg
> 6) {
1017 /* Unsupported register. */
1018 logout("register must be 0...6 but is %u\n", reg
);
1021 TRACE(MDI
, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1022 val
, raiseint
, mdi_op_name
[opcode
], phy
,
1023 reg2name(reg
), data
));
1027 case 0: /* Control Register */
1028 if (data
& 0x8000) {
1029 /* Reset status and control registers to default. */
1030 s
->mdimem
[0] = eepro100_mdi_default
[0];
1031 s
->mdimem
[1] = eepro100_mdi_default
[1];
1032 data
= s
->mdimem
[reg
];
1034 /* Restart Auto Configuration = Normal Operation */
1038 case 1: /* Status Register */
1039 missing("not writable");
1040 data
= s
->mdimem
[reg
];
1042 case 2: /* PHY Identification Register (Word 1) */
1043 case 3: /* PHY Identification Register (Word 2) */
1044 missing("not implemented");
1046 case 4: /* Auto-Negotiation Advertisement Register */
1047 case 5: /* Auto-Negotiation Link Partner Ability Register */
1049 case 6: /* Auto-Negotiation Expansion Register */
1051 missing("not implemented");
1053 s
->mdimem
[reg
] = data
;
1054 } else if (opcode
== 2) {
1057 case 0: /* Control Register */
1058 if (data
& 0x8000) {
1059 /* Reset status and control registers to default. */
1060 s
->mdimem
[0] = eepro100_mdi_default
[0];
1061 s
->mdimem
[1] = eepro100_mdi_default
[1];
1064 case 1: /* Status Register */
1065 s
->mdimem
[reg
] |= 0x0020;
1067 case 2: /* PHY Identification Register (Word 1) */
1068 case 3: /* PHY Identification Register (Word 2) */
1069 case 4: /* Auto-Negotiation Advertisement Register */
1071 case 5: /* Auto-Negotiation Link Partner Ability Register */
1072 s
->mdimem
[reg
] = 0x41fe;
1074 case 6: /* Auto-Negotiation Expansion Register */
1075 s
->mdimem
[reg
] = 0x0001;
1078 data
= s
->mdimem
[reg
];
1080 /* Emulation takes no time to finish MDI transaction.
1081 * Set MDI bit in SCB status register. */
1082 s
->mem
[SCBAck
] |= 0x08;
1085 eepro100_mdi_interrupt(s
);
1088 val
= (val
& 0xffff0000) + data
;
1089 memcpy(&s
->mem
[0x10], &val
, sizeof(val
));
1092 /*****************************************************************************
1096 ****************************************************************************/
1098 #define PORT_SOFTWARE_RESET 0
1099 #define PORT_SELFTEST 1
1100 #define PORT_SELECTIVE_RESET 2
1102 #define PORT_SELECTION_MASK 3
1105 uint32_t st_sign
; /* Self Test Signature */
1106 uint32_t st_result
; /* Self Test Results */
1107 } eepro100_selftest_t
;
1109 static uint32_t eepro100_read_port(EEPRO100State
* s
)
1114 static void eepro100_write_port(EEPRO100State
* s
, uint32_t val
)
1116 val
= le32_to_cpu(val
);
1117 uint32_t address
= (val
& ~PORT_SELECTION_MASK
);
1118 uint8_t selection
= (val
& PORT_SELECTION_MASK
);
1119 switch (selection
) {
1120 case PORT_SOFTWARE_RESET
:
1124 TRACE(OTHER
, logout("selftest address=0x%08x\n", address
));
1125 eepro100_selftest_t data
;
1126 cpu_physical_memory_read(address
, (uint8_t *) & data
, sizeof(data
));
1127 data
.st_sign
= 0xffffffff;
1129 cpu_physical_memory_write(address
, (uint8_t *) & data
, sizeof(data
));
1131 case PORT_SELECTIVE_RESET
:
1132 TRACE(OTHER
, logout("selective reset, selftest address=0x%08x\n", address
));
1133 nic_selective_reset(s
);
1136 logout("val=0x%08x\n", val
);
1137 missing("unknown port selection");
1141 /*****************************************************************************
1143 * General hardware emulation.
1145 ****************************************************************************/
1147 static uint8_t eepro100_read1(EEPRO100State
* s
, uint32_t addr
)
1150 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1151 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1156 //~ val = eepro100_read_status(s);
1157 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1160 //~ val = eepro100_read_status(s);
1161 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1164 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1165 //~ val = eepro100_read_command(s);
1168 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1171 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1174 val
= eepro100_read_eeprom(s
);
1176 case 0x1b: /* PMDR (power management driver register) */
1178 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1180 case 0x1d: /* general status register */
1181 /* 100 Mbps full duplex, valid link */
1183 TRACE(OTHER
, logout("addr=General Status val=%02x\n", val
));
1186 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1187 missing("unknown byte read");
1192 static uint16_t eepro100_read2(EEPRO100State
* s
, uint32_t addr
)
1195 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1196 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1201 //~ val = eepro100_read_status(s);
1202 TRACE(OTHER
, logout("addr=%s val=0x%04x\n", regname(addr
), val
));
1205 val
= eepro100_read_eeprom(s
);
1206 TRACE(OTHER
, logout("addr=%s val=0x%04x\n", regname(addr
), val
));
1209 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1210 missing("unknown word read");
1215 static uint32_t eepro100_read4(EEPRO100State
* s
, uint32_t addr
)
1218 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1219 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1224 //~ val = eepro100_read_status(s);
1225 TRACE(OTHER
, logout("addr=%s val=0x%08x\n", regname(addr
), val
));
1228 //~ val = eepro100_read_pointer(s);
1229 TRACE(OTHER
, logout("addr=%s val=0x%08x\n", regname(addr
), val
));
1232 val
= eepro100_read_port(s
);
1233 TRACE(OTHER
, logout("addr=%s val=0x%08x\n", regname(addr
), val
));
1236 val
= eepro100_read_mdi(s
);
1239 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1240 missing("unknown longword read");
1245 static void eepro100_write1(EEPRO100State
* s
, uint32_t addr
, uint8_t val
)
1247 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1248 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1251 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1255 //~ eepro100_write_status(s, val);
1258 eepro100_acknowledge(s
);
1261 eepro100_write_command(s
, val
);
1265 eepro100_swi_interrupt(s
);
1267 eepro100_interrupt(s
, 0);
1270 case SCBFlow
: /* does not exist on 82557 */
1274 TRACE(OTHER
, logout("addr=%s val=0x%02x\n", regname(addr
), val
));
1277 eepro100_write_eeprom(s
->eeprom
, val
);
1280 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1281 missing("unknown byte write");
1285 static void eepro100_write2(EEPRO100State
* s
, uint32_t addr
, uint16_t val
)
1287 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1288 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1291 TRACE(OTHER
, logout("addr=%s val=0x%04x\n", regname(addr
), val
));
1295 //~ eepro100_write_status(s, val);
1296 eepro100_acknowledge(s
);
1299 eepro100_write_command(s
, val
);
1300 eepro100_write1(s
, SCBIntmask
, val
>> 8);
1303 eepro100_write_eeprom(s
->eeprom
, val
);
1306 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1307 missing("unknown word write");
1311 static void eepro100_write4(EEPRO100State
* s
, uint32_t addr
, uint32_t val
)
1313 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1314 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1319 eepro100_write_pointer(s
, val
);
1322 TRACE(OTHER
, logout("addr=%s val=0x%08x\n", regname(addr
), val
));
1323 eepro100_write_port(s
, val
);
1326 eepro100_write_mdi(s
, val
);
1329 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1330 missing("unknown longword write");
1334 /*****************************************************************************
1338 ****************************************************************************/
1340 static uint32_t ioport_read1(void *opaque
, uint32_t addr
)
1342 EEPRO100State
*s
= opaque
;
1343 //~ logout("addr=%s\n", regname(addr));
1344 return eepro100_read1(s
, addr
- s
->region
[1]);
1347 static uint32_t ioport_read2(void *opaque
, uint32_t addr
)
1349 EEPRO100State
*s
= opaque
;
1350 return eepro100_read2(s
, addr
- s
->region
[1]);
1353 static uint32_t ioport_read4(void *opaque
, uint32_t addr
)
1355 EEPRO100State
*s
= opaque
;
1356 return eepro100_read4(s
, addr
- s
->region
[1]);
1359 static void ioport_write1(void *opaque
, uint32_t addr
, uint32_t val
)
1361 EEPRO100State
*s
= opaque
;
1362 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1363 eepro100_write1(s
, addr
- s
->region
[1], val
);
1366 static void ioport_write2(void *opaque
, uint32_t addr
, uint32_t val
)
1368 EEPRO100State
*s
= opaque
;
1369 eepro100_write2(s
, addr
- s
->region
[1], val
);
1372 static void ioport_write4(void *opaque
, uint32_t addr
, uint32_t val
)
1374 EEPRO100State
*s
= opaque
;
1375 eepro100_write4(s
, addr
- s
->region
[1], val
);
1378 /***********************************************************/
1379 /* PCI EEPRO100 definitions */
1381 static void pci_map(PCIDevice
* pci_dev
, int region_num
,
1382 uint32_t addr
, uint32_t size
, int type
)
1384 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1386 TRACE(OTHER
, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1387 region_num
, addr
, size
, type
));
1389 assert(region_num
== 1);
1390 register_ioport_write(addr
, size
, 1, ioport_write1
, s
);
1391 register_ioport_read(addr
, size
, 1, ioport_read1
, s
);
1392 register_ioport_write(addr
, size
, 2, ioport_write2
, s
);
1393 register_ioport_read(addr
, size
, 2, ioport_read2
, s
);
1394 register_ioport_write(addr
, size
, 4, ioport_write4
, s
);
1395 register_ioport_read(addr
, size
, 4, ioport_read4
, s
);
1397 s
->region
[region_num
] = addr
;
1400 /*****************************************************************************
1402 * Memory mapped I/O.
1404 ****************************************************************************/
1406 static void pci_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1408 EEPRO100State
*s
= opaque
;
1409 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1410 eepro100_write1(s
, addr
, val
);
1413 static void pci_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1415 EEPRO100State
*s
= opaque
;
1416 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1417 eepro100_write2(s
, addr
, val
);
1420 static void pci_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1422 EEPRO100State
*s
= opaque
;
1423 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1424 eepro100_write4(s
, addr
, val
);
1427 static uint32_t pci_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1429 EEPRO100State
*s
= opaque
;
1430 //~ logout("addr=%s\n", regname(addr));
1431 return eepro100_read1(s
, addr
);
1434 static uint32_t pci_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1436 EEPRO100State
*s
= opaque
;
1437 //~ logout("addr=%s\n", regname(addr));
1438 return eepro100_read2(s
, addr
);
1441 static uint32_t pci_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1443 EEPRO100State
*s
= opaque
;
1444 //~ logout("addr=%s\n", regname(addr));
1445 return eepro100_read4(s
, addr
);
1448 static CPUWriteMemoryFunc
* const pci_mmio_write
[] = {
1454 static CPUReadMemoryFunc
* const pci_mmio_read
[] = {
1460 static void pci_mmio_map(PCIDevice
* pci_dev
, int region_num
,
1461 uint32_t addr
, uint32_t size
, int type
)
1463 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1465 TRACE(OTHER
, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1466 region_num
, addr
, size
, type
));
1468 if (region_num
== 0) {
1469 /* Map control / status registers. */
1470 cpu_register_physical_memory(addr
, size
, s
->mmio_index
);
1471 s
->region
[region_num
] = addr
;
1475 static int nic_can_receive(VLANClientState
*vc
)
1477 EEPRO100State
*s
= vc
->opaque
;
1478 TRACE(RXTX
, logout("%p\n", s
));
1479 return get_ru_state(s
) == ru_ready
;
1480 //~ return !eepro100_buffer_full(s);
1483 static ssize_t
nic_receive(VLANClientState
*vc
, const uint8_t * buf
, size_t size
)
1486 * - Magic packets should set bit 30 in power management driver register.
1487 * - Interesting packets should set bit 29 in power management driver register.
1489 EEPRO100State
*s
= vc
->opaque
;
1490 uint16_t rfd_status
= 0xa000;
1491 static const uint8_t broadcast_macaddr
[6] =
1492 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1494 /* TODO: check multiple IA bit. */
1495 assert(!(s
->configuration
[20] & BIT(6)));
1497 if (s
->configuration
[8] & 0x80) {
1498 /* CSMA is disabled. */
1499 logout("%p received while CSMA is disabled\n", s
);
1501 } else if (size
< 64 && (s
->configuration
[7] & 1)) {
1502 /* Short frame and configuration byte 7/0 (discard short receive) set:
1503 * Short frame is discarded */
1504 logout("%p received short frame (%zu byte)\n", s
, size
);
1505 s
->statistics
.rx_short_frame_errors
++;
1507 } else if ((size
> MAX_ETH_FRAME_SIZE
+ 4) && !(s
->configuration
[18] & 8)) {
1508 /* Long frame and configuration byte 18/3 (long receive ok) not set:
1509 * Long frames are discarded. */
1510 logout("%p received long frame (%zu byte), ignored\n", s
, size
);
1512 } else if (memcmp(buf
, s
->macaddr
, 6) == 0) { // !!!
1513 /* Frame matches individual address. */
1514 /* TODO: check configuration byte 15/4 (ignore U/L). */
1515 TRACE(RXTX
, logout("%p received frame for me, len=%zu\n", s
, size
));
1516 } else if (memcmp(buf
, broadcast_macaddr
, 6) == 0) {
1517 /* Broadcast frame. */
1518 TRACE(RXTX
, logout("%p received broadcast, len=%zu\n", s
, size
));
1519 rfd_status
|= 0x0002;
1520 } else if (buf
[0] & 0x01) { // !!!
1521 /* Multicast frame. */
1522 TRACE(RXTX
, logout("%p received multicast, len=%zu\n", s
, size
));
1523 /* TODO: check multicast all bit. */
1524 assert(!(s
->configuration
[21] & BIT(3)));
1525 int mcast_idx
= compute_mcast_idx(buf
);
1526 if (!(s
->mult
[mcast_idx
>> 3] & (1 << (mcast_idx
& 7)))) {
1529 rfd_status
|= 0x0002;
1530 } else if (s
->configuration
[15] & 1) {
1531 /* Promiscuous: receive all. */
1532 TRACE(RXTX
, logout("%p received frame in promiscuous mode, len=%zu\n", s
, size
));
1533 rfd_status
|= 0x0004;
1535 TRACE(RXTX
, logout("%p received frame, ignored, len=%zu,%s\n", s
, size
,
1536 nic_dump(buf
, size
)));
1540 if (get_ru_state(s
) != ru_ready
) {
1541 /* No resources available. */
1542 logout("no resources, state=%u\n", get_ru_state(s
));
1543 s
->statistics
.rx_resource_errors
++;
1544 //~ assert(!"no resources");
1548 //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1550 cpu_physical_memory_read(s
->ru_base
+ s
->ru_offset
, (uint8_t *) & rx
,
1551 offsetof(eepro100_rx_t
, packet
));
1552 uint16_t rfd_command
= le16_to_cpu(rx
.command
);
1553 uint16_t rfd_size
= le16_to_cpu(rx
.size
);
1554 assert(size
<= rfd_size
);
1556 rfd_status
|= 0x0080;
1558 TRACE(OTHER
, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1559 rfd_command
, rx
.link
, rx
.rx_buf_addr
, rfd_size
));
1560 stw_phys(s
->ru_base
+ s
->ru_offset
+ offsetof(eepro100_rx_t
, status
),
1562 stw_phys(s
->ru_base
+ s
->ru_offset
+ offsetof(eepro100_rx_t
, count
), size
);
1563 /* Early receive interrupt not supported. */
1564 //~ eepro100_er_interrupt(s);
1565 /* Receive CRC Transfer not supported. */
1566 assert(!(s
->configuration
[18] & 4));
1567 /* TODO: check stripping enable bit. */
1568 //~ assert(!(s->configuration[17] & 1));
1569 cpu_physical_memory_write(s
->ru_base
+ s
->ru_offset
+
1570 offsetof(eepro100_rx_t
, packet
), buf
, size
);
1571 s
->statistics
.rx_good_frames
++;
1572 eepro100_fr_interrupt(s
);
1573 s
->ru_offset
= le32_to_cpu(rx
.link
);
1574 if (rfd_command
& 0x8000) {
1575 /* EL bit is set, so this was the last frame. */
1578 if (rfd_command
& 0x4000) {
1580 set_ru_state(s
, ru_suspended
);
1585 static int nic_load(QEMUFile
* f
, void *opaque
, int version_id
)
1587 EEPRO100State
*s
= opaque
;
1594 if (version_id
>= 3) {
1595 ret
= pci_device_load(&s
->dev
, f
);
1600 if (version_id
>= 2) {
1601 qemu_get_8s(f
, &s
->rxcr
);
1606 qemu_get_8s(f
, &s
->cmd
);
1607 qemu_get_be32s(f
, &s
->start
);
1608 qemu_get_be32s(f
, &s
->stop
);
1609 qemu_get_8s(f
, &s
->boundary
);
1610 qemu_get_8s(f
, &s
->tsr
);
1611 qemu_get_8s(f
, &s
->tpsr
);
1612 qemu_get_be16s(f
, &s
->tcnt
);
1613 qemu_get_be16s(f
, &s
->rcnt
);
1614 qemu_get_be32s(f
, &s
->rsar
);
1615 qemu_get_8s(f
, &s
->rsr
);
1616 qemu_get_8s(f
, &s
->isr
);
1617 qemu_get_8s(f
, &s
->dcfg
);
1618 qemu_get_8s(f
, &s
->imr
);
1619 qemu_get_buffer(f
, s
->phys
, 6);
1620 qemu_get_8s(f
, &s
->curpag
);
1621 qemu_get_buffer(f
, s
->mult
, 8);
1622 qemu_get_buffer(f
, s
->mem
, sizeof(s
->mem
));
1624 /* Restore all members of struct between scv_stat and mem. */
1625 qemu_get_8s(f
, &s
->scb_stat
);
1626 qemu_get_8s(f
, &s
->int_stat
);
1627 for (i
= 0; i
< 3; i
++) {
1628 qemu_get_be32s(f
, &s
->region
[i
]);
1630 qemu_get_buffer(f
, s
->macaddr
, 6);
1631 for (i
= 0; i
< 19; i
++) {
1632 qemu_get_be32s(f
, &s
->statcounter
[i
]);
1634 for (i
= 0; i
< 32; i
++) {
1635 qemu_get_be16s(f
, &s
->mdimem
[i
]);
1637 /* The eeprom should be saved and restored by its own routines. */
1638 qemu_get_be32s(f
, &s
->device
);
1639 qemu_get_be32s(f
, &s
->pointer
);
1640 qemu_get_be32s(f
, &s
->cu_base
);
1641 qemu_get_be32s(f
, &s
->cu_offset
);
1642 qemu_get_be32s(f
, &s
->ru_base
);
1643 qemu_get_be32s(f
, &s
->ru_offset
);
1644 qemu_get_be32s(f
, &s
->statsaddr
);
1645 /* Restore epro100_stats_t statistics. */
1646 qemu_get_be32s(f
, &s
->statistics
.tx_good_frames
);
1647 qemu_get_be32s(f
, &s
->statistics
.tx_max_collisions
);
1648 qemu_get_be32s(f
, &s
->statistics
.tx_late_collisions
);
1649 qemu_get_be32s(f
, &s
->statistics
.tx_underruns
);
1650 qemu_get_be32s(f
, &s
->statistics
.tx_lost_crs
);
1651 qemu_get_be32s(f
, &s
->statistics
.tx_deferred
);
1652 qemu_get_be32s(f
, &s
->statistics
.tx_single_collisions
);
1653 qemu_get_be32s(f
, &s
->statistics
.tx_multiple_collisions
);
1654 qemu_get_be32s(f
, &s
->statistics
.tx_total_collisions
);
1655 qemu_get_be32s(f
, &s
->statistics
.rx_good_frames
);
1656 qemu_get_be32s(f
, &s
->statistics
.rx_crc_errors
);
1657 qemu_get_be32s(f
, &s
->statistics
.rx_alignment_errors
);
1658 qemu_get_be32s(f
, &s
->statistics
.rx_resource_errors
);
1659 qemu_get_be32s(f
, &s
->statistics
.rx_overrun_errors
);
1660 qemu_get_be32s(f
, &s
->statistics
.rx_cdt_errors
);
1661 qemu_get_be32s(f
, &s
->statistics
.rx_short_frame_errors
);
1662 qemu_get_be32s(f
, &s
->statistics
.fc_xmt_pause
);
1663 qemu_get_be32s(f
, &s
->statistics
.fc_rcv_pause
);
1664 qemu_get_be32s(f
, &s
->statistics
.fc_rcv_unsupported
);
1665 qemu_get_be16s(f
, &s
->statistics
.xmt_tco_frames
);
1666 qemu_get_be16s(f
, &s
->statistics
.rcv_tco_frames
);
1667 qemu_get_be32s(f
, &s
->statistics
.complete
);
1669 qemu_get_be16s(f
, &s
->status
);
1672 /* Configuration bytes. */
1673 qemu_get_buffer(f
, s
->configuration
, sizeof(s
->configuration
));
1678 static void nic_save(QEMUFile
* f
, void *opaque
)
1680 EEPRO100State
*s
= opaque
;
1683 pci_device_save(&s
->dev
, f
);
1685 qemu_put_8s(f
, &s
->rxcr
);
1687 qemu_put_8s(f
, &s
->cmd
);
1688 qemu_put_be32s(f
, &s
->start
);
1689 qemu_put_be32s(f
, &s
->stop
);
1690 qemu_put_8s(f
, &s
->boundary
);
1691 qemu_put_8s(f
, &s
->tsr
);
1692 qemu_put_8s(f
, &s
->tpsr
);
1693 qemu_put_be16s(f
, &s
->tcnt
);
1694 qemu_put_be16s(f
, &s
->rcnt
);
1695 qemu_put_be32s(f
, &s
->rsar
);
1696 qemu_put_8s(f
, &s
->rsr
);
1697 qemu_put_8s(f
, &s
->isr
);
1698 qemu_put_8s(f
, &s
->dcfg
);
1699 qemu_put_8s(f
, &s
->imr
);
1700 qemu_put_buffer(f
, s
->phys
, 6);
1701 qemu_put_8s(f
, &s
->curpag
);
1702 qemu_put_buffer(f
, s
->mult
, 8);
1703 qemu_put_buffer(f
, s
->mem
, sizeof(s
->mem
));
1705 /* Save all members of struct between scv_stat and mem. */
1706 qemu_put_8s(f
, &s
->scb_stat
);
1707 qemu_put_8s(f
, &s
->int_stat
);
1708 for (i
= 0; i
< 3; i
++) {
1709 qemu_put_be32s(f
, &s
->region
[i
]);
1711 qemu_put_buffer(f
, s
->macaddr
, 6);
1712 for (i
= 0; i
< 19; i
++) {
1713 qemu_put_be32s(f
, &s
->statcounter
[i
]);
1715 for (i
= 0; i
< 32; i
++) {
1716 qemu_put_be16s(f
, &s
->mdimem
[i
]);
1718 /* The eeprom should be saved and restored by its own routines. */
1719 qemu_put_be32s(f
, &s
->device
);
1720 qemu_put_be32s(f
, &s
->pointer
);
1721 qemu_put_be32s(f
, &s
->cu_base
);
1722 qemu_put_be32s(f
, &s
->cu_offset
);
1723 qemu_put_be32s(f
, &s
->ru_base
);
1724 qemu_put_be32s(f
, &s
->ru_offset
);
1725 qemu_put_be32s(f
, &s
->statsaddr
);
1726 /* Save epro100_stats_t statistics. */
1727 qemu_put_be32s(f
, &s
->statistics
.tx_good_frames
);
1728 qemu_put_be32s(f
, &s
->statistics
.tx_max_collisions
);
1729 qemu_put_be32s(f
, &s
->statistics
.tx_late_collisions
);
1730 qemu_put_be32s(f
, &s
->statistics
.tx_underruns
);
1731 qemu_put_be32s(f
, &s
->statistics
.tx_lost_crs
);
1732 qemu_put_be32s(f
, &s
->statistics
.tx_deferred
);
1733 qemu_put_be32s(f
, &s
->statistics
.tx_single_collisions
);
1734 qemu_put_be32s(f
, &s
->statistics
.tx_multiple_collisions
);
1735 qemu_put_be32s(f
, &s
->statistics
.tx_total_collisions
);
1736 qemu_put_be32s(f
, &s
->statistics
.rx_good_frames
);
1737 qemu_put_be32s(f
, &s
->statistics
.rx_crc_errors
);
1738 qemu_put_be32s(f
, &s
->statistics
.rx_alignment_errors
);
1739 qemu_put_be32s(f
, &s
->statistics
.rx_resource_errors
);
1740 qemu_put_be32s(f
, &s
->statistics
.rx_overrun_errors
);
1741 qemu_put_be32s(f
, &s
->statistics
.rx_cdt_errors
);
1742 qemu_put_be32s(f
, &s
->statistics
.rx_short_frame_errors
);
1743 qemu_put_be32s(f
, &s
->statistics
.fc_xmt_pause
);
1744 qemu_put_be32s(f
, &s
->statistics
.fc_rcv_pause
);
1745 qemu_put_be32s(f
, &s
->statistics
.fc_rcv_unsupported
);
1746 qemu_put_be16s(f
, &s
->statistics
.xmt_tco_frames
);
1747 qemu_put_be16s(f
, &s
->statistics
.rcv_tco_frames
);
1748 qemu_put_be32s(f
, &s
->statistics
.complete
);
1750 qemu_put_be16s(f
, &s
->status
);
1753 /* Configuration bytes. */
1754 qemu_put_buffer(f
, s
->configuration
, sizeof(s
->configuration
));
1757 static void nic_cleanup(VLANClientState
*vc
)
1759 EEPRO100State
*s
= vc
->opaque
;
1761 unregister_savevm(vc
->model
, s
);
1763 eeprom93xx_free(s
->eeprom
);
1766 static int pci_nic_uninit(PCIDevice
*pci_dev
)
1768 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1770 cpu_unregister_io_memory(s
->mmio_index
);
1775 static int nic_init(PCIDevice
*pci_dev
, uint32_t device
)
1777 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1779 TRACE(OTHER
, logout("\n"));
1785 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1786 * i82559 and later support 64 or 256 word EEPROM. */
1787 s
->eeprom
= eeprom93xx_new(EEPROM_SIZE
);
1789 /* Handler for memory-mapped I/O */
1791 cpu_register_io_memory(pci_mmio_read
, pci_mmio_write
, s
);
1793 pci_register_bar(&s
->dev
, 0, PCI_MEM_SIZE
,
1794 PCI_ADDRESS_SPACE_MEM
|
1795 PCI_ADDRESS_SPACE_MEM_PREFETCH
, pci_mmio_map
);
1796 pci_register_bar(&s
->dev
, 1, PCI_IO_SIZE
, PCI_ADDRESS_SPACE_IO
,
1798 pci_register_bar(&s
->dev
, 2, PCI_FLASH_SIZE
, PCI_ADDRESS_SPACE_MEM
,
1801 qdev_get_macaddr(&s
->dev
.qdev
, s
->macaddr
);
1802 logout("macaddr: %s\n", nic_dump(&s
->macaddr
[0], 6));
1803 assert(s
->region
[1] == 0);
1807 s
->vc
= qdev_get_vlan_client(&s
->dev
.qdev
,
1808 nic_can_receive
, nic_receive
, NULL
,
1811 qemu_format_nic_info_str(s
->vc
, s
->macaddr
);
1812 TRACE(OTHER
, logout("%s\n", s
->vc
->info_str
));
1814 qemu_register_reset(nic_reset
, s
);
1816 register_savevm(s
->vc
->model
, -1, 3, nic_save
, nic_load
, s
);
1820 static int pci_i82550_init(PCIDevice
*pci_dev
)
1822 return nic_init(pci_dev
, i82550
);
1825 static int pci_i82551_init(PCIDevice
*pci_dev
)
1827 return nic_init(pci_dev
, i82551
);
1830 static int pci_i82557a_init(PCIDevice
*pci_dev
)
1832 return nic_init(pci_dev
, i82557A
);
1835 static int pci_i82557b_init(PCIDevice
*pci_dev
)
1837 return nic_init(pci_dev
, i82557B
);
1840 static int pci_i82557c_init(PCIDevice
*pci_dev
)
1842 return nic_init(pci_dev
, i82557C
);
1845 static int pci_i82558a_init(PCIDevice
*pci_dev
)
1847 return nic_init(pci_dev
, i82558A
);
1850 static int pci_i82558b_init(PCIDevice
*pci_dev
)
1852 return nic_init(pci_dev
, i82558B
);
1855 static int pci_i82559a_init(PCIDevice
*pci_dev
)
1857 return nic_init(pci_dev
, i82559A
);
1860 static int pci_i82559b_init(PCIDevice
*pci_dev
)
1862 return nic_init(pci_dev
, i82559B
);
1865 static int pci_i82559c_init(PCIDevice
*pci_dev
)
1867 return nic_init(pci_dev
, i82559C
);
1870 static int pci_i82559er_init(PCIDevice
*pci_dev
)
1872 return nic_init(pci_dev
, i82559ER
);
1875 static int pci_i82562_init(PCIDevice
*pci_dev
)
1877 return nic_init(pci_dev
, i82562
);
1880 static PCIDeviceInfo eepro100_info
[] = {
1882 .qdev
.name
= "i82550",
1883 .qdev
.size
= sizeof(EEPRO100State
),
1884 .init
= pci_i82550_init
,
1886 .qdev
.name
= "i82551",
1887 .qdev
.size
= sizeof(EEPRO100State
),
1888 .init
= pci_i82551_init
,
1889 .exit
= pci_nic_uninit
,
1891 .qdev
.name
= "i82557a",
1892 .qdev
.size
= sizeof(EEPRO100State
),
1893 .init
= pci_i82557a_init
,
1895 .qdev
.name
= "i82557b",
1896 .qdev
.size
= sizeof(EEPRO100State
),
1897 .init
= pci_i82557b_init
,
1898 .exit
= pci_nic_uninit
,
1900 .qdev
.name
= "i82557c",
1901 .qdev
.size
= sizeof(EEPRO100State
),
1902 .init
= pci_i82557c_init
,
1904 .qdev
.name
= "i82558a",
1905 .qdev
.size
= sizeof(EEPRO100State
),
1906 .init
= pci_i82558a_init
,
1908 .qdev
.name
= "i82558b",
1909 .qdev
.size
= sizeof(EEPRO100State
),
1910 .init
= pci_i82558b_init
,
1912 .qdev
.name
= "i82559a",
1913 .qdev
.size
= sizeof(EEPRO100State
),
1914 .init
= pci_i82559a_init
,
1916 .qdev
.name
= "i82559b",
1917 .qdev
.size
= sizeof(EEPRO100State
),
1918 .init
= pci_i82559b_init
,
1920 .qdev
.name
= "i82559c",
1921 .qdev
.size
= sizeof(EEPRO100State
),
1922 .init
= pci_i82559c_init
,
1924 .qdev
.name
= "i82559er",
1925 .qdev
.size
= sizeof(EEPRO100State
),
1926 .init
= pci_i82559er_init
,
1927 .exit
= pci_nic_uninit
,
1929 .qdev
.name
= "i82562",
1930 .qdev
.size
= sizeof(EEPRO100State
),
1931 .init
= pci_i82562_init
,
1937 static void eepro100_register_devices(void)
1939 pci_qdev_register_many(eepro100_info
);
1942 device_init(eepro100_register_devices
)