xref: /netbsd-src/sys/dev/pci/if_vr.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: if_vr.c,v 1.131 2019/09/13 07:55:07 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
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 THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1997, 1998
35  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by Bill Paul.
48  * 4. Neither the name of the author nor the names of any co-contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
62  * THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *	$FreeBSD: if_vr.c,v 1.7 1999/01/10 18:51:49 wpaul Exp $
65  */
66 
67 /*
68  * VIA Rhine fast ethernet PCI NIC driver
69  *
70  * Supports various network adapters based on the VIA Rhine
71  * and Rhine II PCI controllers, including the D-Link DFE530TX.
72  * Datasheets are available at http://www.via.com.tw.
73  *
74  * Written by Bill Paul <wpaul@ctr.columbia.edu>
75  * Electrical Engineering Department
76  * Columbia University, New York City
77  */
78 
79 /*
80  * The VIA Rhine controllers are similar in some respects to the
81  * the DEC tulip chips, except less complicated. The controller
82  * uses an MII bus and an external physical layer interface. The
83  * receiver has a one entry perfect filter and a 64-bit hash table
84  * multicast filter. Transmit and receive descriptors are similar
85  * to the tulip.
86  *
87  * The Rhine has a serious flaw in its transmit DMA mechanism:
88  * transmit buffers must be longword aligned. Unfortunately,
89  * the kernel doesn't guarantee that mbufs will be filled in starting
90  * at longword boundaries, so we have to do a buffer copy before
91  * transmission.
92  *
93  * Apparently, the receive DMA mechanism also has the same flaw.  This
94  * means that on systems with struct alignment requirements, incoming
95  * frames must be copied to a new buffer which shifts the data forward
96  * 2 bytes so that the payload is aligned on a 4-byte boundary.
97  */
98 
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: if_vr.c,v 1.131 2019/09/13 07:55:07 msaitoh Exp $");
101 
102 
103 
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/callout.h>
107 #include <sys/sockio.h>
108 #include <sys/mbuf.h>
109 #include <sys/malloc.h>
110 #include <sys/kernel.h>
111 #include <sys/socket.h>
112 #include <sys/device.h>
113 
114 #include <sys/rndsource.h>
115 
116 #include <net/if.h>
117 #include <net/if_arp.h>
118 #include <net/if_dl.h>
119 #include <net/if_media.h>
120 #include <net/if_ether.h>
121 
122 #include <net/bpf.h>
123 
124 #include <sys/bus.h>
125 #include <sys/intr.h>
126 #include <machine/endian.h>
127 
128 #include <dev/mii/mii.h>
129 #include <dev/mii/miivar.h>
130 #include <dev/mii/mii_bitbang.h>
131 
132 #include <dev/pci/pcireg.h>
133 #include <dev/pci/pcivar.h>
134 #include <dev/pci/pcidevs.h>
135 
136 #include <dev/pci/if_vrreg.h>
137 
138 #define	VR_USEIOSPACE
139 
140 /*
141  * Various supported device vendors/types and their names.
142  */
143 static const struct vr_type {
144 	pci_vendor_id_t		vr_vid;
145 	pci_product_id_t	vr_did;
146 } vr_devs[] = {
147 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT3043 },
148 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT6102 },
149 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT6105 },
150 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT6105M },
151 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A }
152 };
153 
154 /*
155  * Transmit descriptor list size.
156  */
157 #define	VR_NTXDESC		64
158 #define	VR_NTXDESC_MASK		(VR_NTXDESC - 1)
159 #define	VR_NEXTTX(x)		(((x) + 1) & VR_NTXDESC_MASK)
160 
161 /*
162  * Receive descriptor list size.
163  */
164 #define	VR_NRXDESC		64
165 #define	VR_NRXDESC_MASK		(VR_NRXDESC - 1)
166 #define	VR_NEXTRX(x)		(((x) + 1) & VR_NRXDESC_MASK)
167 
168 /*
169  * Control data structres that are DMA'd to the Rhine chip.  We allocate
170  * them in a single clump that maps to a single DMA segment to make several
171  * things easier.
172  *
173  * Note that since we always copy outgoing packets to aligned transmit
174  * buffers, we can reduce the transmit descriptors to one per packet.
175  */
176 struct vr_control_data {
177 	struct vr_desc		vr_txdescs[VR_NTXDESC];
178 	struct vr_desc		vr_rxdescs[VR_NRXDESC];
179 };
180 
181 #define	VR_CDOFF(x)		offsetof(struct vr_control_data, x)
182 #define	VR_CDTXOFF(x)		VR_CDOFF(vr_txdescs[(x)])
183 #define	VR_CDRXOFF(x)		VR_CDOFF(vr_rxdescs[(x)])
184 
185 /*
186  * Software state of transmit and receive descriptors.
187  */
188 struct vr_descsoft {
189 	struct mbuf		*ds_mbuf;	/* head of mbuf chain */
190 	bus_dmamap_t		ds_dmamap;	/* our DMA map */
191 };
192 
193 struct vr_softc {
194 	device_t		vr_dev;
195 	void			*vr_ih;		/* interrupt cookie */
196 	bus_space_tag_t		vr_bst;		/* bus space tag */
197 	bus_space_handle_t	vr_bsh;		/* bus space handle */
198 	bus_dma_tag_t		vr_dmat;	/* bus DMA tag */
199 	pci_chipset_tag_t	vr_pc;		/* PCI chipset info */
200 	pcitag_t		vr_tag;		/* PCI tag */
201 	struct ethercom		vr_ec;		/* Ethernet common info */
202 	uint8_t			vr_enaddr[ETHER_ADDR_LEN];
203 	struct mii_data		vr_mii;		/* MII/media info */
204 
205 	pcireg_t		vr_id;		/* vendor/product ID */
206 	uint8_t			vr_revid;	/* Rhine chip revision */
207 
208 	callout_t		vr_tick_ch;	/* tick callout */
209 
210 	bus_dmamap_t		vr_cddmamap;	/* control data DMA map */
211 #define	vr_cddma	vr_cddmamap->dm_segs[0].ds_addr
212 
213 	/*
214 	 * Software state for transmit and receive descriptors.
215 	 */
216 	struct vr_descsoft	vr_txsoft[VR_NTXDESC];
217 	struct vr_descsoft	vr_rxsoft[VR_NRXDESC];
218 
219 	/*
220 	 * Control data structures.
221 	 */
222 	struct vr_control_data	*vr_control_data;
223 
224 	int	vr_txpending;		/* number of TX requests pending */
225 	int	vr_txdirty;		/* first dirty TX descriptor */
226 	int	vr_txlast;		/* last used TX descriptor */
227 
228 	int	vr_rxptr;		/* next ready RX descriptor */
229 
230 	uint32_t	vr_save_iobase;
231 	uint32_t	vr_save_membase;
232 	uint32_t	vr_save_irq;
233 
234 	bool		vr_link;
235 	int		vr_flags;
236 #define VR_F_RESTART	0x1		/* restart on next tick */
237 	u_short		vr_if_flags;
238 
239 	krndsource_t rnd_source;	/* random source */
240 };
241 
242 #define	VR_CDTXADDR(sc, x)	((sc)->vr_cddma + VR_CDTXOFF((x)))
243 #define	VR_CDRXADDR(sc, x)	((sc)->vr_cddma + VR_CDRXOFF((x)))
244 
245 #define	VR_CDTX(sc, x)		(&(sc)->vr_control_data->vr_txdescs[(x)])
246 #define	VR_CDRX(sc, x)		(&(sc)->vr_control_data->vr_rxdescs[(x)])
247 
248 #define	VR_DSTX(sc, x)		(&(sc)->vr_txsoft[(x)])
249 #define	VR_DSRX(sc, x)		(&(sc)->vr_rxsoft[(x)])
250 
251 #define	VR_CDTXSYNC(sc, x, ops)						\
252 	bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap,		\
253 	    VR_CDTXOFF((x)), sizeof(struct vr_desc), (ops))
254 
255 #define	VR_CDRXSYNC(sc, x, ops)						\
256 	bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap,		\
257 	    VR_CDRXOFF((x)), sizeof(struct vr_desc), (ops))
258 
259 /*
260  * Note we rely on MCLBYTES being a power of two below.
261  */
262 #define	VR_INIT_RXDESC(sc, i)						\
263 do {									\
264 	struct vr_desc *__d = VR_CDRX((sc), (i));			\
265 	struct vr_descsoft *__ds = VR_DSRX((sc), (i));			\
266 									\
267 	__d->vr_next = htole32(VR_CDRXADDR((sc), VR_NEXTRX((i))));	\
268 	__d->vr_data = htole32(__ds->ds_dmamap->dm_segs[0].ds_addr);	\
269 	__d->vr_ctl = htole32(VR_RXCTL_CHAIN | VR_RXCTL_RX_INTR |	\
270 	    ((MCLBYTES - 1) & VR_RXCTL_BUFLEN));			\
271 	__d->vr_status = htole32(VR_RXSTAT_FIRSTFRAG |			\
272 	    VR_RXSTAT_LASTFRAG | VR_RXSTAT_OWN);			\
273 	VR_CDRXSYNC((sc), (i), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \
274 } while (/* CONSTCOND */ 0)
275 
276 /*
277  * register space access macros
278  */
279 #define	CSR_WRITE_4(sc, reg, val)					\
280 	bus_space_write_4(sc->vr_bst, sc->vr_bsh, reg, val)
281 #define	CSR_WRITE_2(sc, reg, val)					\
282 	bus_space_write_2(sc->vr_bst, sc->vr_bsh, reg, val)
283 #define	CSR_WRITE_1(sc, reg, val)					\
284 	bus_space_write_1(sc->vr_bst, sc->vr_bsh, reg, val)
285 
286 #define	CSR_READ_4(sc, reg)						\
287 	bus_space_read_4(sc->vr_bst, sc->vr_bsh, reg)
288 #define	CSR_READ_2(sc, reg)						\
289 	bus_space_read_2(sc->vr_bst, sc->vr_bsh, reg)
290 #define	CSR_READ_1(sc, reg)						\
291 	bus_space_read_1(sc->vr_bst, sc->vr_bsh, reg)
292 
293 #define	VR_TIMEOUT		1000
294 
295 static int	vr_add_rxbuf(struct vr_softc *, int);
296 
297 static void	vr_rxeof(struct vr_softc *);
298 static void	vr_rxeoc(struct vr_softc *);
299 static void	vr_txeof(struct vr_softc *);
300 static int	vr_intr(void *);
301 static void	vr_start(struct ifnet *);
302 static int	vr_ioctl(struct ifnet *, u_long, void *);
303 static int	vr_init(struct ifnet *);
304 static void	vr_stop(struct ifnet *, int);
305 static void	vr_rxdrain(struct vr_softc *);
306 static void	vr_watchdog(struct ifnet *);
307 static void	vr_tick(void *);
308 
309 static int	vr_mii_readreg(device_t, int, int, uint16_t *);
310 static int	vr_mii_writereg(device_t, int, int, uint16_t);
311 static void	vr_mii_statchg(struct ifnet *);
312 
313 static void	vr_setmulti(struct vr_softc *);
314 static void	vr_reset(struct vr_softc *);
315 static int	vr_restore_state(pci_chipset_tag_t, pcitag_t, device_t,
316     pcireg_t);
317 static bool	vr_resume(device_t, const pmf_qual_t *);
318 
319 int	vr_copy_small = 0;
320 
321 #define	VR_SETBIT(sc, reg, x)				\
322 	CSR_WRITE_1(sc, reg,				\
323 	    CSR_READ_1(sc, reg) | (x))
324 
325 #define	VR_CLRBIT(sc, reg, x)				\
326 	CSR_WRITE_1(sc, reg,				\
327 	    CSR_READ_1(sc, reg) & ~(x))
328 
329 #define	VR_SETBIT16(sc, reg, x)				\
330 	CSR_WRITE_2(sc, reg,				\
331 	    CSR_READ_2(sc, reg) | (x))
332 
333 #define	VR_CLRBIT16(sc, reg, x)				\
334 	CSR_WRITE_2(sc, reg,				\
335 	    CSR_READ_2(sc, reg) & ~(x))
336 
337 #define	VR_SETBIT32(sc, reg, x)				\
338 	CSR_WRITE_4(sc, reg,				\
339 	    CSR_READ_4(sc, reg) | (x))
340 
341 #define	VR_CLRBIT32(sc, reg, x)				\
342 	CSR_WRITE_4(sc, reg,				\
343 	    CSR_READ_4(sc, reg) & ~(x))
344 
345 /*
346  * MII bit-bang glue.
347  */
348 static uint32_t vr_mii_bitbang_read(device_t);
349 static void	vr_mii_bitbang_write(device_t, uint32_t);
350 
351 static const struct mii_bitbang_ops vr_mii_bitbang_ops = {
352 	vr_mii_bitbang_read,
353 	vr_mii_bitbang_write,
354 	{
355 		VR_MIICMD_DATAOUT,	/* MII_BIT_MDO */
356 		VR_MIICMD_DATAIN,	/* MII_BIT_MDI */
357 		VR_MIICMD_CLK,		/* MII_BIT_MDC */
358 		VR_MIICMD_DIR,		/* MII_BIT_DIR_HOST_PHY */
359 		0,			/* MII_BIT_DIR_PHY_HOST */
360 	}
361 };
362 
363 static uint32_t
364 vr_mii_bitbang_read(device_t self)
365 {
366 	struct vr_softc *sc = device_private(self);
367 
368 	return (CSR_READ_1(sc, VR_MIICMD));
369 }
370 
371 static void
372 vr_mii_bitbang_write(device_t self, uint32_t val)
373 {
374 	struct vr_softc *sc = device_private(self);
375 
376 	CSR_WRITE_1(sc, VR_MIICMD, (val & 0xff) | VR_MIICMD_DIRECTPGM);
377 }
378 
379 /*
380  * Read an PHY register through the MII.
381  */
382 static int
383 vr_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
384 {
385 	struct vr_softc *sc = device_private(self);
386 
387 	CSR_WRITE_1(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
388 	return (mii_bitbang_readreg(self, &vr_mii_bitbang_ops, phy, reg, val));
389 }
390 
391 /*
392  * Write to a PHY register through the MII.
393  */
394 static int
395 vr_mii_writereg(device_t self, int phy, int reg, uint16_t val)
396 {
397 	struct vr_softc *sc = device_private(self);
398 
399 	CSR_WRITE_1(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
400 	return mii_bitbang_writereg(self, &vr_mii_bitbang_ops, phy, reg, val);
401 }
402 
403 static void
404 vr_mii_statchg(struct ifnet *ifp)
405 {
406 	struct vr_softc *sc = ifp->if_softc;
407 	int i;
408 
409 	/*
410 	 * In order to fiddle with the 'full-duplex' bit in the netconfig
411 	 * register, we first have to put the transmit and/or receive logic
412 	 * in the idle state.
413 	 */
414 	if ((sc->vr_mii.mii_media_status & IFM_ACTIVE) &&
415 	    IFM_SUBTYPE(sc->vr_mii.mii_media_active) != IFM_NONE) {
416 		sc->vr_link = true;
417 
418 		if (CSR_READ_2(sc, VR_COMMAND) & (VR_CMD_TX_ON | VR_CMD_RX_ON))
419 			VR_CLRBIT16(sc, VR_COMMAND,
420 			    (VR_CMD_TX_ON | VR_CMD_RX_ON));
421 
422 		if (sc->vr_mii.mii_media_active & IFM_FDX)
423 			VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
424 		else
425 			VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
426 
427 		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON | VR_CMD_RX_ON);
428 	} else {
429 		sc->vr_link = false;
430 		VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_TX_ON | VR_CMD_RX_ON);
431 		for (i = VR_TIMEOUT; i > 0; i--) {
432 			delay(10);
433 			if (!(CSR_READ_2(sc, VR_COMMAND) &
434 			    (VR_CMD_TX_ON | VR_CMD_RX_ON)))
435 				break;
436 		}
437 		if (i == 0) {
438 #ifdef VR_DEBUG
439 			aprint_error_dev(sc->vr_dev, "rx shutdown error!\n");
440 #endif
441 			sc->vr_flags |= VR_F_RESTART;
442 		}
443 	}
444 }
445 
446 #define	vr_calchash(addr) \
447 	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
448 
449 /*
450  * Program the 64-bit multicast hash filter.
451  */
452 static void
453 vr_setmulti(struct vr_softc *sc)
454 {
455 	struct ethercom *ec = &sc->vr_ec;
456 	struct ifnet *ifp = &ec->ec_if;
457 	int h = 0;
458 	uint32_t hashes[2] = { 0, 0 };
459 	struct ether_multistep step;
460 	struct ether_multi *enm;
461 	int mcnt = 0;
462 	uint8_t rxfilt;
463 
464 	rxfilt = CSR_READ_1(sc, VR_RXCFG);
465 
466 	if (ifp->if_flags & IFF_PROMISC) {
467 allmulti:
468 		ifp->if_flags |= IFF_ALLMULTI;
469 		rxfilt |= VR_RXCFG_RX_MULTI;
470 		CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
471 		CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
472 		CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
473 		return;
474 	}
475 
476 	/* first, zot all the existing hash bits */
477 	CSR_WRITE_4(sc, VR_MAR0, 0);
478 	CSR_WRITE_4(sc, VR_MAR1, 0);
479 
480 	/* now program new ones */
481 	ETHER_LOCK(ec);
482 	ETHER_FIRST_MULTI(step, ec, enm);
483 	while (enm != NULL) {
484 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
485 		    ETHER_ADDR_LEN) != 0) {
486 			ETHER_UNLOCK(ec);
487 			goto allmulti;
488 		}
489 
490 		h = vr_calchash(enm->enm_addrlo);
491 
492 		if (h < 32)
493 			hashes[0] |= (1 << h);
494 		else
495 			hashes[1] |= (1 << (h - 32));
496 		ETHER_NEXT_MULTI(step, enm);
497 		mcnt++;
498 	}
499 	ETHER_UNLOCK(ec);
500 
501 	ifp->if_flags &= ~IFF_ALLMULTI;
502 
503 	if (mcnt)
504 		rxfilt |= VR_RXCFG_RX_MULTI;
505 	else
506 		rxfilt &= ~VR_RXCFG_RX_MULTI;
507 
508 	CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
509 	CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
510 	CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
511 }
512 
513 static void
514 vr_reset(struct vr_softc *sc)
515 {
516 	int i;
517 
518 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
519 
520 	for (i = 0; i < VR_TIMEOUT; i++) {
521 		DELAY(10);
522 		if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
523 			break;
524 	}
525 	if (i == VR_TIMEOUT) {
526 		if (sc->vr_revid < REV_ID_VT3065_A) {
527 			aprint_error_dev(sc->vr_dev,
528 			    "reset never completed!\n");
529 		} else {
530 			/* Use newer force reset command */
531 			aprint_normal_dev(sc->vr_dev,
532 			    "using force reset command.\n");
533 			VR_SETBIT(sc, VR_MISC_CR1, VR_MISCCR1_FORSRST);
534 		}
535 	}
536 
537 	/* Wait a little while for the chip to get its brains in order. */
538 	DELAY(1000);
539 }
540 
541 /*
542  * Initialize an RX descriptor and attach an MBUF cluster.
543  * Note: the length fields are only 11 bits wide, which means the
544  * largest size we can specify is 2047. This is important because
545  * MCLBYTES is 2048, so we have to subtract one otherwise we'll
546  * overflow the field and make a mess.
547  */
548 static int
549 vr_add_rxbuf(struct vr_softc *sc, int i)
550 {
551 	struct vr_descsoft *ds = VR_DSRX(sc, i);
552 	struct mbuf *m_new;
553 	int error;
554 
555 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
556 	if (m_new == NULL)
557 		return (ENOBUFS);
558 
559 	MCLGET(m_new, M_DONTWAIT);
560 	if ((m_new->m_flags & M_EXT) == 0) {
561 		m_freem(m_new);
562 		return (ENOBUFS);
563 	}
564 
565 	if (ds->ds_mbuf != NULL)
566 		bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
567 
568 	ds->ds_mbuf = m_new;
569 
570 	error = bus_dmamap_load(sc->vr_dmat, ds->ds_dmamap,
571 	    m_new->m_ext.ext_buf, m_new->m_ext.ext_size, NULL,
572 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
573 	if (error) {
574 		aprint_error_dev(sc->vr_dev,
575 		    "unable to load rx DMA map %d, error = %d\n", i, error);
576 		panic("vr_add_rxbuf");		/* XXX */
577 	}
578 
579 	bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
580 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
581 
582 	VR_INIT_RXDESC(sc, i);
583 
584 	return (0);
585 }
586 
587 /*
588  * A frame has been uploaded: pass the resulting mbuf chain up to
589  * the higher level protocols.
590  */
591 static void
592 vr_rxeof(struct vr_softc *sc)
593 {
594 	struct mbuf *m;
595 	struct ifnet *ifp;
596 	struct vr_desc *d;
597 	struct vr_descsoft *ds;
598 	int i, total_len;
599 	uint32_t rxstat;
600 
601 	ifp = &sc->vr_ec.ec_if;
602 
603 	for (i = sc->vr_rxptr;; i = VR_NEXTRX(i)) {
604 		d = VR_CDRX(sc, i);
605 		ds = VR_DSRX(sc, i);
606 
607 		VR_CDRXSYNC(sc, i,
608 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
609 
610 		rxstat = le32toh(d->vr_status);
611 
612 		if (rxstat & VR_RXSTAT_OWN) {
613 			/*
614 			 * We have processed all of the receive buffers.
615 			 */
616 			break;
617 		}
618 
619 		/*
620 		 * If an error occurs, update stats, clear the
621 		 * status word and leave the mbuf cluster in place:
622 		 * it should simply get re-used next time this descriptor
623 		 * comes up in the ring.
624 		 */
625 		if (rxstat & VR_RXSTAT_RXERR) {
626 			const char *errstr;
627 
628 			ifp->if_ierrors++;
629 			switch (rxstat & 0x000000FF) {
630 			case VR_RXSTAT_CRCERR:
631 				errstr = "crc error";
632 				break;
633 			case VR_RXSTAT_FRAMEALIGNERR:
634 				errstr = "frame alignment error";
635 				break;
636 			case VR_RXSTAT_FIFOOFLOW:
637 				errstr = "FIFO overflow";
638 				break;
639 			case VR_RXSTAT_GIANT:
640 				errstr = "received giant packet";
641 				break;
642 			case VR_RXSTAT_RUNT:
643 				errstr = "received runt packet";
644 				break;
645 			case VR_RXSTAT_BUSERR:
646 				errstr = "system bus error";
647 				break;
648 			case VR_RXSTAT_BUFFERR:
649 				errstr = "rx buffer error";
650 				break;
651 			default:
652 				errstr = "unknown rx error";
653 				break;
654 			}
655 			aprint_error_dev(sc->vr_dev, "receive error: %s\n",
656 			    errstr);
657 
658 			VR_INIT_RXDESC(sc, i);
659 
660 			continue;
661 		} else if (!(rxstat & VR_RXSTAT_FIRSTFRAG) ||
662 			   !(rxstat & VR_RXSTAT_LASTFRAG)) {
663 			/*
664 			 * This driver expects to receive whole packets every
665 			 * time.  In case we receive a fragment that is not
666 			 * a complete packet, we discard it.
667 			 */
668 			ifp->if_ierrors++;
669 
670 			aprint_error_dev(sc->vr_dev,
671 			    "receive error: incomplete frame; "
672 			    "size = %d, status = 0x%x\n",
673 			    VR_RXBYTES(le32toh(d->vr_status)), rxstat);
674 
675 			VR_INIT_RXDESC(sc, i);
676 
677 			continue;
678 		}
679 
680 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
681 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
682 
683 		/* No errors; receive the packet. */
684 		total_len = VR_RXBYTES(le32toh(d->vr_status));
685 #ifdef DIAGNOSTIC
686 		if (total_len == 0) {
687 			/*
688 			 * If we receive a zero-length packet, we probably
689 			 * missed to handle an error condition above.
690 			 * Discard it to avoid a later crash.
691 			 */
692 			ifp->if_ierrors++;
693 
694 			aprint_error_dev(sc->vr_dev,
695 			    "receive error: zero-length packet; "
696 			    "status = 0x%x\n", rxstat);
697 
698 			VR_INIT_RXDESC(sc, i);
699 
700 			continue;
701 		}
702 #endif
703 
704 		/*
705 		 * The Rhine chip includes the CRC with every packet.
706 		 * Trim it off here.
707 		 */
708 		total_len -= ETHER_CRC_LEN;
709 
710 #ifdef __NO_STRICT_ALIGNMENT
711 		/*
712 		 * If the packet is small enough to fit in a
713 		 * single header mbuf, allocate one and copy
714 		 * the data into it.  This greatly reduces
715 		 * memory consumption when we receive lots
716 		 * of small packets.
717 		 *
718 		 * Otherwise, we add a new buffer to the receive
719 		 * chain.  If this fails, we drop the packet and
720 		 * recycle the old buffer.
721 		 */
722 		if (vr_copy_small != 0 && total_len <= MHLEN) {
723 			MGETHDR(m, M_DONTWAIT, MT_DATA);
724 			if (m == NULL)
725 				goto dropit;
726 			memcpy(mtod(m, void *),
727 			    mtod(ds->ds_mbuf, void *), total_len);
728 			VR_INIT_RXDESC(sc, i);
729 			bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
730 			    ds->ds_dmamap->dm_mapsize,
731 			    BUS_DMASYNC_PREREAD);
732 		} else {
733 			m = ds->ds_mbuf;
734 			if (vr_add_rxbuf(sc, i) == ENOBUFS) {
735  dropit:
736 				ifp->if_ierrors++;
737 				VR_INIT_RXDESC(sc, i);
738 				bus_dmamap_sync(sc->vr_dmat,
739 				    ds->ds_dmamap, 0,
740 				    ds->ds_dmamap->dm_mapsize,
741 				    BUS_DMASYNC_PREREAD);
742 				continue;
743 			}
744 		}
745 #else
746 		/*
747 		 * The Rhine's packet buffers must be 4-byte aligned.
748 		 * But this means that the data after the Ethernet header
749 		 * is misaligned.  We must allocate a new buffer and
750 		 * copy the data, shifted forward 2 bytes.
751 		 */
752 		MGETHDR(m, M_DONTWAIT, MT_DATA);
753 		if (m == NULL) {
754  dropit:
755 			ifp->if_ierrors++;
756 			VR_INIT_RXDESC(sc, i);
757 			bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
758 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
759 			continue;
760 		}
761 		if (total_len > (MHLEN - 2)) {
762 			MCLGET(m, M_DONTWAIT);
763 			if ((m->m_flags & M_EXT) == 0) {
764 				m_freem(m);
765 				goto dropit;
766 			}
767 		}
768 		m->m_data += 2;
769 
770 		/*
771 		 * Note that we use clusters for incoming frames, so the
772 		 * buffer is virtually contiguous.
773 		 */
774 		memcpy(mtod(m, void *), mtod(ds->ds_mbuf, void *),
775 		    total_len);
776 
777 		/* Allow the receive descriptor to continue using its mbuf. */
778 		VR_INIT_RXDESC(sc, i);
779 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
780 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
781 #endif /* __NO_STRICT_ALIGNMENT */
782 
783 		m_set_rcvif(m, ifp);
784 		m->m_pkthdr.len = m->m_len = total_len;
785 		/* Pass it on. */
786 		if_percpuq_enqueue(ifp->if_percpuq, m);
787 	}
788 
789 	/* Update the receive pointer. */
790 	sc->vr_rxptr = i;
791 }
792 
793 void
794 vr_rxeoc(struct vr_softc *sc)
795 {
796 	struct ifnet *ifp;
797 	int i;
798 
799 	ifp = &sc->vr_ec.ec_if;
800 
801 	ifp->if_ierrors++;
802 
803 	VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
804 	for (i = 0; i < VR_TIMEOUT; i++) {
805 		DELAY(10);
806 		if ((CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RX_ON) == 0)
807 			break;
808 	}
809 	if (i == VR_TIMEOUT) {
810 		/* XXX need reset? */
811 		aprint_error_dev(sc->vr_dev, "RX shutdown never completed\n");
812 	}
813 
814 	vr_rxeof(sc);
815 
816 	CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
817 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
818 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
819 }
820 
821 /*
822  * A frame was downloaded to the chip. It's safe for us to clean up
823  * the list buffers.
824  */
825 static void
826 vr_txeof(struct vr_softc *sc)
827 {
828 	struct ifnet *ifp = &sc->vr_ec.ec_if;
829 	struct vr_desc *d;
830 	struct vr_descsoft *ds;
831 	uint32_t txstat;
832 	int i, j;
833 
834 	ifp->if_flags &= ~IFF_OACTIVE;
835 
836 	/*
837 	 * Go through our tx list and free mbufs for those
838 	 * frames that have been transmitted.
839 	 */
840 	for (i = sc->vr_txdirty; sc->vr_txpending != 0;
841 	     i = VR_NEXTTX(i), sc->vr_txpending--) {
842 		d = VR_CDTX(sc, i);
843 		ds = VR_DSTX(sc, i);
844 
845 		VR_CDTXSYNC(sc, i,
846 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
847 
848 		txstat = le32toh(d->vr_status);
849 
850 		if (txstat & (VR_TXSTAT_ABRT | VR_TXSTAT_UDF)) {
851 			VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
852 			for (j = 0; j < VR_TIMEOUT; j++) {
853 				DELAY(10);
854 				if ((CSR_READ_2(sc, VR_COMMAND) &
855 				    VR_CMD_TX_ON) == 0)
856 					break;
857 			}
858 			if (j == VR_TIMEOUT) {
859 				/* XXX need reset? */
860 				aprint_error_dev(sc->vr_dev,
861 				    "TX shutdown never completed\n");
862 			}
863 			d->vr_status = htole32(VR_TXSTAT_OWN);
864 			CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, i));
865 			break;
866 		}
867 
868 		if (txstat & VR_TXSTAT_OWN)
869 			break;
870 
871 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
872 		    0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
873 		bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
874 		m_freem(ds->ds_mbuf);
875 		ds->ds_mbuf = NULL;
876 
877 		if (txstat & VR_TXSTAT_ERRSUM) {
878 			ifp->if_oerrors++;
879 			if (txstat & VR_TXSTAT_DEFER)
880 				ifp->if_collisions++;
881 			if (txstat & VR_TXSTAT_LATECOLL)
882 				ifp->if_collisions++;
883 		}
884 
885 		ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
886 		ifp->if_opackets++;
887 	}
888 
889 	/* Update the dirty transmit buffer pointer. */
890 	sc->vr_txdirty = i;
891 
892 	/*
893 	 * Cancel the watchdog timer if there are no pending
894 	 * transmissions.
895 	 */
896 	if (sc->vr_txpending == 0)
897 		ifp->if_timer = 0;
898 }
899 
900 static int
901 vr_intr(void *arg)
902 {
903 	struct vr_softc *sc;
904 	struct ifnet *ifp;
905 	uint16_t status;
906 	int handled = 0, dotx = 0;
907 
908 	sc = arg;
909 	ifp = &sc->vr_ec.ec_if;
910 
911 	/* Suppress unwanted interrupts. */
912 	if ((ifp->if_flags & IFF_UP) == 0) {
913 		vr_stop(ifp, 1);
914 		return (0);
915 	}
916 
917 	/* Disable interrupts. */
918 	CSR_WRITE_2(sc, VR_IMR, 0x0000);
919 
920 	for (;;) {
921 		status = CSR_READ_2(sc, VR_ISR);
922 		if (status)
923 			CSR_WRITE_2(sc, VR_ISR, status);
924 
925 		if ((status & VR_INTRS) == 0)
926 			break;
927 
928 		handled = 1;
929 
930 		rnd_add_uint32(&sc->rnd_source, status);
931 
932 		if (status & VR_ISR_RX_OK)
933 			vr_rxeof(sc);
934 
935 		if (status & VR_ISR_RX_DROPPED) {
936 			aprint_error_dev(sc->vr_dev, "rx packet lost\n");
937 			ifp->if_ierrors++;
938 		}
939 
940 		if (status &
941 		    (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW))
942 			vr_rxeoc(sc);
943 
944 
945 		if (status & (VR_ISR_BUSERR | VR_ISR_TX_UNDERRUN)) {
946 			if (status & VR_ISR_BUSERR)
947 				aprint_error_dev(sc->vr_dev, "PCI bus error\n");
948 			if (status & VR_ISR_TX_UNDERRUN)
949 				aprint_error_dev(sc->vr_dev,
950 				    "transmit underrun\n");
951 			/* vr_init() calls vr_start() */
952 			dotx = 0;
953 			(void)vr_init(ifp);
954 
955 		}
956 
957 		if (status & VR_ISR_TX_OK) {
958 			dotx = 1;
959 			vr_txeof(sc);
960 		}
961 
962 		if (status &
963 		    (VR_ISR_TX_ABRT | VR_ISR_TX_ABRT2 | VR_ISR_TX_UDFI)) {
964 			if (status & (VR_ISR_TX_ABRT | VR_ISR_TX_ABRT2))
965 				aprint_error_dev(sc->vr_dev,
966 				    "transmit aborted\n");
967 			if (status & VR_ISR_TX_UDFI)
968 				aprint_error_dev(sc->vr_dev,
969 				    "transmit underflow\n");
970 			ifp->if_oerrors++;
971 			dotx = 1;
972 			vr_txeof(sc);
973 			if (sc->vr_txpending) {
974 				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
975 				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
976 			}
977 		}
978 	}
979 
980 	/* Re-enable interrupts. */
981 	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
982 
983 	if (dotx)
984 		if_schedule_deferred_start(ifp);
985 
986 	return (handled);
987 }
988 
989 /*
990  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
991  * to the mbuf data regions directly in the transmit lists. We also save a
992  * copy of the pointers since the transmit list fragment pointers are
993  * physical addresses.
994  */
995 static void
996 vr_start(struct ifnet *ifp)
997 {
998 	struct vr_softc *sc = ifp->if_softc;
999 	struct mbuf *m0, *m;
1000 	struct vr_desc *d;
1001 	struct vr_descsoft *ds;
1002 	int error, firsttx, nexttx, opending;
1003 
1004 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1005 		return;
1006 	if (sc->vr_link == false)
1007 		return;
1008 
1009 	/*
1010 	 * Remember the previous txpending and the first transmit
1011 	 * descriptor we use.
1012 	 */
1013 	opending = sc->vr_txpending;
1014 	firsttx = VR_NEXTTX(sc->vr_txlast);
1015 
1016 	/*
1017 	 * Loop through the send queue, setting up transmit descriptors
1018 	 * until we drain the queue, or use up all available transmit
1019 	 * descriptors.
1020 	 */
1021 	while (sc->vr_txpending < VR_NTXDESC) {
1022 		/*
1023 		 * Grab a packet off the queue.
1024 		 */
1025 		IFQ_POLL(&ifp->if_snd, m0);
1026 		if (m0 == NULL)
1027 			break;
1028 		m = NULL;
1029 
1030 		/*
1031 		 * Get the next available transmit descriptor.
1032 		 */
1033 		nexttx = VR_NEXTTX(sc->vr_txlast);
1034 		d = VR_CDTX(sc, nexttx);
1035 		ds = VR_DSTX(sc, nexttx);
1036 
1037 		/*
1038 		 * Load the DMA map.  If this fails, the packet didn't
1039 		 * fit in one DMA segment, and we need to copy.  Note,
1040 		 * the packet must also be aligned.
1041 		 * if the packet is too small, copy it too, so we're sure
1042 		 * we have enough room for the pad buffer.
1043 		 */
1044 		if ((mtod(m0, uintptr_t) & 3) != 0 ||
1045 		    m0->m_pkthdr.len < VR_MIN_FRAMELEN ||
1046 		    bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
1047 		     BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
1048 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1049 			if (m == NULL) {
1050 				aprint_error_dev(sc->vr_dev,
1051 				    "unable to allocate Tx mbuf\n");
1052 				break;
1053 			}
1054 			if (m0->m_pkthdr.len > MHLEN) {
1055 				MCLGET(m, M_DONTWAIT);
1056 				if ((m->m_flags & M_EXT) == 0) {
1057 					aprint_error_dev(sc->vr_dev,
1058 					    "unable to allocate Tx cluster\n");
1059 					m_freem(m);
1060 					break;
1061 				}
1062 			}
1063 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
1064 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1065 			/*
1066 			 * The Rhine doesn't auto-pad, so we have to do this
1067 			 * ourselves.
1068 			 */
1069 			if (m0->m_pkthdr.len < VR_MIN_FRAMELEN) {
1070 				memset(mtod(m, char *) + m0->m_pkthdr.len,
1071 				    0, VR_MIN_FRAMELEN - m0->m_pkthdr.len);
1072 				m->m_pkthdr.len = m->m_len = VR_MIN_FRAMELEN;
1073 			}
1074 			error = bus_dmamap_load_mbuf(sc->vr_dmat,
1075 			    ds->ds_dmamap, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1076 			if (error) {
1077 				m_freem(m);
1078 				aprint_error_dev(sc->vr_dev, "unable to load "
1079 				    "Tx buffer, error = %d\n", error);
1080 				break;
1081 			}
1082 		}
1083 
1084 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1085 		if (m != NULL) {
1086 			m_freem(m0);
1087 			m0 = m;
1088 		}
1089 
1090 		/* Sync the DMA map. */
1091 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
1092 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1093 
1094 		/*
1095 		 * Store a pointer to the packet so we can free it later.
1096 		 */
1097 		ds->ds_mbuf = m0;
1098 
1099 		/*
1100 		 * If there's a BPF listener, bounce a copy of this frame
1101 		 * to him.
1102 		 */
1103 		bpf_mtap(ifp, m0, BPF_D_OUT);
1104 
1105 		/*
1106 		 * Fill in the transmit descriptor.
1107 		 */
1108 		d->vr_data = htole32(ds->ds_dmamap->dm_segs[0].ds_addr);
1109 		d->vr_ctl = htole32(m0->m_pkthdr.len);
1110 		d->vr_ctl |= htole32(VR_TXCTL_FIRSTFRAG | VR_TXCTL_LASTFRAG);
1111 
1112 		/*
1113 		 * If this is the first descriptor we're enqueuing,
1114 		 * don't give it to the Rhine yet.  That could cause
1115 		 * a race condition.  We'll do it below.
1116 		 */
1117 		if (nexttx == firsttx)
1118 			d->vr_status = 0;
1119 		else
1120 			d->vr_status = htole32(VR_TXSTAT_OWN);
1121 
1122 		VR_CDTXSYNC(sc, nexttx,
1123 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1124 
1125 		/* Advance the tx pointer. */
1126 		sc->vr_txpending++;
1127 		sc->vr_txlast = nexttx;
1128 	}
1129 
1130 	if (sc->vr_txpending == VR_NTXDESC) {
1131 		/* No more slots left; notify upper layer. */
1132 		ifp->if_flags |= IFF_OACTIVE;
1133 	}
1134 
1135 	if (sc->vr_txpending != opending) {
1136 		/*
1137 		 * We enqueued packets.  If the transmitter was idle,
1138 		 * reset the txdirty pointer.
1139 		 */
1140 		if (opending == 0)
1141 			sc->vr_txdirty = firsttx;
1142 
1143 		/*
1144 		 * Cause a transmit interrupt to happen on the
1145 		 * last packet we enqueued.
1146 		 */
1147 		VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= htole32(VR_TXCTL_FINT);
1148 		VR_CDTXSYNC(sc, sc->vr_txlast,
1149 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1150 
1151 		/*
1152 		 * The entire packet chain is set up.  Give the
1153 		 * first descriptor to the Rhine now.
1154 		 */
1155 		VR_CDTX(sc, firsttx)->vr_status = htole32(VR_TXSTAT_OWN);
1156 		VR_CDTXSYNC(sc, firsttx,
1157 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1158 
1159 		/* Start the transmitter. */
1160 		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1161 
1162 		/* Set the watchdog timer in case the chip flakes out. */
1163 		ifp->if_timer = 5;
1164 	}
1165 }
1166 
1167 /*
1168  * Initialize the interface.  Must be called at splnet.
1169  */
1170 static int
1171 vr_init(struct ifnet *ifp)
1172 {
1173 	struct vr_softc *sc = ifp->if_softc;
1174 	struct vr_desc *d;
1175 	struct vr_descsoft *ds;
1176 	int i, error = 0;
1177 
1178 	/* Cancel pending I/O. */
1179 	vr_stop(ifp, 0);
1180 
1181 	/* Reset the Rhine to a known state. */
1182 	vr_reset(sc);
1183 
1184 	/* set DMA length in BCR0 and BCR1 */
1185 	VR_CLRBIT(sc, VR_BCR0, VR_BCR0_DMA_LENGTH);
1186 	VR_SETBIT(sc, VR_BCR0, VR_BCR0_DMA_STORENFWD);
1187 
1188 	VR_CLRBIT(sc, VR_BCR0, VR_BCR0_RX_THRESH);
1189 	VR_SETBIT(sc, VR_BCR0, VR_BCR0_RXTH_128BYTES);
1190 
1191 	VR_CLRBIT(sc, VR_BCR1, VR_BCR1_TX_THRESH);
1192 	VR_SETBIT(sc, VR_BCR1, VR_BCR1_TXTH_STORENFWD);
1193 
1194 	/* set DMA threshold length in RXCFG and TXCFG */
1195 	VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1196 	VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_128BYTES);
1197 
1198 	VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1199 	VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1200 
1201 	/*
1202 	 * Initialize the transmit descriptor ring.  txlast is initialized
1203 	 * to the end of the list so that it will wrap around to the first
1204 	 * descriptor when the first packet is transmitted.
1205 	 */
1206 	for (i = 0; i < VR_NTXDESC; i++) {
1207 		d = VR_CDTX(sc, i);
1208 		memset(d, 0, sizeof(struct vr_desc));
1209 		d->vr_next = htole32(VR_CDTXADDR(sc, VR_NEXTTX(i)));
1210 		VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1211 	}
1212 	sc->vr_txpending = 0;
1213 	sc->vr_txdirty = 0;
1214 	sc->vr_txlast = VR_NTXDESC - 1;
1215 
1216 	/*
1217 	 * Initialize the receive descriptor ring.
1218 	 */
1219 	for (i = 0; i < VR_NRXDESC; i++) {
1220 		ds = VR_DSRX(sc, i);
1221 		if (ds->ds_mbuf == NULL) {
1222 			if ((error = vr_add_rxbuf(sc, i)) != 0) {
1223 				aprint_error_dev(sc->vr_dev,
1224 				    "unable to allocate or map rx buffer %d, "
1225 				    "error = %d\n", i, error);
1226 				/*
1227 				 * XXX Should attempt to run with fewer receive
1228 				 * XXX buffers instead of just failing.
1229 				 */
1230 				vr_rxdrain(sc);
1231 				goto out;
1232 			}
1233 		} else
1234 			VR_INIT_RXDESC(sc, i);
1235 	}
1236 	sc->vr_rxptr = 0;
1237 
1238 	/* If we want promiscuous mode, set the allframes bit. */
1239 	if (ifp->if_flags & IFF_PROMISC)
1240 		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1241 	else
1242 		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1243 
1244 	/* Set capture broadcast bit to capture broadcast frames. */
1245 	if (ifp->if_flags & IFF_BROADCAST)
1246 		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1247 	else
1248 		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1249 
1250 	/* Program the multicast filter, if necessary. */
1251 	vr_setmulti(sc);
1252 
1253 	/* Give the transmit and receive rings to the Rhine. */
1254 	CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
1255 	CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
1256 
1257 	/* Set current media. */
1258 	sc->vr_link = true;
1259 	if ((error = ether_mediachange(ifp)) != 0)
1260 		goto out;
1261 
1262 	/* Enable receiver and transmitter. */
1263 	CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL | VR_CMD_START |
1264 				    VR_CMD_TX_ON | VR_CMD_RX_ON |
1265 				    VR_CMD_RX_GO);
1266 
1267 	/* Enable interrupts. */
1268 	CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1269 	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1270 
1271 	ifp->if_flags |= IFF_RUNNING;
1272 	ifp->if_flags &= ~IFF_OACTIVE;
1273 
1274 	/* Start one second timer. */
1275 	callout_reset(&sc->vr_tick_ch, hz, vr_tick, sc);
1276 
1277 	/* Attempt to start output on the interface. */
1278 	vr_start(ifp);
1279 
1280  out:
1281 	if (error)
1282 		aprint_error_dev(sc->vr_dev, "interface not running\n");
1283 	return (error);
1284 }
1285 
1286 static int
1287 vr_ioctl(struct ifnet *ifp, u_long command, void *data)
1288 {
1289 	struct vr_softc *sc = ifp->if_softc;
1290 	int s, error = 0;
1291 
1292 	s = splnet();
1293 
1294 	switch (command) {
1295 	case SIOCSIFFLAGS:
1296 		if ((error = ifioctl_common(ifp, command, data)) != 0)
1297 			break;
1298 
1299 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1300 		case IFF_RUNNING:
1301 			vr_stop(ifp, 1);
1302 			break;
1303 		case IFF_UP:
1304 			vr_init(ifp);
1305 			break;
1306 		case IFF_UP | IFF_RUNNING:
1307 			if ((ifp->if_flags ^ sc->vr_if_flags) == IFF_PROMISC)
1308 				vr_setmulti(sc);
1309 			else
1310 				vr_init(ifp);
1311 			break;
1312 		}
1313 		sc->vr_if_flags = ifp->if_flags;
1314 		break;
1315 	default:
1316 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
1317 			break;
1318 		error = 0;
1319 		if (command == SIOCADDMULTI || command == SIOCDELMULTI)
1320 			vr_setmulti(sc);
1321 	}
1322 	splx(s);
1323 
1324 	return error;
1325 }
1326 
1327 static void
1328 vr_watchdog(struct ifnet *ifp)
1329 {
1330 	struct vr_softc *sc = ifp->if_softc;
1331 
1332 	aprint_error_dev(sc->vr_dev, "device timeout\n");
1333 	ifp->if_oerrors++;
1334 
1335 	(void) vr_init(ifp);
1336 }
1337 
1338 /*
1339  * One second timer, used to tick MII.
1340  */
1341 static void
1342 vr_tick(void *arg)
1343 {
1344 	struct vr_softc *sc = arg;
1345 	int s;
1346 
1347 	s = splnet();
1348 	if (sc->vr_flags & VR_F_RESTART) {
1349 		aprint_normal_dev(sc->vr_dev, "restarting\n");
1350 		vr_init(&sc->vr_ec.ec_if);
1351 		sc->vr_flags &= ~VR_F_RESTART;
1352 	}
1353 	mii_tick(&sc->vr_mii);
1354 	splx(s);
1355 
1356 	callout_reset(&sc->vr_tick_ch, hz, vr_tick, sc);
1357 }
1358 
1359 /*
1360  * Drain the receive queue.
1361  */
1362 static void
1363 vr_rxdrain(struct vr_softc *sc)
1364 {
1365 	struct vr_descsoft *ds;
1366 	int i;
1367 
1368 	for (i = 0; i < VR_NRXDESC; i++) {
1369 		ds = VR_DSRX(sc, i);
1370 		if (ds->ds_mbuf != NULL) {
1371 			bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1372 			m_freem(ds->ds_mbuf);
1373 			ds->ds_mbuf = NULL;
1374 		}
1375 	}
1376 }
1377 
1378 /*
1379  * Stop the adapter and free any mbufs allocated to the
1380  * transmit lists.
1381  */
1382 static void
1383 vr_stop(struct ifnet *ifp, int disable)
1384 {
1385 	struct vr_softc *sc = ifp->if_softc;
1386 	struct vr_descsoft *ds;
1387 	int i;
1388 
1389 	/* Cancel one second timer. */
1390 	callout_stop(&sc->vr_tick_ch);
1391 
1392 	/* Down the MII. */
1393 	mii_down(&sc->vr_mii);
1394 
1395 	ifp = &sc->vr_ec.ec_if;
1396 	ifp->if_timer = 0;
1397 
1398 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1399 	VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON | VR_CMD_TX_ON));
1400 	CSR_WRITE_2(sc, VR_IMR, 0x0000);
1401 	CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1402 	CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1403 
1404 	/*
1405 	 * Release any queued transmit buffers.
1406 	 */
1407 	for (i = 0; i < VR_NTXDESC; i++) {
1408 		ds = VR_DSTX(sc, i);
1409 		if (ds->ds_mbuf != NULL) {
1410 			bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1411 			m_freem(ds->ds_mbuf);
1412 			ds->ds_mbuf = NULL;
1413 		}
1414 	}
1415 
1416 	/*
1417 	 * Mark the interface down and cancel the watchdog timer.
1418 	 */
1419 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1420 	ifp->if_timer = 0;
1421 
1422 	if (disable)
1423 		vr_rxdrain(sc);
1424 }
1425 
1426 static int	vr_probe(device_t, cfdata_t, void *);
1427 static void	vr_attach(device_t, device_t, void *);
1428 static bool	vr_shutdown(device_t, int);
1429 
1430 CFATTACH_DECL_NEW(vr, sizeof (struct vr_softc),
1431     vr_probe, vr_attach, NULL, NULL);
1432 
1433 static const struct vr_type *
1434 vr_lookup(struct pci_attach_args *pa)
1435 {
1436 	const struct vr_type *vrt;
1437 	int i;
1438 
1439 	for (i = 0; i < __arraycount(vr_devs); i++) {
1440 		vrt = &vr_devs[i];
1441 		if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1442 		    PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1443 			return (vrt);
1444 	}
1445 	return (NULL);
1446 }
1447 
1448 static int
1449 vr_probe(device_t parent, cfdata_t match, void *aux)
1450 {
1451 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1452 
1453 	if (vr_lookup(pa) != NULL)
1454 		return (1);
1455 
1456 	return (0);
1457 }
1458 
1459 /*
1460  * Stop all chip I/O so that the kernel's probe routines don't
1461  * get confused by errant DMAs when rebooting.
1462  */
1463 static bool
1464 vr_shutdown(device_t self, int howto)
1465 {
1466 	struct vr_softc *sc = device_private(self);
1467 
1468 	vr_stop(&sc->vr_ec.ec_if, 1);
1469 
1470 	return true;
1471 }
1472 
1473 /*
1474  * Attach the interface. Allocate softc structures, do ifmedia
1475  * setup and ethernet/BPF attach.
1476  */
1477 static void
1478 vr_attach(device_t parent, device_t self, void *aux)
1479 {
1480 	struct vr_softc *sc = device_private(self);
1481 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1482 	bus_dma_segment_t seg;
1483 	uint32_t reg;
1484 	struct ifnet *ifp;
1485 	struct mii_data * const mii = &sc->vr_mii;
1486 	uint8_t eaddr[ETHER_ADDR_LEN], mac;
1487 	int i, rseg, error;
1488 	char intrbuf[PCI_INTRSTR_LEN];
1489 
1490 #define	PCI_CONF_WRITE(r, v)	pci_conf_write(sc->vr_pc, sc->vr_tag, (r), (v))
1491 #define	PCI_CONF_READ(r)	pci_conf_read(sc->vr_pc, sc->vr_tag, (r))
1492 
1493 	sc->vr_dev = self;
1494 	sc->vr_pc = pa->pa_pc;
1495 	sc->vr_tag = pa->pa_tag;
1496 	sc->vr_id = pa->pa_id;
1497 	callout_init(&sc->vr_tick_ch, 0);
1498 
1499 	pci_aprint_devinfo(pa, NULL);
1500 
1501 	/*
1502 	 * Handle power management nonsense.
1503 	 */
1504 
1505 	sc->vr_save_iobase = PCI_CONF_READ(VR_PCI_LOIO);
1506 	sc->vr_save_membase = PCI_CONF_READ(VR_PCI_LOMEM);
1507 	sc->vr_save_irq = PCI_CONF_READ(PCI_INTERRUPT_REG);
1508 
1509 	/* power up chip */
1510 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
1511 	    vr_restore_state)) && error != EOPNOTSUPP) {
1512 		aprint_error_dev(self, "cannot activate %d\n", error);
1513 		return;
1514 	}
1515 
1516 	/* Make sure bus mastering is enabled. */
1517 	reg = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1518 	reg |= PCI_COMMAND_MASTER_ENABLE;
1519 	PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, reg);
1520 
1521 	/* Get revision */
1522 	sc->vr_revid = PCI_REVISION(pa->pa_class);
1523 
1524 	/*
1525 	 * Map control/status registers.
1526 	 */
1527 	{
1528 		bus_space_tag_t iot, memt;
1529 		bus_space_handle_t ioh, memh;
1530 		int ioh_valid, memh_valid;
1531 		pci_intr_handle_t intrhandle;
1532 		const char *intrstr;
1533 
1534 		ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1535 			PCI_MAPREG_TYPE_IO, 0,
1536 			&iot, &ioh, NULL, NULL) == 0);
1537 		memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1538 			PCI_MAPREG_TYPE_MEM |
1539 			PCI_MAPREG_MEM_TYPE_32BIT,
1540 			0, &memt, &memh, NULL, NULL) == 0);
1541 #if defined(VR_USEIOSPACE)
1542 		if (ioh_valid) {
1543 			sc->vr_bst = iot;
1544 			sc->vr_bsh = ioh;
1545 		} else if (memh_valid) {
1546 			sc->vr_bst = memt;
1547 			sc->vr_bsh = memh;
1548 		}
1549 #else
1550 		if (memh_valid) {
1551 			sc->vr_bst = memt;
1552 			sc->vr_bsh = memh;
1553 		} else if (ioh_valid) {
1554 			sc->vr_bst = iot;
1555 			sc->vr_bsh = ioh;
1556 		}
1557 #endif
1558 		else {
1559 			aprint_error_dev(self,
1560 			    "unable to map device registers\n");
1561 			return;
1562 		}
1563 
1564 		/* Allocate interrupt */
1565 		if (pci_intr_map(pa, &intrhandle)) {
1566 			aprint_error_dev(self, "couldn't map interrupt\n");
1567 			return;
1568 		}
1569 		intrstr = pci_intr_string(pa->pa_pc, intrhandle, intrbuf,
1570 		    sizeof(intrbuf));
1571 		sc->vr_ih = pci_intr_establish_xname(pa->pa_pc, intrhandle,
1572 		    IPL_NET, vr_intr, sc, device_xname(self));
1573 		if (sc->vr_ih == NULL) {
1574 			aprint_error_dev(self, "couldn't establish interrupt");
1575 			if (intrstr != NULL)
1576 				aprint_error(" at %s", intrstr);
1577 			aprint_error("\n");
1578 			return;
1579 		}
1580 		aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1581 	}
1582 
1583 	/*
1584 	 * Windows may put the chip in suspend mode when it
1585 	 * shuts down. Be sure to kick it in the head to wake it
1586 	 * up again.
1587 	 *
1588 	 * Don't touch this register on VT3043 since it causes
1589 	 * kernel MCHK trap on macppc.
1590 	 * (Note some VT86C100A chip returns a product ID of VT3043)
1591 	 */
1592 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT3043)
1593 		VR_CLRBIT(sc, VR_STICKHW, (VR_STICKHW_DS0 | VR_STICKHW_DS1));
1594 
1595 	/* Reset the adapter. */
1596 	vr_reset(sc);
1597 
1598 	/*
1599 	 * Get station address. The way the Rhine chips work,
1600 	 * you're not allowed to directly access the EEPROM once
1601 	 * they've been programmed a special way. Consequently,
1602 	 * we need to read the node address from the PAR registers.
1603 	 *
1604 	 * XXXSCW: On the Rhine III, setting VR_EECSR_LOAD forces a reload
1605 	 *         of the *whole* EEPROM, not just the MAC address. This is
1606 	 *         pretty pointless since the chip does this automatically
1607 	 *         at powerup/reset.
1608 	 *         I suspect the same thing applies to the other Rhine
1609 	 *         variants, but in the absence of a data sheet for those
1610 	 *         (and the lack of anyone else noticing the problems this
1611 	 *         causes) I'm going to retain the old behaviour for the
1612 	 *         other parts.
1613 	 *         In some cases, the chip really does startup without having
1614 	 *         read the EEPROM (kern/34812). To handle this case, we force
1615 	 *         a reload if we see an all-zeroes MAC address.
1616 	 */
1617 	for (mac = 0, i = 0; i < ETHER_ADDR_LEN; i++)
1618 		mac |= (eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i));
1619 
1620 	if (mac == 0 || (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT6105 &&
1621 	    PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT6102)) {
1622 		VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1623 		DELAY(200);
1624 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1625 			eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1626 	}
1627 
1628 	/*
1629 	 * A Rhine chip was detected. Inform the world.
1630 	 */
1631 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
1632 
1633 	memcpy(sc->vr_enaddr, eaddr, ETHER_ADDR_LEN);
1634 
1635 	sc->vr_dmat = pa->pa_dmat;
1636 
1637 	/*
1638 	 * Allocate the control data structures, and create and load
1639 	 * the DMA map for it.
1640 	 */
1641 	if ((error = bus_dmamem_alloc(sc->vr_dmat,
1642 	    sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
1643 	    0)) != 0) {
1644 		aprint_error_dev(self,
1645 		    "unable to allocate control data, error = %d\n", error);
1646 		goto fail_0;
1647 	}
1648 
1649 	if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
1650 	    sizeof(struct vr_control_data), (void **)&sc->vr_control_data,
1651 	    BUS_DMA_COHERENT)) != 0) {
1652 		aprint_error_dev(self,
1653 		    "unable to map control data, error = %d\n", error);
1654 		goto fail_1;
1655 	}
1656 
1657 	if ((error = bus_dmamap_create(sc->vr_dmat,
1658 	    sizeof(struct vr_control_data), 1,
1659 	    sizeof(struct vr_control_data), 0, 0,
1660 	    &sc->vr_cddmamap)) != 0) {
1661 		aprint_error_dev(self,
1662 		    "unable to create control data DMA map, error = %d\n",
1663 		    error);
1664 		goto fail_2;
1665 	}
1666 
1667 	if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
1668 	    sc->vr_control_data, sizeof(struct vr_control_data), NULL,
1669 	    0)) != 0) {
1670 		aprint_error_dev(self,
1671 		    "unable to load control data DMA map, error = %d\n",
1672 		    error);
1673 		goto fail_3;
1674 	}
1675 
1676 	/*
1677 	 * Create the transmit buffer DMA maps.
1678 	 */
1679 	for (i = 0; i < VR_NTXDESC; i++) {
1680 		if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
1681 		    1, MCLBYTES, 0, 0,
1682 		    &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
1683 			aprint_error_dev(self,
1684 			    "unable to create tx DMA map %d, error = %d\n", i,
1685 			    error);
1686 			goto fail_4;
1687 		}
1688 	}
1689 
1690 	/*
1691 	 * Create the receive buffer DMA maps.
1692 	 */
1693 	for (i = 0; i < VR_NRXDESC; i++) {
1694 		if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
1695 		    MCLBYTES, 0, 0,
1696 		    &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
1697 			aprint_error_dev(self,
1698 			    "unable to create rx DMA map %d, error = %d\n", i,
1699 			    error);
1700 			goto fail_5;
1701 		}
1702 		VR_DSRX(sc, i)->ds_mbuf = NULL;
1703 	}
1704 
1705 	ifp = &sc->vr_ec.ec_if;
1706 	ifp->if_softc = sc;
1707 	ifp->if_mtu = ETHERMTU;
1708 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1709 	ifp->if_ioctl = vr_ioctl;
1710 	ifp->if_start = vr_start;
1711 	ifp->if_watchdog = vr_watchdog;
1712 	ifp->if_init = vr_init;
1713 	ifp->if_stop = vr_stop;
1714 	IFQ_SET_READY(&ifp->if_snd);
1715 
1716 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
1717 
1718 	/*
1719 	 * Initialize MII/media info.
1720 	 */
1721 	mii->mii_ifp = ifp;
1722 	mii->mii_readreg = vr_mii_readreg;
1723 	mii->mii_writereg = vr_mii_writereg;
1724 	mii->mii_statchg = vr_mii_statchg;
1725 
1726 	sc->vr_ec.ec_mii = mii;
1727 	ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
1728 		ether_mediastatus);
1729 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY,
1730 	    MII_OFFSET_ANY, MIIF_FORCEANEG);
1731 	if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1732 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1733 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1734 	} else
1735 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1736 
1737 	sc->vr_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1738 
1739 	/*
1740 	 * Call MI attach routines.
1741 	 */
1742 	if_attach(ifp);
1743 	if_deferred_start_init(ifp, NULL);
1744 	ether_ifattach(ifp, sc->vr_enaddr);
1745 
1746 	rnd_attach_source(&sc->rnd_source, device_xname(self),
1747 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
1748 
1749 	if (pmf_device_register1(self, NULL, vr_resume, vr_shutdown))
1750 		pmf_class_network_register(self, ifp);
1751 	else
1752 		aprint_error_dev(self, "couldn't establish power handler\n");
1753 
1754 	return;
1755 
1756  fail_5:
1757 	for (i = 0; i < VR_NRXDESC; i++) {
1758 		if (sc->vr_rxsoft[i].ds_dmamap != NULL)
1759 			bus_dmamap_destroy(sc->vr_dmat,
1760 			    sc->vr_rxsoft[i].ds_dmamap);
1761 	}
1762  fail_4:
1763 	for (i = 0; i < VR_NTXDESC; i++) {
1764 		if (sc->vr_txsoft[i].ds_dmamap != NULL)
1765 			bus_dmamap_destroy(sc->vr_dmat,
1766 			    sc->vr_txsoft[i].ds_dmamap);
1767 	}
1768 	bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
1769  fail_3:
1770 	bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
1771  fail_2:
1772 	bus_dmamem_unmap(sc->vr_dmat, (void *)sc->vr_control_data,
1773 	    sizeof(struct vr_control_data));
1774  fail_1:
1775 	bus_dmamem_free(sc->vr_dmat, &seg, rseg);
1776  fail_0:
1777 	return;
1778 }
1779 
1780 static int
1781 vr_restore_state(pci_chipset_tag_t pc, pcitag_t tag, device_t self,
1782     pcireg_t state)
1783 {
1784 	struct vr_softc *sc = device_private(self);
1785 	int error;
1786 
1787 	if (state == PCI_PMCSR_STATE_D0)
1788 		return 0;
1789 	if ((error = pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0)))
1790 		return error;
1791 
1792 	/* Restore PCI config data. */
1793 	PCI_CONF_WRITE(VR_PCI_LOIO, sc->vr_save_iobase);
1794 	PCI_CONF_WRITE(VR_PCI_LOMEM, sc->vr_save_membase);
1795 	PCI_CONF_WRITE(PCI_INTERRUPT_REG, sc->vr_save_irq);
1796 	return 0;
1797 }
1798 
1799 static bool
1800 vr_resume(device_t self, const pmf_qual_t *qual)
1801 {
1802 	struct vr_softc *sc = device_private(self);
1803 
1804 	if (PCI_PRODUCT(sc->vr_id) != PCI_PRODUCT_VIATECH_VT3043)
1805 		VR_CLRBIT(sc, VR_STICKHW, (VR_STICKHW_DS0 | VR_STICKHW_DS1));
1806 
1807 	return true;
1808 }
1809