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 */
44 #include "eeprom93xx.h"
46 /* Common declarations for all PCI devices. */
48 #define PCI_CONFIG_8(offset, value) \
49 (pci_conf[offset] = (value))
50 #define PCI_CONFIG_16(offset, value) \
51 (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
52 #define PCI_CONFIG_32(offset, value) \
53 (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
57 /* debug EEPRO100 card */
58 //~ #define DEBUG_EEPRO100
61 #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
63 #define logout(fmt, ...) ((void)0)
66 /* Set flags to 0 to disable debug output. */
69 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
71 #define missing(text) assert(!"feature is missing in this emulation: " text)
73 #define MAX_ETH_FRAME_SIZE 1514
75 /* This driver supports several different devices which are declared here. */
76 #define i82551 0x82551
77 #define i82557B 0x82557b
78 #define i82557C 0x82557c
79 #define i82558B 0x82558b
80 #define i82559C 0x82559c
81 #define i82559ER 0x82559e
82 #define i82562 0x82562
84 #define EEPROM_SIZE 64
86 #define PCI_MEM_SIZE (4 * KiB)
87 #define PCI_IO_SIZE 64
88 #define PCI_FLASH_SIZE (128 * KiB)
90 #define BIT(n) (1 << (n))
91 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
93 /* The SCB accepts the following controls for the Tx and Rx units: */
94 #define CU_NOP 0x0000 /* No operation. */
95 #define CU_START 0x0010 /* CU start. */
96 #define CU_RESUME 0x0020 /* CU resume. */
97 #define CU_STATSADDR 0x0040 /* Load dump counters address. */
98 #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
99 #define CU_CMD_BASE 0x0060 /* Load CU base address. */
100 #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
101 #define CU_SRESUME 0x00a0 /* CU static resume. */
103 #define RU_NOP 0x0000
104 #define RX_START 0x0001
105 #define RX_RESUME 0x0002
106 #define RX_ABORT 0x0004
107 #define RX_ADDR_LOAD 0x0006
108 #define RX_RESUMENR 0x0007
109 #define INT_MASK 0x0100
110 #define DRVR_INT 0x0200 /* Driver generated interrupt. */
112 typedef unsigned char bool;
114 /* Offsets to the various registers.
115 All accesses need not be longword aligned. */
116 enum speedo_offsets
{
119 SCBCmd
= 2, /* Rx/Command Unit command and status. */
121 SCBPointer
= 4, /* General purpose pointer. */
122 SCBPort
= 8, /* Misc. commands and operands. */
123 SCBflash
= 12, SCBeeprom
= 14, /* EEPROM and flash memory control. */
124 SCBCtrlMDI
= 16, /* MDI interface control. */
125 SCBEarlyRx
= 20, /* Early receive byte count. */
129 /* A speedo3 transmit buffer descriptor with two buffers... */
133 uint32_t link
; /* void * */
134 uint32_t tx_desc_addr
; /* transmit buffer decsriptor array address. */
135 uint16_t tcb_bytes
; /* transmit command block byte count (in lower 14 bits */
136 uint8_t tx_threshold
; /* transmit threshold */
137 uint8_t tbd_count
; /* TBD number */
138 //~ /* This constitutes two "TBD" entries: hdr and data */
139 //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
140 //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
141 //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
142 //~ int32_t tx_buf_size1; /* Length of Tx data. */
145 /* Receive frame descriptor. */
149 uint32_t link
; /* struct RxFD * */
150 uint32_t rx_buf_addr
; /* void * */
153 char packet
[MAX_ETH_FRAME_SIZE
+ 4];
157 uint32_t tx_good_frames
, tx_max_collisions
, tx_late_collisions
,
158 tx_underruns
, tx_lost_crs
, tx_deferred
, tx_single_collisions
,
159 tx_multiple_collisions
, tx_total_collisions
;
160 uint32_t rx_good_frames
, rx_crc_errors
, rx_alignment_errors
,
161 rx_resource_errors
, rx_overrun_errors
, rx_cdt_errors
,
162 rx_short_frame_errors
;
163 uint32_t fc_xmt_pause
, fc_rcv_pause
, fc_rcv_unsupported
;
164 uint16_t xmt_tco_frames
, rcv_tco_frames
;
200 uint8_t phys
[6]; /* mac address */
202 uint8_t mult
[8]; /* multicast mask array */
206 uint8_t scb_stat
; /* SCB stat/ack byte */
207 uint8_t int_stat
; /* PCI interrupt status */
208 uint32_t region
[3]; /* PCI region addresses */
210 uint32_t statcounter
[19];
213 uint32_t device
; /* device variant */
215 /* (cu_base + cu_offset) address the next command block in the command block list. */
216 uint32_t cu_base
; /* CU base address */
217 uint32_t cu_offset
; /* CU address offset */
218 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
219 uint32_t ru_base
; /* RU base address */
220 uint32_t ru_offset
; /* RU address offset */
221 uint32_t statsaddr
; /* pointer to eepro100_stats_t */
222 eepro100_stats_t statistics
; /* statistical counters */
227 /* Configuration bytes. */
228 uint8_t configuration
[22];
230 /* Data in mem is always in the byte order of the controller (le). */
231 uint8_t mem
[PCI_MEM_SIZE
];
234 /* Default values for MDI (PHY) registers */
235 static const uint16_t eepro100_mdi_default
[] = {
236 /* MDI Registers 0 - 6, 7 */
237 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
238 /* MDI Registers 8 - 15 */
239 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
240 /* MDI Registers 16 - 31 */
241 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
242 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
245 /* Readonly mask for MDI (PHY) registers */
246 static const uint16_t eepro100_mdi_mask
[] = {
247 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
248 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
249 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
250 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
253 #define POLYNOMIAL 0x04c11db6
257 static int compute_mcast_idx(const uint8_t * ep
)
264 for (i
= 0; i
< 6; i
++) {
266 for (j
= 0; j
< 8; j
++) {
267 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
271 crc
= ((crc
^ POLYNOMIAL
) | carry
);
277 #if defined(DEBUG_EEPRO100)
278 static const char *nic_dump(const uint8_t * buf
, unsigned size
)
280 static char dump
[3 * 16 + 1];
285 p
+= sprintf(p
, " %02x", *buf
++);
289 #endif /* DEBUG_EEPRO100 */
292 stat_ack_not_ours
= 0x00,
293 stat_ack_sw_gen
= 0x04,
295 stat_ack_cu_idle
= 0x20,
296 stat_ack_frame_rx
= 0x40,
297 stat_ack_cu_cmd_done
= 0x80,
298 stat_ack_not_present
= 0xFF,
299 stat_ack_rx
= (stat_ack_sw_gen
| stat_ack_rnr
| stat_ack_frame_rx
),
300 stat_ack_tx
= (stat_ack_cu_idle
| stat_ack_cu_cmd_done
),
303 static void disable_interrupt(EEPRO100State
* s
)
306 logout("interrupt disabled\n");
307 qemu_irq_lower(s
->dev
.irq
[0]);
312 static void enable_interrupt(EEPRO100State
* s
)
315 logout("interrupt enabled\n");
316 qemu_irq_raise(s
->dev
.irq
[0]);
321 static void eepro100_acknowledge(EEPRO100State
* s
)
323 s
->scb_stat
&= ~s
->mem
[SCBAck
];
324 s
->mem
[SCBAck
] = s
->scb_stat
;
325 if (s
->scb_stat
== 0) {
326 disable_interrupt(s
);
330 static void eepro100_interrupt(EEPRO100State
* s
, uint8_t stat
)
332 uint8_t mask
= ~s
->mem
[SCBIntmask
];
333 s
->mem
[SCBAck
] |= stat
;
334 stat
= s
->scb_stat
= s
->mem
[SCBAck
];
335 stat
&= (mask
| 0x0f);
336 //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
337 if (stat
&& (mask
& 0x01)) {
338 /* SCB mask and SCB Bit M do not disable interrupt. */
340 } else if (s
->int_stat
) {
341 disable_interrupt(s
);
345 static void eepro100_cx_interrupt(EEPRO100State
* s
)
347 /* CU completed action command. */
348 /* Transmit not ok (82557 only, not in emulation). */
349 eepro100_interrupt(s
, 0x80);
352 static void eepro100_cna_interrupt(EEPRO100State
* s
)
354 /* CU left the active state. */
355 eepro100_interrupt(s
, 0x20);
358 static void eepro100_fr_interrupt(EEPRO100State
* s
)
360 /* RU received a complete frame. */
361 eepro100_interrupt(s
, 0x40);
365 static void eepro100_rnr_interrupt(EEPRO100State
* s
)
367 /* RU is not ready. */
368 eepro100_interrupt(s
, 0x10);
372 static void eepro100_mdi_interrupt(EEPRO100State
* s
)
374 /* MDI completed read or write cycle. */
375 eepro100_interrupt(s
, 0x08);
378 static void eepro100_swi_interrupt(EEPRO100State
* s
)
380 /* Software has requested an interrupt. */
381 eepro100_interrupt(s
, 0x04);
385 static void eepro100_fcp_interrupt(EEPRO100State
* s
)
387 /* Flow control pause interrupt (82558 and later). */
388 eepro100_interrupt(s
, 0x01);
392 static void pci_reset(EEPRO100State
* s
)
394 uint32_t device
= s
->device
;
395 uint8_t *pci_conf
= s
->dev
.config
;
400 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
402 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82551IT
);
404 PCI_CONFIG_16(PCI_COMMAND
, 0x0000);
406 PCI_CONFIG_16(PCI_STATUS
, 0x2800);
407 /* PCI Revision ID */
408 PCI_CONFIG_8(PCI_REVISION_ID
, 0x08);
410 PCI_CONFIG_8(0x09, 0x00);
411 pci_config_set_class(pci_conf
, PCI_CLASS_NETWORK_ETHERNET
);
412 /* PCI Cache Line Size */
413 /* check cache line size!!! */
414 //~ PCI_CONFIG_8(0x0c, 0x00);
415 /* PCI Latency Timer */
416 PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
417 /* PCI Header Type */
418 /* BIST (built-in self test) */
419 #if defined(TARGET_I386)
420 // !!! workaround for buggy bios
421 //~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
424 /* PCI Base Address Registers */
425 /* CSR Memory Mapped Base Address */
426 PCI_CONFIG_32(PCI_BASE_ADDRESS_0
,
427 PCI_ADDRESS_SPACE_MEM
| PCI_ADDRESS_SPACE_MEM_PREFETCH
);
428 /* CSR I/O Mapped Base Address */
429 PCI_CONFIG_32(PCI_BASE_ADDRESS_1
, PCI_ADDRESS_SPACE_IO
);
431 /* Flash Memory Mapped Base Address */
432 PCI_CONFIG_32(PCI_BASE_ADDRESS_2
, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM
);
435 /* Expansion ROM Base Address (depends on boot disable!!!) */
436 PCI_CONFIG_32(0x30, 0x00000000);
437 /* Capability Pointer */
438 PCI_CONFIG_8(0x34, 0xdc);
440 PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
442 PCI_CONFIG_8(0x3e, 0x08);
443 /* Maximum Latency */
444 PCI_CONFIG_8(0x3f, 0x18);
445 /* Power Management Capabilities / Next Item Pointer / Capability ID */
446 PCI_CONFIG_32(0xdc, 0x7e210001);
450 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1209);
451 PCI_CONFIG_8(PCI_REVISION_ID
, 0x0f);
454 PCI_CONFIG_16(PCI_DEVICE_ID
, 0x1229);
455 PCI_CONFIG_8(PCI_REVISION_ID
, 0x02);
458 PCI_CONFIG_16(PCI_DEVICE_ID
, 0x1229);
459 PCI_CONFIG_8(PCI_REVISION_ID
, 0x03);
462 PCI_CONFIG_16(PCI_DEVICE_ID
, 0x1229);
463 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
464 PCI_CONFIG_8(PCI_REVISION_ID
, 0x05);
467 PCI_CONFIG_16(PCI_DEVICE_ID
, 0x1229);
468 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
469 //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
472 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1209);
473 PCI_CONFIG_16(PCI_STATUS
, 0x2810);
474 PCI_CONFIG_8(PCI_REVISION_ID
, 0x09);
476 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
477 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
479 logout("Device %X is undefined!\n", device
);
482 if (device
== i82557C
|| device
== i82558B
|| device
== i82559C
) {
483 logout("Get device id and revision from EEPROM!!!\n");
487 static void nic_selective_reset(EEPRO100State
* s
)
490 uint16_t *eeprom_contents
= eeprom93xx_data(s
->eeprom
);
491 //~ eeprom93xx_reset(s->eeprom);
492 memcpy(eeprom_contents
, s
->macaddr
, 6);
493 eeprom_contents
[0xa] = 0x4000;
495 for (i
= 0; i
< EEPROM_SIZE
- 1; i
++) {
496 sum
+= eeprom_contents
[i
];
498 eeprom_contents
[EEPROM_SIZE
- 1] = 0xbaba - sum
;
500 memset(s
->mem
, 0, sizeof(s
->mem
));
501 uint32_t val
= BIT(21);
502 memcpy(&s
->mem
[SCBCtrlMDI
], &val
, sizeof(val
));
504 assert(sizeof(s
->mdimem
) == sizeof(eepro100_mdi_default
));
505 memcpy(&s
->mdimem
[0], &eepro100_mdi_default
[0], sizeof(s
->mdimem
));
508 static void nic_reset(void *opaque
)
510 EEPRO100State
*s
= opaque
;
516 nic_selective_reset(s
);
519 #if defined(DEBUG_EEPRO100)
520 static const char *reg
[PCI_IO_SIZE
/ 4] = {
524 "EEPROM/Flash Control",
526 "Receive DMA Byte Count",
527 "Flow control register",
528 "General Status/Control"
531 static char *regname(uint32_t addr
)
534 if (addr
< PCI_IO_SIZE
) {
535 const char *r
= reg
[addr
/ 4];
537 sprintf(buf
, "%s+%u", r
, addr
% 4);
539 sprintf(buf
, "0x%02x", addr
);
542 sprintf(buf
, "??? 0x%08x", addr
);
546 #endif /* DEBUG_EEPRO100 */
549 static uint16_t eepro100_read_status(EEPRO100State
* s
)
551 uint16_t val
= s
->status
;
552 logout("val=0x%04x\n", val
);
556 static void eepro100_write_status(EEPRO100State
* s
, uint16_t val
)
558 logout("val=0x%04x\n", val
);
563 /*****************************************************************************
567 ****************************************************************************/
570 static uint16_t eepro100_read_command(EEPRO100State
* s
)
572 uint16_t val
= 0xffff;
573 //~ logout("val=0x%04x\n", val);
578 /* Commands that can be put in a command list entry. */
583 CmdMulticastList
= 3,
585 CmdTDR
= 5, /* load microcode */
589 /* And some extra flags: */
590 CmdSuspend
= 0x4000, /* Suspend after completion. */
591 CmdIntr
= 0x2000, /* Interrupt after completion. */
592 CmdTxFlex
= 0x0008, /* Use "Flexible mode" for CmdTx command. */
595 static cu_state_t
get_cu_state(EEPRO100State
* s
)
597 return ((s
->mem
[SCBStatus
] >> 6) & 0x03);
600 static void set_cu_state(EEPRO100State
* s
, cu_state_t state
)
602 s
->mem
[SCBStatus
] = (s
->mem
[SCBStatus
] & 0x3f) + (state
<< 6);
605 static ru_state_t
get_ru_state(EEPRO100State
* s
)
607 return ((s
->mem
[SCBStatus
] >> 2) & 0x0f);
610 static void set_ru_state(EEPRO100State
* s
, ru_state_t state
)
612 s
->mem
[SCBStatus
] = (s
->mem
[SCBStatus
] & 0xc3) + (state
<< 2);
615 static void dump_statistics(EEPRO100State
* s
)
617 /* Dump statistical data. Most data is never changed by the emulation
618 * and always 0, so we first just copy the whole block and then those
619 * values which really matter.
620 * Number of data should check configuration!!!
622 cpu_physical_memory_write(s
->statsaddr
, (uint8_t *) & s
->statistics
, 64);
623 stl_phys(s
->statsaddr
+ 0, s
->statistics
.tx_good_frames
);
624 stl_phys(s
->statsaddr
+ 36, s
->statistics
.rx_good_frames
);
625 stl_phys(s
->statsaddr
+ 48, s
->statistics
.rx_resource_errors
);
626 stl_phys(s
->statsaddr
+ 60, s
->statistics
.rx_short_frame_errors
);
627 //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
628 //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
629 //~ missing("CU dump statistical counters");
632 static void eepro100_cu_command(EEPRO100State
* s
, uint8_t val
)
641 if (get_cu_state(s
) != cu_idle
) {
642 /* Intel documentation says that CU must be idle for the CU
643 * start command. Intel driver for Linux also starts the CU
644 * from suspended state. */
645 logout("CU state is %u, should be %u\n", get_cu_state(s
), cu_idle
);
646 //~ assert(!"wrong CU state");
648 set_cu_state(s
, cu_active
);
649 s
->cu_offset
= s
->pointer
;
651 cb_address
= s
->cu_base
+ s
->cu_offset
;
652 cpu_physical_memory_read(cb_address
, (uint8_t *) & tx
, sizeof(tx
));
653 uint16_t status
= le16_to_cpu(tx
.status
);
654 uint16_t command
= le16_to_cpu(tx
.command
);
656 ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
657 val
, status
, command
, tx
.link
);
658 bool bit_el
= ((command
& 0x8000) != 0);
659 bool bit_s
= ((command
& 0x4000) != 0);
660 bool bit_i
= ((command
& 0x2000) != 0);
661 bool bit_nc
= ((command
& 0x0010) != 0);
662 //~ bool bit_sf = ((command & 0x0008) != 0);
663 uint16_t cmd
= command
& 0x0007;
664 s
->cu_offset
= le32_to_cpu(tx
.link
);
670 cpu_physical_memory_read(cb_address
+ 8, &s
->macaddr
[0], 6);
671 logout("macaddr: %s\n", nic_dump(&s
->macaddr
[0], 6));
674 cpu_physical_memory_read(cb_address
+ 8, &s
->configuration
[0],
675 sizeof(s
->configuration
));
676 logout("configuration: %s\n", nic_dump(&s
->configuration
[0], 16));
678 case CmdMulticastList
:
679 //~ missing("multicast list");
683 uint32_t tbd_array
= le32_to_cpu(tx
.tx_desc_addr
);
684 uint16_t tcb_bytes
= (le16_to_cpu(tx
.tcb_bytes
) & 0x3fff);
686 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
687 tbd_array
, tcb_bytes
, tx
.tbd_count
);
690 assert(tcb_bytes
<= 2600);
691 /* Next assertion fails for local configuration. */
692 //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
693 if (!((tcb_bytes
> 0) || (tbd_array
!= 0xffffffff))) {
695 ("illegal values of TBD array address and TCB byte count!\n");
697 // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
700 uint32_t tbd_address
= cb_address
+ 0x10;
701 assert(tcb_bytes
<= sizeof(buf
));
702 while (size
< tcb_bytes
) {
703 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
704 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
705 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
708 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
709 tx_buffer_address
, tx_buffer_size
);
710 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
711 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
713 size
+= tx_buffer_size
;
715 if (tbd_array
== 0xffffffff) {
716 /* Simplified mode. Was already handled by code above. */
719 uint8_t tbd_count
= 0;
720 if ((s
->device
>= i82558B
) && !(s
->configuration
[6] & BIT(4))) {
721 /* Extended Flexible TCB. */
722 assert(tcb_bytes
== 0);
723 for (; tbd_count
< 2; tbd_count
++) {
724 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
725 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
726 uint16_t tx_buffer_el
= lduw_phys(tbd_address
+ 6);
729 ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
730 tx_buffer_address
, tx_buffer_size
);
731 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
732 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
734 size
+= tx_buffer_size
;
735 if (tx_buffer_el
& 1) {
740 tbd_address
= tbd_array
;
741 for (; tbd_count
< tx
.tbd_count
; tbd_count
++) {
742 uint32_t tx_buffer_address
= ldl_phys(tbd_address
);
743 uint16_t tx_buffer_size
= lduw_phys(tbd_address
+ 4);
744 uint16_t tx_buffer_el
= lduw_phys(tbd_address
+ 6);
747 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
748 tx_buffer_address
, tx_buffer_size
);
749 tx_buffer_size
= MIN(tx_buffer_size
, sizeof(buf
) - size
);
750 cpu_physical_memory_read(tx_buffer_address
, &buf
[size
],
752 size
+= tx_buffer_size
;
753 if (tx_buffer_el
& 1) {
758 qemu_send_packet(s
->vc
, buf
, size
);
759 s
->statistics
.tx_good_frames
++;
760 /* Transmit with bad status would raise an CX/TNO interrupt.
761 * (82557 only). Emulation never has bad status. */
762 //~ eepro100_cx_interrupt(s);
765 logout("load microcode\n");
766 /* Starting with offset 8, the command contains
767 * 64 dwords microcode which we just ignore here. */
770 missing("undefined command");
772 /* Write new status (success). */
773 stw_phys(cb_address
, status
| 0x8000 | 0x2000);
775 /* CU completed action. */
776 eepro100_cx_interrupt(s
);
779 /* CU becomes idle. */
780 set_cu_state(s
, cu_idle
);
781 eepro100_cna_interrupt(s
);
783 /* CU becomes suspended. */
784 set_cu_state(s
, cu_suspended
);
785 eepro100_cna_interrupt(s
);
787 /* More entries in list. */
788 logout("CU list with at least one more entry\n");
791 logout("CU list empty\n");
792 /* List is empty. Now CU is idle or suspended. */
795 if (get_cu_state(s
) != cu_suspended
) {
796 logout("bad CU resume from CU state %u\n", get_cu_state(s
));
797 /* Workaround for bad Linux eepro100 driver which resumes
798 * from idle state. */
799 //~ missing("cu resume");
800 set_cu_state(s
, cu_suspended
);
802 if (get_cu_state(s
) == cu_suspended
) {
803 logout("CU resuming\n");
804 set_cu_state(s
, cu_active
);
809 /* Load dump counters address. */
810 s
->statsaddr
= s
->pointer
;
811 logout("val=0x%02x (status address)\n", val
);
814 /* Dump statistical counters. */
819 logout("val=0x%02x (CU base address)\n", val
);
820 s
->cu_base
= s
->pointer
;
823 /* Dump and reset statistical counters. */
825 memset(&s
->statistics
, 0, sizeof(s
->statistics
));
828 /* CU static resume. */
829 missing("CU static resume");
832 missing("Undefined CU command");
836 static void eepro100_ru_command(EEPRO100State
* s
, uint8_t val
)
844 if (get_ru_state(s
) != ru_idle
) {
845 logout("RU state is %u, should be %u\n", get_ru_state(s
), ru_idle
);
846 //~ assert(!"wrong RU state");
848 set_ru_state(s
, ru_ready
);
849 s
->ru_offset
= s
->pointer
;
850 logout("val=0x%02x (rx start)\n", val
);
854 if (get_ru_state(s
) != ru_suspended
) {
855 logout("RU state is %u, should be %u\n", get_ru_state(s
),
857 //~ assert(!"wrong RU state");
859 set_ru_state(s
, ru_ready
);
863 logout("val=0x%02x (RU base address)\n", val
);
864 s
->ru_base
= s
->pointer
;
867 logout("val=0x%02x (undefined RU command)\n", val
);
868 missing("Undefined SU command");
872 static void eepro100_write_command(EEPRO100State
* s
, uint8_t val
)
874 eepro100_ru_command(s
, val
& 0x0f);
875 eepro100_cu_command(s
, val
& 0xf0);
877 logout("val=0x%02x\n", val
);
879 /* Clear command byte after command was accepted. */
883 /*****************************************************************************
887 ****************************************************************************/
889 #define EEPROM_CS 0x02
890 #define EEPROM_SK 0x01
891 #define EEPROM_DI 0x04
892 #define EEPROM_DO 0x08
894 static uint16_t eepro100_read_eeprom(EEPRO100State
* s
)
897 memcpy(&val
, &s
->mem
[SCBeeprom
], sizeof(val
));
898 if (eeprom93xx_read(s
->eeprom
)) {
906 static void eepro100_write_eeprom(eeprom_t
* eeprom
, uint8_t val
)
908 logout("write val=0x%02x\n", val
);
910 /* mask unwriteable bits */
911 //~ val = SET_MASKED(val, 0x31, eeprom->value);
913 int eecs
= ((val
& EEPROM_CS
) != 0);
914 int eesk
= ((val
& EEPROM_SK
) != 0);
915 int eedi
= ((val
& EEPROM_DI
) != 0);
916 eeprom93xx_write(eeprom
, eecs
, eesk
, eedi
);
919 static void eepro100_write_pointer(EEPRO100State
* s
, uint32_t val
)
921 s
->pointer
= le32_to_cpu(val
);
922 logout("val=0x%08x\n", val
);
925 /*****************************************************************************
929 ****************************************************************************/
931 #if defined(DEBUG_EEPRO100)
932 static const char *mdi_op_name
[] = {
939 static const char *mdi_reg_name
[] = {
942 "PHY Identification (Word 1)",
943 "PHY Identification (Word 2)",
944 "Auto-Negotiation Advertisement",
945 "Auto-Negotiation Link Partner Ability",
946 "Auto-Negotiation Expansion"
948 #endif /* DEBUG_EEPRO100 */
950 static uint32_t eepro100_read_mdi(EEPRO100State
* s
)
953 memcpy(&val
, &s
->mem
[0x10], sizeof(val
));
955 #ifdef DEBUG_EEPRO100
956 uint8_t raiseint
= (val
& BIT(29)) >> 29;
957 uint8_t opcode
= (val
& BITS(27, 26)) >> 26;
958 uint8_t phy
= (val
& BITS(25, 21)) >> 21;
959 uint8_t reg
= (val
& BITS(20, 16)) >> 16;
960 uint16_t data
= (val
& BITS(15, 0));
962 /* Emulation takes no time to finish MDI transaction. */
964 TRACE(MDI
, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
965 val
, raiseint
, mdi_op_name
[opcode
], phy
,
966 mdi_reg_name
[reg
], data
));
970 //~ #define BITS(val, upper, lower) (val & ???)
971 static void eepro100_write_mdi(EEPRO100State
* s
, uint32_t val
)
973 uint8_t raiseint
= (val
& BIT(29)) >> 29;
974 uint8_t opcode
= (val
& BITS(27, 26)) >> 26;
975 uint8_t phy
= (val
& BITS(25, 21)) >> 21;
976 uint8_t reg
= (val
& BITS(20, 16)) >> 16;
977 uint16_t data
= (val
& BITS(15, 0));
979 /* Unsupported PHY address. */
980 //~ logout("phy must be 1 but is %u\n", phy);
982 } else if (opcode
!= 1 && opcode
!= 2) {
983 /* Unsupported opcode. */
984 logout("opcode must be 1 or 2 but is %u\n", opcode
);
986 } else if (reg
> 6) {
987 /* Unsupported register. */
988 logout("register must be 0...6 but is %u\n", reg
);
991 TRACE(MDI
, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
992 val
, raiseint
, mdi_op_name
[opcode
], phy
,
993 mdi_reg_name
[reg
], data
));
997 case 0: /* Control Register */
999 /* Reset status and control registers to default. */
1000 s
->mdimem
[0] = eepro100_mdi_default
[0];
1001 s
->mdimem
[1] = eepro100_mdi_default
[1];
1002 data
= s
->mdimem
[reg
];
1004 /* Restart Auto Configuration = Normal Operation */
1008 case 1: /* Status Register */
1009 missing("not writable");
1010 data
= s
->mdimem
[reg
];
1012 case 2: /* PHY Identification Register (Word 1) */
1013 case 3: /* PHY Identification Register (Word 2) */
1014 missing("not implemented");
1016 case 4: /* Auto-Negotiation Advertisement Register */
1017 case 5: /* Auto-Negotiation Link Partner Ability Register */
1019 case 6: /* Auto-Negotiation Expansion Register */
1021 missing("not implemented");
1023 s
->mdimem
[reg
] = data
;
1024 } else if (opcode
== 2) {
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];
1034 case 1: /* Status Register */
1035 s
->mdimem
[reg
] |= 0x0020;
1037 case 2: /* PHY Identification Register (Word 1) */
1038 case 3: /* PHY Identification Register (Word 2) */
1039 case 4: /* Auto-Negotiation Advertisement Register */
1041 case 5: /* Auto-Negotiation Link Partner Ability Register */
1042 s
->mdimem
[reg
] = 0x41fe;
1044 case 6: /* Auto-Negotiation Expansion Register */
1045 s
->mdimem
[reg
] = 0x0001;
1048 data
= s
->mdimem
[reg
];
1050 /* Emulation takes no time to finish MDI transaction.
1051 * Set MDI bit in SCB status register. */
1052 s
->mem
[SCBAck
] |= 0x08;
1055 eepro100_mdi_interrupt(s
);
1058 val
= (val
& 0xffff0000) + data
;
1059 memcpy(&s
->mem
[0x10], &val
, sizeof(val
));
1062 /*****************************************************************************
1066 ****************************************************************************/
1068 #define PORT_SOFTWARE_RESET 0
1069 #define PORT_SELFTEST 1
1070 #define PORT_SELECTIVE_RESET 2
1072 #define PORT_SELECTION_MASK 3
1075 uint32_t st_sign
; /* Self Test Signature */
1076 uint32_t st_result
; /* Self Test Results */
1077 } eepro100_selftest_t
;
1079 static uint32_t eepro100_read_port(EEPRO100State
* s
)
1084 static void eepro100_write_port(EEPRO100State
* s
, uint32_t val
)
1086 val
= le32_to_cpu(val
);
1087 uint32_t address
= (val
& ~PORT_SELECTION_MASK
);
1088 uint8_t selection
= (val
& PORT_SELECTION_MASK
);
1089 switch (selection
) {
1090 case PORT_SOFTWARE_RESET
:
1094 logout("selftest address=0x%08x\n", address
);
1095 eepro100_selftest_t data
;
1096 cpu_physical_memory_read(address
, (uint8_t *) & data
, sizeof(data
));
1097 data
.st_sign
= 0xffffffff;
1099 cpu_physical_memory_write(address
, (uint8_t *) & data
, sizeof(data
));
1101 case PORT_SELECTIVE_RESET
:
1102 logout("selective reset, selftest address=0x%08x\n", address
);
1103 nic_selective_reset(s
);
1106 logout("val=0x%08x\n", val
);
1107 missing("unknown port selection");
1111 /*****************************************************************************
1113 * General hardware emulation.
1115 ****************************************************************************/
1117 static uint8_t eepro100_read1(EEPRO100State
* s
, uint32_t addr
)
1120 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1121 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1126 //~ val = eepro100_read_status(s);
1127 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1130 //~ val = eepro100_read_status(s);
1131 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1134 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1135 //~ val = eepro100_read_command(s);
1138 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1141 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1144 val
= eepro100_read_eeprom(s
);
1146 case 0x1b: /* PMDR (power management driver register) */
1148 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1150 case 0x1d: /* general status register */
1151 /* 100 Mbps full duplex, valid link */
1153 logout("addr=General Status val=%02x\n", val
);
1156 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1157 missing("unknown byte read");
1162 static uint16_t eepro100_read2(EEPRO100State
* s
, uint32_t addr
)
1165 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1166 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1169 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1173 //~ val = eepro100_read_status(s);
1176 val
= eepro100_read_eeprom(s
);
1179 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1180 missing("unknown word read");
1185 static uint32_t eepro100_read4(EEPRO100State
* s
, uint32_t addr
)
1188 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1189 memcpy(&val
, &s
->mem
[addr
], sizeof(val
));
1194 //~ val = eepro100_read_status(s);
1195 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1198 //~ val = eepro100_read_pointer(s);
1199 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1202 val
= eepro100_read_port(s
);
1203 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1206 val
= eepro100_read_mdi(s
);
1209 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1210 missing("unknown longword read");
1215 static void eepro100_write1(EEPRO100State
* s
, uint32_t addr
, uint8_t val
)
1217 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1218 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1221 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1225 //~ eepro100_write_status(s, val);
1228 eepro100_acknowledge(s
);
1231 eepro100_write_command(s
, val
);
1235 eepro100_swi_interrupt(s
);
1237 eepro100_interrupt(s
, 0);
1244 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1247 eepro100_write_eeprom(s
->eeprom
, val
);
1250 logout("addr=%s val=0x%02x\n", regname(addr
), val
);
1251 missing("unknown byte write");
1255 static void eepro100_write2(EEPRO100State
* s
, uint32_t addr
, uint16_t val
)
1257 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1258 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1261 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1265 //~ eepro100_write_status(s, val);
1266 eepro100_acknowledge(s
);
1269 eepro100_write_command(s
, val
);
1270 eepro100_write1(s
, SCBIntmask
, val
>> 8);
1273 eepro100_write_eeprom(s
->eeprom
, val
);
1276 logout("addr=%s val=0x%04x\n", regname(addr
), val
);
1277 missing("unknown word write");
1281 static void eepro100_write4(EEPRO100State
* s
, uint32_t addr
, uint32_t val
)
1283 if (addr
<= sizeof(s
->mem
) - sizeof(val
)) {
1284 memcpy(&s
->mem
[addr
], &val
, sizeof(val
));
1289 eepro100_write_pointer(s
, val
);
1292 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1293 eepro100_write_port(s
, val
);
1296 eepro100_write_mdi(s
, val
);
1299 logout("addr=%s val=0x%08x\n", regname(addr
), val
);
1300 missing("unknown longword write");
1304 static uint32_t ioport_read1(void *opaque
, uint32_t addr
)
1306 EEPRO100State
*s
= opaque
;
1307 //~ logout("addr=%s\n", regname(addr));
1308 return eepro100_read1(s
, addr
- s
->region
[1]);
1311 static uint32_t ioport_read2(void *opaque
, uint32_t addr
)
1313 EEPRO100State
*s
= opaque
;
1314 return eepro100_read2(s
, addr
- s
->region
[1]);
1317 static uint32_t ioport_read4(void *opaque
, uint32_t addr
)
1319 EEPRO100State
*s
= opaque
;
1320 return eepro100_read4(s
, addr
- s
->region
[1]);
1323 static void ioport_write1(void *opaque
, uint32_t addr
, uint32_t val
)
1325 EEPRO100State
*s
= opaque
;
1326 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1327 eepro100_write1(s
, addr
- s
->region
[1], val
);
1330 static void ioport_write2(void *opaque
, uint32_t addr
, uint32_t val
)
1332 EEPRO100State
*s
= opaque
;
1333 eepro100_write2(s
, addr
- s
->region
[1], val
);
1336 static void ioport_write4(void *opaque
, uint32_t addr
, uint32_t val
)
1338 EEPRO100State
*s
= opaque
;
1339 eepro100_write4(s
, addr
- s
->region
[1], val
);
1342 /***********************************************************/
1343 /* PCI EEPRO100 definitions */
1345 static void pci_map(PCIDevice
* pci_dev
, int region_num
,
1346 uint32_t addr
, uint32_t size
, int type
)
1348 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1350 logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1351 region_num
, addr
, size
, type
);
1353 assert(region_num
== 1);
1354 register_ioport_write(addr
, size
, 1, ioport_write1
, s
);
1355 register_ioport_read(addr
, size
, 1, ioport_read1
, s
);
1356 register_ioport_write(addr
, size
, 2, ioport_write2
, s
);
1357 register_ioport_read(addr
, size
, 2, ioport_read2
, s
);
1358 register_ioport_write(addr
, size
, 4, ioport_write4
, s
);
1359 register_ioport_read(addr
, size
, 4, ioport_read4
, s
);
1361 s
->region
[region_num
] = addr
;
1364 static void pci_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1366 EEPRO100State
*s
= opaque
;
1367 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1368 eepro100_write1(s
, addr
, val
);
1371 static void pci_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1373 EEPRO100State
*s
= opaque
;
1374 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1375 eepro100_write2(s
, addr
, val
);
1378 static void pci_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1380 EEPRO100State
*s
= opaque
;
1381 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1382 eepro100_write4(s
, addr
, val
);
1385 static uint32_t pci_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1387 EEPRO100State
*s
= opaque
;
1388 //~ logout("addr=%s\n", regname(addr));
1389 return eepro100_read1(s
, addr
);
1392 static uint32_t pci_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1394 EEPRO100State
*s
= opaque
;
1395 //~ logout("addr=%s\n", regname(addr));
1396 return eepro100_read2(s
, addr
);
1399 static uint32_t pci_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1401 EEPRO100State
*s
= opaque
;
1402 //~ logout("addr=%s\n", regname(addr));
1403 return eepro100_read4(s
, addr
);
1406 static CPUWriteMemoryFunc
* const pci_mmio_write
[] = {
1412 static CPUReadMemoryFunc
* const pci_mmio_read
[] = {
1418 static void pci_mmio_map(PCIDevice
* pci_dev
, int region_num
,
1419 uint32_t addr
, uint32_t size
, int type
)
1421 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1423 logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1424 region_num
, addr
, size
, type
);
1426 if (region_num
== 0) {
1427 /* Map control / status registers. */
1428 cpu_register_physical_memory(addr
, size
, s
->mmio_index
);
1429 s
->region
[region_num
] = addr
;
1433 static int nic_can_receive(VLANClientState
*vc
)
1435 EEPRO100State
*s
= vc
->opaque
;
1437 return get_ru_state(s
) == ru_ready
;
1438 //~ return !eepro100_buffer_full(s);
1441 static ssize_t
nic_receive(VLANClientState
*vc
, const uint8_t * buf
, size_t size
)
1444 * - Magic packets should set bit 30 in power management driver register.
1445 * - Interesting packets should set bit 29 in power management driver register.
1447 EEPRO100State
*s
= vc
->opaque
;
1448 uint16_t rfd_status
= 0xa000;
1449 static const uint8_t broadcast_macaddr
[6] =
1450 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1452 /* TODO: check multiple IA bit. */
1453 assert(!(s
->configuration
[20] & BIT(6)));
1455 if (s
->configuration
[8] & 0x80) {
1456 /* CSMA is disabled. */
1457 logout("%p received while CSMA is disabled\n", s
);
1459 } else if (size
< 64 && (s
->configuration
[7] & 1)) {
1460 /* Short frame and configuration byte 7/0 (discard short receive) set:
1461 * Short frame is discarded */
1462 logout("%p received short frame (%d byte)\n", s
, size
);
1463 s
->statistics
.rx_short_frame_errors
++;
1465 } else if ((size
> MAX_ETH_FRAME_SIZE
+ 4) && !(s
->configuration
[18] & 8)) {
1466 /* Long frame and configuration byte 18/3 (long receive ok) not set:
1467 * Long frames are discarded. */
1468 logout("%p received long frame (%d byte), ignored\n", s
, size
);
1470 } else if (memcmp(buf
, s
->macaddr
, 6) == 0) { // !!!
1471 /* Frame matches individual address. */
1472 /* TODO: check configuration byte 15/4 (ignore U/L). */
1473 logout("%p received frame for me, len=%d\n", s
, size
);
1474 } else if (memcmp(buf
, broadcast_macaddr
, 6) == 0) {
1475 /* Broadcast frame. */
1476 logout("%p received broadcast, len=%d\n", s
, size
);
1477 rfd_status
|= 0x0002;
1478 } else if (buf
[0] & 0x01) { // !!!
1479 /* Multicast frame. */
1480 logout("%p received multicast, len=%d\n", s
, size
);
1481 /* TODO: check multicast all bit. */
1482 assert(!(s
->configuration
[21] & BIT(3)));
1483 int mcast_idx
= compute_mcast_idx(buf
);
1484 if (!(s
->mult
[mcast_idx
>> 3] & (1 << (mcast_idx
& 7)))) {
1487 rfd_status
|= 0x0002;
1488 } else if (s
->configuration
[15] & 1) {
1489 /* Promiscuous: receive all. */
1490 logout("%p received frame in promiscuous mode, len=%d\n", s
, size
);
1491 rfd_status
|= 0x0004;
1493 logout("%p received frame, ignored, len=%d,%s\n", s
, size
,
1494 nic_dump(buf
, size
));
1498 if (get_ru_state(s
) != ru_ready
) {
1499 /* No ressources available. */
1500 logout("no ressources, state=%u\n", get_ru_state(s
));
1501 s
->statistics
.rx_resource_errors
++;
1502 //~ assert(!"no ressources");
1506 //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1508 cpu_physical_memory_read(s
->ru_base
+ s
->ru_offset
, (uint8_t *) & rx
,
1509 offsetof(eepro100_rx_t
, packet
));
1510 uint16_t rfd_command
= le16_to_cpu(rx
.command
);
1511 uint16_t rfd_size
= le16_to_cpu(rx
.size
);
1512 assert(size
<= rfd_size
);
1514 rfd_status
|= 0x0080;
1516 logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n", rfd_command
,
1517 rx
.link
, rx
.rx_buf_addr
, rfd_size
);
1518 stw_phys(s
->ru_base
+ s
->ru_offset
+ offsetof(eepro100_rx_t
, status
),
1520 stw_phys(s
->ru_base
+ s
->ru_offset
+ offsetof(eepro100_rx_t
, count
), size
);
1521 /* Early receive interrupt not supported. */
1522 //~ eepro100_er_interrupt(s);
1523 /* Receive CRC Transfer not supported. */
1524 assert(!(s
->configuration
[18] & 4));
1525 /* TODO: check stripping enable bit. */
1526 //~ assert(!(s->configuration[17] & 1));
1527 cpu_physical_memory_write(s
->ru_base
+ s
->ru_offset
+
1528 offsetof(eepro100_rx_t
, packet
), buf
, size
);
1529 s
->statistics
.rx_good_frames
++;
1530 eepro100_fr_interrupt(s
);
1531 s
->ru_offset
= le32_to_cpu(rx
.link
);
1532 if (rfd_command
& 0x8000) {
1533 /* EL bit is set, so this was the last frame. */
1536 if (rfd_command
& 0x4000) {
1538 set_ru_state(s
, ru_suspended
);
1543 static int nic_load(QEMUFile
* f
, void *opaque
, int version_id
)
1545 EEPRO100State
*s
= opaque
;
1552 if (version_id
>= 3) {
1553 ret
= pci_device_load(&s
->dev
, f
);
1558 if (version_id
>= 2) {
1559 qemu_get_8s(f
, &s
->rxcr
);
1564 qemu_get_8s(f
, &s
->cmd
);
1565 qemu_get_be32s(f
, &s
->start
);
1566 qemu_get_be32s(f
, &s
->stop
);
1567 qemu_get_8s(f
, &s
->boundary
);
1568 qemu_get_8s(f
, &s
->tsr
);
1569 qemu_get_8s(f
, &s
->tpsr
);
1570 qemu_get_be16s(f
, &s
->tcnt
);
1571 qemu_get_be16s(f
, &s
->rcnt
);
1572 qemu_get_be32s(f
, &s
->rsar
);
1573 qemu_get_8s(f
, &s
->rsr
);
1574 qemu_get_8s(f
, &s
->isr
);
1575 qemu_get_8s(f
, &s
->dcfg
);
1576 qemu_get_8s(f
, &s
->imr
);
1577 qemu_get_buffer(f
, s
->phys
, 6);
1578 qemu_get_8s(f
, &s
->curpag
);
1579 qemu_get_buffer(f
, s
->mult
, 8);
1580 qemu_get_buffer(f
, s
->mem
, sizeof(s
->mem
));
1582 /* Restore all members of struct between scv_stat and mem */
1583 qemu_get_8s(f
, &s
->scb_stat
);
1584 qemu_get_8s(f
, &s
->int_stat
);
1585 for (i
= 0; i
< 3; i
++)
1586 qemu_get_be32s(f
, &s
->region
[i
]);
1587 qemu_get_buffer(f
, s
->macaddr
, 6);
1588 for (i
= 0; i
< 19; i
++)
1589 qemu_get_be32s(f
, &s
->statcounter
[i
]);
1590 for (i
= 0; i
< 32; i
++)
1591 qemu_get_be16s(f
, &s
->mdimem
[i
]);
1592 /* The eeprom should be saved and restored by its own routines */
1593 qemu_get_be32s(f
, &s
->device
);
1594 qemu_get_be32s(f
, &s
->pointer
);
1595 qemu_get_be32s(f
, &s
->cu_base
);
1596 qemu_get_be32s(f
, &s
->cu_offset
);
1597 qemu_get_be32s(f
, &s
->ru_base
);
1598 qemu_get_be32s(f
, &s
->ru_offset
);
1599 qemu_get_be32s(f
, &s
->statsaddr
);
1600 /* Restore epro100_stats_t statistics */
1601 qemu_get_be32s(f
, &s
->statistics
.tx_good_frames
);
1602 qemu_get_be32s(f
, &s
->statistics
.tx_max_collisions
);
1603 qemu_get_be32s(f
, &s
->statistics
.tx_late_collisions
);
1604 qemu_get_be32s(f
, &s
->statistics
.tx_underruns
);
1605 qemu_get_be32s(f
, &s
->statistics
.tx_lost_crs
);
1606 qemu_get_be32s(f
, &s
->statistics
.tx_deferred
);
1607 qemu_get_be32s(f
, &s
->statistics
.tx_single_collisions
);
1608 qemu_get_be32s(f
, &s
->statistics
.tx_multiple_collisions
);
1609 qemu_get_be32s(f
, &s
->statistics
.tx_total_collisions
);
1610 qemu_get_be32s(f
, &s
->statistics
.rx_good_frames
);
1611 qemu_get_be32s(f
, &s
->statistics
.rx_crc_errors
);
1612 qemu_get_be32s(f
, &s
->statistics
.rx_alignment_errors
);
1613 qemu_get_be32s(f
, &s
->statistics
.rx_resource_errors
);
1614 qemu_get_be32s(f
, &s
->statistics
.rx_overrun_errors
);
1615 qemu_get_be32s(f
, &s
->statistics
.rx_cdt_errors
);
1616 qemu_get_be32s(f
, &s
->statistics
.rx_short_frame_errors
);
1617 qemu_get_be32s(f
, &s
->statistics
.fc_xmt_pause
);
1618 qemu_get_be32s(f
, &s
->statistics
.fc_rcv_pause
);
1619 qemu_get_be32s(f
, &s
->statistics
.fc_rcv_unsupported
);
1620 qemu_get_be16s(f
, &s
->statistics
.xmt_tco_frames
);
1621 qemu_get_be16s(f
, &s
->statistics
.rcv_tco_frames
);
1622 qemu_get_be32s(f
, &s
->statistics
.complete
);
1624 qemu_get_be16s(f
, &s
->status
);
1627 /* Configuration bytes. */
1628 qemu_get_buffer(f
, s
->configuration
, sizeof(s
->configuration
));
1633 static void nic_save(QEMUFile
* f
, void *opaque
)
1635 EEPRO100State
*s
= opaque
;
1638 pci_device_save(&s
->dev
, f
);
1640 qemu_put_8s(f
, &s
->rxcr
);
1642 qemu_put_8s(f
, &s
->cmd
);
1643 qemu_put_be32s(f
, &s
->start
);
1644 qemu_put_be32s(f
, &s
->stop
);
1645 qemu_put_8s(f
, &s
->boundary
);
1646 qemu_put_8s(f
, &s
->tsr
);
1647 qemu_put_8s(f
, &s
->tpsr
);
1648 qemu_put_be16s(f
, &s
->tcnt
);
1649 qemu_put_be16s(f
, &s
->rcnt
);
1650 qemu_put_be32s(f
, &s
->rsar
);
1651 qemu_put_8s(f
, &s
->rsr
);
1652 qemu_put_8s(f
, &s
->isr
);
1653 qemu_put_8s(f
, &s
->dcfg
);
1654 qemu_put_8s(f
, &s
->imr
);
1655 qemu_put_buffer(f
, s
->phys
, 6);
1656 qemu_put_8s(f
, &s
->curpag
);
1657 qemu_put_buffer(f
, s
->mult
, 8);
1658 qemu_put_buffer(f
, s
->mem
, sizeof(s
->mem
));
1660 /* Save all members of struct between scv_stat and mem */
1661 qemu_put_8s(f
, &s
->scb_stat
);
1662 qemu_put_8s(f
, &s
->int_stat
);
1663 for (i
= 0; i
< 3; i
++)
1664 qemu_put_be32s(f
, &s
->region
[i
]);
1665 qemu_put_buffer(f
, s
->macaddr
, 6);
1666 for (i
= 0; i
< 19; i
++)
1667 qemu_put_be32s(f
, &s
->statcounter
[i
]);
1668 for (i
= 0; i
< 32; i
++)
1669 qemu_put_be16s(f
, &s
->mdimem
[i
]);
1670 /* The eeprom should be saved and restored by its own routines */
1671 qemu_put_be32s(f
, &s
->device
);
1672 qemu_put_be32s(f
, &s
->pointer
);
1673 qemu_put_be32s(f
, &s
->cu_base
);
1674 qemu_put_be32s(f
, &s
->cu_offset
);
1675 qemu_put_be32s(f
, &s
->ru_base
);
1676 qemu_put_be32s(f
, &s
->ru_offset
);
1677 qemu_put_be32s(f
, &s
->statsaddr
);
1678 /* Save epro100_stats_t statistics */
1679 qemu_put_be32s(f
, &s
->statistics
.tx_good_frames
);
1680 qemu_put_be32s(f
, &s
->statistics
.tx_max_collisions
);
1681 qemu_put_be32s(f
, &s
->statistics
.tx_late_collisions
);
1682 qemu_put_be32s(f
, &s
->statistics
.tx_underruns
);
1683 qemu_put_be32s(f
, &s
->statistics
.tx_lost_crs
);
1684 qemu_put_be32s(f
, &s
->statistics
.tx_deferred
);
1685 qemu_put_be32s(f
, &s
->statistics
.tx_single_collisions
);
1686 qemu_put_be32s(f
, &s
->statistics
.tx_multiple_collisions
);
1687 qemu_put_be32s(f
, &s
->statistics
.tx_total_collisions
);
1688 qemu_put_be32s(f
, &s
->statistics
.rx_good_frames
);
1689 qemu_put_be32s(f
, &s
->statistics
.rx_crc_errors
);
1690 qemu_put_be32s(f
, &s
->statistics
.rx_alignment_errors
);
1691 qemu_put_be32s(f
, &s
->statistics
.rx_resource_errors
);
1692 qemu_put_be32s(f
, &s
->statistics
.rx_overrun_errors
);
1693 qemu_put_be32s(f
, &s
->statistics
.rx_cdt_errors
);
1694 qemu_put_be32s(f
, &s
->statistics
.rx_short_frame_errors
);
1695 qemu_put_be32s(f
, &s
->statistics
.fc_xmt_pause
);
1696 qemu_put_be32s(f
, &s
->statistics
.fc_rcv_pause
);
1697 qemu_put_be32s(f
, &s
->statistics
.fc_rcv_unsupported
);
1698 qemu_put_be16s(f
, &s
->statistics
.xmt_tco_frames
);
1699 qemu_put_be16s(f
, &s
->statistics
.rcv_tco_frames
);
1700 qemu_put_be32s(f
, &s
->statistics
.complete
);
1702 qemu_put_be16s(f
, &s
->status
);
1705 /* Configuration bytes. */
1706 qemu_put_buffer(f
, s
->configuration
, sizeof(s
->configuration
));
1709 static void nic_cleanup(VLANClientState
*vc
)
1711 EEPRO100State
*s
= vc
->opaque
;
1713 unregister_savevm(vc
->model
, s
);
1715 eeprom93xx_free(s
->eeprom
);
1718 static int pci_nic_uninit(PCIDevice
*dev
)
1720 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, dev
);
1722 cpu_unregister_io_memory(s
->mmio_index
);
1727 static int nic_init(PCIDevice
*pci_dev
, uint32_t device
)
1729 EEPRO100State
*s
= DO_UPCAST(EEPRO100State
, dev
, pci_dev
);
1733 s
->dev
.unregister
= pci_nic_uninit
;
1739 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1740 * i82559 and later support 64 or 256 word EEPROM. */
1741 s
->eeprom
= eeprom93xx_new(EEPROM_SIZE
);
1743 /* Handler for memory-mapped I/O */
1745 cpu_register_io_memory(pci_mmio_read
, pci_mmio_write
, s
);
1747 pci_register_bar(&s
->dev
, 0, PCI_MEM_SIZE
,
1748 PCI_ADDRESS_SPACE_MEM
|
1749 PCI_ADDRESS_SPACE_MEM_PREFETCH
, pci_mmio_map
);
1750 pci_register_bar(&s
->dev
, 1, PCI_IO_SIZE
, PCI_ADDRESS_SPACE_IO
,
1752 pci_register_bar(&s
->dev
, 2, PCI_FLASH_SIZE
, PCI_ADDRESS_SPACE_MEM
,
1755 qdev_get_macaddr(&s
->dev
.qdev
, s
->macaddr
);
1756 logout("macaddr: %s\n", nic_dump(&s
->macaddr
[0], 6));
1757 assert(s
->region
[1] == 0);
1761 s
->vc
= qdev_get_vlan_client(&s
->dev
.qdev
,
1762 nic_can_receive
, nic_receive
, NULL
,
1765 qemu_format_nic_info_str(s
->vc
, s
->macaddr
);
1767 qemu_register_reset(nic_reset
, s
);
1769 register_savevm(s
->vc
->model
, -1, 3, nic_save
, nic_load
, s
);
1773 static int pci_i82551_init(PCIDevice
*dev
)
1775 return nic_init(dev
, i82551
);
1778 static int pci_i82557b_init(PCIDevice
*dev
)
1780 return nic_init(dev
, i82557B
);
1783 static int pci_i82559er_init(PCIDevice
*dev
)
1785 return nic_init(dev
, i82559ER
);
1788 static PCIDeviceInfo eepro100_info
[] = {
1790 .qdev
.name
= "i82551",
1791 .qdev
.size
= sizeof(EEPRO100State
),
1792 .init
= pci_i82551_init
,
1794 .qdev
.name
= "i82557b",
1795 .qdev
.size
= sizeof(EEPRO100State
),
1796 .init
= pci_i82557b_init
,
1798 .qdev
.name
= "i82559er",
1799 .qdev
.size
= sizeof(EEPRO100State
),
1800 .init
= pci_i82559er_init
,
1806 static void eepro100_register_devices(void)
1808 pci_qdev_register_many(eepro100_info
);
1811 device_init(eepro100_register_devices
)