if_wb.c revision 150636
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
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/pci/if_wb.c 150636 2005-09-27 18:10:43Z mlaier $");
35
36/*
37 * Winbond fast ethernet PCI NIC driver
38 *
39 * Supports various cheap network adapters based on the Winbond W89C840F
40 * fast ethernet controller chip. This includes adapters manufactured by
41 * Winbond itself and some made by Linksys.
42 *
43 * Written by Bill Paul <wpaul@ctr.columbia.edu>
44 * Electrical Engineering Department
45 * Columbia University, New York City
46 */
47/*
48 * The Winbond W89C840F chip is a bus master; in some ways it resembles
49 * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has
50 * one major difference which is that while the registers do many of
51 * the same things as a tulip adapter, the offsets are different: where
52 * tulip registers are typically spaced 8 bytes apart, the Winbond
53 * registers are spaced 4 bytes apart. The receiver filter is also
54 * programmed differently.
55 *
56 * Like the tulip, the Winbond chip uses small descriptors containing
57 * a status word, a control word and 32-bit areas that can either be used
58 * to point to two external data blocks, or to point to a single block
59 * and another descriptor in a linked list. Descriptors can be grouped
60 * together in blocks to form fixed length rings or can be chained
61 * together in linked lists. A single packet may be spread out over
62 * several descriptors if necessary.
63 *
64 * For the receive ring, this driver uses a linked list of descriptors,
65 * each pointing to a single mbuf cluster buffer, which us large enough
66 * to hold an entire packet. The link list is looped back to created a
67 * closed ring.
68 *
69 * For transmission, the driver creates a linked list of 'super descriptors'
70 * which each contain several individual descriptors linked toghether.
71 * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we
72 * abuse as fragment pointers. This allows us to use a buffer managment
73 * scheme very similar to that used in the ThunderLAN and Etherlink XL
74 * drivers.
75 *
76 * Autonegotiation is performed using the external PHY via the MII bus.
77 * The sample boards I have all use a Davicom PHY.
78 *
79 * Note: the author of the Linux driver for the Winbond chip alludes
80 * to some sort of flaw in the chip's design that seems to mandate some
81 * drastic workaround which signigicantly impairs transmit performance.
82 * I have no idea what he's on about: transmit performance with all
83 * three of my test boards seems fine.
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/module.h>
92#include <sys/kernel.h>
93#include <sys/socket.h>
94#include <sys/queue.h>
95
96#include <net/if.h>
97#include <net/if_arp.h>
98#include <net/ethernet.h>
99#include <net/if_dl.h>
100#include <net/if_media.h>
101#include <net/if_types.h>
102
103#include <net/bpf.h>
104
105#include <vm/vm.h>              /* for vtophys */
106#include <vm/pmap.h>            /* for vtophys */
107#include <machine/bus.h>
108#include <machine/resource.h>
109#include <sys/bus.h>
110#include <sys/rman.h>
111
112#include <dev/pci/pcireg.h>
113#include <dev/pci/pcivar.h>
114
115#include <dev/mii/mii.h>
116#include <dev/mii/miivar.h>
117
118/* "controller miibus0" required.  See GENERIC if you get errors here. */
119#include "miibus_if.h"
120
121#define WB_USEIOSPACE
122
123#include <pci/if_wbreg.h>
124
125MODULE_DEPEND(wb, pci, 1, 1, 1);
126MODULE_DEPEND(wb, ether, 1, 1, 1);
127MODULE_DEPEND(wb, miibus, 1, 1, 1);
128
129/*
130 * Various supported device vendors/types and their names.
131 */
132static struct wb_type wb_devs[] = {
133	{ WB_VENDORID, WB_DEVICEID_840F,
134		"Winbond W89C840F 10/100BaseTX" },
135	{ CP_VENDORID, CP_DEVICEID_RL100,
136		"Compex RL100-ATX 10/100baseTX" },
137	{ 0, 0, NULL }
138};
139
140static int wb_probe(device_t);
141static int wb_attach(device_t);
142static int wb_detach(device_t);
143
144static void wb_bfree(void *addr, void *args);
145static int wb_newbuf(struct wb_softc *, struct wb_chain_onefrag *,
146		struct mbuf *);
147static int wb_encap(struct wb_softc *, struct wb_chain *, struct mbuf *);
148
149static void wb_rxeof(struct wb_softc *);
150static void wb_rxeoc(struct wb_softc *);
151static void wb_txeof(struct wb_softc *);
152static void wb_txeoc(struct wb_softc *);
153static void wb_intr(void *);
154static void wb_tick(void *);
155static void wb_start(struct ifnet *);
156static int wb_ioctl(struct ifnet *, u_long, caddr_t);
157static void wb_init(void *);
158static void wb_stop(struct wb_softc *);
159static void wb_watchdog(struct ifnet *);
160static void wb_shutdown(device_t);
161static int wb_ifmedia_upd(struct ifnet *);
162static void wb_ifmedia_sts(struct ifnet *, struct ifmediareq *);
163
164static void wb_eeprom_putbyte(struct wb_softc *, int);
165static void wb_eeprom_getword(struct wb_softc *, int, u_int16_t *);
166static void wb_read_eeprom(struct wb_softc *, caddr_t, int, int, int);
167static void wb_mii_sync(struct wb_softc *);
168static void wb_mii_send(struct wb_softc *, u_int32_t, int);
169static int wb_mii_readreg(struct wb_softc *, struct wb_mii_frame *);
170static int wb_mii_writereg(struct wb_softc *, struct wb_mii_frame *);
171
172static void wb_setcfg(struct wb_softc *, u_int32_t);
173static void wb_setmulti(struct wb_softc *);
174static void wb_reset(struct wb_softc *);
175static void wb_fixmedia(struct wb_softc *);
176static int wb_list_rx_init(struct wb_softc *);
177static int wb_list_tx_init(struct wb_softc *);
178
179static int wb_miibus_readreg(device_t, int, int);
180static int wb_miibus_writereg(device_t, int, int, int);
181static void wb_miibus_statchg(device_t);
182
183#ifdef WB_USEIOSPACE
184#define WB_RES			SYS_RES_IOPORT
185#define WB_RID			WB_PCI_LOIO
186#else
187#define WB_RES			SYS_RES_MEMORY
188#define WB_RID			WB_PCI_LOMEM
189#endif
190
191static device_method_t wb_methods[] = {
192	/* Device interface */
193	DEVMETHOD(device_probe,		wb_probe),
194	DEVMETHOD(device_attach,	wb_attach),
195	DEVMETHOD(device_detach,	wb_detach),
196	DEVMETHOD(device_shutdown,	wb_shutdown),
197
198	/* bus interface, for miibus */
199	DEVMETHOD(bus_print_child,	bus_generic_print_child),
200	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
201
202	/* MII interface */
203	DEVMETHOD(miibus_readreg,	wb_miibus_readreg),
204	DEVMETHOD(miibus_writereg,	wb_miibus_writereg),
205	DEVMETHOD(miibus_statchg,	wb_miibus_statchg),
206	{ 0, 0 }
207};
208
209static driver_t wb_driver = {
210	"wb",
211	wb_methods,
212	sizeof(struct wb_softc)
213};
214
215static devclass_t wb_devclass;
216
217DRIVER_MODULE(wb, pci, wb_driver, wb_devclass, 0, 0);
218DRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0);
219
220#define WB_SETBIT(sc, reg, x)				\
221	CSR_WRITE_4(sc, reg,				\
222		CSR_READ_4(sc, reg) | (x))
223
224#define WB_CLRBIT(sc, reg, x)				\
225	CSR_WRITE_4(sc, reg,				\
226		CSR_READ_4(sc, reg) & ~(x))
227
228#define SIO_SET(x)					\
229	CSR_WRITE_4(sc, WB_SIO,				\
230		CSR_READ_4(sc, WB_SIO) | (x))
231
232#define SIO_CLR(x)					\
233	CSR_WRITE_4(sc, WB_SIO,				\
234		CSR_READ_4(sc, WB_SIO) & ~(x))
235
236/*
237 * Send a read command and address to the EEPROM, check for ACK.
238 */
239static void
240wb_eeprom_putbyte(sc, addr)
241	struct wb_softc		*sc;
242	int			addr;
243{
244	register int		d, i;
245
246	d = addr | WB_EECMD_READ;
247
248	/*
249	 * Feed in each bit and stobe the clock.
250	 */
251	for (i = 0x400; i; i >>= 1) {
252		if (d & i) {
253			SIO_SET(WB_SIO_EE_DATAIN);
254		} else {
255			SIO_CLR(WB_SIO_EE_DATAIN);
256		}
257		DELAY(100);
258		SIO_SET(WB_SIO_EE_CLK);
259		DELAY(150);
260		SIO_CLR(WB_SIO_EE_CLK);
261		DELAY(100);
262	}
263
264	return;
265}
266
267/*
268 * Read a word of data stored in the EEPROM at address 'addr.'
269 */
270static void
271wb_eeprom_getword(sc, addr, dest)
272	struct wb_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_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
281
282	/*
283	 * Send address of word we want to read.
284	 */
285	wb_eeprom_putbyte(sc, addr);
286
287	CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
288
289	/*
290	 * Start reading bits from EEPROM.
291	 */
292	for (i = 0x8000; i; i >>= 1) {
293		SIO_SET(WB_SIO_EE_CLK);
294		DELAY(100);
295		if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT)
296			word |= i;
297		SIO_CLR(WB_SIO_EE_CLK);
298		DELAY(100);
299	}
300
301	/* Turn off EEPROM access mode. */
302	CSR_WRITE_4(sc, WB_SIO, 0);
303
304	*dest = word;
305
306	return;
307}
308
309/*
310 * Read a sequence of words from the EEPROM.
311 */
312static void
313wb_read_eeprom(sc, dest, off, cnt, swap)
314	struct wb_softc		*sc;
315	caddr_t			dest;
316	int			off;
317	int			cnt;
318	int			swap;
319{
320	int			i;
321	u_int16_t		word = 0, *ptr;
322
323	for (i = 0; i < cnt; i++) {
324		wb_eeprom_getword(sc, off + i, &word);
325		ptr = (u_int16_t *)(dest + (i * 2));
326		if (swap)
327			*ptr = ntohs(word);
328		else
329			*ptr = word;
330	}
331
332	return;
333}
334
335/*
336 * Sync the PHYs by setting data bit and strobing the clock 32 times.
337 */
338static void
339wb_mii_sync(sc)
340	struct wb_softc		*sc;
341{
342	register int		i;
343
344	SIO_SET(WB_SIO_MII_DIR|WB_SIO_MII_DATAIN);
345
346	for (i = 0; i < 32; i++) {
347		SIO_SET(WB_SIO_MII_CLK);
348		DELAY(1);
349		SIO_CLR(WB_SIO_MII_CLK);
350		DELAY(1);
351	}
352
353	return;
354}
355
356/*
357 * Clock a series of bits through the MII.
358 */
359static void
360wb_mii_send(sc, bits, cnt)
361	struct wb_softc		*sc;
362	u_int32_t		bits;
363	int			cnt;
364{
365	int			i;
366
367	SIO_CLR(WB_SIO_MII_CLK);
368
369	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
370                if (bits & i) {
371			SIO_SET(WB_SIO_MII_DATAIN);
372                } else {
373			SIO_CLR(WB_SIO_MII_DATAIN);
374                }
375		DELAY(1);
376		SIO_CLR(WB_SIO_MII_CLK);
377		DELAY(1);
378		SIO_SET(WB_SIO_MII_CLK);
379	}
380}
381
382/*
383 * Read an PHY register through the MII.
384 */
385static int
386wb_mii_readreg(sc, frame)
387	struct wb_softc		*sc;
388	struct wb_mii_frame	*frame;
389
390{
391	int			i, ack;
392
393	WB_LOCK(sc);
394
395	/*
396	 * Set up frame for RX.
397	 */
398	frame->mii_stdelim = WB_MII_STARTDELIM;
399	frame->mii_opcode = WB_MII_READOP;
400	frame->mii_turnaround = 0;
401	frame->mii_data = 0;
402
403	CSR_WRITE_4(sc, WB_SIO, 0);
404
405	/*
406 	 * Turn on data xmit.
407	 */
408	SIO_SET(WB_SIO_MII_DIR);
409
410	wb_mii_sync(sc);
411
412	/*
413	 * Send command/address info.
414	 */
415	wb_mii_send(sc, frame->mii_stdelim, 2);
416	wb_mii_send(sc, frame->mii_opcode, 2);
417	wb_mii_send(sc, frame->mii_phyaddr, 5);
418	wb_mii_send(sc, frame->mii_regaddr, 5);
419
420	/* Idle bit */
421	SIO_CLR((WB_SIO_MII_CLK|WB_SIO_MII_DATAIN));
422	DELAY(1);
423	SIO_SET(WB_SIO_MII_CLK);
424	DELAY(1);
425
426	/* Turn off xmit. */
427	SIO_CLR(WB_SIO_MII_DIR);
428	/* Check for ack */
429	SIO_CLR(WB_SIO_MII_CLK);
430	DELAY(1);
431	ack = CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT;
432	SIO_SET(WB_SIO_MII_CLK);
433	DELAY(1);
434	SIO_CLR(WB_SIO_MII_CLK);
435	DELAY(1);
436	SIO_SET(WB_SIO_MII_CLK);
437	DELAY(1);
438
439	/*
440	 * Now try reading data bits. If the ack failed, we still
441	 * need to clock through 16 cycles to keep the PHY(s) in sync.
442	 */
443	if (ack) {
444		for(i = 0; i < 16; i++) {
445			SIO_CLR(WB_SIO_MII_CLK);
446			DELAY(1);
447			SIO_SET(WB_SIO_MII_CLK);
448			DELAY(1);
449		}
450		goto fail;
451	}
452
453	for (i = 0x8000; i; i >>= 1) {
454		SIO_CLR(WB_SIO_MII_CLK);
455		DELAY(1);
456		if (!ack) {
457			if (CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT)
458				frame->mii_data |= i;
459			DELAY(1);
460		}
461		SIO_SET(WB_SIO_MII_CLK);
462		DELAY(1);
463	}
464
465fail:
466
467	SIO_CLR(WB_SIO_MII_CLK);
468	DELAY(1);
469	SIO_SET(WB_SIO_MII_CLK);
470	DELAY(1);
471
472	WB_UNLOCK(sc);
473
474	if (ack)
475		return(1);
476	return(0);
477}
478
479/*
480 * Write to a PHY register through the MII.
481 */
482static int
483wb_mii_writereg(sc, frame)
484	struct wb_softc		*sc;
485	struct wb_mii_frame	*frame;
486
487{
488	WB_LOCK(sc);
489
490	/*
491	 * Set up frame for TX.
492	 */
493
494	frame->mii_stdelim = WB_MII_STARTDELIM;
495	frame->mii_opcode = WB_MII_WRITEOP;
496	frame->mii_turnaround = WB_MII_TURNAROUND;
497
498	/*
499 	 * Turn on data output.
500	 */
501	SIO_SET(WB_SIO_MII_DIR);
502
503	wb_mii_sync(sc);
504
505	wb_mii_send(sc, frame->mii_stdelim, 2);
506	wb_mii_send(sc, frame->mii_opcode, 2);
507	wb_mii_send(sc, frame->mii_phyaddr, 5);
508	wb_mii_send(sc, frame->mii_regaddr, 5);
509	wb_mii_send(sc, frame->mii_turnaround, 2);
510	wb_mii_send(sc, frame->mii_data, 16);
511
512	/* Idle bit. */
513	SIO_SET(WB_SIO_MII_CLK);
514	DELAY(1);
515	SIO_CLR(WB_SIO_MII_CLK);
516	DELAY(1);
517
518	/*
519	 * Turn off xmit.
520	 */
521	SIO_CLR(WB_SIO_MII_DIR);
522
523	WB_UNLOCK(sc);
524
525	return(0);
526}
527
528static int
529wb_miibus_readreg(dev, phy, reg)
530	device_t		dev;
531	int			phy, reg;
532{
533	struct wb_softc		*sc;
534	struct wb_mii_frame	frame;
535
536	sc = device_get_softc(dev);
537
538	bzero((char *)&frame, sizeof(frame));
539
540	frame.mii_phyaddr = phy;
541	frame.mii_regaddr = reg;
542	wb_mii_readreg(sc, &frame);
543
544	return(frame.mii_data);
545}
546
547static int
548wb_miibus_writereg(dev, phy, reg, data)
549	device_t		dev;
550	int			phy, reg, data;
551{
552	struct wb_softc		*sc;
553	struct wb_mii_frame	frame;
554
555	sc = device_get_softc(dev);
556
557	bzero((char *)&frame, sizeof(frame));
558
559	frame.mii_phyaddr = phy;
560	frame.mii_regaddr = reg;
561	frame.mii_data = data;
562
563	wb_mii_writereg(sc, &frame);
564
565	return(0);
566}
567
568static void
569wb_miibus_statchg(dev)
570	device_t		dev;
571{
572	struct wb_softc		*sc;
573	struct mii_data		*mii;
574
575	sc = device_get_softc(dev);
576	WB_LOCK(sc);
577	mii = device_get_softc(sc->wb_miibus);
578	wb_setcfg(sc, mii->mii_media_active);
579	WB_UNLOCK(sc);
580
581	return;
582}
583
584/*
585 * Program the 64-bit multicast hash filter.
586 */
587static void
588wb_setmulti(sc)
589	struct wb_softc		*sc;
590{
591	struct ifnet		*ifp;
592	int			h = 0;
593	u_int32_t		hashes[2] = { 0, 0 };
594	struct ifmultiaddr	*ifma;
595	u_int32_t		rxfilt;
596	int			mcnt = 0;
597
598	ifp = sc->wb_ifp;
599
600	rxfilt = CSR_READ_4(sc, WB_NETCFG);
601
602	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
603		rxfilt |= WB_NETCFG_RX_MULTI;
604		CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
605		CSR_WRITE_4(sc, WB_MAR0, 0xFFFFFFFF);
606		CSR_WRITE_4(sc, WB_MAR1, 0xFFFFFFFF);
607		return;
608	}
609
610	/* first, zot all the existing hash bits */
611	CSR_WRITE_4(sc, WB_MAR0, 0);
612	CSR_WRITE_4(sc, WB_MAR1, 0);
613
614	/* now program new ones */
615	IF_ADDR_LOCK(ifp);
616	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
617		if (ifma->ifma_addr->sa_family != AF_LINK)
618			continue;
619		h = ~ether_crc32_be(LLADDR((struct sockaddr_dl *)
620		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
621		if (h < 32)
622			hashes[0] |= (1 << h);
623		else
624			hashes[1] |= (1 << (h - 32));
625		mcnt++;
626	}
627	IF_ADDR_UNLOCK(ifp);
628
629	if (mcnt)
630		rxfilt |= WB_NETCFG_RX_MULTI;
631	else
632		rxfilt &= ~WB_NETCFG_RX_MULTI;
633
634	CSR_WRITE_4(sc, WB_MAR0, hashes[0]);
635	CSR_WRITE_4(sc, WB_MAR1, hashes[1]);
636	CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
637
638	return;
639}
640
641/*
642 * The Winbond manual states that in order to fiddle with the
643 * 'full-duplex' and '100Mbps' bits in the netconfig register, we
644 * first have to put the transmit and/or receive logic in the idle state.
645 */
646static void
647wb_setcfg(sc, media)
648	struct wb_softc		*sc;
649	u_int32_t		media;
650{
651	int			i, restart = 0;
652
653	if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) {
654		restart = 1;
655		WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON));
656
657		for (i = 0; i < WB_TIMEOUT; i++) {
658			DELAY(10);
659			if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) &&
660				(CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE))
661				break;
662		}
663
664		if (i == WB_TIMEOUT)
665			if_printf(sc->wb_ifp,
666			    "failed to force tx and rx to idle state\n");
667	}
668
669	if (IFM_SUBTYPE(media) == IFM_10_T)
670		WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
671	else
672		WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
673
674	if ((media & IFM_GMASK) == IFM_FDX)
675		WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
676	else
677		WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
678
679	if (restart)
680		WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON);
681
682	return;
683}
684
685static void
686wb_reset(sc)
687	struct wb_softc		*sc;
688{
689	register int		i;
690	struct mii_data		*mii;
691
692	CSR_WRITE_4(sc, WB_NETCFG, 0);
693	CSR_WRITE_4(sc, WB_BUSCTL, 0);
694	CSR_WRITE_4(sc, WB_TXADDR, 0);
695	CSR_WRITE_4(sc, WB_RXADDR, 0);
696
697	WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
698	WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
699
700	for (i = 0; i < WB_TIMEOUT; i++) {
701		DELAY(10);
702		if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET))
703			break;
704	}
705	if (i == WB_TIMEOUT)
706		if_printf(sc->wb_ifp, "reset never completed!\n");
707
708	/* Wait a little while for the chip to get its brains in order. */
709	DELAY(1000);
710
711	if (sc->wb_miibus == NULL)
712		return;
713
714	mii = device_get_softc(sc->wb_miibus);
715	if (mii == NULL)
716		return;
717
718        if (mii->mii_instance) {
719                struct mii_softc        *miisc;
720                LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
721                        mii_phy_reset(miisc);
722        }
723
724        return;
725}
726
727static void
728wb_fixmedia(sc)
729	struct wb_softc		*sc;
730{
731	struct mii_data		*mii = NULL;
732	struct ifnet		*ifp;
733	u_int32_t		media;
734
735	if (sc->wb_miibus == NULL)
736		return;
737
738	mii = device_get_softc(sc->wb_miibus);
739	ifp = sc->wb_ifp;
740
741	mii_pollstat(mii);
742	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
743		media = mii->mii_media_active & ~IFM_10_T;
744		media |= IFM_100_TX;
745	} else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
746		media = mii->mii_media_active & ~IFM_100_TX;
747		media |= IFM_10_T;
748	} else
749		return;
750
751	ifmedia_set(&mii->mii_media, media);
752
753	return;
754}
755
756/*
757 * Probe for a Winbond chip. Check the PCI vendor and device
758 * IDs against our list and return a device name if we find a match.
759 */
760static int
761wb_probe(dev)
762	device_t		dev;
763{
764	struct wb_type		*t;
765
766	t = wb_devs;
767
768	while(t->wb_name != NULL) {
769		if ((pci_get_vendor(dev) == t->wb_vid) &&
770		    (pci_get_device(dev) == t->wb_did)) {
771			device_set_desc(dev, t->wb_name);
772			return (BUS_PROBE_DEFAULT);
773		}
774		t++;
775	}
776
777	return(ENXIO);
778}
779
780/*
781 * Attach the interface. Allocate softc structures, do ifmedia
782 * setup and ethernet/BPF attach.
783 */
784static int
785wb_attach(dev)
786	device_t		dev;
787{
788	u_char			eaddr[ETHER_ADDR_LEN];
789	struct wb_softc		*sc;
790	struct ifnet		*ifp;
791	int			error = 0, rid;
792
793	sc = device_get_softc(dev);
794
795	mtx_init(&sc->wb_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
796	    MTX_DEF | MTX_RECURSE);
797	/*
798	 * Map control/status registers.
799	 */
800	pci_enable_busmaster(dev);
801
802	rid = WB_RID;
803	sc->wb_res = bus_alloc_resource_any(dev, WB_RES, &rid, RF_ACTIVE);
804
805	if (sc->wb_res == NULL) {
806		device_printf(dev, "couldn't map ports/memory\n");
807		error = ENXIO;
808		goto fail;
809	}
810
811	sc->wb_btag = rman_get_bustag(sc->wb_res);
812	sc->wb_bhandle = rman_get_bushandle(sc->wb_res);
813
814	/* Allocate interrupt */
815	rid = 0;
816	sc->wb_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
817	    RF_SHAREABLE | RF_ACTIVE);
818
819	if (sc->wb_irq == NULL) {
820		device_printf(dev, "couldn't map interrupt\n");
821		error = ENXIO;
822		goto fail;
823	}
824
825	/* Save the cache line size. */
826	sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF;
827
828	/* Reset the adapter. */
829	wb_reset(sc);
830
831	/*
832	 * Get station address from the EEPROM.
833	 */
834	wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
835
836	sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF,
837	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
838
839	if (sc->wb_ldata == NULL) {
840		device_printf(dev, "no memory for list buffers!\n");
841		error = ENXIO;
842		goto fail;
843	}
844
845	bzero(sc->wb_ldata, sizeof(struct wb_list_data));
846
847	ifp = sc->wb_ifp = if_alloc(IFT_ETHER);
848	if (ifp == NULL) {
849		device_printf(dev, "can not if_alloc()\n");
850		error = ENOSPC;
851		goto fail;
852	}
853	ifp->if_softc = sc;
854	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
855	ifp->if_mtu = ETHERMTU;
856	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
857	    IFF_NEEDSGIANT;
858	ifp->if_ioctl = wb_ioctl;
859	ifp->if_start = wb_start;
860	ifp->if_watchdog = wb_watchdog;
861	ifp->if_init = wb_init;
862	ifp->if_baudrate = 10000000;
863	ifp->if_snd.ifq_maxlen = WB_TX_LIST_CNT - 1;
864
865	/*
866	 * Do MII setup.
867	 */
868	if (mii_phy_probe(dev, &sc->wb_miibus,
869	    wb_ifmedia_upd, wb_ifmedia_sts)) {
870		error = ENXIO;
871		goto fail;
872	}
873
874	/*
875	 * Call MI attach routine.
876	 */
877	ether_ifattach(ifp, eaddr);
878
879	/* Hook interrupt last to avoid having to lock softc */
880	error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET,
881	    wb_intr, sc, &sc->wb_intrhand);
882
883	if (error) {
884		device_printf(dev, "couldn't set up irq\n");
885		ether_ifdetach(ifp);
886		goto fail;
887	}
888
889fail:
890	if (error)
891		wb_detach(dev);
892
893	return(error);
894}
895
896/*
897 * Shutdown hardware and free up resources. This can be called any
898 * time after the mutex has been initialized. It is called in both
899 * the error case in attach and the normal detach case so it needs
900 * to be careful about only freeing resources that have actually been
901 * allocated.
902 */
903static int
904wb_detach(dev)
905	device_t		dev;
906{
907	struct wb_softc		*sc;
908	struct ifnet		*ifp;
909
910	sc = device_get_softc(dev);
911	KASSERT(mtx_initialized(&sc->wb_mtx), ("wb mutex not initialized"));
912	WB_LOCK(sc);
913	ifp = sc->wb_ifp;
914
915	/*
916	 * Delete any miibus and phy devices attached to this interface.
917	 * This should only be done if attach succeeded.
918	 */
919	if (device_is_attached(dev)) {
920		wb_stop(sc);
921		ether_ifdetach(ifp);
922	}
923	if (ifp)
924		if_free(ifp);
925	if (sc->wb_miibus)
926		device_delete_child(dev, sc->wb_miibus);
927	bus_generic_detach(dev);
928
929	if (sc->wb_intrhand)
930		bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand);
931	if (sc->wb_irq)
932		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
933	if (sc->wb_res)
934		bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
935
936	if (sc->wb_ldata) {
937		contigfree(sc->wb_ldata, sizeof(struct wb_list_data) + 8,
938		    M_DEVBUF);
939	}
940
941	WB_UNLOCK(sc);
942	mtx_destroy(&sc->wb_mtx);
943
944	return(0);
945}
946
947/*
948 * Initialize the transmit descriptors.
949 */
950static int
951wb_list_tx_init(sc)
952	struct wb_softc		*sc;
953{
954	struct wb_chain_data	*cd;
955	struct wb_list_data	*ld;
956	int			i;
957
958	cd = &sc->wb_cdata;
959	ld = sc->wb_ldata;
960
961	for (i = 0; i < WB_TX_LIST_CNT; i++) {
962		cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i];
963		if (i == (WB_TX_LIST_CNT - 1)) {
964			cd->wb_tx_chain[i].wb_nextdesc =
965				&cd->wb_tx_chain[0];
966		} else {
967			cd->wb_tx_chain[i].wb_nextdesc =
968				&cd->wb_tx_chain[i + 1];
969		}
970	}
971
972	cd->wb_tx_free = &cd->wb_tx_chain[0];
973	cd->wb_tx_tail = cd->wb_tx_head = NULL;
974
975	return(0);
976}
977
978
979/*
980 * Initialize the RX descriptors and allocate mbufs for them. Note that
981 * we arrange the descriptors in a closed ring, so that the last descriptor
982 * points back to the first.
983 */
984static int
985wb_list_rx_init(sc)
986	struct wb_softc		*sc;
987{
988	struct wb_chain_data	*cd;
989	struct wb_list_data	*ld;
990	int			i;
991
992	cd = &sc->wb_cdata;
993	ld = sc->wb_ldata;
994
995	for (i = 0; i < WB_RX_LIST_CNT; i++) {
996		cd->wb_rx_chain[i].wb_ptr =
997			(struct wb_desc *)&ld->wb_rx_list[i];
998		cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i];
999		if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS)
1000			return(ENOBUFS);
1001		if (i == (WB_RX_LIST_CNT - 1)) {
1002			cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0];
1003			ld->wb_rx_list[i].wb_next =
1004					vtophys(&ld->wb_rx_list[0]);
1005		} else {
1006			cd->wb_rx_chain[i].wb_nextdesc =
1007					&cd->wb_rx_chain[i + 1];
1008			ld->wb_rx_list[i].wb_next =
1009					vtophys(&ld->wb_rx_list[i + 1]);
1010		}
1011	}
1012
1013	cd->wb_rx_head = &cd->wb_rx_chain[0];
1014
1015	return(0);
1016}
1017
1018static void
1019wb_bfree(buf, args)
1020	void			*buf;
1021	void			*args;
1022{
1023	return;
1024}
1025
1026/*
1027 * Initialize an RX descriptor and attach an MBUF cluster.
1028 */
1029static int
1030wb_newbuf(sc, c, m)
1031	struct wb_softc		*sc;
1032	struct wb_chain_onefrag	*c;
1033	struct mbuf		*m;
1034{
1035	struct mbuf		*m_new = NULL;
1036
1037	if (m == NULL) {
1038		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1039		if (m_new == NULL)
1040			return(ENOBUFS);
1041		m_new->m_data = c->wb_buf;
1042		m_new->m_pkthdr.len = m_new->m_len = WB_BUFBYTES;
1043		MEXTADD(m_new, c->wb_buf, WB_BUFBYTES, wb_bfree, NULL, 0,
1044		    EXT_NET_DRV);
1045	} else {
1046		m_new = m;
1047		m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES;
1048		m_new->m_data = m_new->m_ext.ext_buf;
1049	}
1050
1051	m_adj(m_new, sizeof(u_int64_t));
1052
1053	c->wb_mbuf = m_new;
1054	c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t));
1055	c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536;
1056	c->wb_ptr->wb_status = WB_RXSTAT;
1057
1058	return(0);
1059}
1060
1061/*
1062 * A frame has been uploaded: pass the resulting mbuf chain up to
1063 * the higher level protocols.
1064 */
1065static void
1066wb_rxeof(sc)
1067	struct wb_softc		*sc;
1068{
1069        struct mbuf		*m = NULL;
1070        struct ifnet		*ifp;
1071	struct wb_chain_onefrag	*cur_rx;
1072	int			total_len = 0;
1073	u_int32_t		rxstat;
1074
1075	WB_LOCK_ASSERT(sc);
1076
1077	ifp = sc->wb_ifp;
1078
1079	while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) &
1080							WB_RXSTAT_OWN)) {
1081		struct mbuf		*m0 = NULL;
1082
1083		cur_rx = sc->wb_cdata.wb_rx_head;
1084		sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc;
1085
1086		m = cur_rx->wb_mbuf;
1087
1088		if ((rxstat & WB_RXSTAT_MIIERR) ||
1089		    (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) ||
1090		    (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) ||
1091		    !(rxstat & WB_RXSTAT_LASTFRAG) ||
1092		    !(rxstat & WB_RXSTAT_RXCMP)) {
1093			ifp->if_ierrors++;
1094			wb_newbuf(sc, cur_rx, m);
1095			if_printf(ifp, "receiver babbling: possible chip "
1096				"bug, forcing reset\n");
1097			wb_fixmedia(sc);
1098			wb_reset(sc);
1099			wb_init(sc);
1100			return;
1101		}
1102
1103		if (rxstat & WB_RXSTAT_RXERR) {
1104			ifp->if_ierrors++;
1105			wb_newbuf(sc, cur_rx, m);
1106			break;
1107		}
1108
1109		/* No errors; receive the packet. */
1110		total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status);
1111
1112		/*
1113		 * XXX The Winbond chip includes the CRC with every
1114		 * received frame, and there's no way to turn this
1115		 * behavior off (at least, I can't find anything in
1116	 	 * the manual that explains how to do it) so we have
1117		 * to trim off the CRC manually.
1118		 */
1119		total_len -= ETHER_CRC_LEN;
1120
1121		m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp,
1122		    NULL);
1123		wb_newbuf(sc, cur_rx, m);
1124		if (m0 == NULL) {
1125			ifp->if_ierrors++;
1126			break;
1127		}
1128		m = m0;
1129
1130		ifp->if_ipackets++;
1131		WB_UNLOCK(sc);
1132		(*ifp->if_input)(ifp, m);
1133		WB_LOCK(sc);
1134	}
1135}
1136
1137static void
1138wb_rxeoc(sc)
1139	struct wb_softc		*sc;
1140{
1141	wb_rxeof(sc);
1142
1143	WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1144	CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
1145	WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1146	if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND)
1147		CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
1148
1149	return;
1150}
1151
1152/*
1153 * A frame was downloaded to the chip. It's safe for us to clean up
1154 * the list buffers.
1155 */
1156static void
1157wb_txeof(sc)
1158	struct wb_softc		*sc;
1159{
1160	struct wb_chain		*cur_tx;
1161	struct ifnet		*ifp;
1162
1163	ifp = sc->wb_ifp;
1164
1165	/* Clear the timeout timer. */
1166	ifp->if_timer = 0;
1167
1168	if (sc->wb_cdata.wb_tx_head == NULL)
1169		return;
1170
1171	/*
1172	 * Go through our tx list and free mbufs for those
1173	 * frames that have been transmitted.
1174	 */
1175	while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) {
1176		u_int32_t		txstat;
1177
1178		cur_tx = sc->wb_cdata.wb_tx_head;
1179		txstat = WB_TXSTATUS(cur_tx);
1180
1181		if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT)
1182			break;
1183
1184		if (txstat & WB_TXSTAT_TXERR) {
1185			ifp->if_oerrors++;
1186			if (txstat & WB_TXSTAT_ABORT)
1187				ifp->if_collisions++;
1188			if (txstat & WB_TXSTAT_LATECOLL)
1189				ifp->if_collisions++;
1190		}
1191
1192		ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3;
1193
1194		ifp->if_opackets++;
1195		m_freem(cur_tx->wb_mbuf);
1196		cur_tx->wb_mbuf = NULL;
1197
1198		if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) {
1199			sc->wb_cdata.wb_tx_head = NULL;
1200			sc->wb_cdata.wb_tx_tail = NULL;
1201			break;
1202		}
1203
1204		sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc;
1205	}
1206
1207	return;
1208}
1209
1210/*
1211 * TX 'end of channel' interrupt handler.
1212 */
1213static void
1214wb_txeoc(sc)
1215	struct wb_softc		*sc;
1216{
1217	struct ifnet		*ifp;
1218
1219	ifp = sc->wb_ifp;
1220
1221	ifp->if_timer = 0;
1222
1223	if (sc->wb_cdata.wb_tx_head == NULL) {
1224		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1225		sc->wb_cdata.wb_tx_tail = NULL;
1226	} else {
1227		if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) {
1228			WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN;
1229			ifp->if_timer = 5;
1230			CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1231		}
1232	}
1233
1234	return;
1235}
1236
1237static void
1238wb_intr(arg)
1239	void			*arg;
1240{
1241	struct wb_softc		*sc;
1242	struct ifnet		*ifp;
1243	u_int32_t		status;
1244
1245	sc = arg;
1246	WB_LOCK(sc);
1247	ifp = sc->wb_ifp;
1248
1249	if (!(ifp->if_flags & IFF_UP)) {
1250		WB_UNLOCK(sc);
1251		return;
1252	}
1253
1254	/* Disable interrupts. */
1255	CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1256
1257	for (;;) {
1258
1259		status = CSR_READ_4(sc, WB_ISR);
1260		if (status)
1261			CSR_WRITE_4(sc, WB_ISR, status);
1262
1263		if ((status & WB_INTRS) == 0)
1264			break;
1265
1266		if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) {
1267			ifp->if_ierrors++;
1268			wb_reset(sc);
1269			if (status & WB_ISR_RX_ERR)
1270				wb_fixmedia(sc);
1271			wb_init(sc);
1272			continue;
1273		}
1274
1275		if (status & WB_ISR_RX_OK)
1276			wb_rxeof(sc);
1277
1278		if (status & WB_ISR_RX_IDLE)
1279			wb_rxeoc(sc);
1280
1281		if (status & WB_ISR_TX_OK)
1282			wb_txeof(sc);
1283
1284		if (status & WB_ISR_TX_NOBUF)
1285			wb_txeoc(sc);
1286
1287		if (status & WB_ISR_TX_IDLE) {
1288			wb_txeof(sc);
1289			if (sc->wb_cdata.wb_tx_head != NULL) {
1290				WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1291				CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1292			}
1293		}
1294
1295		if (status & WB_ISR_TX_UNDERRUN) {
1296			ifp->if_oerrors++;
1297			wb_txeof(sc);
1298			WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1299			/* Jack up TX threshold */
1300			sc->wb_txthresh += WB_TXTHRESH_CHUNK;
1301			WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1302			WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1303			WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1304		}
1305
1306		if (status & WB_ISR_BUS_ERR) {
1307			wb_reset(sc);
1308			wb_init(sc);
1309		}
1310
1311	}
1312
1313	/* Re-enable interrupts. */
1314	CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1315
1316	if (ifp->if_snd.ifq_head != NULL) {
1317		wb_start(ifp);
1318	}
1319
1320	WB_UNLOCK(sc);
1321
1322	return;
1323}
1324
1325static void
1326wb_tick(xsc)
1327	void			*xsc;
1328{
1329	struct wb_softc		*sc;
1330	struct mii_data		*mii;
1331
1332	sc = xsc;
1333	WB_LOCK(sc);
1334	mii = device_get_softc(sc->wb_miibus);
1335
1336	mii_tick(mii);
1337
1338	sc->wb_stat_ch = timeout(wb_tick, sc, hz);
1339
1340	WB_UNLOCK(sc);
1341
1342	return;
1343}
1344
1345/*
1346 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1347 * pointers to the fragment pointers.
1348 */
1349static int
1350wb_encap(sc, c, m_head)
1351	struct wb_softc		*sc;
1352	struct wb_chain		*c;
1353	struct mbuf		*m_head;
1354{
1355	int			frag = 0;
1356	struct wb_desc		*f = NULL;
1357	int			total_len;
1358	struct mbuf		*m;
1359
1360	/*
1361 	 * Start packing the mbufs in this chain into
1362	 * the fragment pointers. Stop when we run out
1363 	 * of fragments or hit the end of the mbuf chain.
1364	 */
1365	m = m_head;
1366	total_len = 0;
1367
1368	for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1369		if (m->m_len != 0) {
1370			if (frag == WB_MAXFRAGS)
1371				break;
1372			total_len += m->m_len;
1373			f = &c->wb_ptr->wb_frag[frag];
1374			f->wb_ctl = WB_TXCTL_TLINK | m->m_len;
1375			if (frag == 0) {
1376				f->wb_ctl |= WB_TXCTL_FIRSTFRAG;
1377				f->wb_status = 0;
1378			} else
1379				f->wb_status = WB_TXSTAT_OWN;
1380			f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]);
1381			f->wb_data = vtophys(mtod(m, vm_offset_t));
1382			frag++;
1383		}
1384	}
1385
1386	/*
1387	 * Handle special case: we used up all 16 fragments,
1388	 * but we have more mbufs left in the chain. Copy the
1389	 * data into an mbuf cluster. Note that we don't
1390	 * bother clearing the values in the other fragment
1391	 * pointers/counters; it wouldn't gain us anything,
1392	 * and would waste cycles.
1393	 */
1394	if (m != NULL) {
1395		struct mbuf		*m_new = NULL;
1396
1397		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1398		if (m_new == NULL)
1399			return(1);
1400		if (m_head->m_pkthdr.len > MHLEN) {
1401			MCLGET(m_new, M_DONTWAIT);
1402			if (!(m_new->m_flags & M_EXT)) {
1403				m_freem(m_new);
1404				return(1);
1405			}
1406		}
1407		m_copydata(m_head, 0, m_head->m_pkthdr.len,
1408					mtod(m_new, caddr_t));
1409		m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1410		m_freem(m_head);
1411		m_head = m_new;
1412		f = &c->wb_ptr->wb_frag[0];
1413		f->wb_status = 0;
1414		f->wb_data = vtophys(mtod(m_new, caddr_t));
1415		f->wb_ctl = total_len = m_new->m_len;
1416		f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG;
1417		frag = 1;
1418	}
1419
1420	if (total_len < WB_MIN_FRAMELEN) {
1421		f = &c->wb_ptr->wb_frag[frag];
1422		f->wb_ctl = WB_MIN_FRAMELEN - total_len;
1423		f->wb_data = vtophys(&sc->wb_cdata.wb_pad);
1424		f->wb_ctl |= WB_TXCTL_TLINK;
1425		f->wb_status = WB_TXSTAT_OWN;
1426		frag++;
1427	}
1428
1429	c->wb_mbuf = m_head;
1430	c->wb_lastdesc = frag - 1;
1431	WB_TXCTL(c) |= WB_TXCTL_LASTFRAG;
1432	WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]);
1433
1434	return(0);
1435}
1436
1437/*
1438 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1439 * to the mbuf data regions directly in the transmit lists. We also save a
1440 * copy of the pointers since the transmit list fragment pointers are
1441 * physical addresses.
1442 */
1443
1444static void
1445wb_start(ifp)
1446	struct ifnet		*ifp;
1447{
1448	struct wb_softc		*sc;
1449	struct mbuf		*m_head = NULL;
1450	struct wb_chain		*cur_tx = NULL, *start_tx;
1451
1452	sc = ifp->if_softc;
1453	WB_LOCK(sc);
1454
1455	/*
1456	 * Check for an available queue slot. If there are none,
1457	 * punt.
1458	 */
1459	if (sc->wb_cdata.wb_tx_free->wb_mbuf != NULL) {
1460		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1461		WB_UNLOCK(sc);
1462		return;
1463	}
1464
1465	start_tx = sc->wb_cdata.wb_tx_free;
1466
1467	while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) {
1468		IF_DEQUEUE(&ifp->if_snd, m_head);
1469		if (m_head == NULL)
1470			break;
1471
1472		/* Pick a descriptor off the free list. */
1473		cur_tx = sc->wb_cdata.wb_tx_free;
1474		sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc;
1475
1476		/* Pack the data into the descriptor. */
1477		wb_encap(sc, cur_tx, m_head);
1478
1479		if (cur_tx != start_tx)
1480			WB_TXOWN(cur_tx) = WB_TXSTAT_OWN;
1481
1482		/*
1483		 * If there's a BPF listener, bounce a copy of this frame
1484		 * to him.
1485		 */
1486		BPF_MTAP(ifp, cur_tx->wb_mbuf);
1487	}
1488
1489	/*
1490	 * If there are no packets queued, bail.
1491	 */
1492	if (cur_tx == NULL) {
1493		WB_UNLOCK(sc);
1494		return;
1495	}
1496
1497	/*
1498	 * Place the request for the upload interrupt
1499	 * in the last descriptor in the chain. This way, if
1500	 * we're chaining several packets at once, we'll only
1501	 * get an interupt once for the whole chain rather than
1502	 * once for each packet.
1503	 */
1504	WB_TXCTL(cur_tx) |= WB_TXCTL_FINT;
1505	cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT;
1506	sc->wb_cdata.wb_tx_tail = cur_tx;
1507
1508	if (sc->wb_cdata.wb_tx_head == NULL) {
1509		sc->wb_cdata.wb_tx_head = start_tx;
1510		WB_TXOWN(start_tx) = WB_TXSTAT_OWN;
1511		CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1512	} else {
1513		/*
1514		 * We need to distinguish between the case where
1515		 * the own bit is clear because the chip cleared it
1516		 * and where the own bit is clear because we haven't
1517		 * set it yet. The magic value WB_UNSET is just some
1518		 * ramdomly chosen number which doesn't have the own
1519	 	 * bit set. When we actually transmit the frame, the
1520		 * status word will have _only_ the own bit set, so
1521		 * the txeoc handler will be able to tell if it needs
1522		 * to initiate another transmission to flush out pending
1523		 * frames.
1524		 */
1525		WB_TXOWN(start_tx) = WB_UNSENT;
1526	}
1527
1528	/*
1529	 * Set a timeout in case the chip goes out to lunch.
1530	 */
1531	ifp->if_timer = 5;
1532	WB_UNLOCK(sc);
1533
1534	return;
1535}
1536
1537static void
1538wb_init(xsc)
1539	void			*xsc;
1540{
1541	struct wb_softc		*sc = xsc;
1542	struct ifnet		*ifp = sc->wb_ifp;
1543	int			i;
1544	struct mii_data		*mii;
1545
1546	WB_LOCK(sc);
1547	mii = device_get_softc(sc->wb_miibus);
1548
1549	/*
1550	 * Cancel pending I/O and free all RX/TX buffers.
1551	 */
1552	wb_stop(sc);
1553	wb_reset(sc);
1554
1555	sc->wb_txthresh = WB_TXTHRESH_INIT;
1556
1557	/*
1558	 * Set cache alignment and burst length.
1559	 */
1560#ifdef foo
1561	CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG);
1562	WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1563	WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1564#endif
1565
1566	CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION);
1567	WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG);
1568	switch(sc->wb_cachesize) {
1569	case 32:
1570		WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG);
1571		break;
1572	case 16:
1573		WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG);
1574		break;
1575	case 8:
1576		WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG);
1577		break;
1578	case 0:
1579	default:
1580		WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE);
1581		break;
1582	}
1583
1584	/* This doesn't tend to work too well at 100Mbps. */
1585	WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON);
1586
1587	/* Init our MAC address */
1588	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1589		CSR_WRITE_1(sc, WB_NODE0 + i, IFP2ENADDR(sc->wb_ifp)[i]);
1590	}
1591
1592	/* Init circular RX list. */
1593	if (wb_list_rx_init(sc) == ENOBUFS) {
1594		if_printf(ifp,
1595		    "initialization failed: no memory for rx buffers\n");
1596		wb_stop(sc);
1597		WB_UNLOCK(sc);
1598		return;
1599	}
1600
1601	/* Init TX descriptors. */
1602	wb_list_tx_init(sc);
1603
1604	/* If we want promiscuous mode, set the allframes bit. */
1605	if (ifp->if_flags & IFF_PROMISC) {
1606		WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1607	} else {
1608		WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1609	}
1610
1611	/*
1612	 * Set capture broadcast bit to capture broadcast frames.
1613	 */
1614	if (ifp->if_flags & IFF_BROADCAST) {
1615		WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1616	} else {
1617		WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1618	}
1619
1620	/*
1621	 * Program the multicast filter, if necessary.
1622	 */
1623	wb_setmulti(sc);
1624
1625	/*
1626	 * Load the address of the RX list.
1627	 */
1628	WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1629	CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
1630
1631	/*
1632	 * Enable interrupts.
1633	 */
1634	CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1635	CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF);
1636
1637	/* Enable receiver and transmitter. */
1638	WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1639	CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
1640
1641	WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1642	CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0]));
1643	WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1644
1645	mii_mediachg(mii);
1646
1647	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1648	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1649
1650	sc->wb_stat_ch = timeout(wb_tick, sc, hz);
1651	WB_UNLOCK(sc);
1652
1653	return;
1654}
1655
1656/*
1657 * Set media options.
1658 */
1659static int
1660wb_ifmedia_upd(ifp)
1661	struct ifnet		*ifp;
1662{
1663	struct wb_softc		*sc;
1664
1665	sc = ifp->if_softc;
1666
1667	if (ifp->if_flags & IFF_UP)
1668		wb_init(sc);
1669
1670	return(0);
1671}
1672
1673/*
1674 * Report current media status.
1675 */
1676static void
1677wb_ifmedia_sts(ifp, ifmr)
1678	struct ifnet		*ifp;
1679	struct ifmediareq	*ifmr;
1680{
1681	struct wb_softc		*sc;
1682	struct mii_data		*mii;
1683
1684	sc = ifp->if_softc;
1685
1686	mii = device_get_softc(sc->wb_miibus);
1687
1688	mii_pollstat(mii);
1689	ifmr->ifm_active = mii->mii_media_active;
1690	ifmr->ifm_status = mii->mii_media_status;
1691
1692	return;
1693}
1694
1695static int
1696wb_ioctl(ifp, command, data)
1697	struct ifnet		*ifp;
1698	u_long			command;
1699	caddr_t			data;
1700{
1701	struct wb_softc		*sc = ifp->if_softc;
1702	struct mii_data		*mii;
1703	struct ifreq		*ifr = (struct ifreq *) data;
1704	int			error = 0;
1705
1706	WB_LOCK(sc);
1707
1708	switch(command) {
1709	case SIOCSIFFLAGS:
1710		if (ifp->if_flags & IFF_UP) {
1711			wb_init(sc);
1712		} else {
1713			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1714				wb_stop(sc);
1715		}
1716		error = 0;
1717		break;
1718	case SIOCADDMULTI:
1719	case SIOCDELMULTI:
1720		wb_setmulti(sc);
1721		error = 0;
1722		break;
1723	case SIOCGIFMEDIA:
1724	case SIOCSIFMEDIA:
1725		mii = device_get_softc(sc->wb_miibus);
1726		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1727		break;
1728	default:
1729		error = ether_ioctl(ifp, command, data);
1730		break;
1731	}
1732
1733	WB_UNLOCK(sc);
1734
1735	return(error);
1736}
1737
1738static void
1739wb_watchdog(ifp)
1740	struct ifnet		*ifp;
1741{
1742	struct wb_softc		*sc;
1743
1744	sc = ifp->if_softc;
1745
1746	WB_LOCK(sc);
1747	ifp->if_oerrors++;
1748	if_printf(ifp, "watchdog timeout\n");
1749#ifdef foo
1750	if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
1751		if_printf(ifp, "no carrier - transceiver cable problem?\n");
1752#endif
1753	wb_stop(sc);
1754	wb_reset(sc);
1755	wb_init(sc);
1756
1757	if (ifp->if_snd.ifq_head != NULL)
1758		wb_start(ifp);
1759	WB_UNLOCK(sc);
1760
1761	return;
1762}
1763
1764/*
1765 * Stop the adapter and free any mbufs allocated to the
1766 * RX and TX lists.
1767 */
1768static void
1769wb_stop(sc)
1770	struct wb_softc		*sc;
1771{
1772	register int		i;
1773	struct ifnet		*ifp;
1774
1775	WB_LOCK(sc);
1776	ifp = sc->wb_ifp;
1777	ifp->if_timer = 0;
1778
1779	untimeout(wb_tick, sc, sc->wb_stat_ch);
1780
1781	WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON));
1782	CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1783	CSR_WRITE_4(sc, WB_TXADDR, 0x00000000);
1784	CSR_WRITE_4(sc, WB_RXADDR, 0x00000000);
1785
1786	/*
1787	 * Free data in the RX lists.
1788	 */
1789	for (i = 0; i < WB_RX_LIST_CNT; i++) {
1790		if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) {
1791			m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf);
1792			sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL;
1793		}
1794	}
1795	bzero((char *)&sc->wb_ldata->wb_rx_list,
1796		sizeof(sc->wb_ldata->wb_rx_list));
1797
1798	/*
1799	 * Free the TX list buffers.
1800	 */
1801	for (i = 0; i < WB_TX_LIST_CNT; i++) {
1802		if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) {
1803			m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf);
1804			sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL;
1805		}
1806	}
1807
1808	bzero((char *)&sc->wb_ldata->wb_tx_list,
1809		sizeof(sc->wb_ldata->wb_tx_list));
1810
1811	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1812	WB_UNLOCK(sc);
1813
1814	return;
1815}
1816
1817/*
1818 * Stop all chip I/O so that the kernel's probe routines don't
1819 * get confused by errant DMAs when rebooting.
1820 */
1821static void
1822wb_shutdown(dev)
1823	device_t		dev;
1824{
1825	struct wb_softc		*sc;
1826
1827	sc = device_get_softc(dev);
1828	wb_stop(sc);
1829
1830	return;
1831}
1832