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