1a8fe65b8SJeff Kirsher /*
2a8fe65b8SJeff Kirsher
3a8fe65b8SJeff Kirsher 8139too.c: A RealTek RTL-8139 Fast Ethernet driver for Linux.
4a8fe65b8SJeff Kirsher
5a8fe65b8SJeff Kirsher Maintained by Jeff Garzik <jgarzik@pobox.com>
6a8fe65b8SJeff Kirsher Copyright 2000-2002 Jeff Garzik
7a8fe65b8SJeff Kirsher
8a8fe65b8SJeff Kirsher Much code comes from Donald Becker's rtl8139.c driver,
9a8fe65b8SJeff Kirsher versions 1.13 and older. This driver was originally based
10a8fe65b8SJeff Kirsher on rtl8139.c version 1.07. Header of rtl8139.c version 1.13:
11a8fe65b8SJeff Kirsher
12a8fe65b8SJeff Kirsher -----<snip>-----
13a8fe65b8SJeff Kirsher
14a8fe65b8SJeff Kirsher Written 1997-2001 by Donald Becker.
15a8fe65b8SJeff Kirsher This software may be used and distributed according to the
16a8fe65b8SJeff Kirsher terms of the GNU General Public License (GPL), incorporated
17a8fe65b8SJeff Kirsher herein by reference. Drivers based on or derived from this
18a8fe65b8SJeff Kirsher code fall under the GPL and must retain the authorship,
19a8fe65b8SJeff Kirsher copyright and license notice. This file is not a complete
20a8fe65b8SJeff Kirsher program and may only be used when the entire operating
21a8fe65b8SJeff Kirsher system is licensed under the GPL.
22a8fe65b8SJeff Kirsher
23a8fe65b8SJeff Kirsher This driver is for boards based on the RTL8129 and RTL8139
24a8fe65b8SJeff Kirsher PCI ethernet chips.
25a8fe65b8SJeff Kirsher
26a8fe65b8SJeff Kirsher The author may be reached as becker@scyld.com, or C/O Scyld
27a8fe65b8SJeff Kirsher Computing Corporation 410 Severn Ave., Suite 210 Annapolis
28a8fe65b8SJeff Kirsher MD 21403
29a8fe65b8SJeff Kirsher
30a8fe65b8SJeff Kirsher Support and updates available at
31a8fe65b8SJeff Kirsher http://www.scyld.com/network/rtl8139.html
32a8fe65b8SJeff Kirsher
33a8fe65b8SJeff Kirsher Twister-tuning table provided by Kinston
34a8fe65b8SJeff Kirsher <shangh@realtek.com.tw>.
35a8fe65b8SJeff Kirsher
36a8fe65b8SJeff Kirsher -----<snip>-----
37a8fe65b8SJeff Kirsher
38a8fe65b8SJeff Kirsher This software may be used and distributed according to the terms
39a8fe65b8SJeff Kirsher of the GNU General Public License, incorporated herein by reference.
40a8fe65b8SJeff Kirsher
41a8fe65b8SJeff Kirsher Contributors:
42a8fe65b8SJeff Kirsher
43a8fe65b8SJeff Kirsher Donald Becker - he wrote the original driver, kudos to him!
44a8fe65b8SJeff Kirsher (but please don't e-mail him for support, this isn't his driver)
45a8fe65b8SJeff Kirsher
46a8fe65b8SJeff Kirsher Tigran Aivazian - bug fixes, skbuff free cleanup
47a8fe65b8SJeff Kirsher
48a8fe65b8SJeff Kirsher Martin Mares - suggestions for PCI cleanup
49a8fe65b8SJeff Kirsher
50a8fe65b8SJeff Kirsher David S. Miller - PCI DMA and softnet updates
51a8fe65b8SJeff Kirsher
52a8fe65b8SJeff Kirsher Ernst Gill - fixes ported from BSD driver
53a8fe65b8SJeff Kirsher
54a8fe65b8SJeff Kirsher Daniel Kobras - identified specific locations of
55a8fe65b8SJeff Kirsher posted MMIO write bugginess
56a8fe65b8SJeff Kirsher
57a8fe65b8SJeff Kirsher Gerard Sharp - bug fix, testing and feedback
58a8fe65b8SJeff Kirsher
59a8fe65b8SJeff Kirsher David Ford - Rx ring wrap fix
60a8fe65b8SJeff Kirsher
61a8fe65b8SJeff Kirsher Dan DeMaggio - swapped RTL8139 cards with me, and allowed me
62a8fe65b8SJeff Kirsher to find and fix a crucial bug on older chipsets.
63a8fe65b8SJeff Kirsher
64a8fe65b8SJeff Kirsher Donald Becker/Chris Butterworth/Marcus Westergren -
65a8fe65b8SJeff Kirsher Noticed various Rx packet size-related buglets.
66a8fe65b8SJeff Kirsher
67a8fe65b8SJeff Kirsher Santiago Garcia Mantinan - testing and feedback
68a8fe65b8SJeff Kirsher
69a8fe65b8SJeff Kirsher Jens David - 2.2.x kernel backports
70a8fe65b8SJeff Kirsher
71a8fe65b8SJeff Kirsher Martin Dennett - incredibly helpful insight on undocumented
72a8fe65b8SJeff Kirsher features of the 8139 chips
73a8fe65b8SJeff Kirsher
74a8fe65b8SJeff Kirsher Jean-Jacques Michel - bug fix
75a8fe65b8SJeff Kirsher
76a8fe65b8SJeff Kirsher Tobias Ringström - Rx interrupt status checking suggestion
77a8fe65b8SJeff Kirsher
78a8fe65b8SJeff Kirsher Andrew Morton - Clear blocked signals, avoid
79a8fe65b8SJeff Kirsher buffer overrun setting current->comm.
80a8fe65b8SJeff Kirsher
81a8fe65b8SJeff Kirsher Kalle Olavi Niemitalo - Wake-on-LAN ioctls
82a8fe65b8SJeff Kirsher
83a8fe65b8SJeff Kirsher Robert Kuebel - Save kernel thread from dying on any signal.
84a8fe65b8SJeff Kirsher
85a8fe65b8SJeff Kirsher Submitting bug reports:
86a8fe65b8SJeff Kirsher
87a8fe65b8SJeff Kirsher "rtl8139-diag -mmmaaavvveefN" output
88a8fe65b8SJeff Kirsher enable RTL8139_DEBUG below, and look at 'dmesg' or kernel log
89a8fe65b8SJeff Kirsher
90a8fe65b8SJeff Kirsher */
91a8fe65b8SJeff Kirsher
92a8fe65b8SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
93a8fe65b8SJeff Kirsher
94a8fe65b8SJeff Kirsher #define DRV_NAME "8139too"
95a8fe65b8SJeff Kirsher #define DRV_VERSION "0.9.28"
96a8fe65b8SJeff Kirsher
97a8fe65b8SJeff Kirsher
98a8fe65b8SJeff Kirsher #include <linux/module.h>
99a8fe65b8SJeff Kirsher #include <linux/kernel.h>
100a8fe65b8SJeff Kirsher #include <linux/compiler.h>
101a8fe65b8SJeff Kirsher #include <linux/pci.h>
102a8fe65b8SJeff Kirsher #include <linux/init.h>
103a8fe65b8SJeff Kirsher #include <linux/interrupt.h>
104a8fe65b8SJeff Kirsher #include <linux/netdevice.h>
105a8fe65b8SJeff Kirsher #include <linux/etherdevice.h>
106a8fe65b8SJeff Kirsher #include <linux/rtnetlink.h>
107a8fe65b8SJeff Kirsher #include <linux/delay.h>
108a8fe65b8SJeff Kirsher #include <linux/ethtool.h>
109a8fe65b8SJeff Kirsher #include <linux/mii.h>
110a8fe65b8SJeff Kirsher #include <linux/completion.h>
111a8fe65b8SJeff Kirsher #include <linux/crc32.h>
112a8fe65b8SJeff Kirsher #include <linux/io.h>
113a8fe65b8SJeff Kirsher #include <linux/uaccess.h>
114a8fe65b8SJeff Kirsher #include <linux/gfp.h>
1152bbea0a8SAlban Bedel #include <linux/if_vlan.h>
116a8fe65b8SJeff Kirsher #include <asm/irq.h>
117a8fe65b8SJeff Kirsher
118a8fe65b8SJeff Kirsher #define RTL8139_DRIVER_NAME DRV_NAME " Fast Ethernet driver " DRV_VERSION
119a8fe65b8SJeff Kirsher
120a8fe65b8SJeff Kirsher /* Default Message level */
121a8fe65b8SJeff Kirsher #define RTL8139_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
122a8fe65b8SJeff Kirsher NETIF_MSG_PROBE | \
123a8fe65b8SJeff Kirsher NETIF_MSG_LINK)
124a8fe65b8SJeff Kirsher
125a8fe65b8SJeff Kirsher
126a8fe65b8SJeff Kirsher /* define to 1, 2 or 3 to enable copious debugging info */
127a8fe65b8SJeff Kirsher #define RTL8139_DEBUG 0
128a8fe65b8SJeff Kirsher
129a8fe65b8SJeff Kirsher /* define to 1 to disable lightweight runtime debugging checks */
130a8fe65b8SJeff Kirsher #undef RTL8139_NDEBUG
131a8fe65b8SJeff Kirsher
132a8fe65b8SJeff Kirsher
133a8fe65b8SJeff Kirsher #ifdef RTL8139_NDEBUG
134a8fe65b8SJeff Kirsher # define assert(expr) do {} while (0)
135a8fe65b8SJeff Kirsher #else
136a8fe65b8SJeff Kirsher # define assert(expr) \
137a8fe65b8SJeff Kirsher if (unlikely(!(expr))) { \
138a8fe65b8SJeff Kirsher pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
139a8fe65b8SJeff Kirsher #expr, __FILE__, __func__, __LINE__); \
140a8fe65b8SJeff Kirsher }
141a8fe65b8SJeff Kirsher #endif
142a8fe65b8SJeff Kirsher
143a8fe65b8SJeff Kirsher
144a8fe65b8SJeff Kirsher /* A few user-configurable values. */
145a8fe65b8SJeff Kirsher /* media options */
146a8fe65b8SJeff Kirsher #define MAX_UNITS 8
147a8fe65b8SJeff Kirsher static int media[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
148a8fe65b8SJeff Kirsher static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
149a8fe65b8SJeff Kirsher
150a8fe65b8SJeff Kirsher /* Whether to use MMIO or PIO. Default to MMIO. */
151a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_PIO
15265712ec0SFrancois Romieu static bool use_io = true;
153a8fe65b8SJeff Kirsher #else
15465712ec0SFrancois Romieu static bool use_io = false;
155a8fe65b8SJeff Kirsher #endif
156a8fe65b8SJeff Kirsher
157a8fe65b8SJeff Kirsher /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
158a8fe65b8SJeff Kirsher The RTL chips use a 64 element hash table based on the Ethernet CRC. */
159a8fe65b8SJeff Kirsher static int multicast_filter_limit = 32;
160a8fe65b8SJeff Kirsher
161a8fe65b8SJeff Kirsher /* bitmapped message enable number */
162a8fe65b8SJeff Kirsher static int debug = -1;
163a8fe65b8SJeff Kirsher
164a8fe65b8SJeff Kirsher /*
165a8fe65b8SJeff Kirsher * Receive ring size
166a8fe65b8SJeff Kirsher * Warning: 64K ring has hardware issues and may lock up.
167a8fe65b8SJeff Kirsher */
168a8fe65b8SJeff Kirsher #if defined(CONFIG_SH_DREAMCAST)
169a8fe65b8SJeff Kirsher #define RX_BUF_IDX 0 /* 8K ring */
170a8fe65b8SJeff Kirsher #else
171a8fe65b8SJeff Kirsher #define RX_BUF_IDX 2 /* 32K ring */
172a8fe65b8SJeff Kirsher #endif
173a8fe65b8SJeff Kirsher #define RX_BUF_LEN (8192 << RX_BUF_IDX)
174a8fe65b8SJeff Kirsher #define RX_BUF_PAD 16
175a8fe65b8SJeff Kirsher #define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
176a8fe65b8SJeff Kirsher
177a8fe65b8SJeff Kirsher #if RX_BUF_LEN == 65536
178a8fe65b8SJeff Kirsher #define RX_BUF_TOT_LEN RX_BUF_LEN
179a8fe65b8SJeff Kirsher #else
180a8fe65b8SJeff Kirsher #define RX_BUF_TOT_LEN (RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
181a8fe65b8SJeff Kirsher #endif
182a8fe65b8SJeff Kirsher
183a8fe65b8SJeff Kirsher /* Number of Tx descriptor registers. */
184a8fe65b8SJeff Kirsher #define NUM_TX_DESC 4
185a8fe65b8SJeff Kirsher
1862bbea0a8SAlban Bedel /* max supported ethernet frame size -- must be at least (dev->mtu+18+4).*/
1876f6e741fSAlban Bedel #define MAX_ETH_FRAME_SIZE 1792
188a8fe65b8SJeff Kirsher
189ef786f10SAlban Bedel /* max supported payload size */
1902bbea0a8SAlban Bedel #define MAX_ETH_DATA_SIZE (MAX_ETH_FRAME_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN)
191ef786f10SAlban Bedel
1922bbea0a8SAlban Bedel /* Size of the Tx bounce buffers -- must be at least (dev->mtu+18+4). */
193a8fe65b8SJeff Kirsher #define TX_BUF_SIZE MAX_ETH_FRAME_SIZE
194a8fe65b8SJeff Kirsher #define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC)
195a8fe65b8SJeff Kirsher
196a8fe65b8SJeff Kirsher /* PCI Tuning Parameters
197a8fe65b8SJeff Kirsher Threshold is bytes transferred to chip before transmission starts. */
198a8fe65b8SJeff Kirsher #define TX_FIFO_THRESH 256 /* In bytes, rounded down to 32 byte units. */
199a8fe65b8SJeff Kirsher
200a8fe65b8SJeff Kirsher /* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
201a8fe65b8SJeff Kirsher #define RX_FIFO_THRESH 7 /* Rx buffer level before first PCI xfer. */
202a8fe65b8SJeff Kirsher #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
203a8fe65b8SJeff Kirsher #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
204a8fe65b8SJeff Kirsher #define TX_RETRY 8 /* 0-15. retries = 16 + (TX_RETRY * 16) */
205a8fe65b8SJeff Kirsher
206a8fe65b8SJeff Kirsher /* Operational parameters that usually are not changed. */
207a8fe65b8SJeff Kirsher /* Time in jiffies before concluding the transmitter is hung. */
208a8fe65b8SJeff Kirsher #define TX_TIMEOUT (6*HZ)
209a8fe65b8SJeff Kirsher
210a8fe65b8SJeff Kirsher
211a8fe65b8SJeff Kirsher enum {
212a8fe65b8SJeff Kirsher HAS_MII_XCVR = 0x010000,
213a8fe65b8SJeff Kirsher HAS_CHIP_XCVR = 0x020000,
214a8fe65b8SJeff Kirsher HAS_LNK_CHNG = 0x040000,
215a8fe65b8SJeff Kirsher };
216a8fe65b8SJeff Kirsher
217a8fe65b8SJeff Kirsher #define RTL_NUM_STATS 4 /* number of ETHTOOL_GSTATS u64's */
218a8fe65b8SJeff Kirsher #define RTL_REGS_VER 1 /* version of reg. data in ETHTOOL_GREGS */
219a8fe65b8SJeff Kirsher #define RTL_MIN_IO_SIZE 0x80
220a8fe65b8SJeff Kirsher #define RTL8139B_IO_SIZE 256
221a8fe65b8SJeff Kirsher
222a8fe65b8SJeff Kirsher #define RTL8129_CAPS HAS_MII_XCVR
223a8fe65b8SJeff Kirsher #define RTL8139_CAPS (HAS_CHIP_XCVR|HAS_LNK_CHNG)
224a8fe65b8SJeff Kirsher
225a8fe65b8SJeff Kirsher typedef enum {
226a8fe65b8SJeff Kirsher RTL8139 = 0,
227a8fe65b8SJeff Kirsher RTL8129,
228a8fe65b8SJeff Kirsher } board_t;
229a8fe65b8SJeff Kirsher
230a8fe65b8SJeff Kirsher
231a8fe65b8SJeff Kirsher /* indexed by board_t, above */
232a8fe65b8SJeff Kirsher static const struct {
233a8fe65b8SJeff Kirsher const char *name;
234a8fe65b8SJeff Kirsher u32 hw_flags;
235b4e54a94SBill Pemberton } board_info[] = {
236a8fe65b8SJeff Kirsher { "RealTek RTL8139", RTL8139_CAPS },
237a8fe65b8SJeff Kirsher { "RealTek RTL8129", RTL8129_CAPS },
238a8fe65b8SJeff Kirsher };
239a8fe65b8SJeff Kirsher
240a8fe65b8SJeff Kirsher
2419baa3c34SBenoit Taine static const struct pci_device_id rtl8139_pci_tbl[] = {
242a8fe65b8SJeff Kirsher {0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
243a8fe65b8SJeff Kirsher {0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
244a8fe65b8SJeff Kirsher {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
245a8fe65b8SJeff Kirsher {0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
246a8fe65b8SJeff Kirsher {0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
247a8fe65b8SJeff Kirsher {0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
248a8fe65b8SJeff Kirsher {0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
249a8fe65b8SJeff Kirsher {0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
250a8fe65b8SJeff Kirsher {0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
251a8fe65b8SJeff Kirsher {0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
252a8fe65b8SJeff Kirsher {0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
253a8fe65b8SJeff Kirsher {0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
254a8fe65b8SJeff Kirsher {0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
255a8fe65b8SJeff Kirsher {0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
256a8fe65b8SJeff Kirsher {0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
257a8fe65b8SJeff Kirsher {0x018a, 0x0106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
258a8fe65b8SJeff Kirsher {0x126c, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
259a8fe65b8SJeff Kirsher {0x1743, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
260a8fe65b8SJeff Kirsher {0x021b, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
261580411d0SMatthew Whitehead {0x16ec, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
262a8fe65b8SJeff Kirsher
263a8fe65b8SJeff Kirsher #ifdef CONFIG_SH_SECUREEDGE5410
264a8fe65b8SJeff Kirsher /* Bogus 8139 silicon reports 8129 without external PROM :-( */
265a8fe65b8SJeff Kirsher {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
266a8fe65b8SJeff Kirsher #endif
267a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
268a8fe65b8SJeff Kirsher {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },
269a8fe65b8SJeff Kirsher #endif
270a8fe65b8SJeff Kirsher
271a8fe65b8SJeff Kirsher /* some crazy cards report invalid vendor ids like
272a8fe65b8SJeff Kirsher * 0x0001 here. The other ids are valid and constant,
273a8fe65b8SJeff Kirsher * so we simply don't match on the main vendor id.
274a8fe65b8SJeff Kirsher */
275a8fe65b8SJeff Kirsher {PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },
276a8fe65b8SJeff Kirsher {PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },
277a8fe65b8SJeff Kirsher {PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
278a8fe65b8SJeff Kirsher
279a8fe65b8SJeff Kirsher {0,}
280a8fe65b8SJeff Kirsher };
281a8fe65b8SJeff Kirsher MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
282a8fe65b8SJeff Kirsher
283a8fe65b8SJeff Kirsher static struct {
284a8fe65b8SJeff Kirsher const char str[ETH_GSTRING_LEN];
285a8fe65b8SJeff Kirsher } ethtool_stats_keys[] = {
286a8fe65b8SJeff Kirsher { "early_rx" },
287a8fe65b8SJeff Kirsher { "tx_buf_mapped" },
288a8fe65b8SJeff Kirsher { "tx_timeouts" },
289a8fe65b8SJeff Kirsher { "rx_lost_in_ring" },
290a8fe65b8SJeff Kirsher };
291a8fe65b8SJeff Kirsher
292a8fe65b8SJeff Kirsher /* The rest of these values should never change. */
293a8fe65b8SJeff Kirsher
294a8fe65b8SJeff Kirsher /* Symbolic offsets to registers. */
295a8fe65b8SJeff Kirsher enum RTL8139_registers {
296a8fe65b8SJeff Kirsher MAC0 = 0, /* Ethernet hardware address. */
297a8fe65b8SJeff Kirsher MAR0 = 8, /* Multicast filter. */
298a8fe65b8SJeff Kirsher TxStatus0 = 0x10, /* Transmit status (Four 32bit registers). */
299a8fe65b8SJeff Kirsher TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */
300a8fe65b8SJeff Kirsher RxBuf = 0x30,
301a8fe65b8SJeff Kirsher ChipCmd = 0x37,
302a8fe65b8SJeff Kirsher RxBufPtr = 0x38,
303a8fe65b8SJeff Kirsher RxBufAddr = 0x3A,
304a8fe65b8SJeff Kirsher IntrMask = 0x3C,
305a8fe65b8SJeff Kirsher IntrStatus = 0x3E,
306a8fe65b8SJeff Kirsher TxConfig = 0x40,
307a8fe65b8SJeff Kirsher RxConfig = 0x44,
308a8fe65b8SJeff Kirsher Timer = 0x48, /* A general-purpose counter. */
309a8fe65b8SJeff Kirsher RxMissed = 0x4C, /* 24 bits valid, write clears. */
310a8fe65b8SJeff Kirsher Cfg9346 = 0x50,
311a8fe65b8SJeff Kirsher Config0 = 0x51,
312a8fe65b8SJeff Kirsher Config1 = 0x52,
313a8fe65b8SJeff Kirsher TimerInt = 0x54,
314a8fe65b8SJeff Kirsher MediaStatus = 0x58,
315a8fe65b8SJeff Kirsher Config3 = 0x59,
316a8fe65b8SJeff Kirsher Config4 = 0x5A, /* absent on RTL-8139A */
317a8fe65b8SJeff Kirsher HltClk = 0x5B,
318a8fe65b8SJeff Kirsher MultiIntr = 0x5C,
319a8fe65b8SJeff Kirsher TxSummary = 0x60,
320a8fe65b8SJeff Kirsher BasicModeCtrl = 0x62,
321a8fe65b8SJeff Kirsher BasicModeStatus = 0x64,
322a8fe65b8SJeff Kirsher NWayAdvert = 0x66,
323a8fe65b8SJeff Kirsher NWayLPAR = 0x68,
324a8fe65b8SJeff Kirsher NWayExpansion = 0x6A,
325a8fe65b8SJeff Kirsher /* Undocumented registers, but required for proper operation. */
326a8fe65b8SJeff Kirsher FIFOTMS = 0x70, /* FIFO Control and test. */
327a8fe65b8SJeff Kirsher CSCR = 0x74, /* Chip Status and Configuration Register. */
328a8fe65b8SJeff Kirsher PARA78 = 0x78,
329a8fe65b8SJeff Kirsher FlashReg = 0xD4, /* Communication with Flash ROM, four bytes. */
330a8fe65b8SJeff Kirsher PARA7c = 0x7c, /* Magic transceiver parameter register. */
331a8fe65b8SJeff Kirsher Config5 = 0xD8, /* absent on RTL-8139A */
332a8fe65b8SJeff Kirsher };
333a8fe65b8SJeff Kirsher
334a8fe65b8SJeff Kirsher enum ClearBitMasks {
335a8fe65b8SJeff Kirsher MultiIntrClear = 0xF000,
336a8fe65b8SJeff Kirsher ChipCmdClear = 0xE2,
337a8fe65b8SJeff Kirsher Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
338a8fe65b8SJeff Kirsher };
339a8fe65b8SJeff Kirsher
340a8fe65b8SJeff Kirsher enum ChipCmdBits {
341a8fe65b8SJeff Kirsher CmdReset = 0x10,
342a8fe65b8SJeff Kirsher CmdRxEnb = 0x08,
343a8fe65b8SJeff Kirsher CmdTxEnb = 0x04,
344a8fe65b8SJeff Kirsher RxBufEmpty = 0x01,
345a8fe65b8SJeff Kirsher };
346a8fe65b8SJeff Kirsher
347a8fe65b8SJeff Kirsher /* Interrupt register bits, using my own meaningful names. */
348a8fe65b8SJeff Kirsher enum IntrStatusBits {
349a8fe65b8SJeff Kirsher PCIErr = 0x8000,
350a8fe65b8SJeff Kirsher PCSTimeout = 0x4000,
351a8fe65b8SJeff Kirsher RxFIFOOver = 0x40,
352a8fe65b8SJeff Kirsher RxUnderrun = 0x20,
353a8fe65b8SJeff Kirsher RxOverflow = 0x10,
354a8fe65b8SJeff Kirsher TxErr = 0x08,
355a8fe65b8SJeff Kirsher TxOK = 0x04,
356a8fe65b8SJeff Kirsher RxErr = 0x02,
357a8fe65b8SJeff Kirsher RxOK = 0x01,
358a8fe65b8SJeff Kirsher
359a8fe65b8SJeff Kirsher RxAckBits = RxFIFOOver | RxOverflow | RxOK,
360a8fe65b8SJeff Kirsher };
361a8fe65b8SJeff Kirsher
362a8fe65b8SJeff Kirsher enum TxStatusBits {
363a8fe65b8SJeff Kirsher TxHostOwns = 0x2000,
364a8fe65b8SJeff Kirsher TxUnderrun = 0x4000,
365a8fe65b8SJeff Kirsher TxStatOK = 0x8000,
366a8fe65b8SJeff Kirsher TxOutOfWindow = 0x20000000,
367a8fe65b8SJeff Kirsher TxAborted = 0x40000000,
368a8fe65b8SJeff Kirsher TxCarrierLost = 0x80000000,
369a8fe65b8SJeff Kirsher };
370a8fe65b8SJeff Kirsher enum RxStatusBits {
371a8fe65b8SJeff Kirsher RxMulticast = 0x8000,
372a8fe65b8SJeff Kirsher RxPhysical = 0x4000,
373a8fe65b8SJeff Kirsher RxBroadcast = 0x2000,
374a8fe65b8SJeff Kirsher RxBadSymbol = 0x0020,
375a8fe65b8SJeff Kirsher RxRunt = 0x0010,
376a8fe65b8SJeff Kirsher RxTooLong = 0x0008,
377a8fe65b8SJeff Kirsher RxCRCErr = 0x0004,
378a8fe65b8SJeff Kirsher RxBadAlign = 0x0002,
379a8fe65b8SJeff Kirsher RxStatusOK = 0x0001,
380a8fe65b8SJeff Kirsher };
381a8fe65b8SJeff Kirsher
382a8fe65b8SJeff Kirsher /* Bits in RxConfig. */
383a8fe65b8SJeff Kirsher enum rx_mode_bits {
384a8fe65b8SJeff Kirsher AcceptErr = 0x20,
385a8fe65b8SJeff Kirsher AcceptRunt = 0x10,
386a8fe65b8SJeff Kirsher AcceptBroadcast = 0x08,
387a8fe65b8SJeff Kirsher AcceptMulticast = 0x04,
388a8fe65b8SJeff Kirsher AcceptMyPhys = 0x02,
389a8fe65b8SJeff Kirsher AcceptAllPhys = 0x01,
390a8fe65b8SJeff Kirsher };
391a8fe65b8SJeff Kirsher
392a8fe65b8SJeff Kirsher /* Bits in TxConfig. */
393a8fe65b8SJeff Kirsher enum tx_config_bits {
394a8fe65b8SJeff Kirsher /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
395a8fe65b8SJeff Kirsher TxIFGShift = 24,
396a8fe65b8SJeff Kirsher TxIFG84 = (0 << TxIFGShift), /* 8.4us / 840ns (10 / 100Mbps) */
397a8fe65b8SJeff Kirsher TxIFG88 = (1 << TxIFGShift), /* 8.8us / 880ns (10 / 100Mbps) */
398a8fe65b8SJeff Kirsher TxIFG92 = (2 << TxIFGShift), /* 9.2us / 920ns (10 / 100Mbps) */
399a8fe65b8SJeff Kirsher TxIFG96 = (3 << TxIFGShift), /* 9.6us / 960ns (10 / 100Mbps) */
400a8fe65b8SJeff Kirsher
401a8fe65b8SJeff Kirsher TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
402a8fe65b8SJeff Kirsher TxCRC = (1 << 16), /* DISABLE Tx pkt CRC append */
403a8fe65b8SJeff Kirsher TxClearAbt = (1 << 0), /* Clear abort (WO) */
404a8fe65b8SJeff Kirsher TxDMAShift = 8, /* DMA burst value (0-7) is shifted X many bits */
405a8fe65b8SJeff Kirsher TxRetryShift = 4, /* TXRR value (0-15) is shifted X many bits */
406a8fe65b8SJeff Kirsher
407a8fe65b8SJeff Kirsher TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
408a8fe65b8SJeff Kirsher };
409a8fe65b8SJeff Kirsher
410a8fe65b8SJeff Kirsher /* Bits in Config1 */
411a8fe65b8SJeff Kirsher enum Config1Bits {
412a8fe65b8SJeff Kirsher Cfg1_PM_Enable = 0x01,
413a8fe65b8SJeff Kirsher Cfg1_VPD_Enable = 0x02,
414a8fe65b8SJeff Kirsher Cfg1_PIO = 0x04,
415a8fe65b8SJeff Kirsher Cfg1_MMIO = 0x08,
416a8fe65b8SJeff Kirsher LWAKE = 0x10, /* not on 8139, 8139A */
417a8fe65b8SJeff Kirsher Cfg1_Driver_Load = 0x20,
418a8fe65b8SJeff Kirsher Cfg1_LED0 = 0x40,
419a8fe65b8SJeff Kirsher Cfg1_LED1 = 0x80,
420a8fe65b8SJeff Kirsher SLEEP = (1 << 1), /* only on 8139, 8139A */
421a8fe65b8SJeff Kirsher PWRDN = (1 << 0), /* only on 8139, 8139A */
422a8fe65b8SJeff Kirsher };
423a8fe65b8SJeff Kirsher
424a8fe65b8SJeff Kirsher /* Bits in Config3 */
425a8fe65b8SJeff Kirsher enum Config3Bits {
426a8fe65b8SJeff Kirsher Cfg3_FBtBEn = (1 << 0), /* 1 = Fast Back to Back */
427a8fe65b8SJeff Kirsher Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
428a8fe65b8SJeff Kirsher Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
429a8fe65b8SJeff Kirsher Cfg3_CardB_En = (1 << 3), /* 1 = enable CardBus registers */
430a8fe65b8SJeff Kirsher Cfg3_LinkUp = (1 << 4), /* 1 = wake up on link up */
431a8fe65b8SJeff Kirsher Cfg3_Magic = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
432a8fe65b8SJeff Kirsher Cfg3_PARM_En = (1 << 6), /* 0 = software can set twister parameters */
433a8fe65b8SJeff Kirsher Cfg3_GNTSel = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
434a8fe65b8SJeff Kirsher };
435a8fe65b8SJeff Kirsher
436a8fe65b8SJeff Kirsher /* Bits in Config4 */
437a8fe65b8SJeff Kirsher enum Config4Bits {
438a8fe65b8SJeff Kirsher LWPTN = (1 << 2), /* not on 8139, 8139A */
439a8fe65b8SJeff Kirsher };
440a8fe65b8SJeff Kirsher
441a8fe65b8SJeff Kirsher /* Bits in Config5 */
442a8fe65b8SJeff Kirsher enum Config5Bits {
443a8fe65b8SJeff Kirsher Cfg5_PME_STS = (1 << 0), /* 1 = PCI reset resets PME_Status */
444a8fe65b8SJeff Kirsher Cfg5_LANWake = (1 << 1), /* 1 = enable LANWake signal */
445a8fe65b8SJeff Kirsher Cfg5_LDPS = (1 << 2), /* 0 = save power when link is down */
446a8fe65b8SJeff Kirsher Cfg5_FIFOAddrPtr= (1 << 3), /* Realtek internal SRAM testing */
447a8fe65b8SJeff Kirsher Cfg5_UWF = (1 << 4), /* 1 = accept unicast wakeup frame */
448a8fe65b8SJeff Kirsher Cfg5_MWF = (1 << 5), /* 1 = accept multicast wakeup frame */
449a8fe65b8SJeff Kirsher Cfg5_BWF = (1 << 6), /* 1 = accept broadcast wakeup frame */
450a8fe65b8SJeff Kirsher };
451a8fe65b8SJeff Kirsher
452a8fe65b8SJeff Kirsher enum RxConfigBits {
453a8fe65b8SJeff Kirsher /* rx fifo threshold */
454a8fe65b8SJeff Kirsher RxCfgFIFOShift = 13,
455a8fe65b8SJeff Kirsher RxCfgFIFONone = (7 << RxCfgFIFOShift),
456a8fe65b8SJeff Kirsher
457a8fe65b8SJeff Kirsher /* Max DMA burst */
458a8fe65b8SJeff Kirsher RxCfgDMAShift = 8,
459a8fe65b8SJeff Kirsher RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
460a8fe65b8SJeff Kirsher
461a8fe65b8SJeff Kirsher /* rx ring buffer length */
462a8fe65b8SJeff Kirsher RxCfgRcv8K = 0,
463a8fe65b8SJeff Kirsher RxCfgRcv16K = (1 << 11),
464a8fe65b8SJeff Kirsher RxCfgRcv32K = (1 << 12),
465a8fe65b8SJeff Kirsher RxCfgRcv64K = (1 << 11) | (1 << 12),
466a8fe65b8SJeff Kirsher
467a8fe65b8SJeff Kirsher /* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
468a8fe65b8SJeff Kirsher RxNoWrap = (1 << 7),
469a8fe65b8SJeff Kirsher };
470a8fe65b8SJeff Kirsher
471a8fe65b8SJeff Kirsher /* Twister tuning parameters from RealTek.
472a8fe65b8SJeff Kirsher Completely undocumented, but required to tune bad links on some boards. */
473a8fe65b8SJeff Kirsher enum CSCRBits {
474a8fe65b8SJeff Kirsher CSCR_LinkOKBit = 0x0400,
475a8fe65b8SJeff Kirsher CSCR_LinkChangeBit = 0x0800,
476a8fe65b8SJeff Kirsher CSCR_LinkStatusBits = 0x0f000,
477a8fe65b8SJeff Kirsher CSCR_LinkDownOffCmd = 0x003c0,
478a8fe65b8SJeff Kirsher CSCR_LinkDownCmd = 0x0f3c0,
479a8fe65b8SJeff Kirsher };
480a8fe65b8SJeff Kirsher
481a8fe65b8SJeff Kirsher enum Cfg9346Bits {
482a8fe65b8SJeff Kirsher Cfg9346_Lock = 0x00,
483a8fe65b8SJeff Kirsher Cfg9346_Unlock = 0xC0,
484a8fe65b8SJeff Kirsher };
485a8fe65b8SJeff Kirsher
486a8fe65b8SJeff Kirsher typedef enum {
487a8fe65b8SJeff Kirsher CH_8139 = 0,
488a8fe65b8SJeff Kirsher CH_8139_K,
489a8fe65b8SJeff Kirsher CH_8139A,
490a8fe65b8SJeff Kirsher CH_8139A_G,
491a8fe65b8SJeff Kirsher CH_8139B,
492a8fe65b8SJeff Kirsher CH_8130,
493a8fe65b8SJeff Kirsher CH_8139C,
494a8fe65b8SJeff Kirsher CH_8100,
495a8fe65b8SJeff Kirsher CH_8100B_8139D,
496a8fe65b8SJeff Kirsher CH_8101,
497a8fe65b8SJeff Kirsher } chip_t;
498a8fe65b8SJeff Kirsher
499a8fe65b8SJeff Kirsher enum chip_flags {
500a8fe65b8SJeff Kirsher HasHltClk = (1 << 0),
501a8fe65b8SJeff Kirsher HasLWake = (1 << 1),
502a8fe65b8SJeff Kirsher };
503a8fe65b8SJeff Kirsher
504a8fe65b8SJeff Kirsher #define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
505a8fe65b8SJeff Kirsher (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
506a8fe65b8SJeff Kirsher #define HW_REVID_MASK HW_REVID(1, 1, 1, 1, 1, 1, 1)
507a8fe65b8SJeff Kirsher
508a8fe65b8SJeff Kirsher /* directly indexed by chip_t, above */
509a8fe65b8SJeff Kirsher static const struct {
510a8fe65b8SJeff Kirsher const char *name;
511a8fe65b8SJeff Kirsher u32 version; /* from RTL8139C/RTL8139D docs */
512a8fe65b8SJeff Kirsher u32 flags;
513a8fe65b8SJeff Kirsher } rtl_chip_info[] = {
514a8fe65b8SJeff Kirsher { "RTL-8139",
515a8fe65b8SJeff Kirsher HW_REVID(1, 0, 0, 0, 0, 0, 0),
516a8fe65b8SJeff Kirsher HasHltClk,
517a8fe65b8SJeff Kirsher },
518a8fe65b8SJeff Kirsher
519a8fe65b8SJeff Kirsher { "RTL-8139 rev K",
520a8fe65b8SJeff Kirsher HW_REVID(1, 1, 0, 0, 0, 0, 0),
521a8fe65b8SJeff Kirsher HasHltClk,
522a8fe65b8SJeff Kirsher },
523a8fe65b8SJeff Kirsher
524a8fe65b8SJeff Kirsher { "RTL-8139A",
525a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 0, 0, 0, 0),
526a8fe65b8SJeff Kirsher HasHltClk, /* XXX undocumented? */
527a8fe65b8SJeff Kirsher },
528a8fe65b8SJeff Kirsher
529a8fe65b8SJeff Kirsher { "RTL-8139A rev G",
530a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 0, 0, 1, 0),
531a8fe65b8SJeff Kirsher HasHltClk, /* XXX undocumented? */
532a8fe65b8SJeff Kirsher },
533a8fe65b8SJeff Kirsher
534a8fe65b8SJeff Kirsher { "RTL-8139B",
535a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 1, 0, 0, 0),
536a8fe65b8SJeff Kirsher HasLWake,
537a8fe65b8SJeff Kirsher },
538a8fe65b8SJeff Kirsher
539a8fe65b8SJeff Kirsher { "RTL-8130",
540a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 1, 1, 0, 0),
541a8fe65b8SJeff Kirsher HasLWake,
542a8fe65b8SJeff Kirsher },
543a8fe65b8SJeff Kirsher
544a8fe65b8SJeff Kirsher { "RTL-8139C",
545a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 0, 1, 0, 0),
546a8fe65b8SJeff Kirsher HasLWake,
547a8fe65b8SJeff Kirsher },
548a8fe65b8SJeff Kirsher
549a8fe65b8SJeff Kirsher { "RTL-8100",
550a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 1, 0, 1, 0),
551a8fe65b8SJeff Kirsher HasLWake,
552a8fe65b8SJeff Kirsher },
553a8fe65b8SJeff Kirsher
554a8fe65b8SJeff Kirsher { "RTL-8100B/8139D",
555a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 0, 1, 0, 1),
556a8fe65b8SJeff Kirsher HasHltClk /* XXX undocumented? */
557a8fe65b8SJeff Kirsher | HasLWake,
558a8fe65b8SJeff Kirsher },
559a8fe65b8SJeff Kirsher
560a8fe65b8SJeff Kirsher { "RTL-8101",
561a8fe65b8SJeff Kirsher HW_REVID(1, 1, 1, 0, 1, 1, 1),
562a8fe65b8SJeff Kirsher HasLWake,
563a8fe65b8SJeff Kirsher },
564a8fe65b8SJeff Kirsher };
565a8fe65b8SJeff Kirsher
566a8fe65b8SJeff Kirsher struct rtl_extra_stats {
567a8fe65b8SJeff Kirsher unsigned long early_rx;
568a8fe65b8SJeff Kirsher unsigned long tx_buf_mapped;
569a8fe65b8SJeff Kirsher unsigned long tx_timeouts;
570a8fe65b8SJeff Kirsher unsigned long rx_lost_in_ring;
571a8fe65b8SJeff Kirsher };
572a8fe65b8SJeff Kirsher
5739184a227SJunchang Wang struct rtl8139_stats {
5749184a227SJunchang Wang u64 packets;
5759184a227SJunchang Wang u64 bytes;
5769184a227SJunchang Wang struct u64_stats_sync syncp;
5779184a227SJunchang Wang };
5789184a227SJunchang Wang
579a8fe65b8SJeff Kirsher struct rtl8139_private {
580a8fe65b8SJeff Kirsher void __iomem *mmio_addr;
581a8fe65b8SJeff Kirsher int drv_flags;
582a8fe65b8SJeff Kirsher struct pci_dev *pci_dev;
583a8fe65b8SJeff Kirsher u32 msg_enable;
584a8fe65b8SJeff Kirsher struct napi_struct napi;
585a8fe65b8SJeff Kirsher struct net_device *dev;
586a8fe65b8SJeff Kirsher
587a8fe65b8SJeff Kirsher unsigned char *rx_ring;
588a8fe65b8SJeff Kirsher unsigned int cur_rx; /* RX buf index of next pkt */
5899184a227SJunchang Wang struct rtl8139_stats rx_stats;
590a8fe65b8SJeff Kirsher dma_addr_t rx_ring_dma;
591a8fe65b8SJeff Kirsher
592a8fe65b8SJeff Kirsher unsigned int tx_flag;
593a8fe65b8SJeff Kirsher unsigned long cur_tx;
594a8fe65b8SJeff Kirsher unsigned long dirty_tx;
5959184a227SJunchang Wang struct rtl8139_stats tx_stats;
596a8fe65b8SJeff Kirsher unsigned char *tx_buf[NUM_TX_DESC]; /* Tx bounce buffers */
597a8fe65b8SJeff Kirsher unsigned char *tx_bufs; /* Tx bounce buffer region. */
598a8fe65b8SJeff Kirsher dma_addr_t tx_bufs_dma;
599a8fe65b8SJeff Kirsher
600a8fe65b8SJeff Kirsher signed char phys[4]; /* MII device addresses. */
601a8fe65b8SJeff Kirsher
602a8fe65b8SJeff Kirsher /* Twister tune state. */
603a8fe65b8SJeff Kirsher char twistie, twist_row, twist_col;
604a8fe65b8SJeff Kirsher
605a8fe65b8SJeff Kirsher unsigned int watchdog_fired : 1;
606a8fe65b8SJeff Kirsher unsigned int default_port : 4; /* Last dev->if_port value. */
607a8fe65b8SJeff Kirsher unsigned int have_thread : 1;
608a8fe65b8SJeff Kirsher
609a8fe65b8SJeff Kirsher spinlock_t lock;
610a8fe65b8SJeff Kirsher spinlock_t rx_lock;
611a8fe65b8SJeff Kirsher
612a8fe65b8SJeff Kirsher chip_t chipset;
613a8fe65b8SJeff Kirsher u32 rx_config;
614a8fe65b8SJeff Kirsher struct rtl_extra_stats xstats;
615a8fe65b8SJeff Kirsher
616a8fe65b8SJeff Kirsher struct delayed_work thread;
617a8fe65b8SJeff Kirsher
618a8fe65b8SJeff Kirsher struct mii_if_info mii;
619a8fe65b8SJeff Kirsher unsigned int regs_len;
620a8fe65b8SJeff Kirsher unsigned long fifo_copy_timeout;
621a8fe65b8SJeff Kirsher };
622a8fe65b8SJeff Kirsher
623a8fe65b8SJeff Kirsher MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
624a8fe65b8SJeff Kirsher MODULE_DESCRIPTION ("RealTek RTL-8139 Fast Ethernet driver");
625a8fe65b8SJeff Kirsher MODULE_LICENSE("GPL");
626a8fe65b8SJeff Kirsher MODULE_VERSION(DRV_VERSION);
627a8fe65b8SJeff Kirsher
62865712ec0SFrancois Romieu module_param(use_io, bool, 0);
629a8fe65b8SJeff Kirsher MODULE_PARM_DESC(use_io, "Force use of I/O access mode. 0=MMIO 1=PIO");
630a8fe65b8SJeff Kirsher module_param(multicast_filter_limit, int, 0);
631a8fe65b8SJeff Kirsher module_param_array(media, int, NULL, 0);
632a8fe65b8SJeff Kirsher module_param_array(full_duplex, int, NULL, 0);
633a8fe65b8SJeff Kirsher module_param(debug, int, 0);
634a8fe65b8SJeff Kirsher MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
635a8fe65b8SJeff Kirsher MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
636a8fe65b8SJeff Kirsher MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
637a8fe65b8SJeff Kirsher MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
638a8fe65b8SJeff Kirsher
639a8fe65b8SJeff Kirsher static int read_eeprom (void __iomem *ioaddr, int location, int addr_len);
640a8fe65b8SJeff Kirsher static int rtl8139_open (struct net_device *dev);
641a8fe65b8SJeff Kirsher static int mdio_read (struct net_device *dev, int phy_id, int location);
642a8fe65b8SJeff Kirsher static void mdio_write (struct net_device *dev, int phy_id, int location,
643a8fe65b8SJeff Kirsher int val);
644a8fe65b8SJeff Kirsher static void rtl8139_start_thread(struct rtl8139_private *tp);
6450290bd29SMichael S. Tsirkin static void rtl8139_tx_timeout (struct net_device *dev, unsigned int txqueue);
646a8fe65b8SJeff Kirsher static void rtl8139_init_ring (struct net_device *dev);
647a8fe65b8SJeff Kirsher static netdev_tx_t rtl8139_start_xmit (struct sk_buff *skb,
648a8fe65b8SJeff Kirsher struct net_device *dev);
649a8fe65b8SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
650a8fe65b8SJeff Kirsher static void rtl8139_poll_controller(struct net_device *dev);
651a8fe65b8SJeff Kirsher #endif
652a8fe65b8SJeff Kirsher static int rtl8139_set_mac_address(struct net_device *dev, void *p);
653a8fe65b8SJeff Kirsher static int rtl8139_poll(struct napi_struct *napi, int budget);
654a8fe65b8SJeff Kirsher static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance);
655a8fe65b8SJeff Kirsher static int rtl8139_close (struct net_device *dev);
656a8fe65b8SJeff Kirsher static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
657bc1f4470Sstephen hemminger static void rtl8139_get_stats64(struct net_device *dev,
658bc1f4470Sstephen hemminger struct rtnl_link_stats64 *stats);
659a8fe65b8SJeff Kirsher static void rtl8139_set_rx_mode (struct net_device *dev);
660a8fe65b8SJeff Kirsher static void __set_rx_mode (struct net_device *dev);
661a8fe65b8SJeff Kirsher static void rtl8139_hw_start (struct net_device *dev);
662a8fe65b8SJeff Kirsher static void rtl8139_thread (struct work_struct *work);
663a8fe65b8SJeff Kirsher static void rtl8139_tx_timeout_task(struct work_struct *work);
664a8fe65b8SJeff Kirsher static const struct ethtool_ops rtl8139_ethtool_ops;
665a8fe65b8SJeff Kirsher
666a8fe65b8SJeff Kirsher /* write MMIO register, with flush */
667a8fe65b8SJeff Kirsher /* Flush avoids rtl8139 bug w/ posted MMIO writes */
668a8fe65b8SJeff Kirsher #define RTL_W8_F(reg, val8) do { iowrite8 ((val8), ioaddr + (reg)); ioread8 (ioaddr + (reg)); } while (0)
669a8fe65b8SJeff Kirsher #define RTL_W16_F(reg, val16) do { iowrite16 ((val16), ioaddr + (reg)); ioread16 (ioaddr + (reg)); } while (0)
670a8fe65b8SJeff Kirsher #define RTL_W32_F(reg, val32) do { iowrite32 ((val32), ioaddr + (reg)); ioread32 (ioaddr + (reg)); } while (0)
671a8fe65b8SJeff Kirsher
672a8fe65b8SJeff Kirsher /* write MMIO register */
673a8fe65b8SJeff Kirsher #define RTL_W8(reg, val8) iowrite8 ((val8), ioaddr + (reg))
674a8fe65b8SJeff Kirsher #define RTL_W16(reg, val16) iowrite16 ((val16), ioaddr + (reg))
675a8fe65b8SJeff Kirsher #define RTL_W32(reg, val32) iowrite32 ((val32), ioaddr + (reg))
676a8fe65b8SJeff Kirsher
677a8fe65b8SJeff Kirsher /* read MMIO register */
678a8fe65b8SJeff Kirsher #define RTL_R8(reg) ioread8 (ioaddr + (reg))
679a8fe65b8SJeff Kirsher #define RTL_R16(reg) ioread16 (ioaddr + (reg))
680a8fe65b8SJeff Kirsher #define RTL_R32(reg) ioread32 (ioaddr + (reg))
681a8fe65b8SJeff Kirsher
682a8fe65b8SJeff Kirsher
683a8fe65b8SJeff Kirsher static const u16 rtl8139_intr_mask =
684a8fe65b8SJeff Kirsher PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
685a8fe65b8SJeff Kirsher TxErr | TxOK | RxErr | RxOK;
686a8fe65b8SJeff Kirsher
687a8fe65b8SJeff Kirsher static const u16 rtl8139_norx_intr_mask =
688a8fe65b8SJeff Kirsher PCIErr | PCSTimeout | RxUnderrun |
689a8fe65b8SJeff Kirsher TxErr | TxOK | RxErr ;
690a8fe65b8SJeff Kirsher
691a8fe65b8SJeff Kirsher #if RX_BUF_IDX == 0
692a8fe65b8SJeff Kirsher static const unsigned int rtl8139_rx_config =
693a8fe65b8SJeff Kirsher RxCfgRcv8K | RxNoWrap |
694a8fe65b8SJeff Kirsher (RX_FIFO_THRESH << RxCfgFIFOShift) |
695a8fe65b8SJeff Kirsher (RX_DMA_BURST << RxCfgDMAShift);
696a8fe65b8SJeff Kirsher #elif RX_BUF_IDX == 1
697a8fe65b8SJeff Kirsher static const unsigned int rtl8139_rx_config =
698a8fe65b8SJeff Kirsher RxCfgRcv16K | RxNoWrap |
699a8fe65b8SJeff Kirsher (RX_FIFO_THRESH << RxCfgFIFOShift) |
700a8fe65b8SJeff Kirsher (RX_DMA_BURST << RxCfgDMAShift);
701a8fe65b8SJeff Kirsher #elif RX_BUF_IDX == 2
702a8fe65b8SJeff Kirsher static const unsigned int rtl8139_rx_config =
703a8fe65b8SJeff Kirsher RxCfgRcv32K | RxNoWrap |
704a8fe65b8SJeff Kirsher (RX_FIFO_THRESH << RxCfgFIFOShift) |
705a8fe65b8SJeff Kirsher (RX_DMA_BURST << RxCfgDMAShift);
706a8fe65b8SJeff Kirsher #elif RX_BUF_IDX == 3
707a8fe65b8SJeff Kirsher static const unsigned int rtl8139_rx_config =
708a8fe65b8SJeff Kirsher RxCfgRcv64K |
709a8fe65b8SJeff Kirsher (RX_FIFO_THRESH << RxCfgFIFOShift) |
710a8fe65b8SJeff Kirsher (RX_DMA_BURST << RxCfgDMAShift);
711a8fe65b8SJeff Kirsher #else
712a8fe65b8SJeff Kirsher #error "Invalid configuration for 8139_RXBUF_IDX"
713a8fe65b8SJeff Kirsher #endif
714a8fe65b8SJeff Kirsher
715a8fe65b8SJeff Kirsher static const unsigned int rtl8139_tx_config =
716a8fe65b8SJeff Kirsher TxIFG96 | (TX_DMA_BURST << TxDMAShift) | (TX_RETRY << TxRetryShift);
717a8fe65b8SJeff Kirsher
__rtl8139_cleanup_dev(struct net_device * dev)718a8fe65b8SJeff Kirsher static void __rtl8139_cleanup_dev (struct net_device *dev)
719a8fe65b8SJeff Kirsher {
720a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
721a8fe65b8SJeff Kirsher struct pci_dev *pdev;
722a8fe65b8SJeff Kirsher
723a8fe65b8SJeff Kirsher assert (dev != NULL);
724a8fe65b8SJeff Kirsher assert (tp->pci_dev != NULL);
725a8fe65b8SJeff Kirsher pdev = tp->pci_dev;
726a8fe65b8SJeff Kirsher
727a8fe65b8SJeff Kirsher if (tp->mmio_addr)
728a8fe65b8SJeff Kirsher pci_iounmap (pdev, tp->mmio_addr);
729a8fe65b8SJeff Kirsher
730a8fe65b8SJeff Kirsher /* it's ok to call this even if we have no regions to free */
731a8fe65b8SJeff Kirsher pci_release_regions (pdev);
732a8fe65b8SJeff Kirsher
733a8fe65b8SJeff Kirsher free_netdev(dev);
734a8fe65b8SJeff Kirsher }
735a8fe65b8SJeff Kirsher
736a8fe65b8SJeff Kirsher
rtl8139_chip_reset(void __iomem * ioaddr)737a8fe65b8SJeff Kirsher static void rtl8139_chip_reset (void __iomem *ioaddr)
738a8fe65b8SJeff Kirsher {
739a8fe65b8SJeff Kirsher int i;
740a8fe65b8SJeff Kirsher
741a8fe65b8SJeff Kirsher /* Soft reset the chip. */
742a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, CmdReset);
743a8fe65b8SJeff Kirsher
744a8fe65b8SJeff Kirsher /* Check that the chip has finished the reset. */
745a8fe65b8SJeff Kirsher for (i = 1000; i > 0; i--) {
746a8fe65b8SJeff Kirsher barrier();
747a8fe65b8SJeff Kirsher if ((RTL_R8 (ChipCmd) & CmdReset) == 0)
748a8fe65b8SJeff Kirsher break;
749a8fe65b8SJeff Kirsher udelay (10);
750a8fe65b8SJeff Kirsher }
751a8fe65b8SJeff Kirsher }
752a8fe65b8SJeff Kirsher
753a8fe65b8SJeff Kirsher
rtl8139_init_board(struct pci_dev * pdev)754b4e54a94SBill Pemberton static struct net_device *rtl8139_init_board(struct pci_dev *pdev)
755a8fe65b8SJeff Kirsher {
75665712ec0SFrancois Romieu struct device *d = &pdev->dev;
757a8fe65b8SJeff Kirsher void __iomem *ioaddr;
758a8fe65b8SJeff Kirsher struct net_device *dev;
759a8fe65b8SJeff Kirsher struct rtl8139_private *tp;
760a8fe65b8SJeff Kirsher u8 tmp8;
761a8fe65b8SJeff Kirsher int rc, disable_dev_on_err = 0;
76265712ec0SFrancois Romieu unsigned int i, bar;
76365712ec0SFrancois Romieu unsigned long io_len;
764a8fe65b8SJeff Kirsher u32 version;
76565712ec0SFrancois Romieu static const struct {
76665712ec0SFrancois Romieu unsigned long mask;
76765712ec0SFrancois Romieu char *type;
76865712ec0SFrancois Romieu } res[] = {
76965712ec0SFrancois Romieu { IORESOURCE_IO, "PIO" },
77065712ec0SFrancois Romieu { IORESOURCE_MEM, "MMIO" }
77165712ec0SFrancois Romieu };
772a8fe65b8SJeff Kirsher
773a8fe65b8SJeff Kirsher assert (pdev != NULL);
774a8fe65b8SJeff Kirsher
775a8fe65b8SJeff Kirsher /* dev and priv zeroed in alloc_etherdev */
776a8fe65b8SJeff Kirsher dev = alloc_etherdev (sizeof (*tp));
77741de8d4cSJoe Perches if (dev == NULL)
778a8fe65b8SJeff Kirsher return ERR_PTR(-ENOMEM);
77941de8d4cSJoe Perches
780a8fe65b8SJeff Kirsher SET_NETDEV_DEV(dev, &pdev->dev);
781a8fe65b8SJeff Kirsher
782a8fe65b8SJeff Kirsher tp = netdev_priv(dev);
783a8fe65b8SJeff Kirsher tp->pci_dev = pdev;
784a8fe65b8SJeff Kirsher
785a8fe65b8SJeff Kirsher /* enable device (incl. PCI PM wakeup and hotplug setup) */
786a8fe65b8SJeff Kirsher rc = pci_enable_device (pdev);
787a8fe65b8SJeff Kirsher if (rc)
788a8fe65b8SJeff Kirsher goto err_out;
789a8fe65b8SJeff Kirsher
7906402a577SJia-Ju Bai disable_dev_on_err = 1;
791a8fe65b8SJeff Kirsher rc = pci_request_regions (pdev, DRV_NAME);
792a8fe65b8SJeff Kirsher if (rc)
793a8fe65b8SJeff Kirsher goto err_out;
794a8fe65b8SJeff Kirsher
795a8fe65b8SJeff Kirsher pci_set_master (pdev);
796a8fe65b8SJeff Kirsher
797827da44cSJohn Stultz u64_stats_init(&tp->rx_stats.syncp);
798827da44cSJohn Stultz u64_stats_init(&tp->tx_stats.syncp);
799827da44cSJohn Stultz
80065712ec0SFrancois Romieu retry:
80165712ec0SFrancois Romieu /* PIO bar register comes first. */
80265712ec0SFrancois Romieu bar = !use_io;
80365712ec0SFrancois Romieu
80465712ec0SFrancois Romieu io_len = pci_resource_len(pdev, bar);
80565712ec0SFrancois Romieu
80665712ec0SFrancois Romieu dev_dbg(d, "%s region size = 0x%02lX\n", res[bar].type, io_len);
80765712ec0SFrancois Romieu
80865712ec0SFrancois Romieu if (!(pci_resource_flags(pdev, bar) & res[bar].mask)) {
80965712ec0SFrancois Romieu dev_err(d, "region #%d not a %s resource, aborting\n", bar,
81065712ec0SFrancois Romieu res[bar].type);
81165712ec0SFrancois Romieu rc = -ENODEV;
812a8fe65b8SJeff Kirsher goto err_out;
813a8fe65b8SJeff Kirsher }
81465712ec0SFrancois Romieu if (io_len < RTL_MIN_IO_SIZE) {
81565712ec0SFrancois Romieu dev_err(d, "Invalid PCI %s region size(s), aborting\n",
81665712ec0SFrancois Romieu res[bar].type);
81765712ec0SFrancois Romieu rc = -ENODEV;
81865712ec0SFrancois Romieu goto err_out;
81965712ec0SFrancois Romieu }
82065712ec0SFrancois Romieu
82165712ec0SFrancois Romieu ioaddr = pci_iomap(pdev, bar, 0);
82265712ec0SFrancois Romieu if (!ioaddr) {
82365712ec0SFrancois Romieu dev_err(d, "cannot map %s\n", res[bar].type);
82465712ec0SFrancois Romieu if (!use_io) {
82565712ec0SFrancois Romieu use_io = true;
826a8fe65b8SJeff Kirsher goto retry;
827a8fe65b8SJeff Kirsher }
82865712ec0SFrancois Romieu rc = -ENODEV;
82965712ec0SFrancois Romieu goto err_out;
830a8fe65b8SJeff Kirsher }
83165712ec0SFrancois Romieu tp->regs_len = io_len;
832a8fe65b8SJeff Kirsher tp->mmio_addr = ioaddr;
833a8fe65b8SJeff Kirsher
834a8fe65b8SJeff Kirsher /* Bring old chips out of low-power mode. */
835a8fe65b8SJeff Kirsher RTL_W8 (HltClk, 'R');
836a8fe65b8SJeff Kirsher
837a8fe65b8SJeff Kirsher /* check for missing/broken hardware */
838a8fe65b8SJeff Kirsher if (RTL_R32 (TxConfig) == 0xFFFFFFFF) {
839a8fe65b8SJeff Kirsher dev_err(&pdev->dev, "Chip not responding, ignoring board\n");
840a8fe65b8SJeff Kirsher rc = -EIO;
841a8fe65b8SJeff Kirsher goto err_out;
842a8fe65b8SJeff Kirsher }
843a8fe65b8SJeff Kirsher
844a8fe65b8SJeff Kirsher /* identify chip attached to board */
845a8fe65b8SJeff Kirsher version = RTL_R32 (TxConfig) & HW_REVID_MASK;
846a8fe65b8SJeff Kirsher for (i = 0; i < ARRAY_SIZE (rtl_chip_info); i++)
847a8fe65b8SJeff Kirsher if (version == rtl_chip_info[i].version) {
848a8fe65b8SJeff Kirsher tp->chipset = i;
849a8fe65b8SJeff Kirsher goto match;
850a8fe65b8SJeff Kirsher }
851a8fe65b8SJeff Kirsher
852a8fe65b8SJeff Kirsher /* if unknown chip, assume array element #0, original RTL-8139 in this case */
853a8fe65b8SJeff Kirsher i = 0;
854a8fe65b8SJeff Kirsher dev_dbg(&pdev->dev, "unknown chip version, assuming RTL-8139\n");
855a8fe65b8SJeff Kirsher dev_dbg(&pdev->dev, "TxConfig = 0x%x\n", RTL_R32 (TxConfig));
856a8fe65b8SJeff Kirsher tp->chipset = 0;
857a8fe65b8SJeff Kirsher
858a8fe65b8SJeff Kirsher match:
859a8fe65b8SJeff Kirsher pr_debug("chipset id (%d) == index %d, '%s'\n",
860a8fe65b8SJeff Kirsher version, i, rtl_chip_info[i].name);
861a8fe65b8SJeff Kirsher
862a8fe65b8SJeff Kirsher if (tp->chipset >= CH_8139B) {
863a8fe65b8SJeff Kirsher u8 new_tmp8 = tmp8 = RTL_R8 (Config1);
864a8fe65b8SJeff Kirsher pr_debug("PCI PM wakeup\n");
865a8fe65b8SJeff Kirsher if ((rtl_chip_info[tp->chipset].flags & HasLWake) &&
866a8fe65b8SJeff Kirsher (tmp8 & LWAKE))
867a8fe65b8SJeff Kirsher new_tmp8 &= ~LWAKE;
868a8fe65b8SJeff Kirsher new_tmp8 |= Cfg1_PM_Enable;
869a8fe65b8SJeff Kirsher if (new_tmp8 != tmp8) {
870a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
871a8fe65b8SJeff Kirsher RTL_W8 (Config1, tmp8);
872a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
873a8fe65b8SJeff Kirsher }
874a8fe65b8SJeff Kirsher if (rtl_chip_info[tp->chipset].flags & HasLWake) {
875a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (Config4);
876a8fe65b8SJeff Kirsher if (tmp8 & LWPTN) {
877a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
878a8fe65b8SJeff Kirsher RTL_W8 (Config4, tmp8 & ~LWPTN);
879a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
880a8fe65b8SJeff Kirsher }
881a8fe65b8SJeff Kirsher }
882a8fe65b8SJeff Kirsher } else {
883a8fe65b8SJeff Kirsher pr_debug("Old chip wakeup\n");
884a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (Config1);
885a8fe65b8SJeff Kirsher tmp8 &= ~(SLEEP | PWRDN);
886a8fe65b8SJeff Kirsher RTL_W8 (Config1, tmp8);
887a8fe65b8SJeff Kirsher }
888a8fe65b8SJeff Kirsher
889a8fe65b8SJeff Kirsher rtl8139_chip_reset (ioaddr);
890a8fe65b8SJeff Kirsher
891a8fe65b8SJeff Kirsher return dev;
892a8fe65b8SJeff Kirsher
893a8fe65b8SJeff Kirsher err_out:
894a8fe65b8SJeff Kirsher __rtl8139_cleanup_dev (dev);
895a8fe65b8SJeff Kirsher if (disable_dev_on_err)
896a8fe65b8SJeff Kirsher pci_disable_device (pdev);
897a8fe65b8SJeff Kirsher return ERR_PTR(rc);
898a8fe65b8SJeff Kirsher }
899a8fe65b8SJeff Kirsher
rtl8139_set_features(struct net_device * dev,netdev_features_t features)900d95089dcSBen Greear static int rtl8139_set_features(struct net_device *dev, netdev_features_t features)
901d95089dcSBen Greear {
902d95089dcSBen Greear struct rtl8139_private *tp = netdev_priv(dev);
903d95089dcSBen Greear unsigned long flags;
904d95089dcSBen Greear netdev_features_t changed = features ^ dev->features;
905d95089dcSBen Greear void __iomem *ioaddr = tp->mmio_addr;
906d95089dcSBen Greear
907d95089dcSBen Greear if (!(changed & (NETIF_F_RXALL)))
908d95089dcSBen Greear return 0;
909d95089dcSBen Greear
910d95089dcSBen Greear spin_lock_irqsave(&tp->lock, flags);
911d95089dcSBen Greear
912d95089dcSBen Greear if (changed & NETIF_F_RXALL) {
913d95089dcSBen Greear int rx_mode = tp->rx_config;
914d95089dcSBen Greear if (features & NETIF_F_RXALL)
915d95089dcSBen Greear rx_mode |= (AcceptErr | AcceptRunt);
916d95089dcSBen Greear else
917d95089dcSBen Greear rx_mode &= ~(AcceptErr | AcceptRunt);
918d95089dcSBen Greear tp->rx_config = rtl8139_rx_config | rx_mode;
919d95089dcSBen Greear RTL_W32_F(RxConfig, tp->rx_config);
920d95089dcSBen Greear }
921d95089dcSBen Greear
922d95089dcSBen Greear spin_unlock_irqrestore(&tp->lock, flags);
923d95089dcSBen Greear
924d95089dcSBen Greear return 0;
925d95089dcSBen Greear }
926d95089dcSBen Greear
927a8fe65b8SJeff Kirsher static const struct net_device_ops rtl8139_netdev_ops = {
928a8fe65b8SJeff Kirsher .ndo_open = rtl8139_open,
929a8fe65b8SJeff Kirsher .ndo_stop = rtl8139_close,
9309184a227SJunchang Wang .ndo_get_stats64 = rtl8139_get_stats64,
931a8fe65b8SJeff Kirsher .ndo_validate_addr = eth_validate_addr,
932a8fe65b8SJeff Kirsher .ndo_set_mac_address = rtl8139_set_mac_address,
933a8fe65b8SJeff Kirsher .ndo_start_xmit = rtl8139_start_xmit,
934afc4b13dSJiri Pirko .ndo_set_rx_mode = rtl8139_set_rx_mode,
935a7605370SArnd Bergmann .ndo_eth_ioctl = netdev_ioctl,
936a8fe65b8SJeff Kirsher .ndo_tx_timeout = rtl8139_tx_timeout,
937a8fe65b8SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
938a8fe65b8SJeff Kirsher .ndo_poll_controller = rtl8139_poll_controller,
939a8fe65b8SJeff Kirsher #endif
940d95089dcSBen Greear .ndo_set_features = rtl8139_set_features,
941a8fe65b8SJeff Kirsher };
942a8fe65b8SJeff Kirsher
rtl8139_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)943b4e54a94SBill Pemberton static int rtl8139_init_one(struct pci_dev *pdev,
944a8fe65b8SJeff Kirsher const struct pci_device_id *ent)
945a8fe65b8SJeff Kirsher {
946a8fe65b8SJeff Kirsher struct net_device *dev = NULL;
947a8fe65b8SJeff Kirsher struct rtl8139_private *tp;
948923ca6f6SJakub Kicinski __le16 addr[ETH_ALEN / 2];
949a8fe65b8SJeff Kirsher int i, addr_len, option;
950a8fe65b8SJeff Kirsher void __iomem *ioaddr;
951a8fe65b8SJeff Kirsher static int board_idx = -1;
952a8fe65b8SJeff Kirsher
953a8fe65b8SJeff Kirsher assert (pdev != NULL);
954a8fe65b8SJeff Kirsher assert (ent != NULL);
955a8fe65b8SJeff Kirsher
956a8fe65b8SJeff Kirsher board_idx++;
957a8fe65b8SJeff Kirsher
958a8fe65b8SJeff Kirsher /* when we're built into the kernel, the driver version message
959a8fe65b8SJeff Kirsher * is only printed if at least one 8139 board has been found
960a8fe65b8SJeff Kirsher */
961a8fe65b8SJeff Kirsher #ifndef MODULE
962a8fe65b8SJeff Kirsher {
963a8fe65b8SJeff Kirsher static int printed_version;
964a8fe65b8SJeff Kirsher if (!printed_version++)
965a8fe65b8SJeff Kirsher pr_info(RTL8139_DRIVER_NAME "\n");
966a8fe65b8SJeff Kirsher }
967a8fe65b8SJeff Kirsher #endif
968a8fe65b8SJeff Kirsher
969a8fe65b8SJeff Kirsher if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
970a8fe65b8SJeff Kirsher pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pdev->revision >= 0x20) {
971a8fe65b8SJeff Kirsher dev_info(&pdev->dev,
972a8fe65b8SJeff Kirsher "This (id %04x:%04x rev %02x) is an enhanced 8139C+ chip, use 8139cp\n",
973a8fe65b8SJeff Kirsher pdev->vendor, pdev->device, pdev->revision);
974a8fe65b8SJeff Kirsher return -ENODEV;
975a8fe65b8SJeff Kirsher }
976a8fe65b8SJeff Kirsher
977a8fe65b8SJeff Kirsher if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
978a8fe65b8SJeff Kirsher pdev->device == PCI_DEVICE_ID_REALTEK_8139 &&
979a8fe65b8SJeff Kirsher pdev->subsystem_vendor == PCI_VENDOR_ID_ATHEROS &&
980a8fe65b8SJeff Kirsher pdev->subsystem_device == PCI_DEVICE_ID_REALTEK_8139) {
981a8fe65b8SJeff Kirsher pr_info("OQO Model 2 detected. Forcing PIO\n");
98265dc8e12SJason Yan use_io = true;
983a8fe65b8SJeff Kirsher }
984a8fe65b8SJeff Kirsher
985a8fe65b8SJeff Kirsher dev = rtl8139_init_board (pdev);
986a8fe65b8SJeff Kirsher if (IS_ERR(dev))
987a8fe65b8SJeff Kirsher return PTR_ERR(dev);
988a8fe65b8SJeff Kirsher
989a8fe65b8SJeff Kirsher assert (dev != NULL);
990a8fe65b8SJeff Kirsher tp = netdev_priv(dev);
991a8fe65b8SJeff Kirsher tp->dev = dev;
992a8fe65b8SJeff Kirsher
993a8fe65b8SJeff Kirsher ioaddr = tp->mmio_addr;
994a8fe65b8SJeff Kirsher assert (ioaddr != NULL);
995a8fe65b8SJeff Kirsher
996a8fe65b8SJeff Kirsher addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
997a8fe65b8SJeff Kirsher for (i = 0; i < 3; i++)
998923ca6f6SJakub Kicinski addr[i] = cpu_to_le16(read_eeprom (ioaddr, i + 7, addr_len));
999923ca6f6SJakub Kicinski eth_hw_addr_set(dev, (u8 *)addr);
1000a8fe65b8SJeff Kirsher
1001a8fe65b8SJeff Kirsher /* The Rtl8139-specific entries in the device structure. */
1002a8fe65b8SJeff Kirsher dev->netdev_ops = &rtl8139_netdev_ops;
1003a8fe65b8SJeff Kirsher dev->ethtool_ops = &rtl8139_ethtool_ops;
1004a8fe65b8SJeff Kirsher dev->watchdog_timeo = TX_TIMEOUT;
1005b48b89f9SJakub Kicinski netif_napi_add(dev, &tp->napi, rtl8139_poll);
1006a8fe65b8SJeff Kirsher
1007a8fe65b8SJeff Kirsher /* note: the hardware is not capable of sg/csum/highdma, however
1008a8fe65b8SJeff Kirsher * through the use of skb_copy_and_csum_dev we enable these
1009a8fe65b8SJeff Kirsher * features
1010a8fe65b8SJeff Kirsher */
1011a8fe65b8SJeff Kirsher dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA;
1012a8fe65b8SJeff Kirsher dev->vlan_features = dev->features;
1013a8fe65b8SJeff Kirsher
1014d95089dcSBen Greear dev->hw_features |= NETIF_F_RXALL;
1015eeb69aa4SBen Greear dev->hw_features |= NETIF_F_RXFCS;
1016d95089dcSBen Greear
1017c7315a95SJarod Wilson /* MTU range: 68 - 1770 */
1018c7315a95SJarod Wilson dev->min_mtu = ETH_MIN_MTU;
1019c7315a95SJarod Wilson dev->max_mtu = MAX_ETH_DATA_SIZE;
1020c7315a95SJarod Wilson
1021a8fe65b8SJeff Kirsher /* tp zeroed and aligned in alloc_etherdev */
1022a8fe65b8SJeff Kirsher tp = netdev_priv(dev);
1023a8fe65b8SJeff Kirsher
1024a8fe65b8SJeff Kirsher /* note: tp->chipset set in rtl8139_init_board */
1025a8fe65b8SJeff Kirsher tp->drv_flags = board_info[ent->driver_data].hw_flags;
1026a8fe65b8SJeff Kirsher tp->mmio_addr = ioaddr;
1027a8fe65b8SJeff Kirsher tp->msg_enable =
1028a8fe65b8SJeff Kirsher (debug < 0 ? RTL8139_DEF_MSG_ENABLE : ((1 << debug) - 1));
1029a8fe65b8SJeff Kirsher spin_lock_init (&tp->lock);
1030a8fe65b8SJeff Kirsher spin_lock_init (&tp->rx_lock);
1031a8fe65b8SJeff Kirsher INIT_DELAYED_WORK(&tp->thread, rtl8139_thread);
1032a8fe65b8SJeff Kirsher tp->mii.dev = dev;
1033a8fe65b8SJeff Kirsher tp->mii.mdio_read = mdio_read;
1034a8fe65b8SJeff Kirsher tp->mii.mdio_write = mdio_write;
1035a8fe65b8SJeff Kirsher tp->mii.phy_id_mask = 0x3f;
1036a8fe65b8SJeff Kirsher tp->mii.reg_num_mask = 0x1f;
1037a8fe65b8SJeff Kirsher
1038a8fe65b8SJeff Kirsher /* dev is fully set up and ready to use now */
1039a8fe65b8SJeff Kirsher pr_debug("about to register device named %s (%p)...\n",
1040a8fe65b8SJeff Kirsher dev->name, dev);
1041a8fe65b8SJeff Kirsher i = register_netdev (dev);
1042a8fe65b8SJeff Kirsher if (i) goto err_out;
1043a8fe65b8SJeff Kirsher
1044a8fe65b8SJeff Kirsher pci_set_drvdata (pdev, dev);
1045a8fe65b8SJeff Kirsher
104665712ec0SFrancois Romieu netdev_info(dev, "%s at 0x%p, %pM, IRQ %d\n",
1047a8fe65b8SJeff Kirsher board_info[ent->driver_data].name,
104865712ec0SFrancois Romieu ioaddr, dev->dev_addr, pdev->irq);
1049a8fe65b8SJeff Kirsher
1050a8fe65b8SJeff Kirsher netdev_dbg(dev, "Identified 8139 chip type '%s'\n",
1051a8fe65b8SJeff Kirsher rtl_chip_info[tp->chipset].name);
1052a8fe65b8SJeff Kirsher
1053a8fe65b8SJeff Kirsher /* Find the connected MII xcvrs.
1054a8fe65b8SJeff Kirsher Doing this in open() would allow detecting external xcvrs later, but
1055a8fe65b8SJeff Kirsher takes too much time. */
1056a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1057a8fe65b8SJeff Kirsher if (tp->drv_flags & HAS_MII_XCVR) {
1058a8fe65b8SJeff Kirsher int phy, phy_idx = 0;
1059a8fe65b8SJeff Kirsher for (phy = 0; phy < 32 && phy_idx < sizeof(tp->phys); phy++) {
1060a8fe65b8SJeff Kirsher int mii_status = mdio_read(dev, phy, 1);
1061a8fe65b8SJeff Kirsher if (mii_status != 0xffff && mii_status != 0x0000) {
1062a8fe65b8SJeff Kirsher u16 advertising = mdio_read(dev, phy, 4);
1063a8fe65b8SJeff Kirsher tp->phys[phy_idx++] = phy;
1064a8fe65b8SJeff Kirsher netdev_info(dev, "MII transceiver %d status 0x%04x advertising %04x\n",
1065a8fe65b8SJeff Kirsher phy, mii_status, advertising);
1066a8fe65b8SJeff Kirsher }
1067a8fe65b8SJeff Kirsher }
1068a8fe65b8SJeff Kirsher if (phy_idx == 0) {
1069a8fe65b8SJeff Kirsher netdev_info(dev, "No MII transceivers found! Assuming SYM transceiver\n");
1070a8fe65b8SJeff Kirsher tp->phys[0] = 32;
1071a8fe65b8SJeff Kirsher }
1072a8fe65b8SJeff Kirsher } else
1073a8fe65b8SJeff Kirsher #endif
1074a8fe65b8SJeff Kirsher tp->phys[0] = 32;
1075a8fe65b8SJeff Kirsher tp->mii.phy_id = tp->phys[0];
1076a8fe65b8SJeff Kirsher
1077a8fe65b8SJeff Kirsher /* The lower four bits are the media type. */
1078a8fe65b8SJeff Kirsher option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
1079a8fe65b8SJeff Kirsher if (option > 0) {
1080a8fe65b8SJeff Kirsher tp->mii.full_duplex = (option & 0x210) ? 1 : 0;
1081a8fe65b8SJeff Kirsher tp->default_port = option & 0xFF;
1082a8fe65b8SJeff Kirsher if (tp->default_port)
1083a8fe65b8SJeff Kirsher tp->mii.force_media = 1;
1084a8fe65b8SJeff Kirsher }
1085a8fe65b8SJeff Kirsher if (board_idx < MAX_UNITS && full_duplex[board_idx] > 0)
1086a8fe65b8SJeff Kirsher tp->mii.full_duplex = full_duplex[board_idx];
1087a8fe65b8SJeff Kirsher if (tp->mii.full_duplex) {
1088a8fe65b8SJeff Kirsher netdev_info(dev, "Media type forced to Full Duplex\n");
1089a8fe65b8SJeff Kirsher /* Changing the MII-advertised media because might prevent
1090a8fe65b8SJeff Kirsher re-connection. */
1091a8fe65b8SJeff Kirsher tp->mii.force_media = 1;
1092a8fe65b8SJeff Kirsher }
1093a8fe65b8SJeff Kirsher if (tp->default_port) {
1094a8fe65b8SJeff Kirsher netdev_info(dev, " Forcing %dMbps %s-duplex operation\n",
1095a8fe65b8SJeff Kirsher (option & 0x20 ? 100 : 10),
1096a8fe65b8SJeff Kirsher (option & 0x10 ? "full" : "half"));
1097a8fe65b8SJeff Kirsher mdio_write(dev, tp->phys[0], 0,
1098a8fe65b8SJeff Kirsher ((option & 0x20) ? 0x2000 : 0) | /* 100Mbps? */
1099a8fe65b8SJeff Kirsher ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
1100a8fe65b8SJeff Kirsher }
1101a8fe65b8SJeff Kirsher
1102a8fe65b8SJeff Kirsher /* Put the chip into low-power mode. */
1103a8fe65b8SJeff Kirsher if (rtl_chip_info[tp->chipset].flags & HasHltClk)
1104a8fe65b8SJeff Kirsher RTL_W8 (HltClk, 'H'); /* 'R' would leave the clock running. */
1105a8fe65b8SJeff Kirsher
1106a8fe65b8SJeff Kirsher return 0;
1107a8fe65b8SJeff Kirsher
1108a8fe65b8SJeff Kirsher err_out:
1109a8fe65b8SJeff Kirsher __rtl8139_cleanup_dev (dev);
1110a8fe65b8SJeff Kirsher pci_disable_device (pdev);
1111a8fe65b8SJeff Kirsher return i;
1112a8fe65b8SJeff Kirsher }
1113a8fe65b8SJeff Kirsher
1114a8fe65b8SJeff Kirsher
rtl8139_remove_one(struct pci_dev * pdev)1115b4e54a94SBill Pemberton static void rtl8139_remove_one(struct pci_dev *pdev)
1116a8fe65b8SJeff Kirsher {
1117a8fe65b8SJeff Kirsher struct net_device *dev = pci_get_drvdata (pdev);
1118a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1119a8fe65b8SJeff Kirsher
1120a8fe65b8SJeff Kirsher assert (dev != NULL);
1121a8fe65b8SJeff Kirsher
1122a8fe65b8SJeff Kirsher cancel_delayed_work_sync(&tp->thread);
1123a8fe65b8SJeff Kirsher
1124a8fe65b8SJeff Kirsher unregister_netdev (dev);
1125a8fe65b8SJeff Kirsher
1126a8fe65b8SJeff Kirsher __rtl8139_cleanup_dev (dev);
1127a8fe65b8SJeff Kirsher pci_disable_device (pdev);
1128a8fe65b8SJeff Kirsher }
1129a8fe65b8SJeff Kirsher
1130a8fe65b8SJeff Kirsher
1131a8fe65b8SJeff Kirsher /* Serial EEPROM section. */
1132a8fe65b8SJeff Kirsher
1133a8fe65b8SJeff Kirsher /* EEPROM_Ctrl bits. */
1134a8fe65b8SJeff Kirsher #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
1135a8fe65b8SJeff Kirsher #define EE_CS 0x08 /* EEPROM chip select. */
1136a8fe65b8SJeff Kirsher #define EE_DATA_WRITE 0x02 /* EEPROM chip data in. */
1137a8fe65b8SJeff Kirsher #define EE_WRITE_0 0x00
1138a8fe65b8SJeff Kirsher #define EE_WRITE_1 0x02
1139a8fe65b8SJeff Kirsher #define EE_DATA_READ 0x01 /* EEPROM chip data out. */
1140a8fe65b8SJeff Kirsher #define EE_ENB (0x80 | EE_CS)
1141a8fe65b8SJeff Kirsher
1142a8fe65b8SJeff Kirsher /* Delay between EEPROM clock transitions.
1143a8fe65b8SJeff Kirsher No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
1144a8fe65b8SJeff Kirsher */
1145a8fe65b8SJeff Kirsher
11467d03f5a4SJason Wang #define eeprom_delay() (void)RTL_R8(Cfg9346)
1147a8fe65b8SJeff Kirsher
1148a8fe65b8SJeff Kirsher /* The EEPROM commands include the alway-set leading bit. */
1149a8fe65b8SJeff Kirsher #define EE_WRITE_CMD (5)
1150a8fe65b8SJeff Kirsher #define EE_READ_CMD (6)
1151a8fe65b8SJeff Kirsher #define EE_ERASE_CMD (7)
1152a8fe65b8SJeff Kirsher
read_eeprom(void __iomem * ioaddr,int location,int addr_len)1153b4e54a94SBill Pemberton static int read_eeprom(void __iomem *ioaddr, int location, int addr_len)
1154a8fe65b8SJeff Kirsher {
1155a8fe65b8SJeff Kirsher int i;
1156a8fe65b8SJeff Kirsher unsigned retval = 0;
1157a8fe65b8SJeff Kirsher int read_cmd = location | (EE_READ_CMD << addr_len);
1158a8fe65b8SJeff Kirsher
1159a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB & ~EE_CS);
1160a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB);
1161a8fe65b8SJeff Kirsher eeprom_delay ();
1162a8fe65b8SJeff Kirsher
1163a8fe65b8SJeff Kirsher /* Shift the read command bits out. */
1164a8fe65b8SJeff Kirsher for (i = 4 + addr_len; i >= 0; i--) {
1165a8fe65b8SJeff Kirsher int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1166a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB | dataval);
1167a8fe65b8SJeff Kirsher eeprom_delay ();
1168a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB | dataval | EE_SHIFT_CLK);
1169a8fe65b8SJeff Kirsher eeprom_delay ();
1170a8fe65b8SJeff Kirsher }
1171a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB);
1172a8fe65b8SJeff Kirsher eeprom_delay ();
1173a8fe65b8SJeff Kirsher
1174a8fe65b8SJeff Kirsher for (i = 16; i > 0; i--) {
1175a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB | EE_SHIFT_CLK);
1176a8fe65b8SJeff Kirsher eeprom_delay ();
1177a8fe65b8SJeff Kirsher retval =
1178a8fe65b8SJeff Kirsher (retval << 1) | ((RTL_R8 (Cfg9346) & EE_DATA_READ) ? 1 :
1179a8fe65b8SJeff Kirsher 0);
1180a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, EE_ENB);
1181a8fe65b8SJeff Kirsher eeprom_delay ();
1182a8fe65b8SJeff Kirsher }
1183a8fe65b8SJeff Kirsher
1184a8fe65b8SJeff Kirsher /* Terminate the EEPROM access. */
11850bc777bcSJason Wang RTL_W8(Cfg9346, 0);
1186a8fe65b8SJeff Kirsher eeprom_delay ();
1187a8fe65b8SJeff Kirsher
1188a8fe65b8SJeff Kirsher return retval;
1189a8fe65b8SJeff Kirsher }
1190a8fe65b8SJeff Kirsher
1191a8fe65b8SJeff Kirsher /* MII serial management: mostly bogus for now. */
1192a8fe65b8SJeff Kirsher /* Read and write the MII management registers using software-generated
1193a8fe65b8SJeff Kirsher serial MDIO protocol.
1194a8fe65b8SJeff Kirsher The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
1195a8fe65b8SJeff Kirsher met by back-to-back PCI I/O cycles, but we insert a delay to avoid
1196a8fe65b8SJeff Kirsher "overclocking" issues. */
1197a8fe65b8SJeff Kirsher #define MDIO_DIR 0x80
1198a8fe65b8SJeff Kirsher #define MDIO_DATA_OUT 0x04
1199a8fe65b8SJeff Kirsher #define MDIO_DATA_IN 0x02
1200a8fe65b8SJeff Kirsher #define MDIO_CLK 0x01
1201a8fe65b8SJeff Kirsher #define MDIO_WRITE0 (MDIO_DIR)
1202a8fe65b8SJeff Kirsher #define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
1203a8fe65b8SJeff Kirsher
1204a8fe65b8SJeff Kirsher #define mdio_delay() RTL_R8(Config4)
1205a8fe65b8SJeff Kirsher
1206a8fe65b8SJeff Kirsher
1207a8fe65b8SJeff Kirsher static const char mii_2_8139_map[8] = {
1208a8fe65b8SJeff Kirsher BasicModeCtrl,
1209a8fe65b8SJeff Kirsher BasicModeStatus,
1210a8fe65b8SJeff Kirsher 0,
1211a8fe65b8SJeff Kirsher 0,
1212a8fe65b8SJeff Kirsher NWayAdvert,
1213a8fe65b8SJeff Kirsher NWayLPAR,
1214a8fe65b8SJeff Kirsher NWayExpansion,
1215a8fe65b8SJeff Kirsher 0
1216a8fe65b8SJeff Kirsher };
1217a8fe65b8SJeff Kirsher
1218a8fe65b8SJeff Kirsher
1219a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1220a8fe65b8SJeff Kirsher /* Syncronize the MII management interface by shifting 32 one bits out. */
mdio_sync(void __iomem * ioaddr)1221a8fe65b8SJeff Kirsher static void mdio_sync (void __iomem *ioaddr)
1222a8fe65b8SJeff Kirsher {
1223a8fe65b8SJeff Kirsher int i;
1224a8fe65b8SJeff Kirsher
1225a8fe65b8SJeff Kirsher for (i = 32; i >= 0; i--) {
1226a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_WRITE1);
1227a8fe65b8SJeff Kirsher mdio_delay ();
1228a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_WRITE1 | MDIO_CLK);
1229a8fe65b8SJeff Kirsher mdio_delay ();
1230a8fe65b8SJeff Kirsher }
1231a8fe65b8SJeff Kirsher }
1232a8fe65b8SJeff Kirsher #endif
1233a8fe65b8SJeff Kirsher
mdio_read(struct net_device * dev,int phy_id,int location)1234a8fe65b8SJeff Kirsher static int mdio_read (struct net_device *dev, int phy_id, int location)
1235a8fe65b8SJeff Kirsher {
1236a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1237a8fe65b8SJeff Kirsher int retval = 0;
1238a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1239a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1240a8fe65b8SJeff Kirsher int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
1241a8fe65b8SJeff Kirsher int i;
1242a8fe65b8SJeff Kirsher #endif
1243a8fe65b8SJeff Kirsher
1244a8fe65b8SJeff Kirsher if (phy_id > 31) { /* Really a 8139. Use internal registers. */
1245a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1246a8fe65b8SJeff Kirsher return location < 8 && mii_2_8139_map[location] ?
1247a8fe65b8SJeff Kirsher RTL_R16 (mii_2_8139_map[location]) : 0;
1248a8fe65b8SJeff Kirsher }
1249a8fe65b8SJeff Kirsher
1250a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1251a8fe65b8SJeff Kirsher mdio_sync (ioaddr);
1252a8fe65b8SJeff Kirsher /* Shift the read command bits out. */
1253a8fe65b8SJeff Kirsher for (i = 15; i >= 0; i--) {
1254a8fe65b8SJeff Kirsher int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
1255a8fe65b8SJeff Kirsher
1256a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_DIR | dataval);
1257a8fe65b8SJeff Kirsher mdio_delay ();
1258a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_DIR | dataval | MDIO_CLK);
1259a8fe65b8SJeff Kirsher mdio_delay ();
1260a8fe65b8SJeff Kirsher }
1261a8fe65b8SJeff Kirsher
1262a8fe65b8SJeff Kirsher /* Read the two transition, 16 data, and wire-idle bits. */
1263a8fe65b8SJeff Kirsher for (i = 19; i > 0; i--) {
1264a8fe65b8SJeff Kirsher RTL_W8 (Config4, 0);
1265a8fe65b8SJeff Kirsher mdio_delay ();
1266a8fe65b8SJeff Kirsher retval = (retval << 1) | ((RTL_R8 (Config4) & MDIO_DATA_IN) ? 1 : 0);
1267a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_CLK);
1268a8fe65b8SJeff Kirsher mdio_delay ();
1269a8fe65b8SJeff Kirsher }
1270a8fe65b8SJeff Kirsher #endif
1271a8fe65b8SJeff Kirsher
1272a8fe65b8SJeff Kirsher return (retval >> 1) & 0xffff;
1273a8fe65b8SJeff Kirsher }
1274a8fe65b8SJeff Kirsher
1275a8fe65b8SJeff Kirsher
mdio_write(struct net_device * dev,int phy_id,int location,int value)1276a8fe65b8SJeff Kirsher static void mdio_write (struct net_device *dev, int phy_id, int location,
1277a8fe65b8SJeff Kirsher int value)
1278a8fe65b8SJeff Kirsher {
1279a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1280a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1281a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1282a8fe65b8SJeff Kirsher int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
1283a8fe65b8SJeff Kirsher int i;
1284a8fe65b8SJeff Kirsher #endif
1285a8fe65b8SJeff Kirsher
1286a8fe65b8SJeff Kirsher if (phy_id > 31) { /* Really a 8139. Use internal registers. */
1287a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1288a8fe65b8SJeff Kirsher if (location == 0) {
1289a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
1290a8fe65b8SJeff Kirsher RTL_W16 (BasicModeCtrl, value);
1291a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
1292a8fe65b8SJeff Kirsher } else if (location < 8 && mii_2_8139_map[location])
1293a8fe65b8SJeff Kirsher RTL_W16 (mii_2_8139_map[location], value);
1294a8fe65b8SJeff Kirsher return;
1295a8fe65b8SJeff Kirsher }
1296a8fe65b8SJeff Kirsher
1297a8fe65b8SJeff Kirsher #ifdef CONFIG_8139TOO_8129
1298a8fe65b8SJeff Kirsher mdio_sync (ioaddr);
1299a8fe65b8SJeff Kirsher
1300a8fe65b8SJeff Kirsher /* Shift the command bits out. */
1301a8fe65b8SJeff Kirsher for (i = 31; i >= 0; i--) {
1302a8fe65b8SJeff Kirsher int dataval =
1303a8fe65b8SJeff Kirsher (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
1304a8fe65b8SJeff Kirsher RTL_W8 (Config4, dataval);
1305a8fe65b8SJeff Kirsher mdio_delay ();
1306a8fe65b8SJeff Kirsher RTL_W8 (Config4, dataval | MDIO_CLK);
1307a8fe65b8SJeff Kirsher mdio_delay ();
1308a8fe65b8SJeff Kirsher }
1309a8fe65b8SJeff Kirsher /* Clear out extra bits. */
1310a8fe65b8SJeff Kirsher for (i = 2; i > 0; i--) {
1311a8fe65b8SJeff Kirsher RTL_W8 (Config4, 0);
1312a8fe65b8SJeff Kirsher mdio_delay ();
1313a8fe65b8SJeff Kirsher RTL_W8 (Config4, MDIO_CLK);
1314a8fe65b8SJeff Kirsher mdio_delay ();
1315a8fe65b8SJeff Kirsher }
1316a8fe65b8SJeff Kirsher #endif
1317a8fe65b8SJeff Kirsher }
1318a8fe65b8SJeff Kirsher
1319a8fe65b8SJeff Kirsher
rtl8139_open(struct net_device * dev)1320a8fe65b8SJeff Kirsher static int rtl8139_open (struct net_device *dev)
1321a8fe65b8SJeff Kirsher {
1322a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1323a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
132465712ec0SFrancois Romieu const int irq = tp->pci_dev->irq;
132565712ec0SFrancois Romieu int retval;
1326a8fe65b8SJeff Kirsher
132765712ec0SFrancois Romieu retval = request_irq(irq, rtl8139_interrupt, IRQF_SHARED, dev->name, dev);
1328a8fe65b8SJeff Kirsher if (retval)
1329a8fe65b8SJeff Kirsher return retval;
1330a8fe65b8SJeff Kirsher
1331a8fe65b8SJeff Kirsher tp->tx_bufs = dma_alloc_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
1332a8fe65b8SJeff Kirsher &tp->tx_bufs_dma, GFP_KERNEL);
1333a8fe65b8SJeff Kirsher tp->rx_ring = dma_alloc_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
1334a8fe65b8SJeff Kirsher &tp->rx_ring_dma, GFP_KERNEL);
1335a8fe65b8SJeff Kirsher if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
133665712ec0SFrancois Romieu free_irq(irq, dev);
1337a8fe65b8SJeff Kirsher
1338a8fe65b8SJeff Kirsher if (tp->tx_bufs)
1339a8fe65b8SJeff Kirsher dma_free_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
1340a8fe65b8SJeff Kirsher tp->tx_bufs, tp->tx_bufs_dma);
1341a8fe65b8SJeff Kirsher if (tp->rx_ring)
1342a8fe65b8SJeff Kirsher dma_free_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
1343a8fe65b8SJeff Kirsher tp->rx_ring, tp->rx_ring_dma);
1344a8fe65b8SJeff Kirsher
1345a8fe65b8SJeff Kirsher return -ENOMEM;
1346a8fe65b8SJeff Kirsher
1347a8fe65b8SJeff Kirsher }
1348a8fe65b8SJeff Kirsher
1349a8fe65b8SJeff Kirsher napi_enable(&tp->napi);
1350a8fe65b8SJeff Kirsher
1351a8fe65b8SJeff Kirsher tp->mii.full_duplex = tp->mii.force_media;
1352a8fe65b8SJeff Kirsher tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
1353a8fe65b8SJeff Kirsher
1354a8fe65b8SJeff Kirsher rtl8139_init_ring (dev);
1355a8fe65b8SJeff Kirsher rtl8139_hw_start (dev);
1356a8fe65b8SJeff Kirsher netif_start_queue (dev);
1357a8fe65b8SJeff Kirsher
1358a8fe65b8SJeff Kirsher netif_dbg(tp, ifup, dev,
1359a8fe65b8SJeff Kirsher "%s() ioaddr %#llx IRQ %d GP Pins %02x %s-duplex\n",
1360a8fe65b8SJeff Kirsher __func__,
1361a8fe65b8SJeff Kirsher (unsigned long long)pci_resource_start (tp->pci_dev, 1),
136265712ec0SFrancois Romieu irq, RTL_R8 (MediaStatus),
1363a8fe65b8SJeff Kirsher tp->mii.full_duplex ? "full" : "half");
1364a8fe65b8SJeff Kirsher
1365a8fe65b8SJeff Kirsher rtl8139_start_thread(tp);
1366a8fe65b8SJeff Kirsher
1367a8fe65b8SJeff Kirsher return 0;
1368a8fe65b8SJeff Kirsher }
1369a8fe65b8SJeff Kirsher
1370a8fe65b8SJeff Kirsher
rtl_check_media(struct net_device * dev,unsigned int init_media)1371a8fe65b8SJeff Kirsher static void rtl_check_media (struct net_device *dev, unsigned int init_media)
1372a8fe65b8SJeff Kirsher {
1373a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1374a8fe65b8SJeff Kirsher
1375a8fe65b8SJeff Kirsher if (tp->phys[0] >= 0) {
1376a8fe65b8SJeff Kirsher mii_check_media(&tp->mii, netif_msg_link(tp), init_media);
1377a8fe65b8SJeff Kirsher }
1378a8fe65b8SJeff Kirsher }
1379a8fe65b8SJeff Kirsher
1380a8fe65b8SJeff Kirsher /* Start the hardware at open or resume. */
rtl8139_hw_start(struct net_device * dev)1381a8fe65b8SJeff Kirsher static void rtl8139_hw_start (struct net_device *dev)
1382a8fe65b8SJeff Kirsher {
1383a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1384a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1385a8fe65b8SJeff Kirsher u32 i;
1386a8fe65b8SJeff Kirsher u8 tmp;
1387a8fe65b8SJeff Kirsher
1388a8fe65b8SJeff Kirsher /* Bring old chips out of low-power mode. */
1389a8fe65b8SJeff Kirsher if (rtl_chip_info[tp->chipset].flags & HasHltClk)
1390a8fe65b8SJeff Kirsher RTL_W8 (HltClk, 'R');
1391a8fe65b8SJeff Kirsher
1392a8fe65b8SJeff Kirsher rtl8139_chip_reset (ioaddr);
1393a8fe65b8SJeff Kirsher
1394a8fe65b8SJeff Kirsher /* unlock Config[01234] and BMCR register writes */
1395a8fe65b8SJeff Kirsher RTL_W8_F (Cfg9346, Cfg9346_Unlock);
1396a8fe65b8SJeff Kirsher /* Restore our idea of the MAC address. */
1397a8fe65b8SJeff Kirsher RTL_W32_F (MAC0 + 0, le32_to_cpu (*(__le32 *) (dev->dev_addr + 0)));
1398a8fe65b8SJeff Kirsher RTL_W32_F (MAC0 + 4, le16_to_cpu (*(__le16 *) (dev->dev_addr + 4)));
1399a8fe65b8SJeff Kirsher
1400a8fe65b8SJeff Kirsher tp->cur_rx = 0;
1401a8fe65b8SJeff Kirsher
1402a8fe65b8SJeff Kirsher /* init Rx ring buffer DMA address */
1403a8fe65b8SJeff Kirsher RTL_W32_F (RxBuf, tp->rx_ring_dma);
1404a8fe65b8SJeff Kirsher
1405a8fe65b8SJeff Kirsher /* Must enable Tx/Rx before setting transfer thresholds! */
1406a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1407a8fe65b8SJeff Kirsher
1408a8fe65b8SJeff Kirsher tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
1409a8fe65b8SJeff Kirsher RTL_W32 (RxConfig, tp->rx_config);
1410a8fe65b8SJeff Kirsher RTL_W32 (TxConfig, rtl8139_tx_config);
1411a8fe65b8SJeff Kirsher
1412a8fe65b8SJeff Kirsher rtl_check_media (dev, 1);
1413a8fe65b8SJeff Kirsher
1414a8fe65b8SJeff Kirsher if (tp->chipset >= CH_8139B) {
1415a8fe65b8SJeff Kirsher /* Disable magic packet scanning, which is enabled
1416a8fe65b8SJeff Kirsher * when PM is enabled in Config1. It can be reenabled
1417a8fe65b8SJeff Kirsher * via ETHTOOL_SWOL if desired. */
1418a8fe65b8SJeff Kirsher RTL_W8 (Config3, RTL_R8 (Config3) & ~Cfg3_Magic);
1419a8fe65b8SJeff Kirsher }
1420a8fe65b8SJeff Kirsher
1421a8fe65b8SJeff Kirsher netdev_dbg(dev, "init buffer addresses\n");
1422a8fe65b8SJeff Kirsher
1423a8fe65b8SJeff Kirsher /* Lock Config[01234] and BMCR register writes */
1424a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
1425a8fe65b8SJeff Kirsher
1426a8fe65b8SJeff Kirsher /* init Tx buffer DMA addresses */
1427a8fe65b8SJeff Kirsher for (i = 0; i < NUM_TX_DESC; i++)
1428a8fe65b8SJeff Kirsher RTL_W32_F (TxAddr0 + (i * 4), tp->tx_bufs_dma + (tp->tx_buf[i] - tp->tx_bufs));
1429a8fe65b8SJeff Kirsher
1430a8fe65b8SJeff Kirsher RTL_W32 (RxMissed, 0);
1431a8fe65b8SJeff Kirsher
1432a8fe65b8SJeff Kirsher rtl8139_set_rx_mode (dev);
1433a8fe65b8SJeff Kirsher
1434a8fe65b8SJeff Kirsher /* no early-rx interrupts */
1435a8fe65b8SJeff Kirsher RTL_W16 (MultiIntr, RTL_R16 (MultiIntr) & MultiIntrClear);
1436a8fe65b8SJeff Kirsher
1437a8fe65b8SJeff Kirsher /* make sure RxTx has started */
1438a8fe65b8SJeff Kirsher tmp = RTL_R8 (ChipCmd);
1439a8fe65b8SJeff Kirsher if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
1440a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1441a8fe65b8SJeff Kirsher
1442a8fe65b8SJeff Kirsher /* Enable all known interrupts by setting the interrupt mask. */
1443a8fe65b8SJeff Kirsher RTL_W16 (IntrMask, rtl8139_intr_mask);
1444a8fe65b8SJeff Kirsher }
1445a8fe65b8SJeff Kirsher
1446a8fe65b8SJeff Kirsher
1447a8fe65b8SJeff Kirsher /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
rtl8139_init_ring(struct net_device * dev)1448a8fe65b8SJeff Kirsher static void rtl8139_init_ring (struct net_device *dev)
1449a8fe65b8SJeff Kirsher {
1450a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1451a8fe65b8SJeff Kirsher int i;
1452a8fe65b8SJeff Kirsher
1453a8fe65b8SJeff Kirsher tp->cur_rx = 0;
1454a8fe65b8SJeff Kirsher tp->cur_tx = 0;
1455a8fe65b8SJeff Kirsher tp->dirty_tx = 0;
1456a8fe65b8SJeff Kirsher
1457a8fe65b8SJeff Kirsher for (i = 0; i < NUM_TX_DESC; i++)
1458a8fe65b8SJeff Kirsher tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
1459a8fe65b8SJeff Kirsher }
1460a8fe65b8SJeff Kirsher
1461a8fe65b8SJeff Kirsher
1462a8fe65b8SJeff Kirsher /* This must be global for CONFIG_8139TOO_TUNE_TWISTER case */
1463a8fe65b8SJeff Kirsher static int next_tick = 3 * HZ;
1464a8fe65b8SJeff Kirsher
1465a8fe65b8SJeff Kirsher #ifndef CONFIG_8139TOO_TUNE_TWISTER
rtl8139_tune_twister(struct net_device * dev,struct rtl8139_private * tp)1466a8fe65b8SJeff Kirsher static inline void rtl8139_tune_twister (struct net_device *dev,
1467a8fe65b8SJeff Kirsher struct rtl8139_private *tp) {}
1468a8fe65b8SJeff Kirsher #else
1469a8fe65b8SJeff Kirsher enum TwisterParamVals {
1470a8fe65b8SJeff Kirsher PARA78_default = 0x78fa8388,
1471a8fe65b8SJeff Kirsher PARA7c_default = 0xcb38de43, /* param[0][3] */
1472a8fe65b8SJeff Kirsher PARA7c_xxx = 0xcb38de43,
1473a8fe65b8SJeff Kirsher };
1474a8fe65b8SJeff Kirsher
1475a8fe65b8SJeff Kirsher static const unsigned long param[4][4] = {
1476a8fe65b8SJeff Kirsher {0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
1477a8fe65b8SJeff Kirsher {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1478a8fe65b8SJeff Kirsher {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1479a8fe65b8SJeff Kirsher {0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
1480a8fe65b8SJeff Kirsher };
1481a8fe65b8SJeff Kirsher
rtl8139_tune_twister(struct net_device * dev,struct rtl8139_private * tp)1482a8fe65b8SJeff Kirsher static void rtl8139_tune_twister (struct net_device *dev,
1483a8fe65b8SJeff Kirsher struct rtl8139_private *tp)
1484a8fe65b8SJeff Kirsher {
1485a8fe65b8SJeff Kirsher int linkcase;
1486a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1487a8fe65b8SJeff Kirsher
1488a8fe65b8SJeff Kirsher /* This is a complicated state machine to configure the "twister" for
1489a8fe65b8SJeff Kirsher impedance/echos based on the cable length.
1490a8fe65b8SJeff Kirsher All of this is magic and undocumented.
1491a8fe65b8SJeff Kirsher */
1492a8fe65b8SJeff Kirsher switch (tp->twistie) {
1493a8fe65b8SJeff Kirsher case 1:
1494a8fe65b8SJeff Kirsher if (RTL_R16 (CSCR) & CSCR_LinkOKBit) {
1495a8fe65b8SJeff Kirsher /* We have link beat, let us tune the twister. */
1496a8fe65b8SJeff Kirsher RTL_W16 (CSCR, CSCR_LinkDownOffCmd);
1497a8fe65b8SJeff Kirsher tp->twistie = 2; /* Change to state 2. */
1498a8fe65b8SJeff Kirsher next_tick = HZ / 10;
1499a8fe65b8SJeff Kirsher } else {
1500a8fe65b8SJeff Kirsher /* Just put in some reasonable defaults for when beat returns. */
1501a8fe65b8SJeff Kirsher RTL_W16 (CSCR, CSCR_LinkDownCmd);
1502a8fe65b8SJeff Kirsher RTL_W32 (FIFOTMS, 0x20); /* Turn on cable test mode. */
1503a8fe65b8SJeff Kirsher RTL_W32 (PARA78, PARA78_default);
1504a8fe65b8SJeff Kirsher RTL_W32 (PARA7c, PARA7c_default);
1505a8fe65b8SJeff Kirsher tp->twistie = 0; /* Bail from future actions. */
1506a8fe65b8SJeff Kirsher }
1507a8fe65b8SJeff Kirsher break;
1508a8fe65b8SJeff Kirsher case 2:
1509a8fe65b8SJeff Kirsher /* Read how long it took to hear the echo. */
1510a8fe65b8SJeff Kirsher linkcase = RTL_R16 (CSCR) & CSCR_LinkStatusBits;
1511a8fe65b8SJeff Kirsher if (linkcase == 0x7000)
1512a8fe65b8SJeff Kirsher tp->twist_row = 3;
1513a8fe65b8SJeff Kirsher else if (linkcase == 0x3000)
1514a8fe65b8SJeff Kirsher tp->twist_row = 2;
1515a8fe65b8SJeff Kirsher else if (linkcase == 0x1000)
1516a8fe65b8SJeff Kirsher tp->twist_row = 1;
1517a8fe65b8SJeff Kirsher else
1518a8fe65b8SJeff Kirsher tp->twist_row = 0;
1519a8fe65b8SJeff Kirsher tp->twist_col = 0;
1520a8fe65b8SJeff Kirsher tp->twistie = 3; /* Change to state 2. */
1521a8fe65b8SJeff Kirsher next_tick = HZ / 10;
1522a8fe65b8SJeff Kirsher break;
1523a8fe65b8SJeff Kirsher case 3:
1524a8fe65b8SJeff Kirsher /* Put out four tuning parameters, one per 100msec. */
1525a8fe65b8SJeff Kirsher if (tp->twist_col == 0)
1526a8fe65b8SJeff Kirsher RTL_W16 (FIFOTMS, 0);
1527a8fe65b8SJeff Kirsher RTL_W32 (PARA7c, param[(int) tp->twist_row]
1528a8fe65b8SJeff Kirsher [(int) tp->twist_col]);
1529a8fe65b8SJeff Kirsher next_tick = HZ / 10;
1530a8fe65b8SJeff Kirsher if (++tp->twist_col >= 4) {
1531a8fe65b8SJeff Kirsher /* For short cables we are done.
1532a8fe65b8SJeff Kirsher For long cables (row == 3) check for mistune. */
1533a8fe65b8SJeff Kirsher tp->twistie =
1534a8fe65b8SJeff Kirsher (tp->twist_row == 3) ? 4 : 0;
1535a8fe65b8SJeff Kirsher }
1536a8fe65b8SJeff Kirsher break;
1537a8fe65b8SJeff Kirsher case 4:
1538a8fe65b8SJeff Kirsher /* Special case for long cables: check for mistune. */
1539a8fe65b8SJeff Kirsher if ((RTL_R16 (CSCR) &
1540a8fe65b8SJeff Kirsher CSCR_LinkStatusBits) == 0x7000) {
1541a8fe65b8SJeff Kirsher tp->twistie = 0;
1542a8fe65b8SJeff Kirsher break;
1543a8fe65b8SJeff Kirsher } else {
1544a8fe65b8SJeff Kirsher RTL_W32 (PARA7c, 0xfb38de03);
1545a8fe65b8SJeff Kirsher tp->twistie = 5;
1546a8fe65b8SJeff Kirsher next_tick = HZ / 10;
1547a8fe65b8SJeff Kirsher }
1548a8fe65b8SJeff Kirsher break;
1549a8fe65b8SJeff Kirsher case 5:
1550a8fe65b8SJeff Kirsher /* Retune for shorter cable (column 2). */
1551a8fe65b8SJeff Kirsher RTL_W32 (FIFOTMS, 0x20);
1552a8fe65b8SJeff Kirsher RTL_W32 (PARA78, PARA78_default);
1553a8fe65b8SJeff Kirsher RTL_W32 (PARA7c, PARA7c_default);
1554a8fe65b8SJeff Kirsher RTL_W32 (FIFOTMS, 0x00);
1555a8fe65b8SJeff Kirsher tp->twist_row = 2;
1556a8fe65b8SJeff Kirsher tp->twist_col = 0;
1557a8fe65b8SJeff Kirsher tp->twistie = 3;
1558a8fe65b8SJeff Kirsher next_tick = HZ / 10;
1559a8fe65b8SJeff Kirsher break;
1560a8fe65b8SJeff Kirsher
1561a8fe65b8SJeff Kirsher default:
1562a8fe65b8SJeff Kirsher /* do nothing */
1563a8fe65b8SJeff Kirsher break;
1564a8fe65b8SJeff Kirsher }
1565a8fe65b8SJeff Kirsher }
1566a8fe65b8SJeff Kirsher #endif /* CONFIG_8139TOO_TUNE_TWISTER */
1567a8fe65b8SJeff Kirsher
rtl8139_thread_iter(struct net_device * dev,struct rtl8139_private * tp,void __iomem * ioaddr)1568a8fe65b8SJeff Kirsher static inline void rtl8139_thread_iter (struct net_device *dev,
1569a8fe65b8SJeff Kirsher struct rtl8139_private *tp,
1570a8fe65b8SJeff Kirsher void __iomem *ioaddr)
1571a8fe65b8SJeff Kirsher {
1572a8fe65b8SJeff Kirsher int mii_lpa;
1573a8fe65b8SJeff Kirsher
1574a8fe65b8SJeff Kirsher mii_lpa = mdio_read (dev, tp->phys[0], MII_LPA);
1575a8fe65b8SJeff Kirsher
1576a8fe65b8SJeff Kirsher if (!tp->mii.force_media && mii_lpa != 0xffff) {
1577a8fe65b8SJeff Kirsher int duplex = ((mii_lpa & LPA_100FULL) ||
1578a8fe65b8SJeff Kirsher (mii_lpa & 0x01C0) == 0x0040);
1579a8fe65b8SJeff Kirsher if (tp->mii.full_duplex != duplex) {
1580a8fe65b8SJeff Kirsher tp->mii.full_duplex = duplex;
1581a8fe65b8SJeff Kirsher
1582a8fe65b8SJeff Kirsher if (mii_lpa) {
1583a8fe65b8SJeff Kirsher netdev_info(dev, "Setting %s-duplex based on MII #%d link partner ability of %04x\n",
1584a8fe65b8SJeff Kirsher tp->mii.full_duplex ? "full" : "half",
1585a8fe65b8SJeff Kirsher tp->phys[0], mii_lpa);
1586a8fe65b8SJeff Kirsher } else {
1587a8fe65b8SJeff Kirsher netdev_info(dev, "media is unconnected, link down, or incompatible connection\n");
1588a8fe65b8SJeff Kirsher }
1589a8fe65b8SJeff Kirsher #if 0
1590a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
1591a8fe65b8SJeff Kirsher RTL_W8 (Config1, tp->mii.full_duplex ? 0x60 : 0x20);
1592a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
1593a8fe65b8SJeff Kirsher #endif
1594a8fe65b8SJeff Kirsher }
1595a8fe65b8SJeff Kirsher }
1596a8fe65b8SJeff Kirsher
1597a8fe65b8SJeff Kirsher next_tick = HZ * 60;
1598a8fe65b8SJeff Kirsher
1599a8fe65b8SJeff Kirsher rtl8139_tune_twister (dev, tp);
1600a8fe65b8SJeff Kirsher
1601a8fe65b8SJeff Kirsher netdev_dbg(dev, "Media selection tick, Link partner %04x\n",
1602a8fe65b8SJeff Kirsher RTL_R16(NWayLPAR));
1603a8fe65b8SJeff Kirsher netdev_dbg(dev, "Other registers are IntMask %04x IntStatus %04x\n",
1604a8fe65b8SJeff Kirsher RTL_R16(IntrMask), RTL_R16(IntrStatus));
1605a8fe65b8SJeff Kirsher netdev_dbg(dev, "Chip config %02x %02x\n",
1606a8fe65b8SJeff Kirsher RTL_R8(Config0), RTL_R8(Config1));
1607a8fe65b8SJeff Kirsher }
1608a8fe65b8SJeff Kirsher
rtl8139_thread(struct work_struct * work)1609a8fe65b8SJeff Kirsher static void rtl8139_thread (struct work_struct *work)
1610a8fe65b8SJeff Kirsher {
1611a8fe65b8SJeff Kirsher struct rtl8139_private *tp =
1612a8fe65b8SJeff Kirsher container_of(work, struct rtl8139_private, thread.work);
1613a8fe65b8SJeff Kirsher struct net_device *dev = tp->mii.dev;
1614a8fe65b8SJeff Kirsher unsigned long thr_delay = next_tick;
1615a8fe65b8SJeff Kirsher
1616a8fe65b8SJeff Kirsher rtnl_lock();
1617a8fe65b8SJeff Kirsher
1618a8fe65b8SJeff Kirsher if (!netif_running(dev))
1619a8fe65b8SJeff Kirsher goto out_unlock;
1620a8fe65b8SJeff Kirsher
1621a8fe65b8SJeff Kirsher if (tp->watchdog_fired) {
1622a8fe65b8SJeff Kirsher tp->watchdog_fired = 0;
1623a8fe65b8SJeff Kirsher rtl8139_tx_timeout_task(work);
1624a8fe65b8SJeff Kirsher } else
1625a8fe65b8SJeff Kirsher rtl8139_thread_iter(dev, tp, tp->mmio_addr);
1626a8fe65b8SJeff Kirsher
1627a8fe65b8SJeff Kirsher if (tp->have_thread)
1628a8fe65b8SJeff Kirsher schedule_delayed_work(&tp->thread, thr_delay);
1629a8fe65b8SJeff Kirsher out_unlock:
1630a8fe65b8SJeff Kirsher rtnl_unlock ();
1631a8fe65b8SJeff Kirsher }
1632a8fe65b8SJeff Kirsher
rtl8139_start_thread(struct rtl8139_private * tp)1633a8fe65b8SJeff Kirsher static void rtl8139_start_thread(struct rtl8139_private *tp)
1634a8fe65b8SJeff Kirsher {
1635a8fe65b8SJeff Kirsher tp->twistie = 0;
1636a8fe65b8SJeff Kirsher if (tp->chipset == CH_8139_K)
1637a8fe65b8SJeff Kirsher tp->twistie = 1;
1638a8fe65b8SJeff Kirsher else if (tp->drv_flags & HAS_LNK_CHNG)
1639a8fe65b8SJeff Kirsher return;
1640a8fe65b8SJeff Kirsher
1641a8fe65b8SJeff Kirsher tp->have_thread = 1;
1642a8fe65b8SJeff Kirsher tp->watchdog_fired = 0;
1643a8fe65b8SJeff Kirsher
1644a8fe65b8SJeff Kirsher schedule_delayed_work(&tp->thread, next_tick);
1645a8fe65b8SJeff Kirsher }
1646a8fe65b8SJeff Kirsher
rtl8139_tx_clear(struct rtl8139_private * tp)1647a8fe65b8SJeff Kirsher static inline void rtl8139_tx_clear (struct rtl8139_private *tp)
1648a8fe65b8SJeff Kirsher {
1649a8fe65b8SJeff Kirsher tp->cur_tx = 0;
1650a8fe65b8SJeff Kirsher tp->dirty_tx = 0;
1651a8fe65b8SJeff Kirsher
1652a8fe65b8SJeff Kirsher /* XXX account for unsent Tx packets in tp->stats.tx_dropped */
1653a8fe65b8SJeff Kirsher }
1654a8fe65b8SJeff Kirsher
rtl8139_tx_timeout_task(struct work_struct * work)1655a8fe65b8SJeff Kirsher static void rtl8139_tx_timeout_task (struct work_struct *work)
1656a8fe65b8SJeff Kirsher {
1657a8fe65b8SJeff Kirsher struct rtl8139_private *tp =
1658a8fe65b8SJeff Kirsher container_of(work, struct rtl8139_private, thread.work);
1659a8fe65b8SJeff Kirsher struct net_device *dev = tp->mii.dev;
1660a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1661a8fe65b8SJeff Kirsher int i;
1662a8fe65b8SJeff Kirsher u8 tmp8;
1663a8fe65b8SJeff Kirsher
16640a2f0d2dSChun-Hao Lin napi_disable(&tp->napi);
16650a2f0d2dSChun-Hao Lin netif_stop_queue(dev);
166616f11500SPaul E. McKenney synchronize_rcu();
16670a2f0d2dSChun-Hao Lin
1668a8fe65b8SJeff Kirsher netdev_dbg(dev, "Transmit timeout, status %02x %04x %04x media %02x\n",
1669a8fe65b8SJeff Kirsher RTL_R8(ChipCmd), RTL_R16(IntrStatus),
1670a8fe65b8SJeff Kirsher RTL_R16(IntrMask), RTL_R8(MediaStatus));
1671a8fe65b8SJeff Kirsher /* Emit info to figure out what went wrong. */
1672a8fe65b8SJeff Kirsher netdev_dbg(dev, "Tx queue start entry %ld dirty entry %ld\n",
1673a8fe65b8SJeff Kirsher tp->cur_tx, tp->dirty_tx);
1674a8fe65b8SJeff Kirsher for (i = 0; i < NUM_TX_DESC; i++)
1675a8fe65b8SJeff Kirsher netdev_dbg(dev, "Tx descriptor %d is %08x%s\n",
1676a8fe65b8SJeff Kirsher i, RTL_R32(TxStatus0 + (i * 4)),
1677a8fe65b8SJeff Kirsher i == tp->dirty_tx % NUM_TX_DESC ?
1678a8fe65b8SJeff Kirsher " (queue head)" : "");
1679a8fe65b8SJeff Kirsher
1680a8fe65b8SJeff Kirsher tp->xstats.tx_timeouts++;
1681a8fe65b8SJeff Kirsher
1682a8fe65b8SJeff Kirsher /* disable Tx ASAP, if not already */
1683a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (ChipCmd);
1684a8fe65b8SJeff Kirsher if (tmp8 & CmdTxEnb)
1685a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, CmdRxEnb);
1686a8fe65b8SJeff Kirsher
1687a8fe65b8SJeff Kirsher spin_lock_bh(&tp->rx_lock);
1688a8fe65b8SJeff Kirsher /* Disable interrupts by clearing the interrupt mask. */
1689a8fe65b8SJeff Kirsher RTL_W16 (IntrMask, 0x0000);
1690a8fe65b8SJeff Kirsher
1691a8fe65b8SJeff Kirsher /* Stop a shared interrupt from scavenging while we are. */
1692a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
1693a8fe65b8SJeff Kirsher rtl8139_tx_clear (tp);
1694a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
1695a8fe65b8SJeff Kirsher
1696a8fe65b8SJeff Kirsher /* ...and finally, reset everything */
16970a2f0d2dSChun-Hao Lin napi_enable(&tp->napi);
1698a8fe65b8SJeff Kirsher rtl8139_hw_start(dev);
1699a8fe65b8SJeff Kirsher netif_wake_queue(dev);
17000a2f0d2dSChun-Hao Lin
1701a8fe65b8SJeff Kirsher spin_unlock_bh(&tp->rx_lock);
1702a8fe65b8SJeff Kirsher }
1703a8fe65b8SJeff Kirsher
rtl8139_tx_timeout(struct net_device * dev,unsigned int txqueue)17040290bd29SMichael S. Tsirkin static void rtl8139_tx_timeout(struct net_device *dev, unsigned int txqueue)
1705a8fe65b8SJeff Kirsher {
1706a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1707a8fe65b8SJeff Kirsher
1708a8fe65b8SJeff Kirsher tp->watchdog_fired = 1;
1709a8fe65b8SJeff Kirsher if (!tp->have_thread) {
1710a8fe65b8SJeff Kirsher INIT_DELAYED_WORK(&tp->thread, rtl8139_thread);
1711a8fe65b8SJeff Kirsher schedule_delayed_work(&tp->thread, next_tick);
1712a8fe65b8SJeff Kirsher }
1713a8fe65b8SJeff Kirsher }
1714a8fe65b8SJeff Kirsher
rtl8139_start_xmit(struct sk_buff * skb,struct net_device * dev)1715a8fe65b8SJeff Kirsher static netdev_tx_t rtl8139_start_xmit (struct sk_buff *skb,
1716a8fe65b8SJeff Kirsher struct net_device *dev)
1717a8fe65b8SJeff Kirsher {
1718a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
1719a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1720a8fe65b8SJeff Kirsher unsigned int entry;
1721a8fe65b8SJeff Kirsher unsigned int len = skb->len;
1722a8fe65b8SJeff Kirsher unsigned long flags;
1723a8fe65b8SJeff Kirsher
1724a8fe65b8SJeff Kirsher /* Calculate the next Tx descriptor entry. */
1725a8fe65b8SJeff Kirsher entry = tp->cur_tx % NUM_TX_DESC;
1726a8fe65b8SJeff Kirsher
1727a8fe65b8SJeff Kirsher /* Note: the chip doesn't have auto-pad! */
1728a8fe65b8SJeff Kirsher if (likely(len < TX_BUF_SIZE)) {
1729a8fe65b8SJeff Kirsher if (len < ETH_ZLEN)
1730a8fe65b8SJeff Kirsher memset(tp->tx_buf[entry], 0, ETH_ZLEN);
1731a8fe65b8SJeff Kirsher skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
1732a2ccd2e4SEric W. Biederman dev_kfree_skb_any(skb);
1733a8fe65b8SJeff Kirsher } else {
1734a2ccd2e4SEric W. Biederman dev_kfree_skb_any(skb);
1735a8fe65b8SJeff Kirsher dev->stats.tx_dropped++;
1736a8fe65b8SJeff Kirsher return NETDEV_TX_OK;
1737a8fe65b8SJeff Kirsher }
1738a8fe65b8SJeff Kirsher
1739a8fe65b8SJeff Kirsher spin_lock_irqsave(&tp->lock, flags);
1740a8fe65b8SJeff Kirsher /*
1741a8fe65b8SJeff Kirsher * Writing to TxStatus triggers a DMA transfer of the data
1742a8fe65b8SJeff Kirsher * copied to tp->tx_buf[entry] above. Use a memory barrier
1743a8fe65b8SJeff Kirsher * to make sure that the device sees the updated data.
1744a8fe65b8SJeff Kirsher */
1745a8fe65b8SJeff Kirsher wmb();
1746a8fe65b8SJeff Kirsher RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
1747a8fe65b8SJeff Kirsher tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
1748a8fe65b8SJeff Kirsher
1749a8fe65b8SJeff Kirsher tp->cur_tx++;
1750a8fe65b8SJeff Kirsher
1751a8fe65b8SJeff Kirsher if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
1752a8fe65b8SJeff Kirsher netif_stop_queue (dev);
1753a8fe65b8SJeff Kirsher spin_unlock_irqrestore(&tp->lock, flags);
1754a8fe65b8SJeff Kirsher
1755a8fe65b8SJeff Kirsher netif_dbg(tp, tx_queued, dev, "Queued Tx packet size %u to slot %d\n",
1756a8fe65b8SJeff Kirsher len, entry);
1757a8fe65b8SJeff Kirsher
1758a8fe65b8SJeff Kirsher return NETDEV_TX_OK;
1759a8fe65b8SJeff Kirsher }
1760a8fe65b8SJeff Kirsher
1761a8fe65b8SJeff Kirsher
rtl8139_tx_interrupt(struct net_device * dev,struct rtl8139_private * tp,void __iomem * ioaddr)1762a8fe65b8SJeff Kirsher static void rtl8139_tx_interrupt (struct net_device *dev,
1763a8fe65b8SJeff Kirsher struct rtl8139_private *tp,
1764a8fe65b8SJeff Kirsher void __iomem *ioaddr)
1765a8fe65b8SJeff Kirsher {
1766a8fe65b8SJeff Kirsher unsigned long dirty_tx, tx_left;
1767a8fe65b8SJeff Kirsher
1768a8fe65b8SJeff Kirsher assert (dev != NULL);
1769a8fe65b8SJeff Kirsher assert (ioaddr != NULL);
1770a8fe65b8SJeff Kirsher
1771a8fe65b8SJeff Kirsher dirty_tx = tp->dirty_tx;
1772a8fe65b8SJeff Kirsher tx_left = tp->cur_tx - dirty_tx;
1773a8fe65b8SJeff Kirsher while (tx_left > 0) {
1774a8fe65b8SJeff Kirsher int entry = dirty_tx % NUM_TX_DESC;
1775a8fe65b8SJeff Kirsher int txstatus;
1776a8fe65b8SJeff Kirsher
1777a8fe65b8SJeff Kirsher txstatus = RTL_R32 (TxStatus0 + (entry * sizeof (u32)));
1778a8fe65b8SJeff Kirsher
1779a8fe65b8SJeff Kirsher if (!(txstatus & (TxStatOK | TxUnderrun | TxAborted)))
1780a8fe65b8SJeff Kirsher break; /* It still hasn't been Txed */
1781a8fe65b8SJeff Kirsher
1782a8fe65b8SJeff Kirsher /* Note: TxCarrierLost is always asserted at 100mbps. */
1783a8fe65b8SJeff Kirsher if (txstatus & (TxOutOfWindow | TxAborted)) {
1784a8fe65b8SJeff Kirsher /* There was an major error, log it. */
1785a8fe65b8SJeff Kirsher netif_dbg(tp, tx_err, dev, "Transmit error, Tx status %08x\n",
1786a8fe65b8SJeff Kirsher txstatus);
1787a8fe65b8SJeff Kirsher dev->stats.tx_errors++;
1788a8fe65b8SJeff Kirsher if (txstatus & TxAborted) {
1789a8fe65b8SJeff Kirsher dev->stats.tx_aborted_errors++;
1790a8fe65b8SJeff Kirsher RTL_W32 (TxConfig, TxClearAbt);
1791a8fe65b8SJeff Kirsher RTL_W16 (IntrStatus, TxErr);
1792a8fe65b8SJeff Kirsher wmb();
1793a8fe65b8SJeff Kirsher }
1794a8fe65b8SJeff Kirsher if (txstatus & TxCarrierLost)
1795a8fe65b8SJeff Kirsher dev->stats.tx_carrier_errors++;
1796a8fe65b8SJeff Kirsher if (txstatus & TxOutOfWindow)
1797a8fe65b8SJeff Kirsher dev->stats.tx_window_errors++;
1798a8fe65b8SJeff Kirsher } else {
1799a8fe65b8SJeff Kirsher if (txstatus & TxUnderrun) {
1800a8fe65b8SJeff Kirsher /* Add 64 to the Tx FIFO threshold. */
1801a8fe65b8SJeff Kirsher if (tp->tx_flag < 0x00300000)
1802a8fe65b8SJeff Kirsher tp->tx_flag += 0x00020000;
1803a8fe65b8SJeff Kirsher dev->stats.tx_fifo_errors++;
1804a8fe65b8SJeff Kirsher }
1805a8fe65b8SJeff Kirsher dev->stats.collisions += (txstatus >> 24) & 15;
18069184a227SJunchang Wang u64_stats_update_begin(&tp->tx_stats.syncp);
18079184a227SJunchang Wang tp->tx_stats.packets++;
18089184a227SJunchang Wang tp->tx_stats.bytes += txstatus & 0x7ff;
18099184a227SJunchang Wang u64_stats_update_end(&tp->tx_stats.syncp);
1810a8fe65b8SJeff Kirsher }
1811a8fe65b8SJeff Kirsher
1812a8fe65b8SJeff Kirsher dirty_tx++;
1813a8fe65b8SJeff Kirsher tx_left--;
1814a8fe65b8SJeff Kirsher }
1815a8fe65b8SJeff Kirsher
1816a8fe65b8SJeff Kirsher #ifndef RTL8139_NDEBUG
1817a8fe65b8SJeff Kirsher if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
1818a8fe65b8SJeff Kirsher netdev_err(dev, "Out-of-sync dirty pointer, %ld vs. %ld\n",
1819a8fe65b8SJeff Kirsher dirty_tx, tp->cur_tx);
1820a8fe65b8SJeff Kirsher dirty_tx += NUM_TX_DESC;
1821a8fe65b8SJeff Kirsher }
1822a8fe65b8SJeff Kirsher #endif /* RTL8139_NDEBUG */
1823a8fe65b8SJeff Kirsher
1824a8fe65b8SJeff Kirsher /* only wake the queue if we did work, and the queue is stopped */
1825a8fe65b8SJeff Kirsher if (tp->dirty_tx != dirty_tx) {
1826a8fe65b8SJeff Kirsher tp->dirty_tx = dirty_tx;
1827a8fe65b8SJeff Kirsher mb();
1828a8fe65b8SJeff Kirsher netif_wake_queue (dev);
1829a8fe65b8SJeff Kirsher }
1830a8fe65b8SJeff Kirsher }
1831a8fe65b8SJeff Kirsher
1832a8fe65b8SJeff Kirsher
1833a8fe65b8SJeff Kirsher /* TODO: clean this up! Rx reset need not be this intensive */
rtl8139_rx_err(u32 rx_status,struct net_device * dev,struct rtl8139_private * tp,void __iomem * ioaddr)1834a8fe65b8SJeff Kirsher static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
1835a8fe65b8SJeff Kirsher struct rtl8139_private *tp, void __iomem *ioaddr)
1836a8fe65b8SJeff Kirsher {
1837a8fe65b8SJeff Kirsher u8 tmp8;
1838a8fe65b8SJeff Kirsher #ifdef CONFIG_8139_OLD_RX_RESET
1839a8fe65b8SJeff Kirsher int tmp_work;
1840a8fe65b8SJeff Kirsher #endif
1841a8fe65b8SJeff Kirsher
1842a8fe65b8SJeff Kirsher netif_dbg(tp, rx_err, dev, "Ethernet frame had errors, status %08x\n",
1843a8fe65b8SJeff Kirsher rx_status);
1844a8fe65b8SJeff Kirsher dev->stats.rx_errors++;
1845a8fe65b8SJeff Kirsher if (!(rx_status & RxStatusOK)) {
1846a8fe65b8SJeff Kirsher if (rx_status & RxTooLong) {
1847a8fe65b8SJeff Kirsher netdev_dbg(dev, "Oversized Ethernet frame, status %04x!\n",
1848a8fe65b8SJeff Kirsher rx_status);
1849a8fe65b8SJeff Kirsher /* A.C.: The chip hangs here. */
1850a8fe65b8SJeff Kirsher }
1851a8fe65b8SJeff Kirsher if (rx_status & (RxBadSymbol | RxBadAlign))
1852a8fe65b8SJeff Kirsher dev->stats.rx_frame_errors++;
1853a8fe65b8SJeff Kirsher if (rx_status & (RxRunt | RxTooLong))
1854a8fe65b8SJeff Kirsher dev->stats.rx_length_errors++;
1855a8fe65b8SJeff Kirsher if (rx_status & RxCRCErr)
1856a8fe65b8SJeff Kirsher dev->stats.rx_crc_errors++;
1857a8fe65b8SJeff Kirsher } else {
1858a8fe65b8SJeff Kirsher tp->xstats.rx_lost_in_ring++;
1859a8fe65b8SJeff Kirsher }
1860a8fe65b8SJeff Kirsher
1861a8fe65b8SJeff Kirsher #ifndef CONFIG_8139_OLD_RX_RESET
1862a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (ChipCmd);
1863a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, tmp8 & ~CmdRxEnb);
1864a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, tmp8);
1865a8fe65b8SJeff Kirsher RTL_W32 (RxConfig, tp->rx_config);
1866a8fe65b8SJeff Kirsher tp->cur_rx = 0;
1867a8fe65b8SJeff Kirsher #else
1868a8fe65b8SJeff Kirsher /* Reset the receiver, based on RealTek recommendation. (Bug?) */
1869a8fe65b8SJeff Kirsher
1870a8fe65b8SJeff Kirsher /* disable receive */
1871a8fe65b8SJeff Kirsher RTL_W8_F (ChipCmd, CmdTxEnb);
1872a8fe65b8SJeff Kirsher tmp_work = 200;
1873a8fe65b8SJeff Kirsher while (--tmp_work > 0) {
1874a8fe65b8SJeff Kirsher udelay(1);
1875a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (ChipCmd);
1876a8fe65b8SJeff Kirsher if (!(tmp8 & CmdRxEnb))
1877a8fe65b8SJeff Kirsher break;
1878a8fe65b8SJeff Kirsher }
1879a8fe65b8SJeff Kirsher if (tmp_work <= 0)
1880a8fe65b8SJeff Kirsher netdev_warn(dev, "rx stop wait too long\n");
1881a8fe65b8SJeff Kirsher /* restart receive */
1882a8fe65b8SJeff Kirsher tmp_work = 200;
1883a8fe65b8SJeff Kirsher while (--tmp_work > 0) {
1884a8fe65b8SJeff Kirsher RTL_W8_F (ChipCmd, CmdRxEnb | CmdTxEnb);
1885a8fe65b8SJeff Kirsher udelay(1);
1886a8fe65b8SJeff Kirsher tmp8 = RTL_R8 (ChipCmd);
1887a8fe65b8SJeff Kirsher if ((tmp8 & CmdRxEnb) && (tmp8 & CmdTxEnb))
1888a8fe65b8SJeff Kirsher break;
1889a8fe65b8SJeff Kirsher }
1890a8fe65b8SJeff Kirsher if (tmp_work <= 0)
1891a8fe65b8SJeff Kirsher netdev_warn(dev, "tx/rx enable wait too long\n");
1892a8fe65b8SJeff Kirsher
1893a8fe65b8SJeff Kirsher /* and reinitialize all rx related registers */
1894a8fe65b8SJeff Kirsher RTL_W8_F (Cfg9346, Cfg9346_Unlock);
1895a8fe65b8SJeff Kirsher /* Must enable Tx/Rx before setting transfer thresholds! */
1896a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1897a8fe65b8SJeff Kirsher
1898a8fe65b8SJeff Kirsher tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
1899a8fe65b8SJeff Kirsher RTL_W32 (RxConfig, tp->rx_config);
1900a8fe65b8SJeff Kirsher tp->cur_rx = 0;
1901a8fe65b8SJeff Kirsher
1902a8fe65b8SJeff Kirsher netdev_dbg(dev, "init buffer addresses\n");
1903a8fe65b8SJeff Kirsher
1904a8fe65b8SJeff Kirsher /* Lock Config[01234] and BMCR register writes */
1905a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
1906a8fe65b8SJeff Kirsher
1907a8fe65b8SJeff Kirsher /* init Rx ring buffer DMA address */
1908a8fe65b8SJeff Kirsher RTL_W32_F (RxBuf, tp->rx_ring_dma);
1909a8fe65b8SJeff Kirsher
1910a8fe65b8SJeff Kirsher /* A.C.: Reset the multicast list. */
1911a8fe65b8SJeff Kirsher __set_rx_mode (dev);
1912a8fe65b8SJeff Kirsher #endif
1913a8fe65b8SJeff Kirsher }
1914a8fe65b8SJeff Kirsher
1915a8fe65b8SJeff Kirsher #if RX_BUF_IDX == 3
wrap_copy(struct sk_buff * skb,const unsigned char * ring,u32 offset,unsigned int size)1916a8fe65b8SJeff Kirsher static inline void wrap_copy(struct sk_buff *skb, const unsigned char *ring,
1917a8fe65b8SJeff Kirsher u32 offset, unsigned int size)
1918a8fe65b8SJeff Kirsher {
1919a8fe65b8SJeff Kirsher u32 left = RX_BUF_LEN - offset;
1920a8fe65b8SJeff Kirsher
1921a8fe65b8SJeff Kirsher if (size > left) {
1922a8fe65b8SJeff Kirsher skb_copy_to_linear_data(skb, ring + offset, left);
1923a8fe65b8SJeff Kirsher skb_copy_to_linear_data_offset(skb, left, ring, size - left);
1924a8fe65b8SJeff Kirsher } else
1925a8fe65b8SJeff Kirsher skb_copy_to_linear_data(skb, ring + offset, size);
1926a8fe65b8SJeff Kirsher }
1927a8fe65b8SJeff Kirsher #endif
1928a8fe65b8SJeff Kirsher
rtl8139_isr_ack(struct rtl8139_private * tp)1929a8fe65b8SJeff Kirsher static void rtl8139_isr_ack(struct rtl8139_private *tp)
1930a8fe65b8SJeff Kirsher {
1931a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1932a8fe65b8SJeff Kirsher u16 status;
1933a8fe65b8SJeff Kirsher
1934a8fe65b8SJeff Kirsher status = RTL_R16 (IntrStatus) & RxAckBits;
1935a8fe65b8SJeff Kirsher
1936a8fe65b8SJeff Kirsher /* Clear out errors and receive interrupts */
1937a8fe65b8SJeff Kirsher if (likely(status != 0)) {
1938a8fe65b8SJeff Kirsher if (unlikely(status & (RxFIFOOver | RxOverflow))) {
1939a8fe65b8SJeff Kirsher tp->dev->stats.rx_errors++;
1940a8fe65b8SJeff Kirsher if (status & RxFIFOOver)
1941a8fe65b8SJeff Kirsher tp->dev->stats.rx_fifo_errors++;
1942a8fe65b8SJeff Kirsher }
1943a8fe65b8SJeff Kirsher RTL_W16_F (IntrStatus, RxAckBits);
1944a8fe65b8SJeff Kirsher }
1945a8fe65b8SJeff Kirsher }
1946a8fe65b8SJeff Kirsher
rtl8139_rx(struct net_device * dev,struct rtl8139_private * tp,int budget)1947a8fe65b8SJeff Kirsher static int rtl8139_rx(struct net_device *dev, struct rtl8139_private *tp,
1948a8fe65b8SJeff Kirsher int budget)
1949a8fe65b8SJeff Kirsher {
1950a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
1951a8fe65b8SJeff Kirsher int received = 0;
1952a8fe65b8SJeff Kirsher unsigned char *rx_ring = tp->rx_ring;
1953a8fe65b8SJeff Kirsher unsigned int cur_rx = tp->cur_rx;
1954a8fe65b8SJeff Kirsher unsigned int rx_size = 0;
1955a8fe65b8SJeff Kirsher
1956a8fe65b8SJeff Kirsher netdev_dbg(dev, "In %s(), current %04x BufAddr %04x, free to %04x, Cmd %02x\n",
1957a8fe65b8SJeff Kirsher __func__, (u16)cur_rx,
1958a8fe65b8SJeff Kirsher RTL_R16(RxBufAddr), RTL_R16(RxBufPtr), RTL_R8(ChipCmd));
1959a8fe65b8SJeff Kirsher
1960a8fe65b8SJeff Kirsher while (netif_running(dev) && received < budget &&
1961a8fe65b8SJeff Kirsher (RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
1962a8fe65b8SJeff Kirsher u32 ring_offset = cur_rx % RX_BUF_LEN;
1963a8fe65b8SJeff Kirsher u32 rx_status;
1964a8fe65b8SJeff Kirsher unsigned int pkt_size;
1965a8fe65b8SJeff Kirsher struct sk_buff *skb;
1966a8fe65b8SJeff Kirsher
1967a8fe65b8SJeff Kirsher rmb();
1968a8fe65b8SJeff Kirsher
1969a8fe65b8SJeff Kirsher /* read size+status of next frame from DMA ring buffer */
1970a8fe65b8SJeff Kirsher rx_status = le32_to_cpu (*(__le32 *) (rx_ring + ring_offset));
1971a8fe65b8SJeff Kirsher rx_size = rx_status >> 16;
1972eeb69aa4SBen Greear if (likely(!(dev->features & NETIF_F_RXFCS)))
1973a8fe65b8SJeff Kirsher pkt_size = rx_size - 4;
1974eeb69aa4SBen Greear else
1975eeb69aa4SBen Greear pkt_size = rx_size;
1976a8fe65b8SJeff Kirsher
1977a8fe65b8SJeff Kirsher netif_dbg(tp, rx_status, dev, "%s() status %04x, size %04x, cur %04x\n",
1978a8fe65b8SJeff Kirsher __func__, rx_status, rx_size, cur_rx);
1979a8fe65b8SJeff Kirsher #if RTL8139_DEBUG > 2
1980a8fe65b8SJeff Kirsher print_hex_dump(KERN_DEBUG, "Frame contents: ",
1981a8fe65b8SJeff Kirsher DUMP_PREFIX_OFFSET, 16, 1,
1982a8fe65b8SJeff Kirsher &rx_ring[ring_offset], 70, true);
1983a8fe65b8SJeff Kirsher #endif
1984a8fe65b8SJeff Kirsher
1985a8fe65b8SJeff Kirsher /* Packet copy from FIFO still in progress.
1986a8fe65b8SJeff Kirsher * Theoretically, this should never happen
1987a8fe65b8SJeff Kirsher * since EarlyRx is disabled.
1988a8fe65b8SJeff Kirsher */
1989a8fe65b8SJeff Kirsher if (unlikely(rx_size == 0xfff0)) {
1990a8fe65b8SJeff Kirsher if (!tp->fifo_copy_timeout)
1991a8fe65b8SJeff Kirsher tp->fifo_copy_timeout = jiffies + 2;
1992a8fe65b8SJeff Kirsher else if (time_after(jiffies, tp->fifo_copy_timeout)) {
1993a8fe65b8SJeff Kirsher netdev_dbg(dev, "hung FIFO. Reset\n");
1994a8fe65b8SJeff Kirsher rx_size = 0;
1995a8fe65b8SJeff Kirsher goto no_early_rx;
1996a8fe65b8SJeff Kirsher }
1997a8fe65b8SJeff Kirsher netif_dbg(tp, intr, dev, "fifo copy in progress\n");
1998a8fe65b8SJeff Kirsher tp->xstats.early_rx++;
1999a8fe65b8SJeff Kirsher break;
2000a8fe65b8SJeff Kirsher }
2001a8fe65b8SJeff Kirsher
2002a8fe65b8SJeff Kirsher no_early_rx:
2003a8fe65b8SJeff Kirsher tp->fifo_copy_timeout = 0;
2004a8fe65b8SJeff Kirsher
2005a8fe65b8SJeff Kirsher /* If Rx err or invalid rx_size/rx_status received
2006a8fe65b8SJeff Kirsher * (which happens if we get lost in the ring),
2007a8fe65b8SJeff Kirsher * Rx process gets reset, so we abort any further
2008a8fe65b8SJeff Kirsher * Rx processing.
2009a8fe65b8SJeff Kirsher */
2010a8fe65b8SJeff Kirsher if (unlikely((rx_size > (MAX_ETH_FRAME_SIZE+4)) ||
2011a8fe65b8SJeff Kirsher (rx_size < 8) ||
2012a8fe65b8SJeff Kirsher (!(rx_status & RxStatusOK)))) {
2013d95089dcSBen Greear if ((dev->features & NETIF_F_RXALL) &&
2014d95089dcSBen Greear (rx_size <= (MAX_ETH_FRAME_SIZE + 4)) &&
2015d95089dcSBen Greear (rx_size >= 8) &&
2016d95089dcSBen Greear (!(rx_status & RxStatusOK))) {
2017d95089dcSBen Greear /* Length is at least mostly OK, but pkt has
2018d95089dcSBen Greear * error. I'm hoping we can handle some of these
2019d95089dcSBen Greear * errors without resetting the chip. --Ben
2020d95089dcSBen Greear */
2021d95089dcSBen Greear dev->stats.rx_errors++;
2022d95089dcSBen Greear if (rx_status & RxCRCErr) {
2023d95089dcSBen Greear dev->stats.rx_crc_errors++;
2024d95089dcSBen Greear goto keep_pkt;
2025d95089dcSBen Greear }
2026d95089dcSBen Greear if (rx_status & RxRunt) {
2027d95089dcSBen Greear dev->stats.rx_length_errors++;
2028d95089dcSBen Greear goto keep_pkt;
2029d95089dcSBen Greear }
2030d95089dcSBen Greear }
2031a8fe65b8SJeff Kirsher rtl8139_rx_err (rx_status, dev, tp, ioaddr);
2032a8fe65b8SJeff Kirsher received = -1;
2033a8fe65b8SJeff Kirsher goto out;
2034a8fe65b8SJeff Kirsher }
2035a8fe65b8SJeff Kirsher
2036d95089dcSBen Greear keep_pkt:
2037a8fe65b8SJeff Kirsher /* Malloc up new buffer, compatible with net-2e. */
2038a8fe65b8SJeff Kirsher /* Omit the four octet CRC from the length. */
2039a8fe65b8SJeff Kirsher
2040e2338f86SAlexander Duyck skb = napi_alloc_skb(&tp->napi, pkt_size);
2041a8fe65b8SJeff Kirsher if (likely(skb)) {
2042a8fe65b8SJeff Kirsher #if RX_BUF_IDX == 3
2043a8fe65b8SJeff Kirsher wrap_copy(skb, rx_ring, ring_offset+4, pkt_size);
2044a8fe65b8SJeff Kirsher #else
2045a8fe65b8SJeff Kirsher skb_copy_to_linear_data (skb, &rx_ring[ring_offset + 4], pkt_size);
2046a8fe65b8SJeff Kirsher #endif
2047a8fe65b8SJeff Kirsher skb_put (skb, pkt_size);
2048a8fe65b8SJeff Kirsher
2049a8fe65b8SJeff Kirsher skb->protocol = eth_type_trans (skb, dev);
2050a8fe65b8SJeff Kirsher
20519184a227SJunchang Wang u64_stats_update_begin(&tp->rx_stats.syncp);
20529184a227SJunchang Wang tp->rx_stats.packets++;
20539184a227SJunchang Wang tp->rx_stats.bytes += pkt_size;
20549184a227SJunchang Wang u64_stats_update_end(&tp->rx_stats.syncp);
2055a8fe65b8SJeff Kirsher
2056a8fe65b8SJeff Kirsher netif_receive_skb (skb);
2057a8fe65b8SJeff Kirsher } else {
2058a8fe65b8SJeff Kirsher dev->stats.rx_dropped++;
2059a8fe65b8SJeff Kirsher }
2060a8fe65b8SJeff Kirsher received++;
2061a8fe65b8SJeff Kirsher
2062a8fe65b8SJeff Kirsher cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
2063a8fe65b8SJeff Kirsher RTL_W16 (RxBufPtr, (u16) (cur_rx - 16));
2064a8fe65b8SJeff Kirsher
2065a8fe65b8SJeff Kirsher rtl8139_isr_ack(tp);
2066a8fe65b8SJeff Kirsher }
2067a8fe65b8SJeff Kirsher
2068a8fe65b8SJeff Kirsher if (unlikely(!received || rx_size == 0xfff0))
2069a8fe65b8SJeff Kirsher rtl8139_isr_ack(tp);
2070a8fe65b8SJeff Kirsher
2071a8fe65b8SJeff Kirsher netdev_dbg(dev, "Done %s(), current %04x BufAddr %04x, free to %04x, Cmd %02x\n",
2072a8fe65b8SJeff Kirsher __func__, cur_rx,
2073a8fe65b8SJeff Kirsher RTL_R16(RxBufAddr), RTL_R16(RxBufPtr), RTL_R8(ChipCmd));
2074a8fe65b8SJeff Kirsher
2075a8fe65b8SJeff Kirsher tp->cur_rx = cur_rx;
2076a8fe65b8SJeff Kirsher
2077a8fe65b8SJeff Kirsher /*
2078a8fe65b8SJeff Kirsher * The receive buffer should be mostly empty.
2079a8fe65b8SJeff Kirsher * Tell NAPI to reenable the Rx irq.
2080a8fe65b8SJeff Kirsher */
2081a8fe65b8SJeff Kirsher if (tp->fifo_copy_timeout)
2082a8fe65b8SJeff Kirsher received = budget;
2083a8fe65b8SJeff Kirsher
2084a8fe65b8SJeff Kirsher out:
2085a8fe65b8SJeff Kirsher return received;
2086a8fe65b8SJeff Kirsher }
2087a8fe65b8SJeff Kirsher
2088a8fe65b8SJeff Kirsher
rtl8139_weird_interrupt(struct net_device * dev,struct rtl8139_private * tp,void __iomem * ioaddr,int status,int link_changed)2089a8fe65b8SJeff Kirsher static void rtl8139_weird_interrupt (struct net_device *dev,
2090a8fe65b8SJeff Kirsher struct rtl8139_private *tp,
2091a8fe65b8SJeff Kirsher void __iomem *ioaddr,
2092a8fe65b8SJeff Kirsher int status, int link_changed)
2093a8fe65b8SJeff Kirsher {
2094a8fe65b8SJeff Kirsher netdev_dbg(dev, "Abnormal interrupt, status %08x\n", status);
2095a8fe65b8SJeff Kirsher
2096a8fe65b8SJeff Kirsher assert (dev != NULL);
2097a8fe65b8SJeff Kirsher assert (tp != NULL);
2098a8fe65b8SJeff Kirsher assert (ioaddr != NULL);
2099a8fe65b8SJeff Kirsher
2100a8fe65b8SJeff Kirsher /* Update the error count. */
2101a8fe65b8SJeff Kirsher dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
2102a8fe65b8SJeff Kirsher RTL_W32 (RxMissed, 0);
2103a8fe65b8SJeff Kirsher
2104a8fe65b8SJeff Kirsher if ((status & RxUnderrun) && link_changed &&
2105a8fe65b8SJeff Kirsher (tp->drv_flags & HAS_LNK_CHNG)) {
2106a8fe65b8SJeff Kirsher rtl_check_media(dev, 0);
2107a8fe65b8SJeff Kirsher status &= ~RxUnderrun;
2108a8fe65b8SJeff Kirsher }
2109a8fe65b8SJeff Kirsher
2110a8fe65b8SJeff Kirsher if (status & (RxUnderrun | RxErr))
2111a8fe65b8SJeff Kirsher dev->stats.rx_errors++;
2112a8fe65b8SJeff Kirsher
2113a8fe65b8SJeff Kirsher if (status & PCSTimeout)
2114a8fe65b8SJeff Kirsher dev->stats.rx_length_errors++;
2115a8fe65b8SJeff Kirsher if (status & RxUnderrun)
2116a8fe65b8SJeff Kirsher dev->stats.rx_fifo_errors++;
2117a8fe65b8SJeff Kirsher if (status & PCIErr) {
2118a8fe65b8SJeff Kirsher u16 pci_cmd_status;
2119a8fe65b8SJeff Kirsher pci_read_config_word (tp->pci_dev, PCI_STATUS, &pci_cmd_status);
2120a8fe65b8SJeff Kirsher pci_write_config_word (tp->pci_dev, PCI_STATUS, pci_cmd_status);
2121a8fe65b8SJeff Kirsher
2122a8fe65b8SJeff Kirsher netdev_err(dev, "PCI Bus error %04x\n", pci_cmd_status);
2123a8fe65b8SJeff Kirsher }
2124a8fe65b8SJeff Kirsher }
2125a8fe65b8SJeff Kirsher
rtl8139_poll(struct napi_struct * napi,int budget)2126a8fe65b8SJeff Kirsher static int rtl8139_poll(struct napi_struct *napi, int budget)
2127a8fe65b8SJeff Kirsher {
2128a8fe65b8SJeff Kirsher struct rtl8139_private *tp = container_of(napi, struct rtl8139_private, napi);
2129a8fe65b8SJeff Kirsher struct net_device *dev = tp->dev;
2130a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2131a8fe65b8SJeff Kirsher int work_done;
2132a8fe65b8SJeff Kirsher
2133a8fe65b8SJeff Kirsher spin_lock(&tp->rx_lock);
2134a8fe65b8SJeff Kirsher work_done = 0;
2135a8fe65b8SJeff Kirsher if (likely(RTL_R16(IntrStatus) & RxAckBits))
2136a8fe65b8SJeff Kirsher work_done += rtl8139_rx(dev, tp, budget);
2137a8fe65b8SJeff Kirsher
2138129c6cdaSEric Dumazet if (work_done < budget) {
2139a8fe65b8SJeff Kirsher unsigned long flags;
2140617f0121SEric Dumazet
2141a8fe65b8SJeff Kirsher spin_lock_irqsave(&tp->lock, flags);
2142129c6cdaSEric Dumazet if (napi_complete_done(napi, work_done))
2143a8fe65b8SJeff Kirsher RTL_W16_F(IntrMask, rtl8139_intr_mask);
2144a8fe65b8SJeff Kirsher spin_unlock_irqrestore(&tp->lock, flags);
2145a8fe65b8SJeff Kirsher }
2146a8fe65b8SJeff Kirsher spin_unlock(&tp->rx_lock);
2147a8fe65b8SJeff Kirsher
2148a8fe65b8SJeff Kirsher return work_done;
2149a8fe65b8SJeff Kirsher }
2150a8fe65b8SJeff Kirsher
2151a8fe65b8SJeff Kirsher /* The interrupt handler does all of the Rx thread work and cleans up
2152a8fe65b8SJeff Kirsher after the Tx thread. */
rtl8139_interrupt(int irq,void * dev_instance)2153a8fe65b8SJeff Kirsher static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance)
2154a8fe65b8SJeff Kirsher {
2155a8fe65b8SJeff Kirsher struct net_device *dev = (struct net_device *) dev_instance;
2156a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2157a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2158a8fe65b8SJeff Kirsher u16 status, ackstat;
2159a8fe65b8SJeff Kirsher int link_changed = 0; /* avoid bogus "uninit" warning */
2160a8fe65b8SJeff Kirsher int handled = 0;
2161a8fe65b8SJeff Kirsher
2162a8fe65b8SJeff Kirsher spin_lock (&tp->lock);
2163a8fe65b8SJeff Kirsher status = RTL_R16 (IntrStatus);
2164a8fe65b8SJeff Kirsher
2165a8fe65b8SJeff Kirsher /* shared irq? */
2166a8fe65b8SJeff Kirsher if (unlikely((status & rtl8139_intr_mask) == 0))
2167a8fe65b8SJeff Kirsher goto out;
2168a8fe65b8SJeff Kirsher
2169a8fe65b8SJeff Kirsher handled = 1;
2170a8fe65b8SJeff Kirsher
2171a8fe65b8SJeff Kirsher /* h/w no longer present (hotplug?) or major error, bail */
2172a8fe65b8SJeff Kirsher if (unlikely(status == 0xFFFF))
2173a8fe65b8SJeff Kirsher goto out;
2174a8fe65b8SJeff Kirsher
2175a8fe65b8SJeff Kirsher /* close possible race's with dev_close */
2176a8fe65b8SJeff Kirsher if (unlikely(!netif_running(dev))) {
2177a8fe65b8SJeff Kirsher RTL_W16 (IntrMask, 0);
2178a8fe65b8SJeff Kirsher goto out;
2179a8fe65b8SJeff Kirsher }
2180a8fe65b8SJeff Kirsher
2181a8fe65b8SJeff Kirsher /* Acknowledge all of the current interrupt sources ASAP, but
2182a8fe65b8SJeff Kirsher an first get an additional status bit from CSCR. */
2183a8fe65b8SJeff Kirsher if (unlikely(status & RxUnderrun))
2184a8fe65b8SJeff Kirsher link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
2185a8fe65b8SJeff Kirsher
2186a8fe65b8SJeff Kirsher ackstat = status & ~(RxAckBits | TxErr);
2187a8fe65b8SJeff Kirsher if (ackstat)
2188a8fe65b8SJeff Kirsher RTL_W16 (IntrStatus, ackstat);
2189a8fe65b8SJeff Kirsher
2190a8fe65b8SJeff Kirsher /* Receive packets are processed by poll routine.
2191a8fe65b8SJeff Kirsher If not running start it now. */
2192a8fe65b8SJeff Kirsher if (status & RxAckBits){
2193a8fe65b8SJeff Kirsher if (napi_schedule_prep(&tp->napi)) {
2194a8fe65b8SJeff Kirsher RTL_W16_F (IntrMask, rtl8139_norx_intr_mask);
2195a8fe65b8SJeff Kirsher __napi_schedule(&tp->napi);
2196a8fe65b8SJeff Kirsher }
2197a8fe65b8SJeff Kirsher }
2198a8fe65b8SJeff Kirsher
2199a8fe65b8SJeff Kirsher /* Check uncommon events with one test. */
2200a8fe65b8SJeff Kirsher if (unlikely(status & (PCIErr | PCSTimeout | RxUnderrun | RxErr)))
2201a8fe65b8SJeff Kirsher rtl8139_weird_interrupt (dev, tp, ioaddr,
2202a8fe65b8SJeff Kirsher status, link_changed);
2203a8fe65b8SJeff Kirsher
2204a8fe65b8SJeff Kirsher if (status & (TxOK | TxErr)) {
2205a8fe65b8SJeff Kirsher rtl8139_tx_interrupt (dev, tp, ioaddr);
2206a8fe65b8SJeff Kirsher if (status & TxErr)
2207a8fe65b8SJeff Kirsher RTL_W16 (IntrStatus, TxErr);
2208a8fe65b8SJeff Kirsher }
2209a8fe65b8SJeff Kirsher out:
2210a8fe65b8SJeff Kirsher spin_unlock (&tp->lock);
2211a8fe65b8SJeff Kirsher
2212a8fe65b8SJeff Kirsher netdev_dbg(dev, "exiting interrupt, intr_status=%#4.4x\n",
2213a8fe65b8SJeff Kirsher RTL_R16(IntrStatus));
2214a8fe65b8SJeff Kirsher return IRQ_RETVAL(handled);
2215a8fe65b8SJeff Kirsher }
2216a8fe65b8SJeff Kirsher
2217a8fe65b8SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2218a8fe65b8SJeff Kirsher /*
2219a8fe65b8SJeff Kirsher * Polling receive - used by netconsole and other diagnostic tools
2220a8fe65b8SJeff Kirsher * to allow network i/o with interrupts disabled.
2221a8fe65b8SJeff Kirsher */
rtl8139_poll_controller(struct net_device * dev)2222a8fe65b8SJeff Kirsher static void rtl8139_poll_controller(struct net_device *dev)
2223a8fe65b8SJeff Kirsher {
222465712ec0SFrancois Romieu struct rtl8139_private *tp = netdev_priv(dev);
222565712ec0SFrancois Romieu const int irq = tp->pci_dev->irq;
222665712ec0SFrancois Romieu
2227af3e0fcfSIngo Molnar disable_irq_nosync(irq);
222865712ec0SFrancois Romieu rtl8139_interrupt(irq, dev);
222965712ec0SFrancois Romieu enable_irq(irq);
2230a8fe65b8SJeff Kirsher }
2231a8fe65b8SJeff Kirsher #endif
2232a8fe65b8SJeff Kirsher
rtl8139_set_mac_address(struct net_device * dev,void * p)2233a8fe65b8SJeff Kirsher static int rtl8139_set_mac_address(struct net_device *dev, void *p)
2234a8fe65b8SJeff Kirsher {
2235a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2236a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2237a8fe65b8SJeff Kirsher struct sockaddr *addr = p;
2238a8fe65b8SJeff Kirsher
2239a8fe65b8SJeff Kirsher if (!is_valid_ether_addr(addr->sa_data))
2240a8fe65b8SJeff Kirsher return -EADDRNOTAVAIL;
2241a8fe65b8SJeff Kirsher
2242a05e4c0aSJakub Kicinski eth_hw_addr_set(dev, addr->sa_data);
2243a8fe65b8SJeff Kirsher
2244a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2245a8fe65b8SJeff Kirsher
2246a8fe65b8SJeff Kirsher RTL_W8_F(Cfg9346, Cfg9346_Unlock);
2247a8fe65b8SJeff Kirsher RTL_W32_F(MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
2248a8fe65b8SJeff Kirsher RTL_W32_F(MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
2249a8fe65b8SJeff Kirsher RTL_W8_F(Cfg9346, Cfg9346_Lock);
2250a8fe65b8SJeff Kirsher
2251a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2252a8fe65b8SJeff Kirsher
2253a8fe65b8SJeff Kirsher return 0;
2254a8fe65b8SJeff Kirsher }
2255a8fe65b8SJeff Kirsher
rtl8139_close(struct net_device * dev)2256a8fe65b8SJeff Kirsher static int rtl8139_close (struct net_device *dev)
2257a8fe65b8SJeff Kirsher {
2258a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2259a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2260a8fe65b8SJeff Kirsher unsigned long flags;
2261a8fe65b8SJeff Kirsher
2262a8fe65b8SJeff Kirsher netif_stop_queue(dev);
2263a8fe65b8SJeff Kirsher napi_disable(&tp->napi);
2264a8fe65b8SJeff Kirsher
2265a8fe65b8SJeff Kirsher netif_dbg(tp, ifdown, dev, "Shutting down ethercard, status was 0x%04x\n",
2266a8fe65b8SJeff Kirsher RTL_R16(IntrStatus));
2267a8fe65b8SJeff Kirsher
2268a8fe65b8SJeff Kirsher spin_lock_irqsave (&tp->lock, flags);
2269a8fe65b8SJeff Kirsher
2270a8fe65b8SJeff Kirsher /* Stop the chip's Tx and Rx DMA processes. */
2271a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, 0);
2272a8fe65b8SJeff Kirsher
2273a8fe65b8SJeff Kirsher /* Disable interrupts by clearing the interrupt mask. */
2274a8fe65b8SJeff Kirsher RTL_W16 (IntrMask, 0);
2275a8fe65b8SJeff Kirsher
2276a8fe65b8SJeff Kirsher /* Update the error counts. */
2277a8fe65b8SJeff Kirsher dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
2278a8fe65b8SJeff Kirsher RTL_W32 (RxMissed, 0);
2279a8fe65b8SJeff Kirsher
2280a8fe65b8SJeff Kirsher spin_unlock_irqrestore (&tp->lock, flags);
2281a8fe65b8SJeff Kirsher
228265712ec0SFrancois Romieu free_irq(tp->pci_dev->irq, dev);
2283a8fe65b8SJeff Kirsher
2284a8fe65b8SJeff Kirsher rtl8139_tx_clear (tp);
2285a8fe65b8SJeff Kirsher
2286a8fe65b8SJeff Kirsher dma_free_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
2287a8fe65b8SJeff Kirsher tp->rx_ring, tp->rx_ring_dma);
2288a8fe65b8SJeff Kirsher dma_free_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
2289a8fe65b8SJeff Kirsher tp->tx_bufs, tp->tx_bufs_dma);
2290a8fe65b8SJeff Kirsher tp->rx_ring = NULL;
2291a8fe65b8SJeff Kirsher tp->tx_bufs = NULL;
2292a8fe65b8SJeff Kirsher
2293a8fe65b8SJeff Kirsher /* Green! Put the chip in low-power mode. */
2294a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
2295a8fe65b8SJeff Kirsher
2296a8fe65b8SJeff Kirsher if (rtl_chip_info[tp->chipset].flags & HasHltClk)
2297a8fe65b8SJeff Kirsher RTL_W8 (HltClk, 'H'); /* 'R' would leave the clock running. */
2298a8fe65b8SJeff Kirsher
2299a8fe65b8SJeff Kirsher return 0;
2300a8fe65b8SJeff Kirsher }
2301a8fe65b8SJeff Kirsher
2302a8fe65b8SJeff Kirsher
2303a8fe65b8SJeff Kirsher /* Get the ethtool Wake-on-LAN settings. Assumes that wol points to
2304a8fe65b8SJeff Kirsher kernel memory, *wol has been initialized as {ETHTOOL_GWOL}, and
2305a8fe65b8SJeff Kirsher other threads or interrupts aren't messing with the 8139. */
rtl8139_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)2306a8fe65b8SJeff Kirsher static void rtl8139_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2307a8fe65b8SJeff Kirsher {
2308a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2309a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2310a8fe65b8SJeff Kirsher
2311a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2312a8fe65b8SJeff Kirsher if (rtl_chip_info[tp->chipset].flags & HasLWake) {
2313a8fe65b8SJeff Kirsher u8 cfg3 = RTL_R8 (Config3);
2314a8fe65b8SJeff Kirsher u8 cfg5 = RTL_R8 (Config5);
2315a8fe65b8SJeff Kirsher
2316a8fe65b8SJeff Kirsher wol->supported = WAKE_PHY | WAKE_MAGIC
2317a8fe65b8SJeff Kirsher | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
2318a8fe65b8SJeff Kirsher
2319a8fe65b8SJeff Kirsher wol->wolopts = 0;
2320a8fe65b8SJeff Kirsher if (cfg3 & Cfg3_LinkUp)
2321a8fe65b8SJeff Kirsher wol->wolopts |= WAKE_PHY;
2322a8fe65b8SJeff Kirsher if (cfg3 & Cfg3_Magic)
2323a8fe65b8SJeff Kirsher wol->wolopts |= WAKE_MAGIC;
2324a8fe65b8SJeff Kirsher /* (KON)FIXME: See how netdev_set_wol() handles the
2325a8fe65b8SJeff Kirsher following constants. */
2326a8fe65b8SJeff Kirsher if (cfg5 & Cfg5_UWF)
2327a8fe65b8SJeff Kirsher wol->wolopts |= WAKE_UCAST;
2328a8fe65b8SJeff Kirsher if (cfg5 & Cfg5_MWF)
2329a8fe65b8SJeff Kirsher wol->wolopts |= WAKE_MCAST;
2330a8fe65b8SJeff Kirsher if (cfg5 & Cfg5_BWF)
2331a8fe65b8SJeff Kirsher wol->wolopts |= WAKE_BCAST;
2332a8fe65b8SJeff Kirsher }
2333a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2334a8fe65b8SJeff Kirsher }
2335a8fe65b8SJeff Kirsher
2336a8fe65b8SJeff Kirsher
2337a8fe65b8SJeff Kirsher /* Set the ethtool Wake-on-LAN settings. Return 0 or -errno. Assumes
2338a8fe65b8SJeff Kirsher that wol points to kernel memory and other threads or interrupts
2339a8fe65b8SJeff Kirsher aren't messing with the 8139. */
rtl8139_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)2340a8fe65b8SJeff Kirsher static int rtl8139_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2341a8fe65b8SJeff Kirsher {
2342a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2343a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2344a8fe65b8SJeff Kirsher u32 support;
2345a8fe65b8SJeff Kirsher u8 cfg3, cfg5;
2346a8fe65b8SJeff Kirsher
2347a8fe65b8SJeff Kirsher support = ((rtl_chip_info[tp->chipset].flags & HasLWake)
2348a8fe65b8SJeff Kirsher ? (WAKE_PHY | WAKE_MAGIC
2349a8fe65b8SJeff Kirsher | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST)
2350a8fe65b8SJeff Kirsher : 0);
2351a8fe65b8SJeff Kirsher if (wol->wolopts & ~support)
2352a8fe65b8SJeff Kirsher return -EINVAL;
2353a8fe65b8SJeff Kirsher
2354a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2355a8fe65b8SJeff Kirsher cfg3 = RTL_R8 (Config3) & ~(Cfg3_LinkUp | Cfg3_Magic);
2356a8fe65b8SJeff Kirsher if (wol->wolopts & WAKE_PHY)
2357a8fe65b8SJeff Kirsher cfg3 |= Cfg3_LinkUp;
2358a8fe65b8SJeff Kirsher if (wol->wolopts & WAKE_MAGIC)
2359a8fe65b8SJeff Kirsher cfg3 |= Cfg3_Magic;
2360a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Unlock);
2361a8fe65b8SJeff Kirsher RTL_W8 (Config3, cfg3);
2362a8fe65b8SJeff Kirsher RTL_W8 (Cfg9346, Cfg9346_Lock);
2363a8fe65b8SJeff Kirsher
2364a8fe65b8SJeff Kirsher cfg5 = RTL_R8 (Config5) & ~(Cfg5_UWF | Cfg5_MWF | Cfg5_BWF);
2365a8fe65b8SJeff Kirsher /* (KON)FIXME: These are untested. We may have to set the
2366a8fe65b8SJeff Kirsher CRC0, Wakeup0 and LSBCRC0 registers too, but I have no
2367a8fe65b8SJeff Kirsher documentation. */
2368a8fe65b8SJeff Kirsher if (wol->wolopts & WAKE_UCAST)
2369a8fe65b8SJeff Kirsher cfg5 |= Cfg5_UWF;
2370a8fe65b8SJeff Kirsher if (wol->wolopts & WAKE_MCAST)
2371a8fe65b8SJeff Kirsher cfg5 |= Cfg5_MWF;
2372a8fe65b8SJeff Kirsher if (wol->wolopts & WAKE_BCAST)
2373a8fe65b8SJeff Kirsher cfg5 |= Cfg5_BWF;
2374a8fe65b8SJeff Kirsher RTL_W8 (Config5, cfg5); /* need not unlock via Cfg9346 */
2375a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2376a8fe65b8SJeff Kirsher
2377a8fe65b8SJeff Kirsher return 0;
2378a8fe65b8SJeff Kirsher }
2379a8fe65b8SJeff Kirsher
rtl8139_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)2380a8fe65b8SJeff Kirsher static void rtl8139_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2381a8fe65b8SJeff Kirsher {
2382a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2383f029c781SWolfram Sang strscpy(info->driver, DRV_NAME, sizeof(info->driver));
2384f029c781SWolfram Sang strscpy(info->version, DRV_VERSION, sizeof(info->version));
2385f029c781SWolfram Sang strscpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
2386a8fe65b8SJeff Kirsher }
2387a8fe65b8SJeff Kirsher
rtl8139_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)2388182ed075SPhilippe Reynes static int rtl8139_get_link_ksettings(struct net_device *dev,
2389182ed075SPhilippe Reynes struct ethtool_link_ksettings *cmd)
2390a8fe65b8SJeff Kirsher {
2391a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2392a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2393182ed075SPhilippe Reynes mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2394a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2395a8fe65b8SJeff Kirsher return 0;
2396a8fe65b8SJeff Kirsher }
2397a8fe65b8SJeff Kirsher
rtl8139_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)2398182ed075SPhilippe Reynes static int rtl8139_set_link_ksettings(struct net_device *dev,
2399182ed075SPhilippe Reynes const struct ethtool_link_ksettings *cmd)
2400a8fe65b8SJeff Kirsher {
2401a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2402a8fe65b8SJeff Kirsher int rc;
2403a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2404182ed075SPhilippe Reynes rc = mii_ethtool_set_link_ksettings(&tp->mii, cmd);
2405a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2406a8fe65b8SJeff Kirsher return rc;
2407a8fe65b8SJeff Kirsher }
2408a8fe65b8SJeff Kirsher
rtl8139_nway_reset(struct net_device * dev)2409a8fe65b8SJeff Kirsher static int rtl8139_nway_reset(struct net_device *dev)
2410a8fe65b8SJeff Kirsher {
2411a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2412a8fe65b8SJeff Kirsher return mii_nway_restart(&tp->mii);
2413a8fe65b8SJeff Kirsher }
2414a8fe65b8SJeff Kirsher
rtl8139_get_link(struct net_device * dev)2415a8fe65b8SJeff Kirsher static u32 rtl8139_get_link(struct net_device *dev)
2416a8fe65b8SJeff Kirsher {
2417a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2418a8fe65b8SJeff Kirsher return mii_link_ok(&tp->mii);
2419a8fe65b8SJeff Kirsher }
2420a8fe65b8SJeff Kirsher
rtl8139_get_msglevel(struct net_device * dev)2421a8fe65b8SJeff Kirsher static u32 rtl8139_get_msglevel(struct net_device *dev)
2422a8fe65b8SJeff Kirsher {
2423a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2424a8fe65b8SJeff Kirsher return tp->msg_enable;
2425a8fe65b8SJeff Kirsher }
2426a8fe65b8SJeff Kirsher
rtl8139_set_msglevel(struct net_device * dev,u32 datum)2427a8fe65b8SJeff Kirsher static void rtl8139_set_msglevel(struct net_device *dev, u32 datum)
2428a8fe65b8SJeff Kirsher {
2429a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2430a8fe65b8SJeff Kirsher tp->msg_enable = datum;
2431a8fe65b8SJeff Kirsher }
2432a8fe65b8SJeff Kirsher
rtl8139_get_regs_len(struct net_device * dev)2433a8fe65b8SJeff Kirsher static int rtl8139_get_regs_len(struct net_device *dev)
2434a8fe65b8SJeff Kirsher {
2435a8fe65b8SJeff Kirsher struct rtl8139_private *tp;
2436a8fe65b8SJeff Kirsher /* TODO: we are too slack to do reg dumping for pio, for now */
2437a8fe65b8SJeff Kirsher if (use_io)
2438a8fe65b8SJeff Kirsher return 0;
2439a8fe65b8SJeff Kirsher tp = netdev_priv(dev);
2440a8fe65b8SJeff Kirsher return tp->regs_len;
2441a8fe65b8SJeff Kirsher }
2442a8fe65b8SJeff Kirsher
rtl8139_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * regbuf)2443a8fe65b8SJeff Kirsher static void rtl8139_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
2444a8fe65b8SJeff Kirsher {
2445a8fe65b8SJeff Kirsher struct rtl8139_private *tp;
2446a8fe65b8SJeff Kirsher
2447a8fe65b8SJeff Kirsher /* TODO: we are too slack to do reg dumping for pio, for now */
2448a8fe65b8SJeff Kirsher if (use_io)
2449a8fe65b8SJeff Kirsher return;
2450a8fe65b8SJeff Kirsher tp = netdev_priv(dev);
2451a8fe65b8SJeff Kirsher
2452a8fe65b8SJeff Kirsher regs->version = RTL_REGS_VER;
2453a8fe65b8SJeff Kirsher
2454a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2455a8fe65b8SJeff Kirsher memcpy_fromio(regbuf, tp->mmio_addr, regs->len);
2456a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2457a8fe65b8SJeff Kirsher }
2458a8fe65b8SJeff Kirsher
rtl8139_get_sset_count(struct net_device * dev,int sset)2459a8fe65b8SJeff Kirsher static int rtl8139_get_sset_count(struct net_device *dev, int sset)
2460a8fe65b8SJeff Kirsher {
2461a8fe65b8SJeff Kirsher switch (sset) {
2462a8fe65b8SJeff Kirsher case ETH_SS_STATS:
2463a8fe65b8SJeff Kirsher return RTL_NUM_STATS;
2464a8fe65b8SJeff Kirsher default:
2465a8fe65b8SJeff Kirsher return -EOPNOTSUPP;
2466a8fe65b8SJeff Kirsher }
2467a8fe65b8SJeff Kirsher }
2468a8fe65b8SJeff Kirsher
rtl8139_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2469a8fe65b8SJeff Kirsher static void rtl8139_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2470a8fe65b8SJeff Kirsher {
2471a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2472a8fe65b8SJeff Kirsher
2473a8fe65b8SJeff Kirsher data[0] = tp->xstats.early_rx;
2474a8fe65b8SJeff Kirsher data[1] = tp->xstats.tx_buf_mapped;
2475a8fe65b8SJeff Kirsher data[2] = tp->xstats.tx_timeouts;
2476a8fe65b8SJeff Kirsher data[3] = tp->xstats.rx_lost_in_ring;
2477a8fe65b8SJeff Kirsher }
2478a8fe65b8SJeff Kirsher
rtl8139_get_strings(struct net_device * dev,u32 stringset,u8 * data)2479a8fe65b8SJeff Kirsher static void rtl8139_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2480a8fe65b8SJeff Kirsher {
2481a8fe65b8SJeff Kirsher memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2482a8fe65b8SJeff Kirsher }
2483a8fe65b8SJeff Kirsher
2484a8fe65b8SJeff Kirsher static const struct ethtool_ops rtl8139_ethtool_ops = {
2485a8fe65b8SJeff Kirsher .get_drvinfo = rtl8139_get_drvinfo,
2486a8fe65b8SJeff Kirsher .get_regs_len = rtl8139_get_regs_len,
2487a8fe65b8SJeff Kirsher .get_regs = rtl8139_get_regs,
2488a8fe65b8SJeff Kirsher .nway_reset = rtl8139_nway_reset,
2489a8fe65b8SJeff Kirsher .get_link = rtl8139_get_link,
2490a8fe65b8SJeff Kirsher .get_msglevel = rtl8139_get_msglevel,
2491a8fe65b8SJeff Kirsher .set_msglevel = rtl8139_set_msglevel,
2492a8fe65b8SJeff Kirsher .get_wol = rtl8139_get_wol,
2493a8fe65b8SJeff Kirsher .set_wol = rtl8139_set_wol,
2494a8fe65b8SJeff Kirsher .get_strings = rtl8139_get_strings,
2495a8fe65b8SJeff Kirsher .get_sset_count = rtl8139_get_sset_count,
2496a8fe65b8SJeff Kirsher .get_ethtool_stats = rtl8139_get_ethtool_stats,
2497182ed075SPhilippe Reynes .get_link_ksettings = rtl8139_get_link_ksettings,
2498182ed075SPhilippe Reynes .set_link_ksettings = rtl8139_set_link_ksettings,
2499a8fe65b8SJeff Kirsher };
2500a8fe65b8SJeff Kirsher
netdev_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)2501a8fe65b8SJeff Kirsher static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2502a8fe65b8SJeff Kirsher {
2503a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2504a8fe65b8SJeff Kirsher int rc;
2505a8fe65b8SJeff Kirsher
2506a8fe65b8SJeff Kirsher if (!netif_running(dev))
2507a8fe65b8SJeff Kirsher return -EINVAL;
2508a8fe65b8SJeff Kirsher
2509a8fe65b8SJeff Kirsher spin_lock_irq(&tp->lock);
2510a8fe65b8SJeff Kirsher rc = generic_mii_ioctl(&tp->mii, if_mii(rq), cmd, NULL);
2511a8fe65b8SJeff Kirsher spin_unlock_irq(&tp->lock);
2512a8fe65b8SJeff Kirsher
2513a8fe65b8SJeff Kirsher return rc;
2514a8fe65b8SJeff Kirsher }
2515a8fe65b8SJeff Kirsher
2516a8fe65b8SJeff Kirsher
2517bc1f4470Sstephen hemminger static void
rtl8139_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)25189184a227SJunchang Wang rtl8139_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
2519a8fe65b8SJeff Kirsher {
2520a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2521a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2522a8fe65b8SJeff Kirsher unsigned long flags;
25239184a227SJunchang Wang unsigned int start;
2524a8fe65b8SJeff Kirsher
2525a8fe65b8SJeff Kirsher if (netif_running(dev)) {
2526a8fe65b8SJeff Kirsher spin_lock_irqsave (&tp->lock, flags);
2527a8fe65b8SJeff Kirsher dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
2528a8fe65b8SJeff Kirsher RTL_W32 (RxMissed, 0);
2529a8fe65b8SJeff Kirsher spin_unlock_irqrestore (&tp->lock, flags);
2530a8fe65b8SJeff Kirsher }
2531a8fe65b8SJeff Kirsher
25329184a227SJunchang Wang netdev_stats_to_stats64(stats, &dev->stats);
25339184a227SJunchang Wang
25349184a227SJunchang Wang do {
2535*068c38adSThomas Gleixner start = u64_stats_fetch_begin(&tp->rx_stats.syncp);
25369184a227SJunchang Wang stats->rx_packets = tp->rx_stats.packets;
25379184a227SJunchang Wang stats->rx_bytes = tp->rx_stats.bytes;
2538*068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&tp->rx_stats.syncp, start));
25399184a227SJunchang Wang
25409184a227SJunchang Wang do {
2541*068c38adSThomas Gleixner start = u64_stats_fetch_begin(&tp->tx_stats.syncp);
25429184a227SJunchang Wang stats->tx_packets = tp->tx_stats.packets;
25439184a227SJunchang Wang stats->tx_bytes = tp->tx_stats.bytes;
2544*068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&tp->tx_stats.syncp, start));
2545a8fe65b8SJeff Kirsher }
2546a8fe65b8SJeff Kirsher
2547a8fe65b8SJeff Kirsher /* Set or clear the multicast filter for this adaptor.
2548a8fe65b8SJeff Kirsher This routine is not state sensitive and need not be SMP locked. */
2549a8fe65b8SJeff Kirsher
__set_rx_mode(struct net_device * dev)2550a8fe65b8SJeff Kirsher static void __set_rx_mode (struct net_device *dev)
2551a8fe65b8SJeff Kirsher {
2552a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2553a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2554a8fe65b8SJeff Kirsher u32 mc_filter[2]; /* Multicast hash filter */
2555a8fe65b8SJeff Kirsher int rx_mode;
2556a8fe65b8SJeff Kirsher u32 tmp;
2557a8fe65b8SJeff Kirsher
2558a8fe65b8SJeff Kirsher netdev_dbg(dev, "rtl8139_set_rx_mode(%04x) done -- Rx config %08x\n",
2559a8fe65b8SJeff Kirsher dev->flags, RTL_R32(RxConfig));
2560a8fe65b8SJeff Kirsher
2561a8fe65b8SJeff Kirsher /* Note: do not reorder, GCC is clever about common statements. */
2562a8fe65b8SJeff Kirsher if (dev->flags & IFF_PROMISC) {
2563a8fe65b8SJeff Kirsher rx_mode =
2564a8fe65b8SJeff Kirsher AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
2565a8fe65b8SJeff Kirsher AcceptAllPhys;
2566a8fe65b8SJeff Kirsher mc_filter[1] = mc_filter[0] = 0xffffffff;
2567a8fe65b8SJeff Kirsher } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
2568a8fe65b8SJeff Kirsher (dev->flags & IFF_ALLMULTI)) {
2569a8fe65b8SJeff Kirsher /* Too many to filter perfectly -- accept all multicasts. */
2570a8fe65b8SJeff Kirsher rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
2571a8fe65b8SJeff Kirsher mc_filter[1] = mc_filter[0] = 0xffffffff;
2572a8fe65b8SJeff Kirsher } else {
2573a8fe65b8SJeff Kirsher struct netdev_hw_addr *ha;
2574a8fe65b8SJeff Kirsher rx_mode = AcceptBroadcast | AcceptMyPhys;
2575a8fe65b8SJeff Kirsher mc_filter[1] = mc_filter[0] = 0;
2576a8fe65b8SJeff Kirsher netdev_for_each_mc_addr(ha, dev) {
2577a8fe65b8SJeff Kirsher int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2578a8fe65b8SJeff Kirsher
2579a8fe65b8SJeff Kirsher mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2580a8fe65b8SJeff Kirsher rx_mode |= AcceptMulticast;
2581a8fe65b8SJeff Kirsher }
2582a8fe65b8SJeff Kirsher }
2583a8fe65b8SJeff Kirsher
2584d95089dcSBen Greear if (dev->features & NETIF_F_RXALL)
2585d95089dcSBen Greear rx_mode |= (AcceptErr | AcceptRunt);
2586d95089dcSBen Greear
2587a8fe65b8SJeff Kirsher /* We can safely update without stopping the chip. */
2588a8fe65b8SJeff Kirsher tmp = rtl8139_rx_config | rx_mode;
2589a8fe65b8SJeff Kirsher if (tp->rx_config != tmp) {
2590a8fe65b8SJeff Kirsher RTL_W32_F (RxConfig, tmp);
2591a8fe65b8SJeff Kirsher tp->rx_config = tmp;
2592a8fe65b8SJeff Kirsher }
2593a8fe65b8SJeff Kirsher RTL_W32_F (MAR0 + 0, mc_filter[0]);
2594a8fe65b8SJeff Kirsher RTL_W32_F (MAR0 + 4, mc_filter[1]);
2595a8fe65b8SJeff Kirsher }
2596a8fe65b8SJeff Kirsher
rtl8139_set_rx_mode(struct net_device * dev)2597a8fe65b8SJeff Kirsher static void rtl8139_set_rx_mode (struct net_device *dev)
2598a8fe65b8SJeff Kirsher {
2599a8fe65b8SJeff Kirsher unsigned long flags;
2600a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2601a8fe65b8SJeff Kirsher
2602a8fe65b8SJeff Kirsher spin_lock_irqsave (&tp->lock, flags);
2603a8fe65b8SJeff Kirsher __set_rx_mode(dev);
2604a8fe65b8SJeff Kirsher spin_unlock_irqrestore (&tp->lock, flags);
2605a8fe65b8SJeff Kirsher }
2606a8fe65b8SJeff Kirsher
rtl8139_suspend(struct device * device)26076ad70c76SVaibhav Gupta static int __maybe_unused rtl8139_suspend(struct device *device)
2608a8fe65b8SJeff Kirsher {
26096ad70c76SVaibhav Gupta struct net_device *dev = dev_get_drvdata(device);
2610a8fe65b8SJeff Kirsher struct rtl8139_private *tp = netdev_priv(dev);
2611a8fe65b8SJeff Kirsher void __iomem *ioaddr = tp->mmio_addr;
2612a8fe65b8SJeff Kirsher unsigned long flags;
2613a8fe65b8SJeff Kirsher
2614a8fe65b8SJeff Kirsher if (!netif_running (dev))
2615a8fe65b8SJeff Kirsher return 0;
2616a8fe65b8SJeff Kirsher
2617a8fe65b8SJeff Kirsher netif_device_detach (dev);
2618a8fe65b8SJeff Kirsher
2619a8fe65b8SJeff Kirsher spin_lock_irqsave (&tp->lock, flags);
2620a8fe65b8SJeff Kirsher
2621a8fe65b8SJeff Kirsher /* Disable interrupts, stop Tx and Rx. */
2622a8fe65b8SJeff Kirsher RTL_W16 (IntrMask, 0);
2623a8fe65b8SJeff Kirsher RTL_W8 (ChipCmd, 0);
2624a8fe65b8SJeff Kirsher
2625a8fe65b8SJeff Kirsher /* Update the error counts. */
2626a8fe65b8SJeff Kirsher dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
2627a8fe65b8SJeff Kirsher RTL_W32 (RxMissed, 0);
2628a8fe65b8SJeff Kirsher
2629a8fe65b8SJeff Kirsher spin_unlock_irqrestore (&tp->lock, flags);
2630a8fe65b8SJeff Kirsher
2631a8fe65b8SJeff Kirsher return 0;
2632a8fe65b8SJeff Kirsher }
2633a8fe65b8SJeff Kirsher
rtl8139_resume(struct device * device)26346ad70c76SVaibhav Gupta static int __maybe_unused rtl8139_resume(struct device *device)
2635a8fe65b8SJeff Kirsher {
26366ad70c76SVaibhav Gupta struct net_device *dev = dev_get_drvdata(device);
2637a8fe65b8SJeff Kirsher
2638a8fe65b8SJeff Kirsher if (!netif_running (dev))
2639a8fe65b8SJeff Kirsher return 0;
26406ad70c76SVaibhav Gupta
2641a8fe65b8SJeff Kirsher rtl8139_init_ring (dev);
2642a8fe65b8SJeff Kirsher rtl8139_hw_start (dev);
2643a8fe65b8SJeff Kirsher netif_device_attach (dev);
2644a8fe65b8SJeff Kirsher return 0;
2645a8fe65b8SJeff Kirsher }
2646a8fe65b8SJeff Kirsher
26476ad70c76SVaibhav Gupta static SIMPLE_DEV_PM_OPS(rtl8139_pm_ops, rtl8139_suspend, rtl8139_resume);
2648a8fe65b8SJeff Kirsher
2649a8fe65b8SJeff Kirsher static struct pci_driver rtl8139_pci_driver = {
2650a8fe65b8SJeff Kirsher .name = DRV_NAME,
2651a8fe65b8SJeff Kirsher .id_table = rtl8139_pci_tbl,
2652a8fe65b8SJeff Kirsher .probe = rtl8139_init_one,
2653b4e54a94SBill Pemberton .remove = rtl8139_remove_one,
26546ad70c76SVaibhav Gupta .driver.pm = &rtl8139_pm_ops,
2655a8fe65b8SJeff Kirsher };
2656a8fe65b8SJeff Kirsher
2657a8fe65b8SJeff Kirsher
rtl8139_init_module(void)2658a8fe65b8SJeff Kirsher static int __init rtl8139_init_module (void)
2659a8fe65b8SJeff Kirsher {
2660a8fe65b8SJeff Kirsher /* when we're a module, we always print a version message,
2661a8fe65b8SJeff Kirsher * even if no 8139 board is found.
2662a8fe65b8SJeff Kirsher */
2663a8fe65b8SJeff Kirsher #ifdef MODULE
2664a8fe65b8SJeff Kirsher pr_info(RTL8139_DRIVER_NAME "\n");
2665a8fe65b8SJeff Kirsher #endif
2666a8fe65b8SJeff Kirsher
2667a8fe65b8SJeff Kirsher return pci_register_driver(&rtl8139_pci_driver);
2668a8fe65b8SJeff Kirsher }
2669a8fe65b8SJeff Kirsher
2670a8fe65b8SJeff Kirsher
rtl8139_cleanup_module(void)2671a8fe65b8SJeff Kirsher static void __exit rtl8139_cleanup_module (void)
2672a8fe65b8SJeff Kirsher {
2673a8fe65b8SJeff Kirsher pci_unregister_driver (&rtl8139_pci_driver);
2674a8fe65b8SJeff Kirsher }
2675a8fe65b8SJeff Kirsher
2676a8fe65b8SJeff Kirsher
2677a8fe65b8SJeff Kirsher module_init(rtl8139_init_module);
2678a8fe65b8SJeff Kirsher module_exit(rtl8139_cleanup_module);
2679