root / hw / rtl8139.c @ 18dabfd1
History | View | Annotate | Download (101.1 kB)
1 |
/**
|
---|---|
2 |
* QEMU RTL8139 emulation
|
3 |
*
|
4 |
* Copyright (c) 2006 Igor Kovalenko
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
|
24 |
* Modifications:
|
25 |
* 2006-Jan-28 Mark Malakanov : TSAD and CSCR implementation (for Windows driver)
|
26 |
*
|
27 |
* 2006-Apr-28 Juergen Lock : EEPROM emulation changes for FreeBSD driver
|
28 |
* HW revision ID changes for FreeBSD driver
|
29 |
*
|
30 |
* 2006-Jul-01 Igor Kovalenko : Implemented loopback mode for FreeBSD driver
|
31 |
* Corrected packet transfer reassembly routine for 8139C+ mode
|
32 |
* Rearranged debugging print statements
|
33 |
* Implemented PCI timer interrupt (disabled by default)
|
34 |
* Implemented Tally Counters, increased VM load/save version
|
35 |
* Implemented IP/TCP/UDP checksum task offloading
|
36 |
*
|
37 |
* 2006-Jul-04 Igor Kovalenko : Implemented TCP segmentation offloading
|
38 |
* Fixed MTU=1500 for produced ethernet frames
|
39 |
*
|
40 |
* 2006-Jul-09 Igor Kovalenko : Fixed TCP header length calculation while processing
|
41 |
* segmentation offloading
|
42 |
* Removed slirp.h dependency
|
43 |
* Added rx/tx buffer reset when enabling rx/tx operation
|
44 |
*
|
45 |
* 2010-Feb-04 Frediano Ziglio: Rewrote timer support using QEMU timer only
|
46 |
* when strictly needed (required for for
|
47 |
* Darwin)
|
48 |
*/
|
49 |
|
50 |
/* For crc32 */
|
51 |
#include <zlib.h> |
52 |
|
53 |
#include "hw.h" |
54 |
#include "pci.h" |
55 |
#include "qemu-timer.h" |
56 |
#include "net.h" |
57 |
#include "loader.h" |
58 |
#include "sysemu.h" |
59 |
|
60 |
/* debug RTL8139 card */
|
61 |
//#define DEBUG_RTL8139 1
|
62 |
|
63 |
#define PCI_FREQUENCY 33000000L |
64 |
|
65 |
/* debug RTL8139 card C+ mode only */
|
66 |
//#define DEBUG_RTL8139CP 1
|
67 |
|
68 |
#define SET_MASKED(input, mask, curr) \
|
69 |
( ( (input) & ~(mask) ) | ( (curr) & (mask) ) ) |
70 |
|
71 |
/* arg % size for size which is a power of 2 */
|
72 |
#define MOD2(input, size) \
|
73 |
( ( input ) & ( size - 1 ) )
|
74 |
|
75 |
#define ETHER_ADDR_LEN 6 |
76 |
#define ETHER_TYPE_LEN 2 |
77 |
#define ETH_HLEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) |
78 |
#define ETH_P_IP 0x0800 /* Internet Protocol packet */ |
79 |
#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */ |
80 |
#define ETH_MTU 1500 |
81 |
|
82 |
#define VLAN_TCI_LEN 2 |
83 |
#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
|
84 |
|
85 |
#if defined (DEBUG_RTL8139)
|
86 |
# define DEBUG_PRINT(x) do { printf x ; } while (0) |
87 |
#else
|
88 |
# define DEBUG_PRINT(x)
|
89 |
#endif
|
90 |
|
91 |
/* Symbolic offsets to registers. */
|
92 |
enum RTL8139_registers {
|
93 |
MAC0 = 0, /* Ethernet hardware address. */ |
94 |
MAR0 = 8, /* Multicast filter. */ |
95 |
TxStatus0 = 0x10,/* Transmit status (Four 32bit registers). C mode only */ |
96 |
/* Dump Tally Conter control register(64bit). C+ mode only */
|
97 |
TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */ |
98 |
RxBuf = 0x30,
|
99 |
ChipCmd = 0x37,
|
100 |
RxBufPtr = 0x38,
|
101 |
RxBufAddr = 0x3A,
|
102 |
IntrMask = 0x3C,
|
103 |
IntrStatus = 0x3E,
|
104 |
TxConfig = 0x40,
|
105 |
RxConfig = 0x44,
|
106 |
Timer = 0x48, /* A general-purpose counter. */ |
107 |
RxMissed = 0x4C, /* 24 bits valid, write clears. */ |
108 |
Cfg9346 = 0x50,
|
109 |
Config0 = 0x51,
|
110 |
Config1 = 0x52,
|
111 |
FlashReg = 0x54,
|
112 |
MediaStatus = 0x58,
|
113 |
Config3 = 0x59,
|
114 |
Config4 = 0x5A, /* absent on RTL-8139A */ |
115 |
HltClk = 0x5B,
|
116 |
MultiIntr = 0x5C,
|
117 |
PCIRevisionID = 0x5E,
|
118 |
TxSummary = 0x60, /* TSAD register. Transmit Status of All Descriptors*/ |
119 |
BasicModeCtrl = 0x62,
|
120 |
BasicModeStatus = 0x64,
|
121 |
NWayAdvert = 0x66,
|
122 |
NWayLPAR = 0x68,
|
123 |
NWayExpansion = 0x6A,
|
124 |
/* Undocumented registers, but required for proper operation. */
|
125 |
FIFOTMS = 0x70, /* FIFO Control and test. */ |
126 |
CSCR = 0x74, /* Chip Status and Configuration Register. */ |
127 |
PARA78 = 0x78,
|
128 |
PARA7c = 0x7c, /* Magic transceiver parameter register. */ |
129 |
Config5 = 0xD8, /* absent on RTL-8139A */ |
130 |
/* C+ mode */
|
131 |
TxPoll = 0xD9, /* Tell chip to check Tx descriptors for work */ |
132 |
RxMaxSize = 0xDA, /* Max size of an Rx packet (8169 only) */ |
133 |
CpCmd = 0xE0, /* C+ Command register (C+ mode only) */ |
134 |
IntrMitigate = 0xE2, /* rx/tx interrupt mitigation control */ |
135 |
RxRingAddrLO = 0xE4, /* 64-bit start addr of Rx ring */ |
136 |
RxRingAddrHI = 0xE8, /* 64-bit start addr of Rx ring */ |
137 |
TxThresh = 0xEC, /* Early Tx threshold */ |
138 |
}; |
139 |
|
140 |
enum ClearBitMasks {
|
141 |
MultiIntrClear = 0xF000,
|
142 |
ChipCmdClear = 0xE2,
|
143 |
Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1), |
144 |
}; |
145 |
|
146 |
enum ChipCmdBits {
|
147 |
CmdReset = 0x10,
|
148 |
CmdRxEnb = 0x08,
|
149 |
CmdTxEnb = 0x04,
|
150 |
RxBufEmpty = 0x01,
|
151 |
}; |
152 |
|
153 |
/* C+ mode */
|
154 |
enum CplusCmdBits {
|
155 |
CPlusRxVLAN = 0x0040, /* enable receive VLAN detagging */ |
156 |
CPlusRxChkSum = 0x0020, /* enable receive checksum offloading */ |
157 |
CPlusRxEnb = 0x0002,
|
158 |
CPlusTxEnb = 0x0001,
|
159 |
}; |
160 |
|
161 |
/* Interrupt register bits, using my own meaningful names. */
|
162 |
enum IntrStatusBits {
|
163 |
PCIErr = 0x8000,
|
164 |
PCSTimeout = 0x4000,
|
165 |
RxFIFOOver = 0x40,
|
166 |
RxUnderrun = 0x20,
|
167 |
RxOverflow = 0x10,
|
168 |
TxErr = 0x08,
|
169 |
TxOK = 0x04,
|
170 |
RxErr = 0x02,
|
171 |
RxOK = 0x01,
|
172 |
|
173 |
RxAckBits = RxFIFOOver | RxOverflow | RxOK, |
174 |
}; |
175 |
|
176 |
enum TxStatusBits {
|
177 |
TxHostOwns = 0x2000,
|
178 |
TxUnderrun = 0x4000,
|
179 |
TxStatOK = 0x8000,
|
180 |
TxOutOfWindow = 0x20000000,
|
181 |
TxAborted = 0x40000000,
|
182 |
TxCarrierLost = 0x80000000,
|
183 |
}; |
184 |
enum RxStatusBits {
|
185 |
RxMulticast = 0x8000,
|
186 |
RxPhysical = 0x4000,
|
187 |
RxBroadcast = 0x2000,
|
188 |
RxBadSymbol = 0x0020,
|
189 |
RxRunt = 0x0010,
|
190 |
RxTooLong = 0x0008,
|
191 |
RxCRCErr = 0x0004,
|
192 |
RxBadAlign = 0x0002,
|
193 |
RxStatusOK = 0x0001,
|
194 |
}; |
195 |
|
196 |
/* Bits in RxConfig. */
|
197 |
enum rx_mode_bits {
|
198 |
AcceptErr = 0x20,
|
199 |
AcceptRunt = 0x10,
|
200 |
AcceptBroadcast = 0x08,
|
201 |
AcceptMulticast = 0x04,
|
202 |
AcceptMyPhys = 0x02,
|
203 |
AcceptAllPhys = 0x01,
|
204 |
}; |
205 |
|
206 |
/* Bits in TxConfig. */
|
207 |
enum tx_config_bits {
|
208 |
|
209 |
/* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
|
210 |
TxIFGShift = 24,
|
211 |
TxIFG84 = (0 << TxIFGShift), /* 8.4us / 840ns (10 / 100Mbps) */ |
212 |
TxIFG88 = (1 << TxIFGShift), /* 8.8us / 880ns (10 / 100Mbps) */ |
213 |
TxIFG92 = (2 << TxIFGShift), /* 9.2us / 920ns (10 / 100Mbps) */ |
214 |
TxIFG96 = (3 << TxIFGShift), /* 9.6us / 960ns (10 / 100Mbps) */ |
215 |
|
216 |
TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */ |
217 |
TxCRC = (1 << 16), /* DISABLE appending CRC to end of Tx packets */ |
218 |
TxClearAbt = (1 << 0), /* Clear abort (WO) */ |
219 |
TxDMAShift = 8, /* DMA burst value (0-7) is shifted this many bits */ |
220 |
TxRetryShift = 4, /* TXRR value (0-15) is shifted this many bits */ |
221 |
|
222 |
TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */ |
223 |
}; |
224 |
|
225 |
|
226 |
/* Transmit Status of All Descriptors (TSAD) Register */
|
227 |
enum TSAD_bits {
|
228 |
TSAD_TOK3 = 1<<15, // TOK bit of Descriptor 3 |
229 |
TSAD_TOK2 = 1<<14, // TOK bit of Descriptor 2 |
230 |
TSAD_TOK1 = 1<<13, // TOK bit of Descriptor 1 |
231 |
TSAD_TOK0 = 1<<12, // TOK bit of Descriptor 0 |
232 |
TSAD_TUN3 = 1<<11, // TUN bit of Descriptor 3 |
233 |
TSAD_TUN2 = 1<<10, // TUN bit of Descriptor 2 |
234 |
TSAD_TUN1 = 1<<9, // TUN bit of Descriptor 1 |
235 |
TSAD_TUN0 = 1<<8, // TUN bit of Descriptor 0 |
236 |
TSAD_TABT3 = 1<<07, // TABT bit of Descriptor 3 |
237 |
TSAD_TABT2 = 1<<06, // TABT bit of Descriptor 2 |
238 |
TSAD_TABT1 = 1<<05, // TABT bit of Descriptor 1 |
239 |
TSAD_TABT0 = 1<<04, // TABT bit of Descriptor 0 |
240 |
TSAD_OWN3 = 1<<03, // OWN bit of Descriptor 3 |
241 |
TSAD_OWN2 = 1<<02, // OWN bit of Descriptor 2 |
242 |
TSAD_OWN1 = 1<<01, // OWN bit of Descriptor 1 |
243 |
TSAD_OWN0 = 1<<00, // OWN bit of Descriptor 0 |
244 |
}; |
245 |
|
246 |
|
247 |
/* Bits in Config1 */
|
248 |
enum Config1Bits {
|
249 |
Cfg1_PM_Enable = 0x01,
|
250 |
Cfg1_VPD_Enable = 0x02,
|
251 |
Cfg1_PIO = 0x04,
|
252 |
Cfg1_MMIO = 0x08,
|
253 |
LWAKE = 0x10, /* not on 8139, 8139A */ |
254 |
Cfg1_Driver_Load = 0x20,
|
255 |
Cfg1_LED0 = 0x40,
|
256 |
Cfg1_LED1 = 0x80,
|
257 |
SLEEP = (1 << 1), /* only on 8139, 8139A */ |
258 |
PWRDN = (1 << 0), /* only on 8139, 8139A */ |
259 |
}; |
260 |
|
261 |
/* Bits in Config3 */
|
262 |
enum Config3Bits {
|
263 |
Cfg3_FBtBEn = (1 << 0), /* 1 = Fast Back to Back */ |
264 |
Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */ |
265 |
Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */ |
266 |
Cfg3_CardB_En = (1 << 3), /* 1 = enable CardBus registers */ |
267 |
Cfg3_LinkUp = (1 << 4), /* 1 = wake up on link up */ |
268 |
Cfg3_Magic = (1 << 5), /* 1 = wake up on Magic Packet (tm) */ |
269 |
Cfg3_PARM_En = (1 << 6), /* 0 = software can set twister parameters */ |
270 |
Cfg3_GNTSel = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */ |
271 |
}; |
272 |
|
273 |
/* Bits in Config4 */
|
274 |
enum Config4Bits {
|
275 |
LWPTN = (1 << 2), /* not on 8139, 8139A */ |
276 |
}; |
277 |
|
278 |
/* Bits in Config5 */
|
279 |
enum Config5Bits {
|
280 |
Cfg5_PME_STS = (1 << 0), /* 1 = PCI reset resets PME_Status */ |
281 |
Cfg5_LANWake = (1 << 1), /* 1 = enable LANWake signal */ |
282 |
Cfg5_LDPS = (1 << 2), /* 0 = save power when link is down */ |
283 |
Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */ |
284 |
Cfg5_UWF = (1 << 4), /* 1 = accept unicast wakeup frame */ |
285 |
Cfg5_MWF = (1 << 5), /* 1 = accept multicast wakeup frame */ |
286 |
Cfg5_BWF = (1 << 6), /* 1 = accept broadcast wakeup frame */ |
287 |
}; |
288 |
|
289 |
enum RxConfigBits {
|
290 |
/* rx fifo threshold */
|
291 |
RxCfgFIFOShift = 13,
|
292 |
RxCfgFIFONone = (7 << RxCfgFIFOShift),
|
293 |
|
294 |
/* Max DMA burst */
|
295 |
RxCfgDMAShift = 8,
|
296 |
RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
|
297 |
|
298 |
/* rx ring buffer length */
|
299 |
RxCfgRcv8K = 0,
|
300 |
RxCfgRcv16K = (1 << 11), |
301 |
RxCfgRcv32K = (1 << 12), |
302 |
RxCfgRcv64K = (1 << 11) | (1 << 12), |
303 |
|
304 |
/* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
|
305 |
RxNoWrap = (1 << 7), |
306 |
}; |
307 |
|
308 |
/* Twister tuning parameters from RealTek.
|
309 |
Completely undocumented, but required to tune bad links on some boards. */
|
310 |
/*
|
311 |
enum CSCRBits {
|
312 |
CSCR_LinkOKBit = 0x0400,
|
313 |
CSCR_LinkChangeBit = 0x0800,
|
314 |
CSCR_LinkStatusBits = 0x0f000,
|
315 |
CSCR_LinkDownOffCmd = 0x003c0,
|
316 |
CSCR_LinkDownCmd = 0x0f3c0,
|
317 |
*/
|
318 |
enum CSCRBits {
|
319 |
CSCR_Testfun = 1<<15, /* 1 = Auto-neg speeds up internal timer, WO, def 0 */ |
320 |
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*/ |
321 |
CSCR_HEART_BIT = 1<<8, /* 1 = HEART BEAT enable, 0 = HEART BEAT disable. HEART BEAT function is only valid in 10Mbps mode. def 1*/ |
322 |
CSCR_JBEN = 1<<7, /* 1 = enable jabber function. 0 = disable jabber function, def 1*/ |
323 |
CSCR_F_LINK_100 = 1<<6, /* Used to login force good link in 100Mbps for diagnostic purposes. 1 = DISABLE, 0 = ENABLE. def 1*/ |
324 |
CSCR_F_Connect = 1<<5, /* Assertion of this bit forces the disconnect function to be bypassed. def 0*/ |
325 |
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*/ |
326 |
CSCR_Con_status_En = 1<<2, /* Assertion of this bit configures LED1 pin to indicate connection status. def 0*/ |
327 |
CSCR_PASS_SCR = 1<<0, /* Bypass Scramble, def 0*/ |
328 |
}; |
329 |
|
330 |
enum Cfg9346Bits {
|
331 |
Cfg9346_Lock = 0x00,
|
332 |
Cfg9346_Unlock = 0xC0,
|
333 |
}; |
334 |
|
335 |
typedef enum { |
336 |
CH_8139 = 0,
|
337 |
CH_8139_K, |
338 |
CH_8139A, |
339 |
CH_8139A_G, |
340 |
CH_8139B, |
341 |
CH_8130, |
342 |
CH_8139C, |
343 |
CH_8100, |
344 |
CH_8100B_8139D, |
345 |
CH_8101, |
346 |
} chip_t; |
347 |
|
348 |
enum chip_flags {
|
349 |
HasHltClk = (1 << 0), |
350 |
HasLWake = (1 << 1), |
351 |
}; |
352 |
|
353 |
#define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
|
354 |
(b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22) |
355 |
#define HW_REVID_MASK HW_REVID(1, 1, 1, 1, 1, 1, 1) |
356 |
|
357 |
#define RTL8139_PCI_REVID_8139 0x10 |
358 |
#define RTL8139_PCI_REVID_8139CPLUS 0x20 |
359 |
|
360 |
#define RTL8139_PCI_REVID RTL8139_PCI_REVID_8139CPLUS
|
361 |
|
362 |
/* Size is 64 * 16bit words */
|
363 |
#define EEPROM_9346_ADDR_BITS 6 |
364 |
#define EEPROM_9346_SIZE (1 << EEPROM_9346_ADDR_BITS) |
365 |
#define EEPROM_9346_ADDR_MASK (EEPROM_9346_SIZE - 1) |
366 |
|
367 |
enum Chip9346Operation
|
368 |
{ |
369 |
Chip9346_op_mask = 0xc0, /* 10 zzzzzz */ |
370 |
Chip9346_op_read = 0x80, /* 10 AAAAAA */ |
371 |
Chip9346_op_write = 0x40, /* 01 AAAAAA D(15)..D(0) */ |
372 |
Chip9346_op_ext_mask = 0xf0, /* 11 zzzzzz */ |
373 |
Chip9346_op_write_enable = 0x30, /* 00 11zzzz */ |
374 |
Chip9346_op_write_all = 0x10, /* 00 01zzzz */ |
375 |
Chip9346_op_write_disable = 0x00, /* 00 00zzzz */ |
376 |
}; |
377 |
|
378 |
enum Chip9346Mode
|
379 |
{ |
380 |
Chip9346_none = 0,
|
381 |
Chip9346_enter_command_mode, |
382 |
Chip9346_read_command, |
383 |
Chip9346_data_read, /* from output register */
|
384 |
Chip9346_data_write, /* to input register, then to contents at specified address */
|
385 |
Chip9346_data_write_all, /* to input register, then filling contents */
|
386 |
}; |
387 |
|
388 |
typedef struct EEprom9346 |
389 |
{ |
390 |
uint16_t contents[EEPROM_9346_SIZE]; |
391 |
int mode;
|
392 |
uint32_t tick; |
393 |
uint8_t address; |
394 |
uint16_t input; |
395 |
uint16_t output; |
396 |
|
397 |
uint8_t eecs; |
398 |
uint8_t eesk; |
399 |
uint8_t eedi; |
400 |
uint8_t eedo; |
401 |
} EEprom9346; |
402 |
|
403 |
typedef struct RTL8139TallyCounters |
404 |
{ |
405 |
/* Tally counters */
|
406 |
uint64_t TxOk; |
407 |
uint64_t RxOk; |
408 |
uint64_t TxERR; |
409 |
uint32_t RxERR; |
410 |
uint16_t MissPkt; |
411 |
uint16_t FAE; |
412 |
uint32_t Tx1Col; |
413 |
uint32_t TxMCol; |
414 |
uint64_t RxOkPhy; |
415 |
uint64_t RxOkBrd; |
416 |
uint32_t RxOkMul; |
417 |
uint16_t TxAbt; |
418 |
uint16_t TxUndrn; |
419 |
} RTL8139TallyCounters; |
420 |
|
421 |
/* Clears all tally counters */
|
422 |
static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters); |
423 |
|
424 |
/* Writes tally counters to specified physical memory address */
|
425 |
static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr, RTL8139TallyCounters* counters); |
426 |
|
427 |
typedef struct RTL8139State { |
428 |
PCIDevice dev; |
429 |
uint8_t phys[8]; /* mac address */ |
430 |
uint8_t mult[8]; /* multicast mask array */ |
431 |
|
432 |
uint32_t TxStatus[4]; /* TxStatus0 in C mode*/ /* also DTCCR[0] and DTCCR[1] in C+ mode */ |
433 |
uint32_t TxAddr[4]; /* TxAddr0 */ |
434 |
uint32_t RxBuf; /* Receive buffer */
|
435 |
uint32_t RxBufferSize;/* internal variable, receive ring buffer size in C mode */
|
436 |
uint32_t RxBufPtr; |
437 |
uint32_t RxBufAddr; |
438 |
|
439 |
uint16_t IntrStatus; |
440 |
uint16_t IntrMask; |
441 |
|
442 |
uint32_t TxConfig; |
443 |
uint32_t RxConfig; |
444 |
uint32_t RxMissed; |
445 |
|
446 |
uint16_t CSCR; |
447 |
|
448 |
uint8_t Cfg9346; |
449 |
uint8_t Config0; |
450 |
uint8_t Config1; |
451 |
uint8_t Config3; |
452 |
uint8_t Config4; |
453 |
uint8_t Config5; |
454 |
|
455 |
uint8_t clock_enabled; |
456 |
uint8_t bChipCmdState; |
457 |
|
458 |
uint16_t MultiIntr; |
459 |
|
460 |
uint16_t BasicModeCtrl; |
461 |
uint16_t BasicModeStatus; |
462 |
uint16_t NWayAdvert; |
463 |
uint16_t NWayLPAR; |
464 |
uint16_t NWayExpansion; |
465 |
|
466 |
uint16_t CpCmd; |
467 |
uint8_t TxThresh; |
468 |
|
469 |
NICState *nic; |
470 |
NICConf conf; |
471 |
int rtl8139_mmio_io_addr;
|
472 |
|
473 |
/* C ring mode */
|
474 |
uint32_t currTxDesc; |
475 |
|
476 |
/* C+ mode */
|
477 |
uint32_t cplus_enabled; |
478 |
|
479 |
uint32_t currCPlusRxDesc; |
480 |
uint32_t currCPlusTxDesc; |
481 |
|
482 |
uint32_t RxRingAddrLO; |
483 |
uint32_t RxRingAddrHI; |
484 |
|
485 |
EEprom9346 eeprom; |
486 |
|
487 |
uint32_t TCTR; |
488 |
uint32_t TimerInt; |
489 |
int64_t TCTR_base; |
490 |
|
491 |
/* Tally counters */
|
492 |
RTL8139TallyCounters tally_counters; |
493 |
|
494 |
/* Non-persistent data */
|
495 |
uint8_t *cplus_txbuffer; |
496 |
int cplus_txbuffer_len;
|
497 |
int cplus_txbuffer_offset;
|
498 |
|
499 |
/* PCI interrupt timer */
|
500 |
QEMUTimer *timer; |
501 |
int64_t TimerExpire; |
502 |
|
503 |
/* Support migration to/from old versions */
|
504 |
int rtl8139_mmio_io_addr_dummy;
|
505 |
} RTL8139State; |
506 |
|
507 |
static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time); |
508 |
|
509 |
static void prom9346_decode_command(EEprom9346 *eeprom, uint8_t command) |
510 |
{ |
511 |
DEBUG_PRINT(("RTL8139: eeprom command 0x%02x\n", command));
|
512 |
|
513 |
switch (command & Chip9346_op_mask)
|
514 |
{ |
515 |
case Chip9346_op_read:
|
516 |
{ |
517 |
eeprom->address = command & EEPROM_9346_ADDR_MASK; |
518 |
eeprom->output = eeprom->contents[eeprom->address]; |
519 |
eeprom->eedo = 0;
|
520 |
eeprom->tick = 0;
|
521 |
eeprom->mode = Chip9346_data_read; |
522 |
DEBUG_PRINT(("RTL8139: eeprom read from address 0x%02x data=0x%04x\n",
|
523 |
eeprom->address, eeprom->output)); |
524 |
} |
525 |
break;
|
526 |
|
527 |
case Chip9346_op_write:
|
528 |
{ |
529 |
eeprom->address = command & EEPROM_9346_ADDR_MASK; |
530 |
eeprom->input = 0;
|
531 |
eeprom->tick = 0;
|
532 |
eeprom->mode = Chip9346_none; /* Chip9346_data_write */
|
533 |
DEBUG_PRINT(("RTL8139: eeprom begin write to address 0x%02x\n",
|
534 |
eeprom->address)); |
535 |
} |
536 |
break;
|
537 |
default:
|
538 |
eeprom->mode = Chip9346_none; |
539 |
switch (command & Chip9346_op_ext_mask)
|
540 |
{ |
541 |
case Chip9346_op_write_enable:
|
542 |
DEBUG_PRINT(("RTL8139: eeprom write enabled\n"));
|
543 |
break;
|
544 |
case Chip9346_op_write_all:
|
545 |
DEBUG_PRINT(("RTL8139: eeprom begin write all\n"));
|
546 |
break;
|
547 |
case Chip9346_op_write_disable:
|
548 |
DEBUG_PRINT(("RTL8139: eeprom write disabled\n"));
|
549 |
break;
|
550 |
} |
551 |
break;
|
552 |
} |
553 |
} |
554 |
|
555 |
static void prom9346_shift_clock(EEprom9346 *eeprom) |
556 |
{ |
557 |
int bit = eeprom->eedi?1:0; |
558 |
|
559 |
++ eeprom->tick; |
560 |
|
561 |
DEBUG_PRINT(("eeprom: tick %d eedi=%d eedo=%d\n", eeprom->tick, eeprom->eedi, eeprom->eedo));
|
562 |
|
563 |
switch (eeprom->mode)
|
564 |
{ |
565 |
case Chip9346_enter_command_mode:
|
566 |
if (bit)
|
567 |
{ |
568 |
eeprom->mode = Chip9346_read_command; |
569 |
eeprom->tick = 0;
|
570 |
eeprom->input = 0;
|
571 |
DEBUG_PRINT(("eeprom: +++ synchronized, begin command read\n"));
|
572 |
} |
573 |
break;
|
574 |
|
575 |
case Chip9346_read_command:
|
576 |
eeprom->input = (eeprom->input << 1) | (bit & 1); |
577 |
if (eeprom->tick == 8) |
578 |
{ |
579 |
prom9346_decode_command(eeprom, eeprom->input & 0xff);
|
580 |
} |
581 |
break;
|
582 |
|
583 |
case Chip9346_data_read:
|
584 |
eeprom->eedo = (eeprom->output & 0x8000)?1:0; |
585 |
eeprom->output <<= 1;
|
586 |
if (eeprom->tick == 16) |
587 |
{ |
588 |
#if 1 |
589 |
// the FreeBSD drivers (rl and re) don't explicitly toggle
|
590 |
// CS between reads (or does setting Cfg9346 to 0 count too?),
|
591 |
// so we need to enter wait-for-command state here
|
592 |
eeprom->mode = Chip9346_enter_command_mode; |
593 |
eeprom->input = 0;
|
594 |
eeprom->tick = 0;
|
595 |
|
596 |
DEBUG_PRINT(("eeprom: +++ end of read, awaiting next command\n"));
|
597 |
#else
|
598 |
// original behaviour
|
599 |
++eeprom->address; |
600 |
eeprom->address &= EEPROM_9346_ADDR_MASK; |
601 |
eeprom->output = eeprom->contents[eeprom->address]; |
602 |
eeprom->tick = 0;
|
603 |
|
604 |
DEBUG_PRINT(("eeprom: +++ read next address 0x%02x data=0x%04x\n",
|
605 |
eeprom->address, eeprom->output)); |
606 |
#endif
|
607 |
} |
608 |
break;
|
609 |
|
610 |
case Chip9346_data_write:
|
611 |
eeprom->input = (eeprom->input << 1) | (bit & 1); |
612 |
if (eeprom->tick == 16) |
613 |
{ |
614 |
DEBUG_PRINT(("RTL8139: eeprom write to address 0x%02x data=0x%04x\n",
|
615 |
eeprom->address, eeprom->input)); |
616 |
|
617 |
eeprom->contents[eeprom->address] = eeprom->input; |
618 |
eeprom->mode = Chip9346_none; /* waiting for next command after CS cycle */
|
619 |
eeprom->tick = 0;
|
620 |
eeprom->input = 0;
|
621 |
} |
622 |
break;
|
623 |
|
624 |
case Chip9346_data_write_all:
|
625 |
eeprom->input = (eeprom->input << 1) | (bit & 1); |
626 |
if (eeprom->tick == 16) |
627 |
{ |
628 |
int i;
|
629 |
for (i = 0; i < EEPROM_9346_SIZE; i++) |
630 |
{ |
631 |
eeprom->contents[i] = eeprom->input; |
632 |
} |
633 |
DEBUG_PRINT(("RTL8139: eeprom filled with data=0x%04x\n",
|
634 |
eeprom->input)); |
635 |
|
636 |
eeprom->mode = Chip9346_enter_command_mode; |
637 |
eeprom->tick = 0;
|
638 |
eeprom->input = 0;
|
639 |
} |
640 |
break;
|
641 |
|
642 |
default:
|
643 |
break;
|
644 |
} |
645 |
} |
646 |
|
647 |
static int prom9346_get_wire(RTL8139State *s) |
648 |
{ |
649 |
EEprom9346 *eeprom = &s->eeprom; |
650 |
if (!eeprom->eecs)
|
651 |
return 0; |
652 |
|
653 |
return eeprom->eedo;
|
654 |
} |
655 |
|
656 |
/* FIXME: This should be merged into/replaced by eeprom93xx.c. */
|
657 |
static void prom9346_set_wire(RTL8139State *s, int eecs, int eesk, int eedi) |
658 |
{ |
659 |
EEprom9346 *eeprom = &s->eeprom; |
660 |
uint8_t old_eecs = eeprom->eecs; |
661 |
uint8_t old_eesk = eeprom->eesk; |
662 |
|
663 |
eeprom->eecs = eecs; |
664 |
eeprom->eesk = eesk; |
665 |
eeprom->eedi = eedi; |
666 |
|
667 |
DEBUG_PRINT(("eeprom: +++ wires CS=%d SK=%d DI=%d DO=%d\n",
|
668 |
eeprom->eecs, eeprom->eesk, eeprom->eedi, eeprom->eedo)); |
669 |
|
670 |
if (!old_eecs && eecs)
|
671 |
{ |
672 |
/* Synchronize start */
|
673 |
eeprom->tick = 0;
|
674 |
eeprom->input = 0;
|
675 |
eeprom->output = 0;
|
676 |
eeprom->mode = Chip9346_enter_command_mode; |
677 |
|
678 |
DEBUG_PRINT(("=== eeprom: begin access, enter command mode\n"));
|
679 |
} |
680 |
|
681 |
if (!eecs)
|
682 |
{ |
683 |
DEBUG_PRINT(("=== eeprom: end access\n"));
|
684 |
return;
|
685 |
} |
686 |
|
687 |
if (!old_eesk && eesk)
|
688 |
{ |
689 |
/* SK front rules */
|
690 |
prom9346_shift_clock(eeprom); |
691 |
} |
692 |
} |
693 |
|
694 |
static void rtl8139_update_irq(RTL8139State *s) |
695 |
{ |
696 |
int isr;
|
697 |
isr = (s->IntrStatus & s->IntrMask) & 0xffff;
|
698 |
|
699 |
DEBUG_PRINT(("RTL8139: Set IRQ to %d (%04x %04x)\n",
|
700 |
isr ? 1 : 0, s->IntrStatus, s->IntrMask)); |
701 |
|
702 |
qemu_set_irq(s->dev.irq[0], (isr != 0)); |
703 |
} |
704 |
|
705 |
#define POLYNOMIAL 0x04c11db6 |
706 |
|
707 |
/* From FreeBSD */
|
708 |
/* XXX: optimize */
|
709 |
static int compute_mcast_idx(const uint8_t *ep) |
710 |
{ |
711 |
uint32_t crc; |
712 |
int carry, i, j;
|
713 |
uint8_t b; |
714 |
|
715 |
crc = 0xffffffff;
|
716 |
for (i = 0; i < 6; i++) { |
717 |
b = *ep++; |
718 |
for (j = 0; j < 8; j++) { |
719 |
carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); |
720 |
crc <<= 1;
|
721 |
b >>= 1;
|
722 |
if (carry)
|
723 |
crc = ((crc ^ POLYNOMIAL) | carry); |
724 |
} |
725 |
} |
726 |
return (crc >> 26); |
727 |
} |
728 |
|
729 |
static int rtl8139_RxWrap(RTL8139State *s) |
730 |
{ |
731 |
/* wrapping enabled; assume 1.5k more buffer space if size < 65536 */
|
732 |
return (s->RxConfig & (1 << 7)); |
733 |
} |
734 |
|
735 |
static int rtl8139_receiver_enabled(RTL8139State *s) |
736 |
{ |
737 |
return s->bChipCmdState & CmdRxEnb;
|
738 |
} |
739 |
|
740 |
static int rtl8139_transmitter_enabled(RTL8139State *s) |
741 |
{ |
742 |
return s->bChipCmdState & CmdTxEnb;
|
743 |
} |
744 |
|
745 |
static int rtl8139_cp_receiver_enabled(RTL8139State *s) |
746 |
{ |
747 |
return s->CpCmd & CPlusRxEnb;
|
748 |
} |
749 |
|
750 |
static int rtl8139_cp_transmitter_enabled(RTL8139State *s) |
751 |
{ |
752 |
return s->CpCmd & CPlusTxEnb;
|
753 |
} |
754 |
|
755 |
static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size) |
756 |
{ |
757 |
if (s->RxBufAddr + size > s->RxBufferSize)
|
758 |
{ |
759 |
int wrapped = MOD2(s->RxBufAddr + size, s->RxBufferSize);
|
760 |
|
761 |
/* write packet data */
|
762 |
if (wrapped && !(s->RxBufferSize < 65536 && rtl8139_RxWrap(s))) |
763 |
{ |
764 |
DEBUG_PRINT((">>> RTL8139: rx packet wrapped in buffer at %d\n", size-wrapped));
|
765 |
|
766 |
if (size > wrapped)
|
767 |
{ |
768 |
cpu_physical_memory_write( s->RxBuf + s->RxBufAddr, |
769 |
buf, size-wrapped ); |
770 |
} |
771 |
|
772 |
/* reset buffer pointer */
|
773 |
s->RxBufAddr = 0;
|
774 |
|
775 |
cpu_physical_memory_write( s->RxBuf + s->RxBufAddr, |
776 |
buf + (size-wrapped), wrapped ); |
777 |
|
778 |
s->RxBufAddr = wrapped; |
779 |
|
780 |
return;
|
781 |
} |
782 |
} |
783 |
|
784 |
/* non-wrapping path or overwrapping enabled */
|
785 |
cpu_physical_memory_write( s->RxBuf + s->RxBufAddr, buf, size ); |
786 |
|
787 |
s->RxBufAddr += size; |
788 |
} |
789 |
|
790 |
#define MIN_BUF_SIZE 60 |
791 |
static inline target_phys_addr_t rtl8139_addr64(uint32_t low, uint32_t high) |
792 |
{ |
793 |
#if TARGET_PHYS_ADDR_BITS > 32 |
794 |
return low | ((target_phys_addr_t)high << 32); |
795 |
#else
|
796 |
return low;
|
797 |
#endif
|
798 |
} |
799 |
|
800 |
static int rtl8139_can_receive(VLANClientState *nc) |
801 |
{ |
802 |
RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; |
803 |
int avail;
|
804 |
|
805 |
/* Receive (drop) packets if card is disabled. */
|
806 |
if (!s->clock_enabled)
|
807 |
return 1; |
808 |
if (!rtl8139_receiver_enabled(s))
|
809 |
return 1; |
810 |
|
811 |
if (rtl8139_cp_receiver_enabled(s)) {
|
812 |
/* ??? Flow control not implemented in c+ mode.
|
813 |
This is a hack to work around slirp deficiencies anyway. */
|
814 |
return 1; |
815 |
} else {
|
816 |
avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, |
817 |
s->RxBufferSize); |
818 |
return (avail == 0 || avail >= 1514); |
819 |
} |
820 |
} |
821 |
|
822 |
static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_t size_, int do_interrupt) |
823 |
{ |
824 |
RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; |
825 |
/* size is the length of the buffer passed to the driver */
|
826 |
int size = size_;
|
827 |
const uint8_t *dot1q_buf = NULL; |
828 |
|
829 |
uint32_t packet_header = 0;
|
830 |
|
831 |
uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN]; |
832 |
static const uint8_t broadcast_macaddr[6] = |
833 |
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
834 |
|
835 |
DEBUG_PRINT((">>> RTL8139: received len=%d\n", size));
|
836 |
|
837 |
/* test if board clock is stopped */
|
838 |
if (!s->clock_enabled)
|
839 |
{ |
840 |
DEBUG_PRINT(("RTL8139: stopped ==========================\n"));
|
841 |
return -1; |
842 |
} |
843 |
|
844 |
/* first check if receiver is enabled */
|
845 |
|
846 |
if (!rtl8139_receiver_enabled(s))
|
847 |
{ |
848 |
DEBUG_PRINT(("RTL8139: receiver disabled ================\n"));
|
849 |
return -1; |
850 |
} |
851 |
|
852 |
/* XXX: check this */
|
853 |
if (s->RxConfig & AcceptAllPhys) {
|
854 |
/* promiscuous: receive all */
|
855 |
DEBUG_PRINT((">>> RTL8139: packet received in promiscuous mode\n"));
|
856 |
|
857 |
} else {
|
858 |
if (!memcmp(buf, broadcast_macaddr, 6)) { |
859 |
/* broadcast address */
|
860 |
if (!(s->RxConfig & AcceptBroadcast))
|
861 |
{ |
862 |
DEBUG_PRINT((">>> RTL8139: broadcast packet rejected\n"));
|
863 |
|
864 |
/* update tally counter */
|
865 |
++s->tally_counters.RxERR; |
866 |
|
867 |
return size;
|
868 |
} |
869 |
|
870 |
packet_header |= RxBroadcast; |
871 |
|
872 |
DEBUG_PRINT((">>> RTL8139: broadcast packet received\n"));
|
873 |
|
874 |
/* update tally counter */
|
875 |
++s->tally_counters.RxOkBrd; |
876 |
|
877 |
} else if (buf[0] & 0x01) { |
878 |
/* multicast */
|
879 |
if (!(s->RxConfig & AcceptMulticast))
|
880 |
{ |
881 |
DEBUG_PRINT((">>> RTL8139: multicast packet rejected\n"));
|
882 |
|
883 |
/* update tally counter */
|
884 |
++s->tally_counters.RxERR; |
885 |
|
886 |
return size;
|
887 |
} |
888 |
|
889 |
int mcast_idx = compute_mcast_idx(buf);
|
890 |
|
891 |
if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) |
892 |
{ |
893 |
DEBUG_PRINT((">>> RTL8139: multicast address mismatch\n"));
|
894 |
|
895 |
/* update tally counter */
|
896 |
++s->tally_counters.RxERR; |
897 |
|
898 |
return size;
|
899 |
} |
900 |
|
901 |
packet_header |= RxMulticast; |
902 |
|
903 |
DEBUG_PRINT((">>> RTL8139: multicast packet received\n"));
|
904 |
|
905 |
/* update tally counter */
|
906 |
++s->tally_counters.RxOkMul; |
907 |
|
908 |
} else if (s->phys[0] == buf[0] && |
909 |
s->phys[1] == buf[1] && |
910 |
s->phys[2] == buf[2] && |
911 |
s->phys[3] == buf[3] && |
912 |
s->phys[4] == buf[4] && |
913 |
s->phys[5] == buf[5]) { |
914 |
/* match */
|
915 |
if (!(s->RxConfig & AcceptMyPhys))
|
916 |
{ |
917 |
DEBUG_PRINT((">>> RTL8139: rejecting physical address matching packet\n"));
|
918 |
|
919 |
/* update tally counter */
|
920 |
++s->tally_counters.RxERR; |
921 |
|
922 |
return size;
|
923 |
} |
924 |
|
925 |
packet_header |= RxPhysical; |
926 |
|
927 |
DEBUG_PRINT((">>> RTL8139: physical address matching packet received\n"));
|
928 |
|
929 |
/* update tally counter */
|
930 |
++s->tally_counters.RxOkPhy; |
931 |
|
932 |
} else {
|
933 |
|
934 |
DEBUG_PRINT((">>> RTL8139: unknown packet\n"));
|
935 |
|
936 |
/* update tally counter */
|
937 |
++s->tally_counters.RxERR; |
938 |
|
939 |
return size;
|
940 |
} |
941 |
} |
942 |
|
943 |
/* if too small buffer, then expand it
|
944 |
* Include some tailroom in case a vlan tag is later removed. */
|
945 |
if (size < MIN_BUF_SIZE + VLAN_HLEN) {
|
946 |
memcpy(buf1, buf, size); |
947 |
memset(buf1 + size, 0, MIN_BUF_SIZE + VLAN_HLEN - size);
|
948 |
buf = buf1; |
949 |
if (size < MIN_BUF_SIZE) {
|
950 |
size = MIN_BUF_SIZE; |
951 |
} |
952 |
} |
953 |
|
954 |
if (rtl8139_cp_receiver_enabled(s))
|
955 |
{ |
956 |
DEBUG_PRINT(("RTL8139: in C+ Rx mode ================\n"));
|
957 |
|
958 |
/* begin C+ receiver mode */
|
959 |
|
960 |
/* w0 ownership flag */
|
961 |
#define CP_RX_OWN (1<<31) |
962 |
/* w0 end of ring flag */
|
963 |
#define CP_RX_EOR (1<<30) |
964 |
/* w0 bits 0...12 : buffer size */
|
965 |
#define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1) |
966 |
/* w1 tag available flag */
|
967 |
#define CP_RX_TAVA (1<<16) |
968 |
/* w1 bits 0...15 : VLAN tag */
|
969 |
#define CP_RX_VLAN_TAG_MASK ((1<<16) - 1) |
970 |
/* w2 low 32bit of Rx buffer ptr */
|
971 |
/* w3 high 32bit of Rx buffer ptr */
|
972 |
|
973 |
int descriptor = s->currCPlusRxDesc;
|
974 |
target_phys_addr_t cplus_rx_ring_desc; |
975 |
|
976 |
cplus_rx_ring_desc = rtl8139_addr64(s->RxRingAddrLO, s->RxRingAddrHI); |
977 |
cplus_rx_ring_desc += 16 * descriptor;
|
978 |
|
979 |
DEBUG_PRINT(("RTL8139: +++ C+ mode reading RX descriptor %d from host memory at %08x %08x = %016" PRIx64 "\n", |
980 |
descriptor, s->RxRingAddrHI, s->RxRingAddrLO, (uint64_t)cplus_rx_ring_desc)); |
981 |
|
982 |
uint32_t val, rxdw0,rxdw1,rxbufLO,rxbufHI; |
983 |
|
984 |
cpu_physical_memory_read(cplus_rx_ring_desc, (uint8_t *)&val, 4);
|
985 |
rxdw0 = le32_to_cpu(val); |
986 |
cpu_physical_memory_read(cplus_rx_ring_desc+4, (uint8_t *)&val, 4); |
987 |
rxdw1 = le32_to_cpu(val); |
988 |
cpu_physical_memory_read(cplus_rx_ring_desc+8, (uint8_t *)&val, 4); |
989 |
rxbufLO = le32_to_cpu(val); |
990 |
cpu_physical_memory_read(cplus_rx_ring_desc+12, (uint8_t *)&val, 4); |
991 |
rxbufHI = le32_to_cpu(val); |
992 |
|
993 |
DEBUG_PRINT(("RTL8139: +++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
|
994 |
descriptor, |
995 |
rxdw0, rxdw1, rxbufLO, rxbufHI)); |
996 |
|
997 |
if (!(rxdw0 & CP_RX_OWN))
|
998 |
{ |
999 |
DEBUG_PRINT(("RTL8139: C+ Rx mode : descriptor %d is owned by host\n", descriptor));
|
1000 |
|
1001 |
s->IntrStatus |= RxOverflow; |
1002 |
++s->RxMissed; |
1003 |
|
1004 |
/* update tally counter */
|
1005 |
++s->tally_counters.RxERR; |
1006 |
++s->tally_counters.MissPkt; |
1007 |
|
1008 |
rtl8139_update_irq(s); |
1009 |
return size_;
|
1010 |
} |
1011 |
|
1012 |
uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK; |
1013 |
|
1014 |
/* write VLAN info to descriptor variables. */
|
1015 |
if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *)
|
1016 |
&buf[ETHER_ADDR_LEN * 2]) == ETH_P_8021Q) {
|
1017 |
dot1q_buf = &buf[ETHER_ADDR_LEN * 2];
|
1018 |
size -= VLAN_HLEN; |
1019 |
/* if too small buffer, use the tailroom added duing expansion */
|
1020 |
if (size < MIN_BUF_SIZE) {
|
1021 |
size = MIN_BUF_SIZE; |
1022 |
} |
1023 |
|
1024 |
rxdw1 &= ~CP_RX_VLAN_TAG_MASK; |
1025 |
/* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
|
1026 |
rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *) |
1027 |
&dot1q_buf[ETHER_TYPE_LEN]); |
1028 |
|
1029 |
DEBUG_PRINT(("RTL8139: C+ Rx mode : extracted vlan tag with tci: "
|
1030 |
"%u\n", be16_to_cpup((uint16_t *)
|
1031 |
&dot1q_buf[ETHER_TYPE_LEN]))); |
1032 |
} else {
|
1033 |
/* reset VLAN tag flag */
|
1034 |
rxdw1 &= ~CP_RX_TAVA; |
1035 |
} |
1036 |
|
1037 |
/* TODO: scatter the packet over available receive ring descriptors space */
|
1038 |
|
1039 |
if (size+4 > rx_space) |
1040 |
{ |
1041 |
DEBUG_PRINT(("RTL8139: C+ Rx mode : descriptor %d size %d received %d + 4\n",
|
1042 |
descriptor, rx_space, size)); |
1043 |
|
1044 |
s->IntrStatus |= RxOverflow; |
1045 |
++s->RxMissed; |
1046 |
|
1047 |
/* update tally counter */
|
1048 |
++s->tally_counters.RxERR; |
1049 |
++s->tally_counters.MissPkt; |
1050 |
|
1051 |
rtl8139_update_irq(s); |
1052 |
return size_;
|
1053 |
} |
1054 |
|
1055 |
target_phys_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI); |
1056 |
|
1057 |
/* receive/copy to target memory */
|
1058 |
if (dot1q_buf) {
|
1059 |
cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN);
|
1060 |
cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN,
|
1061 |
buf + 2 * ETHER_ADDR_LEN + VLAN_HLEN,
|
1062 |
size - 2 * ETHER_ADDR_LEN);
|
1063 |
} else {
|
1064 |
cpu_physical_memory_write(rx_addr, buf, size); |
1065 |
} |
1066 |
|
1067 |
if (s->CpCmd & CPlusRxChkSum)
|
1068 |
{ |
1069 |
/* do some packet checksumming */
|
1070 |
} |
1071 |
|
1072 |
/* write checksum */
|
1073 |
val = cpu_to_le32(crc32(0, buf, size_));
|
1074 |
cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4);
|
1075 |
|
1076 |
/* first segment of received packet flag */
|
1077 |
#define CP_RX_STATUS_FS (1<<29) |
1078 |
/* last segment of received packet flag */
|
1079 |
#define CP_RX_STATUS_LS (1<<28) |
1080 |
/* multicast packet flag */
|
1081 |
#define CP_RX_STATUS_MAR (1<<26) |
1082 |
/* physical-matching packet flag */
|
1083 |
#define CP_RX_STATUS_PAM (1<<25) |
1084 |
/* broadcast packet flag */
|
1085 |
#define CP_RX_STATUS_BAR (1<<24) |
1086 |
/* runt packet flag */
|
1087 |
#define CP_RX_STATUS_RUNT (1<<19) |
1088 |
/* crc error flag */
|
1089 |
#define CP_RX_STATUS_CRC (1<<18) |
1090 |
/* IP checksum error flag */
|
1091 |
#define CP_RX_STATUS_IPF (1<<15) |
1092 |
/* UDP checksum error flag */
|
1093 |
#define CP_RX_STATUS_UDPF (1<<14) |
1094 |
/* TCP checksum error flag */
|
1095 |
#define CP_RX_STATUS_TCPF (1<<13) |
1096 |
|
1097 |
/* transfer ownership to target */
|
1098 |
rxdw0 &= ~CP_RX_OWN; |
1099 |
|
1100 |
/* set first segment bit */
|
1101 |
rxdw0 |= CP_RX_STATUS_FS; |
1102 |
|
1103 |
/* set last segment bit */
|
1104 |
rxdw0 |= CP_RX_STATUS_LS; |
1105 |
|
1106 |
/* set received packet type flags */
|
1107 |
if (packet_header & RxBroadcast)
|
1108 |
rxdw0 |= CP_RX_STATUS_BAR; |
1109 |
if (packet_header & RxMulticast)
|
1110 |
rxdw0 |= CP_RX_STATUS_MAR; |
1111 |
if (packet_header & RxPhysical)
|
1112 |
rxdw0 |= CP_RX_STATUS_PAM; |
1113 |
|
1114 |
/* set received size */
|
1115 |
rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK; |
1116 |
rxdw0 |= (size+4);
|
1117 |
|
1118 |
/* update ring data */
|
1119 |
val = cpu_to_le32(rxdw0); |
1120 |
cpu_physical_memory_write(cplus_rx_ring_desc, (uint8_t *)&val, 4);
|
1121 |
val = cpu_to_le32(rxdw1); |
1122 |
cpu_physical_memory_write(cplus_rx_ring_desc+4, (uint8_t *)&val, 4); |
1123 |
|
1124 |
/* update tally counter */
|
1125 |
++s->tally_counters.RxOk; |
1126 |
|
1127 |
/* seek to next Rx descriptor */
|
1128 |
if (rxdw0 & CP_RX_EOR)
|
1129 |
{ |
1130 |
s->currCPlusRxDesc = 0;
|
1131 |
} |
1132 |
else
|
1133 |
{ |
1134 |
++s->currCPlusRxDesc; |
1135 |
} |
1136 |
|
1137 |
DEBUG_PRINT(("RTL8139: done C+ Rx mode ----------------\n"));
|
1138 |
|
1139 |
} |
1140 |
else
|
1141 |
{ |
1142 |
DEBUG_PRINT(("RTL8139: in ring Rx mode ================\n"));
|
1143 |
|
1144 |
/* begin ring receiver mode */
|
1145 |
int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize);
|
1146 |
|
1147 |
/* if receiver buffer is empty then avail == 0 */
|
1148 |
|
1149 |
if (avail != 0 && size + 8 >= avail) |
1150 |
{ |
1151 |
DEBUG_PRINT(("rx overflow: rx buffer length %d head 0x%04x read 0x%04x === available 0x%04x need 0x%04x\n",
|
1152 |
s->RxBufferSize, s->RxBufAddr, s->RxBufPtr, avail, size + 8));
|
1153 |
|
1154 |
s->IntrStatus |= RxOverflow; |
1155 |
++s->RxMissed; |
1156 |
rtl8139_update_irq(s); |
1157 |
return size_;
|
1158 |
} |
1159 |
|
1160 |
packet_header |= RxStatusOK; |
1161 |
|
1162 |
packet_header |= (((size+4) << 16) & 0xffff0000); |
1163 |
|
1164 |
/* write header */
|
1165 |
uint32_t val = cpu_to_le32(packet_header); |
1166 |
|
1167 |
rtl8139_write_buffer(s, (uint8_t *)&val, 4);
|
1168 |
|
1169 |
rtl8139_write_buffer(s, buf, size); |
1170 |
|
1171 |
/* write checksum */
|
1172 |
val = cpu_to_le32(crc32(0, buf, size));
|
1173 |
rtl8139_write_buffer(s, (uint8_t *)&val, 4);
|
1174 |
|
1175 |
/* correct buffer write pointer */
|
1176 |
s->RxBufAddr = MOD2((s->RxBufAddr + 3) & ~0x3, s->RxBufferSize); |
1177 |
|
1178 |
/* now we can signal we have received something */
|
1179 |
|
1180 |
DEBUG_PRINT((" received: rx buffer length %d head 0x%04x read 0x%04x\n",
|
1181 |
s->RxBufferSize, s->RxBufAddr, s->RxBufPtr)); |
1182 |
} |
1183 |
|
1184 |
s->IntrStatus |= RxOK; |
1185 |
|
1186 |
if (do_interrupt)
|
1187 |
{ |
1188 |
rtl8139_update_irq(s); |
1189 |
} |
1190 |
|
1191 |
return size_;
|
1192 |
} |
1193 |
|
1194 |
static ssize_t rtl8139_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
1195 |
{ |
1196 |
return rtl8139_do_receive(nc, buf, size, 1); |
1197 |
} |
1198 |
|
1199 |
static void rtl8139_reset_rxring(RTL8139State *s, uint32_t bufferSize) |
1200 |
{ |
1201 |
s->RxBufferSize = bufferSize; |
1202 |
s->RxBufPtr = 0;
|
1203 |
s->RxBufAddr = 0;
|
1204 |
} |
1205 |
|
1206 |
static void rtl8139_reset(DeviceState *d) |
1207 |
{ |
1208 |
RTL8139State *s = container_of(d, RTL8139State, dev.qdev); |
1209 |
int i;
|
1210 |
|
1211 |
/* restore MAC address */
|
1212 |
memcpy(s->phys, s->conf.macaddr.a, 6);
|
1213 |
|
1214 |
/* reset interrupt mask */
|
1215 |
s->IntrStatus = 0;
|
1216 |
s->IntrMask = 0;
|
1217 |
|
1218 |
rtl8139_update_irq(s); |
1219 |
|
1220 |
/* mark all status registers as owned by host */
|
1221 |
for (i = 0; i < 4; ++i) |
1222 |
{ |
1223 |
s->TxStatus[i] = TxHostOwns; |
1224 |
} |
1225 |
|
1226 |
s->currTxDesc = 0;
|
1227 |
s->currCPlusRxDesc = 0;
|
1228 |
s->currCPlusTxDesc = 0;
|
1229 |
|
1230 |
s->RxRingAddrLO = 0;
|
1231 |
s->RxRingAddrHI = 0;
|
1232 |
|
1233 |
s->RxBuf = 0;
|
1234 |
|
1235 |
rtl8139_reset_rxring(s, 8192);
|
1236 |
|
1237 |
/* ACK the reset */
|
1238 |
s->TxConfig = 0;
|
1239 |
|
1240 |
#if 0
|
1241 |
// s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139 HasHltClk
|
1242 |
s->clock_enabled = 0;
|
1243 |
#else
|
1244 |
s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake |
1245 |
s->clock_enabled = 1;
|
1246 |
#endif
|
1247 |
|
1248 |
s->bChipCmdState = CmdReset; /* RxBufEmpty bit is calculated on read from ChipCmd */;
|
1249 |
|
1250 |
/* set initial state data */
|
1251 |
s->Config0 = 0x0; /* No boot ROM */ |
1252 |
s->Config1 = 0xC; /* IO mapped and MEM mapped registers available */ |
1253 |
s->Config3 = 0x1; /* fast back-to-back compatible */ |
1254 |
s->Config5 = 0x0;
|
1255 |
|
1256 |
s->CSCR = CSCR_F_LINK_100 | CSCR_HEART_BIT | CSCR_LD; |
1257 |
|
1258 |
s->CpCmd = 0x0; /* reset C+ mode */ |
1259 |
s->cplus_enabled = 0;
|
1260 |
|
1261 |
|
1262 |
// s->BasicModeCtrl = 0x3100; // 100Mbps, full duplex, autonegotiation
|
1263 |
// s->BasicModeCtrl = 0x2100; // 100Mbps, full duplex
|
1264 |
s->BasicModeCtrl = 0x1000; // autonegotiation |
1265 |
|
1266 |
s->BasicModeStatus = 0x7809;
|
1267 |
//s->BasicModeStatus |= 0x0040; /* UTP medium */
|
1268 |
s->BasicModeStatus |= 0x0020; /* autonegotiation completed */ |
1269 |
s->BasicModeStatus |= 0x0004; /* link is up */ |
1270 |
|
1271 |
s->NWayAdvert = 0x05e1; /* all modes, full duplex */ |
1272 |
s->NWayLPAR = 0x05e1; /* all modes, full duplex */ |
1273 |
s->NWayExpansion = 0x0001; /* autonegotiation supported */ |
1274 |
|
1275 |
/* also reset timer and disable timer interrupt */
|
1276 |
s->TCTR = 0;
|
1277 |
s->TimerInt = 0;
|
1278 |
s->TCTR_base = 0;
|
1279 |
|
1280 |
/* reset tally counters */
|
1281 |
RTL8139TallyCounters_clear(&s->tally_counters); |
1282 |
} |
1283 |
|
1284 |
static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters) |
1285 |
{ |
1286 |
counters->TxOk = 0;
|
1287 |
counters->RxOk = 0;
|
1288 |
counters->TxERR = 0;
|
1289 |
counters->RxERR = 0;
|
1290 |
counters->MissPkt = 0;
|
1291 |
counters->FAE = 0;
|
1292 |
counters->Tx1Col = 0;
|
1293 |
counters->TxMCol = 0;
|
1294 |
counters->RxOkPhy = 0;
|
1295 |
counters->RxOkBrd = 0;
|
1296 |
counters->RxOkMul = 0;
|
1297 |
counters->TxAbt = 0;
|
1298 |
counters->TxUndrn = 0;
|
1299 |
} |
1300 |
|
1301 |
static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr, RTL8139TallyCounters* tally_counters) |
1302 |
{ |
1303 |
uint16_t val16; |
1304 |
uint32_t val32; |
1305 |
uint64_t val64; |
1306 |
|
1307 |
val64 = cpu_to_le64(tally_counters->TxOk); |
1308 |
cpu_physical_memory_write(tc_addr + 0, (uint8_t *)&val64, 8); |
1309 |
|
1310 |
val64 = cpu_to_le64(tally_counters->RxOk); |
1311 |
cpu_physical_memory_write(tc_addr + 8, (uint8_t *)&val64, 8); |
1312 |
|
1313 |
val64 = cpu_to_le64(tally_counters->TxERR); |
1314 |
cpu_physical_memory_write(tc_addr + 16, (uint8_t *)&val64, 8); |
1315 |
|
1316 |
val32 = cpu_to_le32(tally_counters->RxERR); |
1317 |
cpu_physical_memory_write(tc_addr + 24, (uint8_t *)&val32, 4); |
1318 |
|
1319 |
val16 = cpu_to_le16(tally_counters->MissPkt); |
1320 |
cpu_physical_memory_write(tc_addr + 28, (uint8_t *)&val16, 2); |
1321 |
|
1322 |
val16 = cpu_to_le16(tally_counters->FAE); |
1323 |
cpu_physical_memory_write(tc_addr + 30, (uint8_t *)&val16, 2); |
1324 |
|
1325 |
val32 = cpu_to_le32(tally_counters->Tx1Col); |
1326 |
cpu_physical_memory_write(tc_addr + 32, (uint8_t *)&val32, 4); |
1327 |
|
1328 |
val32 = cpu_to_le32(tally_counters->TxMCol); |
1329 |
cpu_physical_memory_write(tc_addr + 36, (uint8_t *)&val32, 4); |
1330 |
|
1331 |
val64 = cpu_to_le64(tally_counters->RxOkPhy); |
1332 |
cpu_physical_memory_write(tc_addr + 40, (uint8_t *)&val64, 8); |
1333 |
|
1334 |
val64 = cpu_to_le64(tally_counters->RxOkBrd); |
1335 |
cpu_physical_memory_write(tc_addr + 48, (uint8_t *)&val64, 8); |
1336 |
|
1337 |
val32 = cpu_to_le32(tally_counters->RxOkMul); |
1338 |
cpu_physical_memory_write(tc_addr + 56, (uint8_t *)&val32, 4); |
1339 |
|
1340 |
val16 = cpu_to_le16(tally_counters->TxAbt); |
1341 |
cpu_physical_memory_write(tc_addr + 60, (uint8_t *)&val16, 2); |
1342 |
|
1343 |
val16 = cpu_to_le16(tally_counters->TxUndrn); |
1344 |
cpu_physical_memory_write(tc_addr + 62, (uint8_t *)&val16, 2); |
1345 |
} |
1346 |
|
1347 |
/* Loads values of tally counters from VM state file */
|
1348 |
|
1349 |
static const VMStateDescription vmstate_tally_counters = { |
1350 |
.name = "tally_counters",
|
1351 |
.version_id = 1,
|
1352 |
.minimum_version_id = 1,
|
1353 |
.minimum_version_id_old = 1,
|
1354 |
.fields = (VMStateField []) { |
1355 |
VMSTATE_UINT64(TxOk, RTL8139TallyCounters), |
1356 |
VMSTATE_UINT64(RxOk, RTL8139TallyCounters), |
1357 |
VMSTATE_UINT64(TxERR, RTL8139TallyCounters), |
1358 |
VMSTATE_UINT32(RxERR, RTL8139TallyCounters), |
1359 |
VMSTATE_UINT16(MissPkt, RTL8139TallyCounters), |
1360 |
VMSTATE_UINT16(FAE, RTL8139TallyCounters), |
1361 |
VMSTATE_UINT32(Tx1Col, RTL8139TallyCounters), |
1362 |
VMSTATE_UINT32(TxMCol, RTL8139TallyCounters), |
1363 |
VMSTATE_UINT64(RxOkPhy, RTL8139TallyCounters), |
1364 |
VMSTATE_UINT64(RxOkBrd, RTL8139TallyCounters), |
1365 |
VMSTATE_UINT16(TxAbt, RTL8139TallyCounters), |
1366 |
VMSTATE_UINT16(TxUndrn, RTL8139TallyCounters), |
1367 |
VMSTATE_END_OF_LIST() |
1368 |
} |
1369 |
}; |
1370 |
|
1371 |
static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val) |
1372 |
{ |
1373 |
val &= 0xff;
|
1374 |
|
1375 |
DEBUG_PRINT(("RTL8139: ChipCmd write val=0x%08x\n", val));
|
1376 |
|
1377 |
if (val & CmdReset)
|
1378 |
{ |
1379 |
DEBUG_PRINT(("RTL8139: ChipCmd reset\n"));
|
1380 |
rtl8139_reset(&s->dev.qdev); |
1381 |
} |
1382 |
if (val & CmdRxEnb)
|
1383 |
{ |
1384 |
DEBUG_PRINT(("RTL8139: ChipCmd enable receiver\n"));
|
1385 |
|
1386 |
s->currCPlusRxDesc = 0;
|
1387 |
} |
1388 |
if (val & CmdTxEnb)
|
1389 |
{ |
1390 |
DEBUG_PRINT(("RTL8139: ChipCmd enable transmitter\n"));
|
1391 |
|
1392 |
s->currCPlusTxDesc = 0;
|
1393 |
} |
1394 |
|
1395 |
/* mask unwriteable bits */
|
1396 |
val = SET_MASKED(val, 0xe3, s->bChipCmdState);
|
1397 |
|
1398 |
/* Deassert reset pin before next read */
|
1399 |
val &= ~CmdReset; |
1400 |
|
1401 |
s->bChipCmdState = val; |
1402 |
} |
1403 |
|
1404 |
static int rtl8139_RxBufferEmpty(RTL8139State *s) |
1405 |
{ |
1406 |
int unread = MOD2(s->RxBufferSize + s->RxBufAddr - s->RxBufPtr, s->RxBufferSize);
|
1407 |
|
1408 |
if (unread != 0) |
1409 |
{ |
1410 |
DEBUG_PRINT(("RTL8139: receiver buffer data available 0x%04x\n", unread));
|
1411 |
return 0; |
1412 |
} |
1413 |
|
1414 |
DEBUG_PRINT(("RTL8139: receiver buffer is empty\n"));
|
1415 |
|
1416 |
return 1; |
1417 |
} |
1418 |
|
1419 |
static uint32_t rtl8139_ChipCmd_read(RTL8139State *s)
|
1420 |
{ |
1421 |
uint32_t ret = s->bChipCmdState; |
1422 |
|
1423 |
if (rtl8139_RxBufferEmpty(s))
|
1424 |
ret |= RxBufEmpty; |
1425 |
|
1426 |
DEBUG_PRINT(("RTL8139: ChipCmd read val=0x%04x\n", ret));
|
1427 |
|
1428 |
return ret;
|
1429 |
} |
1430 |
|
1431 |
static void rtl8139_CpCmd_write(RTL8139State *s, uint32_t val) |
1432 |
{ |
1433 |
val &= 0xffff;
|
1434 |
|
1435 |
DEBUG_PRINT(("RTL8139C+ command register write(w) val=0x%04x\n", val));
|
1436 |
|
1437 |
s->cplus_enabled = 1;
|
1438 |
|
1439 |
/* mask unwriteable bits */
|
1440 |
val = SET_MASKED(val, 0xff84, s->CpCmd);
|
1441 |
|
1442 |
s->CpCmd = val; |
1443 |
} |
1444 |
|
1445 |
static uint32_t rtl8139_CpCmd_read(RTL8139State *s)
|
1446 |
{ |
1447 |
uint32_t ret = s->CpCmd; |
1448 |
|
1449 |
DEBUG_PRINT(("RTL8139C+ command register read(w) val=0x%04x\n", ret));
|
1450 |
|
1451 |
return ret;
|
1452 |
} |
1453 |
|
1454 |
static void rtl8139_IntrMitigate_write(RTL8139State *s, uint32_t val) |
1455 |
{ |
1456 |
DEBUG_PRINT(("RTL8139C+ IntrMitigate register write(w) val=0x%04x\n", val));
|
1457 |
} |
1458 |
|
1459 |
static uint32_t rtl8139_IntrMitigate_read(RTL8139State *s)
|
1460 |
{ |
1461 |
uint32_t ret = 0;
|
1462 |
|
1463 |
DEBUG_PRINT(("RTL8139C+ IntrMitigate register read(w) val=0x%04x\n", ret));
|
1464 |
|
1465 |
return ret;
|
1466 |
} |
1467 |
|
1468 |
static int rtl8139_config_writeable(RTL8139State *s) |
1469 |
{ |
1470 |
if (s->Cfg9346 & Cfg9346_Unlock)
|
1471 |
{ |
1472 |
return 1; |
1473 |
} |
1474 |
|
1475 |
DEBUG_PRINT(("RTL8139: Configuration registers are write-protected\n"));
|
1476 |
|
1477 |
return 0; |
1478 |
} |
1479 |
|
1480 |
static void rtl8139_BasicModeCtrl_write(RTL8139State *s, uint32_t val) |
1481 |
{ |
1482 |
val &= 0xffff;
|
1483 |
|
1484 |
DEBUG_PRINT(("RTL8139: BasicModeCtrl register write(w) val=0x%04x\n", val));
|
1485 |
|
1486 |
/* mask unwriteable bits */
|
1487 |
uint32_t mask = 0x4cff;
|
1488 |
|
1489 |
if (1 || !rtl8139_config_writeable(s)) |
1490 |
{ |
1491 |
/* Speed setting and autonegotiation enable bits are read-only */
|
1492 |
mask |= 0x3000;
|
1493 |
/* Duplex mode setting is read-only */
|
1494 |
mask |= 0x0100;
|
1495 |
} |
1496 |
|
1497 |
val = SET_MASKED(val, mask, s->BasicModeCtrl); |
1498 |
|
1499 |
s->BasicModeCtrl = val; |
1500 |
} |
1501 |
|
1502 |
static uint32_t rtl8139_BasicModeCtrl_read(RTL8139State *s)
|
1503 |
{ |
1504 |
uint32_t ret = s->BasicModeCtrl; |
1505 |
|
1506 |
DEBUG_PRINT(("RTL8139: BasicModeCtrl register read(w) val=0x%04x\n", ret));
|
1507 |
|
1508 |
return ret;
|
1509 |
} |
1510 |
|
1511 |
static void rtl8139_BasicModeStatus_write(RTL8139State *s, uint32_t val) |
1512 |
{ |
1513 |
val &= 0xffff;
|
1514 |
|
1515 |
DEBUG_PRINT(("RTL8139: BasicModeStatus register write(w) val=0x%04x\n", val));
|
1516 |
|
1517 |
/* mask unwriteable bits */
|
1518 |
val = SET_MASKED(val, 0xff3f, s->BasicModeStatus);
|
1519 |
|
1520 |
s->BasicModeStatus = val; |
1521 |
} |
1522 |
|
1523 |
static uint32_t rtl8139_BasicModeStatus_read(RTL8139State *s)
|
1524 |
{ |
1525 |
uint32_t ret = s->BasicModeStatus; |
1526 |
|
1527 |
DEBUG_PRINT(("RTL8139: BasicModeStatus register read(w) val=0x%04x\n", ret));
|
1528 |
|
1529 |
return ret;
|
1530 |
} |
1531 |
|
1532 |
static void rtl8139_Cfg9346_write(RTL8139State *s, uint32_t val) |
1533 |
{ |
1534 |
val &= 0xff;
|
1535 |
|
1536 |
DEBUG_PRINT(("RTL8139: Cfg9346 write val=0x%02x\n", val));
|
1537 |
|
1538 |
/* mask unwriteable bits */
|
1539 |
val = SET_MASKED(val, 0x31, s->Cfg9346);
|
1540 |
|
1541 |
uint32_t opmode = val & 0xc0;
|
1542 |
uint32_t eeprom_val = val & 0xf;
|
1543 |
|
1544 |
if (opmode == 0x80) { |
1545 |
/* eeprom access */
|
1546 |
int eecs = (eeprom_val & 0x08)?1:0; |
1547 |
int eesk = (eeprom_val & 0x04)?1:0; |
1548 |
int eedi = (eeprom_val & 0x02)?1:0; |
1549 |
prom9346_set_wire(s, eecs, eesk, eedi); |
1550 |
} else if (opmode == 0x40) { |
1551 |
/* Reset. */
|
1552 |
val = 0;
|
1553 |
rtl8139_reset(&s->dev.qdev); |
1554 |
} |
1555 |
|
1556 |
s->Cfg9346 = val; |
1557 |
} |
1558 |
|
1559 |
static uint32_t rtl8139_Cfg9346_read(RTL8139State *s)
|
1560 |
{ |
1561 |
uint32_t ret = s->Cfg9346; |
1562 |
|
1563 |
uint32_t opmode = ret & 0xc0;
|
1564 |
|
1565 |
if (opmode == 0x80) |
1566 |
{ |
1567 |
/* eeprom access */
|
1568 |
int eedo = prom9346_get_wire(s);
|
1569 |
if (eedo)
|
1570 |
{ |
1571 |
ret |= 0x01;
|
1572 |
} |
1573 |
else
|
1574 |
{ |
1575 |
ret &= ~0x01;
|
1576 |
} |
1577 |
} |
1578 |
|
1579 |
DEBUG_PRINT(("RTL8139: Cfg9346 read val=0x%02x\n", ret));
|
1580 |
|
1581 |
return ret;
|
1582 |
} |
1583 |
|
1584 |
static void rtl8139_Config0_write(RTL8139State *s, uint32_t val) |
1585 |
{ |
1586 |
val &= 0xff;
|
1587 |
|
1588 |
DEBUG_PRINT(("RTL8139: Config0 write val=0x%02x\n", val));
|
1589 |
|
1590 |
if (!rtl8139_config_writeable(s))
|
1591 |
return;
|
1592 |
|
1593 |
/* mask unwriteable bits */
|
1594 |
val = SET_MASKED(val, 0xf8, s->Config0);
|
1595 |
|
1596 |
s->Config0 = val; |
1597 |
} |
1598 |
|
1599 |
static uint32_t rtl8139_Config0_read(RTL8139State *s)
|
1600 |
{ |
1601 |
uint32_t ret = s->Config0; |
1602 |
|
1603 |
DEBUG_PRINT(("RTL8139: Config0 read val=0x%02x\n", ret));
|
1604 |
|
1605 |
return ret;
|
1606 |
} |
1607 |
|
1608 |
static void rtl8139_Config1_write(RTL8139State *s, uint32_t val) |
1609 |
{ |
1610 |
val &= 0xff;
|
1611 |
|
1612 |
DEBUG_PRINT(("RTL8139: Config1 write val=0x%02x\n", val));
|
1613 |
|
1614 |
if (!rtl8139_config_writeable(s))
|
1615 |
return;
|
1616 |
|
1617 |
/* mask unwriteable bits */
|
1618 |
val = SET_MASKED(val, 0xC, s->Config1);
|
1619 |
|
1620 |
s->Config1 = val; |
1621 |
} |
1622 |
|
1623 |
static uint32_t rtl8139_Config1_read(RTL8139State *s)
|
1624 |
{ |
1625 |
uint32_t ret = s->Config1; |
1626 |
|
1627 |
DEBUG_PRINT(("RTL8139: Config1 read val=0x%02x\n", ret));
|
1628 |
|
1629 |
return ret;
|
1630 |
} |
1631 |
|
1632 |
static void rtl8139_Config3_write(RTL8139State *s, uint32_t val) |
1633 |
{ |
1634 |
val &= 0xff;
|
1635 |
|
1636 |
DEBUG_PRINT(("RTL8139: Config3 write val=0x%02x\n", val));
|
1637 |
|
1638 |
if (!rtl8139_config_writeable(s))
|
1639 |
return;
|
1640 |
|
1641 |
/* mask unwriteable bits */
|
1642 |
val = SET_MASKED(val, 0x8F, s->Config3);
|
1643 |
|
1644 |
s->Config3 = val; |
1645 |
} |
1646 |
|
1647 |
static uint32_t rtl8139_Config3_read(RTL8139State *s)
|
1648 |
{ |
1649 |
uint32_t ret = s->Config3; |
1650 |
|
1651 |
DEBUG_PRINT(("RTL8139: Config3 read val=0x%02x\n", ret));
|
1652 |
|
1653 |
return ret;
|
1654 |
} |
1655 |
|
1656 |
static void rtl8139_Config4_write(RTL8139State *s, uint32_t val) |
1657 |
{ |
1658 |
val &= 0xff;
|
1659 |
|
1660 |
DEBUG_PRINT(("RTL8139: Config4 write val=0x%02x\n", val));
|
1661 |
|
1662 |
if (!rtl8139_config_writeable(s))
|
1663 |
return;
|
1664 |
|
1665 |
/* mask unwriteable bits */
|
1666 |
val = SET_MASKED(val, 0x0a, s->Config4);
|
1667 |
|
1668 |
s->Config4 = val; |
1669 |
} |
1670 |
|
1671 |
static uint32_t rtl8139_Config4_read(RTL8139State *s)
|
1672 |
{ |
1673 |
uint32_t ret = s->Config4; |
1674 |
|
1675 |
DEBUG_PRINT(("RTL8139: Config4 read val=0x%02x\n", ret));
|
1676 |
|
1677 |
return ret;
|
1678 |
} |
1679 |
|
1680 |
static void rtl8139_Config5_write(RTL8139State *s, uint32_t val) |
1681 |
{ |
1682 |
val &= 0xff;
|
1683 |
|
1684 |
DEBUG_PRINT(("RTL8139: Config5 write val=0x%02x\n", val));
|
1685 |
|
1686 |
/* mask unwriteable bits */
|
1687 |
val = SET_MASKED(val, 0x80, s->Config5);
|
1688 |
|
1689 |
s->Config5 = val; |
1690 |
} |
1691 |
|
1692 |
static uint32_t rtl8139_Config5_read(RTL8139State *s)
|
1693 |
{ |
1694 |
uint32_t ret = s->Config5; |
1695 |
|
1696 |
DEBUG_PRINT(("RTL8139: Config5 read val=0x%02x\n", ret));
|
1697 |
|
1698 |
return ret;
|
1699 |
} |
1700 |
|
1701 |
static void rtl8139_TxConfig_write(RTL8139State *s, uint32_t val) |
1702 |
{ |
1703 |
if (!rtl8139_transmitter_enabled(s))
|
1704 |
{ |
1705 |
DEBUG_PRINT(("RTL8139: transmitter disabled; no TxConfig write val=0x%08x\n", val));
|
1706 |
return;
|
1707 |
} |
1708 |
|
1709 |
DEBUG_PRINT(("RTL8139: TxConfig write val=0x%08x\n", val));
|
1710 |
|
1711 |
val = SET_MASKED(val, TxVersionMask | 0x8070f80f, s->TxConfig);
|
1712 |
|
1713 |
s->TxConfig = val; |
1714 |
} |
1715 |
|
1716 |
static void rtl8139_TxConfig_writeb(RTL8139State *s, uint32_t val) |
1717 |
{ |
1718 |
DEBUG_PRINT(("RTL8139C TxConfig via write(b) val=0x%02x\n", val));
|
1719 |
|
1720 |
uint32_t tc = s->TxConfig; |
1721 |
tc &= 0xFFFFFF00;
|
1722 |
tc |= (val & 0x000000FF);
|
1723 |
rtl8139_TxConfig_write(s, tc); |
1724 |
} |
1725 |
|
1726 |
static uint32_t rtl8139_TxConfig_read(RTL8139State *s)
|
1727 |
{ |
1728 |
uint32_t ret = s->TxConfig; |
1729 |
|
1730 |
DEBUG_PRINT(("RTL8139: TxConfig read val=0x%04x\n", ret));
|
1731 |
|
1732 |
return ret;
|
1733 |
} |
1734 |
|
1735 |
static void rtl8139_RxConfig_write(RTL8139State *s, uint32_t val) |
1736 |
{ |
1737 |
DEBUG_PRINT(("RTL8139: RxConfig write val=0x%08x\n", val));
|
1738 |
|
1739 |
/* mask unwriteable bits */
|
1740 |
val = SET_MASKED(val, 0xf0fc0040, s->RxConfig);
|
1741 |
|
1742 |
s->RxConfig = val; |
1743 |
|
1744 |
/* reset buffer size and read/write pointers */
|
1745 |
rtl8139_reset_rxring(s, 8192 << ((s->RxConfig >> 11) & 0x3)); |
1746 |
|
1747 |
DEBUG_PRINT(("RTL8139: RxConfig write reset buffer size to %d\n", s->RxBufferSize));
|
1748 |
} |
1749 |
|
1750 |
static uint32_t rtl8139_RxConfig_read(RTL8139State *s)
|
1751 |
{ |
1752 |
uint32_t ret = s->RxConfig; |
1753 |
|
1754 |
DEBUG_PRINT(("RTL8139: RxConfig read val=0x%08x\n", ret));
|
1755 |
|
1756 |
return ret;
|
1757 |
} |
1758 |
|
1759 |
static void rtl8139_transfer_frame(RTL8139State *s, const uint8_t *buf, int size, int do_interrupt) |
1760 |
{ |
1761 |
if (!size)
|
1762 |
{ |
1763 |
DEBUG_PRINT(("RTL8139: +++ empty ethernet frame\n"));
|
1764 |
return;
|
1765 |
} |
1766 |
|
1767 |
if (TxLoopBack == (s->TxConfig & TxLoopBack))
|
1768 |
{ |
1769 |
DEBUG_PRINT(("RTL8139: +++ transmit loopback mode\n"));
|
1770 |
rtl8139_do_receive(&s->nic->nc, buf, size, do_interrupt); |
1771 |
} |
1772 |
else
|
1773 |
{ |
1774 |
qemu_send_packet(&s->nic->nc, buf, size); |
1775 |
} |
1776 |
} |
1777 |
|
1778 |
static int rtl8139_transmit_one(RTL8139State *s, int descriptor) |
1779 |
{ |
1780 |
if (!rtl8139_transmitter_enabled(s))
|
1781 |
{ |
1782 |
DEBUG_PRINT(("RTL8139: +++ cannot transmit from descriptor %d: transmitter disabled\n",
|
1783 |
descriptor)); |
1784 |
return 0; |
1785 |
} |
1786 |
|
1787 |
if (s->TxStatus[descriptor] & TxHostOwns)
|
1788 |
{ |
1789 |
DEBUG_PRINT(("RTL8139: +++ cannot transmit from descriptor %d: owned by host (%08x)\n",
|
1790 |
descriptor, s->TxStatus[descriptor])); |
1791 |
return 0; |
1792 |
} |
1793 |
|
1794 |
DEBUG_PRINT(("RTL8139: +++ transmitting from descriptor %d\n", descriptor));
|
1795 |
|
1796 |
int txsize = s->TxStatus[descriptor] & 0x1fff; |
1797 |
uint8_t txbuffer[0x2000];
|
1798 |
|
1799 |
DEBUG_PRINT(("RTL8139: +++ transmit reading %d bytes from host memory at 0x%08x\n",
|
1800 |
txsize, s->TxAddr[descriptor])); |
1801 |
|
1802 |
cpu_physical_memory_read(s->TxAddr[descriptor], txbuffer, txsize); |
1803 |
|
1804 |
/* Mark descriptor as transferred */
|
1805 |
s->TxStatus[descriptor] |= TxHostOwns; |
1806 |
s->TxStatus[descriptor] |= TxStatOK; |
1807 |
|
1808 |
rtl8139_transfer_frame(s, txbuffer, txsize, 0);
|
1809 |
|
1810 |
DEBUG_PRINT(("RTL8139: +++ transmitted %d bytes from descriptor %d\n", txsize, descriptor));
|
1811 |
|
1812 |
/* update interrupt */
|
1813 |
s->IntrStatus |= TxOK; |
1814 |
rtl8139_update_irq(s); |
1815 |
|
1816 |
return 1; |
1817 |
} |
1818 |
|
1819 |
/* structures and macros for task offloading */
|
1820 |
typedef struct ip_header |
1821 |
{ |
1822 |
uint8_t ip_ver_len; /* version and header length */
|
1823 |
uint8_t ip_tos; /* type of service */
|
1824 |
uint16_t ip_len; /* total length */
|
1825 |
uint16_t ip_id; /* identification */
|
1826 |
uint16_t ip_off; /* fragment offset field */
|
1827 |
uint8_t ip_ttl; /* time to live */
|
1828 |
uint8_t ip_p; /* protocol */
|
1829 |
uint16_t ip_sum; /* checksum */
|
1830 |
uint32_t ip_src,ip_dst; /* source and dest address */
|
1831 |
} ip_header; |
1832 |
|
1833 |
#define IP_HEADER_VERSION_4 4 |
1834 |
#define IP_HEADER_VERSION(ip) ((ip->ip_ver_len >> 4)&0xf) |
1835 |
#define IP_HEADER_LENGTH(ip) (((ip->ip_ver_len)&0xf) << 2) |
1836 |
|
1837 |
typedef struct tcp_header |
1838 |
{ |
1839 |
uint16_t th_sport; /* source port */
|
1840 |
uint16_t th_dport; /* destination port */
|
1841 |
uint32_t th_seq; /* sequence number */
|
1842 |
uint32_t th_ack; /* acknowledgement number */
|
1843 |
uint16_t th_offset_flags; /* data offset, reserved 6 bits, TCP protocol flags */
|
1844 |
uint16_t th_win; /* window */
|
1845 |
uint16_t th_sum; /* checksum */
|
1846 |
uint16_t th_urp; /* urgent pointer */
|
1847 |
} tcp_header; |
1848 |
|
1849 |
typedef struct udp_header |
1850 |
{ |
1851 |
uint16_t uh_sport; /* source port */
|
1852 |
uint16_t uh_dport; /* destination port */
|
1853 |
uint16_t uh_ulen; /* udp length */
|
1854 |
uint16_t uh_sum; /* udp checksum */
|
1855 |
} udp_header; |
1856 |
|
1857 |
typedef struct ip_pseudo_header |
1858 |
{ |
1859 |
uint32_t ip_src; |
1860 |
uint32_t ip_dst; |
1861 |
uint8_t zeros; |
1862 |
uint8_t ip_proto; |
1863 |
uint16_t ip_payload; |
1864 |
} ip_pseudo_header; |
1865 |
|
1866 |
#define IP_PROTO_TCP 6 |
1867 |
#define IP_PROTO_UDP 17 |
1868 |
|
1869 |
#define TCP_HEADER_DATA_OFFSET(tcp) (((be16_to_cpu(tcp->th_offset_flags) >> 12)&0xf) << 2) |
1870 |
#define TCP_FLAGS_ONLY(flags) ((flags)&0x3f) |
1871 |
#define TCP_HEADER_FLAGS(tcp) TCP_FLAGS_ONLY(be16_to_cpu(tcp->th_offset_flags))
|
1872 |
|
1873 |
#define TCP_HEADER_CLEAR_FLAGS(tcp, off) ((tcp)->th_offset_flags &= cpu_to_be16(~TCP_FLAGS_ONLY(off)))
|
1874 |
|
1875 |
#define TCP_FLAG_FIN 0x01 |
1876 |
#define TCP_FLAG_PUSH 0x08 |
1877 |
|
1878 |
/* produces ones' complement sum of data */
|
1879 |
static uint16_t ones_complement_sum(uint8_t *data, size_t len)
|
1880 |
{ |
1881 |
uint32_t result = 0;
|
1882 |
|
1883 |
for (; len > 1; data+=2, len-=2) |
1884 |
{ |
1885 |
result += *(uint16_t*)data; |
1886 |
} |
1887 |
|
1888 |
/* add the remainder byte */
|
1889 |
if (len)
|
1890 |
{ |
1891 |
uint8_t odd[2] = {*data, 0}; |
1892 |
result += *(uint16_t*)odd; |
1893 |
} |
1894 |
|
1895 |
while (result>>16) |
1896 |
result = (result & 0xffff) + (result >> 16); |
1897 |
|
1898 |
return result;
|
1899 |
} |
1900 |
|
1901 |
static uint16_t ip_checksum(void *data, size_t len) |
1902 |
{ |
1903 |
return ~ones_complement_sum((uint8_t*)data, len);
|
1904 |
} |
1905 |
|
1906 |
static int rtl8139_cplus_transmit_one(RTL8139State *s) |
1907 |
{ |
1908 |
if (!rtl8139_transmitter_enabled(s))
|
1909 |
{ |
1910 |
DEBUG_PRINT(("RTL8139: +++ C+ mode: transmitter disabled\n"));
|
1911 |
return 0; |
1912 |
} |
1913 |
|
1914 |
if (!rtl8139_cp_transmitter_enabled(s))
|
1915 |
{ |
1916 |
DEBUG_PRINT(("RTL8139: +++ C+ mode: C+ transmitter disabled\n"));
|
1917 |
return 0 ; |
1918 |
} |
1919 |
|
1920 |
int descriptor = s->currCPlusTxDesc;
|
1921 |
|
1922 |
target_phys_addr_t cplus_tx_ring_desc = |
1923 |
rtl8139_addr64(s->TxAddr[0], s->TxAddr[1]); |
1924 |
|
1925 |
/* Normal priority ring */
|
1926 |
cplus_tx_ring_desc += 16 * descriptor;
|
1927 |
|
1928 |
DEBUG_PRINT(("RTL8139: +++ C+ mode reading TX descriptor %d from host memory at %08x0x%08x = 0x%8lx\n",
|
1929 |
descriptor, s->TxAddr[1], s->TxAddr[0], cplus_tx_ring_desc)); |
1930 |
|
1931 |
uint32_t val, txdw0,txdw1,txbufLO,txbufHI; |
1932 |
|
1933 |
cpu_physical_memory_read(cplus_tx_ring_desc, (uint8_t *)&val, 4);
|
1934 |
txdw0 = le32_to_cpu(val); |
1935 |
/* TODO: implement VLAN tagging support, VLAN tag data is read to txdw1 */
|
1936 |
cpu_physical_memory_read(cplus_tx_ring_desc+4, (uint8_t *)&val, 4); |
1937 |
txdw1 = le32_to_cpu(val); |
1938 |
cpu_physical_memory_read(cplus_tx_ring_desc+8, (uint8_t *)&val, 4); |
1939 |
txbufLO = le32_to_cpu(val); |
1940 |
cpu_physical_memory_read(cplus_tx_ring_desc+12, (uint8_t *)&val, 4); |
1941 |
txbufHI = le32_to_cpu(val); |
1942 |
|
1943 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TX descriptor %d %08x %08x %08x %08x\n",
|
1944 |
descriptor, |
1945 |
txdw0, txdw1, txbufLO, txbufHI)); |
1946 |
|
1947 |
/* TODO: the following discard cast should clean clang analyzer output */
|
1948 |
(void)txdw1;
|
1949 |
|
1950 |
/* w0 ownership flag */
|
1951 |
#define CP_TX_OWN (1<<31) |
1952 |
/* w0 end of ring flag */
|
1953 |
#define CP_TX_EOR (1<<30) |
1954 |
/* first segment of received packet flag */
|
1955 |
#define CP_TX_FS (1<<29) |
1956 |
/* last segment of received packet flag */
|
1957 |
#define CP_TX_LS (1<<28) |
1958 |
/* large send packet flag */
|
1959 |
#define CP_TX_LGSEN (1<<27) |
1960 |
/* large send MSS mask, bits 16...25 */
|
1961 |
#define CP_TC_LGSEN_MSS_MASK ((1 << 12) - 1) |
1962 |
|
1963 |
/* IP checksum offload flag */
|
1964 |
#define CP_TX_IPCS (1<<18) |
1965 |
/* UDP checksum offload flag */
|
1966 |
#define CP_TX_UDPCS (1<<17) |
1967 |
/* TCP checksum offload flag */
|
1968 |
#define CP_TX_TCPCS (1<<16) |
1969 |
|
1970 |
/* w0 bits 0...15 : buffer size */
|
1971 |
#define CP_TX_BUFFER_SIZE (1<<16) |
1972 |
#define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1) |
1973 |
/* w1 tag available flag */
|
1974 |
#define CP_RX_TAGC (1<<17) |
1975 |
/* w1 bits 0...15 : VLAN tag */
|
1976 |
#define CP_TX_VLAN_TAG_MASK ((1<<16) - 1) |
1977 |
/* w2 low 32bit of Rx buffer ptr */
|
1978 |
/* w3 high 32bit of Rx buffer ptr */
|
1979 |
|
1980 |
/* set after transmission */
|
1981 |
/* FIFO underrun flag */
|
1982 |
#define CP_TX_STATUS_UNF (1<<25) |
1983 |
/* transmit error summary flag, valid if set any of three below */
|
1984 |
#define CP_TX_STATUS_TES (1<<23) |
1985 |
/* out-of-window collision flag */
|
1986 |
#define CP_TX_STATUS_OWC (1<<22) |
1987 |
/* link failure flag */
|
1988 |
#define CP_TX_STATUS_LNKF (1<<21) |
1989 |
/* excessive collisions flag */
|
1990 |
#define CP_TX_STATUS_EXC (1<<20) |
1991 |
|
1992 |
if (!(txdw0 & CP_TX_OWN))
|
1993 |
{ |
1994 |
DEBUG_PRINT(("RTL8139: C+ Tx mode : descriptor %d is owned by host\n", descriptor));
|
1995 |
return 0 ; |
1996 |
} |
1997 |
|
1998 |
DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : transmitting from descriptor %d\n", descriptor));
|
1999 |
|
2000 |
if (txdw0 & CP_TX_FS)
|
2001 |
{ |
2002 |
DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : descriptor %d is first segment descriptor\n", descriptor));
|
2003 |
|
2004 |
/* reset internal buffer offset */
|
2005 |
s->cplus_txbuffer_offset = 0;
|
2006 |
} |
2007 |
|
2008 |
int txsize = txdw0 & CP_TX_BUFFER_SIZE_MASK;
|
2009 |
target_phys_addr_t tx_addr = rtl8139_addr64(txbufLO, txbufHI); |
2010 |
|
2011 |
/* make sure we have enough space to assemble the packet */
|
2012 |
if (!s->cplus_txbuffer)
|
2013 |
{ |
2014 |
s->cplus_txbuffer_len = CP_TX_BUFFER_SIZE; |
2015 |
s->cplus_txbuffer = qemu_malloc(s->cplus_txbuffer_len); |
2016 |
s->cplus_txbuffer_offset = 0;
|
2017 |
|
2018 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmission buffer allocated space %d\n", s->cplus_txbuffer_len));
|
2019 |
} |
2020 |
|
2021 |
while (s->cplus_txbuffer && s->cplus_txbuffer_offset + txsize >= s->cplus_txbuffer_len)
|
2022 |
{ |
2023 |
s->cplus_txbuffer_len += CP_TX_BUFFER_SIZE; |
2024 |
s->cplus_txbuffer = qemu_realloc(s->cplus_txbuffer, s->cplus_txbuffer_len); |
2025 |
|
2026 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmission buffer space changed to %d\n", s->cplus_txbuffer_len));
|
2027 |
} |
2028 |
|
2029 |
if (!s->cplus_txbuffer)
|
2030 |
{ |
2031 |
/* out of memory */
|
2032 |
|
2033 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmiter failed to reallocate %d bytes\n", s->cplus_txbuffer_len));
|
2034 |
|
2035 |
/* update tally counter */
|
2036 |
++s->tally_counters.TxERR; |
2037 |
++s->tally_counters.TxAbt; |
2038 |
|
2039 |
return 0; |
2040 |
} |
2041 |
|
2042 |
/* append more data to the packet */
|
2043 |
|
2044 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmit reading %d bytes from host memory at %016" PRIx64 " to offset %d\n", |
2045 |
txsize, (uint64_t)tx_addr, s->cplus_txbuffer_offset)); |
2046 |
|
2047 |
cpu_physical_memory_read(tx_addr, s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize); |
2048 |
s->cplus_txbuffer_offset += txsize; |
2049 |
|
2050 |
/* seek to next Rx descriptor */
|
2051 |
if (txdw0 & CP_TX_EOR)
|
2052 |
{ |
2053 |
s->currCPlusTxDesc = 0;
|
2054 |
} |
2055 |
else
|
2056 |
{ |
2057 |
++s->currCPlusTxDesc; |
2058 |
if (s->currCPlusTxDesc >= 64) |
2059 |
s->currCPlusTxDesc = 0;
|
2060 |
} |
2061 |
|
2062 |
/* transfer ownership to target */
|
2063 |
txdw0 &= ~CP_RX_OWN; |
2064 |
|
2065 |
/* reset error indicator bits */
|
2066 |
txdw0 &= ~CP_TX_STATUS_UNF; |
2067 |
txdw0 &= ~CP_TX_STATUS_TES; |
2068 |
txdw0 &= ~CP_TX_STATUS_OWC; |
2069 |
txdw0 &= ~CP_TX_STATUS_LNKF; |
2070 |
txdw0 &= ~CP_TX_STATUS_EXC; |
2071 |
|
2072 |
/* update ring data */
|
2073 |
val = cpu_to_le32(txdw0); |
2074 |
cpu_physical_memory_write(cplus_tx_ring_desc, (uint8_t *)&val, 4);
|
2075 |
/* TODO: implement VLAN tagging support, VLAN tag data is read to txdw1 */
|
2076 |
// val = cpu_to_le32(txdw1);
|
2077 |
// cpu_physical_memory_write(cplus_tx_ring_desc+4, &val, 4);
|
2078 |
|
2079 |
/* Now decide if descriptor being processed is holding the last segment of packet */
|
2080 |
if (txdw0 & CP_TX_LS)
|
2081 |
{ |
2082 |
DEBUG_PRINT(("RTL8139: +++ C+ Tx mode : descriptor %d is last segment descriptor\n", descriptor));
|
2083 |
|
2084 |
/* can transfer fully assembled packet */
|
2085 |
|
2086 |
uint8_t *saved_buffer = s->cplus_txbuffer; |
2087 |
int saved_size = s->cplus_txbuffer_offset;
|
2088 |
int saved_buffer_len = s->cplus_txbuffer_len;
|
2089 |
|
2090 |
/* reset the card space to protect from recursive call */
|
2091 |
s->cplus_txbuffer = NULL;
|
2092 |
s->cplus_txbuffer_offset = 0;
|
2093 |
s->cplus_txbuffer_len = 0;
|
2094 |
|
2095 |
if (txdw0 & (CP_TX_IPCS | CP_TX_UDPCS | CP_TX_TCPCS | CP_TX_LGSEN))
|
2096 |
{ |
2097 |
DEBUG_PRINT(("RTL8139: +++ C+ mode offloaded task checksum\n"));
|
2098 |
|
2099 |
/* ip packet header */
|
2100 |
ip_header *ip = NULL;
|
2101 |
int hlen = 0; |
2102 |
uint8_t ip_protocol = 0;
|
2103 |
uint16_t ip_data_len = 0;
|
2104 |
|
2105 |
uint8_t *eth_payload_data = NULL;
|
2106 |
size_t eth_payload_len = 0;
|
2107 |
|
2108 |
int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12)); |
2109 |
if (proto == ETH_P_IP)
|
2110 |
{ |
2111 |
DEBUG_PRINT(("RTL8139: +++ C+ mode has IP packet\n"));
|
2112 |
|
2113 |
/* not aligned */
|
2114 |
eth_payload_data = saved_buffer + ETH_HLEN; |
2115 |
eth_payload_len = saved_size - ETH_HLEN; |
2116 |
|
2117 |
ip = (ip_header*)eth_payload_data; |
2118 |
|
2119 |
if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
|
2120 |
DEBUG_PRINT(("RTL8139: +++ C+ mode packet has bad IP version %d expected %d\n", IP_HEADER_VERSION(ip), IP_HEADER_VERSION_4));
|
2121 |
ip = NULL;
|
2122 |
} else {
|
2123 |
hlen = IP_HEADER_LENGTH(ip); |
2124 |
ip_protocol = ip->ip_p; |
2125 |
ip_data_len = be16_to_cpu(ip->ip_len) - hlen; |
2126 |
} |
2127 |
} |
2128 |
|
2129 |
if (ip)
|
2130 |
{ |
2131 |
if (txdw0 & CP_TX_IPCS)
|
2132 |
{ |
2133 |
DEBUG_PRINT(("RTL8139: +++ C+ mode need IP checksum\n"));
|
2134 |
|
2135 |
if (hlen<sizeof(ip_header) || hlen>eth_payload_len) {/* min header length */ |
2136 |
/* bad packet header len */
|
2137 |
/* or packet too short */
|
2138 |
} |
2139 |
else
|
2140 |
{ |
2141 |
ip->ip_sum = 0;
|
2142 |
ip->ip_sum = ip_checksum(ip, hlen); |
2143 |
DEBUG_PRINT(("RTL8139: +++ C+ mode IP header len=%d checksum=%04x\n", hlen, ip->ip_sum));
|
2144 |
} |
2145 |
} |
2146 |
|
2147 |
if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
|
2148 |
{ |
2149 |
#if defined (DEBUG_RTL8139)
|
2150 |
int large_send_mss = (txdw0 >> 16) & CP_TC_LGSEN_MSS_MASK; |
2151 |
#endif
|
2152 |
DEBUG_PRINT(("RTL8139: +++ C+ mode offloaded task TSO MTU=%d IP data %d frame data %d specified MSS=%d\n",
|
2153 |
ETH_MTU, ip_data_len, saved_size - ETH_HLEN, large_send_mss)); |
2154 |
|
2155 |
int tcp_send_offset = 0; |
2156 |
int send_count = 0; |
2157 |
|
2158 |
/* maximum IP header length is 60 bytes */
|
2159 |
uint8_t saved_ip_header[60];
|
2160 |
|
2161 |
/* save IP header template; data area is used in tcp checksum calculation */
|
2162 |
memcpy(saved_ip_header, eth_payload_data, hlen); |
2163 |
|
2164 |
/* a placeholder for checksum calculation routine in tcp case */
|
2165 |
uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
|
2166 |
// size_t data_to_checksum_len = eth_payload_len - hlen + 12;
|
2167 |
|
2168 |
/* pointer to TCP header */
|
2169 |
tcp_header *p_tcp_hdr = (tcp_header*)(eth_payload_data + hlen); |
2170 |
|
2171 |
int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
|
2172 |
|
2173 |
/* ETH_MTU = ip header len + tcp header len + payload */
|
2174 |
int tcp_data_len = ip_data_len - tcp_hlen;
|
2175 |
int tcp_chunk_size = ETH_MTU - hlen - tcp_hlen;
|
2176 |
|
2177 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO IP data len %d TCP hlen %d TCP data len %d TCP chunk size %d\n",
|
2178 |
ip_data_len, tcp_hlen, tcp_data_len, tcp_chunk_size)); |
2179 |
|
2180 |
/* note the cycle below overwrites IP header data,
|
2181 |
but restores it from saved_ip_header before sending packet */
|
2182 |
|
2183 |
int is_last_frame = 0; |
2184 |
|
2185 |
for (tcp_send_offset = 0; tcp_send_offset < tcp_data_len; tcp_send_offset += tcp_chunk_size) |
2186 |
{ |
2187 |
uint16_t chunk_size = tcp_chunk_size; |
2188 |
|
2189 |
/* check if this is the last frame */
|
2190 |
if (tcp_send_offset + tcp_chunk_size >= tcp_data_len)
|
2191 |
{ |
2192 |
is_last_frame = 1;
|
2193 |
chunk_size = tcp_data_len - tcp_send_offset; |
2194 |
} |
2195 |
|
2196 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO TCP seqno %08x\n", be32_to_cpu(p_tcp_hdr->th_seq)));
|
2197 |
|
2198 |
/* add 4 TCP pseudoheader fields */
|
2199 |
/* copy IP source and destination fields */
|
2200 |
memcpy(data_to_checksum, saved_ip_header + 12, 8); |
2201 |
|
2202 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO calculating TCP checksum for packet with %d bytes data\n", tcp_hlen + chunk_size));
|
2203 |
|
2204 |
if (tcp_send_offset)
|
2205 |
{ |
2206 |
memcpy((uint8_t*)p_tcp_hdr + tcp_hlen, (uint8_t*)p_tcp_hdr + tcp_hlen + tcp_send_offset, chunk_size); |
2207 |
} |
2208 |
|
2209 |
/* keep PUSH and FIN flags only for the last frame */
|
2210 |
if (!is_last_frame)
|
2211 |
{ |
2212 |
TCP_HEADER_CLEAR_FLAGS(p_tcp_hdr, TCP_FLAG_PUSH|TCP_FLAG_FIN); |
2213 |
} |
2214 |
|
2215 |
/* recalculate TCP checksum */
|
2216 |
ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum; |
2217 |
p_tcpip_hdr->zeros = 0;
|
2218 |
p_tcpip_hdr->ip_proto = IP_PROTO_TCP; |
2219 |
p_tcpip_hdr->ip_payload = cpu_to_be16(tcp_hlen + chunk_size); |
2220 |
|
2221 |
p_tcp_hdr->th_sum = 0;
|
2222 |
|
2223 |
int tcp_checksum = ip_checksum(data_to_checksum, tcp_hlen + chunk_size + 12); |
2224 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO TCP checksum %04x\n", tcp_checksum));
|
2225 |
|
2226 |
p_tcp_hdr->th_sum = tcp_checksum; |
2227 |
|
2228 |
/* restore IP header */
|
2229 |
memcpy(eth_payload_data, saved_ip_header, hlen); |
2230 |
|
2231 |
/* set IP data length and recalculate IP checksum */
|
2232 |
ip->ip_len = cpu_to_be16(hlen + tcp_hlen + chunk_size); |
2233 |
|
2234 |
/* increment IP id for subsequent frames */
|
2235 |
ip->ip_id = cpu_to_be16(tcp_send_offset/tcp_chunk_size + be16_to_cpu(ip->ip_id)); |
2236 |
|
2237 |
ip->ip_sum = 0;
|
2238 |
ip->ip_sum = ip_checksum(eth_payload_data, hlen); |
2239 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO IP header len=%d checksum=%04x\n", hlen, ip->ip_sum));
|
2240 |
|
2241 |
int tso_send_size = ETH_HLEN + hlen + tcp_hlen + chunk_size;
|
2242 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TSO transferring packet size %d\n", tso_send_size));
|
2243 |
rtl8139_transfer_frame(s, saved_buffer, tso_send_size, 0);
|
2244 |
|
2245 |
/* add transferred count to TCP sequence number */
|
2246 |
p_tcp_hdr->th_seq = cpu_to_be32(chunk_size + be32_to_cpu(p_tcp_hdr->th_seq)); |
2247 |
++send_count; |
2248 |
} |
2249 |
|
2250 |
/* Stop sending this frame */
|
2251 |
saved_size = 0;
|
2252 |
} |
2253 |
else if (txdw0 & (CP_TX_TCPCS|CP_TX_UDPCS)) |
2254 |
{ |
2255 |
DEBUG_PRINT(("RTL8139: +++ C+ mode need TCP or UDP checksum\n"));
|
2256 |
|
2257 |
/* maximum IP header length is 60 bytes */
|
2258 |
uint8_t saved_ip_header[60];
|
2259 |
memcpy(saved_ip_header, eth_payload_data, hlen); |
2260 |
|
2261 |
uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
|
2262 |
// size_t data_to_checksum_len = eth_payload_len - hlen + 12;
|
2263 |
|
2264 |
/* add 4 TCP pseudoheader fields */
|
2265 |
/* copy IP source and destination fields */
|
2266 |
memcpy(data_to_checksum, saved_ip_header + 12, 8); |
2267 |
|
2268 |
if ((txdw0 & CP_TX_TCPCS) && ip_protocol == IP_PROTO_TCP)
|
2269 |
{ |
2270 |
DEBUG_PRINT(("RTL8139: +++ C+ mode calculating TCP checksum for packet with %d bytes data\n", ip_data_len));
|
2271 |
|
2272 |
ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum; |
2273 |
p_tcpip_hdr->zeros = 0;
|
2274 |
p_tcpip_hdr->ip_proto = IP_PROTO_TCP; |
2275 |
p_tcpip_hdr->ip_payload = cpu_to_be16(ip_data_len); |
2276 |
|
2277 |
tcp_header* p_tcp_hdr = (tcp_header *) (data_to_checksum+12);
|
2278 |
|
2279 |
p_tcp_hdr->th_sum = 0;
|
2280 |
|
2281 |
int tcp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12); |
2282 |
DEBUG_PRINT(("RTL8139: +++ C+ mode TCP checksum %04x\n", tcp_checksum));
|
2283 |
|
2284 |
p_tcp_hdr->th_sum = tcp_checksum; |
2285 |
} |
2286 |
else if ((txdw0 & CP_TX_UDPCS) && ip_protocol == IP_PROTO_UDP) |
2287 |
{ |
2288 |
DEBUG_PRINT(("RTL8139: +++ C+ mode calculating UDP checksum for packet with %d bytes data\n", ip_data_len));
|
2289 |
|
2290 |
ip_pseudo_header *p_udpip_hdr = (ip_pseudo_header *)data_to_checksum; |
2291 |
p_udpip_hdr->zeros = 0;
|
2292 |
p_udpip_hdr->ip_proto = IP_PROTO_UDP; |
2293 |
p_udpip_hdr->ip_payload = cpu_to_be16(ip_data_len); |
2294 |
|
2295 |
udp_header *p_udp_hdr = (udp_header *) (data_to_checksum+12);
|
2296 |
|
2297 |
p_udp_hdr->uh_sum = 0;
|
2298 |
|
2299 |
int udp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12); |
2300 |
DEBUG_PRINT(("RTL8139: +++ C+ mode UDP checksum %04x\n", udp_checksum));
|
2301 |
|
2302 |
p_udp_hdr->uh_sum = udp_checksum; |
2303 |
} |
2304 |
|
2305 |
/* restore IP header */
|
2306 |
memcpy(eth_payload_data, saved_ip_header, hlen); |
2307 |
} |
2308 |
} |
2309 |
} |
2310 |
|
2311 |
/* update tally counter */
|
2312 |
++s->tally_counters.TxOk; |
2313 |
|
2314 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmitting %d bytes packet\n", saved_size));
|
2315 |
|
2316 |
rtl8139_transfer_frame(s, saved_buffer, saved_size, 1);
|
2317 |
|
2318 |
/* restore card space if there was no recursion and reset offset */
|
2319 |
if (!s->cplus_txbuffer)
|
2320 |
{ |
2321 |
s->cplus_txbuffer = saved_buffer; |
2322 |
s->cplus_txbuffer_len = saved_buffer_len; |
2323 |
s->cplus_txbuffer_offset = 0;
|
2324 |
} |
2325 |
else
|
2326 |
{ |
2327 |
qemu_free(saved_buffer); |
2328 |
} |
2329 |
} |
2330 |
else
|
2331 |
{ |
2332 |
DEBUG_PRINT(("RTL8139: +++ C+ mode transmission continue to next descriptor\n"));
|
2333 |
} |
2334 |
|
2335 |
return 1; |
2336 |
} |
2337 |
|
2338 |
static void rtl8139_cplus_transmit(RTL8139State *s) |
2339 |
{ |
2340 |
int txcount = 0; |
2341 |
|
2342 |
while (rtl8139_cplus_transmit_one(s))
|
2343 |
{ |
2344 |
++txcount; |
2345 |
} |
2346 |
|
2347 |
/* Mark transfer completed */
|
2348 |
if (!txcount)
|
2349 |
{ |
2350 |
DEBUG_PRINT(("RTL8139: C+ mode : transmitter queue stalled, current TxDesc = %d\n",
|
2351 |
s->currCPlusTxDesc)); |
2352 |
} |
2353 |
else
|
2354 |
{ |
2355 |
/* update interrupt status */
|
2356 |
s->IntrStatus |= TxOK; |
2357 |
rtl8139_update_irq(s); |
2358 |
} |
2359 |
} |
2360 |
|
2361 |
static void rtl8139_transmit(RTL8139State *s) |
2362 |
{ |
2363 |
int descriptor = s->currTxDesc, txcount = 0; |
2364 |
|
2365 |
/*while*/
|
2366 |
if (rtl8139_transmit_one(s, descriptor))
|
2367 |
{ |
2368 |
++s->currTxDesc; |
2369 |
s->currTxDesc %= 4;
|
2370 |
++txcount; |
2371 |
} |
2372 |
|
2373 |
/* Mark transfer completed */
|
2374 |
if (!txcount)
|
2375 |
{ |
2376 |
DEBUG_PRINT(("RTL8139: transmitter queue stalled, current TxDesc = %d\n", s->currTxDesc));
|
2377 |
} |
2378 |
} |
2379 |
|
2380 |
static void rtl8139_TxStatus_write(RTL8139State *s, uint32_t txRegOffset, uint32_t val) |
2381 |
{ |
2382 |
|
2383 |
int descriptor = txRegOffset/4; |
2384 |
|
2385 |
/* handle C+ transmit mode register configuration */
|
2386 |
|
2387 |
if (s->cplus_enabled)
|
2388 |
{ |
2389 |
DEBUG_PRINT(("RTL8139C+ DTCCR write offset=0x%x val=0x%08x descriptor=%d\n", txRegOffset, val, descriptor));
|
2390 |
|
2391 |
/* handle Dump Tally Counters command */
|
2392 |
s->TxStatus[descriptor] = val; |
2393 |
|
2394 |
if (descriptor == 0 && (val & 0x8)) |
2395 |
{ |
2396 |
target_phys_addr_t tc_addr = rtl8139_addr64(s->TxStatus[0] & ~0x3f, s->TxStatus[1]); |
2397 |
|
2398 |
/* dump tally counters to specified memory location */
|
2399 |
RTL8139TallyCounters_physical_memory_write( tc_addr, &s->tally_counters); |
2400 |
|
2401 |
/* mark dump completed */
|
2402 |
s->TxStatus[0] &= ~0x8; |
2403 |
} |
2404 |
|
2405 |
return;
|
2406 |
} |
2407 |
|
2408 |
DEBUG_PRINT(("RTL8139: TxStatus write offset=0x%x val=0x%08x descriptor=%d\n", txRegOffset, val, descriptor));
|
2409 |
|
2410 |
/* mask only reserved bits */
|
2411 |
val &= ~0xff00c000; /* these bits are reset on write */ |
2412 |
val = SET_MASKED(val, 0x00c00000, s->TxStatus[descriptor]);
|
2413 |
|
2414 |
s->TxStatus[descriptor] = val; |
2415 |
|
2416 |
/* attempt to start transmission */
|
2417 |
rtl8139_transmit(s); |
2418 |
} |
2419 |
|
2420 |
static uint32_t rtl8139_TxStatus_read(RTL8139State *s, uint32_t txRegOffset)
|
2421 |
{ |
2422 |
uint32_t ret = s->TxStatus[txRegOffset/4];
|
2423 |
|
2424 |
DEBUG_PRINT(("RTL8139: TxStatus read offset=0x%x val=0x%08x\n", txRegOffset, ret));
|
2425 |
|
2426 |
return ret;
|
2427 |
} |
2428 |
|
2429 |
static uint16_t rtl8139_TSAD_read(RTL8139State *s)
|
2430 |
{ |
2431 |
uint16_t ret = 0;
|
2432 |
|
2433 |
/* Simulate TSAD, it is read only anyway */
|
2434 |
|
2435 |
ret = ((s->TxStatus[3] & TxStatOK )?TSAD_TOK3:0) |
2436 |
|((s->TxStatus[2] & TxStatOK )?TSAD_TOK2:0) |
2437 |
|((s->TxStatus[1] & TxStatOK )?TSAD_TOK1:0) |
2438 |
|((s->TxStatus[0] & TxStatOK )?TSAD_TOK0:0) |
2439 |
|
2440 |
|((s->TxStatus[3] & TxUnderrun)?TSAD_TUN3:0) |
2441 |
|((s->TxStatus[2] & TxUnderrun)?TSAD_TUN2:0) |
2442 |
|((s->TxStatus[1] & TxUnderrun)?TSAD_TUN1:0) |
2443 |
|((s->TxStatus[0] & TxUnderrun)?TSAD_TUN0:0) |
2444 |
|
2445 |
|((s->TxStatus[3] & TxAborted )?TSAD_TABT3:0) |
2446 |
|((s->TxStatus[2] & TxAborted )?TSAD_TABT2:0) |
2447 |
|((s->TxStatus[1] & TxAborted )?TSAD_TABT1:0) |
2448 |
|((s->TxStatus[0] & TxAborted )?TSAD_TABT0:0) |
2449 |
|
2450 |
|((s->TxStatus[3] & TxHostOwns )?TSAD_OWN3:0) |
2451 |
|((s->TxStatus[2] & TxHostOwns )?TSAD_OWN2:0) |
2452 |
|((s->TxStatus[1] & TxHostOwns )?TSAD_OWN1:0) |
2453 |
|((s->TxStatus[0] & TxHostOwns )?TSAD_OWN0:0) ; |
2454 |
|
2455 |
|
2456 |
DEBUG_PRINT(("RTL8139: TSAD read val=0x%04x\n", ret));
|
2457 |
|
2458 |
return ret;
|
2459 |
} |
2460 |
|
2461 |
static uint16_t rtl8139_CSCR_read(RTL8139State *s)
|
2462 |
{ |
2463 |
uint16_t ret = s->CSCR; |
2464 |
|
2465 |
DEBUG_PRINT(("RTL8139: CSCR read val=0x%04x\n", ret));
|
2466 |
|
2467 |
return ret;
|
2468 |
} |
2469 |
|
2470 |
static void rtl8139_TxAddr_write(RTL8139State *s, uint32_t txAddrOffset, uint32_t val) |
2471 |
{ |
2472 |
DEBUG_PRINT(("RTL8139: TxAddr write offset=0x%x val=0x%08x\n", txAddrOffset, val));
|
2473 |
|
2474 |
s->TxAddr[txAddrOffset/4] = val;
|
2475 |
} |
2476 |
|
2477 |
static uint32_t rtl8139_TxAddr_read(RTL8139State *s, uint32_t txAddrOffset)
|
2478 |
{ |
2479 |
uint32_t ret = s->TxAddr[txAddrOffset/4];
|
2480 |
|
2481 |
DEBUG_PRINT(("RTL8139: TxAddr read offset=0x%x val=0x%08x\n", txAddrOffset, ret));
|
2482 |
|
2483 |
return ret;
|
2484 |
} |
2485 |
|
2486 |
static void rtl8139_RxBufPtr_write(RTL8139State *s, uint32_t val) |
2487 |
{ |
2488 |
DEBUG_PRINT(("RTL8139: RxBufPtr write val=0x%04x\n", val));
|
2489 |
|
2490 |
/* this value is off by 16 */
|
2491 |
s->RxBufPtr = MOD2(val + 0x10, s->RxBufferSize);
|
2492 |
|
2493 |
DEBUG_PRINT((" CAPR write: rx buffer length %d head 0x%04x read 0x%04x\n",
|
2494 |
s->RxBufferSize, s->RxBufAddr, s->RxBufPtr)); |
2495 |
} |
2496 |
|
2497 |
static uint32_t rtl8139_RxBufPtr_read(RTL8139State *s)
|
2498 |
{ |
2499 |
/* this value is off by 16 */
|
2500 |
uint32_t ret = s->RxBufPtr - 0x10;
|
2501 |
|
2502 |
DEBUG_PRINT(("RTL8139: RxBufPtr read val=0x%04x\n", ret));
|
2503 |
|
2504 |
return ret;
|
2505 |
} |
2506 |
|
2507 |
static uint32_t rtl8139_RxBufAddr_read(RTL8139State *s)
|
2508 |
{ |
2509 |
/* this value is NOT off by 16 */
|
2510 |
uint32_t ret = s->RxBufAddr; |
2511 |
|
2512 |
DEBUG_PRINT(("RTL8139: RxBufAddr read val=0x%04x\n", ret));
|
2513 |
|
2514 |
return ret;
|
2515 |
} |
2516 |
|
2517 |
static void rtl8139_RxBuf_write(RTL8139State *s, uint32_t val) |
2518 |
{ |
2519 |
DEBUG_PRINT(("RTL8139: RxBuf write val=0x%08x\n", val));
|
2520 |
|
2521 |
s->RxBuf = val; |
2522 |
|
2523 |
/* may need to reset rxring here */
|
2524 |
} |
2525 |
|
2526 |
static uint32_t rtl8139_RxBuf_read(RTL8139State *s)
|
2527 |
{ |
2528 |
uint32_t ret = s->RxBuf; |
2529 |
|
2530 |
DEBUG_PRINT(("RTL8139: RxBuf read val=0x%08x\n", ret));
|
2531 |
|
2532 |
return ret;
|
2533 |
} |
2534 |
|
2535 |
static void rtl8139_IntrMask_write(RTL8139State *s, uint32_t val) |
2536 |
{ |
2537 |
DEBUG_PRINT(("RTL8139: IntrMask write(w) val=0x%04x\n", val));
|
2538 |
|
2539 |
/* mask unwriteable bits */
|
2540 |
val = SET_MASKED(val, 0x1e00, s->IntrMask);
|
2541 |
|
2542 |
s->IntrMask = val; |
2543 |
|
2544 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
2545 |
rtl8139_update_irq(s); |
2546 |
|
2547 |
} |
2548 |
|
2549 |
static uint32_t rtl8139_IntrMask_read(RTL8139State *s)
|
2550 |
{ |
2551 |
uint32_t ret = s->IntrMask; |
2552 |
|
2553 |
DEBUG_PRINT(("RTL8139: IntrMask read(w) val=0x%04x\n", ret));
|
2554 |
|
2555 |
return ret;
|
2556 |
} |
2557 |
|
2558 |
static void rtl8139_IntrStatus_write(RTL8139State *s, uint32_t val) |
2559 |
{ |
2560 |
DEBUG_PRINT(("RTL8139: IntrStatus write(w) val=0x%04x\n", val));
|
2561 |
|
2562 |
#if 0
|
2563 |
|
2564 |
/* writing to ISR has no effect */
|
2565 |
|
2566 |
return;
|
2567 |
|
2568 |
#else
|
2569 |
uint16_t newStatus = s->IntrStatus & ~val; |
2570 |
|
2571 |
/* mask unwriteable bits */
|
2572 |
newStatus = SET_MASKED(newStatus, 0x1e00, s->IntrStatus);
|
2573 |
|
2574 |
/* writing 1 to interrupt status register bit clears it */
|
2575 |
s->IntrStatus = 0;
|
2576 |
rtl8139_update_irq(s); |
2577 |
|
2578 |
s->IntrStatus = newStatus; |
2579 |
/*
|
2580 |
* Computing if we miss an interrupt here is not that correct but
|
2581 |
* considered that we should have had already an interrupt
|
2582 |
* and probably emulated is slower is better to assume this resetting was
|
2583 |
* done before testing on previous rtl8139_update_irq lead to IRQ loosing
|
2584 |
*/
|
2585 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
2586 |
rtl8139_update_irq(s); |
2587 |
|
2588 |
#endif
|
2589 |
} |
2590 |
|
2591 |
static uint32_t rtl8139_IntrStatus_read(RTL8139State *s)
|
2592 |
{ |
2593 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
2594 |
|
2595 |
uint32_t ret = s->IntrStatus; |
2596 |
|
2597 |
DEBUG_PRINT(("RTL8139: IntrStatus read(w) val=0x%04x\n", ret));
|
2598 |
|
2599 |
#if 0
|
2600 |
|
2601 |
/* reading ISR clears all interrupts */
|
2602 |
s->IntrStatus = 0;
|
2603 |
|
2604 |
rtl8139_update_irq(s);
|
2605 |
|
2606 |
#endif
|
2607 |
|
2608 |
return ret;
|
2609 |
} |
2610 |
|
2611 |
static void rtl8139_MultiIntr_write(RTL8139State *s, uint32_t val) |
2612 |
{ |
2613 |
DEBUG_PRINT(("RTL8139: MultiIntr write(w) val=0x%04x\n", val));
|
2614 |
|
2615 |
/* mask unwriteable bits */
|
2616 |
val = SET_MASKED(val, 0xf000, s->MultiIntr);
|
2617 |
|
2618 |
s->MultiIntr = val; |
2619 |
} |
2620 |
|
2621 |
static uint32_t rtl8139_MultiIntr_read(RTL8139State *s)
|
2622 |
{ |
2623 |
uint32_t ret = s->MultiIntr; |
2624 |
|
2625 |
DEBUG_PRINT(("RTL8139: MultiIntr read(w) val=0x%04x\n", ret));
|
2626 |
|
2627 |
return ret;
|
2628 |
} |
2629 |
|
2630 |
static void rtl8139_io_writeb(void *opaque, uint8_t addr, uint32_t val) |
2631 |
{ |
2632 |
RTL8139State *s = opaque; |
2633 |
|
2634 |
addr &= 0xff;
|
2635 |
|
2636 |
switch (addr)
|
2637 |
{ |
2638 |
case MAC0 ... MAC0+5: |
2639 |
s->phys[addr - MAC0] = val; |
2640 |
break;
|
2641 |
case MAC0+6 ... MAC0+7: |
2642 |
/* reserved */
|
2643 |
break;
|
2644 |
case MAR0 ... MAR0+7: |
2645 |
s->mult[addr - MAR0] = val; |
2646 |
break;
|
2647 |
case ChipCmd:
|
2648 |
rtl8139_ChipCmd_write(s, val); |
2649 |
break;
|
2650 |
case Cfg9346:
|
2651 |
rtl8139_Cfg9346_write(s, val); |
2652 |
break;
|
2653 |
case TxConfig: /* windows driver sometimes writes using byte-lenth call */ |
2654 |
rtl8139_TxConfig_writeb(s, val); |
2655 |
break;
|
2656 |
case Config0:
|
2657 |
rtl8139_Config0_write(s, val); |
2658 |
break;
|
2659 |
case Config1:
|
2660 |
rtl8139_Config1_write(s, val); |
2661 |
break;
|
2662 |
case Config3:
|
2663 |
rtl8139_Config3_write(s, val); |
2664 |
break;
|
2665 |
case Config4:
|
2666 |
rtl8139_Config4_write(s, val); |
2667 |
break;
|
2668 |
case Config5:
|
2669 |
rtl8139_Config5_write(s, val); |
2670 |
break;
|
2671 |
case MediaStatus:
|
2672 |
/* ignore */
|
2673 |
DEBUG_PRINT(("RTL8139: not implemented write(b) to MediaStatus val=0x%02x\n", val));
|
2674 |
break;
|
2675 |
|
2676 |
case HltClk:
|
2677 |
DEBUG_PRINT(("RTL8139: HltClk write val=0x%08x\n", val));
|
2678 |
if (val == 'R') |
2679 |
{ |
2680 |
s->clock_enabled = 1;
|
2681 |
} |
2682 |
else if (val == 'H') |
2683 |
{ |
2684 |
s->clock_enabled = 0;
|
2685 |
} |
2686 |
break;
|
2687 |
|
2688 |
case TxThresh:
|
2689 |
DEBUG_PRINT(("RTL8139C+ TxThresh write(b) val=0x%02x\n", val));
|
2690 |
s->TxThresh = val; |
2691 |
break;
|
2692 |
|
2693 |
case TxPoll:
|
2694 |
DEBUG_PRINT(("RTL8139C+ TxPoll write(b) val=0x%02x\n", val));
|
2695 |
if (val & (1 << 7)) |
2696 |
{ |
2697 |
DEBUG_PRINT(("RTL8139C+ TxPoll high priority transmission (not implemented)\n"));
|
2698 |
//rtl8139_cplus_transmit(s);
|
2699 |
} |
2700 |
if (val & (1 << 6)) |
2701 |
{ |
2702 |
DEBUG_PRINT(("RTL8139C+ TxPoll normal priority transmission\n"));
|
2703 |
rtl8139_cplus_transmit(s); |
2704 |
} |
2705 |
|
2706 |
break;
|
2707 |
|
2708 |
default:
|
2709 |
DEBUG_PRINT(("RTL8139: not implemented write(b) addr=0x%x val=0x%02x\n", addr, val));
|
2710 |
break;
|
2711 |
} |
2712 |
} |
2713 |
|
2714 |
static void rtl8139_io_writew(void *opaque, uint8_t addr, uint32_t val) |
2715 |
{ |
2716 |
RTL8139State *s = opaque; |
2717 |
|
2718 |
addr &= 0xfe;
|
2719 |
|
2720 |
switch (addr)
|
2721 |
{ |
2722 |
case IntrMask:
|
2723 |
rtl8139_IntrMask_write(s, val); |
2724 |
break;
|
2725 |
|
2726 |
case IntrStatus:
|
2727 |
rtl8139_IntrStatus_write(s, val); |
2728 |
break;
|
2729 |
|
2730 |
case MultiIntr:
|
2731 |
rtl8139_MultiIntr_write(s, val); |
2732 |
break;
|
2733 |
|
2734 |
case RxBufPtr:
|
2735 |
rtl8139_RxBufPtr_write(s, val); |
2736 |
break;
|
2737 |
|
2738 |
case BasicModeCtrl:
|
2739 |
rtl8139_BasicModeCtrl_write(s, val); |
2740 |
break;
|
2741 |
case BasicModeStatus:
|
2742 |
rtl8139_BasicModeStatus_write(s, val); |
2743 |
break;
|
2744 |
case NWayAdvert:
|
2745 |
DEBUG_PRINT(("RTL8139: NWayAdvert write(w) val=0x%04x\n", val));
|
2746 |
s->NWayAdvert = val; |
2747 |
break;
|
2748 |
case NWayLPAR:
|
2749 |
DEBUG_PRINT(("RTL8139: forbidden NWayLPAR write(w) val=0x%04x\n", val));
|
2750 |
break;
|
2751 |
case NWayExpansion:
|
2752 |
DEBUG_PRINT(("RTL8139: NWayExpansion write(w) val=0x%04x\n", val));
|
2753 |
s->NWayExpansion = val; |
2754 |
break;
|
2755 |
|
2756 |
case CpCmd:
|
2757 |
rtl8139_CpCmd_write(s, val); |
2758 |
break;
|
2759 |
|
2760 |
case IntrMitigate:
|
2761 |
rtl8139_IntrMitigate_write(s, val); |
2762 |
break;
|
2763 |
|
2764 |
default:
|
2765 |
DEBUG_PRINT(("RTL8139: ioport write(w) addr=0x%x val=0x%04x via write(b)\n", addr, val));
|
2766 |
|
2767 |
rtl8139_io_writeb(opaque, addr, val & 0xff);
|
2768 |
rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff); |
2769 |
break;
|
2770 |
} |
2771 |
} |
2772 |
|
2773 |
static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time) |
2774 |
{ |
2775 |
int64_t pci_time, next_time; |
2776 |
uint32_t low_pci; |
2777 |
|
2778 |
DEBUG_PRINT(("RTL8139: entered rtl8139_set_next_tctr_time\n"));
|
2779 |
|
2780 |
if (s->TimerExpire && current_time >= s->TimerExpire) {
|
2781 |
s->IntrStatus |= PCSTimeout; |
2782 |
rtl8139_update_irq(s); |
2783 |
} |
2784 |
|
2785 |
/* Set QEMU timer only if needed that is
|
2786 |
* - TimerInt <> 0 (we have a timer)
|
2787 |
* - mask = 1 (we want an interrupt timer)
|
2788 |
* - irq = 0 (irq is not already active)
|
2789 |
* If any of above change we need to compute timer again
|
2790 |
* Also we must check if timer is passed without QEMU timer
|
2791 |
*/
|
2792 |
s->TimerExpire = 0;
|
2793 |
if (!s->TimerInt) {
|
2794 |
return;
|
2795 |
} |
2796 |
|
2797 |
pci_time = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY, |
2798 |
get_ticks_per_sec()); |
2799 |
low_pci = pci_time & 0xffffffff;
|
2800 |
pci_time = pci_time - low_pci + s->TimerInt; |
2801 |
if (low_pci >= s->TimerInt) {
|
2802 |
pci_time += 0x100000000LL;
|
2803 |
} |
2804 |
next_time = s->TCTR_base + muldiv64(pci_time, get_ticks_per_sec(), |
2805 |
PCI_FREQUENCY); |
2806 |
s->TimerExpire = next_time; |
2807 |
|
2808 |
if ((s->IntrMask & PCSTimeout) != 0 && (s->IntrStatus & PCSTimeout) == 0) { |
2809 |
qemu_mod_timer(s->timer, next_time); |
2810 |
} |
2811 |
} |
2812 |
|
2813 |
static void rtl8139_io_writel(void *opaque, uint8_t addr, uint32_t val) |
2814 |
{ |
2815 |
RTL8139State *s = opaque; |
2816 |
|
2817 |
addr &= 0xfc;
|
2818 |
|
2819 |
switch (addr)
|
2820 |
{ |
2821 |
case RxMissed:
|
2822 |
DEBUG_PRINT(("RTL8139: RxMissed clearing on write\n"));
|
2823 |
s->RxMissed = 0;
|
2824 |
break;
|
2825 |
|
2826 |
case TxConfig:
|
2827 |
rtl8139_TxConfig_write(s, val); |
2828 |
break;
|
2829 |
|
2830 |
case RxConfig:
|
2831 |
rtl8139_RxConfig_write(s, val); |
2832 |
break;
|
2833 |
|
2834 |
case TxStatus0 ... TxStatus0+4*4-1: |
2835 |
rtl8139_TxStatus_write(s, addr-TxStatus0, val); |
2836 |
break;
|
2837 |
|
2838 |
case TxAddr0 ... TxAddr0+4*4-1: |
2839 |
rtl8139_TxAddr_write(s, addr-TxAddr0, val); |
2840 |
break;
|
2841 |
|
2842 |
case RxBuf:
|
2843 |
rtl8139_RxBuf_write(s, val); |
2844 |
break;
|
2845 |
|
2846 |
case RxRingAddrLO:
|
2847 |
DEBUG_PRINT(("RTL8139: C+ RxRing low bits write val=0x%08x\n", val));
|
2848 |
s->RxRingAddrLO = val; |
2849 |
break;
|
2850 |
|
2851 |
case RxRingAddrHI:
|
2852 |
DEBUG_PRINT(("RTL8139: C+ RxRing high bits write val=0x%08x\n", val));
|
2853 |
s->RxRingAddrHI = val; |
2854 |
break;
|
2855 |
|
2856 |
case Timer:
|
2857 |
DEBUG_PRINT(("RTL8139: TCTR Timer reset on write\n"));
|
2858 |
s->TCTR_base = qemu_get_clock_ns(vm_clock); |
2859 |
rtl8139_set_next_tctr_time(s, s->TCTR_base); |
2860 |
break;
|
2861 |
|
2862 |
case FlashReg:
|
2863 |
DEBUG_PRINT(("RTL8139: FlashReg TimerInt write val=0x%08x\n", val));
|
2864 |
if (s->TimerInt != val) {
|
2865 |
s->TimerInt = val; |
2866 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
2867 |
} |
2868 |
break;
|
2869 |
|
2870 |
default:
|
2871 |
DEBUG_PRINT(("RTL8139: ioport write(l) addr=0x%x val=0x%08x via write(b)\n", addr, val));
|
2872 |
rtl8139_io_writeb(opaque, addr, val & 0xff);
|
2873 |
rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff); |
2874 |
rtl8139_io_writeb(opaque, addr + 2, (val >> 16) & 0xff); |
2875 |
rtl8139_io_writeb(opaque, addr + 3, (val >> 24) & 0xff); |
2876 |
break;
|
2877 |
} |
2878 |
} |
2879 |
|
2880 |
static uint32_t rtl8139_io_readb(void *opaque, uint8_t addr) |
2881 |
{ |
2882 |
RTL8139State *s = opaque; |
2883 |
int ret;
|
2884 |
|
2885 |
addr &= 0xff;
|
2886 |
|
2887 |
switch (addr)
|
2888 |
{ |
2889 |
case MAC0 ... MAC0+5: |
2890 |
ret = s->phys[addr - MAC0]; |
2891 |
break;
|
2892 |
case MAC0+6 ... MAC0+7: |
2893 |
ret = 0;
|
2894 |
break;
|
2895 |
case MAR0 ... MAR0+7: |
2896 |
ret = s->mult[addr - MAR0]; |
2897 |
break;
|
2898 |
case ChipCmd:
|
2899 |
ret = rtl8139_ChipCmd_read(s); |
2900 |
break;
|
2901 |
case Cfg9346:
|
2902 |
ret = rtl8139_Cfg9346_read(s); |
2903 |
break;
|
2904 |
case Config0:
|
2905 |
ret = rtl8139_Config0_read(s); |
2906 |
break;
|
2907 |
case Config1:
|
2908 |
ret = rtl8139_Config1_read(s); |
2909 |
break;
|
2910 |
case Config3:
|
2911 |
ret = rtl8139_Config3_read(s); |
2912 |
break;
|
2913 |
case Config4:
|
2914 |
ret = rtl8139_Config4_read(s); |
2915 |
break;
|
2916 |
case Config5:
|
2917 |
ret = rtl8139_Config5_read(s); |
2918 |
break;
|
2919 |
|
2920 |
case MediaStatus:
|
2921 |
ret = 0xd0;
|
2922 |
DEBUG_PRINT(("RTL8139: MediaStatus read 0x%x\n", ret));
|
2923 |
break;
|
2924 |
|
2925 |
case HltClk:
|
2926 |
ret = s->clock_enabled; |
2927 |
DEBUG_PRINT(("RTL8139: HltClk read 0x%x\n", ret));
|
2928 |
break;
|
2929 |
|
2930 |
case PCIRevisionID:
|
2931 |
ret = RTL8139_PCI_REVID; |
2932 |
DEBUG_PRINT(("RTL8139: PCI Revision ID read 0x%x\n", ret));
|
2933 |
break;
|
2934 |
|
2935 |
case TxThresh:
|
2936 |
ret = s->TxThresh; |
2937 |
DEBUG_PRINT(("RTL8139C+ TxThresh read(b) val=0x%02x\n", ret));
|
2938 |
break;
|
2939 |
|
2940 |
case 0x43: /* Part of TxConfig register. Windows driver tries to read it */ |
2941 |
ret = s->TxConfig >> 24;
|
2942 |
DEBUG_PRINT(("RTL8139C TxConfig at 0x43 read(b) val=0x%02x\n", ret));
|
2943 |
break;
|
2944 |
|
2945 |
default:
|
2946 |
DEBUG_PRINT(("RTL8139: not implemented read(b) addr=0x%x\n", addr));
|
2947 |
ret = 0;
|
2948 |
break;
|
2949 |
} |
2950 |
|
2951 |
return ret;
|
2952 |
} |
2953 |
|
2954 |
static uint32_t rtl8139_io_readw(void *opaque, uint8_t addr) |
2955 |
{ |
2956 |
RTL8139State *s = opaque; |
2957 |
uint32_t ret; |
2958 |
|
2959 |
addr &= 0xfe; /* mask lower bit */ |
2960 |
|
2961 |
switch (addr)
|
2962 |
{ |
2963 |
case IntrMask:
|
2964 |
ret = rtl8139_IntrMask_read(s); |
2965 |
break;
|
2966 |
|
2967 |
case IntrStatus:
|
2968 |
ret = rtl8139_IntrStatus_read(s); |
2969 |
break;
|
2970 |
|
2971 |
case MultiIntr:
|
2972 |
ret = rtl8139_MultiIntr_read(s); |
2973 |
break;
|
2974 |
|
2975 |
case RxBufPtr:
|
2976 |
ret = rtl8139_RxBufPtr_read(s); |
2977 |
break;
|
2978 |
|
2979 |
case RxBufAddr:
|
2980 |
ret = rtl8139_RxBufAddr_read(s); |
2981 |
break;
|
2982 |
|
2983 |
case BasicModeCtrl:
|
2984 |
ret = rtl8139_BasicModeCtrl_read(s); |
2985 |
break;
|
2986 |
case BasicModeStatus:
|
2987 |
ret = rtl8139_BasicModeStatus_read(s); |
2988 |
break;
|
2989 |
case NWayAdvert:
|
2990 |
ret = s->NWayAdvert; |
2991 |
DEBUG_PRINT(("RTL8139: NWayAdvert read(w) val=0x%04x\n", ret));
|
2992 |
break;
|
2993 |
case NWayLPAR:
|
2994 |
ret = s->NWayLPAR; |
2995 |
DEBUG_PRINT(("RTL8139: NWayLPAR read(w) val=0x%04x\n", ret));
|
2996 |
break;
|
2997 |
case NWayExpansion:
|
2998 |
ret = s->NWayExpansion; |
2999 |
DEBUG_PRINT(("RTL8139: NWayExpansion read(w) val=0x%04x\n", ret));
|
3000 |
break;
|
3001 |
|
3002 |
case CpCmd:
|
3003 |
ret = rtl8139_CpCmd_read(s); |
3004 |
break;
|
3005 |
|
3006 |
case IntrMitigate:
|
3007 |
ret = rtl8139_IntrMitigate_read(s); |
3008 |
break;
|
3009 |
|
3010 |
case TxSummary:
|
3011 |
ret = rtl8139_TSAD_read(s); |
3012 |
break;
|
3013 |
|
3014 |
case CSCR:
|
3015 |
ret = rtl8139_CSCR_read(s); |
3016 |
break;
|
3017 |
|
3018 |
default:
|
3019 |
DEBUG_PRINT(("RTL8139: ioport read(w) addr=0x%x via read(b)\n", addr));
|
3020 |
|
3021 |
ret = rtl8139_io_readb(opaque, addr); |
3022 |
ret |= rtl8139_io_readb(opaque, addr + 1) << 8; |
3023 |
|
3024 |
DEBUG_PRINT(("RTL8139: ioport read(w) addr=0x%x val=0x%04x\n", addr, ret));
|
3025 |
break;
|
3026 |
} |
3027 |
|
3028 |
return ret;
|
3029 |
} |
3030 |
|
3031 |
static uint32_t rtl8139_io_readl(void *opaque, uint8_t addr) |
3032 |
{ |
3033 |
RTL8139State *s = opaque; |
3034 |
uint32_t ret; |
3035 |
|
3036 |
addr &= 0xfc; /* also mask low 2 bits */ |
3037 |
|
3038 |
switch (addr)
|
3039 |
{ |
3040 |
case RxMissed:
|
3041 |
ret = s->RxMissed; |
3042 |
|
3043 |
DEBUG_PRINT(("RTL8139: RxMissed read val=0x%08x\n", ret));
|
3044 |
break;
|
3045 |
|
3046 |
case TxConfig:
|
3047 |
ret = rtl8139_TxConfig_read(s); |
3048 |
break;
|
3049 |
|
3050 |
case RxConfig:
|
3051 |
ret = rtl8139_RxConfig_read(s); |
3052 |
break;
|
3053 |
|
3054 |
case TxStatus0 ... TxStatus0+4*4-1: |
3055 |
ret = rtl8139_TxStatus_read(s, addr-TxStatus0); |
3056 |
break;
|
3057 |
|
3058 |
case TxAddr0 ... TxAddr0+4*4-1: |
3059 |
ret = rtl8139_TxAddr_read(s, addr-TxAddr0); |
3060 |
break;
|
3061 |
|
3062 |
case RxBuf:
|
3063 |
ret = rtl8139_RxBuf_read(s); |
3064 |
break;
|
3065 |
|
3066 |
case RxRingAddrLO:
|
3067 |
ret = s->RxRingAddrLO; |
3068 |
DEBUG_PRINT(("RTL8139: C+ RxRing low bits read val=0x%08x\n", ret));
|
3069 |
break;
|
3070 |
|
3071 |
case RxRingAddrHI:
|
3072 |
ret = s->RxRingAddrHI; |
3073 |
DEBUG_PRINT(("RTL8139: C+ RxRing high bits read val=0x%08x\n", ret));
|
3074 |
break;
|
3075 |
|
3076 |
case Timer:
|
3077 |
ret = muldiv64(qemu_get_clock_ns(vm_clock) - s->TCTR_base, |
3078 |
PCI_FREQUENCY, get_ticks_per_sec()); |
3079 |
DEBUG_PRINT(("RTL8139: TCTR Timer read val=0x%08x\n", ret));
|
3080 |
break;
|
3081 |
|
3082 |
case FlashReg:
|
3083 |
ret = s->TimerInt; |
3084 |
DEBUG_PRINT(("RTL8139: FlashReg TimerInt read val=0x%08x\n", ret));
|
3085 |
break;
|
3086 |
|
3087 |
default:
|
3088 |
DEBUG_PRINT(("RTL8139: ioport read(l) addr=0x%x via read(b)\n", addr));
|
3089 |
|
3090 |
ret = rtl8139_io_readb(opaque, addr); |
3091 |
ret |= rtl8139_io_readb(opaque, addr + 1) << 8; |
3092 |
ret |= rtl8139_io_readb(opaque, addr + 2) << 16; |
3093 |
ret |= rtl8139_io_readb(opaque, addr + 3) << 24; |
3094 |
|
3095 |
DEBUG_PRINT(("RTL8139: read(l) addr=0x%x val=%08x\n", addr, ret));
|
3096 |
break;
|
3097 |
} |
3098 |
|
3099 |
return ret;
|
3100 |
} |
3101 |
|
3102 |
/* */
|
3103 |
|
3104 |
static void rtl8139_ioport_writeb(void *opaque, uint32_t addr, uint32_t val) |
3105 |
{ |
3106 |
rtl8139_io_writeb(opaque, addr & 0xFF, val);
|
3107 |
} |
3108 |
|
3109 |
static void rtl8139_ioport_writew(void *opaque, uint32_t addr, uint32_t val) |
3110 |
{ |
3111 |
rtl8139_io_writew(opaque, addr & 0xFF, val);
|
3112 |
} |
3113 |
|
3114 |
static void rtl8139_ioport_writel(void *opaque, uint32_t addr, uint32_t val) |
3115 |
{ |
3116 |
rtl8139_io_writel(opaque, addr & 0xFF, val);
|
3117 |
} |
3118 |
|
3119 |
static uint32_t rtl8139_ioport_readb(void *opaque, uint32_t addr) |
3120 |
{ |
3121 |
return rtl8139_io_readb(opaque, addr & 0xFF); |
3122 |
} |
3123 |
|
3124 |
static uint32_t rtl8139_ioport_readw(void *opaque, uint32_t addr) |
3125 |
{ |
3126 |
return rtl8139_io_readw(opaque, addr & 0xFF); |
3127 |
} |
3128 |
|
3129 |
static uint32_t rtl8139_ioport_readl(void *opaque, uint32_t addr) |
3130 |
{ |
3131 |
return rtl8139_io_readl(opaque, addr & 0xFF); |
3132 |
} |
3133 |
|
3134 |
/* */
|
3135 |
|
3136 |
static void rtl8139_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
3137 |
{ |
3138 |
rtl8139_io_writeb(opaque, addr & 0xFF, val);
|
3139 |
} |
3140 |
|
3141 |
static void rtl8139_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
3142 |
{ |
3143 |
rtl8139_io_writew(opaque, addr & 0xFF, val);
|
3144 |
} |
3145 |
|
3146 |
static void rtl8139_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
3147 |
{ |
3148 |
rtl8139_io_writel(opaque, addr & 0xFF, val);
|
3149 |
} |
3150 |
|
3151 |
static uint32_t rtl8139_mmio_readb(void *opaque, target_phys_addr_t addr) |
3152 |
{ |
3153 |
return rtl8139_io_readb(opaque, addr & 0xFF); |
3154 |
} |
3155 |
|
3156 |
static uint32_t rtl8139_mmio_readw(void *opaque, target_phys_addr_t addr) |
3157 |
{ |
3158 |
uint32_t val = rtl8139_io_readw(opaque, addr & 0xFF);
|
3159 |
return val;
|
3160 |
} |
3161 |
|
3162 |
static uint32_t rtl8139_mmio_readl(void *opaque, target_phys_addr_t addr) |
3163 |
{ |
3164 |
uint32_t val = rtl8139_io_readl(opaque, addr & 0xFF);
|
3165 |
return val;
|
3166 |
} |
3167 |
|
3168 |
static int rtl8139_post_load(void *opaque, int version_id) |
3169 |
{ |
3170 |
RTL8139State* s = opaque; |
3171 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
3172 |
if (version_id < 4) { |
3173 |
s->cplus_enabled = s->CpCmd != 0;
|
3174 |
} |
3175 |
|
3176 |
return 0; |
3177 |
} |
3178 |
|
3179 |
static bool rtl8139_hotplug_ready_needed(void *opaque) |
3180 |
{ |
3181 |
return qdev_machine_modified();
|
3182 |
} |
3183 |
|
3184 |
static const VMStateDescription vmstate_rtl8139_hotplug_ready ={ |
3185 |
.name = "rtl8139/hotplug_ready",
|
3186 |
.version_id = 1,
|
3187 |
.minimum_version_id = 1,
|
3188 |
.minimum_version_id_old = 1,
|
3189 |
.fields = (VMStateField []) { |
3190 |
VMSTATE_END_OF_LIST() |
3191 |
} |
3192 |
}; |
3193 |
|
3194 |
static void rtl8139_pre_save(void *opaque) |
3195 |
{ |
3196 |
RTL8139State* s = opaque; |
3197 |
int64_t current_time = qemu_get_clock_ns(vm_clock); |
3198 |
|
3199 |
/* set IntrStatus correctly */
|
3200 |
rtl8139_set_next_tctr_time(s, current_time); |
3201 |
s->TCTR = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY, |
3202 |
get_ticks_per_sec()); |
3203 |
s->rtl8139_mmio_io_addr_dummy = s->rtl8139_mmio_io_addr; |
3204 |
} |
3205 |
|
3206 |
static const VMStateDescription vmstate_rtl8139 = { |
3207 |
.name = "rtl8139",
|
3208 |
.version_id = 4,
|
3209 |
.minimum_version_id = 3,
|
3210 |
.minimum_version_id_old = 3,
|
3211 |
.post_load = rtl8139_post_load, |
3212 |
.pre_save = rtl8139_pre_save, |
3213 |
.fields = (VMStateField []) { |
3214 |
VMSTATE_PCI_DEVICE(dev, RTL8139State), |
3215 |
VMSTATE_PARTIAL_BUFFER(phys, RTL8139State, 6),
|
3216 |
VMSTATE_BUFFER(mult, RTL8139State), |
3217 |
VMSTATE_UINT32_ARRAY(TxStatus, RTL8139State, 4),
|
3218 |
VMSTATE_UINT32_ARRAY(TxAddr, RTL8139State, 4),
|
3219 |
|
3220 |
VMSTATE_UINT32(RxBuf, RTL8139State), |
3221 |
VMSTATE_UINT32(RxBufferSize, RTL8139State), |
3222 |
VMSTATE_UINT32(RxBufPtr, RTL8139State), |
3223 |
VMSTATE_UINT32(RxBufAddr, RTL8139State), |
3224 |
|
3225 |
VMSTATE_UINT16(IntrStatus, RTL8139State), |
3226 |
VMSTATE_UINT16(IntrMask, RTL8139State), |
3227 |
|
3228 |
VMSTATE_UINT32(TxConfig, RTL8139State), |
3229 |
VMSTATE_UINT32(RxConfig, RTL8139State), |
3230 |
VMSTATE_UINT32(RxMissed, RTL8139State), |
3231 |
VMSTATE_UINT16(CSCR, RTL8139State), |
3232 |
|
3233 |
VMSTATE_UINT8(Cfg9346, RTL8139State), |
3234 |
VMSTATE_UINT8(Config0, RTL8139State), |
3235 |
VMSTATE_UINT8(Config1, RTL8139State), |
3236 |
VMSTATE_UINT8(Config3, RTL8139State), |
3237 |
VMSTATE_UINT8(Config4, RTL8139State), |
3238 |
VMSTATE_UINT8(Config5, RTL8139State), |
3239 |
|
3240 |
VMSTATE_UINT8(clock_enabled, RTL8139State), |
3241 |
VMSTATE_UINT8(bChipCmdState, RTL8139State), |
3242 |
|
3243 |
VMSTATE_UINT16(MultiIntr, RTL8139State), |
3244 |
|
3245 |
VMSTATE_UINT16(BasicModeCtrl, RTL8139State), |
3246 |
VMSTATE_UINT16(BasicModeStatus, RTL8139State), |
3247 |
VMSTATE_UINT16(NWayAdvert, RTL8139State), |
3248 |
VMSTATE_UINT16(NWayLPAR, RTL8139State), |
3249 |
VMSTATE_UINT16(NWayExpansion, RTL8139State), |
3250 |
|
3251 |
VMSTATE_UINT16(CpCmd, RTL8139State), |
3252 |
VMSTATE_UINT8(TxThresh, RTL8139State), |
3253 |
|
3254 |
VMSTATE_UNUSED(4),
|
3255 |
VMSTATE_MACADDR(conf.macaddr, RTL8139State), |
3256 |
VMSTATE_INT32(rtl8139_mmio_io_addr_dummy, RTL8139State), |
3257 |
|
3258 |
VMSTATE_UINT32(currTxDesc, RTL8139State), |
3259 |
VMSTATE_UINT32(currCPlusRxDesc, RTL8139State), |
3260 |
VMSTATE_UINT32(currCPlusTxDesc, RTL8139State), |
3261 |
VMSTATE_UINT32(RxRingAddrLO, RTL8139State), |
3262 |
VMSTATE_UINT32(RxRingAddrHI, RTL8139State), |
3263 |
|
3264 |
VMSTATE_UINT16_ARRAY(eeprom.contents, RTL8139State, EEPROM_9346_SIZE), |
3265 |
VMSTATE_INT32(eeprom.mode, RTL8139State), |
3266 |
VMSTATE_UINT32(eeprom.tick, RTL8139State), |
3267 |
VMSTATE_UINT8(eeprom.address, RTL8139State), |
3268 |
VMSTATE_UINT16(eeprom.input, RTL8139State), |
3269 |
VMSTATE_UINT16(eeprom.output, RTL8139State), |
3270 |
|
3271 |
VMSTATE_UINT8(eeprom.eecs, RTL8139State), |
3272 |
VMSTATE_UINT8(eeprom.eesk, RTL8139State), |
3273 |
VMSTATE_UINT8(eeprom.eedi, RTL8139State), |
3274 |
VMSTATE_UINT8(eeprom.eedo, RTL8139State), |
3275 |
|
3276 |
VMSTATE_UINT32(TCTR, RTL8139State), |
3277 |
VMSTATE_UINT32(TimerInt, RTL8139State), |
3278 |
VMSTATE_INT64(TCTR_base, RTL8139State), |
3279 |
|
3280 |
VMSTATE_STRUCT(tally_counters, RTL8139State, 0,
|
3281 |
vmstate_tally_counters, RTL8139TallyCounters), |
3282 |
|
3283 |
VMSTATE_UINT32_V(cplus_enabled, RTL8139State, 4),
|
3284 |
VMSTATE_END_OF_LIST() |
3285 |
}, |
3286 |
.subsections = (VMStateSubsection []) { |
3287 |
{ |
3288 |
.vmsd = &vmstate_rtl8139_hotplug_ready, |
3289 |
.needed = rtl8139_hotplug_ready_needed, |
3290 |
}, { |
3291 |
/* empty */
|
3292 |
} |
3293 |
} |
3294 |
}; |
3295 |
|
3296 |
/***********************************************************/
|
3297 |
/* PCI RTL8139 definitions */
|
3298 |
|
3299 |
static void rtl8139_mmio_map(PCIDevice *pci_dev, int region_num, |
3300 |
pcibus_t addr, pcibus_t size, int type)
|
3301 |
{ |
3302 |
RTL8139State *s = DO_UPCAST(RTL8139State, dev, pci_dev); |
3303 |
|
3304 |
cpu_register_physical_memory(addr + 0, 0x100, s->rtl8139_mmio_io_addr); |
3305 |
} |
3306 |
|
3307 |
static void rtl8139_ioport_map(PCIDevice *pci_dev, int region_num, |
3308 |
pcibus_t addr, pcibus_t size, int type)
|
3309 |
{ |
3310 |
RTL8139State *s = DO_UPCAST(RTL8139State, dev, pci_dev); |
3311 |
|
3312 |
register_ioport_write(addr, 0x100, 1, rtl8139_ioport_writeb, s); |
3313 |
register_ioport_read( addr, 0x100, 1, rtl8139_ioport_readb, s); |
3314 |
|
3315 |
register_ioport_write(addr, 0x100, 2, rtl8139_ioport_writew, s); |
3316 |
register_ioport_read( addr, 0x100, 2, rtl8139_ioport_readw, s); |
3317 |
|
3318 |
register_ioport_write(addr, 0x100, 4, rtl8139_ioport_writel, s); |
3319 |
register_ioport_read( addr, 0x100, 4, rtl8139_ioport_readl, s); |
3320 |
} |
3321 |
|
3322 |
static CPUReadMemoryFunc * const rtl8139_mmio_read[3] = { |
3323 |
rtl8139_mmio_readb, |
3324 |
rtl8139_mmio_readw, |
3325 |
rtl8139_mmio_readl, |
3326 |
}; |
3327 |
|
3328 |
static CPUWriteMemoryFunc * const rtl8139_mmio_write[3] = { |
3329 |
rtl8139_mmio_writeb, |
3330 |
rtl8139_mmio_writew, |
3331 |
rtl8139_mmio_writel, |
3332 |
}; |
3333 |
|
3334 |
static void rtl8139_timer(void *opaque) |
3335 |
{ |
3336 |
RTL8139State *s = opaque; |
3337 |
|
3338 |
if (!s->clock_enabled)
|
3339 |
{ |
3340 |
DEBUG_PRINT(("RTL8139: >>> timer: clock is not running\n"));
|
3341 |
return;
|
3342 |
} |
3343 |
|
3344 |
s->IntrStatus |= PCSTimeout; |
3345 |
rtl8139_update_irq(s); |
3346 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
3347 |
} |
3348 |
|
3349 |
static void rtl8139_cleanup(VLANClientState *nc) |
3350 |
{ |
3351 |
RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; |
3352 |
|
3353 |
s->nic = NULL;
|
3354 |
} |
3355 |
|
3356 |
static int pci_rtl8139_uninit(PCIDevice *dev) |
3357 |
{ |
3358 |
RTL8139State *s = DO_UPCAST(RTL8139State, dev, dev); |
3359 |
|
3360 |
cpu_unregister_io_memory(s->rtl8139_mmio_io_addr); |
3361 |
if (s->cplus_txbuffer) {
|
3362 |
qemu_free(s->cplus_txbuffer); |
3363 |
s->cplus_txbuffer = NULL;
|
3364 |
} |
3365 |
qemu_del_timer(s->timer); |
3366 |
qemu_free_timer(s->timer); |
3367 |
qemu_del_vlan_client(&s->nic->nc); |
3368 |
return 0; |
3369 |
} |
3370 |
|
3371 |
static NetClientInfo net_rtl8139_info = {
|
3372 |
.type = NET_CLIENT_TYPE_NIC, |
3373 |
.size = sizeof(NICState),
|
3374 |
.can_receive = rtl8139_can_receive, |
3375 |
.receive = rtl8139_receive, |
3376 |
.cleanup = rtl8139_cleanup, |
3377 |
}; |
3378 |
|
3379 |
static int pci_rtl8139_init(PCIDevice *dev) |
3380 |
{ |
3381 |
RTL8139State * s = DO_UPCAST(RTL8139State, dev, dev); |
3382 |
uint8_t *pci_conf; |
3383 |
|
3384 |
pci_conf = s->dev.config; |
3385 |
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK); |
3386 |
pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8139); |
3387 |
pci_conf[PCI_REVISION_ID] = RTL8139_PCI_REVID; /* >=0x20 is for 8139C+ */
|
3388 |
pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET); |
3389 |
pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin 0 */ |
3390 |
/* TODO: start of capability list, but no capability
|
3391 |
* list bit in status register, and offset 0xdc seems unused. */
|
3392 |
pci_conf[PCI_CAPABILITY_LIST] = 0xdc;
|
3393 |
|
3394 |
/* I/O handler for memory-mapped I/O */
|
3395 |
s->rtl8139_mmio_io_addr = |
3396 |
cpu_register_io_memory(rtl8139_mmio_read, rtl8139_mmio_write, s, |
3397 |
DEVICE_LITTLE_ENDIAN); |
3398 |
|
3399 |
pci_register_bar(&s->dev, 0, 0x100, |
3400 |
PCI_BASE_ADDRESS_SPACE_IO, rtl8139_ioport_map); |
3401 |
|
3402 |
pci_register_bar(&s->dev, 1, 0x100, |
3403 |
PCI_BASE_ADDRESS_SPACE_MEMORY, rtl8139_mmio_map); |
3404 |
|
3405 |
qemu_macaddr_default_if_unset(&s->conf.macaddr); |
3406 |
|
3407 |
/* prepare eeprom */
|
3408 |
s->eeprom.contents[0] = 0x8129; |
3409 |
#if 1 |
3410 |
/* PCI vendor and device ID should be mirrored here */
|
3411 |
s->eeprom.contents[1] = PCI_VENDOR_ID_REALTEK;
|
3412 |
s->eeprom.contents[2] = PCI_DEVICE_ID_REALTEK_8139;
|
3413 |
#endif
|
3414 |
s->eeprom.contents[7] = s->conf.macaddr.a[0] | s->conf.macaddr.a[1] << 8; |
3415 |
s->eeprom.contents[8] = s->conf.macaddr.a[2] | s->conf.macaddr.a[3] << 8; |
3416 |
s->eeprom.contents[9] = s->conf.macaddr.a[4] | s->conf.macaddr.a[5] << 8; |
3417 |
|
3418 |
s->nic = qemu_new_nic(&net_rtl8139_info, &s->conf, |
3419 |
dev->qdev.info->name, dev->qdev.id, s); |
3420 |
qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a); |
3421 |
|
3422 |
s->cplus_txbuffer = NULL;
|
3423 |
s->cplus_txbuffer_len = 0;
|
3424 |
s->cplus_txbuffer_offset = 0;
|
3425 |
|
3426 |
s->TimerExpire = 0;
|
3427 |
s->timer = qemu_new_timer_ns(vm_clock, rtl8139_timer, s); |
3428 |
rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock)); |
3429 |
|
3430 |
add_boot_device_path(s->conf.bootindex, &dev->qdev, "/ethernet-phy@0");
|
3431 |
|
3432 |
return 0; |
3433 |
} |
3434 |
|
3435 |
static PCIDeviceInfo rtl8139_info = {
|
3436 |
.qdev.name = "rtl8139",
|
3437 |
.qdev.size = sizeof(RTL8139State),
|
3438 |
.qdev.reset = rtl8139_reset, |
3439 |
.qdev.vmsd = &vmstate_rtl8139, |
3440 |
.init = pci_rtl8139_init, |
3441 |
.exit = pci_rtl8139_uninit, |
3442 |
.romfile = "pxe-rtl8139.bin",
|
3443 |
.qdev.props = (Property[]) { |
3444 |
DEFINE_NIC_PROPERTIES(RTL8139State, conf), |
3445 |
DEFINE_PROP_END_OF_LIST(), |
3446 |
} |
3447 |
}; |
3448 |
|
3449 |
static void rtl8139_register_devices(void) |
3450 |
{ |
3451 |
pci_qdev_register(&rtl8139_info); |
3452 |
} |
3453 |
|
3454 |
device_init(rtl8139_register_devices) |