2 * QEMU RTL8139 emulation
4 * Copyright (c) 2006 Igor Kovalenko
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * 2006-Jan-28 Mark Malakanov : TSAD and CSCR implementation (for Windows driver)
27 * 2006-Apr-28 Juergen Lock : EEPROM emulation changes for FreeBSD driver
28 * HW revision ID changes for FreeBSD driver
30 * 2006-Jul-01 Igor Kovalenko : Implemented loopback mode for FreeBSD driver
31 * Corrected packet transfer reassembly routine for 8139C+ mode
32 * Rearranged debugging print statements
33 * Implemented PCI timer interrupt (disabled by default)
34 * Implemented Tally Counters, increased VM load/save version
35 * Implemented IP/TCP/UDP checksum task offloading
40 /* XXX: such dependency must be suppressed */
41 #include <slirp/slirp.h>
43 /* debug RTL8139 card */
44 //#define DEBUG_RTL8139 1
46 #define PCI_FREQUENCY 33000000L
48 /* debug RTL8139 card C+ mode only */
49 //#define DEBUG_RTL8139CP 1
51 /* RTL8139 provides frame CRC with received packet, this feature seems to be
52 ignored by most drivers, disabled by default */
53 //#define RTL8139_CALCULATE_RXCRC 1
55 /* Uncomment to enable on-board timer interrupts */
56 //#define RTL8139_ONBOARD_TIMER 1
58 #if defined(RTL8139_CALCULATE_RXCRC)
63 #define SET_MASKED(input, mask, curr) \
64 ( ( (input) & ~(mask) ) | ( (curr) & (mask) ) )
66 /* arg % size for size which is a power of 2 */
67 #define MOD2(input, size) \
68 ( ( input ) & ( size - 1 ) )
70 #if defined (DEBUG_RTL8139)
71 # define DEBUG_PRINT(x) do { printf x ; } while (0)
73 # define DEBUG_PRINT(x)
76 /* Symbolic offsets to registers. */
77 enum RTL8139_registers
{
78 MAC0
= 0, /* Ethernet hardware address. */
79 MAR0
= 8, /* Multicast filter. */
80 TxStatus0
= 0x10,/* Transmit status (Four 32bit registers). C mode only */
81 /* Dump Tally Conter control register(64bit). C+ mode only */
82 TxAddr0
= 0x20, /* Tx descriptors (also four 32bit). */
91 Timer
= 0x48, /* A general-purpose counter. */
92 RxMissed
= 0x4C, /* 24 bits valid, write clears. */
99 Config4
= 0x5A, /* absent on RTL-8139A */
102 PCIRevisionID
= 0x5E,
103 TxSummary
= 0x60, /* TSAD register. Transmit Status of All Descriptors*/
104 BasicModeCtrl
= 0x62,
105 BasicModeStatus
= 0x64,
108 NWayExpansion
= 0x6A,
109 /* Undocumented registers, but required for proper operation. */
110 FIFOTMS
= 0x70, /* FIFO Control and test. */
111 CSCR
= 0x74, /* Chip Status and Configuration Register. */
113 PARA7c
= 0x7c, /* Magic transceiver parameter register. */
114 Config5
= 0xD8, /* absent on RTL-8139A */
116 TxPoll
= 0xD9, /* Tell chip to check Tx descriptors for work */
117 RxMaxSize
= 0xDA, /* Max size of an Rx packet (8169 only) */
118 CpCmd
= 0xE0, /* C+ Command register (C+ mode only) */
119 IntrMitigate
= 0xE2, /* rx/tx interrupt mitigation control */
120 RxRingAddrLO
= 0xE4, /* 64-bit start addr of Rx ring */
121 RxRingAddrHI
= 0xE8, /* 64-bit start addr of Rx ring */
122 TxThresh
= 0xEC, /* Early Tx threshold */
126 MultiIntrClear
= 0xF000,
128 Config1Clear
= (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
140 CPlusRxVLAN
= 0x0040, /* enable receive VLAN detagging */
141 CPlusRxChkSum
= 0x0020, /* enable receive checksum offloading */
146 /* Interrupt register bits, using my own meaningful names. */
147 enum IntrStatusBits
{
158 RxAckBits
= RxFIFOOver
| RxOverflow
| RxOK
,
165 TxOutOfWindow
= 0x20000000,
166 TxAborted
= 0x40000000,
167 TxCarrierLost
= 0x80000000,
170 RxMulticast
= 0x8000,
172 RxBroadcast
= 0x2000,
173 RxBadSymbol
= 0x0020,
181 /* Bits in RxConfig. */
185 AcceptBroadcast
= 0x08,
186 AcceptMulticast
= 0x04,
188 AcceptAllPhys
= 0x01,
191 /* Bits in TxConfig. */
192 enum tx_config_bits
{
194 /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
196 TxIFG84
= (0 << TxIFGShift
), /* 8.4us / 840ns (10 / 100Mbps) */
197 TxIFG88
= (1 << TxIFGShift
), /* 8.8us / 880ns (10 / 100Mbps) */
198 TxIFG92
= (2 << TxIFGShift
), /* 9.2us / 920ns (10 / 100Mbps) */
199 TxIFG96
= (3 << TxIFGShift
), /* 9.6us / 960ns (10 / 100Mbps) */
201 TxLoopBack
= (1 << 18) | (1 << 17), /* enable loopback test mode */
202 TxCRC
= (1 << 16), /* DISABLE appending CRC to end of Tx packets */
203 TxClearAbt
= (1 << 0), /* Clear abort (WO) */
204 TxDMAShift
= 8, /* DMA burst value (0-7) is shifted this many bits */
205 TxRetryShift
= 4, /* TXRR value (0-15) is shifted this many bits */
207 TxVersionMask
= 0x7C800000, /* mask out version bits 30-26, 23 */
211 /* Transmit Status of All Descriptors (TSAD) Register */
213 TSAD_TOK3
= 1<<15, // TOK bit of Descriptor 3
214 TSAD_TOK2
= 1<<14, // TOK bit of Descriptor 2
215 TSAD_TOK1
= 1<<13, // TOK bit of Descriptor 1
216 TSAD_TOK0
= 1<<12, // TOK bit of Descriptor 0
217 TSAD_TUN3
= 1<<11, // TUN bit of Descriptor 3
218 TSAD_TUN2
= 1<<10, // TUN bit of Descriptor 2
219 TSAD_TUN1
= 1<<9, // TUN bit of Descriptor 1
220 TSAD_TUN0
= 1<<8, // TUN bit of Descriptor 0
221 TSAD_TABT3
= 1<<07, // TABT bit of Descriptor 3
222 TSAD_TABT2
= 1<<06, // TABT bit of Descriptor 2
223 TSAD_TABT1
= 1<<05, // TABT bit of Descriptor 1
224 TSAD_TABT0
= 1<<04, // TABT bit of Descriptor 0
225 TSAD_OWN3
= 1<<03, // OWN bit of Descriptor 3
226 TSAD_OWN2
= 1<<02, // OWN bit of Descriptor 2
227 TSAD_OWN1
= 1<<01, // OWN bit of Descriptor 1
228 TSAD_OWN0
= 1<<00, // OWN bit of Descriptor 0
232 /* Bits in Config1 */
234 Cfg1_PM_Enable
= 0x01,
235 Cfg1_VPD_Enable
= 0x02,
238 LWAKE
= 0x10, /* not on 8139, 8139A */
239 Cfg1_Driver_Load
= 0x20,
242 SLEEP
= (1 << 1), /* only on 8139, 8139A */
243 PWRDN
= (1 << 0), /* only on 8139, 8139A */
246 /* Bits in Config3 */
248 Cfg3_FBtBEn
= (1 << 0), /* 1 = Fast Back to Back */
249 Cfg3_FuncRegEn
= (1 << 1), /* 1 = enable CardBus Function registers */
250 Cfg3_CLKRUN_En
= (1 << 2), /* 1 = enable CLKRUN */
251 Cfg3_CardB_En
= (1 << 3), /* 1 = enable CardBus registers */
252 Cfg3_LinkUp
= (1 << 4), /* 1 = wake up on link up */
253 Cfg3_Magic
= (1 << 5), /* 1 = wake up on Magic Packet (tm) */
254 Cfg3_PARM_En
= (1 << 6), /* 0 = software can set twister parameters */
255 Cfg3_GNTSel
= (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
258 /* Bits in Config4 */
260 LWPTN
= (1 << 2), /* not on 8139, 8139A */
263 /* Bits in Config5 */
265 Cfg5_PME_STS
= (1 << 0), /* 1 = PCI reset resets PME_Status */
266 Cfg5_LANWake
= (1 << 1), /* 1 = enable LANWake signal */
267 Cfg5_LDPS
= (1 << 2), /* 0 = save power when link is down */
268 Cfg5_FIFOAddrPtr
= (1 << 3), /* Realtek internal SRAM testing */
269 Cfg5_UWF
= (1 << 4), /* 1 = accept unicast wakeup frame */
270 Cfg5_MWF
= (1 << 5), /* 1 = accept multicast wakeup frame */
271 Cfg5_BWF
= (1 << 6), /* 1 = accept broadcast wakeup frame */
275 /* rx fifo threshold */
277 RxCfgFIFONone
= (7 << RxCfgFIFOShift
),
281 RxCfgDMAUnlimited
= (7 << RxCfgDMAShift
),
283 /* rx ring buffer length */
285 RxCfgRcv16K
= (1 << 11),
286 RxCfgRcv32K
= (1 << 12),
287 RxCfgRcv64K
= (1 << 11) | (1 << 12),
289 /* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
293 /* Twister tuning parameters from RealTek.
294 Completely undocumented, but required to tune bad links on some boards. */
297 CSCR_LinkOKBit = 0x0400,
298 CSCR_LinkChangeBit = 0x0800,
299 CSCR_LinkStatusBits = 0x0f000,
300 CSCR_LinkDownOffCmd = 0x003c0,
301 CSCR_LinkDownCmd = 0x0f3c0,
304 CSCR_Testfun
= 1<<15, /* 1 = Auto-neg speeds up internal timer, WO, def 0 */
305 CSCR_LD
= 1<<9, /* Active low TPI link disable signal. When low, TPI still transmits link pulses and TPI stays in good link state. def 1*/
306 CSCR_HEART_BIT
= 1<<8, /* 1 = HEART BEAT enable, 0 = HEART BEAT disable. HEART BEAT function is only valid in 10Mbps mode. def 1*/
307 CSCR_JBEN
= 1<<7, /* 1 = enable jabber function. 0 = disable jabber function, def 1*/
308 CSCR_F_LINK_100
= 1<<6, /* Used to login force good link in 100Mbps for diagnostic purposes. 1 = DISABLE, 0 = ENABLE. def 1*/
309 CSCR_F_Connect
= 1<<5, /* Assertion of this bit forces the disconnect function to be bypassed. def 0*/
310 CSCR_Con_status
= 1<<3, /* This bit indicates the status of the connection. 1 = valid connected link detected; 0 = disconnected link detected. RO def 0*/
311 CSCR_Con_status_En
= 1<<2, /* Assertion of this bit configures LED1 pin to indicate connection status. def 0*/
312 CSCR_PASS_SCR
= 1<<0, /* Bypass Scramble, def 0*/
317 Cfg9346_Unlock
= 0xC0,
334 HasHltClk
= (1 << 0),
338 #define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
339 (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
340 #define HW_REVID_MASK HW_REVID(1, 1, 1, 1, 1, 1, 1)
342 #define RTL8139_PCI_REVID_8139 0x10
343 #define RTL8139_PCI_REVID_8139CPLUS 0x20
345 #define RTL8139_PCI_REVID RTL8139_PCI_REVID_8139CPLUS
347 /* Size is 64 * 16bit words */
348 #define EEPROM_9346_ADDR_BITS 6
349 #define EEPROM_9346_SIZE (1 << EEPROM_9346_ADDR_BITS)
350 #define EEPROM_9346_ADDR_MASK (EEPROM_9346_SIZE - 1)
352 enum Chip9346Operation
354 Chip9346_op_mask
= 0xc0, /* 10 zzzzzz */
355 Chip9346_op_read
= 0x80, /* 10 AAAAAA */
356 Chip9346_op_write
= 0x40, /* 01 AAAAAA D(15)..D(0) */
357 Chip9346_op_ext_mask
= 0xf0, /* 11 zzzzzz */
358 Chip9346_op_write_enable
= 0x30, /* 00 11zzzz */
359 Chip9346_op_write_all
= 0x10, /* 00 01zzzz */
360 Chip9346_op_write_disable
= 0x00, /* 00 00zzzz */
366 Chip9346_enter_command_mode
,
367 Chip9346_read_command
,
368 Chip9346_data_read
, /* from output register */
369 Chip9346_data_write
, /* to input register, then to contents at specified address */
370 Chip9346_data_write_all
, /* to input register, then filling contents */
373 typedef struct EEprom9346
375 uint16_t contents
[EEPROM_9346_SIZE
];
388 typedef struct RTL8139TallyCounters
404 } RTL8139TallyCounters
;
406 /* Clears all tally counters */
407 static void RTL8139TallyCounters_clear(RTL8139TallyCounters
* counters
);
409 /* Writes tally counters to specified physical memory address */
410 static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr
, RTL8139TallyCounters
* counters
);
412 /* Loads values of tally counters from VM state file */
413 static void RTL8139TallyCounters_load(QEMUFile
* f
, RTL8139TallyCounters
*tally_counters
);
415 /* Saves values of tally counters to VM state file */
416 static void RTL8139TallyCounters_save(QEMUFile
* f
, RTL8139TallyCounters
*tally_counters
);
418 typedef struct RTL8139State
{
419 uint8_t phys
[8]; /* mac address */
420 uint8_t mult
[8]; /* multicast mask array */
422 uint32_t TxStatus
[4]; /* TxStatus0 in C mode*/ /* also DTCCR[0] and DTCCR[1] in C+ mode */
423 uint32_t TxAddr
[4]; /* TxAddr0 */
424 uint32_t RxBuf
; /* Receive buffer */
425 uint32_t RxBufferSize
;/* internal variable, receive ring buffer size in C mode */
445 uint8_t clock_enabled
;
446 uint8_t bChipCmdState
;
450 uint16_t BasicModeCtrl
;
451 uint16_t BasicModeStatus
;
454 uint16_t NWayExpansion
;
463 int rtl8139_mmio_io_addr
;
469 uint32_t currCPlusRxDesc
;
470 uint32_t currCPlusTxDesc
;
472 uint32_t RxRingAddrLO
;
473 uint32_t RxRingAddrHI
;
482 RTL8139TallyCounters tally_counters
;
484 /* Non-persistent data */
485 uint8_t *cplus_txbuffer
;
486 int cplus_txbuffer_len
;
487 int cplus_txbuffer_offset
;
489 /* PCI interrupt timer */
494 void prom9346_decode_command(EEprom9346
*eeprom
, uint8_t command
)
496 DEBUG_PRINT(("RTL8139: eeprom command 0x%02x\n", command
));
498 switch (command
& Chip9346_op_mask
)
500 case Chip9346_op_read
:
502 eeprom
->address
= command
& EEPROM_9346_ADDR_MASK
;
503 eeprom
->output
= eeprom
->contents
[eeprom
->address
];
506 eeprom
->mode
= Chip9346_data_read
;
507 DEBUG_PRINT(("RTL8139: eeprom read from address 0x%02x data=0x%04x\n",
508 eeprom
->address
, eeprom
->output
));
512 case Chip9346_op_write
:
514 eeprom
->address
= command
& EEPROM_9346_ADDR_MASK
;
517 eeprom
->mode
= Chip9346_none
; /* Chip9346_data_write */
518 DEBUG_PRINT(("RTL8139: eeprom begin write to address 0x%02x\n",
523 eeprom
->mode
= Chip9346_none
;
524 switch (command
& Chip9346_op_ext_mask
)
526 case Chip9346_op_write_enable
:
527 DEBUG_PRINT(("RTL8139: eeprom write enabled\n"));
529 case Chip9346_op_write_all
:
530 DEBUG_PRINT(("RTL8139: eeprom begin write all\n"));
532 case Chip9346_op_write_disable
:
533 DEBUG_PRINT(("RTL8139: eeprom write disabled\n"));
540 void prom9346_shift_clock(EEprom9346
*eeprom
)
542 int bit
= eeprom
->eedi
?1:0;
546 DEBUG_PRINT(("eeprom: tick %d eedi=%d eedo=%d\n", eeprom
->tick
, eeprom
->eedi
, eeprom
->eedo
));
548 switch (eeprom
->mode
)
550 case Chip9346_enter_command_mode
:
553 eeprom
->mode
= Chip9346_read_command
;
556 DEBUG_PRINT(("eeprom: +++ synchronized, begin command read\n"));
560 case Chip9346_read_command
:
561 eeprom
->input
= (eeprom
->input
<< 1) | (bit
& 1);
562 if (eeprom
->tick
== 8)
564 prom9346_decode_command(eeprom
, eeprom
->input
& 0xff);
568 case Chip9346_data_read
:
569 eeprom
->eedo
= (eeprom
->output
& 0x8000)?1:0;
570 eeprom
->output
<<= 1;
571 if (eeprom
->tick
== 16)
574 // the FreeBSD drivers (rl and re) don't explicitly toggle
575 // CS between reads (or does setting Cfg9346 to 0 count too?),
576 // so we need to enter wait-for-command state here
577 eeprom
->mode
= Chip9346_enter_command_mode
;
581 DEBUG_PRINT(("eeprom: +++ end of read, awaiting next command\n"));
583 // original behaviour
585 eeprom
->address
&= EEPROM_9346_ADDR_MASK
;
586 eeprom
->output
= eeprom
->contents
[eeprom
->address
];
589 DEBUG_PRINT(("eeprom: +++ read next address 0x%02x data=0x%04x\n",
590 eeprom
->address
, eeprom
->output
));
595 case Chip9346_data_write
:
596 eeprom
->input
= (eeprom
->input
<< 1) | (bit
& 1);
597 if (eeprom
->tick
== 16)
599 DEBUG_PRINT(("RTL8139: eeprom write to address 0x%02x data=0x%04x\n",
600 eeprom
->address
, eeprom
->input
));
602 eeprom
->contents
[eeprom
->address
] = eeprom
->input
;
603 eeprom
->mode
= Chip9346_none
; /* waiting for next command after CS cycle */
609 case Chip9346_data_write_all
:
610 eeprom
->input
= (eeprom
->input
<< 1) | (bit
& 1);
611 if (eeprom
->tick
== 16)
614 for (i
= 0; i
< EEPROM_9346_SIZE
; i
++)
616 eeprom
->contents
[i
] = eeprom
->input
;
618 DEBUG_PRINT(("RTL8139: eeprom filled with data=0x%04x\n",
621 eeprom
->mode
= Chip9346_enter_command_mode
;
632 int prom9346_get_wire(RTL8139State
*s
)
634 EEprom9346
*eeprom
= &s
->eeprom
;
641 void prom9346_set_wire(RTL8139State
*s
, int eecs
, int eesk
, int eedi
)
643 EEprom9346
*eeprom
= &s
->eeprom
;
644 uint8_t old_eecs
= eeprom
->eecs
;
645 uint8_t old_eesk
= eeprom
->eesk
;
651 DEBUG_PRINT(("eeprom: +++ wires CS=%d SK=%d DI=%d DO=%d\n",
652 eeprom
->eecs
, eeprom
->eesk
, eeprom
->eedi
, eeprom
->eedo
));
654 if (!old_eecs
&& eecs
)
656 /* Synchronize start */
660 eeprom
->mode
= Chip9346_enter_command_mode
;
662 DEBUG_PRINT(("=== eeprom: begin access, enter command mode\n"));
667 DEBUG_PRINT(("=== eeprom: end access\n"));
671 if (!old_eesk
&& eesk
)
674 prom9346_shift_clock(eeprom
);
678 static void rtl8139_update_irq(RTL8139State
*s
)
681 isr
= (s
->IntrStatus
& s
->IntrMask
) & 0xffff;
683 DEBUG_PRINT(("RTL8139: Set IRQ line %d to %d (%04x %04x)\n",
684 s
->irq
, isr
? 1 : 0, s
->IntrStatus
, s
->IntrMask
));
688 pci_set_irq(s
->pci_dev
, 0, (isr
!= 0));
691 pic_set_irq(s
->irq
, (isr
!= 0));
695 #define POLYNOMIAL 0x04c11db6
699 static int compute_mcast_idx(const uint8_t *ep
)
706 for (i
= 0; i
< 6; i
++) {
708 for (j
= 0; j
< 8; j
++) {
709 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
713 crc
= ((crc
^ POLYNOMIAL
) | carry
);
719 static int rtl8139_RxWrap(RTL8139State
*s
)
721 /* wrapping enabled; assume 1.5k more buffer space if size < 65536 */
722 return (s
->RxConfig
& (1 << 7));
725 static int rtl8139_receiver_enabled(RTL8139State
*s
)
727 return s
->bChipCmdState
& CmdRxEnb
;
730 static int rtl8139_transmitter_enabled(RTL8139State
*s
)
732 return s
->bChipCmdState
& CmdTxEnb
;
735 static int rtl8139_cp_receiver_enabled(RTL8139State
*s
)
737 return s
->CpCmd
& CPlusRxEnb
;
740 static int rtl8139_cp_transmitter_enabled(RTL8139State
*s
)
742 return s
->CpCmd
& CPlusTxEnb
;
745 static void rtl8139_write_buffer(RTL8139State
*s
, const void *buf
, int size
)
747 if (s
->RxBufAddr
+ size
> s
->RxBufferSize
)
749 int wrapped
= MOD2(s
->RxBufAddr
+ size
, s
->RxBufferSize
);
751 /* write packet data */
752 if (wrapped
&& s
->RxBufferSize
< 65536 && !rtl8139_RxWrap(s
))
754 DEBUG_PRINT((">>> RTL8139: rx packet wrapped in buffer at %d\n", size
-wrapped
));
758 cpu_physical_memory_write( s
->RxBuf
+ s
->RxBufAddr
,
762 /* reset buffer pointer */
765 cpu_physical_memory_write( s
->RxBuf
+ s
->RxBufAddr
,
766 buf
+ (size
-wrapped
), wrapped
);
768 s
->RxBufAddr
= wrapped
;
774 /* non-wrapping path or overwrapping enabled */
775 cpu_physical_memory_write( s
->RxBuf
+ s
->RxBufAddr
, buf
, size
);
777 s
->RxBufAddr
+= size
;
780 #define MIN_BUF_SIZE 60
781 static inline target_phys_addr_t
rtl8139_addr64(uint32_t low
, uint32_t high
)
783 #if TARGET_PHYS_ADDR_BITS > 32
784 return low
| ((target_phys_addr_t
)high
<< 32);
790 static int rtl8139_can_receive(void *opaque
)
792 RTL8139State
*s
= opaque
;
795 /* Recieve (drop) packets if card is disabled. */
796 if (!s
->clock_enabled
)
798 if (!rtl8139_receiver_enabled(s
))
801 if (rtl8139_cp_receiver_enabled(s
)) {
802 /* ??? Flow control not implemented in c+ mode.
803 This is a hack to work around slirp deficiencies anyway. */
806 avail
= MOD2(s
->RxBufferSize
+ s
->RxBufPtr
- s
->RxBufAddr
,
808 return (avail
== 0 || avail
>= 1514);
812 static void rtl8139_do_receive(void *opaque
, const uint8_t *buf
, int size
, int do_interrupt
)
814 RTL8139State
*s
= opaque
;
816 uint32_t packet_header
= 0;
819 static const uint8_t broadcast_macaddr
[6] =
820 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
822 DEBUG_PRINT((">>> RTL8139: received len=%d\n", size
));
824 /* test if board clock is stopped */
825 if (!s
->clock_enabled
)
827 DEBUG_PRINT(("RTL8139: stopped ==========================\n"));
831 /* first check if receiver is enabled */
833 if (!rtl8139_receiver_enabled(s
))
835 DEBUG_PRINT(("RTL8139: receiver disabled ================\n"));
839 /* XXX: check this */
840 if (s
->RxConfig
& AcceptAllPhys
) {
841 /* promiscuous: receive all */
842 DEBUG_PRINT((">>> RTL8139: packet received in promiscuous mode\n"));
845 if (!memcmp(buf
, broadcast_macaddr
, 6)) {
846 /* broadcast address */
847 if (!(s
->RxConfig
& AcceptBroadcast
))
849 DEBUG_PRINT((">>> RTL8139: broadcast packet rejected\n"));
851 /* update tally counter */
852 ++s
->tally_counters
.RxERR
;
857 packet_header
|= RxBroadcast
;
859 DEBUG_PRINT((">>> RTL8139: broadcast packet received\n"));
861 /* update tally counter */
862 ++s
->tally_counters
.RxOkBrd
;
864 } else if (buf
[0] & 0x01) {
866 if (!(s
->RxConfig
& AcceptMulticast
))
868 DEBUG_PRINT((">>> RTL8139: multicast packet rejected\n"));
870 /* update tally counter */
871 ++s
->tally_counters
.RxERR
;
876 int mcast_idx
= compute_mcast_idx(buf
);
878 if (!(s
->mult
[mcast_idx
>> 3] & (1 << (mcast_idx
& 7))))
880 DEBUG_PRINT((">>> RTL8139: multicast address mismatch\n"));
882 /* update tally counter */
883 ++s
->tally_counters
.RxERR
;
888 packet_header
|= RxMulticast
;
890 DEBUG_PRINT((">>> RTL8139: multicast packet received\n"));
892 /* update tally counter */
893 ++s
->tally_counters
.RxOkMul
;
895 } else if (s
->phys
[0] == buf
[0] &&
896 s
->phys
[1] == buf
[1] &&
897 s
->phys
[2] == buf
[2] &&
898 s
->phys
[3] == buf
[3] &&
899 s
->phys
[4] == buf
[4] &&
900 s
->phys
[5] == buf
[5]) {
902 if (!(s
->RxConfig
& AcceptMyPhys
))
904 DEBUG_PRINT((">>> RTL8139: rejecting physical address matching packet\n"));
906 /* update tally counter */
907 ++s
->tally_counters
.RxERR
;
912 packet_header
|= RxPhysical
;
914 DEBUG_PRINT((">>> RTL8139: physical address matching packet received\n"));
916 /* update tally counter */
917 ++s
->tally_counters
.RxOkPhy
;
921 DEBUG_PRINT((">>> RTL8139: unknown packet\n"));
923 /* update tally counter */
924 ++s
->tally_counters
.RxERR
;
930 /* if too small buffer, then expand it */
931 if (size
< MIN_BUF_SIZE
) {
932 memcpy(buf1
, buf
, size
);
933 memset(buf1
+ size
, 0, MIN_BUF_SIZE
- size
);
938 if (rtl8139_cp_receiver_enabled(s
))
940 DEBUG_PRINT(("RTL8139: in C+ Rx mode ================\n"));
942 /* begin C+ receiver mode */
944 /* w0 ownership flag */
945 #define CP_RX_OWN (1<<31)
946 /* w0 end of ring flag */
947 #define CP_RX_EOR (1<<30)
948 /* w0 bits 0...12 : buffer size */
949 #define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1)
950 /* w1 tag available flag */
951 #define CP_RX_TAVA (1<<16)
952 /* w1 bits 0...15 : VLAN tag */
953 #define CP_RX_VLAN_TAG_MASK ((1<<16) - 1)
954 /* w2 low 32bit of Rx buffer ptr */
955 /* w3 high 32bit of Rx buffer ptr */
957 int descriptor
= s
->currCPlusRxDesc
;
958 target_phys_addr_t cplus_rx_ring_desc
;
960 cplus_rx_ring_desc
= rtl8139_addr64(s
->RxRingAddrLO
, s
->RxRingAddrHI
);
961 cplus_rx_ring_desc
+= 16 * descriptor
;
963 DEBUG_PRINT(("RTL8139: +++ C+ mode reading RX descriptor %d from host memory at %08x %08x = %016" PRIx64
"\n",
964 descriptor
, s
->RxRingAddrHI
, s
->RxRingAddrLO
, (uint64_t)cplus_rx_ring_desc
));
966 uint32_t val
, rxdw0
,rxdw1
,rxbufLO
,rxbufHI
;
968 cpu_physical_memory_read(cplus_rx_ring_desc
, (uint8_t *)&val
, 4);
969 rxdw0
= le32_to_cpu(val
);
970 cpu_physical_memory_read(cplus_rx_ring_desc
+4, (uint8_t *)&val
, 4);
971 rxdw1
= le32_to_cpu(val
);
972 cpu_physical_memory_read(cplus_rx_ring_desc
+8, (uint8_t *)&val
, 4);
973 rxbufLO
= le32_to_cpu(val
);
974 cpu_physical_memory_read(cplus_rx_ring_desc
+12, (uint8_t *)&val
, 4);
975 rxbufHI
= le32_to_cpu(val
);
977 DEBUG_PRINT(("RTL8139: +++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
979 rxdw0
, rxdw1
, rxbufLO
, rxbufHI
));
981 if (!(rxdw0
& CP_RX_OWN
))
983 DEBUG_PRINT(("RTL8139: C+ Rx mode : descriptor %d is owned by host\n", descriptor
));
985 s
->IntrStatus
|= RxOverflow
;
988 /* update tally counter */
989 ++s
->tally_counters
.RxERR
;
990 ++s
->tally_counters
.MissPkt
;
992 rtl8139_update_irq(s
);
996 uint32_t rx_space
= rxdw0
& CP_RX_BUFFER_SIZE_MASK
;
998 /* TODO: scatter the packet over available receive ring descriptors space */
1000 if (size
+4 > rx_space
)
1002 DEBUG_PRINT(("RTL8139: C+ Rx mode : descriptor %d size %d received %d + 4\n",
1003 descriptor
, rx_space
, size
));
1005 s
->IntrStatus
|= RxOverflow
;
1008 /* update tally counter */
1009 ++s
->tally_counters
.RxERR
;
1010 ++s
->tally_counters
.MissPkt
;
1012 rtl8139_update_irq(s
);
1016 target_phys_addr_t rx_addr
= rtl8139_addr64(rxbufLO
, rxbufHI
);
1018 /* receive/copy to target memory */
1019 cpu_physical_memory_write( rx_addr
, buf
, size
);
1021 if (s
->CpCmd
& CPlusRxChkSum
)
1023 /* do some packet checksumming */
1026 /* write checksum */
1027 #if defined (RTL8139_CALCULATE_RXCRC)
1028 val
= cpu_to_le32(crc32(~0, buf
, size
));
1032 cpu_physical_memory_write( rx_addr
+size
, (uint8_t *)&val
, 4);
1034 /* first segment of received packet flag */
1035 #define CP_RX_STATUS_FS (1<<29)
1036 /* last segment of received packet flag */
1037 #define CP_RX_STATUS_LS (1<<28)
1038 /* multicast packet flag */
1039 #define CP_RX_STATUS_MAR (1<<26)
1040 /* physical-matching packet flag */
1041 #define CP_RX_STATUS_PAM (1<<25)
1042 /* broadcast packet flag */
1043 #define CP_RX_STATUS_BAR (1<<24)
1044 /* runt packet flag */
1045 #define CP_RX_STATUS_RUNT (1<<19)
1046 /* crc error flag */
1047 #define CP_RX_STATUS_CRC (1<<18)
1048 /* IP checksum error flag */
1049 #define CP_RX_STATUS_IPF (1<<15)
1050 /* UDP checksum error flag */
1051 #define CP_RX_STATUS_UDPF (1<<14)
1052 /* TCP checksum error flag */
1053 #define CP_RX_STATUS_TCPF (1<<13)
1055 /* transfer ownership to target */
1056 rxdw0
&= ~CP_RX_OWN
;
1058 /* set first segment bit */
1059 rxdw0
|= CP_RX_STATUS_FS
;
1061 /* set last segment bit */
1062 rxdw0
|= CP_RX_STATUS_LS
;
1064 /* set received packet type flags */
1065 if (packet_header
& RxBroadcast
)
1066 rxdw0
|= CP_RX_STATUS_BAR
;
1067 if (packet_header
& RxMulticast
)
1068 rxdw0
|= CP_RX_STATUS_MAR
;
1069 if (packet_header
& RxPhysical
)
1070 rxdw0
|= CP_RX_STATUS_PAM
;
1072 /* set received size */
1073 rxdw0
&= ~CP_RX_BUFFER_SIZE_MASK
;
1076 /* reset VLAN tag flag */
1077 rxdw1
&= ~CP_RX_TAVA
;
1079 /* update ring data */
1080 val
= cpu_to_le32(rxdw0
);
1081 cpu_physical_memory_write(cplus_rx_ring_desc
, (uint8_t *)&val
, 4);
1082 val
= cpu_to_le32(rxdw1
);
1083 cpu_physical_memory_write(cplus_rx_ring_desc
+4, (uint8_t *)&val
, 4);
1085 /* update tally counter */
1086 ++s
->tally_counters
.RxOk
;
1088 /* seek to next Rx descriptor */
1089 if (rxdw0
& CP_RX_EOR
)
1091 s
->currCPlusRxDesc
= 0;
1095 ++s
->currCPlusRxDesc
;
1098 DEBUG_PRINT(("RTL8139: done C+ Rx mode ----------------\n"));
1103 DEBUG_PRINT(("RTL8139: in ring Rx mode ================\n"));
1105 /* begin ring receiver mode */
1106 int avail
= MOD2(s
->RxBufferSize
+ s
->RxBufPtr
- s
->RxBufAddr
, s
->RxBufferSize
);
1108 /* if receiver buffer is empty then avail == 0 */
1110 if (avail
!= 0 && size
+ 8 >= avail
)
1112 DEBUG_PRINT(("rx overflow: rx buffer length %d head 0x%04x read 0x%04x === available 0x%04x need 0x%04x\n",
1113 s
->RxBufferSize
, s
->RxBufAddr
, s
->RxBufPtr
, avail
, size
+ 8));
1115 s
->IntrStatus
|= RxOverflow
;
1117 rtl8139_update_irq(s
);
1121 packet_header
|= RxStatusOK
;
1123 packet_header
|= (((size
+4) << 16) & 0xffff0000);
1126 uint32_t val
= cpu_to_le32(packet_header
);
1128 rtl8139_write_buffer(s
, (uint8_t *)&val
, 4);
1130 rtl8139_write_buffer(s
, buf
, size
);
1132 /* write checksum */
1133 #if defined (RTL8139_CALCULATE_RXCRC)
1134 val
= cpu_to_le32(crc32(~0, buf
, size
));
1139 rtl8139_write_buffer(s
, (uint8_t *)&val
, 4);
1141 /* correct buffer write pointer */
1142 s
->RxBufAddr
= MOD2((s
->RxBufAddr
+ 3) & ~0x3, s
->RxBufferSize
);
1144 /* now we can signal we have received something */
1146 DEBUG_PRINT((" received: rx buffer length %d head 0x%04x read 0x%04x\n",
1147 s
->RxBufferSize
, s
->RxBufAddr
, s
->RxBufPtr
));
1150 s
->IntrStatus
|= RxOK
;
1154 rtl8139_update_irq(s
);
1158 static void rtl8139_receive(void *opaque
, const uint8_t *buf
, int size
)
1160 rtl8139_do_receive(opaque
, buf
, size
, 1);
1163 static void rtl8139_reset_rxring(RTL8139State
*s
, uint32_t bufferSize
)
1165 s
->RxBufferSize
= bufferSize
;
1170 static void rtl8139_reset(RTL8139State
*s
)
1174 /* restore MAC address */
1175 memcpy(s
->phys
, s
->macaddr
, 6);
1177 /* reset interrupt mask */
1181 rtl8139_update_irq(s
);
1183 /* prepare eeprom */
1184 s
->eeprom
.contents
[0] = 0x8129;
1186 // PCI vendor and device ID should be mirrored here
1187 s
->eeprom
.contents
[1] = 0x10ec;
1188 s
->eeprom
.contents
[2] = 0x8139;
1190 memcpy(&s
->eeprom
.contents
[7], s
->macaddr
, 6);
1192 /* mark all status registers as owned by host */
1193 for (i
= 0; i
< 4; ++i
)
1195 s
->TxStatus
[i
] = TxHostOwns
;
1199 s
->currCPlusRxDesc
= 0;
1200 s
->currCPlusTxDesc
= 0;
1202 s
->RxRingAddrLO
= 0;
1203 s
->RxRingAddrHI
= 0;
1207 rtl8139_reset_rxring(s
, 8192);
1213 // s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139 HasHltClk
1214 s
->clock_enabled
= 0;
1216 s
->TxConfig
|= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake
1217 s
->clock_enabled
= 1;
1220 s
->bChipCmdState
= CmdReset
; /* RxBufEmpty bit is calculated on read from ChipCmd */;
1222 /* set initial state data */
1223 s
->Config0
= 0x0; /* No boot ROM */
1224 s
->Config1
= 0xC; /* IO mapped and MEM mapped registers available */
1225 s
->Config3
= 0x1; /* fast back-to-back compatible */
1228 s
->CSCR
= CSCR_F_LINK_100
| CSCR_HEART_BIT
| CSCR_LD
;
1230 s
->CpCmd
= 0x0; /* reset C+ mode */
1232 // s->BasicModeCtrl = 0x3100; // 100Mbps, full duplex, autonegotiation
1233 // s->BasicModeCtrl = 0x2100; // 100Mbps, full duplex
1234 s
->BasicModeCtrl
= 0x1000; // autonegotiation
1236 s
->BasicModeStatus
= 0x7809;
1237 //s->BasicModeStatus |= 0x0040; /* UTP medium */
1238 s
->BasicModeStatus
|= 0x0020; /* autonegotiation completed */
1239 s
->BasicModeStatus
|= 0x0004; /* link is up */
1241 s
->NWayAdvert
= 0x05e1; /* all modes, full duplex */
1242 s
->NWayLPAR
= 0x05e1; /* all modes, full duplex */
1243 s
->NWayExpansion
= 0x0001; /* autonegotiation supported */
1245 /* also reset timer and disable timer interrupt */
1250 /* reset tally counters */
1251 RTL8139TallyCounters_clear(&s
->tally_counters
);
1254 void RTL8139TallyCounters_clear(RTL8139TallyCounters
* counters
)
1258 counters
->TxERR
= 0;
1259 counters
->RxERR
= 0;
1260 counters
->MissPkt
= 0;
1262 counters
->Tx1Col
= 0;
1263 counters
->TxMCol
= 0;
1264 counters
->RxOkPhy
= 0;
1265 counters
->RxOkBrd
= 0;
1266 counters
->RxOkMul
= 0;
1267 counters
->TxAbt
= 0;
1268 counters
->TxUndrn
= 0;
1271 static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr
, RTL8139TallyCounters
* tally_counters
)
1277 val64
= cpu_to_le64(tally_counters
->TxOk
);
1278 cpu_physical_memory_write(tc_addr
+ 0, (uint8_t *)&val64
, 8);
1280 val64
= cpu_to_le64(tally_counters
->RxOk
);
1281 cpu_physical_memory_write(tc_addr
+ 8, (uint8_t *)&val64
, 8);
1283 val64
= cpu_to_le64(tally_counters
->TxERR
);
1284 cpu_physical_memory_write(tc_addr
+ 16, (uint8_t *)&val64
, 8);
1286 val32
= cpu_to_le32(tally_counters
->RxERR
);
1287 cpu_physical_memory_write(tc_addr
+ 24, (uint8_t *)&val32
, 4);
1289 val16
= cpu_to_le16(tally_counters
->MissPkt
);
1290 cpu_physical_memory_write(tc_addr
+ 28, (uint8_t *)&val16
, 2);
1292 val16
= cpu_to_le16(tally_counters
->FAE
);
1293 cpu_physical_memory_write(tc_addr
+ 30, (uint8_t *)&val16
, 2);
1295 val32
= cpu_to_le32(tally_counters
->Tx1Col
);
1296 cpu_physical_memory_write(tc_addr
+ 32, (uint8_t *)&val32
, 4);
1298 val32
= cpu_to_le32(tally_counters
->TxMCol
);
1299 cpu_physical_memory_write(tc_addr
+ 36, (uint8_t *)&val32
, 4);
1301 val64
= cpu_to_le64(tally_counters
->RxOkPhy
);
1302 cpu_physical_memory_write(tc_addr
+ 40, (uint8_t *)&val64
, 8);
1304 val64
= cpu_to_le64(tally_counters
->RxOkBrd
);
1305 cpu_physical_memory_write(tc_addr
+ 48, (uint8_t *)&val64
, 8);
1307 val32
= cpu_to_le32(tally_counters
->RxOkMul
);
1308 cpu_physical_memory_write(tc_addr
+ 56, (uint8_t *)&val32
, 4);
1310 val16
= cpu_to_le16(tally_counters
->TxAbt
);
1311 cpu_physical_memory_write(tc_addr
+ 60, (uint8_t *)&val16
, 2);
1313 val16
= cpu_to_le16(tally_counters
->TxUndrn
);
1314 cpu_physical_memory_write(tc_addr
+ 62, (uint8_t *)&val16
, 2);
1317 /* Loads values of tally counters from VM state file */
1318 static void RTL8139TallyCounters_load(QEMUFile
* f
, RTL8139TallyCounters
*tally_counters
)
1320 qemu_get_be64s(f
, &tally_counters
->TxOk
);
1321 qemu_get_be64s(f
, &tally_counters
->RxOk
);
1322 qemu_get_be64s(f
, &tally_counters
->TxERR
);
1323 qemu_get_be32s(f
, &tally_counters
->RxERR
);
1324 qemu_get_be16s(f
, &tally_counters
->MissPkt
);
1325 qemu_get_be16s(f
, &tally_counters
->FAE
);
1326 qemu_get_be32s(f
, &tally_counters
->Tx1Col
);
1327 qemu_get_be32s(f
, &tally_counters
->TxMCol
);
1328 qemu_get_be64s(f
, &tally_counters
->RxOkPhy
);
1329 qemu_get_be64s(f
, &tally_counters
->RxOkBrd
);
1330 qemu_get_be32s(f
, &tally_counters
->RxOkMul
);
1331 qemu_get_be16s(f
, &tally_counters
->TxAbt
);
1332 qemu_get_be16s(f
, &tally_counters
->TxUndrn
);
1335 /* Saves values of tally counters to VM state file */
1336 static void RTL8139TallyCounters_save(QEMUFile
* f
, RTL8139TallyCounters
*tally_counters
)
1338 qemu_put_be64s(f
, &tally_counters
->TxOk
);
1339 qemu_put_be64s(f
, &tally_counters
->RxOk
);
1340 qemu_put_be64s(f
, &tally_counters
->TxERR
);
1341 qemu_put_be32s(f
, &tally_counters
->RxERR
);
1342 qemu_put_be16s(f
, &tally_counters
->MissPkt
);
1343 qemu_put_be16s(f
, &tally_counters
->FAE
);
1344 qemu_put_be32s(f
, &tally_counters
->Tx1Col
);
1345 qemu_put_be32s(f
, &tally_counters
->TxMCol
);
1346 qemu_put_be64s(f
, &tally_counters
->RxOkPhy
);
1347 qemu_put_be64s(f
, &tally_counters
->RxOkBrd
);
1348 qemu_put_be32s(f
, &tally_counters
->RxOkMul
);
1349 qemu_put_be16s(f
, &tally_counters
->TxAbt
);
1350 qemu_put_be16s(f
, &tally_counters
->TxUndrn
);
1353 static void rtl8139_ChipCmd_write(RTL8139State
*s
, uint32_t val
)
1357 DEBUG_PRINT(("RTL8139: ChipCmd write val=0x%08x\n", val
));
1361 DEBUG_PRINT(("RTL8139: ChipCmd reset\n"));
1366 DEBUG_PRINT(("RTL8139: ChipCmd enable receiver\n"));
1370 DEBUG_PRINT(("RTL8139: ChipCmd enable transmitter\n"));
1373 /* mask unwriteable bits */
1374 val
= SET_MASKED(val
, 0xe3, s
->bChipCmdState
);
1376 /* Deassert reset pin before next read */
1379 s
->bChipCmdState
= val
;
1382 static int rtl8139_RxBufferEmpty(RTL8139State
*s
)
1384 int unread
= MOD2(s
->RxBufferSize
+ s
->RxBufAddr
- s
->RxBufPtr
, s
->RxBufferSize
);
1388 DEBUG_PRINT(("RTL8139: receiver buffer data available 0x%04x\n", unread
));
1392 DEBUG_PRINT(("RTL8139: receiver buffer is empty\n"));
1397 static uint32_t rtl8139_ChipCmd_read(RTL8139State
*s
)
1399 uint32_t ret
= s
->bChipCmdState
;
1401 if (rtl8139_RxBufferEmpty(s
))
1404 DEBUG_PRINT(("RTL8139: ChipCmd read val=0x%04x\n", ret
));
1409 static void rtl8139_CpCmd_write(RTL8139State
*s
, uint32_t val
)
1413 DEBUG_PRINT(("RTL8139C+ command register write(w) val=0x%04x\n", val
));
1415 /* mask unwriteable bits */
1416 val
= SET_MASKED(val
, 0xff84, s
->CpCmd
);
1421 static uint32_t rtl8139_CpCmd_read(RTL8139State
*s
)
1423 uint32_t ret
= s
->CpCmd
;
1425 DEBUG_PRINT(("RTL8139C+ command register read(w) val=0x%04x\n", ret
));
1430 static void rtl8139_IntrMitigate_write(RTL8139State
*s
, uint32_t val
)
1432 DEBUG_PRINT(("RTL8139C+ IntrMitigate register write(w) val=0x%04x\n", val
));
1435 static uint32_t rtl8139_IntrMitigate_read(RTL8139State
*s
)
1439 DEBUG_PRINT(("RTL8139C+ IntrMitigate register read(w) val=0x%04x\n", ret
));
1444 int rtl8139_config_writeable(RTL8139State
*s
)
1446 if (s
->Cfg9346
& Cfg9346_Unlock
)
1451 DEBUG_PRINT(("RTL8139: Configuration registers are write-protected\n"));
1456 static void rtl8139_BasicModeCtrl_write(RTL8139State
*s
, uint32_t val
)
1460 DEBUG_PRINT(("RTL8139: BasicModeCtrl register write(w) val=0x%04x\n", val
));
1462 /* mask unwriteable bits */
1463 uint32 mask
= 0x4cff;
1465 if (1 || !rtl8139_config_writeable(s
))
1467 /* Speed setting and autonegotiation enable bits are read-only */
1469 /* Duplex mode setting is read-only */
1473 val
= SET_MASKED(val
, mask
, s
->BasicModeCtrl
);
1475 s
->BasicModeCtrl
= val
;
1478 static uint32_t rtl8139_BasicModeCtrl_read(RTL8139State
*s
)
1480 uint32_t ret
= s
->BasicModeCtrl
;
1482 DEBUG_PRINT(("RTL8139: BasicModeCtrl register read(w) val=0x%04x\n", ret
));
1487 static void rtl8139_BasicModeStatus_write(RTL8139State
*s
, uint32_t val
)
1491 DEBUG_PRINT(("RTL8139: BasicModeStatus register write(w) val=0x%04x\n", val
));
1493 /* mask unwriteable bits */
1494 val
= SET_MASKED(val
, 0xff3f, s
->BasicModeStatus
);
1496 s
->BasicModeStatus
= val
;
1499 static uint32_t rtl8139_BasicModeStatus_read(RTL8139State
*s
)
1501 uint32_t ret
= s
->BasicModeStatus
;
1503 DEBUG_PRINT(("RTL8139: BasicModeStatus register read(w) val=0x%04x\n", ret
));
1508 static void rtl8139_Cfg9346_write(RTL8139State
*s
, uint32_t val
)
1512 DEBUG_PRINT(("RTL8139: Cfg9346 write val=0x%02x\n", val
));
1514 /* mask unwriteable bits */
1515 val
= SET_MASKED(val
, 0x31, s
->Cfg9346
);
1517 uint32_t opmode
= val
& 0xc0;
1518 uint32_t eeprom_val
= val
& 0xf;
1520 if (opmode
== 0x80) {
1522 int eecs
= (eeprom_val
& 0x08)?1:0;
1523 int eesk
= (eeprom_val
& 0x04)?1:0;
1524 int eedi
= (eeprom_val
& 0x02)?1:0;
1525 prom9346_set_wire(s
, eecs
, eesk
, eedi
);
1526 } else if (opmode
== 0x40) {
1535 static uint32_t rtl8139_Cfg9346_read(RTL8139State
*s
)
1537 uint32_t ret
= s
->Cfg9346
;
1539 uint32_t opmode
= ret
& 0xc0;
1544 int eedo
= prom9346_get_wire(s
);
1555 DEBUG_PRINT(("RTL8139: Cfg9346 read val=0x%02x\n", ret
));
1560 static void rtl8139_Config0_write(RTL8139State
*s
, uint32_t val
)
1564 DEBUG_PRINT(("RTL8139: Config0 write val=0x%02x\n", val
));
1566 if (!rtl8139_config_writeable(s
))
1569 /* mask unwriteable bits */
1570 val
= SET_MASKED(val
, 0xf8, s
->Config0
);
1575 static uint32_t rtl8139_Config0_read(RTL8139State
*s
)
1577 uint32_t ret
= s
->Config0
;
1579 DEBUG_PRINT(("RTL8139: Config0 read val=0x%02x\n", ret
));
1584 static void rtl8139_Config1_write(RTL8139State
*s
, uint32_t val
)
1588 DEBUG_PRINT(("RTL8139: Config1 write val=0x%02x\n", val
));
1590 if (!rtl8139_config_writeable(s
))
1593 /* mask unwriteable bits */
1594 val
= SET_MASKED(val
, 0xC, s
->Config1
);
1599 static uint32_t rtl8139_Config1_read(RTL8139State
*s
)
1601 uint32_t ret
= s
->Config1
;
1603 DEBUG_PRINT(("RTL8139: Config1 read val=0x%02x\n", ret
));
1608 static void rtl8139_Config3_write(RTL8139State
*s
, uint32_t val
)
1612 DEBUG_PRINT(("RTL8139: Config3 write val=0x%02x\n", val
));
1614 if (!rtl8139_config_writeable(s
))
1617 /* mask unwriteable bits */
1618 val
= SET_MASKED(val
, 0x8F, s
->Config3
);
1623 static uint32_t rtl8139_Config3_read(RTL8139State
*s
)
1625 uint32_t ret
= s
->Config3
;
1627 DEBUG_PRINT(("RTL8139: Config3 read val=0x%02x\n", ret
));
1632 static void rtl8139_Config4_write(RTL8139State
*s
, uint32_t val
)
1636 DEBUG_PRINT(("RTL8139: Config4 write val=0x%02x\n", val
));
1638 if (!rtl8139_config_writeable(s
))
1641 /* mask unwriteable bits */
1642 val
= SET_MASKED(val
, 0x0a, s
->Config4
);
1647 static uint32_t rtl8139_Config4_read(RTL8139State
*s
)
1649 uint32_t ret
= s
->Config4
;
1651 DEBUG_PRINT(("RTL8139: Config4 read val=0x%02x\n", ret
));
1656 static void rtl8139_Config5_write(RTL8139State
*s
, uint32_t val
)
1660 DEBUG_PRINT(("RTL8139: Config5 write val=0x%02x\n", val
));
1662 /* mask unwriteable bits */
1663 val
= SET_MASKED(val
, 0x80, s
->Config5
);
1668 static uint32_t rtl8139_Config5_read(RTL8139State
*s
)
1670 uint32_t ret
= s
->Config5
;
1672 DEBUG_PRINT(("RTL8139: Config5 read val=0x%02x\n", ret
));
1677 static void rtl8139_TxConfig_write(RTL8139State
*s
, uint32_t val
)
1679 if (!rtl8139_transmitter_enabled(s
))
1681 DEBUG_PRINT(("RTL8139: transmitter disabled; no TxConfig write val=0x%08x\n", val
));
1685 DEBUG_PRINT(("RTL8139: TxConfig write val=0x%08x\n", val
));
1687 val
= SET_MASKED(val
, TxVersionMask
| 0x8070f80f, s
->TxConfig
);
1692 static void rtl8139_TxConfig_writeb(RTL8139State
*s
, uint32_t val
)
1694 DEBUG_PRINT(("RTL8139C TxConfig via write(b) val=0x%02x\n", val
));
1696 uint32_t tc
= s
->TxConfig
;
1698 tc
|= (val
& 0x000000FF);
1699 rtl8139_TxConfig_write(s
, tc
);
1702 static uint32_t rtl8139_TxConfig_read(RTL8139State
*s
)
1704 uint32_t ret
= s
->TxConfig
;
1706 DEBUG_PRINT(("RTL8139: TxConfig read val=0x%04x\n", ret
));
1711 static void rtl8139_RxConfig_write(RTL8139State
*s
, uint32_t val
)
1713 DEBUG_PRINT(("RTL8139: RxConfig write val=0x%08x\n", val
));
1715 /* mask unwriteable bits */
1716 val
= SET_MASKED(val
, 0xf0fc0040, s
->RxConfig
);
1720 /* reset buffer size and read/write pointers */
1721 rtl8139_reset_rxring(s
, 8192 << ((s
->RxConfig
>> 11) & 0x3));
1723 DEBUG_PRINT(("RTL8139: RxConfig write reset buffer size to %d\n", s
->RxBufferSize
));
1726 static uint32_t rtl8139_RxConfig_read(RTL8139State
*s
)
1728 uint32_t ret
= s
->RxConfig
;
1730 DEBUG_PRINT(("RTL8139: RxConfig read val=0x%08x\n", ret
));
1735 static int rtl8139_transmit_one(RTL8139State
*s
, int descriptor
)
1737 if (!rtl8139_transmitter_enabled(s
))
1739 DEBUG_PRINT(("RTL8139: +++ cannot transmit from descriptor %d: transmitter disabled\n",
1744 if (s
->TxStatus
[descriptor
] & TxHostOwns
)
1746 DEBUG_PRINT(("RTL8139: +++ cannot transmit from descriptor %d: owned by host (%08x)\n",
1747 descriptor
, s
->TxStatus
[descriptor
]));
1751 DEBUG_PRINT(("RTL8139: +++ transmitting from descriptor %d\n", descriptor
));
1753 int txsize
= s
->TxStatus
[descriptor
] & 0x1fff;
1754 uint8_t txbuffer
[0x2000];
1756 DEBUG_PRINT(("RTL8139: +++ transmit reading %d bytes from host memory at 0x%08x\n",
1757 txsize
, s
->TxAddr
[descriptor
]));
1759 cpu_physical_memory_read(s
->TxAddr
[descriptor
], txbuffer
, txsize
);
1761 /* Mark descriptor as transferred */
1762 s
->TxStatus
[descriptor
] |= TxHostOwns
;
1763 s
->TxStatus
[descriptor
] |= TxStatOK
;
1765 if (TxLoopBack
== (s
->TxConfig
& TxLoopBack
))
1767 DEBUG_PRINT(("RTL8139: +++ transmit loopback mode\n"));
1768 rtl8139_do_receive(s
, txbuffer
, txsize
, 0);
1772 qemu_send_packet(s
->vc
, txbuffer
, txsize
);
1775 DEBUG_PRINT(("RTL8139: +++ transmitted %d bytes from descriptor %d\n", txsize
, descriptor
));
1777 /* update interrupt */
1778 s
->IntrStatus
|= TxOK
;
1779 rtl8139_update_irq(s
);
1784 static int rtl8139_cplus_transmit_one(RTL8139State
*s
)
1786 if (!rtl8139_transmitter_enabled(s
))
1788 DEBUG_PRINT(("RTL8139: +++ C+ mode: transmitter disabled\n"));
1792 if (!rtl8139_cp_transmitter_enabled(s
))
1794 DEBUG_PRINT(("RTL8139: +++ C+ mode: C+ transmitter disabled\n"));
1798 int descriptor
= s
->currCPlusTxDesc
;
1800 target_phys_addr_t cplus_tx_ring_desc
=
1801 rtl8139_addr64(s
->TxAddr
[0], s
->TxAddr
[1]);
1803 /* Normal priority ring */
1804 cplus_tx_ring_desc
+= 16 * descriptor
;
1806 DEBUG_PRINT(("RTL8139: +++ C+ mode reading TX descriptor %d from host memory at %08x0x%08x = 0x%8lx\n",
1807 descriptor
, s
->TxAddr
[1], s
->TxAddr
[0], cplus_tx_ring_desc
));
1809 uint32_t val
, txdw0
,txdw1
,txbufLO
,txbufHI
;
1811 cpu_physical_memory_read(cplus_tx_ring_desc
, (uint8_t *)&val
, 4);
1812 txdw0
= le32_to_cpu(val
);
1813 cpu_physical_memory_read(cplus_tx_ring_desc
+4, (uint8_t *)&val
, 4);
1814 txdw1
= le32_to_cpu(val
);
1815 cpu_physical_memory_read(cplus_tx_ring_desc
+8, (uint8_t *)&val
, 4);
1816 txbufLO
= le32_to_cpu(val
);
1817 cpu_physical_memory_read(cplus_tx_ring_desc
+12, (uint8_t *)&val
, 4);
1818 txbufHI
= le32_to_cpu(val
);
1820 DEBUG_PRINT(("RTL8139: +++ C+ mode TX descriptor %d %08x %08x %08x %08x\n",
1822 txdw0
, txdw1
, txbufLO
, txbufHI
));
1824 /* w0 ownership flag */
1825 #define CP_TX_OWN (1<<31)
1826 /* w0 end of ring flag */
1827 #define CP_TX_EOR (1<<30)
1828 /* first segment of received packet flag */
1829 #define CP_TX_FS (1<<29)
1830 /* last segment of received packet flag */
1831 #define CP_TX_LS (1<<28)
1832 /* large send packet flag */
1833 #define CP_TX_LGSEN (1<<27)
1834 /* IP checksum offload flag */
1835 #define CP_TX_IPCS (1<<18)
1836 /* UDP checksum offload flag */
1837 #define CP_TX_UDPCS (1<<17)
1838 /* TCP checksum offload flag */
1839 #define CP_TX_TCPCS (1<<16)
1841 /* w0 bits 0...15 : buffer size */
1842 #define CP_TX_BUFFER_SIZE (1<<16)
1843 #define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1)
1844 /* w1 tag available flag */
1845 #define CP_RX_TAGC (1<<17)
1846 /* w1 bits 0...15 : VLAN tag */
1847 #define CP_TX_VLAN_TAG_MASK ((1<<16) - 1)
1848 /* w2 low 32bit of Rx buffer ptr */
1849 /* w3 high 32bit of Rx buffer ptr */
1851 /* set after transmission */
1852 /* FIFO underrun flag */
1853 #define CP_TX_STATUS_UNF (1<<25)
1854 /* transmit error summary flag, valid if set any of three below */
1855 #define CP_TX_STATUS_TES (1<<23)
1856 /* out-of-window collision flag */
1857 #define CP_TX_STATUS_OWC (1<<22)
1858 /* link failure flag */
1859 #define CP_TX_STATUS_LNKF (1<<21)
1860 /* excessive collisions flag */
1861 #define CP_TX_STATUS_EXC (1<<20)
1863 if (!(txdw0
& CP_TX_OWN
))
1865 DEBUG_PRINT(("RTL8139: C+ Tx mode : descriptor %d is owned by host\n", descriptor
));
1869 DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : transmitting from descriptor %d\n", descriptor
));
1871 if (txdw0
& CP_TX_FS
)
1873 DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : descriptor %d is first segment descriptor\n", descriptor
));
1875 /* reset internal buffer offset */
1876 s
->cplus_txbuffer_offset
= 0;
1879 int txsize
= txdw0
& CP_TX_BUFFER_SIZE_MASK
;
1880 target_phys_addr_t tx_addr
= rtl8139_addr64(txbufLO
, txbufHI
);
1882 /* make sure we have enough space to assemble the packet */
1883 if (!s
->cplus_txbuffer
)
1885 s
->cplus_txbuffer_len
= CP_TX_BUFFER_SIZE
;
1886 s
->cplus_txbuffer
= malloc(s
->cplus_txbuffer_len
);
1887 s
->cplus_txbuffer_offset
= 0;
1890 while (s
->cplus_txbuffer
&& s
->cplus_txbuffer_offset
+ txsize
>= s
->cplus_txbuffer_len
)
1892 s
->cplus_txbuffer_len
+= CP_TX_BUFFER_SIZE
;
1893 s
->cplus_txbuffer
= realloc(s
->cplus_txbuffer
, s
->cplus_txbuffer_len
);
1895 DEBUG_PRINT(("RTL8139: +++ C+ mode transmission buffer space changed to %d\n", s
->cplus_txbuffer_len
));
1898 if (!s
->cplus_txbuffer
)
1902 DEBUG_PRINT(("RTL8139: +++ C+ mode transmiter failed to reallocate %d bytes\n", s
->cplus_txbuffer_len
));
1904 /* update tally counter */
1905 ++s
->tally_counters
.TxERR
;
1906 ++s
->tally_counters
.TxAbt
;
1911 /* append more data to the packet */
1913 DEBUG_PRINT(("RTL8139: +++ C+ mode transmit reading %d bytes from host memory at %016" PRIx64
" to offset %d\n",
1914 txsize
, (uint64_t)tx_addr
, s
->cplus_txbuffer_offset
));
1916 cpu_physical_memory_read(tx_addr
, s
->cplus_txbuffer
+ s
->cplus_txbuffer_offset
, txsize
);
1917 s
->cplus_txbuffer_offset
+= txsize
;
1919 /* seek to next Rx descriptor */
1920 if (txdw0
& CP_TX_EOR
)
1922 s
->currCPlusTxDesc
= 0;
1926 ++s
->currCPlusTxDesc
;
1927 if (s
->currCPlusTxDesc
>= 64)
1928 s
->currCPlusTxDesc
= 0;
1931 /* transfer ownership to target */
1932 txdw0
&= ~CP_RX_OWN
;
1934 /* reset error indicator bits */
1935 txdw0
&= ~CP_TX_STATUS_UNF
;
1936 txdw0
&= ~CP_TX_STATUS_TES
;
1937 txdw0
&= ~CP_TX_STATUS_OWC
;
1938 txdw0
&= ~CP_TX_STATUS_LNKF
;
1939 txdw0
&= ~CP_TX_STATUS_EXC
;
1941 /* update ring data */
1942 val
= cpu_to_le32(txdw0
);
1943 cpu_physical_memory_write(cplus_tx_ring_desc
, (uint8_t *)&val
, 4);
1944 // val = cpu_to_le32(txdw1);
1945 // cpu_physical_memory_write(cplus_tx_ring_desc+4, &val, 4);
1947 /* Now decide if descriptor being processed is holding the last segment of packet */
1948 if (txdw0
& CP_TX_LS
)
1950 DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : descriptor %d is last segment descriptor\n", descriptor
));
1952 /* can transfer fully assembled packet */
1954 uint8_t *saved_buffer
= s
->cplus_txbuffer
;
1955 int saved_size
= s
->cplus_txbuffer_offset
;
1956 int saved_buffer_len
= s
->cplus_txbuffer_len
;
1958 /* reset the card space to protect from recursive call */
1959 s
->cplus_txbuffer
= NULL
;
1960 s
->cplus_txbuffer_offset
= 0;
1961 s
->cplus_txbuffer_len
= 0;
1963 if (txdw0
& (CP_TX_IPCS
| CP_TX_UDPCS
| CP_TX_TCPCS
))
1965 DEBUG_PRINT(("RTL8139: +++ C+ mode offloaded task checksum\n"));
1967 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
1970 /* ip packet header */
1971 register struct ip
*ip
= 0;
1974 struct mbuf local_m
;
1976 int proto
= ntohs(*(uint16_t *)(saved_buffer
+ 12));
1977 if (proto
== ETH_P_IP
)
1979 DEBUG_PRINT(("RTL8139: +++ C+ mode has IP packet\n"));
1982 local_m
.m_data
= saved_buffer
+ ETH_HLEN
;
1983 local_m
.m_len
= saved_size
- ETH_HLEN
;
1985 ip
= mtod(&local_m
, struct ip
*);
1987 if (ip
->ip_v
!= IPVERSION
) {
1988 DEBUG_PRINT(("RTL8139: +++ C+ mode packet has bad IP version %d expected %d\n", ip
->ip_v
, IPVERSION
));
1991 hlen
= ip
->ip_hl
<< 2;
1997 if (txdw0
& CP_TX_IPCS
)
1999 DEBUG_PRINT(("RTL8139: +++ C+ mode need IP checksum\n"));
2001 if (hlen
<sizeof(struct ip
) || hlen
>local_m
.m_len
) {/* min header length */
2002 /* bad packet header len */
2003 /* or packet too short */
2008 ip
->ip_sum
= cksum(&local_m
, hlen
);
2009 DEBUG_PRINT(("RTL8139: +++ C+ mode IP header len=%d checksum=%04x\n", hlen
, ip
->ip_sum
));
2013 if (txdw0
& (CP_TX_TCPCS
|CP_TX_UDPCS
))
2015 DEBUG_PRINT(("RTL8139: +++ C+ mode need TCP or UDP checksum\n"));
2017 u_int8_t ip_protocol
= ip
->ip_p
;
2018 u_int16_t ip_data_len
= ntohs(ip
->ip_len
) - hlen
;
2020 /* maximum IP header length is 60 bytes */
2021 uint8_t saved_ip_header
[60];
2022 memcpy(saved_ip_header
, local_m
.m_data
, hlen
);
2024 struct mbuf local_checksum_m
;
2026 local_checksum_m
.m_data
= local_m
.m_data
+ hlen
- 12;
2027 local_checksum_m
.m_len
= local_m
.m_len
- hlen
+ 12;
2029 /* add 4 TCP pseudoheader fields */
2030 /* copy IP source and destination fields */
2031 memcpy(local_checksum_m
.m_data
, saved_ip_header
+ 12, 8);
2033 if ((txdw0
& CP_TX_TCPCS
) && ip_protocol
== IPPROTO_TCP
)
2035 DEBUG_PRINT(("RTL8139: +++ C+ mode calculating TCP checksum for packet with %d bytes data\n", ip_data_len
));
2037 struct tcpiphdr
* p_tcpip_hdr
= (struct tcpiphdr
*)local_checksum_m
.m_data
;
2038 p_tcpip_hdr
->ti_x1
= 0;
2039 p_tcpip_hdr
->ti_pr
= IPPROTO_TCP
;
2040 p_tcpip_hdr
->ti_len
= htons(ip_data_len
);
2042 struct tcphdr
* p_tcp_hdr
= (struct tcphdr
*) (local_checksum_m
.m_data
+12);
2044 p_tcp_hdr
->th_sum
= 0;
2046 int tcp_checksum
= cksum(&local_checksum_m
, ip_data_len
+ 12);
2047 DEBUG_PRINT(("RTL8139: +++ C+ mode TCP checksum %04x\n", tcp_checksum
));
2049 p_tcp_hdr
->th_sum
= tcp_checksum
;
2051 else if ((txdw0
& CP_TX_UDPCS
) && ip_protocol
== IPPROTO_UDP
)
2053 DEBUG_PRINT(("RTL8139: +++ C+ mode calculating UDP checksum for packet with %d bytes data\n", ip_data_len
));
2055 struct udpiphdr
* p_udpip_hdr
= (struct udpiphdr
*)local_checksum_m
.m_data
;
2056 p_udpip_hdr
->ui_x1
= 0;
2057 p_udpip_hdr
->ui_pr
= IPPROTO_UDP
;
2058 p_udpip_hdr
->ui_len
= htons(ip_data_len
);
2060 struct udphdr
* p_udp_hdr
= (struct udphdr
*) (local_checksum_m
.m_data
+12);
2062 int old_csum
= p_udp_hdr
->uh_sum
;
2063 p_udp_hdr
->uh_sum
= 0;
2065 int udp_checksum
= cksum(&local_checksum_m
, ip_data_len
+ 12);
2066 DEBUG_PRINT(("RTL8139: +++ C+ mode UDP checksum %04x\n", udp_checksum
));
2068 if (old_csum
!= udp_checksum
)
2070 DEBUG_PRINT(("RTL8139: +++ C+ mode UDP checksum mismatch old=%04x new=%04x\n",
2071 old_csum
, udp_checksum
));
2074 p_udp_hdr
->uh_sum
= udp_checksum
;
2077 /* restore IP header */
2078 memcpy(local_m
.m_data
, saved_ip_header
, hlen
);
2083 /* update tally counter */
2084 ++s
->tally_counters
.TxOk
;
2086 DEBUG_PRINT(("RTL8139: +++ C+ mode transmitting %d bytes packet\n", saved_size
));
2088 if (TxLoopBack
== (s
->TxConfig
& TxLoopBack
))
2090 DEBUG_PRINT(("RTL8139: +++ C+ transmit loopback mode\n"));
2091 rtl8139_receive(s
, saved_buffer
, saved_size
);
2095 /* transmit the packet */
2096 qemu_send_packet(s
->vc
, saved_buffer
, saved_size
);
2099 /* restore card space if there was no recursion and reset offset */
2100 if (!s
->cplus_txbuffer
)
2102 s
->cplus_txbuffer
= saved_buffer
;
2103 s
->cplus_txbuffer_len
= saved_buffer_len
;
2104 s
->cplus_txbuffer_offset
= 0;
2113 DEBUG_PRINT(("RTL8139: +++ C+ mode transmission continue to next descriptor\n"));
2119 static void rtl8139_cplus_transmit(RTL8139State
*s
)
2123 while (rtl8139_cplus_transmit_one(s
))
2128 /* Mark transfer completed */
2131 DEBUG_PRINT(("RTL8139: C+ mode : transmitter queue stalled, current TxDesc = %d\n",
2132 s
->currCPlusTxDesc
));
2136 /* update interrupt status */
2137 s
->IntrStatus
|= TxOK
;
2138 rtl8139_update_irq(s
);
2142 static void rtl8139_transmit(RTL8139State
*s
)
2144 int descriptor
= s
->currTxDesc
, txcount
= 0;
2147 if (rtl8139_transmit_one(s
, descriptor
))
2154 /* Mark transfer completed */
2157 DEBUG_PRINT(("RTL8139: transmitter queue stalled, current TxDesc = %d\n", s
->currTxDesc
));
2161 static void rtl8139_TxStatus_write(RTL8139State
*s
, uint32_t txRegOffset
, uint32_t val
)
2164 int descriptor
= txRegOffset
/4;
2166 /* handle C+ transmit mode register configuration */
2168 if (rtl8139_cp_transmitter_enabled(s
))
2170 DEBUG_PRINT(("RTL8139C+ DTCCR write offset=0x%x val=0x%08x descriptor=%d\n", txRegOffset
, val
, descriptor
));
2172 /* handle Dump Tally Counters command */
2173 s
->TxStatus
[descriptor
] = val
;
2175 if (descriptor
== 0 && (val
& 0x8))
2177 target_phys_addr_t tc_addr
= rtl8139_addr64(s
->TxStatus
[0] & ~0x3f, s
->TxStatus
[1]);
2179 /* dump tally counters to specified memory location */
2180 RTL8139TallyCounters_physical_memory_write( tc_addr
, &s
->tally_counters
);
2182 /* mark dump completed */
2183 s
->TxStatus
[0] &= ~0x8;
2189 DEBUG_PRINT(("RTL8139: TxStatus write offset=0x%x val=0x%08x descriptor=%d\n", txRegOffset
, val
, descriptor
));
2191 /* mask only reserved bits */
2192 val
&= ~0xff00c000; /* these bits are reset on write */
2193 val
= SET_MASKED(val
, 0x00c00000, s
->TxStatus
[descriptor
]);
2195 s
->TxStatus
[descriptor
] = val
;
2197 /* attempt to start transmission */
2198 rtl8139_transmit(s
);
2201 static uint32_t rtl8139_TxStatus_read(RTL8139State
*s
, uint32_t txRegOffset
)
2203 uint32_t ret
= s
->TxStatus
[txRegOffset
/4];
2205 DEBUG_PRINT(("RTL8139: TxStatus read offset=0x%x val=0x%08x\n", txRegOffset
, ret
));
2210 static uint16_t rtl8139_TSAD_read(RTL8139State
*s
)
2214 /* Simulate TSAD, it is read only anyway */
2216 ret
= ((s
->TxStatus
[3] & TxStatOK
)?TSAD_TOK3
:0)
2217 |((s
->TxStatus
[2] & TxStatOK
)?TSAD_TOK2
:0)
2218 |((s
->TxStatus
[1] & TxStatOK
)?TSAD_TOK1
:0)
2219 |((s
->TxStatus
[0] & TxStatOK
)?TSAD_TOK0
:0)
2221 |((s
->TxStatus
[3] & TxUnderrun
)?TSAD_TUN3
:0)
2222 |((s
->TxStatus
[2] & TxUnderrun
)?TSAD_TUN2
:0)
2223 |((s
->TxStatus
[1] & TxUnderrun
)?TSAD_TUN1
:0)
2224 |((s
->TxStatus
[0] & TxUnderrun
)?TSAD_TUN0
:0)
2226 |((s
->TxStatus
[3] & TxAborted
)?TSAD_TABT3
:0)
2227 |((s
->TxStatus
[2] & TxAborted
)?TSAD_TABT2
:0)
2228 |((s
->TxStatus
[1] & TxAborted
)?TSAD_TABT1
:0)
2229 |((s
->TxStatus
[0] & TxAborted
)?TSAD_TABT0
:0)
2231 |((s
->TxStatus
[3] & TxHostOwns
)?TSAD_OWN3
:0)
2232 |((s
->TxStatus
[2] & TxHostOwns
)?TSAD_OWN2
:0)
2233 |((s
->TxStatus
[1] & TxHostOwns
)?TSAD_OWN1
:0)
2234 |((s
->TxStatus
[0] & TxHostOwns
)?TSAD_OWN0
:0) ;
2237 DEBUG_PRINT(("RTL8139: TSAD read val=0x%04x\n", ret
));
2242 static uint16_t rtl8139_CSCR_read(RTL8139State
*s
)
2244 uint16_t ret
= s
->CSCR
;
2246 DEBUG_PRINT(("RTL8139: CSCR read val=0x%04x\n", ret
));
2251 static void rtl8139_TxAddr_write(RTL8139State
*s
, uint32_t txAddrOffset
, uint32_t val
)
2253 DEBUG_PRINT(("RTL8139: TxAddr write offset=0x%x val=0x%08x\n", txAddrOffset
, val
));
2255 s
->TxAddr
[txAddrOffset
/4] = le32_to_cpu(val
);
2257 s
->currCPlusTxDesc
= 0;
2260 static uint32_t rtl8139_TxAddr_read(RTL8139State
*s
, uint32_t txAddrOffset
)
2262 uint32_t ret
= cpu_to_le32(s
->TxAddr
[txAddrOffset
/4]);
2264 DEBUG_PRINT(("RTL8139: TxAddr read offset=0x%x val=0x%08x\n", txAddrOffset
, ret
));
2269 static void rtl8139_RxBufPtr_write(RTL8139State
*s
, uint32_t val
)
2271 DEBUG_PRINT(("RTL8139: RxBufPtr write val=0x%04x\n", val
));
2273 /* this value is off by 16 */
2274 s
->RxBufPtr
= MOD2(val
+ 0x10, s
->RxBufferSize
);
2276 DEBUG_PRINT((" CAPR write: rx buffer length %d head 0x%04x read 0x%04x\n",
2277 s
->RxBufferSize
, s
->RxBufAddr
, s
->RxBufPtr
));
2280 static uint32_t rtl8139_RxBufPtr_read(RTL8139State
*s
)
2282 /* this value is off by 16 */
2283 uint32_t ret
= s
->RxBufPtr
- 0x10;
2285 DEBUG_PRINT(("RTL8139: RxBufPtr read val=0x%04x\n", ret
));
2290 static uint32_t rtl8139_RxBufAddr_read(RTL8139State
*s
)
2292 /* this value is NOT off by 16 */
2293 uint32_t ret
= s
->RxBufAddr
;
2295 DEBUG_PRINT(("RTL8139: RxBufAddr read val=0x%04x\n", ret
));
2300 static void rtl8139_RxBuf_write(RTL8139State
*s
, uint32_t val
)
2302 DEBUG_PRINT(("RTL8139: RxBuf write val=0x%08x\n", val
));
2306 /* may need to reset rxring here */
2309 static uint32_t rtl8139_RxBuf_read(RTL8139State
*s
)
2311 uint32_t ret
= s
->RxBuf
;
2313 DEBUG_PRINT(("RTL8139: RxBuf read val=0x%08x\n", ret
));
2318 static void rtl8139_IntrMask_write(RTL8139State
*s
, uint32_t val
)
2320 DEBUG_PRINT(("RTL8139: IntrMask write(w) val=0x%04x\n", val
));
2322 /* mask unwriteable bits */
2323 val
= SET_MASKED(val
, 0x1e00, s
->IntrMask
);
2327 rtl8139_update_irq(s
);
2330 static uint32_t rtl8139_IntrMask_read(RTL8139State
*s
)
2332 uint32_t ret
= s
->IntrMask
;
2334 DEBUG_PRINT(("RTL8139: IntrMask read(w) val=0x%04x\n", ret
));
2339 static void rtl8139_IntrStatus_write(RTL8139State
*s
, uint32_t val
)
2341 DEBUG_PRINT(("RTL8139: IntrStatus write(w) val=0x%04x\n", val
));
2345 /* writing to ISR has no effect */
2350 uint16_t newStatus
= s
->IntrStatus
& ~val
;
2352 /* mask unwriteable bits */
2353 newStatus
= SET_MASKED(newStatus
, 0x1e00, s
->IntrStatus
);
2355 /* writing 1 to interrupt status register bit clears it */
2357 rtl8139_update_irq(s
);
2359 s
->IntrStatus
= newStatus
;
2360 rtl8139_update_irq(s
);
2364 static uint32_t rtl8139_IntrStatus_read(RTL8139State
*s
)
2366 uint32_t ret
= s
->IntrStatus
;
2368 DEBUG_PRINT(("RTL8139: IntrStatus read(w) val=0x%04x\n", ret
));
2372 /* reading ISR clears all interrupts */
2375 rtl8139_update_irq(s
);
2382 static void rtl8139_MultiIntr_write(RTL8139State
*s
, uint32_t val
)
2384 DEBUG_PRINT(("RTL8139: MultiIntr write(w) val=0x%04x\n", val
));
2386 /* mask unwriteable bits */
2387 val
= SET_MASKED(val
, 0xf000, s
->MultiIntr
);
2392 static uint32_t rtl8139_MultiIntr_read(RTL8139State
*s
)
2394 uint32_t ret
= s
->MultiIntr
;
2396 DEBUG_PRINT(("RTL8139: MultiIntr read(w) val=0x%04x\n", ret
));
2401 static void rtl8139_io_writeb(void *opaque
, uint8_t addr
, uint32_t val
)
2403 RTL8139State
*s
= opaque
;
2409 case MAC0
... MAC0
+5:
2410 s
->phys
[addr
- MAC0
] = val
;
2412 case MAC0
+6 ... MAC0
+7:
2415 case MAR0
... MAR0
+7:
2416 s
->mult
[addr
- MAR0
] = val
;
2419 rtl8139_ChipCmd_write(s
, val
);
2422 rtl8139_Cfg9346_write(s
, val
);
2424 case TxConfig
: /* windows driver sometimes writes using byte-lenth call */
2425 rtl8139_TxConfig_writeb(s
, val
);
2428 rtl8139_Config0_write(s
, val
);
2431 rtl8139_Config1_write(s
, val
);
2434 rtl8139_Config3_write(s
, val
);
2437 rtl8139_Config4_write(s
, val
);
2440 rtl8139_Config5_write(s
, val
);
2444 DEBUG_PRINT(("RTL8139: not implemented write(b) to MediaStatus val=0x%02x\n", val
));
2448 DEBUG_PRINT(("RTL8139: HltClk write val=0x%08x\n", val
));
2451 s
->clock_enabled
= 1;
2453 else if (val
== 'H')
2455 s
->clock_enabled
= 0;
2460 DEBUG_PRINT(("RTL8139C+ TxThresh write(b) val=0x%02x\n", val
));
2465 DEBUG_PRINT(("RTL8139C+ TxPoll write(b) val=0x%02x\n", val
));
2468 DEBUG_PRINT(("RTL8139C+ TxPoll high priority transmission (not implemented)\n"));
2469 //rtl8139_cplus_transmit(s);
2473 DEBUG_PRINT(("RTL8139C+ TxPoll normal priority transmission\n"));
2474 rtl8139_cplus_transmit(s
);
2480 DEBUG_PRINT(("RTL8139: not implemented write(b) addr=0x%x val=0x%02x\n", addr
, val
));
2485 static void rtl8139_io_writew(void *opaque
, uint8_t addr
, uint32_t val
)
2487 RTL8139State
*s
= opaque
;
2494 rtl8139_IntrMask_write(s
, val
);
2498 rtl8139_IntrStatus_write(s
, val
);
2502 rtl8139_MultiIntr_write(s
, val
);
2506 rtl8139_RxBufPtr_write(s
, val
);
2510 rtl8139_BasicModeCtrl_write(s
, val
);
2512 case BasicModeStatus
:
2513 rtl8139_BasicModeStatus_write(s
, val
);
2516 DEBUG_PRINT(("RTL8139: NWayAdvert write(w) val=0x%04x\n", val
));
2517 s
->NWayAdvert
= val
;
2520 DEBUG_PRINT(("RTL8139: forbidden NWayLPAR write(w) val=0x%04x\n", val
));
2523 DEBUG_PRINT(("RTL8139: NWayExpansion write(w) val=0x%04x\n", val
));
2524 s
->NWayExpansion
= val
;
2528 rtl8139_CpCmd_write(s
, val
);
2532 rtl8139_IntrMitigate_write(s
, val
);
2536 DEBUG_PRINT(("RTL8139: ioport write(w) addr=0x%x val=0x%04x via write(b)\n", addr
, val
));
2538 #ifdef TARGET_WORDS_BIGENDIAN
2539 rtl8139_io_writeb(opaque
, addr
, (val
>> 8) & 0xff);
2540 rtl8139_io_writeb(opaque
, addr
+ 1, val
& 0xff);
2542 rtl8139_io_writeb(opaque
, addr
, val
& 0xff);
2543 rtl8139_io_writeb(opaque
, addr
+ 1, (val
>> 8) & 0xff);
2549 static void rtl8139_io_writel(void *opaque
, uint8_t addr
, uint32_t val
)
2551 RTL8139State
*s
= opaque
;
2558 DEBUG_PRINT(("RTL8139: RxMissed clearing on write\n"));
2563 rtl8139_TxConfig_write(s
, val
);
2567 rtl8139_RxConfig_write(s
, val
);
2570 case TxStatus0
... TxStatus0
+4*4-1:
2571 rtl8139_TxStatus_write(s
, addr
-TxStatus0
, val
);
2574 case TxAddr0
... TxAddr0
+4*4-1:
2575 rtl8139_TxAddr_write(s
, addr
-TxAddr0
, val
);
2579 rtl8139_RxBuf_write(s
, val
);
2583 DEBUG_PRINT(("RTL8139: C+ RxRing low bits write val=0x%08x\n", val
));
2584 s
->RxRingAddrLO
= val
;
2588 DEBUG_PRINT(("RTL8139: C+ RxRing high bits write val=0x%08x\n", val
));
2589 s
->RxRingAddrHI
= val
;
2593 DEBUG_PRINT(("RTL8139: TCTR Timer reset on write\n"));
2595 s
->TCTR_base
= qemu_get_clock(vm_clock
);
2599 DEBUG_PRINT(("RTL8139: FlashReg TimerInt write val=0x%08x\n", val
));
2604 DEBUG_PRINT(("RTL8139: ioport write(l) addr=0x%x val=0x%08x via write(b)\n", addr
, val
));
2605 #ifdef TARGET_WORDS_BIGENDIAN
2606 rtl8139_io_writeb(opaque
, addr
, (val
>> 24) & 0xff);
2607 rtl8139_io_writeb(opaque
, addr
+ 1, (val
>> 16) & 0xff);
2608 rtl8139_io_writeb(opaque
, addr
+ 2, (val
>> 8) & 0xff);
2609 rtl8139_io_writeb(opaque
, addr
+ 3, val
& 0xff);
2611 rtl8139_io_writeb(opaque
, addr
, val
& 0xff);
2612 rtl8139_io_writeb(opaque
, addr
+ 1, (val
>> 8) & 0xff);
2613 rtl8139_io_writeb(opaque
, addr
+ 2, (val
>> 16) & 0xff);
2614 rtl8139_io_writeb(opaque
, addr
+ 3, (val
>> 24) & 0xff);
2620 static uint32_t rtl8139_io_readb(void *opaque
, uint8_t addr
)
2622 RTL8139State
*s
= opaque
;
2629 case MAC0
... MAC0
+5:
2630 ret
= s
->phys
[addr
- MAC0
];
2632 case MAC0
+6 ... MAC0
+7:
2635 case MAR0
... MAR0
+7:
2636 ret
= s
->mult
[addr
- MAR0
];
2639 ret
= rtl8139_ChipCmd_read(s
);
2642 ret
= rtl8139_Cfg9346_read(s
);
2645 ret
= rtl8139_Config0_read(s
);
2648 ret
= rtl8139_Config1_read(s
);
2651 ret
= rtl8139_Config3_read(s
);
2654 ret
= rtl8139_Config4_read(s
);
2657 ret
= rtl8139_Config5_read(s
);
2662 DEBUG_PRINT(("RTL8139: MediaStatus read 0x%x\n", ret
));
2666 ret
= s
->clock_enabled
;
2667 DEBUG_PRINT(("RTL8139: HltClk read 0x%x\n", ret
));
2671 ret
= RTL8139_PCI_REVID
;
2672 DEBUG_PRINT(("RTL8139: PCI Revision ID read 0x%x\n", ret
));
2677 DEBUG_PRINT(("RTL8139C+ TxThresh read(b) val=0x%02x\n", ret
));
2680 case 0x43: /* Part of TxConfig register. Windows driver tries to read it */
2681 ret
= s
->TxConfig
>> 24;
2682 DEBUG_PRINT(("RTL8139C TxConfig at 0x43 read(b) val=0x%02x\n", ret
));
2686 DEBUG_PRINT(("RTL8139: not implemented read(b) addr=0x%x\n", addr
));
2694 static uint32_t rtl8139_io_readw(void *opaque
, uint8_t addr
)
2696 RTL8139State
*s
= opaque
;
2699 addr
&= 0xfe; /* mask lower bit */
2704 ret
= rtl8139_IntrMask_read(s
);
2708 ret
= rtl8139_IntrStatus_read(s
);
2712 ret
= rtl8139_MultiIntr_read(s
);
2716 ret
= rtl8139_RxBufPtr_read(s
);
2720 ret
= rtl8139_RxBufAddr_read(s
);
2724 ret
= rtl8139_BasicModeCtrl_read(s
);
2726 case BasicModeStatus
:
2727 ret
= rtl8139_BasicModeStatus_read(s
);
2730 ret
= s
->NWayAdvert
;
2731 DEBUG_PRINT(("RTL8139: NWayAdvert read(w) val=0x%04x\n", ret
));
2735 DEBUG_PRINT(("RTL8139: NWayLPAR read(w) val=0x%04x\n", ret
));
2738 ret
= s
->NWayExpansion
;
2739 DEBUG_PRINT(("RTL8139: NWayExpansion read(w) val=0x%04x\n", ret
));
2743 ret
= rtl8139_CpCmd_read(s
);
2747 ret
= rtl8139_IntrMitigate_read(s
);
2751 ret
= rtl8139_TSAD_read(s
);
2755 ret
= rtl8139_CSCR_read(s
);
2759 DEBUG_PRINT(("RTL8139: ioport read(w) addr=0x%x via read(b)\n", addr
));
2761 #ifdef TARGET_WORDS_BIGENDIAN
2762 ret
= rtl8139_io_readb(opaque
, addr
) << 8;
2763 ret
|= rtl8139_io_readb(opaque
, addr
+ 1);
2765 ret
= rtl8139_io_readb(opaque
, addr
);
2766 ret
|= rtl8139_io_readb(opaque
, addr
+ 1) << 8;
2769 DEBUG_PRINT(("RTL8139: ioport read(w) addr=0x%x val=0x%04x\n", addr
, ret
));
2776 static uint32_t rtl8139_io_readl(void *opaque
, uint8_t addr
)
2778 RTL8139State
*s
= opaque
;
2781 addr
&= 0xfc; /* also mask low 2 bits */
2788 DEBUG_PRINT(("RTL8139: RxMissed read val=0x%08x\n", ret
));
2792 ret
= rtl8139_TxConfig_read(s
);
2796 ret
= rtl8139_RxConfig_read(s
);
2799 case TxStatus0
... TxStatus0
+4*4-1:
2800 ret
= rtl8139_TxStatus_read(s
, addr
-TxStatus0
);
2803 case TxAddr0
... TxAddr0
+4*4-1:
2804 ret
= rtl8139_TxAddr_read(s
, addr
-TxAddr0
);
2808 ret
= rtl8139_RxBuf_read(s
);
2812 ret
= s
->RxRingAddrLO
;
2813 DEBUG_PRINT(("RTL8139: C+ RxRing low bits read val=0x%08x\n", ret
));
2817 ret
= s
->RxRingAddrHI
;
2818 DEBUG_PRINT(("RTL8139: C+ RxRing high bits read val=0x%08x\n", ret
));
2823 DEBUG_PRINT(("RTL8139: TCTR Timer read val=0x%08x\n", ret
));
2828 DEBUG_PRINT(("RTL8139: FlashReg TimerInt read val=0x%08x\n", ret
));
2832 DEBUG_PRINT(("RTL8139: ioport read(l) addr=0x%x via read(b)\n", addr
));
2834 #ifdef TARGET_WORDS_BIGENDIAN
2835 ret
= rtl8139_io_readb(opaque
, addr
) << 24;
2836 ret
|= rtl8139_io_readb(opaque
, addr
+ 1) << 16;
2837 ret
|= rtl8139_io_readb(opaque
, addr
+ 2) << 8;
2838 ret
|= rtl8139_io_readb(opaque
, addr
+ 3);
2840 ret
= rtl8139_io_readb(opaque
, addr
);
2841 ret
|= rtl8139_io_readb(opaque
, addr
+ 1) << 8;
2842 ret
|= rtl8139_io_readb(opaque
, addr
+ 2) << 16;
2843 ret
|= rtl8139_io_readb(opaque
, addr
+ 3) << 24;
2846 DEBUG_PRINT(("RTL8139: read(l) addr=0x%x val=%08x\n", addr
, ret
));
2855 static void rtl8139_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
2857 rtl8139_io_writeb(opaque
, addr
& 0xFF, val
);
2860 static void rtl8139_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
2862 rtl8139_io_writew(opaque
, addr
& 0xFF, val
);
2865 static void rtl8139_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
2867 rtl8139_io_writel(opaque
, addr
& 0xFF, val
);
2870 static uint32_t rtl8139_ioport_readb(void *opaque
, uint32_t addr
)
2872 return rtl8139_io_readb(opaque
, addr
& 0xFF);
2875 static uint32_t rtl8139_ioport_readw(void *opaque
, uint32_t addr
)
2877 return rtl8139_io_readw(opaque
, addr
& 0xFF);
2880 static uint32_t rtl8139_ioport_readl(void *opaque
, uint32_t addr
)
2882 return rtl8139_io_readl(opaque
, addr
& 0xFF);
2887 static void rtl8139_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2889 rtl8139_io_writeb(opaque
, addr
& 0xFF, val
);
2892 static void rtl8139_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2894 rtl8139_io_writew(opaque
, addr
& 0xFF, val
);
2897 static void rtl8139_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2899 rtl8139_io_writel(opaque
, addr
& 0xFF, val
);
2902 static uint32_t rtl8139_mmio_readb(void *opaque
, target_phys_addr_t addr
)
2904 return rtl8139_io_readb(opaque
, addr
& 0xFF);
2907 static uint32_t rtl8139_mmio_readw(void *opaque
, target_phys_addr_t addr
)
2909 return rtl8139_io_readw(opaque
, addr
& 0xFF);
2912 static uint32_t rtl8139_mmio_readl(void *opaque
, target_phys_addr_t addr
)
2914 return rtl8139_io_readl(opaque
, addr
& 0xFF);
2919 static void rtl8139_save(QEMUFile
* f
,void* opaque
)
2921 RTL8139State
* s
=(RTL8139State
*)opaque
;
2924 qemu_put_buffer(f
, s
->phys
, 6);
2925 qemu_put_buffer(f
, s
->mult
, 8);
2929 qemu_put_be32s(f
, &s
->TxStatus
[i
]); /* TxStatus0 */
2933 qemu_put_be32s(f
, &s
->TxAddr
[i
]); /* TxAddr0 */
2936 qemu_put_be32s(f
, &s
->RxBuf
); /* Receive buffer */
2937 qemu_put_be32s(f
, &s
->RxBufferSize
);/* internal variable, receive ring buffer size in C mode */
2938 qemu_put_be32s(f
, &s
->RxBufPtr
);
2939 qemu_put_be32s(f
, &s
->RxBufAddr
);
2941 qemu_put_be16s(f
, &s
->IntrStatus
);
2942 qemu_put_be16s(f
, &s
->IntrMask
);
2944 qemu_put_be32s(f
, &s
->TxConfig
);
2945 qemu_put_be32s(f
, &s
->RxConfig
);
2946 qemu_put_be32s(f
, &s
->RxMissed
);
2947 qemu_put_be16s(f
, &s
->CSCR
);
2949 qemu_put_8s(f
, &s
->Cfg9346
);
2950 qemu_put_8s(f
, &s
->Config0
);
2951 qemu_put_8s(f
, &s
->Config1
);
2952 qemu_put_8s(f
, &s
->Config3
);
2953 qemu_put_8s(f
, &s
->Config4
);
2954 qemu_put_8s(f
, &s
->Config5
);
2956 qemu_put_8s(f
, &s
->clock_enabled
);
2957 qemu_put_8s(f
, &s
->bChipCmdState
);
2959 qemu_put_be16s(f
, &s
->MultiIntr
);
2961 qemu_put_be16s(f
, &s
->BasicModeCtrl
);
2962 qemu_put_be16s(f
, &s
->BasicModeStatus
);
2963 qemu_put_be16s(f
, &s
->NWayAdvert
);
2964 qemu_put_be16s(f
, &s
->NWayLPAR
);
2965 qemu_put_be16s(f
, &s
->NWayExpansion
);
2967 qemu_put_be16s(f
, &s
->CpCmd
);
2968 qemu_put_8s(f
, &s
->TxThresh
);
2970 qemu_put_be32s(f
, &s
->irq
);
2971 qemu_put_buffer(f
, s
->macaddr
, 6);
2972 qemu_put_be32s(f
, &s
->rtl8139_mmio_io_addr
);
2974 qemu_put_be32s(f
, &s
->currTxDesc
);
2975 qemu_put_be32s(f
, &s
->currCPlusRxDesc
);
2976 qemu_put_be32s(f
, &s
->currCPlusTxDesc
);
2977 qemu_put_be32s(f
, &s
->RxRingAddrLO
);
2978 qemu_put_be32s(f
, &s
->RxRingAddrHI
);
2980 for (i
=0; i
<EEPROM_9346_SIZE
; ++i
)
2982 qemu_put_be16s(f
, &s
->eeprom
.contents
[i
]);
2984 qemu_put_be32s(f
, &s
->eeprom
.mode
);
2985 qemu_put_be32s(f
, &s
->eeprom
.tick
);
2986 qemu_put_8s(f
, &s
->eeprom
.address
);
2987 qemu_put_be16s(f
, &s
->eeprom
.input
);
2988 qemu_put_be16s(f
, &s
->eeprom
.output
);
2990 qemu_put_8s(f
, &s
->eeprom
.eecs
);
2991 qemu_put_8s(f
, &s
->eeprom
.eesk
);
2992 qemu_put_8s(f
, &s
->eeprom
.eedi
);
2993 qemu_put_8s(f
, &s
->eeprom
.eedo
);
2995 qemu_put_be32s(f
, &s
->TCTR
);
2996 qemu_put_be32s(f
, &s
->TimerInt
);
2997 qemu_put_be64s(f
, &s
->TCTR_base
);
2999 RTL8139TallyCounters_save(f
, &s
->tally_counters
);
3002 static int rtl8139_load(QEMUFile
* f
,void* opaque
,int version_id
)
3004 RTL8139State
* s
=(RTL8139State
*)opaque
;
3007 /* just 2 versions for now */
3011 /* saved since version 1 */
3012 qemu_get_buffer(f
, s
->phys
, 6);
3013 qemu_get_buffer(f
, s
->mult
, 8);
3017 qemu_get_be32s(f
, &s
->TxStatus
[i
]); /* TxStatus0 */
3021 qemu_get_be32s(f
, &s
->TxAddr
[i
]); /* TxAddr0 */
3024 qemu_get_be32s(f
, &s
->RxBuf
); /* Receive buffer */
3025 qemu_get_be32s(f
, &s
->RxBufferSize
);/* internal variable, receive ring buffer size in C mode */
3026 qemu_get_be32s(f
, &s
->RxBufPtr
);
3027 qemu_get_be32s(f
, &s
->RxBufAddr
);
3029 qemu_get_be16s(f
, &s
->IntrStatus
);
3030 qemu_get_be16s(f
, &s
->IntrMask
);
3032 qemu_get_be32s(f
, &s
->TxConfig
);
3033 qemu_get_be32s(f
, &s
->RxConfig
);
3034 qemu_get_be32s(f
, &s
->RxMissed
);
3035 qemu_get_be16s(f
, &s
->CSCR
);
3037 qemu_get_8s(f
, &s
->Cfg9346
);
3038 qemu_get_8s(f
, &s
->Config0
);
3039 qemu_get_8s(f
, &s
->Config1
);
3040 qemu_get_8s(f
, &s
->Config3
);
3041 qemu_get_8s(f
, &s
->Config4
);
3042 qemu_get_8s(f
, &s
->Config5
);
3044 qemu_get_8s(f
, &s
->clock_enabled
);
3045 qemu_get_8s(f
, &s
->bChipCmdState
);
3047 qemu_get_be16s(f
, &s
->MultiIntr
);
3049 qemu_get_be16s(f
, &s
->BasicModeCtrl
);
3050 qemu_get_be16s(f
, &s
->BasicModeStatus
);
3051 qemu_get_be16s(f
, &s
->NWayAdvert
);
3052 qemu_get_be16s(f
, &s
->NWayLPAR
);
3053 qemu_get_be16s(f
, &s
->NWayExpansion
);
3055 qemu_get_be16s(f
, &s
->CpCmd
);
3056 qemu_get_8s(f
, &s
->TxThresh
);
3058 qemu_get_be32s(f
, &s
->irq
);
3059 qemu_get_buffer(f
, s
->macaddr
, 6);
3060 qemu_get_be32s(f
, &s
->rtl8139_mmio_io_addr
);
3062 qemu_get_be32s(f
, &s
->currTxDesc
);
3063 qemu_get_be32s(f
, &s
->currCPlusRxDesc
);
3064 qemu_get_be32s(f
, &s
->currCPlusTxDesc
);
3065 qemu_get_be32s(f
, &s
->RxRingAddrLO
);
3066 qemu_get_be32s(f
, &s
->RxRingAddrHI
);
3068 for (i
=0; i
<EEPROM_9346_SIZE
; ++i
)
3070 qemu_get_be16s(f
, &s
->eeprom
.contents
[i
]);
3072 qemu_get_be32s(f
, &s
->eeprom
.mode
);
3073 qemu_get_be32s(f
, &s
->eeprom
.tick
);
3074 qemu_get_8s(f
, &s
->eeprom
.address
);
3075 qemu_get_be16s(f
, &s
->eeprom
.input
);
3076 qemu_get_be16s(f
, &s
->eeprom
.output
);
3078 qemu_get_8s(f
, &s
->eeprom
.eecs
);
3079 qemu_get_8s(f
, &s
->eeprom
.eesk
);
3080 qemu_get_8s(f
, &s
->eeprom
.eedi
);
3081 qemu_get_8s(f
, &s
->eeprom
.eedo
);
3083 /* saved since version 2 */
3084 if (version_id
>= 2)
3086 qemu_get_be32s(f
, &s
->TCTR
);
3087 qemu_get_be32s(f
, &s
->TimerInt
);
3088 qemu_get_be64s(f
, &s
->TCTR_base
);
3090 RTL8139TallyCounters_load(f
, &s
->tally_counters
);
3094 /* not saved, use default */
3099 RTL8139TallyCounters_clear(&s
->tally_counters
);
3105 /***********************************************************/
3106 /* PCI RTL8139 definitions */
3108 typedef struct PCIRTL8139State
{
3110 RTL8139State rtl8139
;
3113 static void rtl8139_mmio_map(PCIDevice
*pci_dev
, int region_num
,
3114 uint32_t addr
, uint32_t size
, int type
)
3116 PCIRTL8139State
*d
= (PCIRTL8139State
*)pci_dev
;
3117 RTL8139State
*s
= &d
->rtl8139
;
3119 cpu_register_physical_memory(addr
+ 0, 0x100, s
->rtl8139_mmio_io_addr
);
3122 static void rtl8139_ioport_map(PCIDevice
*pci_dev
, int region_num
,
3123 uint32_t addr
, uint32_t size
, int type
)
3125 PCIRTL8139State
*d
= (PCIRTL8139State
*)pci_dev
;
3126 RTL8139State
*s
= &d
->rtl8139
;
3128 register_ioport_write(addr
, 0x100, 1, rtl8139_ioport_writeb
, s
);
3129 register_ioport_read( addr
, 0x100, 1, rtl8139_ioport_readb
, s
);
3131 register_ioport_write(addr
, 0x100, 2, rtl8139_ioport_writew
, s
);
3132 register_ioport_read( addr
, 0x100, 2, rtl8139_ioport_readw
, s
);
3134 register_ioport_write(addr
, 0x100, 4, rtl8139_ioport_writel
, s
);
3135 register_ioport_read( addr
, 0x100, 4, rtl8139_ioport_readl
, s
);
3138 static CPUReadMemoryFunc
*rtl8139_mmio_read
[3] = {
3144 static CPUWriteMemoryFunc
*rtl8139_mmio_write
[3] = {
3145 rtl8139_mmio_writeb
,
3146 rtl8139_mmio_writew
,
3147 rtl8139_mmio_writel
,
3150 static inline int64_t rtl8139_get_next_tctr_time(RTL8139State
*s
, int64_t current_time
)
3152 int64_t next_time
= current_time
+
3153 muldiv64(1, ticks_per_sec
, PCI_FREQUENCY
);
3154 if (next_time
<= current_time
)
3155 next_time
= current_time
+ 1;
3159 #if RTL8139_ONBOARD_TIMER
3160 static void rtl8139_timer(void *opaque
)
3162 RTL8139State
*s
= opaque
;
3169 if (!s
->clock_enabled
)
3171 DEBUG_PRINT(("RTL8139: >>> timer: clock is not running\n"));
3175 curr_time
= qemu_get_clock(vm_clock
);
3177 curr_tick
= muldiv64(curr_time
- s
->TCTR_base
, PCI_FREQUENCY
, ticks_per_sec
);
3179 if (s
->TimerInt
&& curr_tick
>= s
->TimerInt
)
3181 if (s
->TCTR
< s
->TimerInt
|| curr_tick
< s
->TCTR
)
3187 s
->TCTR
= curr_tick
;
3189 // DEBUG_PRINT(("RTL8139: >>> timer: tick=%08u\n", s->TCTR));
3193 DEBUG_PRINT(("RTL8139: >>> timer: timeout tick=%08u\n", s
->TCTR
));
3194 s
->IntrStatus
|= PCSTimeout
;
3195 rtl8139_update_irq(s
);
3198 qemu_mod_timer(s
->timer
,
3199 rtl8139_get_next_tctr_time(s
,curr_time
));
3201 #endif /* RTL8139_ONBOARD_TIMER */
3203 void pci_rtl8139_init(PCIBus
*bus
, NICInfo
*nd
)
3209 d
= (PCIRTL8139State
*)pci_register_device(bus
,
3210 "RTL8139", sizeof(PCIRTL8139State
),
3213 pci_conf
= d
->dev
.config
;
3214 pci_conf
[0x00] = 0xec; /* Realtek 8139 */
3215 pci_conf
[0x01] = 0x10;
3216 pci_conf
[0x02] = 0x39;
3217 pci_conf
[0x03] = 0x81;
3218 pci_conf
[0x04] = 0x05; /* command = I/O space, Bus Master */
3219 pci_conf
[0x08] = RTL8139_PCI_REVID
; /* PCI revision ID; >=0x20 is for 8139C+ */
3220 pci_conf
[0x0a] = 0x00; /* ethernet network controller */
3221 pci_conf
[0x0b] = 0x02;
3222 pci_conf
[0x0e] = 0x00; /* header_type */
3223 pci_conf
[0x3d] = 1; /* interrupt pin 0 */
3224 pci_conf
[0x34] = 0xdc;
3228 /* I/O handler for memory-mapped I/O */
3229 s
->rtl8139_mmio_io_addr
=
3230 cpu_register_io_memory(0, rtl8139_mmio_read
, rtl8139_mmio_write
, s
);
3232 pci_register_io_region(&d
->dev
, 0, 0x100,
3233 PCI_ADDRESS_SPACE_IO
, rtl8139_ioport_map
);
3235 pci_register_io_region(&d
->dev
, 1, 0x100,
3236 PCI_ADDRESS_SPACE_MEM
, rtl8139_mmio_map
);
3238 s
->irq
= 16; /* PCI interrupt */
3239 s
->pci_dev
= (PCIDevice
*)d
;
3240 memcpy(s
->macaddr
, nd
->macaddr
, 6);
3242 s
->vc
= qemu_new_vlan_client(nd
->vlan
, rtl8139_receive
,
3243 rtl8139_can_receive
, s
);
3245 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
3246 "rtl8139 pci macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
3254 s
->cplus_txbuffer
= NULL
;
3255 s
->cplus_txbuffer_len
= 0;
3256 s
->cplus_txbuffer_offset
= 0;
3258 /* XXX: instance number ? */
3259 register_savevm("rtl8139", 0, 2, rtl8139_save
, rtl8139_load
, s
);
3260 register_savevm("rtl8139_pci", 0, 1, generic_pci_save
, generic_pci_load
,
3263 #if RTL8139_ONBOARD_TIMER
3264 s
->timer
= qemu_new_timer(vm_clock
, rtl8139_timer
, s
);
3266 qemu_mod_timer(s
->timer
,
3267 rtl8139_get_next_tctr_time(s
,qemu_get_clock(vm_clock
)));
3268 #endif /* RTL8139_ONBOARD_TIMER */