xref: /freebsd/sys/dev/vge/if_vge.c (revision 098ca2bda93c701c5331d4e6aace072495b4caaa)
1098ca2bdSWarner Losh /*-
2a07bd003SBill Paul  * Copyright (c) 2004
3a07bd003SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4a07bd003SBill Paul  *
5a07bd003SBill Paul  * Redistribution and use in source and binary forms, with or without
6a07bd003SBill Paul  * modification, are permitted provided that the following conditions
7a07bd003SBill Paul  * are met:
8a07bd003SBill Paul  * 1. Redistributions of source code must retain the above copyright
9a07bd003SBill Paul  *    notice, this list of conditions and the following disclaimer.
10a07bd003SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11a07bd003SBill Paul  *    notice, this list of conditions and the following disclaimer in the
12a07bd003SBill Paul  *    documentation and/or other materials provided with the distribution.
13a07bd003SBill Paul  * 3. All advertising materials mentioning features or use of this software
14a07bd003SBill Paul  *    must display the following acknowledgement:
15a07bd003SBill Paul  *	This product includes software developed by Bill Paul.
16a07bd003SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17a07bd003SBill Paul  *    may be used to endorse or promote products derived from this software
18a07bd003SBill Paul  *    without specific prior written permission.
19a07bd003SBill Paul  *
20a07bd003SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21a07bd003SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a07bd003SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a07bd003SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24a07bd003SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25a07bd003SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26a07bd003SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27a07bd003SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28a07bd003SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a07bd003SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30a07bd003SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
31a07bd003SBill Paul  */
32a07bd003SBill Paul 
33a07bd003SBill Paul #include <sys/cdefs.h>
34a07bd003SBill Paul __FBSDID("$FreeBSD$");
35a07bd003SBill Paul 
36a07bd003SBill Paul /*
37a07bd003SBill Paul  * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
38a07bd003SBill Paul  *
39a07bd003SBill Paul  * Written by Bill Paul <wpaul@windriver.com>
40a07bd003SBill Paul  * Senior Networking Software Engineer
41a07bd003SBill Paul  * Wind River Systems
42a07bd003SBill Paul  */
43a07bd003SBill Paul 
44a07bd003SBill Paul /*
45a07bd003SBill Paul  * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that
46a07bd003SBill Paul  * combines a tri-speed ethernet MAC and PHY, with the following
47a07bd003SBill Paul  * features:
48a07bd003SBill Paul  *
49a07bd003SBill Paul  *	o Jumbo frame support up to 16K
50a07bd003SBill Paul  *	o Transmit and receive flow control
51a07bd003SBill Paul  *	o IPv4 checksum offload
52a07bd003SBill Paul  *	o VLAN tag insertion and stripping
53a07bd003SBill Paul  *	o TCP large send
54a07bd003SBill Paul  *	o 64-bit multicast hash table filter
55a07bd003SBill Paul  *	o 64 entry CAM filter
56a07bd003SBill Paul  *	o 16K RX FIFO and 48K TX FIFO memory
57a07bd003SBill Paul  *	o Interrupt moderation
58a07bd003SBill Paul  *
59a07bd003SBill Paul  * The VT6122 supports up to four transmit DMA queues. The descriptors
60a07bd003SBill Paul  * in the transmit ring can address up to 7 data fragments; frames which
61a07bd003SBill Paul  * span more than 7 data buffers must be coalesced, but in general the
62a07bd003SBill Paul  * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
63a07bd003SBill Paul  * long. The receive descriptors address only a single buffer.
64a07bd003SBill Paul  *
65a07bd003SBill Paul  * There are two peculiar design issues with the VT6122. One is that
66a07bd003SBill Paul  * receive data buffers must be aligned on a 32-bit boundary. This is
67a07bd003SBill Paul  * not a problem where the VT6122 is used as a LOM device in x86-based
68a07bd003SBill Paul  * systems, but on architectures that generate unaligned access traps, we
69a07bd003SBill Paul  * have to do some copying.
70a07bd003SBill Paul  *
71a07bd003SBill Paul  * The other issue has to do with the way 64-bit addresses are handled.
72a07bd003SBill Paul  * The DMA descriptors only allow you to specify 48 bits of addressing
73a07bd003SBill Paul  * information. The remaining 16 bits are specified using one of the
74a07bd003SBill Paul  * I/O registers. If you only have a 32-bit system, then this isn't
75a07bd003SBill Paul  * an issue, but if you have a 64-bit system and more than 4GB of
76a07bd003SBill Paul  * memory, you must have to make sure your network data buffers reside
77a07bd003SBill Paul  * in the same 48-bit 'segment.'
78a07bd003SBill Paul  *
79a07bd003SBill Paul  * Special thanks to Ryan Fu at VIA Networking for providing documentation
80a07bd003SBill Paul  * and sample NICs for testing.
81a07bd003SBill Paul  */
82a07bd003SBill Paul 
83a07bd003SBill Paul #include <sys/param.h>
84a07bd003SBill Paul #include <sys/endian.h>
85a07bd003SBill Paul #include <sys/systm.h>
86a07bd003SBill Paul #include <sys/sockio.h>
87a07bd003SBill Paul #include <sys/mbuf.h>
88a07bd003SBill Paul #include <sys/malloc.h>
89a07bd003SBill Paul #include <sys/module.h>
90a07bd003SBill Paul #include <sys/kernel.h>
91a07bd003SBill Paul #include <sys/socket.h>
92a07bd003SBill Paul #include <sys/taskqueue.h>
93a07bd003SBill Paul 
94a07bd003SBill Paul #include <net/if.h>
95a07bd003SBill Paul #include <net/if_arp.h>
96a07bd003SBill Paul #include <net/ethernet.h>
97a07bd003SBill Paul #include <net/if_dl.h>
98a07bd003SBill Paul #include <net/if_media.h>
99a07bd003SBill Paul #include <net/if_vlan_var.h>
100a07bd003SBill Paul #include <net/route.h>
101a07bd003SBill Paul 
102a07bd003SBill Paul #include <net/bpf.h>
103a07bd003SBill Paul 
104a07bd003SBill Paul #include <machine/bus_pio.h>
105a07bd003SBill Paul #include <machine/bus_memio.h>
106a07bd003SBill Paul #include <machine/bus.h>
107a07bd003SBill Paul #include <machine/resource.h>
108a07bd003SBill Paul #include <sys/bus.h>
109a07bd003SBill Paul #include <sys/rman.h>
110a07bd003SBill Paul 
111a07bd003SBill Paul #include <dev/mii/mii.h>
112a07bd003SBill Paul #include <dev/mii/miivar.h>
113a07bd003SBill Paul 
114a07bd003SBill Paul #include <dev/pci/pcireg.h>
115a07bd003SBill Paul #include <dev/pci/pcivar.h>
116a07bd003SBill Paul 
117a07bd003SBill Paul MODULE_DEPEND(vge, pci, 1, 1, 1);
118a07bd003SBill Paul MODULE_DEPEND(vge, ether, 1, 1, 1);
119a07bd003SBill Paul MODULE_DEPEND(vge, miibus, 1, 1, 1);
120a07bd003SBill Paul 
121a07bd003SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
122a07bd003SBill Paul #include "miibus_if.h"
123a07bd003SBill Paul 
124a07bd003SBill Paul #include <dev/vge/if_vgereg.h>
125a07bd003SBill Paul #include <dev/vge/if_vgevar.h>
126a07bd003SBill Paul 
127a07bd003SBill Paul #define VGE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
128a07bd003SBill Paul 
129a07bd003SBill Paul /*
130a07bd003SBill Paul  * Various supported device vendors/types and their names.
131a07bd003SBill Paul  */
132a07bd003SBill Paul static struct vge_type vge_devs[] = {
133a07bd003SBill Paul 	{ VIA_VENDORID, VIA_DEVICEID_61XX,
134a07bd003SBill Paul 		"VIA Networking Gigabit Ethernet" },
135a07bd003SBill Paul 	{ 0, 0, NULL }
136a07bd003SBill Paul };
137a07bd003SBill Paul 
138a07bd003SBill Paul static int vge_probe		(device_t);
139a07bd003SBill Paul static int vge_attach		(device_t);
140a07bd003SBill Paul static int vge_detach		(device_t);
141a07bd003SBill Paul 
142a07bd003SBill Paul static int vge_encap		(struct vge_softc *, struct mbuf *, int);
143a07bd003SBill Paul 
144a07bd003SBill Paul static void vge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
145a07bd003SBill Paul static void vge_dma_map_rx_desc	(void *, bus_dma_segment_t *, int,
146a07bd003SBill Paul 				    bus_size_t, int);
147a07bd003SBill Paul static void vge_dma_map_tx_desc	(void *, bus_dma_segment_t *, int,
148a07bd003SBill Paul 				    bus_size_t, int);
149a07bd003SBill Paul static int vge_allocmem		(device_t, struct vge_softc *);
150a07bd003SBill Paul static int vge_newbuf		(struct vge_softc *, int, struct mbuf *);
151a07bd003SBill Paul static int vge_rx_list_init	(struct vge_softc *);
152a07bd003SBill Paul static int vge_tx_list_init	(struct vge_softc *);
153a07bd003SBill Paul #ifdef VGE_FIXUP_RX
154a07bd003SBill Paul static __inline void vge_fixup_rx
155a07bd003SBill Paul 				(struct mbuf *);
156a07bd003SBill Paul #endif
157a07bd003SBill Paul static void vge_rxeof		(struct vge_softc *);
158a07bd003SBill Paul static void vge_txeof		(struct vge_softc *);
159a07bd003SBill Paul static void vge_intr		(void *);
160a07bd003SBill Paul static void vge_tick		(void *);
161a07bd003SBill Paul static void vge_tx_task		(void *, int);
162a07bd003SBill Paul static void vge_start		(struct ifnet *);
163a07bd003SBill Paul static int vge_ioctl		(struct ifnet *, u_long, caddr_t);
164a07bd003SBill Paul static void vge_init		(void *);
165a07bd003SBill Paul static void vge_stop		(struct vge_softc *);
166a07bd003SBill Paul static void vge_watchdog	(struct ifnet *);
167a07bd003SBill Paul static int vge_suspend		(device_t);
168a07bd003SBill Paul static int vge_resume		(device_t);
169a07bd003SBill Paul static void vge_shutdown	(device_t);
170a07bd003SBill Paul static int vge_ifmedia_upd	(struct ifnet *);
171a07bd003SBill Paul static void vge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
172a07bd003SBill Paul 
173a07bd003SBill Paul static void vge_eeprom_getword	(struct vge_softc *, int, u_int16_t *);
174a07bd003SBill Paul static void vge_read_eeprom	(struct vge_softc *, caddr_t, int, int, int);
175a07bd003SBill Paul 
176a07bd003SBill Paul static void vge_miipoll_start	(struct vge_softc *);
177a07bd003SBill Paul static void vge_miipoll_stop	(struct vge_softc *);
178a07bd003SBill Paul static int vge_miibus_readreg	(device_t, int, int);
179a07bd003SBill Paul static int vge_miibus_writereg	(device_t, int, int, int);
180a07bd003SBill Paul static void vge_miibus_statchg	(device_t);
181a07bd003SBill Paul 
182a07bd003SBill Paul static void vge_cam_clear	(struct vge_softc *);
183a07bd003SBill Paul static int vge_cam_set		(struct vge_softc *, uint8_t *);
184a07bd003SBill Paul #if __FreeBSD_version < 502113
185a07bd003SBill Paul static uint32_t vge_mchash	(uint8_t *);
186a07bd003SBill Paul #endif
187a07bd003SBill Paul static void vge_setmulti	(struct vge_softc *);
188a07bd003SBill Paul static void vge_reset		(struct vge_softc *);
189a07bd003SBill Paul 
190a07bd003SBill Paul #define VGE_PCI_LOIO             0x10
191a07bd003SBill Paul #define VGE_PCI_LOMEM            0x14
192a07bd003SBill Paul 
193a07bd003SBill Paul static device_method_t vge_methods[] = {
194a07bd003SBill Paul 	/* Device interface */
195a07bd003SBill Paul 	DEVMETHOD(device_probe,		vge_probe),
196a07bd003SBill Paul 	DEVMETHOD(device_attach,	vge_attach),
197a07bd003SBill Paul 	DEVMETHOD(device_detach,	vge_detach),
198a07bd003SBill Paul 	DEVMETHOD(device_suspend,	vge_suspend),
199a07bd003SBill Paul 	DEVMETHOD(device_resume,	vge_resume),
200a07bd003SBill Paul 	DEVMETHOD(device_shutdown,	vge_shutdown),
201a07bd003SBill Paul 
202a07bd003SBill Paul 	/* bus interface */
203a07bd003SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
204a07bd003SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
205a07bd003SBill Paul 
206a07bd003SBill Paul 	/* MII interface */
207a07bd003SBill Paul 	DEVMETHOD(miibus_readreg,	vge_miibus_readreg),
208a07bd003SBill Paul 	DEVMETHOD(miibus_writereg,	vge_miibus_writereg),
209a07bd003SBill Paul 	DEVMETHOD(miibus_statchg,	vge_miibus_statchg),
210a07bd003SBill Paul 
211a07bd003SBill Paul 	{ 0, 0 }
212a07bd003SBill Paul };
213a07bd003SBill Paul 
214a07bd003SBill Paul static driver_t vge_driver = {
215a07bd003SBill Paul 	"vge",
216a07bd003SBill Paul 	vge_methods,
217a07bd003SBill Paul 	sizeof(struct vge_softc)
218a07bd003SBill Paul };
219a07bd003SBill Paul 
220a07bd003SBill Paul static devclass_t vge_devclass;
221a07bd003SBill Paul 
222a07bd003SBill Paul DRIVER_MODULE(vge, pci, vge_driver, vge_devclass, 0, 0);
223a07bd003SBill Paul DRIVER_MODULE(vge, cardbus, vge_driver, vge_devclass, 0, 0);
224a07bd003SBill Paul DRIVER_MODULE(miibus, vge, miibus_driver, miibus_devclass, 0, 0);
225a07bd003SBill Paul 
226a07bd003SBill Paul /*
227a07bd003SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
228a07bd003SBill Paul  */
229a07bd003SBill Paul static void
230a07bd003SBill Paul vge_eeprom_getword(sc, addr, dest)
231a07bd003SBill Paul 	struct vge_softc	*sc;
232a07bd003SBill Paul 	int			addr;
233a07bd003SBill Paul 	u_int16_t		*dest;
234a07bd003SBill Paul {
235a07bd003SBill Paul 	register int		i;
236a07bd003SBill Paul 	u_int16_t		word = 0;
237a07bd003SBill Paul 
238a07bd003SBill Paul 	/*
239a07bd003SBill Paul 	 * Enter EEPROM embedded programming mode. In order to
240a07bd003SBill Paul 	 * access the EEPROM at all, we first have to set the
241a07bd003SBill Paul 	 * EELOAD bit in the CHIPCFG2 register.
242a07bd003SBill Paul 	 */
243a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
244a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
245a07bd003SBill Paul 
246a07bd003SBill Paul 	/* Select the address of the word we want to read */
247a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_EEADDR, addr);
248a07bd003SBill Paul 
249a07bd003SBill Paul 	/* Issue read command */
250a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
251a07bd003SBill Paul 
252a07bd003SBill Paul 	/* Wait for the done bit to be set. */
253a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
254a07bd003SBill Paul 		if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
255a07bd003SBill Paul 			break;
256a07bd003SBill Paul 	}
257a07bd003SBill Paul 
258a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
259a07bd003SBill Paul 		device_printf(sc->vge_dev, "EEPROM read timed out\n");
260a07bd003SBill Paul 		*dest = 0;
261a07bd003SBill Paul 		return;
262a07bd003SBill Paul 	}
263a07bd003SBill Paul 
264a07bd003SBill Paul 	/* Read the result */
265a07bd003SBill Paul 	word = CSR_READ_2(sc, VGE_EERDDAT);
266a07bd003SBill Paul 
267a07bd003SBill Paul 	/* Turn off EEPROM access mode. */
268a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
269a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
270a07bd003SBill Paul 
271a07bd003SBill Paul 	*dest = word;
272a07bd003SBill Paul 
273a07bd003SBill Paul 	return;
274a07bd003SBill Paul }
275a07bd003SBill Paul 
276a07bd003SBill Paul /*
277a07bd003SBill Paul  * Read a sequence of words from the EEPROM.
278a07bd003SBill Paul  */
279a07bd003SBill Paul static void
280a07bd003SBill Paul vge_read_eeprom(sc, dest, off, cnt, swap)
281a07bd003SBill Paul 	struct vge_softc	*sc;
282a07bd003SBill Paul 	caddr_t			dest;
283a07bd003SBill Paul 	int			off;
284a07bd003SBill Paul 	int			cnt;
285a07bd003SBill Paul 	int			swap;
286a07bd003SBill Paul {
287a07bd003SBill Paul 	int			i;
288a07bd003SBill Paul 	u_int16_t		word = 0, *ptr;
289a07bd003SBill Paul 
290a07bd003SBill Paul 	for (i = 0; i < cnt; i++) {
291a07bd003SBill Paul 		vge_eeprom_getword(sc, off + i, &word);
292a07bd003SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
293a07bd003SBill Paul 		if (swap)
294a07bd003SBill Paul 			*ptr = ntohs(word);
295a07bd003SBill Paul 		else
296a07bd003SBill Paul 			*ptr = word;
297a07bd003SBill Paul 	}
298a07bd003SBill Paul }
299a07bd003SBill Paul 
300a07bd003SBill Paul static void
301a07bd003SBill Paul vge_miipoll_stop(sc)
302a07bd003SBill Paul 	struct vge_softc	*sc;
303a07bd003SBill Paul {
304a07bd003SBill Paul 	int			i;
305a07bd003SBill Paul 
306a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIICMD, 0);
307a07bd003SBill Paul 
308a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
309a07bd003SBill Paul 		DELAY(1);
310a07bd003SBill Paul 		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
311a07bd003SBill Paul 			break;
312a07bd003SBill Paul 	}
313a07bd003SBill Paul 
314a07bd003SBill Paul 	if (i == VGE_TIMEOUT)
315a07bd003SBill Paul 		device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
316a07bd003SBill Paul 
317a07bd003SBill Paul 	return;
318a07bd003SBill Paul }
319a07bd003SBill Paul 
320a07bd003SBill Paul static void
321a07bd003SBill Paul vge_miipoll_start(sc)
322a07bd003SBill Paul 	struct vge_softc	*sc;
323a07bd003SBill Paul {
324a07bd003SBill Paul 	int			i;
325a07bd003SBill Paul 
326a07bd003SBill Paul 	/* First, make sure we're idle. */
327a07bd003SBill Paul 
328a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIICMD, 0);
329a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
330a07bd003SBill Paul 
331a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
332a07bd003SBill Paul 		DELAY(1);
333a07bd003SBill Paul 		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
334a07bd003SBill Paul 			break;
335a07bd003SBill Paul 	}
336a07bd003SBill Paul 
337a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
338a07bd003SBill Paul 		device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
339a07bd003SBill Paul 		return;
340a07bd003SBill Paul 	}
341a07bd003SBill Paul 
342a07bd003SBill Paul 	/* Now enable auto poll mode. */
343a07bd003SBill Paul 
344a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
345a07bd003SBill Paul 
346a07bd003SBill Paul 	/* And make sure it started. */
347a07bd003SBill Paul 
348a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
349a07bd003SBill Paul 		DELAY(1);
350a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
351a07bd003SBill Paul 			break;
352a07bd003SBill Paul 	}
353a07bd003SBill Paul 
354a07bd003SBill Paul 	if (i == VGE_TIMEOUT)
355a07bd003SBill Paul 		device_printf(sc->vge_dev, "failed to start MII autopoll\n");
356a07bd003SBill Paul 
357a07bd003SBill Paul 	return;
358a07bd003SBill Paul }
359a07bd003SBill Paul 
360a07bd003SBill Paul static int
361a07bd003SBill Paul vge_miibus_readreg(dev, phy, reg)
362a07bd003SBill Paul 	device_t		dev;
363a07bd003SBill Paul 	int			phy, reg;
364a07bd003SBill Paul {
365a07bd003SBill Paul 	struct vge_softc	*sc;
366a07bd003SBill Paul 	int			i;
367a07bd003SBill Paul 	u_int16_t		rval = 0;
368a07bd003SBill Paul 
369a07bd003SBill Paul 	sc = device_get_softc(dev);
370a07bd003SBill Paul 
371a07bd003SBill Paul 	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
372a07bd003SBill Paul 		return(0);
373a07bd003SBill Paul 
374a07bd003SBill Paul 	VGE_LOCK(sc);
375a07bd003SBill Paul 	vge_miipoll_stop(sc);
376a07bd003SBill Paul 
377a07bd003SBill Paul 	/* Specify the register we want to read. */
378a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
379a07bd003SBill Paul 
380a07bd003SBill Paul 	/* Issue read command. */
381a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
382a07bd003SBill Paul 
383a07bd003SBill Paul 	/* Wait for the read command bit to self-clear. */
384a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
385a07bd003SBill Paul 		DELAY(1);
386a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
387a07bd003SBill Paul 			break;
388a07bd003SBill Paul 	}
389a07bd003SBill Paul 
390a07bd003SBill Paul 	if (i == VGE_TIMEOUT)
391a07bd003SBill Paul 		device_printf(sc->vge_dev, "MII read timed out\n");
392a07bd003SBill Paul 	else
393a07bd003SBill Paul 		rval = CSR_READ_2(sc, VGE_MIIDATA);
394a07bd003SBill Paul 
395a07bd003SBill Paul 	vge_miipoll_start(sc);
396a07bd003SBill Paul 	VGE_UNLOCK(sc);
397a07bd003SBill Paul 
398a07bd003SBill Paul 	return (rval);
399a07bd003SBill Paul }
400a07bd003SBill Paul 
401a07bd003SBill Paul static int
402a07bd003SBill Paul vge_miibus_writereg(dev, phy, reg, data)
403a07bd003SBill Paul 	device_t		dev;
404a07bd003SBill Paul 	int			phy, reg, data;
405a07bd003SBill Paul {
406a07bd003SBill Paul 	struct vge_softc	*sc;
407a07bd003SBill Paul 	int			i, rval = 0;
408a07bd003SBill Paul 
409a07bd003SBill Paul 	sc = device_get_softc(dev);
410a07bd003SBill Paul 
411a07bd003SBill Paul 	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
412a07bd003SBill Paul 		return(0);
413a07bd003SBill Paul 
414a07bd003SBill Paul 	VGE_LOCK(sc);
415a07bd003SBill Paul 	vge_miipoll_stop(sc);
416a07bd003SBill Paul 
417a07bd003SBill Paul 	/* Specify the register we want to write. */
418a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
419a07bd003SBill Paul 
420a07bd003SBill Paul 	/* Specify the data we want to write. */
421a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_MIIDATA, data);
422a07bd003SBill Paul 
423a07bd003SBill Paul 	/* Issue write command. */
424a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
425a07bd003SBill Paul 
426a07bd003SBill Paul 	/* Wait for the write command bit to self-clear. */
427a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
428a07bd003SBill Paul 		DELAY(1);
429a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
430a07bd003SBill Paul 			break;
431a07bd003SBill Paul 	}
432a07bd003SBill Paul 
433a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
434a07bd003SBill Paul 		device_printf(sc->vge_dev, "MII write timed out\n");
435a07bd003SBill Paul 		rval = EIO;
436a07bd003SBill Paul 	}
437a07bd003SBill Paul 
438a07bd003SBill Paul 	vge_miipoll_start(sc);
439a07bd003SBill Paul 	VGE_UNLOCK(sc);
440a07bd003SBill Paul 
441a07bd003SBill Paul 	return (rval);
442a07bd003SBill Paul }
443a07bd003SBill Paul 
444a07bd003SBill Paul static void
445a07bd003SBill Paul vge_cam_clear(sc)
446a07bd003SBill Paul 	struct vge_softc	*sc;
447a07bd003SBill Paul {
448a07bd003SBill Paul 	int			i;
449a07bd003SBill Paul 
450a07bd003SBill Paul 	/*
451a07bd003SBill Paul 	 * Turn off all the mask bits. This tells the chip
452a07bd003SBill Paul 	 * that none of the entries in the CAM filter are valid.
453a07bd003SBill Paul 	 * desired entries will be enabled as we fill the filter in.
454a07bd003SBill Paul 	 */
455a07bd003SBill Paul 
456a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
457a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
458a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
459a07bd003SBill Paul 	for (i = 0; i < 8; i++)
460a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
461a07bd003SBill Paul 
462a07bd003SBill Paul 	/* Clear the VLAN filter too. */
463a07bd003SBill Paul 
464a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
465a07bd003SBill Paul 	for (i = 0; i < 8; i++)
466a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
467a07bd003SBill Paul 
468a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
469a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
470a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
471a07bd003SBill Paul 
472a07bd003SBill Paul 	sc->vge_camidx = 0;
473a07bd003SBill Paul 
474a07bd003SBill Paul 	return;
475a07bd003SBill Paul }
476a07bd003SBill Paul 
477a07bd003SBill Paul static int
478a07bd003SBill Paul vge_cam_set(sc, addr)
479a07bd003SBill Paul 	struct vge_softc	*sc;
480a07bd003SBill Paul 	uint8_t			*addr;
481a07bd003SBill Paul {
482a07bd003SBill Paul 	int			i, error = 0;
483a07bd003SBill Paul 
484a07bd003SBill Paul 	if (sc->vge_camidx == VGE_CAM_MAXADDRS)
485a07bd003SBill Paul 		return(ENOSPC);
486a07bd003SBill Paul 
487a07bd003SBill Paul 	/* Select the CAM data page. */
488a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
489a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
490a07bd003SBill Paul 
491a07bd003SBill Paul 	/* Set the filter entry we want to update and enable writing. */
492a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx);
493a07bd003SBill Paul 
494a07bd003SBill Paul 	/* Write the address to the CAM registers */
495a07bd003SBill Paul 	for (i = 0; i < ETHER_ADDR_LEN; i++)
496a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
497a07bd003SBill Paul 
498a07bd003SBill Paul 	/* Issue a write command. */
499a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
500a07bd003SBill Paul 
501a07bd003SBill Paul 	/* Wake for it to clear. */
502a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
503a07bd003SBill Paul 		DELAY(1);
504a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
505a07bd003SBill Paul 			break;
506a07bd003SBill Paul 	}
507a07bd003SBill Paul 
508a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
509a07bd003SBill Paul 		device_printf(sc->vge_dev, "setting CAM filter failed\n");
510a07bd003SBill Paul 		error = EIO;
511a07bd003SBill Paul 		goto fail;
512a07bd003SBill Paul 	}
513a07bd003SBill Paul 
514a07bd003SBill Paul 	/* Select the CAM mask page. */
515a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
516a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
517a07bd003SBill Paul 
518a07bd003SBill Paul 	/* Set the mask bit that enables this filter. */
519a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8),
520a07bd003SBill Paul 	    1<<(sc->vge_camidx & 7));
521a07bd003SBill Paul 
522a07bd003SBill Paul 	sc->vge_camidx++;
523a07bd003SBill Paul 
524a07bd003SBill Paul fail:
525a07bd003SBill Paul 	/* Turn off access to CAM. */
526a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
527a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
528a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
529a07bd003SBill Paul 
530a07bd003SBill Paul 	return (error);
531a07bd003SBill Paul }
532a07bd003SBill Paul 
533a07bd003SBill Paul #if __FreeBSD_version < 502113
534a07bd003SBill Paul static uint32_t
535a07bd003SBill Paul vge_mchash(addr)
536a07bd003SBill Paul         uint8_t			*addr;
537a07bd003SBill Paul {
538a07bd003SBill Paul 	uint32_t		crc, carry;
539a07bd003SBill Paul 	int			idx, bit;
540a07bd003SBill Paul 	uint8_t			data;
541a07bd003SBill Paul 
542a07bd003SBill Paul 	/* Compute CRC for the address value. */
543a07bd003SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
544a07bd003SBill Paul 
545a07bd003SBill Paul 	for (idx = 0; idx < 6; idx++) {
546a07bd003SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
547a07bd003SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
548a07bd003SBill Paul 			crc <<= 1;
549a07bd003SBill Paul 			if (carry)
550a07bd003SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
551a07bd003SBill Paul 		}
552a07bd003SBill Paul 	}
553a07bd003SBill Paul 
554a07bd003SBill Paul 	return(crc);
555a07bd003SBill Paul }
556a07bd003SBill Paul #endif
557a07bd003SBill Paul 
558a07bd003SBill Paul /*
559a07bd003SBill Paul  * Program the multicast filter. We use the 64-entry CAM filter
560a07bd003SBill Paul  * for perfect filtering. If there's more than 64 multicast addresses,
561a07bd003SBill Paul  * we use the hash filter insted.
562a07bd003SBill Paul  */
563a07bd003SBill Paul static void
564a07bd003SBill Paul vge_setmulti(sc)
565a07bd003SBill Paul 	struct vge_softc	*sc;
566a07bd003SBill Paul {
567a07bd003SBill Paul 	struct ifnet		*ifp;
568a07bd003SBill Paul 	int			error = 0/*, h = 0*/;
569a07bd003SBill Paul 	struct ifmultiaddr	*ifma;
570a07bd003SBill Paul 	u_int32_t		h, hashes[2] = { 0, 0 };
571a07bd003SBill Paul 
572a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
573a07bd003SBill Paul 
574a07bd003SBill Paul 	/* First, zot all the multicast entries. */
575a07bd003SBill Paul 	vge_cam_clear(sc);
576a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_MAR0, 0);
577a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_MAR1, 0);
578a07bd003SBill Paul 
579a07bd003SBill Paul 	/*
580a07bd003SBill Paul 	 * If the user wants allmulti or promisc mode, enable reception
581a07bd003SBill Paul 	 * of all multicast frames.
582a07bd003SBill Paul 	 */
583a07bd003SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
584a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF);
585a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF);
586a07bd003SBill Paul 		return;
587a07bd003SBill Paul 	}
588a07bd003SBill Paul 
589a07bd003SBill Paul 	/* Now program new ones */
590a07bd003SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
591a07bd003SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
592a07bd003SBill Paul 			continue;
593a07bd003SBill Paul 		error = vge_cam_set(sc,
594a07bd003SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
595a07bd003SBill Paul 		if (error)
596a07bd003SBill Paul 			break;
597a07bd003SBill Paul 	}
598a07bd003SBill Paul 
599a07bd003SBill Paul 	/* If there were too many addresses, use the hash filter. */
600a07bd003SBill Paul 	if (error) {
601a07bd003SBill Paul 		vge_cam_clear(sc);
602a07bd003SBill Paul 
603a07bd003SBill Paul 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
604a07bd003SBill Paul 			if (ifma->ifma_addr->sa_family != AF_LINK)
605a07bd003SBill Paul 				continue;
606a07bd003SBill Paul #if __FreeBSD_version < 502113
607a07bd003SBill Paul 			h = vge_mchash(LLADDR((struct sockaddr_dl *)
608a07bd003SBill Paul 			    ifma->ifma_addr)) >> 26;
609a07bd003SBill Paul #else
610a07bd003SBill Paul 			h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
611a07bd003SBill Paul 			    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
612a07bd003SBill Paul #endif
613a07bd003SBill Paul 			if (h < 32)
614a07bd003SBill Paul 				hashes[0] |= (1 << h);
615a07bd003SBill Paul 			else
616a07bd003SBill Paul 				hashes[1] |= (1 << (h - 32));
617a07bd003SBill Paul 		}
618a07bd003SBill Paul 
619a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
620a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
621a07bd003SBill Paul 	}
622a07bd003SBill Paul 
623a07bd003SBill Paul 	return;
624a07bd003SBill Paul }
625a07bd003SBill Paul 
626a07bd003SBill Paul static void
627a07bd003SBill Paul vge_reset(sc)
628a07bd003SBill Paul 	struct vge_softc		*sc;
629a07bd003SBill Paul {
630a07bd003SBill Paul 	register int		i;
631a07bd003SBill Paul 
632a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
633a07bd003SBill Paul 
634a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
635a07bd003SBill Paul 		DELAY(5);
636a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
637a07bd003SBill Paul 			break;
638a07bd003SBill Paul 	}
639a07bd003SBill Paul 
640a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
641a07bd003SBill Paul 		device_printf(sc->vge_dev, "soft reset timed out");
642a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
643a07bd003SBill Paul 		DELAY(2000);
644a07bd003SBill Paul 	}
645a07bd003SBill Paul 
646a07bd003SBill Paul 	DELAY(5000);
647a07bd003SBill Paul 
648a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
649a07bd003SBill Paul 
650a07bd003SBill Paul 	for (i = 0; i < VGE_TIMEOUT; i++) {
651a07bd003SBill Paul 		DELAY(5);
652a07bd003SBill Paul 		if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
653a07bd003SBill Paul 			break;
654a07bd003SBill Paul 	}
655a07bd003SBill Paul 
656a07bd003SBill Paul 	if (i == VGE_TIMEOUT) {
657a07bd003SBill Paul 		device_printf(sc->vge_dev, "EEPROM reload timed out\n");
658a07bd003SBill Paul 		return;
659a07bd003SBill Paul 	}
660a07bd003SBill Paul 
661a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
662a07bd003SBill Paul 
663a07bd003SBill Paul 	return;
664a07bd003SBill Paul }
665a07bd003SBill Paul 
666a07bd003SBill Paul /*
667a07bd003SBill Paul  * Probe for a VIA gigabit chip. Check the PCI vendor and device
668a07bd003SBill Paul  * IDs against our list and return a device name if we find a match.
669a07bd003SBill Paul  */
670a07bd003SBill Paul static int
671a07bd003SBill Paul vge_probe(dev)
672a07bd003SBill Paul 	device_t		dev;
673a07bd003SBill Paul {
674a07bd003SBill Paul 	struct vge_type		*t;
675a07bd003SBill Paul 	struct vge_softc	*sc;
676a07bd003SBill Paul 
677a07bd003SBill Paul 	t = vge_devs;
678a07bd003SBill Paul 	sc = device_get_softc(dev);
679a07bd003SBill Paul 
680a07bd003SBill Paul 	while (t->vge_name != NULL) {
681a07bd003SBill Paul 		if ((pci_get_vendor(dev) == t->vge_vid) &&
682a07bd003SBill Paul 		    (pci_get_device(dev) == t->vge_did)) {
683a07bd003SBill Paul 			device_set_desc(dev, t->vge_name);
684a07bd003SBill Paul 			return (0);
685a07bd003SBill Paul 		}
686a07bd003SBill Paul 		t++;
687a07bd003SBill Paul 	}
688a07bd003SBill Paul 
689a07bd003SBill Paul 	return (ENXIO);
690a07bd003SBill Paul }
691a07bd003SBill Paul 
692a07bd003SBill Paul static void
693a07bd003SBill Paul vge_dma_map_rx_desc(arg, segs, nseg, mapsize, error)
694a07bd003SBill Paul 	void			*arg;
695a07bd003SBill Paul 	bus_dma_segment_t	*segs;
696a07bd003SBill Paul 	int			nseg;
697a07bd003SBill Paul 	bus_size_t		mapsize;
698a07bd003SBill Paul 	int			error;
699a07bd003SBill Paul {
700a07bd003SBill Paul 
701a07bd003SBill Paul 	struct vge_dmaload_arg	*ctx;
702a07bd003SBill Paul 	struct vge_rx_desc	*d = NULL;
703a07bd003SBill Paul 
704a07bd003SBill Paul 	if (error)
705a07bd003SBill Paul 		return;
706a07bd003SBill Paul 
707a07bd003SBill Paul 	ctx = arg;
708a07bd003SBill Paul 
709a07bd003SBill Paul 	/* Signal error to caller if there's too many segments */
710a07bd003SBill Paul 	if (nseg > ctx->vge_maxsegs) {
711a07bd003SBill Paul 		ctx->vge_maxsegs = 0;
712a07bd003SBill Paul 		return;
713a07bd003SBill Paul 	}
714a07bd003SBill Paul 
715a07bd003SBill Paul 	/*
716a07bd003SBill Paul 	 * Map the segment array into descriptors.
717a07bd003SBill Paul 	 */
718a07bd003SBill Paul 
719a07bd003SBill Paul 	d = &ctx->sc->vge_ldata.vge_rx_list[ctx->vge_idx];
720a07bd003SBill Paul 
721a07bd003SBill Paul 	/* If this descriptor is still owned by the chip, bail. */
722a07bd003SBill Paul 
723a07bd003SBill Paul 	if (le32toh(d->vge_sts) & VGE_RDSTS_OWN) {
724a07bd003SBill Paul 		device_printf(ctx->sc->vge_dev,
725a07bd003SBill Paul 		    "tried to map busy descriptor\n");
726a07bd003SBill Paul 		ctx->vge_maxsegs = 0;
727a07bd003SBill Paul 		return;
728a07bd003SBill Paul 	}
729a07bd003SBill Paul 
730a07bd003SBill Paul 	d->vge_buflen = htole16(VGE_BUFLEN(segs[0].ds_len) | VGE_RXDESC_I);
731a07bd003SBill Paul 	d->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
732a07bd003SBill Paul 	d->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF);
733a07bd003SBill Paul 	d->vge_sts = 0;
734a07bd003SBill Paul 	d->vge_ctl = 0;
735a07bd003SBill Paul 
736a07bd003SBill Paul 	ctx->vge_maxsegs = 1;
737a07bd003SBill Paul 
738a07bd003SBill Paul 	return;
739a07bd003SBill Paul }
740a07bd003SBill Paul 
741a07bd003SBill Paul static void
742a07bd003SBill Paul vge_dma_map_tx_desc(arg, segs, nseg, mapsize, error)
743a07bd003SBill Paul 	void			*arg;
744a07bd003SBill Paul 	bus_dma_segment_t	*segs;
745a07bd003SBill Paul 	int			nseg;
746a07bd003SBill Paul 	bus_size_t		mapsize;
747a07bd003SBill Paul 	int			error;
748a07bd003SBill Paul {
749a07bd003SBill Paul 	struct vge_dmaload_arg	*ctx;
750a07bd003SBill Paul 	struct vge_tx_desc	*d = NULL;
751a07bd003SBill Paul 	struct vge_tx_frag	*f;
752a07bd003SBill Paul 	int			i = 0;
753a07bd003SBill Paul 
754a07bd003SBill Paul 	if (error)
755a07bd003SBill Paul 		return;
756a07bd003SBill Paul 
757a07bd003SBill Paul 	ctx = arg;
758a07bd003SBill Paul 
759a07bd003SBill Paul 	/* Signal error to caller if there's too many segments */
760a07bd003SBill Paul 	if (nseg > ctx->vge_maxsegs) {
761a07bd003SBill Paul 		ctx->vge_maxsegs = 0;
762a07bd003SBill Paul 		return;
763a07bd003SBill Paul 	}
764a07bd003SBill Paul 
765a07bd003SBill Paul 	/* Map the segment array into descriptors. */
766a07bd003SBill Paul 
767a07bd003SBill Paul 	d = &ctx->sc->vge_ldata.vge_tx_list[ctx->vge_idx];
768a07bd003SBill Paul 
769a07bd003SBill Paul 	/* If this descriptor is still owned by the chip, bail. */
770a07bd003SBill Paul 
771a07bd003SBill Paul 	if (le32toh(d->vge_sts) & VGE_TDSTS_OWN) {
772a07bd003SBill Paul 		ctx->vge_maxsegs = 0;
773a07bd003SBill Paul 		return;
774a07bd003SBill Paul 	}
775a07bd003SBill Paul 
776a07bd003SBill Paul 	for (i = 0; i < nseg; i++) {
777a07bd003SBill Paul 		f = &d->vge_frag[i];
778a07bd003SBill Paul 		f->vge_buflen = htole16(VGE_BUFLEN(segs[i].ds_len));
779a07bd003SBill Paul 		f->vge_addrlo = htole32(VGE_ADDR_LO(segs[i].ds_addr));
780a07bd003SBill Paul 		f->vge_addrhi = htole16(VGE_ADDR_HI(segs[i].ds_addr) & 0xFFFF);
781a07bd003SBill Paul 	}
782a07bd003SBill Paul 
783a07bd003SBill Paul 	/* Argh. This chip does not autopad short frames */
784a07bd003SBill Paul 
785a07bd003SBill Paul 	if (ctx->vge_m0->m_pkthdr.len < VGE_MIN_FRAMELEN) {
786a07bd003SBill Paul 		f = &d->vge_frag[i];
787a07bd003SBill Paul 		f->vge_buflen = htole16(VGE_BUFLEN(VGE_MIN_FRAMELEN -
788a07bd003SBill Paul 		    ctx->vge_m0->m_pkthdr.len));
789a07bd003SBill Paul 		f->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
790a07bd003SBill Paul 		f->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF);
791a07bd003SBill Paul 		ctx->vge_m0->m_pkthdr.len = VGE_MIN_FRAMELEN;
792a07bd003SBill Paul 		i++;
793a07bd003SBill Paul 	}
794a07bd003SBill Paul 
795a07bd003SBill Paul 	/*
796a07bd003SBill Paul 	 * When telling the chip how many segments there are, we
797a07bd003SBill Paul 	 * must use nsegs + 1 instead of just nsegs. Darned if I
798a07bd003SBill Paul 	 * know why.
799a07bd003SBill Paul 	 */
800a07bd003SBill Paul 	i++;
801a07bd003SBill Paul 
802a07bd003SBill Paul 	d->vge_sts = ctx->vge_m0->m_pkthdr.len << 16;
803a07bd003SBill Paul 	d->vge_ctl = ctx->vge_flags|(i << 28)|VGE_TD_LS_NORM;
804a07bd003SBill Paul 
805a07bd003SBill Paul 	if (ctx->vge_m0->m_pkthdr.len > ETHERMTU + ETHER_HDR_LEN)
806a07bd003SBill Paul 		d->vge_ctl |= VGE_TDCTL_JUMBO;
807a07bd003SBill Paul 
808a07bd003SBill Paul 	ctx->vge_maxsegs = nseg;
809a07bd003SBill Paul 
810a07bd003SBill Paul 	return;
811a07bd003SBill Paul }
812a07bd003SBill Paul 
813a07bd003SBill Paul /*
814a07bd003SBill Paul  * Map a single buffer address.
815a07bd003SBill Paul  */
816a07bd003SBill Paul 
817a07bd003SBill Paul static void
818a07bd003SBill Paul vge_dma_map_addr(arg, segs, nseg, error)
819a07bd003SBill Paul 	void			*arg;
820a07bd003SBill Paul 	bus_dma_segment_t	*segs;
821a07bd003SBill Paul 	int			nseg;
822a07bd003SBill Paul 	int			error;
823a07bd003SBill Paul {
824a07bd003SBill Paul 	bus_addr_t		*addr;
825a07bd003SBill Paul 
826a07bd003SBill Paul 	if (error)
827a07bd003SBill Paul 		return;
828a07bd003SBill Paul 
829a07bd003SBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
830a07bd003SBill Paul 	addr = arg;
831a07bd003SBill Paul 	*addr = segs->ds_addr;
832a07bd003SBill Paul 
833a07bd003SBill Paul 	return;
834a07bd003SBill Paul }
835a07bd003SBill Paul 
836a07bd003SBill Paul static int
837a07bd003SBill Paul vge_allocmem(dev, sc)
838a07bd003SBill Paul 	device_t		dev;
839a07bd003SBill Paul 	struct vge_softc		*sc;
840a07bd003SBill Paul {
841a07bd003SBill Paul 	int			error;
842a07bd003SBill Paul 	int			nseg;
843a07bd003SBill Paul 	int			i;
844a07bd003SBill Paul 
845a07bd003SBill Paul 	/*
846a07bd003SBill Paul 	 * Allocate map for RX mbufs.
847a07bd003SBill Paul 	 */
848a07bd003SBill Paul 	nseg = 32;
849a07bd003SBill Paul 	error = bus_dma_tag_create(sc->vge_parent_tag, ETHER_ALIGN, 0,
850a07bd003SBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
851a07bd003SBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
852a07bd003SBill Paul 	    NULL, NULL, &sc->vge_ldata.vge_mtag);
853a07bd003SBill Paul 	if (error) {
854a07bd003SBill Paul 		device_printf(dev, "could not allocate dma tag\n");
855a07bd003SBill Paul 		return (ENOMEM);
856a07bd003SBill Paul 	}
857a07bd003SBill Paul 
858a07bd003SBill Paul 	/*
859a07bd003SBill Paul 	 * Allocate map for TX descriptor list.
860a07bd003SBill Paul 	 */
861a07bd003SBill Paul 	error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN,
862a07bd003SBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
863a07bd003SBill Paul 	    NULL, VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
864a07bd003SBill Paul 	    NULL, NULL, &sc->vge_ldata.vge_tx_list_tag);
865a07bd003SBill Paul 	if (error) {
866a07bd003SBill Paul 		device_printf(dev, "could not allocate dma tag\n");
867a07bd003SBill Paul 		return (ENOMEM);
868a07bd003SBill Paul 	}
869a07bd003SBill Paul 
870a07bd003SBill Paul 	/* Allocate DMA'able memory for the TX ring */
871a07bd003SBill Paul 
872a07bd003SBill Paul 	error = bus_dmamem_alloc(sc->vge_ldata.vge_tx_list_tag,
873a07bd003SBill Paul 	    (void **)&sc->vge_ldata.vge_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
874a07bd003SBill Paul 	    &sc->vge_ldata.vge_tx_list_map);
875a07bd003SBill Paul 	if (error)
876a07bd003SBill Paul 		return (ENOMEM);
877a07bd003SBill Paul 
878a07bd003SBill Paul 	/* Load the map for the TX ring. */
879a07bd003SBill Paul 
880a07bd003SBill Paul 	error = bus_dmamap_load(sc->vge_ldata.vge_tx_list_tag,
881a07bd003SBill Paul 	     sc->vge_ldata.vge_tx_list_map, sc->vge_ldata.vge_tx_list,
882a07bd003SBill Paul 	     VGE_TX_LIST_SZ, vge_dma_map_addr,
883a07bd003SBill Paul 	     &sc->vge_ldata.vge_tx_list_addr, BUS_DMA_NOWAIT);
884a07bd003SBill Paul 
885a07bd003SBill Paul 	/* Create DMA maps for TX buffers */
886a07bd003SBill Paul 
887a07bd003SBill Paul 	for (i = 0; i < VGE_TX_DESC_CNT; i++) {
888a07bd003SBill Paul 		error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0,
889a07bd003SBill Paul 			    &sc->vge_ldata.vge_tx_dmamap[i]);
890a07bd003SBill Paul 		if (error) {
891a07bd003SBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
892a07bd003SBill Paul 			return (ENOMEM);
893a07bd003SBill Paul 		}
894a07bd003SBill Paul 	}
895a07bd003SBill Paul 
896a07bd003SBill Paul 	/*
897a07bd003SBill Paul 	 * Allocate map for RX descriptor list.
898a07bd003SBill Paul 	 */
899a07bd003SBill Paul 	error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN,
900a07bd003SBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
901a07bd003SBill Paul 	    NULL, VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
902a07bd003SBill Paul 	    NULL, NULL, &sc->vge_ldata.vge_rx_list_tag);
903a07bd003SBill Paul 	if (error) {
904a07bd003SBill Paul 		device_printf(dev, "could not allocate dma tag\n");
905a07bd003SBill Paul 		return (ENOMEM);
906a07bd003SBill Paul 	}
907a07bd003SBill Paul 
908a07bd003SBill Paul 	/* Allocate DMA'able memory for the RX ring */
909a07bd003SBill Paul 
910a07bd003SBill Paul 	error = bus_dmamem_alloc(sc->vge_ldata.vge_rx_list_tag,
911a07bd003SBill Paul 	    (void **)&sc->vge_ldata.vge_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
912a07bd003SBill Paul 	    &sc->vge_ldata.vge_rx_list_map);
913a07bd003SBill Paul 	if (error)
914a07bd003SBill Paul 		return (ENOMEM);
915a07bd003SBill Paul 
916a07bd003SBill Paul 	/* Load the map for the RX ring. */
917a07bd003SBill Paul 
918a07bd003SBill Paul 	error = bus_dmamap_load(sc->vge_ldata.vge_rx_list_tag,
919a07bd003SBill Paul 	     sc->vge_ldata.vge_rx_list_map, sc->vge_ldata.vge_rx_list,
920a07bd003SBill Paul 	     VGE_TX_LIST_SZ, vge_dma_map_addr,
921a07bd003SBill Paul 	     &sc->vge_ldata.vge_rx_list_addr, BUS_DMA_NOWAIT);
922a07bd003SBill Paul 
923a07bd003SBill Paul 	/* Create DMA maps for RX buffers */
924a07bd003SBill Paul 
925a07bd003SBill Paul 	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
926a07bd003SBill Paul 		error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0,
927a07bd003SBill Paul 			    &sc->vge_ldata.vge_rx_dmamap[i]);
928a07bd003SBill Paul 		if (error) {
929a07bd003SBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
930a07bd003SBill Paul 			return (ENOMEM);
931a07bd003SBill Paul 		}
932a07bd003SBill Paul 	}
933a07bd003SBill Paul 
934a07bd003SBill Paul 	return (0);
935a07bd003SBill Paul }
936a07bd003SBill Paul 
937a07bd003SBill Paul /*
938a07bd003SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
939a07bd003SBill Paul  * setup and ethernet/BPF attach.
940a07bd003SBill Paul  */
941a07bd003SBill Paul static int
942a07bd003SBill Paul vge_attach(dev)
943a07bd003SBill Paul 	device_t		dev;
944a07bd003SBill Paul {
945a07bd003SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
946a07bd003SBill Paul 	struct vge_softc	*sc;
947a07bd003SBill Paul 	struct ifnet		*ifp;
948a07bd003SBill Paul 	int			unit, error = 0, rid;
949a07bd003SBill Paul 
950a07bd003SBill Paul 	sc = device_get_softc(dev);
951a07bd003SBill Paul 	unit = device_get_unit(dev);
952a07bd003SBill Paul 	sc->vge_dev = dev;
953a07bd003SBill Paul 
954a07bd003SBill Paul 	mtx_init(&sc->vge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
955a07bd003SBill Paul 	    MTX_DEF | MTX_RECURSE);
956a07bd003SBill Paul 	/*
957a07bd003SBill Paul 	 * Map control/status registers.
958a07bd003SBill Paul 	 */
959a07bd003SBill Paul 	pci_enable_busmaster(dev);
960a07bd003SBill Paul 
961a07bd003SBill Paul 	rid = VGE_PCI_LOMEM;
962a07bd003SBill Paul 	sc->vge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
963a07bd003SBill Paul 	    0, ~0, 1, RF_ACTIVE);
964a07bd003SBill Paul 
965a07bd003SBill Paul 	if (sc->vge_res == NULL) {
966a07bd003SBill Paul 		printf ("vge%d: couldn't map ports/memory\n", unit);
967a07bd003SBill Paul 		error = ENXIO;
968a07bd003SBill Paul 		goto fail;
969a07bd003SBill Paul 	}
970a07bd003SBill Paul 
971a07bd003SBill Paul 	sc->vge_btag = rman_get_bustag(sc->vge_res);
972a07bd003SBill Paul 	sc->vge_bhandle = rman_get_bushandle(sc->vge_res);
973a07bd003SBill Paul 
974a07bd003SBill Paul 	/* Allocate interrupt */
975a07bd003SBill Paul 	rid = 0;
976a07bd003SBill Paul 	sc->vge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
977a07bd003SBill Paul 	    0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
978a07bd003SBill Paul 
979a07bd003SBill Paul 	if (sc->vge_irq == NULL) {
980a07bd003SBill Paul 		printf("vge%d: couldn't map interrupt\n", unit);
981a07bd003SBill Paul 		error = ENXIO;
982a07bd003SBill Paul 		goto fail;
983a07bd003SBill Paul 	}
984a07bd003SBill Paul 
985a07bd003SBill Paul 	/* Reset the adapter. */
986a07bd003SBill Paul 	vge_reset(sc);
987a07bd003SBill Paul 
988a07bd003SBill Paul 	/*
989a07bd003SBill Paul 	 * Get station address from the EEPROM.
990a07bd003SBill Paul 	 */
991a07bd003SBill Paul 	vge_read_eeprom(sc, (caddr_t)eaddr, VGE_EE_EADDR, 3, 0);
992a07bd003SBill Paul 
993a07bd003SBill Paul 	sc->vge_unit = unit;
994a07bd003SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
995a07bd003SBill Paul 
996a07bd003SBill Paul #if __FreeBSD_version < 502113
997a07bd003SBill Paul 	printf("vge%d: Ethernet address: %6D\n", unit, eaddr, ":");
998a07bd003SBill Paul #endif
999a07bd003SBill Paul 
1000a07bd003SBill Paul 	/*
1001a07bd003SBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1002a07bd003SBill Paul 	 */
1003a07bd003SBill Paul #define VGE_NSEG_NEW 32
1004a07bd003SBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1005a07bd003SBill Paul 			1, 0,			/* alignment, boundary */
1006a07bd003SBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1007a07bd003SBill Paul 			BUS_SPACE_MAXADDR,	/* highaddr */
1008a07bd003SBill Paul 			NULL, NULL,		/* filter, filterarg */
1009a07bd003SBill Paul 			MAXBSIZE, VGE_NSEG_NEW,	/* maxsize, nsegments */
1010a07bd003SBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1011a07bd003SBill Paul 			BUS_DMA_ALLOCNOW,	/* flags */
1012a07bd003SBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1013a07bd003SBill Paul 			&sc->vge_parent_tag);
1014a07bd003SBill Paul 	if (error)
1015a07bd003SBill Paul 		goto fail;
1016a07bd003SBill Paul 
1017a07bd003SBill Paul 	error = vge_allocmem(dev, sc);
1018a07bd003SBill Paul 
1019a07bd003SBill Paul 	if (error)
1020a07bd003SBill Paul 		goto fail;
1021a07bd003SBill Paul 
1022a07bd003SBill Paul 	/* Do MII setup */
1023a07bd003SBill Paul 	if (mii_phy_probe(dev, &sc->vge_miibus,
1024a07bd003SBill Paul 	    vge_ifmedia_upd, vge_ifmedia_sts)) {
1025a07bd003SBill Paul 		printf("vge%d: MII without any phy!\n", sc->vge_unit);
1026a07bd003SBill Paul 		error = ENXIO;
1027a07bd003SBill Paul 		goto fail;
1028a07bd003SBill Paul 	}
1029a07bd003SBill Paul 
1030a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1031a07bd003SBill Paul 	ifp->if_softc = sc;
1032a07bd003SBill Paul 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1033a07bd003SBill Paul 	ifp->if_mtu = ETHERMTU;
1034a07bd003SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1035a07bd003SBill Paul 	ifp->if_ioctl = vge_ioctl;
1036a07bd003SBill Paul 	ifp->if_capabilities = IFCAP_VLAN_MTU;
1037a07bd003SBill Paul 	ifp->if_start = vge_start;
1038a07bd003SBill Paul 	ifp->if_hwassist = VGE_CSUM_FEATURES;
1039a07bd003SBill Paul 	ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING;
1040a07bd003SBill Paul #ifdef DEVICE_POLLING
1041a07bd003SBill Paul #ifdef IFCAP_POLLING
1042a07bd003SBill Paul 	ifp->if_capabilities |= IFCAP_POLLING;
1043a07bd003SBill Paul #endif
1044a07bd003SBill Paul #endif
1045a07bd003SBill Paul 	ifp->if_watchdog = vge_watchdog;
1046a07bd003SBill Paul 	ifp->if_init = vge_init;
1047a07bd003SBill Paul 	ifp->if_baudrate = 1000000000;
1048a07bd003SBill Paul 	ifp->if_snd.ifq_maxlen = VGE_IFQ_MAXLEN;
1049a07bd003SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1050a07bd003SBill Paul 
1051a07bd003SBill Paul 	TASK_INIT(&sc->vge_txtask, 0, vge_tx_task, ifp);
1052a07bd003SBill Paul 
1053a07bd003SBill Paul 	/*
1054a07bd003SBill Paul 	 * Call MI attach routine.
1055a07bd003SBill Paul 	 */
1056a07bd003SBill Paul 	ether_ifattach(ifp, eaddr);
1057a07bd003SBill Paul 
1058a07bd003SBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1059a07bd003SBill Paul 	error = bus_setup_intr(dev, sc->vge_irq, INTR_TYPE_NET|INTR_MPSAFE,
1060a07bd003SBill Paul 	    vge_intr, sc, &sc->vge_intrhand);
1061a07bd003SBill Paul 
1062a07bd003SBill Paul 	if (error) {
1063a07bd003SBill Paul 		printf("vge%d: couldn't set up irq\n", unit);
1064a07bd003SBill Paul 		ether_ifdetach(ifp);
1065a07bd003SBill Paul 		goto fail;
1066a07bd003SBill Paul 	}
1067a07bd003SBill Paul 
1068a07bd003SBill Paul fail:
1069a07bd003SBill Paul 	if (error)
1070a07bd003SBill Paul 		vge_detach(dev);
1071a07bd003SBill Paul 
1072a07bd003SBill Paul 	return (error);
1073a07bd003SBill Paul }
1074a07bd003SBill Paul 
1075a07bd003SBill Paul /*
1076a07bd003SBill Paul  * Shutdown hardware and free up resources. This can be called any
1077a07bd003SBill Paul  * time after the mutex has been initialized. It is called in both
1078a07bd003SBill Paul  * the error case in attach and the normal detach case so it needs
1079a07bd003SBill Paul  * to be careful about only freeing resources that have actually been
1080a07bd003SBill Paul  * allocated.
1081a07bd003SBill Paul  */
1082a07bd003SBill Paul static int
1083a07bd003SBill Paul vge_detach(dev)
1084a07bd003SBill Paul 	device_t		dev;
1085a07bd003SBill Paul {
1086a07bd003SBill Paul 	struct vge_softc		*sc;
1087a07bd003SBill Paul 	struct ifnet		*ifp;
1088a07bd003SBill Paul 	int			i;
1089a07bd003SBill Paul 
1090a07bd003SBill Paul 	sc = device_get_softc(dev);
1091a07bd003SBill Paul 	KASSERT(mtx_initialized(&sc->vge_mtx), ("vge mutex not initialized"));
1092a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1093a07bd003SBill Paul 
1094a07bd003SBill Paul 	/* These should only be active if attach succeeded */
1095a07bd003SBill Paul 	if (device_is_attached(dev)) {
1096a07bd003SBill Paul 		vge_stop(sc);
1097a07bd003SBill Paul 		/*
1098a07bd003SBill Paul 		 * Force off the IFF_UP flag here, in case someone
1099a07bd003SBill Paul 		 * still had a BPF descriptor attached to this
1100a07bd003SBill Paul 		 * interface. If they do, ether_ifattach() will cause
1101a07bd003SBill Paul 		 * the BPF code to try and clear the promisc mode
1102a07bd003SBill Paul 		 * flag, which will bubble down to vge_ioctl(),
1103a07bd003SBill Paul 		 * which will try to call vge_init() again. This will
1104a07bd003SBill Paul 		 * turn the NIC back on and restart the MII ticker,
1105a07bd003SBill Paul 		 * which will panic the system when the kernel tries
1106a07bd003SBill Paul 		 * to invoke the vge_tick() function that isn't there
1107a07bd003SBill Paul 		 * anymore.
1108a07bd003SBill Paul 		 */
1109a07bd003SBill Paul 		ifp->if_flags &= ~IFF_UP;
1110a07bd003SBill Paul 		ether_ifdetach(ifp);
1111a07bd003SBill Paul 	}
1112a07bd003SBill Paul 	if (sc->vge_miibus)
1113a07bd003SBill Paul 		device_delete_child(dev, sc->vge_miibus);
1114a07bd003SBill Paul 	bus_generic_detach(dev);
1115a07bd003SBill Paul 
1116a07bd003SBill Paul 	if (sc->vge_intrhand)
1117a07bd003SBill Paul 		bus_teardown_intr(dev, sc->vge_irq, sc->vge_intrhand);
1118a07bd003SBill Paul 	if (sc->vge_irq)
1119a07bd003SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vge_irq);
1120a07bd003SBill Paul 	if (sc->vge_res)
1121a07bd003SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
1122a07bd003SBill Paul 		    VGE_PCI_LOMEM, sc->vge_res);
1123a07bd003SBill Paul 
1124a07bd003SBill Paul 	/* Unload and free the RX DMA ring memory and map */
1125a07bd003SBill Paul 
1126a07bd003SBill Paul 	if (sc->vge_ldata.vge_rx_list_tag) {
1127a07bd003SBill Paul 		bus_dmamap_unload(sc->vge_ldata.vge_rx_list_tag,
1128a07bd003SBill Paul 		    sc->vge_ldata.vge_rx_list_map);
1129a07bd003SBill Paul 		bus_dmamem_free(sc->vge_ldata.vge_rx_list_tag,
1130a07bd003SBill Paul 		    sc->vge_ldata.vge_rx_list,
1131a07bd003SBill Paul 		    sc->vge_ldata.vge_rx_list_map);
1132a07bd003SBill Paul 		bus_dma_tag_destroy(sc->vge_ldata.vge_rx_list_tag);
1133a07bd003SBill Paul 	}
1134a07bd003SBill Paul 
1135a07bd003SBill Paul 	/* Unload and free the TX DMA ring memory and map */
1136a07bd003SBill Paul 
1137a07bd003SBill Paul 	if (sc->vge_ldata.vge_tx_list_tag) {
1138a07bd003SBill Paul 		bus_dmamap_unload(sc->vge_ldata.vge_tx_list_tag,
1139a07bd003SBill Paul 		    sc->vge_ldata.vge_tx_list_map);
1140a07bd003SBill Paul 		bus_dmamem_free(sc->vge_ldata.vge_tx_list_tag,
1141a07bd003SBill Paul 		    sc->vge_ldata.vge_tx_list,
1142a07bd003SBill Paul 		    sc->vge_ldata.vge_tx_list_map);
1143a07bd003SBill Paul 		bus_dma_tag_destroy(sc->vge_ldata.vge_tx_list_tag);
1144a07bd003SBill Paul 	}
1145a07bd003SBill Paul 
1146a07bd003SBill Paul 	/* Destroy all the RX and TX buffer maps */
1147a07bd003SBill Paul 
1148a07bd003SBill Paul 	if (sc->vge_ldata.vge_mtag) {
1149a07bd003SBill Paul 		for (i = 0; i < VGE_TX_DESC_CNT; i++)
1150a07bd003SBill Paul 			bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
1151a07bd003SBill Paul 			    sc->vge_ldata.vge_tx_dmamap[i]);
1152a07bd003SBill Paul 		for (i = 0; i < VGE_RX_DESC_CNT; i++)
1153a07bd003SBill Paul 			bus_dmamap_destroy(sc->vge_ldata.vge_mtag,
1154a07bd003SBill Paul 			    sc->vge_ldata.vge_rx_dmamap[i]);
1155a07bd003SBill Paul 		bus_dma_tag_destroy(sc->vge_ldata.vge_mtag);
1156a07bd003SBill Paul 	}
1157a07bd003SBill Paul 
1158a07bd003SBill Paul 	if (sc->vge_parent_tag)
1159a07bd003SBill Paul 		bus_dma_tag_destroy(sc->vge_parent_tag);
1160a07bd003SBill Paul 
1161a07bd003SBill Paul 	mtx_destroy(&sc->vge_mtx);
1162a07bd003SBill Paul 
1163a07bd003SBill Paul 	return (0);
1164a07bd003SBill Paul }
1165a07bd003SBill Paul 
1166a07bd003SBill Paul static int
1167a07bd003SBill Paul vge_newbuf(sc, idx, m)
1168a07bd003SBill Paul 	struct vge_softc	*sc;
1169a07bd003SBill Paul 	int			idx;
1170a07bd003SBill Paul 	struct mbuf		*m;
1171a07bd003SBill Paul {
1172a07bd003SBill Paul 	struct vge_dmaload_arg	arg;
1173a07bd003SBill Paul 	struct mbuf		*n = NULL;
1174a07bd003SBill Paul 	int			i, error;
1175a07bd003SBill Paul 
1176a07bd003SBill Paul 	if (m == NULL) {
1177a07bd003SBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1178a07bd003SBill Paul 		if (n == NULL)
1179a07bd003SBill Paul 			return (ENOBUFS);
1180a07bd003SBill Paul 		m = n;
1181a07bd003SBill Paul 	} else
1182a07bd003SBill Paul 		m->m_data = m->m_ext.ext_buf;
1183a07bd003SBill Paul 
1184a07bd003SBill Paul 
1185a07bd003SBill Paul #ifdef VGE_FIXUP_RX
1186a07bd003SBill Paul 	/*
1187a07bd003SBill Paul 	 * This is part of an evil trick to deal with non-x86 platforms.
1188a07bd003SBill Paul 	 * The VIA chip requires RX buffers to be aligned on 32-bit
1189a07bd003SBill Paul 	 * boundaries, but that will hose non-x86 machines. To get around
1190a07bd003SBill Paul 	 * this, we leave some empty space at the start of each buffer
1191a07bd003SBill Paul 	 * and for non-x86 hosts, we copy the buffer back two bytes
1192a07bd003SBill Paul 	 * to achieve word alignment. This is slightly more efficient
1193a07bd003SBill Paul 	 * than allocating a new buffer, copying the contents, and
1194a07bd003SBill Paul 	 * discarding the old buffer.
1195a07bd003SBill Paul 	 */
1196a07bd003SBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES - VGE_ETHER_ALIGN;
1197a07bd003SBill Paul 	m_adj(m, VGE_ETHER_ALIGN);
1198a07bd003SBill Paul #else
1199a07bd003SBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1200a07bd003SBill Paul #endif
1201a07bd003SBill Paul 
1202a07bd003SBill Paul 	arg.sc = sc;
1203a07bd003SBill Paul 	arg.vge_idx = idx;
1204a07bd003SBill Paul 	arg.vge_maxsegs = 1;
1205a07bd003SBill Paul 	arg.vge_flags = 0;
1206a07bd003SBill Paul 
1207a07bd003SBill Paul 	error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag,
1208a07bd003SBill Paul 	    sc->vge_ldata.vge_rx_dmamap[idx], m, vge_dma_map_rx_desc,
1209a07bd003SBill Paul 	    &arg, BUS_DMA_NOWAIT);
1210a07bd003SBill Paul 	if (error || arg.vge_maxsegs != 1) {
1211a07bd003SBill Paul 		if (n != NULL)
1212a07bd003SBill Paul 			m_freem(n);
1213a07bd003SBill Paul 		return (ENOMEM);
1214a07bd003SBill Paul 	}
1215a07bd003SBill Paul 
1216a07bd003SBill Paul 	/*
1217a07bd003SBill Paul 	 * Note: the manual fails to document the fact that for
1218a07bd003SBill Paul 	 * proper opration, the driver needs to replentish the RX
1219a07bd003SBill Paul 	 * DMA ring 4 descriptors at a time (rather than one at a
1220a07bd003SBill Paul 	 * time, like most chips). We can allocate the new buffers
1221a07bd003SBill Paul 	 * but we should not set the OWN bits until we're ready
1222a07bd003SBill Paul 	 * to hand back 4 of them in one shot.
1223a07bd003SBill Paul 	 */
1224a07bd003SBill Paul 
1225a07bd003SBill Paul #define VGE_RXCHUNK 4
1226a07bd003SBill Paul 	sc->vge_rx_consumed++;
1227a07bd003SBill Paul 	if (sc->vge_rx_consumed == VGE_RXCHUNK) {
1228a07bd003SBill Paul 		for (i = idx; i != idx - sc->vge_rx_consumed; i--)
1229a07bd003SBill Paul 			sc->vge_ldata.vge_rx_list[i].vge_sts |=
1230a07bd003SBill Paul 			    htole32(VGE_RDSTS_OWN);
1231a07bd003SBill Paul 		sc->vge_rx_consumed = 0;
1232a07bd003SBill Paul 	}
1233a07bd003SBill Paul 
1234a07bd003SBill Paul 	sc->vge_ldata.vge_rx_mbuf[idx] = m;
1235a07bd003SBill Paul 
1236a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_mtag,
1237a07bd003SBill Paul 	    sc->vge_ldata.vge_rx_dmamap[idx],
1238a07bd003SBill Paul 	    BUS_DMASYNC_PREREAD);
1239a07bd003SBill Paul 
1240a07bd003SBill Paul 	return (0);
1241a07bd003SBill Paul }
1242a07bd003SBill Paul 
1243a07bd003SBill Paul static int
1244a07bd003SBill Paul vge_tx_list_init(sc)
1245a07bd003SBill Paul 	struct vge_softc		*sc;
1246a07bd003SBill Paul {
1247a07bd003SBill Paul 	bzero ((char *)sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ);
1248a07bd003SBill Paul 	bzero ((char *)&sc->vge_ldata.vge_tx_mbuf,
1249a07bd003SBill Paul 	    (VGE_TX_DESC_CNT * sizeof(struct mbuf *)));
1250a07bd003SBill Paul 
1251a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1252a07bd003SBill Paul 	    sc->vge_ldata.vge_tx_list_map, BUS_DMASYNC_PREWRITE);
1253a07bd003SBill Paul 	sc->vge_ldata.vge_tx_prodidx = 0;
1254a07bd003SBill Paul 	sc->vge_ldata.vge_tx_considx = 0;
1255a07bd003SBill Paul 	sc->vge_ldata.vge_tx_free = VGE_TX_DESC_CNT;
1256a07bd003SBill Paul 
1257a07bd003SBill Paul 	return (0);
1258a07bd003SBill Paul }
1259a07bd003SBill Paul 
1260a07bd003SBill Paul static int
1261a07bd003SBill Paul vge_rx_list_init(sc)
1262a07bd003SBill Paul 	struct vge_softc		*sc;
1263a07bd003SBill Paul {
1264a07bd003SBill Paul 	int			i;
1265a07bd003SBill Paul 
1266a07bd003SBill Paul 	bzero ((char *)sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ);
1267a07bd003SBill Paul 	bzero ((char *)&sc->vge_ldata.vge_rx_mbuf,
1268a07bd003SBill Paul 	    (VGE_RX_DESC_CNT * sizeof(struct mbuf *)));
1269a07bd003SBill Paul 
1270a07bd003SBill Paul 	sc->vge_rx_consumed = 0;
1271a07bd003SBill Paul 
1272a07bd003SBill Paul 	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1273a07bd003SBill Paul 		if (vge_newbuf(sc, i, NULL) == ENOBUFS)
1274a07bd003SBill Paul 			return (ENOBUFS);
1275a07bd003SBill Paul 	}
1276a07bd003SBill Paul 
1277a07bd003SBill Paul 	/* Flush the RX descriptors */
1278a07bd003SBill Paul 
1279a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1280a07bd003SBill Paul 	    sc->vge_ldata.vge_rx_list_map,
1281a07bd003SBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1282a07bd003SBill Paul 
1283a07bd003SBill Paul 	sc->vge_ldata.vge_rx_prodidx = 0;
1284a07bd003SBill Paul 	sc->vge_rx_consumed = 0;
1285a07bd003SBill Paul 	sc->vge_head = sc->vge_tail = NULL;
1286a07bd003SBill Paul 
1287a07bd003SBill Paul 	return (0);
1288a07bd003SBill Paul }
1289a07bd003SBill Paul 
1290a07bd003SBill Paul #ifdef VGE_FIXUP_RX
1291a07bd003SBill Paul static __inline void
1292a07bd003SBill Paul vge_fixup_rx(m)
1293a07bd003SBill Paul 	struct mbuf		*m;
1294a07bd003SBill Paul {
1295a07bd003SBill Paul 	int			i;
1296a07bd003SBill Paul 	uint16_t		*src, *dst;
1297a07bd003SBill Paul 
1298a07bd003SBill Paul 	src = mtod(m, uint16_t *);
1299a07bd003SBill Paul 	dst = src - 1;
1300a07bd003SBill Paul 
1301a07bd003SBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1302a07bd003SBill Paul 		*dst++ = *src++;
1303a07bd003SBill Paul 
1304a07bd003SBill Paul 	m->m_data -= ETHER_ALIGN;
1305a07bd003SBill Paul 
1306a07bd003SBill Paul 	return;
1307a07bd003SBill Paul }
1308a07bd003SBill Paul #endif
1309a07bd003SBill Paul 
1310a07bd003SBill Paul /*
1311a07bd003SBill Paul  * RX handler. We support the reception of jumbo frames that have
1312a07bd003SBill Paul  * been fragmented across multiple 2K mbuf cluster buffers.
1313a07bd003SBill Paul  */
1314a07bd003SBill Paul static void
1315a07bd003SBill Paul vge_rxeof(sc)
1316a07bd003SBill Paul 	struct vge_softc	*sc;
1317a07bd003SBill Paul {
1318a07bd003SBill Paul 	struct mbuf		*m;
1319a07bd003SBill Paul 	struct ifnet		*ifp;
1320a07bd003SBill Paul 	int			i, total_len;
1321a07bd003SBill Paul 	int			lim = 0;
1322a07bd003SBill Paul 	struct vge_rx_desc	*cur_rx;
1323a07bd003SBill Paul 	u_int32_t		rxstat, rxctl;
1324a07bd003SBill Paul 
1325a07bd003SBill Paul 	VGE_LOCK_ASSERT(sc);
1326a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1327a07bd003SBill Paul 	i = sc->vge_ldata.vge_rx_prodidx;
1328a07bd003SBill Paul 
1329a07bd003SBill Paul 	/* Invalidate the descriptor memory */
1330a07bd003SBill Paul 
1331a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1332a07bd003SBill Paul 	    sc->vge_ldata.vge_rx_list_map,
1333a07bd003SBill Paul 	    BUS_DMASYNC_POSTREAD);
1334a07bd003SBill Paul 
1335a07bd003SBill Paul 	while (!VGE_OWN(&sc->vge_ldata.vge_rx_list[i])) {
1336a07bd003SBill Paul 
1337a07bd003SBill Paul #ifdef DEVICE_POLLING
1338a07bd003SBill Paul 		if (ifp->if_flags & IFF_POLLING) {
1339a07bd003SBill Paul 			if (sc->rxcycles <= 0)
1340a07bd003SBill Paul 				break;
1341a07bd003SBill Paul 			sc->rxcycles--;
1342a07bd003SBill Paul 		}
1343a07bd003SBill Paul #endif /* DEVICE_POLLING */
1344a07bd003SBill Paul 
1345a07bd003SBill Paul 		cur_rx = &sc->vge_ldata.vge_rx_list[i];
1346a07bd003SBill Paul 		m = sc->vge_ldata.vge_rx_mbuf[i];
1347a07bd003SBill Paul 		total_len = VGE_RXBYTES(cur_rx);
1348a07bd003SBill Paul 		rxstat = le32toh(cur_rx->vge_sts);
1349a07bd003SBill Paul 		rxctl = le32toh(cur_rx->vge_ctl);
1350a07bd003SBill Paul 
1351a07bd003SBill Paul 		/* Invalidate the RX mbuf and unload its map */
1352a07bd003SBill Paul 
1353a07bd003SBill Paul 		bus_dmamap_sync(sc->vge_ldata.vge_mtag,
1354a07bd003SBill Paul 		    sc->vge_ldata.vge_rx_dmamap[i],
1355a07bd003SBill Paul 		    BUS_DMASYNC_POSTWRITE);
1356a07bd003SBill Paul 		bus_dmamap_unload(sc->vge_ldata.vge_mtag,
1357a07bd003SBill Paul 		    sc->vge_ldata.vge_rx_dmamap[i]);
1358a07bd003SBill Paul 
1359a07bd003SBill Paul 		/*
1360a07bd003SBill Paul 		 * If the 'start of frame' bit is set, this indicates
1361a07bd003SBill Paul 		 * either the first fragment in a multi-fragment receive,
1362a07bd003SBill Paul 		 * or an intermediate fragment. Either way, we want to
1363a07bd003SBill Paul 		 * accumulate the buffers.
1364a07bd003SBill Paul 		 */
1365a07bd003SBill Paul 		if (rxstat & VGE_RXPKT_SOF) {
1366a07bd003SBill Paul 			m->m_len = MCLBYTES - VGE_ETHER_ALIGN;
1367a07bd003SBill Paul 			if (sc->vge_head == NULL)
1368a07bd003SBill Paul 				sc->vge_head = sc->vge_tail = m;
1369a07bd003SBill Paul 			else {
1370a07bd003SBill Paul 				m->m_flags &= ~M_PKTHDR;
1371a07bd003SBill Paul 				sc->vge_tail->m_next = m;
1372a07bd003SBill Paul 				sc->vge_tail = m;
1373a07bd003SBill Paul 			}
1374a07bd003SBill Paul 			vge_newbuf(sc, i, NULL);
1375a07bd003SBill Paul 			VGE_RX_DESC_INC(i);
1376a07bd003SBill Paul 			continue;
1377a07bd003SBill Paul 		}
1378a07bd003SBill Paul 
1379a07bd003SBill Paul 		/*
1380a07bd003SBill Paul 		 * Bad/error frames will have the RXOK bit cleared.
1381a07bd003SBill Paul 		 * However, there's one error case we want to allow:
1382a07bd003SBill Paul 		 * if a VLAN tagged frame arrives and the chip can't
1383a07bd003SBill Paul 		 * match it against the CAM filter, it considers this
1384a07bd003SBill Paul 		 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1385a07bd003SBill Paul 		 * We don't want to drop the frame though: our VLAN
1386a07bd003SBill Paul 		 * filtering is done in software.
1387a07bd003SBill Paul 		 */
1388a07bd003SBill Paul 		if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
1389a07bd003SBill Paul 		    && !(rxstat & VGE_RDSTS_CSUMERR)) {
1390a07bd003SBill Paul 			ifp->if_ierrors++;
1391a07bd003SBill Paul 			/*
1392a07bd003SBill Paul 			 * If this is part of a multi-fragment packet,
1393a07bd003SBill Paul 			 * discard all the pieces.
1394a07bd003SBill Paul 			 */
1395a07bd003SBill Paul 			if (sc->vge_head != NULL) {
1396a07bd003SBill Paul 				m_freem(sc->vge_head);
1397a07bd003SBill Paul 				sc->vge_head = sc->vge_tail = NULL;
1398a07bd003SBill Paul 			}
1399a07bd003SBill Paul 			vge_newbuf(sc, i, m);
1400a07bd003SBill Paul 			VGE_RX_DESC_INC(i);
1401a07bd003SBill Paul 			continue;
1402a07bd003SBill Paul 		}
1403a07bd003SBill Paul 
1404a07bd003SBill Paul 		/*
1405a07bd003SBill Paul 		 * If allocating a replacement mbuf fails,
1406a07bd003SBill Paul 		 * reload the current one.
1407a07bd003SBill Paul 		 */
1408a07bd003SBill Paul 
1409a07bd003SBill Paul 		if (vge_newbuf(sc, i, NULL)) {
1410a07bd003SBill Paul 			ifp->if_ierrors++;
1411a07bd003SBill Paul 			if (sc->vge_head != NULL) {
1412a07bd003SBill Paul 				m_freem(sc->vge_head);
1413a07bd003SBill Paul 				sc->vge_head = sc->vge_tail = NULL;
1414a07bd003SBill Paul 			}
1415a07bd003SBill Paul 			vge_newbuf(sc, i, m);
1416a07bd003SBill Paul 			VGE_RX_DESC_INC(i);
1417a07bd003SBill Paul 			continue;
1418a07bd003SBill Paul 		}
1419a07bd003SBill Paul 
1420a07bd003SBill Paul 		VGE_RX_DESC_INC(i);
1421a07bd003SBill Paul 
1422a07bd003SBill Paul 		if (sc->vge_head != NULL) {
1423a07bd003SBill Paul 			m->m_len = total_len % (MCLBYTES - VGE_ETHER_ALIGN);
1424a07bd003SBill Paul 			/*
1425a07bd003SBill Paul 			 * Special case: if there's 4 bytes or less
1426a07bd003SBill Paul 			 * in this buffer, the mbuf can be discarded:
1427a07bd003SBill Paul 			 * the last 4 bytes is the CRC, which we don't
1428a07bd003SBill Paul 			 * care about anyway.
1429a07bd003SBill Paul 			 */
1430a07bd003SBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1431a07bd003SBill Paul 				sc->vge_tail->m_len -=
1432a07bd003SBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1433a07bd003SBill Paul 				m_freem(m);
1434a07bd003SBill Paul 			} else {
1435a07bd003SBill Paul 				m->m_len -= ETHER_CRC_LEN;
1436a07bd003SBill Paul 				m->m_flags &= ~M_PKTHDR;
1437a07bd003SBill Paul 				sc->vge_tail->m_next = m;
1438a07bd003SBill Paul 			}
1439a07bd003SBill Paul 			m = sc->vge_head;
1440a07bd003SBill Paul 			sc->vge_head = sc->vge_tail = NULL;
1441a07bd003SBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1442a07bd003SBill Paul 		} else
1443a07bd003SBill Paul 			m->m_pkthdr.len = m->m_len =
1444a07bd003SBill Paul 			    (total_len - ETHER_CRC_LEN);
1445a07bd003SBill Paul 
1446a07bd003SBill Paul #ifdef VGE_FIXUP_RX
1447a07bd003SBill Paul 		vge_fixup_rx(m);
1448a07bd003SBill Paul #endif
1449a07bd003SBill Paul 		ifp->if_ipackets++;
1450a07bd003SBill Paul 		m->m_pkthdr.rcvif = ifp;
1451a07bd003SBill Paul 
1452a07bd003SBill Paul 		/* Do RX checksumming if enabled */
1453a07bd003SBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1454a07bd003SBill Paul 
1455a07bd003SBill Paul 			/* Check IP header checksum */
1456a07bd003SBill Paul 			if (rxctl & VGE_RDCTL_IPPKT)
1457a07bd003SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1458a07bd003SBill Paul 			if (rxctl & VGE_RDCTL_IPCSUMOK)
1459a07bd003SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1460a07bd003SBill Paul 
1461a07bd003SBill Paul 			/* Check TCP/UDP checksum */
1462a07bd003SBill Paul 			if (rxctl & (VGE_RDCTL_TCPPKT|VGE_RDCTL_UDPPKT) &&
1463a07bd003SBill Paul 			    rxctl & VGE_RDCTL_PROTOCSUMOK) {
1464a07bd003SBill Paul 				m->m_pkthdr.csum_flags |=
1465a07bd003SBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1466a07bd003SBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1467a07bd003SBill Paul 			}
1468a07bd003SBill Paul 		}
1469a07bd003SBill Paul 
1470a07bd003SBill Paul 		if (rxstat & VGE_RDSTS_VTAG)
1471a07bd003SBill Paul 			VLAN_INPUT_TAG(ifp, m,
1472a07bd003SBill Paul 			    ntohs((rxctl & VGE_RDCTL_VLANID)), continue);
1473a07bd003SBill Paul 
1474a07bd003SBill Paul 		VGE_UNLOCK(sc);
1475a07bd003SBill Paul 		(*ifp->if_input)(ifp, m);
1476a07bd003SBill Paul 		VGE_LOCK(sc);
1477a07bd003SBill Paul 
1478a07bd003SBill Paul 		lim++;
1479a07bd003SBill Paul 		if (lim == VGE_RX_DESC_CNT)
1480a07bd003SBill Paul 			break;
1481a07bd003SBill Paul 
1482a07bd003SBill Paul 	}
1483a07bd003SBill Paul 
1484a07bd003SBill Paul 	/* Flush the RX DMA ring */
1485a07bd003SBill Paul 
1486a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag,
1487a07bd003SBill Paul 	    sc->vge_ldata.vge_rx_list_map,
1488a07bd003SBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1489a07bd003SBill Paul 
1490a07bd003SBill Paul 	sc->vge_ldata.vge_rx_prodidx = i;
1491a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1492a07bd003SBill Paul 
1493a07bd003SBill Paul 
1494a07bd003SBill Paul 	return;
1495a07bd003SBill Paul }
1496a07bd003SBill Paul 
1497a07bd003SBill Paul static void
1498a07bd003SBill Paul vge_txeof(sc)
1499a07bd003SBill Paul 	struct vge_softc		*sc;
1500a07bd003SBill Paul {
1501a07bd003SBill Paul 	struct ifnet		*ifp;
1502a07bd003SBill Paul 	u_int32_t		txstat;
1503a07bd003SBill Paul 	int			idx;
1504a07bd003SBill Paul 
1505a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1506a07bd003SBill Paul 	idx = sc->vge_ldata.vge_tx_considx;
1507a07bd003SBill Paul 
1508a07bd003SBill Paul 	/* Invalidate the TX descriptor list */
1509a07bd003SBill Paul 
1510a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1511a07bd003SBill Paul 	    sc->vge_ldata.vge_tx_list_map,
1512a07bd003SBill Paul 	    BUS_DMASYNC_POSTREAD);
1513a07bd003SBill Paul 
1514a07bd003SBill Paul 	while (idx != sc->vge_ldata.vge_tx_prodidx) {
1515a07bd003SBill Paul 
1516a07bd003SBill Paul 		txstat = le32toh(sc->vge_ldata.vge_tx_list[idx].vge_sts);
1517a07bd003SBill Paul 		if (txstat & VGE_TDSTS_OWN)
1518a07bd003SBill Paul 			break;
1519a07bd003SBill Paul 
1520a07bd003SBill Paul 		m_freem(sc->vge_ldata.vge_tx_mbuf[idx]);
1521a07bd003SBill Paul 		sc->vge_ldata.vge_tx_mbuf[idx] = NULL;
1522a07bd003SBill Paul 		bus_dmamap_unload(sc->vge_ldata.vge_mtag,
1523a07bd003SBill Paul 		    sc->vge_ldata.vge_tx_dmamap[idx]);
1524a07bd003SBill Paul 		if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1525a07bd003SBill Paul 			ifp->if_collisions++;
1526a07bd003SBill Paul 		if (txstat & VGE_TDSTS_TXERR)
1527a07bd003SBill Paul 			ifp->if_oerrors++;
1528a07bd003SBill Paul 		else
1529a07bd003SBill Paul 			ifp->if_opackets++;
1530a07bd003SBill Paul 
1531a07bd003SBill Paul 		sc->vge_ldata.vge_tx_free++;
1532a07bd003SBill Paul 		VGE_TX_DESC_INC(idx);
1533a07bd003SBill Paul 	}
1534a07bd003SBill Paul 
1535a07bd003SBill Paul 	/* No changes made to the TX ring, so no flush needed */
1536a07bd003SBill Paul 
1537a07bd003SBill Paul 	if (idx != sc->vge_ldata.vge_tx_considx) {
1538a07bd003SBill Paul 		sc->vge_ldata.vge_tx_considx = idx;
1539a07bd003SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1540a07bd003SBill Paul 		ifp->if_timer = 0;
1541a07bd003SBill Paul 	}
1542a07bd003SBill Paul 
1543a07bd003SBill Paul 	/*
1544a07bd003SBill Paul 	 * If not all descriptors have been released reaped yet,
1545a07bd003SBill Paul 	 * reload the timer so that we will eventually get another
1546a07bd003SBill Paul 	 * interrupt that will cause us to re-enter this routine.
1547a07bd003SBill Paul 	 * This is done in case the transmitter has gone idle.
1548a07bd003SBill Paul 	 */
1549a07bd003SBill Paul 	if (sc->vge_ldata.vge_tx_free != VGE_TX_DESC_CNT) {
1550a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1551a07bd003SBill Paul 	}
1552a07bd003SBill Paul 
1553a07bd003SBill Paul 	return;
1554a07bd003SBill Paul }
1555a07bd003SBill Paul 
1556a07bd003SBill Paul static void
1557a07bd003SBill Paul vge_tick(xsc)
1558a07bd003SBill Paul 	void			*xsc;
1559a07bd003SBill Paul {
1560a07bd003SBill Paul 	struct vge_softc	*sc;
1561a07bd003SBill Paul 	struct ifnet		*ifp;
1562a07bd003SBill Paul 	struct mii_data		*mii;
1563a07bd003SBill Paul 
1564a07bd003SBill Paul 	sc = xsc;
1565a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1566a07bd003SBill Paul 	VGE_LOCK(sc);
1567a07bd003SBill Paul 	mii = device_get_softc(sc->vge_miibus);
1568a07bd003SBill Paul 
1569a07bd003SBill Paul 	mii_tick(mii);
1570a07bd003SBill Paul 	if (sc->vge_link) {
1571a07bd003SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE)) {
1572a07bd003SBill Paul 			sc->vge_link = 0;
1573a07bd003SBill Paul #ifdef LINK_STATE_UP
1574a07bd003SBill Paul 			sc->arpcom.ac_if.if_link_state = LINK_STATE_UP;
1575a07bd003SBill Paul 			rt_ifmsg(&(sc->arpcom.ac_if));
1576a07bd003SBill Paul #endif /* LINK_STATE_UP */
1577a07bd003SBill Paul 		}
1578a07bd003SBill Paul 	} else {
1579a07bd003SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1580a07bd003SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1581a07bd003SBill Paul 			sc->vge_link = 1;
1582a07bd003SBill Paul #ifdef LINK_STATE_DOWN
1583a07bd003SBill Paul 			sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN;
1584a07bd003SBill Paul 			rt_ifmsg(&(sc->arpcom.ac_if));
1585a07bd003SBill Paul #endif /* LINK_STATE_DOWN */
1586a07bd003SBill Paul #if __FreeBSD_version < 502114
1587a07bd003SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
1588a07bd003SBill Paul #else
1589a07bd003SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1590a07bd003SBill Paul #endif
1591a07bd003SBill Paul 				taskqueue_enqueue(taskqueue_swi,
1592a07bd003SBill Paul 				    &sc->vge_txtask);
1593a07bd003SBill Paul 		}
1594a07bd003SBill Paul 	}
1595a07bd003SBill Paul 
1596a07bd003SBill Paul 	VGE_UNLOCK(sc);
1597a07bd003SBill Paul 
1598a07bd003SBill Paul 	return;
1599a07bd003SBill Paul }
1600a07bd003SBill Paul 
1601a07bd003SBill Paul #ifdef DEVICE_POLLING
1602a07bd003SBill Paul static void
1603a07bd003SBill Paul vge_poll (struct ifnet *ifp, enum poll_cmd cmd, int count)
1604a07bd003SBill Paul {
1605a07bd003SBill Paul 	struct vge_softc *sc = ifp->if_softc;
1606a07bd003SBill Paul 
1607a07bd003SBill Paul 	VGE_LOCK(sc);
1608a07bd003SBill Paul #ifdef IFCAP_POLLING
1609a07bd003SBill Paul 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
1610a07bd003SBill Paul 		ether_poll_deregister(ifp);
1611a07bd003SBill Paul 		cmd = POLL_DEREGISTER;
1612a07bd003SBill Paul 	}
1613a07bd003SBill Paul #endif
1614a07bd003SBill Paul 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1615a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1616a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
1617a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1618a07bd003SBill Paul 		goto done;
1619a07bd003SBill Paul 	}
1620a07bd003SBill Paul 
1621a07bd003SBill Paul 	sc->rxcycles = count;
1622a07bd003SBill Paul 	vge_rxeof(sc);
1623a07bd003SBill Paul 	vge_txeof(sc);
1624a07bd003SBill Paul 
1625a07bd003SBill Paul #if __FreeBSD_version < 502114
1626a07bd003SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1627a07bd003SBill Paul #else
1628a07bd003SBill Paul 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1629a07bd003SBill Paul #endif
1630a07bd003SBill Paul 		taskqueue_enqueue(taskqueue_swi, &sc->vge_txtask);
1631a07bd003SBill Paul 
1632a07bd003SBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1633a07bd003SBill Paul 		u_int32_t       status;
1634a07bd003SBill Paul 		status = CSR_READ_4(sc, VGE_ISR);
1635a07bd003SBill Paul 		if (status == 0xFFFFFFFF)
1636a07bd003SBill Paul 			goto done;
1637a07bd003SBill Paul 		if (status)
1638a07bd003SBill Paul 			CSR_WRITE_4(sc, VGE_ISR, status);
1639a07bd003SBill Paul 
1640a07bd003SBill Paul 		/*
1641a07bd003SBill Paul 		 * XXX check behaviour on receiver stalls.
1642a07bd003SBill Paul 		 */
1643a07bd003SBill Paul 
1644a07bd003SBill Paul 		if (status & VGE_ISR_TXDMA_STALL ||
1645a07bd003SBill Paul 		    status & VGE_ISR_RXDMA_STALL)
1646a07bd003SBill Paul 			vge_init(sc);
1647a07bd003SBill Paul 
1648a07bd003SBill Paul 		if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1649a07bd003SBill Paul 			vge_rxeof(sc);
1650a07bd003SBill Paul 			ifp->if_ierrors++;
1651a07bd003SBill Paul 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1652a07bd003SBill Paul 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1653a07bd003SBill Paul 		}
1654a07bd003SBill Paul 	}
1655a07bd003SBill Paul done:
1656a07bd003SBill Paul 	VGE_UNLOCK(sc);
1657a07bd003SBill Paul }
1658a07bd003SBill Paul #endif /* DEVICE_POLLING */
1659a07bd003SBill Paul 
1660a07bd003SBill Paul static void
1661a07bd003SBill Paul vge_intr(arg)
1662a07bd003SBill Paul 	void			*arg;
1663a07bd003SBill Paul {
1664a07bd003SBill Paul 	struct vge_softc	*sc;
1665a07bd003SBill Paul 	struct ifnet		*ifp;
1666a07bd003SBill Paul 	u_int32_t		status;
1667a07bd003SBill Paul 
1668a07bd003SBill Paul 	sc = arg;
1669a07bd003SBill Paul 
1670a07bd003SBill Paul 	if (sc->suspended) {
1671a07bd003SBill Paul 		return;
1672a07bd003SBill Paul 	}
1673a07bd003SBill Paul 
1674a07bd003SBill Paul 	VGE_LOCK(sc);
1675a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
1676a07bd003SBill Paul 
1677a07bd003SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1678a07bd003SBill Paul 		VGE_UNLOCK(sc);
1679a07bd003SBill Paul 		return;
1680a07bd003SBill Paul 	}
1681a07bd003SBill Paul 
1682a07bd003SBill Paul #ifdef DEVICE_POLLING
1683a07bd003SBill Paul 	if  (ifp->if_flags & IFF_POLLING)
1684a07bd003SBill Paul 		goto done;
1685a07bd003SBill Paul 	if (
1686a07bd003SBill Paul #ifdef IFCAP_POLLING
1687a07bd003SBill Paul 	    (ifp->if_capenable & IFCAP_POLLING) &&
1688a07bd003SBill Paul #endif
1689a07bd003SBill Paul 	    ether_poll_register(vge_poll, ifp)) { /* ok, disable interrupts */
1690a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_IMR, 0);
1691a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1692a07bd003SBill Paul 		vge_poll(ifp, 0, 1);
1693a07bd003SBill Paul 		goto done;
1694a07bd003SBill Paul 	}
1695a07bd003SBill Paul 
1696a07bd003SBill Paul #endif /* DEVICE_POLLING */
1697a07bd003SBill Paul 
1698a07bd003SBill Paul 	/* Disable interrupts */
1699a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1700a07bd003SBill Paul 
1701a07bd003SBill Paul 	for (;;) {
1702a07bd003SBill Paul 
1703a07bd003SBill Paul 		status = CSR_READ_4(sc, VGE_ISR);
1704a07bd003SBill Paul 		/* If the card has gone away the read returns 0xffff. */
1705a07bd003SBill Paul 		if (status == 0xFFFFFFFF)
1706a07bd003SBill Paul 			break;
1707a07bd003SBill Paul 
1708a07bd003SBill Paul 		if (status)
1709a07bd003SBill Paul 			CSR_WRITE_4(sc, VGE_ISR, status);
1710a07bd003SBill Paul 
1711a07bd003SBill Paul 		if ((status & VGE_INTRS) == 0)
1712a07bd003SBill Paul 			break;
1713a07bd003SBill Paul 
1714a07bd003SBill Paul 		if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1715a07bd003SBill Paul 			vge_rxeof(sc);
1716a07bd003SBill Paul 
1717a07bd003SBill Paul 		if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1718a07bd003SBill Paul 			vge_rxeof(sc);
1719a07bd003SBill Paul 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1720a07bd003SBill Paul 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1721a07bd003SBill Paul 		}
1722a07bd003SBill Paul 
1723a07bd003SBill Paul 		if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1724a07bd003SBill Paul 			vge_txeof(sc);
1725a07bd003SBill Paul 
1726a07bd003SBill Paul 		if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
1727a07bd003SBill Paul 			vge_init(sc);
1728a07bd003SBill Paul 
1729a07bd003SBill Paul 		if (status & VGE_ISR_LINKSTS)
1730a07bd003SBill Paul 			vge_tick(sc);
1731a07bd003SBill Paul 	}
1732a07bd003SBill Paul 
1733a07bd003SBill Paul 	/* Re-enable interrupts */
1734a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1735a07bd003SBill Paul 
1736a07bd003SBill Paul #ifdef DEVICE_POLLING
1737a07bd003SBill Paul done:
1738a07bd003SBill Paul #endif
1739a07bd003SBill Paul 	VGE_UNLOCK(sc);
1740a07bd003SBill Paul 
1741a07bd003SBill Paul #if __FreeBSD_version < 502114
1742a07bd003SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1743a07bd003SBill Paul #else
1744a07bd003SBill Paul 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1745a07bd003SBill Paul #endif
1746a07bd003SBill Paul 		taskqueue_enqueue(taskqueue_swi, &sc->vge_txtask);
1747a07bd003SBill Paul 
1748a07bd003SBill Paul 	return;
1749a07bd003SBill Paul }
1750a07bd003SBill Paul 
1751a07bd003SBill Paul static int
1752a07bd003SBill Paul vge_encap(sc, m_head, idx)
1753a07bd003SBill Paul 	struct vge_softc	*sc;
1754a07bd003SBill Paul 	struct mbuf		*m_head;
1755a07bd003SBill Paul 	int			idx;
1756a07bd003SBill Paul {
1757a07bd003SBill Paul 	struct mbuf		*m_new = NULL;
1758a07bd003SBill Paul 	struct vge_dmaload_arg	arg;
1759a07bd003SBill Paul 	bus_dmamap_t		map;
1760a07bd003SBill Paul 	int			error;
1761a07bd003SBill Paul 	struct m_tag		*mtag;
1762a07bd003SBill Paul 
1763a07bd003SBill Paul 	if (sc->vge_ldata.vge_tx_free <= 2)
1764a07bd003SBill Paul 		return (EFBIG);
1765a07bd003SBill Paul 
1766a07bd003SBill Paul 	arg.vge_flags = 0;
1767a07bd003SBill Paul 
1768a07bd003SBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_IP)
1769a07bd003SBill Paul 		arg.vge_flags |= VGE_TDCTL_IPCSUM;
1770a07bd003SBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
1771a07bd003SBill Paul 		arg.vge_flags |= VGE_TDCTL_TCPCSUM;
1772a07bd003SBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
1773a07bd003SBill Paul 		arg.vge_flags |= VGE_TDCTL_UDPCSUM;
1774a07bd003SBill Paul 
1775a07bd003SBill Paul 	arg.sc = sc;
1776a07bd003SBill Paul 	arg.vge_idx = idx;
1777a07bd003SBill Paul 	arg.vge_m0 = m_head;
1778a07bd003SBill Paul 	arg.vge_maxsegs = VGE_TX_FRAGS;
1779a07bd003SBill Paul 
1780a07bd003SBill Paul 	map = sc->vge_ldata.vge_tx_dmamap[idx];
1781a07bd003SBill Paul 	error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map,
1782a07bd003SBill Paul 	    m_head, vge_dma_map_tx_desc, &arg, BUS_DMA_NOWAIT);
1783a07bd003SBill Paul 
1784a07bd003SBill Paul 	if (error && error != EFBIG) {
1785a07bd003SBill Paul 		printf("vge%d: can't map mbuf (error %d)\n",
1786a07bd003SBill Paul 		    sc->vge_unit, error);
1787a07bd003SBill Paul 		return (ENOBUFS);
1788a07bd003SBill Paul 	}
1789a07bd003SBill Paul 
1790a07bd003SBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
1791a07bd003SBill Paul 
1792a07bd003SBill Paul 	if (error || arg.vge_maxsegs == 0) {
1793a07bd003SBill Paul 		m_new = m_defrag(m_head, M_DONTWAIT);
1794a07bd003SBill Paul 		if (m_new == NULL)
1795a07bd003SBill Paul 			return (1);
1796a07bd003SBill Paul 		else
1797a07bd003SBill Paul 			m_head = m_new;
1798a07bd003SBill Paul 
1799a07bd003SBill Paul 		arg.sc = sc;
1800a07bd003SBill Paul 		arg.vge_m0 = m_head;
1801a07bd003SBill Paul 		arg.vge_idx = idx;
1802a07bd003SBill Paul 		arg.vge_maxsegs = 1;
1803a07bd003SBill Paul 
1804a07bd003SBill Paul 		error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map,
1805a07bd003SBill Paul 		    m_head, vge_dma_map_tx_desc, &arg, BUS_DMA_NOWAIT);
1806a07bd003SBill Paul 		if (error) {
1807a07bd003SBill Paul 			printf("vge%d: can't map mbuf (error %d)\n",
1808a07bd003SBill Paul 			    sc->vge_unit, error);
1809a07bd003SBill Paul 			return (EFBIG);
1810a07bd003SBill Paul 		}
1811a07bd003SBill Paul 	}
1812a07bd003SBill Paul 
1813a07bd003SBill Paul 	sc->vge_ldata.vge_tx_mbuf[idx] = m_head;
1814a07bd003SBill Paul 	sc->vge_ldata.vge_tx_free--;
1815a07bd003SBill Paul 
1816a07bd003SBill Paul 	/*
1817a07bd003SBill Paul 	 * Set up hardware VLAN tagging.
1818a07bd003SBill Paul 	 */
1819a07bd003SBill Paul 
1820a07bd003SBill Paul 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
1821a07bd003SBill Paul 	if (mtag != NULL)
1822a07bd003SBill Paul 		sc->vge_ldata.vge_tx_list[idx].vge_ctl |=
1823a07bd003SBill Paul 		    htole32(htons(VLAN_TAG_VALUE(mtag)) | VGE_TDCTL_VTAG);
1824a07bd003SBill Paul 
1825a07bd003SBill Paul 	sc->vge_ldata.vge_tx_list[idx].vge_sts |= htole32(VGE_TDSTS_OWN);
1826a07bd003SBill Paul 
1827a07bd003SBill Paul 	return (0);
1828a07bd003SBill Paul }
1829a07bd003SBill Paul 
1830a07bd003SBill Paul static void
1831a07bd003SBill Paul vge_tx_task(arg, npending)
1832a07bd003SBill Paul 	void			*arg;
1833a07bd003SBill Paul 	int			npending;
1834a07bd003SBill Paul {
1835a07bd003SBill Paul 	struct ifnet		*ifp;
1836a07bd003SBill Paul 
1837a07bd003SBill Paul 	ifp = arg;
1838a07bd003SBill Paul 	vge_start(ifp);
1839a07bd003SBill Paul 
1840a07bd003SBill Paul 	return;
1841a07bd003SBill Paul }
1842a07bd003SBill Paul 
1843a07bd003SBill Paul /*
1844a07bd003SBill Paul  * Main transmit routine.
1845a07bd003SBill Paul  */
1846a07bd003SBill Paul 
1847a07bd003SBill Paul static void
1848a07bd003SBill Paul vge_start(ifp)
1849a07bd003SBill Paul 	struct ifnet		*ifp;
1850a07bd003SBill Paul {
1851a07bd003SBill Paul 	struct vge_softc	*sc;
1852a07bd003SBill Paul 	struct mbuf		*m_head = NULL;
1853a07bd003SBill Paul 	int			idx, pidx = 0;
1854a07bd003SBill Paul 
1855a07bd003SBill Paul 	sc = ifp->if_softc;
1856a07bd003SBill Paul 	VGE_LOCK(sc);
1857a07bd003SBill Paul 
1858a07bd003SBill Paul 	if (!sc->vge_link || ifp->if_flags & IFF_OACTIVE) {
1859a07bd003SBill Paul 		VGE_UNLOCK(sc);
1860a07bd003SBill Paul 		return;
1861a07bd003SBill Paul 	}
1862a07bd003SBill Paul 
1863a07bd003SBill Paul #if __FreeBSD_version < 502114
1864a07bd003SBill Paul 	if (ifp->if_snd.ifq_head == NULL) {
1865a07bd003SBill Paul #else
1866a07bd003SBill Paul 	if (IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1867a07bd003SBill Paul #endif
1868a07bd003SBill Paul 		VGE_UNLOCK(sc);
1869a07bd003SBill Paul 		return;
1870a07bd003SBill Paul 	}
1871a07bd003SBill Paul 
1872a07bd003SBill Paul 	idx = sc->vge_ldata.vge_tx_prodidx;
1873a07bd003SBill Paul 
1874a07bd003SBill Paul 	pidx = idx - 1;
1875a07bd003SBill Paul 	if (pidx < 0)
1876a07bd003SBill Paul 		pidx = VGE_TX_DESC_CNT - 1;
1877a07bd003SBill Paul 
1878a07bd003SBill Paul 
1879a07bd003SBill Paul 	while (sc->vge_ldata.vge_tx_mbuf[idx] == NULL) {
1880a07bd003SBill Paul #if __FreeBSD_version < 502114
1881a07bd003SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1882a07bd003SBill Paul #else
1883a07bd003SBill Paul 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1884a07bd003SBill Paul #endif
1885a07bd003SBill Paul 		if (m_head == NULL)
1886a07bd003SBill Paul 			break;
1887a07bd003SBill Paul 
1888a07bd003SBill Paul 		if (vge_encap(sc, m_head, idx)) {
1889a07bd003SBill Paul #if __FreeBSD_version >= 502114
1890a07bd003SBill Paul 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1891a07bd003SBill Paul #else
1892a07bd003SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1893a07bd003SBill Paul #endif
1894a07bd003SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1895a07bd003SBill Paul 			break;
1896a07bd003SBill Paul 		}
1897a07bd003SBill Paul 
1898a07bd003SBill Paul 		sc->vge_ldata.vge_tx_list[pidx].vge_frag[0].vge_buflen |=
1899a07bd003SBill Paul 		    htole16(VGE_TXDESC_Q);
1900a07bd003SBill Paul 
1901a07bd003SBill Paul 		pidx = idx;
1902a07bd003SBill Paul 		VGE_TX_DESC_INC(idx);
1903a07bd003SBill Paul 
1904a07bd003SBill Paul 		/*
1905a07bd003SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1906a07bd003SBill Paul 		 * to him.
1907a07bd003SBill Paul 		 */
1908a07bd003SBill Paul 		BPF_MTAP(ifp, m_head);
1909a07bd003SBill Paul 	}
1910a07bd003SBill Paul 
1911a07bd003SBill Paul 	if (idx == sc->vge_ldata.vge_tx_prodidx) {
1912a07bd003SBill Paul 		VGE_UNLOCK(sc);
1913a07bd003SBill Paul 		return;
1914a07bd003SBill Paul 	}
1915a07bd003SBill Paul 
1916a07bd003SBill Paul 	/* Flush the TX descriptors */
1917a07bd003SBill Paul 
1918a07bd003SBill Paul 	bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag,
1919a07bd003SBill Paul 	    sc->vge_ldata.vge_tx_list_map,
1920a07bd003SBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1921a07bd003SBill Paul 
1922a07bd003SBill Paul 	/* Issue a transmit command. */
1923a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1924a07bd003SBill Paul 
1925a07bd003SBill Paul 	sc->vge_ldata.vge_tx_prodidx = idx;
1926a07bd003SBill Paul 
1927a07bd003SBill Paul 	/*
1928a07bd003SBill Paul 	 * Use the countdown timer for interrupt moderation.
1929a07bd003SBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
1930a07bd003SBill Paul 	 * countdown timer, which will begin counting until it hits
1931a07bd003SBill Paul 	 * the value in the SSTIMER register, and then trigger an
1932a07bd003SBill Paul 	 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1933a07bd003SBill Paul 	 * the timer count is reloaded. Only when the transmitter
1934a07bd003SBill Paul 	 * is idle will the timer hit 0 and an interrupt fire.
1935a07bd003SBill Paul 	 */
1936a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1937a07bd003SBill Paul 
1938a07bd003SBill Paul 	VGE_UNLOCK(sc);
1939a07bd003SBill Paul 
1940a07bd003SBill Paul 	/*
1941a07bd003SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1942a07bd003SBill Paul 	 */
1943a07bd003SBill Paul 	ifp->if_timer = 5;
1944a07bd003SBill Paul 
1945a07bd003SBill Paul 	return;
1946a07bd003SBill Paul }
1947a07bd003SBill Paul 
1948a07bd003SBill Paul static void
1949a07bd003SBill Paul vge_init(xsc)
1950a07bd003SBill Paul 	void			*xsc;
1951a07bd003SBill Paul {
1952a07bd003SBill Paul 	struct vge_softc	*sc = xsc;
1953a07bd003SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1954a07bd003SBill Paul 	struct mii_data		*mii;
1955a07bd003SBill Paul 	int			i;
1956a07bd003SBill Paul 
1957a07bd003SBill Paul 	VGE_LOCK(sc);
1958a07bd003SBill Paul 	mii = device_get_softc(sc->vge_miibus);
1959a07bd003SBill Paul 
1960a07bd003SBill Paul 	/*
1961a07bd003SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1962a07bd003SBill Paul 	 */
1963a07bd003SBill Paul 	vge_stop(sc);
1964a07bd003SBill Paul 	vge_reset(sc);
1965a07bd003SBill Paul 
1966a07bd003SBill Paul 	/*
1967a07bd003SBill Paul 	 * Initialize the RX and TX descriptors and mbufs.
1968a07bd003SBill Paul 	 */
1969a07bd003SBill Paul 
1970a07bd003SBill Paul 	vge_rx_list_init(sc);
1971a07bd003SBill Paul 	vge_tx_list_init(sc);
1972a07bd003SBill Paul 
1973a07bd003SBill Paul 	/* Set our station address */
1974a07bd003SBill Paul 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1975a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1976a07bd003SBill Paul 
1977a07bd003SBill Paul 	/*
1978a07bd003SBill Paul 	 * Set receive FIFO threshold. Also allow transmission and
1979a07bd003SBill Paul 	 * reception of VLAN tagged frames.
1980a07bd003SBill Paul 	 */
1981a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1982a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1983a07bd003SBill Paul 
1984a07bd003SBill Paul 	/* Set DMA burst length */
1985a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1986a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1987a07bd003SBill Paul 
1988a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1989a07bd003SBill Paul 
1990a07bd003SBill Paul 	/* Set collision backoff algorithm */
1991a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1992a07bd003SBill Paul 	    VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1993a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1994a07bd003SBill Paul 
1995a07bd003SBill Paul 	/* Disable LPSEL field in priority resolution */
1996a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1997a07bd003SBill Paul 
1998a07bd003SBill Paul 	/*
1999a07bd003SBill Paul 	 * Load the addresses of the DMA queues into the chip.
2000a07bd003SBill Paul 	 * Note that we only use one transmit queue.
2001a07bd003SBill Paul 	 */
2002a07bd003SBill Paul 
2003a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
2004a07bd003SBill Paul 	    VGE_ADDR_LO(sc->vge_ldata.vge_tx_list_addr));
2005a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
2006a07bd003SBill Paul 
2007a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
2008a07bd003SBill Paul 	    VGE_ADDR_LO(sc->vge_ldata.vge_rx_list_addr));
2009a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
2010a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
2011a07bd003SBill Paul 
2012a07bd003SBill Paul 	/* Enable and wake up the RX descriptor queue */
2013a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
2014a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
2015a07bd003SBill Paul 
2016a07bd003SBill Paul 	/* Enable the TX descriptor queue */
2017a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
2018a07bd003SBill Paul 
2019a07bd003SBill Paul 	/* Set up the receive filter -- allow large frames for VLANs. */
2020a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
2021a07bd003SBill Paul 
2022a07bd003SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
2023a07bd003SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
2024a07bd003SBill Paul 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
2025a07bd003SBill Paul 	}
2026a07bd003SBill Paul 
2027a07bd003SBill Paul 	/* Set capture broadcast bit to capture broadcast frames. */
2028a07bd003SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
2029a07bd003SBill Paul 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
2030a07bd003SBill Paul 	}
2031a07bd003SBill Paul 
2032a07bd003SBill Paul 	/* Set multicast bit to capture multicast frames. */
2033a07bd003SBill Paul 	if (ifp->if_flags & IFF_MULTICAST) {
2034a07bd003SBill Paul 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
2035a07bd003SBill Paul 	}
2036a07bd003SBill Paul 
2037a07bd003SBill Paul 	/* Init the cam filter. */
2038a07bd003SBill Paul 	vge_cam_clear(sc);
2039a07bd003SBill Paul 
2040a07bd003SBill Paul 	/* Init the multicast filter. */
2041a07bd003SBill Paul 	vge_setmulti(sc);
2042a07bd003SBill Paul 
2043a07bd003SBill Paul 	/* Enable flow control */
2044a07bd003SBill Paul 
2045a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
2046a07bd003SBill Paul 
2047a07bd003SBill Paul 	/* Enable jumbo frame reception (if desired) */
2048a07bd003SBill Paul 
2049a07bd003SBill Paul 	/* Start the MAC. */
2050a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
2051a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
2052a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS0,
2053a07bd003SBill Paul 	    VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
2054a07bd003SBill Paul 
2055a07bd003SBill Paul 	/*
2056a07bd003SBill Paul 	 * Configure one-shot timer for microsecond
2057a07bd003SBill Paul 	 * resulution and load it for 500 usecs.
2058a07bd003SBill Paul 	 */
2059a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
2060a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_SSTIMER, 400);
2061a07bd003SBill Paul 
2062a07bd003SBill Paul 	/*
2063a07bd003SBill Paul 	 * Configure interrupt moderation for receive. Enable
2064a07bd003SBill Paul 	 * the holdoff counter and load it, and set the RX
2065a07bd003SBill Paul 	 * suppression count to the number of descriptors we
2066a07bd003SBill Paul 	 * want to allow before triggering an interrupt.
2067a07bd003SBill Paul 	 * The holdoff timer is in units of 20 usecs.
2068a07bd003SBill Paul 	 */
2069a07bd003SBill Paul 
2070a07bd003SBill Paul #ifdef notyet
2071a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
2072a07bd003SBill Paul 	/* Select the interrupt holdoff timer page. */
2073a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
2074a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
2075a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
2076a07bd003SBill Paul 
2077a07bd003SBill Paul 	/* Enable use of the holdoff timer. */
2078a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
2079a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
2080a07bd003SBill Paul 
2081a07bd003SBill Paul 	/* Select the RX suppression threshold page. */
2082a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
2083a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
2084a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
2085a07bd003SBill Paul 
2086a07bd003SBill Paul 	/* Restore the page select bits. */
2087a07bd003SBill Paul 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
2088a07bd003SBill Paul 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
2089a07bd003SBill Paul #endif
2090a07bd003SBill Paul 
2091a07bd003SBill Paul #ifdef DEVICE_POLLING
2092a07bd003SBill Paul 	/*
2093a07bd003SBill Paul 	 * Disable interrupts if we are polling.
2094a07bd003SBill Paul 	 */
2095a07bd003SBill Paul 	if (ifp->if_flags & IFF_POLLING) {
2096a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_IMR, 0);
2097a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2098a07bd003SBill Paul 	} else	/* otherwise ... */
2099a07bd003SBill Paul #endif /* DEVICE_POLLING */
2100a07bd003SBill Paul 	{
2101a07bd003SBill Paul 	/*
2102a07bd003SBill Paul 	 * Enable interrupts.
2103a07bd003SBill Paul 	 */
2104a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
2105a07bd003SBill Paul 		CSR_WRITE_4(sc, VGE_ISR, 0);
2106a07bd003SBill Paul 		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2107a07bd003SBill Paul 	}
2108a07bd003SBill Paul 
2109a07bd003SBill Paul 	mii_mediachg(mii);
2110a07bd003SBill Paul 
2111a07bd003SBill Paul 	ifp->if_flags |= IFF_RUNNING;
2112a07bd003SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
2113a07bd003SBill Paul 
2114a07bd003SBill Paul 	sc->vge_if_flags = 0;
2115a07bd003SBill Paul 	sc->vge_link = 0;
2116a07bd003SBill Paul 
2117a07bd003SBill Paul 	VGE_UNLOCK(sc);
2118a07bd003SBill Paul 
2119a07bd003SBill Paul 	return;
2120a07bd003SBill Paul }
2121a07bd003SBill Paul 
2122a07bd003SBill Paul /*
2123a07bd003SBill Paul  * Set media options.
2124a07bd003SBill Paul  */
2125a07bd003SBill Paul static int
2126a07bd003SBill Paul vge_ifmedia_upd(ifp)
2127a07bd003SBill Paul 	struct ifnet		*ifp;
2128a07bd003SBill Paul {
2129a07bd003SBill Paul 	struct vge_softc	*sc;
2130a07bd003SBill Paul 	struct mii_data		*mii;
2131a07bd003SBill Paul 
2132a07bd003SBill Paul 	sc = ifp->if_softc;
2133a07bd003SBill Paul 	mii = device_get_softc(sc->vge_miibus);
2134a07bd003SBill Paul 	mii_mediachg(mii);
2135a07bd003SBill Paul 
2136a07bd003SBill Paul 	return (0);
2137a07bd003SBill Paul }
2138a07bd003SBill Paul 
2139a07bd003SBill Paul /*
2140a07bd003SBill Paul  * Report current media status.
2141a07bd003SBill Paul  */
2142a07bd003SBill Paul static void
2143a07bd003SBill Paul vge_ifmedia_sts(ifp, ifmr)
2144a07bd003SBill Paul 	struct ifnet		*ifp;
2145a07bd003SBill Paul 	struct ifmediareq	*ifmr;
2146a07bd003SBill Paul {
2147a07bd003SBill Paul 	struct vge_softc	*sc;
2148a07bd003SBill Paul 	struct mii_data		*mii;
2149a07bd003SBill Paul 
2150a07bd003SBill Paul 	sc = ifp->if_softc;
2151a07bd003SBill Paul 	mii = device_get_softc(sc->vge_miibus);
2152a07bd003SBill Paul 
2153a07bd003SBill Paul 	mii_pollstat(mii);
2154a07bd003SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2155a07bd003SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2156a07bd003SBill Paul 
2157a07bd003SBill Paul 	return;
2158a07bd003SBill Paul }
2159a07bd003SBill Paul 
2160a07bd003SBill Paul static void
2161a07bd003SBill Paul vge_miibus_statchg(dev)
2162a07bd003SBill Paul 	device_t		dev;
2163a07bd003SBill Paul {
2164a07bd003SBill Paul 	struct vge_softc	*sc;
2165a07bd003SBill Paul 	struct mii_data		*mii;
2166a07bd003SBill Paul 	struct ifmedia_entry	*ife;
2167a07bd003SBill Paul 
2168a07bd003SBill Paul 	sc = device_get_softc(dev);
2169a07bd003SBill Paul 	mii = device_get_softc(sc->vge_miibus);
2170a07bd003SBill Paul 	ife = mii->mii_media.ifm_cur;
2171a07bd003SBill Paul 
2172a07bd003SBill Paul 	/*
2173a07bd003SBill Paul 	 * If the user manually selects a media mode, we need to turn
2174a07bd003SBill Paul 	 * on the forced MAC mode bit in the DIAGCTL register. If the
2175a07bd003SBill Paul 	 * user happens to choose a full duplex mode, we also need to
2176a07bd003SBill Paul 	 * set the 'force full duplex' bit. This applies only to
2177a07bd003SBill Paul 	 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
2178a07bd003SBill Paul 	 * mode is disabled, and in 1000baseT mode, full duplex is
2179a07bd003SBill Paul 	 * always implied, so we turn on the forced mode bit but leave
2180a07bd003SBill Paul 	 * the FDX bit cleared.
2181a07bd003SBill Paul 	 */
2182a07bd003SBill Paul 
2183a07bd003SBill Paul 	switch (IFM_SUBTYPE(ife->ifm_media)) {
2184a07bd003SBill Paul 	case IFM_AUTO:
2185a07bd003SBill Paul 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2186a07bd003SBill Paul 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2187a07bd003SBill Paul 		break;
2188a07bd003SBill Paul 	case IFM_1000_T:
2189a07bd003SBill Paul 		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2190a07bd003SBill Paul 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2191a07bd003SBill Paul 		break;
2192a07bd003SBill Paul 	case IFM_100_TX:
2193a07bd003SBill Paul 	case IFM_10_T:
2194a07bd003SBill Paul 		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2195a07bd003SBill Paul 		if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2196a07bd003SBill Paul 			CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2197a07bd003SBill Paul 		} else {
2198a07bd003SBill Paul 			CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2199a07bd003SBill Paul 		}
2200a07bd003SBill Paul 		break;
2201a07bd003SBill Paul 	default:
2202a07bd003SBill Paul 		device_printf(dev, "unknown media type: %x\n",
2203a07bd003SBill Paul 		    IFM_SUBTYPE(ife->ifm_media));
2204a07bd003SBill Paul 		break;
2205a07bd003SBill Paul 	}
2206a07bd003SBill Paul 
2207a07bd003SBill Paul 	return;
2208a07bd003SBill Paul }
2209a07bd003SBill Paul 
2210a07bd003SBill Paul static int
2211a07bd003SBill Paul vge_ioctl(ifp, command, data)
2212a07bd003SBill Paul 	struct ifnet		*ifp;
2213a07bd003SBill Paul 	u_long			command;
2214a07bd003SBill Paul 	caddr_t			data;
2215a07bd003SBill Paul {
2216a07bd003SBill Paul 	struct vge_softc	*sc = ifp->if_softc;
2217a07bd003SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2218a07bd003SBill Paul 	struct mii_data		*mii;
2219a07bd003SBill Paul 	int			error = 0;
2220a07bd003SBill Paul 
2221a07bd003SBill Paul 	switch (command) {
2222a07bd003SBill Paul 	case SIOCSIFMTU:
2223a07bd003SBill Paul 		if (ifr->ifr_mtu > VGE_JUMBO_MTU)
2224a07bd003SBill Paul 			error = EINVAL;
2225a07bd003SBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2226a07bd003SBill Paul 		break;
2227a07bd003SBill Paul 	case SIOCSIFFLAGS:
2228a07bd003SBill Paul 		if (ifp->if_flags & IFF_UP) {
2229a07bd003SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
2230a07bd003SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
2231a07bd003SBill Paul 			    !(sc->vge_if_flags & IFF_PROMISC)) {
2232a07bd003SBill Paul 				CSR_SETBIT_1(sc, VGE_RXCTL,
2233a07bd003SBill Paul 				    VGE_RXCTL_RX_PROMISC);
2234a07bd003SBill Paul 				vge_setmulti(sc);
2235a07bd003SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
2236a07bd003SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
2237a07bd003SBill Paul 			    sc->vge_if_flags & IFF_PROMISC) {
2238a07bd003SBill Paul 				CSR_CLRBIT_1(sc, VGE_RXCTL,
2239a07bd003SBill Paul 				    VGE_RXCTL_RX_PROMISC);
2240a07bd003SBill Paul 				vge_setmulti(sc);
2241a07bd003SBill Paul                         } else
2242a07bd003SBill Paul 				vge_init(sc);
2243a07bd003SBill Paul 		} else {
2244a07bd003SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
2245a07bd003SBill Paul 				vge_stop(sc);
2246a07bd003SBill Paul 		}
2247a07bd003SBill Paul 		sc->vge_if_flags = ifp->if_flags;
2248a07bd003SBill Paul 		break;
2249a07bd003SBill Paul 	case SIOCADDMULTI:
2250a07bd003SBill Paul 	case SIOCDELMULTI:
2251a07bd003SBill Paul 		vge_setmulti(sc);
2252a07bd003SBill Paul 		break;
2253a07bd003SBill Paul 	case SIOCGIFMEDIA:
2254a07bd003SBill Paul 	case SIOCSIFMEDIA:
2255a07bd003SBill Paul 		mii = device_get_softc(sc->vge_miibus);
2256a07bd003SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2257a07bd003SBill Paul 		break;
2258a07bd003SBill Paul 	case SIOCSIFCAP:
2259a07bd003SBill Paul #ifdef IFCAP_POLLING
2260a07bd003SBill Paul 		ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_POLLING);
2261a07bd003SBill Paul #else
2262a07bd003SBill Paul 		ifp->if_capenable &= ~(IFCAP_HWCSUM);
2263a07bd003SBill Paul #endif
2264a07bd003SBill Paul 		ifp->if_capenable |=
2265a07bd003SBill Paul #ifdef IFCAP_POLLING
2266a07bd003SBill Paul 		    ifr->ifr_reqcap & (IFCAP_HWCSUM | IFCAP_POLLING);
2267a07bd003SBill Paul #else
2268a07bd003SBill Paul 		    ifr->ifr_reqcap & (IFCAP_HWCSUM);
2269a07bd003SBill Paul #endif
2270a07bd003SBill Paul 		if (ifp->if_capenable & IFCAP_TXCSUM)
2271a07bd003SBill Paul 			ifp->if_hwassist = VGE_CSUM_FEATURES;
2272a07bd003SBill Paul 		else
2273a07bd003SBill Paul 			ifp->if_hwassist = 0;
2274a07bd003SBill Paul 		if (ifp->if_flags & IFF_RUNNING)
2275a07bd003SBill Paul 			vge_init(sc);
2276a07bd003SBill Paul 		break;
2277a07bd003SBill Paul 	default:
2278a07bd003SBill Paul 		error = ether_ioctl(ifp, command, data);
2279a07bd003SBill Paul 		break;
2280a07bd003SBill Paul 	}
2281a07bd003SBill Paul 
2282a07bd003SBill Paul 	return (error);
2283a07bd003SBill Paul }
2284a07bd003SBill Paul 
2285a07bd003SBill Paul static void
2286a07bd003SBill Paul vge_watchdog(ifp)
2287a07bd003SBill Paul 	struct ifnet		*ifp;
2288a07bd003SBill Paul {
2289a07bd003SBill Paul 	struct vge_softc		*sc;
2290a07bd003SBill Paul 
2291a07bd003SBill Paul 	sc = ifp->if_softc;
2292a07bd003SBill Paul 	VGE_LOCK(sc);
2293a07bd003SBill Paul 	printf("vge%d: watchdog timeout\n", sc->vge_unit);
2294a07bd003SBill Paul 	ifp->if_oerrors++;
2295a07bd003SBill Paul 
2296a07bd003SBill Paul 	vge_txeof(sc);
2297a07bd003SBill Paul 	vge_rxeof(sc);
2298a07bd003SBill Paul 
2299a07bd003SBill Paul 	vge_init(sc);
2300a07bd003SBill Paul 
2301a07bd003SBill Paul 	VGE_UNLOCK(sc);
2302a07bd003SBill Paul 
2303a07bd003SBill Paul 	return;
2304a07bd003SBill Paul }
2305a07bd003SBill Paul 
2306a07bd003SBill Paul /*
2307a07bd003SBill Paul  * Stop the adapter and free any mbufs allocated to the
2308a07bd003SBill Paul  * RX and TX lists.
2309a07bd003SBill Paul  */
2310a07bd003SBill Paul static void
2311a07bd003SBill Paul vge_stop(sc)
2312a07bd003SBill Paul 	struct vge_softc		*sc;
2313a07bd003SBill Paul {
2314a07bd003SBill Paul 	register int		i;
2315a07bd003SBill Paul 	struct ifnet		*ifp;
2316a07bd003SBill Paul 
2317a07bd003SBill Paul 	VGE_LOCK(sc);
2318a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
2319a07bd003SBill Paul 	ifp->if_timer = 0;
2320a07bd003SBill Paul 
2321a07bd003SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2322a07bd003SBill Paul #ifdef DEVICE_POLLING
2323a07bd003SBill Paul 	ether_poll_deregister(ifp);
2324a07bd003SBill Paul #endif /* DEVICE_POLLING */
2325a07bd003SBill Paul 
2326a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2327a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2328a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2329a07bd003SBill Paul 	CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2330a07bd003SBill Paul 	CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2331a07bd003SBill Paul 	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2332a07bd003SBill Paul 
2333a07bd003SBill Paul 	if (sc->vge_head != NULL) {
2334a07bd003SBill Paul 		m_freem(sc->vge_head);
2335a07bd003SBill Paul 		sc->vge_head = sc->vge_tail = NULL;
2336a07bd003SBill Paul 	}
2337a07bd003SBill Paul 
2338a07bd003SBill Paul 	/* Free the TX list buffers. */
2339a07bd003SBill Paul 
2340a07bd003SBill Paul 	for (i = 0; i < VGE_TX_DESC_CNT; i++) {
2341a07bd003SBill Paul 		if (sc->vge_ldata.vge_tx_mbuf[i] != NULL) {
2342a07bd003SBill Paul 			bus_dmamap_unload(sc->vge_ldata.vge_mtag,
2343a07bd003SBill Paul 			    sc->vge_ldata.vge_tx_dmamap[i]);
2344a07bd003SBill Paul 			m_freem(sc->vge_ldata.vge_tx_mbuf[i]);
2345a07bd003SBill Paul 			sc->vge_ldata.vge_tx_mbuf[i] = NULL;
2346a07bd003SBill Paul 		}
2347a07bd003SBill Paul 	}
2348a07bd003SBill Paul 
2349a07bd003SBill Paul 	/* Free the RX list buffers. */
2350a07bd003SBill Paul 
2351a07bd003SBill Paul 	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
2352a07bd003SBill Paul 		if (sc->vge_ldata.vge_rx_mbuf[i] != NULL) {
2353a07bd003SBill Paul 			bus_dmamap_unload(sc->vge_ldata.vge_mtag,
2354a07bd003SBill Paul 			    sc->vge_ldata.vge_rx_dmamap[i]);
2355a07bd003SBill Paul 			m_freem(sc->vge_ldata.vge_rx_mbuf[i]);
2356a07bd003SBill Paul 			sc->vge_ldata.vge_rx_mbuf[i] = NULL;
2357a07bd003SBill Paul 		}
2358a07bd003SBill Paul 	}
2359a07bd003SBill Paul 
2360a07bd003SBill Paul 	VGE_UNLOCK(sc);
2361a07bd003SBill Paul 
2362a07bd003SBill Paul 	return;
2363a07bd003SBill Paul }
2364a07bd003SBill Paul 
2365a07bd003SBill Paul /*
2366a07bd003SBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2367a07bd003SBill Paul  * settings in case the BIOS doesn't restore them properly on
2368a07bd003SBill Paul  * resume.
2369a07bd003SBill Paul  */
2370a07bd003SBill Paul static int
2371a07bd003SBill Paul vge_suspend(dev)
2372a07bd003SBill Paul 	device_t		dev;
2373a07bd003SBill Paul {
2374a07bd003SBill Paul 	struct vge_softc	*sc;
2375a07bd003SBill Paul 	int			i;
2376a07bd003SBill Paul 
2377a07bd003SBill Paul 	sc = device_get_softc(dev);
2378a07bd003SBill Paul 
2379a07bd003SBill Paul 	vge_stop(sc);
2380a07bd003SBill Paul 
2381a07bd003SBill Paul         for (i = 0; i < 5; i++)
2382a07bd003SBill Paul 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2383a07bd003SBill Paul 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2384a07bd003SBill Paul 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2385a07bd003SBill Paul 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2386a07bd003SBill Paul 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2387a07bd003SBill Paul 
2388a07bd003SBill Paul 	sc->suspended = 1;
2389a07bd003SBill Paul 
2390a07bd003SBill Paul 	return (0);
2391a07bd003SBill Paul }
2392a07bd003SBill Paul 
2393a07bd003SBill Paul /*
2394a07bd003SBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2395a07bd003SBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2396a07bd003SBill Paul  * appropriate.
2397a07bd003SBill Paul  */
2398a07bd003SBill Paul static int
2399a07bd003SBill Paul vge_resume(dev)
2400a07bd003SBill Paul 	device_t		dev;
2401a07bd003SBill Paul {
2402a07bd003SBill Paul 	struct vge_softc	*sc;
2403a07bd003SBill Paul 	struct ifnet		*ifp;
2404a07bd003SBill Paul 	int			i;
2405a07bd003SBill Paul 
2406a07bd003SBill Paul 	sc = device_get_softc(dev);
2407a07bd003SBill Paul 	ifp = &sc->arpcom.ac_if;
2408a07bd003SBill Paul 
2409a07bd003SBill Paul         /* better way to do this? */
2410a07bd003SBill Paul 	for (i = 0; i < 5; i++)
2411a07bd003SBill Paul 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
2412a07bd003SBill Paul 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
2413a07bd003SBill Paul 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
2414a07bd003SBill Paul 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
2415a07bd003SBill Paul 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
2416a07bd003SBill Paul 
2417a07bd003SBill Paul 	/* reenable busmastering */
2418a07bd003SBill Paul 	pci_enable_busmaster(dev);
2419a07bd003SBill Paul 	pci_enable_io(dev, SYS_RES_MEMORY);
2420a07bd003SBill Paul 
2421a07bd003SBill Paul 	/* reinitialize interface if necessary */
2422a07bd003SBill Paul 	if (ifp->if_flags & IFF_UP)
2423a07bd003SBill Paul 		vge_init(sc);
2424a07bd003SBill Paul 
2425a07bd003SBill Paul 	sc->suspended = 0;
2426a07bd003SBill Paul 
2427a07bd003SBill Paul 	return (0);
2428a07bd003SBill Paul }
2429a07bd003SBill Paul 
2430a07bd003SBill Paul /*
2431a07bd003SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2432a07bd003SBill Paul  * get confused by errant DMAs when rebooting.
2433a07bd003SBill Paul  */
2434a07bd003SBill Paul static void
2435a07bd003SBill Paul vge_shutdown(dev)
2436a07bd003SBill Paul 	device_t		dev;
2437a07bd003SBill Paul {
2438a07bd003SBill Paul 	struct vge_softc		*sc;
2439a07bd003SBill Paul 
2440a07bd003SBill Paul 	sc = device_get_softc(dev);
2441a07bd003SBill Paul 
2442a07bd003SBill Paul 	vge_stop(sc);
2443a07bd003SBill Paul }
2444