if_vr.c revision 127901
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/dev/vr/if_vr.c 127901 2004-04-05 17:39:57Z ru $");
35
36/*
37 * VIA Rhine fast ethernet PCI NIC driver
38 *
39 * Supports various network adapters based on the VIA Rhine
40 * and Rhine II PCI controllers, including the D-Link DFE530TX.
41 * Datasheets are available at http://www.via.com.tw.
42 *
43 * Written by Bill Paul <wpaul@ctr.columbia.edu>
44 * Electrical Engineering Department
45 * Columbia University, New York City
46 */
47/*
48 * The VIA Rhine controllers are similar in some respects to the
49 * the DEC tulip chips, except less complicated. The controller
50 * uses an MII bus and an external physical layer interface. The
51 * receiver has a one entry perfect filter and a 64-bit hash table
52 * multicast filter. Transmit and receive descriptors are similar
53 * to the tulip.
54 *
55 * The Rhine has a serious flaw in its transmit DMA mechanism:
56 * transmit buffers must be longword aligned. Unfortunately,
57 * FreeBSD doesn't guarantee that mbufs will be filled in starting
58 * at longword boundaries, so we have to do a buffer copy before
59 * transmission.
60 */
61
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <sys/sockio.h>
65#include <sys/mbuf.h>
66#include <sys/malloc.h>
67#include <sys/kernel.h>
68#include <sys/socket.h>
69
70#include <net/if.h>
71#include <net/if_arp.h>
72#include <net/ethernet.h>
73#include <net/if_dl.h>
74#include <net/if_media.h>
75
76#include <net/bpf.h>
77
78#include <vm/vm.h>              /* for vtophys */
79#include <vm/pmap.h>            /* for vtophys */
80#include <machine/bus_pio.h>
81#include <machine/bus_memio.h>
82#include <machine/bus.h>
83#include <machine/resource.h>
84#include <sys/bus.h>
85#include <sys/rman.h>
86
87#include <dev/mii/mii.h>
88#include <dev/mii/miivar.h>
89
90#include <dev/pci/pcireg.h>
91#include <dev/pci/pcivar.h>
92
93#define VR_USEIOSPACE
94
95#include <pci/if_vrreg.h>
96
97MODULE_DEPEND(vr, pci, 1, 1, 1);
98MODULE_DEPEND(vr, ether, 1, 1, 1);
99MODULE_DEPEND(vr, miibus, 1, 1, 1);
100
101/* "controller miibus0" required.  See GENERIC if you get errors here. */
102#include "miibus_if.h"
103
104#undef VR_USESWSHIFT
105
106/*
107 * Various supported device vendors/types and their names.
108 */
109static struct vr_type vr_devs[] = {
110	{ VIA_VENDORID, VIA_DEVICEID_RHINE,
111		"VIA VT3043 Rhine I 10/100BaseTX" },
112	{ VIA_VENDORID, VIA_DEVICEID_RHINE_II,
113		"VIA VT86C100A Rhine II 10/100BaseTX" },
114	{ VIA_VENDORID, VIA_DEVICEID_RHINE_II_2,
115		"VIA VT6102 Rhine II 10/100BaseTX" },
116	{ VIA_VENDORID, VIA_DEVICEID_RHINE_III,
117		"VIA VT6105 Rhine III 10/100BaseTX" },
118	{ VIA_VENDORID, VIA_DEVICEID_RHINE_III_M,
119		"VIA VT6105M Rhine III 10/100BaseTX" },
120	{ DELTA_VENDORID, DELTA_DEVICEID_RHINE_II,
121		"Delta Electronics Rhine II 10/100BaseTX" },
122	{ ADDTRON_VENDORID, ADDTRON_DEVICEID_RHINE_II,
123		"Addtron Technology Rhine II 10/100BaseTX" },
124	{ 0, 0, NULL }
125};
126
127static int vr_probe		(device_t);
128static int vr_attach		(device_t);
129static int vr_detach		(device_t);
130
131static int vr_newbuf		(struct vr_softc *,
132					struct vr_chain_onefrag *,
133					struct mbuf *);
134static int vr_encap		(struct vr_softc *, struct vr_chain *,
135						struct mbuf * );
136
137static void vr_rxeof		(struct vr_softc *);
138static void vr_rxeoc		(struct vr_softc *);
139static void vr_txeof		(struct vr_softc *);
140static void vr_tick		(void *);
141static void vr_intr		(void *);
142static void vr_start		(struct ifnet *);
143static int vr_ioctl		(struct ifnet *, u_long, caddr_t);
144static void vr_init		(void *);
145static void vr_stop		(struct vr_softc *);
146static void vr_watchdog		(struct ifnet *);
147static void vr_shutdown		(device_t);
148static int vr_ifmedia_upd	(struct ifnet *);
149static void vr_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
150
151#ifdef VR_USESWSHIFT
152static void vr_mii_sync		(struct vr_softc *);
153static void vr_mii_send		(struct vr_softc *, u_int32_t, int);
154#endif
155static int vr_mii_readreg	(struct vr_softc *, struct vr_mii_frame *);
156static int vr_mii_writereg	(struct vr_softc *, struct vr_mii_frame *);
157static int vr_miibus_readreg	(device_t, int, int);
158static int vr_miibus_writereg	(device_t, int, int, int);
159static void vr_miibus_statchg	(device_t);
160
161static void vr_setcfg		(struct vr_softc *, int);
162static uint32_t vr_mchash	(const uint8_t *);
163static void vr_setmulti		(struct vr_softc *);
164static void vr_reset		(struct vr_softc *);
165static int vr_list_rx_init	(struct vr_softc *);
166static int vr_list_tx_init	(struct vr_softc *);
167
168#ifdef VR_USEIOSPACE
169#define VR_RES			SYS_RES_IOPORT
170#define VR_RID			VR_PCI_LOIO
171#else
172#define VR_RES			SYS_RES_MEMORY
173#define VR_RID			VR_PCI_LOMEM
174#endif
175
176static device_method_t vr_methods[] = {
177	/* Device interface */
178	DEVMETHOD(device_probe,		vr_probe),
179	DEVMETHOD(device_attach,	vr_attach),
180	DEVMETHOD(device_detach, 	vr_detach),
181	DEVMETHOD(device_shutdown,	vr_shutdown),
182
183	/* bus interface */
184	DEVMETHOD(bus_print_child,	bus_generic_print_child),
185	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
186
187	/* MII interface */
188	DEVMETHOD(miibus_readreg,	vr_miibus_readreg),
189	DEVMETHOD(miibus_writereg,	vr_miibus_writereg),
190	DEVMETHOD(miibus_statchg,	vr_miibus_statchg),
191
192	{ 0, 0 }
193};
194
195static driver_t vr_driver = {
196	"vr",
197	vr_methods,
198	sizeof(struct vr_softc)
199};
200
201static devclass_t vr_devclass;
202
203DRIVER_MODULE(vr, pci, vr_driver, vr_devclass, 0, 0);
204DRIVER_MODULE(miibus, vr, miibus_driver, miibus_devclass, 0, 0);
205
206#define VR_SETBIT(sc, reg, x)				\
207	CSR_WRITE_1(sc, reg,				\
208		CSR_READ_1(sc, reg) | (x))
209
210#define VR_CLRBIT(sc, reg, x)				\
211	CSR_WRITE_1(sc, reg,				\
212		CSR_READ_1(sc, reg) & ~(x))
213
214#define VR_SETBIT16(sc, reg, x)				\
215	CSR_WRITE_2(sc, reg,				\
216		CSR_READ_2(sc, reg) | (x))
217
218#define VR_CLRBIT16(sc, reg, x)				\
219	CSR_WRITE_2(sc, reg,				\
220		CSR_READ_2(sc, reg) & ~(x))
221
222#define VR_SETBIT32(sc, reg, x)				\
223	CSR_WRITE_4(sc, reg,				\
224		CSR_READ_4(sc, reg) | (x))
225
226#define VR_CLRBIT32(sc, reg, x)				\
227	CSR_WRITE_4(sc, reg,				\
228		CSR_READ_4(sc, reg) & ~(x))
229
230#define SIO_SET(x)					\
231	CSR_WRITE_1(sc, VR_MIICMD,			\
232		CSR_READ_1(sc, VR_MIICMD) | (x))
233
234#define SIO_CLR(x)					\
235	CSR_WRITE_1(sc, VR_MIICMD,			\
236		CSR_READ_1(sc, VR_MIICMD) & ~(x))
237
238#ifdef VR_USESWSHIFT
239/*
240 * Sync the PHYs by setting data bit and strobing the clock 32 times.
241 */
242static void
243vr_mii_sync(sc)
244	struct vr_softc		*sc;
245{
246	register int		i;
247
248	SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAIN);
249
250	for (i = 0; i < 32; i++) {
251		SIO_SET(VR_MIICMD_CLK);
252		DELAY(1);
253		SIO_CLR(VR_MIICMD_CLK);
254		DELAY(1);
255	}
256
257	return;
258}
259
260/*
261 * Clock a series of bits through the MII.
262 */
263static void
264vr_mii_send(sc, bits, cnt)
265	struct vr_softc		*sc;
266	u_int32_t		bits;
267	int			cnt;
268{
269	int			i;
270
271	SIO_CLR(VR_MIICMD_CLK);
272
273	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
274                if (bits & i) {
275			SIO_SET(VR_MIICMD_DATAIN);
276                } else {
277			SIO_CLR(VR_MIICMD_DATAIN);
278                }
279		DELAY(1);
280		SIO_CLR(VR_MIICMD_CLK);
281		DELAY(1);
282		SIO_SET(VR_MIICMD_CLK);
283	}
284}
285#endif
286
287/*
288 * Read an PHY register through the MII.
289 */
290static int
291vr_mii_readreg(sc, frame)
292	struct vr_softc		*sc;
293	struct vr_mii_frame	*frame;
294
295#ifdef VR_USESWSHIFT
296{
297	int			i, ack;
298
299	VR_LOCK(sc);
300
301	/*
302	 * Set up frame for RX.
303	 */
304	frame->mii_stdelim = VR_MII_STARTDELIM;
305	frame->mii_opcode = VR_MII_READOP;
306	frame->mii_turnaround = 0;
307	frame->mii_data = 0;
308
309	CSR_WRITE_1(sc, VR_MIICMD, 0);
310	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
311
312	/*
313 	 * Turn on data xmit.
314	 */
315	SIO_SET(VR_MIICMD_DIR);
316
317	vr_mii_sync(sc);
318
319	/*
320	 * Send command/address info.
321	 */
322	vr_mii_send(sc, frame->mii_stdelim, 2);
323	vr_mii_send(sc, frame->mii_opcode, 2);
324	vr_mii_send(sc, frame->mii_phyaddr, 5);
325	vr_mii_send(sc, frame->mii_regaddr, 5);
326
327	/* Idle bit */
328	SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAIN));
329	DELAY(1);
330	SIO_SET(VR_MIICMD_CLK);
331	DELAY(1);
332
333	/* Turn off xmit. */
334	SIO_CLR(VR_MIICMD_DIR);
335
336	/* Check for ack */
337	SIO_CLR(VR_MIICMD_CLK);
338	DELAY(1);
339	ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT;
340	SIO_SET(VR_MIICMD_CLK);
341	DELAY(1);
342
343	/*
344	 * Now try reading data bits. If the ack failed, we still
345	 * need to clock through 16 cycles to keep the PHY(s) in sync.
346	 */
347	if (ack) {
348		for(i = 0; i < 16; i++) {
349			SIO_CLR(VR_MIICMD_CLK);
350			DELAY(1);
351			SIO_SET(VR_MIICMD_CLK);
352			DELAY(1);
353		}
354		goto fail;
355	}
356
357	for (i = 0x8000; i; i >>= 1) {
358		SIO_CLR(VR_MIICMD_CLK);
359		DELAY(1);
360		if (!ack) {
361			if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT)
362				frame->mii_data |= i;
363			DELAY(1);
364		}
365		SIO_SET(VR_MIICMD_CLK);
366		DELAY(1);
367	}
368
369fail:
370
371	SIO_CLR(VR_MIICMD_CLK);
372	DELAY(1);
373	SIO_SET(VR_MIICMD_CLK);
374	DELAY(1);
375
376	VR_UNLOCK(sc);
377
378	if (ack)
379		return(1);
380	return(0);
381}
382#else
383{
384	int			s, i;
385
386	s = splimp();
387
388  	/* Set the PHY-adress */
389	CSR_WRITE_1(sc, VR_PHYADDR, (CSR_READ_1(sc, VR_PHYADDR)& 0xe0)|
390	    frame->mii_phyaddr);
391
392  	/* Set the register-adress */
393	CSR_WRITE_1(sc, VR_MIIADDR, frame->mii_regaddr);
394	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_READ_ENB);
395
396	for (i = 0; i < 10000; i++) {
397		if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_READ_ENB) == 0)
398			break;
399		DELAY(1);
400	}
401
402	frame->mii_data = CSR_READ_2(sc, VR_MIIDATA);
403
404	(void)splx(s);
405
406	return(0);
407}
408#endif
409
410
411/*
412 * Write to a PHY register through the MII.
413 */
414static int
415vr_mii_writereg(sc, frame)
416	struct vr_softc		*sc;
417	struct vr_mii_frame	*frame;
418
419#ifdef VR_USESWSHIFT
420{
421	VR_LOCK(sc);
422
423	CSR_WRITE_1(sc, VR_MIICMD, 0);
424	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
425
426	/*
427	 * Set up frame for TX.
428	 */
429
430	frame->mii_stdelim = VR_MII_STARTDELIM;
431	frame->mii_opcode = VR_MII_WRITEOP;
432	frame->mii_turnaround = VR_MII_TURNAROUND;
433
434	/*
435 	 * Turn on data output.
436	 */
437	SIO_SET(VR_MIICMD_DIR);
438
439	vr_mii_sync(sc);
440
441	vr_mii_send(sc, frame->mii_stdelim, 2);
442	vr_mii_send(sc, frame->mii_opcode, 2);
443	vr_mii_send(sc, frame->mii_phyaddr, 5);
444	vr_mii_send(sc, frame->mii_regaddr, 5);
445	vr_mii_send(sc, frame->mii_turnaround, 2);
446	vr_mii_send(sc, frame->mii_data, 16);
447
448	/* Idle bit. */
449	SIO_SET(VR_MIICMD_CLK);
450	DELAY(1);
451	SIO_CLR(VR_MIICMD_CLK);
452	DELAY(1);
453
454	/*
455	 * Turn off xmit.
456	 */
457	SIO_CLR(VR_MIICMD_DIR);
458
459	VR_UNLOCK(sc);
460
461	return(0);
462}
463#else
464{
465	int			s, i;
466
467	s = splimp();
468
469  	/* Set the PHY-adress */
470	CSR_WRITE_1(sc, VR_PHYADDR, (CSR_READ_1(sc, VR_PHYADDR)& 0xe0)|
471		    frame->mii_phyaddr);
472
473  	/* Set the register-adress and data to write */
474	CSR_WRITE_1(sc, VR_MIIADDR, frame->mii_regaddr);
475	CSR_WRITE_2(sc, VR_MIIDATA, frame->mii_data);
476
477	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_WRITE_ENB);
478
479	for (i = 0; i < 10000; i++) {
480		if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_WRITE_ENB) == 0)
481			break;
482		DELAY(1);
483	}
484
485	(void)splx(s);
486
487	return(0);
488}
489#endif
490
491static int
492vr_miibus_readreg(dev, phy, reg)
493	device_t		dev;
494	int			phy, reg;
495{
496	struct vr_softc		*sc;
497	struct vr_mii_frame	frame;
498
499	sc = device_get_softc(dev);
500
501	switch (sc->vr_revid) {
502		case REV_ID_VT6102_APOLLO:
503			if (phy != 1)
504				return 0;
505		default:
506			break;
507		}
508
509	bzero((char *)&frame, sizeof(frame));
510
511	frame.mii_phyaddr = phy;
512	frame.mii_regaddr = reg;
513	vr_mii_readreg(sc, &frame);
514
515	return(frame.mii_data);
516}
517
518static int
519vr_miibus_writereg(dev, phy, reg, data)
520	device_t		dev;
521	u_int16_t		phy, reg, data;
522{
523	struct vr_softc		*sc;
524	struct vr_mii_frame	frame;
525
526	sc = device_get_softc(dev);
527
528	switch (sc->vr_revid) {
529		case REV_ID_VT6102_APOLLO:
530			if (phy != 1)
531				return 0;
532		default:
533			break;
534		}
535
536	bzero((char *)&frame, sizeof(frame));
537
538	frame.mii_phyaddr = phy;
539	frame.mii_regaddr = reg;
540	frame.mii_data = data;
541
542	vr_mii_writereg(sc, &frame);
543
544	return(0);
545}
546
547static void
548vr_miibus_statchg(dev)
549	device_t		dev;
550{
551	struct vr_softc		*sc;
552	struct mii_data		*mii;
553
554	sc = device_get_softc(dev);
555	VR_LOCK(sc);
556	mii = device_get_softc(sc->vr_miibus);
557	vr_setcfg(sc, mii->mii_media_active);
558	VR_UNLOCK(sc);
559
560	return;
561}
562
563/*
564 * Calculate CRC of a multicast group address, return the lower 6 bits.
565 */
566static u_int32_t
567vr_mchash(addr)
568	const uint8_t *addr;
569{
570	uint32_t crc, carry;
571	int idx, bit;
572	uint8_t data;
573
574	/* Compute CRC for the address value. */
575	crc = 0xFFFFFFFF; /* initial value */
576
577	for (idx = 0; idx < 6; idx++) {
578		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
579			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
580			crc <<= 1;
581			if (carry)
582				crc = (crc ^ 0x04c11db6) | carry;
583		}
584	}
585
586	/* return the filter bit position */
587	return((crc >> 26) & 0x0000003F);
588}
589
590/*
591 * Program the 64-bit multicast hash filter.
592 */
593static void
594vr_setmulti(sc)
595	struct vr_softc		*sc;
596{
597	struct ifnet		*ifp;
598	int			h = 0;
599	u_int32_t		hashes[2] = { 0, 0 };
600	struct ifmultiaddr	*ifma;
601	u_int8_t		rxfilt;
602	int			mcnt = 0;
603
604	ifp = &sc->arpcom.ac_if;
605
606	rxfilt = CSR_READ_1(sc, VR_RXCFG);
607
608	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
609		rxfilt |= VR_RXCFG_RX_MULTI;
610		CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
611		CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
612		CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
613		return;
614	}
615
616	/* first, zot all the existing hash bits */
617	CSR_WRITE_4(sc, VR_MAR0, 0);
618	CSR_WRITE_4(sc, VR_MAR1, 0);
619
620	/* now program new ones */
621	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
622		if (ifma->ifma_addr->sa_family != AF_LINK)
623			continue;
624		h = vr_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
625		if (h < 32)
626			hashes[0] |= (1 << h);
627		else
628			hashes[1] |= (1 << (h - 32));
629		mcnt++;
630	}
631
632	if (mcnt)
633		rxfilt |= VR_RXCFG_RX_MULTI;
634	else
635		rxfilt &= ~VR_RXCFG_RX_MULTI;
636
637	CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
638	CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
639	CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
640
641	return;
642}
643
644/*
645 * In order to fiddle with the
646 * 'full-duplex' and '100Mbps' bits in the netconfig register, we
647 * first have to put the transmit and/or receive logic in the idle state.
648 */
649static void
650vr_setcfg(sc, media)
651	struct vr_softc		*sc;
652	int			media;
653{
654	int			restart = 0;
655
656	if (CSR_READ_2(sc, VR_COMMAND) & (VR_CMD_TX_ON|VR_CMD_RX_ON)) {
657		restart = 1;
658		VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
659	}
660
661	if ((media & IFM_GMASK) == IFM_FDX)
662		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
663	else
664		VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
665
666	if (restart)
667		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
668
669	return;
670}
671
672static void
673vr_reset(sc)
674	struct vr_softc		*sc;
675{
676	register int		i;
677
678	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
679
680	for (i = 0; i < VR_TIMEOUT; i++) {
681		DELAY(10);
682		if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
683			break;
684	}
685	if (i == VR_TIMEOUT) {
686		if (sc->vr_revid < REV_ID_VT3065_A)
687			printf("vr%d: reset never completed!\n", sc->vr_unit);
688		else {
689			/* Use newer force reset command */
690			printf("vr%d: Using force reset command.\n", sc->vr_unit);
691			VR_SETBIT(sc, VR_MISC_CR1, VR_MISCCR1_FORSRST);
692		}
693	}
694
695	/* Wait a little while for the chip to get its brains in order. */
696	DELAY(1000);
697
698        return;
699}
700
701/*
702 * Probe for a VIA Rhine chip. Check the PCI vendor and device
703 * IDs against our list and return a device name if we find a match.
704 */
705static int
706vr_probe(dev)
707	device_t		dev;
708{
709	struct vr_type		*t;
710
711	t = vr_devs;
712
713	while(t->vr_name != NULL) {
714		if ((pci_get_vendor(dev) == t->vr_vid) &&
715		    (pci_get_device(dev) == t->vr_did)) {
716			device_set_desc(dev, t->vr_name);
717			return(0);
718		}
719		t++;
720	}
721
722	return(ENXIO);
723}
724
725/*
726 * Attach the interface. Allocate softc structures, do ifmedia
727 * setup and ethernet/BPF attach.
728 */
729static int
730vr_attach(dev)
731	device_t		dev;
732{
733	int			i;
734	u_char			eaddr[ETHER_ADDR_LEN];
735	struct vr_softc		*sc;
736	struct ifnet		*ifp;
737	int			unit, error = 0, rid;
738
739	sc = device_get_softc(dev);
740	unit = device_get_unit(dev);
741
742	mtx_init(&sc->vr_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
743	    MTX_DEF | MTX_RECURSE);
744#ifndef BURN_BRIDGES
745	/*
746	 * Handle power management nonsense.
747	 */
748	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
749		u_int32_t		iobase, membase, irq;
750
751		/* Save important PCI config data. */
752		iobase = pci_read_config(dev, VR_PCI_LOIO, 4);
753		membase = pci_read_config(dev, VR_PCI_LOMEM, 4);
754		irq = pci_read_config(dev, VR_PCI_INTLINE, 4);
755
756		/* Reset the power state. */
757		printf("vr%d: chip is in D%d power mode "
758		    "-- setting to D0\n", unit,
759		    pci_get_powerstate(dev));
760		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
761
762			/* Restore PCI config data. */
763		pci_write_config(dev, VR_PCI_LOIO, iobase, 4);
764		pci_write_config(dev, VR_PCI_LOMEM, membase, 4);
765		pci_write_config(dev, VR_PCI_INTLINE, irq, 4);
766	}
767#endif
768	/*
769	 * Map control/status registers.
770	 */
771	pci_enable_busmaster(dev);
772	sc->vr_revid = pci_read_config(dev, VR_PCI_REVID, 4) & 0x000000FF;
773
774	rid = VR_RID;
775	sc->vr_res = bus_alloc_resource_any(dev, VR_RES, &rid, RF_ACTIVE);
776
777	if (sc->vr_res == NULL) {
778		printf("vr%d: couldn't map ports/memory\n", unit);
779		error = ENXIO;
780		goto fail;
781	}
782
783	sc->vr_btag = rman_get_bustag(sc->vr_res);
784	sc->vr_bhandle = rman_get_bushandle(sc->vr_res);
785
786	/* Allocate interrupt */
787	rid = 0;
788	sc->vr_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
789	    RF_SHAREABLE | RF_ACTIVE);
790
791	if (sc->vr_irq == NULL) {
792		printf("vr%d: couldn't map interrupt\n", unit);
793		error = ENXIO;
794		goto fail;
795	}
796
797	/*
798	 * Windows may put the chip in suspend mode when it
799	 * shuts down. Be sure to kick it in the head to wake it
800	 * up again.
801	 */
802	VR_CLRBIT(sc, VR_STICKHW, (VR_STICKHW_DS0|VR_STICKHW_DS1));
803
804	/* Reset the adapter. */
805	vr_reset(sc);
806
807        /*
808	 * Turn on bit2 (MIION) in PCI configuration register 0x53 during
809	 * initialization and disable AUTOPOLL.
810	 */
811        pci_write_config(dev, VR_PCI_MODE,
812	    pci_read_config(dev, VR_PCI_MODE, 4) | (VR_MODE3_MIION << 24), 4);
813	VR_CLRBIT(sc, VR_MIICMD, VR_MIICMD_AUTOPOLL);
814
815	/*
816	 * Get station address. The way the Rhine chips work,
817	 * you're not allowed to directly access the EEPROM once
818	 * they've been programmed a special way. Consequently,
819	 * we need to read the node address from the PAR0 and PAR1
820	 * registers.
821	 */
822	VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
823	DELAY(200);
824	for (i = 0; i < ETHER_ADDR_LEN; i++)
825		eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
826
827	sc->vr_unit = unit;
828	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
829
830	sc->vr_ldata = contigmalloc(sizeof(struct vr_list_data), M_DEVBUF,
831	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
832
833	if (sc->vr_ldata == NULL) {
834		printf("vr%d: no memory for list buffers!\n", unit);
835		error = ENXIO;
836		goto fail;
837	}
838
839	bzero(sc->vr_ldata, sizeof(struct vr_list_data));
840
841	ifp = &sc->arpcom.ac_if;
842	ifp->if_softc = sc;
843	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
844	ifp->if_mtu = ETHERMTU;
845	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
846	ifp->if_ioctl = vr_ioctl;
847	ifp->if_start = vr_start;
848	ifp->if_watchdog = vr_watchdog;
849	ifp->if_init = vr_init;
850	ifp->if_baudrate = 10000000;
851	ifp->if_snd.ifq_maxlen = VR_TX_LIST_CNT - 1;
852
853	/*
854	 * Do MII setup.
855	 */
856	if (mii_phy_probe(dev, &sc->vr_miibus,
857	    vr_ifmedia_upd, vr_ifmedia_sts)) {
858		printf("vr%d: MII without any phy!\n", sc->vr_unit);
859		error = ENXIO;
860		goto fail;
861	}
862
863	callout_handle_init(&sc->vr_stat_ch);
864
865	/*
866	 * Call MI attach routine.
867	 */
868	ether_ifattach(ifp, eaddr);
869
870	/* Hook interrupt last to avoid having to lock softc */
871	error = bus_setup_intr(dev, sc->vr_irq, INTR_TYPE_NET,
872	    vr_intr, sc, &sc->vr_intrhand);
873
874	if (error) {
875		printf("vr%d: couldn't set up irq\n", unit);
876		ether_ifdetach(ifp);
877		goto fail;
878	}
879
880fail:
881	if (error)
882		vr_detach(dev);
883
884	return(error);
885}
886
887/*
888 * Shutdown hardware and free up resources. This can be called any
889 * time after the mutex has been initialized. It is called in both
890 * the error case in attach and the normal detach case so it needs
891 * to be careful about only freeing resources that have actually been
892 * allocated.
893 */
894static int
895vr_detach(dev)
896	device_t		dev;
897{
898	struct vr_softc		*sc;
899	struct ifnet		*ifp;
900
901	sc = device_get_softc(dev);
902	KASSERT(mtx_initialized(&sc->vr_mtx), ("vr mutex not initialized"));
903	VR_LOCK(sc);
904	ifp = &sc->arpcom.ac_if;
905
906	/* These should only be active if attach succeeded */
907	if (device_is_attached(dev)) {
908		vr_stop(sc);
909		ether_ifdetach(ifp);
910	}
911	if (sc->vr_miibus)
912		device_delete_child(dev, sc->vr_miibus);
913	bus_generic_detach(dev);
914
915	if (sc->vr_intrhand)
916		bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand);
917	if (sc->vr_irq)
918		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq);
919	if (sc->vr_res)
920		bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res);
921
922	if (sc->vr_ldata)
923		contigfree(sc->vr_ldata, sizeof(struct vr_list_data), M_DEVBUF);
924
925	VR_UNLOCK(sc);
926	mtx_destroy(&sc->vr_mtx);
927
928	return(0);
929}
930
931/*
932 * Initialize the transmit descriptors.
933 */
934static int
935vr_list_tx_init(sc)
936	struct vr_softc		*sc;
937{
938	struct vr_chain_data	*cd;
939	struct vr_list_data	*ld;
940	int			i;
941
942	cd = &sc->vr_cdata;
943	ld = sc->vr_ldata;
944	for (i = 0; i < VR_TX_LIST_CNT; i++) {
945		cd->vr_tx_chain[i].vr_ptr = &ld->vr_tx_list[i];
946		if (i == (VR_TX_LIST_CNT - 1))
947			cd->vr_tx_chain[i].vr_nextdesc =
948				&cd->vr_tx_chain[0];
949		else
950			cd->vr_tx_chain[i].vr_nextdesc =
951				&cd->vr_tx_chain[i + 1];
952	}
953
954	cd->vr_tx_cons = cd->vr_tx_prod = &cd->vr_tx_chain[0];
955
956	return(0);
957}
958
959
960/*
961 * Initialize the RX descriptors and allocate mbufs for them. Note that
962 * we arrange the descriptors in a closed ring, so that the last descriptor
963 * points back to the first.
964 */
965static int
966vr_list_rx_init(sc)
967	struct vr_softc		*sc;
968{
969	struct vr_chain_data	*cd;
970	struct vr_list_data	*ld;
971	int			i;
972
973	cd = &sc->vr_cdata;
974	ld = sc->vr_ldata;
975
976	for (i = 0; i < VR_RX_LIST_CNT; i++) {
977		cd->vr_rx_chain[i].vr_ptr =
978			(struct vr_desc *)&ld->vr_rx_list[i];
979		if (vr_newbuf(sc, &cd->vr_rx_chain[i], NULL) == ENOBUFS)
980			return(ENOBUFS);
981		if (i == (VR_RX_LIST_CNT - 1)) {
982			cd->vr_rx_chain[i].vr_nextdesc =
983					&cd->vr_rx_chain[0];
984			ld->vr_rx_list[i].vr_next =
985					vtophys(&ld->vr_rx_list[0]);
986		} else {
987			cd->vr_rx_chain[i].vr_nextdesc =
988					&cd->vr_rx_chain[i + 1];
989			ld->vr_rx_list[i].vr_next =
990					vtophys(&ld->vr_rx_list[i + 1]);
991		}
992	}
993
994	cd->vr_rx_head = &cd->vr_rx_chain[0];
995
996	return(0);
997}
998
999/*
1000 * Initialize an RX descriptor and attach an MBUF cluster.
1001 * Note: the length fields are only 11 bits wide, which means the
1002 * largest size we can specify is 2047. This is important because
1003 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
1004 * overflow the field and make a mess.
1005 */
1006static int
1007vr_newbuf(sc, c, m)
1008	struct vr_softc		*sc;
1009	struct vr_chain_onefrag	*c;
1010	struct mbuf		*m;
1011{
1012	struct mbuf		*m_new = NULL;
1013
1014	if (m == NULL) {
1015		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1016		if (m_new == NULL)
1017			return(ENOBUFS);
1018
1019		MCLGET(m_new, M_DONTWAIT);
1020		if (!(m_new->m_flags & M_EXT)) {
1021			m_freem(m_new);
1022			return(ENOBUFS);
1023		}
1024		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1025	} else {
1026		m_new = m;
1027		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1028		m_new->m_data = m_new->m_ext.ext_buf;
1029	}
1030
1031	m_adj(m_new, sizeof(u_int64_t));
1032
1033	c->vr_mbuf = m_new;
1034	c->vr_ptr->vr_status = VR_RXSTAT;
1035	c->vr_ptr->vr_data = vtophys(mtod(m_new, caddr_t));
1036	c->vr_ptr->vr_ctl = VR_RXCTL | VR_RXLEN;
1037
1038	return(0);
1039}
1040
1041/*
1042 * A frame has been uploaded: pass the resulting mbuf chain up to
1043 * the higher level protocols.
1044 */
1045static void
1046vr_rxeof(sc)
1047	struct vr_softc		*sc;
1048{
1049        struct mbuf		*m, *m0;
1050        struct ifnet		*ifp;
1051	struct vr_chain_onefrag	*cur_rx;
1052	int			total_len = 0;
1053	u_int32_t		rxstat;
1054
1055	VR_LOCK_ASSERT(sc);
1056
1057	ifp = &sc->arpcom.ac_if;
1058
1059	while(!((rxstat = sc->vr_cdata.vr_rx_head->vr_ptr->vr_status) &
1060							VR_RXSTAT_OWN)) {
1061#ifdef DEVICE_POLLING
1062		if (ifp->if_flags & IFF_POLLING) {
1063			if (sc->rxcycles <= 0)
1064				break;
1065			sc->rxcycles--;
1066		}
1067#endif /* DEVICE_POLLING */
1068		m0 = NULL;
1069		cur_rx = sc->vr_cdata.vr_rx_head;
1070		sc->vr_cdata.vr_rx_head = cur_rx->vr_nextdesc;
1071		m = cur_rx->vr_mbuf;
1072
1073		/*
1074		 * If an error occurs, update stats, clear the
1075		 * status word and leave the mbuf cluster in place:
1076		 * it should simply get re-used next time this descriptor
1077	 	 * comes up in the ring.
1078		 */
1079		if (rxstat & VR_RXSTAT_RXERR) {
1080			ifp->if_ierrors++;
1081			printf("vr%d: rx error (%02x):",
1082			       sc->vr_unit, rxstat & 0x000000ff);
1083			if (rxstat & VR_RXSTAT_CRCERR)
1084				printf(" crc error");
1085			if (rxstat & VR_RXSTAT_FRAMEALIGNERR)
1086				printf(" frame alignment error\n");
1087			if (rxstat & VR_RXSTAT_FIFOOFLOW)
1088				printf(" FIFO overflow");
1089			if (rxstat & VR_RXSTAT_GIANT)
1090				printf(" received giant packet");
1091			if (rxstat & VR_RXSTAT_RUNT)
1092				printf(" received runt packet");
1093			if (rxstat & VR_RXSTAT_BUSERR)
1094				printf(" system bus error");
1095			if (rxstat & VR_RXSTAT_BUFFERR)
1096				printf("rx buffer error");
1097			printf("\n");
1098			vr_newbuf(sc, cur_rx, m);
1099			continue;
1100		}
1101
1102		/* No errors; receive the packet. */
1103		total_len = VR_RXBYTES(cur_rx->vr_ptr->vr_status);
1104
1105		/*
1106		 * XXX The VIA Rhine chip includes the CRC with every
1107		 * received frame, and there's no way to turn this
1108		 * behavior off (at least, I can't find anything in
1109	 	 * the manual that explains how to do it) so we have
1110		 * to trim off the CRC manually.
1111		 */
1112		total_len -= ETHER_CRC_LEN;
1113
1114		m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp,
1115		    NULL);
1116		vr_newbuf(sc, cur_rx, m);
1117		if (m0 == NULL) {
1118			ifp->if_ierrors++;
1119			continue;
1120		}
1121		m = m0;
1122
1123		ifp->if_ipackets++;
1124		VR_UNLOCK(sc);
1125		(*ifp->if_input)(ifp, m);
1126		VR_LOCK(sc);
1127	}
1128
1129	return;
1130}
1131
1132static void
1133vr_rxeoc(sc)
1134	struct vr_softc		*sc;
1135{
1136	struct ifnet		*ifp;
1137	int			i;
1138
1139	ifp = &sc->arpcom.ac_if;
1140
1141	ifp->if_ierrors++;
1142
1143	VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
1144        DELAY(10000);
1145
1146	for (i = 0x400;
1147	     i && (CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RX_ON);
1148	     i--)
1149		;	/* Wait for receiver to stop */
1150
1151	if (!i) {
1152		printf("vr%d: rx shutdown error!\n", sc->vr_unit);
1153		sc->vr_flags |= VR_F_RESTART;
1154		return;
1155		}
1156
1157	vr_rxeof(sc);
1158
1159	CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr));
1160	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
1161	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
1162
1163	return;
1164}
1165
1166/*
1167 * A frame was downloaded to the chip. It's safe for us to clean up
1168 * the list buffers.
1169 */
1170
1171static void
1172vr_txeof(sc)
1173	struct vr_softc		*sc;
1174{
1175	struct vr_chain		*cur_tx;
1176	struct ifnet		*ifp;
1177
1178	ifp = &sc->arpcom.ac_if;
1179
1180	/*
1181	 * Go through our tx list and free mbufs for those
1182	 * frames that have been transmitted.
1183	 */
1184	cur_tx = sc->vr_cdata.vr_tx_cons;
1185	while (cur_tx->vr_mbuf != NULL) {
1186		u_int32_t		txstat;
1187		int			i;
1188
1189		txstat = cur_tx->vr_ptr->vr_status;
1190
1191		if ((txstat & VR_TXSTAT_ABRT) ||
1192		    (txstat & VR_TXSTAT_UDF)) {
1193			for (i = 0x400;
1194			     i && (CSR_READ_2(sc, VR_COMMAND) & VR_CMD_TX_ON);
1195			     i--)
1196				;	/* Wait for chip to shutdown */
1197			if (!i) {
1198				printf("vr%d: tx shutdown timeout\n", sc->vr_unit);
1199				sc->vr_flags |= VR_F_RESTART;
1200				break;
1201			}
1202			VR_TXOWN(cur_tx) = VR_TXSTAT_OWN;
1203			CSR_WRITE_4(sc, VR_TXADDR, vtophys(cur_tx->vr_ptr));
1204			break;
1205		}
1206
1207		if (txstat & VR_TXSTAT_OWN)
1208			break;
1209
1210		if (txstat & VR_TXSTAT_ERRSUM) {
1211			ifp->if_oerrors++;
1212			if (txstat & VR_TXSTAT_DEFER)
1213				ifp->if_collisions++;
1214			if (txstat & VR_TXSTAT_LATECOLL)
1215				ifp->if_collisions++;
1216		}
1217
1218		ifp->if_collisions +=(txstat & VR_TXSTAT_COLLCNT) >> 3;
1219
1220		ifp->if_opackets++;
1221		m_freem(cur_tx->vr_mbuf);
1222		cur_tx->vr_mbuf = NULL;
1223		ifp->if_flags &= ~IFF_OACTIVE;
1224
1225		cur_tx = cur_tx->vr_nextdesc;
1226	}
1227	sc->vr_cdata.vr_tx_cons = cur_tx;
1228	if (cur_tx->vr_mbuf == NULL)
1229		ifp->if_timer = 0;
1230}
1231
1232static void
1233vr_tick(xsc)
1234	void			*xsc;
1235{
1236	struct vr_softc		*sc;
1237	struct mii_data		*mii;
1238
1239	sc = xsc;
1240	VR_LOCK(sc);
1241	if (sc->vr_flags & VR_F_RESTART) {
1242		printf("vr%d: restarting\n", sc->vr_unit);
1243		vr_stop(sc);
1244		vr_reset(sc);
1245		vr_init(sc);
1246		sc->vr_flags &= ~VR_F_RESTART;
1247	}
1248
1249	mii = device_get_softc(sc->vr_miibus);
1250	mii_tick(mii);
1251
1252	sc->vr_stat_ch = timeout(vr_tick, sc, hz);
1253
1254	VR_UNLOCK(sc);
1255
1256	return;
1257}
1258
1259#ifdef DEVICE_POLLING
1260static poll_handler_t vr_poll;
1261
1262static void
1263vr_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1264{
1265	struct vr_softc *sc = ifp->if_softc;
1266
1267	VR_LOCK(sc);
1268	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1269		CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1270		goto done;
1271	}
1272
1273	sc->rxcycles = count;
1274	vr_rxeof(sc);
1275	vr_txeof(sc);
1276	if (ifp->if_snd.ifq_head != NULL)
1277		vr_start(ifp);
1278
1279	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1280		u_int16_t status;
1281
1282		status = CSR_READ_2(sc, VR_ISR);
1283		if (status)
1284			CSR_WRITE_2(sc, VR_ISR, status);
1285
1286		if ((status & VR_INTRS) == 0)
1287			goto done;
1288
1289		if (status & VR_ISR_RX_DROPPED) {
1290			printf("vr%d: rx packet lost\n", sc->vr_unit);
1291			ifp->if_ierrors++;
1292		}
1293
1294		if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) ||
1295		    (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW)) {
1296			printf("vr%d: receive error (%04x)",
1297			       sc->vr_unit, status);
1298			if (status & VR_ISR_RX_NOBUF)
1299				printf(" no buffers");
1300			if (status & VR_ISR_RX_OFLOW)
1301				printf(" overflow");
1302			if (status & VR_ISR_RX_DROPPED)
1303				printf(" packet lost");
1304			printf("\n");
1305			vr_rxeoc(sc);
1306		}
1307
1308		if ((status & VR_ISR_BUSERR) || (status & VR_ISR_TX_UNDERRUN)) {
1309			vr_reset(sc);
1310			vr_init(sc);
1311			goto done;
1312		}
1313
1314		if ((status & VR_ISR_UDFI) ||
1315		    (status & VR_ISR_TX_ABRT2) ||
1316		    (status & VR_ISR_TX_ABRT)) {
1317			ifp->if_oerrors++;
1318			if (sc->vr_cdata.vr_tx_cons->vr_mbuf != NULL) {
1319				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1320				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1321			}
1322		}
1323	}
1324
1325done:
1326	VR_UNLOCK(sc);
1327
1328}
1329#endif /* DEVICE_POLLING */
1330
1331static void
1332vr_intr(arg)
1333	void			*arg;
1334{
1335	struct vr_softc		*sc;
1336	struct ifnet		*ifp;
1337	u_int16_t		status;
1338
1339	sc = arg;
1340	VR_LOCK(sc);
1341	ifp = &sc->arpcom.ac_if;
1342
1343#ifdef DEVICE_POLLING
1344	if (ifp->if_flags & IFF_POLLING)
1345		goto done;
1346	if (ether_poll_register(vr_poll, ifp)) { /* ok, disable interrupts */
1347		CSR_WRITE_2(sc, VR_IMR, 0x0000);
1348		vr_poll(ifp, 0, 1);
1349		goto done;
1350	}
1351#endif /* DEVICE_POLLING */
1352
1353	/* Supress unwanted interrupts. */
1354	if (!(ifp->if_flags & IFF_UP)) {
1355		vr_stop(sc);
1356		VR_UNLOCK(sc);
1357		return;
1358	}
1359
1360	/* Disable interrupts. */
1361	CSR_WRITE_2(sc, VR_IMR, 0x0000);
1362
1363	for (;;) {
1364
1365		status = CSR_READ_2(sc, VR_ISR);
1366		if (status)
1367			CSR_WRITE_2(sc, VR_ISR, status);
1368
1369		if ((status & VR_INTRS) == 0)
1370			break;
1371
1372		if (status & VR_ISR_RX_OK)
1373			vr_rxeof(sc);
1374
1375		if (status & VR_ISR_RX_DROPPED) {
1376			printf("vr%d: rx packet lost\n", sc->vr_unit);
1377			ifp->if_ierrors++;
1378			}
1379
1380		if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) ||
1381		    (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW)) {
1382			printf("vr%d: receive error (%04x)",
1383			       sc->vr_unit, status);
1384			if (status & VR_ISR_RX_NOBUF)
1385				printf(" no buffers");
1386			if (status & VR_ISR_RX_OFLOW)
1387				printf(" overflow");
1388			if (status & VR_ISR_RX_DROPPED)
1389				printf(" packet lost");
1390			printf("\n");
1391			vr_rxeoc(sc);
1392		}
1393
1394		if ((status & VR_ISR_BUSERR) || (status & VR_ISR_TX_UNDERRUN)) {
1395			vr_reset(sc);
1396			vr_init(sc);
1397			break;
1398		}
1399
1400		if ((status & VR_ISR_TX_OK) || (status & VR_ISR_TX_ABRT) ||
1401		    (status & VR_ISR_TX_ABRT2) || (status & VR_ISR_UDFI)) {
1402			vr_txeof(sc);
1403			if ((status & VR_ISR_UDFI) ||
1404			    (status & VR_ISR_TX_ABRT2) ||
1405			    (status & VR_ISR_TX_ABRT)) {
1406				ifp->if_oerrors++;
1407				if (sc->vr_cdata.vr_tx_cons->vr_mbuf != NULL) {
1408					VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1409					VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1410				}
1411			}
1412		}
1413
1414	}
1415
1416	/* Re-enable interrupts. */
1417	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1418
1419	if (ifp->if_snd.ifq_head != NULL) {
1420		vr_start(ifp);
1421	}
1422
1423#ifdef DEVICE_POLLING
1424done:
1425#endif /* DEVICE_POLLING */
1426	VR_UNLOCK(sc);
1427
1428	return;
1429}
1430
1431/*
1432 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1433 * pointers to the fragment pointers.
1434 */
1435static int
1436vr_encap(sc, c, m_head)
1437	struct vr_softc		*sc;
1438	struct vr_chain		*c;
1439	struct mbuf		*m_head;
1440{
1441	struct vr_desc		*f = NULL;
1442	struct mbuf		*m;
1443
1444	/*
1445	 * The VIA Rhine wants packet buffers to be longword
1446	 * aligned, but very often our mbufs aren't. Rather than
1447	 * waste time trying to decide when to copy and when not
1448	 * to copy, just do it all the time.
1449	 */
1450	m = m_defrag(m_head, M_DONTWAIT);
1451	if (m == NULL) {
1452		return(1);
1453	}
1454
1455	/*
1456	 * The Rhine chip doesn't auto-pad, so we have to make
1457	 * sure to pad short frames out to the minimum frame length
1458	 * ourselves.
1459	 */
1460	if (m->m_len < VR_MIN_FRAMELEN) {
1461		m->m_pkthdr.len += VR_MIN_FRAMELEN - m->m_len;
1462		m->m_len = m->m_pkthdr.len;
1463	}
1464
1465	c->vr_mbuf = m;
1466	f = c->vr_ptr;
1467	f->vr_data = vtophys(mtod(m, caddr_t));
1468	f->vr_ctl = m->m_len;
1469	f->vr_ctl |= VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG;
1470	f->vr_status = 0;
1471	f->vr_ctl |= VR_TXCTL_LASTFRAG|VR_TXCTL_FINT;
1472	f->vr_next = vtophys(c->vr_nextdesc->vr_ptr);
1473
1474	return(0);
1475}
1476
1477/*
1478 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1479 * to the mbuf data regions directly in the transmit lists. We also save a
1480 * copy of the pointers since the transmit list fragment pointers are
1481 * physical addresses.
1482 */
1483
1484static void
1485vr_start(ifp)
1486	struct ifnet		*ifp;
1487{
1488	struct vr_softc		*sc;
1489	struct mbuf		*m_head;
1490	struct vr_chain		*cur_tx;
1491
1492	if (ifp->if_flags & IFF_OACTIVE)
1493		return;
1494
1495	sc = ifp->if_softc;
1496
1497	VR_LOCK(sc);
1498
1499	cur_tx = sc->vr_cdata.vr_tx_prod;
1500	while (cur_tx->vr_mbuf == NULL) {
1501		IF_DEQUEUE(&ifp->if_snd, m_head);
1502		if (m_head == NULL)
1503			break;
1504
1505		/* Pack the data into the descriptor. */
1506		if (vr_encap(sc, cur_tx, m_head)) {
1507			/* Rollback, send what we were able to encap. */
1508			IF_PREPEND(&ifp->if_snd, m_head);
1509			break;
1510		}
1511
1512		VR_TXOWN(cur_tx) = VR_TXSTAT_OWN;
1513
1514		/*
1515		 * If there's a BPF listener, bounce a copy of this frame
1516		 * to him.
1517		 */
1518		BPF_MTAP(ifp, cur_tx->vr_mbuf);
1519
1520		cur_tx = cur_tx->vr_nextdesc;
1521	}
1522	if (cur_tx != sc->vr_cdata.vr_tx_prod || cur_tx->vr_mbuf != NULL) {
1523		sc->vr_cdata.vr_tx_prod = cur_tx;
1524
1525		/* Tell the chip to start transmitting. */
1526		VR_SETBIT16(sc, VR_COMMAND, /*VR_CMD_TX_ON|*/VR_CMD_TX_GO);
1527
1528		/* Set a timeout in case the chip goes out to lunch. */
1529		ifp->if_timer = 5;
1530
1531		if (cur_tx->vr_mbuf != NULL)
1532			ifp->if_flags |= IFF_OACTIVE;
1533	}
1534
1535	VR_UNLOCK(sc);
1536
1537	return;
1538}
1539
1540static void
1541vr_init(xsc)
1542	void			*xsc;
1543{
1544	struct vr_softc		*sc = xsc;
1545	struct ifnet		*ifp = &sc->arpcom.ac_if;
1546	struct mii_data		*mii;
1547	int			i;
1548
1549	VR_LOCK(sc);
1550
1551	mii = device_get_softc(sc->vr_miibus);
1552
1553	/*
1554	 * Cancel pending I/O and free all RX/TX buffers.
1555	 */
1556	vr_stop(sc);
1557	vr_reset(sc);
1558
1559	/*
1560	 * Set our station address.
1561	 */
1562	for (i = 0; i < ETHER_ADDR_LEN; i++)
1563		CSR_WRITE_1(sc, VR_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1564
1565	/* Set DMA size */
1566	VR_CLRBIT(sc, VR_BCR0, VR_BCR0_DMA_LENGTH);
1567	VR_SETBIT(sc, VR_BCR0, VR_BCR0_DMA_STORENFWD);
1568
1569	/*
1570	 * BCR0 and BCR1 can override the RXCFG and TXCFG registers,
1571	 * so we must set both.
1572	 */
1573	VR_CLRBIT(sc, VR_BCR0, VR_BCR0_RX_THRESH);
1574	VR_SETBIT(sc, VR_BCR0, VR_BCR0_RXTHRESH128BYTES);
1575
1576	VR_CLRBIT(sc, VR_BCR1, VR_BCR1_TX_THRESH);
1577	VR_SETBIT(sc, VR_BCR1, VR_BCR1_TXTHRESHSTORENFWD);
1578
1579	VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1580	VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_128BYTES);
1581
1582	VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1583	VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1584
1585	/* Init circular RX list. */
1586	if (vr_list_rx_init(sc) == ENOBUFS) {
1587		printf("vr%d: initialization failed: no "
1588			"memory for rx buffers\n", sc->vr_unit);
1589		vr_stop(sc);
1590		VR_UNLOCK(sc);
1591		return;
1592	}
1593
1594	/*
1595	 * Init tx descriptors.
1596	 */
1597	vr_list_tx_init(sc);
1598
1599	/* If we want promiscuous mode, set the allframes bit. */
1600	if (ifp->if_flags & IFF_PROMISC)
1601		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1602	else
1603		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1604
1605	/* Set capture broadcast bit to capture broadcast frames. */
1606	if (ifp->if_flags & IFF_BROADCAST)
1607		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1608	else
1609		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1610
1611	/*
1612	 * Program the multicast filter, if necessary.
1613	 */
1614	vr_setmulti(sc);
1615
1616	/*
1617	 * Load the address of the RX list.
1618	 */
1619	CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr));
1620
1621	/* Enable receiver and transmitter. */
1622	CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1623				    VR_CMD_TX_ON|VR_CMD_RX_ON|
1624				    VR_CMD_RX_GO);
1625
1626	CSR_WRITE_4(sc, VR_TXADDR, vtophys(&sc->vr_ldata->vr_tx_list[0]));
1627
1628	CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1629#ifdef DEVICE_POLLING
1630	/*
1631	 * Disable interrupts if we are polling.
1632	 */
1633	if (ifp->if_flags & IFF_POLLING)
1634		CSR_WRITE_2(sc, VR_IMR, 0);
1635	else
1636#endif /* DEVICE_POLLING */
1637	/*
1638	 * Enable interrupts.
1639	 */
1640	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1641
1642	mii_mediachg(mii);
1643
1644	ifp->if_flags |= IFF_RUNNING;
1645	ifp->if_flags &= ~IFF_OACTIVE;
1646
1647	sc->vr_stat_ch = timeout(vr_tick, sc, hz);
1648
1649	VR_UNLOCK(sc);
1650
1651	return;
1652}
1653
1654/*
1655 * Set media options.
1656 */
1657static int
1658vr_ifmedia_upd(ifp)
1659	struct ifnet		*ifp;
1660{
1661	struct vr_softc		*sc;
1662
1663	sc = ifp->if_softc;
1664
1665	if (ifp->if_flags & IFF_UP)
1666		vr_init(sc);
1667
1668	return(0);
1669}
1670
1671/*
1672 * Report current media status.
1673 */
1674static void
1675vr_ifmedia_sts(ifp, ifmr)
1676	struct ifnet		*ifp;
1677	struct ifmediareq	*ifmr;
1678{
1679	struct vr_softc		*sc;
1680	struct mii_data		*mii;
1681
1682	sc = ifp->if_softc;
1683	mii = device_get_softc(sc->vr_miibus);
1684	mii_pollstat(mii);
1685	ifmr->ifm_active = mii->mii_media_active;
1686	ifmr->ifm_status = mii->mii_media_status;
1687
1688	return;
1689}
1690
1691static int
1692vr_ioctl(ifp, command, data)
1693	struct ifnet		*ifp;
1694	u_long			command;
1695	caddr_t			data;
1696{
1697	struct vr_softc		*sc = ifp->if_softc;
1698	struct ifreq		*ifr = (struct ifreq *) data;
1699	struct mii_data		*mii;
1700	int			error = 0;
1701
1702	VR_LOCK(sc);
1703
1704	switch(command) {
1705	case SIOCSIFFLAGS:
1706		if (ifp->if_flags & IFF_UP) {
1707			vr_init(sc);
1708		} else {
1709			if (ifp->if_flags & IFF_RUNNING)
1710				vr_stop(sc);
1711		}
1712		error = 0;
1713		break;
1714	case SIOCADDMULTI:
1715	case SIOCDELMULTI:
1716		vr_setmulti(sc);
1717		error = 0;
1718		break;
1719	case SIOCGIFMEDIA:
1720	case SIOCSIFMEDIA:
1721		mii = device_get_softc(sc->vr_miibus);
1722		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1723		break;
1724	default:
1725		error = ether_ioctl(ifp, command, data);
1726		break;
1727	}
1728
1729	VR_UNLOCK(sc);
1730
1731	return(error);
1732}
1733
1734static void
1735vr_watchdog(ifp)
1736	struct ifnet		*ifp;
1737{
1738	struct vr_softc		*sc;
1739
1740	sc = ifp->if_softc;
1741
1742	VR_LOCK(sc);
1743	ifp->if_oerrors++;
1744	printf("vr%d: watchdog timeout\n", sc->vr_unit);
1745
1746	vr_stop(sc);
1747	vr_reset(sc);
1748	vr_init(sc);
1749
1750	if (ifp->if_snd.ifq_head != NULL)
1751		vr_start(ifp);
1752
1753	VR_UNLOCK(sc);
1754
1755	return;
1756}
1757
1758/*
1759 * Stop the adapter and free any mbufs allocated to the
1760 * RX and TX lists.
1761 */
1762static void
1763vr_stop(sc)
1764	struct vr_softc		*sc;
1765{
1766	register int		i;
1767	struct ifnet		*ifp;
1768
1769	VR_LOCK(sc);
1770
1771	ifp = &sc->arpcom.ac_if;
1772	ifp->if_timer = 0;
1773
1774	untimeout(vr_tick, sc, sc->vr_stat_ch);
1775	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1776#ifdef DEVICE_POLLING
1777	ether_poll_deregister(ifp);
1778#endif /* DEVICE_POLLING */
1779
1780	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1781	VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1782	CSR_WRITE_2(sc, VR_IMR, 0x0000);
1783	CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1784	CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1785
1786	/*
1787	 * Free data in the RX lists.
1788	 */
1789	for (i = 0; i < VR_RX_LIST_CNT; i++) {
1790		if (sc->vr_cdata.vr_rx_chain[i].vr_mbuf != NULL) {
1791			m_freem(sc->vr_cdata.vr_rx_chain[i].vr_mbuf);
1792			sc->vr_cdata.vr_rx_chain[i].vr_mbuf = NULL;
1793		}
1794	}
1795	bzero((char *)&sc->vr_ldata->vr_rx_list,
1796		sizeof(sc->vr_ldata->vr_rx_list));
1797
1798	/*
1799	 * Free the TX list buffers.
1800	 */
1801	for (i = 0; i < VR_TX_LIST_CNT; i++) {
1802		if (sc->vr_cdata.vr_tx_chain[i].vr_mbuf != NULL) {
1803			m_freem(sc->vr_cdata.vr_tx_chain[i].vr_mbuf);
1804			sc->vr_cdata.vr_tx_chain[i].vr_mbuf = NULL;
1805		}
1806	}
1807
1808	bzero((char *)&sc->vr_ldata->vr_tx_list,
1809		sizeof(sc->vr_ldata->vr_tx_list));
1810
1811	VR_UNLOCK(sc);
1812
1813	return;
1814}
1815
1816/*
1817 * Stop all chip I/O so that the kernel's probe routines don't
1818 * get confused by errant DMAs when rebooting.
1819 */
1820static void
1821vr_shutdown(dev)
1822	device_t		dev;
1823{
1824	struct vr_softc		*sc;
1825
1826	sc = device_get_softc(dev);
1827
1828	vr_stop(sc);
1829
1830	return;
1831}
1832