if_vge.c revision 1.24
1/*	$OpenBSD: if_vge.c,v 1.24 2006/05/28 00:20:21 brad Exp $	*/
2/*	$FreeBSD: if_vge.c,v 1.3 2004/09/11 22:13:25 wpaul Exp $	*/
3/*
4 * Copyright (c) 2004
5 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
37 *
38 * Written by Bill Paul <wpaul@windriver.com>
39 * Senior Networking Software Engineer
40 * Wind River Systems
41 *
42 * Ported to OpenBSD by Peter Valchev <pvalchev@openbsd.org>
43 */
44
45/*
46 * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that
47 * combines a tri-speed ethernet MAC and PHY, with the following
48 * features:
49 *
50 *	o Jumbo frame support up to 16K
51 *	o Transmit and receive flow control
52 *	o IPv4 checksum offload
53 *	o VLAN tag insertion and stripping
54 *	o TCP large send
55 *	o 64-bit multicast hash table filter
56 *	o 64 entry CAM filter
57 *	o 16K RX FIFO and 48K TX FIFO memory
58 *	o Interrupt moderation
59 *
60 * The VT6122 supports up to four transmit DMA queues. The descriptors
61 * in the transmit ring can address up to 7 data fragments; frames which
62 * span more than 7 data buffers must be coalesced, but in general the
63 * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
64 * long. The receive descriptors address only a single buffer.
65 *
66 * There are two peculiar design issues with the VT6122. One is that
67 * receive data buffers must be aligned on a 32-bit boundary. This is
68 * not a problem where the VT6122 is used as a LOM device in x86-based
69 * systems, but on architectures that generate unaligned access traps, we
70 * have to do some copying.
71 *
72 * The other issue has to do with the way 64-bit addresses are handled.
73 * The DMA descriptors only allow you to specify 48 bits of addressing
74 * information. The remaining 16 bits are specified using one of the
75 * I/O registers. If you only have a 32-bit system, then this isn't
76 * an issue, but if you have a 64-bit system and more than 4GB of
77 * memory, you must have to make sure your network data buffers reside
78 * in the same 48-bit 'segment.'
79 *
80 * Special thanks to Ryan Fu at VIA Networking for providing documentation
81 * and sample NICs for testing.
82 */
83
84#include "bpfilter.h"
85#include "vlan.h"
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/kernel.h>
94#include <sys/device.h>
95#include <sys/socket.h>
96
97#include <net/if.h>
98#include <net/if_dl.h>
99#include <net/if_media.h>
100
101#ifdef INET
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/in_var.h>
105#include <netinet/ip.h>
106#include <netinet/if_ether.h>
107#endif
108
109#if NVLAN > 0
110#include <net/if_types.h>
111#include <net/if_vlan_var.h>
112#endif
113
114#if NBPFILTER > 0
115#include <net/bpf.h>
116#endif
117
118#include <dev/mii/mii.h>
119#include <dev/mii/miivar.h>
120
121#include <dev/pci/pcireg.h>
122#include <dev/pci/pcivar.h>
123#include <dev/pci/pcidevs.h>
124
125#include <dev/pci/if_vgereg.h>
126#include <dev/pci/if_vgevar.h>
127
128int vge_probe		(struct device *, void *, void *);
129void vge_attach		(struct device *, struct device *, void *);
130
131int vge_encap		(struct vge_softc *, struct mbuf *, int);
132
133int vge_allocmem		(struct vge_softc *);
134int vge_newbuf		(struct vge_softc *, int, struct mbuf *);
135int vge_rx_list_init	(struct vge_softc *);
136int vge_tx_list_init	(struct vge_softc *);
137void vge_rxeof		(struct vge_softc *);
138void vge_txeof		(struct vge_softc *);
139int vge_intr		(void *);
140void vge_tick		(void *);
141void vge_start		(struct ifnet *);
142int vge_ioctl		(struct ifnet *, u_long, caddr_t);
143int vge_init		(struct ifnet *);
144void vge_stop		(struct vge_softc *);
145void vge_watchdog	(struct ifnet *);
146int vge_ifmedia_upd	(struct ifnet *);
147void vge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
148
149#ifdef VGE_EEPROM
150void vge_eeprom_getword	(struct vge_softc *, int, u_int16_t *);
151#endif
152void vge_read_eeprom	(struct vge_softc *, caddr_t, int, int, int);
153
154void vge_miipoll_start	(struct vge_softc *);
155void vge_miipoll_stop	(struct vge_softc *);
156int vge_miibus_readreg	(struct device *, int, int);
157void vge_miibus_writereg (struct device *, int, int, int);
158void vge_miibus_statchg	(struct device *);
159
160void vge_cam_clear	(struct vge_softc *);
161int vge_cam_set		(struct vge_softc *, uint8_t *);
162void vge_setmulti	(struct vge_softc *);
163void vge_reset		(struct vge_softc *);
164
165struct cfattach vge_ca = {
166	sizeof(struct vge_softc), vge_probe, vge_attach
167};
168
169struct cfdriver vge_cd = {
170	0, "vge", DV_IFNET
171};
172
173#define VGE_PCI_LOIO             0x10
174#define VGE_PCI_LOMEM            0x14
175
176int vge_debug = 0;
177#define DPRINTF(x)	if (vge_debug) printf x
178#define DPRINTFN(n, x)	if (vge_debug >= (n)) printf x
179
180const struct pci_matchid vge_devices[] = {
181	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT612x },
182};
183
184#ifdef VGE_EEPROM
185/*
186 * Read a word of data stored in the EEPROM at address 'addr.'
187 */
188void
189vge_eeprom_getword(struct vge_softc *sc, int addr, u_int16_t *dest)
190{
191	int			i;
192	u_int16_t		word = 0;
193
194	/*
195	 * Enter EEPROM embedded programming mode. In order to
196	 * access the EEPROM at all, we first have to set the
197	 * EELOAD bit in the CHIPCFG2 register.
198	 */
199	CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
200	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
201
202	/* Select the address of the word we want to read */
203	CSR_WRITE_1(sc, VGE_EEADDR, addr);
204
205	/* Issue read command */
206	CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
207
208	/* Wait for the done bit to be set. */
209	for (i = 0; i < VGE_TIMEOUT; i++) {
210		if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
211			break;
212	}
213
214	if (i == VGE_TIMEOUT) {
215		printf("%s: EEPROM read timed out\n", sc->vge_dev.dv_xname);
216		*dest = 0;
217		return;
218	}
219
220	/* Read the result */
221	word = CSR_READ_2(sc, VGE_EERDDAT);
222
223	/* Turn off EEPROM access mode. */
224	CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
225	CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
226
227	*dest = word;
228}
229#endif
230
231/*
232 * Read a sequence of words from the EEPROM.
233 */
234void
235vge_read_eeprom(struct vge_softc *sc, caddr_t dest, int off, int cnt,
236    int swap)
237{
238	int			i;
239#ifdef VGE_EEPROM
240	u_int16_t		word = 0, *ptr;
241
242	for (i = 0; i < cnt; i++) {
243		vge_eeprom_getword(sc, off + i, &word);
244		ptr = (u_int16_t *)(dest + (i * 2));
245		if (swap)
246			*ptr = ntohs(word);
247		else
248			*ptr = word;
249	}
250#else
251	for (i = 0; i < ETHER_ADDR_LEN; i++)
252		dest[i] = CSR_READ_1(sc, VGE_PAR0 + i);
253#endif
254}
255
256void
257vge_miipoll_stop(struct vge_softc *sc)
258{
259	int			i;
260
261	CSR_WRITE_1(sc, VGE_MIICMD, 0);
262
263	for (i = 0; i < VGE_TIMEOUT; i++) {
264		DELAY(1);
265		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
266			break;
267	}
268
269	if (i == VGE_TIMEOUT)
270		printf("%s: failed to idle MII autopoll\n", sc->vge_dev.dv_xname);
271}
272
273void
274vge_miipoll_start(struct vge_softc *sc)
275{
276	int			i;
277
278	/* First, make sure we're idle. */
279
280	CSR_WRITE_1(sc, VGE_MIICMD, 0);
281	CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
282
283	for (i = 0; i < VGE_TIMEOUT; i++) {
284		DELAY(1);
285		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
286			break;
287	}
288
289	if (i == VGE_TIMEOUT) {
290		printf("%s: failed to idle MII autopoll\n", sc->vge_dev.dv_xname);
291		return;
292	}
293
294	/* Now enable auto poll mode. */
295
296	CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
297
298	/* And make sure it started. */
299
300	for (i = 0; i < VGE_TIMEOUT; i++) {
301		DELAY(1);
302		if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
303			break;
304	}
305
306	if (i == VGE_TIMEOUT)
307		printf("%s: failed to start MII autopoll\n", sc->vge_dev.dv_xname);
308}
309
310int
311vge_miibus_readreg(struct device *dev, int phy, int reg)
312{
313	struct vge_softc	*sc = (struct vge_softc *)dev;
314	int			i, s;
315	u_int16_t		rval = 0;
316
317	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
318		return(0);
319
320	s = splnet();
321
322	vge_miipoll_stop(sc);
323
324	/* Specify the register we want to read. */
325	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
326
327	/* Issue read command. */
328	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
329
330	/* Wait for the read command bit to self-clear. */
331	for (i = 0; i < VGE_TIMEOUT; i++) {
332		DELAY(1);
333		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
334			break;
335	}
336
337	if (i == VGE_TIMEOUT)
338		printf("%s: MII read timed out\n", sc->vge_dev.dv_xname);
339	else
340		rval = CSR_READ_2(sc, VGE_MIIDATA);
341
342	vge_miipoll_start(sc);
343	splx(s);
344
345	return (rval);
346}
347
348void
349vge_miibus_writereg(struct device *dev, int phy, int reg, int data)
350{
351	struct vge_softc	*sc = (struct vge_softc *)dev;
352	int			i, s;
353
354	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
355		return;
356
357	s = splnet();
358	vge_miipoll_stop(sc);
359
360	/* Specify the register we want to write. */
361	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
362
363	/* Specify the data we want to write. */
364	CSR_WRITE_2(sc, VGE_MIIDATA, data);
365
366	/* Issue write command. */
367	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
368
369	/* Wait for the write command bit to self-clear. */
370	for (i = 0; i < VGE_TIMEOUT; i++) {
371		DELAY(1);
372		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
373			break;
374	}
375
376	if (i == VGE_TIMEOUT) {
377		printf("%s: MII write timed out\n", sc->vge_dev.dv_xname);
378	}
379
380	vge_miipoll_start(sc);
381	splx(s);
382}
383
384void
385vge_cam_clear(struct vge_softc *sc)
386{
387	int			i;
388
389	/*
390	 * Turn off all the mask bits. This tells the chip
391	 * that none of the entries in the CAM filter are valid.
392	 * desired entries will be enabled as we fill the filter in.
393	 */
394
395	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
396	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
397	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
398	for (i = 0; i < 8; i++)
399		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
400
401	/* Clear the VLAN filter too. */
402
403	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
404	for (i = 0; i < 8; i++)
405		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
406
407	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
408	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
409	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
410
411	sc->vge_camidx = 0;
412}
413
414int
415vge_cam_set(struct vge_softc *sc, uint8_t *addr)
416{
417	int			i, error = 0;
418
419	if (sc->vge_camidx == VGE_CAM_MAXADDRS)
420		return(ENOSPC);
421
422	/* Select the CAM data page. */
423	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
424	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
425
426	/* Set the filter entry we want to update and enable writing. */
427	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx);
428
429	/* Write the address to the CAM registers */
430	for (i = 0; i < ETHER_ADDR_LEN; i++)
431		CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
432
433	/* Issue a write command. */
434	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
435
436	/* Wake for it to clear. */
437	for (i = 0; i < VGE_TIMEOUT; i++) {
438		DELAY(1);
439		if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
440			break;
441	}
442
443	if (i == VGE_TIMEOUT) {
444		printf("%s: setting CAM filter failed\n", sc->vge_dev.dv_xname);
445		error = EIO;
446		goto fail;
447	}
448
449	/* Select the CAM mask page. */
450	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
451	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
452
453	/* Set the mask bit that enables this filter. */
454	CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8),
455	    1<<(sc->vge_camidx & 7));
456
457	sc->vge_camidx++;
458
459fail:
460	/* Turn off access to CAM. */
461	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
462	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
463	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
464
465	return (error);
466}
467
468/*
469 * Program the multicast filter. We use the 64-entry CAM filter
470 * for perfect filtering. If there's more than 64 multicast addresses,
471 * we use the hash filter instead.
472 */
473void
474vge_setmulti(struct vge_softc *sc)
475{
476	struct arpcom		*ac = &sc->arpcom;
477	struct ifnet		*ifp = &ac->ac_if;
478	struct ether_multi	*enm;
479	struct ether_multistep	step;
480	int			error;
481	u_int32_t		h = 0, hashes[2] = { 0, 0 };
482
483	/* First, zot all the multicast entries. */
484	vge_cam_clear(sc);
485	CSR_WRITE_4(sc, VGE_MAR0, 0);
486	CSR_WRITE_4(sc, VGE_MAR1, 0);
487	ifp->if_flags &= ~IFF_ALLMULTI;
488
489	/*
490	 * If the user wants allmulti or promisc mode, enable reception
491	 * of all multicast frames.
492	 */
493	if (ifp->if_flags & IFF_PROMISC) {
494allmulti:
495		CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF);
496		CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF);
497		ifp->if_flags |= IFF_ALLMULTI;
498		return;
499	}
500
501	/* Now program new ones */
502	ETHER_FIRST_MULTI(step, ac, enm);
503	while (enm != NULL) {
504		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN))
505			goto allmulti;
506
507		error = vge_cam_set(sc, enm->enm_addrlo);
508		if (error)
509			break;
510
511		ETHER_NEXT_MULTI(step, enm);
512	}
513
514	/* If there were too many addresses, use the hash filter. */
515	if (error) {
516		vge_cam_clear(sc);
517
518		ETHER_FIRST_MULTI(step, ac, enm);
519		while (enm != NULL) {
520			h = ether_crc32_be(enm->enm_addrlo,
521			    ETHER_ADDR_LEN) >> 26;
522			hashes[h >> 5] |= 1 << (h & 0x1f);
523
524			ETHER_NEXT_MULTI(step, enm);
525		}
526
527		CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
528		CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
529	}
530}
531
532void
533vge_reset(struct vge_softc *sc)
534{
535	int			i;
536
537	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
538
539	for (i = 0; i < VGE_TIMEOUT; i++) {
540		DELAY(5);
541		if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
542			break;
543	}
544
545	if (i == VGE_TIMEOUT) {
546		printf("%s: soft reset timed out", sc->vge_dev.dv_xname);
547		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
548		DELAY(2000);
549	}
550
551	DELAY(5000);
552
553	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
554
555	for (i = 0; i < VGE_TIMEOUT; i++) {
556		DELAY(5);
557		if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
558			break;
559	}
560
561	if (i == VGE_TIMEOUT) {
562		printf("%s: EEPROM reload timed out\n", sc->vge_dev.dv_xname);
563		return;
564	}
565
566	CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
567}
568
569/*
570 * Probe for a VIA gigabit chip. Check the PCI vendor and device
571 * IDs against our list and return a device name if we find a match.
572 */
573int
574vge_probe(struct device *dev, void *match, void *aux)
575{
576	return (pci_matchbyid((struct pci_attach_args *)aux, vge_devices,
577	    sizeof(vge_devices)/sizeof(vge_devices[0])));
578}
579
580/*
581 * Allocate memory for RX/TX rings
582 */
583int
584vge_allocmem(struct vge_softc *sc)
585{
586	int			nseg, rseg;
587	int			i, error;
588
589	nseg = 32;
590
591	/* Allocate DMA'able memory for the TX ring */
592
593	error = bus_dmamap_create(sc->sc_dmat, VGE_TX_LIST_SZ, 1,
594	    VGE_TX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
595	    &sc->vge_ldata.vge_tx_list_map);
596	if (error)
597		return (ENOMEM);
598	error = bus_dmamem_alloc(sc->sc_dmat, VGE_TX_LIST_SZ,
599	    ETHER_ALIGN, 0,
600	    &sc->vge_ldata.vge_tx_listseg, 1, &rseg, BUS_DMA_NOWAIT);
601	if (error) {
602		printf("%s: can't alloc TX list\n", sc->vge_dev.dv_xname);
603		return (ENOMEM);
604	}
605
606	/* Load the map for the TX ring. */
607	error = bus_dmamem_map(sc->sc_dmat, &sc->vge_ldata.vge_tx_listseg,
608	     1, VGE_TX_LIST_SZ,
609	     (caddr_t *)&sc->vge_ldata.vge_tx_list, BUS_DMA_NOWAIT);
610	memset(sc->vge_ldata.vge_tx_list, 0, VGE_TX_LIST_SZ);
611	if (error) {
612		printf("%s: can't map TX dma buffers\n",
613		    sc->vge_dev.dv_xname);
614		bus_dmamem_free(sc->sc_dmat, &sc->vge_ldata.vge_tx_listseg, rseg);
615		return (ENOMEM);
616	}
617
618	error = bus_dmamap_load(sc->sc_dmat, sc->vge_ldata.vge_tx_list_map,
619	    sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ, NULL, BUS_DMA_NOWAIT);
620	if (error) {
621		printf("%s: can't load TX dma map\n", sc->vge_dev.dv_xname);
622		bus_dmamap_destroy(sc->sc_dmat, sc->vge_ldata.vge_tx_list_map);
623		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->vge_ldata.vge_tx_list,
624		    VGE_TX_LIST_SZ);
625		bus_dmamem_free(sc->sc_dmat, &sc->vge_ldata.vge_tx_listseg, rseg);
626		return (ENOMEM);
627	}
628
629	/* Create DMA maps for TX buffers */
630
631	for (i = 0; i < VGE_TX_DESC_CNT; i++) {
632		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES * nseg, nseg,
633		    MCLBYTES, 0, BUS_DMA_ALLOCNOW,
634		    &sc->vge_ldata.vge_tx_dmamap[i]);
635		if (error) {
636			printf("%s: can't create DMA map for TX\n",
637			    sc->vge_dev.dv_xname);
638			return (ENOMEM);
639		}
640	}
641
642	/* Allocate DMA'able memory for the RX ring */
643
644	error = bus_dmamap_create(sc->sc_dmat, VGE_RX_LIST_SZ, 1,
645	    VGE_RX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
646	    &sc->vge_ldata.vge_rx_list_map);
647	if (error)
648		return (ENOMEM);
649	error = bus_dmamem_alloc(sc->sc_dmat, VGE_RX_LIST_SZ, VGE_RING_ALIGN,
650	    0, &sc->vge_ldata.vge_rx_listseg, 1, &rseg, BUS_DMA_NOWAIT);
651	if (error) {
652		printf("%s: can't alloc RX list\n", sc->vge_dev.dv_xname);
653		return (ENOMEM);
654	}
655
656	/* Load the map for the RX ring. */
657
658	error = bus_dmamem_map(sc->sc_dmat, &sc->vge_ldata.vge_rx_listseg,
659	     1, VGE_RX_LIST_SZ,
660	     (caddr_t *)&sc->vge_ldata.vge_rx_list, BUS_DMA_NOWAIT);
661	memset(sc->vge_ldata.vge_rx_list, 0, VGE_RX_LIST_SZ);
662	if (error) {
663		printf("%s: can't map RX dma buffers\n",
664		    sc->vge_dev.dv_xname);
665		bus_dmamem_free(sc->sc_dmat, &sc->vge_ldata.vge_rx_listseg, rseg);
666		return (ENOMEM);
667	}
668	error = bus_dmamap_load(sc->sc_dmat, sc->vge_ldata.vge_rx_list_map,
669	    sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ, NULL, BUS_DMA_NOWAIT);
670	if (error) {
671		printf("%s: can't load RX dma map\n", sc->vge_dev.dv_xname);
672		bus_dmamap_destroy(sc->sc_dmat, sc->vge_ldata.vge_rx_list_map);
673		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->vge_ldata.vge_rx_list,
674		    VGE_RX_LIST_SZ);
675		bus_dmamem_free(sc->sc_dmat, &sc->vge_ldata.vge_rx_listseg, rseg);
676		return (ENOMEM);
677	}
678
679	/* Create DMA maps for RX buffers */
680
681	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
682		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES * nseg, nseg,
683		    MCLBYTES, 0, BUS_DMA_ALLOCNOW,
684		    &sc->vge_ldata.vge_rx_dmamap[i]);
685		if (error) {
686			printf("%s: can't create DMA map for RX\n",
687			    sc->vge_dev.dv_xname);
688			return (ENOMEM);
689		}
690	}
691
692	return (0);
693}
694
695/*
696 * Attach the interface. Allocate softc structures, do ifmedia
697 * setup and ethernet/BPF attach.
698 */
699void
700vge_attach(struct device *parent, struct device *self, void *aux)
701{
702	u_char			eaddr[ETHER_ADDR_LEN];
703	u_int16_t		as[3];
704	struct vge_softc	*sc = (struct vge_softc *)self;
705	struct pci_attach_args	*pa = aux;
706	pci_chipset_tag_t	pc = pa->pa_pc;
707	pci_intr_handle_t	ih;
708	const char		*intrstr = NULL;
709	struct ifnet		*ifp;
710	int			error = 0, i;
711	bus_size_t		iosize;
712	pcireg_t		command;
713
714	/*
715	 * Map control/status registers.
716	 */
717	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
718	if ((command & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) == 0) {
719		printf(": neither i/o nor mem enabled\n");
720		return;
721	}
722
723	if (command & PCI_COMMAND_MEM_ENABLE) {
724		if (pci_mapreg_map(pa, VGE_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
725		    &sc->vge_btag, &sc->vge_bhandle, NULL, &iosize, 0)) {
726			printf(": can't map mem space\n");
727			return;
728		}
729	} else {
730		if (pci_mapreg_map(pa, VGE_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
731		    &sc->vge_btag, &sc->vge_bhandle, NULL, &iosize, 0)) {
732			printf(": can't map i/o space\n");
733			return;
734		}
735	}
736
737	/* Allocate interrupt */
738	if (pci_intr_map(pa, &ih)) {
739		printf(": couldn't map interrupt\n");
740		return;
741	}
742	intrstr = pci_intr_string(pc, ih);
743	sc->vge_intrhand = pci_intr_establish(pc, ih, IPL_NET, vge_intr, sc,
744	    sc->vge_dev.dv_xname);
745	if (sc->vge_intrhand == NULL) {
746		printf(": couldn't establish interrupt");
747		if (intrstr != NULL)
748			printf(" at %s", intrstr);
749		return;
750	}
751	printf(": %s", intrstr);
752
753	sc->sc_dmat = pa->pa_dmat;
754
755	/* Reset the adapter. */
756	vge_reset(sc);
757
758	/*
759	 * Get station address from the EEPROM.
760	 */
761	vge_read_eeprom(sc, (caddr_t)as, VGE_EE_EADDR, 3, 0);
762	for (i = 0; i < 3; i++) {
763		eaddr[(i * 2) + 0] = as[i] & 0xff;
764		eaddr[(i * 2) + 1] = as[i] >> 8;
765	}
766
767	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
768
769	printf(", address %s\n",
770	    ether_sprintf(sc->arpcom.ac_enaddr));
771
772	error = vge_allocmem(sc);
773
774	if (error)
775		return;
776
777	ifp = &sc->arpcom.ac_if;
778	ifp->if_softc = sc;
779	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
780	ifp->if_ioctl = vge_ioctl;
781	ifp->if_start = vge_start;
782	ifp->if_watchdog = vge_watchdog;
783	ifp->if_init = vge_init;
784	ifp->if_baudrate = 1000000000;
785	ifp->if_hardmtu = VGE_JUMBO_MTU;
786	IFQ_SET_MAXLEN(&ifp->if_snd, VGE_IFQ_MAXLEN);
787	IFQ_SET_READY(&ifp->if_snd);
788
789	ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_CSUM_IPv4 |
790				IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
791
792#ifdef VGE_VLAN
793	ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
794#endif
795
796	/* Set interface name */
797	strlcpy(ifp->if_xname, sc->vge_dev.dv_xname, IFNAMSIZ);
798
799	/* Do MII setup */
800	sc->sc_mii.mii_ifp = ifp;
801	sc->sc_mii.mii_readreg = vge_miibus_readreg;
802	sc->sc_mii.mii_writereg = vge_miibus_writereg;
803	sc->sc_mii.mii_statchg = vge_miibus_statchg;
804	ifmedia_init(&sc->sc_mii.mii_media, 0,
805	    vge_ifmedia_upd, vge_ifmedia_sts);
806	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
807	    MII_OFFSET_ANY, 0);
808	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
809		printf("%s: no PHY found!\n", sc->vge_dev.dv_xname);
810		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL,
811		    0, NULL);
812		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
813	} else
814		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
815
816	timeout_set(&sc->timer_handle, vge_tick, sc);
817
818	/*
819	 * Call MI attach routine.
820	 */
821	if_attach(ifp);
822	ether_ifattach(ifp);
823}
824
825int
826vge_newbuf(struct vge_softc *sc, int idx, struct mbuf *m)
827{
828	struct mbuf		*m_new = NULL;
829	struct vge_rx_desc	*r;
830	bus_dmamap_t		rxmap = sc->vge_ldata.vge_rx_dmamap[idx];
831	int			i;
832
833	if (m == NULL) {
834		/* Allocate a new mbuf */
835		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
836		if (m_new == NULL)
837			return (ENOBUFS);
838
839		/* Allocate a cluster */
840		MCLGET(m_new, M_DONTWAIT);
841		if (!(m_new->m_flags & M_EXT)) {
842			m_freem(m_new);
843			return (ENOBUFS);
844		}
845	} else
846		m_new->m_data = m_new->m_ext.ext_buf;
847
848	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
849	/* Fix-up alignment so payload is doubleword-aligned */
850	/* XXX m_adj(m_new, ETHER_ALIGN); */
851
852	if (bus_dmamap_load_mbuf(sc->sc_dmat, rxmap, m_new, BUS_DMA_NOWAIT))
853		return (ENOBUFS);
854
855	if (rxmap->dm_nsegs > 1)
856		goto out;
857
858	/* Map the segments into RX descriptors */
859	r = &sc->vge_ldata.vge_rx_list[idx];
860
861	if (letoh32(r->vge_sts) & VGE_RDSTS_OWN) {
862		printf("%s: tried to map a busy RX descriptor\n",
863		    sc->vge_dev.dv_xname);
864		goto out;
865	}
866	r->vge_buflen = htole16(VGE_BUFLEN(rxmap->dm_segs[0].ds_len) | VGE_RXDESC_I);
867	r->vge_addrlo = htole32(VGE_ADDR_LO(rxmap->dm_segs[0].ds_addr));
868	r->vge_addrhi = htole16(VGE_ADDR_HI(rxmap->dm_segs[0].ds_addr) & 0xFFFF);
869	r->vge_sts = htole32(0);
870	r->vge_ctl = htole32(0);
871
872	/*
873	 * Note: the manual fails to document the fact that for
874	 * proper operation, the driver needs to replenish the RX
875	 * DMA ring 4 descriptors at a time (rather than one at a
876	 * time, like most chips). We can allocate the new buffers
877	 * but we should not set the OWN bits until we're ready
878	 * to hand back 4 of them in one shot.
879	 */
880#define VGE_RXCHUNK 4
881	sc->vge_rx_consumed++;
882	if (sc->vge_rx_consumed == VGE_RXCHUNK) {
883		for (i = idx; i != idx - sc->vge_rx_consumed; i--)
884			sc->vge_ldata.vge_rx_list[i].vge_sts |=
885			    htole32(VGE_RDSTS_OWN);
886		sc->vge_rx_consumed = 0;
887	}
888
889	sc->vge_ldata.vge_rx_mbuf[idx] = m_new;
890
891	bus_dmamap_sync(sc->sc_dmat, rxmap, 0,
892	    rxmap->dm_mapsize, BUS_DMASYNC_PREREAD);
893
894	return (0);
895out:
896	DPRINTF(("vge_newbuf: out of memory\n"));
897	if (m_new != NULL)
898		m_freem(m_new);
899	return (ENOMEM);
900}
901
902int
903vge_tx_list_init(struct vge_softc *sc)
904{
905	bzero ((char *)sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ);
906	bzero ((char *)&sc->vge_ldata.vge_tx_mbuf,
907	    (VGE_TX_DESC_CNT * sizeof(struct mbuf *)));
908
909	bus_dmamap_sync(sc->sc_dmat,
910	    sc->vge_ldata.vge_tx_list_map, 0,
911	    sc->vge_ldata.vge_tx_list_map->dm_mapsize,
912	    BUS_DMASYNC_PREWRITE);
913	sc->vge_ldata.vge_tx_prodidx = 0;
914	sc->vge_ldata.vge_tx_considx = 0;
915	sc->vge_ldata.vge_tx_free = VGE_TX_DESC_CNT;
916
917	return (0);
918}
919
920/* Init RX descriptors and allocate mbufs with vge_newbuf()
921 * A ring is used, and last descriptor points to first. */
922int
923vge_rx_list_init(struct vge_softc *sc)
924{
925	int			i;
926
927	bzero ((char *)sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ);
928	bzero ((char *)&sc->vge_ldata.vge_rx_mbuf,
929	    (VGE_RX_DESC_CNT * sizeof(struct mbuf *)));
930
931	sc->vge_rx_consumed = 0;
932
933	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
934		if (vge_newbuf(sc, i, NULL) == ENOBUFS)
935			return (ENOBUFS);
936	}
937
938	/* Flush the RX descriptors */
939
940	bus_dmamap_sync(sc->sc_dmat,
941	    sc->vge_ldata.vge_rx_list_map,
942	    0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
943	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
944
945	sc->vge_ldata.vge_rx_prodidx = 0;
946	sc->vge_rx_consumed = 0;
947	sc->vge_head = sc->vge_tail = NULL;
948
949	return (0);
950}
951
952/*
953 * RX handler. We support the reception of jumbo frames that have
954 * been fragmented across multiple 2K mbuf cluster buffers.
955 */
956void
957vge_rxeof(struct vge_softc *sc)
958{
959	struct mbuf		*m;
960	struct ifnet		*ifp;
961	int			i, total_len;
962	int			lim = 0;
963	struct vge_rx_desc	*cur_rx;
964	u_int32_t		rxstat, rxctl;
965
966	ifp = &sc->arpcom.ac_if;
967	i = sc->vge_ldata.vge_rx_prodidx;
968
969	/* Invalidate the descriptor memory */
970
971	bus_dmamap_sync(sc->sc_dmat,
972	    sc->vge_ldata.vge_rx_list_map,
973	    0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
974	    BUS_DMASYNC_POSTREAD);
975
976	while (!VGE_OWN(&sc->vge_ldata.vge_rx_list[i])) {
977		struct mbuf *m0 = NULL;
978
979		cur_rx = &sc->vge_ldata.vge_rx_list[i];
980		m = sc->vge_ldata.vge_rx_mbuf[i];
981		total_len = VGE_RXBYTES(cur_rx);
982		rxstat = letoh32(cur_rx->vge_sts);
983		rxctl = letoh32(cur_rx->vge_ctl);
984
985		/* Invalidate the RX mbuf and unload its map */
986
987		bus_dmamap_sync(sc->sc_dmat,
988		    sc->vge_ldata.vge_rx_dmamap[i],
989		    0, sc->vge_ldata.vge_rx_dmamap[i]->dm_mapsize,
990		    BUS_DMASYNC_POSTWRITE);
991		bus_dmamap_unload(sc->sc_dmat,
992		    sc->vge_ldata.vge_rx_dmamap[i]);
993
994		/*
995		 * If the 'start of frame' bit is set, this indicates
996		 * either the first fragment in a multi-fragment receive,
997		 * or an intermediate fragment. Either way, we want to
998		 * accumulate the buffers.
999		 */
1000		if (rxstat & VGE_RXPKT_SOF) {
1001			DPRINTF(("vge_rxeof: SOF\n"));
1002			m->m_len = MCLBYTES;
1003			if (sc->vge_head == NULL)
1004				sc->vge_head = sc->vge_tail = m;
1005			else {
1006				m->m_flags &= ~M_PKTHDR;
1007				sc->vge_tail->m_next = m;
1008				sc->vge_tail = m;
1009			}
1010			vge_newbuf(sc, i, NULL);
1011			VGE_RX_DESC_INC(i);
1012			continue;
1013		}
1014
1015		/*
1016		 * Bad/error frames will have the RXOK bit cleared.
1017		 * However, there's one error case we want to allow:
1018		 * if a VLAN tagged frame arrives and the chip can't
1019		 * match it against the CAM filter, it considers this
1020		 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1021		 * We don't want to drop the frame though: our VLAN
1022		 * filtering is done in software.
1023		 */
1024		if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
1025		    && !(rxstat & VGE_RDSTS_CSUMERR)) {
1026			ifp->if_ierrors++;
1027			/*
1028			 * If this is part of a multi-fragment packet,
1029			 * discard all the pieces.
1030			 */
1031			if (sc->vge_head != NULL) {
1032				m_freem(sc->vge_head);
1033				sc->vge_head = sc->vge_tail = NULL;
1034			}
1035			vge_newbuf(sc, i, m);
1036			VGE_RX_DESC_INC(i);
1037			continue;
1038		}
1039
1040		/*
1041		 * If allocating a replacement mbuf fails,
1042		 * reload the current one.
1043		 */
1044
1045		if (vge_newbuf(sc, i, NULL) == ENOBUFS) {
1046			if (sc->vge_head != NULL) {
1047				m_freem(sc->vge_head);
1048				sc->vge_head = sc->vge_tail = NULL;
1049			}
1050
1051			m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1052			    total_len - ETHER_CRC_LEN + ETHER_ALIGN,
1053			    0, ifp, NULL);
1054			vge_newbuf(sc, i, m);
1055			if (m0 == NULL) {
1056				ifp->if_ierrors++;
1057				continue;
1058			}
1059			m_adj(m0, ETHER_ALIGN);
1060			m = m0;
1061
1062			VGE_RX_DESC_INC(i);
1063			continue;
1064		}
1065
1066		VGE_RX_DESC_INC(i);
1067
1068		if (sc->vge_head != NULL) {
1069			m->m_len = total_len % MCLBYTES;
1070			/*
1071			 * Special case: if there's 4 bytes or less
1072			 * in this buffer, the mbuf can be discarded:
1073			 * the last 4 bytes is the CRC, which we don't
1074			 * care about anyway.
1075			 */
1076			if (m->m_len <= ETHER_CRC_LEN) {
1077				sc->vge_tail->m_len -=
1078				    (ETHER_CRC_LEN - m->m_len);
1079				m_freem(m);
1080			} else {
1081				m->m_len -= ETHER_CRC_LEN;
1082				m->m_flags &= ~M_PKTHDR;
1083				sc->vge_tail->m_next = m;
1084			}
1085			m = sc->vge_head;
1086			sc->vge_head = sc->vge_tail = NULL;
1087			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1088		} else
1089			m->m_pkthdr.len = m->m_len =
1090			    (total_len - ETHER_CRC_LEN);
1091
1092#ifdef __STRICT_ALIGNMENT
1093		bcopy(m->m_data, m->m_data + ETHER_ALIGN,
1094		    total_len);
1095		m->m_data += ETHER_ALIGN;
1096#endif
1097		ifp->if_ipackets++;
1098		m->m_pkthdr.rcvif = ifp;
1099
1100		/* Do RX checksumming */
1101
1102		/* Check IP header checksum */
1103		if ((rxctl & VGE_RDCTL_IPPKT) &&
1104		    (rxctl & VGE_RDCTL_IPCSUMOK))
1105			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_IN_OK;
1106
1107		/* Check TCP/UDP checksum */
1108		if ((rxctl & (VGE_RDCTL_TCPPKT|VGE_RDCTL_UDPPKT)) &&
1109		    (rxctl & VGE_RDCTL_PROTOCSUMOK))
1110			m->m_pkthdr.csum_flags |= M_TCP_CSUM_IN_OK | M_UDP_CSUM_IN_OK;
1111
1112#if NBPFILTER > 0
1113		if (ifp->if_bpf)
1114			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1115#endif
1116		ether_input_mbuf(ifp, m);
1117
1118		lim++;
1119		if (lim == VGE_RX_DESC_CNT)
1120			break;
1121	}
1122
1123	/* Flush the RX DMA ring */
1124	bus_dmamap_sync(sc->sc_dmat,
1125	    sc->vge_ldata.vge_rx_list_map,
1126	    0, sc->vge_ldata.vge_rx_list_map->dm_mapsize,
1127	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1128
1129	sc->vge_ldata.vge_rx_prodidx = i;
1130	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
1131}
1132
1133void
1134vge_txeof(struct vge_softc *sc)
1135{
1136	struct ifnet		*ifp;
1137	u_int32_t		txstat;
1138	int			idx;
1139
1140	ifp = &sc->arpcom.ac_if;
1141	idx = sc->vge_ldata.vge_tx_considx;
1142
1143	/* Invalidate the TX descriptor list */
1144
1145	bus_dmamap_sync(sc->sc_dmat,
1146	    sc->vge_ldata.vge_tx_list_map,
1147	    0, sc->vge_ldata.vge_tx_list_map->dm_mapsize,
1148	    BUS_DMASYNC_POSTREAD);
1149
1150	/* Transmitted frames can be now free'd from the TX list */
1151	while (idx != sc->vge_ldata.vge_tx_prodidx) {
1152		txstat = letoh32(sc->vge_ldata.vge_tx_list[idx].vge_sts);
1153		if (txstat & VGE_TDSTS_OWN)
1154			break;
1155
1156		m_freem(sc->vge_ldata.vge_tx_mbuf[idx]);
1157		sc->vge_ldata.vge_tx_mbuf[idx] = NULL;
1158		bus_dmamap_unload(sc->sc_dmat,
1159		    sc->vge_ldata.vge_tx_dmamap[idx]);
1160		if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
1161			ifp->if_collisions++;
1162		if (txstat & VGE_TDSTS_TXERR)
1163			ifp->if_oerrors++;
1164		else
1165			ifp->if_opackets++;
1166
1167		sc->vge_ldata.vge_tx_free++;
1168		VGE_TX_DESC_INC(idx);
1169	}
1170
1171	/* No changes made to the TX ring, so no flush needed */
1172
1173	if (idx != sc->vge_ldata.vge_tx_considx) {
1174		sc->vge_ldata.vge_tx_considx = idx;
1175		ifp->if_flags &= ~IFF_OACTIVE;
1176		ifp->if_timer = 0;
1177	}
1178
1179	/*
1180	 * If not all descriptors have been released reaped yet,
1181	 * reload the timer so that we will eventually get another
1182	 * interrupt that will cause us to re-enter this routine.
1183	 * This is done in case the transmitter has gone idle.
1184	 */
1185	if (sc->vge_ldata.vge_tx_free != VGE_TX_DESC_CNT)
1186		CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1187}
1188
1189void
1190vge_tick(void *xsc)
1191{
1192	struct vge_softc	*sc = xsc;
1193	struct ifnet		*ifp = &sc->arpcom.ac_if;
1194	struct mii_data		*mii = &sc->sc_mii;
1195	int s;
1196
1197	s = splnet();
1198
1199	mii_tick(mii);
1200
1201	if (sc->vge_link) {
1202		if (!(mii->mii_media_status & IFM_ACTIVE))
1203			sc->vge_link = 0;
1204			ifp->if_link_state = LINK_STATE_DOWN;
1205			if_link_state_change(ifp);
1206	} else {
1207		if (mii->mii_media_status & IFM_ACTIVE &&
1208		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1209			sc->vge_link = 1;
1210			ifp->if_link_state = LINK_STATE_UP;
1211			if_link_state_change(ifp);
1212			if (!IFQ_IS_EMPTY(&ifp->if_snd))
1213				vge_start(ifp);
1214		}
1215	}
1216	timeout_add(&sc->timer_handle, hz);
1217	splx(s);
1218}
1219
1220int
1221vge_intr(void *arg)
1222{
1223	struct vge_softc	*sc = arg;
1224	struct ifnet		*ifp;
1225	u_int32_t		status;
1226	int			claimed = 0;
1227
1228	ifp = &sc->arpcom.ac_if;
1229
1230	if (!(ifp->if_flags & IFF_UP))
1231		return 0;
1232
1233	/* Disable interrupts */
1234	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1235
1236	for (;;) {
1237		status = CSR_READ_4(sc, VGE_ISR);
1238		DPRINTFN(3, ("vge_intr: status=%#x\n", status));
1239
1240		/* If the card has gone away the read returns 0xffffffff. */
1241		if (status == 0xFFFFFFFF)
1242			break;
1243
1244		if (status) {
1245			CSR_WRITE_4(sc, VGE_ISR, status);
1246		}
1247
1248		if ((status & VGE_INTRS) == 0)
1249			break;
1250
1251		claimed = 1;
1252
1253		if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1254			vge_rxeof(sc);
1255
1256		if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1257			DPRINTFN(2, ("vge_intr: RX error, recovering\n"));
1258			vge_rxeof(sc);
1259			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1260			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1261		}
1262
1263		if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
1264			vge_txeof(sc);
1265
1266		if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL)) {
1267			DPRINTFN(2, ("DMA_STALL\n"));
1268			vge_init(ifp);
1269		}
1270
1271		if (status & VGE_ISR_LINKSTS) {
1272			timeout_del(&sc->timer_handle);
1273			vge_tick(sc);
1274		}
1275	}
1276
1277	/* Re-enable interrupts */
1278	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1279
1280	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1281		vge_start(ifp);
1282
1283	return (claimed);
1284}
1285
1286/*
1287 * Encapsulate an mbuf chain into the TX ring by combining it w/
1288 * the descriptors.
1289 */
1290int
1291vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
1292{
1293	bus_dmamap_t		txmap;
1294	struct vge_tx_desc	*d = NULL;
1295	struct vge_tx_frag	*f;
1296	int			error, frag;
1297	u_int32_t		vge_flags;
1298
1299	vge_flags = 0;
1300
1301	if (m_head->m_pkthdr.csum_flags & M_IPV4_CSUM_OUT)
1302		vge_flags |= VGE_TDCTL_IPCSUM;
1303	if (m_head->m_pkthdr.csum_flags & M_TCPV4_CSUM_OUT)
1304		vge_flags |= VGE_TDCTL_TCPCSUM;
1305	if (m_head->m_pkthdr.csum_flags & M_UDPV4_CSUM_OUT)
1306		vge_flags |= VGE_TDCTL_UDPCSUM;
1307
1308	txmap = sc->vge_ldata.vge_tx_dmamap[idx];
1309repack:
1310	error = bus_dmamap_load_mbuf(sc->sc_dmat, txmap,
1311	    m_head, BUS_DMA_NOWAIT);
1312	if (error) {
1313		printf("%s: can't map mbuf (error %d)\n",
1314		    sc->vge_dev.dv_xname, error);
1315		return (ENOBUFS);
1316	}
1317
1318	d = &sc->vge_ldata.vge_tx_list[idx];
1319	/* If owned by chip, fail */
1320	if (letoh32(d->vge_sts) & VGE_TDSTS_OWN)
1321		return (ENOBUFS);
1322
1323	for (frag = 0; frag < txmap->dm_nsegs; frag++) {
1324		/* Check if we have used all 7 fragments. */
1325		if (frag == VGE_TX_FRAGS)
1326			break;
1327		f = &d->vge_frag[frag];
1328		f->vge_buflen = htole16(VGE_BUFLEN(txmap->dm_segs[frag].ds_len));
1329		f->vge_addrlo = htole32(VGE_ADDR_LO(txmap->dm_segs[frag].ds_addr));
1330		f->vge_addrhi = htole16(VGE_ADDR_HI(txmap->dm_segs[frag].ds_addr) & 0xFFFF);
1331	}
1332
1333	/*
1334	 * We used up all 7 fragments!  Now what we have to do is
1335	 * copy the data into a mbuf cluster and map that.
1336	 */
1337	if (frag == VGE_TX_FRAGS) {
1338		struct mbuf *m = NULL;
1339
1340		MGETHDR(m, M_DONTWAIT, MT_DATA);
1341		if (m == NULL) {
1342			m_freem(m_head);
1343			return (ENOBUFS);
1344		}
1345		if (m_head->m_pkthdr.len > MHLEN) {
1346			MCLGET(m, M_DONTWAIT);
1347			if (!(m->m_flags & M_EXT)) {
1348				m_freem(m);
1349				m_freem(m_head);
1350				return (ENOBUFS);
1351			}
1352		}
1353		m_copydata(m_head, 0, m_head->m_pkthdr.len,
1354		    mtod(m, caddr_t));
1355		m->m_pkthdr.len = m->m_len = m_head->m_pkthdr.len;
1356		m_freem(m_head);
1357		m_head = m;
1358		goto repack;
1359	}
1360
1361	/* This chip does not do auto-padding */
1362	if (m_head->m_pkthdr.len < VGE_MIN_FRAMELEN) {
1363		f = &d->vge_frag[frag];
1364
1365		f->vge_buflen = htole16(VGE_BUFLEN(VGE_MIN_FRAMELEN -
1366		    m_head->m_pkthdr.len));
1367		f->vge_addrlo = htole32(VGE_ADDR_LO(txmap->dm_segs[0].ds_addr));
1368		f->vge_addrhi = htole16(VGE_ADDR_HI(txmap->dm_segs[0].ds_addr) & 0xFFFF);
1369		m_head->m_pkthdr.len = VGE_MIN_FRAMELEN;
1370		frag++;
1371	}
1372	/* For some reason, we need to tell the card fragment + 1 */
1373	frag++;
1374
1375	bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
1376	    BUS_DMASYNC_PREWRITE);
1377
1378	d->vge_sts = htole32(m_head->m_pkthdr.len << 16);
1379	d->vge_ctl = htole32(vge_flags|(frag << 28) | VGE_TD_LS_NORM);
1380
1381	if (m_head->m_pkthdr.len > ETHERMTU + ETHER_HDR_LEN)
1382		d->vge_ctl |= htole32(VGE_TDCTL_JUMBO);
1383
1384	sc->vge_ldata.vge_tx_dmamap[idx] = txmap;
1385	sc->vge_ldata.vge_tx_mbuf[idx] = m_head;
1386	sc->vge_ldata.vge_tx_free--;
1387	sc->vge_ldata.vge_tx_list[idx].vge_sts |= htole32(VGE_TDSTS_OWN);
1388
1389	/*
1390	 * Set up hardware VLAN tagging.
1391	 */
1392#ifdef VGE_VLAN
1393	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
1394	if (mtag != NULL)
1395		sc->vge_ldata.vge_tx_list[idx].vge_ctl |=
1396		    htole32(htons(VLAN_TAG_VALUE(mtag)) | VGE_TDCTL_VTAG);
1397#endif
1398
1399	idx++;
1400
1401	return (0);
1402}
1403
1404/*
1405 * Main transmit routine.
1406 */
1407void
1408vge_start(struct ifnet *ifp)
1409{
1410	struct vge_softc	*sc;
1411	struct mbuf		*m_head = NULL;
1412	int			idx, pidx = 0;
1413
1414	sc = ifp->if_softc;
1415
1416	if (!sc->vge_link || ifp->if_flags & IFF_OACTIVE)
1417		return;
1418
1419	if (IFQ_IS_EMPTY(&ifp->if_snd))
1420		return;
1421
1422	idx = sc->vge_ldata.vge_tx_prodidx;
1423
1424	pidx = idx - 1;
1425	if (pidx < 0)
1426		pidx = VGE_TX_DESC_CNT - 1;
1427
1428	while (sc->vge_ldata.vge_tx_mbuf[idx] == NULL) {
1429		IFQ_DEQUEUE(&ifp->if_snd, m_head);
1430		if (m_head == NULL)
1431			break;
1432
1433		/*
1434		 * If there's a BPF listener, bounce a copy of this frame
1435		 * to him.
1436		 */
1437#if NBPFILTER > 0
1438		if (ifp->if_bpf)
1439			bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1440#endif
1441
1442		if (vge_encap(sc, m_head, idx)) {
1443			IF_PREPEND(&ifp->if_snd, m_head);
1444			ifp->if_flags |= IFF_OACTIVE;
1445			break;
1446		}
1447
1448		sc->vge_ldata.vge_tx_list[pidx].vge_frag[0].vge_buflen |=
1449		    htole16(VGE_TXDESC_Q);
1450
1451		pidx = idx;
1452		VGE_TX_DESC_INC(idx);
1453	}
1454
1455	if (idx == sc->vge_ldata.vge_tx_prodidx) {
1456		return;
1457	}
1458
1459	/* Flush the TX descriptors */
1460
1461	bus_dmamap_sync(sc->sc_dmat,
1462	    sc->vge_ldata.vge_tx_list_map,
1463	    0, sc->vge_ldata.vge_tx_list_map->dm_mapsize,
1464	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1465
1466	/* Issue a transmit command. */
1467	CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1468
1469	sc->vge_ldata.vge_tx_prodidx = idx;
1470
1471	/*
1472	 * Use the countdown timer for interrupt moderation.
1473	 * 'TX done' interrupts are disabled. Instead, we reset the
1474	 * countdown timer, which will begin counting until it hits
1475	 * the value in the SSTIMER register, and then trigger an
1476	 * interrupt. Each time we set the TIMER0_ENABLE bit, the
1477	 * the timer count is reloaded. Only when the transmitter
1478	 * is idle will the timer hit 0 and an interrupt fire.
1479	 */
1480	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
1481
1482	/*
1483	 * Set a timeout in case the chip goes out to lunch.
1484	 */
1485	ifp->if_timer = 5;
1486}
1487
1488int
1489vge_init(struct ifnet *ifp)
1490{
1491	struct vge_softc	*sc = ifp->if_softc;
1492	int			i;
1493
1494	/*
1495	 * Cancel pending I/O and free all RX/TX buffers.
1496	 */
1497	vge_stop(sc);
1498	vge_reset(sc);
1499
1500	/* Initialize RX descriptors list */
1501	if (vge_rx_list_init(sc) == ENOBUFS) {
1502		printf("%s: init failed: no memory for RX buffers\n",
1503		    sc->vge_dev.dv_xname);
1504		vge_stop(sc);
1505		return (ENOBUFS);
1506	}
1507	/* Initialize TX descriptors */
1508	if (vge_tx_list_init(sc) == ENOBUFS) {
1509		printf("%s: init failed: no memory for TX buffers\n",
1510		    sc->vge_dev.dv_xname);
1511		vge_stop(sc);
1512		return (ENOBUFS);
1513	}
1514
1515	/* Set our station address */
1516	for (i = 0; i < ETHER_ADDR_LEN; i++)
1517		CSR_WRITE_1(sc, VGE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1518
1519	/*
1520	 * Set receive FIFO threshold. Also allow transmission and
1521	 * reception of VLAN tagged frames.
1522	 */
1523	CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
1524	CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
1525
1526	/* Set DMA burst length */
1527	CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
1528	CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
1529
1530	CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
1531
1532	/* Set collision backoff algorithm */
1533	CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
1534	    VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
1535	CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
1536
1537	/* Disable LPSEL field in priority resolution */
1538	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
1539
1540	/*
1541	 * Load the addresses of the DMA queues into the chip.
1542	 * Note that we only use one transmit queue.
1543	 */
1544
1545	CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
1546	    VGE_ADDR_LO(sc->vge_ldata.vge_tx_listseg.ds_addr));
1547	CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
1548
1549	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
1550	    VGE_ADDR_LO(sc->vge_ldata.vge_rx_listseg.ds_addr));
1551	CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
1552	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
1553
1554	/* Enable and wake up the RX descriptor queue */
1555	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1556	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1557
1558	/* Enable the TX descriptor queue */
1559	CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
1560
1561	/* Set up the receive filter -- allow large frames for VLANs. */
1562	CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
1563
1564	/* If we want promiscuous mode, set the allframes bit. */
1565	if (ifp->if_flags & IFF_PROMISC) {
1566		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
1567	}
1568
1569	/* Set capture broadcast bit to capture broadcast frames. */
1570	if (ifp->if_flags & IFF_BROADCAST) {
1571		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
1572	}
1573
1574	/* Set multicast bit to capture multicast frames. */
1575	if (ifp->if_flags & IFF_MULTICAST) {
1576		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
1577	}
1578
1579	/* Init the cam filter. */
1580	vge_cam_clear(sc);
1581
1582	/* Init the multicast filter. */
1583	vge_setmulti(sc);
1584
1585	/* Enable flow control */
1586
1587	CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
1588
1589	/* Enable jumbo frame reception (if desired) */
1590
1591	/* Start the MAC. */
1592	CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
1593	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
1594	CSR_WRITE_1(sc, VGE_CRS0,
1595	    VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
1596
1597	/*
1598	 * Configure one-shot timer for microsecond
1599	 * resulution and load it for 500 usecs.
1600	 */
1601	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
1602	CSR_WRITE_2(sc, VGE_SSTIMER, 400);
1603
1604	/*
1605	 * Configure interrupt moderation for receive. Enable
1606	 * the holdoff counter and load it, and set the RX
1607	 * suppression count to the number of descriptors we
1608	 * want to allow before triggering an interrupt.
1609	 * The holdoff timer is in units of 20 usecs.
1610	 */
1611
1612#ifdef notyet
1613	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
1614	/* Select the interrupt holdoff timer page. */
1615	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1616	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
1617	CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
1618
1619	/* Enable use of the holdoff timer. */
1620	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
1621	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
1622
1623	/* Select the RX suppression threshold page. */
1624	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1625	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
1626	CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
1627
1628	/* Restore the page select bits. */
1629	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
1630	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
1631#endif
1632
1633	/*
1634	 * Enable interrupts.
1635	 */
1636	CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
1637	CSR_WRITE_4(sc, VGE_ISR, 0);
1638	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1639
1640	/* Restore BMCR state */
1641	mii_mediachg(&sc->sc_mii);
1642
1643	ifp->if_flags |= IFF_RUNNING;
1644	ifp->if_flags &= ~IFF_OACTIVE;
1645
1646	sc->vge_if_flags = 0;
1647	sc->vge_link = 0;
1648
1649	if (!timeout_pending(&sc->timer_handle))
1650		timeout_add(&sc->timer_handle, hz);
1651
1652	return (0);
1653}
1654
1655/*
1656 * Set media options.
1657 */
1658int
1659vge_ifmedia_upd(struct ifnet *ifp)
1660{
1661	struct vge_softc *sc = ifp->if_softc;
1662
1663	return (mii_mediachg(&sc->sc_mii));
1664}
1665
1666/*
1667 * Report current media status.
1668 */
1669void
1670vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1671{
1672	struct vge_softc *sc = ifp->if_softc;
1673
1674	mii_pollstat(&sc->sc_mii);
1675	ifmr->ifm_active = sc->sc_mii.mii_media_active;
1676	ifmr->ifm_status = sc->sc_mii.mii_media_status;
1677}
1678
1679void
1680vge_miibus_statchg(struct device *dev)
1681{
1682	struct vge_softc	*sc = (struct vge_softc *)dev;
1683	struct mii_data		*mii;
1684	struct ifmedia_entry	*ife;
1685
1686	mii = &sc->sc_mii;
1687	ife = mii->mii_media.ifm_cur;
1688
1689	/*
1690	 * If the user manually selects a media mode, we need to turn
1691	 * on the forced MAC mode bit in the DIAGCTL register. If the
1692	 * user happens to choose a full duplex mode, we also need to
1693	 * set the 'force full duplex' bit. This applies only to
1694	 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
1695	 * mode is disabled, and in 1000baseT mode, full duplex is
1696	 * always implied, so we turn on the forced mode bit but leave
1697	 * the FDX bit cleared.
1698	 */
1699
1700	switch (IFM_SUBTYPE(ife->ifm_media)) {
1701	case IFM_AUTO:
1702		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1703		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1704		break;
1705	case IFM_1000_T:
1706		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1707		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1708		break;
1709	case IFM_100_TX:
1710	case IFM_10_T:
1711		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
1712		if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
1713			CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1714		} else {
1715			CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
1716		}
1717		break;
1718	default:
1719		printf("%s: unknown media type: %x\n",
1720		    sc->vge_dev.dv_xname, IFM_SUBTYPE(ife->ifm_media));
1721		break;
1722	}
1723}
1724
1725int
1726vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1727{
1728	struct vge_softc	*sc = ifp->if_softc;
1729	struct ifreq		*ifr = (struct ifreq *) data;
1730	struct ifaddr		*ifa = (struct ifaddr *) data;
1731	int			s, error = 0;
1732
1733	s = splnet();
1734
1735	if ((error = ether_ioctl(ifp, &sc->arpcom, command, data)) > 0) {
1736		splx(s);
1737		return (error);
1738	}
1739
1740	switch (command) {
1741	case SIOCSIFADDR:
1742		ifp->if_flags |= IFF_UP;
1743		switch (ifa->ifa_addr->sa_family) {
1744#ifdef INET
1745		case AF_INET:
1746			vge_init(ifp);
1747			arp_ifinit(&sc->arpcom, ifa);
1748			break;
1749#endif
1750		default:
1751			vge_init(ifp);
1752			break;
1753		}
1754		break;
1755	case SIOCSIFMTU:
1756		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
1757			error = EINVAL;
1758		else if (ifp->if_mtu != ifr->ifr_mtu)
1759			ifp->if_mtu = ifr->ifr_mtu;
1760		break;
1761	case SIOCSIFFLAGS:
1762		if (ifp->if_flags & IFF_UP) {
1763			if (ifp->if_flags & IFF_RUNNING &&
1764			    ifp->if_flags & IFF_PROMISC &&
1765			    !(sc->vge_if_flags & IFF_PROMISC)) {
1766				CSR_SETBIT_1(sc, VGE_RXCTL,
1767				    VGE_RXCTL_RX_PROMISC);
1768				vge_setmulti(sc);
1769			} else if (ifp->if_flags & IFF_RUNNING &&
1770			    !(ifp->if_flags & IFF_PROMISC) &&
1771			    sc->vge_if_flags & IFF_PROMISC) {
1772				CSR_CLRBIT_1(sc, VGE_RXCTL,
1773				    VGE_RXCTL_RX_PROMISC);
1774				vge_setmulti(sc);
1775                        } else
1776				vge_init(ifp);
1777		} else {
1778			if (ifp->if_flags & IFF_RUNNING)
1779				vge_stop(sc);
1780		}
1781		sc->vge_if_flags = ifp->if_flags;
1782		break;
1783	case SIOCADDMULTI:
1784	case SIOCDELMULTI:
1785		error = (command == SIOCADDMULTI) ?
1786		    ether_addmulti(ifr, &sc->arpcom) :
1787		    ether_delmulti(ifr, &sc->arpcom);
1788
1789		if (error == ENETRESET) {
1790			if (ifp->if_flags & IFF_RUNNING)
1791				vge_setmulti(sc);
1792			error = 0;
1793		}
1794		break;
1795	case SIOCGIFMEDIA:
1796	case SIOCSIFMEDIA:
1797		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
1798		break;
1799	default:
1800		error = ENOTTY;
1801		break;
1802	}
1803
1804	splx(s);
1805	return (error);
1806}
1807
1808void
1809vge_watchdog(struct ifnet *ifp)
1810{
1811	struct vge_softc *sc = ifp->if_softc;
1812	int s;
1813
1814	s = splnet();
1815	printf("%s: watchdog timeout\n", sc->vge_dev.dv_xname);
1816	ifp->if_oerrors++;
1817
1818	vge_txeof(sc);
1819	vge_rxeof(sc);
1820
1821	vge_init(ifp);
1822
1823	splx(s);
1824}
1825
1826/*
1827 * Stop the adapter and free any mbufs allocated to the
1828 * RX and TX lists.
1829 */
1830void
1831vge_stop(struct vge_softc *sc)
1832{
1833	int			i;
1834	struct ifnet		*ifp;
1835
1836	ifp = &sc->arpcom.ac_if;
1837	ifp->if_timer = 0;
1838	if (timeout_pending(&sc->timer_handle))
1839		timeout_del(&sc->timer_handle);
1840
1841	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1842
1843	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1844	CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
1845	CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
1846	CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
1847	CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
1848	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
1849
1850	if (sc->vge_head != NULL) {
1851		m_freem(sc->vge_head);
1852		sc->vge_head = sc->vge_tail = NULL;
1853	}
1854
1855	/* Free the TX list buffers. */
1856	for (i = 0; i < VGE_TX_DESC_CNT; i++) {
1857		if (sc->vge_ldata.vge_tx_mbuf[i] != NULL) {
1858			bus_dmamap_unload(sc->sc_dmat,
1859			    sc->vge_ldata.vge_tx_dmamap[i]);
1860			m_freem(sc->vge_ldata.vge_tx_mbuf[i]);
1861			sc->vge_ldata.vge_tx_mbuf[i] = NULL;
1862		}
1863	}
1864
1865	/* Free the RX list buffers. */
1866	for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1867		if (sc->vge_ldata.vge_rx_mbuf[i] != NULL) {
1868			bus_dmamap_unload(sc->sc_dmat,
1869			    sc->vge_ldata.vge_rx_dmamap[i]);
1870			m_freem(sc->vge_ldata.vge_rx_mbuf[i]);
1871			sc->vge_ldata.vge_rx_mbuf[i] = NULL;
1872		}
1873	}
1874}
1875