xref: /freebsd/sys/dev/lge/if_lge.c (revision 1c352ef74a6708cfc6c57f12e51786867bdf7fde)
1c678bc4fSBill Paul /*
2c678bc4fSBill Paul  * Copyright (c) 2001 Wind River Systems
3c678bc4fSBill Paul  * Copyright (c) 1997, 1998, 1999, 2000, 2001
4c678bc4fSBill Paul  *	Bill Paul <william.paul@windriver.com>.  All rights reserved.
5c678bc4fSBill Paul  *
6c678bc4fSBill Paul  * Redistribution and use in source and binary forms, with or without
7c678bc4fSBill Paul  * modification, are permitted provided that the following conditions
8c678bc4fSBill Paul  * are met:
9c678bc4fSBill Paul  * 1. Redistributions of source code must retain the above copyright
10c678bc4fSBill Paul  *    notice, this list of conditions and the following disclaimer.
11c678bc4fSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
12c678bc4fSBill Paul  *    notice, this list of conditions and the following disclaimer in the
13c678bc4fSBill Paul  *    documentation and/or other materials provided with the distribution.
14c678bc4fSBill Paul  * 3. All advertising materials mentioning features or use of this software
15c678bc4fSBill Paul  *    must display the following acknowledgement:
16c678bc4fSBill Paul  *	This product includes software developed by Bill Paul.
17c678bc4fSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
18c678bc4fSBill Paul  *    may be used to endorse or promote products derived from this software
19c678bc4fSBill Paul  *    without specific prior written permission.
20c678bc4fSBill Paul  *
21c678bc4fSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22c678bc4fSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c678bc4fSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c678bc4fSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25c678bc4fSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26c678bc4fSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27c678bc4fSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28c678bc4fSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29c678bc4fSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30c678bc4fSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31c678bc4fSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
32c678bc4fSBill Paul  *
33c678bc4fSBill Paul  * $FreeBSD$
34c678bc4fSBill Paul  */
35c678bc4fSBill Paul 
36c678bc4fSBill Paul /*
37c678bc4fSBill Paul  * Level 1 LXT1001 gigabit ethernet driver for FreeBSD. Public
38c678bc4fSBill Paul  * documentation not available, but ask me nicely.
39c678bc4fSBill Paul  *
40c678bc4fSBill Paul  * Written by Bill Paul <william.paul@windriver.com>
41c678bc4fSBill Paul  * Wind River Systems
42c678bc4fSBill Paul  */
43c678bc4fSBill Paul 
44c678bc4fSBill Paul /*
45c678bc4fSBill Paul  * The Level 1 chip is used on some D-Link, SMC and Addtron NICs.
46c678bc4fSBill Paul  * It's a 64-bit PCI part that supports TCP/IP checksum offload,
47c678bc4fSBill Paul  * VLAN tagging/insertion, GMII and TBI (1000baseX) ports. There
48c678bc4fSBill Paul  * are three supported methods for data transfer between host and
49c678bc4fSBill Paul  * NIC: programmed I/O, traditional scatter/gather DMA and Packet
50c678bc4fSBill Paul  * Propulsion Technology (tm) DMA. The latter mechanism is a form
51c678bc4fSBill Paul  * of double buffer DMA where the packet data is copied to a
52c678bc4fSBill Paul  * pre-allocated DMA buffer who's physical address has been loaded
53c678bc4fSBill Paul  * into a table at device initialization time. The rationale is that
54c678bc4fSBill Paul  * the virtual to physical address translation needed for normal
55c678bc4fSBill Paul  * scatter/gather DMA is more expensive than the data copy needed
56c678bc4fSBill Paul  * for double buffering. This may be true in Windows NT and the like,
57c678bc4fSBill Paul  * but it isn't true for us, at least on the x86 arch. This driver
58c678bc4fSBill Paul  * uses the scatter/gather I/O method for both TX and RX.
59c678bc4fSBill Paul  *
60c678bc4fSBill Paul  * The LXT1001 only supports TCP/IP checksum offload on receive.
61c678bc4fSBill Paul  * Also, the VLAN tagging is done using a 16-entry table which allows
62c678bc4fSBill Paul  * the chip to perform hardware filtering based on VLAN tags. Sadly,
63c678bc4fSBill Paul  * our vlan support doesn't currently play well with this kind of
64c678bc4fSBill Paul  * hardware support.
65c678bc4fSBill Paul  *
66c678bc4fSBill Paul  * Special thanks to:
67c678bc4fSBill Paul  * - Jeff James at Intel, for arranging to have the LXT1001 manual
68c678bc4fSBill Paul  *   released (at long last)
69c678bc4fSBill Paul  * - Beny Chen at D-Link, for actually sending it to me
70c678bc4fSBill Paul  * - Brad Short and Keith Alexis at SMC, for sending me sample
71c678bc4fSBill Paul  *   SMC9462SX and SMC9462TX adapters for testing
72c678bc4fSBill Paul  * - Paul Saab at Y!, for not killing me (though it remains to be seen
73c678bc4fSBill Paul  *   if in fact he did me much of a favor)
74c678bc4fSBill Paul  */
75c678bc4fSBill Paul 
76c678bc4fSBill Paul #include <sys/param.h>
77c678bc4fSBill Paul #include <sys/systm.h>
78c678bc4fSBill Paul #include <sys/sockio.h>
79c678bc4fSBill Paul #include <sys/mbuf.h>
80c678bc4fSBill Paul #include <sys/malloc.h>
81c678bc4fSBill Paul #include <sys/kernel.h>
82c678bc4fSBill Paul #include <sys/socket.h>
83c678bc4fSBill Paul 
84c678bc4fSBill Paul #include <net/if.h>
85c678bc4fSBill Paul #include <net/if_arp.h>
86c678bc4fSBill Paul #include <net/ethernet.h>
87c678bc4fSBill Paul #include <net/if_dl.h>
88c678bc4fSBill Paul #include <net/if_media.h>
89c678bc4fSBill Paul 
90c678bc4fSBill Paul #include <net/bpf.h>
91c678bc4fSBill Paul 
92c678bc4fSBill Paul #include <vm/vm.h>              /* for vtophys */
93c678bc4fSBill Paul #include <vm/pmap.h>            /* for vtophys */
94c678bc4fSBill Paul #include <machine/clock.h>      /* for DELAY */
95c678bc4fSBill Paul #include <machine/bus_pio.h>
96c678bc4fSBill Paul #include <machine/bus_memio.h>
97c678bc4fSBill Paul #include <machine/bus.h>
98c678bc4fSBill Paul #include <machine/resource.h>
99c678bc4fSBill Paul #include <sys/bus.h>
100c678bc4fSBill Paul #include <sys/rman.h>
101c678bc4fSBill Paul 
102c678bc4fSBill Paul #include <dev/mii/mii.h>
103c678bc4fSBill Paul #include <dev/mii/miivar.h>
104c678bc4fSBill Paul 
105c678bc4fSBill Paul #include <pci/pcireg.h>
106c678bc4fSBill Paul #include <pci/pcivar.h>
107c678bc4fSBill Paul 
108c678bc4fSBill Paul #define LGE_USEIOSPACE
109c678bc4fSBill Paul 
110c678bc4fSBill Paul #include <dev/lge/if_lgereg.h>
111c678bc4fSBill Paul 
112c678bc4fSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
113c678bc4fSBill Paul #include "miibus_if.h"
114c678bc4fSBill Paul 
115c678bc4fSBill Paul #ifndef lint
116c678bc4fSBill Paul static const char rcsid[] =
117c678bc4fSBill Paul   "$FreeBSD$";
118c678bc4fSBill Paul #endif
119c678bc4fSBill Paul 
120c678bc4fSBill Paul /*
121c678bc4fSBill Paul  * Various supported device vendors/types and their names.
122c678bc4fSBill Paul  */
123c678bc4fSBill Paul static struct lge_type lge_devs[] = {
124c678bc4fSBill Paul 	{ LGE_VENDORID, LGE_DEVICEID, "Level 1 Gigabit Ethernet" },
125c678bc4fSBill Paul 	{ 0, 0, NULL }
126c678bc4fSBill Paul };
127c678bc4fSBill Paul 
128c678bc4fSBill Paul static int lge_probe		__P((device_t));
129c678bc4fSBill Paul static int lge_attach		__P((device_t));
130c678bc4fSBill Paul static int lge_detach		__P((device_t));
131c678bc4fSBill Paul 
132c678bc4fSBill Paul static int lge_alloc_jumbo_mem	__P((struct lge_softc *));
133c678bc4fSBill Paul static void lge_free_jumbo_mem	__P((struct lge_softc *));
134c678bc4fSBill Paul static void *lge_jalloc		__P((struct lge_softc *));
135c678bc4fSBill Paul static void lge_jfree		__P((caddr_t, void *));
136c678bc4fSBill Paul 
137c678bc4fSBill Paul static int lge_newbuf		__P((struct lge_softc *,
138c678bc4fSBill Paul 					struct lge_rx_desc *,
139c678bc4fSBill Paul 					struct mbuf *));
140c678bc4fSBill Paul static int lge_encap		__P((struct lge_softc *,
141c678bc4fSBill Paul 					struct mbuf *, u_int32_t *));
142c678bc4fSBill Paul static void lge_rxeof		__P((struct lge_softc *, int));
143c678bc4fSBill Paul static void lge_rxeoc		__P((struct lge_softc *));
144c678bc4fSBill Paul static void lge_txeof		__P((struct lge_softc *));
145c678bc4fSBill Paul static void lge_intr		__P((void *));
146c678bc4fSBill Paul static void lge_tick		__P((void *));
147c678bc4fSBill Paul static void lge_start		__P((struct ifnet *));
148c678bc4fSBill Paul static int lge_ioctl		__P((struct ifnet *, u_long, caddr_t));
149c678bc4fSBill Paul static void lge_init		__P((void *));
150c678bc4fSBill Paul static void lge_stop		__P((struct lge_softc *));
151c678bc4fSBill Paul static void lge_watchdog		__P((struct ifnet *));
152c678bc4fSBill Paul static void lge_shutdown		__P((device_t));
153c678bc4fSBill Paul static int lge_ifmedia_upd	__P((struct ifnet *));
154c678bc4fSBill Paul static void lge_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
155c678bc4fSBill Paul 
156c678bc4fSBill Paul static void lge_eeprom_getword	__P((struct lge_softc *, int, u_int16_t *));
157c678bc4fSBill Paul static void lge_read_eeprom	__P((struct lge_softc *, caddr_t, int,
158c678bc4fSBill Paul 							int, int));
159c678bc4fSBill Paul 
160c678bc4fSBill Paul static int lge_miibus_readreg	__P((device_t, int, int));
161c678bc4fSBill Paul static int lge_miibus_writereg	__P((device_t, int, int, int));
162c678bc4fSBill Paul static void lge_miibus_statchg	__P((device_t));
163c678bc4fSBill Paul 
164c678bc4fSBill Paul static void lge_setmulti	__P((struct lge_softc *));
165c678bc4fSBill Paul static u_int32_t lge_crc	__P((struct lge_softc *, caddr_t));
166c678bc4fSBill Paul static void lge_reset		__P((struct lge_softc *));
167c678bc4fSBill Paul static int lge_list_rx_init	__P((struct lge_softc *));
168c678bc4fSBill Paul static int lge_list_tx_init	__P((struct lge_softc *));
169c678bc4fSBill Paul 
170c678bc4fSBill Paul #ifdef LGE_USEIOSPACE
171c678bc4fSBill Paul #define LGE_RES			SYS_RES_IOPORT
172c678bc4fSBill Paul #define LGE_RID			LGE_PCI_LOIO
173c678bc4fSBill Paul #else
174c678bc4fSBill Paul #define LGE_RES			SYS_RES_MEMORY
175c678bc4fSBill Paul #define LGE_RID			LGE_PCI_LOMEM
176c678bc4fSBill Paul #endif
177c678bc4fSBill Paul 
178c678bc4fSBill Paul static device_method_t lge_methods[] = {
179c678bc4fSBill Paul 	/* Device interface */
180c678bc4fSBill Paul 	DEVMETHOD(device_probe,		lge_probe),
181c678bc4fSBill Paul 	DEVMETHOD(device_attach,	lge_attach),
182c678bc4fSBill Paul 	DEVMETHOD(device_detach,	lge_detach),
183c678bc4fSBill Paul 	DEVMETHOD(device_shutdown,	lge_shutdown),
184c678bc4fSBill Paul 
185c678bc4fSBill Paul 	/* bus interface */
186c678bc4fSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
187c678bc4fSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
188c678bc4fSBill Paul 
189c678bc4fSBill Paul 	/* MII interface */
190c678bc4fSBill Paul 	DEVMETHOD(miibus_readreg,	lge_miibus_readreg),
191c678bc4fSBill Paul 	DEVMETHOD(miibus_writereg,	lge_miibus_writereg),
192c678bc4fSBill Paul 	DEVMETHOD(miibus_statchg,	lge_miibus_statchg),
193c678bc4fSBill Paul 
194c678bc4fSBill Paul 	{ 0, 0 }
195c678bc4fSBill Paul };
196c678bc4fSBill Paul 
197c678bc4fSBill Paul static driver_t lge_driver = {
198c678bc4fSBill Paul 	"lge",
199c678bc4fSBill Paul 	lge_methods,
200c678bc4fSBill Paul 	sizeof(struct lge_softc)
201c678bc4fSBill Paul };
202c678bc4fSBill Paul 
203c678bc4fSBill Paul static devclass_t lge_devclass;
204c678bc4fSBill Paul 
205c678bc4fSBill Paul DRIVER_MODULE(if_lge, pci, lge_driver, lge_devclass, 0, 0);
206c678bc4fSBill Paul DRIVER_MODULE(miibus, lge, miibus_driver, miibus_devclass, 0, 0);
207c678bc4fSBill Paul 
208c678bc4fSBill Paul #define LGE_SETBIT(sc, reg, x)				\
209c678bc4fSBill Paul 	CSR_WRITE_4(sc, reg,				\
210c678bc4fSBill Paul 		CSR_READ_4(sc, reg) | (x))
211c678bc4fSBill Paul 
212c678bc4fSBill Paul #define LGE_CLRBIT(sc, reg, x)				\
213c678bc4fSBill Paul 	CSR_WRITE_4(sc, reg,				\
214c678bc4fSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
215c678bc4fSBill Paul 
216c678bc4fSBill Paul #define SIO_SET(x)					\
217c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) | x)
218c678bc4fSBill Paul 
219c678bc4fSBill Paul #define SIO_CLR(x)					\
220c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) & ~x)
221c678bc4fSBill Paul 
222c678bc4fSBill Paul /*
223c678bc4fSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
224c678bc4fSBill Paul  */
225c678bc4fSBill Paul static void lge_eeprom_getword(sc, addr, dest)
226c678bc4fSBill Paul 	struct lge_softc	*sc;
227c678bc4fSBill Paul 	int			addr;
228c678bc4fSBill Paul 	u_int16_t		*dest;
229c678bc4fSBill Paul {
230c678bc4fSBill Paul 	register int		i;
231c678bc4fSBill Paul 	u_int32_t		val;
232c678bc4fSBill Paul 
233c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_EECTL, LGE_EECTL_CMD_READ|
234c678bc4fSBill Paul 	    LGE_EECTL_SINGLEACCESS|((addr >> 1) << 8));
235c678bc4fSBill Paul 
236c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
237c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_EECTL) & LGE_EECTL_CMD_READ))
238c678bc4fSBill Paul 			break;
239c678bc4fSBill Paul 
240c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
241c678bc4fSBill Paul 		printf("lge%d: EEPROM read timed out\n", sc->lge_unit);
242c678bc4fSBill Paul 		return;
243c678bc4fSBill Paul 	}
244c678bc4fSBill Paul 
245c678bc4fSBill Paul 	val = CSR_READ_4(sc, LGE_EEDATA);
246c678bc4fSBill Paul 
247c678bc4fSBill Paul 	if (addr & 1)
248c678bc4fSBill Paul 		*dest = (val >> 16) & 0xFFFF;
249c678bc4fSBill Paul 	else
250c678bc4fSBill Paul 		*dest = val & 0xFFFF;
251c678bc4fSBill Paul 
252c678bc4fSBill Paul 	return;
253c678bc4fSBill Paul }
254c678bc4fSBill Paul 
255c678bc4fSBill Paul /*
256c678bc4fSBill Paul  * Read a sequence of words from the EEPROM.
257c678bc4fSBill Paul  */
258c678bc4fSBill Paul static void lge_read_eeprom(sc, dest, off, cnt, swap)
259c678bc4fSBill Paul 	struct lge_softc	*sc;
260c678bc4fSBill Paul 	caddr_t			dest;
261c678bc4fSBill Paul 	int			off;
262c678bc4fSBill Paul 	int			cnt;
263c678bc4fSBill Paul 	int			swap;
264c678bc4fSBill Paul {
265c678bc4fSBill Paul 	int			i;
266c678bc4fSBill Paul 	u_int16_t		word = 0, *ptr;
267c678bc4fSBill Paul 
268c678bc4fSBill Paul 	for (i = 0; i < cnt; i++) {
269c678bc4fSBill Paul 		lge_eeprom_getword(sc, off + i, &word);
270c678bc4fSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
271c678bc4fSBill Paul 		if (swap)
272c678bc4fSBill Paul 			*ptr = ntohs(word);
273c678bc4fSBill Paul 		else
274c678bc4fSBill Paul 			*ptr = word;
275c678bc4fSBill Paul 	}
276c678bc4fSBill Paul 
277c678bc4fSBill Paul 	return;
278c678bc4fSBill Paul }
279c678bc4fSBill Paul 
280c678bc4fSBill Paul static int lge_miibus_readreg(dev, phy, reg)
281c678bc4fSBill Paul 	device_t		dev;
282c678bc4fSBill Paul 	int			phy, reg;
283c678bc4fSBill Paul {
284c678bc4fSBill Paul 	struct lge_softc	*sc;
285c678bc4fSBill Paul 	int			i;
286c678bc4fSBill Paul 
287c678bc4fSBill Paul 	sc = device_get_softc(dev);
288c678bc4fSBill Paul 
289c678bc4fSBill Paul 	/*
290c678bc4fSBill Paul 	 * If we have a non-PCS PHY, pretend that the internal
291c678bc4fSBill Paul 	 * autoneg stuff at PHY address 0 isn't there so that
292c678bc4fSBill Paul 	 * the miibus code will find only the GMII PHY.
293c678bc4fSBill Paul 	 */
294c678bc4fSBill Paul 	if (sc->lge_pcs == 0 && phy == 0)
295c678bc4fSBill Paul 		return(0);
296c678bc4fSBill Paul 
297c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_GMIICTL, (phy << 8) | reg | LGE_GMIICMD_READ);
298c678bc4fSBill Paul 
299c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
300c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
301c678bc4fSBill Paul 			break;
302c678bc4fSBill Paul 
303c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
304c678bc4fSBill Paul 		printf("lge%d: PHY read timed out\n", sc->lge_unit);
305c678bc4fSBill Paul 		return(0);
306c678bc4fSBill Paul 	}
307c678bc4fSBill Paul 
308c678bc4fSBill Paul 	return(CSR_READ_4(sc, LGE_GMIICTL) >> 16);
309c678bc4fSBill Paul }
310c678bc4fSBill Paul 
311c678bc4fSBill Paul static int lge_miibus_writereg(dev, phy, reg, data)
312c678bc4fSBill Paul 	device_t		dev;
313c678bc4fSBill Paul 	int			phy, reg, data;
314c678bc4fSBill Paul {
315c678bc4fSBill Paul 	struct lge_softc	*sc;
316c678bc4fSBill Paul 	int			i;
317c678bc4fSBill Paul 
318c678bc4fSBill Paul 	sc = device_get_softc(dev);
319c678bc4fSBill Paul 
320c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_GMIICTL,
321c678bc4fSBill Paul 	    (data << 16) | (phy << 8) | reg | LGE_GMIICMD_WRITE);
322c678bc4fSBill Paul 
323c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
324c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
325c678bc4fSBill Paul 			break;
326c678bc4fSBill Paul 
327c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
328c678bc4fSBill Paul 		printf("lge%d: PHY write timed out\n", sc->lge_unit);
329c678bc4fSBill Paul 		return(0);
330c678bc4fSBill Paul 	}
331c678bc4fSBill Paul 
332c678bc4fSBill Paul 	return(0);
333c678bc4fSBill Paul }
334c678bc4fSBill Paul 
335c678bc4fSBill Paul static void lge_miibus_statchg(dev)
336c678bc4fSBill Paul 	device_t		dev;
337c678bc4fSBill Paul {
338c678bc4fSBill Paul 	struct lge_softc	*sc;
339c678bc4fSBill Paul 	struct mii_data		*mii;
340c678bc4fSBill Paul 
341c678bc4fSBill Paul 	sc = device_get_softc(dev);
342c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
343c678bc4fSBill Paul 
344c678bc4fSBill Paul 	LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_SPEED);
345c678bc4fSBill Paul 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
346c678bc4fSBill Paul 	case IFM_1000_TX:
347c678bc4fSBill Paul 	case IFM_1000_SX:
348c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
349c678bc4fSBill Paul 		break;
350c678bc4fSBill Paul 	case IFM_100_TX:
351c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_100);
352c678bc4fSBill Paul 		break;
353c678bc4fSBill Paul 	case IFM_10_T:
354c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_10);
355c678bc4fSBill Paul 		break;
356c678bc4fSBill Paul 	default:
357c678bc4fSBill Paul 		/*
358c678bc4fSBill Paul 		 * Choose something, even if it's wrong. Clearing
359c678bc4fSBill Paul 		 * all the bits will hose autoneg on the internal
360c678bc4fSBill Paul 		 * PHY.
361c678bc4fSBill Paul 		 */
362c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
363c678bc4fSBill Paul 		break;
364c678bc4fSBill Paul 	}
365c678bc4fSBill Paul 
366c678bc4fSBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
367c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
368c678bc4fSBill Paul 	} else {
369c678bc4fSBill Paul 		LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
370c678bc4fSBill Paul 	}
371c678bc4fSBill Paul 
372c678bc4fSBill Paul 	return;
373c678bc4fSBill Paul }
374c678bc4fSBill Paul 
375c678bc4fSBill Paul static u_int32_t lge_crc(sc, addr)
376c678bc4fSBill Paul 	struct lge_softc	*sc;
377c678bc4fSBill Paul 	caddr_t			addr;
378c678bc4fSBill Paul {
379c678bc4fSBill Paul 	u_int32_t		crc, carry;
380c678bc4fSBill Paul 	int			i, j;
381c678bc4fSBill Paul 	u_int8_t		c;
382c678bc4fSBill Paul 
383c678bc4fSBill Paul 	/* Compute CRC for the address value. */
384c678bc4fSBill Paul 	crc = 0xFFFFFFFF; /* initial value */
385c678bc4fSBill Paul 
386c678bc4fSBill Paul 	for (i = 0; i < 6; i++) {
387c678bc4fSBill Paul 		c = *(addr + i);
388c678bc4fSBill Paul 		for (j = 0; j < 8; j++) {
389c678bc4fSBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
390c678bc4fSBill Paul 			crc <<= 1;
391c678bc4fSBill Paul 			c >>= 1;
392c678bc4fSBill Paul 			if (carry)
393c678bc4fSBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
394c678bc4fSBill Paul 		}
395c678bc4fSBill Paul 	}
396c678bc4fSBill Paul 
397c678bc4fSBill Paul 	/*
398c678bc4fSBill Paul 	 * return the filter bit position
399c678bc4fSBill Paul 	 */
400c678bc4fSBill Paul 	return((crc >> 26) & 0x0000003F);
401c678bc4fSBill Paul }
402c678bc4fSBill Paul 
403c678bc4fSBill Paul static void lge_setmulti(sc)
404c678bc4fSBill Paul 	struct lge_softc	*sc;
405c678bc4fSBill Paul {
406c678bc4fSBill Paul 	struct ifnet		*ifp;
407c678bc4fSBill Paul 	struct ifmultiaddr	*ifma;
408c678bc4fSBill Paul 	u_int32_t		h = 0, hashes[2] = { 0, 0 };
409c678bc4fSBill Paul 
410c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
411c678bc4fSBill Paul 
412c678bc4fSBill Paul 	/* Make sure multicast hash table is enabled. */
413c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_MCAST);
414c678bc4fSBill Paul 
415c678bc4fSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
416c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MAR0, 0xFFFFFFFF);
417c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MAR1, 0xFFFFFFFF);
418c678bc4fSBill Paul 		return;
419c678bc4fSBill Paul 	}
420c678bc4fSBill Paul 
421c678bc4fSBill Paul 	/* first, zot all the existing hash bits */
422c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR0, 0);
423c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR1, 0);
424c678bc4fSBill Paul 
425c678bc4fSBill Paul 	/* now program new ones */
426c678bc4fSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
427c678bc4fSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
428c678bc4fSBill Paul 			continue;
429c678bc4fSBill Paul 		h = lge_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
430c678bc4fSBill Paul 		if (h < 32)
431c678bc4fSBill Paul 			hashes[0] |= (1 << h);
432c678bc4fSBill Paul 		else
433c678bc4fSBill Paul 			hashes[1] |= (1 << (h - 32));
434c678bc4fSBill Paul 	}
435c678bc4fSBill Paul 
436c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR0, hashes[0]);
437c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR1, hashes[1]);
438c678bc4fSBill Paul 
439c678bc4fSBill Paul 	return;
440c678bc4fSBill Paul }
441c678bc4fSBill Paul 
442c678bc4fSBill Paul static void lge_reset(sc)
443c678bc4fSBill Paul 	struct lge_softc	*sc;
444c678bc4fSBill Paul {
445c678bc4fSBill Paul 	register int		i;
446c678bc4fSBill Paul 
447c678bc4fSBill Paul 	LGE_SETBIT(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_SOFTRST);
448c678bc4fSBill Paul 
449c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++) {
450c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_MODE1) & LGE_MODE1_SOFTRST))
451c678bc4fSBill Paul 			break;
452c678bc4fSBill Paul 	}
453c678bc4fSBill Paul 
454c678bc4fSBill Paul 	if (i == LGE_TIMEOUT)
455c678bc4fSBill Paul 		printf("lge%d: reset never completed\n", sc->lge_unit);
456c678bc4fSBill Paul 
457c678bc4fSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
458c678bc4fSBill Paul 	DELAY(1000);
459c678bc4fSBill Paul 
460c678bc4fSBill Paul         return;
461c678bc4fSBill Paul }
462c678bc4fSBill Paul 
463c678bc4fSBill Paul /*
464c678bc4fSBill Paul  * Probe for a Level 1 chip. Check the PCI vendor and device
465c678bc4fSBill Paul  * IDs against our list and return a device name if we find a match.
466c678bc4fSBill Paul  */
467c678bc4fSBill Paul static int lge_probe(dev)
468c678bc4fSBill Paul 	device_t		dev;
469c678bc4fSBill Paul {
470c678bc4fSBill Paul 	struct lge_type		*t;
471c678bc4fSBill Paul 
472c678bc4fSBill Paul 	t = lge_devs;
473c678bc4fSBill Paul 
474c678bc4fSBill Paul 	while(t->lge_name != NULL) {
475c678bc4fSBill Paul 		if ((pci_get_vendor(dev) == t->lge_vid) &&
476c678bc4fSBill Paul 		    (pci_get_device(dev) == t->lge_did)) {
477c678bc4fSBill Paul 			device_set_desc(dev, t->lge_name);
478c678bc4fSBill Paul 			return(0);
479c678bc4fSBill Paul 		}
480c678bc4fSBill Paul 		t++;
481c678bc4fSBill Paul 	}
482c678bc4fSBill Paul 
483c678bc4fSBill Paul 	return(ENXIO);
484c678bc4fSBill Paul }
485c678bc4fSBill Paul 
486c678bc4fSBill Paul /*
487c678bc4fSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
488c678bc4fSBill Paul  * setup and ethernet/BPF attach.
489c678bc4fSBill Paul  */
490c678bc4fSBill Paul static int lge_attach(dev)
491c678bc4fSBill Paul 	device_t		dev;
492c678bc4fSBill Paul {
493c678bc4fSBill Paul 	int			s;
494c678bc4fSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
495c678bc4fSBill Paul 	u_int32_t		command;
496c678bc4fSBill Paul 	struct lge_softc	*sc;
497c678bc4fSBill Paul 	struct ifnet		*ifp;
498c678bc4fSBill Paul 	int			unit, error = 0, rid;
499c678bc4fSBill Paul 
500c678bc4fSBill Paul 	s = splimp();
501c678bc4fSBill Paul 
502c678bc4fSBill Paul 	sc = device_get_softc(dev);
503c678bc4fSBill Paul 	unit = device_get_unit(dev);
504c678bc4fSBill Paul 	bzero(sc, sizeof(struct lge_softc));
505c678bc4fSBill Paul 
506c678bc4fSBill Paul 	/*
507c678bc4fSBill Paul 	 * Handle power management nonsense.
508c678bc4fSBill Paul 	 */
509c678bc4fSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
510c678bc4fSBill Paul 		u_int32_t		iobase, membase, irq;
511c678bc4fSBill Paul 
512c678bc4fSBill Paul 		/* Save important PCI config data. */
513c678bc4fSBill Paul 		iobase = pci_read_config(dev, LGE_PCI_LOIO, 4);
514c678bc4fSBill Paul 		membase = pci_read_config(dev, LGE_PCI_LOMEM, 4);
515c678bc4fSBill Paul 		irq = pci_read_config(dev, LGE_PCI_INTLINE, 4);
516c678bc4fSBill Paul 
517c678bc4fSBill Paul 		/* Reset the power state. */
518c678bc4fSBill Paul 		printf("lge%d: chip is in D%d power mode "
519c678bc4fSBill Paul 		    "-- setting to D0\n", unit,
520c678bc4fSBill Paul 		    pci_get_powerstate(dev));
521c678bc4fSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
522c678bc4fSBill Paul 
523c678bc4fSBill Paul 		/* Restore PCI config data. */
524c678bc4fSBill Paul 		pci_write_config(dev, LGE_PCI_LOIO, iobase, 4);
525c678bc4fSBill Paul 		pci_write_config(dev, LGE_PCI_LOMEM, membase, 4);
526c678bc4fSBill Paul 		pci_write_config(dev, LGE_PCI_INTLINE, irq, 4);
527c678bc4fSBill Paul 	}
528c678bc4fSBill Paul 
529c678bc4fSBill Paul 	/*
530c678bc4fSBill Paul 	 * Map control/status registers.
531c678bc4fSBill Paul 	 */
532c678bc4fSBill Paul 	pci_enable_busmaster(dev);
533c678bc4fSBill Paul 	pci_enable_io(dev, PCIM_CMD_PORTEN);
534c678bc4fSBill Paul 	pci_enable_io(dev, PCIM_CMD_MEMEN);
535c678bc4fSBill Paul 	command = pci_read_config(dev, PCIR_COMMAND, 4);
536c678bc4fSBill Paul 
537c678bc4fSBill Paul #ifdef LGE_USEIOSPACE
538c678bc4fSBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
539c678bc4fSBill Paul 		printf("lge%d: failed to enable I/O ports!\n", unit);
540c678bc4fSBill Paul 		error = ENXIO;;
541c678bc4fSBill Paul 		goto fail;
542c678bc4fSBill Paul 	}
543c678bc4fSBill Paul #else
544c678bc4fSBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
545c678bc4fSBill Paul 		printf("lge%d: failed to enable memory mapping!\n", unit);
546c678bc4fSBill Paul 		error = ENXIO;;
547c678bc4fSBill Paul 		goto fail;
548c678bc4fSBill Paul 	}
549c678bc4fSBill Paul #endif
550c678bc4fSBill Paul 
551c678bc4fSBill Paul 	rid = LGE_RID;
552c678bc4fSBill Paul 	sc->lge_res = bus_alloc_resource(dev, LGE_RES, &rid,
553c678bc4fSBill Paul 	    0, ~0, 1, RF_ACTIVE);
554c678bc4fSBill Paul 
555c678bc4fSBill Paul 	if (sc->lge_res == NULL) {
556c678bc4fSBill Paul 		printf("lge%d: couldn't map ports/memory\n", unit);
557c678bc4fSBill Paul 		error = ENXIO;
558c678bc4fSBill Paul 		goto fail;
559c678bc4fSBill Paul 	}
560c678bc4fSBill Paul 
561c678bc4fSBill Paul 	sc->lge_btag = rman_get_bustag(sc->lge_res);
562c678bc4fSBill Paul 	sc->lge_bhandle = rman_get_bushandle(sc->lge_res);
563c678bc4fSBill Paul 
564c678bc4fSBill Paul 	/* Allocate interrupt */
565c678bc4fSBill Paul 	rid = 0;
566c678bc4fSBill Paul 	sc->lge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
567c678bc4fSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
568c678bc4fSBill Paul 
569c678bc4fSBill Paul 	if (sc->lge_irq == NULL) {
570c678bc4fSBill Paul 		printf("lge%d: couldn't map interrupt\n", unit);
571c678bc4fSBill Paul 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
572c678bc4fSBill Paul 		error = ENXIO;
573c678bc4fSBill Paul 		goto fail;
574c678bc4fSBill Paul 	}
575c678bc4fSBill Paul 
576c678bc4fSBill Paul 	error = bus_setup_intr(dev, sc->lge_irq, INTR_TYPE_NET,
577c678bc4fSBill Paul 	    lge_intr, sc, &sc->lge_intrhand);
578c678bc4fSBill Paul 
579c678bc4fSBill Paul 	if (error) {
580c678bc4fSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
581c678bc4fSBill Paul 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
582c678bc4fSBill Paul 		printf("lge%d: couldn't set up irq\n", unit);
583c678bc4fSBill Paul 		goto fail;
584c678bc4fSBill Paul 	}
585c678bc4fSBill Paul 
586c678bc4fSBill Paul 	/* Reset the adapter. */
587c678bc4fSBill Paul 	lge_reset(sc);
588c678bc4fSBill Paul 
589c678bc4fSBill Paul 	/*
590c678bc4fSBill Paul 	 * Get station address from the EEPROM.
591c678bc4fSBill Paul 	 */
592c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[0], LGE_EE_NODEADDR_0, 1, 0);
593c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[2], LGE_EE_NODEADDR_1, 1, 0);
594c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[4], LGE_EE_NODEADDR_2, 1, 0);
595c678bc4fSBill Paul 
596c678bc4fSBill Paul 	/*
597c678bc4fSBill Paul 	 * A Level 1 chip was detected. Inform the world.
598c678bc4fSBill Paul 	 */
599c678bc4fSBill Paul 	printf("lge%d: Ethernet address: %6D\n", unit, eaddr, ":");
600c678bc4fSBill Paul 
601c678bc4fSBill Paul 	sc->lge_unit = unit;
602c678bc4fSBill Paul 	callout_handle_init(&sc->lge_stat_ch);
603c678bc4fSBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
604c678bc4fSBill Paul 
605c678bc4fSBill Paul 	sc->lge_ldata = contigmalloc(sizeof(struct lge_list_data), M_DEVBUF,
606c678bc4fSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
607c678bc4fSBill Paul 
608c678bc4fSBill Paul 	if (sc->lge_ldata == NULL) {
609c678bc4fSBill Paul 		printf("lge%d: no memory for list buffers!\n", unit);
610c678bc4fSBill Paul 		bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
611c678bc4fSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
612c678bc4fSBill Paul 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
613c678bc4fSBill Paul 		error = ENXIO;
614c678bc4fSBill Paul 		goto fail;
615c678bc4fSBill Paul 	}
616c678bc4fSBill Paul 	bzero(sc->lge_ldata, sizeof(struct lge_list_data));
617c678bc4fSBill Paul 
618c678bc4fSBill Paul 	/* Try to allocate memory for jumbo buffers. */
619c678bc4fSBill Paul 	if (lge_alloc_jumbo_mem(sc)) {
620c678bc4fSBill Paul 		printf("lge%d: jumbo buffer allocation failed\n",
621c678bc4fSBill Paul                     sc->lge_unit);
622c678bc4fSBill Paul 		contigfree(sc->lge_ldata,
623c678bc4fSBill Paul 		    sizeof(struct lge_list_data), M_DEVBUF);
624c678bc4fSBill Paul 		bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
625c678bc4fSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
626c678bc4fSBill Paul 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
627c678bc4fSBill Paul 		error = ENXIO;
628c678bc4fSBill Paul 		goto fail;
629c678bc4fSBill Paul 	}
630c678bc4fSBill Paul 
631c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
632c678bc4fSBill Paul 	ifp->if_softc = sc;
633c678bc4fSBill Paul 	ifp->if_unit = unit;
634c678bc4fSBill Paul 	ifp->if_name = "lge";
635c678bc4fSBill Paul 	ifp->if_mtu = ETHERMTU;
636c678bc4fSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
637c678bc4fSBill Paul 	ifp->if_ioctl = lge_ioctl;
638c678bc4fSBill Paul 	ifp->if_output = ether_output;
639c678bc4fSBill Paul 	ifp->if_start = lge_start;
640c678bc4fSBill Paul 	ifp->if_watchdog = lge_watchdog;
641c678bc4fSBill Paul 	ifp->if_init = lge_init;
642c678bc4fSBill Paul 	ifp->if_baudrate = 1000000000;
643c678bc4fSBill Paul 	ifp->if_snd.ifq_maxlen = LGE_TX_LIST_CNT - 1;
644c678bc4fSBill Paul 
645c678bc4fSBill Paul 	if (CSR_READ_4(sc, LGE_GMIIMODE) & LGE_GMIIMODE_PCSENH)
646c678bc4fSBill Paul 		sc->lge_pcs = 1;
647c678bc4fSBill Paul 	else
648c678bc4fSBill Paul 		sc->lge_pcs = 0;
649c678bc4fSBill Paul 
650c678bc4fSBill Paul 	/*
651c678bc4fSBill Paul 	 * Do MII setup.
652c678bc4fSBill Paul 	 */
653c678bc4fSBill Paul 	if (mii_phy_probe(dev, &sc->lge_miibus,
654c678bc4fSBill Paul 	    lge_ifmedia_upd, lge_ifmedia_sts)) {
655c678bc4fSBill Paul 		printf("lge%d: MII without any PHY!\n", sc->lge_unit);
656c678bc4fSBill Paul 		contigfree(sc->lge_ldata,
657c678bc4fSBill Paul 		    sizeof(struct lge_list_data), M_DEVBUF);
658c678bc4fSBill Paul 		lge_free_jumbo_mem(sc);
659c678bc4fSBill Paul 		bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
660c678bc4fSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
661c678bc4fSBill Paul 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
662c678bc4fSBill Paul 		error = ENXIO;
663c678bc4fSBill Paul 		goto fail;
664c678bc4fSBill Paul 	}
665c678bc4fSBill Paul 
666c678bc4fSBill Paul 	/*
667c678bc4fSBill Paul 	 * Call MI attach routine.
668c678bc4fSBill Paul 	 */
669c678bc4fSBill Paul 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
670c678bc4fSBill Paul 	callout_handle_init(&sc->lge_stat_ch);
671c678bc4fSBill Paul 
672c678bc4fSBill Paul fail:
673c678bc4fSBill Paul 	splx(s);
674c678bc4fSBill Paul 	return(error);
675c678bc4fSBill Paul }
676c678bc4fSBill Paul 
677c678bc4fSBill Paul static int lge_detach(dev)
678c678bc4fSBill Paul 	device_t		dev;
679c678bc4fSBill Paul {
680c678bc4fSBill Paul 	struct lge_softc	*sc;
681c678bc4fSBill Paul 	struct ifnet		*ifp;
682c678bc4fSBill Paul 	int			s;
683c678bc4fSBill Paul 
684c678bc4fSBill Paul 	s = splimp();
685c678bc4fSBill Paul 
686c678bc4fSBill Paul 	sc = device_get_softc(dev);
687c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
688c678bc4fSBill Paul 
689c678bc4fSBill Paul 	lge_reset(sc);
690c678bc4fSBill Paul 	lge_stop(sc);
691c678bc4fSBill Paul 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
692c678bc4fSBill Paul 
693c678bc4fSBill Paul 	bus_generic_detach(dev);
694c678bc4fSBill Paul 	device_delete_child(dev, sc->lge_miibus);
695c678bc4fSBill Paul 
696c678bc4fSBill Paul 	bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
697c678bc4fSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
698c678bc4fSBill Paul 	bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
699c678bc4fSBill Paul 
700c678bc4fSBill Paul 	contigfree(sc->lge_ldata, sizeof(struct lge_list_data), M_DEVBUF);
701c678bc4fSBill Paul 	lge_free_jumbo_mem(sc);
702c678bc4fSBill Paul 
703c678bc4fSBill Paul 	splx(s);
704c678bc4fSBill Paul 
705c678bc4fSBill Paul 	return(0);
706c678bc4fSBill Paul }
707c678bc4fSBill Paul 
708c678bc4fSBill Paul /*
709c678bc4fSBill Paul  * Initialize the transmit descriptors.
710c678bc4fSBill Paul  */
711c678bc4fSBill Paul static int lge_list_tx_init(sc)
712c678bc4fSBill Paul 	struct lge_softc	*sc;
713c678bc4fSBill Paul {
714c678bc4fSBill Paul 	struct lge_list_data	*ld;
715c678bc4fSBill Paul 	struct lge_ring_data	*cd;
716c678bc4fSBill Paul 	int			i;
717c678bc4fSBill Paul 
718c678bc4fSBill Paul 	cd = &sc->lge_cdata;
719c678bc4fSBill Paul 	ld = sc->lge_ldata;
720c678bc4fSBill Paul 	for (i = 0; i < LGE_TX_LIST_CNT; i++) {
721c678bc4fSBill Paul 		ld->lge_tx_list[i].lge_mbuf = NULL;
722c678bc4fSBill Paul 		ld->lge_tx_list[i].lge_ctl = 0;
723c678bc4fSBill Paul 	}
724c678bc4fSBill Paul 
725c678bc4fSBill Paul 	cd->lge_tx_prod = cd->lge_tx_cons = 0;
726c678bc4fSBill Paul 
727c678bc4fSBill Paul 	return(0);
728c678bc4fSBill Paul }
729c678bc4fSBill Paul 
730c678bc4fSBill Paul 
731c678bc4fSBill Paul /*
732c678bc4fSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
733c678bc4fSBill Paul  * we arralge the descriptors in a closed ring, so that the last descriptor
734c678bc4fSBill Paul  * points back to the first.
735c678bc4fSBill Paul  */
736c678bc4fSBill Paul static int lge_list_rx_init(sc)
737c678bc4fSBill Paul 	struct lge_softc	*sc;
738c678bc4fSBill Paul {
739c678bc4fSBill Paul 	struct lge_list_data	*ld;
740c678bc4fSBill Paul 	struct lge_ring_data	*cd;
741c678bc4fSBill Paul 	int			i;
742c678bc4fSBill Paul 
743c678bc4fSBill Paul 	ld = sc->lge_ldata;
744c678bc4fSBill Paul 	cd = &sc->lge_cdata;
745c678bc4fSBill Paul 
746c678bc4fSBill Paul 	cd->lge_rx_prod = cd->lge_rx_cons = 0;
747c678bc4fSBill Paul 
748c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
749c678bc4fSBill Paul 
750c678bc4fSBill Paul 	for (i = 0; i < LGE_RX_LIST_CNT; i++) {
751c678bc4fSBill Paul 		if (CSR_READ_1(sc, LGE_RXCMDFREE_8BIT) == 0)
752c678bc4fSBill Paul 			break;
753c678bc4fSBill Paul 		if (lge_newbuf(sc, &ld->lge_rx_list[i], NULL) == ENOBUFS)
754c678bc4fSBill Paul 			return(ENOBUFS);
755c678bc4fSBill Paul 	}
756c678bc4fSBill Paul 
757c678bc4fSBill Paul 	/* Clear possible 'rx command queue empty' interrupt. */
758c678bc4fSBill Paul 	CSR_READ_4(sc, LGE_ISR);
759c678bc4fSBill Paul 
760c678bc4fSBill Paul 	return(0);
761c678bc4fSBill Paul }
762c678bc4fSBill Paul 
763c678bc4fSBill Paul /*
764c678bc4fSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
765c678bc4fSBill Paul  */
766c678bc4fSBill Paul static int lge_newbuf(sc, c, m)
767c678bc4fSBill Paul 	struct lge_softc	*sc;
768c678bc4fSBill Paul 	struct lge_rx_desc	*c;
769c678bc4fSBill Paul 	struct mbuf		*m;
770c678bc4fSBill Paul {
771c678bc4fSBill Paul 	struct mbuf		*m_new = NULL;
772c678bc4fSBill Paul 	caddr_t			*buf = NULL;
773c678bc4fSBill Paul 
774c678bc4fSBill Paul 	if (m == NULL) {
775c678bc4fSBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
776c678bc4fSBill Paul 		if (m_new == NULL) {
777c678bc4fSBill Paul 			printf("lge%d: no memory for rx list "
778c678bc4fSBill Paul 			    "-- packet dropped!\n", sc->lge_unit);
779c678bc4fSBill Paul 			return(ENOBUFS);
780c678bc4fSBill Paul 		}
781c678bc4fSBill Paul 
782c678bc4fSBill Paul 		/* Allocate the jumbo buffer */
783c678bc4fSBill Paul 		buf = lge_jalloc(sc);
784c678bc4fSBill Paul 		if (buf == NULL) {
785c678bc4fSBill Paul #ifdef LGE_VERBOSE
786c678bc4fSBill Paul 			printf("lge%d: jumbo allocation failed "
787c678bc4fSBill Paul 			    "-- packet dropped!\n", sc->lge_unit);
788c678bc4fSBill Paul #endif
789c678bc4fSBill Paul 			m_freem(m_new);
790c678bc4fSBill Paul 			return(ENOBUFS);
791c678bc4fSBill Paul 		}
792c678bc4fSBill Paul 		/* Attach the buffer to the mbuf */
793c678bc4fSBill Paul 		m_new->m_data = (void *)buf;
794c678bc4fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = LGE_MCLBYTES;
795c678bc4fSBill Paul 		MEXTADD(m_new, buf, LGE_MCLBYTES, lge_jfree,
796c678bc4fSBill Paul 		    (struct lge_softc *)sc, 0, EXT_NET_DRV);
797c678bc4fSBill Paul 	} else {
798c678bc4fSBill Paul 		m_new = m;
799c678bc4fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = LGE_MCLBYTES;
800c678bc4fSBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
801c678bc4fSBill Paul 	}
802c678bc4fSBill Paul 
803c678bc4fSBill Paul 	/*
804c678bc4fSBill Paul 	 * Adjust alignment so packet payload begins on a
805c678bc4fSBill Paul 	 * longword boundary. Mandatory for Alpha, useful on
806c678bc4fSBill Paul 	 * x86 too.
807c678bc4fSBill Paul 	*/
808c678bc4fSBill Paul 	m_adj(m_new, ETHER_ALIGN);
809c678bc4fSBill Paul 
810c678bc4fSBill Paul 	c->lge_mbuf = m_new;
811c678bc4fSBill Paul 	c->lge_fragptr_hi = 0;
812c678bc4fSBill Paul 	c->lge_fragptr_lo = vtophys(mtod(m_new, caddr_t));
813c678bc4fSBill Paul 	c->lge_fraglen = m_new->m_len;
814c678bc4fSBill Paul 	c->lge_ctl = m_new->m_len | LGE_RXCTL_WANTINTR | LGE_FRAGCNT(1);
815c678bc4fSBill Paul 	c->lge_sts = 0;
816c678bc4fSBill Paul 
817c678bc4fSBill Paul 	/*
818c678bc4fSBill Paul 	 * Put this buffer in the RX command FIFO. To do this,
819c678bc4fSBill Paul 	 * we just write the physical address of the descriptor
820c678bc4fSBill Paul 	 * into the RX descriptor address registers. Note that
821c678bc4fSBill Paul 	 * there are two registers, one high DWORD and one low
822c678bc4fSBill Paul 	 * DWORD, which lets us specify a 64-bit address if
823c678bc4fSBill Paul 	 * desired. We only use a 32-bit address for now.
824c678bc4fSBill Paul 	 * Writing to the low DWORD register is what actually
825c678bc4fSBill Paul 	 * causes the command to be issued, so we do that
826c678bc4fSBill Paul 	 * last.
827c678bc4fSBill Paul 	 */
828c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_LO, vtophys(c));
829c678bc4fSBill Paul 	LGE_INC(sc->lge_cdata.lge_rx_prod, LGE_RX_LIST_CNT);
830c678bc4fSBill Paul 
831c678bc4fSBill Paul 	return(0);
832c678bc4fSBill Paul }
833c678bc4fSBill Paul 
834c678bc4fSBill Paul static int lge_alloc_jumbo_mem(sc)
835c678bc4fSBill Paul 	struct lge_softc	*sc;
836c678bc4fSBill Paul {
837c678bc4fSBill Paul 	caddr_t			ptr;
838c678bc4fSBill Paul 	register int		i;
839c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
840c678bc4fSBill Paul 
841c678bc4fSBill Paul 	/* Grab a big chunk o' storage. */
842c678bc4fSBill Paul 	sc->lge_cdata.lge_jumbo_buf = contigmalloc(LGE_JMEM, M_DEVBUF,
843c678bc4fSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
844c678bc4fSBill Paul 
845c678bc4fSBill Paul 	if (sc->lge_cdata.lge_jumbo_buf == NULL) {
846c678bc4fSBill Paul 		printf("lge%d: no memory for jumbo buffers!\n", sc->lge_unit);
847c678bc4fSBill Paul 		return(ENOBUFS);
848c678bc4fSBill Paul 	}
849c678bc4fSBill Paul 
850c678bc4fSBill Paul 	SLIST_INIT(&sc->lge_jfree_listhead);
851c678bc4fSBill Paul 	SLIST_INIT(&sc->lge_jinuse_listhead);
852c678bc4fSBill Paul 
853c678bc4fSBill Paul 	/*
854c678bc4fSBill Paul 	 * Now divide it up into 9K pieces and save the addresses
855c678bc4fSBill Paul 	 * in an array.
856c678bc4fSBill Paul 	 */
857c678bc4fSBill Paul 	ptr = sc->lge_cdata.lge_jumbo_buf;
858c678bc4fSBill Paul 	for (i = 0; i < LGE_JSLOTS; i++) {
859c678bc4fSBill Paul 		sc->lge_cdata.lge_jslots[i] = ptr;
860c678bc4fSBill Paul 		ptr += LGE_MCLBYTES;
861c678bc4fSBill Paul 		entry = malloc(sizeof(struct lge_jpool_entry),
862c678bc4fSBill Paul 		    M_DEVBUF, M_NOWAIT);
863c678bc4fSBill Paul 		if (entry == NULL) {
864c678bc4fSBill Paul 			free(sc->lge_cdata.lge_jumbo_buf, M_DEVBUF);
865c678bc4fSBill Paul 			sc->lge_cdata.lge_jumbo_buf = NULL;
866c678bc4fSBill Paul 			printf("lge%d: no memory for jumbo "
867c678bc4fSBill Paul 			    "buffer queue!\n", sc->lge_unit);
868c678bc4fSBill Paul 			return(ENOBUFS);
869c678bc4fSBill Paul 		}
870c678bc4fSBill Paul 		entry->slot = i;
871c678bc4fSBill Paul 		SLIST_INSERT_HEAD(&sc->lge_jfree_listhead,
872c678bc4fSBill Paul 		    entry, jpool_entries);
873c678bc4fSBill Paul 	}
874c678bc4fSBill Paul 
875c678bc4fSBill Paul 	return(0);
876c678bc4fSBill Paul }
877c678bc4fSBill Paul 
878c678bc4fSBill Paul static void lge_free_jumbo_mem(sc)
879c678bc4fSBill Paul 	struct lge_softc	*sc;
880c678bc4fSBill Paul {
881c678bc4fSBill Paul 	int			i;
882c678bc4fSBill Paul 	struct lge_jpool_entry	*entry;
883c678bc4fSBill Paul 
884c678bc4fSBill Paul 	for (i = 0; i < LGE_JSLOTS; i++) {
885c678bc4fSBill Paul 		entry = SLIST_FIRST(&sc->lge_jfree_listhead);
886c678bc4fSBill Paul 		free(entry, M_DEVBUF);
887c678bc4fSBill Paul 		SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
888c678bc4fSBill Paul 	}
889c678bc4fSBill Paul 
890c678bc4fSBill Paul 	contigfree(sc->lge_cdata.lge_jumbo_buf, LGE_JMEM, M_DEVBUF);
891c678bc4fSBill Paul 
892c678bc4fSBill Paul 	return;
893c678bc4fSBill Paul }
894c678bc4fSBill Paul 
895c678bc4fSBill Paul /*
896c678bc4fSBill Paul  * Allocate a jumbo buffer.
897c678bc4fSBill Paul  */
898c678bc4fSBill Paul static void *lge_jalloc(sc)
899c678bc4fSBill Paul 	struct lge_softc	*sc;
900c678bc4fSBill Paul {
901c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
902c678bc4fSBill Paul 
903c678bc4fSBill Paul 	entry = SLIST_FIRST(&sc->lge_jfree_listhead);
904c678bc4fSBill Paul 
905c678bc4fSBill Paul 	if (entry == NULL) {
906c678bc4fSBill Paul #ifdef LGE_VERBOSE
907c678bc4fSBill Paul 		printf("lge%d: no free jumbo buffers\n", sc->lge_unit);
908c678bc4fSBill Paul #endif
909c678bc4fSBill Paul 		return(NULL);
910c678bc4fSBill Paul 	}
911c678bc4fSBill Paul 
912c678bc4fSBill Paul 	SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
913c678bc4fSBill Paul 	SLIST_INSERT_HEAD(&sc->lge_jinuse_listhead, entry, jpool_entries);
914c678bc4fSBill Paul 	return(sc->lge_cdata.lge_jslots[entry->slot]);
915c678bc4fSBill Paul }
916c678bc4fSBill Paul 
917c678bc4fSBill Paul /*
918c678bc4fSBill Paul  * Release a jumbo buffer.
919c678bc4fSBill Paul  */
920c678bc4fSBill Paul static void lge_jfree(buf, args)
921c678bc4fSBill Paul 	caddr_t			buf;
922c678bc4fSBill Paul 	void			*args;
923c678bc4fSBill Paul {
924c678bc4fSBill Paul 	struct lge_softc	*sc;
925c678bc4fSBill Paul 	int		        i;
926c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
927c678bc4fSBill Paul 
928c678bc4fSBill Paul 	/* Extract the softc struct pointer. */
929c678bc4fSBill Paul 	sc = args;
930c678bc4fSBill Paul 
931c678bc4fSBill Paul 	if (sc == NULL)
932c678bc4fSBill Paul 		panic("lge_jfree: can't find softc pointer!");
933c678bc4fSBill Paul 
934c678bc4fSBill Paul 	/* calculate the slot this buffer belongs to */
935c678bc4fSBill Paul 	i = ((vm_offset_t)buf
936c678bc4fSBill Paul 	     - (vm_offset_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN;
937c678bc4fSBill Paul 
938c678bc4fSBill Paul 	if ((i < 0) || (i >= LGE_JSLOTS))
939c678bc4fSBill Paul 		panic("lge_jfree: asked to free buffer that we don't manage!");
940c678bc4fSBill Paul 
941c678bc4fSBill Paul 	entry = SLIST_FIRST(&sc->lge_jinuse_listhead);
942c678bc4fSBill Paul 	if (entry == NULL)
943c678bc4fSBill Paul 		panic("lge_jfree: buffer not in use!");
944c678bc4fSBill Paul 	entry->slot = i;
945c678bc4fSBill Paul 	SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead, jpool_entries);
946c678bc4fSBill Paul 	SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry, jpool_entries);
947c678bc4fSBill Paul 
948c678bc4fSBill Paul 	return;
949c678bc4fSBill Paul }
950c678bc4fSBill Paul 
951c678bc4fSBill Paul /*
952c678bc4fSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
953c678bc4fSBill Paul  * the higher level protocols.
954c678bc4fSBill Paul  */
955c678bc4fSBill Paul static void lge_rxeof(sc, cnt)
956c678bc4fSBill Paul 	struct lge_softc	*sc;
957c678bc4fSBill Paul 	int			cnt;
958c678bc4fSBill Paul {
959c678bc4fSBill Paul         struct ether_header	*eh;
960c678bc4fSBill Paul         struct mbuf		*m;
961c678bc4fSBill Paul         struct ifnet		*ifp;
962c678bc4fSBill Paul 	struct lge_rx_desc	*cur_rx;
963c678bc4fSBill Paul 	int			c, i, total_len = 0;
964c678bc4fSBill Paul 	u_int32_t		rxsts, rxctl;
965c678bc4fSBill Paul 
966c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
967c678bc4fSBill Paul 
968c678bc4fSBill Paul 	/* Find out how many frames were processed. */
969c678bc4fSBill Paul 	c = cnt;
970c678bc4fSBill Paul 	i = sc->lge_cdata.lge_rx_cons;
971c678bc4fSBill Paul 
972c678bc4fSBill Paul 	/* Suck them in. */
973c678bc4fSBill Paul 	while(c) {
974c678bc4fSBill Paul 		struct mbuf		*m0 = NULL;
975c678bc4fSBill Paul 
976c678bc4fSBill Paul 		cur_rx = &sc->lge_ldata->lge_rx_list[i];
977c678bc4fSBill Paul 		rxctl = cur_rx->lge_ctl;
978c678bc4fSBill Paul 		rxsts = cur_rx->lge_sts;
979c678bc4fSBill Paul 		m = cur_rx->lge_mbuf;
980c678bc4fSBill Paul 		cur_rx->lge_mbuf = NULL;
981c678bc4fSBill Paul 		total_len = LGE_RXBYTES(cur_rx);
982c678bc4fSBill Paul 		LGE_INC(i, LGE_RX_LIST_CNT);
983c678bc4fSBill Paul 		c--;
984c678bc4fSBill Paul 
985c678bc4fSBill Paul 		/*
986c678bc4fSBill Paul 		 * If an error occurs, update stats, clear the
987c678bc4fSBill Paul 		 * status word and leave the mbuf cluster in place:
988c678bc4fSBill Paul 		 * it should simply get re-used next time this descriptor
989c678bc4fSBill Paul 	 	 * comes up in the ring.
990c678bc4fSBill Paul 		 */
991c678bc4fSBill Paul 		if (rxctl & LGE_RXCTL_ERRMASK) {
992c678bc4fSBill Paul 			ifp->if_ierrors++;
993c678bc4fSBill Paul 			lge_newbuf(sc, &LGE_RXTAIL(sc), m);
994c678bc4fSBill Paul 			continue;
995c678bc4fSBill Paul 		}
996c678bc4fSBill Paul 
997c678bc4fSBill Paul 		if (lge_newbuf(sc, &LGE_RXTAIL(sc), NULL) == ENOBUFS) {
998c678bc4fSBill Paul 			m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
999c678bc4fSBill Paul 			    total_len + ETHER_ALIGN, 0, ifp, NULL);
1000c678bc4fSBill Paul 			lge_newbuf(sc, &LGE_RXTAIL(sc), m);
1001c678bc4fSBill Paul 			if (m0 == NULL) {
1002c678bc4fSBill Paul 				printf("lge%d: no receive buffers "
1003c678bc4fSBill Paul 				    "available -- packet dropped!\n",
1004c678bc4fSBill Paul 				    sc->lge_unit);
1005c678bc4fSBill Paul 				ifp->if_ierrors++;
1006c678bc4fSBill Paul 				continue;
1007c678bc4fSBill Paul 			}
1008c678bc4fSBill Paul 			m_adj(m0, ETHER_ALIGN);
1009c678bc4fSBill Paul 			m = m0;
1010c678bc4fSBill Paul 		} else {
1011c678bc4fSBill Paul 			m->m_pkthdr.rcvif = ifp;
1012c678bc4fSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1013c678bc4fSBill Paul 		}
1014c678bc4fSBill Paul 
1015c678bc4fSBill Paul 		ifp->if_ipackets++;
1016c678bc4fSBill Paul 		eh = mtod(m, struct ether_header *);
1017c678bc4fSBill Paul 
1018c678bc4fSBill Paul 		/* Remove header from mbuf and pass it on. */
1019c678bc4fSBill Paul 		m_adj(m, sizeof(struct ether_header));
1020c678bc4fSBill Paul 
1021c678bc4fSBill Paul 		/* Do IP checksum checking. */
1022c678bc4fSBill Paul 		if (rxsts & LGE_RXSTS_ISIP)
1023c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1024c678bc4fSBill Paul 		if (!(rxsts & LGE_RXSTS_IPCSUMERR))
1025c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
10261c352ef7SBill Paul #ifdef notyet
1027c678bc4fSBill Paul 		if ((rxsts & LGE_RXSTS_ISTCP &&
1028c678bc4fSBill Paul 		    !(rxsts & LGE_RXSTS_TCPCSUMERR)) ||
1029c678bc4fSBill Paul 		    (rxsts & LGE_RXSTS_ISUDP &&
1030c678bc4fSBill Paul 		    !(rxsts & LGE_RXSTS_UDPCSUMERR))) {
1031c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |=
1032c678bc4fSBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1033c678bc4fSBill Paul 			m->m_pkthdr.csum_data = 0;
1034c678bc4fSBill Paul 		}
10351c352ef7SBill Paul #endif
1036c678bc4fSBill Paul 		ether_input(ifp, eh, m);
1037c678bc4fSBill Paul 	}
1038c678bc4fSBill Paul 
1039c678bc4fSBill Paul 	sc->lge_cdata.lge_rx_cons = i;
1040c678bc4fSBill Paul 
1041c678bc4fSBill Paul 	return;
1042c678bc4fSBill Paul }
1043c678bc4fSBill Paul 
1044c678bc4fSBill Paul void lge_rxeoc(sc)
1045c678bc4fSBill Paul 	struct lge_softc	*sc;
1046c678bc4fSBill Paul {
1047c678bc4fSBill Paul 	struct ifnet		*ifp;
1048c678bc4fSBill Paul 
1049c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
1050c678bc4fSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
1051c678bc4fSBill Paul 	lge_init(sc);
1052c678bc4fSBill Paul 	return;
1053c678bc4fSBill Paul }
1054c678bc4fSBill Paul 
1055c678bc4fSBill Paul /*
1056c678bc4fSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1057c678bc4fSBill Paul  * the list buffers.
1058c678bc4fSBill Paul  */
1059c678bc4fSBill Paul 
1060c678bc4fSBill Paul static void lge_txeof(sc)
1061c678bc4fSBill Paul 	struct lge_softc	*sc;
1062c678bc4fSBill Paul {
1063c678bc4fSBill Paul 	struct lge_tx_desc	*cur_tx = NULL;
1064c678bc4fSBill Paul 	struct ifnet		*ifp;
1065c678bc4fSBill Paul 	u_int32_t		idx, txdone;
1066c678bc4fSBill Paul 
1067c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
1068c678bc4fSBill Paul 
1069c678bc4fSBill Paul 	/* Clear the timeout timer. */
1070c678bc4fSBill Paul 	ifp->if_timer = 0;
1071c678bc4fSBill Paul 
1072c678bc4fSBill Paul 	/*
1073c678bc4fSBill Paul 	 * Go through our tx list and free mbufs for those
1074c678bc4fSBill Paul 	 * frames that have been transmitted.
1075c678bc4fSBill Paul 	 */
1076c678bc4fSBill Paul 	idx = sc->lge_cdata.lge_tx_cons;
1077c678bc4fSBill Paul 	txdone = CSR_READ_1(sc, LGE_TXDMADONE_8BIT);
1078c678bc4fSBill Paul 
1079c678bc4fSBill Paul 	while (idx != sc->lge_cdata.lge_tx_prod && txdone) {
1080c678bc4fSBill Paul 		cur_tx = &sc->lge_ldata->lge_tx_list[idx];
1081c678bc4fSBill Paul 
1082c678bc4fSBill Paul 		ifp->if_opackets++;
1083c678bc4fSBill Paul 		if (cur_tx->lge_mbuf != NULL) {
1084c678bc4fSBill Paul 			m_freem(cur_tx->lge_mbuf);
1085c678bc4fSBill Paul 			cur_tx->lge_mbuf = NULL;
1086c678bc4fSBill Paul 		}
1087c678bc4fSBill Paul 		cur_tx->lge_ctl = 0;
1088c678bc4fSBill Paul 
1089c678bc4fSBill Paul 		txdone--;
1090c678bc4fSBill Paul 		LGE_INC(idx, LGE_TX_LIST_CNT);
1091c678bc4fSBill Paul 		ifp->if_timer = 0;
1092c678bc4fSBill Paul 	}
1093c678bc4fSBill Paul 
1094c678bc4fSBill Paul 	sc->lge_cdata.lge_tx_cons = idx;
1095c678bc4fSBill Paul 
1096c678bc4fSBill Paul 	if (cur_tx != NULL)
1097c678bc4fSBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1098c678bc4fSBill Paul 
1099c678bc4fSBill Paul 	return;
1100c678bc4fSBill Paul }
1101c678bc4fSBill Paul 
1102c678bc4fSBill Paul static void lge_tick(xsc)
1103c678bc4fSBill Paul 	void			*xsc;
1104c678bc4fSBill Paul {
1105c678bc4fSBill Paul 	struct lge_softc	*sc;
1106c678bc4fSBill Paul 	struct mii_data		*mii;
1107c678bc4fSBill Paul 	struct ifnet		*ifp;
1108c678bc4fSBill Paul 	int			s;
1109c678bc4fSBill Paul 
1110c678bc4fSBill Paul 	s = splimp();
1111c678bc4fSBill Paul 
1112c678bc4fSBill Paul 	sc = xsc;
1113c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
1114c678bc4fSBill Paul 
1115c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_SINGLE_COLL_PKTS);
1116c678bc4fSBill Paul 	ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1117c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_MULTI_COLL_PKTS);
1118c678bc4fSBill Paul 	ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1119c678bc4fSBill Paul 
1120c678bc4fSBill Paul 	if (!sc->lge_link) {
1121c678bc4fSBill Paul 		mii = device_get_softc(sc->lge_miibus);
1122c678bc4fSBill Paul 		mii_tick(mii);
1123c678bc4fSBill Paul 		mii_pollstat(mii);
1124c678bc4fSBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1125c678bc4fSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1126c678bc4fSBill Paul 			sc->lge_link++;
1127c678bc4fSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX||
1128c678bc4fSBill Paul 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX)
1129c678bc4fSBill Paul 				printf("lge%d: gigabit link up\n",
1130c678bc4fSBill Paul 				    sc->lge_unit);
1131c678bc4fSBill Paul 			if (ifp->if_snd.ifq_head != NULL)
1132c678bc4fSBill Paul 				lge_start(ifp);
1133c678bc4fSBill Paul 		}
1134c678bc4fSBill Paul 	}
1135c678bc4fSBill Paul 
1136c678bc4fSBill Paul 	sc->lge_stat_ch = timeout(lge_tick, sc, hz);
1137c678bc4fSBill Paul 
1138c678bc4fSBill Paul 	splx(s);
1139c678bc4fSBill Paul 
1140c678bc4fSBill Paul 	return;
1141c678bc4fSBill Paul }
1142c678bc4fSBill Paul 
1143c678bc4fSBill Paul static void lge_intr(arg)
1144c678bc4fSBill Paul 	void			*arg;
1145c678bc4fSBill Paul {
1146c678bc4fSBill Paul 	struct lge_softc	*sc;
1147c678bc4fSBill Paul 	struct ifnet		*ifp;
1148c678bc4fSBill Paul 	u_int32_t		status;
1149c678bc4fSBill Paul 
1150c678bc4fSBill Paul 	sc = arg;
1151c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
1152c678bc4fSBill Paul 
1153c678bc4fSBill Paul 	/* Supress unwanted interrupts */
1154c678bc4fSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1155c678bc4fSBill Paul 		lge_stop(sc);
1156c678bc4fSBill Paul 		return;
1157c678bc4fSBill Paul 	}
1158c678bc4fSBill Paul 
1159c678bc4fSBill Paul 	for (;;) {
1160c678bc4fSBill Paul 		/*
1161c678bc4fSBill Paul 		 * Reading the ISR register clears all interrupts, and
1162c678bc4fSBill Paul 		 * clears the 'interrupts enabled' bit in the IMR
1163c678bc4fSBill Paul 		 * register.
1164c678bc4fSBill Paul 		 */
1165c678bc4fSBill Paul 		status = CSR_READ_4(sc, LGE_ISR);
1166c678bc4fSBill Paul 
1167c678bc4fSBill Paul 		if ((status & LGE_INTRS) == 0)
1168c678bc4fSBill Paul 			break;
1169c678bc4fSBill Paul 
1170c678bc4fSBill Paul 		if ((status & (LGE_ISR_TXCMDFIFO_EMPTY|LGE_ISR_TXDMA_DONE)))
1171c678bc4fSBill Paul 			lge_txeof(sc);
1172c678bc4fSBill Paul 
1173c678bc4fSBill Paul 		if (status & LGE_ISR_RXDMA_DONE)
1174c678bc4fSBill Paul 			lge_rxeof(sc, LGE_RX_DMACNT(status));
1175c678bc4fSBill Paul 
1176c678bc4fSBill Paul 		if (status & LGE_ISR_RXCMDFIFO_EMPTY)
1177c678bc4fSBill Paul 			lge_rxeoc(sc);
1178c678bc4fSBill Paul 
1179c678bc4fSBill Paul 		if (status & LGE_ISR_PHY_INTR) {
1180c678bc4fSBill Paul 			sc->lge_link = 0;
1181c678bc4fSBill Paul 			untimeout(lge_tick, sc, sc->lge_stat_ch);
1182c678bc4fSBill Paul 			lge_tick(sc);
1183c678bc4fSBill Paul 		}
1184c678bc4fSBill Paul 	}
1185c678bc4fSBill Paul 
1186c678bc4fSBill Paul 	/* Re-enable interrupts. */
1187c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|LGE_IMR_INTR_ENB);
1188c678bc4fSBill Paul 
1189c678bc4fSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1190c678bc4fSBill Paul 		lge_start(ifp);
1191c678bc4fSBill Paul 
1192c678bc4fSBill Paul 	return;
1193c678bc4fSBill Paul }
1194c678bc4fSBill Paul 
1195c678bc4fSBill Paul /*
1196c678bc4fSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1197c678bc4fSBill Paul  * pointers to the fragment pointers.
1198c678bc4fSBill Paul  */
1199c678bc4fSBill Paul static int lge_encap(sc, m_head, txidx)
1200c678bc4fSBill Paul 	struct lge_softc	*sc;
1201c678bc4fSBill Paul 	struct mbuf		*m_head;
1202c678bc4fSBill Paul 	u_int32_t		*txidx;
1203c678bc4fSBill Paul {
1204c678bc4fSBill Paul 	struct lge_frag		*f = NULL;
1205c678bc4fSBill Paul 	struct lge_tx_desc	*cur_tx;
1206c678bc4fSBill Paul 	struct mbuf		*m;
1207c678bc4fSBill Paul 	int			frag = 0, tot_len = 0;
1208c678bc4fSBill Paul 
1209c678bc4fSBill Paul 	/*
1210c678bc4fSBill Paul  	 * Start packing the mbufs in this chain into
1211c678bc4fSBill Paul 	 * the fragment pointers. Stop when we run out
1212c678bc4fSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1213c678bc4fSBill Paul 	 */
1214c678bc4fSBill Paul 	m = m_head;
1215c678bc4fSBill Paul 	cur_tx = &sc->lge_ldata->lge_tx_list[*txidx];
1216c678bc4fSBill Paul 	frag = 0;
1217c678bc4fSBill Paul 
1218c678bc4fSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1219c678bc4fSBill Paul 		if (m->m_len != 0) {
1220c678bc4fSBill Paul 			tot_len += m->m_len;
1221c678bc4fSBill Paul 			f = &cur_tx->lge_frags[frag];
1222c678bc4fSBill Paul 			f->lge_fraglen = m->m_len;
1223c678bc4fSBill Paul 			f->lge_fragptr_lo = vtophys(mtod(m, vm_offset_t));
1224c678bc4fSBill Paul 			f->lge_fragptr_hi = 0;
1225c678bc4fSBill Paul 			frag++;
1226c678bc4fSBill Paul 		}
1227c678bc4fSBill Paul 	}
1228c678bc4fSBill Paul 
1229c678bc4fSBill Paul 	if (m != NULL)
1230c678bc4fSBill Paul 		return(ENOBUFS);
1231c678bc4fSBill Paul 
1232c678bc4fSBill Paul 	cur_tx->lge_mbuf = m_head;
1233c678bc4fSBill Paul 	cur_tx->lge_ctl = LGE_TXCTL_WANTINTR|LGE_FRAGCNT(frag)|tot_len;
12341c352ef7SBill Paul 	LGE_INC((*txidx), LGE_TX_LIST_CNT);
1235c678bc4fSBill Paul 
1236c678bc4fSBill Paul 	/* Queue for transmit */
1237c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_TXDESC_ADDR_LO, vtophys(cur_tx));
1238c678bc4fSBill Paul 
1239c678bc4fSBill Paul 	return(0);
1240c678bc4fSBill Paul }
1241c678bc4fSBill Paul 
1242c678bc4fSBill Paul /*
1243c678bc4fSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1244c678bc4fSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1245c678bc4fSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1246c678bc4fSBill Paul  * physical addresses.
1247c678bc4fSBill Paul  */
1248c678bc4fSBill Paul 
1249c678bc4fSBill Paul static void lge_start(ifp)
1250c678bc4fSBill Paul 	struct ifnet		*ifp;
1251c678bc4fSBill Paul {
1252c678bc4fSBill Paul 	struct lge_softc	*sc;
1253c678bc4fSBill Paul 	struct mbuf		*m_head = NULL;
1254c678bc4fSBill Paul 	u_int32_t		idx;
1255c678bc4fSBill Paul 
1256c678bc4fSBill Paul 	sc = ifp->if_softc;
1257c678bc4fSBill Paul 
1258c678bc4fSBill Paul 	if (!sc->lge_link)
1259c678bc4fSBill Paul 		return;
1260c678bc4fSBill Paul 
1261c678bc4fSBill Paul 	idx = sc->lge_cdata.lge_tx_prod;
1262c678bc4fSBill Paul 
1263c678bc4fSBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
1264c678bc4fSBill Paul 		return;
1265c678bc4fSBill Paul 
1266c678bc4fSBill Paul 	while(sc->lge_ldata->lge_tx_list[idx].lge_mbuf == NULL) {
1267c678bc4fSBill Paul 		if (CSR_READ_1(sc, LGE_TXCMDFREE_8BIT) == 0)
1268c678bc4fSBill Paul 			break;
1269c678bc4fSBill Paul 
1270c678bc4fSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1271c678bc4fSBill Paul 		if (m_head == NULL)
1272c678bc4fSBill Paul 			break;
1273c678bc4fSBill Paul 
1274c678bc4fSBill Paul 		if (lge_encap(sc, m_head, &idx)) {
1275c678bc4fSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1276c678bc4fSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1277c678bc4fSBill Paul 			break;
1278c678bc4fSBill Paul 		}
1279c678bc4fSBill Paul 
1280c678bc4fSBill Paul 		/*
1281c678bc4fSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1282c678bc4fSBill Paul 		 * to him.
1283c678bc4fSBill Paul 		 */
1284c678bc4fSBill Paul 		if (ifp->if_bpf)
1285c678bc4fSBill Paul 			bpf_mtap(ifp, m_head);
1286c678bc4fSBill Paul 	}
1287c678bc4fSBill Paul 
1288c678bc4fSBill Paul 	sc->lge_cdata.lge_tx_prod = idx;
1289c678bc4fSBill Paul 
1290c678bc4fSBill Paul 	/*
1291c678bc4fSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1292c678bc4fSBill Paul 	 */
1293c678bc4fSBill Paul 	ifp->if_timer = 5;
1294c678bc4fSBill Paul 
1295c678bc4fSBill Paul 	return;
1296c678bc4fSBill Paul }
1297c678bc4fSBill Paul 
1298c678bc4fSBill Paul static void lge_init(xsc)
1299c678bc4fSBill Paul 	void			*xsc;
1300c678bc4fSBill Paul {
1301c678bc4fSBill Paul 	struct lge_softc	*sc = xsc;
1302c678bc4fSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1303c678bc4fSBill Paul 	struct mii_data		*mii;
1304c678bc4fSBill Paul 	int			s;
1305c678bc4fSBill Paul 
1306c678bc4fSBill Paul 	if (ifp->if_flags & IFF_RUNNING)
1307c678bc4fSBill Paul 		return;
1308c678bc4fSBill Paul 
1309c678bc4fSBill Paul 	s = splimp();
1310c678bc4fSBill Paul 
1311c678bc4fSBill Paul 	/*
1312c678bc4fSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1313c678bc4fSBill Paul 	 */
1314c678bc4fSBill Paul 	lge_stop(sc);
1315c678bc4fSBill Paul 	lge_reset(sc);
1316c678bc4fSBill Paul 
1317c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
1318c678bc4fSBill Paul 
1319c678bc4fSBill Paul 	/* Set MAC address */
1320c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
1321c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
1322c678bc4fSBill Paul 
1323c678bc4fSBill Paul 	/* Init circular RX list. */
1324c678bc4fSBill Paul 	if (lge_list_rx_init(sc) == ENOBUFS) {
1325c678bc4fSBill Paul 		printf("lge%d: initialization failed: no "
1326c678bc4fSBill Paul 		    "memory for rx buffers\n", sc->lge_unit);
1327c678bc4fSBill Paul 		lge_stop(sc);
1328c678bc4fSBill Paul 		(void)splx(s);
1329c678bc4fSBill Paul 		return;
1330c678bc4fSBill Paul 	}
1331c678bc4fSBill Paul 
1332c678bc4fSBill Paul 	/*
1333c678bc4fSBill Paul 	 * Init tx descriptors.
1334c678bc4fSBill Paul 	 */
1335c678bc4fSBill Paul 	lge_list_tx_init(sc);
1336c678bc4fSBill Paul 
1337c678bc4fSBill Paul 	/* Set initial value for MODE1 register. */
1338c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_UCAST|
1339c678bc4fSBill Paul 	    LGE_MODE1_TX_CRC|LGE_MODE1_TXPAD|
1340c678bc4fSBill Paul 	    LGE_MODE1_RX_FLOWCTL|LGE_MODE1_SETRST_CTL0|
1341c678bc4fSBill Paul 	    LGE_MODE1_SETRST_CTL1|LGE_MODE1_SETRST_CTL2);
1342c678bc4fSBill Paul 
1343c678bc4fSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1344c678bc4fSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1345c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1,
1346c678bc4fSBill Paul 		    LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_PROMISC);
1347c678bc4fSBill Paul 	} else {
1348c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_PROMISC);
1349c678bc4fSBill Paul 	}
1350c678bc4fSBill Paul 
1351c678bc4fSBill Paul 	/*
1352c678bc4fSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1353c678bc4fSBill Paul 	 */
1354c678bc4fSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1355c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1,
1356c678bc4fSBill Paul 		    LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_BCAST);
1357c678bc4fSBill Paul 	} else {
1358c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_BCAST);
1359c678bc4fSBill Paul 	}
1360c678bc4fSBill Paul 
1361c678bc4fSBill Paul 	/* Packet padding workaround? */
1362c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RMVPAD);
1363c678bc4fSBill Paul 
1364c678bc4fSBill Paul 	/* No error frames */
1365c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ERRPKTS);
1366c678bc4fSBill Paul 
1367c678bc4fSBill Paul 	/* Receive large frames */
1368c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_GIANTS);
1369c678bc4fSBill Paul 
1370c678bc4fSBill Paul 	/* Workaround: disable RX/TX flow control */
1371c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_TX_FLOWCTL);
1372c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_FLOWCTL);
1373c678bc4fSBill Paul 
1374c678bc4fSBill Paul 	/* Make sure to strip CRC from received frames */
1375c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_CRC);
1376c678bc4fSBill Paul 
1377c678bc4fSBill Paul 	/* Turn off magic packet mode */
1378c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_MPACK_ENB);
1379c678bc4fSBill Paul 
1380c678bc4fSBill Paul 	/* Turn off all VLAN stuff */
1381c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_VLAN_RX|LGE_MODE1_VLAN_TX|
1382c678bc4fSBill Paul 	    LGE_MODE1_VLAN_STRIP|LGE_MODE1_VLAN_INSERT);
1383c678bc4fSBill Paul 
1384c678bc4fSBill Paul 	/* Workarond: FIFO overflow */
1385c678bc4fSBill Paul 	CSR_WRITE_2(sc, LGE_RXFIFO_HIWAT, 0x3FFF);
1386c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL1|LGE_IMR_RXFIFO_WAT);
1387c678bc4fSBill Paul 
1388c678bc4fSBill Paul 	/*
1389c678bc4fSBill Paul 	 * Load the multicast filter.
1390c678bc4fSBill Paul 	 */
1391c678bc4fSBill Paul 	lge_setmulti(sc);
1392c678bc4fSBill Paul 
1393c678bc4fSBill Paul 	/*
1394c678bc4fSBill Paul 	 * Enable hardware checksum validation for all received IPv4
1395c678bc4fSBill Paul 	 * packets, do not reject packets with bad checksums.
1396c678bc4fSBill Paul 	 */
1397c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE2, LGE_MODE2_RX_IPCSUM|
1398c678bc4fSBill Paul 	    LGE_MODE2_RX_TCPCSUM|LGE_MODE2_RX_UDPCSUM|
1399c678bc4fSBill Paul 	    LGE_MODE2_RX_ERRCSUM);
1400c678bc4fSBill Paul 
1401c678bc4fSBill Paul 	/*
1402c678bc4fSBill Paul 	 * Enable the delivery of PHY interrupts based on
1403c678bc4fSBill Paul 	 * link/speed/duplex status chalges.
1404c678bc4fSBill Paul 	 */
1405c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_GMIIPOLL);
1406c678bc4fSBill Paul 
1407c678bc4fSBill Paul 	/* Enable receiver and transmitter. */
1408c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
1409c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_ENB);
1410c678bc4fSBill Paul 
1411c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_TXDESC_ADDR_HI, 0);
1412c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_TX_ENB);
1413c678bc4fSBill Paul 
1414c678bc4fSBill Paul 	/*
1415c678bc4fSBill Paul 	 * Enable interrupts.
1416c678bc4fSBill Paul 	 */
1417c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|
1418c678bc4fSBill Paul 	    LGE_IMR_SETRST_CTL1|LGE_IMR_INTR_ENB|LGE_INTRS);
1419c678bc4fSBill Paul 
1420c678bc4fSBill Paul 	lge_ifmedia_upd(ifp);
1421c678bc4fSBill Paul 
1422c678bc4fSBill Paul 	ifp->if_flags |= IFF_RUNNING;
1423c678bc4fSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
1424c678bc4fSBill Paul 
1425c678bc4fSBill Paul 	(void)splx(s);
1426c678bc4fSBill Paul 
1427c678bc4fSBill Paul 	sc->lge_stat_ch = timeout(lge_tick, sc, hz);
1428c678bc4fSBill Paul 
1429c678bc4fSBill Paul 	return;
1430c678bc4fSBill Paul }
1431c678bc4fSBill Paul 
1432c678bc4fSBill Paul /*
1433c678bc4fSBill Paul  * Set media options.
1434c678bc4fSBill Paul  */
1435c678bc4fSBill Paul static int lge_ifmedia_upd(ifp)
1436c678bc4fSBill Paul 	struct ifnet		*ifp;
1437c678bc4fSBill Paul {
1438c678bc4fSBill Paul 	struct lge_softc	*sc;
1439c678bc4fSBill Paul 	struct mii_data		*mii;
1440c678bc4fSBill Paul 
1441c678bc4fSBill Paul 	sc = ifp->if_softc;
1442c678bc4fSBill Paul 
1443c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
1444c678bc4fSBill Paul 	sc->lge_link = 0;
1445c678bc4fSBill Paul 	if (mii->mii_instance) {
1446c678bc4fSBill Paul 		struct mii_softc	*miisc;
1447c678bc4fSBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1448c678bc4fSBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
1449c678bc4fSBill Paul 			mii_phy_reset(miisc);
1450c678bc4fSBill Paul 	}
1451c678bc4fSBill Paul 	mii_mediachg(mii);
1452c678bc4fSBill Paul 
1453c678bc4fSBill Paul 	return(0);
1454c678bc4fSBill Paul }
1455c678bc4fSBill Paul 
1456c678bc4fSBill Paul /*
1457c678bc4fSBill Paul  * Report current media status.
1458c678bc4fSBill Paul  */
1459c678bc4fSBill Paul static void lge_ifmedia_sts(ifp, ifmr)
1460c678bc4fSBill Paul 	struct ifnet		*ifp;
1461c678bc4fSBill Paul 	struct ifmediareq	*ifmr;
1462c678bc4fSBill Paul {
1463c678bc4fSBill Paul 	struct lge_softc	*sc;
1464c678bc4fSBill Paul 	struct mii_data		*mii;
1465c678bc4fSBill Paul 
1466c678bc4fSBill Paul 	sc = ifp->if_softc;
1467c678bc4fSBill Paul 
1468c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
1469c678bc4fSBill Paul 	mii_pollstat(mii);
1470c678bc4fSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
1471c678bc4fSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
1472c678bc4fSBill Paul 
1473c678bc4fSBill Paul 	return;
1474c678bc4fSBill Paul }
1475c678bc4fSBill Paul 
1476c678bc4fSBill Paul static int lge_ioctl(ifp, command, data)
1477c678bc4fSBill Paul 	struct ifnet		*ifp;
1478c678bc4fSBill Paul 	u_long			command;
1479c678bc4fSBill Paul 	caddr_t			data;
1480c678bc4fSBill Paul {
1481c678bc4fSBill Paul 	struct lge_softc	*sc = ifp->if_softc;
1482c678bc4fSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
1483c678bc4fSBill Paul 	struct mii_data		*mii;
1484c678bc4fSBill Paul 	int			s, error = 0;
1485c678bc4fSBill Paul 
1486c678bc4fSBill Paul 	s = splimp();
1487c678bc4fSBill Paul 
1488c678bc4fSBill Paul 	switch(command) {
1489c678bc4fSBill Paul 	case SIOCSIFADDR:
1490c678bc4fSBill Paul 	case SIOCGIFADDR:
1491c678bc4fSBill Paul 		error = ether_ioctl(ifp, command, data);
1492c678bc4fSBill Paul 		break;
1493c678bc4fSBill Paul 	case SIOCSIFMTU:
1494c678bc4fSBill Paul 		if (ifr->ifr_mtu > LGE_JUMBO_MTU)
1495c678bc4fSBill Paul 			error = EINVAL;
1496c678bc4fSBill Paul 		else
1497c678bc4fSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
1498c678bc4fSBill Paul 		break;
1499c678bc4fSBill Paul 	case SIOCSIFFLAGS:
1500c678bc4fSBill Paul 		if (ifp->if_flags & IFF_UP) {
1501c678bc4fSBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
1502c678bc4fSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
1503c678bc4fSBill Paul 			    !(sc->lge_if_flags & IFF_PROMISC)) {
1504c678bc4fSBill Paul 				CSR_WRITE_4(sc, LGE_MODE1,
1505c678bc4fSBill Paul 				    LGE_MODE1_SETRST_CTL1|
1506c678bc4fSBill Paul 				    LGE_MODE1_RX_PROMISC);
1507c678bc4fSBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
1508c678bc4fSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
1509c678bc4fSBill Paul 			    sc->lge_if_flags & IFF_PROMISC) {
1510c678bc4fSBill Paul 				CSR_WRITE_4(sc, LGE_MODE1,
1511c678bc4fSBill Paul 				    LGE_MODE1_RX_PROMISC);
1512c678bc4fSBill Paul 			} else {
1513c678bc4fSBill Paul 				ifp->if_flags &= ~IFF_RUNNING;
1514c678bc4fSBill Paul 				lge_init(sc);
1515c678bc4fSBill Paul 			}
1516c678bc4fSBill Paul 		} else {
1517c678bc4fSBill Paul 			if (ifp->if_flags & IFF_RUNNING)
1518c678bc4fSBill Paul 				lge_stop(sc);
1519c678bc4fSBill Paul 		}
1520c678bc4fSBill Paul 		sc->lge_if_flags = ifp->if_flags;
1521c678bc4fSBill Paul 		error = 0;
1522c678bc4fSBill Paul 		break;
1523c678bc4fSBill Paul 	case SIOCADDMULTI:
1524c678bc4fSBill Paul 	case SIOCDELMULTI:
1525c678bc4fSBill Paul 		lge_setmulti(sc);
1526c678bc4fSBill Paul 		error = 0;
1527c678bc4fSBill Paul 		break;
1528c678bc4fSBill Paul 	case SIOCGIFMEDIA:
1529c678bc4fSBill Paul 	case SIOCSIFMEDIA:
1530c678bc4fSBill Paul 		mii = device_get_softc(sc->lge_miibus);
1531c678bc4fSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1532c678bc4fSBill Paul 		break;
1533c678bc4fSBill Paul 	default:
1534c678bc4fSBill Paul 		error = EINVAL;
1535c678bc4fSBill Paul 		break;
1536c678bc4fSBill Paul 	}
1537c678bc4fSBill Paul 
1538c678bc4fSBill Paul 	(void)splx(s);
1539c678bc4fSBill Paul 
1540c678bc4fSBill Paul 	return(error);
1541c678bc4fSBill Paul }
1542c678bc4fSBill Paul 
1543c678bc4fSBill Paul static void lge_watchdog(ifp)
1544c678bc4fSBill Paul 	struct ifnet		*ifp;
1545c678bc4fSBill Paul {
1546c678bc4fSBill Paul 	struct lge_softc	*sc;
1547c678bc4fSBill Paul 
1548c678bc4fSBill Paul 	sc = ifp->if_softc;
1549c678bc4fSBill Paul 
1550c678bc4fSBill Paul 	ifp->if_oerrors++;
1551c678bc4fSBill Paul 	printf("lge%d: watchdog timeout\n", sc->lge_unit);
1552c678bc4fSBill Paul 
1553c678bc4fSBill Paul 	lge_stop(sc);
1554c678bc4fSBill Paul 	lge_reset(sc);
1555c678bc4fSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
1556c678bc4fSBill Paul 	lge_init(sc);
1557c678bc4fSBill Paul 
1558c678bc4fSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1559c678bc4fSBill Paul 		lge_start(ifp);
1560c678bc4fSBill Paul 
1561c678bc4fSBill Paul 	return;
1562c678bc4fSBill Paul }
1563c678bc4fSBill Paul 
1564c678bc4fSBill Paul /*
1565c678bc4fSBill Paul  * Stop the adapter and free any mbufs allocated to the
1566c678bc4fSBill Paul  * RX and TX lists.
1567c678bc4fSBill Paul  */
1568c678bc4fSBill Paul static void lge_stop(sc)
1569c678bc4fSBill Paul 	struct lge_softc	*sc;
1570c678bc4fSBill Paul {
1571c678bc4fSBill Paul 	register int		i;
1572c678bc4fSBill Paul 	struct ifnet		*ifp;
1573c678bc4fSBill Paul 
1574c678bc4fSBill Paul 	ifp = &sc->arpcom.ac_if;
1575c678bc4fSBill Paul 	ifp->if_timer = 0;
1576c678bc4fSBill Paul 	untimeout(lge_tick, sc, sc->lge_stat_ch);
1577c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_INTR_ENB);
1578c678bc4fSBill Paul 
1579c678bc4fSBill Paul 	/* Disable receiver and transmitter. */
1580c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ENB|LGE_MODE1_TX_ENB);
1581c678bc4fSBill Paul 	sc->lge_link = 0;
1582c678bc4fSBill Paul 
1583c678bc4fSBill Paul 	/*
1584c678bc4fSBill Paul 	 * Free data in the RX lists.
1585c678bc4fSBill Paul 	 */
1586c678bc4fSBill Paul 	for (i = 0; i < LGE_RX_LIST_CNT; i++) {
1587c678bc4fSBill Paul 		if (sc->lge_ldata->lge_rx_list[i].lge_mbuf != NULL) {
1588c678bc4fSBill Paul 			m_freem(sc->lge_ldata->lge_rx_list[i].lge_mbuf);
1589c678bc4fSBill Paul 			sc->lge_ldata->lge_rx_list[i].lge_mbuf = NULL;
1590c678bc4fSBill Paul 		}
1591c678bc4fSBill Paul 	}
1592c678bc4fSBill Paul 	bzero((char *)&sc->lge_ldata->lge_rx_list,
1593c678bc4fSBill Paul 		sizeof(sc->lge_ldata->lge_rx_list));
1594c678bc4fSBill Paul 
1595c678bc4fSBill Paul 	/*
1596c678bc4fSBill Paul 	 * Free the TX list buffers.
1597c678bc4fSBill Paul 	 */
1598c678bc4fSBill Paul 	for (i = 0; i < LGE_TX_LIST_CNT; i++) {
1599c678bc4fSBill Paul 		if (sc->lge_ldata->lge_tx_list[i].lge_mbuf != NULL) {
1600c678bc4fSBill Paul 			m_freem(sc->lge_ldata->lge_tx_list[i].lge_mbuf);
1601c678bc4fSBill Paul 			sc->lge_ldata->lge_tx_list[i].lge_mbuf = NULL;
1602c678bc4fSBill Paul 		}
1603c678bc4fSBill Paul 	}
1604c678bc4fSBill Paul 
1605c678bc4fSBill Paul 	bzero((char *)&sc->lge_ldata->lge_tx_list,
1606c678bc4fSBill Paul 		sizeof(sc->lge_ldata->lge_tx_list));
1607c678bc4fSBill Paul 
1608c678bc4fSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1609c678bc4fSBill Paul 
1610c678bc4fSBill Paul 	return;
1611c678bc4fSBill Paul }
1612c678bc4fSBill Paul 
1613c678bc4fSBill Paul /*
1614c678bc4fSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
1615c678bc4fSBill Paul  * get confused by errant DMAs when rebooting.
1616c678bc4fSBill Paul  */
1617c678bc4fSBill Paul static void lge_shutdown(dev)
1618c678bc4fSBill Paul 	device_t		dev;
1619c678bc4fSBill Paul {
1620c678bc4fSBill Paul 	struct lge_softc	*sc;
1621c678bc4fSBill Paul 
1622c678bc4fSBill Paul 	sc = device_get_softc(dev);
1623c678bc4fSBill Paul 
1624c678bc4fSBill Paul 	lge_reset(sc);
1625c678bc4fSBill Paul 	lge_stop(sc);
1626c678bc4fSBill Paul 
1627c678bc4fSBill Paul 	return;
1628c678bc4fSBill Paul }
1629