Statistics
| Branch: | Revision:

root / hw / rtl8139.c @ c65adf9b

History | View | Annotate | Download (99.8 kB)

1 a41b2ff2 pbrook
/**
2 a41b2ff2 pbrook
 * QEMU RTL8139 emulation
3 5fafdf24 ths
 *
4 a41b2ff2 pbrook
 * Copyright (c) 2006 Igor Kovalenko
5 5fafdf24 ths
 *
6 a41b2ff2 pbrook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 a41b2ff2 pbrook
 * of this software and associated documentation files (the "Software"), to deal
8 a41b2ff2 pbrook
 * in the Software without restriction, including without limitation the rights
9 a41b2ff2 pbrook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 a41b2ff2 pbrook
 * copies of the Software, and to permit persons to whom the Software is
11 a41b2ff2 pbrook
 * furnished to do so, subject to the following conditions:
12 a41b2ff2 pbrook
 *
13 a41b2ff2 pbrook
 * The above copyright notice and this permission notice shall be included in
14 a41b2ff2 pbrook
 * all copies or substantial portions of the Software.
15 a41b2ff2 pbrook
 *
16 a41b2ff2 pbrook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 a41b2ff2 pbrook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 a41b2ff2 pbrook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 a41b2ff2 pbrook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 a41b2ff2 pbrook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 a41b2ff2 pbrook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 a41b2ff2 pbrook
 * THE SOFTWARE.
23 5fafdf24 ths

24 a41b2ff2 pbrook
 * Modifications:
25 a41b2ff2 pbrook
 *  2006-Jan-28  Mark Malakanov :   TSAD and CSCR implementation (for Windows driver)
26 5fafdf24 ths
 *
27 6cadb320 bellard
 *  2006-Apr-28  Juergen Lock   :   EEPROM emulation changes for FreeBSD driver
28 6cadb320 bellard
 *                                  HW revision ID changes for FreeBSD driver
29 5fafdf24 ths
 *
30 6cadb320 bellard
 *  2006-Jul-01  Igor Kovalenko :   Implemented loopback mode for FreeBSD driver
31 6cadb320 bellard
 *                                  Corrected packet transfer reassembly routine for 8139C+ mode
32 6cadb320 bellard
 *                                  Rearranged debugging print statements
33 6cadb320 bellard
 *                                  Implemented PCI timer interrupt (disabled by default)
34 6cadb320 bellard
 *                                  Implemented Tally Counters, increased VM load/save version
35 6cadb320 bellard
 *                                  Implemented IP/TCP/UDP checksum task offloading
36 718da2b9 bellard
 *
37 718da2b9 bellard
 *  2006-Jul-04  Igor Kovalenko :   Implemented TCP segmentation offloading
38 718da2b9 bellard
 *                                  Fixed MTU=1500 for produced ethernet frames
39 718da2b9 bellard
 *
40 718da2b9 bellard
 *  2006-Jul-09  Igor Kovalenko :   Fixed TCP header length calculation while processing
41 718da2b9 bellard
 *                                  segmentation offloading
42 718da2b9 bellard
 *                                  Removed slirp.h dependency
43 718da2b9 bellard
 *                                  Added rx/tx buffer reset when enabling rx/tx operation
44 05447803 Frediano Ziglio
 *
45 05447803 Frediano Ziglio
 *  2010-Feb-04  Frediano Ziglio:   Rewrote timer support using QEMU timer only
46 05447803 Frediano Ziglio
 *                                  when strictly needed (required for for
47 05447803 Frediano Ziglio
 *                                  Darwin)
48 bf6b87a8 Benjamin Poirier
 *  2011-Mar-22  Benjamin Poirier:  Implemented VLAN offloading
49 a41b2ff2 pbrook
 */
50 a41b2ff2 pbrook
51 2c406b8f Benjamin Poirier
/* For crc32 */
52 2c406b8f Benjamin Poirier
#include <zlib.h>
53 2c406b8f Benjamin Poirier
54 87ecb68b pbrook
#include "hw.h"
55 87ecb68b pbrook
#include "pci.h"
56 3ada003a Eduard - Gabriel Munteanu
#include "dma.h"
57 87ecb68b pbrook
#include "qemu-timer.h"
58 87ecb68b pbrook
#include "net.h"
59 254111ec Gerd Hoffmann
#include "loader.h"
60 1ca4d09a Gleb Natapov
#include "sysemu.h"
61 bf6b87a8 Benjamin Poirier
#include "iov.h"
62 a41b2ff2 pbrook
63 a41b2ff2 pbrook
/* debug RTL8139 card */
64 a41b2ff2 pbrook
//#define DEBUG_RTL8139 1
65 a41b2ff2 pbrook
66 6cadb320 bellard
#define PCI_FREQUENCY 33000000L
67 6cadb320 bellard
68 a41b2ff2 pbrook
/* debug RTL8139 card C+ mode only */
69 a41b2ff2 pbrook
//#define DEBUG_RTL8139CP 1
70 a41b2ff2 pbrook
71 a41b2ff2 pbrook
#define SET_MASKED(input, mask, curr) \
72 a41b2ff2 pbrook
    ( ( (input) & ~(mask) ) | ( (curr) & (mask) ) )
73 a41b2ff2 pbrook
74 a41b2ff2 pbrook
/* arg % size for size which is a power of 2 */
75 a41b2ff2 pbrook
#define MOD2(input, size) \
76 a41b2ff2 pbrook
    ( ( input ) & ( size - 1 )  )
77 a41b2ff2 pbrook
78 18dabfd1 Benjamin Poirier
#define ETHER_ADDR_LEN 6
79 18dabfd1 Benjamin Poirier
#define ETHER_TYPE_LEN 2
80 18dabfd1 Benjamin Poirier
#define ETH_HLEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
81 18dabfd1 Benjamin Poirier
#define ETH_P_IP    0x0800      /* Internet Protocol packet */
82 18dabfd1 Benjamin Poirier
#define ETH_P_8021Q 0x8100      /* 802.1Q VLAN Extended Header  */
83 18dabfd1 Benjamin Poirier
#define ETH_MTU     1500
84 18dabfd1 Benjamin Poirier
85 18dabfd1 Benjamin Poirier
#define VLAN_TCI_LEN 2
86 18dabfd1 Benjamin Poirier
#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
87 18dabfd1 Benjamin Poirier
88 6cadb320 bellard
#if defined (DEBUG_RTL8139)
89 7cdeb319 Benjamin Poirier
#  define DPRINTF(fmt, ...) \
90 7cdeb319 Benjamin Poirier
    do { fprintf(stderr, "RTL8139: " fmt, ## __VA_ARGS__); } while (0)
91 6cadb320 bellard
#else
92 c6a0487b Stefan Weil
static inline GCC_FMT_ATTR(1, 2) int DPRINTF(const char *fmt, ...)
93 ec48c774 Benjamin Poirier
{
94 ec48c774 Benjamin Poirier
    return 0;
95 ec48c774 Benjamin Poirier
}
96 6cadb320 bellard
#endif
97 6cadb320 bellard
98 a41b2ff2 pbrook
/* Symbolic offsets to registers. */
99 a41b2ff2 pbrook
enum RTL8139_registers {
100 a41b2ff2 pbrook
    MAC0 = 0,        /* Ethernet hardware address. */
101 a41b2ff2 pbrook
    MAR0 = 8,        /* Multicast filter. */
102 6cadb320 bellard
    TxStatus0 = 0x10,/* Transmit status (Four 32bit registers). C mode only */
103 6cadb320 bellard
                     /* Dump Tally Conter control register(64bit). C+ mode only */
104 6cadb320 bellard
    TxAddr0 = 0x20,  /* Tx descriptors (also four 32bit). */
105 a41b2ff2 pbrook
    RxBuf = 0x30,
106 a41b2ff2 pbrook
    ChipCmd = 0x37,
107 a41b2ff2 pbrook
    RxBufPtr = 0x38,
108 a41b2ff2 pbrook
    RxBufAddr = 0x3A,
109 a41b2ff2 pbrook
    IntrMask = 0x3C,
110 a41b2ff2 pbrook
    IntrStatus = 0x3E,
111 a41b2ff2 pbrook
    TxConfig = 0x40,
112 a41b2ff2 pbrook
    RxConfig = 0x44,
113 a41b2ff2 pbrook
    Timer = 0x48,        /* A general-purpose counter. */
114 a41b2ff2 pbrook
    RxMissed = 0x4C,    /* 24 bits valid, write clears. */
115 a41b2ff2 pbrook
    Cfg9346 = 0x50,
116 a41b2ff2 pbrook
    Config0 = 0x51,
117 a41b2ff2 pbrook
    Config1 = 0x52,
118 a41b2ff2 pbrook
    FlashReg = 0x54,
119 a41b2ff2 pbrook
    MediaStatus = 0x58,
120 a41b2ff2 pbrook
    Config3 = 0x59,
121 a41b2ff2 pbrook
    Config4 = 0x5A,        /* absent on RTL-8139A */
122 a41b2ff2 pbrook
    HltClk = 0x5B,
123 a41b2ff2 pbrook
    MultiIntr = 0x5C,
124 a41b2ff2 pbrook
    PCIRevisionID = 0x5E,
125 a41b2ff2 pbrook
    TxSummary = 0x60, /* TSAD register. Transmit Status of All Descriptors*/
126 a41b2ff2 pbrook
    BasicModeCtrl = 0x62,
127 a41b2ff2 pbrook
    BasicModeStatus = 0x64,
128 a41b2ff2 pbrook
    NWayAdvert = 0x66,
129 a41b2ff2 pbrook
    NWayLPAR = 0x68,
130 a41b2ff2 pbrook
    NWayExpansion = 0x6A,
131 a41b2ff2 pbrook
    /* Undocumented registers, but required for proper operation. */
132 a41b2ff2 pbrook
    FIFOTMS = 0x70,        /* FIFO Control and test. */
133 a41b2ff2 pbrook
    CSCR = 0x74,        /* Chip Status and Configuration Register. */
134 a41b2ff2 pbrook
    PARA78 = 0x78,
135 a41b2ff2 pbrook
    PARA7c = 0x7c,        /* Magic transceiver parameter register. */
136 a41b2ff2 pbrook
    Config5 = 0xD8,        /* absent on RTL-8139A */
137 a41b2ff2 pbrook
    /* C+ mode */
138 a41b2ff2 pbrook
    TxPoll        = 0xD9,    /* Tell chip to check Tx descriptors for work */
139 a41b2ff2 pbrook
    RxMaxSize    = 0xDA, /* Max size of an Rx packet (8169 only) */
140 a41b2ff2 pbrook
    CpCmd        = 0xE0, /* C+ Command register (C+ mode only) */
141 a41b2ff2 pbrook
    IntrMitigate    = 0xE2,    /* rx/tx interrupt mitigation control */
142 a41b2ff2 pbrook
    RxRingAddrLO    = 0xE4, /* 64-bit start addr of Rx ring */
143 a41b2ff2 pbrook
    RxRingAddrHI    = 0xE8, /* 64-bit start addr of Rx ring */
144 a41b2ff2 pbrook
    TxThresh    = 0xEC, /* Early Tx threshold */
145 a41b2ff2 pbrook
};
146 a41b2ff2 pbrook
147 a41b2ff2 pbrook
enum ClearBitMasks {
148 a41b2ff2 pbrook
    MultiIntrClear = 0xF000,
149 a41b2ff2 pbrook
    ChipCmdClear = 0xE2,
150 a41b2ff2 pbrook
    Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
151 a41b2ff2 pbrook
};
152 a41b2ff2 pbrook
153 a41b2ff2 pbrook
enum ChipCmdBits {
154 a41b2ff2 pbrook
    CmdReset = 0x10,
155 a41b2ff2 pbrook
    CmdRxEnb = 0x08,
156 a41b2ff2 pbrook
    CmdTxEnb = 0x04,
157 a41b2ff2 pbrook
    RxBufEmpty = 0x01,
158 a41b2ff2 pbrook
};
159 a41b2ff2 pbrook
160 a41b2ff2 pbrook
/* C+ mode */
161 a41b2ff2 pbrook
enum CplusCmdBits {
162 6cadb320 bellard
    CPlusRxVLAN   = 0x0040, /* enable receive VLAN detagging */
163 6cadb320 bellard
    CPlusRxChkSum = 0x0020, /* enable receive checksum offloading */
164 6cadb320 bellard
    CPlusRxEnb    = 0x0002,
165 6cadb320 bellard
    CPlusTxEnb    = 0x0001,
166 a41b2ff2 pbrook
};
167 a41b2ff2 pbrook
168 a41b2ff2 pbrook
/* Interrupt register bits, using my own meaningful names. */
169 a41b2ff2 pbrook
enum IntrStatusBits {
170 a41b2ff2 pbrook
    PCIErr = 0x8000,
171 a41b2ff2 pbrook
    PCSTimeout = 0x4000,
172 a41b2ff2 pbrook
    RxFIFOOver = 0x40,
173 a41b2ff2 pbrook
    RxUnderrun = 0x20,
174 a41b2ff2 pbrook
    RxOverflow = 0x10,
175 a41b2ff2 pbrook
    TxErr = 0x08,
176 a41b2ff2 pbrook
    TxOK = 0x04,
177 a41b2ff2 pbrook
    RxErr = 0x02,
178 a41b2ff2 pbrook
    RxOK = 0x01,
179 a41b2ff2 pbrook
180 a41b2ff2 pbrook
    RxAckBits = RxFIFOOver | RxOverflow | RxOK,
181 a41b2ff2 pbrook
};
182 a41b2ff2 pbrook
183 a41b2ff2 pbrook
enum TxStatusBits {
184 a41b2ff2 pbrook
    TxHostOwns = 0x2000,
185 a41b2ff2 pbrook
    TxUnderrun = 0x4000,
186 a41b2ff2 pbrook
    TxStatOK = 0x8000,
187 a41b2ff2 pbrook
    TxOutOfWindow = 0x20000000,
188 a41b2ff2 pbrook
    TxAborted = 0x40000000,
189 a41b2ff2 pbrook
    TxCarrierLost = 0x80000000,
190 a41b2ff2 pbrook
};
191 a41b2ff2 pbrook
enum RxStatusBits {
192 a41b2ff2 pbrook
    RxMulticast = 0x8000,
193 a41b2ff2 pbrook
    RxPhysical = 0x4000,
194 a41b2ff2 pbrook
    RxBroadcast = 0x2000,
195 a41b2ff2 pbrook
    RxBadSymbol = 0x0020,
196 a41b2ff2 pbrook
    RxRunt = 0x0010,
197 a41b2ff2 pbrook
    RxTooLong = 0x0008,
198 a41b2ff2 pbrook
    RxCRCErr = 0x0004,
199 a41b2ff2 pbrook
    RxBadAlign = 0x0002,
200 a41b2ff2 pbrook
    RxStatusOK = 0x0001,
201 a41b2ff2 pbrook
};
202 a41b2ff2 pbrook
203 a41b2ff2 pbrook
/* Bits in RxConfig. */
204 a41b2ff2 pbrook
enum rx_mode_bits {
205 a41b2ff2 pbrook
    AcceptErr = 0x20,
206 a41b2ff2 pbrook
    AcceptRunt = 0x10,
207 a41b2ff2 pbrook
    AcceptBroadcast = 0x08,
208 a41b2ff2 pbrook
    AcceptMulticast = 0x04,
209 a41b2ff2 pbrook
    AcceptMyPhys = 0x02,
210 a41b2ff2 pbrook
    AcceptAllPhys = 0x01,
211 a41b2ff2 pbrook
};
212 a41b2ff2 pbrook
213 a41b2ff2 pbrook
/* Bits in TxConfig. */
214 a41b2ff2 pbrook
enum tx_config_bits {
215 a41b2ff2 pbrook
216 a41b2ff2 pbrook
        /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
217 a41b2ff2 pbrook
        TxIFGShift = 24,
218 a41b2ff2 pbrook
        TxIFG84 = (0 << TxIFGShift),    /* 8.4us / 840ns (10 / 100Mbps) */
219 a41b2ff2 pbrook
        TxIFG88 = (1 << TxIFGShift),    /* 8.8us / 880ns (10 / 100Mbps) */
220 a41b2ff2 pbrook
        TxIFG92 = (2 << TxIFGShift),    /* 9.2us / 920ns (10 / 100Mbps) */
221 a41b2ff2 pbrook
        TxIFG96 = (3 << TxIFGShift),    /* 9.6us / 960ns (10 / 100Mbps) */
222 a41b2ff2 pbrook
223 a41b2ff2 pbrook
    TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
224 a41b2ff2 pbrook
    TxCRC = (1 << 16),    /* DISABLE appending CRC to end of Tx packets */
225 a41b2ff2 pbrook
    TxClearAbt = (1 << 0),    /* Clear abort (WO) */
226 a41b2ff2 pbrook
    TxDMAShift = 8,        /* DMA burst value (0-7) is shifted this many bits */
227 a41b2ff2 pbrook
    TxRetryShift = 4,    /* TXRR value (0-15) is shifted this many bits */
228 a41b2ff2 pbrook
229 a41b2ff2 pbrook
    TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
230 a41b2ff2 pbrook
};
231 a41b2ff2 pbrook
232 a41b2ff2 pbrook
233 a41b2ff2 pbrook
/* Transmit Status of All Descriptors (TSAD) Register */
234 a41b2ff2 pbrook
enum TSAD_bits {
235 a41b2ff2 pbrook
 TSAD_TOK3 = 1<<15, // TOK bit of Descriptor 3
236 a41b2ff2 pbrook
 TSAD_TOK2 = 1<<14, // TOK bit of Descriptor 2
237 a41b2ff2 pbrook
 TSAD_TOK1 = 1<<13, // TOK bit of Descriptor 1
238 a41b2ff2 pbrook
 TSAD_TOK0 = 1<<12, // TOK bit of Descriptor 0
239 a41b2ff2 pbrook
 TSAD_TUN3 = 1<<11, // TUN bit of Descriptor 3
240 a41b2ff2 pbrook
 TSAD_TUN2 = 1<<10, // TUN bit of Descriptor 2
241 a41b2ff2 pbrook
 TSAD_TUN1 = 1<<9, // TUN bit of Descriptor 1
242 a41b2ff2 pbrook
 TSAD_TUN0 = 1<<8, // TUN bit of Descriptor 0
243 a41b2ff2 pbrook
 TSAD_TABT3 = 1<<07, // TABT bit of Descriptor 3
244 a41b2ff2 pbrook
 TSAD_TABT2 = 1<<06, // TABT bit of Descriptor 2
245 a41b2ff2 pbrook
 TSAD_TABT1 = 1<<05, // TABT bit of Descriptor 1
246 a41b2ff2 pbrook
 TSAD_TABT0 = 1<<04, // TABT bit of Descriptor 0
247 a41b2ff2 pbrook
 TSAD_OWN3 = 1<<03, // OWN bit of Descriptor 3
248 a41b2ff2 pbrook
 TSAD_OWN2 = 1<<02, // OWN bit of Descriptor 2
249 a41b2ff2 pbrook
 TSAD_OWN1 = 1<<01, // OWN bit of Descriptor 1
250 a41b2ff2 pbrook
 TSAD_OWN0 = 1<<00, // OWN bit of Descriptor 0
251 a41b2ff2 pbrook
};
252 a41b2ff2 pbrook
253 a41b2ff2 pbrook
254 a41b2ff2 pbrook
/* Bits in Config1 */
255 a41b2ff2 pbrook
enum Config1Bits {
256 a41b2ff2 pbrook
    Cfg1_PM_Enable = 0x01,
257 a41b2ff2 pbrook
    Cfg1_VPD_Enable = 0x02,
258 a41b2ff2 pbrook
    Cfg1_PIO = 0x04,
259 a41b2ff2 pbrook
    Cfg1_MMIO = 0x08,
260 a41b2ff2 pbrook
    LWAKE = 0x10,        /* not on 8139, 8139A */
261 a41b2ff2 pbrook
    Cfg1_Driver_Load = 0x20,
262 a41b2ff2 pbrook
    Cfg1_LED0 = 0x40,
263 a41b2ff2 pbrook
    Cfg1_LED1 = 0x80,
264 a41b2ff2 pbrook
    SLEEP = (1 << 1),    /* only on 8139, 8139A */
265 a41b2ff2 pbrook
    PWRDN = (1 << 0),    /* only on 8139, 8139A */
266 a41b2ff2 pbrook
};
267 a41b2ff2 pbrook
268 a41b2ff2 pbrook
/* Bits in Config3 */
269 a41b2ff2 pbrook
enum Config3Bits {
270 a41b2ff2 pbrook
    Cfg3_FBtBEn    = (1 << 0), /* 1 = Fast Back to Back */
271 a41b2ff2 pbrook
    Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
272 a41b2ff2 pbrook
    Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
273 a41b2ff2 pbrook
    Cfg3_CardB_En  = (1 << 3), /* 1 = enable CardBus registers */
274 a41b2ff2 pbrook
    Cfg3_LinkUp    = (1 << 4), /* 1 = wake up on link up */
275 a41b2ff2 pbrook
    Cfg3_Magic     = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
276 a41b2ff2 pbrook
    Cfg3_PARM_En   = (1 << 6), /* 0 = software can set twister parameters */
277 a41b2ff2 pbrook
    Cfg3_GNTSel    = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
278 a41b2ff2 pbrook
};
279 a41b2ff2 pbrook
280 a41b2ff2 pbrook
/* Bits in Config4 */
281 a41b2ff2 pbrook
enum Config4Bits {
282 a41b2ff2 pbrook
    LWPTN = (1 << 2),    /* not on 8139, 8139A */
283 a41b2ff2 pbrook
};
284 a41b2ff2 pbrook
285 a41b2ff2 pbrook
/* Bits in Config5 */
286 a41b2ff2 pbrook
enum Config5Bits {
287 a41b2ff2 pbrook
    Cfg5_PME_STS     = (1 << 0), /* 1 = PCI reset resets PME_Status */
288 a41b2ff2 pbrook
    Cfg5_LANWake     = (1 << 1), /* 1 = enable LANWake signal */
289 a41b2ff2 pbrook
    Cfg5_LDPS        = (1 << 2), /* 0 = save power when link is down */
290 a41b2ff2 pbrook
    Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */
291 a41b2ff2 pbrook
    Cfg5_UWF         = (1 << 4), /* 1 = accept unicast wakeup frame */
292 a41b2ff2 pbrook
    Cfg5_MWF         = (1 << 5), /* 1 = accept multicast wakeup frame */
293 a41b2ff2 pbrook
    Cfg5_BWF         = (1 << 6), /* 1 = accept broadcast wakeup frame */
294 a41b2ff2 pbrook
};
295 a41b2ff2 pbrook
296 a41b2ff2 pbrook
enum RxConfigBits {
297 a41b2ff2 pbrook
    /* rx fifo threshold */
298 a41b2ff2 pbrook
    RxCfgFIFOShift = 13,
299 a41b2ff2 pbrook
    RxCfgFIFONone = (7 << RxCfgFIFOShift),
300 a41b2ff2 pbrook
301 a41b2ff2 pbrook
    /* Max DMA burst */
302 a41b2ff2 pbrook
    RxCfgDMAShift = 8,
303 a41b2ff2 pbrook
    RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
304 a41b2ff2 pbrook
305 a41b2ff2 pbrook
    /* rx ring buffer length */
306 a41b2ff2 pbrook
    RxCfgRcv8K = 0,
307 a41b2ff2 pbrook
    RxCfgRcv16K = (1 << 11),
308 a41b2ff2 pbrook
    RxCfgRcv32K = (1 << 12),
309 a41b2ff2 pbrook
    RxCfgRcv64K = (1 << 11) | (1 << 12),
310 a41b2ff2 pbrook
311 a41b2ff2 pbrook
    /* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
312 a41b2ff2 pbrook
    RxNoWrap = (1 << 7),
313 a41b2ff2 pbrook
};
314 a41b2ff2 pbrook
315 a41b2ff2 pbrook
/* Twister tuning parameters from RealTek.
316 a41b2ff2 pbrook
   Completely undocumented, but required to tune bad links on some boards. */
317 a41b2ff2 pbrook
/*
318 a41b2ff2 pbrook
enum CSCRBits {
319 a41b2ff2 pbrook
    CSCR_LinkOKBit = 0x0400,
320 a41b2ff2 pbrook
    CSCR_LinkChangeBit = 0x0800,
321 a41b2ff2 pbrook
    CSCR_LinkStatusBits = 0x0f000,
322 a41b2ff2 pbrook
    CSCR_LinkDownOffCmd = 0x003c0,
323 a41b2ff2 pbrook
    CSCR_LinkDownCmd = 0x0f3c0,
324 a41b2ff2 pbrook
*/
325 a41b2ff2 pbrook
enum CSCRBits {
326 5fafdf24 ths
    CSCR_Testfun = 1<<15, /* 1 = Auto-neg speeds up internal timer, WO, def 0 */
327 a41b2ff2 pbrook
    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*/
328 a41b2ff2 pbrook
    CSCR_HEART_BIT = 1<<8,  /* 1 = HEART BEAT enable, 0 = HEART BEAT disable. HEART BEAT function is only valid in 10Mbps mode. def 1*/
329 a41b2ff2 pbrook
    CSCR_JBEN = 1<<7,  /* 1 = enable jabber function. 0 = disable jabber function, def 1*/
330 5fafdf24 ths
    CSCR_F_LINK_100 = 1<<6, /* Used to login force good link in 100Mbps for diagnostic purposes. 1 = DISABLE, 0 = ENABLE. def 1*/
331 a41b2ff2 pbrook
    CSCR_F_Connect  = 1<<5,  /* Assertion of this bit forces the disconnect function to be bypassed. def 0*/
332 a41b2ff2 pbrook
    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*/
333 a41b2ff2 pbrook
    CSCR_Con_status_En = 1<<2, /* Assertion of this bit configures LED1 pin to indicate connection status. def 0*/
334 a41b2ff2 pbrook
    CSCR_PASS_SCR = 1<<0, /* Bypass Scramble, def 0*/
335 a41b2ff2 pbrook
};
336 a41b2ff2 pbrook
337 a41b2ff2 pbrook
enum Cfg9346Bits {
338 a41b2ff2 pbrook
    Cfg9346_Lock = 0x00,
339 a41b2ff2 pbrook
    Cfg9346_Unlock = 0xC0,
340 a41b2ff2 pbrook
};
341 a41b2ff2 pbrook
342 a41b2ff2 pbrook
typedef enum {
343 a41b2ff2 pbrook
    CH_8139 = 0,
344 a41b2ff2 pbrook
    CH_8139_K,
345 a41b2ff2 pbrook
    CH_8139A,
346 a41b2ff2 pbrook
    CH_8139A_G,
347 a41b2ff2 pbrook
    CH_8139B,
348 a41b2ff2 pbrook
    CH_8130,
349 a41b2ff2 pbrook
    CH_8139C,
350 a41b2ff2 pbrook
    CH_8100,
351 a41b2ff2 pbrook
    CH_8100B_8139D,
352 a41b2ff2 pbrook
    CH_8101,
353 c227f099 Anthony Liguori
} chip_t;
354 a41b2ff2 pbrook
355 a41b2ff2 pbrook
enum chip_flags {
356 a41b2ff2 pbrook
    HasHltClk = (1 << 0),
357 a41b2ff2 pbrook
    HasLWake = (1 << 1),
358 a41b2ff2 pbrook
};
359 a41b2ff2 pbrook
360 a41b2ff2 pbrook
#define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
361 a41b2ff2 pbrook
    (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
362 a41b2ff2 pbrook
#define HW_REVID_MASK    HW_REVID(1, 1, 1, 1, 1, 1, 1)
363 a41b2ff2 pbrook
364 6cadb320 bellard
#define RTL8139_PCI_REVID_8139      0x10
365 6cadb320 bellard
#define RTL8139_PCI_REVID_8139CPLUS 0x20
366 6cadb320 bellard
367 6cadb320 bellard
#define RTL8139_PCI_REVID           RTL8139_PCI_REVID_8139CPLUS
368 6cadb320 bellard
369 a41b2ff2 pbrook
/* Size is 64 * 16bit words */
370 a41b2ff2 pbrook
#define EEPROM_9346_ADDR_BITS 6
371 a41b2ff2 pbrook
#define EEPROM_9346_SIZE  (1 << EEPROM_9346_ADDR_BITS)
372 a41b2ff2 pbrook
#define EEPROM_9346_ADDR_MASK (EEPROM_9346_SIZE - 1)
373 a41b2ff2 pbrook
374 a41b2ff2 pbrook
enum Chip9346Operation
375 a41b2ff2 pbrook
{
376 a41b2ff2 pbrook
    Chip9346_op_mask = 0xc0,          /* 10 zzzzzz */
377 a41b2ff2 pbrook
    Chip9346_op_read = 0x80,          /* 10 AAAAAA */
378 a41b2ff2 pbrook
    Chip9346_op_write = 0x40,         /* 01 AAAAAA D(15)..D(0) */
379 a41b2ff2 pbrook
    Chip9346_op_ext_mask = 0xf0,      /* 11 zzzzzz */
380 a41b2ff2 pbrook
    Chip9346_op_write_enable = 0x30,  /* 00 11zzzz */
381 a41b2ff2 pbrook
    Chip9346_op_write_all = 0x10,     /* 00 01zzzz */
382 a41b2ff2 pbrook
    Chip9346_op_write_disable = 0x00, /* 00 00zzzz */
383 a41b2ff2 pbrook
};
384 a41b2ff2 pbrook
385 a41b2ff2 pbrook
enum Chip9346Mode
386 a41b2ff2 pbrook
{
387 a41b2ff2 pbrook
    Chip9346_none = 0,
388 a41b2ff2 pbrook
    Chip9346_enter_command_mode,
389 a41b2ff2 pbrook
    Chip9346_read_command,
390 a41b2ff2 pbrook
    Chip9346_data_read,      /* from output register */
391 a41b2ff2 pbrook
    Chip9346_data_write,     /* to input register, then to contents at specified address */
392 a41b2ff2 pbrook
    Chip9346_data_write_all, /* to input register, then filling contents */
393 a41b2ff2 pbrook
};
394 a41b2ff2 pbrook
395 a41b2ff2 pbrook
typedef struct EEprom9346
396 a41b2ff2 pbrook
{
397 a41b2ff2 pbrook
    uint16_t contents[EEPROM_9346_SIZE];
398 a41b2ff2 pbrook
    int      mode;
399 a41b2ff2 pbrook
    uint32_t tick;
400 a41b2ff2 pbrook
    uint8_t  address;
401 a41b2ff2 pbrook
    uint16_t input;
402 a41b2ff2 pbrook
    uint16_t output;
403 a41b2ff2 pbrook
404 a41b2ff2 pbrook
    uint8_t eecs;
405 a41b2ff2 pbrook
    uint8_t eesk;
406 a41b2ff2 pbrook
    uint8_t eedi;
407 a41b2ff2 pbrook
    uint8_t eedo;
408 a41b2ff2 pbrook
} EEprom9346;
409 a41b2ff2 pbrook
410 6cadb320 bellard
typedef struct RTL8139TallyCounters
411 6cadb320 bellard
{
412 6cadb320 bellard
    /* Tally counters */
413 6cadb320 bellard
    uint64_t   TxOk;
414 6cadb320 bellard
    uint64_t   RxOk;
415 6cadb320 bellard
    uint64_t   TxERR;
416 6cadb320 bellard
    uint32_t   RxERR;
417 6cadb320 bellard
    uint16_t   MissPkt;
418 6cadb320 bellard
    uint16_t   FAE;
419 6cadb320 bellard
    uint32_t   Tx1Col;
420 6cadb320 bellard
    uint32_t   TxMCol;
421 6cadb320 bellard
    uint64_t   RxOkPhy;
422 6cadb320 bellard
    uint64_t   RxOkBrd;
423 6cadb320 bellard
    uint32_t   RxOkMul;
424 6cadb320 bellard
    uint16_t   TxAbt;
425 6cadb320 bellard
    uint16_t   TxUndrn;
426 6cadb320 bellard
} RTL8139TallyCounters;
427 6cadb320 bellard
428 6cadb320 bellard
/* Clears all tally counters */
429 6cadb320 bellard
static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters);
430 6cadb320 bellard
431 a41b2ff2 pbrook
typedef struct RTL8139State {
432 efd6dd45 Juan Quintela
    PCIDevice dev;
433 a41b2ff2 pbrook
    uint8_t phys[8]; /* mac address */
434 a41b2ff2 pbrook
    uint8_t mult[8]; /* multicast mask array */
435 a41b2ff2 pbrook
436 6cadb320 bellard
    uint32_t TxStatus[4]; /* TxStatus0 in C mode*/ /* also DTCCR[0] and DTCCR[1] in C+ mode */
437 a41b2ff2 pbrook
    uint32_t TxAddr[4];   /* TxAddr0 */
438 a41b2ff2 pbrook
    uint32_t RxBuf;       /* Receive buffer */
439 a41b2ff2 pbrook
    uint32_t RxBufferSize;/* internal variable, receive ring buffer size in C mode */
440 a41b2ff2 pbrook
    uint32_t RxBufPtr;
441 a41b2ff2 pbrook
    uint32_t RxBufAddr;
442 a41b2ff2 pbrook
443 a41b2ff2 pbrook
    uint16_t IntrStatus;
444 a41b2ff2 pbrook
    uint16_t IntrMask;
445 a41b2ff2 pbrook
446 a41b2ff2 pbrook
    uint32_t TxConfig;
447 a41b2ff2 pbrook
    uint32_t RxConfig;
448 a41b2ff2 pbrook
    uint32_t RxMissed;
449 a41b2ff2 pbrook
450 a41b2ff2 pbrook
    uint16_t CSCR;
451 a41b2ff2 pbrook
452 a41b2ff2 pbrook
    uint8_t  Cfg9346;
453 a41b2ff2 pbrook
    uint8_t  Config0;
454 a41b2ff2 pbrook
    uint8_t  Config1;
455 a41b2ff2 pbrook
    uint8_t  Config3;
456 a41b2ff2 pbrook
    uint8_t  Config4;
457 a41b2ff2 pbrook
    uint8_t  Config5;
458 a41b2ff2 pbrook
459 a41b2ff2 pbrook
    uint8_t  clock_enabled;
460 a41b2ff2 pbrook
    uint8_t  bChipCmdState;
461 a41b2ff2 pbrook
462 a41b2ff2 pbrook
    uint16_t MultiIntr;
463 a41b2ff2 pbrook
464 a41b2ff2 pbrook
    uint16_t BasicModeCtrl;
465 a41b2ff2 pbrook
    uint16_t BasicModeStatus;
466 a41b2ff2 pbrook
    uint16_t NWayAdvert;
467 a41b2ff2 pbrook
    uint16_t NWayLPAR;
468 a41b2ff2 pbrook
    uint16_t NWayExpansion;
469 a41b2ff2 pbrook
470 a41b2ff2 pbrook
    uint16_t CpCmd;
471 a41b2ff2 pbrook
    uint8_t  TxThresh;
472 a41b2ff2 pbrook
473 1673ad51 Mark McLoughlin
    NICState *nic;
474 254111ec Gerd Hoffmann
    NICConf conf;
475 a41b2ff2 pbrook
476 a41b2ff2 pbrook
    /* C ring mode */
477 a41b2ff2 pbrook
    uint32_t   currTxDesc;
478 a41b2ff2 pbrook
479 a41b2ff2 pbrook
    /* C+ mode */
480 2c3891ab aliguori
    uint32_t   cplus_enabled;
481 2c3891ab aliguori
482 a41b2ff2 pbrook
    uint32_t   currCPlusRxDesc;
483 a41b2ff2 pbrook
    uint32_t   currCPlusTxDesc;
484 a41b2ff2 pbrook
485 a41b2ff2 pbrook
    uint32_t   RxRingAddrLO;
486 a41b2ff2 pbrook
    uint32_t   RxRingAddrHI;
487 a41b2ff2 pbrook
488 a41b2ff2 pbrook
    EEprom9346 eeprom;
489 6cadb320 bellard
490 6cadb320 bellard
    uint32_t   TCTR;
491 6cadb320 bellard
    uint32_t   TimerInt;
492 6cadb320 bellard
    int64_t    TCTR_base;
493 6cadb320 bellard
494 6cadb320 bellard
    /* Tally counters */
495 6cadb320 bellard
    RTL8139TallyCounters tally_counters;
496 6cadb320 bellard
497 6cadb320 bellard
    /* Non-persistent data */
498 6cadb320 bellard
    uint8_t   *cplus_txbuffer;
499 6cadb320 bellard
    int        cplus_txbuffer_len;
500 6cadb320 bellard
    int        cplus_txbuffer_offset;
501 6cadb320 bellard
502 6cadb320 bellard
    /* PCI interrupt timer */
503 6cadb320 bellard
    QEMUTimer *timer;
504 05447803 Frediano Ziglio
    int64_t TimerExpire;
505 6cadb320 bellard
506 bd80f3fc Avi Kivity
    MemoryRegion bar_io;
507 bd80f3fc Avi Kivity
    MemoryRegion bar_mem;
508 bd80f3fc Avi Kivity
509 c574ba5a Alex Williamson
    /* Support migration to/from old versions */
510 c574ba5a Alex Williamson
    int rtl8139_mmio_io_addr_dummy;
511 a41b2ff2 pbrook
} RTL8139State;
512 a41b2ff2 pbrook
513 3ada003a Eduard - Gabriel Munteanu
/* Writes tally counters to memory via DMA */
514 3ada003a Eduard - Gabriel Munteanu
static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr);
515 3ada003a Eduard - Gabriel Munteanu
516 05447803 Frediano Ziglio
static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time);
517 05447803 Frediano Ziglio
518 9596ebb7 pbrook
static void prom9346_decode_command(EEprom9346 *eeprom, uint8_t command)
519 a41b2ff2 pbrook
{
520 7cdeb319 Benjamin Poirier
    DPRINTF("eeprom command 0x%02x\n", command);
521 a41b2ff2 pbrook
522 a41b2ff2 pbrook
    switch (command & Chip9346_op_mask)
523 a41b2ff2 pbrook
    {
524 a41b2ff2 pbrook
        case Chip9346_op_read:
525 a41b2ff2 pbrook
        {
526 a41b2ff2 pbrook
            eeprom->address = command & EEPROM_9346_ADDR_MASK;
527 a41b2ff2 pbrook
            eeprom->output = eeprom->contents[eeprom->address];
528 a41b2ff2 pbrook
            eeprom->eedo = 0;
529 a41b2ff2 pbrook
            eeprom->tick = 0;
530 a41b2ff2 pbrook
            eeprom->mode = Chip9346_data_read;
531 7cdeb319 Benjamin Poirier
            DPRINTF("eeprom read from address 0x%02x data=0x%04x\n",
532 7cdeb319 Benjamin Poirier
                eeprom->address, eeprom->output);
533 a41b2ff2 pbrook
        }
534 a41b2ff2 pbrook
        break;
535 a41b2ff2 pbrook
536 a41b2ff2 pbrook
        case Chip9346_op_write:
537 a41b2ff2 pbrook
        {
538 a41b2ff2 pbrook
            eeprom->address = command & EEPROM_9346_ADDR_MASK;
539 a41b2ff2 pbrook
            eeprom->input = 0;
540 a41b2ff2 pbrook
            eeprom->tick = 0;
541 a41b2ff2 pbrook
            eeprom->mode = Chip9346_none; /* Chip9346_data_write */
542 7cdeb319 Benjamin Poirier
            DPRINTF("eeprom begin write to address 0x%02x\n",
543 7cdeb319 Benjamin Poirier
                eeprom->address);
544 a41b2ff2 pbrook
        }
545 a41b2ff2 pbrook
        break;
546 a41b2ff2 pbrook
        default:
547 a41b2ff2 pbrook
            eeprom->mode = Chip9346_none;
548 a41b2ff2 pbrook
            switch (command & Chip9346_op_ext_mask)
549 a41b2ff2 pbrook
            {
550 a41b2ff2 pbrook
                case Chip9346_op_write_enable:
551 7cdeb319 Benjamin Poirier
                    DPRINTF("eeprom write enabled\n");
552 a41b2ff2 pbrook
                    break;
553 a41b2ff2 pbrook
                case Chip9346_op_write_all:
554 7cdeb319 Benjamin Poirier
                    DPRINTF("eeprom begin write all\n");
555 a41b2ff2 pbrook
                    break;
556 a41b2ff2 pbrook
                case Chip9346_op_write_disable:
557 7cdeb319 Benjamin Poirier
                    DPRINTF("eeprom write disabled\n");
558 a41b2ff2 pbrook
                    break;
559 a41b2ff2 pbrook
            }
560 a41b2ff2 pbrook
            break;
561 a41b2ff2 pbrook
    }
562 a41b2ff2 pbrook
}
563 a41b2ff2 pbrook
564 9596ebb7 pbrook
static void prom9346_shift_clock(EEprom9346 *eeprom)
565 a41b2ff2 pbrook
{
566 a41b2ff2 pbrook
    int bit = eeprom->eedi?1:0;
567 a41b2ff2 pbrook
568 a41b2ff2 pbrook
    ++ eeprom->tick;
569 a41b2ff2 pbrook
570 7cdeb319 Benjamin Poirier
    DPRINTF("eeprom: tick %d eedi=%d eedo=%d\n", eeprom->tick, eeprom->eedi,
571 7cdeb319 Benjamin Poirier
        eeprom->eedo);
572 a41b2ff2 pbrook
573 a41b2ff2 pbrook
    switch (eeprom->mode)
574 a41b2ff2 pbrook
    {
575 a41b2ff2 pbrook
        case Chip9346_enter_command_mode:
576 a41b2ff2 pbrook
            if (bit)
577 a41b2ff2 pbrook
            {
578 a41b2ff2 pbrook
                eeprom->mode = Chip9346_read_command;
579 a41b2ff2 pbrook
                eeprom->tick = 0;
580 a41b2ff2 pbrook
                eeprom->input = 0;
581 7cdeb319 Benjamin Poirier
                DPRINTF("eeprom: +++ synchronized, begin command read\n");
582 a41b2ff2 pbrook
            }
583 a41b2ff2 pbrook
            break;
584 a41b2ff2 pbrook
585 a41b2ff2 pbrook
        case Chip9346_read_command:
586 a41b2ff2 pbrook
            eeprom->input = (eeprom->input << 1) | (bit & 1);
587 a41b2ff2 pbrook
            if (eeprom->tick == 8)
588 a41b2ff2 pbrook
            {
589 a41b2ff2 pbrook
                prom9346_decode_command(eeprom, eeprom->input & 0xff);
590 a41b2ff2 pbrook
            }
591 a41b2ff2 pbrook
            break;
592 a41b2ff2 pbrook
593 a41b2ff2 pbrook
        case Chip9346_data_read:
594 a41b2ff2 pbrook
            eeprom->eedo = (eeprom->output & 0x8000)?1:0;
595 a41b2ff2 pbrook
            eeprom->output <<= 1;
596 a41b2ff2 pbrook
            if (eeprom->tick == 16)
597 a41b2ff2 pbrook
            {
598 6cadb320 bellard
#if 1
599 6cadb320 bellard
        // the FreeBSD drivers (rl and re) don't explicitly toggle
600 6cadb320 bellard
        // CS between reads (or does setting Cfg9346 to 0 count too?),
601 6cadb320 bellard
        // so we need to enter wait-for-command state here
602 6cadb320 bellard
                eeprom->mode = Chip9346_enter_command_mode;
603 6cadb320 bellard
                eeprom->input = 0;
604 6cadb320 bellard
                eeprom->tick = 0;
605 6cadb320 bellard
606 7cdeb319 Benjamin Poirier
                DPRINTF("eeprom: +++ end of read, awaiting next command\n");
607 6cadb320 bellard
#else
608 6cadb320 bellard
        // original behaviour
609 a41b2ff2 pbrook
                ++eeprom->address;
610 a41b2ff2 pbrook
                eeprom->address &= EEPROM_9346_ADDR_MASK;
611 a41b2ff2 pbrook
                eeprom->output = eeprom->contents[eeprom->address];
612 a41b2ff2 pbrook
                eeprom->tick = 0;
613 a41b2ff2 pbrook
614 7cdeb319 Benjamin Poirier
                DPRINTF("eeprom: +++ read next address 0x%02x data=0x%04x\n",
615 7cdeb319 Benjamin Poirier
                    eeprom->address, eeprom->output);
616 a41b2ff2 pbrook
#endif
617 a41b2ff2 pbrook
            }
618 a41b2ff2 pbrook
            break;
619 a41b2ff2 pbrook
620 a41b2ff2 pbrook
        case Chip9346_data_write:
621 a41b2ff2 pbrook
            eeprom->input = (eeprom->input << 1) | (bit & 1);
622 a41b2ff2 pbrook
            if (eeprom->tick == 16)
623 a41b2ff2 pbrook
            {
624 7cdeb319 Benjamin Poirier
                DPRINTF("eeprom write to address 0x%02x data=0x%04x\n",
625 7cdeb319 Benjamin Poirier
                    eeprom->address, eeprom->input);
626 6cadb320 bellard
627 a41b2ff2 pbrook
                eeprom->contents[eeprom->address] = eeprom->input;
628 a41b2ff2 pbrook
                eeprom->mode = Chip9346_none; /* waiting for next command after CS cycle */
629 a41b2ff2 pbrook
                eeprom->tick = 0;
630 a41b2ff2 pbrook
                eeprom->input = 0;
631 a41b2ff2 pbrook
            }
632 a41b2ff2 pbrook
            break;
633 a41b2ff2 pbrook
634 a41b2ff2 pbrook
        case Chip9346_data_write_all:
635 a41b2ff2 pbrook
            eeprom->input = (eeprom->input << 1) | (bit & 1);
636 a41b2ff2 pbrook
            if (eeprom->tick == 16)
637 a41b2ff2 pbrook
            {
638 a41b2ff2 pbrook
                int i;
639 a41b2ff2 pbrook
                for (i = 0; i < EEPROM_9346_SIZE; i++)
640 a41b2ff2 pbrook
                {
641 a41b2ff2 pbrook
                    eeprom->contents[i] = eeprom->input;
642 a41b2ff2 pbrook
                }
643 7cdeb319 Benjamin Poirier
                DPRINTF("eeprom filled with data=0x%04x\n", eeprom->input);
644 6cadb320 bellard
645 a41b2ff2 pbrook
                eeprom->mode = Chip9346_enter_command_mode;
646 a41b2ff2 pbrook
                eeprom->tick = 0;
647 a41b2ff2 pbrook
                eeprom->input = 0;
648 a41b2ff2 pbrook
            }
649 a41b2ff2 pbrook
            break;
650 a41b2ff2 pbrook
651 a41b2ff2 pbrook
        default:
652 a41b2ff2 pbrook
            break;
653 a41b2ff2 pbrook
    }
654 a41b2ff2 pbrook
}
655 a41b2ff2 pbrook
656 9596ebb7 pbrook
static int prom9346_get_wire(RTL8139State *s)
657 a41b2ff2 pbrook
{
658 a41b2ff2 pbrook
    EEprom9346 *eeprom = &s->eeprom;
659 a41b2ff2 pbrook
    if (!eeprom->eecs)
660 a41b2ff2 pbrook
        return 0;
661 a41b2ff2 pbrook
662 a41b2ff2 pbrook
    return eeprom->eedo;
663 a41b2ff2 pbrook
}
664 a41b2ff2 pbrook
665 9596ebb7 pbrook
/* FIXME: This should be merged into/replaced by eeprom93xx.c.  */
666 9596ebb7 pbrook
static void prom9346_set_wire(RTL8139State *s, int eecs, int eesk, int eedi)
667 a41b2ff2 pbrook
{
668 a41b2ff2 pbrook
    EEprom9346 *eeprom = &s->eeprom;
669 a41b2ff2 pbrook
    uint8_t old_eecs = eeprom->eecs;
670 a41b2ff2 pbrook
    uint8_t old_eesk = eeprom->eesk;
671 a41b2ff2 pbrook
672 a41b2ff2 pbrook
    eeprom->eecs = eecs;
673 a41b2ff2 pbrook
    eeprom->eesk = eesk;
674 a41b2ff2 pbrook
    eeprom->eedi = eedi;
675 a41b2ff2 pbrook
676 7cdeb319 Benjamin Poirier
    DPRINTF("eeprom: +++ wires CS=%d SK=%d DI=%d DO=%d\n", eeprom->eecs,
677 7cdeb319 Benjamin Poirier
        eeprom->eesk, eeprom->eedi, eeprom->eedo);
678 a41b2ff2 pbrook
679 a41b2ff2 pbrook
    if (!old_eecs && eecs)
680 a41b2ff2 pbrook
    {
681 a41b2ff2 pbrook
        /* Synchronize start */
682 a41b2ff2 pbrook
        eeprom->tick = 0;
683 a41b2ff2 pbrook
        eeprom->input = 0;
684 a41b2ff2 pbrook
        eeprom->output = 0;
685 a41b2ff2 pbrook
        eeprom->mode = Chip9346_enter_command_mode;
686 a41b2ff2 pbrook
687 7cdeb319 Benjamin Poirier
        DPRINTF("=== eeprom: begin access, enter command mode\n");
688 a41b2ff2 pbrook
    }
689 a41b2ff2 pbrook
690 a41b2ff2 pbrook
    if (!eecs)
691 a41b2ff2 pbrook
    {
692 7cdeb319 Benjamin Poirier
        DPRINTF("=== eeprom: end access\n");
693 a41b2ff2 pbrook
        return;
694 a41b2ff2 pbrook
    }
695 a41b2ff2 pbrook
696 a41b2ff2 pbrook
    if (!old_eesk && eesk)
697 a41b2ff2 pbrook
    {
698 a41b2ff2 pbrook
        /* SK front rules */
699 a41b2ff2 pbrook
        prom9346_shift_clock(eeprom);
700 a41b2ff2 pbrook
    }
701 a41b2ff2 pbrook
}
702 a41b2ff2 pbrook
703 a41b2ff2 pbrook
static void rtl8139_update_irq(RTL8139State *s)
704 a41b2ff2 pbrook
{
705 a41b2ff2 pbrook
    int isr;
706 a41b2ff2 pbrook
    isr = (s->IntrStatus & s->IntrMask) & 0xffff;
707 6cadb320 bellard
708 7cdeb319 Benjamin Poirier
    DPRINTF("Set IRQ to %d (%04x %04x)\n", isr ? 1 : 0, s->IntrStatus,
709 7cdeb319 Benjamin Poirier
        s->IntrMask);
710 6cadb320 bellard
711 efd6dd45 Juan Quintela
    qemu_set_irq(s->dev.irq[0], (isr != 0));
712 a41b2ff2 pbrook
}
713 a41b2ff2 pbrook
714 a41b2ff2 pbrook
#define POLYNOMIAL 0x04c11db6
715 a41b2ff2 pbrook
716 a41b2ff2 pbrook
/* From FreeBSD */
717 a41b2ff2 pbrook
/* XXX: optimize */
718 a41b2ff2 pbrook
static int compute_mcast_idx(const uint8_t *ep)
719 a41b2ff2 pbrook
{
720 a41b2ff2 pbrook
    uint32_t crc;
721 a41b2ff2 pbrook
    int carry, i, j;
722 a41b2ff2 pbrook
    uint8_t b;
723 a41b2ff2 pbrook
724 a41b2ff2 pbrook
    crc = 0xffffffff;
725 a41b2ff2 pbrook
    for (i = 0; i < 6; i++) {
726 a41b2ff2 pbrook
        b = *ep++;
727 a41b2ff2 pbrook
        for (j = 0; j < 8; j++) {
728 a41b2ff2 pbrook
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
729 a41b2ff2 pbrook
            crc <<= 1;
730 a41b2ff2 pbrook
            b >>= 1;
731 a41b2ff2 pbrook
            if (carry)
732 a41b2ff2 pbrook
                crc = ((crc ^ POLYNOMIAL) | carry);
733 a41b2ff2 pbrook
        }
734 a41b2ff2 pbrook
    }
735 a41b2ff2 pbrook
    return (crc >> 26);
736 a41b2ff2 pbrook
}
737 a41b2ff2 pbrook
738 a41b2ff2 pbrook
static int rtl8139_RxWrap(RTL8139State *s)
739 a41b2ff2 pbrook
{
740 a41b2ff2 pbrook
    /* wrapping enabled; assume 1.5k more buffer space if size < 65536 */
741 a41b2ff2 pbrook
    return (s->RxConfig & (1 << 7));
742 a41b2ff2 pbrook
}
743 a41b2ff2 pbrook
744 a41b2ff2 pbrook
static int rtl8139_receiver_enabled(RTL8139State *s)
745 a41b2ff2 pbrook
{
746 a41b2ff2 pbrook
    return s->bChipCmdState & CmdRxEnb;
747 a41b2ff2 pbrook
}
748 a41b2ff2 pbrook
749 a41b2ff2 pbrook
static int rtl8139_transmitter_enabled(RTL8139State *s)
750 a41b2ff2 pbrook
{
751 a41b2ff2 pbrook
    return s->bChipCmdState & CmdTxEnb;
752 a41b2ff2 pbrook
}
753 a41b2ff2 pbrook
754 a41b2ff2 pbrook
static int rtl8139_cp_receiver_enabled(RTL8139State *s)
755 a41b2ff2 pbrook
{
756 a41b2ff2 pbrook
    return s->CpCmd & CPlusRxEnb;
757 a41b2ff2 pbrook
}
758 a41b2ff2 pbrook
759 a41b2ff2 pbrook
static int rtl8139_cp_transmitter_enabled(RTL8139State *s)
760 a41b2ff2 pbrook
{
761 a41b2ff2 pbrook
    return s->CpCmd & CPlusTxEnb;
762 a41b2ff2 pbrook
}
763 a41b2ff2 pbrook
764 a41b2ff2 pbrook
static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size)
765 a41b2ff2 pbrook
{
766 a41b2ff2 pbrook
    if (s->RxBufAddr + size > s->RxBufferSize)
767 a41b2ff2 pbrook
    {
768 a41b2ff2 pbrook
        int wrapped = MOD2(s->RxBufAddr + size, s->RxBufferSize);
769 a41b2ff2 pbrook
770 a41b2ff2 pbrook
        /* write packet data */
771 ccf1d14a ths
        if (wrapped && !(s->RxBufferSize < 65536 && rtl8139_RxWrap(s)))
772 a41b2ff2 pbrook
        {
773 7cdeb319 Benjamin Poirier
            DPRINTF(">>> rx packet wrapped in buffer at %d\n", size - wrapped);
774 a41b2ff2 pbrook
775 a41b2ff2 pbrook
            if (size > wrapped)
776 a41b2ff2 pbrook
            {
777 3ada003a Eduard - Gabriel Munteanu
                pci_dma_write(&s->dev, s->RxBuf + s->RxBufAddr,
778 3ada003a Eduard - Gabriel Munteanu
                              buf, size-wrapped);
779 a41b2ff2 pbrook
            }
780 a41b2ff2 pbrook
781 a41b2ff2 pbrook
            /* reset buffer pointer */
782 a41b2ff2 pbrook
            s->RxBufAddr = 0;
783 a41b2ff2 pbrook
784 3ada003a Eduard - Gabriel Munteanu
            pci_dma_write(&s->dev, s->RxBuf + s->RxBufAddr,
785 3ada003a Eduard - Gabriel Munteanu
                          buf + (size-wrapped), wrapped);
786 a41b2ff2 pbrook
787 a41b2ff2 pbrook
            s->RxBufAddr = wrapped;
788 a41b2ff2 pbrook
789 a41b2ff2 pbrook
            return;
790 a41b2ff2 pbrook
        }
791 a41b2ff2 pbrook
    }
792 a41b2ff2 pbrook
793 a41b2ff2 pbrook
    /* non-wrapping path or overwrapping enabled */
794 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, s->RxBuf + s->RxBufAddr, buf, size);
795 a41b2ff2 pbrook
796 a41b2ff2 pbrook
    s->RxBufAddr += size;
797 a41b2ff2 pbrook
}
798 a41b2ff2 pbrook
799 a41b2ff2 pbrook
#define MIN_BUF_SIZE 60
800 3ada003a Eduard - Gabriel Munteanu
static inline dma_addr_t rtl8139_addr64(uint32_t low, uint32_t high)
801 a41b2ff2 pbrook
{
802 a41b2ff2 pbrook
#if TARGET_PHYS_ADDR_BITS > 32
803 c227f099 Anthony Liguori
    return low | ((target_phys_addr_t)high << 32);
804 a41b2ff2 pbrook
#else
805 a41b2ff2 pbrook
    return low;
806 a41b2ff2 pbrook
#endif
807 a41b2ff2 pbrook
}
808 a41b2ff2 pbrook
809 1673ad51 Mark McLoughlin
static int rtl8139_can_receive(VLANClientState *nc)
810 a41b2ff2 pbrook
{
811 1673ad51 Mark McLoughlin
    RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
812 a41b2ff2 pbrook
    int avail;
813 a41b2ff2 pbrook
814 aa1f17c1 ths
    /* Receive (drop) packets if card is disabled.  */
815 a41b2ff2 pbrook
    if (!s->clock_enabled)
816 a41b2ff2 pbrook
      return 1;
817 a41b2ff2 pbrook
    if (!rtl8139_receiver_enabled(s))
818 a41b2ff2 pbrook
      return 1;
819 a41b2ff2 pbrook
820 a41b2ff2 pbrook
    if (rtl8139_cp_receiver_enabled(s)) {
821 a41b2ff2 pbrook
        /* ??? Flow control not implemented in c+ mode.
822 a41b2ff2 pbrook
           This is a hack to work around slirp deficiencies anyway.  */
823 a41b2ff2 pbrook
        return 1;
824 a41b2ff2 pbrook
    } else {
825 a41b2ff2 pbrook
        avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr,
826 a41b2ff2 pbrook
                     s->RxBufferSize);
827 a41b2ff2 pbrook
        return (avail == 0 || avail >= 1514);
828 a41b2ff2 pbrook
    }
829 a41b2ff2 pbrook
}
830 a41b2ff2 pbrook
831 1673ad51 Mark McLoughlin
static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_t size_, int do_interrupt)
832 a41b2ff2 pbrook
{
833 1673ad51 Mark McLoughlin
    RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
834 18dabfd1 Benjamin Poirier
    /* size is the length of the buffer passed to the driver */
835 4f1c942b Mark McLoughlin
    int size = size_;
836 18dabfd1 Benjamin Poirier
    const uint8_t *dot1q_buf = NULL;
837 a41b2ff2 pbrook
838 a41b2ff2 pbrook
    uint32_t packet_header = 0;
839 a41b2ff2 pbrook
840 18dabfd1 Benjamin Poirier
    uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];
841 5fafdf24 ths
    static const uint8_t broadcast_macaddr[6] =
842 a41b2ff2 pbrook
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
843 a41b2ff2 pbrook
844 7cdeb319 Benjamin Poirier
    DPRINTF(">>> received len=%d\n", size);
845 a41b2ff2 pbrook
846 a41b2ff2 pbrook
    /* test if board clock is stopped */
847 a41b2ff2 pbrook
    if (!s->clock_enabled)
848 a41b2ff2 pbrook
    {
849 7cdeb319 Benjamin Poirier
        DPRINTF("stopped ==========================\n");
850 4f1c942b Mark McLoughlin
        return -1;
851 a41b2ff2 pbrook
    }
852 a41b2ff2 pbrook
853 a41b2ff2 pbrook
    /* first check if receiver is enabled */
854 a41b2ff2 pbrook
855 a41b2ff2 pbrook
    if (!rtl8139_receiver_enabled(s))
856 a41b2ff2 pbrook
    {
857 7cdeb319 Benjamin Poirier
        DPRINTF("receiver disabled ================\n");
858 4f1c942b Mark McLoughlin
        return -1;
859 a41b2ff2 pbrook
    }
860 a41b2ff2 pbrook
861 a41b2ff2 pbrook
    /* XXX: check this */
862 a41b2ff2 pbrook
    if (s->RxConfig & AcceptAllPhys) {
863 a41b2ff2 pbrook
        /* promiscuous: receive all */
864 7cdeb319 Benjamin Poirier
        DPRINTF(">>> packet received in promiscuous mode\n");
865 a41b2ff2 pbrook
866 a41b2ff2 pbrook
    } else {
867 a41b2ff2 pbrook
        if (!memcmp(buf,  broadcast_macaddr, 6)) {
868 a41b2ff2 pbrook
            /* broadcast address */
869 a41b2ff2 pbrook
            if (!(s->RxConfig & AcceptBroadcast))
870 a41b2ff2 pbrook
            {
871 7cdeb319 Benjamin Poirier
                DPRINTF(">>> broadcast packet rejected\n");
872 6cadb320 bellard
873 6cadb320 bellard
                /* update tally counter */
874 6cadb320 bellard
                ++s->tally_counters.RxERR;
875 6cadb320 bellard
876 4f1c942b Mark McLoughlin
                return size;
877 a41b2ff2 pbrook
            }
878 a41b2ff2 pbrook
879 a41b2ff2 pbrook
            packet_header |= RxBroadcast;
880 a41b2ff2 pbrook
881 7cdeb319 Benjamin Poirier
            DPRINTF(">>> broadcast packet received\n");
882 6cadb320 bellard
883 6cadb320 bellard
            /* update tally counter */
884 6cadb320 bellard
            ++s->tally_counters.RxOkBrd;
885 6cadb320 bellard
886 a41b2ff2 pbrook
        } else if (buf[0] & 0x01) {
887 a41b2ff2 pbrook
            /* multicast */
888 a41b2ff2 pbrook
            if (!(s->RxConfig & AcceptMulticast))
889 a41b2ff2 pbrook
            {
890 7cdeb319 Benjamin Poirier
                DPRINTF(">>> multicast packet rejected\n");
891 6cadb320 bellard
892 6cadb320 bellard
                /* update tally counter */
893 6cadb320 bellard
                ++s->tally_counters.RxERR;
894 6cadb320 bellard
895 4f1c942b Mark McLoughlin
                return size;
896 a41b2ff2 pbrook
            }
897 a41b2ff2 pbrook
898 a41b2ff2 pbrook
            int mcast_idx = compute_mcast_idx(buf);
899 a41b2ff2 pbrook
900 a41b2ff2 pbrook
            if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
901 a41b2ff2 pbrook
            {
902 7cdeb319 Benjamin Poirier
                DPRINTF(">>> multicast address mismatch\n");
903 6cadb320 bellard
904 6cadb320 bellard
                /* update tally counter */
905 6cadb320 bellard
                ++s->tally_counters.RxERR;
906 6cadb320 bellard
907 4f1c942b Mark McLoughlin
                return size;
908 a41b2ff2 pbrook
            }
909 a41b2ff2 pbrook
910 a41b2ff2 pbrook
            packet_header |= RxMulticast;
911 a41b2ff2 pbrook
912 7cdeb319 Benjamin Poirier
            DPRINTF(">>> multicast packet received\n");
913 6cadb320 bellard
914 6cadb320 bellard
            /* update tally counter */
915 6cadb320 bellard
            ++s->tally_counters.RxOkMul;
916 6cadb320 bellard
917 a41b2ff2 pbrook
        } else if (s->phys[0] == buf[0] &&
918 3b46e624 ths
                   s->phys[1] == buf[1] &&
919 3b46e624 ths
                   s->phys[2] == buf[2] &&
920 3b46e624 ths
                   s->phys[3] == buf[3] &&
921 3b46e624 ths
                   s->phys[4] == buf[4] &&
922 a41b2ff2 pbrook
                   s->phys[5] == buf[5]) {
923 a41b2ff2 pbrook
            /* match */
924 a41b2ff2 pbrook
            if (!(s->RxConfig & AcceptMyPhys))
925 a41b2ff2 pbrook
            {
926 7cdeb319 Benjamin Poirier
                DPRINTF(">>> rejecting physical address matching packet\n");
927 6cadb320 bellard
928 6cadb320 bellard
                /* update tally counter */
929 6cadb320 bellard
                ++s->tally_counters.RxERR;
930 6cadb320 bellard
931 4f1c942b Mark McLoughlin
                return size;
932 a41b2ff2 pbrook
            }
933 a41b2ff2 pbrook
934 a41b2ff2 pbrook
            packet_header |= RxPhysical;
935 a41b2ff2 pbrook
936 7cdeb319 Benjamin Poirier
            DPRINTF(">>> physical address matching packet received\n");
937 6cadb320 bellard
938 6cadb320 bellard
            /* update tally counter */
939 6cadb320 bellard
            ++s->tally_counters.RxOkPhy;
940 a41b2ff2 pbrook
941 a41b2ff2 pbrook
        } else {
942 a41b2ff2 pbrook
943 7cdeb319 Benjamin Poirier
            DPRINTF(">>> unknown packet\n");
944 6cadb320 bellard
945 6cadb320 bellard
            /* update tally counter */
946 6cadb320 bellard
            ++s->tally_counters.RxERR;
947 6cadb320 bellard
948 4f1c942b Mark McLoughlin
            return size;
949 a41b2ff2 pbrook
        }
950 a41b2ff2 pbrook
    }
951 a41b2ff2 pbrook
952 18dabfd1 Benjamin Poirier
    /* if too small buffer, then expand it
953 18dabfd1 Benjamin Poirier
     * Include some tailroom in case a vlan tag is later removed. */
954 18dabfd1 Benjamin Poirier
    if (size < MIN_BUF_SIZE + VLAN_HLEN) {
955 a41b2ff2 pbrook
        memcpy(buf1, buf, size);
956 18dabfd1 Benjamin Poirier
        memset(buf1 + size, 0, MIN_BUF_SIZE + VLAN_HLEN - size);
957 a41b2ff2 pbrook
        buf = buf1;
958 18dabfd1 Benjamin Poirier
        if (size < MIN_BUF_SIZE) {
959 18dabfd1 Benjamin Poirier
            size = MIN_BUF_SIZE;
960 18dabfd1 Benjamin Poirier
        }
961 a41b2ff2 pbrook
    }
962 a41b2ff2 pbrook
963 a41b2ff2 pbrook
    if (rtl8139_cp_receiver_enabled(s))
964 a41b2ff2 pbrook
    {
965 7cdeb319 Benjamin Poirier
        DPRINTF("in C+ Rx mode ================\n");
966 a41b2ff2 pbrook
967 a41b2ff2 pbrook
        /* begin C+ receiver mode */
968 a41b2ff2 pbrook
969 a41b2ff2 pbrook
/* w0 ownership flag */
970 a41b2ff2 pbrook
#define CP_RX_OWN (1<<31)
971 a41b2ff2 pbrook
/* w0 end of ring flag */
972 a41b2ff2 pbrook
#define CP_RX_EOR (1<<30)
973 a41b2ff2 pbrook
/* w0 bits 0...12 : buffer size */
974 a41b2ff2 pbrook
#define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1)
975 a41b2ff2 pbrook
/* w1 tag available flag */
976 a41b2ff2 pbrook
#define CP_RX_TAVA (1<<16)
977 a41b2ff2 pbrook
/* w1 bits 0...15 : VLAN tag */
978 a41b2ff2 pbrook
#define CP_RX_VLAN_TAG_MASK ((1<<16) - 1)
979 a41b2ff2 pbrook
/* w2 low  32bit of Rx buffer ptr */
980 a41b2ff2 pbrook
/* w3 high 32bit of Rx buffer ptr */
981 a41b2ff2 pbrook
982 a41b2ff2 pbrook
        int descriptor = s->currCPlusRxDesc;
983 3ada003a Eduard - Gabriel Munteanu
        dma_addr_t cplus_rx_ring_desc;
984 a41b2ff2 pbrook
985 a41b2ff2 pbrook
        cplus_rx_ring_desc = rtl8139_addr64(s->RxRingAddrLO, s->RxRingAddrHI);
986 a41b2ff2 pbrook
        cplus_rx_ring_desc += 16 * descriptor;
987 a41b2ff2 pbrook
988 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode reading RX descriptor %d from host memory at "
989 3ada003a Eduard - Gabriel Munteanu
            "%08x %08x = "DMA_ADDR_FMT"\n", descriptor, s->RxRingAddrHI,
990 7cdeb319 Benjamin Poirier
            s->RxRingAddrLO, cplus_rx_ring_desc);
991 a41b2ff2 pbrook
992 a41b2ff2 pbrook
        uint32_t val, rxdw0,rxdw1,rxbufLO,rxbufHI;
993 a41b2ff2 pbrook
994 a6a29eea David Gibson
        pci_dma_read(&s->dev, cplus_rx_ring_desc, &val, 4);
995 a41b2ff2 pbrook
        rxdw0 = le32_to_cpu(val);
996 a6a29eea David Gibson
        pci_dma_read(&s->dev, cplus_rx_ring_desc+4, &val, 4);
997 a41b2ff2 pbrook
        rxdw1 = le32_to_cpu(val);
998 a6a29eea David Gibson
        pci_dma_read(&s->dev, cplus_rx_ring_desc+8, &val, 4);
999 a41b2ff2 pbrook
        rxbufLO = le32_to_cpu(val);
1000 a6a29eea David Gibson
        pci_dma_read(&s->dev, cplus_rx_ring_desc+12, &val, 4);
1001 a41b2ff2 pbrook
        rxbufHI = le32_to_cpu(val);
1002 a41b2ff2 pbrook
1003 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
1004 7cdeb319 Benjamin Poirier
            descriptor, rxdw0, rxdw1, rxbufLO, rxbufHI);
1005 a41b2ff2 pbrook
1006 a41b2ff2 pbrook
        if (!(rxdw0 & CP_RX_OWN))
1007 a41b2ff2 pbrook
        {
1008 7cdeb319 Benjamin Poirier
            DPRINTF("C+ Rx mode : descriptor %d is owned by host\n",
1009 7cdeb319 Benjamin Poirier
                descriptor);
1010 6cadb320 bellard
1011 a41b2ff2 pbrook
            s->IntrStatus |= RxOverflow;
1012 a41b2ff2 pbrook
            ++s->RxMissed;
1013 6cadb320 bellard
1014 6cadb320 bellard
            /* update tally counter */
1015 6cadb320 bellard
            ++s->tally_counters.RxERR;
1016 6cadb320 bellard
            ++s->tally_counters.MissPkt;
1017 6cadb320 bellard
1018 a41b2ff2 pbrook
            rtl8139_update_irq(s);
1019 4f1c942b Mark McLoughlin
            return size_;
1020 a41b2ff2 pbrook
        }
1021 a41b2ff2 pbrook
1022 a41b2ff2 pbrook
        uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK;
1023 a41b2ff2 pbrook
1024 18dabfd1 Benjamin Poirier
        /* write VLAN info to descriptor variables. */
1025 18dabfd1 Benjamin Poirier
        if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *)
1026 18dabfd1 Benjamin Poirier
                &buf[ETHER_ADDR_LEN * 2]) == ETH_P_8021Q) {
1027 18dabfd1 Benjamin Poirier
            dot1q_buf = &buf[ETHER_ADDR_LEN * 2];
1028 18dabfd1 Benjamin Poirier
            size -= VLAN_HLEN;
1029 18dabfd1 Benjamin Poirier
            /* if too small buffer, use the tailroom added duing expansion */
1030 18dabfd1 Benjamin Poirier
            if (size < MIN_BUF_SIZE) {
1031 18dabfd1 Benjamin Poirier
                size = MIN_BUF_SIZE;
1032 18dabfd1 Benjamin Poirier
            }
1033 18dabfd1 Benjamin Poirier
1034 18dabfd1 Benjamin Poirier
            rxdw1 &= ~CP_RX_VLAN_TAG_MASK;
1035 18dabfd1 Benjamin Poirier
            /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
1036 18dabfd1 Benjamin Poirier
            rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *)
1037 18dabfd1 Benjamin Poirier
                &dot1q_buf[ETHER_TYPE_LEN]);
1038 18dabfd1 Benjamin Poirier
1039 7cdeb319 Benjamin Poirier
            DPRINTF("C+ Rx mode : extracted vlan tag with tci: ""%u\n",
1040 7cdeb319 Benjamin Poirier
                be16_to_cpup((uint16_t *)&dot1q_buf[ETHER_TYPE_LEN]));
1041 18dabfd1 Benjamin Poirier
        } else {
1042 18dabfd1 Benjamin Poirier
            /* reset VLAN tag flag */
1043 18dabfd1 Benjamin Poirier
            rxdw1 &= ~CP_RX_TAVA;
1044 18dabfd1 Benjamin Poirier
        }
1045 18dabfd1 Benjamin Poirier
1046 6cadb320 bellard
        /* TODO: scatter the packet over available receive ring descriptors space */
1047 6cadb320 bellard
1048 a41b2ff2 pbrook
        if (size+4 > rx_space)
1049 a41b2ff2 pbrook
        {
1050 7cdeb319 Benjamin Poirier
            DPRINTF("C+ Rx mode : descriptor %d size %d received %d + 4\n",
1051 7cdeb319 Benjamin Poirier
                descriptor, rx_space, size);
1052 6cadb320 bellard
1053 a41b2ff2 pbrook
            s->IntrStatus |= RxOverflow;
1054 a41b2ff2 pbrook
            ++s->RxMissed;
1055 6cadb320 bellard
1056 6cadb320 bellard
            /* update tally counter */
1057 6cadb320 bellard
            ++s->tally_counters.RxERR;
1058 6cadb320 bellard
            ++s->tally_counters.MissPkt;
1059 6cadb320 bellard
1060 a41b2ff2 pbrook
            rtl8139_update_irq(s);
1061 4f1c942b Mark McLoughlin
            return size_;
1062 a41b2ff2 pbrook
        }
1063 a41b2ff2 pbrook
1064 3ada003a Eduard - Gabriel Munteanu
        dma_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI);
1065 a41b2ff2 pbrook
1066 a41b2ff2 pbrook
        /* receive/copy to target memory */
1067 18dabfd1 Benjamin Poirier
        if (dot1q_buf) {
1068 3ada003a Eduard - Gabriel Munteanu
            pci_dma_write(&s->dev, rx_addr, buf, 2 * ETHER_ADDR_LEN);
1069 3ada003a Eduard - Gabriel Munteanu
            pci_dma_write(&s->dev, rx_addr + 2 * ETHER_ADDR_LEN,
1070 3ada003a Eduard - Gabriel Munteanu
                          buf + 2 * ETHER_ADDR_LEN + VLAN_HLEN,
1071 3ada003a Eduard - Gabriel Munteanu
                          size - 2 * ETHER_ADDR_LEN);
1072 18dabfd1 Benjamin Poirier
        } else {
1073 3ada003a Eduard - Gabriel Munteanu
            pci_dma_write(&s->dev, rx_addr, buf, size);
1074 18dabfd1 Benjamin Poirier
        }
1075 a41b2ff2 pbrook
1076 6cadb320 bellard
        if (s->CpCmd & CPlusRxChkSum)
1077 6cadb320 bellard
        {
1078 6cadb320 bellard
            /* do some packet checksumming */
1079 6cadb320 bellard
        }
1080 6cadb320 bellard
1081 a41b2ff2 pbrook
        /* write checksum */
1082 18dabfd1 Benjamin Poirier
        val = cpu_to_le32(crc32(0, buf, size_));
1083 3ada003a Eduard - Gabriel Munteanu
        pci_dma_write(&s->dev, rx_addr+size, (uint8_t *)&val, 4);
1084 a41b2ff2 pbrook
1085 a41b2ff2 pbrook
/* first segment of received packet flag */
1086 a41b2ff2 pbrook
#define CP_RX_STATUS_FS (1<<29)
1087 a41b2ff2 pbrook
/* last segment of received packet flag */
1088 a41b2ff2 pbrook
#define CP_RX_STATUS_LS (1<<28)
1089 a41b2ff2 pbrook
/* multicast packet flag */
1090 a41b2ff2 pbrook
#define CP_RX_STATUS_MAR (1<<26)
1091 a41b2ff2 pbrook
/* physical-matching packet flag */
1092 a41b2ff2 pbrook
#define CP_RX_STATUS_PAM (1<<25)
1093 a41b2ff2 pbrook
/* broadcast packet flag */
1094 a41b2ff2 pbrook
#define CP_RX_STATUS_BAR (1<<24)
1095 a41b2ff2 pbrook
/* runt packet flag */
1096 a41b2ff2 pbrook
#define CP_RX_STATUS_RUNT (1<<19)
1097 a41b2ff2 pbrook
/* crc error flag */
1098 a41b2ff2 pbrook
#define CP_RX_STATUS_CRC (1<<18)
1099 a41b2ff2 pbrook
/* IP checksum error flag */
1100 a41b2ff2 pbrook
#define CP_RX_STATUS_IPF (1<<15)
1101 a41b2ff2 pbrook
/* UDP checksum error flag */
1102 a41b2ff2 pbrook
#define CP_RX_STATUS_UDPF (1<<14)
1103 a41b2ff2 pbrook
/* TCP checksum error flag */
1104 a41b2ff2 pbrook
#define CP_RX_STATUS_TCPF (1<<13)
1105 a41b2ff2 pbrook
1106 a41b2ff2 pbrook
        /* transfer ownership to target */
1107 a41b2ff2 pbrook
        rxdw0 &= ~CP_RX_OWN;
1108 a41b2ff2 pbrook
1109 a41b2ff2 pbrook
        /* set first segment bit */
1110 a41b2ff2 pbrook
        rxdw0 |= CP_RX_STATUS_FS;
1111 a41b2ff2 pbrook
1112 a41b2ff2 pbrook
        /* set last segment bit */
1113 a41b2ff2 pbrook
        rxdw0 |= CP_RX_STATUS_LS;
1114 a41b2ff2 pbrook
1115 a41b2ff2 pbrook
        /* set received packet type flags */
1116 a41b2ff2 pbrook
        if (packet_header & RxBroadcast)
1117 a41b2ff2 pbrook
            rxdw0 |= CP_RX_STATUS_BAR;
1118 a41b2ff2 pbrook
        if (packet_header & RxMulticast)
1119 a41b2ff2 pbrook
            rxdw0 |= CP_RX_STATUS_MAR;
1120 a41b2ff2 pbrook
        if (packet_header & RxPhysical)
1121 a41b2ff2 pbrook
            rxdw0 |= CP_RX_STATUS_PAM;
1122 a41b2ff2 pbrook
1123 a41b2ff2 pbrook
        /* set received size */
1124 a41b2ff2 pbrook
        rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK;
1125 a41b2ff2 pbrook
        rxdw0 |= (size+4);
1126 a41b2ff2 pbrook
1127 a41b2ff2 pbrook
        /* update ring data */
1128 a41b2ff2 pbrook
        val = cpu_to_le32(rxdw0);
1129 3ada003a Eduard - Gabriel Munteanu
        pci_dma_write(&s->dev, cplus_rx_ring_desc, (uint8_t *)&val, 4);
1130 a41b2ff2 pbrook
        val = cpu_to_le32(rxdw1);
1131 3ada003a Eduard - Gabriel Munteanu
        pci_dma_write(&s->dev, cplus_rx_ring_desc+4, (uint8_t *)&val, 4);
1132 a41b2ff2 pbrook
1133 6cadb320 bellard
        /* update tally counter */
1134 6cadb320 bellard
        ++s->tally_counters.RxOk;
1135 6cadb320 bellard
1136 a41b2ff2 pbrook
        /* seek to next Rx descriptor */
1137 a41b2ff2 pbrook
        if (rxdw0 & CP_RX_EOR)
1138 a41b2ff2 pbrook
        {
1139 a41b2ff2 pbrook
            s->currCPlusRxDesc = 0;
1140 a41b2ff2 pbrook
        }
1141 a41b2ff2 pbrook
        else
1142 a41b2ff2 pbrook
        {
1143 a41b2ff2 pbrook
            ++s->currCPlusRxDesc;
1144 a41b2ff2 pbrook
        }
1145 a41b2ff2 pbrook
1146 7cdeb319 Benjamin Poirier
        DPRINTF("done C+ Rx mode ----------------\n");
1147 a41b2ff2 pbrook
1148 a41b2ff2 pbrook
    }
1149 a41b2ff2 pbrook
    else
1150 a41b2ff2 pbrook
    {
1151 7cdeb319 Benjamin Poirier
        DPRINTF("in ring Rx mode ================\n");
1152 6cadb320 bellard
1153 a41b2ff2 pbrook
        /* begin ring receiver mode */
1154 a41b2ff2 pbrook
        int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize);
1155 a41b2ff2 pbrook
1156 a41b2ff2 pbrook
        /* if receiver buffer is empty then avail == 0 */
1157 a41b2ff2 pbrook
1158 a41b2ff2 pbrook
        if (avail != 0 && size + 8 >= avail)
1159 a41b2ff2 pbrook
        {
1160 7cdeb319 Benjamin Poirier
            DPRINTF("rx overflow: rx buffer length %d head 0x%04x "
1161 7cdeb319 Benjamin Poirier
                "read 0x%04x === available 0x%04x need 0x%04x\n",
1162 7cdeb319 Benjamin Poirier
                s->RxBufferSize, s->RxBufAddr, s->RxBufPtr, avail, size + 8);
1163 6cadb320 bellard
1164 a41b2ff2 pbrook
            s->IntrStatus |= RxOverflow;
1165 a41b2ff2 pbrook
            ++s->RxMissed;
1166 a41b2ff2 pbrook
            rtl8139_update_irq(s);
1167 4f1c942b Mark McLoughlin
            return size_;
1168 a41b2ff2 pbrook
        }
1169 a41b2ff2 pbrook
1170 a41b2ff2 pbrook
        packet_header |= RxStatusOK;
1171 a41b2ff2 pbrook
1172 a41b2ff2 pbrook
        packet_header |= (((size+4) << 16) & 0xffff0000);
1173 a41b2ff2 pbrook
1174 a41b2ff2 pbrook
        /* write header */
1175 a41b2ff2 pbrook
        uint32_t val = cpu_to_le32(packet_header);
1176 a41b2ff2 pbrook
1177 a41b2ff2 pbrook
        rtl8139_write_buffer(s, (uint8_t *)&val, 4);
1178 a41b2ff2 pbrook
1179 a41b2ff2 pbrook
        rtl8139_write_buffer(s, buf, size);
1180 a41b2ff2 pbrook
1181 a41b2ff2 pbrook
        /* write checksum */
1182 ccf1d14a ths
        val = cpu_to_le32(crc32(0, buf, size));
1183 a41b2ff2 pbrook
        rtl8139_write_buffer(s, (uint8_t *)&val, 4);
1184 a41b2ff2 pbrook
1185 a41b2ff2 pbrook
        /* correct buffer write pointer */
1186 a41b2ff2 pbrook
        s->RxBufAddr = MOD2((s->RxBufAddr + 3) & ~0x3, s->RxBufferSize);
1187 a41b2ff2 pbrook
1188 a41b2ff2 pbrook
        /* now we can signal we have received something */
1189 a41b2ff2 pbrook
1190 7cdeb319 Benjamin Poirier
        DPRINTF("received: rx buffer length %d head 0x%04x read 0x%04x\n",
1191 7cdeb319 Benjamin Poirier
            s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
1192 a41b2ff2 pbrook
    }
1193 a41b2ff2 pbrook
1194 a41b2ff2 pbrook
    s->IntrStatus |= RxOK;
1195 6cadb320 bellard
1196 6cadb320 bellard
    if (do_interrupt)
1197 6cadb320 bellard
    {
1198 6cadb320 bellard
        rtl8139_update_irq(s);
1199 6cadb320 bellard
    }
1200 4f1c942b Mark McLoughlin
1201 4f1c942b Mark McLoughlin
    return size_;
1202 6cadb320 bellard
}
1203 6cadb320 bellard
1204 1673ad51 Mark McLoughlin
static ssize_t rtl8139_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
1205 6cadb320 bellard
{
1206 1673ad51 Mark McLoughlin
    return rtl8139_do_receive(nc, buf, size, 1);
1207 a41b2ff2 pbrook
}
1208 a41b2ff2 pbrook
1209 a41b2ff2 pbrook
static void rtl8139_reset_rxring(RTL8139State *s, uint32_t bufferSize)
1210 a41b2ff2 pbrook
{
1211 a41b2ff2 pbrook
    s->RxBufferSize = bufferSize;
1212 a41b2ff2 pbrook
    s->RxBufPtr  = 0;
1213 a41b2ff2 pbrook
    s->RxBufAddr = 0;
1214 a41b2ff2 pbrook
}
1215 a41b2ff2 pbrook
1216 7f23f812 Michael S. Tsirkin
static void rtl8139_reset(DeviceState *d)
1217 a41b2ff2 pbrook
{
1218 7f23f812 Michael S. Tsirkin
    RTL8139State *s = container_of(d, RTL8139State, dev.qdev);
1219 a41b2ff2 pbrook
    int i;
1220 a41b2ff2 pbrook
1221 a41b2ff2 pbrook
    /* restore MAC address */
1222 254111ec Gerd Hoffmann
    memcpy(s->phys, s->conf.macaddr.a, 6);
1223 a41b2ff2 pbrook
1224 a41b2ff2 pbrook
    /* reset interrupt mask */
1225 a41b2ff2 pbrook
    s->IntrStatus = 0;
1226 a41b2ff2 pbrook
    s->IntrMask = 0;
1227 a41b2ff2 pbrook
1228 a41b2ff2 pbrook
    rtl8139_update_irq(s);
1229 a41b2ff2 pbrook
1230 a41b2ff2 pbrook
    /* mark all status registers as owned by host */
1231 a41b2ff2 pbrook
    for (i = 0; i < 4; ++i)
1232 a41b2ff2 pbrook
    {
1233 a41b2ff2 pbrook
        s->TxStatus[i] = TxHostOwns;
1234 a41b2ff2 pbrook
    }
1235 a41b2ff2 pbrook
1236 a41b2ff2 pbrook
    s->currTxDesc = 0;
1237 a41b2ff2 pbrook
    s->currCPlusRxDesc = 0;
1238 a41b2ff2 pbrook
    s->currCPlusTxDesc = 0;
1239 a41b2ff2 pbrook
1240 a41b2ff2 pbrook
    s->RxRingAddrLO = 0;
1241 a41b2ff2 pbrook
    s->RxRingAddrHI = 0;
1242 a41b2ff2 pbrook
1243 a41b2ff2 pbrook
    s->RxBuf = 0;
1244 a41b2ff2 pbrook
1245 a41b2ff2 pbrook
    rtl8139_reset_rxring(s, 8192);
1246 a41b2ff2 pbrook
1247 a41b2ff2 pbrook
    /* ACK the reset */
1248 a41b2ff2 pbrook
    s->TxConfig = 0;
1249 a41b2ff2 pbrook
1250 a41b2ff2 pbrook
#if 0
1251 a41b2ff2 pbrook
//    s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139  HasHltClk
1252 a41b2ff2 pbrook
    s->clock_enabled = 0;
1253 a41b2ff2 pbrook
#else
1254 6cadb320 bellard
    s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake
1255 a41b2ff2 pbrook
    s->clock_enabled = 1;
1256 a41b2ff2 pbrook
#endif
1257 a41b2ff2 pbrook
1258 a41b2ff2 pbrook
    s->bChipCmdState = CmdReset; /* RxBufEmpty bit is calculated on read from ChipCmd */;
1259 a41b2ff2 pbrook
1260 a41b2ff2 pbrook
    /* set initial state data */
1261 a41b2ff2 pbrook
    s->Config0 = 0x0; /* No boot ROM */
1262 a41b2ff2 pbrook
    s->Config1 = 0xC; /* IO mapped and MEM mapped registers available */
1263 a41b2ff2 pbrook
    s->Config3 = 0x1; /* fast back-to-back compatible */
1264 a41b2ff2 pbrook
    s->Config5 = 0x0;
1265 a41b2ff2 pbrook
1266 5fafdf24 ths
    s->CSCR = CSCR_F_LINK_100 | CSCR_HEART_BIT | CSCR_LD;
1267 a41b2ff2 pbrook
1268 a41b2ff2 pbrook
    s->CpCmd   = 0x0; /* reset C+ mode */
1269 2c3891ab aliguori
    s->cplus_enabled = 0;
1270 2c3891ab aliguori
1271 a41b2ff2 pbrook
1272 a41b2ff2 pbrook
//    s->BasicModeCtrl = 0x3100; // 100Mbps, full duplex, autonegotiation
1273 a41b2ff2 pbrook
//    s->BasicModeCtrl = 0x2100; // 100Mbps, full duplex
1274 a41b2ff2 pbrook
    s->BasicModeCtrl = 0x1000; // autonegotiation
1275 a41b2ff2 pbrook
1276 a41b2ff2 pbrook
    s->BasicModeStatus  = 0x7809;
1277 a41b2ff2 pbrook
    //s->BasicModeStatus |= 0x0040; /* UTP medium */
1278 a41b2ff2 pbrook
    s->BasicModeStatus |= 0x0020; /* autonegotiation completed */
1279 a41b2ff2 pbrook
    s->BasicModeStatus |= 0x0004; /* link is up */
1280 a41b2ff2 pbrook
1281 a41b2ff2 pbrook
    s->NWayAdvert    = 0x05e1; /* all modes, full duplex */
1282 a41b2ff2 pbrook
    s->NWayLPAR      = 0x05e1; /* all modes, full duplex */
1283 a41b2ff2 pbrook
    s->NWayExpansion = 0x0001; /* autonegotiation supported */
1284 6cadb320 bellard
1285 6cadb320 bellard
    /* also reset timer and disable timer interrupt */
1286 6cadb320 bellard
    s->TCTR = 0;
1287 6cadb320 bellard
    s->TimerInt = 0;
1288 6cadb320 bellard
    s->TCTR_base = 0;
1289 6cadb320 bellard
1290 6cadb320 bellard
    /* reset tally counters */
1291 6cadb320 bellard
    RTL8139TallyCounters_clear(&s->tally_counters);
1292 6cadb320 bellard
}
1293 6cadb320 bellard
1294 b1d8e52e blueswir1
static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters)
1295 6cadb320 bellard
{
1296 6cadb320 bellard
    counters->TxOk = 0;
1297 6cadb320 bellard
    counters->RxOk = 0;
1298 6cadb320 bellard
    counters->TxERR = 0;
1299 6cadb320 bellard
    counters->RxERR = 0;
1300 6cadb320 bellard
    counters->MissPkt = 0;
1301 6cadb320 bellard
    counters->FAE = 0;
1302 6cadb320 bellard
    counters->Tx1Col = 0;
1303 6cadb320 bellard
    counters->TxMCol = 0;
1304 6cadb320 bellard
    counters->RxOkPhy = 0;
1305 6cadb320 bellard
    counters->RxOkBrd = 0;
1306 6cadb320 bellard
    counters->RxOkMul = 0;
1307 6cadb320 bellard
    counters->TxAbt = 0;
1308 6cadb320 bellard
    counters->TxUndrn = 0;
1309 6cadb320 bellard
}
1310 6cadb320 bellard
1311 3ada003a Eduard - Gabriel Munteanu
static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr)
1312 6cadb320 bellard
{
1313 3ada003a Eduard - Gabriel Munteanu
    RTL8139TallyCounters *tally_counters = &s->tally_counters;
1314 6cadb320 bellard
    uint16_t val16;
1315 6cadb320 bellard
    uint32_t val32;
1316 6cadb320 bellard
    uint64_t val64;
1317 6cadb320 bellard
1318 6cadb320 bellard
    val64 = cpu_to_le64(tally_counters->TxOk);
1319 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 0,     (uint8_t *)&val64, 8);
1320 6cadb320 bellard
1321 6cadb320 bellard
    val64 = cpu_to_le64(tally_counters->RxOk);
1322 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 8,     (uint8_t *)&val64, 8);
1323 6cadb320 bellard
1324 6cadb320 bellard
    val64 = cpu_to_le64(tally_counters->TxERR);
1325 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 16,    (uint8_t *)&val64, 8);
1326 6cadb320 bellard
1327 6cadb320 bellard
    val32 = cpu_to_le32(tally_counters->RxERR);
1328 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 24,    (uint8_t *)&val32, 4);
1329 6cadb320 bellard
1330 6cadb320 bellard
    val16 = cpu_to_le16(tally_counters->MissPkt);
1331 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 28,    (uint8_t *)&val16, 2);
1332 6cadb320 bellard
1333 6cadb320 bellard
    val16 = cpu_to_le16(tally_counters->FAE);
1334 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 30,    (uint8_t *)&val16, 2);
1335 6cadb320 bellard
1336 6cadb320 bellard
    val32 = cpu_to_le32(tally_counters->Tx1Col);
1337 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 32,    (uint8_t *)&val32, 4);
1338 6cadb320 bellard
1339 6cadb320 bellard
    val32 = cpu_to_le32(tally_counters->TxMCol);
1340 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 36,    (uint8_t *)&val32, 4);
1341 6cadb320 bellard
1342 6cadb320 bellard
    val64 = cpu_to_le64(tally_counters->RxOkPhy);
1343 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 40,    (uint8_t *)&val64, 8);
1344 6cadb320 bellard
1345 6cadb320 bellard
    val64 = cpu_to_le64(tally_counters->RxOkBrd);
1346 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 48,    (uint8_t *)&val64, 8);
1347 6cadb320 bellard
1348 6cadb320 bellard
    val32 = cpu_to_le32(tally_counters->RxOkMul);
1349 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 56,    (uint8_t *)&val32, 4);
1350 6cadb320 bellard
1351 6cadb320 bellard
    val16 = cpu_to_le16(tally_counters->TxAbt);
1352 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 60,    (uint8_t *)&val16, 2);
1353 6cadb320 bellard
1354 6cadb320 bellard
    val16 = cpu_to_le16(tally_counters->TxUndrn);
1355 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, tc_addr + 62,    (uint8_t *)&val16, 2);
1356 6cadb320 bellard
}
1357 6cadb320 bellard
1358 6cadb320 bellard
/* Loads values of tally counters from VM state file */
1359 9d29cdea Juan Quintela
1360 9d29cdea Juan Quintela
static const VMStateDescription vmstate_tally_counters = {
1361 9d29cdea Juan Quintela
    .name = "tally_counters",
1362 9d29cdea Juan Quintela
    .version_id = 1,
1363 9d29cdea Juan Quintela
    .minimum_version_id = 1,
1364 9d29cdea Juan Quintela
    .minimum_version_id_old = 1,
1365 9d29cdea Juan Quintela
    .fields      = (VMStateField []) {
1366 9d29cdea Juan Quintela
        VMSTATE_UINT64(TxOk, RTL8139TallyCounters),
1367 9d29cdea Juan Quintela
        VMSTATE_UINT64(RxOk, RTL8139TallyCounters),
1368 9d29cdea Juan Quintela
        VMSTATE_UINT64(TxERR, RTL8139TallyCounters),
1369 9d29cdea Juan Quintela
        VMSTATE_UINT32(RxERR, RTL8139TallyCounters),
1370 9d29cdea Juan Quintela
        VMSTATE_UINT16(MissPkt, RTL8139TallyCounters),
1371 9d29cdea Juan Quintela
        VMSTATE_UINT16(FAE, RTL8139TallyCounters),
1372 9d29cdea Juan Quintela
        VMSTATE_UINT32(Tx1Col, RTL8139TallyCounters),
1373 9d29cdea Juan Quintela
        VMSTATE_UINT32(TxMCol, RTL8139TallyCounters),
1374 9d29cdea Juan Quintela
        VMSTATE_UINT64(RxOkPhy, RTL8139TallyCounters),
1375 9d29cdea Juan Quintela
        VMSTATE_UINT64(RxOkBrd, RTL8139TallyCounters),
1376 9d29cdea Juan Quintela
        VMSTATE_UINT16(TxAbt, RTL8139TallyCounters),
1377 9d29cdea Juan Quintela
        VMSTATE_UINT16(TxUndrn, RTL8139TallyCounters),
1378 9d29cdea Juan Quintela
        VMSTATE_END_OF_LIST()
1379 9d29cdea Juan Quintela
    }
1380 9d29cdea Juan Quintela
};
1381 a41b2ff2 pbrook
1382 a41b2ff2 pbrook
static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val)
1383 a41b2ff2 pbrook
{
1384 a41b2ff2 pbrook
    val &= 0xff;
1385 a41b2ff2 pbrook
1386 7cdeb319 Benjamin Poirier
    DPRINTF("ChipCmd write val=0x%08x\n", val);
1387 a41b2ff2 pbrook
1388 a41b2ff2 pbrook
    if (val & CmdReset)
1389 a41b2ff2 pbrook
    {
1390 7cdeb319 Benjamin Poirier
        DPRINTF("ChipCmd reset\n");
1391 7f23f812 Michael S. Tsirkin
        rtl8139_reset(&s->dev.qdev);
1392 a41b2ff2 pbrook
    }
1393 a41b2ff2 pbrook
    if (val & CmdRxEnb)
1394 a41b2ff2 pbrook
    {
1395 7cdeb319 Benjamin Poirier
        DPRINTF("ChipCmd enable receiver\n");
1396 718da2b9 bellard
1397 718da2b9 bellard
        s->currCPlusRxDesc = 0;
1398 a41b2ff2 pbrook
    }
1399 a41b2ff2 pbrook
    if (val & CmdTxEnb)
1400 a41b2ff2 pbrook
    {
1401 7cdeb319 Benjamin Poirier
        DPRINTF("ChipCmd enable transmitter\n");
1402 718da2b9 bellard
1403 718da2b9 bellard
        s->currCPlusTxDesc = 0;
1404 a41b2ff2 pbrook
    }
1405 a41b2ff2 pbrook
1406 ebabb67a Stefan Weil
    /* mask unwritable bits */
1407 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xe3, s->bChipCmdState);
1408 a41b2ff2 pbrook
1409 a41b2ff2 pbrook
    /* Deassert reset pin before next read */
1410 a41b2ff2 pbrook
    val &= ~CmdReset;
1411 a41b2ff2 pbrook
1412 a41b2ff2 pbrook
    s->bChipCmdState = val;
1413 a41b2ff2 pbrook
}
1414 a41b2ff2 pbrook
1415 a41b2ff2 pbrook
static int rtl8139_RxBufferEmpty(RTL8139State *s)
1416 a41b2ff2 pbrook
{
1417 a41b2ff2 pbrook
    int unread = MOD2(s->RxBufferSize + s->RxBufAddr - s->RxBufPtr, s->RxBufferSize);
1418 a41b2ff2 pbrook
1419 a41b2ff2 pbrook
    if (unread != 0)
1420 a41b2ff2 pbrook
    {
1421 7cdeb319 Benjamin Poirier
        DPRINTF("receiver buffer data available 0x%04x\n", unread);
1422 a41b2ff2 pbrook
        return 0;
1423 a41b2ff2 pbrook
    }
1424 a41b2ff2 pbrook
1425 7cdeb319 Benjamin Poirier
    DPRINTF("receiver buffer is empty\n");
1426 a41b2ff2 pbrook
1427 a41b2ff2 pbrook
    return 1;
1428 a41b2ff2 pbrook
}
1429 a41b2ff2 pbrook
1430 a41b2ff2 pbrook
static uint32_t rtl8139_ChipCmd_read(RTL8139State *s)
1431 a41b2ff2 pbrook
{
1432 a41b2ff2 pbrook
    uint32_t ret = s->bChipCmdState;
1433 a41b2ff2 pbrook
1434 a41b2ff2 pbrook
    if (rtl8139_RxBufferEmpty(s))
1435 a41b2ff2 pbrook
        ret |= RxBufEmpty;
1436 a41b2ff2 pbrook
1437 7cdeb319 Benjamin Poirier
    DPRINTF("ChipCmd read val=0x%04x\n", ret);
1438 a41b2ff2 pbrook
1439 a41b2ff2 pbrook
    return ret;
1440 a41b2ff2 pbrook
}
1441 a41b2ff2 pbrook
1442 a41b2ff2 pbrook
static void rtl8139_CpCmd_write(RTL8139State *s, uint32_t val)
1443 a41b2ff2 pbrook
{
1444 a41b2ff2 pbrook
    val &= 0xffff;
1445 a41b2ff2 pbrook
1446 7cdeb319 Benjamin Poirier
    DPRINTF("C+ command register write(w) val=0x%04x\n", val);
1447 a41b2ff2 pbrook
1448 2c3891ab aliguori
    s->cplus_enabled = 1;
1449 2c3891ab aliguori
1450 ebabb67a Stefan Weil
    /* mask unwritable bits */
1451 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xff84, s->CpCmd);
1452 a41b2ff2 pbrook
1453 a41b2ff2 pbrook
    s->CpCmd = val;
1454 a41b2ff2 pbrook
}
1455 a41b2ff2 pbrook
1456 a41b2ff2 pbrook
static uint32_t rtl8139_CpCmd_read(RTL8139State *s)
1457 a41b2ff2 pbrook
{
1458 a41b2ff2 pbrook
    uint32_t ret = s->CpCmd;
1459 a41b2ff2 pbrook
1460 7cdeb319 Benjamin Poirier
    DPRINTF("C+ command register read(w) val=0x%04x\n", ret);
1461 6cadb320 bellard
1462 6cadb320 bellard
    return ret;
1463 6cadb320 bellard
}
1464 6cadb320 bellard
1465 6cadb320 bellard
static void rtl8139_IntrMitigate_write(RTL8139State *s, uint32_t val)
1466 6cadb320 bellard
{
1467 7cdeb319 Benjamin Poirier
    DPRINTF("C+ IntrMitigate register write(w) val=0x%04x\n", val);
1468 6cadb320 bellard
}
1469 6cadb320 bellard
1470 6cadb320 bellard
static uint32_t rtl8139_IntrMitigate_read(RTL8139State *s)
1471 6cadb320 bellard
{
1472 6cadb320 bellard
    uint32_t ret = 0;
1473 6cadb320 bellard
1474 7cdeb319 Benjamin Poirier
    DPRINTF("C+ IntrMitigate register read(w) val=0x%04x\n", ret);
1475 a41b2ff2 pbrook
1476 a41b2ff2 pbrook
    return ret;
1477 a41b2ff2 pbrook
}
1478 a41b2ff2 pbrook
1479 ebabb67a Stefan Weil
static int rtl8139_config_writable(RTL8139State *s)
1480 a41b2ff2 pbrook
{
1481 a41b2ff2 pbrook
    if (s->Cfg9346 & Cfg9346_Unlock)
1482 a41b2ff2 pbrook
    {
1483 a41b2ff2 pbrook
        return 1;
1484 a41b2ff2 pbrook
    }
1485 a41b2ff2 pbrook
1486 7cdeb319 Benjamin Poirier
    DPRINTF("Configuration registers are write-protected\n");
1487 a41b2ff2 pbrook
1488 a41b2ff2 pbrook
    return 0;
1489 a41b2ff2 pbrook
}
1490 a41b2ff2 pbrook
1491 a41b2ff2 pbrook
static void rtl8139_BasicModeCtrl_write(RTL8139State *s, uint32_t val)
1492 a41b2ff2 pbrook
{
1493 a41b2ff2 pbrook
    val &= 0xffff;
1494 a41b2ff2 pbrook
1495 7cdeb319 Benjamin Poirier
    DPRINTF("BasicModeCtrl register write(w) val=0x%04x\n", val);
1496 a41b2ff2 pbrook
1497 ebabb67a Stefan Weil
    /* mask unwritable bits */
1498 e3d7e843 ths
    uint32_t mask = 0x4cff;
1499 a41b2ff2 pbrook
1500 ebabb67a Stefan Weil
    if (1 || !rtl8139_config_writable(s))
1501 a41b2ff2 pbrook
    {
1502 a41b2ff2 pbrook
        /* Speed setting and autonegotiation enable bits are read-only */
1503 a41b2ff2 pbrook
        mask |= 0x3000;
1504 a41b2ff2 pbrook
        /* Duplex mode setting is read-only */
1505 a41b2ff2 pbrook
        mask |= 0x0100;
1506 a41b2ff2 pbrook
    }
1507 a41b2ff2 pbrook
1508 a41b2ff2 pbrook
    val = SET_MASKED(val, mask, s->BasicModeCtrl);
1509 a41b2ff2 pbrook
1510 a41b2ff2 pbrook
    s->BasicModeCtrl = val;
1511 a41b2ff2 pbrook
}
1512 a41b2ff2 pbrook
1513 a41b2ff2 pbrook
static uint32_t rtl8139_BasicModeCtrl_read(RTL8139State *s)
1514 a41b2ff2 pbrook
{
1515 a41b2ff2 pbrook
    uint32_t ret = s->BasicModeCtrl;
1516 a41b2ff2 pbrook
1517 7cdeb319 Benjamin Poirier
    DPRINTF("BasicModeCtrl register read(w) val=0x%04x\n", ret);
1518 a41b2ff2 pbrook
1519 a41b2ff2 pbrook
    return ret;
1520 a41b2ff2 pbrook
}
1521 a41b2ff2 pbrook
1522 a41b2ff2 pbrook
static void rtl8139_BasicModeStatus_write(RTL8139State *s, uint32_t val)
1523 a41b2ff2 pbrook
{
1524 a41b2ff2 pbrook
    val &= 0xffff;
1525 a41b2ff2 pbrook
1526 7cdeb319 Benjamin Poirier
    DPRINTF("BasicModeStatus register write(w) val=0x%04x\n", val);
1527 a41b2ff2 pbrook
1528 ebabb67a Stefan Weil
    /* mask unwritable bits */
1529 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xff3f, s->BasicModeStatus);
1530 a41b2ff2 pbrook
1531 a41b2ff2 pbrook
    s->BasicModeStatus = val;
1532 a41b2ff2 pbrook
}
1533 a41b2ff2 pbrook
1534 a41b2ff2 pbrook
static uint32_t rtl8139_BasicModeStatus_read(RTL8139State *s)
1535 a41b2ff2 pbrook
{
1536 a41b2ff2 pbrook
    uint32_t ret = s->BasicModeStatus;
1537 a41b2ff2 pbrook
1538 7cdeb319 Benjamin Poirier
    DPRINTF("BasicModeStatus register read(w) val=0x%04x\n", ret);
1539 a41b2ff2 pbrook
1540 a41b2ff2 pbrook
    return ret;
1541 a41b2ff2 pbrook
}
1542 a41b2ff2 pbrook
1543 a41b2ff2 pbrook
static void rtl8139_Cfg9346_write(RTL8139State *s, uint32_t val)
1544 a41b2ff2 pbrook
{
1545 a41b2ff2 pbrook
    val &= 0xff;
1546 a41b2ff2 pbrook
1547 7cdeb319 Benjamin Poirier
    DPRINTF("Cfg9346 write val=0x%02x\n", val);
1548 a41b2ff2 pbrook
1549 ebabb67a Stefan Weil
    /* mask unwritable bits */
1550 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x31, s->Cfg9346);
1551 a41b2ff2 pbrook
1552 a41b2ff2 pbrook
    uint32_t opmode = val & 0xc0;
1553 a41b2ff2 pbrook
    uint32_t eeprom_val = val & 0xf;
1554 a41b2ff2 pbrook
1555 a41b2ff2 pbrook
    if (opmode == 0x80) {
1556 a41b2ff2 pbrook
        /* eeprom access */
1557 a41b2ff2 pbrook
        int eecs = (eeprom_val & 0x08)?1:0;
1558 a41b2ff2 pbrook
        int eesk = (eeprom_val & 0x04)?1:0;
1559 a41b2ff2 pbrook
        int eedi = (eeprom_val & 0x02)?1:0;
1560 a41b2ff2 pbrook
        prom9346_set_wire(s, eecs, eesk, eedi);
1561 a41b2ff2 pbrook
    } else if (opmode == 0x40) {
1562 a41b2ff2 pbrook
        /* Reset.  */
1563 a41b2ff2 pbrook
        val = 0;
1564 7f23f812 Michael S. Tsirkin
        rtl8139_reset(&s->dev.qdev);
1565 a41b2ff2 pbrook
    }
1566 a41b2ff2 pbrook
1567 a41b2ff2 pbrook
    s->Cfg9346 = val;
1568 a41b2ff2 pbrook
}
1569 a41b2ff2 pbrook
1570 a41b2ff2 pbrook
static uint32_t rtl8139_Cfg9346_read(RTL8139State *s)
1571 a41b2ff2 pbrook
{
1572 a41b2ff2 pbrook
    uint32_t ret = s->Cfg9346;
1573 a41b2ff2 pbrook
1574 a41b2ff2 pbrook
    uint32_t opmode = ret & 0xc0;
1575 a41b2ff2 pbrook
1576 a41b2ff2 pbrook
    if (opmode == 0x80)
1577 a41b2ff2 pbrook
    {
1578 a41b2ff2 pbrook
        /* eeprom access */
1579 a41b2ff2 pbrook
        int eedo = prom9346_get_wire(s);
1580 a41b2ff2 pbrook
        if (eedo)
1581 a41b2ff2 pbrook
        {
1582 a41b2ff2 pbrook
            ret |=  0x01;
1583 a41b2ff2 pbrook
        }
1584 a41b2ff2 pbrook
        else
1585 a41b2ff2 pbrook
        {
1586 a41b2ff2 pbrook
            ret &= ~0x01;
1587 a41b2ff2 pbrook
        }
1588 a41b2ff2 pbrook
    }
1589 a41b2ff2 pbrook
1590 7cdeb319 Benjamin Poirier
    DPRINTF("Cfg9346 read val=0x%02x\n", ret);
1591 a41b2ff2 pbrook
1592 a41b2ff2 pbrook
    return ret;
1593 a41b2ff2 pbrook
}
1594 a41b2ff2 pbrook
1595 a41b2ff2 pbrook
static void rtl8139_Config0_write(RTL8139State *s, uint32_t val)
1596 a41b2ff2 pbrook
{
1597 a41b2ff2 pbrook
    val &= 0xff;
1598 a41b2ff2 pbrook
1599 7cdeb319 Benjamin Poirier
    DPRINTF("Config0 write val=0x%02x\n", val);
1600 a41b2ff2 pbrook
1601 ebabb67a Stefan Weil
    if (!rtl8139_config_writable(s)) {
1602 a41b2ff2 pbrook
        return;
1603 ebabb67a Stefan Weil
    }
1604 a41b2ff2 pbrook
1605 ebabb67a Stefan Weil
    /* mask unwritable bits */
1606 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xf8, s->Config0);
1607 a41b2ff2 pbrook
1608 a41b2ff2 pbrook
    s->Config0 = val;
1609 a41b2ff2 pbrook
}
1610 a41b2ff2 pbrook
1611 a41b2ff2 pbrook
static uint32_t rtl8139_Config0_read(RTL8139State *s)
1612 a41b2ff2 pbrook
{
1613 a41b2ff2 pbrook
    uint32_t ret = s->Config0;
1614 a41b2ff2 pbrook
1615 7cdeb319 Benjamin Poirier
    DPRINTF("Config0 read val=0x%02x\n", ret);
1616 a41b2ff2 pbrook
1617 a41b2ff2 pbrook
    return ret;
1618 a41b2ff2 pbrook
}
1619 a41b2ff2 pbrook
1620 a41b2ff2 pbrook
static void rtl8139_Config1_write(RTL8139State *s, uint32_t val)
1621 a41b2ff2 pbrook
{
1622 a41b2ff2 pbrook
    val &= 0xff;
1623 a41b2ff2 pbrook
1624 7cdeb319 Benjamin Poirier
    DPRINTF("Config1 write val=0x%02x\n", val);
1625 a41b2ff2 pbrook
1626 ebabb67a Stefan Weil
    if (!rtl8139_config_writable(s)) {
1627 a41b2ff2 pbrook
        return;
1628 ebabb67a Stefan Weil
    }
1629 a41b2ff2 pbrook
1630 ebabb67a Stefan Weil
    /* mask unwritable bits */
1631 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xC, s->Config1);
1632 a41b2ff2 pbrook
1633 a41b2ff2 pbrook
    s->Config1 = val;
1634 a41b2ff2 pbrook
}
1635 a41b2ff2 pbrook
1636 a41b2ff2 pbrook
static uint32_t rtl8139_Config1_read(RTL8139State *s)
1637 a41b2ff2 pbrook
{
1638 a41b2ff2 pbrook
    uint32_t ret = s->Config1;
1639 a41b2ff2 pbrook
1640 7cdeb319 Benjamin Poirier
    DPRINTF("Config1 read val=0x%02x\n", ret);
1641 a41b2ff2 pbrook
1642 a41b2ff2 pbrook
    return ret;
1643 a41b2ff2 pbrook
}
1644 a41b2ff2 pbrook
1645 a41b2ff2 pbrook
static void rtl8139_Config3_write(RTL8139State *s, uint32_t val)
1646 a41b2ff2 pbrook
{
1647 a41b2ff2 pbrook
    val &= 0xff;
1648 a41b2ff2 pbrook
1649 7cdeb319 Benjamin Poirier
    DPRINTF("Config3 write val=0x%02x\n", val);
1650 a41b2ff2 pbrook
1651 ebabb67a Stefan Weil
    if (!rtl8139_config_writable(s)) {
1652 a41b2ff2 pbrook
        return;
1653 ebabb67a Stefan Weil
    }
1654 a41b2ff2 pbrook
1655 ebabb67a Stefan Weil
    /* mask unwritable bits */
1656 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x8F, s->Config3);
1657 a41b2ff2 pbrook
1658 a41b2ff2 pbrook
    s->Config3 = val;
1659 a41b2ff2 pbrook
}
1660 a41b2ff2 pbrook
1661 a41b2ff2 pbrook
static uint32_t rtl8139_Config3_read(RTL8139State *s)
1662 a41b2ff2 pbrook
{
1663 a41b2ff2 pbrook
    uint32_t ret = s->Config3;
1664 a41b2ff2 pbrook
1665 7cdeb319 Benjamin Poirier
    DPRINTF("Config3 read val=0x%02x\n", ret);
1666 a41b2ff2 pbrook
1667 a41b2ff2 pbrook
    return ret;
1668 a41b2ff2 pbrook
}
1669 a41b2ff2 pbrook
1670 a41b2ff2 pbrook
static void rtl8139_Config4_write(RTL8139State *s, uint32_t val)
1671 a41b2ff2 pbrook
{
1672 a41b2ff2 pbrook
    val &= 0xff;
1673 a41b2ff2 pbrook
1674 7cdeb319 Benjamin Poirier
    DPRINTF("Config4 write val=0x%02x\n", val);
1675 a41b2ff2 pbrook
1676 ebabb67a Stefan Weil
    if (!rtl8139_config_writable(s)) {
1677 a41b2ff2 pbrook
        return;
1678 ebabb67a Stefan Weil
    }
1679 a41b2ff2 pbrook
1680 ebabb67a Stefan Weil
    /* mask unwritable bits */
1681 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x0a, s->Config4);
1682 a41b2ff2 pbrook
1683 a41b2ff2 pbrook
    s->Config4 = val;
1684 a41b2ff2 pbrook
}
1685 a41b2ff2 pbrook
1686 a41b2ff2 pbrook
static uint32_t rtl8139_Config4_read(RTL8139State *s)
1687 a41b2ff2 pbrook
{
1688 a41b2ff2 pbrook
    uint32_t ret = s->Config4;
1689 a41b2ff2 pbrook
1690 7cdeb319 Benjamin Poirier
    DPRINTF("Config4 read val=0x%02x\n", ret);
1691 a41b2ff2 pbrook
1692 a41b2ff2 pbrook
    return ret;
1693 a41b2ff2 pbrook
}
1694 a41b2ff2 pbrook
1695 a41b2ff2 pbrook
static void rtl8139_Config5_write(RTL8139State *s, uint32_t val)
1696 a41b2ff2 pbrook
{
1697 a41b2ff2 pbrook
    val &= 0xff;
1698 a41b2ff2 pbrook
1699 7cdeb319 Benjamin Poirier
    DPRINTF("Config5 write val=0x%02x\n", val);
1700 a41b2ff2 pbrook
1701 ebabb67a Stefan Weil
    /* mask unwritable bits */
1702 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x80, s->Config5);
1703 a41b2ff2 pbrook
1704 a41b2ff2 pbrook
    s->Config5 = val;
1705 a41b2ff2 pbrook
}
1706 a41b2ff2 pbrook
1707 a41b2ff2 pbrook
static uint32_t rtl8139_Config5_read(RTL8139State *s)
1708 a41b2ff2 pbrook
{
1709 a41b2ff2 pbrook
    uint32_t ret = s->Config5;
1710 a41b2ff2 pbrook
1711 7cdeb319 Benjamin Poirier
    DPRINTF("Config5 read val=0x%02x\n", ret);
1712 a41b2ff2 pbrook
1713 a41b2ff2 pbrook
    return ret;
1714 a41b2ff2 pbrook
}
1715 a41b2ff2 pbrook
1716 a41b2ff2 pbrook
static void rtl8139_TxConfig_write(RTL8139State *s, uint32_t val)
1717 a41b2ff2 pbrook
{
1718 a41b2ff2 pbrook
    if (!rtl8139_transmitter_enabled(s))
1719 a41b2ff2 pbrook
    {
1720 7cdeb319 Benjamin Poirier
        DPRINTF("transmitter disabled; no TxConfig write val=0x%08x\n", val);
1721 a41b2ff2 pbrook
        return;
1722 a41b2ff2 pbrook
    }
1723 a41b2ff2 pbrook
1724 7cdeb319 Benjamin Poirier
    DPRINTF("TxConfig write val=0x%08x\n", val);
1725 a41b2ff2 pbrook
1726 a41b2ff2 pbrook
    val = SET_MASKED(val, TxVersionMask | 0x8070f80f, s->TxConfig);
1727 a41b2ff2 pbrook
1728 a41b2ff2 pbrook
    s->TxConfig = val;
1729 a41b2ff2 pbrook
}
1730 a41b2ff2 pbrook
1731 a41b2ff2 pbrook
static void rtl8139_TxConfig_writeb(RTL8139State *s, uint32_t val)
1732 a41b2ff2 pbrook
{
1733 7cdeb319 Benjamin Poirier
    DPRINTF("RTL8139C TxConfig via write(b) val=0x%02x\n", val);
1734 6cadb320 bellard
1735 6cadb320 bellard
    uint32_t tc = s->TxConfig;
1736 6cadb320 bellard
    tc &= 0xFFFFFF00;
1737 6cadb320 bellard
    tc |= (val & 0x000000FF);
1738 6cadb320 bellard
    rtl8139_TxConfig_write(s, tc);
1739 a41b2ff2 pbrook
}
1740 a41b2ff2 pbrook
1741 a41b2ff2 pbrook
static uint32_t rtl8139_TxConfig_read(RTL8139State *s)
1742 a41b2ff2 pbrook
{
1743 a41b2ff2 pbrook
    uint32_t ret = s->TxConfig;
1744 a41b2ff2 pbrook
1745 7cdeb319 Benjamin Poirier
    DPRINTF("TxConfig read val=0x%04x\n", ret);
1746 a41b2ff2 pbrook
1747 a41b2ff2 pbrook
    return ret;
1748 a41b2ff2 pbrook
}
1749 a41b2ff2 pbrook
1750 a41b2ff2 pbrook
static void rtl8139_RxConfig_write(RTL8139State *s, uint32_t val)
1751 a41b2ff2 pbrook
{
1752 7cdeb319 Benjamin Poirier
    DPRINTF("RxConfig write val=0x%08x\n", val);
1753 a41b2ff2 pbrook
1754 ebabb67a Stefan Weil
    /* mask unwritable bits */
1755 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xf0fc0040, s->RxConfig);
1756 a41b2ff2 pbrook
1757 a41b2ff2 pbrook
    s->RxConfig = val;
1758 a41b2ff2 pbrook
1759 a41b2ff2 pbrook
    /* reset buffer size and read/write pointers */
1760 a41b2ff2 pbrook
    rtl8139_reset_rxring(s, 8192 << ((s->RxConfig >> 11) & 0x3));
1761 a41b2ff2 pbrook
1762 7cdeb319 Benjamin Poirier
    DPRINTF("RxConfig write reset buffer size to %d\n", s->RxBufferSize);
1763 a41b2ff2 pbrook
}
1764 a41b2ff2 pbrook
1765 a41b2ff2 pbrook
static uint32_t rtl8139_RxConfig_read(RTL8139State *s)
1766 a41b2ff2 pbrook
{
1767 a41b2ff2 pbrook
    uint32_t ret = s->RxConfig;
1768 a41b2ff2 pbrook
1769 7cdeb319 Benjamin Poirier
    DPRINTF("RxConfig read val=0x%08x\n", ret);
1770 a41b2ff2 pbrook
1771 a41b2ff2 pbrook
    return ret;
1772 a41b2ff2 pbrook
}
1773 a41b2ff2 pbrook
1774 bf6b87a8 Benjamin Poirier
static void rtl8139_transfer_frame(RTL8139State *s, uint8_t *buf, int size,
1775 bf6b87a8 Benjamin Poirier
    int do_interrupt, const uint8_t *dot1q_buf)
1776 718da2b9 bellard
{
1777 bf6b87a8 Benjamin Poirier
    struct iovec *iov = NULL;
1778 bf6b87a8 Benjamin Poirier
1779 718da2b9 bellard
    if (!size)
1780 718da2b9 bellard
    {
1781 7cdeb319 Benjamin Poirier
        DPRINTF("+++ empty ethernet frame\n");
1782 718da2b9 bellard
        return;
1783 718da2b9 bellard
    }
1784 718da2b9 bellard
1785 bf6b87a8 Benjamin Poirier
    if (dot1q_buf && size >= ETHER_ADDR_LEN * 2) {
1786 bf6b87a8 Benjamin Poirier
        iov = (struct iovec[3]) {
1787 bf6b87a8 Benjamin Poirier
            { .iov_base = buf, .iov_len = ETHER_ADDR_LEN * 2 },
1788 bf6b87a8 Benjamin Poirier
            { .iov_base = (void *) dot1q_buf, .iov_len = VLAN_HLEN },
1789 bf6b87a8 Benjamin Poirier
            { .iov_base = buf + ETHER_ADDR_LEN * 2,
1790 bf6b87a8 Benjamin Poirier
                .iov_len = size - ETHER_ADDR_LEN * 2 },
1791 bf6b87a8 Benjamin Poirier
        };
1792 bf6b87a8 Benjamin Poirier
    }
1793 bf6b87a8 Benjamin Poirier
1794 718da2b9 bellard
    if (TxLoopBack == (s->TxConfig & TxLoopBack))
1795 718da2b9 bellard
    {
1796 bf6b87a8 Benjamin Poirier
        size_t buf2_size;
1797 bf6b87a8 Benjamin Poirier
        uint8_t *buf2;
1798 bf6b87a8 Benjamin Poirier
1799 bf6b87a8 Benjamin Poirier
        if (iov) {
1800 bf6b87a8 Benjamin Poirier
            buf2_size = iov_size(iov, 3);
1801 7267c094 Anthony Liguori
            buf2 = g_malloc(buf2_size);
1802 bf6b87a8 Benjamin Poirier
            iov_to_buf(iov, 3, buf2, 0, buf2_size);
1803 bf6b87a8 Benjamin Poirier
            buf = buf2;
1804 bf6b87a8 Benjamin Poirier
        }
1805 bf6b87a8 Benjamin Poirier
1806 7cdeb319 Benjamin Poirier
        DPRINTF("+++ transmit loopback mode\n");
1807 1673ad51 Mark McLoughlin
        rtl8139_do_receive(&s->nic->nc, buf, size, do_interrupt);
1808 bf6b87a8 Benjamin Poirier
1809 bf6b87a8 Benjamin Poirier
        if (iov) {
1810 7267c094 Anthony Liguori
            g_free(buf2);
1811 bf6b87a8 Benjamin Poirier
        }
1812 718da2b9 bellard
    }
1813 718da2b9 bellard
    else
1814 718da2b9 bellard
    {
1815 bf6b87a8 Benjamin Poirier
        if (iov) {
1816 bf6b87a8 Benjamin Poirier
            qemu_sendv_packet(&s->nic->nc, iov, 3);
1817 bf6b87a8 Benjamin Poirier
        } else {
1818 bf6b87a8 Benjamin Poirier
            qemu_send_packet(&s->nic->nc, buf, size);
1819 bf6b87a8 Benjamin Poirier
        }
1820 718da2b9 bellard
    }
1821 718da2b9 bellard
}
1822 718da2b9 bellard
1823 a41b2ff2 pbrook
static int rtl8139_transmit_one(RTL8139State *s, int descriptor)
1824 a41b2ff2 pbrook
{
1825 a41b2ff2 pbrook
    if (!rtl8139_transmitter_enabled(s))
1826 a41b2ff2 pbrook
    {
1827 7cdeb319 Benjamin Poirier
        DPRINTF("+++ cannot transmit from descriptor %d: transmitter "
1828 7cdeb319 Benjamin Poirier
            "disabled\n", descriptor);
1829 a41b2ff2 pbrook
        return 0;
1830 a41b2ff2 pbrook
    }
1831 a41b2ff2 pbrook
1832 a41b2ff2 pbrook
    if (s->TxStatus[descriptor] & TxHostOwns)
1833 a41b2ff2 pbrook
    {
1834 7cdeb319 Benjamin Poirier
        DPRINTF("+++ cannot transmit from descriptor %d: owned by host "
1835 7cdeb319 Benjamin Poirier
            "(%08x)\n", descriptor, s->TxStatus[descriptor]);
1836 a41b2ff2 pbrook
        return 0;
1837 a41b2ff2 pbrook
    }
1838 a41b2ff2 pbrook
1839 7cdeb319 Benjamin Poirier
    DPRINTF("+++ transmitting from descriptor %d\n", descriptor);
1840 a41b2ff2 pbrook
1841 a41b2ff2 pbrook
    int txsize = s->TxStatus[descriptor] & 0x1fff;
1842 a41b2ff2 pbrook
    uint8_t txbuffer[0x2000];
1843 a41b2ff2 pbrook
1844 7cdeb319 Benjamin Poirier
    DPRINTF("+++ transmit reading %d bytes from host memory at 0x%08x\n",
1845 7cdeb319 Benjamin Poirier
        txsize, s->TxAddr[descriptor]);
1846 a41b2ff2 pbrook
1847 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, s->TxAddr[descriptor], txbuffer, txsize);
1848 a41b2ff2 pbrook
1849 a41b2ff2 pbrook
    /* Mark descriptor as transferred */
1850 a41b2ff2 pbrook
    s->TxStatus[descriptor] |= TxHostOwns;
1851 a41b2ff2 pbrook
    s->TxStatus[descriptor] |= TxStatOK;
1852 a41b2ff2 pbrook
1853 bf6b87a8 Benjamin Poirier
    rtl8139_transfer_frame(s, txbuffer, txsize, 0, NULL);
1854 6cadb320 bellard
1855 7cdeb319 Benjamin Poirier
    DPRINTF("+++ transmitted %d bytes from descriptor %d\n", txsize,
1856 7cdeb319 Benjamin Poirier
        descriptor);
1857 a41b2ff2 pbrook
1858 a41b2ff2 pbrook
    /* update interrupt */
1859 a41b2ff2 pbrook
    s->IntrStatus |= TxOK;
1860 a41b2ff2 pbrook
    rtl8139_update_irq(s);
1861 a41b2ff2 pbrook
1862 a41b2ff2 pbrook
    return 1;
1863 a41b2ff2 pbrook
}
1864 a41b2ff2 pbrook
1865 718da2b9 bellard
/* structures and macros for task offloading */
1866 718da2b9 bellard
typedef struct ip_header
1867 718da2b9 bellard
{
1868 718da2b9 bellard
    uint8_t  ip_ver_len;    /* version and header length */
1869 718da2b9 bellard
    uint8_t  ip_tos;        /* type of service */
1870 718da2b9 bellard
    uint16_t ip_len;        /* total length */
1871 718da2b9 bellard
    uint16_t ip_id;         /* identification */
1872 718da2b9 bellard
    uint16_t ip_off;        /* fragment offset field */
1873 718da2b9 bellard
    uint8_t  ip_ttl;        /* time to live */
1874 718da2b9 bellard
    uint8_t  ip_p;          /* protocol */
1875 718da2b9 bellard
    uint16_t ip_sum;        /* checksum */
1876 718da2b9 bellard
    uint32_t ip_src,ip_dst; /* source and dest address */
1877 718da2b9 bellard
} ip_header;
1878 718da2b9 bellard
1879 718da2b9 bellard
#define IP_HEADER_VERSION_4 4
1880 718da2b9 bellard
#define IP_HEADER_VERSION(ip) ((ip->ip_ver_len >> 4)&0xf)
1881 718da2b9 bellard
#define IP_HEADER_LENGTH(ip) (((ip->ip_ver_len)&0xf) << 2)
1882 718da2b9 bellard
1883 718da2b9 bellard
typedef struct tcp_header
1884 718da2b9 bellard
{
1885 718da2b9 bellard
    uint16_t th_sport;                /* source port */
1886 718da2b9 bellard
    uint16_t th_dport;                /* destination port */
1887 718da2b9 bellard
    uint32_t th_seq;                        /* sequence number */
1888 718da2b9 bellard
    uint32_t th_ack;                        /* acknowledgement number */
1889 718da2b9 bellard
    uint16_t th_offset_flags; /* data offset, reserved 6 bits, TCP protocol flags */
1890 718da2b9 bellard
    uint16_t th_win;                        /* window */
1891 718da2b9 bellard
    uint16_t th_sum;                        /* checksum */
1892 718da2b9 bellard
    uint16_t th_urp;                        /* urgent pointer */
1893 718da2b9 bellard
} tcp_header;
1894 718da2b9 bellard
1895 718da2b9 bellard
typedef struct udp_header
1896 718da2b9 bellard
{
1897 718da2b9 bellard
    uint16_t uh_sport; /* source port */
1898 718da2b9 bellard
    uint16_t uh_dport; /* destination port */
1899 718da2b9 bellard
    uint16_t uh_ulen;  /* udp length */
1900 718da2b9 bellard
    uint16_t uh_sum;   /* udp checksum */
1901 718da2b9 bellard
} udp_header;
1902 718da2b9 bellard
1903 718da2b9 bellard
typedef struct ip_pseudo_header
1904 718da2b9 bellard
{
1905 718da2b9 bellard
    uint32_t ip_src;
1906 718da2b9 bellard
    uint32_t ip_dst;
1907 718da2b9 bellard
    uint8_t  zeros;
1908 718da2b9 bellard
    uint8_t  ip_proto;
1909 718da2b9 bellard
    uint16_t ip_payload;
1910 718da2b9 bellard
} ip_pseudo_header;
1911 718da2b9 bellard
1912 718da2b9 bellard
#define IP_PROTO_TCP 6
1913 718da2b9 bellard
#define IP_PROTO_UDP 17
1914 718da2b9 bellard
1915 718da2b9 bellard
#define TCP_HEADER_DATA_OFFSET(tcp) (((be16_to_cpu(tcp->th_offset_flags) >> 12)&0xf) << 2)
1916 718da2b9 bellard
#define TCP_FLAGS_ONLY(flags) ((flags)&0x3f)
1917 718da2b9 bellard
#define TCP_HEADER_FLAGS(tcp) TCP_FLAGS_ONLY(be16_to_cpu(tcp->th_offset_flags))
1918 718da2b9 bellard
1919 718da2b9 bellard
#define TCP_HEADER_CLEAR_FLAGS(tcp, off) ((tcp)->th_offset_flags &= cpu_to_be16(~TCP_FLAGS_ONLY(off)))
1920 718da2b9 bellard
1921 718da2b9 bellard
#define TCP_FLAG_FIN  0x01
1922 718da2b9 bellard
#define TCP_FLAG_PUSH 0x08
1923 718da2b9 bellard
1924 718da2b9 bellard
/* produces ones' complement sum of data */
1925 718da2b9 bellard
static uint16_t ones_complement_sum(uint8_t *data, size_t len)
1926 718da2b9 bellard
{
1927 718da2b9 bellard
    uint32_t result = 0;
1928 718da2b9 bellard
1929 718da2b9 bellard
    for (; len > 1; data+=2, len-=2)
1930 718da2b9 bellard
    {
1931 718da2b9 bellard
        result += *(uint16_t*)data;
1932 718da2b9 bellard
    }
1933 718da2b9 bellard
1934 718da2b9 bellard
    /* add the remainder byte */
1935 718da2b9 bellard
    if (len)
1936 718da2b9 bellard
    {
1937 718da2b9 bellard
        uint8_t odd[2] = {*data, 0};
1938 718da2b9 bellard
        result += *(uint16_t*)odd;
1939 718da2b9 bellard
    }
1940 718da2b9 bellard
1941 718da2b9 bellard
    while (result>>16)
1942 718da2b9 bellard
        result = (result & 0xffff) + (result >> 16);
1943 718da2b9 bellard
1944 718da2b9 bellard
    return result;
1945 718da2b9 bellard
}
1946 718da2b9 bellard
1947 718da2b9 bellard
static uint16_t ip_checksum(void *data, size_t len)
1948 718da2b9 bellard
{
1949 718da2b9 bellard
    return ~ones_complement_sum((uint8_t*)data, len);
1950 718da2b9 bellard
}
1951 718da2b9 bellard
1952 a41b2ff2 pbrook
static int rtl8139_cplus_transmit_one(RTL8139State *s)
1953 a41b2ff2 pbrook
{
1954 a41b2ff2 pbrook
    if (!rtl8139_transmitter_enabled(s))
1955 a41b2ff2 pbrook
    {
1956 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode: transmitter disabled\n");
1957 a41b2ff2 pbrook
        return 0;
1958 a41b2ff2 pbrook
    }
1959 a41b2ff2 pbrook
1960 a41b2ff2 pbrook
    if (!rtl8139_cp_transmitter_enabled(s))
1961 a41b2ff2 pbrook
    {
1962 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode: C+ transmitter disabled\n");
1963 a41b2ff2 pbrook
        return 0 ;
1964 a41b2ff2 pbrook
    }
1965 a41b2ff2 pbrook
1966 a41b2ff2 pbrook
    int descriptor = s->currCPlusTxDesc;
1967 a41b2ff2 pbrook
1968 3ada003a Eduard - Gabriel Munteanu
    dma_addr_t cplus_tx_ring_desc = rtl8139_addr64(s->TxAddr[0], s->TxAddr[1]);
1969 a41b2ff2 pbrook
1970 a41b2ff2 pbrook
    /* Normal priority ring */
1971 a41b2ff2 pbrook
    cplus_tx_ring_desc += 16 * descriptor;
1972 a41b2ff2 pbrook
1973 7cdeb319 Benjamin Poirier
    DPRINTF("+++ C+ mode reading TX descriptor %d from host memory at "
1974 4abf12f4 Julian Pidancet
        "%08x %08x = 0x"DMA_ADDR_FMT"\n", descriptor, s->TxAddr[1],
1975 7cdeb319 Benjamin Poirier
        s->TxAddr[0], cplus_tx_ring_desc);
1976 a41b2ff2 pbrook
1977 a41b2ff2 pbrook
    uint32_t val, txdw0,txdw1,txbufLO,txbufHI;
1978 a41b2ff2 pbrook
1979 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, cplus_tx_ring_desc,    (uint8_t *)&val, 4);
1980 a41b2ff2 pbrook
    txdw0 = le32_to_cpu(val);
1981 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, cplus_tx_ring_desc+4,  (uint8_t *)&val, 4);
1982 a41b2ff2 pbrook
    txdw1 = le32_to_cpu(val);
1983 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, cplus_tx_ring_desc+8,  (uint8_t *)&val, 4);
1984 a41b2ff2 pbrook
    txbufLO = le32_to_cpu(val);
1985 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, cplus_tx_ring_desc+12, (uint8_t *)&val, 4);
1986 a41b2ff2 pbrook
    txbufHI = le32_to_cpu(val);
1987 a41b2ff2 pbrook
1988 7cdeb319 Benjamin Poirier
    DPRINTF("+++ C+ mode TX descriptor %d %08x %08x %08x %08x\n", descriptor,
1989 7cdeb319 Benjamin Poirier
        txdw0, txdw1, txbufLO, txbufHI);
1990 a41b2ff2 pbrook
1991 a41b2ff2 pbrook
/* w0 ownership flag */
1992 a41b2ff2 pbrook
#define CP_TX_OWN (1<<31)
1993 a41b2ff2 pbrook
/* w0 end of ring flag */
1994 a41b2ff2 pbrook
#define CP_TX_EOR (1<<30)
1995 a41b2ff2 pbrook
/* first segment of received packet flag */
1996 a41b2ff2 pbrook
#define CP_TX_FS (1<<29)
1997 a41b2ff2 pbrook
/* last segment of received packet flag */
1998 a41b2ff2 pbrook
#define CP_TX_LS (1<<28)
1999 a41b2ff2 pbrook
/* large send packet flag */
2000 a41b2ff2 pbrook
#define CP_TX_LGSEN (1<<27)
2001 718da2b9 bellard
/* large send MSS mask, bits 16...25 */
2002 718da2b9 bellard
#define CP_TC_LGSEN_MSS_MASK ((1 << 12) - 1)
2003 718da2b9 bellard
2004 a41b2ff2 pbrook
/* IP checksum offload flag */
2005 a41b2ff2 pbrook
#define CP_TX_IPCS (1<<18)
2006 a41b2ff2 pbrook
/* UDP checksum offload flag */
2007 a41b2ff2 pbrook
#define CP_TX_UDPCS (1<<17)
2008 a41b2ff2 pbrook
/* TCP checksum offload flag */
2009 a41b2ff2 pbrook
#define CP_TX_TCPCS (1<<16)
2010 a41b2ff2 pbrook
2011 a41b2ff2 pbrook
/* w0 bits 0...15 : buffer size */
2012 a41b2ff2 pbrook
#define CP_TX_BUFFER_SIZE (1<<16)
2013 a41b2ff2 pbrook
#define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1)
2014 bf6b87a8 Benjamin Poirier
/* w1 add tag flag */
2015 bf6b87a8 Benjamin Poirier
#define CP_TX_TAGC (1<<17)
2016 bf6b87a8 Benjamin Poirier
/* w1 bits 0...15 : VLAN tag (big endian) */
2017 a41b2ff2 pbrook
#define CP_TX_VLAN_TAG_MASK ((1<<16) - 1)
2018 a41b2ff2 pbrook
/* w2 low  32bit of Rx buffer ptr */
2019 a41b2ff2 pbrook
/* w3 high 32bit of Rx buffer ptr */
2020 a41b2ff2 pbrook
2021 a41b2ff2 pbrook
/* set after transmission */
2022 a41b2ff2 pbrook
/* FIFO underrun flag */
2023 a41b2ff2 pbrook
#define CP_TX_STATUS_UNF (1<<25)
2024 a41b2ff2 pbrook
/* transmit error summary flag, valid if set any of three below */
2025 a41b2ff2 pbrook
#define CP_TX_STATUS_TES (1<<23)
2026 a41b2ff2 pbrook
/* out-of-window collision flag */
2027 a41b2ff2 pbrook
#define CP_TX_STATUS_OWC (1<<22)
2028 a41b2ff2 pbrook
/* link failure flag */
2029 a41b2ff2 pbrook
#define CP_TX_STATUS_LNKF (1<<21)
2030 a41b2ff2 pbrook
/* excessive collisions flag */
2031 a41b2ff2 pbrook
#define CP_TX_STATUS_EXC (1<<20)
2032 a41b2ff2 pbrook
2033 a41b2ff2 pbrook
    if (!(txdw0 & CP_TX_OWN))
2034 a41b2ff2 pbrook
    {
2035 7cdeb319 Benjamin Poirier
        DPRINTF("C+ Tx mode : descriptor %d is owned by host\n", descriptor);
2036 a41b2ff2 pbrook
        return 0 ;
2037 a41b2ff2 pbrook
    }
2038 a41b2ff2 pbrook
2039 7cdeb319 Benjamin Poirier
    DPRINTF("+++ C+ Tx mode : transmitting from descriptor %d\n", descriptor);
2040 6cadb320 bellard
2041 6cadb320 bellard
    if (txdw0 & CP_TX_FS)
2042 6cadb320 bellard
    {
2043 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ Tx mode : descriptor %d is first segment "
2044 7cdeb319 Benjamin Poirier
            "descriptor\n", descriptor);
2045 6cadb320 bellard
2046 6cadb320 bellard
        /* reset internal buffer offset */
2047 6cadb320 bellard
        s->cplus_txbuffer_offset = 0;
2048 6cadb320 bellard
    }
2049 a41b2ff2 pbrook
2050 a41b2ff2 pbrook
    int txsize = txdw0 & CP_TX_BUFFER_SIZE_MASK;
2051 3ada003a Eduard - Gabriel Munteanu
    dma_addr_t tx_addr = rtl8139_addr64(txbufLO, txbufHI);
2052 a41b2ff2 pbrook
2053 6cadb320 bellard
    /* make sure we have enough space to assemble the packet */
2054 6cadb320 bellard
    if (!s->cplus_txbuffer)
2055 6cadb320 bellard
    {
2056 6cadb320 bellard
        s->cplus_txbuffer_len = CP_TX_BUFFER_SIZE;
2057 7267c094 Anthony Liguori
        s->cplus_txbuffer = g_malloc(s->cplus_txbuffer_len);
2058 6cadb320 bellard
        s->cplus_txbuffer_offset = 0;
2059 718da2b9 bellard
2060 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode transmission buffer allocated space %d\n",
2061 7cdeb319 Benjamin Poirier
            s->cplus_txbuffer_len);
2062 6cadb320 bellard
    }
2063 6cadb320 bellard
2064 6cadb320 bellard
    while (s->cplus_txbuffer && s->cplus_txbuffer_offset + txsize >= s->cplus_txbuffer_len)
2065 6cadb320 bellard
    {
2066 6cadb320 bellard
        s->cplus_txbuffer_len += CP_TX_BUFFER_SIZE;
2067 7267c094 Anthony Liguori
        s->cplus_txbuffer = g_realloc(s->cplus_txbuffer, s->cplus_txbuffer_len);
2068 a41b2ff2 pbrook
2069 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode transmission buffer space changed to %d\n",
2070 7cdeb319 Benjamin Poirier
            s->cplus_txbuffer_len);
2071 6cadb320 bellard
    }
2072 6cadb320 bellard
2073 6cadb320 bellard
    if (!s->cplus_txbuffer)
2074 6cadb320 bellard
    {
2075 6cadb320 bellard
        /* out of memory */
2076 a41b2ff2 pbrook
2077 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode transmiter failed to reallocate %d bytes\n",
2078 7cdeb319 Benjamin Poirier
            s->cplus_txbuffer_len);
2079 6cadb320 bellard
2080 6cadb320 bellard
        /* update tally counter */
2081 6cadb320 bellard
        ++s->tally_counters.TxERR;
2082 6cadb320 bellard
        ++s->tally_counters.TxAbt;
2083 6cadb320 bellard
2084 6cadb320 bellard
        return 0;
2085 6cadb320 bellard
    }
2086 6cadb320 bellard
2087 6cadb320 bellard
    /* append more data to the packet */
2088 6cadb320 bellard
2089 7cdeb319 Benjamin Poirier
    DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
2090 3ada003a Eduard - Gabriel Munteanu
            DMA_ADDR_FMT" to offset %d\n", txsize, tx_addr,
2091 3ada003a Eduard - Gabriel Munteanu
            s->cplus_txbuffer_offset);
2092 6cadb320 bellard
2093 3ada003a Eduard - Gabriel Munteanu
    pci_dma_read(&s->dev, tx_addr,
2094 3ada003a Eduard - Gabriel Munteanu
                 s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize);
2095 6cadb320 bellard
    s->cplus_txbuffer_offset += txsize;
2096 6cadb320 bellard
2097 6cadb320 bellard
    /* seek to next Rx descriptor */
2098 6cadb320 bellard
    if (txdw0 & CP_TX_EOR)
2099 6cadb320 bellard
    {
2100 6cadb320 bellard
        s->currCPlusTxDesc = 0;
2101 6cadb320 bellard
    }
2102 6cadb320 bellard
    else
2103 6cadb320 bellard
    {
2104 6cadb320 bellard
        ++s->currCPlusTxDesc;
2105 6cadb320 bellard
        if (s->currCPlusTxDesc >= 64)
2106 6cadb320 bellard
            s->currCPlusTxDesc = 0;
2107 6cadb320 bellard
    }
2108 a41b2ff2 pbrook
2109 a41b2ff2 pbrook
    /* transfer ownership to target */
2110 a41b2ff2 pbrook
    txdw0 &= ~CP_RX_OWN;
2111 a41b2ff2 pbrook
2112 a41b2ff2 pbrook
    /* reset error indicator bits */
2113 a41b2ff2 pbrook
    txdw0 &= ~CP_TX_STATUS_UNF;
2114 a41b2ff2 pbrook
    txdw0 &= ~CP_TX_STATUS_TES;
2115 a41b2ff2 pbrook
    txdw0 &= ~CP_TX_STATUS_OWC;
2116 a41b2ff2 pbrook
    txdw0 &= ~CP_TX_STATUS_LNKF;
2117 a41b2ff2 pbrook
    txdw0 &= ~CP_TX_STATUS_EXC;
2118 a41b2ff2 pbrook
2119 a41b2ff2 pbrook
    /* update ring data */
2120 a41b2ff2 pbrook
    val = cpu_to_le32(txdw0);
2121 3ada003a Eduard - Gabriel Munteanu
    pci_dma_write(&s->dev, cplus_tx_ring_desc, (uint8_t *)&val, 4);
2122 a41b2ff2 pbrook
2123 6cadb320 bellard
    /* Now decide if descriptor being processed is holding the last segment of packet */
2124 6cadb320 bellard
    if (txdw0 & CP_TX_LS)
2125 a41b2ff2 pbrook
    {
2126 bf6b87a8 Benjamin Poirier
        uint8_t dot1q_buffer_space[VLAN_HLEN];
2127 bf6b87a8 Benjamin Poirier
        uint16_t *dot1q_buffer;
2128 bf6b87a8 Benjamin Poirier
2129 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ Tx mode : descriptor %d is last segment descriptor\n",
2130 7cdeb319 Benjamin Poirier
            descriptor);
2131 6cadb320 bellard
2132 6cadb320 bellard
        /* can transfer fully assembled packet */
2133 6cadb320 bellard
2134 6cadb320 bellard
        uint8_t *saved_buffer  = s->cplus_txbuffer;
2135 6cadb320 bellard
        int      saved_size    = s->cplus_txbuffer_offset;
2136 6cadb320 bellard
        int      saved_buffer_len = s->cplus_txbuffer_len;
2137 6cadb320 bellard
2138 bf6b87a8 Benjamin Poirier
        /* create vlan tag */
2139 bf6b87a8 Benjamin Poirier
        if (txdw1 & CP_TX_TAGC) {
2140 bf6b87a8 Benjamin Poirier
            /* the vlan tag is in BE byte order in the descriptor
2141 bf6b87a8 Benjamin Poirier
             * BE + le_to_cpu() + ~swap()~ = cpu */
2142 7cdeb319 Benjamin Poirier
            DPRINTF("+++ C+ Tx mode : inserting vlan tag with ""tci: %u\n",
2143 7cdeb319 Benjamin Poirier
                bswap16(txdw1 & CP_TX_VLAN_TAG_MASK));
2144 bf6b87a8 Benjamin Poirier
2145 bf6b87a8 Benjamin Poirier
            dot1q_buffer = (uint16_t *) dot1q_buffer_space;
2146 bf6b87a8 Benjamin Poirier
            dot1q_buffer[0] = cpu_to_be16(ETH_P_8021Q);
2147 bf6b87a8 Benjamin Poirier
            /* BE + le_to_cpu() + ~cpu_to_le()~ = BE */
2148 bf6b87a8 Benjamin Poirier
            dot1q_buffer[1] = cpu_to_le16(txdw1 & CP_TX_VLAN_TAG_MASK);
2149 bf6b87a8 Benjamin Poirier
        } else {
2150 bf6b87a8 Benjamin Poirier
            dot1q_buffer = NULL;
2151 bf6b87a8 Benjamin Poirier
        }
2152 bf6b87a8 Benjamin Poirier
2153 6cadb320 bellard
        /* reset the card space to protect from recursive call */
2154 6cadb320 bellard
        s->cplus_txbuffer = NULL;
2155 6cadb320 bellard
        s->cplus_txbuffer_offset = 0;
2156 6cadb320 bellard
        s->cplus_txbuffer_len = 0;
2157 6cadb320 bellard
2158 718da2b9 bellard
        if (txdw0 & (CP_TX_IPCS | CP_TX_UDPCS | CP_TX_TCPCS | CP_TX_LGSEN))
2159 6cadb320 bellard
        {
2160 7cdeb319 Benjamin Poirier
            DPRINTF("+++ C+ mode offloaded task checksum\n");
2161 6cadb320 bellard
2162 6cadb320 bellard
            /* ip packet header */
2163 660f11be Blue Swirl
            ip_header *ip = NULL;
2164 6cadb320 bellard
            int hlen = 0;
2165 718da2b9 bellard
            uint8_t  ip_protocol = 0;
2166 718da2b9 bellard
            uint16_t ip_data_len = 0;
2167 6cadb320 bellard
2168 660f11be Blue Swirl
            uint8_t *eth_payload_data = NULL;
2169 718da2b9 bellard
            size_t   eth_payload_len  = 0;
2170 6cadb320 bellard
2171 718da2b9 bellard
            int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12));
2172 6cadb320 bellard
            if (proto == ETH_P_IP)
2173 6cadb320 bellard
            {
2174 7cdeb319 Benjamin Poirier
                DPRINTF("+++ C+ mode has IP packet\n");
2175 6cadb320 bellard
2176 6cadb320 bellard
                /* not aligned */
2177 718da2b9 bellard
                eth_payload_data = saved_buffer + ETH_HLEN;
2178 718da2b9 bellard
                eth_payload_len  = saved_size   - ETH_HLEN;
2179 6cadb320 bellard
2180 718da2b9 bellard
                ip = (ip_header*)eth_payload_data;
2181 6cadb320 bellard
2182 718da2b9 bellard
                if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
2183 7cdeb319 Benjamin Poirier
                    DPRINTF("+++ C+ mode packet has bad IP version %d "
2184 7cdeb319 Benjamin Poirier
                        "expected %d\n", IP_HEADER_VERSION(ip),
2185 7cdeb319 Benjamin Poirier
                        IP_HEADER_VERSION_4);
2186 6cadb320 bellard
                    ip = NULL;
2187 6cadb320 bellard
                } else {
2188 718da2b9 bellard
                    hlen = IP_HEADER_LENGTH(ip);
2189 718da2b9 bellard
                    ip_protocol = ip->ip_p;
2190 718da2b9 bellard
                    ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
2191 6cadb320 bellard
                }
2192 6cadb320 bellard
            }
2193 6cadb320 bellard
2194 6cadb320 bellard
            if (ip)
2195 6cadb320 bellard
            {
2196 6cadb320 bellard
                if (txdw0 & CP_TX_IPCS)
2197 6cadb320 bellard
                {
2198 7cdeb319 Benjamin Poirier
                    DPRINTF("+++ C+ mode need IP checksum\n");
2199 6cadb320 bellard
2200 718da2b9 bellard
                    if (hlen<sizeof(ip_header) || hlen>eth_payload_len) {/* min header length */
2201 6cadb320 bellard
                        /* bad packet header len */
2202 6cadb320 bellard
                        /* or packet too short */
2203 6cadb320 bellard
                    }
2204 6cadb320 bellard
                    else
2205 6cadb320 bellard
                    {
2206 6cadb320 bellard
                        ip->ip_sum = 0;
2207 718da2b9 bellard
                        ip->ip_sum = ip_checksum(ip, hlen);
2208 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode IP header len=%d checksum=%04x\n",
2209 7cdeb319 Benjamin Poirier
                            hlen, ip->ip_sum);
2210 6cadb320 bellard
                    }
2211 6cadb320 bellard
                }
2212 6cadb320 bellard
2213 718da2b9 bellard
                if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
2214 6cadb320 bellard
                {
2215 718da2b9 bellard
                    int large_send_mss = (txdw0 >> 16) & CP_TC_LGSEN_MSS_MASK;
2216 ec48c774 Benjamin Poirier
2217 7cdeb319 Benjamin Poirier
                    DPRINTF("+++ C+ mode offloaded task TSO MTU=%d IP data %d "
2218 7cdeb319 Benjamin Poirier
                        "frame data %d specified MSS=%d\n", ETH_MTU,
2219 7cdeb319 Benjamin Poirier
                        ip_data_len, saved_size - ETH_HLEN, large_send_mss);
2220 6cadb320 bellard
2221 718da2b9 bellard
                    int tcp_send_offset = 0;
2222 718da2b9 bellard
                    int send_count = 0;
2223 6cadb320 bellard
2224 6cadb320 bellard
                    /* maximum IP header length is 60 bytes */
2225 6cadb320 bellard
                    uint8_t saved_ip_header[60];
2226 6cadb320 bellard
2227 718da2b9 bellard
                    /* save IP header template; data area is used in tcp checksum calculation */
2228 718da2b9 bellard
                    memcpy(saved_ip_header, eth_payload_data, hlen);
2229 718da2b9 bellard
2230 718da2b9 bellard
                    /* a placeholder for checksum calculation routine in tcp case */
2231 718da2b9 bellard
                    uint8_t *data_to_checksum     = eth_payload_data + hlen - 12;
2232 718da2b9 bellard
                    //                    size_t   data_to_checksum_len = eth_payload_len  - hlen + 12;
2233 718da2b9 bellard
2234 718da2b9 bellard
                    /* pointer to TCP header */
2235 718da2b9 bellard
                    tcp_header *p_tcp_hdr = (tcp_header*)(eth_payload_data + hlen);
2236 718da2b9 bellard
2237 718da2b9 bellard
                    int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
2238 718da2b9 bellard
2239 718da2b9 bellard
                    /* ETH_MTU = ip header len + tcp header len + payload */
2240 718da2b9 bellard
                    int tcp_data_len = ip_data_len - tcp_hlen;
2241 718da2b9 bellard
                    int tcp_chunk_size = ETH_MTU - hlen - tcp_hlen;
2242 718da2b9 bellard
2243 7cdeb319 Benjamin Poirier
                    DPRINTF("+++ C+ mode TSO IP data len %d TCP hlen %d TCP "
2244 7cdeb319 Benjamin Poirier
                        "data len %d TCP chunk size %d\n", ip_data_len,
2245 7cdeb319 Benjamin Poirier
                        tcp_hlen, tcp_data_len, tcp_chunk_size);
2246 718da2b9 bellard
2247 718da2b9 bellard
                    /* note the cycle below overwrites IP header data,
2248 718da2b9 bellard
                       but restores it from saved_ip_header before sending packet */
2249 718da2b9 bellard
2250 718da2b9 bellard
                    int is_last_frame = 0;
2251 718da2b9 bellard
2252 718da2b9 bellard
                    for (tcp_send_offset = 0; tcp_send_offset < tcp_data_len; tcp_send_offset += tcp_chunk_size)
2253 718da2b9 bellard
                    {
2254 718da2b9 bellard
                        uint16_t chunk_size = tcp_chunk_size;
2255 718da2b9 bellard
2256 718da2b9 bellard
                        /* check if this is the last frame */
2257 718da2b9 bellard
                        if (tcp_send_offset + tcp_chunk_size >= tcp_data_len)
2258 718da2b9 bellard
                        {
2259 718da2b9 bellard
                            is_last_frame = 1;
2260 718da2b9 bellard
                            chunk_size = tcp_data_len - tcp_send_offset;
2261 718da2b9 bellard
                        }
2262 718da2b9 bellard
2263 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TSO TCP seqno %08x\n",
2264 7cdeb319 Benjamin Poirier
                            be32_to_cpu(p_tcp_hdr->th_seq));
2265 718da2b9 bellard
2266 718da2b9 bellard
                        /* add 4 TCP pseudoheader fields */
2267 718da2b9 bellard
                        /* copy IP source and destination fields */
2268 718da2b9 bellard
                        memcpy(data_to_checksum, saved_ip_header + 12, 8);
2269 718da2b9 bellard
2270 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TSO calculating TCP checksum for "
2271 7cdeb319 Benjamin Poirier
                            "packet with %d bytes data\n", tcp_hlen +
2272 7cdeb319 Benjamin Poirier
                            chunk_size);
2273 718da2b9 bellard
2274 718da2b9 bellard
                        if (tcp_send_offset)
2275 718da2b9 bellard
                        {
2276 718da2b9 bellard
                            memcpy((uint8_t*)p_tcp_hdr + tcp_hlen, (uint8_t*)p_tcp_hdr + tcp_hlen + tcp_send_offset, chunk_size);
2277 718da2b9 bellard
                        }
2278 718da2b9 bellard
2279 718da2b9 bellard
                        /* keep PUSH and FIN flags only for the last frame */
2280 718da2b9 bellard
                        if (!is_last_frame)
2281 718da2b9 bellard
                        {
2282 718da2b9 bellard
                            TCP_HEADER_CLEAR_FLAGS(p_tcp_hdr, TCP_FLAG_PUSH|TCP_FLAG_FIN);
2283 718da2b9 bellard
                        }
2284 6cadb320 bellard
2285 718da2b9 bellard
                        /* recalculate TCP checksum */
2286 718da2b9 bellard
                        ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
2287 718da2b9 bellard
                        p_tcpip_hdr->zeros      = 0;
2288 718da2b9 bellard
                        p_tcpip_hdr->ip_proto   = IP_PROTO_TCP;
2289 718da2b9 bellard
                        p_tcpip_hdr->ip_payload = cpu_to_be16(tcp_hlen + chunk_size);
2290 718da2b9 bellard
2291 718da2b9 bellard
                        p_tcp_hdr->th_sum = 0;
2292 718da2b9 bellard
2293 718da2b9 bellard
                        int tcp_checksum = ip_checksum(data_to_checksum, tcp_hlen + chunk_size + 12);
2294 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TSO TCP checksum %04x\n",
2295 7cdeb319 Benjamin Poirier
                            tcp_checksum);
2296 718da2b9 bellard
2297 718da2b9 bellard
                        p_tcp_hdr->th_sum = tcp_checksum;
2298 718da2b9 bellard
2299 718da2b9 bellard
                        /* restore IP header */
2300 718da2b9 bellard
                        memcpy(eth_payload_data, saved_ip_header, hlen);
2301 718da2b9 bellard
2302 718da2b9 bellard
                        /* set IP data length and recalculate IP checksum */
2303 718da2b9 bellard
                        ip->ip_len = cpu_to_be16(hlen + tcp_hlen + chunk_size);
2304 718da2b9 bellard
2305 718da2b9 bellard
                        /* increment IP id for subsequent frames */
2306 718da2b9 bellard
                        ip->ip_id = cpu_to_be16(tcp_send_offset/tcp_chunk_size + be16_to_cpu(ip->ip_id));
2307 718da2b9 bellard
2308 718da2b9 bellard
                        ip->ip_sum = 0;
2309 718da2b9 bellard
                        ip->ip_sum = ip_checksum(eth_payload_data, hlen);
2310 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TSO IP header len=%d "
2311 7cdeb319 Benjamin Poirier
                            "checksum=%04x\n", hlen, ip->ip_sum);
2312 718da2b9 bellard
2313 718da2b9 bellard
                        int tso_send_size = ETH_HLEN + hlen + tcp_hlen + chunk_size;
2314 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TSO transferring packet size "
2315 7cdeb319 Benjamin Poirier
                            "%d\n", tso_send_size);
2316 bf6b87a8 Benjamin Poirier
                        rtl8139_transfer_frame(s, saved_buffer, tso_send_size,
2317 bf6b87a8 Benjamin Poirier
                            0, (uint8_t *) dot1q_buffer);
2318 718da2b9 bellard
2319 718da2b9 bellard
                        /* add transferred count to TCP sequence number */
2320 718da2b9 bellard
                        p_tcp_hdr->th_seq = cpu_to_be32(chunk_size + be32_to_cpu(p_tcp_hdr->th_seq));
2321 718da2b9 bellard
                        ++send_count;
2322 718da2b9 bellard
                    }
2323 718da2b9 bellard
2324 718da2b9 bellard
                    /* Stop sending this frame */
2325 718da2b9 bellard
                    saved_size = 0;
2326 718da2b9 bellard
                }
2327 718da2b9 bellard
                else if (txdw0 & (CP_TX_TCPCS|CP_TX_UDPCS))
2328 718da2b9 bellard
                {
2329 7cdeb319 Benjamin Poirier
                    DPRINTF("+++ C+ mode need TCP or UDP checksum\n");
2330 718da2b9 bellard
2331 718da2b9 bellard
                    /* maximum IP header length is 60 bytes */
2332 718da2b9 bellard
                    uint8_t saved_ip_header[60];
2333 718da2b9 bellard
                    memcpy(saved_ip_header, eth_payload_data, hlen);
2334 718da2b9 bellard
2335 718da2b9 bellard
                    uint8_t *data_to_checksum     = eth_payload_data + hlen - 12;
2336 718da2b9 bellard
                    //                    size_t   data_to_checksum_len = eth_payload_len  - hlen + 12;
2337 6cadb320 bellard
2338 6cadb320 bellard
                    /* add 4 TCP pseudoheader fields */
2339 6cadb320 bellard
                    /* copy IP source and destination fields */
2340 718da2b9 bellard
                    memcpy(data_to_checksum, saved_ip_header + 12, 8);
2341 6cadb320 bellard
2342 718da2b9 bellard
                    if ((txdw0 & CP_TX_TCPCS) && ip_protocol == IP_PROTO_TCP)
2343 6cadb320 bellard
                    {
2344 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode calculating TCP checksum for "
2345 7cdeb319 Benjamin Poirier
                            "packet with %d bytes data\n", ip_data_len);
2346 6cadb320 bellard
2347 718da2b9 bellard
                        ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
2348 718da2b9 bellard
                        p_tcpip_hdr->zeros      = 0;
2349 718da2b9 bellard
                        p_tcpip_hdr->ip_proto   = IP_PROTO_TCP;
2350 718da2b9 bellard
                        p_tcpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
2351 6cadb320 bellard
2352 718da2b9 bellard
                        tcp_header* p_tcp_hdr = (tcp_header *) (data_to_checksum+12);
2353 6cadb320 bellard
2354 6cadb320 bellard
                        p_tcp_hdr->th_sum = 0;
2355 6cadb320 bellard
2356 718da2b9 bellard
                        int tcp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
2357 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode TCP checksum %04x\n",
2358 7cdeb319 Benjamin Poirier
                            tcp_checksum);
2359 6cadb320 bellard
2360 6cadb320 bellard
                        p_tcp_hdr->th_sum = tcp_checksum;
2361 6cadb320 bellard
                    }
2362 718da2b9 bellard
                    else if ((txdw0 & CP_TX_UDPCS) && ip_protocol == IP_PROTO_UDP)
2363 6cadb320 bellard
                    {
2364 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode calculating UDP checksum for "
2365 7cdeb319 Benjamin Poirier
                            "packet with %d bytes data\n", ip_data_len);
2366 6cadb320 bellard
2367 718da2b9 bellard
                        ip_pseudo_header *p_udpip_hdr = (ip_pseudo_header *)data_to_checksum;
2368 718da2b9 bellard
                        p_udpip_hdr->zeros      = 0;
2369 718da2b9 bellard
                        p_udpip_hdr->ip_proto   = IP_PROTO_UDP;
2370 718da2b9 bellard
                        p_udpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
2371 6cadb320 bellard
2372 718da2b9 bellard
                        udp_header *p_udp_hdr = (udp_header *) (data_to_checksum+12);
2373 6cadb320 bellard
2374 6cadb320 bellard
                        p_udp_hdr->uh_sum = 0;
2375 6cadb320 bellard
2376 718da2b9 bellard
                        int udp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
2377 7cdeb319 Benjamin Poirier
                        DPRINTF("+++ C+ mode UDP checksum %04x\n",
2378 7cdeb319 Benjamin Poirier
                            udp_checksum);
2379 6cadb320 bellard
2380 6cadb320 bellard
                        p_udp_hdr->uh_sum = udp_checksum;
2381 6cadb320 bellard
                    }
2382 6cadb320 bellard
2383 6cadb320 bellard
                    /* restore IP header */
2384 718da2b9 bellard
                    memcpy(eth_payload_data, saved_ip_header, hlen);
2385 6cadb320 bellard
                }
2386 6cadb320 bellard
            }
2387 6cadb320 bellard
        }
2388 6cadb320 bellard
2389 6cadb320 bellard
        /* update tally counter */
2390 6cadb320 bellard
        ++s->tally_counters.TxOk;
2391 6cadb320 bellard
2392 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode transmitting %d bytes packet\n", saved_size);
2393 6cadb320 bellard
2394 bf6b87a8 Benjamin Poirier
        rtl8139_transfer_frame(s, saved_buffer, saved_size, 1,
2395 bf6b87a8 Benjamin Poirier
            (uint8_t *) dot1q_buffer);
2396 6cadb320 bellard
2397 6cadb320 bellard
        /* restore card space if there was no recursion and reset offset */
2398 6cadb320 bellard
        if (!s->cplus_txbuffer)
2399 6cadb320 bellard
        {
2400 6cadb320 bellard
            s->cplus_txbuffer        = saved_buffer;
2401 6cadb320 bellard
            s->cplus_txbuffer_len    = saved_buffer_len;
2402 6cadb320 bellard
            s->cplus_txbuffer_offset = 0;
2403 6cadb320 bellard
        }
2404 6cadb320 bellard
        else
2405 6cadb320 bellard
        {
2406 7267c094 Anthony Liguori
            g_free(saved_buffer);
2407 6cadb320 bellard
        }
2408 a41b2ff2 pbrook
    }
2409 a41b2ff2 pbrook
    else
2410 a41b2ff2 pbrook
    {
2411 7cdeb319 Benjamin Poirier
        DPRINTF("+++ C+ mode transmission continue to next descriptor\n");
2412 a41b2ff2 pbrook
    }
2413 a41b2ff2 pbrook
2414 a41b2ff2 pbrook
    return 1;
2415 a41b2ff2 pbrook
}
2416 a41b2ff2 pbrook
2417 a41b2ff2 pbrook
static void rtl8139_cplus_transmit(RTL8139State *s)
2418 a41b2ff2 pbrook
{
2419 a41b2ff2 pbrook
    int txcount = 0;
2420 a41b2ff2 pbrook
2421 a41b2ff2 pbrook
    while (rtl8139_cplus_transmit_one(s))
2422 a41b2ff2 pbrook
    {
2423 a41b2ff2 pbrook
        ++txcount;
2424 a41b2ff2 pbrook
    }
2425 a41b2ff2 pbrook
2426 a41b2ff2 pbrook
    /* Mark transfer completed */
2427 a41b2ff2 pbrook
    if (!txcount)
2428 a41b2ff2 pbrook
    {
2429 7cdeb319 Benjamin Poirier
        DPRINTF("C+ mode : transmitter queue stalled, current TxDesc = %d\n",
2430 7cdeb319 Benjamin Poirier
            s->currCPlusTxDesc);
2431 a41b2ff2 pbrook
    }
2432 a41b2ff2 pbrook
    else
2433 a41b2ff2 pbrook
    {
2434 a41b2ff2 pbrook
        /* update interrupt status */
2435 a41b2ff2 pbrook
        s->IntrStatus |= TxOK;
2436 a41b2ff2 pbrook
        rtl8139_update_irq(s);
2437 a41b2ff2 pbrook
    }
2438 a41b2ff2 pbrook
}
2439 a41b2ff2 pbrook
2440 a41b2ff2 pbrook
static void rtl8139_transmit(RTL8139State *s)
2441 a41b2ff2 pbrook
{
2442 a41b2ff2 pbrook
    int descriptor = s->currTxDesc, txcount = 0;
2443 a41b2ff2 pbrook
2444 a41b2ff2 pbrook
    /*while*/
2445 a41b2ff2 pbrook
    if (rtl8139_transmit_one(s, descriptor))
2446 a41b2ff2 pbrook
    {
2447 a41b2ff2 pbrook
        ++s->currTxDesc;
2448 a41b2ff2 pbrook
        s->currTxDesc %= 4;
2449 a41b2ff2 pbrook
        ++txcount;
2450 a41b2ff2 pbrook
    }
2451 a41b2ff2 pbrook
2452 a41b2ff2 pbrook
    /* Mark transfer completed */
2453 a41b2ff2 pbrook
    if (!txcount)
2454 a41b2ff2 pbrook
    {
2455 7cdeb319 Benjamin Poirier
        DPRINTF("transmitter queue stalled, current TxDesc = %d\n",
2456 7cdeb319 Benjamin Poirier
            s->currTxDesc);
2457 a41b2ff2 pbrook
    }
2458 a41b2ff2 pbrook
}
2459 a41b2ff2 pbrook
2460 a41b2ff2 pbrook
static void rtl8139_TxStatus_write(RTL8139State *s, uint32_t txRegOffset, uint32_t val)
2461 a41b2ff2 pbrook
{
2462 a41b2ff2 pbrook
2463 a41b2ff2 pbrook
    int descriptor = txRegOffset/4;
2464 6cadb320 bellard
2465 6cadb320 bellard
    /* handle C+ transmit mode register configuration */
2466 6cadb320 bellard
2467 2c3891ab aliguori
    if (s->cplus_enabled)
2468 6cadb320 bellard
    {
2469 7cdeb319 Benjamin Poirier
        DPRINTF("RTL8139C+ DTCCR write offset=0x%x val=0x%08x "
2470 7cdeb319 Benjamin Poirier
            "descriptor=%d\n", txRegOffset, val, descriptor);
2471 6cadb320 bellard
2472 6cadb320 bellard
        /* handle Dump Tally Counters command */
2473 6cadb320 bellard
        s->TxStatus[descriptor] = val;
2474 6cadb320 bellard
2475 6cadb320 bellard
        if (descriptor == 0 && (val & 0x8))
2476 6cadb320 bellard
        {
2477 c227f099 Anthony Liguori
            target_phys_addr_t tc_addr = rtl8139_addr64(s->TxStatus[0] & ~0x3f, s->TxStatus[1]);
2478 6cadb320 bellard
2479 6cadb320 bellard
            /* dump tally counters to specified memory location */
2480 3ada003a Eduard - Gabriel Munteanu
            RTL8139TallyCounters_dma_write(s, tc_addr);
2481 6cadb320 bellard
2482 6cadb320 bellard
            /* mark dump completed */
2483 6cadb320 bellard
            s->TxStatus[0] &= ~0x8;
2484 6cadb320 bellard
        }
2485 6cadb320 bellard
2486 6cadb320 bellard
        return;
2487 6cadb320 bellard
    }
2488 6cadb320 bellard
2489 7cdeb319 Benjamin Poirier
    DPRINTF("TxStatus write offset=0x%x val=0x%08x descriptor=%d\n",
2490 7cdeb319 Benjamin Poirier
        txRegOffset, val, descriptor);
2491 a41b2ff2 pbrook
2492 a41b2ff2 pbrook
    /* mask only reserved bits */
2493 a41b2ff2 pbrook
    val &= ~0xff00c000; /* these bits are reset on write */
2494 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x00c00000, s->TxStatus[descriptor]);
2495 a41b2ff2 pbrook
2496 a41b2ff2 pbrook
    s->TxStatus[descriptor] = val;
2497 a41b2ff2 pbrook
2498 a41b2ff2 pbrook
    /* attempt to start transmission */
2499 a41b2ff2 pbrook
    rtl8139_transmit(s);
2500 a41b2ff2 pbrook
}
2501 a41b2ff2 pbrook
2502 a41b2ff2 pbrook
static uint32_t rtl8139_TxStatus_read(RTL8139State *s, uint32_t txRegOffset)
2503 a41b2ff2 pbrook
{
2504 a41b2ff2 pbrook
    uint32_t ret = s->TxStatus[txRegOffset/4];
2505 a41b2ff2 pbrook
2506 7cdeb319 Benjamin Poirier
    DPRINTF("TxStatus read offset=0x%x val=0x%08x\n", txRegOffset, ret);
2507 a41b2ff2 pbrook
2508 a41b2ff2 pbrook
    return ret;
2509 a41b2ff2 pbrook
}
2510 a41b2ff2 pbrook
2511 a41b2ff2 pbrook
static uint16_t rtl8139_TSAD_read(RTL8139State *s)
2512 a41b2ff2 pbrook
{
2513 a41b2ff2 pbrook
    uint16_t ret = 0;
2514 a41b2ff2 pbrook
2515 a41b2ff2 pbrook
    /* Simulate TSAD, it is read only anyway */
2516 a41b2ff2 pbrook
2517 a41b2ff2 pbrook
    ret = ((s->TxStatus[3] & TxStatOK  )?TSAD_TOK3:0)
2518 a41b2ff2 pbrook
         |((s->TxStatus[2] & TxStatOK  )?TSAD_TOK2:0)
2519 a41b2ff2 pbrook
         |((s->TxStatus[1] & TxStatOK  )?TSAD_TOK1:0)
2520 a41b2ff2 pbrook
         |((s->TxStatus[0] & TxStatOK  )?TSAD_TOK0:0)
2521 a41b2ff2 pbrook
2522 a41b2ff2 pbrook
         |((s->TxStatus[3] & TxUnderrun)?TSAD_TUN3:0)
2523 a41b2ff2 pbrook
         |((s->TxStatus[2] & TxUnderrun)?TSAD_TUN2:0)
2524 a41b2ff2 pbrook
         |((s->TxStatus[1] & TxUnderrun)?TSAD_TUN1:0)
2525 a41b2ff2 pbrook
         |((s->TxStatus[0] & TxUnderrun)?TSAD_TUN0:0)
2526 3b46e624 ths
2527 a41b2ff2 pbrook
         |((s->TxStatus[3] & TxAborted )?TSAD_TABT3:0)
2528 a41b2ff2 pbrook
         |((s->TxStatus[2] & TxAborted )?TSAD_TABT2:0)
2529 a41b2ff2 pbrook
         |((s->TxStatus[1] & TxAborted )?TSAD_TABT1:0)
2530 a41b2ff2 pbrook
         |((s->TxStatus[0] & TxAborted )?TSAD_TABT0:0)
2531 3b46e624 ths
2532 a41b2ff2 pbrook
         |((s->TxStatus[3] & TxHostOwns )?TSAD_OWN3:0)
2533 a41b2ff2 pbrook
         |((s->TxStatus[2] & TxHostOwns )?TSAD_OWN2:0)
2534 a41b2ff2 pbrook
         |((s->TxStatus[1] & TxHostOwns )?TSAD_OWN1:0)
2535 a41b2ff2 pbrook
         |((s->TxStatus[0] & TxHostOwns )?TSAD_OWN0:0) ;
2536 3b46e624 ths
2537 a41b2ff2 pbrook
2538 7cdeb319 Benjamin Poirier
    DPRINTF("TSAD read val=0x%04x\n", ret);
2539 a41b2ff2 pbrook
2540 a41b2ff2 pbrook
    return ret;
2541 a41b2ff2 pbrook
}
2542 a41b2ff2 pbrook
2543 a41b2ff2 pbrook
static uint16_t rtl8139_CSCR_read(RTL8139State *s)
2544 a41b2ff2 pbrook
{
2545 a41b2ff2 pbrook
    uint16_t ret = s->CSCR;
2546 a41b2ff2 pbrook
2547 7cdeb319 Benjamin Poirier
    DPRINTF("CSCR read val=0x%04x\n", ret);
2548 a41b2ff2 pbrook
2549 a41b2ff2 pbrook
    return ret;
2550 a41b2ff2 pbrook
}
2551 a41b2ff2 pbrook
2552 a41b2ff2 pbrook
static void rtl8139_TxAddr_write(RTL8139State *s, uint32_t txAddrOffset, uint32_t val)
2553 a41b2ff2 pbrook
{
2554 7cdeb319 Benjamin Poirier
    DPRINTF("TxAddr write offset=0x%x val=0x%08x\n", txAddrOffset, val);
2555 a41b2ff2 pbrook
2556 290a0933 ths
    s->TxAddr[txAddrOffset/4] = val;
2557 a41b2ff2 pbrook
}
2558 a41b2ff2 pbrook
2559 a41b2ff2 pbrook
static uint32_t rtl8139_TxAddr_read(RTL8139State *s, uint32_t txAddrOffset)
2560 a41b2ff2 pbrook
{
2561 290a0933 ths
    uint32_t ret = s->TxAddr[txAddrOffset/4];
2562 a41b2ff2 pbrook
2563 7cdeb319 Benjamin Poirier
    DPRINTF("TxAddr read offset=0x%x val=0x%08x\n", txAddrOffset, ret);
2564 a41b2ff2 pbrook
2565 a41b2ff2 pbrook
    return ret;
2566 a41b2ff2 pbrook
}
2567 a41b2ff2 pbrook
2568 a41b2ff2 pbrook
static void rtl8139_RxBufPtr_write(RTL8139State *s, uint32_t val)
2569 a41b2ff2 pbrook
{
2570 7cdeb319 Benjamin Poirier
    DPRINTF("RxBufPtr write val=0x%04x\n", val);
2571 a41b2ff2 pbrook
2572 a41b2ff2 pbrook
    /* this value is off by 16 */
2573 a41b2ff2 pbrook
    s->RxBufPtr = MOD2(val + 0x10, s->RxBufferSize);
2574 a41b2ff2 pbrook
2575 7cdeb319 Benjamin Poirier
    DPRINTF(" CAPR write: rx buffer length %d head 0x%04x read 0x%04x\n",
2576 7cdeb319 Benjamin Poirier
        s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
2577 a41b2ff2 pbrook
}
2578 a41b2ff2 pbrook
2579 a41b2ff2 pbrook
static uint32_t rtl8139_RxBufPtr_read(RTL8139State *s)
2580 a41b2ff2 pbrook
{
2581 a41b2ff2 pbrook
    /* this value is off by 16 */
2582 a41b2ff2 pbrook
    uint32_t ret = s->RxBufPtr - 0x10;
2583 a41b2ff2 pbrook
2584 7cdeb319 Benjamin Poirier
    DPRINTF("RxBufPtr read val=0x%04x\n", ret);
2585 6cadb320 bellard
2586 6cadb320 bellard
    return ret;
2587 6cadb320 bellard
}
2588 6cadb320 bellard
2589 6cadb320 bellard
static uint32_t rtl8139_RxBufAddr_read(RTL8139State *s)
2590 6cadb320 bellard
{
2591 6cadb320 bellard
    /* this value is NOT off by 16 */
2592 6cadb320 bellard
    uint32_t ret = s->RxBufAddr;
2593 6cadb320 bellard
2594 7cdeb319 Benjamin Poirier
    DPRINTF("RxBufAddr read val=0x%04x\n", ret);
2595 a41b2ff2 pbrook
2596 a41b2ff2 pbrook
    return ret;
2597 a41b2ff2 pbrook
}
2598 a41b2ff2 pbrook
2599 a41b2ff2 pbrook
static void rtl8139_RxBuf_write(RTL8139State *s, uint32_t val)
2600 a41b2ff2 pbrook
{
2601 7cdeb319 Benjamin Poirier
    DPRINTF("RxBuf write val=0x%08x\n", val);
2602 a41b2ff2 pbrook
2603 a41b2ff2 pbrook
    s->RxBuf = val;
2604 a41b2ff2 pbrook
2605 a41b2ff2 pbrook
    /* may need to reset rxring here */
2606 a41b2ff2 pbrook
}
2607 a41b2ff2 pbrook
2608 a41b2ff2 pbrook
static uint32_t rtl8139_RxBuf_read(RTL8139State *s)
2609 a41b2ff2 pbrook
{
2610 a41b2ff2 pbrook
    uint32_t ret = s->RxBuf;
2611 a41b2ff2 pbrook
2612 7cdeb319 Benjamin Poirier
    DPRINTF("RxBuf read val=0x%08x\n", ret);
2613 a41b2ff2 pbrook
2614 a41b2ff2 pbrook
    return ret;
2615 a41b2ff2 pbrook
}
2616 a41b2ff2 pbrook
2617 a41b2ff2 pbrook
static void rtl8139_IntrMask_write(RTL8139State *s, uint32_t val)
2618 a41b2ff2 pbrook
{
2619 7cdeb319 Benjamin Poirier
    DPRINTF("IntrMask write(w) val=0x%04x\n", val);
2620 a41b2ff2 pbrook
2621 ebabb67a Stefan Weil
    /* mask unwritable bits */
2622 a41b2ff2 pbrook
    val = SET_MASKED(val, 0x1e00, s->IntrMask);
2623 a41b2ff2 pbrook
2624 a41b2ff2 pbrook
    s->IntrMask = val;
2625 a41b2ff2 pbrook
2626 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
2627 a41b2ff2 pbrook
    rtl8139_update_irq(s);
2628 05447803 Frediano Ziglio
2629 a41b2ff2 pbrook
}
2630 a41b2ff2 pbrook
2631 a41b2ff2 pbrook
static uint32_t rtl8139_IntrMask_read(RTL8139State *s)
2632 a41b2ff2 pbrook
{
2633 a41b2ff2 pbrook
    uint32_t ret = s->IntrMask;
2634 a41b2ff2 pbrook
2635 7cdeb319 Benjamin Poirier
    DPRINTF("IntrMask read(w) val=0x%04x\n", ret);
2636 a41b2ff2 pbrook
2637 a41b2ff2 pbrook
    return ret;
2638 a41b2ff2 pbrook
}
2639 a41b2ff2 pbrook
2640 a41b2ff2 pbrook
static void rtl8139_IntrStatus_write(RTL8139State *s, uint32_t val)
2641 a41b2ff2 pbrook
{
2642 7cdeb319 Benjamin Poirier
    DPRINTF("IntrStatus write(w) val=0x%04x\n", val);
2643 a41b2ff2 pbrook
2644 a41b2ff2 pbrook
#if 0
2645 a41b2ff2 pbrook

2646 a41b2ff2 pbrook
    /* writing to ISR has no effect */
2647 a41b2ff2 pbrook

2648 a41b2ff2 pbrook
    return;
2649 a41b2ff2 pbrook

2650 a41b2ff2 pbrook
#else
2651 a41b2ff2 pbrook
    uint16_t newStatus = s->IntrStatus & ~val;
2652 a41b2ff2 pbrook
2653 ebabb67a Stefan Weil
    /* mask unwritable bits */
2654 a41b2ff2 pbrook
    newStatus = SET_MASKED(newStatus, 0x1e00, s->IntrStatus);
2655 a41b2ff2 pbrook
2656 a41b2ff2 pbrook
    /* writing 1 to interrupt status register bit clears it */
2657 a41b2ff2 pbrook
    s->IntrStatus = 0;
2658 a41b2ff2 pbrook
    rtl8139_update_irq(s);
2659 a41b2ff2 pbrook
2660 a41b2ff2 pbrook
    s->IntrStatus = newStatus;
2661 05447803 Frediano Ziglio
    /*
2662 05447803 Frediano Ziglio
     * Computing if we miss an interrupt here is not that correct but
2663 05447803 Frediano Ziglio
     * considered that we should have had already an interrupt
2664 05447803 Frediano Ziglio
     * and probably emulated is slower is better to assume this resetting was
2665 26404edc Stefan Weil
     * done before testing on previous rtl8139_update_irq lead to IRQ losing
2666 05447803 Frediano Ziglio
     */
2667 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
2668 a41b2ff2 pbrook
    rtl8139_update_irq(s);
2669 05447803 Frediano Ziglio
2670 a41b2ff2 pbrook
#endif
2671 a41b2ff2 pbrook
}
2672 a41b2ff2 pbrook
2673 a41b2ff2 pbrook
static uint32_t rtl8139_IntrStatus_read(RTL8139State *s)
2674 a41b2ff2 pbrook
{
2675 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
2676 05447803 Frediano Ziglio
2677 a41b2ff2 pbrook
    uint32_t ret = s->IntrStatus;
2678 a41b2ff2 pbrook
2679 7cdeb319 Benjamin Poirier
    DPRINTF("IntrStatus read(w) val=0x%04x\n", ret);
2680 a41b2ff2 pbrook
2681 a41b2ff2 pbrook
#if 0
2682 a41b2ff2 pbrook

2683 a41b2ff2 pbrook
    /* reading ISR clears all interrupts */
2684 a41b2ff2 pbrook
    s->IntrStatus = 0;
2685 a41b2ff2 pbrook

2686 a41b2ff2 pbrook
    rtl8139_update_irq(s);
2687 a41b2ff2 pbrook

2688 a41b2ff2 pbrook
#endif
2689 a41b2ff2 pbrook
2690 a41b2ff2 pbrook
    return ret;
2691 a41b2ff2 pbrook
}
2692 a41b2ff2 pbrook
2693 a41b2ff2 pbrook
static void rtl8139_MultiIntr_write(RTL8139State *s, uint32_t val)
2694 a41b2ff2 pbrook
{
2695 7cdeb319 Benjamin Poirier
    DPRINTF("MultiIntr write(w) val=0x%04x\n", val);
2696 a41b2ff2 pbrook
2697 ebabb67a Stefan Weil
    /* mask unwritable bits */
2698 a41b2ff2 pbrook
    val = SET_MASKED(val, 0xf000, s->MultiIntr);
2699 a41b2ff2 pbrook
2700 a41b2ff2 pbrook
    s->MultiIntr = val;
2701 a41b2ff2 pbrook
}
2702 a41b2ff2 pbrook
2703 a41b2ff2 pbrook
static uint32_t rtl8139_MultiIntr_read(RTL8139State *s)
2704 a41b2ff2 pbrook
{
2705 a41b2ff2 pbrook
    uint32_t ret = s->MultiIntr;
2706 a41b2ff2 pbrook
2707 7cdeb319 Benjamin Poirier
    DPRINTF("MultiIntr read(w) val=0x%04x\n", ret);
2708 a41b2ff2 pbrook
2709 a41b2ff2 pbrook
    return ret;
2710 a41b2ff2 pbrook
}
2711 a41b2ff2 pbrook
2712 a41b2ff2 pbrook
static void rtl8139_io_writeb(void *opaque, uint8_t addr, uint32_t val)
2713 a41b2ff2 pbrook
{
2714 a41b2ff2 pbrook
    RTL8139State *s = opaque;
2715 a41b2ff2 pbrook
2716 a41b2ff2 pbrook
    switch (addr)
2717 a41b2ff2 pbrook
    {
2718 a41b2ff2 pbrook
        case MAC0 ... MAC0+5:
2719 a41b2ff2 pbrook
            s->phys[addr - MAC0] = val;
2720 a41b2ff2 pbrook
            break;
2721 a41b2ff2 pbrook
        case MAC0+6 ... MAC0+7:
2722 a41b2ff2 pbrook
            /* reserved */
2723 a41b2ff2 pbrook
            break;
2724 a41b2ff2 pbrook
        case MAR0 ... MAR0+7:
2725 a41b2ff2 pbrook
            s->mult[addr - MAR0] = val;
2726 a41b2ff2 pbrook
            break;
2727 a41b2ff2 pbrook
        case ChipCmd:
2728 a41b2ff2 pbrook
            rtl8139_ChipCmd_write(s, val);
2729 a41b2ff2 pbrook
            break;
2730 a41b2ff2 pbrook
        case Cfg9346:
2731 a41b2ff2 pbrook
            rtl8139_Cfg9346_write(s, val);
2732 a41b2ff2 pbrook
            break;
2733 a41b2ff2 pbrook
        case TxConfig: /* windows driver sometimes writes using byte-lenth call */
2734 a41b2ff2 pbrook
            rtl8139_TxConfig_writeb(s, val);
2735 a41b2ff2 pbrook
            break;
2736 a41b2ff2 pbrook
        case Config0:
2737 a41b2ff2 pbrook
            rtl8139_Config0_write(s, val);
2738 a41b2ff2 pbrook
            break;
2739 a41b2ff2 pbrook
        case Config1:
2740 a41b2ff2 pbrook
            rtl8139_Config1_write(s, val);
2741 a41b2ff2 pbrook
            break;
2742 a41b2ff2 pbrook
        case Config3:
2743 a41b2ff2 pbrook
            rtl8139_Config3_write(s, val);
2744 a41b2ff2 pbrook
            break;
2745 a41b2ff2 pbrook
        case Config4:
2746 a41b2ff2 pbrook
            rtl8139_Config4_write(s, val);
2747 a41b2ff2 pbrook
            break;
2748 a41b2ff2 pbrook
        case Config5:
2749 a41b2ff2 pbrook
            rtl8139_Config5_write(s, val);
2750 a41b2ff2 pbrook
            break;
2751 a41b2ff2 pbrook
        case MediaStatus:
2752 a41b2ff2 pbrook
            /* ignore */
2753 7cdeb319 Benjamin Poirier
            DPRINTF("not implemented write(b) to MediaStatus val=0x%02x\n",
2754 7cdeb319 Benjamin Poirier
                val);
2755 a41b2ff2 pbrook
            break;
2756 a41b2ff2 pbrook
2757 a41b2ff2 pbrook
        case HltClk:
2758 7cdeb319 Benjamin Poirier
            DPRINTF("HltClk write val=0x%08x\n", val);
2759 a41b2ff2 pbrook
            if (val == 'R')
2760 a41b2ff2 pbrook
            {
2761 a41b2ff2 pbrook
                s->clock_enabled = 1;
2762 a41b2ff2 pbrook
            }
2763 a41b2ff2 pbrook
            else if (val == 'H')
2764 a41b2ff2 pbrook
            {
2765 a41b2ff2 pbrook
                s->clock_enabled = 0;
2766 a41b2ff2 pbrook
            }
2767 a41b2ff2 pbrook
            break;
2768 a41b2ff2 pbrook
2769 a41b2ff2 pbrook
        case TxThresh:
2770 7cdeb319 Benjamin Poirier
            DPRINTF("C+ TxThresh write(b) val=0x%02x\n", val);
2771 a41b2ff2 pbrook
            s->TxThresh = val;
2772 a41b2ff2 pbrook
            break;
2773 a41b2ff2 pbrook
2774 a41b2ff2 pbrook
        case TxPoll:
2775 7cdeb319 Benjamin Poirier
            DPRINTF("C+ TxPoll write(b) val=0x%02x\n", val);
2776 a41b2ff2 pbrook
            if (val & (1 << 7))
2777 a41b2ff2 pbrook
            {
2778 7cdeb319 Benjamin Poirier
                DPRINTF("C+ TxPoll high priority transmission (not "
2779 7cdeb319 Benjamin Poirier
                    "implemented)\n");
2780 a41b2ff2 pbrook
                //rtl8139_cplus_transmit(s);
2781 a41b2ff2 pbrook
            }
2782 a41b2ff2 pbrook
            if (val & (1 << 6))
2783 a41b2ff2 pbrook
            {
2784 7cdeb319 Benjamin Poirier
                DPRINTF("C+ TxPoll normal priority transmission\n");
2785 a41b2ff2 pbrook
                rtl8139_cplus_transmit(s);
2786 a41b2ff2 pbrook
            }
2787 a41b2ff2 pbrook
2788 a41b2ff2 pbrook
            break;
2789 a41b2ff2 pbrook
2790 a41b2ff2 pbrook
        default:
2791 7cdeb319 Benjamin Poirier
            DPRINTF("not implemented write(b) addr=0x%x val=0x%02x\n", addr,
2792 7cdeb319 Benjamin Poirier
                val);
2793 a41b2ff2 pbrook
            break;
2794 a41b2ff2 pbrook
    }
2795 a41b2ff2 pbrook
}
2796 a41b2ff2 pbrook
2797 a41b2ff2 pbrook
static void rtl8139_io_writew(void *opaque, uint8_t addr, uint32_t val)
2798 a41b2ff2 pbrook
{
2799 a41b2ff2 pbrook
    RTL8139State *s = opaque;
2800 a41b2ff2 pbrook
2801 a41b2ff2 pbrook
    switch (addr)
2802 a41b2ff2 pbrook
    {
2803 a41b2ff2 pbrook
        case IntrMask:
2804 a41b2ff2 pbrook
            rtl8139_IntrMask_write(s, val);
2805 a41b2ff2 pbrook
            break;
2806 a41b2ff2 pbrook
2807 a41b2ff2 pbrook
        case IntrStatus:
2808 a41b2ff2 pbrook
            rtl8139_IntrStatus_write(s, val);
2809 a41b2ff2 pbrook
            break;
2810 a41b2ff2 pbrook
2811 a41b2ff2 pbrook
        case MultiIntr:
2812 a41b2ff2 pbrook
            rtl8139_MultiIntr_write(s, val);
2813 a41b2ff2 pbrook
            break;
2814 a41b2ff2 pbrook
2815 a41b2ff2 pbrook
        case RxBufPtr:
2816 a41b2ff2 pbrook
            rtl8139_RxBufPtr_write(s, val);
2817 a41b2ff2 pbrook
            break;
2818 a41b2ff2 pbrook
2819 a41b2ff2 pbrook
        case BasicModeCtrl:
2820 a41b2ff2 pbrook
            rtl8139_BasicModeCtrl_write(s, val);
2821 a41b2ff2 pbrook
            break;
2822 a41b2ff2 pbrook
        case BasicModeStatus:
2823 a41b2ff2 pbrook
            rtl8139_BasicModeStatus_write(s, val);
2824 a41b2ff2 pbrook
            break;
2825 a41b2ff2 pbrook
        case NWayAdvert:
2826 7cdeb319 Benjamin Poirier
            DPRINTF("NWayAdvert write(w) val=0x%04x\n", val);
2827 a41b2ff2 pbrook
            s->NWayAdvert = val;
2828 a41b2ff2 pbrook
            break;
2829 a41b2ff2 pbrook
        case NWayLPAR:
2830 7cdeb319 Benjamin Poirier
            DPRINTF("forbidden NWayLPAR write(w) val=0x%04x\n", val);
2831 a41b2ff2 pbrook
            break;
2832 a41b2ff2 pbrook
        case NWayExpansion:
2833 7cdeb319 Benjamin Poirier
            DPRINTF("NWayExpansion write(w) val=0x%04x\n", val);
2834 a41b2ff2 pbrook
            s->NWayExpansion = val;
2835 a41b2ff2 pbrook
            break;
2836 a41b2ff2 pbrook
2837 a41b2ff2 pbrook
        case CpCmd:
2838 a41b2ff2 pbrook
            rtl8139_CpCmd_write(s, val);
2839 a41b2ff2 pbrook
            break;
2840 a41b2ff2 pbrook
2841 6cadb320 bellard
        case IntrMitigate:
2842 6cadb320 bellard
            rtl8139_IntrMitigate_write(s, val);
2843 6cadb320 bellard
            break;
2844 6cadb320 bellard
2845 a41b2ff2 pbrook
        default:
2846 7cdeb319 Benjamin Poirier
            DPRINTF("ioport write(w) addr=0x%x val=0x%04x via write(b)\n",
2847 7cdeb319 Benjamin Poirier
                addr, val);
2848 a41b2ff2 pbrook
2849 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr, val & 0xff);
2850 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2851 a41b2ff2 pbrook
            break;
2852 a41b2ff2 pbrook
    }
2853 a41b2ff2 pbrook
}
2854 a41b2ff2 pbrook
2855 05447803 Frediano Ziglio
static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time)
2856 05447803 Frediano Ziglio
{
2857 05447803 Frediano Ziglio
    int64_t pci_time, next_time;
2858 05447803 Frediano Ziglio
    uint32_t low_pci;
2859 05447803 Frediano Ziglio
2860 7cdeb319 Benjamin Poirier
    DPRINTF("entered rtl8139_set_next_tctr_time\n");
2861 05447803 Frediano Ziglio
2862 05447803 Frediano Ziglio
    if (s->TimerExpire && current_time >= s->TimerExpire) {
2863 05447803 Frediano Ziglio
        s->IntrStatus |= PCSTimeout;
2864 05447803 Frediano Ziglio
        rtl8139_update_irq(s);
2865 05447803 Frediano Ziglio
    }
2866 05447803 Frediano Ziglio
2867 05447803 Frediano Ziglio
    /* Set QEMU timer only if needed that is
2868 05447803 Frediano Ziglio
     * - TimerInt <> 0 (we have a timer)
2869 05447803 Frediano Ziglio
     * - mask = 1 (we want an interrupt timer)
2870 05447803 Frediano Ziglio
     * - irq = 0  (irq is not already active)
2871 05447803 Frediano Ziglio
     * If any of above change we need to compute timer again
2872 05447803 Frediano Ziglio
     * Also we must check if timer is passed without QEMU timer
2873 05447803 Frediano Ziglio
     */
2874 05447803 Frediano Ziglio
    s->TimerExpire = 0;
2875 05447803 Frediano Ziglio
    if (!s->TimerInt) {
2876 05447803 Frediano Ziglio
        return;
2877 05447803 Frediano Ziglio
    }
2878 05447803 Frediano Ziglio
2879 05447803 Frediano Ziglio
    pci_time = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY,
2880 05447803 Frediano Ziglio
                                get_ticks_per_sec());
2881 05447803 Frediano Ziglio
    low_pci = pci_time & 0xffffffff;
2882 05447803 Frediano Ziglio
    pci_time = pci_time - low_pci + s->TimerInt;
2883 05447803 Frediano Ziglio
    if (low_pci >= s->TimerInt) {
2884 05447803 Frediano Ziglio
        pci_time += 0x100000000LL;
2885 05447803 Frediano Ziglio
    }
2886 05447803 Frediano Ziglio
    next_time = s->TCTR_base + muldiv64(pci_time, get_ticks_per_sec(),
2887 05447803 Frediano Ziglio
                                                PCI_FREQUENCY);
2888 05447803 Frediano Ziglio
    s->TimerExpire = next_time;
2889 05447803 Frediano Ziglio
2890 05447803 Frediano Ziglio
    if ((s->IntrMask & PCSTimeout) != 0 && (s->IntrStatus & PCSTimeout) == 0) {
2891 05447803 Frediano Ziglio
        qemu_mod_timer(s->timer, next_time);
2892 05447803 Frediano Ziglio
    }
2893 05447803 Frediano Ziglio
}
2894 05447803 Frediano Ziglio
2895 a41b2ff2 pbrook
static void rtl8139_io_writel(void *opaque, uint8_t addr, uint32_t val)
2896 a41b2ff2 pbrook
{
2897 a41b2ff2 pbrook
    RTL8139State *s = opaque;
2898 a41b2ff2 pbrook
2899 a41b2ff2 pbrook
    switch (addr)
2900 a41b2ff2 pbrook
    {
2901 a41b2ff2 pbrook
        case RxMissed:
2902 7cdeb319 Benjamin Poirier
            DPRINTF("RxMissed clearing on write\n");
2903 a41b2ff2 pbrook
            s->RxMissed = 0;
2904 a41b2ff2 pbrook
            break;
2905 a41b2ff2 pbrook
2906 a41b2ff2 pbrook
        case TxConfig:
2907 a41b2ff2 pbrook
            rtl8139_TxConfig_write(s, val);
2908 a41b2ff2 pbrook
            break;
2909 a41b2ff2 pbrook
2910 a41b2ff2 pbrook
        case RxConfig:
2911 a41b2ff2 pbrook
            rtl8139_RxConfig_write(s, val);
2912 a41b2ff2 pbrook
            break;
2913 a41b2ff2 pbrook
2914 a41b2ff2 pbrook
        case TxStatus0 ... TxStatus0+4*4-1:
2915 a41b2ff2 pbrook
            rtl8139_TxStatus_write(s, addr-TxStatus0, val);
2916 a41b2ff2 pbrook
            break;
2917 a41b2ff2 pbrook
2918 a41b2ff2 pbrook
        case TxAddr0 ... TxAddr0+4*4-1:
2919 a41b2ff2 pbrook
            rtl8139_TxAddr_write(s, addr-TxAddr0, val);
2920 a41b2ff2 pbrook
            break;
2921 a41b2ff2 pbrook
2922 a41b2ff2 pbrook
        case RxBuf:
2923 a41b2ff2 pbrook
            rtl8139_RxBuf_write(s, val);
2924 a41b2ff2 pbrook
            break;
2925 a41b2ff2 pbrook
2926 a41b2ff2 pbrook
        case RxRingAddrLO:
2927 7cdeb319 Benjamin Poirier
            DPRINTF("C+ RxRing low bits write val=0x%08x\n", val);
2928 a41b2ff2 pbrook
            s->RxRingAddrLO = val;
2929 a41b2ff2 pbrook
            break;
2930 a41b2ff2 pbrook
2931 a41b2ff2 pbrook
        case RxRingAddrHI:
2932 7cdeb319 Benjamin Poirier
            DPRINTF("C+ RxRing high bits write val=0x%08x\n", val);
2933 a41b2ff2 pbrook
            s->RxRingAddrHI = val;
2934 a41b2ff2 pbrook
            break;
2935 a41b2ff2 pbrook
2936 6cadb320 bellard
        case Timer:
2937 7cdeb319 Benjamin Poirier
            DPRINTF("TCTR Timer reset on write\n");
2938 74475455 Paolo Bonzini
            s->TCTR_base = qemu_get_clock_ns(vm_clock);
2939 05447803 Frediano Ziglio
            rtl8139_set_next_tctr_time(s, s->TCTR_base);
2940 6cadb320 bellard
            break;
2941 6cadb320 bellard
2942 6cadb320 bellard
        case FlashReg:
2943 7cdeb319 Benjamin Poirier
            DPRINTF("FlashReg TimerInt write val=0x%08x\n", val);
2944 05447803 Frediano Ziglio
            if (s->TimerInt != val) {
2945 05447803 Frediano Ziglio
                s->TimerInt = val;
2946 74475455 Paolo Bonzini
                rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
2947 05447803 Frediano Ziglio
            }
2948 6cadb320 bellard
            break;
2949 6cadb320 bellard
2950 a41b2ff2 pbrook
        default:
2951 7cdeb319 Benjamin Poirier
            DPRINTF("ioport write(l) addr=0x%x val=0x%08x via write(b)\n",
2952 7cdeb319 Benjamin Poirier
                addr, val);
2953 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr, val & 0xff);
2954 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2955 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2956 a41b2ff2 pbrook
            rtl8139_io_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2957 a41b2ff2 pbrook
            break;
2958 a41b2ff2 pbrook
    }
2959 a41b2ff2 pbrook
}
2960 a41b2ff2 pbrook
2961 a41b2ff2 pbrook
static uint32_t rtl8139_io_readb(void *opaque, uint8_t addr)
2962 a41b2ff2 pbrook
{
2963 a41b2ff2 pbrook
    RTL8139State *s = opaque;
2964 a41b2ff2 pbrook
    int ret;
2965 a41b2ff2 pbrook
2966 a41b2ff2 pbrook
    switch (addr)
2967 a41b2ff2 pbrook
    {
2968 a41b2ff2 pbrook
        case MAC0 ... MAC0+5:
2969 a41b2ff2 pbrook
            ret = s->phys[addr - MAC0];
2970 a41b2ff2 pbrook
            break;
2971 a41b2ff2 pbrook
        case MAC0+6 ... MAC0+7:
2972 a41b2ff2 pbrook
            ret = 0;
2973 a41b2ff2 pbrook
            break;
2974 a41b2ff2 pbrook
        case MAR0 ... MAR0+7:
2975 a41b2ff2 pbrook
            ret = s->mult[addr - MAR0];
2976 a41b2ff2 pbrook
            break;
2977 a41b2ff2 pbrook
        case ChipCmd:
2978 a41b2ff2 pbrook
            ret = rtl8139_ChipCmd_read(s);
2979 a41b2ff2 pbrook
            break;
2980 a41b2ff2 pbrook
        case Cfg9346:
2981 a41b2ff2 pbrook
            ret = rtl8139_Cfg9346_read(s);
2982 a41b2ff2 pbrook
            break;
2983 a41b2ff2 pbrook
        case Config0:
2984 a41b2ff2 pbrook
            ret = rtl8139_Config0_read(s);
2985 a41b2ff2 pbrook
            break;
2986 a41b2ff2 pbrook
        case Config1:
2987 a41b2ff2 pbrook
            ret = rtl8139_Config1_read(s);
2988 a41b2ff2 pbrook
            break;
2989 a41b2ff2 pbrook
        case Config3:
2990 a41b2ff2 pbrook
            ret = rtl8139_Config3_read(s);
2991 a41b2ff2 pbrook
            break;
2992 a41b2ff2 pbrook
        case Config4:
2993 a41b2ff2 pbrook
            ret = rtl8139_Config4_read(s);
2994 a41b2ff2 pbrook
            break;
2995 a41b2ff2 pbrook
        case Config5:
2996 a41b2ff2 pbrook
            ret = rtl8139_Config5_read(s);
2997 a41b2ff2 pbrook
            break;
2998 a41b2ff2 pbrook
2999 a41b2ff2 pbrook
        case MediaStatus:
3000 a41b2ff2 pbrook
            ret = 0xd0;
3001 7cdeb319 Benjamin Poirier
            DPRINTF("MediaStatus read 0x%x\n", ret);
3002 a41b2ff2 pbrook
            break;
3003 a41b2ff2 pbrook
3004 a41b2ff2 pbrook
        case HltClk:
3005 a41b2ff2 pbrook
            ret = s->clock_enabled;
3006 7cdeb319 Benjamin Poirier
            DPRINTF("HltClk read 0x%x\n", ret);
3007 a41b2ff2 pbrook
            break;
3008 a41b2ff2 pbrook
3009 a41b2ff2 pbrook
        case PCIRevisionID:
3010 6cadb320 bellard
            ret = RTL8139_PCI_REVID;
3011 7cdeb319 Benjamin Poirier
            DPRINTF("PCI Revision ID read 0x%x\n", ret);
3012 a41b2ff2 pbrook
            break;
3013 a41b2ff2 pbrook
3014 a41b2ff2 pbrook
        case TxThresh:
3015 a41b2ff2 pbrook
            ret = s->TxThresh;
3016 7cdeb319 Benjamin Poirier
            DPRINTF("C+ TxThresh read(b) val=0x%02x\n", ret);
3017 a41b2ff2 pbrook
            break;
3018 a41b2ff2 pbrook
3019 a41b2ff2 pbrook
        case 0x43: /* Part of TxConfig register. Windows driver tries to read it */
3020 a41b2ff2 pbrook
            ret = s->TxConfig >> 24;
3021 7cdeb319 Benjamin Poirier
            DPRINTF("RTL8139C TxConfig at 0x43 read(b) val=0x%02x\n", ret);
3022 a41b2ff2 pbrook
            break;
3023 a41b2ff2 pbrook
3024 a41b2ff2 pbrook
        default:
3025 7cdeb319 Benjamin Poirier
            DPRINTF("not implemented read(b) addr=0x%x\n", addr);
3026 a41b2ff2 pbrook
            ret = 0;
3027 a41b2ff2 pbrook
            break;
3028 a41b2ff2 pbrook
    }
3029 a41b2ff2 pbrook
3030 a41b2ff2 pbrook
    return ret;
3031 a41b2ff2 pbrook
}
3032 a41b2ff2 pbrook
3033 a41b2ff2 pbrook
static uint32_t rtl8139_io_readw(void *opaque, uint8_t addr)
3034 a41b2ff2 pbrook
{
3035 a41b2ff2 pbrook
    RTL8139State *s = opaque;
3036 a41b2ff2 pbrook
    uint32_t ret;
3037 a41b2ff2 pbrook
3038 a41b2ff2 pbrook
    switch (addr)
3039 a41b2ff2 pbrook
    {
3040 a41b2ff2 pbrook
        case IntrMask:
3041 a41b2ff2 pbrook
            ret = rtl8139_IntrMask_read(s);
3042 a41b2ff2 pbrook
            break;
3043 a41b2ff2 pbrook
3044 a41b2ff2 pbrook
        case IntrStatus:
3045 a41b2ff2 pbrook
            ret = rtl8139_IntrStatus_read(s);
3046 a41b2ff2 pbrook
            break;
3047 a41b2ff2 pbrook
3048 a41b2ff2 pbrook
        case MultiIntr:
3049 a41b2ff2 pbrook
            ret = rtl8139_MultiIntr_read(s);
3050 a41b2ff2 pbrook
            break;
3051 a41b2ff2 pbrook
3052 a41b2ff2 pbrook
        case RxBufPtr:
3053 a41b2ff2 pbrook
            ret = rtl8139_RxBufPtr_read(s);
3054 a41b2ff2 pbrook
            break;
3055 a41b2ff2 pbrook
3056 6cadb320 bellard
        case RxBufAddr:
3057 6cadb320 bellard
            ret = rtl8139_RxBufAddr_read(s);
3058 6cadb320 bellard
            break;
3059 6cadb320 bellard
3060 a41b2ff2 pbrook
        case BasicModeCtrl:
3061 a41b2ff2 pbrook
            ret = rtl8139_BasicModeCtrl_read(s);
3062 a41b2ff2 pbrook
            break;
3063 a41b2ff2 pbrook
        case BasicModeStatus:
3064 a41b2ff2 pbrook
            ret = rtl8139_BasicModeStatus_read(s);
3065 a41b2ff2 pbrook
            break;
3066 a41b2ff2 pbrook
        case NWayAdvert:
3067 a41b2ff2 pbrook
            ret = s->NWayAdvert;
3068 7cdeb319 Benjamin Poirier
            DPRINTF("NWayAdvert read(w) val=0x%04x\n", ret);
3069 a41b2ff2 pbrook
            break;
3070 a41b2ff2 pbrook
        case NWayLPAR:
3071 a41b2ff2 pbrook
            ret = s->NWayLPAR;
3072 7cdeb319 Benjamin Poirier
            DPRINTF("NWayLPAR read(w) val=0x%04x\n", ret);
3073 a41b2ff2 pbrook
            break;
3074 a41b2ff2 pbrook
        case NWayExpansion:
3075 a41b2ff2 pbrook
            ret = s->NWayExpansion;
3076 7cdeb319 Benjamin Poirier
            DPRINTF("NWayExpansion read(w) val=0x%04x\n", ret);
3077 a41b2ff2 pbrook
            break;
3078 a41b2ff2 pbrook
3079 a41b2ff2 pbrook
        case CpCmd:
3080 a41b2ff2 pbrook
            ret = rtl8139_CpCmd_read(s);
3081 a41b2ff2 pbrook
            break;
3082 a41b2ff2 pbrook
3083 6cadb320 bellard
        case IntrMitigate:
3084 6cadb320 bellard
            ret = rtl8139_IntrMitigate_read(s);
3085 6cadb320 bellard
            break;
3086 6cadb320 bellard
3087 a41b2ff2 pbrook
        case TxSummary:
3088 a41b2ff2 pbrook
            ret = rtl8139_TSAD_read(s);
3089 a41b2ff2 pbrook
            break;
3090 a41b2ff2 pbrook
3091 a41b2ff2 pbrook
        case CSCR:
3092 a41b2ff2 pbrook
            ret = rtl8139_CSCR_read(s);
3093 a41b2ff2 pbrook
            break;
3094 a41b2ff2 pbrook
3095 a41b2ff2 pbrook
        default:
3096 7cdeb319 Benjamin Poirier
            DPRINTF("ioport read(w) addr=0x%x via read(b)\n", addr);
3097 a41b2ff2 pbrook
3098 a41b2ff2 pbrook
            ret  = rtl8139_io_readb(opaque, addr);
3099 a41b2ff2 pbrook
            ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
3100 a41b2ff2 pbrook
3101 7cdeb319 Benjamin Poirier
            DPRINTF("ioport read(w) addr=0x%x val=0x%04x\n", addr, ret);
3102 a41b2ff2 pbrook
            break;
3103 a41b2ff2 pbrook
    }
3104 a41b2ff2 pbrook
3105 a41b2ff2 pbrook
    return ret;
3106 a41b2ff2 pbrook
}
3107 a41b2ff2 pbrook
3108 a41b2ff2 pbrook
static uint32_t rtl8139_io_readl(void *opaque, uint8_t addr)
3109 a41b2ff2 pbrook
{
3110 a41b2ff2 pbrook
    RTL8139State *s = opaque;
3111 a41b2ff2 pbrook
    uint32_t ret;
3112 a41b2ff2 pbrook
3113 a41b2ff2 pbrook
    switch (addr)
3114 a41b2ff2 pbrook
    {
3115 a41b2ff2 pbrook
        case RxMissed:
3116 a41b2ff2 pbrook
            ret = s->RxMissed;
3117 a41b2ff2 pbrook
3118 7cdeb319 Benjamin Poirier
            DPRINTF("RxMissed read val=0x%08x\n", ret);
3119 a41b2ff2 pbrook
            break;
3120 a41b2ff2 pbrook
3121 a41b2ff2 pbrook
        case TxConfig:
3122 a41b2ff2 pbrook
            ret = rtl8139_TxConfig_read(s);
3123 a41b2ff2 pbrook
            break;
3124 a41b2ff2 pbrook
3125 a41b2ff2 pbrook
        case RxConfig:
3126 a41b2ff2 pbrook
            ret = rtl8139_RxConfig_read(s);
3127 a41b2ff2 pbrook
            break;
3128 a41b2ff2 pbrook
3129 a41b2ff2 pbrook
        case TxStatus0 ... TxStatus0+4*4-1:
3130 a41b2ff2 pbrook
            ret = rtl8139_TxStatus_read(s, addr-TxStatus0);
3131 a41b2ff2 pbrook
            break;
3132 a41b2ff2 pbrook
3133 a41b2ff2 pbrook
        case TxAddr0 ... TxAddr0+4*4-1:
3134 a41b2ff2 pbrook
            ret = rtl8139_TxAddr_read(s, addr-TxAddr0);
3135 a41b2ff2 pbrook
            break;
3136 a41b2ff2 pbrook
3137 a41b2ff2 pbrook
        case RxBuf:
3138 a41b2ff2 pbrook
            ret = rtl8139_RxBuf_read(s);
3139 a41b2ff2 pbrook
            break;
3140 a41b2ff2 pbrook
3141 a41b2ff2 pbrook
        case RxRingAddrLO:
3142 a41b2ff2 pbrook
            ret = s->RxRingAddrLO;
3143 7cdeb319 Benjamin Poirier
            DPRINTF("C+ RxRing low bits read val=0x%08x\n", ret);
3144 a41b2ff2 pbrook
            break;
3145 a41b2ff2 pbrook
3146 a41b2ff2 pbrook
        case RxRingAddrHI:
3147 a41b2ff2 pbrook
            ret = s->RxRingAddrHI;
3148 7cdeb319 Benjamin Poirier
            DPRINTF("C+ RxRing high bits read val=0x%08x\n", ret);
3149 6cadb320 bellard
            break;
3150 6cadb320 bellard
3151 6cadb320 bellard
        case Timer:
3152 74475455 Paolo Bonzini
            ret = muldiv64(qemu_get_clock_ns(vm_clock) - s->TCTR_base,
3153 05447803 Frediano Ziglio
                           PCI_FREQUENCY, get_ticks_per_sec());
3154 7cdeb319 Benjamin Poirier
            DPRINTF("TCTR Timer read val=0x%08x\n", ret);
3155 6cadb320 bellard
            break;
3156 6cadb320 bellard
3157 6cadb320 bellard
        case FlashReg:
3158 6cadb320 bellard
            ret = s->TimerInt;
3159 7cdeb319 Benjamin Poirier
            DPRINTF("FlashReg TimerInt read val=0x%08x\n", ret);
3160 a41b2ff2 pbrook
            break;
3161 a41b2ff2 pbrook
3162 a41b2ff2 pbrook
        default:
3163 7cdeb319 Benjamin Poirier
            DPRINTF("ioport read(l) addr=0x%x via read(b)\n", addr);
3164 a41b2ff2 pbrook
3165 a41b2ff2 pbrook
            ret  = rtl8139_io_readb(opaque, addr);
3166 a41b2ff2 pbrook
            ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
3167 a41b2ff2 pbrook
            ret |= rtl8139_io_readb(opaque, addr + 2) << 16;
3168 a41b2ff2 pbrook
            ret |= rtl8139_io_readb(opaque, addr + 3) << 24;
3169 a41b2ff2 pbrook
3170 7cdeb319 Benjamin Poirier
            DPRINTF("read(l) addr=0x%x val=%08x\n", addr, ret);
3171 a41b2ff2 pbrook
            break;
3172 a41b2ff2 pbrook
    }
3173 a41b2ff2 pbrook
3174 a41b2ff2 pbrook
    return ret;
3175 a41b2ff2 pbrook
}
3176 a41b2ff2 pbrook
3177 a41b2ff2 pbrook
/* */
3178 a41b2ff2 pbrook
3179 a41b2ff2 pbrook
static void rtl8139_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
3180 a41b2ff2 pbrook
{
3181 a41b2ff2 pbrook
    rtl8139_io_writeb(opaque, addr & 0xFF, val);
3182 a41b2ff2 pbrook
}
3183 a41b2ff2 pbrook
3184 a41b2ff2 pbrook
static void rtl8139_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
3185 a41b2ff2 pbrook
{
3186 a41b2ff2 pbrook
    rtl8139_io_writew(opaque, addr & 0xFF, val);
3187 a41b2ff2 pbrook
}
3188 a41b2ff2 pbrook
3189 a41b2ff2 pbrook
static void rtl8139_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
3190 a41b2ff2 pbrook
{
3191 a41b2ff2 pbrook
    rtl8139_io_writel(opaque, addr & 0xFF, val);
3192 a41b2ff2 pbrook
}
3193 a41b2ff2 pbrook
3194 a41b2ff2 pbrook
static uint32_t rtl8139_ioport_readb(void *opaque, uint32_t addr)
3195 a41b2ff2 pbrook
{
3196 a41b2ff2 pbrook
    return rtl8139_io_readb(opaque, addr & 0xFF);
3197 a41b2ff2 pbrook
}
3198 a41b2ff2 pbrook
3199 a41b2ff2 pbrook
static uint32_t rtl8139_ioport_readw(void *opaque, uint32_t addr)
3200 a41b2ff2 pbrook
{
3201 a41b2ff2 pbrook
    return rtl8139_io_readw(opaque, addr & 0xFF);
3202 a41b2ff2 pbrook
}
3203 a41b2ff2 pbrook
3204 a41b2ff2 pbrook
static uint32_t rtl8139_ioport_readl(void *opaque, uint32_t addr)
3205 a41b2ff2 pbrook
{
3206 a41b2ff2 pbrook
    return rtl8139_io_readl(opaque, addr & 0xFF);
3207 a41b2ff2 pbrook
}
3208 a41b2ff2 pbrook
3209 a41b2ff2 pbrook
/* */
3210 a41b2ff2 pbrook
3211 c227f099 Anthony Liguori
static void rtl8139_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
3212 a41b2ff2 pbrook
{
3213 a41b2ff2 pbrook
    rtl8139_io_writeb(opaque, addr & 0xFF, val);
3214 a41b2ff2 pbrook
}
3215 a41b2ff2 pbrook
3216 c227f099 Anthony Liguori
static void rtl8139_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
3217 a41b2ff2 pbrook
{
3218 a41b2ff2 pbrook
    rtl8139_io_writew(opaque, addr & 0xFF, val);
3219 a41b2ff2 pbrook
}
3220 a41b2ff2 pbrook
3221 c227f099 Anthony Liguori
static void rtl8139_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
3222 a41b2ff2 pbrook
{
3223 a41b2ff2 pbrook
    rtl8139_io_writel(opaque, addr & 0xFF, val);
3224 a41b2ff2 pbrook
}
3225 a41b2ff2 pbrook
3226 c227f099 Anthony Liguori
static uint32_t rtl8139_mmio_readb(void *opaque, target_phys_addr_t addr)
3227 a41b2ff2 pbrook
{
3228 a41b2ff2 pbrook
    return rtl8139_io_readb(opaque, addr & 0xFF);
3229 a41b2ff2 pbrook
}
3230 a41b2ff2 pbrook
3231 c227f099 Anthony Liguori
static uint32_t rtl8139_mmio_readw(void *opaque, target_phys_addr_t addr)
3232 a41b2ff2 pbrook
{
3233 5fedc612 aurel32
    uint32_t val = rtl8139_io_readw(opaque, addr & 0xFF);
3234 5fedc612 aurel32
    return val;
3235 a41b2ff2 pbrook
}
3236 a41b2ff2 pbrook
3237 c227f099 Anthony Liguori
static uint32_t rtl8139_mmio_readl(void *opaque, target_phys_addr_t addr)
3238 a41b2ff2 pbrook
{
3239 5fedc612 aurel32
    uint32_t val = rtl8139_io_readl(opaque, addr & 0xFF);
3240 5fedc612 aurel32
    return val;
3241 a41b2ff2 pbrook
}
3242 a41b2ff2 pbrook
3243 060110c3 Juan Quintela
static int rtl8139_post_load(void *opaque, int version_id)
3244 a41b2ff2 pbrook
{
3245 6597ebbb Juan Quintela
    RTL8139State* s = opaque;
3246 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
3247 060110c3 Juan Quintela
    if (version_id < 4) {
3248 2c3891ab aliguori
        s->cplus_enabled = s->CpCmd != 0;
3249 2c3891ab aliguori
    }
3250 2c3891ab aliguori
3251 a41b2ff2 pbrook
    return 0;
3252 a41b2ff2 pbrook
}
3253 a41b2ff2 pbrook
3254 c574ba5a Alex Williamson
static bool rtl8139_hotplug_ready_needed(void *opaque)
3255 c574ba5a Alex Williamson
{
3256 c574ba5a Alex Williamson
    return qdev_machine_modified();
3257 c574ba5a Alex Williamson
}
3258 c574ba5a Alex Williamson
3259 c574ba5a Alex Williamson
static const VMStateDescription vmstate_rtl8139_hotplug_ready ={
3260 c574ba5a Alex Williamson
    .name = "rtl8139/hotplug_ready",
3261 c574ba5a Alex Williamson
    .version_id = 1,
3262 c574ba5a Alex Williamson
    .minimum_version_id = 1,
3263 c574ba5a Alex Williamson
    .minimum_version_id_old = 1,
3264 c574ba5a Alex Williamson
    .fields      = (VMStateField []) {
3265 c574ba5a Alex Williamson
        VMSTATE_END_OF_LIST()
3266 c574ba5a Alex Williamson
    }
3267 c574ba5a Alex Williamson
};
3268 c574ba5a Alex Williamson
3269 05447803 Frediano Ziglio
static void rtl8139_pre_save(void *opaque)
3270 05447803 Frediano Ziglio
{
3271 05447803 Frediano Ziglio
    RTL8139State* s = opaque;
3272 74475455 Paolo Bonzini
    int64_t current_time = qemu_get_clock_ns(vm_clock);
3273 05447803 Frediano Ziglio
3274 05447803 Frediano Ziglio
    /* set IntrStatus correctly */
3275 05447803 Frediano Ziglio
    rtl8139_set_next_tctr_time(s, current_time);
3276 05447803 Frediano Ziglio
    s->TCTR = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY,
3277 05447803 Frediano Ziglio
                       get_ticks_per_sec());
3278 bd80f3fc Avi Kivity
    s->rtl8139_mmio_io_addr_dummy = 0;
3279 05447803 Frediano Ziglio
}
3280 05447803 Frediano Ziglio
3281 060110c3 Juan Quintela
static const VMStateDescription vmstate_rtl8139 = {
3282 060110c3 Juan Quintela
    .name = "rtl8139",
3283 060110c3 Juan Quintela
    .version_id = 4,
3284 060110c3 Juan Quintela
    .minimum_version_id = 3,
3285 060110c3 Juan Quintela
    .minimum_version_id_old = 3,
3286 060110c3 Juan Quintela
    .post_load = rtl8139_post_load,
3287 05447803 Frediano Ziglio
    .pre_save  = rtl8139_pre_save,
3288 060110c3 Juan Quintela
    .fields      = (VMStateField []) {
3289 060110c3 Juan Quintela
        VMSTATE_PCI_DEVICE(dev, RTL8139State),
3290 060110c3 Juan Quintela
        VMSTATE_PARTIAL_BUFFER(phys, RTL8139State, 6),
3291 060110c3 Juan Quintela
        VMSTATE_BUFFER(mult, RTL8139State),
3292 060110c3 Juan Quintela
        VMSTATE_UINT32_ARRAY(TxStatus, RTL8139State, 4),
3293 060110c3 Juan Quintela
        VMSTATE_UINT32_ARRAY(TxAddr, RTL8139State, 4),
3294 060110c3 Juan Quintela
3295 060110c3 Juan Quintela
        VMSTATE_UINT32(RxBuf, RTL8139State),
3296 060110c3 Juan Quintela
        VMSTATE_UINT32(RxBufferSize, RTL8139State),
3297 060110c3 Juan Quintela
        VMSTATE_UINT32(RxBufPtr, RTL8139State),
3298 060110c3 Juan Quintela
        VMSTATE_UINT32(RxBufAddr, RTL8139State),
3299 060110c3 Juan Quintela
3300 060110c3 Juan Quintela
        VMSTATE_UINT16(IntrStatus, RTL8139State),
3301 060110c3 Juan Quintela
        VMSTATE_UINT16(IntrMask, RTL8139State),
3302 060110c3 Juan Quintela
3303 060110c3 Juan Quintela
        VMSTATE_UINT32(TxConfig, RTL8139State),
3304 060110c3 Juan Quintela
        VMSTATE_UINT32(RxConfig, RTL8139State),
3305 060110c3 Juan Quintela
        VMSTATE_UINT32(RxMissed, RTL8139State),
3306 060110c3 Juan Quintela
        VMSTATE_UINT16(CSCR, RTL8139State),
3307 060110c3 Juan Quintela
3308 060110c3 Juan Quintela
        VMSTATE_UINT8(Cfg9346, RTL8139State),
3309 060110c3 Juan Quintela
        VMSTATE_UINT8(Config0, RTL8139State),
3310 060110c3 Juan Quintela
        VMSTATE_UINT8(Config1, RTL8139State),
3311 060110c3 Juan Quintela
        VMSTATE_UINT8(Config3, RTL8139State),
3312 060110c3 Juan Quintela
        VMSTATE_UINT8(Config4, RTL8139State),
3313 060110c3 Juan Quintela
        VMSTATE_UINT8(Config5, RTL8139State),
3314 060110c3 Juan Quintela
3315 060110c3 Juan Quintela
        VMSTATE_UINT8(clock_enabled, RTL8139State),
3316 060110c3 Juan Quintela
        VMSTATE_UINT8(bChipCmdState, RTL8139State),
3317 060110c3 Juan Quintela
3318 060110c3 Juan Quintela
        VMSTATE_UINT16(MultiIntr, RTL8139State),
3319 060110c3 Juan Quintela
3320 060110c3 Juan Quintela
        VMSTATE_UINT16(BasicModeCtrl, RTL8139State),
3321 060110c3 Juan Quintela
        VMSTATE_UINT16(BasicModeStatus, RTL8139State),
3322 060110c3 Juan Quintela
        VMSTATE_UINT16(NWayAdvert, RTL8139State),
3323 060110c3 Juan Quintela
        VMSTATE_UINT16(NWayLPAR, RTL8139State),
3324 060110c3 Juan Quintela
        VMSTATE_UINT16(NWayExpansion, RTL8139State),
3325 060110c3 Juan Quintela
3326 060110c3 Juan Quintela
        VMSTATE_UINT16(CpCmd, RTL8139State),
3327 060110c3 Juan Quintela
        VMSTATE_UINT8(TxThresh, RTL8139State),
3328 060110c3 Juan Quintela
3329 060110c3 Juan Quintela
        VMSTATE_UNUSED(4),
3330 060110c3 Juan Quintela
        VMSTATE_MACADDR(conf.macaddr, RTL8139State),
3331 c574ba5a Alex Williamson
        VMSTATE_INT32(rtl8139_mmio_io_addr_dummy, RTL8139State),
3332 060110c3 Juan Quintela
3333 060110c3 Juan Quintela
        VMSTATE_UINT32(currTxDesc, RTL8139State),
3334 060110c3 Juan Quintela
        VMSTATE_UINT32(currCPlusRxDesc, RTL8139State),
3335 060110c3 Juan Quintela
        VMSTATE_UINT32(currCPlusTxDesc, RTL8139State),
3336 060110c3 Juan Quintela
        VMSTATE_UINT32(RxRingAddrLO, RTL8139State),
3337 060110c3 Juan Quintela
        VMSTATE_UINT32(RxRingAddrHI, RTL8139State),
3338 060110c3 Juan Quintela
3339 060110c3 Juan Quintela
        VMSTATE_UINT16_ARRAY(eeprom.contents, RTL8139State, EEPROM_9346_SIZE),
3340 060110c3 Juan Quintela
        VMSTATE_INT32(eeprom.mode, RTL8139State),
3341 060110c3 Juan Quintela
        VMSTATE_UINT32(eeprom.tick, RTL8139State),
3342 060110c3 Juan Quintela
        VMSTATE_UINT8(eeprom.address, RTL8139State),
3343 060110c3 Juan Quintela
        VMSTATE_UINT16(eeprom.input, RTL8139State),
3344 060110c3 Juan Quintela
        VMSTATE_UINT16(eeprom.output, RTL8139State),
3345 060110c3 Juan Quintela
3346 060110c3 Juan Quintela
        VMSTATE_UINT8(eeprom.eecs, RTL8139State),
3347 060110c3 Juan Quintela
        VMSTATE_UINT8(eeprom.eesk, RTL8139State),
3348 060110c3 Juan Quintela
        VMSTATE_UINT8(eeprom.eedi, RTL8139State),
3349 060110c3 Juan Quintela
        VMSTATE_UINT8(eeprom.eedo, RTL8139State),
3350 060110c3 Juan Quintela
3351 060110c3 Juan Quintela
        VMSTATE_UINT32(TCTR, RTL8139State),
3352 060110c3 Juan Quintela
        VMSTATE_UINT32(TimerInt, RTL8139State),
3353 060110c3 Juan Quintela
        VMSTATE_INT64(TCTR_base, RTL8139State),
3354 060110c3 Juan Quintela
3355 060110c3 Juan Quintela
        VMSTATE_STRUCT(tally_counters, RTL8139State, 0,
3356 060110c3 Juan Quintela
                       vmstate_tally_counters, RTL8139TallyCounters),
3357 060110c3 Juan Quintela
3358 060110c3 Juan Quintela
        VMSTATE_UINT32_V(cplus_enabled, RTL8139State, 4),
3359 060110c3 Juan Quintela
        VMSTATE_END_OF_LIST()
3360 c574ba5a Alex Williamson
    },
3361 c574ba5a Alex Williamson
    .subsections = (VMStateSubsection []) {
3362 c574ba5a Alex Williamson
        {
3363 c574ba5a Alex Williamson
            .vmsd = &vmstate_rtl8139_hotplug_ready,
3364 c574ba5a Alex Williamson
            .needed = rtl8139_hotplug_ready_needed,
3365 c574ba5a Alex Williamson
        }, {
3366 c574ba5a Alex Williamson
            /* empty */
3367 c574ba5a Alex Williamson
        }
3368 060110c3 Juan Quintela
    }
3369 060110c3 Juan Quintela
};
3370 060110c3 Juan Quintela
3371 a41b2ff2 pbrook
/***********************************************************/
3372 a41b2ff2 pbrook
/* PCI RTL8139 definitions */
3373 a41b2ff2 pbrook
3374 bd80f3fc Avi Kivity
static const MemoryRegionPortio rtl8139_portio[] = {
3375 bd80f3fc Avi Kivity
    { 0, 0x100, 1, .read = rtl8139_ioport_readb, },
3376 bd80f3fc Avi Kivity
    { 0, 0x100, 1, .write = rtl8139_ioport_writeb, },
3377 bd80f3fc Avi Kivity
    { 0, 0x100, 2, .read = rtl8139_ioport_readw, },
3378 bd80f3fc Avi Kivity
    { 0, 0x100, 2, .write = rtl8139_ioport_writew, },
3379 bd80f3fc Avi Kivity
    { 0, 0x100, 4, .read = rtl8139_ioport_readl, },
3380 bd80f3fc Avi Kivity
    { 0, 0x100, 4, .write = rtl8139_ioport_writel, },
3381 bd80f3fc Avi Kivity
    PORTIO_END_OF_LIST()
3382 bd80f3fc Avi Kivity
};
3383 a41b2ff2 pbrook
3384 bd80f3fc Avi Kivity
static const MemoryRegionOps rtl8139_io_ops = {
3385 bd80f3fc Avi Kivity
    .old_portio = rtl8139_portio,
3386 bd80f3fc Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
3387 a41b2ff2 pbrook
};
3388 a41b2ff2 pbrook
3389 bd80f3fc Avi Kivity
static const MemoryRegionOps rtl8139_mmio_ops = {
3390 bd80f3fc Avi Kivity
    .old_mmio = {
3391 bd80f3fc Avi Kivity
        .read = {
3392 bd80f3fc Avi Kivity
            rtl8139_mmio_readb,
3393 bd80f3fc Avi Kivity
            rtl8139_mmio_readw,
3394 bd80f3fc Avi Kivity
            rtl8139_mmio_readl,
3395 bd80f3fc Avi Kivity
        },
3396 bd80f3fc Avi Kivity
        .write = {
3397 bd80f3fc Avi Kivity
            rtl8139_mmio_writeb,
3398 bd80f3fc Avi Kivity
            rtl8139_mmio_writew,
3399 bd80f3fc Avi Kivity
            rtl8139_mmio_writel,
3400 bd80f3fc Avi Kivity
        },
3401 bd80f3fc Avi Kivity
    },
3402 bd80f3fc Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
3403 a41b2ff2 pbrook
};
3404 a41b2ff2 pbrook
3405 6cadb320 bellard
static void rtl8139_timer(void *opaque)
3406 6cadb320 bellard
{
3407 6cadb320 bellard
    RTL8139State *s = opaque;
3408 6cadb320 bellard
3409 6cadb320 bellard
    if (!s->clock_enabled)
3410 6cadb320 bellard
    {
3411 7cdeb319 Benjamin Poirier
        DPRINTF(">>> timer: clock is not running\n");
3412 6cadb320 bellard
        return;
3413 6cadb320 bellard
    }
3414 6cadb320 bellard
3415 05447803 Frediano Ziglio
    s->IntrStatus |= PCSTimeout;
3416 05447803 Frediano Ziglio
    rtl8139_update_irq(s);
3417 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
3418 6cadb320 bellard
}
3419 6cadb320 bellard
3420 1673ad51 Mark McLoughlin
static void rtl8139_cleanup(VLANClientState *nc)
3421 b946a153 aliguori
{
3422 1673ad51 Mark McLoughlin
    RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
3423 b946a153 aliguori
3424 1673ad51 Mark McLoughlin
    s->nic = NULL;
3425 254111ec Gerd Hoffmann
}
3426 254111ec Gerd Hoffmann
3427 254111ec Gerd Hoffmann
static int pci_rtl8139_uninit(PCIDevice *dev)
3428 254111ec Gerd Hoffmann
{
3429 254111ec Gerd Hoffmann
    RTL8139State *s = DO_UPCAST(RTL8139State, dev, dev);
3430 254111ec Gerd Hoffmann
3431 bd80f3fc Avi Kivity
    memory_region_destroy(&s->bar_io);
3432 bd80f3fc Avi Kivity
    memory_region_destroy(&s->bar_mem);
3433 b946a153 aliguori
    if (s->cplus_txbuffer) {
3434 7267c094 Anthony Liguori
        g_free(s->cplus_txbuffer);
3435 b946a153 aliguori
        s->cplus_txbuffer = NULL;
3436 b946a153 aliguori
    }
3437 b946a153 aliguori
    qemu_del_timer(s->timer);
3438 b946a153 aliguori
    qemu_free_timer(s->timer);
3439 1673ad51 Mark McLoughlin
    qemu_del_vlan_client(&s->nic->nc);
3440 b946a153 aliguori
    return 0;
3441 b946a153 aliguori
}
3442 b946a153 aliguori
3443 1673ad51 Mark McLoughlin
static NetClientInfo net_rtl8139_info = {
3444 1673ad51 Mark McLoughlin
    .type = NET_CLIENT_TYPE_NIC,
3445 1673ad51 Mark McLoughlin
    .size = sizeof(NICState),
3446 1673ad51 Mark McLoughlin
    .can_receive = rtl8139_can_receive,
3447 1673ad51 Mark McLoughlin
    .receive = rtl8139_receive,
3448 1673ad51 Mark McLoughlin
    .cleanup = rtl8139_cleanup,
3449 1673ad51 Mark McLoughlin
};
3450 1673ad51 Mark McLoughlin
3451 81a322d4 Gerd Hoffmann
static int pci_rtl8139_init(PCIDevice *dev)
3452 a41b2ff2 pbrook
{
3453 efd6dd45 Juan Quintela
    RTL8139State * s = DO_UPCAST(RTL8139State, dev, dev);
3454 a41b2ff2 pbrook
    uint8_t *pci_conf;
3455 3b46e624 ths
3456 efd6dd45 Juan Quintela
    pci_conf = s->dev.config;
3457 817e0b6f Michael S. Tsirkin
    pci_conf[PCI_INTERRUPT_PIN] = 1;    /* interrupt pin A */
3458 0b5b3547 Michael S. Tsirkin
    /* TODO: start of capability list, but no capability
3459 0b5b3547 Michael S. Tsirkin
     * list bit in status register, and offset 0xdc seems unused. */
3460 0b5b3547 Michael S. Tsirkin
    pci_conf[PCI_CAPABILITY_LIST] = 0xdc;
3461 a41b2ff2 pbrook
3462 bd80f3fc Avi Kivity
    memory_region_init_io(&s->bar_io, &rtl8139_io_ops, s, "rtl8139", 0x100);
3463 bd80f3fc Avi Kivity
    memory_region_init_io(&s->bar_mem, &rtl8139_mmio_ops, s, "rtl8139", 0x100);
3464 e824b2cc Avi Kivity
    pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->bar_io);
3465 e824b2cc Avi Kivity
    pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar_mem);
3466 a41b2ff2 pbrook
3467 254111ec Gerd Hoffmann
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
3468 c1699988 Glauber Costa
3469 7165448a William Dauchy
    /* prepare eeprom */
3470 7165448a William Dauchy
    s->eeprom.contents[0] = 0x8129;
3471 7165448a William Dauchy
#if 1
3472 7165448a William Dauchy
    /* PCI vendor and device ID should be mirrored here */
3473 7165448a William Dauchy
    s->eeprom.contents[1] = PCI_VENDOR_ID_REALTEK;
3474 7165448a William Dauchy
    s->eeprom.contents[2] = PCI_DEVICE_ID_REALTEK_8139;
3475 7165448a William Dauchy
#endif
3476 7165448a William Dauchy
    s->eeprom.contents[7] = s->conf.macaddr.a[0] | s->conf.macaddr.a[1] << 8;
3477 7165448a William Dauchy
    s->eeprom.contents[8] = s->conf.macaddr.a[2] | s->conf.macaddr.a[3] << 8;
3478 7165448a William Dauchy
    s->eeprom.contents[9] = s->conf.macaddr.a[4] | s->conf.macaddr.a[5] << 8;
3479 7165448a William Dauchy
3480 1673ad51 Mark McLoughlin
    s->nic = qemu_new_nic(&net_rtl8139_info, &s->conf,
3481 1673ad51 Mark McLoughlin
                          dev->qdev.info->name, dev->qdev.id, s);
3482 1673ad51 Mark McLoughlin
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
3483 6cadb320 bellard
3484 6cadb320 bellard
    s->cplus_txbuffer = NULL;
3485 6cadb320 bellard
    s->cplus_txbuffer_len = 0;
3486 6cadb320 bellard
    s->cplus_txbuffer_offset = 0;
3487 3b46e624 ths
3488 05447803 Frediano Ziglio
    s->TimerExpire = 0;
3489 74475455 Paolo Bonzini
    s->timer = qemu_new_timer_ns(vm_clock, rtl8139_timer, s);
3490 74475455 Paolo Bonzini
    rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
3491 1ca4d09a Gleb Natapov
3492 1ca4d09a Gleb Natapov
    add_boot_device_path(s->conf.bootindex, &dev->qdev, "/ethernet-phy@0");
3493 1ca4d09a Gleb Natapov
3494 81a322d4 Gerd Hoffmann
    return 0;
3495 a41b2ff2 pbrook
}
3496 9d07d757 Paul Brook
3497 0aab0d3a Gerd Hoffmann
static PCIDeviceInfo rtl8139_info = {
3498 f82de8f0 Gerd Hoffmann
    .qdev.name  = "rtl8139",
3499 f82de8f0 Gerd Hoffmann
    .qdev.size  = sizeof(RTL8139State),
3500 f82de8f0 Gerd Hoffmann
    .qdev.reset = rtl8139_reset,
3501 be73cfe2 Juan Quintela
    .qdev.vmsd  = &vmstate_rtl8139,
3502 f82de8f0 Gerd Hoffmann
    .init       = pci_rtl8139_init,
3503 e3936fa5 Gerd Hoffmann
    .exit       = pci_rtl8139_uninit,
3504 5ee8ad71 Alex Williamson
    .romfile    = "pxe-rtl8139.rom",
3505 7cba16a7 Isaku Yamahata
    .vendor_id  = PCI_VENDOR_ID_REALTEK,
3506 7cba16a7 Isaku Yamahata
    .device_id  = PCI_DEVICE_ID_REALTEK_8139,
3507 7cba16a7 Isaku Yamahata
    .revision   = RTL8139_PCI_REVID, /* >=0x20 is for 8139C+ */
3508 7cba16a7 Isaku Yamahata
    .class_id   = PCI_CLASS_NETWORK_ETHERNET,
3509 254111ec Gerd Hoffmann
    .qdev.props = (Property[]) {
3510 254111ec Gerd Hoffmann
        DEFINE_NIC_PROPERTIES(RTL8139State, conf),
3511 254111ec Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
3512 254111ec Gerd Hoffmann
    }
3513 0aab0d3a Gerd Hoffmann
};
3514 0aab0d3a Gerd Hoffmann
3515 9d07d757 Paul Brook
static void rtl8139_register_devices(void)
3516 9d07d757 Paul Brook
{
3517 0aab0d3a Gerd Hoffmann
    pci_qdev_register(&rtl8139_info);
3518 9d07d757 Paul Brook
}
3519 9d07d757 Paul Brook
3520 9d07d757 Paul Brook
device_init(rtl8139_register_devices)