xref: /freebsd/sys/dev/re/if_re.c (revision b0c14eae10609f7d688b31d112de2a641d9cc516)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997, 1998-2003
5  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 /*
37  * Realtek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
38  *
39  * Written by Bill Paul <wpaul@windriver.com>
40  * Senior Networking Software Engineer
41  * Wind River Systems
42  */
43 
44 /*
45  * This driver is designed to support Realtek's next generation of
46  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
47  * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
48  * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
49  *
50  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
51  * with the older 8139 family, however it also supports a special
52  * C+ mode of operation that provides several new performance enhancing
53  * features. These include:
54  *
55  *	o Descriptor based DMA mechanism. Each descriptor represents
56  *	  a single packet fragment. Data buffers may be aligned on
57  *	  any byte boundary.
58  *
59  *	o 64-bit DMA
60  *
61  *	o TCP/IP checksum offload for both RX and TX
62  *
63  *	o High and normal priority transmit DMA rings
64  *
65  *	o VLAN tag insertion and extraction
66  *
67  *	o TCP large send (segmentation offload)
68  *
69  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
70  * programming API is fairly straightforward. The RX filtering, EEPROM
71  * access and PHY access is the same as it is on the older 8139 series
72  * chips.
73  *
74  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
75  * same programming API and feature set as the 8139C+ with the following
76  * differences and additions:
77  *
78  *	o 1000Mbps mode
79  *
80  *	o Jumbo frames
81  *
82  *	o GMII and TBI ports/registers for interfacing with copper
83  *	  or fiber PHYs
84  *
85  *	o RX and TX DMA rings can have up to 1024 descriptors
86  *	  (the 8139C+ allows a maximum of 64)
87  *
88  *	o Slight differences in register layout from the 8139C+
89  *
90  * The TX start and timer interrupt registers are at different locations
91  * on the 8169 than they are on the 8139C+. Also, the status word in the
92  * RX descriptor has a slightly different bit layout. The 8169 does not
93  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
94  * copper gigE PHY.
95  *
96  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
97  * (the 'S' stands for 'single-chip'). These devices have the same
98  * programming API as the older 8169, but also have some vendor-specific
99  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
100  * part designed to be pin-compatible with the Realtek 8100 10/100 chip.
101  *
102  * This driver takes advantage of the RX and TX checksum offload and
103  * VLAN tag insertion/extraction features. It also implements TX
104  * interrupt moderation using the timer interrupt registers, which
105  * significantly reduces TX interrupt load. There is also support
106  * for jumbo frames, however the 8169/8169S/8110S can not transmit
107  * jumbo frames larger than 7440, so the max MTU possible with this
108  * driver is 7422 bytes.
109  */
110 
111 #ifdef HAVE_KERNEL_OPTION_HEADERS
112 #include "opt_device_polling.h"
113 #endif
114 
115 #include <sys/param.h>
116 #include <sys/endian.h>
117 #include <sys/systm.h>
118 #include <sys/sockio.h>
119 #include <sys/mbuf.h>
120 #include <sys/malloc.h>
121 #include <sys/module.h>
122 #include <sys/kernel.h>
123 #include <sys/socket.h>
124 #include <sys/lock.h>
125 #include <sys/mutex.h>
126 #include <sys/sysctl.h>
127 #include <sys/taskqueue.h>
128 
129 #include <net/debugnet.h>
130 #include <net/if.h>
131 #include <net/if_var.h>
132 #include <net/if_arp.h>
133 #include <net/ethernet.h>
134 #include <net/if_dl.h>
135 #include <net/if_media.h>
136 #include <net/if_types.h>
137 #include <net/if_vlan_var.h>
138 
139 #include <net/bpf.h>
140 
141 #include <machine/bus.h>
142 #include <machine/resource.h>
143 #include <sys/bus.h>
144 #include <sys/rman.h>
145 
146 #include <dev/mii/mii.h>
147 #include <dev/mii/miivar.h>
148 
149 #include <dev/pci/pcireg.h>
150 #include <dev/pci/pcivar.h>
151 
152 #include <dev/rl/if_rlreg.h>
153 
154 MODULE_DEPEND(re, pci, 1, 1, 1);
155 MODULE_DEPEND(re, ether, 1, 1, 1);
156 MODULE_DEPEND(re, miibus, 1, 1, 1);
157 
158 /* "device miibus" required.  See GENERIC if you get errors here. */
159 #include "miibus_if.h"
160 
161 /* Tunables. */
162 static int intr_filter = 0;
163 TUNABLE_INT("hw.re.intr_filter", &intr_filter);
164 static int msi_disable = 0;
165 TUNABLE_INT("hw.re.msi_disable", &msi_disable);
166 static int msix_disable = 0;
167 TUNABLE_INT("hw.re.msix_disable", &msix_disable);
168 static int prefer_iomap = 0;
169 TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
170 static int aspm_disable = 1;
171 TUNABLE_INT("hw.re.aspm_disable", &aspm_disable);
172 
173 #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
174 
175 /*
176  * Various supported device vendors/types and their names.
177  */
178 static const struct rl_type re_devs[] = {
179 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
180 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
181 	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
182 	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
183 	{ RT_VENDORID, RT_DEVICEID_2600, 0,
184 	   "Realtek Killer E2600 Gigabit Ethernet Controller" },
185 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
186 	    "Realtek 8139C+ 10/100BaseTX" },
187 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
188 	    "Realtek 810xE PCIe 10/100baseTX" },
189 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
190 	    "Realtek 8168/8111 B/C/CP/D/DP/E/F/G PCIe Gigabit Ethernet" },
191 	{ RT_VENDORID, RT_DEVICEID_8161, 0,
192 	    "Realtek 8168 Gigabit Ethernet" },
193 	{ NCUBE_VENDORID, RT_DEVICEID_8168, 0,
194 	    "TP-Link TG-3468 v2 (RTL8168) Gigabit Ethernet" },
195 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
196 	    "Realtek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
197 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
198 	    "Realtek 8169SC/8110SC Single-chip Gigabit Ethernet" },
199 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
200 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
201 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
202 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
203 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
204 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
205 };
206 
207 static const struct rl_hwrev re_hwrevs[] = {
208 	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
209 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
210 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
211 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
212 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
213 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
214 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
215 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
216 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
217 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
218 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
219 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
220 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
221 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
222 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
223 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
224 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
225 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
226 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
227 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
228 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
229 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
230 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
231 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
232 	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
233 	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
234 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
235 	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
236 	{ RL_HWREV_8106E, RL_8169, "8106E", RL_MTU },
237 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
238 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
239 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
240 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
241 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
242 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
243 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
244 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
245 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
246 	{ RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K},
247 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
248 	{ RL_HWREV_8168FP, RL_8169, "8168FP/8111FP", RL_JUMBO_MTU_9K},
249 	{ RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K},
250 	{ RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K},
251 	{ RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K},
252 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
253 	{ RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K},
254 	{ 0, 0, NULL, 0 }
255 };
256 
257 static int re_probe		(device_t);
258 static int re_attach		(device_t);
259 static int re_detach		(device_t);
260 
261 static int re_encap		(struct rl_softc *, struct mbuf **);
262 
263 static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
264 static int re_allocmem		(device_t, struct rl_softc *);
265 static __inline void re_discard_rxbuf
266 				(struct rl_softc *, int);
267 static int re_newbuf		(struct rl_softc *, int);
268 static int re_jumbo_newbuf	(struct rl_softc *, int);
269 static int re_rx_list_init	(struct rl_softc *);
270 static int re_jrx_list_init	(struct rl_softc *);
271 static int re_tx_list_init	(struct rl_softc *);
272 #ifdef RE_FIXUP_RX
273 static __inline void re_fixup_rx
274 				(struct mbuf *);
275 #endif
276 static int re_rxeof		(struct rl_softc *, int *);
277 static void re_txeof		(struct rl_softc *);
278 #ifdef DEVICE_POLLING
279 static int re_poll		(if_t, enum poll_cmd, int);
280 static int re_poll_locked	(if_t, enum poll_cmd, int);
281 #endif
282 static int re_intr		(void *);
283 static void re_intr_msi		(void *);
284 static void re_tick		(void *);
285 static void re_int_task		(void *, int);
286 static void re_start		(if_t);
287 static void re_start_locked	(if_t);
288 static void re_start_tx		(struct rl_softc *);
289 static int re_ioctl		(if_t, u_long, caddr_t);
290 static void re_init		(void *);
291 static void re_init_locked	(struct rl_softc *);
292 static void re_stop		(struct rl_softc *);
293 static void re_watchdog		(struct rl_softc *);
294 static int re_suspend		(device_t);
295 static int re_resume		(device_t);
296 static int re_shutdown		(device_t);
297 static int re_ifmedia_upd	(if_t);
298 static void re_ifmedia_sts	(if_t, struct ifmediareq *);
299 
300 static void re_eeprom_putbyte	(struct rl_softc *, int);
301 static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
302 static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
303 static int re_gmii_readreg	(device_t, int, int);
304 static int re_gmii_writereg	(device_t, int, int, int);
305 
306 static int re_miibus_readreg	(device_t, int, int);
307 static int re_miibus_writereg	(device_t, int, int, int);
308 static void re_miibus_statchg	(device_t);
309 
310 static void re_set_jumbo	(struct rl_softc *, int);
311 static void re_set_rxmode		(struct rl_softc *);
312 static void re_reset		(struct rl_softc *);
313 static void re_setwol		(struct rl_softc *);
314 static void re_clrwol		(struct rl_softc *);
315 static void re_set_linkspeed	(struct rl_softc *);
316 
317 DEBUGNET_DEFINE(re);
318 
319 #ifdef DEV_NETMAP	/* see ixgbe.c for details */
320 #include <dev/netmap/if_re_netmap.h>
321 MODULE_DEPEND(re, netmap, 1, 1, 1);
322 #endif /* !DEV_NETMAP */
323 
324 #ifdef RE_DIAG
325 static int re_diag		(struct rl_softc *);
326 #endif
327 
328 static void re_add_sysctls	(struct rl_softc *);
329 static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
330 static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
331 static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
332 
333 static device_method_t re_methods[] = {
334 	/* Device interface */
335 	DEVMETHOD(device_probe,		re_probe),
336 	DEVMETHOD(device_attach,	re_attach),
337 	DEVMETHOD(device_detach,	re_detach),
338 	DEVMETHOD(device_suspend,	re_suspend),
339 	DEVMETHOD(device_resume,	re_resume),
340 	DEVMETHOD(device_shutdown,	re_shutdown),
341 
342 	/* MII interface */
343 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
344 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
345 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
346 
347 	DEVMETHOD_END
348 };
349 
350 static driver_t re_driver = {
351 	"re",
352 	re_methods,
353 	sizeof(struct rl_softc)
354 };
355 
356 DRIVER_MODULE(re, pci, re_driver, 0, 0);
357 DRIVER_MODULE(miibus, re, miibus_driver, 0, 0);
358 MODULE_PNP_INFO("U16:vendor;U16:device;U32:#;D:#", pci, re, re_devs,
359     nitems(re_devs) - 1);
360 
361 #define EE_SET(x)					\
362 	CSR_WRITE_1(sc, RL_EECMD,			\
363 		CSR_READ_1(sc, RL_EECMD) | x)
364 
365 #define EE_CLR(x)					\
366 	CSR_WRITE_1(sc, RL_EECMD,			\
367 		CSR_READ_1(sc, RL_EECMD) & ~x)
368 
369 /*
370  * Send a read command and address to the EEPROM, check for ACK.
371  */
372 static void
re_eeprom_putbyte(struct rl_softc * sc,int addr)373 re_eeprom_putbyte(struct rl_softc *sc, int addr)
374 {
375 	int			d, i;
376 
377 	d = addr | (RL_9346_READ << sc->rl_eewidth);
378 
379 	/*
380 	 * Feed in each bit and strobe the clock.
381 	 */
382 
383 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
384 		if (d & i) {
385 			EE_SET(RL_EE_DATAIN);
386 		} else {
387 			EE_CLR(RL_EE_DATAIN);
388 		}
389 		DELAY(100);
390 		EE_SET(RL_EE_CLK);
391 		DELAY(150);
392 		EE_CLR(RL_EE_CLK);
393 		DELAY(100);
394 	}
395 }
396 
397 /*
398  * Read a word of data stored in the EEPROM at address 'addr.'
399  */
400 static void
re_eeprom_getword(struct rl_softc * sc,int addr,u_int16_t * dest)401 re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
402 {
403 	int			i;
404 	u_int16_t		word = 0;
405 
406 	/*
407 	 * Send address of word we want to read.
408 	 */
409 	re_eeprom_putbyte(sc, addr);
410 
411 	/*
412 	 * Start reading bits from EEPROM.
413 	 */
414 	for (i = 0x8000; i; i >>= 1) {
415 		EE_SET(RL_EE_CLK);
416 		DELAY(100);
417 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
418 			word |= i;
419 		EE_CLR(RL_EE_CLK);
420 		DELAY(100);
421 	}
422 
423 	*dest = word;
424 }
425 
426 /*
427  * Read a sequence of words from the EEPROM.
428  */
429 static void
re_read_eeprom(struct rl_softc * sc,caddr_t dest,int off,int cnt)430 re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
431 {
432 	int			i;
433 	u_int16_t		word = 0, *ptr;
434 
435 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
436 
437         DELAY(100);
438 
439 	for (i = 0; i < cnt; i++) {
440 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
441 		re_eeprom_getword(sc, off + i, &word);
442 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
443 		ptr = (u_int16_t *)(dest + (i * 2));
444                 *ptr = word;
445 	}
446 
447 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
448 }
449 
450 static int
re_gmii_readreg(device_t dev,int phy,int reg)451 re_gmii_readreg(device_t dev, int phy, int reg)
452 {
453 	struct rl_softc		*sc;
454 	u_int32_t		rval;
455 	int			i;
456 
457 	sc = device_get_softc(dev);
458 
459 	/* Let the rgephy driver read the GMEDIASTAT register */
460 
461 	if (reg == RL_GMEDIASTAT) {
462 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
463 		return (rval);
464 	}
465 
466 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
467 
468 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
469 		rval = CSR_READ_4(sc, RL_PHYAR);
470 		if (rval & RL_PHYAR_BUSY)
471 			break;
472 		DELAY(25);
473 	}
474 
475 	if (i == RL_PHY_TIMEOUT) {
476 		device_printf(sc->rl_dev, "PHY read failed\n");
477 		return (0);
478 	}
479 
480 	/*
481 	 * Controller requires a 20us delay to process next MDIO request.
482 	 */
483 	DELAY(20);
484 
485 	return (rval & RL_PHYAR_PHYDATA);
486 }
487 
488 static int
re_gmii_writereg(device_t dev,int phy,int reg,int data)489 re_gmii_writereg(device_t dev, int phy, int reg, int data)
490 {
491 	struct rl_softc		*sc;
492 	u_int32_t		rval;
493 	int			i;
494 
495 	sc = device_get_softc(dev);
496 
497 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
498 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
499 
500 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
501 		rval = CSR_READ_4(sc, RL_PHYAR);
502 		if (!(rval & RL_PHYAR_BUSY))
503 			break;
504 		DELAY(25);
505 	}
506 
507 	if (i == RL_PHY_TIMEOUT) {
508 		device_printf(sc->rl_dev, "PHY write failed\n");
509 		return (0);
510 	}
511 
512 	/*
513 	 * Controller requires a 20us delay to process next MDIO request.
514 	 */
515 	DELAY(20);
516 
517 	return (0);
518 }
519 
520 static int
re_miibus_readreg(device_t dev,int phy,int reg)521 re_miibus_readreg(device_t dev, int phy, int reg)
522 {
523 	struct rl_softc		*sc;
524 	u_int16_t		rval = 0;
525 	u_int16_t		re8139_reg = 0;
526 
527 	sc = device_get_softc(dev);
528 
529 	if (sc->rl_type == RL_8169) {
530 		rval = re_gmii_readreg(dev, phy, reg);
531 		return (rval);
532 	}
533 
534 	switch (reg) {
535 	case MII_BMCR:
536 		re8139_reg = RL_BMCR;
537 		break;
538 	case MII_BMSR:
539 		re8139_reg = RL_BMSR;
540 		break;
541 	case MII_ANAR:
542 		re8139_reg = RL_ANAR;
543 		break;
544 	case MII_ANER:
545 		re8139_reg = RL_ANER;
546 		break;
547 	case MII_ANLPAR:
548 		re8139_reg = RL_LPAR;
549 		break;
550 	case MII_PHYIDR1:
551 	case MII_PHYIDR2:
552 		return (0);
553 	/*
554 	 * Allow the rlphy driver to read the media status
555 	 * register. If we have a link partner which does not
556 	 * support NWAY, this is the register which will tell
557 	 * us the results of parallel detection.
558 	 */
559 	case RL_MEDIASTAT:
560 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
561 		return (rval);
562 	default:
563 		device_printf(sc->rl_dev, "bad phy register\n");
564 		return (0);
565 	}
566 	rval = CSR_READ_2(sc, re8139_reg);
567 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
568 		/* 8139C+ has different bit layout. */
569 		rval &= ~(BMCR_LOOP | BMCR_ISO);
570 	}
571 	return (rval);
572 }
573 
574 static int
re_miibus_writereg(device_t dev,int phy,int reg,int data)575 re_miibus_writereg(device_t dev, int phy, int reg, int data)
576 {
577 	struct rl_softc		*sc;
578 	u_int16_t		re8139_reg = 0;
579 	int			rval = 0;
580 
581 	sc = device_get_softc(dev);
582 
583 	if (sc->rl_type == RL_8169) {
584 		rval = re_gmii_writereg(dev, phy, reg, data);
585 		return (rval);
586 	}
587 
588 	switch (reg) {
589 	case MII_BMCR:
590 		re8139_reg = RL_BMCR;
591 		if (sc->rl_type == RL_8139CPLUS) {
592 			/* 8139C+ has different bit layout. */
593 			data &= ~(BMCR_LOOP | BMCR_ISO);
594 		}
595 		break;
596 	case MII_BMSR:
597 		re8139_reg = RL_BMSR;
598 		break;
599 	case MII_ANAR:
600 		re8139_reg = RL_ANAR;
601 		break;
602 	case MII_ANER:
603 		re8139_reg = RL_ANER;
604 		break;
605 	case MII_ANLPAR:
606 		re8139_reg = RL_LPAR;
607 		break;
608 	case MII_PHYIDR1:
609 	case MII_PHYIDR2:
610 		return (0);
611 		break;
612 	default:
613 		device_printf(sc->rl_dev, "bad phy register\n");
614 		return (0);
615 	}
616 	CSR_WRITE_2(sc, re8139_reg, data);
617 	return (0);
618 }
619 
620 static void
re_miibus_statchg(device_t dev)621 re_miibus_statchg(device_t dev)
622 {
623 	struct rl_softc		*sc;
624 	if_t ifp;
625 	struct mii_data		*mii;
626 
627 	sc = device_get_softc(dev);
628 	mii = device_get_softc(sc->rl_miibus);
629 	ifp = sc->rl_ifp;
630 	if (mii == NULL || ifp == NULL ||
631 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
632 		return;
633 
634 	sc->rl_flags &= ~RL_FLAG_LINK;
635 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
636 	    (IFM_ACTIVE | IFM_AVALID)) {
637 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
638 		case IFM_10_T:
639 		case IFM_100_TX:
640 			sc->rl_flags |= RL_FLAG_LINK;
641 			break;
642 		case IFM_1000_T:
643 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
644 				break;
645 			sc->rl_flags |= RL_FLAG_LINK;
646 			break;
647 		default:
648 			break;
649 		}
650 	}
651 	/*
652 	 * Realtek controllers do not provide any interface to the RX/TX
653 	 * MACs for resolved speed, duplex and flow-control parameters.
654 	 */
655 }
656 
657 static u_int
re_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)658 re_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
659 {
660 	uint32_t h, *hashes = arg;
661 
662 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
663 	if (h < 32)
664 		hashes[0] |= (1 << h);
665 	else
666 		hashes[1] |= (1 << (h - 32));
667 
668 	return (1);
669 }
670 
671 /*
672  * Set the RX configuration and 64-bit multicast hash filter.
673  */
674 static void
re_set_rxmode(struct rl_softc * sc)675 re_set_rxmode(struct rl_softc *sc)
676 {
677 	if_t ifp;
678 	uint32_t		h, hashes[2] = { 0, 0 };
679 	uint32_t		rxfilt;
680 
681 	RL_LOCK_ASSERT(sc);
682 
683 	ifp = sc->rl_ifp;
684 
685 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
686 	if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0)
687 		rxfilt |= RL_RXCFG_EARLYOFF;
688 	else if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
689 		rxfilt |= RL_RXCFG_EARLYOFFV2;
690 
691 	if (if_getflags(ifp) & (IFF_ALLMULTI | IFF_PROMISC)) {
692 		if (if_getflags(ifp) & IFF_PROMISC)
693 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
694 		/*
695 		 * Unlike other hardwares, we have to explicitly set
696 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
697 		 * promiscuous mode.
698 		 */
699 		rxfilt |= RL_RXCFG_RX_MULTI;
700 		hashes[0] = hashes[1] = 0xffffffff;
701 		goto done;
702 	}
703 
704 	if_foreach_llmaddr(ifp, re_hash_maddr, hashes);
705 
706 	if (hashes[0] != 0 || hashes[1] != 0) {
707 		/*
708 		 * For some unfathomable reason, Realtek decided to
709 		 * reverse the order of the multicast hash registers
710 		 * in the PCI Express parts.  This means we have to
711 		 * write the hash pattern in reverse order for those
712 		 * devices.
713 		 */
714 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
715 			h = bswap32(hashes[0]);
716 			hashes[0] = bswap32(hashes[1]);
717 			hashes[1] = h;
718 		}
719 		rxfilt |= RL_RXCFG_RX_MULTI;
720 	}
721 
722 	if  (sc->rl_hwrev->rl_rev == RL_HWREV_8168F) {
723 		/* Disable multicast filtering due to silicon bug. */
724 		hashes[0] = 0xffffffff;
725 		hashes[1] = 0xffffffff;
726 	}
727 
728 done:
729 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
730 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
731 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
732 }
733 
734 static void
re_reset(struct rl_softc * sc)735 re_reset(struct rl_softc *sc)
736 {
737 	int			i;
738 
739 	RL_LOCK_ASSERT(sc);
740 
741 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
742 
743 	for (i = 0; i < RL_TIMEOUT; i++) {
744 		DELAY(10);
745 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
746 			break;
747 	}
748 	if (i == RL_TIMEOUT)
749 		device_printf(sc->rl_dev, "reset never completed!\n");
750 
751 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
752 		CSR_WRITE_1(sc, 0x82, 1);
753 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
754 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
755 }
756 
757 #ifdef RE_DIAG
758 
759 /*
760  * The following routine is designed to test for a defect on some
761  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
762  * lines connected to the bus, however for a 32-bit only card, they
763  * should be pulled high. The result of this defect is that the
764  * NIC will not work right if you plug it into a 64-bit slot: DMA
765  * operations will be done with 64-bit transfers, which will fail
766  * because the 64-bit data lines aren't connected.
767  *
768  * There's no way to work around this (short of talking a soldering
769  * iron to the board), however we can detect it. The method we use
770  * here is to put the NIC into digital loopback mode, set the receiver
771  * to promiscuous mode, and then try to send a frame. We then compare
772  * the frame data we sent to what was received. If the data matches,
773  * then the NIC is working correctly, otherwise we know the user has
774  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
775  * slot. In the latter case, there's no way the NIC can work correctly,
776  * so we print out a message on the console and abort the device attach.
777  */
778 
779 static int
re_diag(struct rl_softc * sc)780 re_diag(struct rl_softc *sc)
781 {
782 	if_t ifp = sc->rl_ifp;
783 	struct mbuf		*m0;
784 	struct ether_header	*eh;
785 	struct rl_desc		*cur_rx;
786 	u_int16_t		status;
787 	u_int32_t		rxstat;
788 	int			total_len, i, error = 0, phyaddr;
789 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
790 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
791 
792 	/* Allocate a single mbuf */
793 	MGETHDR(m0, M_NOWAIT, MT_DATA);
794 	if (m0 == NULL)
795 		return (ENOBUFS);
796 
797 	RL_LOCK(sc);
798 
799 	/*
800 	 * Initialize the NIC in test mode. This sets the chip up
801 	 * so that it can send and receive frames, but performs the
802 	 * following special functions:
803 	 * - Puts receiver in promiscuous mode
804 	 * - Enables digital loopback mode
805 	 * - Leaves interrupts turned off
806 	 */
807 
808 	if_setflagbit(ifp, IFF_PROMISC, 0);
809 	sc->rl_testmode = 1;
810 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
811 	re_init_locked(sc);
812 	sc->rl_flags |= RL_FLAG_LINK;
813 	if (sc->rl_type == RL_8169)
814 		phyaddr = 1;
815 	else
816 		phyaddr = 0;
817 
818 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
819 	for (i = 0; i < RL_TIMEOUT; i++) {
820 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
821 		if (!(status & BMCR_RESET))
822 			break;
823 	}
824 
825 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
826 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
827 
828 	DELAY(100000);
829 
830 	/* Put some data in the mbuf */
831 
832 	eh = mtod(m0, struct ether_header *);
833 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
834 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
835 	eh->ether_type = htons(ETHERTYPE_IP);
836 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
837 
838 	/*
839 	 * Queue the packet, start transmission.
840 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
841 	 */
842 
843 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
844 	RL_UNLOCK(sc);
845 	/* XXX: re_diag must not be called when in ALTQ mode */
846 	if_handoff(ifp, m0, ifp);
847 	RL_LOCK(sc);
848 	m0 = NULL;
849 
850 	/* Wait for it to propagate through the chip */
851 
852 	DELAY(100000);
853 	for (i = 0; i < RL_TIMEOUT; i++) {
854 		status = CSR_READ_2(sc, RL_ISR);
855 		CSR_WRITE_2(sc, RL_ISR, status);
856 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
857 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
858 			break;
859 		DELAY(10);
860 	}
861 
862 	if (i == RL_TIMEOUT) {
863 		device_printf(sc->rl_dev,
864 		    "diagnostic failed, failed to receive packet in"
865 		    " loopback mode\n");
866 		error = EIO;
867 		goto done;
868 	}
869 
870 	/*
871 	 * The packet should have been dumped into the first
872 	 * entry in the RX DMA ring. Grab it from there.
873 	 */
874 
875 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
876 	    sc->rl_ldata.rl_rx_list_map,
877 	    BUS_DMASYNC_POSTREAD);
878 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
879 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
880 	    BUS_DMASYNC_POSTREAD);
881 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
882 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
883 
884 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
885 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
886 	eh = mtod(m0, struct ether_header *);
887 
888 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
889 	total_len = RL_RXBYTES(cur_rx);
890 	rxstat = le32toh(cur_rx->rl_cmdstat);
891 
892 	if (total_len != ETHER_MIN_LEN) {
893 		device_printf(sc->rl_dev,
894 		    "diagnostic failed, received short packet\n");
895 		error = EIO;
896 		goto done;
897 	}
898 
899 	/* Test that the received packet data matches what we sent. */
900 
901 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
902 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
903 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
904 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
905 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
906 		    dst, ":", src, ":", ETHERTYPE_IP);
907 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
908 		    eh->ether_dhost, ":", eh->ether_shost, ":",
909 		    ntohs(eh->ether_type));
910 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
911 		    "NIC plugged into a 64-bit PCI slot.\n");
912 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
913 		    "32-bit slot for proper operation.\n");
914 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
915 		    "details.\n");
916 		error = EIO;
917 	}
918 
919 done:
920 	/* Turn interface off, release resources */
921 
922 	sc->rl_testmode = 0;
923 	sc->rl_flags &= ~RL_FLAG_LINK;
924 	if_setflagbit(ifp, 0, IFF_PROMISC);
925 	re_stop(sc);
926 	if (m0 != NULL)
927 		m_freem(m0);
928 
929 	RL_UNLOCK(sc);
930 
931 	return (error);
932 }
933 
934 #endif
935 
936 /*
937  * Probe for a Realtek 8139C+/8169/8110 chip. Check the PCI vendor and device
938  * IDs against our list and return a device name if we find a match.
939  */
940 static int
re_probe(device_t dev)941 re_probe(device_t dev)
942 {
943 	const struct rl_type	*t;
944 	uint16_t		devid, vendor;
945 	uint16_t		revid, sdevid;
946 	int			i;
947 
948 	vendor = pci_get_vendor(dev);
949 	devid = pci_get_device(dev);
950 	revid = pci_get_revid(dev);
951 	sdevid = pci_get_subdevice(dev);
952 
953 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
954 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
955 			/*
956 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
957 			 * Rev. 2 is supported by sk(4).
958 			 */
959 			return (ENXIO);
960 		}
961 	}
962 
963 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
964 		if (revid != 0x20) {
965 			/* 8139, let rl(4) take care of this device. */
966 			return (ENXIO);
967 		}
968 	}
969 
970 	t = re_devs;
971 	for (i = 0; i < nitems(re_devs); i++, t++) {
972 		if (vendor == t->rl_vid && devid == t->rl_did) {
973 			device_set_desc(dev, t->rl_name);
974 			return (BUS_PROBE_DEFAULT);
975 		}
976 	}
977 
978 	return (ENXIO);
979 }
980 
981 /*
982  * Map a single buffer address.
983  */
984 
985 static void
re_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)986 re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
987 {
988 	bus_addr_t		*addr;
989 
990 	if (error)
991 		return;
992 
993 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
994 	addr = arg;
995 	*addr = segs->ds_addr;
996 }
997 
998 static int
re_allocmem(device_t dev,struct rl_softc * sc)999 re_allocmem(device_t dev, struct rl_softc *sc)
1000 {
1001 	bus_addr_t		lowaddr;
1002 	bus_size_t		rx_list_size, tx_list_size;
1003 	int			error;
1004 	int			i;
1005 
1006 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
1007 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
1008 
1009 	/*
1010 	 * Allocate the parent bus DMA tag appropriate for PCI.
1011 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
1012 	 * register should be set. However some Realtek chips are known
1013 	 * to be buggy on DAC handling, therefore disable DAC by limiting
1014 	 * DMA address space to 32bit. PCIe variants of Realtek chips
1015 	 * may not have the limitation.
1016 	 */
1017 	lowaddr = BUS_SPACE_MAXADDR;
1018 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
1019 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
1020 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
1021 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
1022 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1023 	    NULL, NULL, &sc->rl_parent_tag);
1024 	if (error) {
1025 		device_printf(dev, "could not allocate parent DMA tag\n");
1026 		return (error);
1027 	}
1028 
1029 	/*
1030 	 * Allocate map for TX mbufs.
1031 	 */
1032 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1033 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1034 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1035 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1036 	if (error) {
1037 		device_printf(dev, "could not allocate TX DMA tag\n");
1038 		return (error);
1039 	}
1040 
1041 	/*
1042 	 * Allocate map for RX mbufs.
1043 	 */
1044 
1045 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1046 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
1047 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1048 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
1049 		    &sc->rl_ldata.rl_jrx_mtag);
1050 		if (error) {
1051 			device_printf(dev,
1052 			    "could not allocate jumbo RX DMA tag\n");
1053 			return (error);
1054 		}
1055 	}
1056 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1057 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1058 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1059 	if (error) {
1060 		device_printf(dev, "could not allocate RX DMA tag\n");
1061 		return (error);
1062 	}
1063 
1064 	/*
1065 	 * Allocate map for TX descriptor list.
1066 	 */
1067 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1068 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1069 	    NULL, tx_list_size, 1, tx_list_size, 0,
1070 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1071 	if (error) {
1072 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1073 		return (error);
1074 	}
1075 
1076 	/* Allocate DMA'able memory for the TX ring */
1077 
1078 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1079 	    (void **)&sc->rl_ldata.rl_tx_list,
1080 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1081 	    &sc->rl_ldata.rl_tx_list_map);
1082 	if (error) {
1083 		device_printf(dev, "could not allocate TX DMA ring\n");
1084 		return (error);
1085 	}
1086 
1087 	/* Load the map for the TX ring. */
1088 
1089 	sc->rl_ldata.rl_tx_list_addr = 0;
1090 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1091 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1092 	     tx_list_size, re_dma_map_addr,
1093 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1094 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1095 		device_printf(dev, "could not load TX DMA ring\n");
1096 		return (ENOMEM);
1097 	}
1098 
1099 	/* Create DMA maps for TX buffers */
1100 
1101 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1102 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1103 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1104 		if (error) {
1105 			device_printf(dev, "could not create DMA map for TX\n");
1106 			return (error);
1107 		}
1108 	}
1109 
1110 	/*
1111 	 * Allocate map for RX descriptor list.
1112 	 */
1113 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1114 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1115 	    NULL, rx_list_size, 1, rx_list_size, 0,
1116 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1117 	if (error) {
1118 		device_printf(dev, "could not create RX DMA ring tag\n");
1119 		return (error);
1120 	}
1121 
1122 	/* Allocate DMA'able memory for the RX ring */
1123 
1124 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1125 	    (void **)&sc->rl_ldata.rl_rx_list,
1126 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1127 	    &sc->rl_ldata.rl_rx_list_map);
1128 	if (error) {
1129 		device_printf(dev, "could not allocate RX DMA ring\n");
1130 		return (error);
1131 	}
1132 
1133 	/* Load the map for the RX ring. */
1134 
1135 	sc->rl_ldata.rl_rx_list_addr = 0;
1136 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1137 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1138 	     rx_list_size, re_dma_map_addr,
1139 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1140 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1141 		device_printf(dev, "could not load RX DMA ring\n");
1142 		return (ENOMEM);
1143 	}
1144 
1145 	/* Create DMA maps for RX buffers */
1146 
1147 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1148 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1149 		    &sc->rl_ldata.rl_jrx_sparemap);
1150 		if (error) {
1151 			device_printf(dev,
1152 			    "could not create spare DMA map for jumbo RX\n");
1153 			return (error);
1154 		}
1155 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1156 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1157 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1158 			if (error) {
1159 				device_printf(dev,
1160 				    "could not create DMA map for jumbo RX\n");
1161 				return (error);
1162 			}
1163 		}
1164 	}
1165 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1166 	    &sc->rl_ldata.rl_rx_sparemap);
1167 	if (error) {
1168 		device_printf(dev, "could not create spare DMA map for RX\n");
1169 		return (error);
1170 	}
1171 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1172 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1173 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1174 		if (error) {
1175 			device_printf(dev, "could not create DMA map for RX\n");
1176 			return (error);
1177 		}
1178 	}
1179 
1180 	/* Create DMA map for statistics. */
1181 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
1182 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1183 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
1184 	    &sc->rl_ldata.rl_stag);
1185 	if (error) {
1186 		device_printf(dev, "could not create statistics DMA tag\n");
1187 		return (error);
1188 	}
1189 	/* Allocate DMA'able memory for statistics. */
1190 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
1191 	    (void **)&sc->rl_ldata.rl_stats,
1192 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1193 	    &sc->rl_ldata.rl_smap);
1194 	if (error) {
1195 		device_printf(dev,
1196 		    "could not allocate statistics DMA memory\n");
1197 		return (error);
1198 	}
1199 	/* Load the map for statistics. */
1200 	sc->rl_ldata.rl_stats_addr = 0;
1201 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
1202 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
1203 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
1204 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
1205 		device_printf(dev, "could not load statistics DMA memory\n");
1206 		return (ENOMEM);
1207 	}
1208 
1209 	return (0);
1210 }
1211 
1212 /*
1213  * Attach the interface. Allocate softc structures, do ifmedia
1214  * setup and ethernet/BPF attach.
1215  */
1216 static int
re_attach(device_t dev)1217 re_attach(device_t dev)
1218 {
1219 	u_char			eaddr[ETHER_ADDR_LEN];
1220 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1221 	struct rl_softc		*sc;
1222 	if_t ifp;
1223 	const struct rl_hwrev	*hw_rev;
1224 	int			capmask, error = 0, hwrev, i, msic, msixc,
1225 				phy, reg, rid;
1226 	u_int32_t		cap, ctl;
1227 	u_int16_t		devid, re_did = 0;
1228 	uint8_t			cfg;
1229 
1230 	sc = device_get_softc(dev);
1231 	sc->rl_dev = dev;
1232 
1233 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1234 	    MTX_DEF);
1235 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1236 
1237 	/*
1238 	 * Map control/status registers.
1239 	 */
1240 	pci_enable_busmaster(dev);
1241 
1242 	devid = pci_get_device(dev);
1243 	/*
1244 	 * Prefer memory space register mapping over IO space.
1245 	 * Because RTL8169SC does not seem to work when memory mapping
1246 	 * is used always activate io mapping.
1247 	 */
1248 	if (devid == RT_DEVICEID_8169SC)
1249 		prefer_iomap = 1;
1250 	if (prefer_iomap == 0) {
1251 		sc->rl_res_id = PCIR_BAR(1);
1252 		sc->rl_res_type = SYS_RES_MEMORY;
1253 		/* RTL8168/8101E seems to use different BARs. */
1254 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1255 			sc->rl_res_id = PCIR_BAR(2);
1256 	} else {
1257 		sc->rl_res_id = PCIR_BAR(0);
1258 		sc->rl_res_type = SYS_RES_IOPORT;
1259 	}
1260 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1261 	    &sc->rl_res_id, RF_ACTIVE);
1262 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1263 		sc->rl_res_id = PCIR_BAR(0);
1264 		sc->rl_res_type = SYS_RES_IOPORT;
1265 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1266 		    &sc->rl_res_id, RF_ACTIVE);
1267 	}
1268 	if (sc->rl_res == NULL) {
1269 		device_printf(dev, "couldn't map ports/memory\n");
1270 		error = ENXIO;
1271 		goto fail;
1272 	}
1273 
1274 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1275 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1276 
1277 	msic = pci_msi_count(dev);
1278 	msixc = pci_msix_count(dev);
1279 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
1280 		sc->rl_flags |= RL_FLAG_PCIE;
1281 		sc->rl_expcap = reg;
1282 	}
1283 	if (bootverbose) {
1284 		device_printf(dev, "MSI count : %d\n", msic);
1285 		device_printf(dev, "MSI-X count : %d\n", msixc);
1286 	}
1287 	if (msix_disable > 0)
1288 		msixc = 0;
1289 	if (msi_disable > 0)
1290 		msic = 0;
1291 	/* Prefer MSI-X to MSI. */
1292 	if (msixc > 0) {
1293 		msixc = RL_MSI_MESSAGES;
1294 		rid = PCIR_BAR(4);
1295 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1296 		    &rid, RF_ACTIVE);
1297 		if (sc->rl_res_pba == NULL) {
1298 			device_printf(sc->rl_dev,
1299 			    "could not allocate MSI-X PBA resource\n");
1300 		}
1301 		if (sc->rl_res_pba != NULL &&
1302 		    pci_alloc_msix(dev, &msixc) == 0) {
1303 			if (msixc == RL_MSI_MESSAGES) {
1304 				device_printf(dev, "Using %d MSI-X message\n",
1305 				    msixc);
1306 				sc->rl_flags |= RL_FLAG_MSIX;
1307 			} else
1308 				pci_release_msi(dev);
1309 		}
1310 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
1311 			if (sc->rl_res_pba != NULL)
1312 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
1313 				    sc->rl_res_pba);
1314 			sc->rl_res_pba = NULL;
1315 			msixc = 0;
1316 		}
1317 	}
1318 	/* Prefer MSI to INTx. */
1319 	if (msixc == 0 && msic > 0) {
1320 		msic = RL_MSI_MESSAGES;
1321 		if (pci_alloc_msi(dev, &msic) == 0) {
1322 			if (msic == RL_MSI_MESSAGES) {
1323 				device_printf(dev, "Using %d MSI message\n",
1324 				    msic);
1325 				sc->rl_flags |= RL_FLAG_MSI;
1326 				/* Explicitly set MSI enable bit. */
1327 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1328 				cfg = CSR_READ_1(sc, RL_CFG2);
1329 				cfg |= RL_CFG2_MSI;
1330 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1331 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1332 			} else
1333 				pci_release_msi(dev);
1334 		}
1335 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
1336 			msic = 0;
1337 	}
1338 
1339 	/* Allocate interrupt */
1340 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
1341 		rid = 0;
1342 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1343 		    RF_SHAREABLE | RF_ACTIVE);
1344 		if (sc->rl_irq[0] == NULL) {
1345 			device_printf(dev, "couldn't allocate IRQ resources\n");
1346 			error = ENXIO;
1347 			goto fail;
1348 		}
1349 	} else {
1350 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
1351 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
1352 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
1353 			if (sc->rl_irq[i] == NULL) {
1354 				device_printf(dev,
1355 				    "couldn't allocate IRQ resources for "
1356 				    "message %d\n", rid);
1357 				error = ENXIO;
1358 				goto fail;
1359 			}
1360 		}
1361 	}
1362 
1363 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
1364 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1365 		cfg = CSR_READ_1(sc, RL_CFG2);
1366 		if ((cfg & RL_CFG2_MSI) != 0) {
1367 			device_printf(dev, "turning off MSI enable bit.\n");
1368 			cfg &= ~RL_CFG2_MSI;
1369 			CSR_WRITE_1(sc, RL_CFG2, cfg);
1370 		}
1371 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1372 	}
1373 
1374 	/*
1375 	 * Disable ASPM L0S/L1 and CLKREQ.  ASPM is implicated in Tx
1376 	 * stalls and watchdog timeouts on many chip revisions, so it
1377 	 * is turned off by default; set the hw.re.aspm_disable tunable
1378 	 * to 0 to keep the firmware-configured ASPM state and trade
1379 	 * reliability under sustained load for link power management.
1380 	 */
1381 	if (aspm_disable != 0 && sc->rl_expcap != 0) {
1382 		cap = pci_read_config(dev, sc->rl_expcap +
1383 		    PCIER_LINK_CAP, 2);
1384 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1385 			ctl = pci_read_config(dev, sc->rl_expcap +
1386 			    PCIER_LINK_CTL, 2);
1387 			if ((ctl & (PCIEM_LINK_CTL_ECPM |
1388 			    PCIEM_LINK_CTL_ASPMC))!= 0) {
1389 				ctl &= ~(PCIEM_LINK_CTL_ECPM |
1390 				    PCIEM_LINK_CTL_ASPMC);
1391 				pci_write_config(dev, sc->rl_expcap +
1392 				    PCIER_LINK_CTL, ctl, 2);
1393 				device_printf(dev, "ASPM disabled\n");
1394 			}
1395 		} else
1396 			device_printf(dev, "no ASPM capability\n");
1397 	}
1398 
1399 	hw_rev = re_hwrevs;
1400 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1401 	switch (hwrev & 0x70000000) {
1402 	case 0x00000000:
1403 	case 0x10000000:
1404 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1405 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1406 		break;
1407 	default:
1408 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1409 		sc->rl_macrev = hwrev & 0x00700000;
1410 		hwrev &= RL_TXCFG_HWREV;
1411 		break;
1412 	}
1413 	device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev);
1414 	while (hw_rev->rl_desc != NULL) {
1415 		if (hw_rev->rl_rev == hwrev) {
1416 			sc->rl_type = hw_rev->rl_type;
1417 			sc->rl_hwrev = hw_rev;
1418 			break;
1419 		}
1420 		hw_rev++;
1421 	}
1422 	if (hw_rev->rl_desc == NULL) {
1423 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1424 		error = ENXIO;
1425 		goto fail;
1426 	}
1427 
1428 	switch (hw_rev->rl_rev) {
1429 	case RL_HWREV_8139CPLUS:
1430 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1431 		break;
1432 	case RL_HWREV_8100E:
1433 	case RL_HWREV_8101E:
1434 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1435 		break;
1436 	case RL_HWREV_8102E:
1437 	case RL_HWREV_8102EL:
1438 	case RL_HWREV_8102EL_SPIN1:
1439 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1440 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1441 		    RL_FLAG_AUTOPAD;
1442 		break;
1443 	case RL_HWREV_8103E:
1444 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1445 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1446 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
1447 		break;
1448 	case RL_HWREV_8401E:
1449 	case RL_HWREV_8105E:
1450 	case RL_HWREV_8105E_SPIN1:
1451 	case RL_HWREV_8106E:
1452 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1453 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1454 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1455 		break;
1456 	case RL_HWREV_8402:
1457 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1458 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1459 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1460 		    RL_FLAG_CMDSTOP_WAIT_TXQ;
1461 		break;
1462 	case RL_HWREV_8168B_SPIN1:
1463 	case RL_HWREV_8168B_SPIN2:
1464 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1465 		/* FALLTHROUGH */
1466 	case RL_HWREV_8168B_SPIN3:
1467 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1468 		break;
1469 	case RL_HWREV_8168C_SPIN2:
1470 		sc->rl_flags |= RL_FLAG_MACSLEEP;
1471 		/* FALLTHROUGH */
1472 	case RL_HWREV_8168C:
1473 		if (sc->rl_macrev == 0x00200000)
1474 			sc->rl_flags |= RL_FLAG_MACSLEEP;
1475 		/* FALLTHROUGH */
1476 	case RL_HWREV_8168CP:
1477 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1478 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1479 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1480 		break;
1481 	case RL_HWREV_8168D:
1482 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1483 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1484 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1485 		    RL_FLAG_WOL_MANLINK;
1486 		break;
1487 	case RL_HWREV_8168DP:
1488 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1489 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
1490 		    RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1491 		break;
1492 	case RL_HWREV_8168E:
1493 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1494 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1495 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1496 		    RL_FLAG_WOL_MANLINK;
1497 		break;
1498 	case RL_HWREV_8168E_VL:
1499 	case RL_HWREV_8168F:
1500 		sc->rl_flags |= RL_FLAG_EARLYOFF;
1501 		/* FALLTHROUGH */
1502 	case RL_HWREV_8411:
1503 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1504 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1505 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1506 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1507 		break;
1508 	case RL_HWREV_8168EP:
1509 	case RL_HWREV_8168FP:
1510 	case RL_HWREV_8168G:
1511 	case RL_HWREV_8411B:
1512 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1513 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1514 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1515 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK |
1516 		    RL_FLAG_8168G_PLUS;
1517 		break;
1518 	case RL_HWREV_8168GU:
1519 	case RL_HWREV_8168H:
1520 		if (pci_get_device(dev) == RT_DEVICEID_8101E) {
1521 			/* RTL8106E(US), RTL8107E */
1522 			sc->rl_flags |= RL_FLAG_FASTETHER;
1523 		} else
1524 			sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1525 
1526 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1527 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1528 		    RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ |
1529 		    RL_FLAG_8168G_PLUS;
1530 		break;
1531 	case RL_HWREV_8169_8110SB:
1532 	case RL_HWREV_8169_8110SBL:
1533 	case RL_HWREV_8169_8110SC:
1534 	case RL_HWREV_8169_8110SCE:
1535 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1536 		/* FALLTHROUGH */
1537 	case RL_HWREV_8169:
1538 	case RL_HWREV_8169S:
1539 	case RL_HWREV_8110S:
1540 		sc->rl_flags |= RL_FLAG_MACRESET;
1541 		break;
1542 	default:
1543 		break;
1544 	}
1545 
1546 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1547 		sc->rl_cfg0 = RL_8139_CFG0;
1548 		sc->rl_cfg1 = RL_8139_CFG1;
1549 		sc->rl_cfg2 = 0;
1550 		sc->rl_cfg3 = RL_8139_CFG3;
1551 		sc->rl_cfg4 = RL_8139_CFG4;
1552 		sc->rl_cfg5 = RL_8139_CFG5;
1553 	} else {
1554 		sc->rl_cfg0 = RL_CFG0;
1555 		sc->rl_cfg1 = RL_CFG1;
1556 		sc->rl_cfg2 = RL_CFG2;
1557 		sc->rl_cfg3 = RL_CFG3;
1558 		sc->rl_cfg4 = RL_CFG4;
1559 		sc->rl_cfg5 = RL_CFG5;
1560 	}
1561 
1562 	/* Reset the adapter. */
1563 	RL_LOCK(sc);
1564 	re_reset(sc);
1565 	RL_UNLOCK(sc);
1566 
1567 	/* Enable PME. */
1568 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1569 	cfg = CSR_READ_1(sc, sc->rl_cfg1);
1570 	cfg |= RL_CFG1_PME;
1571 	CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1572 	cfg = CSR_READ_1(sc, sc->rl_cfg5);
1573 	cfg &= RL_CFG5_PME_STS;
1574 	CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1575 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1576 
1577 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1578 		/*
1579 		 * XXX Should have a better way to extract station
1580 		 * address from EEPROM.
1581 		 */
1582 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1583 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1584 	} else {
1585 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1586 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1587 		if (re_did != 0x8129)
1588 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1589 
1590 		/*
1591 		 * Get station address from the EEPROM.
1592 		 */
1593 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1594 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1595 			as[i] = le16toh(as[i]);
1596 		bcopy(as, eaddr, ETHER_ADDR_LEN);
1597 	}
1598 
1599 	if (sc->rl_type == RL_8169) {
1600 		/* Set RX length mask and number of descriptors. */
1601 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1602 		sc->rl_txstart = RL_GTXSTART;
1603 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1604 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1605 	} else {
1606 		/* Set RX length mask and number of descriptors. */
1607 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1608 		sc->rl_txstart = RL_TXSTART;
1609 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1610 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1611 	}
1612 
1613 	error = re_allocmem(dev, sc);
1614 	if (error)
1615 		goto fail;
1616 	re_add_sysctls(sc);
1617 
1618 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1619 
1620 	/* Take controller out of deep sleep mode. */
1621 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
1622 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
1623 			CSR_WRITE_1(sc, RL_GPIO,
1624 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
1625 		else
1626 			CSR_WRITE_1(sc, RL_GPIO,
1627 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
1628 	}
1629 
1630 	/* Take PHY out of power down mode. */
1631 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1632 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1633 		if (hw_rev->rl_rev == RL_HWREV_8401E)
1634 			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
1635 	}
1636 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1637 		re_gmii_writereg(dev, 1, 0x1f, 0);
1638 		re_gmii_writereg(dev, 1, 0x0e, 0);
1639 	}
1640 
1641 	if_setsoftc(ifp, sc);
1642 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1643 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1644 	if_setioctlfn(ifp, re_ioctl);
1645 	if_setstartfn(ifp, re_start);
1646 	/*
1647 	 * RTL8168/8111C generates wrong IP checksummed frame if the
1648 	 * packet has IP options so disable TX checksum offloading.
1649 	 */
1650 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
1651 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 ||
1652 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168CP) {
1653 		if_sethwassist(ifp, 0);
1654 		if_setcapabilities(ifp, IFCAP_RXCSUM | IFCAP_TSO4);
1655 	} else {
1656 		if_sethwassist(ifp, CSUM_IP | CSUM_TCP | CSUM_UDP);
1657 		if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_TSO4);
1658 	}
1659 	if_sethwassistbits(ifp, CSUM_TSO, 0);
1660 	if_setcapenable(ifp, if_getcapabilities(ifp));
1661 	if_setinitfn(ifp, re_init);
1662 	if_setsendqlen(ifp, RL_IFQ_MAXLEN);
1663 	if_setsendqready(ifp);
1664 
1665 	NET_TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1666 
1667 #define	RE_PHYAD_INTERNAL	 0
1668 
1669 	/* Do MII setup. */
1670 	phy = RE_PHYAD_INTERNAL;
1671 	if (sc->rl_type == RL_8169)
1672 		phy = 1;
1673 	capmask = BMSR_DEFCAPMASK;
1674 	if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
1675 		 capmask &= ~BMSR_EXTSTAT;
1676 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
1677 	    re_ifmedia_sts, capmask, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1678 	if (error != 0) {
1679 		device_printf(dev, "attaching PHYs failed\n");
1680 		goto fail;
1681 	}
1682 
1683 	/* If address was not found, create one based on the hostid and name. */
1684 	if (ETHER_IS_ZERO(eaddr)) {
1685 		ether_gen_addr(ifp, (struct ether_addr *)eaddr);
1686 	}
1687 
1688 	/*
1689 	 * Call MI attach routine.
1690 	 */
1691 	ether_ifattach(ifp, eaddr);
1692 
1693 	/* VLAN capability setup */
1694 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING, 0);
1695 	if (if_getcapabilities(ifp) & IFCAP_HWCSUM)
1696 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
1697 	/* Enable WOL if PM is supported. */
1698 	if (pci_has_pm(sc->rl_dev))
1699 		if_setcapabilitiesbit(ifp, IFCAP_WOL, 0);
1700 	if_setcapenable(ifp, if_getcapabilities(ifp));
1701 	if_setcapenablebit(ifp, 0, (IFCAP_WOL_UCAST | IFCAP_WOL_MCAST));
1702 	/*
1703 	 * Don't enable TSO by default.  It is known to generate
1704 	 * corrupted TCP segments(bad TCP options) under certain
1705 	 * circumstances.
1706 	 */
1707 	if_sethwassistbits(ifp, 0, CSUM_TSO);
1708 	if_setcapenablebit(ifp, 0, (IFCAP_TSO4 | IFCAP_VLAN_HWTSO));
1709 #ifdef DEVICE_POLLING
1710 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
1711 #endif
1712 	/*
1713 	 * Tell the upper layer(s) we support long frames.
1714 	 * Must appear after the call to ether_ifattach() because
1715 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1716 	 */
1717 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
1718 
1719 #ifdef DEV_NETMAP
1720 	re_netmap_attach(sc);
1721 #endif /* DEV_NETMAP */
1722 
1723 #ifdef RE_DIAG
1724 	/*
1725 	 * Perform hardware diagnostic on the original RTL8169.
1726 	 * Some 32-bit cards were incorrectly wired and would
1727 	 * malfunction if plugged into a 64-bit slot.
1728 	 */
1729 	if (hwrev == RL_HWREV_8169) {
1730 		error = re_diag(sc);
1731 		if (error) {
1732 			device_printf(dev,
1733 		    	"attach aborted due to hardware diag failure\n");
1734 			ether_ifdetach(ifp);
1735 			goto fail;
1736 		}
1737 	}
1738 #endif
1739 
1740 #ifdef RE_TX_MODERATION
1741 	intr_filter = 1;
1742 #endif
1743 	/* Hook interrupt last to avoid having to lock softc */
1744 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1745 	    intr_filter == 0) {
1746 		error = bus_setup_intr(dev, sc->rl_irq[0],
1747 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1748 		    &sc->rl_intrhand[0]);
1749 	} else {
1750 		error = bus_setup_intr(dev, sc->rl_irq[0],
1751 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
1752 		    &sc->rl_intrhand[0]);
1753 	}
1754 	if (error) {
1755 		device_printf(dev, "couldn't set up irq\n");
1756 		ether_ifdetach(ifp);
1757 		goto fail;
1758 	}
1759 
1760 	DEBUGNET_SET(ifp, re);
1761 
1762 fail:
1763 	if (error)
1764 		re_detach(dev);
1765 
1766 	return (error);
1767 }
1768 
1769 /*
1770  * Shutdown hardware and free up resources. This can be called any
1771  * time after the mutex has been initialized. It is called in both
1772  * the error case in attach and the normal detach case so it needs
1773  * to be careful about only freeing resources that have actually been
1774  * allocated.
1775  */
1776 static int
re_detach(device_t dev)1777 re_detach(device_t dev)
1778 {
1779 	struct rl_softc		*sc;
1780 	if_t ifp;
1781 	int			i, rid;
1782 
1783 	sc = device_get_softc(dev);
1784 	ifp = sc->rl_ifp;
1785 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
1786 
1787 	/* These should only be active if attach succeeded */
1788 	if (device_is_attached(dev)) {
1789 #ifdef DEVICE_POLLING
1790 		if (if_getcapenable(ifp) & IFCAP_POLLING)
1791 			ether_poll_deregister(ifp);
1792 #endif
1793 		RL_LOCK(sc);
1794 #if 0
1795 		sc->suspended = 1;
1796 #endif
1797 		re_stop(sc);
1798 		RL_UNLOCK(sc);
1799 		callout_drain(&sc->rl_stat_callout);
1800 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1801 		/*
1802 		 * Force off the IFF_UP flag here, in case someone
1803 		 * still had a BPF descriptor attached to this
1804 		 * interface. If they do, ether_ifdetach() will cause
1805 		 * the BPF code to try and clear the promisc mode
1806 		 * flag, which will bubble down to re_ioctl(),
1807 		 * which will try to call re_init() again. This will
1808 		 * turn the NIC back on and restart the MII ticker,
1809 		 * which will panic the system when the kernel tries
1810 		 * to invoke the re_tick() function that isn't there
1811 		 * anymore.
1812 		 */
1813 		if_setflagbits(ifp, 0, IFF_UP);
1814 		ether_ifdetach(ifp);
1815 	}
1816 	bus_generic_detach(dev);
1817 
1818 	/*
1819 	 * The rest is resource deallocation, so we should already be
1820 	 * stopped here.
1821 	 */
1822 
1823 	if (sc->rl_intrhand[0] != NULL) {
1824 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1825 		sc->rl_intrhand[0] = NULL;
1826 	}
1827 	if (ifp != NULL) {
1828 #ifdef DEV_NETMAP
1829 		netmap_detach(ifp);
1830 #endif /* DEV_NETMAP */
1831 		if_free(ifp);
1832 	}
1833 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1834 		rid = 0;
1835 	else
1836 		rid = 1;
1837 	if (sc->rl_irq[0] != NULL) {
1838 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
1839 		sc->rl_irq[0] = NULL;
1840 	}
1841 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
1842 		pci_release_msi(dev);
1843 	if (sc->rl_res_pba) {
1844 		rid = PCIR_BAR(4);
1845 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
1846 	}
1847 	if (sc->rl_res)
1848 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1849 		    sc->rl_res);
1850 
1851 	/* Unload and free the RX DMA ring memory and map */
1852 
1853 	if (sc->rl_ldata.rl_rx_list_tag) {
1854 		if (sc->rl_ldata.rl_rx_list_addr)
1855 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1856 			    sc->rl_ldata.rl_rx_list_map);
1857 		if (sc->rl_ldata.rl_rx_list)
1858 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1859 			    sc->rl_ldata.rl_rx_list,
1860 			    sc->rl_ldata.rl_rx_list_map);
1861 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1862 	}
1863 
1864 	/* Unload and free the TX DMA ring memory and map */
1865 
1866 	if (sc->rl_ldata.rl_tx_list_tag) {
1867 		if (sc->rl_ldata.rl_tx_list_addr)
1868 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1869 			    sc->rl_ldata.rl_tx_list_map);
1870 		if (sc->rl_ldata.rl_tx_list)
1871 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1872 			    sc->rl_ldata.rl_tx_list,
1873 			    sc->rl_ldata.rl_tx_list_map);
1874 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1875 	}
1876 
1877 	/* Destroy all the RX and TX buffer maps */
1878 
1879 	if (sc->rl_ldata.rl_tx_mtag) {
1880 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1881 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1882 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1883 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1884 		}
1885 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1886 	}
1887 	if (sc->rl_ldata.rl_rx_mtag) {
1888 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1889 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1890 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1891 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1892 		}
1893 		if (sc->rl_ldata.rl_rx_sparemap)
1894 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1895 			    sc->rl_ldata.rl_rx_sparemap);
1896 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1897 	}
1898 	if (sc->rl_ldata.rl_jrx_mtag) {
1899 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1900 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
1901 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1902 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1903 		}
1904 		if (sc->rl_ldata.rl_jrx_sparemap)
1905 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1906 			    sc->rl_ldata.rl_jrx_sparemap);
1907 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
1908 	}
1909 	/* Unload and free the stats buffer and map */
1910 
1911 	if (sc->rl_ldata.rl_stag) {
1912 		if (sc->rl_ldata.rl_stats_addr)
1913 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1914 			    sc->rl_ldata.rl_smap);
1915 		if (sc->rl_ldata.rl_stats)
1916 			bus_dmamem_free(sc->rl_ldata.rl_stag,
1917 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1918 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1919 	}
1920 
1921 	if (sc->rl_parent_tag)
1922 		bus_dma_tag_destroy(sc->rl_parent_tag);
1923 
1924 	mtx_destroy(&sc->rl_mtx);
1925 
1926 	return (0);
1927 }
1928 
1929 static __inline void
re_discard_rxbuf(struct rl_softc * sc,int idx)1930 re_discard_rxbuf(struct rl_softc *sc, int idx)
1931 {
1932 	struct rl_desc		*desc;
1933 	struct rl_rxdesc	*rxd;
1934 	uint32_t		cmdstat;
1935 
1936 	if (if_getmtu(sc->rl_ifp) > RL_MTU &&
1937 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
1938 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
1939 	else
1940 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1941 	desc = &sc->rl_ldata.rl_rx_list[idx];
1942 	desc->rl_vlanctl = 0;
1943 	cmdstat = rxd->rx_size;
1944 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1945 		cmdstat |= RL_RDESC_CMD_EOR;
1946 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1947 }
1948 
1949 static int
re_newbuf(struct rl_softc * sc,int idx)1950 re_newbuf(struct rl_softc *sc, int idx)
1951 {
1952 	struct mbuf		*m;
1953 	struct rl_rxdesc	*rxd;
1954 	bus_dma_segment_t	segs[1];
1955 	bus_dmamap_t		map;
1956 	struct rl_desc		*desc;
1957 	uint32_t		cmdstat;
1958 	int			error, nsegs;
1959 
1960 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1961 	if (m == NULL)
1962 		return (ENOBUFS);
1963 
1964 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1965 #ifdef RE_FIXUP_RX
1966 	/*
1967 	 * This is part of an evil trick to deal with non-x86 platforms.
1968 	 * The Realtek chip requires RX buffers to be aligned on 64-bit
1969 	 * boundaries, but that will hose non-x86 machines. To get around
1970 	 * this, we leave some empty space at the start of each buffer
1971 	 * and for non-x86 hosts, we copy the buffer back six bytes
1972 	 * to achieve word alignment. This is slightly more efficient
1973 	 * than allocating a new buffer, copying the contents, and
1974 	 * discarding the old buffer.
1975 	 */
1976 	m_adj(m, RE_ETHER_ALIGN);
1977 #endif
1978 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1979 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1980 	if (error != 0) {
1981 		m_freem(m);
1982 		return (ENOBUFS);
1983 	}
1984 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1985 
1986 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1987 	if (rxd->rx_m != NULL) {
1988 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1989 		    BUS_DMASYNC_POSTREAD);
1990 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1991 	}
1992 
1993 	rxd->rx_m = m;
1994 	map = rxd->rx_dmamap;
1995 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1996 	rxd->rx_size = segs[0].ds_len;
1997 	sc->rl_ldata.rl_rx_sparemap = map;
1998 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1999 	    BUS_DMASYNC_PREREAD);
2000 
2001 	desc = &sc->rl_ldata.rl_rx_list[idx];
2002 	desc->rl_vlanctl = 0;
2003 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
2004 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
2005 	cmdstat = segs[0].ds_len;
2006 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
2007 		cmdstat |= RL_RDESC_CMD_EOR;
2008 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
2009 
2010 	return (0);
2011 }
2012 
2013 static int
re_jumbo_newbuf(struct rl_softc * sc,int idx)2014 re_jumbo_newbuf(struct rl_softc *sc, int idx)
2015 {
2016 	struct mbuf		*m;
2017 	struct rl_rxdesc	*rxd;
2018 	bus_dma_segment_t	segs[1];
2019 	bus_dmamap_t		map;
2020 	struct rl_desc		*desc;
2021 	uint32_t		cmdstat;
2022 	int			error, nsegs;
2023 
2024 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
2025 	if (m == NULL)
2026 		return (ENOBUFS);
2027 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
2028 #ifdef RE_FIXUP_RX
2029 	m_adj(m, RE_ETHER_ALIGN);
2030 #endif
2031 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
2032 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
2033 	if (error != 0) {
2034 		m_freem(m);
2035 		return (ENOBUFS);
2036 	}
2037 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
2038 
2039 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
2040 	if (rxd->rx_m != NULL) {
2041 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
2042 		    BUS_DMASYNC_POSTREAD);
2043 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
2044 	}
2045 
2046 	rxd->rx_m = m;
2047 	map = rxd->rx_dmamap;
2048 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
2049 	rxd->rx_size = segs[0].ds_len;
2050 	sc->rl_ldata.rl_jrx_sparemap = map;
2051 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
2052 	    BUS_DMASYNC_PREREAD);
2053 
2054 	desc = &sc->rl_ldata.rl_rx_list[idx];
2055 	desc->rl_vlanctl = 0;
2056 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
2057 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
2058 	cmdstat = segs[0].ds_len;
2059 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
2060 		cmdstat |= RL_RDESC_CMD_EOR;
2061 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
2062 
2063 	return (0);
2064 }
2065 
2066 #ifdef RE_FIXUP_RX
2067 static __inline void
re_fixup_rx(struct mbuf * m)2068 re_fixup_rx(struct mbuf *m)
2069 {
2070 	int                     i;
2071 	uint16_t                *src, *dst;
2072 
2073 	src = mtod(m, uint16_t *);
2074 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
2075 
2076 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2077 		*dst++ = *src++;
2078 
2079 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
2080 }
2081 #endif
2082 
2083 static int
re_tx_list_init(struct rl_softc * sc)2084 re_tx_list_init(struct rl_softc *sc)
2085 {
2086 	struct rl_desc		*desc;
2087 	int			i;
2088 
2089 	RL_LOCK_ASSERT(sc);
2090 
2091 	bzero(sc->rl_ldata.rl_tx_list,
2092 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2093 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2094 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2095 #ifdef DEV_NETMAP
2096 	re_netmap_tx_init(sc);
2097 #endif /* DEV_NETMAP */
2098 	/* Set EOR. */
2099 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2100 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2101 
2102 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2103 	    sc->rl_ldata.rl_tx_list_map,
2104 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2105 
2106 	sc->rl_ldata.rl_tx_prodidx = 0;
2107 	sc->rl_ldata.rl_tx_considx = 0;
2108 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2109 
2110 	return (0);
2111 }
2112 
2113 static int
re_rx_list_init(struct rl_softc * sc)2114 re_rx_list_init(struct rl_softc *sc)
2115 {
2116 	int			error, i;
2117 
2118 	bzero(sc->rl_ldata.rl_rx_list,
2119 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2120 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2121 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2122 		if ((error = re_newbuf(sc, i)) != 0)
2123 			return (error);
2124 	}
2125 #ifdef DEV_NETMAP
2126 	re_netmap_rx_init(sc);
2127 #endif /* DEV_NETMAP */
2128 
2129 	/* Flush the RX descriptors */
2130 
2131 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2132 	    sc->rl_ldata.rl_rx_list_map,
2133 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2134 
2135 	sc->rl_ldata.rl_rx_prodidx = 0;
2136 	sc->rl_head = sc->rl_tail = NULL;
2137 	sc->rl_int_rx_act = 0;
2138 
2139 	return (0);
2140 }
2141 
2142 static int
re_jrx_list_init(struct rl_softc * sc)2143 re_jrx_list_init(struct rl_softc *sc)
2144 {
2145 	int			error, i;
2146 
2147 	bzero(sc->rl_ldata.rl_rx_list,
2148 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2149 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2150 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
2151 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
2152 			return (error);
2153 	}
2154 
2155 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2156 	    sc->rl_ldata.rl_rx_list_map,
2157 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2158 
2159 	sc->rl_ldata.rl_rx_prodidx = 0;
2160 	sc->rl_head = sc->rl_tail = NULL;
2161 	sc->rl_int_rx_act = 0;
2162 
2163 	return (0);
2164 }
2165 
2166 /*
2167  * RX handler for C+ and 8169. For the gigE chips, we support
2168  * the reception of jumbo frames that have been fragmented
2169  * across multiple 2K mbuf cluster buffers.
2170  */
2171 static int
re_rxeof(struct rl_softc * sc,int * rx_npktsp)2172 re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2173 {
2174 	struct mbuf		*m;
2175 	if_t ifp;
2176 	int			i, rxerr, total_len;
2177 	struct rl_desc		*cur_rx;
2178 	u_int32_t		rxstat, rxvlan;
2179 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2180 
2181 	RL_LOCK_ASSERT(sc);
2182 
2183 	ifp = sc->rl_ifp;
2184 #ifdef DEV_NETMAP
2185 	if (netmap_rx_irq(ifp, 0, &rx_npkts))
2186 		return 0;
2187 #endif /* DEV_NETMAP */
2188 	if (if_getmtu(ifp) > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
2189 		jumbo = 1;
2190 	else
2191 		jumbo = 0;
2192 
2193 	/* Invalidate the descriptor memory */
2194 
2195 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2196 	    sc->rl_ldata.rl_rx_list_map,
2197 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2198 
2199 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2200 	    i = RL_RX_DESC_NXT(sc, i)) {
2201 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
2202 			break;
2203 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2204 		rxstat = le32toh(cur_rx->rl_cmdstat);
2205 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2206 			break;
2207 		total_len = rxstat & sc->rl_rxlenmask;
2208 		rxvlan = le32toh(cur_rx->rl_vlanctl);
2209 		if (jumbo != 0)
2210 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
2211 		else
2212 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2213 
2214 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
2215 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
2216 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
2217 			/*
2218 			 * RTL8168C or later controllers do not
2219 			 * support multi-fragment packet.
2220 			 */
2221 			re_discard_rxbuf(sc, i);
2222 			continue;
2223 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2224 			if (re_newbuf(sc, i) != 0) {
2225 				/*
2226 				 * If this is part of a multi-fragment packet,
2227 				 * discard all the pieces.
2228 				 */
2229 				if (sc->rl_head != NULL) {
2230 					m_freem(sc->rl_head);
2231 					sc->rl_head = sc->rl_tail = NULL;
2232 				}
2233 				re_discard_rxbuf(sc, i);
2234 				continue;
2235 			}
2236 			m->m_len = RE_RX_DESC_BUFLEN;
2237 			if (sc->rl_head == NULL)
2238 				sc->rl_head = sc->rl_tail = m;
2239 			else {
2240 				m->m_flags &= ~M_PKTHDR;
2241 				sc->rl_tail->m_next = m;
2242 				sc->rl_tail = m;
2243 			}
2244 			continue;
2245 		}
2246 
2247 		/*
2248 		 * NOTE: for the 8139C+, the frame length field
2249 		 * is always 12 bits in size, but for the gigE chips,
2250 		 * it is 13 bits (since the max RX frame length is 16K).
2251 		 * Unfortunately, all 32 bits in the status word
2252 		 * were already used, so to make room for the extra
2253 		 * length bit, Realtek took out the 'frame alignment
2254 		 * error' bit and shifted the other status bits
2255 		 * over one slot. The OWN, EOR, FS and LS bits are
2256 		 * still in the same places. We have already extracted
2257 		 * the frame length and checked the OWN bit, so rather
2258 		 * than using an alternate bit mapping, we shift the
2259 		 * status bits one space to the right so we can evaluate
2260 		 * them using the 8169 status as though it was in the
2261 		 * same format as that of the 8139C+.
2262 		 */
2263 		if (sc->rl_type == RL_8169)
2264 			rxstat >>= 1;
2265 
2266 		/*
2267 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
2268 		 * set, but if CRC is clear, it will still be a valid frame.
2269 		 */
2270 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
2271 			rxerr = 1;
2272 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
2273 			    total_len > 8191 &&
2274 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
2275 				rxerr = 0;
2276 			if (rxerr != 0) {
2277 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2278 				/*
2279 				 * If this is part of a multi-fragment packet,
2280 				 * discard all the pieces.
2281 				 */
2282 				if (sc->rl_head != NULL) {
2283 					m_freem(sc->rl_head);
2284 					sc->rl_head = sc->rl_tail = NULL;
2285 				}
2286 				re_discard_rxbuf(sc, i);
2287 				continue;
2288 			}
2289 		}
2290 
2291 		/*
2292 		 * If allocating a replacement mbuf fails,
2293 		 * reload the current one.
2294 		 */
2295 		if (jumbo != 0)
2296 			rxerr = re_jumbo_newbuf(sc, i);
2297 		else
2298 			rxerr = re_newbuf(sc, i);
2299 		if (rxerr != 0) {
2300 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2301 			if (sc->rl_head != NULL) {
2302 				m_freem(sc->rl_head);
2303 				sc->rl_head = sc->rl_tail = NULL;
2304 			}
2305 			re_discard_rxbuf(sc, i);
2306 			continue;
2307 		}
2308 
2309 		if (sc->rl_head != NULL) {
2310 			if (jumbo != 0)
2311 				m->m_len = total_len;
2312 			else {
2313 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
2314 				if (m->m_len == 0)
2315 					m->m_len = RE_RX_DESC_BUFLEN;
2316 			}
2317 			/*
2318 			 * Special case: if there's 4 bytes or less
2319 			 * in this buffer, the mbuf can be discarded:
2320 			 * the last 4 bytes is the CRC, which we don't
2321 			 * care about anyway.
2322 			 */
2323 			if (m->m_len <= ETHER_CRC_LEN) {
2324 				sc->rl_tail->m_len -=
2325 				    (ETHER_CRC_LEN - m->m_len);
2326 				m_freem(m);
2327 			} else {
2328 				m->m_len -= ETHER_CRC_LEN;
2329 				m->m_flags &= ~M_PKTHDR;
2330 				sc->rl_tail->m_next = m;
2331 			}
2332 			m = sc->rl_head;
2333 			sc->rl_head = sc->rl_tail = NULL;
2334 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2335 		} else
2336 			m->m_pkthdr.len = m->m_len =
2337 			    (total_len - ETHER_CRC_LEN);
2338 
2339 #ifdef RE_FIXUP_RX
2340 		re_fixup_rx(m);
2341 #endif
2342 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2343 		m->m_pkthdr.rcvif = ifp;
2344 
2345 		/* Do RX checksumming if enabled */
2346 
2347 		if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
2348 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2349 				/* Check IP header checksum */
2350 				if (rxstat & RL_RDESC_STAT_PROTOID)
2351 					m->m_pkthdr.csum_flags |=
2352 					    CSUM_IP_CHECKED;
2353 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2354 					m->m_pkthdr.csum_flags |=
2355 					    CSUM_IP_VALID;
2356 
2357 				/* Check TCP/UDP checksum */
2358 				if ((RL_TCPPKT(rxstat) &&
2359 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2360 				    (RL_UDPPKT(rxstat) &&
2361 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2362 					m->m_pkthdr.csum_flags |=
2363 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2364 					m->m_pkthdr.csum_data = 0xffff;
2365 				}
2366 			} else {
2367 				/*
2368 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2369 				 */
2370 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2371 				    (rxvlan & RL_RDESC_IPV4))
2372 					m->m_pkthdr.csum_flags |=
2373 					    CSUM_IP_CHECKED;
2374 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2375 				    (rxvlan & RL_RDESC_IPV4))
2376 					m->m_pkthdr.csum_flags |=
2377 					    CSUM_IP_VALID;
2378 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2379 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2380 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2381 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2382 					m->m_pkthdr.csum_flags |=
2383 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2384 					m->m_pkthdr.csum_data = 0xffff;
2385 				}
2386 			}
2387 		}
2388 		maxpkt--;
2389 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
2390 			m->m_pkthdr.ether_vtag =
2391 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
2392 			m->m_flags |= M_VLANTAG;
2393 		}
2394 		RL_UNLOCK(sc);
2395 		if_input(ifp, m);
2396 		RL_LOCK(sc);
2397 		rx_npkts++;
2398 	}
2399 
2400 	/* Flush the RX DMA ring */
2401 
2402 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2403 	    sc->rl_ldata.rl_rx_list_map,
2404 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2405 
2406 	sc->rl_ldata.rl_rx_prodidx = i;
2407 
2408 	if (rx_npktsp != NULL)
2409 		*rx_npktsp = rx_npkts;
2410 	if (maxpkt)
2411 		return (EAGAIN);
2412 
2413 	return (0);
2414 }
2415 
2416 static void
re_txeof(struct rl_softc * sc)2417 re_txeof(struct rl_softc *sc)
2418 {
2419 	if_t ifp;
2420 	struct rl_txdesc	*txd;
2421 	u_int32_t		txstat;
2422 	int			cons;
2423 
2424 	cons = sc->rl_ldata.rl_tx_considx;
2425 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2426 		return;
2427 
2428 	ifp = sc->rl_ifp;
2429 #ifdef DEV_NETMAP
2430 	if (netmap_tx_irq(ifp, 0))
2431 		return;
2432 #endif /* DEV_NETMAP */
2433 	/* Invalidate the TX descriptor list */
2434 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2435 	    sc->rl_ldata.rl_tx_list_map,
2436 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2437 
2438 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2439 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2440 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2441 		if (txstat & RL_TDESC_STAT_OWN)
2442 			break;
2443 		/*
2444 		 * We only stash mbufs in the last descriptor
2445 		 * in a fragment chain, which also happens to
2446 		 * be the only place where the TX status bits
2447 		 * are valid.
2448 		 */
2449 		if (txstat & RL_TDESC_CMD_EOF) {
2450 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2451 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2452 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2453 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2454 			    txd->tx_dmamap);
2455 			KASSERT(txd->tx_m != NULL,
2456 			    ("%s: freeing NULL mbufs!", __func__));
2457 			m_freem(txd->tx_m);
2458 			txd->tx_m = NULL;
2459 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2460 			    RL_TDESC_STAT_COLCNT))
2461 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
2462 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2463 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2464 			else
2465 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2466 		}
2467 		sc->rl_ldata.rl_tx_free++;
2468 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
2469 	}
2470 	sc->rl_ldata.rl_tx_considx = cons;
2471 
2472 	/* No changes made to the TX ring, so no flush needed */
2473 
2474 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2475 		/*
2476 		 * Some descriptors are still owned by the controller.  On PCIe
2477 		 * parts a TxPoll request can be lost when Tx packets are queued
2478 		 * too close together, leaving a non-empty ring with no transfer
2479 		 * in progress.  Re-arm the transmitter here -- this routine is
2480 		 * reached from the interrupt handlers, re_tick() and
2481 		 * re_watchdog() -- so a lost poll cannot stall the ring until
2482 		 * the watchdog fires.
2483 		 */
2484 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0)
2485 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2486 #ifdef RE_TX_MODERATION
2487 		/*
2488 		 * If not all descriptors have been reaped yet, reload
2489 		 * the timer so that we will eventually get another
2490 		 * interrupt that will cause us to re-enter this routine.
2491 		 * This is done in case the transmitter has gone idle.
2492 		 */
2493 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2494 #endif
2495 	} else
2496 		sc->rl_watchdog_timer = 0;
2497 }
2498 
2499 static void
re_tick(void * xsc)2500 re_tick(void *xsc)
2501 {
2502 	struct rl_softc		*sc;
2503 	struct mii_data		*mii;
2504 
2505 	sc = xsc;
2506 
2507 	RL_LOCK_ASSERT(sc);
2508 
2509 	mii = device_get_softc(sc->rl_miibus);
2510 	mii_tick(mii);
2511 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
2512 		re_miibus_statchg(sc->rl_dev);
2513 	/*
2514 	 * Reclaim transmitted frames here. Technically it is not
2515 	 * necessary to do here but it ensures periodic reclamation
2516 	 * regardless of Tx completion interrupt which seems to be
2517 	 * lost on PCIe based controllers under certain situations.
2518 	 */
2519 	re_txeof(sc);
2520 	re_watchdog(sc);
2521 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2522 }
2523 
2524 #ifdef DEVICE_POLLING
2525 static int
re_poll(if_t ifp,enum poll_cmd cmd,int count)2526 re_poll(if_t ifp, enum poll_cmd cmd, int count)
2527 {
2528 	struct rl_softc *sc = if_getsoftc(ifp);
2529 	int rx_npkts = 0;
2530 
2531 	RL_LOCK(sc);
2532 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2533 		rx_npkts = re_poll_locked(ifp, cmd, count);
2534 	RL_UNLOCK(sc);
2535 	return (rx_npkts);
2536 }
2537 
2538 static int
re_poll_locked(if_t ifp,enum poll_cmd cmd,int count)2539 re_poll_locked(if_t ifp, enum poll_cmd cmd, int count)
2540 {
2541 	struct rl_softc *sc = if_getsoftc(ifp);
2542 	int rx_npkts;
2543 
2544 	RL_LOCK_ASSERT(sc);
2545 
2546 	sc->rxcycles = count;
2547 	re_rxeof(sc, &rx_npkts);
2548 	re_txeof(sc);
2549 
2550 	if (!if_sendq_empty(ifp))
2551 		re_start_locked(ifp);
2552 
2553 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2554 		u_int16_t       status;
2555 
2556 		status = CSR_READ_2(sc, RL_ISR);
2557 		if (status == 0xffff)
2558 			return (rx_npkts);
2559 		if (status)
2560 			CSR_WRITE_2(sc, RL_ISR, status);
2561 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2562 		    (sc->rl_flags & RL_FLAG_PCIE))
2563 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2564 
2565 		/*
2566 		 * XXX check behaviour on receiver stalls.
2567 		 */
2568 
2569 		if (status & RL_ISR_SYSTEM_ERR) {
2570 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2571 			re_init_locked(sc);
2572 		}
2573 	}
2574 	return (rx_npkts);
2575 }
2576 #endif /* DEVICE_POLLING */
2577 
2578 static int
re_intr(void * arg)2579 re_intr(void *arg)
2580 {
2581 	struct rl_softc		*sc;
2582 	uint16_t		status;
2583 
2584 	sc = arg;
2585 
2586 	status = CSR_READ_2(sc, RL_ISR);
2587 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2588                 return (FILTER_STRAY);
2589 	CSR_WRITE_2(sc, RL_IMR, 0);
2590 
2591 	taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask);
2592 
2593 	return (FILTER_HANDLED);
2594 }
2595 
2596 static void
re_int_task(void * arg,int npending)2597 re_int_task(void *arg, int npending)
2598 {
2599 	struct rl_softc		*sc;
2600 	if_t ifp;
2601 	u_int16_t		status;
2602 	int			rval = 0;
2603 
2604 	sc = arg;
2605 	ifp = sc->rl_ifp;
2606 
2607 	RL_LOCK(sc);
2608 
2609 	status = CSR_READ_2(sc, RL_ISR);
2610         CSR_WRITE_2(sc, RL_ISR, status);
2611 
2612 	if (sc->suspended ||
2613 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2614 		RL_UNLOCK(sc);
2615 		return;
2616 	}
2617 
2618 #ifdef DEVICE_POLLING
2619 	if  (if_getcapenable(ifp) & IFCAP_POLLING) {
2620 		RL_UNLOCK(sc);
2621 		return;
2622 	}
2623 #endif
2624 
2625 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2626 		rval = re_rxeof(sc, NULL);
2627 
2628 	/*
2629 	 * Some chips will ignore a second TX request issued
2630 	 * while an existing transmission is in progress. If
2631 	 * the transmitter goes idle but there are still
2632 	 * packets waiting to be sent, we need to restart the
2633 	 * channel here to flush them out. This only seems to
2634 	 * be required with the PCIe devices.
2635 	 */
2636 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2637 	    (sc->rl_flags & RL_FLAG_PCIE))
2638 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2639 	if (status & (
2640 #ifdef RE_TX_MODERATION
2641 	    RL_ISR_TIMEOUT_EXPIRED|
2642 #else
2643 	    RL_ISR_TX_OK|
2644 #endif
2645 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2646 		re_txeof(sc);
2647 
2648 	if (status & RL_ISR_SYSTEM_ERR) {
2649 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2650 		re_init_locked(sc);
2651 	}
2652 
2653 	if (!if_sendq_empty(ifp))
2654 		re_start_locked(ifp);
2655 
2656 	RL_UNLOCK(sc);
2657 
2658         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2659 		taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask);
2660 		return;
2661 	}
2662 
2663 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2664 }
2665 
2666 static void
re_intr_msi(void * xsc)2667 re_intr_msi(void *xsc)
2668 {
2669 	struct rl_softc		*sc;
2670 	if_t ifp;
2671 	uint16_t		intrs, status;
2672 
2673 	sc = xsc;
2674 	RL_LOCK(sc);
2675 
2676 	ifp = sc->rl_ifp;
2677 #ifdef DEVICE_POLLING
2678 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
2679 		RL_UNLOCK(sc);
2680 		return;
2681 	}
2682 #endif
2683 	/* Disable interrupts. */
2684 	CSR_WRITE_2(sc, RL_IMR, 0);
2685 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2686 		RL_UNLOCK(sc);
2687 		return;
2688 	}
2689 
2690 	intrs = RL_INTRS_CPLUS;
2691 	status = CSR_READ_2(sc, RL_ISR);
2692         CSR_WRITE_2(sc, RL_ISR, status);
2693 	if (sc->rl_int_rx_act > 0) {
2694 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2695 		    RL_ISR_RX_OVERRUN);
2696 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2697 		    RL_ISR_RX_OVERRUN);
2698 	}
2699 
2700 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2701 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2702 		re_rxeof(sc, NULL);
2703 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2704 			if (sc->rl_int_rx_mod != 0 &&
2705 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2706 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2707 				/* Rearm one-shot timer. */
2708 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2709 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2710 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2711 				sc->rl_int_rx_act = 1;
2712 			} else {
2713 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2714 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2715 				sc->rl_int_rx_act = 0;
2716 			}
2717 		}
2718 	}
2719 
2720 	/*
2721 	 * Some chips will ignore a second TX request issued
2722 	 * while an existing transmission is in progress. If
2723 	 * the transmitter goes idle but there are still
2724 	 * packets waiting to be sent, we need to restart the
2725 	 * channel here to flush them out. This only seems to
2726 	 * be required with the PCIe devices.
2727 	 */
2728 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2729 	    (sc->rl_flags & RL_FLAG_PCIE))
2730 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2731 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2732 		re_txeof(sc);
2733 
2734 	if (status & RL_ISR_SYSTEM_ERR) {
2735 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2736 		re_init_locked(sc);
2737 	}
2738 
2739 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2740 		if (!if_sendq_empty(ifp))
2741 			re_start_locked(ifp);
2742 		/*
2743 		 * A Tx completion can set a Tx status bit between the ISR ack
2744 		 * near the top of this routine and re-enabling interrupts
2745 		 * below.  These controllers do not re-assert the MSI for a
2746 		 * status bit that is already set, so the completion would not
2747 		 * be serviced until the next interrupt or the 1 Hz re_tick()
2748 		 * reclaim (this is why hw.re.msi_disable is a known
2749 		 * workaround).  If a Tx bit is pending, clear just that bit and
2750 		 * reap the ring now; leave any Rx bits set so they re-arm the
2751 		 * interrupt normally and the Rx moderation state is untouched.
2752 		 */
2753 		status = CSR_READ_2(sc, RL_ISR);
2754 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_ERR |
2755 		    RL_ISR_TX_DESC_UNAVAIL)) != 0) {
2756 			CSR_WRITE_2(sc, RL_ISR, status & (RL_ISR_TX_OK |
2757 			    RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL));
2758 			if ((status & (RL_ISR_TX_OK |
2759 			    RL_ISR_TX_DESC_UNAVAIL)) != 0 &&
2760 			    (sc->rl_flags & RL_FLAG_PCIE) != 0)
2761 				CSR_WRITE_1(sc, sc->rl_txstart,
2762 				    RL_TXSTART_START);
2763 			re_txeof(sc);
2764 			if (!if_sendq_empty(ifp))
2765 				re_start_locked(ifp);
2766 		}
2767 		CSR_WRITE_2(sc, RL_IMR, intrs);
2768 		/* Flush the posted IMR write. */
2769 		(void)CSR_READ_2(sc, RL_ISR);
2770 	}
2771 	RL_UNLOCK(sc);
2772 }
2773 
2774 static int
re_encap(struct rl_softc * sc,struct mbuf ** m_head)2775 re_encap(struct rl_softc *sc, struct mbuf **m_head)
2776 {
2777 	struct rl_txdesc	*txd, *txd_last;
2778 	bus_dma_segment_t	segs[RL_NTXSEGS];
2779 	bus_dmamap_t		map;
2780 	struct mbuf		*m_new;
2781 	struct rl_desc		*desc;
2782 	int			nsegs, prod;
2783 	int			i, error, ei, si;
2784 	int			padlen;
2785 	uint32_t		cmdstat, csum_flags, vlanctl;
2786 
2787 	RL_LOCK_ASSERT(sc);
2788 	M_ASSERTPKTHDR((*m_head));
2789 
2790 	/*
2791 	 * With some of the Realtek chips, using the checksum offload
2792 	 * support in conjunction with the autopadding feature results
2793 	 * in the transmission of corrupt frames. For example, if we
2794 	 * need to send a really small IP fragment that's less than 60
2795 	 * bytes in size, and IP header checksumming is enabled, the
2796 	 * resulting ethernet frame that appears on the wire will
2797 	 * have garbled payload. To work around this, if TX IP checksum
2798 	 * offload is enabled, we always manually pad short frames out
2799 	 * to the minimum ethernet frame size.
2800 	 */
2801 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2802 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
2803 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2804 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2805 		if (M_WRITABLE(*m_head) == 0) {
2806 			/* Get a writable copy. */
2807 			m_new = m_dup(*m_head, M_NOWAIT);
2808 			m_freem(*m_head);
2809 			if (m_new == NULL) {
2810 				*m_head = NULL;
2811 				return (ENOBUFS);
2812 			}
2813 			*m_head = m_new;
2814 		}
2815 		if ((*m_head)->m_next != NULL ||
2816 		    M_TRAILINGSPACE(*m_head) < padlen) {
2817 			m_new = m_defrag(*m_head, M_NOWAIT);
2818 			if (m_new == NULL) {
2819 				m_freem(*m_head);
2820 				*m_head = NULL;
2821 				return (ENOBUFS);
2822 			}
2823 		} else
2824 			m_new = *m_head;
2825 
2826 		/*
2827 		 * Manually pad short frames, and zero the pad space
2828 		 * to avoid leaking data.
2829 		 */
2830 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2831 		m_new->m_pkthdr.len += padlen;
2832 		m_new->m_len = m_new->m_pkthdr.len;
2833 		*m_head = m_new;
2834 	}
2835 
2836 	prod = sc->rl_ldata.rl_tx_prodidx;
2837 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2838 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2839 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2840 	if (error == EFBIG) {
2841 		m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2842 		if (m_new == NULL) {
2843 			m_freem(*m_head);
2844 			*m_head = NULL;
2845 			return (ENOBUFS);
2846 		}
2847 		*m_head = m_new;
2848 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2849 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2850 		if (error != 0) {
2851 			m_freem(*m_head);
2852 			*m_head = NULL;
2853 			return (error);
2854 		}
2855 	} else if (error != 0)
2856 		return (error);
2857 	if (nsegs == 0) {
2858 		m_freem(*m_head);
2859 		*m_head = NULL;
2860 		return (EIO);
2861 	}
2862 
2863 	/* Check for number of available descriptors. */
2864 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2865 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2866 		return (ENOBUFS);
2867 	}
2868 
2869 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2870 	    BUS_DMASYNC_PREWRITE);
2871 
2872 	/*
2873 	 * Set up checksum offload. Note: checksum offload bits must
2874 	 * appear in all descriptors of a multi-descriptor transmit
2875 	 * attempt. This is according to testing done with an 8169
2876 	 * chip. This is a requirement.
2877 	 */
2878 	vlanctl = 0;
2879 	csum_flags = 0;
2880 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2881 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2882 			csum_flags |= RL_TDESC_CMD_LGSEND;
2883 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2884 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2885 		} else {
2886 			csum_flags |= RL_TDESC_CMD_LGSEND |
2887 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2888 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2889 		}
2890 	} else {
2891 		/*
2892 		 * Unconditionally enable IP checksum if TCP or UDP
2893 		 * checksum is required. Otherwise, TCP/UDP checksum
2894 		 * doesn't make effects.
2895 		 */
2896 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2897 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2898 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2899 				if (((*m_head)->m_pkthdr.csum_flags &
2900 				    CSUM_TCP) != 0)
2901 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2902 				if (((*m_head)->m_pkthdr.csum_flags &
2903 				    CSUM_UDP) != 0)
2904 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2905 			} else {
2906 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2907 				if (((*m_head)->m_pkthdr.csum_flags &
2908 				    CSUM_TCP) != 0)
2909 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2910 				if (((*m_head)->m_pkthdr.csum_flags &
2911 				    CSUM_UDP) != 0)
2912 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2913 			}
2914 		}
2915 	}
2916 
2917 	/*
2918 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2919 	 * appear in all descriptors of a multi-descriptor
2920 	 * transmission attempt.
2921 	 */
2922 	if ((*m_head)->m_flags & M_VLANTAG)
2923 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2924 		    RL_TDESC_VLANCTL_TAG;
2925 
2926 	si = prod;
2927 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2928 		desc = &sc->rl_ldata.rl_tx_list[prod];
2929 		desc->rl_vlanctl = htole32(vlanctl);
2930 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2931 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2932 		cmdstat = segs[i].ds_len;
2933 		if (i != 0)
2934 			cmdstat |= RL_TDESC_CMD_OWN;
2935 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2936 			cmdstat |= RL_TDESC_CMD_EOR;
2937 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2938 		sc->rl_ldata.rl_tx_free--;
2939 	}
2940 	/* Update producer index. */
2941 	sc->rl_ldata.rl_tx_prodidx = prod;
2942 
2943 	/* Set EOF on the last descriptor. */
2944 	ei = RL_TX_DESC_PRV(sc, prod);
2945 	desc = &sc->rl_ldata.rl_tx_list[ei];
2946 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2947 
2948 	desc = &sc->rl_ldata.rl_tx_list[si];
2949 	/* Set SOF and transfer ownership of packet to the chip. */
2950 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2951 
2952 	/*
2953 	 * Insure that the map for this transmission
2954 	 * is placed at the array index of the last descriptor
2955 	 * in this chain.  (Swap last and first dmamaps.)
2956 	 */
2957 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2958 	map = txd->tx_dmamap;
2959 	txd->tx_dmamap = txd_last->tx_dmamap;
2960 	txd_last->tx_dmamap = map;
2961 	txd_last->tx_m = *m_head;
2962 
2963 	return (0);
2964 }
2965 
2966 static void
re_start(if_t ifp)2967 re_start(if_t ifp)
2968 {
2969 	struct rl_softc		*sc;
2970 
2971 	sc = if_getsoftc(ifp);
2972 	RL_LOCK(sc);
2973 	re_start_locked(ifp);
2974 	RL_UNLOCK(sc);
2975 }
2976 
2977 /*
2978  * Main transmit routine for C+ and gigE NICs.
2979  */
2980 static void
re_start_locked(if_t ifp)2981 re_start_locked(if_t ifp)
2982 {
2983 	struct rl_softc		*sc;
2984 	struct mbuf		*m_head;
2985 	int			queued;
2986 
2987 	sc = if_getsoftc(ifp);
2988 
2989 #ifdef DEV_NETMAP
2990 	/* XXX is this necessary ? */
2991 	if (if_getcapenable(ifp) & IFCAP_NETMAP) {
2992 		struct netmap_kring *kring = NA(ifp)->tx_rings[0];
2993 		if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2994 			/* kick the tx unit */
2995 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2996 #ifdef RE_TX_MODERATION
2997 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2998 #endif
2999 			sc->rl_watchdog_timer = 5;
3000 		}
3001 		return;
3002 	}
3003 #endif /* DEV_NETMAP */
3004 
3005 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
3006 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
3007 		return;
3008 
3009 	for (queued = 0; !if_sendq_empty(ifp) &&
3010 	    sc->rl_ldata.rl_tx_free > 1;) {
3011 		m_head = if_dequeue(ifp);
3012 		if (m_head == NULL)
3013 			break;
3014 
3015 		if (re_encap(sc, &m_head) != 0) {
3016 			if (m_head == NULL)
3017 				break;
3018 			if_sendq_prepend(ifp, m_head);
3019 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
3020 			break;
3021 		}
3022 
3023 		/*
3024 		 * If there's a BPF listener, bounce a copy of this frame
3025 		 * to him.
3026 		 */
3027 		ETHER_BPF_MTAP(ifp, m_head);
3028 
3029 		queued++;
3030 	}
3031 
3032 	if (queued == 0) {
3033 #ifdef RE_TX_MODERATION
3034 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
3035 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
3036 #endif
3037 		return;
3038 	}
3039 
3040 	re_start_tx(sc);
3041 }
3042 
3043 static void
re_start_tx(struct rl_softc * sc)3044 re_start_tx(struct rl_softc *sc)
3045 {
3046 
3047 	/* Flush the TX descriptors */
3048 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
3049 	    sc->rl_ldata.rl_tx_list_map,
3050 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
3051 
3052 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
3053 
3054 #ifdef RE_TX_MODERATION
3055 	/*
3056 	 * Use the countdown timer for interrupt moderation.
3057 	 * 'TX done' interrupts are disabled. Instead, we reset the
3058 	 * countdown timer, which will begin counting until it hits
3059 	 * the value in the TIMERINT register, and then trigger an
3060 	 * interrupt. Each time we write to the TIMERCNT register,
3061 	 * the timer count is reset to 0.
3062 	 */
3063 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
3064 #endif
3065 
3066 	/*
3067 	 * Set a timeout in case the chip goes out to lunch.
3068 	 */
3069 	sc->rl_watchdog_timer = 5;
3070 }
3071 
3072 static void
re_set_jumbo(struct rl_softc * sc,int jumbo)3073 re_set_jumbo(struct rl_softc *sc, int jumbo)
3074 {
3075 
3076 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
3077 		pci_set_max_read_req(sc->rl_dev, 4096);
3078 		return;
3079 	}
3080 
3081 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3082 	if (jumbo != 0) {
3083 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
3084 		    RL_CFG3_JUMBO_EN0);
3085 		switch (sc->rl_hwrev->rl_rev) {
3086 		case RL_HWREV_8168DP:
3087 			break;
3088 		case RL_HWREV_8168E:
3089 			CSR_WRITE_1(sc, sc->rl_cfg4,
3090 			    CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
3091 			break;
3092 		default:
3093 			CSR_WRITE_1(sc, sc->rl_cfg4,
3094 			    CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
3095 		}
3096 	} else {
3097 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
3098 		    ~RL_CFG3_JUMBO_EN0);
3099 		switch (sc->rl_hwrev->rl_rev) {
3100 		case RL_HWREV_8168DP:
3101 			break;
3102 		case RL_HWREV_8168E:
3103 			CSR_WRITE_1(sc, sc->rl_cfg4,
3104 			    CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
3105 			break;
3106 		default:
3107 			CSR_WRITE_1(sc, sc->rl_cfg4,
3108 			    CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
3109 		}
3110 	}
3111 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3112 
3113 	switch (sc->rl_hwrev->rl_rev) {
3114 	case RL_HWREV_8168DP:
3115 		pci_set_max_read_req(sc->rl_dev, 4096);
3116 		break;
3117 	default:
3118 		if (jumbo != 0)
3119 			pci_set_max_read_req(sc->rl_dev, 512);
3120 		else
3121 			pci_set_max_read_req(sc->rl_dev, 4096);
3122 	}
3123 }
3124 
3125 static void
re_init(void * xsc)3126 re_init(void *xsc)
3127 {
3128 	struct rl_softc		*sc = xsc;
3129 
3130 	RL_LOCK(sc);
3131 	re_init_locked(sc);
3132 	RL_UNLOCK(sc);
3133 }
3134 
3135 static void
re_init_locked(struct rl_softc * sc)3136 re_init_locked(struct rl_softc *sc)
3137 {
3138 	if_t ifp = sc->rl_ifp;
3139 	struct mii_data		*mii;
3140 	uint32_t		reg;
3141 	uint16_t		cfg;
3142 	uint32_t		idr[2];
3143 
3144 	RL_LOCK_ASSERT(sc);
3145 
3146 	mii = device_get_softc(sc->rl_miibus);
3147 
3148 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3149 		return;
3150 
3151 	/*
3152 	 * Cancel pending I/O and free all RX/TX buffers.
3153 	 */
3154 	re_stop(sc);
3155 
3156 	/* Put controller into known state. */
3157 	re_reset(sc);
3158 
3159 	/*
3160 	 * For C+ mode, initialize the RX descriptors and mbufs.
3161 	 */
3162 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3163 		if (if_getmtu(ifp) > RL_MTU) {
3164 			if (re_jrx_list_init(sc) != 0) {
3165 				device_printf(sc->rl_dev,
3166 				    "no memory for jumbo RX buffers\n");
3167 				re_stop(sc);
3168 				return;
3169 			}
3170 			/* Disable checksum offloading for jumbo frames. */
3171 			if_setcapenablebit(ifp, 0, (IFCAP_HWCSUM | IFCAP_TSO4));
3172 			if_sethwassistbits(ifp, 0, (RE_CSUM_FEATURES | CSUM_TSO));
3173 		} else {
3174 			if (re_rx_list_init(sc) != 0) {
3175 				device_printf(sc->rl_dev,
3176 				    "no memory for RX buffers\n");
3177 				re_stop(sc);
3178 				return;
3179 			}
3180 		}
3181 		re_set_jumbo(sc, if_getmtu(ifp) > RL_MTU);
3182 	} else {
3183 		if (re_rx_list_init(sc) != 0) {
3184 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
3185 			re_stop(sc);
3186 			return;
3187 		}
3188 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3189 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
3190 			if (if_getmtu(ifp) > RL_MTU)
3191 				pci_set_max_read_req(sc->rl_dev, 512);
3192 			else
3193 				pci_set_max_read_req(sc->rl_dev, 4096);
3194 		}
3195 	}
3196 	re_tx_list_init(sc);
3197 
3198 	/*
3199 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3200 	 * RX checksum offload. We must configure the C+ register
3201 	 * before all others.
3202 	 */
3203 	cfg = RL_CPLUSCMD_PCI_MRW;
3204 	if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
3205 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
3206 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
3207 		cfg |= RL_CPLUSCMD_VLANSTRIP;
3208 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3209 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3210 		/* XXX magic. */
3211 		cfg |= 0x0001;
3212 	} else
3213 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3214 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
3215 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
3216 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3217 		reg = 0x000fff00;
3218 		if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3219 			reg |= 0x000000ff;
3220 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3221 			reg |= 0x00f00000;
3222 		CSR_WRITE_4(sc, 0x7c, reg);
3223 		/* Disable interrupt mitigation. */
3224 		CSR_WRITE_2(sc, 0xe2, 0);
3225 	}
3226 	/*
3227 	 * Disable TSO if interface MTU size is greater than MSS
3228 	 * allowed in controller.
3229 	 */
3230 	if (if_getmtu(ifp) > RL_TSO_MTU && (if_getcapenable(ifp) & IFCAP_TSO4) != 0) {
3231 		if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3232 		if_sethwassistbits(ifp, 0, CSUM_TSO);
3233 	}
3234 
3235 	/*
3236 	 * Init our MAC address.  Even though the chipset
3237 	 * documentation doesn't mention it, we need to enter "Config
3238 	 * register write enable" mode to modify the ID registers.
3239 	 */
3240 	/* Copy MAC address on stack to align. */
3241 	bzero(idr, sizeof(idr));
3242 	bcopy(if_getlladdr(ifp), idr, ETHER_ADDR_LEN);
3243 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3244 	CSR_WRITE_4(sc, RL_IDR0, htole32(idr[0]));
3245 	CSR_WRITE_4(sc, RL_IDR4, htole32(idr[1]));
3246 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3247 
3248 	/*
3249 	 * Load the addresses of the RX and TX lists into the chip.
3250 	 */
3251 
3252 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3253 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3254 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3255 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3256 
3257 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3258 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3259 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3260 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3261 
3262 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3263 		/* Disable RXDV gate. */
3264 		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
3265 		    ~0x00080000);
3266 	}
3267 
3268 	/*
3269 	 * Enable transmit and receive for pre-RTL8168G controllers.
3270 	 * RX/TX MACs should be enabled before RX/TX configuration.
3271 	 */
3272 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) == 0)
3273 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
3274 
3275 	/*
3276 	 * Set the initial TX configuration.
3277 	 */
3278 	if (sc->rl_testmode) {
3279 		if (sc->rl_type == RL_8169)
3280 			CSR_WRITE_4(sc, RL_TXCFG,
3281 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3282 		else
3283 			CSR_WRITE_4(sc, RL_TXCFG,
3284 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3285 	} else
3286 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3287 
3288 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3289 
3290 	/*
3291 	 * Set the initial RX configuration.
3292 	 */
3293 	re_set_rxmode(sc);
3294 
3295 	/* Configure interrupt moderation. */
3296 	if (sc->rl_type == RL_8169) {
3297 		/* Magic from vendor. */
3298 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3299 	}
3300 
3301 	/*
3302 	 * Enable transmit and receive for RTL8168G and later controllers.
3303 	 * RX/TX MACs should be enabled after RX/TX configuration.
3304 	 */
3305 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
3306 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
3307 
3308 #ifdef DEVICE_POLLING
3309 	/*
3310 	 * Disable interrupts if we are polling.
3311 	 */
3312 	if (if_getcapenable(ifp) & IFCAP_POLLING)
3313 		CSR_WRITE_2(sc, RL_IMR, 0);
3314 	else	/* otherwise ... */
3315 #endif
3316 
3317 	/*
3318 	 * Enable interrupts.
3319 	 */
3320 	if (sc->rl_testmode)
3321 		CSR_WRITE_2(sc, RL_IMR, 0);
3322 	else
3323 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3324 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3325 
3326 	/* Set initial TX threshold */
3327 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3328 
3329 	/* Start RX/TX process. */
3330 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3331 
3332 	/*
3333 	 * Initialize the timer interrupt register so that
3334 	 * a timer interrupt will be generated once the timer
3335 	 * reaches a certain number of ticks. The timer is
3336 	 * reloaded on each transmit.
3337 	 */
3338 #ifdef RE_TX_MODERATION
3339 	/*
3340 	 * Use timer interrupt register to moderate TX interrupt
3341 	 * moderation, which dramatically improves TX frame rate.
3342 	 */
3343 	if (sc->rl_type == RL_8169)
3344 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3345 	else
3346 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3347 #else
3348 	/*
3349 	 * Use timer interrupt register to moderate RX interrupt
3350 	 * moderation.
3351 	 */
3352 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3353 	    intr_filter == 0) {
3354 		if (sc->rl_type == RL_8169)
3355 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3356 			    RL_USECS(sc->rl_int_rx_mod));
3357 	} else {
3358 		if (sc->rl_type == RL_8169)
3359 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3360 	}
3361 #endif
3362 
3363 	/*
3364 	 * For 8169 gigE NICs, set the max allowed RX packet
3365 	 * size so we can receive jumbo frames.
3366 	 */
3367 	if (sc->rl_type == RL_8169) {
3368 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3369 			/*
3370 			 * For controllers that use new jumbo frame scheme,
3371 			 * set maximum size of jumbo frame depending on
3372 			 * controller revisions.
3373 			 */
3374 			if (if_getmtu(ifp) > RL_MTU)
3375 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3376 				    sc->rl_hwrev->rl_max_mtu +
3377 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
3378 				    ETHER_CRC_LEN);
3379 			else
3380 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3381 				    RE_RX_DESC_BUFLEN);
3382 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3383 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
3384 			/* RTL810x has no jumbo frame support. */
3385 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
3386 		} else
3387 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
3388 	}
3389 
3390 	if (sc->rl_testmode)
3391 		return;
3392 
3393 	CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3394 	    RL_CFG1_DRVLOAD);
3395 
3396 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
3397 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3398 
3399 	sc->rl_flags &= ~RL_FLAG_LINK;
3400 	mii_mediachg(mii);
3401 
3402 	sc->rl_watchdog_timer = 0;
3403 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3404 
3405 #ifdef DEV_NETMAP
3406 	netmap_enable_all_rings(ifp);
3407 #endif /* DEV_NETMAP */
3408 }
3409 
3410 /*
3411  * Set media options.
3412  */
3413 static int
re_ifmedia_upd(if_t ifp)3414 re_ifmedia_upd(if_t ifp)
3415 {
3416 	struct rl_softc		*sc;
3417 	struct mii_data		*mii;
3418 	int			error;
3419 
3420 	sc = if_getsoftc(ifp);
3421 	mii = device_get_softc(sc->rl_miibus);
3422 	RL_LOCK(sc);
3423 	error = mii_mediachg(mii);
3424 	RL_UNLOCK(sc);
3425 
3426 	return (error);
3427 }
3428 
3429 /*
3430  * Report current media status.
3431  */
3432 static void
re_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)3433 re_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
3434 {
3435 	struct rl_softc		*sc;
3436 	struct mii_data		*mii;
3437 
3438 	sc = if_getsoftc(ifp);
3439 	mii = device_get_softc(sc->rl_miibus);
3440 
3441 	RL_LOCK(sc);
3442 	mii_pollstat(mii);
3443 	ifmr->ifm_active = mii->mii_media_active;
3444 	ifmr->ifm_status = mii->mii_media_status;
3445 	RL_UNLOCK(sc);
3446 }
3447 
3448 static int
re_ioctl(if_t ifp,u_long command,caddr_t data)3449 re_ioctl(if_t ifp, u_long command, caddr_t data)
3450 {
3451 	struct rl_softc		*sc = if_getsoftc(ifp);
3452 	struct ifreq		*ifr = (struct ifreq *) data;
3453 	struct mii_data		*mii;
3454 	int			error = 0;
3455 
3456 	switch (command) {
3457 	case SIOCSIFMTU:
3458 		if (ifr->ifr_mtu < ETHERMIN ||
3459 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu ||
3460 		    ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 &&
3461 		    ifr->ifr_mtu > RL_MTU)) {
3462 			error = EINVAL;
3463 			break;
3464 		}
3465 		RL_LOCK(sc);
3466 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
3467 			if_setmtu(ifp, ifr->ifr_mtu);
3468 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3469 			    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
3470 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3471 				re_init_locked(sc);
3472 			}
3473 			if (if_getmtu(ifp) > RL_TSO_MTU &&
3474 			    (if_getcapenable(ifp) & IFCAP_TSO4) != 0) {
3475 				if_setcapenablebit(ifp, 0,
3476 				    IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
3477 				if_sethwassistbits(ifp, 0, CSUM_TSO);
3478 			}
3479 			VLAN_CAPABILITIES(ifp);
3480 		}
3481 		RL_UNLOCK(sc);
3482 		break;
3483 	case SIOCSIFFLAGS:
3484 		RL_LOCK(sc);
3485 		if ((if_getflags(ifp) & IFF_UP) != 0) {
3486 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
3487 				if (((if_getflags(ifp) ^ sc->rl_if_flags)
3488 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3489 					re_set_rxmode(sc);
3490 			} else
3491 				re_init_locked(sc);
3492 		} else {
3493 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3494 				re_stop(sc);
3495 		}
3496 		sc->rl_if_flags = if_getflags(ifp);
3497 		RL_UNLOCK(sc);
3498 		break;
3499 	case SIOCADDMULTI:
3500 	case SIOCDELMULTI:
3501 		RL_LOCK(sc);
3502 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3503 			re_set_rxmode(sc);
3504 		RL_UNLOCK(sc);
3505 		break;
3506 	case SIOCGIFMEDIA:
3507 	case SIOCSIFMEDIA:
3508 		mii = device_get_softc(sc->rl_miibus);
3509 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3510 		break;
3511 	case SIOCSIFCAP:
3512 	    {
3513 		int mask, reinit;
3514 
3515 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3516 		reinit = 0;
3517 #ifdef DEVICE_POLLING
3518 		if (mask & IFCAP_POLLING) {
3519 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
3520 				error = ether_poll_register(re_poll, ifp);
3521 				if (error)
3522 					return (error);
3523 				RL_LOCK(sc);
3524 				/* Disable interrupts */
3525 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
3526 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
3527 				RL_UNLOCK(sc);
3528 			} else {
3529 				error = ether_poll_deregister(ifp);
3530 				/* Enable interrupts. */
3531 				RL_LOCK(sc);
3532 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3533 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
3534 				RL_UNLOCK(sc);
3535 			}
3536 		}
3537 #endif /* DEVICE_POLLING */
3538 		RL_LOCK(sc);
3539 		if ((mask & IFCAP_TXCSUM) != 0 &&
3540 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
3541 			if_togglecapenable(ifp, IFCAP_TXCSUM);
3542 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
3543 				if_sethwassistbits(ifp, RE_CSUM_FEATURES, 0);
3544 			else
3545 				if_sethwassistbits(ifp, 0, RE_CSUM_FEATURES);
3546 			reinit = 1;
3547 		}
3548 		if ((mask & IFCAP_RXCSUM) != 0 &&
3549 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0) {
3550 			if_togglecapenable(ifp, IFCAP_RXCSUM);
3551 			reinit = 1;
3552 		}
3553 		if ((mask & IFCAP_TSO4) != 0 &&
3554 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
3555 			if_togglecapenable(ifp, IFCAP_TSO4);
3556 			if ((IFCAP_TSO4 & if_getcapenable(ifp)) != 0)
3557 				if_sethwassistbits(ifp, CSUM_TSO, 0);
3558 			else
3559 				if_sethwassistbits(ifp, 0, CSUM_TSO);
3560 			if (if_getmtu(ifp) > RL_TSO_MTU &&
3561 			    (if_getcapenable(ifp) & IFCAP_TSO4) != 0) {
3562 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3563 				if_sethwassistbits(ifp, 0, CSUM_TSO);
3564 			}
3565 		}
3566 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3567 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
3568 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3569 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3570 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
3571 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3572 			/* TSO over VLAN requires VLAN hardware tagging. */
3573 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
3574 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
3575 			reinit = 1;
3576 		}
3577 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3578 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
3579 		    IFCAP_VLAN_HWTSO)) != 0)
3580 				reinit = 1;
3581 		if ((mask & IFCAP_WOL) != 0 &&
3582 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0) {
3583 			if ((mask & IFCAP_WOL_UCAST) != 0)
3584 				if_togglecapenable(ifp, IFCAP_WOL_UCAST);
3585 			if ((mask & IFCAP_WOL_MCAST) != 0)
3586 				if_togglecapenable(ifp, IFCAP_WOL_MCAST);
3587 			if ((mask & IFCAP_WOL_MAGIC) != 0)
3588 				if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
3589 		}
3590 		if (reinit && if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3591 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3592 			re_init_locked(sc);
3593 		}
3594 		RL_UNLOCK(sc);
3595 		VLAN_CAPABILITIES(ifp);
3596 	    }
3597 		break;
3598 	default:
3599 		error = ether_ioctl(ifp, command, data);
3600 		break;
3601 	}
3602 
3603 	return (error);
3604 }
3605 
3606 static void
re_watchdog(struct rl_softc * sc)3607 re_watchdog(struct rl_softc *sc)
3608 {
3609 	struct epoch_tracker et;
3610 	if_t ifp;
3611 	const char *imode;
3612 	uint32_t isr, imr, txcfg;
3613 
3614 	RL_LOCK_ASSERT(sc);
3615 
3616 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
3617 		return;
3618 
3619 	ifp = sc->rl_ifp;
3620 	/*
3621 	 * Snapshot the controller state before reclaim for the diagnostics
3622 	 * below.  Reading RL_ISR is side-effect free: the interrupt status
3623 	 * is cleared by writing ones, not by reading.
3624 	 */
3625 	imode = (sc->rl_flags & RL_FLAG_MSIX) ? "MSI-X" :
3626 	    (sc->rl_flags & RL_FLAG_MSI) ? "MSI" : "INTx";
3627 	isr = CSR_READ_2(sc, RL_ISR);
3628 	imr = CSR_READ_2(sc, RL_IMR);
3629 	re_txeof(sc);
3630 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3631 		/*
3632 		 * The retry reclaim drained the ring: a Tx completion interrupt
3633 		 * was lost.  Log the interrupt state so the lost-interrupt path
3634 		 * can be diagnosed without a debug build.
3635 		 */
3636 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) -- "
3637 		    "recovering (ISR 0x%04x IMR 0x%04x %s)\n",
3638 		    isr, imr, imode);
3639 		if (!if_sendq_empty(ifp))
3640 			re_start_locked(ifp);
3641 		return;
3642 	}
3643 
3644 	txcfg = CSR_READ_4(sc, RL_TXCFG);
3645 	/*
3646 	 * A genuine Tx stall.  Log the Tx ring position and controller
3647 	 * state in a single line -- enough to distinguish a lost doorbell,
3648 	 * a DMA stall, and a controller that has fallen off the bus.
3649 	 */
3650 	if_printf(ifp, "watchdog timeout -- resetting (tx %d/%d cons %d "
3651 	    "prod %d ISR 0x%04x IMR 0x%04x TXCFG 0x%08x %s)\n",
3652 	    sc->rl_ldata.rl_tx_free, sc->rl_ldata.rl_tx_desc_cnt,
3653 	    sc->rl_ldata.rl_tx_considx, sc->rl_ldata.rl_tx_prodidx,
3654 	    isr, imr, txcfg, imode);
3655 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3656 
3657 	/*
3658 	 * If the controller reads back all-ones it has fallen off the bus;
3659 	 * re_init_locked() cannot recover it, so flag it and bail rather
3660 	 * than spinning through reset after reset.
3661 	 */
3662 	if (txcfg == 0xFFFFFFFF) {
3663 		if_printf(ifp,
3664 		    "controller not responding; not reinitializing\n");
3665 		return;
3666 	}
3667 
3668 	/*
3669 	 * ASPM is a common cause of these Tx timeouts.  Unless the
3670 	 * administrator has opted to keep ASPM enabled, make sure L0s/L1
3671 	 * and CLKREQ are still disabled on the link before reinitializing;
3672 	 * firmware or a power transition may have re-armed them.
3673 	 */
3674 	if (aspm_disable != 0 && sc->rl_expcap != 0) {
3675 		uint16_t ctl;
3676 
3677 		ctl = pci_read_config(sc->rl_dev,
3678 		    sc->rl_expcap + PCIER_LINK_CTL, 2);
3679 		if ((ctl & (PCIEM_LINK_CTL_ECPM | PCIEM_LINK_CTL_ASPMC)) != 0) {
3680 			ctl &= ~(PCIEM_LINK_CTL_ECPM | PCIEM_LINK_CTL_ASPMC);
3681 			pci_write_config(sc->rl_dev,
3682 			    sc->rl_expcap + PCIER_LINK_CTL, ctl, 2);
3683 		}
3684 	}
3685 
3686 	NET_EPOCH_ENTER(et);
3687 	re_rxeof(sc, NULL);
3688 	NET_EPOCH_EXIT(et);
3689 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3690 	re_init_locked(sc);
3691 	if (!if_sendq_empty(ifp))
3692 		re_start_locked(ifp);
3693 }
3694 
3695 /*
3696  * Stop the adapter and free any mbufs allocated to the
3697  * RX and TX lists.
3698  */
3699 static void
re_stop(struct rl_softc * sc)3700 re_stop(struct rl_softc *sc)
3701 {
3702 	int			i;
3703 	if_t ifp;
3704 	struct rl_txdesc	*txd;
3705 	struct rl_rxdesc	*rxd;
3706 
3707 	RL_LOCK_ASSERT(sc);
3708 
3709 	ifp = sc->rl_ifp;
3710 
3711 	sc->rl_watchdog_timer = 0;
3712 	callout_stop(&sc->rl_stat_callout);
3713 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
3714 
3715 #ifdef DEV_NETMAP
3716 	netmap_disable_all_rings(ifp);
3717 #endif /* DEV_NETMAP */
3718 
3719 	/*
3720 	 * Disable accepting frames to put RX MAC into idle state.
3721 	 * Otherwise it's possible to get frames while stop command
3722 	 * execution is in progress and controller can DMA the frame
3723 	 * to already freed RX buffer during that period.
3724 	 */
3725 	CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3726 	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3727 	    RL_RXCFG_RX_BROAD));
3728 
3729 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3730 		/* Enable RXDV gate. */
3731 		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) |
3732 		    0x00080000);
3733 	}
3734 
3735 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3736 		/*
3737 		 * RTL8168G and later.  The STOPREQ command is defined only for
3738 		 * earlier controllers; issuing it on these parts can leave the
3739 		 * MAC wedged.  With the RXDV gate enabled above, drain the TX
3740 		 * descriptor queue and the on-chip TX/RX FIFOs and clear the
3741 		 * TX/RX enable bits so the DMA engine is idle before the reset
3742 		 * and buffer free below.  All waits are bounded.
3743 		 */
3744 		DELAY(2000);
3745 		for (i = RL_TIMEOUT; i > 0; i--) {
3746 			if ((CSR_READ_4(sc, RL_TXCFG) &
3747 			    RL_TXCFG_QUEUE_EMPTY) != 0)
3748 				break;
3749 			DELAY(100);
3750 		}
3751 		if (i == 0)
3752 			device_printf(sc->rl_dev, "stopping TXQ timed out!\n");
3753 		CSR_WRITE_1(sc, RL_COMMAND, CSR_READ_1(sc, RL_COMMAND) &
3754 		    ~(RL_CMD_TX_ENB | RL_CMD_RX_ENB));
3755 		for (i = RL_TIMEOUT * 3; i > 0; i--) {
3756 			if ((CSR_READ_1(sc, RL_MCU_CMD) &
3757 			    (RL_MCU_TXFIFO_EMPTY | RL_MCU_RXFIFO_EMPTY)) ==
3758 			    (RL_MCU_TXFIFO_EMPTY | RL_MCU_RXFIFO_EMPTY))
3759 				break;
3760 			DELAY(20);
3761 		}
3762 		if (i == 0)
3763 			device_printf(sc->rl_dev,
3764 			    "TX/RX FIFO drain timed out!\n");
3765 	} else if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3766 		for (i = RL_TIMEOUT; i > 0; i--) {
3767 			if ((CSR_READ_1(sc, sc->rl_txstart) &
3768 			    RL_TXSTART_START) == 0)
3769 				break;
3770 			DELAY(20);
3771 		}
3772 		if (i == 0)
3773 			device_printf(sc->rl_dev,
3774 			    "stopping TX poll timed out!\n");
3775 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3776 	} else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3777 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3778 		    RL_CMD_RX_ENB);
3779 		if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3780 			for (i = RL_TIMEOUT; i > 0; i--) {
3781 				if ((CSR_READ_4(sc, RL_TXCFG) &
3782 				    RL_TXCFG_QUEUE_EMPTY) != 0)
3783 					break;
3784 				DELAY(100);
3785 			}
3786 			if (i == 0)
3787 				device_printf(sc->rl_dev,
3788 				   "stopping TXQ timed out!\n");
3789 		}
3790 	} else
3791 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3792 	DELAY(1000);
3793 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3794 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3795 
3796 	/*
3797 	 * Reset the controller before freeing the DMA buffers below.  A
3798 	 * controller that has not fully quiesced can keep fetching stale,
3799 	 * still-owned descriptors that point at about-to-be-freed mbufs.
3800 	 * re_init_locked() resets again on the reinit path; the extra reset
3801 	 * is idempotent and cheap.
3802 	 */
3803 	re_reset(sc);
3804 
3805 	if (sc->rl_head != NULL) {
3806 		m_freem(sc->rl_head);
3807 		sc->rl_head = sc->rl_tail = NULL;
3808 	}
3809 
3810 	/* Free the TX list buffers. */
3811 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3812 		txd = &sc->rl_ldata.rl_tx_desc[i];
3813 		if (txd->tx_m != NULL) {
3814 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3815 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3816 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3817 			    txd->tx_dmamap);
3818 			m_freem(txd->tx_m);
3819 			txd->tx_m = NULL;
3820 		}
3821 	}
3822 
3823 	/* Free the RX list buffers. */
3824 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3825 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3826 		if (rxd->rx_m != NULL) {
3827 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3828 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3829 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3830 			    rxd->rx_dmamap);
3831 			m_freem(rxd->rx_m);
3832 			rxd->rx_m = NULL;
3833 		}
3834 	}
3835 
3836 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3837 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3838 			rxd = &sc->rl_ldata.rl_jrx_desc[i];
3839 			if (rxd->rx_m != NULL) {
3840 				bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
3841 				    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3842 				bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
3843 				    rxd->rx_dmamap);
3844 				m_freem(rxd->rx_m);
3845 				rxd->rx_m = NULL;
3846 			}
3847 		}
3848 	}
3849 }
3850 
3851 /*
3852  * Device suspend routine.  Stop the interface and save some PCI
3853  * settings in case the BIOS doesn't restore them properly on
3854  * resume.
3855  */
3856 static int
re_suspend(device_t dev)3857 re_suspend(device_t dev)
3858 {
3859 	struct rl_softc		*sc;
3860 
3861 	sc = device_get_softc(dev);
3862 
3863 	RL_LOCK(sc);
3864 	re_stop(sc);
3865 	re_setwol(sc);
3866 	sc->suspended = 1;
3867 	RL_UNLOCK(sc);
3868 
3869 	return (0);
3870 }
3871 
3872 /*
3873  * Device resume routine.  Restore some PCI settings in case the BIOS
3874  * doesn't, re-enable busmastering, and restart the interface if
3875  * appropriate.
3876  */
3877 static int
re_resume(device_t dev)3878 re_resume(device_t dev)
3879 {
3880 	struct rl_softc		*sc;
3881 	if_t ifp;
3882 
3883 	sc = device_get_softc(dev);
3884 
3885 	RL_LOCK(sc);
3886 
3887 	ifp = sc->rl_ifp;
3888 	/* Take controller out of sleep mode. */
3889 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
3890 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
3891 			CSR_WRITE_1(sc, RL_GPIO,
3892 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
3893 	}
3894 
3895 	/*
3896 	 * Clear WOL matching such that normal Rx filtering
3897 	 * wouldn't interfere with WOL patterns.
3898 	 */
3899 	re_clrwol(sc);
3900 
3901 	/* reinitialize interface if necessary */
3902 	if (if_getflags(ifp) & IFF_UP)
3903 		re_init_locked(sc);
3904 
3905 	sc->suspended = 0;
3906 	RL_UNLOCK(sc);
3907 
3908 	return (0);
3909 }
3910 
3911 /*
3912  * Stop all chip I/O so that the kernel's probe routines don't
3913  * get confused by errant DMAs when rebooting.
3914  */
3915 static int
re_shutdown(device_t dev)3916 re_shutdown(device_t dev)
3917 {
3918 	struct rl_softc		*sc;
3919 
3920 	sc = device_get_softc(dev);
3921 
3922 	RL_LOCK(sc);
3923 	re_stop(sc);
3924 	/*
3925 	 * Mark interface as down since otherwise we will panic if
3926 	 * interrupt comes in later on, which can happen in some
3927 	 * cases.
3928 	 */
3929 	if_setflagbits(sc->rl_ifp, 0, IFF_UP);
3930 	re_setwol(sc);
3931 	RL_UNLOCK(sc);
3932 
3933 	return (0);
3934 }
3935 
3936 static void
re_set_linkspeed(struct rl_softc * sc)3937 re_set_linkspeed(struct rl_softc *sc)
3938 {
3939 	struct mii_softc *miisc;
3940 	struct mii_data *mii;
3941 	int aneg, i, phyno;
3942 
3943 	RL_LOCK_ASSERT(sc);
3944 
3945 	mii = device_get_softc(sc->rl_miibus);
3946 	mii_pollstat(mii);
3947 	aneg = 0;
3948 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
3949 	    (IFM_ACTIVE | IFM_AVALID)) {
3950 		switch IFM_SUBTYPE(mii->mii_media_active) {
3951 		case IFM_10_T:
3952 		case IFM_100_TX:
3953 			return;
3954 		case IFM_1000_T:
3955 			aneg++;
3956 			break;
3957 		default:
3958 			break;
3959 		}
3960 	}
3961 	miisc = LIST_FIRST(&mii->mii_phys);
3962 	phyno = miisc->mii_phy;
3963 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3964 		PHY_RESET(miisc);
3965 	re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
3966 	re_miibus_writereg(sc->rl_dev, phyno,
3967 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
3968 	re_miibus_writereg(sc->rl_dev, phyno,
3969 	    MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
3970 	DELAY(1000);
3971 	if (aneg != 0) {
3972 		/*
3973 		 * Poll link state until re(4) get a 10/100Mbps link.
3974 		 */
3975 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
3976 			mii_pollstat(mii);
3977 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
3978 			    == (IFM_ACTIVE | IFM_AVALID)) {
3979 				switch (IFM_SUBTYPE(mii->mii_media_active)) {
3980 				case IFM_10_T:
3981 				case IFM_100_TX:
3982 					return;
3983 				default:
3984 					break;
3985 				}
3986 			}
3987 			RL_UNLOCK(sc);
3988 			pause("relnk", hz);
3989 			RL_LOCK(sc);
3990 		}
3991 		if (i == MII_ANEGTICKS_GIGE)
3992 			device_printf(sc->rl_dev,
3993 			    "establishing a link failed, WOL may not work!");
3994 	}
3995 	/*
3996 	 * No link, force MAC to have 100Mbps, full-duplex link.
3997 	 * MAC does not require reprogramming on resolved speed/duplex,
3998 	 * so this is just for completeness.
3999 	 */
4000 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
4001 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
4002 }
4003 
4004 static void
re_setwol(struct rl_softc * sc)4005 re_setwol(struct rl_softc *sc)
4006 {
4007 	if_t ifp;
4008 	uint8_t			v;
4009 
4010 	RL_LOCK_ASSERT(sc);
4011 
4012 	if (!pci_has_pm(sc->rl_dev))
4013 		return;
4014 
4015 	ifp = sc->rl_ifp;
4016 	/* Put controller into sleep mode. */
4017 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
4018 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
4019 			CSR_WRITE_1(sc, RL_GPIO,
4020 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
4021 	}
4022 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
4023 		if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
4024 			/* Disable RXDV gate. */
4025 			CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
4026 			    ~0x00080000);
4027 		}
4028 		re_set_rxmode(sc);
4029 		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
4030 			re_set_linkspeed(sc);
4031 		if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
4032 			CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
4033 	}
4034 	/* Enable config register write. */
4035 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
4036 
4037 	/* Enable PME. */
4038 	v = CSR_READ_1(sc, sc->rl_cfg1);
4039 	v &= ~RL_CFG1_PME;
4040 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
4041 		v |= RL_CFG1_PME;
4042 	CSR_WRITE_1(sc, sc->rl_cfg1, v);
4043 
4044 	v = CSR_READ_1(sc, sc->rl_cfg3);
4045 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
4046 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
4047 		v |= RL_CFG3_WOL_MAGIC;
4048 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
4049 
4050 	v = CSR_READ_1(sc, sc->rl_cfg5);
4051 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
4052 	    RL_CFG5_WOL_LANWAKE);
4053 	if ((if_getcapenable(ifp) & IFCAP_WOL_UCAST) != 0)
4054 		v |= RL_CFG5_WOL_UCAST;
4055 	if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
4056 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
4057 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
4058 		v |= RL_CFG5_WOL_LANWAKE;
4059 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
4060 
4061 	/* Config register write done. */
4062 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
4063 
4064 	if ((if_getcapenable(ifp) & IFCAP_WOL) == 0 &&
4065 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
4066 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
4067 	/*
4068 	 * It seems that hardware resets its link speed to 100Mbps in
4069 	 * power down mode so switching to 100Mbps in driver is not
4070 	 * needed.
4071 	 */
4072 
4073 	/* Request PME if WOL is requested. */
4074 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
4075 		pci_enable_pme(sc->rl_dev);
4076 }
4077 
4078 static void
re_clrwol(struct rl_softc * sc)4079 re_clrwol(struct rl_softc *sc)
4080 {
4081 	uint8_t			v;
4082 
4083 	RL_LOCK_ASSERT(sc);
4084 
4085 	if (!pci_has_pm(sc->rl_dev))
4086 		return;
4087 
4088 	/* Enable config register write. */
4089 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
4090 
4091 	v = CSR_READ_1(sc, sc->rl_cfg3);
4092 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
4093 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
4094 
4095 	/* Config register write done. */
4096 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
4097 
4098 	v = CSR_READ_1(sc, sc->rl_cfg5);
4099 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
4100 	v &= ~RL_CFG5_WOL_LANWAKE;
4101 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
4102 }
4103 
4104 static void
re_add_sysctls(struct rl_softc * sc)4105 re_add_sysctls(struct rl_softc *sc)
4106 {
4107 	struct sysctl_ctx_list	*ctx;
4108 	struct sysctl_oid_list	*children;
4109 	int			error;
4110 
4111 	ctx = device_get_sysctl_ctx(sc->rl_dev);
4112 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
4113 
4114 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
4115 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
4116 	    re_sysctl_stats, "I", "Statistics Information");
4117 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
4118 		return;
4119 
4120 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
4121 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
4122 	    &sc->rl_int_rx_mod, 0, sysctl_hw_re_int_mod, "I",
4123 	    "re RX interrupt moderation");
4124 	/* Pull in device tunables. */
4125 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
4126 	error = resource_int_value(device_get_name(sc->rl_dev),
4127 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
4128 	if (error == 0) {
4129 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
4130 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
4131 			device_printf(sc->rl_dev, "int_rx_mod value out of "
4132 			    "range; using default: %d\n",
4133 			    RL_TIMER_DEFAULT);
4134 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
4135 		}
4136 	}
4137 }
4138 
4139 static int
re_sysctl_stats(SYSCTL_HANDLER_ARGS)4140 re_sysctl_stats(SYSCTL_HANDLER_ARGS)
4141 {
4142 	struct rl_softc		*sc;
4143 	struct rl_stats		*stats;
4144 	int			error, i, result;
4145 
4146 	result = -1;
4147 	error = sysctl_handle_int(oidp, &result, 0, req);
4148 	if (error || req->newptr == NULL)
4149 		return (error);
4150 
4151 	if (result == 1) {
4152 		sc = (struct rl_softc *)arg1;
4153 		RL_LOCK(sc);
4154 		if ((if_getdrvflags(sc->rl_ifp) & IFF_DRV_RUNNING) == 0) {
4155 			RL_UNLOCK(sc);
4156 			goto done;
4157 		}
4158 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
4159 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
4160 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
4161 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
4162 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
4163 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
4164 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
4165 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
4166 		    RL_DUMPSTATS_START));
4167 		for (i = RL_TIMEOUT; i > 0; i--) {
4168 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
4169 			    RL_DUMPSTATS_START) == 0)
4170 				break;
4171 			DELAY(1000);
4172 		}
4173 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
4174 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
4175 		RL_UNLOCK(sc);
4176 		if (i == 0) {
4177 			device_printf(sc->rl_dev,
4178 			    "DUMP statistics request timed out\n");
4179 			return (ETIMEDOUT);
4180 		}
4181 done:
4182 		stats = sc->rl_ldata.rl_stats;
4183 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
4184 		printf("Tx frames : %ju\n",
4185 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
4186 		printf("Rx frames : %ju\n",
4187 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
4188 		printf("Tx errors : %ju\n",
4189 		    (uintmax_t)le64toh(stats->rl_tx_errs));
4190 		printf("Rx errors : %u\n",
4191 		    le32toh(stats->rl_rx_errs));
4192 		printf("Rx missed frames : %u\n",
4193 		    (uint32_t)le16toh(stats->rl_missed_pkts));
4194 		printf("Rx frame alignment errs : %u\n",
4195 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
4196 		printf("Tx single collisions : %u\n",
4197 		    le32toh(stats->rl_tx_onecoll));
4198 		printf("Tx multiple collisions : %u\n",
4199 		    le32toh(stats->rl_tx_multicolls));
4200 		printf("Rx unicast frames : %ju\n",
4201 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
4202 		printf("Rx broadcast frames : %ju\n",
4203 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
4204 		printf("Rx multicast frames : %u\n",
4205 		    le32toh(stats->rl_rx_mcasts));
4206 		printf("Tx aborts : %u\n",
4207 		    (uint32_t)le16toh(stats->rl_tx_aborts));
4208 		printf("Tx underruns : %u\n",
4209 		    (uint32_t)le16toh(stats->rl_rx_underruns));
4210 	}
4211 
4212 	return (error);
4213 }
4214 
4215 static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)4216 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4217 {
4218 	int error, value;
4219 
4220 	if (arg1 == NULL)
4221 		return (EINVAL);
4222 	value = *(int *)arg1;
4223 	error = sysctl_handle_int(oidp, &value, 0, req);
4224 	if (error || req->newptr == NULL)
4225 		return (error);
4226 	if (value < low || value > high)
4227 		return (EINVAL);
4228 	*(int *)arg1 = value;
4229 
4230 	return (0);
4231 }
4232 
4233 static int
sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)4234 sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4235 {
4236 
4237 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
4238 	    RL_TIMER_MAX));
4239 }
4240 
4241 #ifdef DEBUGNET
4242 static void
re_debugnet_init(if_t ifp,int * nrxr,int * ncl,int * clsize)4243 re_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
4244 {
4245 	struct rl_softc *sc;
4246 
4247 	sc = if_getsoftc(ifp);
4248 	RL_LOCK(sc);
4249 	*nrxr = sc->rl_ldata.rl_rx_desc_cnt;
4250 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
4251 	*clsize = (if_getmtu(ifp) > RL_MTU &&
4252 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) ? MJUM9BYTES : MCLBYTES;
4253 	RL_UNLOCK(sc);
4254 }
4255 
4256 static void
re_debugnet_event(if_t ifp __unused,enum debugnet_ev event __unused)4257 re_debugnet_event(if_t ifp __unused, enum debugnet_ev event __unused)
4258 {
4259 }
4260 
4261 static int
re_debugnet_transmit(if_t ifp,struct mbuf * m)4262 re_debugnet_transmit(if_t ifp, struct mbuf *m)
4263 {
4264 	struct rl_softc *sc;
4265 	int error;
4266 
4267 	sc = if_getsoftc(ifp);
4268 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4269 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
4270 		return (EBUSY);
4271 
4272 	error = re_encap(sc, &m);
4273 	if (error == 0)
4274 		re_start_tx(sc);
4275 	return (error);
4276 }
4277 
4278 static int
re_debugnet_poll(if_t ifp,int count)4279 re_debugnet_poll(if_t ifp, int count)
4280 {
4281 	struct rl_softc *sc;
4282 	int error;
4283 
4284 	sc = if_getsoftc(ifp);
4285 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
4286 	    (sc->rl_flags & RL_FLAG_LINK) == 0)
4287 		return (EBUSY);
4288 
4289 	re_txeof(sc);
4290 	error = re_rxeof(sc, NULL);
4291 	if (error != 0 && error != EAGAIN)
4292 		return (error);
4293 	return (0);
4294 }
4295 #endif /* DEBUGNET */
4296