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