if_rl.c revision 72084
1/*
2 * Copyright (c) 1997, 1998
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  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 * $FreeBSD: head/sys/pci/if_rl.c 72084 2001-02-06 10:12:15Z phk $
33 */
34
35/*
36 * RealTek 8129/8139 PCI NIC driver
37 *
38 * Supports several extremely cheap PCI 10/100 adapters based on
39 * the RealTek chipset. Datasheets can be obtained from
40 * www.realtek.com.tw.
41 *
42 * Written by Bill Paul <wpaul@ctr.columbia.edu>
43 * Electrical Engineering Department
44 * Columbia University, New York City
45 */
46
47/*
48 * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
49 * probably the worst PCI ethernet controller ever made, with the possible
50 * exception of the FEAST chip made by SMC. The 8139 supports bus-master
51 * DMA, but it has a terrible interface that nullifies any performance
52 * gains that bus-master DMA usually offers.
53 *
54 * For transmission, the chip offers a series of four TX descriptor
55 * registers. Each transmit frame must be in a contiguous buffer, aligned
56 * on a longword (32-bit) boundary. This means we almost always have to
57 * do mbuf copies in order to transmit a frame, except in the unlikely
58 * case where a) the packet fits into a single mbuf, and b) the packet
59 * is 32-bit aligned within the mbuf's data area. The presence of only
60 * four descriptor registers means that we can never have more than four
61 * packets queued for transmission at any one time.
62 *
63 * Reception is not much better. The driver has to allocate a single large
64 * buffer area (up to 64K in size) into which the chip will DMA received
65 * frames. Because we don't know where within this region received packets
66 * will begin or end, we have no choice but to copy data from the buffer
67 * area into mbufs in order to pass the packets up to the higher protocol
68 * levels.
69 *
70 * It's impossible given this rotten design to really achieve decent
71 * performance at 100Mbps, unless you happen to have a 400Mhz PII or
72 * some equally overmuscled CPU to drive it.
73 *
74 * On the bright side, the 8139 does have a built-in PHY, although
75 * rather than using an MDIO serial interface like most other NICs, the
76 * PHY registers are directly accessible through the 8139's register
77 * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
78 * filter.
79 *
80 * The 8129 chip is an older version of the 8139 that uses an external PHY
81 * chip. The 8129 has a serial MDIO interface for accessing the MII where
82 * the 8139 lets you directly access the on-board PHY registers. We need
83 * to select which interface to use depending on the chip type.
84 */
85
86#include <sys/param.h>
87#include <sys/systm.h>
88#include <sys/sockio.h>
89#include <sys/mbuf.h>
90#include <sys/malloc.h>
91#include <sys/kernel.h>
92#include <sys/socket.h>
93
94#include <net/if.h>
95#include <net/if_arp.h>
96#include <net/ethernet.h>
97#include <net/if_dl.h>
98#include <net/if_media.h>
99
100#include <net/bpf.h>
101
102#include <vm/vm.h>              /* for vtophys */
103#include <vm/pmap.h>            /* for vtophys */
104#include <machine/bus_pio.h>
105#include <machine/bus_memio.h>
106#include <machine/bus.h>
107#include <machine/resource.h>
108#include <sys/bus.h>
109#include <sys/rman.h>
110
111#include <dev/mii/mii.h>
112#include <dev/mii/miivar.h>
113
114#include <pci/pcireg.h>
115#include <pci/pcivar.h>
116
117MODULE_DEPEND(rl, miibus, 1, 1, 1);
118
119/* "controller miibus0" required.  See GENERIC if you get errors here. */
120#include "miibus_if.h"
121
122/*
123 * Default to using PIO access for this driver. On SMP systems,
124 * there appear to be problems with memory mapped mode: it looks like
125 * doing too many memory mapped access back to back in rapid succession
126 * can hang the bus. I'm inclined to blame this on crummy design/construction
127 * on the part of RealTek. Memory mapped mode does appear to work on
128 * uniprocessor systems though.
129 */
130#define RL_USEIOSPACE
131
132#include <pci/if_rlreg.h>
133
134#ifndef lint
135static const char rcsid[] =
136  "$FreeBSD: head/sys/pci/if_rl.c 72084 2001-02-06 10:12:15Z phk $";
137#endif
138
139/*
140 * Various supported device vendors/types and their names.
141 */
142static struct rl_type rl_devs[] = {
143	{ RT_VENDORID, RT_DEVICEID_8129,
144		"RealTek 8129 10/100BaseTX" },
145	{ RT_VENDORID, RT_DEVICEID_8139,
146		"RealTek 8139 10/100BaseTX" },
147	{ RT_VENDORID, RT_DEVICEID_8138,
148		"RealTek 8139 10/100BaseTX CardBus" },
149	{ ACCTON_VENDORID, ACCTON_DEVICEID_5030,
150		"Accton MPX 5030/5038 10/100BaseTX" },
151	{ DELTA_VENDORID, DELTA_DEVICEID_8139,
152		"Delta Electronics 8139 10/100BaseTX" },
153	{ ADDTRON_VENDORID, ADDTRON_DEVICEID_8139,
154		"Addtron Technolgy 8139 10/100BaseTX" },
155	{ 0, 0, NULL }
156};
157
158static int rl_probe		__P((device_t));
159static int rl_attach		__P((device_t));
160static int rl_detach		__P((device_t));
161
162static int rl_encap		__P((struct rl_softc *, struct mbuf * ));
163
164static void rl_rxeof		__P((struct rl_softc *));
165static void rl_txeof		__P((struct rl_softc *));
166static void rl_intr		__P((void *));
167static void rl_tick		__P((void *));
168static void rl_start		__P((struct ifnet *));
169static int rl_ioctl		__P((struct ifnet *, u_long, caddr_t));
170static void rl_init		__P((void *));
171static void rl_stop		__P((struct rl_softc *));
172static void rl_watchdog		__P((struct ifnet *));
173static void rl_shutdown		__P((device_t));
174static int rl_ifmedia_upd	__P((struct ifnet *));
175static void rl_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
176
177static void rl_eeprom_putbyte	__P((struct rl_softc *, int));
178static void rl_eeprom_getword	__P((struct rl_softc *, int, u_int16_t *));
179static void rl_read_eeprom	__P((struct rl_softc *, caddr_t,
180					int, int, int));
181static void rl_mii_sync		__P((struct rl_softc *));
182static void rl_mii_send		__P((struct rl_softc *, u_int32_t, int));
183static int rl_mii_readreg	__P((struct rl_softc *, struct rl_mii_frame *));
184static int rl_mii_writereg	__P((struct rl_softc *, struct rl_mii_frame *));
185
186static int rl_miibus_readreg	__P((device_t, int, int));
187static int rl_miibus_writereg	__P((device_t, int, int, int));
188static void rl_miibus_statchg	__P((device_t));
189
190static u_int8_t rl_calchash	__P((caddr_t));
191static void rl_setmulti		__P((struct rl_softc *));
192static void rl_reset		__P((struct rl_softc *));
193static int rl_list_tx_init	__P((struct rl_softc *));
194
195#ifdef RL_USEIOSPACE
196#define RL_RES			SYS_RES_IOPORT
197#define RL_RID			RL_PCI_LOIO
198#else
199#define RL_RES			SYS_RES_MEMORY
200#define RL_RID			RL_PCI_LOMEM
201#endif
202
203static device_method_t rl_methods[] = {
204	/* Device interface */
205	DEVMETHOD(device_probe,		rl_probe),
206	DEVMETHOD(device_attach,	rl_attach),
207	DEVMETHOD(device_detach,	rl_detach),
208	DEVMETHOD(device_shutdown,	rl_shutdown),
209
210	/* bus interface */
211	DEVMETHOD(bus_print_child,	bus_generic_print_child),
212	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
213
214	/* MII interface */
215	DEVMETHOD(miibus_readreg,	rl_miibus_readreg),
216	DEVMETHOD(miibus_writereg,	rl_miibus_writereg),
217	DEVMETHOD(miibus_statchg,	rl_miibus_statchg),
218
219	{ 0, 0 }
220};
221
222static driver_t rl_driver = {
223	"rl",
224	rl_methods,
225	sizeof(struct rl_softc)
226};
227
228static devclass_t rl_devclass;
229
230DRIVER_MODULE(if_rl, pci, rl_driver, rl_devclass, 0, 0);
231DRIVER_MODULE(if_rl, cardbus, rl_driver, rl_devclass, 0, 0);
232DRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0);
233
234#define EE_SET(x)					\
235	CSR_WRITE_1(sc, RL_EECMD,			\
236		CSR_READ_1(sc, RL_EECMD) | x)
237
238#define EE_CLR(x)					\
239	CSR_WRITE_1(sc, RL_EECMD,			\
240		CSR_READ_1(sc, RL_EECMD) & ~x)
241
242/*
243 * Send a read command and address to the EEPROM, check for ACK.
244 */
245static void rl_eeprom_putbyte(sc, addr)
246	struct rl_softc		*sc;
247	int			addr;
248{
249	register int		d, i;
250
251	d = addr | sc->rl_eecmd_read;
252
253	/*
254	 * Feed in each bit and strobe the clock.
255	 */
256	for (i = 0x400; i; i >>= 1) {
257		if (d & i) {
258			EE_SET(RL_EE_DATAIN);
259		} else {
260			EE_CLR(RL_EE_DATAIN);
261		}
262		DELAY(100);
263		EE_SET(RL_EE_CLK);
264		DELAY(150);
265		EE_CLR(RL_EE_CLK);
266		DELAY(100);
267	}
268
269	return;
270}
271
272/*
273 * Read a word of data stored in the EEPROM at address 'addr.'
274 */
275static void rl_eeprom_getword(sc, addr, dest)
276	struct rl_softc		*sc;
277	int			addr;
278	u_int16_t		*dest;
279{
280	register int		i;
281	u_int16_t		word = 0;
282
283	/* Enter EEPROM access mode. */
284	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
285
286	/*
287	 * Send address of word we want to read.
288	 */
289	rl_eeprom_putbyte(sc, addr);
290
291	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
292
293	/*
294	 * Start reading bits from EEPROM.
295	 */
296	for (i = 0x8000; i; i >>= 1) {
297		EE_SET(RL_EE_CLK);
298		DELAY(100);
299		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
300			word |= i;
301		EE_CLR(RL_EE_CLK);
302		DELAY(100);
303	}
304
305	/* Turn off EEPROM access mode. */
306	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
307
308	*dest = word;
309
310	return;
311}
312
313/*
314 * Read a sequence of words from the EEPROM.
315 */
316static void rl_read_eeprom(sc, dest, off, cnt, swap)
317	struct rl_softc		*sc;
318	caddr_t			dest;
319	int			off;
320	int			cnt;
321	int			swap;
322{
323	int			i;
324	u_int16_t		word = 0, *ptr;
325
326	for (i = 0; i < cnt; i++) {
327		rl_eeprom_getword(sc, off + i, &word);
328		ptr = (u_int16_t *)(dest + (i * 2));
329		if (swap)
330			*ptr = ntohs(word);
331		else
332			*ptr = word;
333	}
334
335	return;
336}
337
338
339/*
340 * MII access routines are provided for the 8129, which
341 * doesn't have a built-in PHY. For the 8139, we fake things
342 * up by diverting rl_phy_readreg()/rl_phy_writereg() to the
343 * direct access PHY registers.
344 */
345#define MII_SET(x)					\
346	CSR_WRITE_1(sc, RL_MII,				\
347		CSR_READ_1(sc, RL_MII) | x)
348
349#define MII_CLR(x)					\
350	CSR_WRITE_1(sc, RL_MII,				\
351		CSR_READ_1(sc, RL_MII) & ~x)
352
353/*
354 * Sync the PHYs by setting data bit and strobing the clock 32 times.
355 */
356static void rl_mii_sync(sc)
357	struct rl_softc		*sc;
358{
359	register int		i;
360
361	MII_SET(RL_MII_DIR|RL_MII_DATAOUT);
362
363	for (i = 0; i < 32; i++) {
364		MII_SET(RL_MII_CLK);
365		DELAY(1);
366		MII_CLR(RL_MII_CLK);
367		DELAY(1);
368	}
369
370	return;
371}
372
373/*
374 * Clock a series of bits through the MII.
375 */
376static void rl_mii_send(sc, bits, cnt)
377	struct rl_softc		*sc;
378	u_int32_t		bits;
379	int			cnt;
380{
381	int			i;
382
383	MII_CLR(RL_MII_CLK);
384
385	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
386                if (bits & i) {
387			MII_SET(RL_MII_DATAOUT);
388                } else {
389			MII_CLR(RL_MII_DATAOUT);
390                }
391		DELAY(1);
392		MII_CLR(RL_MII_CLK);
393		DELAY(1);
394		MII_SET(RL_MII_CLK);
395	}
396}
397
398/*
399 * Read an PHY register through the MII.
400 */
401static int rl_mii_readreg(sc, frame)
402	struct rl_softc		*sc;
403	struct rl_mii_frame	*frame;
404
405{
406	int			i, ack;
407
408	RL_LOCK(sc);
409
410	/*
411	 * Set up frame for RX.
412	 */
413	frame->mii_stdelim = RL_MII_STARTDELIM;
414	frame->mii_opcode = RL_MII_READOP;
415	frame->mii_turnaround = 0;
416	frame->mii_data = 0;
417
418	CSR_WRITE_2(sc, RL_MII, 0);
419
420	/*
421 	 * Turn on data xmit.
422	 */
423	MII_SET(RL_MII_DIR);
424
425	rl_mii_sync(sc);
426
427	/*
428	 * Send command/address info.
429	 */
430	rl_mii_send(sc, frame->mii_stdelim, 2);
431	rl_mii_send(sc, frame->mii_opcode, 2);
432	rl_mii_send(sc, frame->mii_phyaddr, 5);
433	rl_mii_send(sc, frame->mii_regaddr, 5);
434
435	/* Idle bit */
436	MII_CLR((RL_MII_CLK|RL_MII_DATAOUT));
437	DELAY(1);
438	MII_SET(RL_MII_CLK);
439	DELAY(1);
440
441	/* Turn off xmit. */
442	MII_CLR(RL_MII_DIR);
443
444	/* Check for ack */
445	MII_CLR(RL_MII_CLK);
446	DELAY(1);
447	MII_SET(RL_MII_CLK);
448	DELAY(1);
449	ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN;
450
451	/*
452	 * Now try reading data bits. If the ack failed, we still
453	 * need to clock through 16 cycles to keep the PHY(s) in sync.
454	 */
455	if (ack) {
456		for(i = 0; i < 16; i++) {
457			MII_CLR(RL_MII_CLK);
458			DELAY(1);
459			MII_SET(RL_MII_CLK);
460			DELAY(1);
461		}
462		goto fail;
463	}
464
465	for (i = 0x8000; i; i >>= 1) {
466		MII_CLR(RL_MII_CLK);
467		DELAY(1);
468		if (!ack) {
469			if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN)
470				frame->mii_data |= i;
471			DELAY(1);
472		}
473		MII_SET(RL_MII_CLK);
474		DELAY(1);
475	}
476
477fail:
478
479	MII_CLR(RL_MII_CLK);
480	DELAY(1);
481	MII_SET(RL_MII_CLK);
482	DELAY(1);
483
484	RL_UNLOCK(sc);
485
486	if (ack)
487		return(1);
488	return(0);
489}
490
491/*
492 * Write to a PHY register through the MII.
493 */
494static int rl_mii_writereg(sc, frame)
495	struct rl_softc		*sc;
496	struct rl_mii_frame	*frame;
497
498{
499	RL_LOCK(sc);
500
501	/*
502	 * Set up frame for TX.
503	 */
504
505	frame->mii_stdelim = RL_MII_STARTDELIM;
506	frame->mii_opcode = RL_MII_WRITEOP;
507	frame->mii_turnaround = RL_MII_TURNAROUND;
508
509	/*
510 	 * Turn on data output.
511	 */
512	MII_SET(RL_MII_DIR);
513
514	rl_mii_sync(sc);
515
516	rl_mii_send(sc, frame->mii_stdelim, 2);
517	rl_mii_send(sc, frame->mii_opcode, 2);
518	rl_mii_send(sc, frame->mii_phyaddr, 5);
519	rl_mii_send(sc, frame->mii_regaddr, 5);
520	rl_mii_send(sc, frame->mii_turnaround, 2);
521	rl_mii_send(sc, frame->mii_data, 16);
522
523	/* Idle bit. */
524	MII_SET(RL_MII_CLK);
525	DELAY(1);
526	MII_CLR(RL_MII_CLK);
527	DELAY(1);
528
529	/*
530	 * Turn off xmit.
531	 */
532	MII_CLR(RL_MII_DIR);
533
534	RL_UNLOCK(sc);
535
536	return(0);
537}
538
539static int rl_miibus_readreg(dev, phy, reg)
540	device_t		dev;
541	int			phy, reg;
542{
543	struct rl_softc		*sc;
544	struct rl_mii_frame	frame;
545	u_int16_t		rval = 0;
546	u_int16_t		rl8139_reg = 0;
547
548	sc = device_get_softc(dev);
549	RL_LOCK(sc);
550
551	if (sc->rl_type == RL_8139) {
552		/* Pretend the internal PHY is only at address 0 */
553		if (phy) {
554			RL_UNLOCK(sc);
555			return(0);
556		}
557		switch(reg) {
558		case MII_BMCR:
559			rl8139_reg = RL_BMCR;
560			break;
561		case MII_BMSR:
562			rl8139_reg = RL_BMSR;
563			break;
564		case MII_ANAR:
565			rl8139_reg = RL_ANAR;
566			break;
567		case MII_ANER:
568			rl8139_reg = RL_ANER;
569			break;
570		case MII_ANLPAR:
571			rl8139_reg = RL_LPAR;
572			break;
573		case MII_PHYIDR1:
574		case MII_PHYIDR2:
575			RL_UNLOCK(sc);
576			return(0);
577			break;
578		default:
579			printf("rl%d: bad phy register\n", sc->rl_unit);
580			RL_UNLOCK(sc);
581			return(0);
582		}
583		rval = CSR_READ_2(sc, rl8139_reg);
584		RL_UNLOCK(sc);
585		return(rval);
586	}
587
588	bzero((char *)&frame, sizeof(frame));
589
590	frame.mii_phyaddr = phy;
591	frame.mii_regaddr = reg;
592	rl_mii_readreg(sc, &frame);
593	RL_UNLOCK(sc);
594
595	return(frame.mii_data);
596}
597
598static int rl_miibus_writereg(dev, phy, reg, data)
599	device_t		dev;
600	int			phy, reg, data;
601{
602	struct rl_softc		*sc;
603	struct rl_mii_frame	frame;
604	u_int16_t		rl8139_reg = 0;
605
606	sc = device_get_softc(dev);
607	RL_LOCK(sc);
608
609	if (sc->rl_type == RL_8139) {
610		/* Pretend the internal PHY is only at address 0 */
611		if (phy) {
612			RL_UNLOCK(sc);
613			return(0);
614		}
615		switch(reg) {
616		case MII_BMCR:
617			rl8139_reg = RL_BMCR;
618			break;
619		case MII_BMSR:
620			rl8139_reg = RL_BMSR;
621			break;
622		case MII_ANAR:
623			rl8139_reg = RL_ANAR;
624			break;
625		case MII_ANER:
626			rl8139_reg = RL_ANER;
627			break;
628		case MII_ANLPAR:
629			rl8139_reg = RL_LPAR;
630			break;
631		case MII_PHYIDR1:
632		case MII_PHYIDR2:
633			RL_UNLOCK(sc);
634			return(0);
635			break;
636		default:
637			printf("rl%d: bad phy register\n", sc->rl_unit);
638			RL_UNLOCK(sc);
639			return(0);
640		}
641		CSR_WRITE_2(sc, rl8139_reg, data);
642		RL_UNLOCK(sc);
643		return(0);
644	}
645
646	bzero((char *)&frame, sizeof(frame));
647
648	frame.mii_phyaddr = phy;
649	frame.mii_regaddr = reg;
650	frame.mii_data = data;
651
652	rl_mii_writereg(sc, &frame);
653
654	RL_UNLOCK(sc);
655	return(0);
656}
657
658static void rl_miibus_statchg(dev)
659	device_t		dev;
660{
661	return;
662}
663
664/*
665 * Calculate CRC of a multicast group address, return the upper 6 bits.
666 */
667static u_int8_t rl_calchash(addr)
668	caddr_t			addr;
669{
670	u_int32_t		crc, carry;
671	int			i, j;
672	u_int8_t		c;
673
674	/* Compute CRC for the address value. */
675	crc = 0xFFFFFFFF; /* initial value */
676
677	for (i = 0; i < 6; i++) {
678		c = *(addr + i);
679		for (j = 0; j < 8; j++) {
680			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
681			crc <<= 1;
682			c >>= 1;
683			if (carry)
684				crc = (crc ^ 0x04c11db6) | carry;
685		}
686	}
687
688	/* return the filter bit position */
689	return(crc >> 26);
690}
691
692/*
693 * Program the 64-bit multicast hash filter.
694 */
695static void rl_setmulti(sc)
696	struct rl_softc		*sc;
697{
698	struct ifnet		*ifp;
699	int			h = 0;
700	u_int32_t		hashes[2] = { 0, 0 };
701	struct ifmultiaddr	*ifma;
702	u_int32_t		rxfilt;
703	int			mcnt = 0;
704
705	ifp = &sc->arpcom.ac_if;
706
707	rxfilt = CSR_READ_4(sc, RL_RXCFG);
708
709	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
710		rxfilt |= RL_RXCFG_RX_MULTI;
711		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
712		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
713		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
714		return;
715	}
716
717	/* first, zot all the existing hash bits */
718	CSR_WRITE_4(sc, RL_MAR0, 0);
719	CSR_WRITE_4(sc, RL_MAR4, 0);
720
721	/* now program new ones */
722	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
723		if (ifma->ifma_addr->sa_family != AF_LINK)
724			continue;
725		h = rl_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
726		if (h < 32)
727			hashes[0] |= (1 << h);
728		else
729			hashes[1] |= (1 << (h - 32));
730		mcnt++;
731	}
732
733	if (mcnt)
734		rxfilt |= RL_RXCFG_RX_MULTI;
735	else
736		rxfilt &= ~RL_RXCFG_RX_MULTI;
737
738	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
739	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
740	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
741
742	return;
743}
744
745static void rl_reset(sc)
746	struct rl_softc		*sc;
747{
748	register int		i;
749
750	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
751
752	for (i = 0; i < RL_TIMEOUT; i++) {
753		DELAY(10);
754		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
755			break;
756	}
757	if (i == RL_TIMEOUT)
758		printf("rl%d: reset never completed!\n", sc->rl_unit);
759
760        return;
761}
762
763/*
764 * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device
765 * IDs against our list and return a device name if we find a match.
766 */
767static int rl_probe(dev)
768	device_t		dev;
769{
770	struct rl_type		*t;
771
772	t = rl_devs;
773
774	while(t->rl_name != NULL) {
775		if ((pci_get_vendor(dev) == t->rl_vid) &&
776		    (pci_get_device(dev) == t->rl_did)) {
777			device_set_desc(dev, t->rl_name);
778			return(0);
779		}
780		t++;
781	}
782
783	return(ENXIO);
784}
785
786/*
787 * Attach the interface. Allocate softc structures, do ifmedia
788 * setup and ethernet/BPF attach.
789 */
790static int rl_attach(dev)
791	device_t		dev;
792{
793	u_char			eaddr[ETHER_ADDR_LEN];
794	u_int32_t		command;
795	struct rl_softc		*sc;
796	struct ifnet		*ifp;
797	u_int16_t		rl_did = 0;
798	int			unit, error = 0, rid;
799
800	sc = device_get_softc(dev);
801	unit = device_get_unit(dev);
802	bzero(sc, sizeof(struct rl_softc));
803
804	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
805	RL_LOCK(sc);
806
807	/*
808	 * Handle power management nonsense.
809	 */
810
811	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
812		u_int32_t		iobase, membase, irq;
813
814		/* Save important PCI config data. */
815		iobase = pci_read_config(dev, RL_PCI_LOIO, 4);
816		membase = pci_read_config(dev, RL_PCI_LOMEM, 4);
817		irq = pci_read_config(dev, RL_PCI_INTLINE, 4);
818
819		/* Reset the power state. */
820		printf("rl%d: chip is is in D%d power mode "
821		    "-- setting to D0\n", unit,
822		    pci_get_powerstate(dev));
823
824		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
825
826		/* Restore PCI config data. */
827		pci_write_config(dev, RL_PCI_LOIO, iobase, 4);
828		pci_write_config(dev, RL_PCI_LOMEM, membase, 4);
829		pci_write_config(dev, RL_PCI_INTLINE, irq, 4);
830	}
831
832	/*
833	 * Map control/status registers.
834	 */
835	command = pci_read_config(dev, PCIR_COMMAND, 4);
836	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
837	pci_write_config(dev, PCIR_COMMAND, command, 4);
838	command = pci_read_config(dev, PCIR_COMMAND, 4);
839
840#ifdef RL_USEIOSPACE
841	if (!(command & PCIM_CMD_PORTEN)) {
842		printf("rl%d: failed to enable I/O ports!\n", unit);
843		error = ENXIO;
844		goto fail;
845	}
846#else
847	if (!(command & PCIM_CMD_MEMEN)) {
848		printf("rl%d: failed to enable memory mapping!\n", unit);
849		error = ENXIO;
850		goto fail;
851	}
852#endif
853
854	rid = RL_RID;
855	sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid,
856	    0, ~0, 1, RF_ACTIVE);
857
858	if (sc->rl_res == NULL) {
859		printf ("rl%d: couldn't map ports/memory\n", unit);
860		error = ENXIO;
861		goto fail;
862	}
863
864	/* Detect the Realtek 8139B. For some reason, this chip is very
865	 * unstable when left to autoselect the media
866	 * The best workaround is to set the device to the required
867	 * media type or to set it to the 10 Meg speed.
868	 */
869
870	if ((rman_get_end(sc->rl_res)-rman_get_start(sc->rl_res))==0xff) {
871		printf("rl%d: Realtek 8139B detected. Warning, this may be unstable in autoselect mode\n", unit);
872	}
873
874	sc->rl_btag = rman_get_bustag(sc->rl_res);
875	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
876
877	rid = 0;
878	sc->rl_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
879	    RF_SHAREABLE | RF_ACTIVE);
880
881	if (sc->rl_irq == NULL) {
882		printf("rl%d: couldn't map interrupt\n", unit);
883		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
884		error = ENXIO;
885		goto fail;
886	}
887
888	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET,
889	    rl_intr, sc, &sc->rl_intrhand);
890
891	if (error) {
892		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
893		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
894		printf("rl%d: couldn't set up irq\n", unit);
895		goto fail;
896	}
897
898	callout_handle_init(&sc->rl_stat_ch);
899
900	/* Reset the adapter. */
901	rl_reset(sc);
902	sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
903	rl_read_eeprom(sc, (caddr_t)&rl_did, 0, 1, 0);
904	if (rl_did != 0x8129)
905		sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
906
907	/*
908	 * Get station address from the EEPROM.
909	 */
910	rl_read_eeprom(sc, (caddr_t)&eaddr, RL_EE_EADDR, 3, 0);
911
912	/*
913	 * A RealTek chip was detected. Inform the world.
914	 */
915	printf("rl%d: Ethernet address: %6D\n", unit, eaddr, ":");
916
917	sc->rl_unit = unit;
918	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
919
920	/*
921	 * Now read the exact device type from the EEPROM to find
922	 * out if it's an 8129 or 8139.
923	 */
924	rl_read_eeprom(sc, (caddr_t)&rl_did, RL_EE_PCI_DID, 1, 0);
925
926	if (rl_did == RT_DEVICEID_8139 || rl_did == ACCTON_DEVICEID_5030 ||
927	    rl_did == DELTA_DEVICEID_8139 || rl_did == ADDTRON_DEVICEID_8139 ||
928	    rl_did == RT_DEVICEID_8138)
929		sc->rl_type = RL_8139;
930	else if (rl_did == RT_DEVICEID_8129)
931		sc->rl_type = RL_8129;
932	else {
933		printf("rl%d: unknown device ID: %x\n", unit, rl_did);
934		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
935		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
936		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
937		error = ENXIO;
938		goto fail;
939	}
940
941	sc->rl_cdata.rl_rx_buf = contigmalloc(RL_RXBUFLEN + 1518, M_DEVBUF,
942		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
943
944	if (sc->rl_cdata.rl_rx_buf == NULL) {
945		printf("rl%d: no memory for list buffers!\n", unit);
946		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
947		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
948		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
949		error = ENXIO;
950		goto fail;
951	}
952
953	/* Leave a few bytes before the start of the RX ring buffer. */
954	sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
955	sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t);
956
957	/* Do MII setup */
958	if (mii_phy_probe(dev, &sc->rl_miibus,
959	    rl_ifmedia_upd, rl_ifmedia_sts)) {
960		printf("rl%d: MII without any phy!\n", sc->rl_unit);
961		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
962		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
963		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
964		free(sc->rl_cdata.rl_rx_buf, M_DEVBUF);
965		error = ENXIO;
966		goto fail;
967	}
968
969	ifp = &sc->arpcom.ac_if;
970	ifp->if_softc = sc;
971	ifp->if_unit = unit;
972	ifp->if_name = "rl";
973	ifp->if_mtu = ETHERMTU;
974	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
975	ifp->if_ioctl = rl_ioctl;
976	ifp->if_output = ether_output;
977	ifp->if_start = rl_start;
978	ifp->if_watchdog = rl_watchdog;
979	ifp->if_init = rl_init;
980	ifp->if_baudrate = 10000000;
981	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
982
983	/*
984	 * Call MI attach routine.
985	 */
986	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
987	RL_UNLOCK(sc);
988	return(0);
989
990fail:
991	RL_UNLOCK(sc);
992	mtx_destroy(&sc->rl_mtx);
993	return(error);
994}
995
996static int rl_detach(dev)
997	device_t		dev;
998{
999	struct rl_softc		*sc;
1000	struct ifnet		*ifp;
1001
1002	sc = device_get_softc(dev);
1003	RL_LOCK(sc);
1004	ifp = &sc->arpcom.ac_if;
1005
1006	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1007	rl_stop(sc);
1008
1009	bus_generic_detach(dev);
1010	device_delete_child(dev, sc->rl_miibus);
1011
1012	bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1013	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1014	bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1015
1016	contigfree(sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN + 32, M_DEVBUF);
1017
1018	RL_UNLOCK(sc);
1019	mtx_destroy(&sc->rl_mtx);
1020
1021	return(0);
1022}
1023
1024/*
1025 * Initialize the transmit descriptors.
1026 */
1027static int rl_list_tx_init(sc)
1028	struct rl_softc		*sc;
1029{
1030	struct rl_chain_data	*cd;
1031	int			i;
1032
1033	cd = &sc->rl_cdata;
1034	for (i = 0; i < RL_TX_LIST_CNT; i++) {
1035		cd->rl_tx_chain[i] = NULL;
1036		CSR_WRITE_4(sc,
1037		    RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
1038	}
1039
1040	sc->rl_cdata.cur_tx = 0;
1041	sc->rl_cdata.last_tx = 0;
1042
1043	return(0);
1044}
1045
1046/*
1047 * A frame has been uploaded: pass the resulting mbuf chain up to
1048 * the higher level protocols.
1049 *
1050 * You know there's something wrong with a PCI bus-master chip design
1051 * when you have to use m_devget().
1052 *
1053 * The receive operation is badly documented in the datasheet, so I'll
1054 * attempt to document it here. The driver provides a buffer area and
1055 * places its base address in the RX buffer start address register.
1056 * The chip then begins copying frames into the RX buffer. Each frame
1057 * is preceeded by a 32-bit RX status word which specifies the length
1058 * of the frame and certain other status bits. Each frame (starting with
1059 * the status word) is also 32-bit aligned. The frame length is in the
1060 * first 16 bits of the status word; the lower 15 bits correspond with
1061 * the 'rx status register' mentioned in the datasheet.
1062 *
1063 * Note: to make the Alpha happy, the frame payload needs to be aligned
1064 * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
1065 * the ring buffer starting at an address two bytes before the actual
1066 * data location. We can then shave off the first two bytes using m_adj().
1067 * The reason we do this is because m_devget() doesn't let us specify an
1068 * offset into the mbuf storage space, so we have to artificially create
1069 * one. The ring is allocated in such a way that there are a few unused
1070 * bytes of space preceecing it so that it will be safe for us to do the
1071 * 2-byte backstep even if reading from the ring at offset 0.
1072 */
1073static void rl_rxeof(sc)
1074	struct rl_softc		*sc;
1075{
1076        struct ether_header	*eh;
1077        struct mbuf		*m;
1078        struct ifnet		*ifp;
1079	int			total_len = 0;
1080	u_int32_t		rxstat;
1081	caddr_t			rxbufpos;
1082	int			wrap = 0;
1083	u_int16_t		cur_rx;
1084	u_int16_t		limit;
1085	u_int16_t		rx_bytes = 0, max_bytes;
1086
1087	ifp = &sc->arpcom.ac_if;
1088
1089	cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
1090
1091	/* Do not try to read past this point. */
1092	limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
1093
1094	if (limit < cur_rx)
1095		max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
1096	else
1097		max_bytes = limit - cur_rx;
1098
1099	while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
1100		rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
1101		rxstat = *(u_int32_t *)rxbufpos;
1102
1103		/*
1104		 * Here's a totally undocumented fact for you. When the
1105		 * RealTek chip is in the process of copying a packet into
1106		 * RAM for you, the length will be 0xfff0. If you spot a
1107		 * packet header with this value, you need to stop. The
1108		 * datasheet makes absolutely no mention of this and
1109		 * RealTek should be shot for this.
1110		 */
1111		if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED)
1112			break;
1113
1114		if (!(rxstat & RL_RXSTAT_RXOK)) {
1115			ifp->if_ierrors++;
1116			rl_init(sc);
1117			return;
1118		}
1119
1120		/* No errors; receive the packet. */
1121		total_len = rxstat >> 16;
1122		rx_bytes += total_len + 4;
1123
1124		/*
1125		 * XXX The RealTek chip includes the CRC with every
1126		 * received frame, and there's no way to turn this
1127		 * behavior off (at least, I can't find anything in
1128	 	 * the manual that explains how to do it) so we have
1129		 * to trim off the CRC manually.
1130		 */
1131		total_len -= ETHER_CRC_LEN;
1132
1133		/*
1134		 * Avoid trying to read more bytes than we know
1135		 * the chip has prepared for us.
1136		 */
1137		if (rx_bytes > max_bytes)
1138			break;
1139
1140		rxbufpos = sc->rl_cdata.rl_rx_buf +
1141			((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN);
1142
1143		if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
1144			rxbufpos = sc->rl_cdata.rl_rx_buf;
1145
1146		wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
1147
1148		if (total_len > wrap) {
1149			/*
1150			 * Fool m_devget() into thinking we want to copy
1151			 * the whole buffer so we don't end up fragmenting
1152			 * the data.
1153			 */
1154			m = m_devget(rxbufpos - RL_ETHER_ALIGN,
1155			    total_len + RL_ETHER_ALIGN, 0, ifp, NULL);
1156			if (m == NULL) {
1157				ifp->if_ierrors++;
1158				printf("rl%d: out of mbufs, tried to "
1159				    "copy %d bytes\n", sc->rl_unit, wrap);
1160			} else {
1161				m_adj(m, RL_ETHER_ALIGN);
1162				m_copyback(m, wrap, total_len - wrap,
1163					sc->rl_cdata.rl_rx_buf);
1164			}
1165			cur_rx = (total_len - wrap + ETHER_CRC_LEN);
1166		} else {
1167			m = m_devget(rxbufpos - RL_ETHER_ALIGN,
1168			    total_len + RL_ETHER_ALIGN, 0, ifp, NULL);
1169			if (m == NULL) {
1170				ifp->if_ierrors++;
1171				printf("rl%d: out of mbufs, tried to "
1172				    "copy %d bytes\n", sc->rl_unit, total_len);
1173			} else
1174				m_adj(m, RL_ETHER_ALIGN);
1175			cur_rx += total_len + 4 + ETHER_CRC_LEN;
1176		}
1177
1178		/*
1179		 * Round up to 32-bit boundary.
1180		 */
1181		cur_rx = (cur_rx + 3) & ~3;
1182		CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
1183
1184		if (m == NULL)
1185			continue;
1186
1187		eh = mtod(m, struct ether_header *);
1188		ifp->if_ipackets++;
1189
1190		/* Remove header from mbuf and pass it on. */
1191		m_adj(m, sizeof(struct ether_header));
1192		ether_input(ifp, eh, m);
1193	}
1194
1195	return;
1196}
1197
1198/*
1199 * A frame was downloaded to the chip. It's safe for us to clean up
1200 * the list buffers.
1201 */
1202static void rl_txeof(sc)
1203	struct rl_softc		*sc;
1204{
1205	struct ifnet		*ifp;
1206	u_int32_t		txstat;
1207
1208	ifp = &sc->arpcom.ac_if;
1209
1210	/* Clear the timeout timer. */
1211	ifp->if_timer = 0;
1212
1213	/*
1214	 * Go through our tx list and free mbufs for those
1215	 * frames that have been uploaded.
1216	 */
1217	do {
1218		txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
1219		if (!(txstat & (RL_TXSTAT_TX_OK|
1220		    RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
1221			break;
1222
1223		ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
1224
1225		if (RL_LAST_TXMBUF(sc) != NULL) {
1226			m_freem(RL_LAST_TXMBUF(sc));
1227			RL_LAST_TXMBUF(sc) = NULL;
1228		}
1229		if (txstat & RL_TXSTAT_TX_OK)
1230			ifp->if_opackets++;
1231		else {
1232			int			oldthresh;
1233			ifp->if_oerrors++;
1234			if ((txstat & RL_TXSTAT_TXABRT) ||
1235			    (txstat & RL_TXSTAT_OUTOFWIN))
1236				CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1237			oldthresh = sc->rl_txthresh;
1238			/* error recovery */
1239			rl_reset(sc);
1240			rl_init(sc);
1241			/*
1242			 * If there was a transmit underrun,
1243			 * bump the TX threshold.
1244			 */
1245			if (txstat & RL_TXSTAT_TX_UNDERRUN)
1246				sc->rl_txthresh = oldthresh + 32;
1247			return;
1248		}
1249		RL_INC(sc->rl_cdata.last_tx);
1250		ifp->if_flags &= ~IFF_OACTIVE;
1251	} while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
1252
1253	return;
1254}
1255
1256static void rl_tick(xsc)
1257	void			*xsc;
1258{
1259	struct rl_softc		*sc;
1260	struct mii_data		*mii;
1261
1262	sc = xsc;
1263	RL_LOCK(sc);
1264	mii = device_get_softc(sc->rl_miibus);
1265
1266	mii_tick(mii);
1267
1268	sc->rl_stat_ch = timeout(rl_tick, sc, hz);
1269	RL_UNLOCK(sc);
1270
1271	return;
1272}
1273
1274static void rl_intr(arg)
1275	void			*arg;
1276{
1277	struct rl_softc		*sc;
1278	struct ifnet		*ifp;
1279	u_int16_t		status;
1280
1281	sc = arg;
1282	RL_LOCK(sc);
1283	ifp = &sc->arpcom.ac_if;
1284
1285	/* Disable interrupts. */
1286	CSR_WRITE_2(sc, RL_IMR, 0x0000);
1287
1288	for (;;) {
1289
1290		status = CSR_READ_2(sc, RL_ISR);
1291		if (status)
1292			CSR_WRITE_2(sc, RL_ISR, status);
1293
1294		if ((status & RL_INTRS) == 0)
1295			break;
1296
1297		if (status & RL_ISR_RX_OK)
1298			rl_rxeof(sc);
1299
1300		if (status & RL_ISR_RX_ERR)
1301			rl_rxeof(sc);
1302
1303		if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR))
1304			rl_txeof(sc);
1305
1306		if (status & RL_ISR_SYSTEM_ERR) {
1307			rl_reset(sc);
1308			rl_init(sc);
1309		}
1310
1311	}
1312
1313	/* Re-enable interrupts. */
1314	CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1315
1316	if (ifp->if_snd.ifq_head != NULL)
1317		rl_start(ifp);
1318
1319	RL_UNLOCK(sc);
1320
1321	return;
1322}
1323
1324/*
1325 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1326 * pointers to the fragment pointers.
1327 */
1328static int rl_encap(sc, m_head)
1329	struct rl_softc		*sc;
1330	struct mbuf		*m_head;
1331{
1332	struct mbuf		*m_new = NULL;
1333
1334	/*
1335	 * The RealTek is brain damaged and wants longword-aligned
1336	 * TX buffers, plus we can only have one fragment buffer
1337	 * per packet. We have to copy pretty much all the time.
1338	 */
1339
1340	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1341	if (m_new == NULL) {
1342		printf("rl%d: no memory for tx list", sc->rl_unit);
1343		return(1);
1344	}
1345	if (m_head->m_pkthdr.len > MHLEN) {
1346		MCLGET(m_new, M_DONTWAIT);
1347		if (!(m_new->m_flags & M_EXT)) {
1348			m_freem(m_new);
1349			printf("rl%d: no memory for tx list",
1350					sc->rl_unit);
1351			return(1);
1352		}
1353	}
1354	m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t));
1355	m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1356	m_freem(m_head);
1357	m_head = m_new;
1358
1359	/* Pad frames to at least 60 bytes. */
1360	if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) {
1361		/*
1362		 * Make security concious people happy: zero out the
1363		 * bytes in the pad area, since we don't know what
1364		 * this mbuf cluster buffer's previous user might
1365		 * have left in it.
1366	 	 */
1367		bzero(mtod(m_head, char *) + m_head->m_pkthdr.len,
1368		     RL_MIN_FRAMELEN - m_head->m_pkthdr.len);
1369		m_head->m_pkthdr.len +=
1370		    (RL_MIN_FRAMELEN - m_head->m_pkthdr.len);
1371		m_head->m_len = m_head->m_pkthdr.len;
1372	}
1373
1374	RL_CUR_TXMBUF(sc) = m_head;
1375
1376	return(0);
1377}
1378
1379/*
1380 * Main transmit routine.
1381 */
1382
1383static void rl_start(ifp)
1384	struct ifnet		*ifp;
1385{
1386	struct rl_softc		*sc;
1387	struct mbuf		*m_head = NULL;
1388
1389	sc = ifp->if_softc;
1390	RL_LOCK(sc);
1391
1392	while(RL_CUR_TXMBUF(sc) == NULL) {
1393		IF_DEQUEUE(&ifp->if_snd, m_head);
1394		if (m_head == NULL)
1395			break;
1396
1397		if (rl_encap(sc, m_head)) {
1398			IF_PREPEND(&ifp->if_snd, m_head);
1399			ifp->if_flags |= IFF_OACTIVE;
1400			break;
1401		}
1402
1403		/*
1404		 * If there's a BPF listener, bounce a copy of this frame
1405		 * to him.
1406		 */
1407		if (ifp->if_bpf)
1408			bpf_mtap(ifp, RL_CUR_TXMBUF(sc));
1409
1410		/*
1411		 * Transmit the frame.
1412	 	 */
1413		CSR_WRITE_4(sc, RL_CUR_TXADDR(sc),
1414		    vtophys(mtod(RL_CUR_TXMBUF(sc), caddr_t)));
1415		CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
1416		    RL_TXTHRESH(sc->rl_txthresh) |
1417		    RL_CUR_TXMBUF(sc)->m_pkthdr.len);
1418
1419		RL_INC(sc->rl_cdata.cur_tx);
1420	}
1421
1422	/*
1423	 * We broke out of the loop because all our TX slots are
1424	 * full. Mark the NIC as busy until it drains some of the
1425	 * packets from the queue.
1426	 */
1427	if (RL_CUR_TXMBUF(sc) != NULL)
1428		ifp->if_flags |= IFF_OACTIVE;
1429
1430	/*
1431	 * Set a timeout in case the chip goes out to lunch.
1432	 */
1433	ifp->if_timer = 5;
1434	RL_UNLOCK(sc);
1435
1436	return;
1437}
1438
1439static void rl_init(xsc)
1440	void			*xsc;
1441{
1442	struct rl_softc		*sc = xsc;
1443	struct ifnet		*ifp = &sc->arpcom.ac_if;
1444	struct mii_data		*mii;
1445	int			i;
1446	u_int32_t		rxcfg = 0;
1447
1448	RL_LOCK(sc);
1449	mii = device_get_softc(sc->rl_miibus);
1450
1451	/*
1452	 * Cancel pending I/O and free all RX/TX buffers.
1453	 */
1454	rl_stop(sc);
1455
1456	/* Init our MAC address */
1457	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1458		CSR_WRITE_1(sc, RL_IDR0 + i, sc->arpcom.ac_enaddr[i]);
1459	}
1460
1461	/* Init the RX buffer pointer register. */
1462	CSR_WRITE_4(sc, RL_RXADDR, vtophys(sc->rl_cdata.rl_rx_buf));
1463
1464	/* Init TX descriptors. */
1465	rl_list_tx_init(sc);
1466
1467	/*
1468	 * Enable transmit and receive.
1469	 */
1470	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1471
1472	/*
1473	 * Set the initial TX and RX configuration.
1474	 */
1475	CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1476	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
1477
1478	/* Set the individual bit to receive frames for this host only. */
1479	rxcfg = CSR_READ_4(sc, RL_RXCFG);
1480	rxcfg |= RL_RXCFG_RX_INDIV;
1481
1482	/* If we want promiscuous mode, set the allframes bit. */
1483	if (ifp->if_flags & IFF_PROMISC) {
1484		rxcfg |= RL_RXCFG_RX_ALLPHYS;
1485		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1486	} else {
1487		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
1488		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1489	}
1490
1491	/*
1492	 * Set capture broadcast bit to capture broadcast frames.
1493	 */
1494	if (ifp->if_flags & IFF_BROADCAST) {
1495		rxcfg |= RL_RXCFG_RX_BROAD;
1496		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1497	} else {
1498		rxcfg &= ~RL_RXCFG_RX_BROAD;
1499		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1500	}
1501
1502	/*
1503	 * Program the multicast filter, if necessary.
1504	 */
1505	rl_setmulti(sc);
1506
1507	/*
1508	 * Enable interrupts.
1509	 */
1510	CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1511
1512	/* Set initial TX threshold */
1513	sc->rl_txthresh = RL_TX_THRESH_INIT;
1514
1515	/* Start RX/TX process. */
1516	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
1517
1518	/* Enable receiver and transmitter. */
1519	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1520
1521	mii_mediachg(mii);
1522
1523	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
1524
1525	ifp->if_flags |= IFF_RUNNING;
1526	ifp->if_flags &= ~IFF_OACTIVE;
1527
1528	sc->rl_stat_ch = timeout(rl_tick, sc, hz);
1529	RL_UNLOCK(sc);
1530
1531	return;
1532}
1533
1534/*
1535 * Set media options.
1536 */
1537static int rl_ifmedia_upd(ifp)
1538	struct ifnet		*ifp;
1539{
1540	struct rl_softc		*sc;
1541	struct mii_data		*mii;
1542
1543	sc = ifp->if_softc;
1544	mii = device_get_softc(sc->rl_miibus);
1545	mii_mediachg(mii);
1546
1547	return(0);
1548}
1549
1550/*
1551 * Report current media status.
1552 */
1553static void rl_ifmedia_sts(ifp, ifmr)
1554	struct ifnet		*ifp;
1555	struct ifmediareq	*ifmr;
1556{
1557	struct rl_softc		*sc;
1558	struct mii_data		*mii;
1559
1560	sc = ifp->if_softc;
1561	mii = device_get_softc(sc->rl_miibus);
1562
1563	mii_pollstat(mii);
1564	ifmr->ifm_active = mii->mii_media_active;
1565	ifmr->ifm_status = mii->mii_media_status;
1566
1567	return;
1568}
1569
1570static int rl_ioctl(ifp, command, data)
1571	struct ifnet		*ifp;
1572	u_long			command;
1573	caddr_t			data;
1574{
1575	struct rl_softc		*sc = ifp->if_softc;
1576	struct ifreq		*ifr = (struct ifreq *) data;
1577	struct mii_data		*mii;
1578	int			error = 0;
1579
1580	RL_LOCK(sc);
1581
1582	switch(command) {
1583	case SIOCSIFADDR:
1584	case SIOCGIFADDR:
1585	case SIOCSIFMTU:
1586		error = ether_ioctl(ifp, command, data);
1587		break;
1588	case SIOCSIFFLAGS:
1589		if (ifp->if_flags & IFF_UP) {
1590			rl_init(sc);
1591		} else {
1592			if (ifp->if_flags & IFF_RUNNING)
1593				rl_stop(sc);
1594		}
1595		error = 0;
1596		break;
1597	case SIOCADDMULTI:
1598	case SIOCDELMULTI:
1599		rl_setmulti(sc);
1600		error = 0;
1601		break;
1602	case SIOCGIFMEDIA:
1603	case SIOCSIFMEDIA:
1604		mii = device_get_softc(sc->rl_miibus);
1605		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1606		break;
1607	default:
1608		error = EINVAL;
1609		break;
1610	}
1611
1612	RL_UNLOCK(sc);
1613
1614	return(error);
1615}
1616
1617static void rl_watchdog(ifp)
1618	struct ifnet		*ifp;
1619{
1620	struct rl_softc		*sc;
1621
1622	sc = ifp->if_softc;
1623	RL_LOCK(sc);
1624	printf("rl%d: watchdog timeout\n", sc->rl_unit);
1625	ifp->if_oerrors++;
1626
1627	rl_txeof(sc);
1628	rl_rxeof(sc);
1629	rl_init(sc);
1630	RL_UNLOCK(sc);
1631
1632	return;
1633}
1634
1635/*
1636 * Stop the adapter and free any mbufs allocated to the
1637 * RX and TX lists.
1638 */
1639static void rl_stop(sc)
1640	struct rl_softc		*sc;
1641{
1642	register int		i;
1643	struct ifnet		*ifp;
1644
1645	RL_LOCK(sc);
1646	ifp = &sc->arpcom.ac_if;
1647	ifp->if_timer = 0;
1648
1649	untimeout(rl_tick, sc, sc->rl_stat_ch);
1650
1651	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
1652	CSR_WRITE_2(sc, RL_IMR, 0x0000);
1653
1654	/*
1655	 * Free the TX list buffers.
1656	 */
1657	for (i = 0; i < RL_TX_LIST_CNT; i++) {
1658		if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
1659			m_freem(sc->rl_cdata.rl_tx_chain[i]);
1660			sc->rl_cdata.rl_tx_chain[i] = NULL;
1661			CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000);
1662		}
1663	}
1664
1665	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1666	RL_UNLOCK(sc);
1667	return;
1668}
1669
1670/*
1671 * Stop all chip I/O so that the kernel's probe routines don't
1672 * get confused by errant DMAs when rebooting.
1673 */
1674static void rl_shutdown(dev)
1675	device_t		dev;
1676{
1677	struct rl_softc		*sc;
1678
1679	sc = device_get_softc(dev);
1680
1681	rl_stop(sc);
1682
1683	return;
1684}
1685