if_re.c revision 135468
1/*
2 * Copyright (c) 1997, 1998-2003
3 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/re/if_re.c 135468 2004-09-19 17:51:41Z jmg $");
35
36/*
37 * RealTek 8139C+/8169/8169S/8110S 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 * four devices in this family: the RTL8139C+, the RTL8169, the RTL8169S
48 * and the RTL8110S.
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 7.5K, so the max MTU possible with this
108 * driver is 7500 bytes.
109 */
110
111#include <sys/param.h>
112#include <sys/endian.h>
113#include <sys/systm.h>
114#include <sys/sockio.h>
115#include <sys/mbuf.h>
116#include <sys/malloc.h>
117#include <sys/module.h>
118#include <sys/kernel.h>
119#include <sys/socket.h>
120
121#include <net/if.h>
122#include <net/if_arp.h>
123#include <net/ethernet.h>
124#include <net/if_dl.h>
125#include <net/if_media.h>
126#include <net/if_vlan_var.h>
127
128#include <net/bpf.h>
129
130#include <machine/bus_pio.h>
131#include <machine/bus_memio.h>
132#include <machine/bus.h>
133#include <machine/resource.h>
134#include <sys/bus.h>
135#include <sys/rman.h>
136
137#include <dev/mii/mii.h>
138#include <dev/mii/miivar.h>
139
140#include <dev/pci/pcireg.h>
141#include <dev/pci/pcivar.h>
142
143MODULE_DEPEND(re, pci, 1, 1, 1);
144MODULE_DEPEND(re, ether, 1, 1, 1);
145MODULE_DEPEND(re, miibus, 1, 1, 1);
146
147/* "controller miibus0" required.  See GENERIC if you get errors here. */
148#include "miibus_if.h"
149
150/*
151 * Default to using PIO access for this driver.
152 */
153#define RE_USEIOSPACE
154
155#include <pci/if_rlreg.h>
156
157#define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
158
159/*
160 * Various supported device vendors/types and their names.
161 */
162static struct rl_type re_devs[] = {
163	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
164		"RealTek 8139C+ 10/100BaseTX" },
165	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
166		"RealTek 8169 Gigabit Ethernet" },
167	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
168		"RealTek 8169S Single-chip Gigabit Ethernet" },
169	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
170		"RealTek 8110S Single-chip Gigabit Ethernet" },
171	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
172		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
173	{ 0, 0, 0, NULL }
174};
175
176static struct rl_hwrev re_hwrevs[] = {
177	{ RL_HWREV_8139, RL_8139,  "" },
178	{ RL_HWREV_8139A, RL_8139, "A" },
179	{ RL_HWREV_8139AG, RL_8139, "A-G" },
180	{ RL_HWREV_8139B, RL_8139, "B" },
181	{ RL_HWREV_8130, RL_8139, "8130" },
182	{ RL_HWREV_8139C, RL_8139, "C" },
183	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
184	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
185	{ RL_HWREV_8169, RL_8169, "8169"},
186	{ RL_HWREV_8169S, RL_8169, "8169S"},
187	{ RL_HWREV_8110S, RL_8169, "8110S"},
188	{ RL_HWREV_8100, RL_8139, "8100"},
189	{ RL_HWREV_8101, RL_8139, "8101"},
190	{ 0, 0, NULL }
191};
192
193static int re_probe		(device_t);
194static int re_attach		(device_t);
195static int re_detach		(device_t);
196
197static int re_encap		(struct rl_softc *, struct mbuf **, int *);
198
199static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
200static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
201				    bus_size_t, int);
202static int re_allocmem		(device_t, struct rl_softc *);
203static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
204static int re_rx_list_init	(struct rl_softc *);
205static int re_tx_list_init	(struct rl_softc *);
206static void re_rxeof		(struct rl_softc *);
207static void re_txeof		(struct rl_softc *);
208#ifdef DEVICE_POLLING
209static void re_poll		(struct ifnet *, enum poll_cmd, int);
210static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
211#endif
212static void re_intr		(void *);
213static void re_tick		(void *);
214static void re_tick_locked	(struct rl_softc *);
215static void re_start		(struct ifnet *);
216static void re_start_locked	(struct ifnet *);
217static int re_ioctl		(struct ifnet *, u_long, caddr_t);
218static void re_init		(void *);
219static void re_init_locked	(struct rl_softc *);
220static void re_stop		(struct rl_softc *);
221static void re_watchdog		(struct ifnet *);
222static int re_suspend		(device_t);
223static int re_resume		(device_t);
224static void re_shutdown		(device_t);
225static int re_ifmedia_upd	(struct ifnet *);
226static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
227
228static void re_eeprom_putbyte	(struct rl_softc *, int);
229static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
230static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int, int);
231static int re_gmii_readreg	(device_t, int, int);
232static int re_gmii_writereg	(device_t, int, int, int);
233
234static int re_miibus_readreg	(device_t, int, int);
235static int re_miibus_writereg	(device_t, int, int, int);
236static void re_miibus_statchg	(device_t);
237
238static void re_setmulti		(struct rl_softc *);
239static void re_reset		(struct rl_softc *);
240
241static int re_diag		(struct rl_softc *);
242
243#ifdef RE_USEIOSPACE
244#define RL_RES			SYS_RES_IOPORT
245#define RL_RID			RL_PCI_LOIO
246#else
247#define RL_RES			SYS_RES_MEMORY
248#define RL_RID			RL_PCI_LOMEM
249#endif
250
251static device_method_t re_methods[] = {
252	/* Device interface */
253	DEVMETHOD(device_probe,		re_probe),
254	DEVMETHOD(device_attach,	re_attach),
255	DEVMETHOD(device_detach,	re_detach),
256	DEVMETHOD(device_suspend,	re_suspend),
257	DEVMETHOD(device_resume,	re_resume),
258	DEVMETHOD(device_shutdown,	re_shutdown),
259
260	/* bus interface */
261	DEVMETHOD(bus_print_child,	bus_generic_print_child),
262	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
263
264	/* MII interface */
265	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
266	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
267	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
268
269	{ 0, 0 }
270};
271
272static driver_t re_driver = {
273	"re",
274	re_methods,
275	sizeof(struct rl_softc)
276};
277
278static devclass_t re_devclass;
279
280DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
281DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
282DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
283
284#define EE_SET(x)					\
285	CSR_WRITE_1(sc, RL_EECMD,			\
286		CSR_READ_1(sc, RL_EECMD) | x)
287
288#define EE_CLR(x)					\
289	CSR_WRITE_1(sc, RL_EECMD,			\
290		CSR_READ_1(sc, RL_EECMD) & ~x)
291
292/*
293 * Send a read command and address to the EEPROM, check for ACK.
294 */
295static void
296re_eeprom_putbyte(sc, addr)
297	struct rl_softc		*sc;
298	int			addr;
299{
300	register int		d, i;
301
302	d = addr | sc->rl_eecmd_read;
303
304	/*
305	 * Feed in each bit and strobe the clock.
306	 */
307	for (i = 0x400; i; i >>= 1) {
308		if (d & i) {
309			EE_SET(RL_EE_DATAIN);
310		} else {
311			EE_CLR(RL_EE_DATAIN);
312		}
313		DELAY(100);
314		EE_SET(RL_EE_CLK);
315		DELAY(150);
316		EE_CLR(RL_EE_CLK);
317		DELAY(100);
318	}
319}
320
321/*
322 * Read a word of data stored in the EEPROM at address 'addr.'
323 */
324static void
325re_eeprom_getword(sc, addr, dest)
326	struct rl_softc		*sc;
327	int			addr;
328	u_int16_t		*dest;
329{
330	register int		i;
331	u_int16_t		word = 0;
332
333	/* Enter EEPROM access mode. */
334	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
335
336	/*
337	 * Send address of word we want to read.
338	 */
339	re_eeprom_putbyte(sc, addr);
340
341	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
342
343	/*
344	 * Start reading bits from EEPROM.
345	 */
346	for (i = 0x8000; i; i >>= 1) {
347		EE_SET(RL_EE_CLK);
348		DELAY(100);
349		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
350			word |= i;
351		EE_CLR(RL_EE_CLK);
352		DELAY(100);
353	}
354
355	/* Turn off EEPROM access mode. */
356	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
357
358	*dest = word;
359}
360
361/*
362 * Read a sequence of words from the EEPROM.
363 */
364static void
365re_read_eeprom(sc, dest, off, cnt, swap)
366	struct rl_softc		*sc;
367	caddr_t			dest;
368	int			off;
369	int			cnt;
370	int			swap;
371{
372	int			i;
373	u_int16_t		word = 0, *ptr;
374
375	for (i = 0; i < cnt; i++) {
376		re_eeprom_getword(sc, off + i, &word);
377		ptr = (u_int16_t *)(dest + (i * 2));
378		if (swap)
379			*ptr = ntohs(word);
380		else
381			*ptr = word;
382	}
383}
384
385static int
386re_gmii_readreg(dev, phy, reg)
387	device_t		dev;
388	int			phy, reg;
389{
390	struct rl_softc		*sc;
391	u_int32_t		rval;
392	int			i;
393
394	if (phy != 1)
395		return (0);
396
397	sc = device_get_softc(dev);
398
399	/* Let the rgephy driver read the GMEDIASTAT register */
400
401	if (reg == RL_GMEDIASTAT) {
402		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
403		return (rval);
404	}
405
406	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
407	DELAY(1000);
408
409	for (i = 0; i < RL_TIMEOUT; i++) {
410		rval = CSR_READ_4(sc, RL_PHYAR);
411		if (rval & RL_PHYAR_BUSY)
412			break;
413		DELAY(100);
414	}
415
416	if (i == RL_TIMEOUT) {
417		printf ("re%d: PHY read failed\n", sc->rl_unit);
418		return (0);
419	}
420
421	return (rval & RL_PHYAR_PHYDATA);
422}
423
424static int
425re_gmii_writereg(dev, phy, reg, data)
426	device_t		dev;
427	int			phy, reg, data;
428{
429	struct rl_softc		*sc;
430	u_int32_t		rval;
431	int			i;
432
433	sc = device_get_softc(dev);
434
435	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
436	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
437	DELAY(1000);
438
439	for (i = 0; i < RL_TIMEOUT; i++) {
440		rval = CSR_READ_4(sc, RL_PHYAR);
441		if (!(rval & RL_PHYAR_BUSY))
442			break;
443		DELAY(100);
444	}
445
446	if (i == RL_TIMEOUT) {
447		printf ("re%d: PHY write failed\n", sc->rl_unit);
448		return (0);
449	}
450
451	return (0);
452}
453
454static int
455re_miibus_readreg(dev, phy, reg)
456	device_t		dev;
457	int			phy, reg;
458{
459	struct rl_softc		*sc;
460	u_int16_t		rval = 0;
461	u_int16_t		re8139_reg = 0;
462
463	sc = device_get_softc(dev);
464
465	if (sc->rl_type == RL_8169) {
466		rval = re_gmii_readreg(dev, phy, reg);
467		return (rval);
468	}
469
470	/* Pretend the internal PHY is only at address 0 */
471	if (phy) {
472		return (0);
473	}
474	switch (reg) {
475	case MII_BMCR:
476		re8139_reg = RL_BMCR;
477		break;
478	case MII_BMSR:
479		re8139_reg = RL_BMSR;
480		break;
481	case MII_ANAR:
482		re8139_reg = RL_ANAR;
483		break;
484	case MII_ANER:
485		re8139_reg = RL_ANER;
486		break;
487	case MII_ANLPAR:
488		re8139_reg = RL_LPAR;
489		break;
490	case MII_PHYIDR1:
491	case MII_PHYIDR2:
492		return (0);
493	/*
494	 * Allow the rlphy driver to read the media status
495	 * register. If we have a link partner which does not
496	 * support NWAY, this is the register which will tell
497	 * us the results of parallel detection.
498	 */
499	case RL_MEDIASTAT:
500		rval = CSR_READ_1(sc, RL_MEDIASTAT);
501		return (rval);
502	default:
503		printf("re%d: bad phy register\n", sc->rl_unit);
504		return (0);
505	}
506	rval = CSR_READ_2(sc, re8139_reg);
507	return (rval);
508}
509
510static int
511re_miibus_writereg(dev, phy, reg, data)
512	device_t		dev;
513	int			phy, reg, data;
514{
515	struct rl_softc		*sc;
516	u_int16_t		re8139_reg = 0;
517	int			rval = 0;
518
519	sc = device_get_softc(dev);
520
521	if (sc->rl_type == RL_8169) {
522		rval = re_gmii_writereg(dev, phy, reg, data);
523		return (rval);
524	}
525
526	/* Pretend the internal PHY is only at address 0 */
527	if (phy)
528		return (0);
529
530	switch (reg) {
531	case MII_BMCR:
532		re8139_reg = RL_BMCR;
533		break;
534	case MII_BMSR:
535		re8139_reg = RL_BMSR;
536		break;
537	case MII_ANAR:
538		re8139_reg = RL_ANAR;
539		break;
540	case MII_ANER:
541		re8139_reg = RL_ANER;
542		break;
543	case MII_ANLPAR:
544		re8139_reg = RL_LPAR;
545		break;
546	case MII_PHYIDR1:
547	case MII_PHYIDR2:
548		return (0);
549		break;
550	default:
551		printf("re%d: bad phy register\n", sc->rl_unit);
552		return (0);
553	}
554	CSR_WRITE_2(sc, re8139_reg, data);
555	return (0);
556}
557
558static void
559re_miibus_statchg(dev)
560	device_t		dev;
561{
562
563}
564
565/*
566 * Program the 64-bit multicast hash filter.
567 */
568static void
569re_setmulti(sc)
570	struct rl_softc		*sc;
571{
572	struct ifnet		*ifp;
573	int			h = 0;
574	u_int32_t		hashes[2] = { 0, 0 };
575	struct ifmultiaddr	*ifma;
576	u_int32_t		rxfilt;
577	int			mcnt = 0;
578
579	RL_LOCK_ASSERT(sc);
580
581	ifp = &sc->arpcom.ac_if;
582
583	rxfilt = CSR_READ_4(sc, RL_RXCFG);
584
585	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
586		rxfilt |= RL_RXCFG_RX_MULTI;
587		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
588		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
589		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
590		return;
591	}
592
593	/* first, zot all the existing hash bits */
594	CSR_WRITE_4(sc, RL_MAR0, 0);
595	CSR_WRITE_4(sc, RL_MAR4, 0);
596
597	/* now program new ones */
598	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
599		if (ifma->ifma_addr->sa_family != AF_LINK)
600			continue;
601		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
602		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
603		if (h < 32)
604			hashes[0] |= (1 << h);
605		else
606			hashes[1] |= (1 << (h - 32));
607		mcnt++;
608	}
609
610	if (mcnt)
611		rxfilt |= RL_RXCFG_RX_MULTI;
612	else
613		rxfilt &= ~RL_RXCFG_RX_MULTI;
614
615	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
616	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
617	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
618}
619
620static void
621re_reset(sc)
622	struct rl_softc		*sc;
623{
624	register int		i;
625
626	RL_LOCK_ASSERT(sc);
627
628	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
629
630	for (i = 0; i < RL_TIMEOUT; i++) {
631		DELAY(10);
632		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
633			break;
634	}
635	if (i == RL_TIMEOUT)
636		printf("re%d: reset never completed!\n", sc->rl_unit);
637
638	CSR_WRITE_1(sc, 0x82, 1);
639}
640
641/*
642 * The following routine is designed to test for a defect on some
643 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
644 * lines connected to the bus, however for a 32-bit only card, they
645 * should be pulled high. The result of this defect is that the
646 * NIC will not work right if you plug it into a 64-bit slot: DMA
647 * operations will be done with 64-bit transfers, which will fail
648 * because the 64-bit data lines aren't connected.
649 *
650 * There's no way to work around this (short of talking a soldering
651 * iron to the board), however we can detect it. The method we use
652 * here is to put the NIC into digital loopback mode, set the receiver
653 * to promiscuous mode, and then try to send a frame. We then compare
654 * the frame data we sent to what was received. If the data matches,
655 * then the NIC is working correctly, otherwise we know the user has
656 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
657 * slot. In the latter case, there's no way the NIC can work correctly,
658 * so we print out a message on the console and abort the device attach.
659 */
660
661static int
662re_diag(sc)
663	struct rl_softc		*sc;
664{
665	struct ifnet		*ifp = &sc->arpcom.ac_if;
666	struct mbuf		*m0;
667	struct ether_header	*eh;
668	struct rl_desc		*cur_rx;
669	u_int16_t		status;
670	u_int32_t		rxstat;
671	int			total_len, i, error = 0;
672	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
673	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
674
675	/* Allocate a single mbuf */
676	MGETHDR(m0, M_DONTWAIT, MT_DATA);
677	if (m0 == NULL)
678		return (ENOBUFS);
679
680	RL_LOCK(sc);
681
682	/*
683	 * Initialize the NIC in test mode. This sets the chip up
684	 * so that it can send and receive frames, but performs the
685	 * following special functions:
686	 * - Puts receiver in promiscuous mode
687	 * - Enables digital loopback mode
688	 * - Leaves interrupts turned off
689	 */
690
691	ifp->if_flags |= IFF_PROMISC;
692	sc->rl_testmode = 1;
693	re_init_locked(sc);
694	re_stop(sc);
695	DELAY(100000);
696	re_init_locked(sc);
697
698	/* Put some data in the mbuf */
699
700	eh = mtod(m0, struct ether_header *);
701	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
702	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
703	eh->ether_type = htons(ETHERTYPE_IP);
704	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
705
706	/*
707	 * Queue the packet, start transmission.
708	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
709	 */
710
711	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
712	RL_UNLOCK(sc);
713	IF_HANDOFF(&ifp->if_snd, m0, ifp);
714	RL_LOCK(sc);
715	m0 = NULL;
716
717	/* Wait for it to propagate through the chip */
718
719	DELAY(100000);
720	for (i = 0; i < RL_TIMEOUT; i++) {
721		status = CSR_READ_2(sc, RL_ISR);
722		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
723		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
724			break;
725		DELAY(10);
726	}
727
728	if (i == RL_TIMEOUT) {
729		printf("re%d: diagnostic failed, failed to receive packet "
730		    "in loopback mode\n", sc->rl_unit);
731		error = EIO;
732		goto done;
733	}
734
735	/*
736	 * The packet should have been dumped into the first
737	 * entry in the RX DMA ring. Grab it from there.
738	 */
739
740	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
741	    sc->rl_ldata.rl_rx_list_map,
742	    BUS_DMASYNC_POSTREAD);
743	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
744	    sc->rl_ldata.rl_rx_dmamap[0],
745	    BUS_DMASYNC_POSTWRITE);
746	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
747	    sc->rl_ldata.rl_rx_dmamap[0]);
748
749	m0 = sc->rl_ldata.rl_rx_mbuf[0];
750	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
751	eh = mtod(m0, struct ether_header *);
752
753	cur_rx = &sc->rl_ldata.rl_rx_list[0];
754	total_len = RL_RXBYTES(cur_rx);
755	rxstat = le32toh(cur_rx->rl_cmdstat);
756
757	if (total_len != ETHER_MIN_LEN) {
758		printf("re%d: diagnostic failed, received short packet\n",
759		    sc->rl_unit);
760		error = EIO;
761		goto done;
762	}
763
764	/* Test that the received packet data matches what we sent. */
765
766	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
767	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
768	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
769		printf("re%d: WARNING, DMA FAILURE!\n", sc->rl_unit);
770		printf("re%d: expected TX data: %6D/%6D/0x%x\n", sc->rl_unit,
771		    dst, ":", src, ":", ETHERTYPE_IP);
772		printf("re%d: received RX data: %6D/%6D/0x%x\n", sc->rl_unit,
773		    eh->ether_dhost, ":",  eh->ether_shost, ":",
774		    ntohs(eh->ether_type));
775		printf("re%d: You may have a defective 32-bit NIC plugged "
776		    "into a 64-bit PCI slot.\n", sc->rl_unit);
777		printf("re%d: Please re-install the NIC in a 32-bit slot "
778		    "for proper operation.\n", sc->rl_unit);
779		printf("re%d: Read the re(4) man page for more details.\n",
780		    sc->rl_unit);
781		error = EIO;
782	}
783
784done:
785	/* Turn interface off, release resources */
786
787	sc->rl_testmode = 0;
788	ifp->if_flags &= ~IFF_PROMISC;
789	re_stop(sc);
790	if (m0 != NULL)
791		m_freem(m0);
792
793	RL_UNLOCK(sc);
794
795	return (error);
796}
797
798/*
799 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
800 * IDs against our list and return a device name if we find a match.
801 */
802static int
803re_probe(dev)
804	device_t		dev;
805{
806	struct rl_type		*t;
807	struct rl_softc		*sc;
808	int			rid;
809	u_int32_t		hwrev;
810
811	t = re_devs;
812	sc = device_get_softc(dev);
813
814	while (t->rl_name != NULL) {
815		if ((pci_get_vendor(dev) == t->rl_vid) &&
816		    (pci_get_device(dev) == t->rl_did)) {
817
818			/*
819			 * Temporarily map the I/O space
820			 * so we can read the chip ID register.
821			 */
822			rid = RL_RID;
823			sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
824			    RF_ACTIVE);
825			if (sc->rl_res == NULL) {
826				device_printf(dev,
827				    "couldn't map ports/memory\n");
828				return (ENXIO);
829			}
830			sc->rl_btag = rman_get_bustag(sc->rl_res);
831			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
832			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
833			bus_release_resource(dev, RL_RES,
834			    RL_RID, sc->rl_res);
835			if (t->rl_basetype == hwrev) {
836				device_set_desc(dev, t->rl_name);
837				return (0);
838			}
839		}
840		t++;
841	}
842
843	return (ENXIO);
844}
845
846/*
847 * This routine takes the segment list provided as the result of
848 * a bus_dma_map_load() operation and assigns the addresses/lengths
849 * to RealTek DMA descriptors. This can be called either by the RX
850 * code or the TX code. In the RX case, we'll probably wind up mapping
851 * at most one segment. For the TX case, there could be any number of
852 * segments since TX packets may span multiple mbufs. In either case,
853 * if the number of segments is larger than the rl_maxsegs limit
854 * specified by the caller, we abort the mapping operation. Sadly,
855 * whoever designed the buffer mapping API did not provide a way to
856 * return an error from here, so we have to fake it a bit.
857 */
858
859static void
860re_dma_map_desc(arg, segs, nseg, mapsize, error)
861	void			*arg;
862	bus_dma_segment_t	*segs;
863	int			nseg;
864	bus_size_t		mapsize;
865	int			error;
866{
867	struct rl_dmaload_arg	*ctx;
868	struct rl_desc		*d = NULL;
869	int			i = 0, idx;
870
871	if (error)
872		return;
873
874	ctx = arg;
875
876	/* Signal error to caller if there's too many segments */
877	if (nseg > ctx->rl_maxsegs) {
878		ctx->rl_maxsegs = 0;
879		return;
880	}
881
882	/*
883	 * Map the segment array into descriptors. Note that we set the
884	 * start-of-frame and end-of-frame markers for either TX or RX, but
885	 * they really only have meaning in the TX case. (In the RX case,
886	 * it's the chip that tells us where packets begin and end.)
887	 * We also keep track of the end of the ring and set the
888	 * end-of-ring bits as needed, and we set the ownership bits
889	 * in all except the very first descriptor. (The caller will
890	 * set this descriptor later when it start transmission or
891	 * reception.)
892	 */
893	idx = ctx->rl_idx;
894	for (;;) {
895		u_int32_t		cmdstat;
896		d = &ctx->rl_ring[idx];
897		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
898			ctx->rl_maxsegs = 0;
899			return;
900		}
901		cmdstat = segs[i].ds_len;
902		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
903		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
904		if (i == 0)
905			cmdstat |= RL_TDESC_CMD_SOF;
906		else
907			cmdstat |= RL_TDESC_CMD_OWN;
908		if (idx == (RL_RX_DESC_CNT - 1))
909			cmdstat |= RL_TDESC_CMD_EOR;
910		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
911		i++;
912		if (i == nseg)
913			break;
914		RL_DESC_INC(idx);
915	}
916
917	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
918	ctx->rl_maxsegs = nseg;
919	ctx->rl_idx = idx;
920}
921
922/*
923 * Map a single buffer address.
924 */
925
926static void
927re_dma_map_addr(arg, segs, nseg, error)
928	void			*arg;
929	bus_dma_segment_t	*segs;
930	int			nseg;
931	int			error;
932{
933	u_int32_t		*addr;
934
935	if (error)
936		return;
937
938	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
939	addr = arg;
940	*addr = segs->ds_addr;
941}
942
943static int
944re_allocmem(dev, sc)
945	device_t		dev;
946	struct rl_softc		*sc;
947{
948	int			error;
949	int			nseg;
950	int			i;
951
952	/*
953	 * Allocate map for RX mbufs.
954	 */
955	nseg = 32;
956	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
957	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
958	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
959	    NULL, NULL, &sc->rl_ldata.rl_mtag);
960	if (error) {
961		device_printf(dev, "could not allocate dma tag\n");
962		return (ENOMEM);
963	}
964
965	/*
966	 * Allocate map for TX descriptor list.
967	 */
968	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
969	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
970	    NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
971	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
972	if (error) {
973		device_printf(dev, "could not allocate dma tag\n");
974		return (ENOMEM);
975	}
976
977	/* Allocate DMA'able memory for the TX ring */
978
979	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
980	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
981	    &sc->rl_ldata.rl_tx_list_map);
982	if (error)
983		return (ENOMEM);
984
985	/* Load the map for the TX ring. */
986
987	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
988	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
989	     RL_TX_LIST_SZ, re_dma_map_addr,
990	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
991
992	/* Create DMA maps for TX buffers */
993
994	for (i = 0; i < RL_TX_DESC_CNT; i++) {
995		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
996			    &sc->rl_ldata.rl_tx_dmamap[i]);
997		if (error) {
998			device_printf(dev, "can't create DMA map for TX\n");
999			return (ENOMEM);
1000		}
1001	}
1002
1003	/*
1004	 * Allocate map for RX descriptor list.
1005	 */
1006	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1007	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1008	    NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, BUS_DMA_ALLOCNOW,
1009	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1010	if (error) {
1011		device_printf(dev, "could not allocate dma tag\n");
1012		return (ENOMEM);
1013	}
1014
1015	/* Allocate DMA'able memory for the RX ring */
1016
1017	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1018	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1019	    &sc->rl_ldata.rl_rx_list_map);
1020	if (error)
1021		return (ENOMEM);
1022
1023	/* Load the map for the RX ring. */
1024
1025	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1026	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1027	     RL_RX_LIST_SZ, re_dma_map_addr,
1028	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1029
1030	/* Create DMA maps for RX buffers */
1031
1032	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1033		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1034			    &sc->rl_ldata.rl_rx_dmamap[i]);
1035		if (error) {
1036			device_printf(dev, "can't create DMA map for RX\n");
1037			return (ENOMEM);
1038		}
1039	}
1040
1041	return (0);
1042}
1043
1044/*
1045 * Attach the interface. Allocate softc structures, do ifmedia
1046 * setup and ethernet/BPF attach.
1047 */
1048static int
1049re_attach(dev)
1050	device_t		dev;
1051{
1052	u_char			eaddr[ETHER_ADDR_LEN];
1053	u_int16_t		as[3];
1054	struct rl_softc		*sc;
1055	struct ifnet		*ifp;
1056	struct rl_hwrev		*hw_rev;
1057	int			hwrev;
1058	u_int16_t		re_did = 0;
1059	int			unit, error = 0, rid, i;
1060
1061	sc = device_get_softc(dev);
1062	unit = device_get_unit(dev);
1063
1064	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1065	    MTX_DEF);
1066	/*
1067	 * Map control/status registers.
1068	 */
1069	pci_enable_busmaster(dev);
1070
1071	rid = RL_RID;
1072	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
1073	    RF_ACTIVE);
1074
1075	if (sc->rl_res == NULL) {
1076		printf ("re%d: couldn't map ports/memory\n", unit);
1077		error = ENXIO;
1078		goto fail;
1079	}
1080
1081	sc->rl_btag = rman_get_bustag(sc->rl_res);
1082	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1083
1084	/* Allocate interrupt */
1085	rid = 0;
1086	sc->rl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1087	    RF_SHAREABLE | RF_ACTIVE);
1088
1089	if (sc->rl_irq == NULL) {
1090		printf("re%d: couldn't map interrupt\n", unit);
1091		error = ENXIO;
1092		goto fail;
1093	}
1094
1095	/* Reset the adapter. */
1096	RL_LOCK(sc);
1097	re_reset(sc);
1098	RL_UNLOCK(sc);
1099
1100	hw_rev = re_hwrevs;
1101	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1102	while (hw_rev->rl_desc != NULL) {
1103		if (hw_rev->rl_rev == hwrev) {
1104			sc->rl_type = hw_rev->rl_type;
1105			break;
1106		}
1107		hw_rev++;
1108	}
1109
1110	if (sc->rl_type == RL_8169) {
1111
1112		/* Set RX length mask */
1113
1114		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1115
1116		/* Force station address autoload from the EEPROM */
1117
1118		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_AUTOLOAD);
1119		for (i = 0; i < RL_TIMEOUT; i++) {
1120			if (!(CSR_READ_1(sc, RL_EECMD) & RL_EEMODE_AUTOLOAD))
1121				break;
1122			DELAY(100);
1123		}
1124		if (i == RL_TIMEOUT)
1125			printf ("re%d: eeprom autoload timed out\n", unit);
1126
1127			for (i = 0; i < ETHER_ADDR_LEN; i++)
1128				eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1129	} else {
1130
1131		/* Set RX length mask */
1132
1133		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1134
1135		sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
1136		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1, 0);
1137		if (re_did != 0x8129)
1138			sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
1139
1140		/*
1141		 * Get station address from the EEPROM.
1142		 */
1143		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3, 0);
1144		for (i = 0; i < 3; i++) {
1145			eaddr[(i * 2) + 0] = as[i] & 0xff;
1146			eaddr[(i * 2) + 1] = as[i] >> 8;
1147		}
1148	}
1149
1150	sc->rl_unit = unit;
1151	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1152
1153	/*
1154	 * Allocate the parent bus DMA tag appropriate for PCI.
1155	 */
1156#define RL_NSEG_NEW 32
1157	error = bus_dma_tag_create(NULL,	/* parent */
1158			1, 0,			/* alignment, boundary */
1159			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1160			BUS_SPACE_MAXADDR,	/* highaddr */
1161			NULL, NULL,		/* filter, filterarg */
1162			MAXBSIZE, RL_NSEG_NEW,	/* maxsize, nsegments */
1163			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1164			BUS_DMA_ALLOCNOW,	/* flags */
1165			NULL, NULL,		/* lockfunc, lockarg */
1166			&sc->rl_parent_tag);
1167	if (error)
1168		goto fail;
1169
1170	error = re_allocmem(dev, sc);
1171
1172	if (error)
1173		goto fail;
1174
1175	/* Do MII setup */
1176	if (mii_phy_probe(dev, &sc->rl_miibus,
1177	    re_ifmedia_upd, re_ifmedia_sts)) {
1178		printf("re%d: MII without any phy!\n", sc->rl_unit);
1179		error = ENXIO;
1180		goto fail;
1181	}
1182
1183	ifp = &sc->arpcom.ac_if;
1184	ifp->if_softc = sc;
1185	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1186	ifp->if_mtu = ETHERMTU;
1187	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1188	ifp->if_ioctl = re_ioctl;
1189	ifp->if_capabilities = IFCAP_VLAN_MTU;
1190	ifp->if_start = re_start;
1191	ifp->if_hwassist = RE_CSUM_FEATURES;
1192	ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING;
1193#ifdef DEVICE_POLLING
1194	ifp->if_capabilities |= IFCAP_POLLING;
1195#endif
1196	ifp->if_watchdog = re_watchdog;
1197	ifp->if_init = re_init;
1198	if (sc->rl_type == RL_8169)
1199		ifp->if_baudrate = 1000000000;
1200	else
1201		ifp->if_baudrate = 100000000;
1202	ifp->if_snd.ifq_maxlen = RL_IFQ_MAXLEN;
1203	ifp->if_capenable = ifp->if_capabilities;
1204
1205	callout_handle_init(&sc->rl_stat_ch);
1206
1207	/*
1208	 * Call MI attach routine.
1209	 */
1210	ether_ifattach(ifp, eaddr);
1211
1212	/* Perform hardware diagnostic. */
1213	error = re_diag(sc);
1214
1215	if (error) {
1216		printf("re%d: attach aborted due to hardware diag failure\n",
1217		    unit);
1218		ether_ifdetach(ifp);
1219		goto fail;
1220	}
1221
1222	/* Hook interrupt last to avoid having to lock softc */
1223	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET | INTR_MPSAFE,
1224	    re_intr, sc, &sc->rl_intrhand);
1225	if (error) {
1226		printf("re%d: couldn't set up irq\n", unit);
1227		ether_ifdetach(ifp);
1228	}
1229
1230fail:
1231	if (error)
1232		re_detach(dev);
1233
1234	return (error);
1235}
1236
1237/*
1238 * Shutdown hardware and free up resources. This can be called any
1239 * time after the mutex has been initialized. It is called in both
1240 * the error case in attach and the normal detach case so it needs
1241 * to be careful about only freeing resources that have actually been
1242 * allocated.
1243 */
1244static int
1245re_detach(dev)
1246	device_t		dev;
1247{
1248	struct rl_softc		*sc;
1249	struct ifnet		*ifp;
1250	int			i;
1251	int			attached;
1252
1253	sc = device_get_softc(dev);
1254	ifp = &sc->arpcom.ac_if;
1255	KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized"));
1256
1257	attached = device_is_attached(dev);
1258	/* These should only be active if attach succeeded */
1259	if (attached)
1260		ether_ifdetach(ifp);
1261
1262	RL_LOCK(sc);
1263#if 0
1264	sc->suspended = 1;
1265#endif
1266
1267	/* These should only be active if attach succeeded */
1268	if (attached) {
1269		re_stop(sc);
1270		/*
1271		 * Force off the IFF_UP flag here, in case someone
1272		 * still had a BPF descriptor attached to this
1273		 * interface. If they do, ether_ifdetach() will cause
1274		 * the BPF code to try and clear the promisc mode
1275		 * flag, which will bubble down to re_ioctl(),
1276		 * which will try to call re_init() again. This will
1277		 * turn the NIC back on and restart the MII ticker,
1278		 * which will panic the system when the kernel tries
1279		 * to invoke the re_tick() function that isn't there
1280		 * anymore.
1281		 */
1282		ifp->if_flags &= ~IFF_UP;
1283		ether_ifdetach(ifp);
1284	}
1285	if (sc->rl_miibus)
1286		device_delete_child(dev, sc->rl_miibus);
1287	bus_generic_detach(dev);
1288
1289	/*
1290	 * The rest is resource deallocation, so we should already be
1291	 * stopped here.
1292	 */
1293	RL_UNLOCK(sc);
1294
1295	if (sc->rl_intrhand)
1296		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1297	if (sc->rl_irq)
1298		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1299	if (sc->rl_res)
1300		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1301
1302
1303	/* Unload and free the RX DMA ring memory and map */
1304
1305	if (sc->rl_ldata.rl_rx_list_tag) {
1306		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1307		    sc->rl_ldata.rl_rx_list_map);
1308		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1309		    sc->rl_ldata.rl_rx_list,
1310		    sc->rl_ldata.rl_rx_list_map);
1311		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1312	}
1313
1314	/* Unload and free the TX DMA ring memory and map */
1315
1316	if (sc->rl_ldata.rl_tx_list_tag) {
1317		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1318		    sc->rl_ldata.rl_tx_list_map);
1319		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1320		    sc->rl_ldata.rl_tx_list,
1321		    sc->rl_ldata.rl_tx_list_map);
1322		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1323	}
1324
1325	/* Destroy all the RX and TX buffer maps */
1326
1327	if (sc->rl_ldata.rl_mtag) {
1328		for (i = 0; i < RL_TX_DESC_CNT; i++)
1329			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1330			    sc->rl_ldata.rl_tx_dmamap[i]);
1331		for (i = 0; i < RL_RX_DESC_CNT; i++)
1332			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1333			    sc->rl_ldata.rl_rx_dmamap[i]);
1334		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1335	}
1336
1337	/* Unload and free the stats buffer and map */
1338
1339	if (sc->rl_ldata.rl_stag) {
1340		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1341		    sc->rl_ldata.rl_rx_list_map);
1342		bus_dmamem_free(sc->rl_ldata.rl_stag,
1343		    sc->rl_ldata.rl_stats,
1344		    sc->rl_ldata.rl_smap);
1345		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1346	}
1347
1348	if (sc->rl_parent_tag)
1349		bus_dma_tag_destroy(sc->rl_parent_tag);
1350
1351	mtx_destroy(&sc->rl_mtx);
1352
1353	return (0);
1354}
1355
1356static int
1357re_newbuf(sc, idx, m)
1358	struct rl_softc		*sc;
1359	int			idx;
1360	struct mbuf		*m;
1361{
1362	struct rl_dmaload_arg	arg;
1363	struct mbuf		*n = NULL;
1364	int			error;
1365
1366	if (m == NULL) {
1367		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1368		if (n == NULL)
1369			return (ENOBUFS);
1370		m = n;
1371	} else
1372		m->m_data = m->m_ext.ext_buf;
1373
1374	/*
1375	 * Initialize mbuf length fields and fixup
1376	 * alignment so that the frame payload is
1377	 * longword aligned.
1378	 */
1379	m->m_len = m->m_pkthdr.len = MCLBYTES;
1380	m_adj(m, ETHER_ALIGN);
1381
1382	arg.sc = sc;
1383	arg.rl_idx = idx;
1384	arg.rl_maxsegs = 1;
1385	arg.rl_flags = 0;
1386	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1387
1388	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1389	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1390	    &arg, BUS_DMA_NOWAIT);
1391	if (error || arg.rl_maxsegs != 1) {
1392		if (n != NULL)
1393			m_freem(n);
1394		return (ENOMEM);
1395	}
1396
1397	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1398	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1399
1400	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1401	    sc->rl_ldata.rl_rx_dmamap[idx],
1402	    BUS_DMASYNC_PREREAD);
1403
1404	return (0);
1405}
1406
1407static int
1408re_tx_list_init(sc)
1409	struct rl_softc		*sc;
1410{
1411
1412	RL_LOCK_ASSERT(sc);
1413
1414	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1415	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1416	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1417
1418	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1419	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1420	sc->rl_ldata.rl_tx_prodidx = 0;
1421	sc->rl_ldata.rl_tx_considx = 0;
1422	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1423
1424	return (0);
1425}
1426
1427static int
1428re_rx_list_init(sc)
1429	struct rl_softc		*sc;
1430{
1431	int			i;
1432
1433	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1434	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1435	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1436
1437	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1438		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1439			return (ENOBUFS);
1440	}
1441
1442	/* Flush the RX descriptors */
1443
1444	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1445	    sc->rl_ldata.rl_rx_list_map,
1446	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1447
1448	sc->rl_ldata.rl_rx_prodidx = 0;
1449	sc->rl_head = sc->rl_tail = NULL;
1450
1451	return (0);
1452}
1453
1454/*
1455 * RX handler for C+ and 8169. For the gigE chips, we support
1456 * the reception of jumbo frames that have been fragmented
1457 * across multiple 2K mbuf cluster buffers.
1458 */
1459static void
1460re_rxeof(sc)
1461	struct rl_softc		*sc;
1462{
1463	struct mbuf		*m;
1464	struct ifnet		*ifp;
1465	int			i, total_len;
1466	struct rl_desc		*cur_rx;
1467	u_int32_t		rxstat, rxvlan;
1468
1469	RL_LOCK_ASSERT(sc);
1470
1471	ifp = &sc->arpcom.ac_if;
1472	i = sc->rl_ldata.rl_rx_prodidx;
1473
1474	/* Invalidate the descriptor memory */
1475
1476	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1477	    sc->rl_ldata.rl_rx_list_map,
1478	    BUS_DMASYNC_POSTREAD);
1479
1480	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i])) {
1481
1482		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1483		m = sc->rl_ldata.rl_rx_mbuf[i];
1484		total_len = RL_RXBYTES(cur_rx);
1485		rxstat = le32toh(cur_rx->rl_cmdstat);
1486		rxvlan = le32toh(cur_rx->rl_vlanctl);
1487
1488		/* Invalidate the RX mbuf and unload its map */
1489
1490		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1491		    sc->rl_ldata.rl_rx_dmamap[i],
1492		    BUS_DMASYNC_POSTWRITE);
1493		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1494		    sc->rl_ldata.rl_rx_dmamap[i]);
1495
1496		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1497			m->m_len = MCLBYTES - ETHER_ALIGN;
1498			if (sc->rl_head == NULL)
1499				sc->rl_head = sc->rl_tail = m;
1500			else {
1501				m->m_flags &= ~M_PKTHDR;
1502				sc->rl_tail->m_next = m;
1503				sc->rl_tail = m;
1504			}
1505			re_newbuf(sc, i, NULL);
1506			RL_DESC_INC(i);
1507			continue;
1508		}
1509
1510		/*
1511		 * NOTE: for the 8139C+, the frame length field
1512		 * is always 12 bits in size, but for the gigE chips,
1513		 * it is 13 bits (since the max RX frame length is 16K).
1514		 * Unfortunately, all 32 bits in the status word
1515		 * were already used, so to make room for the extra
1516		 * length bit, RealTek took out the 'frame alignment
1517		 * error' bit and shifted the other status bits
1518		 * over one slot. The OWN, EOR, FS and LS bits are
1519		 * still in the same places. We have already extracted
1520		 * the frame length and checked the OWN bit, so rather
1521		 * than using an alternate bit mapping, we shift the
1522		 * status bits one space to the right so we can evaluate
1523		 * them using the 8169 status as though it was in the
1524		 * same format as that of the 8139C+.
1525		 */
1526		if (sc->rl_type == RL_8169)
1527			rxstat >>= 1;
1528
1529		if (rxstat & RL_RDESC_STAT_RXERRSUM) {
1530			ifp->if_ierrors++;
1531			/*
1532			 * If this is part of a multi-fragment packet,
1533			 * discard all the pieces.
1534			 */
1535			if (sc->rl_head != NULL) {
1536				m_freem(sc->rl_head);
1537				sc->rl_head = sc->rl_tail = NULL;
1538			}
1539			re_newbuf(sc, i, m);
1540			RL_DESC_INC(i);
1541			continue;
1542		}
1543
1544		/*
1545		 * If allocating a replacement mbuf fails,
1546		 * reload the current one.
1547		 */
1548
1549		if (re_newbuf(sc, i, NULL)) {
1550			ifp->if_ierrors++;
1551			if (sc->rl_head != NULL) {
1552				m_freem(sc->rl_head);
1553				sc->rl_head = sc->rl_tail = NULL;
1554			}
1555			re_newbuf(sc, i, m);
1556			RL_DESC_INC(i);
1557			continue;
1558		}
1559
1560		RL_DESC_INC(i);
1561
1562		if (sc->rl_head != NULL) {
1563			m->m_len = total_len % (MCLBYTES - ETHER_ALIGN);
1564			/*
1565			 * Special case: if there's 4 bytes or less
1566			 * in this buffer, the mbuf can be discarded:
1567			 * the last 4 bytes is the CRC, which we don't
1568			 * care about anyway.
1569			 */
1570			if (m->m_len <= ETHER_CRC_LEN) {
1571				sc->rl_tail->m_len -=
1572				    (ETHER_CRC_LEN - m->m_len);
1573				m_freem(m);
1574			} else {
1575				m->m_len -= ETHER_CRC_LEN;
1576				m->m_flags &= ~M_PKTHDR;
1577				sc->rl_tail->m_next = m;
1578			}
1579			m = sc->rl_head;
1580			sc->rl_head = sc->rl_tail = NULL;
1581			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1582		} else
1583			m->m_pkthdr.len = m->m_len =
1584			    (total_len - ETHER_CRC_LEN);
1585
1586		ifp->if_ipackets++;
1587		m->m_pkthdr.rcvif = ifp;
1588
1589		/* Do RX checksumming if enabled */
1590
1591		if (ifp->if_capenable & IFCAP_RXCSUM) {
1592
1593			/* Check IP header checksum */
1594			if (rxstat & RL_RDESC_STAT_PROTOID)
1595				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1596			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1597				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1598
1599			/* Check TCP/UDP checksum */
1600			if ((RL_TCPPKT(rxstat) &&
1601			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1602			    (RL_UDPPKT(rxstat) &&
1603			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1604				m->m_pkthdr.csum_flags |=
1605				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1606				m->m_pkthdr.csum_data = 0xffff;
1607			}
1608		}
1609
1610		if (rxvlan & RL_RDESC_VLANCTL_TAG)
1611			VLAN_INPUT_TAG(ifp, m,
1612			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)), continue);
1613		RL_UNLOCK(sc);
1614		(*ifp->if_input)(ifp, m);
1615		RL_LOCK(sc);
1616	}
1617
1618	/* Flush the RX DMA ring */
1619
1620	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1621	    sc->rl_ldata.rl_rx_list_map,
1622	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1623
1624	sc->rl_ldata.rl_rx_prodidx = i;
1625}
1626
1627static void
1628re_txeof(sc)
1629	struct rl_softc		*sc;
1630{
1631	struct ifnet		*ifp;
1632	u_int32_t		txstat;
1633	int			idx;
1634
1635	ifp = &sc->arpcom.ac_if;
1636	idx = sc->rl_ldata.rl_tx_considx;
1637
1638	/* Invalidate the TX descriptor list */
1639
1640	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1641	    sc->rl_ldata.rl_tx_list_map,
1642	    BUS_DMASYNC_POSTREAD);
1643
1644	while (idx != sc->rl_ldata.rl_tx_prodidx) {
1645
1646		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1647		if (txstat & RL_TDESC_CMD_OWN)
1648			break;
1649
1650		/*
1651		 * We only stash mbufs in the last descriptor
1652		 * in a fragment chain, which also happens to
1653		 * be the only place where the TX status bits
1654		 * are valid.
1655		 */
1656
1657		if (txstat & RL_TDESC_CMD_EOF) {
1658			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1659			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1660			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1661			    sc->rl_ldata.rl_tx_dmamap[idx]);
1662			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1663			    RL_TDESC_STAT_COLCNT))
1664				ifp->if_collisions++;
1665			if (txstat & RL_TDESC_STAT_TXERRSUM)
1666				ifp->if_oerrors++;
1667			else
1668				ifp->if_opackets++;
1669		}
1670		sc->rl_ldata.rl_tx_free++;
1671		RL_DESC_INC(idx);
1672	}
1673
1674	/* No changes made to the TX ring, so no flush needed */
1675
1676	if (idx != sc->rl_ldata.rl_tx_considx) {
1677		sc->rl_ldata.rl_tx_considx = idx;
1678		ifp->if_flags &= ~IFF_OACTIVE;
1679		ifp->if_timer = 0;
1680	}
1681
1682	/*
1683	 * If not all descriptors have been released reaped yet,
1684	 * reload the timer so that we will eventually get another
1685	 * interrupt that will cause us to re-enter this routine.
1686	 * This is done in case the transmitter has gone idle.
1687	 */
1688	if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
1689		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1690}
1691
1692static void
1693re_tick(xsc)
1694	void			*xsc;
1695{
1696	struct rl_softc		*sc;
1697
1698	sc = xsc;
1699	RL_LOCK(sc);
1700	re_tick_locked(sc);
1701	RL_UNLOCK(sc);
1702}
1703
1704static void
1705re_tick_locked(sc)
1706	struct rl_softc		*sc;
1707{
1708	struct mii_data		*mii;
1709
1710	RL_LOCK_ASSERT(sc);
1711
1712	mii = device_get_softc(sc->rl_miibus);
1713
1714	mii_tick(mii);
1715
1716	sc->rl_stat_ch = timeout(re_tick, sc, hz);
1717}
1718
1719#ifdef DEVICE_POLLING
1720static void
1721re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1722{
1723	struct rl_softc *sc = ifp->if_softc;
1724
1725	RL_LOCK(sc);
1726	re_poll_locked(ifp, cmd, count);
1727	RL_UNLOCK(sc);
1728}
1729
1730static void
1731re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
1732{
1733	struct rl_softc *sc = ifp->if_softc;
1734
1735	RL_LOCK_ASSERT(sc);
1736
1737	if (!(ifp->if_capenable & IFCAP_POLLING)) {
1738		ether_poll_deregister(ifp);
1739		cmd = POLL_DEREGISTER;
1740	}
1741	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1742		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
1743		return;
1744	}
1745
1746	sc->rxcycles = count;
1747	re_rxeof(sc);
1748	re_txeof(sc);
1749
1750	if (ifp->if_snd.ifq_head != NULL)
1751		re_start_locked(ifp);
1752
1753	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1754		u_int16_t       status;
1755
1756		status = CSR_READ_2(sc, RL_ISR);
1757		if (status == 0xffff)
1758			return;
1759		if (status)
1760			CSR_WRITE_2(sc, RL_ISR, status);
1761
1762		/*
1763		 * XXX check behaviour on receiver stalls.
1764		 */
1765
1766		if (status & RL_ISR_SYSTEM_ERR) {
1767			re_reset(sc);
1768			re_init_locked(sc);
1769		}
1770	}
1771}
1772#endif /* DEVICE_POLLING */
1773
1774static void
1775re_intr(arg)
1776	void			*arg;
1777{
1778	struct rl_softc		*sc;
1779	struct ifnet		*ifp;
1780	u_int16_t		status;
1781
1782	sc = arg;
1783
1784	RL_LOCK(sc);
1785
1786	ifp = &sc->arpcom.ac_if;
1787
1788	if (sc->suspended || !(ifp->if_flags & IFF_UP))
1789		goto done_locked;
1790
1791#ifdef DEVICE_POLLING
1792	if  (ifp->if_flags & IFF_POLLING)
1793		goto done_locked;
1794	if ((ifp->if_capenable & IFCAP_POLLING) &&
1795	    ether_poll_register(re_poll, ifp)) { /* ok, disable interrupts */
1796		CSR_WRITE_2(sc, RL_IMR, 0x0000);
1797		re_poll_locked(ifp, 0, 1);
1798		goto done_locked;
1799	}
1800#endif /* DEVICE_POLLING */
1801
1802	for (;;) {
1803
1804		status = CSR_READ_2(sc, RL_ISR);
1805		/* If the card has gone away the read returns 0xffff. */
1806		if (status == 0xffff)
1807			break;
1808		if (status)
1809			CSR_WRITE_2(sc, RL_ISR, status);
1810
1811		if ((status & RL_INTRS_CPLUS) == 0)
1812			break;
1813
1814		if ((status & RL_ISR_RX_OK) ||
1815		    (status & RL_ISR_RX_ERR))
1816			re_rxeof(sc);
1817
1818		if ((status & RL_ISR_TIMEOUT_EXPIRED) ||
1819		    (status & RL_ISR_TX_ERR) ||
1820		    (status & RL_ISR_TX_DESC_UNAVAIL))
1821			re_txeof(sc);
1822
1823		if (status & RL_ISR_SYSTEM_ERR) {
1824			re_reset(sc);
1825			re_init_locked(sc);
1826		}
1827
1828		if (status & RL_ISR_LINKCHG) {
1829			untimeout(re_tick, sc, sc->rl_stat_ch);
1830			re_tick_locked(sc);
1831		}
1832	}
1833
1834	if (ifp->if_snd.ifq_head != NULL)
1835		re_start_locked(ifp);
1836
1837done_locked:
1838	RL_UNLOCK(sc);
1839}
1840
1841static int
1842re_encap(sc, m_head, idx)
1843	struct rl_softc		*sc;
1844	struct mbuf		**m_head;
1845	int			*idx;
1846{
1847	struct mbuf		*m_new = NULL;
1848	struct rl_dmaload_arg	arg;
1849	bus_dmamap_t		map;
1850	int			error;
1851	struct m_tag		*mtag;
1852
1853	RL_LOCK_ASSERT(sc);
1854
1855	if (sc->rl_ldata.rl_tx_free <= 4)
1856		return (EFBIG);
1857
1858	/*
1859	 * Set up checksum offload. Note: checksum offload bits must
1860	 * appear in all descriptors of a multi-descriptor transmit
1861	 * attempt. (This is according to testing done with an 8169
1862	 * chip. I'm not sure if this is a requirement or a bug.)
1863	 */
1864
1865	arg.rl_flags = 0;
1866
1867	if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
1868		arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
1869	if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
1870		arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
1871	if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
1872		arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
1873
1874	arg.sc = sc;
1875	arg.rl_idx = *idx;
1876	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
1877	if (arg.rl_maxsegs > 4)
1878		arg.rl_maxsegs -= 4;
1879	arg.rl_ring = sc->rl_ldata.rl_tx_list;
1880
1881	map = sc->rl_ldata.rl_tx_dmamap[*idx];
1882	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
1883	    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1884
1885	if (error && error != EFBIG) {
1886		printf("re%d: can't map mbuf (error %d)\n", sc->rl_unit, error);
1887		return (ENOBUFS);
1888	}
1889
1890	/* Too many segments to map, coalesce into a single mbuf */
1891
1892	if (error || arg.rl_maxsegs == 0) {
1893		m_new = m_defrag(*m_head, M_DONTWAIT);
1894		if (m_new == NULL)
1895			return (ENOBUFS);
1896		else
1897			*m_head = m_new;
1898
1899		arg.sc = sc;
1900		arg.rl_idx = *idx;
1901		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
1902		arg.rl_ring = sc->rl_ldata.rl_tx_list;
1903
1904		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
1905		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1906		if (error) {
1907			printf("re%d: can't map mbuf (error %d)\n",
1908			    sc->rl_unit, error);
1909			return (EFBIG);
1910		}
1911	}
1912
1913	/*
1914	 * Insure that the map for this transmission
1915	 * is placed at the array index of the last descriptor
1916	 * in this chain.
1917	 */
1918	sc->rl_ldata.rl_tx_dmamap[*idx] =
1919	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
1920	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
1921
1922	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head;
1923	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
1924
1925	/*
1926	 * Set up hardware VLAN tagging. Note: vlan tag info must
1927	 * appear in the first descriptor of a multi-descriptor
1928	 * transmission attempt.
1929	 */
1930
1931	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, *m_head);
1932	if (mtag != NULL)
1933		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
1934		    htole32(htons(VLAN_TAG_VALUE(mtag)) | RL_TDESC_VLANCTL_TAG);
1935
1936	/* Transfer ownership of packet to the chip. */
1937
1938	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
1939	    htole32(RL_TDESC_CMD_OWN);
1940	if (*idx != arg.rl_idx)
1941		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
1942		    htole32(RL_TDESC_CMD_OWN);
1943
1944	RL_DESC_INC(arg.rl_idx);
1945	*idx = arg.rl_idx;
1946
1947	return (0);
1948}
1949
1950static void
1951re_start(ifp)
1952	struct ifnet		*ifp;
1953{
1954	struct rl_softc		*sc;
1955
1956	sc = ifp->if_softc;
1957	RL_LOCK(sc);
1958	re_start_locked(ifp);
1959	RL_UNLOCK(sc);
1960}
1961
1962/*
1963 * Main transmit routine for C+ and gigE NICs.
1964 */
1965static void
1966re_start_locked(ifp)
1967	struct ifnet		*ifp;
1968{
1969	struct rl_softc		*sc;
1970	struct mbuf		*m_head = NULL;
1971	int			idx;
1972
1973	sc = ifp->if_softc;
1974
1975	RL_LOCK_ASSERT(sc);
1976
1977	idx = sc->rl_ldata.rl_tx_prodidx;
1978
1979	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
1980		IF_DEQUEUE(&ifp->if_snd, m_head);
1981		if (m_head == NULL)
1982			break;
1983
1984		if (re_encap(sc, &m_head, &idx)) {
1985			IF_PREPEND(&ifp->if_snd, m_head);
1986			ifp->if_flags |= IFF_OACTIVE;
1987			break;
1988		}
1989
1990		/*
1991		 * If there's a BPF listener, bounce a copy of this frame
1992		 * to him.
1993		 */
1994		BPF_MTAP(ifp, m_head);
1995	}
1996
1997	/* Flush the TX descriptors */
1998
1999	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2000	    sc->rl_ldata.rl_tx_list_map,
2001	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2002
2003	sc->rl_ldata.rl_tx_prodidx = idx;
2004
2005	/*
2006	 * RealTek put the TX poll request register in a different
2007	 * location on the 8169 gigE chip. I don't know why.
2008	 */
2009
2010	if (sc->rl_type == RL_8169)
2011		CSR_WRITE_2(sc, RL_GTXSTART, RL_TXSTART_START);
2012	else
2013		CSR_WRITE_2(sc, RL_TXSTART, RL_TXSTART_START);
2014
2015	/*
2016	 * Use the countdown timer for interrupt moderation.
2017	 * 'TX done' interrupts are disabled. Instead, we reset the
2018	 * countdown timer, which will begin counting until it hits
2019	 * the value in the TIMERINT register, and then trigger an
2020	 * interrupt. Each time we write to the TIMERCNT register,
2021	 * the timer count is reset to 0.
2022	 */
2023	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2024
2025	/*
2026	 * Set a timeout in case the chip goes out to lunch.
2027	 */
2028	ifp->if_timer = 5;
2029}
2030
2031static void
2032re_init(xsc)
2033	void			*xsc;
2034{
2035	struct rl_softc		*sc = xsc;
2036
2037	RL_LOCK(sc);
2038	re_init_locked(sc);
2039	RL_UNLOCK(sc);
2040}
2041
2042static void
2043re_init_locked(sc)
2044	struct rl_softc		*sc;
2045{
2046	struct ifnet		*ifp = &sc->arpcom.ac_if;
2047	struct mii_data		*mii;
2048	u_int32_t		rxcfg = 0;
2049
2050	RL_LOCK_ASSERT(sc);
2051
2052	mii = device_get_softc(sc->rl_miibus);
2053
2054	/*
2055	 * Cancel pending I/O and free all RX/TX buffers.
2056	 */
2057	re_stop(sc);
2058
2059	/*
2060	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2061	 * RX checksum offload. We must configure the C+ register
2062	 * before all others.
2063	 */
2064	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2065	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2066	    RL_CPLUSCMD_VLANSTRIP|
2067	    (ifp->if_capenable & IFCAP_RXCSUM ?
2068	    RL_CPLUSCMD_RXCSUM_ENB : 0));
2069
2070	/*
2071	 * Init our MAC address.  Even though the chipset
2072	 * documentation doesn't mention it, we need to enter "Config
2073	 * register write enable" mode to modify the ID registers.
2074	 */
2075	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2076	CSR_WRITE_STREAM_4(sc, RL_IDR0,
2077	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
2078	CSR_WRITE_STREAM_4(sc, RL_IDR4,
2079	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
2080	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2081
2082	/*
2083	 * For C+ mode, initialize the RX descriptors and mbufs.
2084	 */
2085	re_rx_list_init(sc);
2086	re_tx_list_init(sc);
2087
2088	/*
2089	 * Enable transmit and receive.
2090	 */
2091	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2092
2093	/*
2094	 * Set the initial TX and RX configuration.
2095	 */
2096	if (sc->rl_testmode) {
2097		if (sc->rl_type == RL_8169)
2098			CSR_WRITE_4(sc, RL_TXCFG,
2099			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2100		else
2101			CSR_WRITE_4(sc, RL_TXCFG,
2102			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2103	} else
2104		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2105	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2106
2107	/* Set the individual bit to receive frames for this host only. */
2108	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2109	rxcfg |= RL_RXCFG_RX_INDIV;
2110
2111	/* If we want promiscuous mode, set the allframes bit. */
2112	if (ifp->if_flags & IFF_PROMISC)
2113		rxcfg |= RL_RXCFG_RX_ALLPHYS;
2114	else
2115		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2116	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2117
2118	/*
2119	 * Set capture broadcast bit to capture broadcast frames.
2120	 */
2121	if (ifp->if_flags & IFF_BROADCAST)
2122		rxcfg |= RL_RXCFG_RX_BROAD;
2123	else
2124		rxcfg &= ~RL_RXCFG_RX_BROAD;
2125	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2126
2127	/*
2128	 * Program the multicast filter, if necessary.
2129	 */
2130	re_setmulti(sc);
2131
2132#ifdef DEVICE_POLLING
2133	/*
2134	 * Disable interrupts if we are polling.
2135	 */
2136	if (ifp->if_flags & IFF_POLLING)
2137		CSR_WRITE_2(sc, RL_IMR, 0);
2138	else	/* otherwise ... */
2139#endif /* DEVICE_POLLING */
2140	/*
2141	 * Enable interrupts.
2142	 */
2143	if (sc->rl_testmode)
2144		CSR_WRITE_2(sc, RL_IMR, 0);
2145	else
2146		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2147
2148	/* Set initial TX threshold */
2149	sc->rl_txthresh = RL_TX_THRESH_INIT;
2150
2151	/* Start RX/TX process. */
2152	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2153#ifdef notdef
2154	/* Enable receiver and transmitter. */
2155	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2156#endif
2157	/*
2158	 * Load the addresses of the RX and TX lists into the chip.
2159	 */
2160
2161	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2162	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2163	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2164	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2165
2166	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2167	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2168	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2169	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2170
2171	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2172
2173	/*
2174	 * Initialize the timer interrupt register so that
2175	 * a timer interrupt will be generated once the timer
2176	 * reaches a certain number of ticks. The timer is
2177	 * reloaded on each transmit. This gives us TX interrupt
2178	 * moderation, which dramatically improves TX frame rate.
2179	 */
2180
2181	if (sc->rl_type == RL_8169)
2182		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2183	else
2184		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2185
2186	/*
2187	 * For 8169 gigE NICs, set the max allowed RX packet
2188	 * size so we can receive jumbo frames.
2189	 */
2190	if (sc->rl_type == RL_8169)
2191		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2192
2193	if (sc->rl_testmode)
2194		return;
2195
2196	mii_mediachg(mii);
2197
2198	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
2199
2200	ifp->if_flags |= IFF_RUNNING;
2201	ifp->if_flags &= ~IFF_OACTIVE;
2202
2203	sc->rl_stat_ch = timeout(re_tick, sc, hz);
2204}
2205
2206/*
2207 * Set media options.
2208 */
2209static int
2210re_ifmedia_upd(ifp)
2211	struct ifnet		*ifp;
2212{
2213	struct rl_softc		*sc;
2214	struct mii_data		*mii;
2215
2216	sc = ifp->if_softc;
2217	mii = device_get_softc(sc->rl_miibus);
2218	mii_mediachg(mii);
2219
2220	return (0);
2221}
2222
2223/*
2224 * Report current media status.
2225 */
2226static void
2227re_ifmedia_sts(ifp, ifmr)
2228	struct ifnet		*ifp;
2229	struct ifmediareq	*ifmr;
2230{
2231	struct rl_softc		*sc;
2232	struct mii_data		*mii;
2233
2234	sc = ifp->if_softc;
2235	mii = device_get_softc(sc->rl_miibus);
2236
2237	mii_pollstat(mii);
2238	ifmr->ifm_active = mii->mii_media_active;
2239	ifmr->ifm_status = mii->mii_media_status;
2240}
2241
2242static int
2243re_ioctl(ifp, command, data)
2244	struct ifnet		*ifp;
2245	u_long			command;
2246	caddr_t			data;
2247{
2248	struct rl_softc		*sc = ifp->if_softc;
2249	struct ifreq		*ifr = (struct ifreq *) data;
2250	struct mii_data		*mii;
2251	int			error = 0;
2252
2253	switch (command) {
2254	case SIOCSIFMTU:
2255		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2256			error = EINVAL;
2257		ifp->if_mtu = ifr->ifr_mtu;
2258		break;
2259	case SIOCSIFFLAGS:
2260		RL_LOCK(sc);
2261		if (ifp->if_flags & IFF_UP)
2262			re_init_locked(sc);
2263		else if (ifp->if_flags & IFF_RUNNING)
2264			re_stop(sc);
2265		RL_UNLOCK(sc);
2266		error = 0;
2267		break;
2268	case SIOCADDMULTI:
2269	case SIOCDELMULTI:
2270		RL_LOCK(sc);
2271		re_setmulti(sc);
2272		RL_UNLOCK(sc);
2273		error = 0;
2274		break;
2275	case SIOCGIFMEDIA:
2276	case SIOCSIFMEDIA:
2277		mii = device_get_softc(sc->rl_miibus);
2278		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2279		break;
2280	case SIOCSIFCAP:
2281		ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_POLLING);
2282		ifp->if_capenable |=
2283		    ifr->ifr_reqcap & (IFCAP_HWCSUM | IFCAP_POLLING);
2284		if (ifp->if_capenable & IFCAP_TXCSUM)
2285			ifp->if_hwassist = RE_CSUM_FEATURES;
2286		else
2287			ifp->if_hwassist = 0;
2288		if (ifp->if_flags & IFF_RUNNING)
2289			re_init(sc);
2290		break;
2291	default:
2292		error = ether_ioctl(ifp, command, data);
2293		break;
2294	}
2295
2296	return (error);
2297}
2298
2299static void
2300re_watchdog(ifp)
2301	struct ifnet		*ifp;
2302{
2303	struct rl_softc		*sc;
2304
2305	sc = ifp->if_softc;
2306	RL_LOCK(sc);
2307	printf("re%d: watchdog timeout\n", sc->rl_unit);
2308	ifp->if_oerrors++;
2309
2310	re_txeof(sc);
2311	re_rxeof(sc);
2312	re_init_locked(sc);
2313
2314	RL_UNLOCK(sc);
2315}
2316
2317/*
2318 * Stop the adapter and free any mbufs allocated to the
2319 * RX and TX lists.
2320 */
2321static void
2322re_stop(sc)
2323	struct rl_softc		*sc;
2324{
2325	register int		i;
2326	struct ifnet		*ifp;
2327
2328	RL_LOCK_ASSERT(sc);
2329
2330	ifp = &sc->arpcom.ac_if;
2331	ifp->if_timer = 0;
2332
2333	untimeout(re_tick, sc, sc->rl_stat_ch);
2334	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2335#ifdef DEVICE_POLLING
2336	ether_poll_deregister(ifp);
2337#endif /* DEVICE_POLLING */
2338
2339	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2340	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2341
2342	if (sc->rl_head != NULL) {
2343		m_freem(sc->rl_head);
2344		sc->rl_head = sc->rl_tail = NULL;
2345	}
2346
2347	/* Free the TX list buffers. */
2348
2349	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2350		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2351			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2352			    sc->rl_ldata.rl_tx_dmamap[i]);
2353			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2354			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2355		}
2356	}
2357
2358	/* Free the RX list buffers. */
2359
2360	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2361		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2362			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2363			    sc->rl_ldata.rl_rx_dmamap[i]);
2364			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2365			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2366		}
2367	}
2368}
2369
2370/*
2371 * Device suspend routine.  Stop the interface and save some PCI
2372 * settings in case the BIOS doesn't restore them properly on
2373 * resume.
2374 */
2375static int
2376re_suspend(dev)
2377	device_t		dev;
2378{
2379	struct rl_softc		*sc;
2380
2381	sc = device_get_softc(dev);
2382
2383	RL_LOCK(sc);
2384	re_stop(sc);
2385	sc->suspended = 1;
2386	RL_UNLOCK(sc);
2387
2388	return (0);
2389}
2390
2391/*
2392 * Device resume routine.  Restore some PCI settings in case the BIOS
2393 * doesn't, re-enable busmastering, and restart the interface if
2394 * appropriate.
2395 */
2396static int
2397re_resume(dev)
2398	device_t		dev;
2399{
2400	struct rl_softc		*sc;
2401	struct ifnet		*ifp;
2402
2403	sc = device_get_softc(dev);
2404
2405	RL_LOCK(sc);
2406
2407	ifp = &sc->arpcom.ac_if;
2408
2409	/* reinitialize interface if necessary */
2410	if (ifp->if_flags & IFF_UP)
2411		re_init_locked(sc);
2412
2413	sc->suspended = 0;
2414	RL_UNLOCK(sc);
2415
2416	return (0);
2417}
2418
2419/*
2420 * Stop all chip I/O so that the kernel's probe routines don't
2421 * get confused by errant DMAs when rebooting.
2422 */
2423static void
2424re_shutdown(dev)
2425	device_t		dev;
2426{
2427	struct rl_softc		*sc;
2428
2429	sc = device_get_softc(dev);
2430
2431	RL_LOCK(sc);
2432	re_stop(sc);
2433	RL_UNLOCK(sc);
2434}
2435