xref: /freebsd/sys/dev/lge/if_lge.c (revision 6aa1145c847382c9c1bc54810c52fc6b25c83181)
1098ca2bdSWarner Losh /*-
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 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
37c678bc4fSBill Paul /*
38c678bc4fSBill Paul  * Level 1 LXT1001 gigabit ethernet driver for FreeBSD. Public
39c678bc4fSBill Paul  * documentation not available, but ask me nicely.
40c678bc4fSBill Paul  *
41c678bc4fSBill Paul  * The Level 1 chip is used on some D-Link, SMC and Addtron NICs.
42c678bc4fSBill Paul  * It's a 64-bit PCI part that supports TCP/IP checksum offload,
43c678bc4fSBill Paul  * VLAN tagging/insertion, GMII and TBI (1000baseX) ports. There
44c678bc4fSBill Paul  * are three supported methods for data transfer between host and
45c678bc4fSBill Paul  * NIC: programmed I/O, traditional scatter/gather DMA and Packet
46c678bc4fSBill Paul  * Propulsion Technology (tm) DMA. The latter mechanism is a form
47c678bc4fSBill Paul  * of double buffer DMA where the packet data is copied to a
48c678bc4fSBill Paul  * pre-allocated DMA buffer who's physical address has been loaded
49c678bc4fSBill Paul  * into a table at device initialization time. The rationale is that
50c678bc4fSBill Paul  * the virtual to physical address translation needed for normal
51c678bc4fSBill Paul  * scatter/gather DMA is more expensive than the data copy needed
52c678bc4fSBill Paul  * for double buffering. This may be true in Windows NT and the like,
53c678bc4fSBill Paul  * but it isn't true for us, at least on the x86 arch. This driver
54c678bc4fSBill Paul  * uses the scatter/gather I/O method for both TX and RX.
55c678bc4fSBill Paul  *
56c678bc4fSBill Paul  * The LXT1001 only supports TCP/IP checksum offload on receive.
57c678bc4fSBill Paul  * Also, the VLAN tagging is done using a 16-entry table which allows
58c678bc4fSBill Paul  * the chip to perform hardware filtering based on VLAN tags. Sadly,
59c678bc4fSBill Paul  * our vlan support doesn't currently play well with this kind of
60c678bc4fSBill Paul  * hardware support.
61c678bc4fSBill Paul  *
62c678bc4fSBill Paul  * Special thanks to:
63c678bc4fSBill Paul  * - Jeff James at Intel, for arranging to have the LXT1001 manual
64c678bc4fSBill Paul  *   released (at long last)
65c678bc4fSBill Paul  * - Beny Chen at D-Link, for actually sending it to me
66c678bc4fSBill Paul  * - Brad Short and Keith Alexis at SMC, for sending me sample
67c678bc4fSBill Paul  *   SMC9462SX and SMC9462TX adapters for testing
68c678bc4fSBill Paul  * - Paul Saab at Y!, for not killing me (though it remains to be seen
69c678bc4fSBill Paul  *   if in fact he did me much of a favor)
70c678bc4fSBill Paul  */
71c678bc4fSBill Paul 
72c678bc4fSBill Paul #include <sys/param.h>
73c678bc4fSBill Paul #include <sys/systm.h>
74c678bc4fSBill Paul #include <sys/sockio.h>
75c678bc4fSBill Paul #include <sys/mbuf.h>
76c678bc4fSBill Paul #include <sys/malloc.h>
77c678bc4fSBill Paul #include <sys/kernel.h>
78fe12f24bSPoul-Henning Kamp #include <sys/module.h>
79c678bc4fSBill Paul #include <sys/socket.h>
80c678bc4fSBill Paul 
81c678bc4fSBill Paul #include <net/if.h>
82c678bc4fSBill Paul #include <net/if_arp.h>
83c678bc4fSBill Paul #include <net/ethernet.h>
84c678bc4fSBill Paul #include <net/if_dl.h>
85c678bc4fSBill Paul #include <net/if_media.h>
86fc74a9f9SBrooks Davis #include <net/if_types.h>
87c678bc4fSBill Paul 
88c678bc4fSBill Paul #include <net/bpf.h>
89c678bc4fSBill Paul 
90c678bc4fSBill Paul #include <vm/vm.h>              /* for vtophys */
91c678bc4fSBill Paul #include <vm/pmap.h>            /* for vtophys */
92c678bc4fSBill Paul #include <machine/bus.h>
93c678bc4fSBill Paul #include <machine/resource.h>
94c678bc4fSBill Paul #include <sys/bus.h>
95c678bc4fSBill Paul #include <sys/rman.h>
96c678bc4fSBill Paul 
97c678bc4fSBill Paul #include <dev/mii/mii.h>
98c678bc4fSBill Paul #include <dev/mii/miivar.h>
99c678bc4fSBill Paul 
100d2c5276dSWarner Losh #include <dev/pci/pcireg.h>
101d2c5276dSWarner Losh #include <dev/pci/pcivar.h>
102c678bc4fSBill Paul 
103c678bc4fSBill Paul #define LGE_USEIOSPACE
104c678bc4fSBill Paul 
105c678bc4fSBill Paul #include <dev/lge/if_lgereg.h>
106c678bc4fSBill Paul 
1077b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
108c678bc4fSBill Paul #include "miibus_if.h"
109c678bc4fSBill Paul 
110c678bc4fSBill Paul /*
111c678bc4fSBill Paul  * Various supported device vendors/types and their names.
112c678bc4fSBill Paul  */
113*6aa1145cSMarius Strobl static const struct lge_type const lge_devs[] = {
114c678bc4fSBill Paul 	{ LGE_VENDORID, LGE_DEVICEID, "Level 1 Gigabit Ethernet" },
115c678bc4fSBill Paul 	{ 0, 0, NULL }
116c678bc4fSBill Paul };
117c678bc4fSBill Paul 
118e51a25f8SAlfred Perlstein static int lge_probe(device_t);
119e51a25f8SAlfred Perlstein static int lge_attach(device_t);
120e51a25f8SAlfred Perlstein static int lge_detach(device_t);
121c678bc4fSBill Paul 
122e51a25f8SAlfred Perlstein static int lge_alloc_jumbo_mem(struct lge_softc *);
123e51a25f8SAlfred Perlstein static void lge_free_jumbo_mem(struct lge_softc *);
124e51a25f8SAlfred Perlstein static void *lge_jalloc(struct lge_softc *);
125914596abSAlfred Perlstein static void lge_jfree(void *, void *);
126c678bc4fSBill Paul 
1279bee8811SAlfred Perlstein static int lge_newbuf(struct lge_softc *, struct lge_rx_desc *, struct mbuf *);
1289bee8811SAlfred Perlstein static int lge_encap(struct lge_softc *, struct mbuf *, u_int32_t *);
129e51a25f8SAlfred Perlstein static void lge_rxeof(struct lge_softc *, int);
130e51a25f8SAlfred Perlstein static void lge_rxeoc(struct lge_softc *);
131e51a25f8SAlfred Perlstein static void lge_txeof(struct lge_softc *);
132e51a25f8SAlfred Perlstein static void lge_intr(void *);
133e51a25f8SAlfred Perlstein static void lge_tick(void *);
134e51a25f8SAlfred Perlstein static void lge_start(struct ifnet *);
135b05223a3SJohn Baldwin static void lge_start_locked(struct ifnet *);
136e51a25f8SAlfred Perlstein static int lge_ioctl(struct ifnet *, u_long, caddr_t);
137e51a25f8SAlfred Perlstein static void lge_init(void *);
138b05223a3SJohn Baldwin static void lge_init_locked(struct lge_softc *);
139e51a25f8SAlfred Perlstein static void lge_stop(struct lge_softc *);
140e55bc015SJohn Baldwin static void lge_watchdog(struct lge_softc *);
1416a087a87SPyun YongHyeon static int lge_shutdown(device_t);
142e51a25f8SAlfred Perlstein static int lge_ifmedia_upd(struct ifnet *);
143b05223a3SJohn Baldwin static void lge_ifmedia_upd_locked(struct ifnet *);
144e51a25f8SAlfred Perlstein static void lge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
145c678bc4fSBill Paul 
146e51a25f8SAlfred Perlstein static void lge_eeprom_getword(struct lge_softc *, int, u_int16_t *);
147e51a25f8SAlfred Perlstein static void lge_read_eeprom(struct lge_softc *, caddr_t, int, int, int);
148c678bc4fSBill Paul 
149e51a25f8SAlfred Perlstein static int lge_miibus_readreg(device_t, int, int);
150e51a25f8SAlfred Perlstein static int lge_miibus_writereg(device_t, int, int, int);
151e51a25f8SAlfred Perlstein static void lge_miibus_statchg(device_t);
152c678bc4fSBill Paul 
153e51a25f8SAlfred Perlstein static void lge_setmulti(struct lge_softc *);
154e51a25f8SAlfred Perlstein static void lge_reset(struct lge_softc *);
155e51a25f8SAlfred Perlstein static int lge_list_rx_init(struct lge_softc *);
156e51a25f8SAlfred Perlstein static int lge_list_tx_init(struct lge_softc *);
157c678bc4fSBill Paul 
158c678bc4fSBill Paul #ifdef LGE_USEIOSPACE
159c678bc4fSBill Paul #define LGE_RES			SYS_RES_IOPORT
160c678bc4fSBill Paul #define LGE_RID			LGE_PCI_LOIO
161c678bc4fSBill Paul #else
162c678bc4fSBill Paul #define LGE_RES			SYS_RES_MEMORY
163c678bc4fSBill Paul #define LGE_RID			LGE_PCI_LOMEM
164c678bc4fSBill Paul #endif
165c678bc4fSBill Paul 
166c678bc4fSBill Paul static device_method_t lge_methods[] = {
167c678bc4fSBill Paul 	/* Device interface */
168c678bc4fSBill Paul 	DEVMETHOD(device_probe,		lge_probe),
169c678bc4fSBill Paul 	DEVMETHOD(device_attach,	lge_attach),
170c678bc4fSBill Paul 	DEVMETHOD(device_detach,	lge_detach),
171c678bc4fSBill Paul 	DEVMETHOD(device_shutdown,	lge_shutdown),
172c678bc4fSBill Paul 
173c678bc4fSBill Paul 	/* bus interface */
174c678bc4fSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
175c678bc4fSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
176c678bc4fSBill Paul 
177c678bc4fSBill Paul 	/* MII interface */
178c678bc4fSBill Paul 	DEVMETHOD(miibus_readreg,	lge_miibus_readreg),
179c678bc4fSBill Paul 	DEVMETHOD(miibus_writereg,	lge_miibus_writereg),
180c678bc4fSBill Paul 	DEVMETHOD(miibus_statchg,	lge_miibus_statchg),
181c678bc4fSBill Paul 
182c678bc4fSBill Paul 	{ 0, 0 }
183c678bc4fSBill Paul };
184c678bc4fSBill Paul 
185c678bc4fSBill Paul static driver_t lge_driver = {
186c678bc4fSBill Paul 	"lge",
187c678bc4fSBill Paul 	lge_methods,
188c678bc4fSBill Paul 	sizeof(struct lge_softc)
189c678bc4fSBill Paul };
190c678bc4fSBill Paul 
191c678bc4fSBill Paul static devclass_t lge_devclass;
192c678bc4fSBill Paul 
193f246e4a1SMatthew N. Dodd DRIVER_MODULE(lge, pci, lge_driver, lge_devclass, 0, 0);
194c678bc4fSBill Paul DRIVER_MODULE(miibus, lge, miibus_driver, miibus_devclass, 0, 0);
195f246e4a1SMatthew N. Dodd MODULE_DEPEND(lge, pci, 1, 1, 1);
196f246e4a1SMatthew N. Dodd MODULE_DEPEND(lge, ether, 1, 1, 1);
197f246e4a1SMatthew N. Dodd MODULE_DEPEND(lge, miibus, 1, 1, 1);
198c678bc4fSBill Paul 
199c678bc4fSBill Paul #define LGE_SETBIT(sc, reg, x)				\
200c678bc4fSBill Paul 	CSR_WRITE_4(sc, reg,				\
201c678bc4fSBill Paul 		CSR_READ_4(sc, reg) | (x))
202c678bc4fSBill Paul 
203c678bc4fSBill Paul #define LGE_CLRBIT(sc, reg, x)				\
204c678bc4fSBill Paul 	CSR_WRITE_4(sc, reg,				\
205c678bc4fSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
206c678bc4fSBill Paul 
207c678bc4fSBill Paul #define SIO_SET(x)					\
208c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) | x)
209c678bc4fSBill Paul 
210c678bc4fSBill Paul #define SIO_CLR(x)					\
211c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) & ~x)
212c678bc4fSBill Paul 
213c678bc4fSBill Paul /*
214c678bc4fSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
215c678bc4fSBill Paul  */
2169bee8811SAlfred Perlstein static void
2179bee8811SAlfred Perlstein lge_eeprom_getword(sc, addr, dest)
218c678bc4fSBill Paul 	struct lge_softc	*sc;
219c678bc4fSBill Paul 	int			addr;
220c678bc4fSBill Paul 	u_int16_t		*dest;
221c678bc4fSBill Paul {
222c678bc4fSBill Paul 	register int		i;
223c678bc4fSBill Paul 	u_int32_t		val;
224c678bc4fSBill Paul 
225c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_EECTL, LGE_EECTL_CMD_READ|
226c678bc4fSBill Paul 	    LGE_EECTL_SINGLEACCESS|((addr >> 1) << 8));
227c678bc4fSBill Paul 
228c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
229c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_EECTL) & LGE_EECTL_CMD_READ))
230c678bc4fSBill Paul 			break;
231c678bc4fSBill Paul 
232c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
2336b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "EEPROM read timed out\n");
234c678bc4fSBill Paul 		return;
235c678bc4fSBill Paul 	}
236c678bc4fSBill Paul 
237c678bc4fSBill Paul 	val = CSR_READ_4(sc, LGE_EEDATA);
238c678bc4fSBill Paul 
239c678bc4fSBill Paul 	if (addr & 1)
240c678bc4fSBill Paul 		*dest = (val >> 16) & 0xFFFF;
241c678bc4fSBill Paul 	else
242c678bc4fSBill Paul 		*dest = val & 0xFFFF;
243c678bc4fSBill Paul 
244c678bc4fSBill Paul 	return;
245c678bc4fSBill Paul }
246c678bc4fSBill Paul 
247c678bc4fSBill Paul /*
248c678bc4fSBill Paul  * Read a sequence of words from the EEPROM.
249c678bc4fSBill Paul  */
2509bee8811SAlfred Perlstein static void
2519bee8811SAlfred Perlstein lge_read_eeprom(sc, dest, off, cnt, swap)
252c678bc4fSBill Paul 	struct lge_softc	*sc;
253c678bc4fSBill Paul 	caddr_t			dest;
254c678bc4fSBill Paul 	int			off;
255c678bc4fSBill Paul 	int			cnt;
256c678bc4fSBill Paul 	int			swap;
257c678bc4fSBill Paul {
258c678bc4fSBill Paul 	int			i;
259c678bc4fSBill Paul 	u_int16_t		word = 0, *ptr;
260c678bc4fSBill Paul 
261c678bc4fSBill Paul 	for (i = 0; i < cnt; i++) {
262c678bc4fSBill Paul 		lge_eeprom_getword(sc, off + i, &word);
263c678bc4fSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
264c678bc4fSBill Paul 		if (swap)
265c678bc4fSBill Paul 			*ptr = ntohs(word);
266c678bc4fSBill Paul 		else
267c678bc4fSBill Paul 			*ptr = word;
268c678bc4fSBill Paul 	}
269c678bc4fSBill Paul 
270c678bc4fSBill Paul 	return;
271c678bc4fSBill Paul }
272c678bc4fSBill Paul 
2739bee8811SAlfred Perlstein static int
2749bee8811SAlfred Perlstein lge_miibus_readreg(dev, phy, reg)
275c678bc4fSBill Paul 	device_t		dev;
276c678bc4fSBill Paul 	int			phy, reg;
277c678bc4fSBill Paul {
278c678bc4fSBill Paul 	struct lge_softc	*sc;
279c678bc4fSBill Paul 	int			i;
280c678bc4fSBill Paul 
281c678bc4fSBill Paul 	sc = device_get_softc(dev);
282c678bc4fSBill Paul 
283c678bc4fSBill Paul 	/*
284c678bc4fSBill Paul 	 * If we have a non-PCS PHY, pretend that the internal
285c678bc4fSBill Paul 	 * autoneg stuff at PHY address 0 isn't there so that
286c678bc4fSBill Paul 	 * the miibus code will find only the GMII PHY.
287c678bc4fSBill Paul 	 */
288c678bc4fSBill Paul 	if (sc->lge_pcs == 0 && phy == 0)
289c678bc4fSBill Paul 		return(0);
290c678bc4fSBill Paul 
291c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_GMIICTL, (phy << 8) | reg | LGE_GMIICMD_READ);
292c678bc4fSBill Paul 
293c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
294c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
295c678bc4fSBill Paul 			break;
296c678bc4fSBill Paul 
297c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
2986b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "PHY read timed out\n");
299c678bc4fSBill Paul 		return(0);
300c678bc4fSBill Paul 	}
301c678bc4fSBill Paul 
302c678bc4fSBill Paul 	return(CSR_READ_4(sc, LGE_GMIICTL) >> 16);
303c678bc4fSBill Paul }
304c678bc4fSBill Paul 
3059bee8811SAlfred Perlstein static int
3069bee8811SAlfred Perlstein lge_miibus_writereg(dev, phy, reg, data)
307c678bc4fSBill Paul 	device_t		dev;
308c678bc4fSBill Paul 	int			phy, reg, data;
309c678bc4fSBill Paul {
310c678bc4fSBill Paul 	struct lge_softc	*sc;
311c678bc4fSBill Paul 	int			i;
312c678bc4fSBill Paul 
313c678bc4fSBill Paul 	sc = device_get_softc(dev);
314c678bc4fSBill Paul 
315c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_GMIICTL,
316c678bc4fSBill Paul 	    (data << 16) | (phy << 8) | reg | LGE_GMIICMD_WRITE);
317c678bc4fSBill Paul 
318c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++)
319c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY))
320c678bc4fSBill Paul 			break;
321c678bc4fSBill Paul 
322c678bc4fSBill Paul 	if (i == LGE_TIMEOUT) {
3236b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "PHY write timed out\n");
324c678bc4fSBill Paul 		return(0);
325c678bc4fSBill Paul 	}
326c678bc4fSBill Paul 
327c678bc4fSBill Paul 	return(0);
328c678bc4fSBill Paul }
329c678bc4fSBill Paul 
3309bee8811SAlfred Perlstein static void
3319bee8811SAlfred Perlstein lge_miibus_statchg(dev)
332c678bc4fSBill Paul 	device_t		dev;
333c678bc4fSBill Paul {
334c678bc4fSBill Paul 	struct lge_softc	*sc;
335c678bc4fSBill Paul 	struct mii_data		*mii;
336c678bc4fSBill Paul 
337c678bc4fSBill Paul 	sc = device_get_softc(dev);
338c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
339c678bc4fSBill Paul 
340c678bc4fSBill Paul 	LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_SPEED);
341c678bc4fSBill Paul 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
342b418ad5cSPoul-Henning Kamp 	case IFM_1000_T:
343c678bc4fSBill Paul 	case IFM_1000_SX:
344c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
345c678bc4fSBill Paul 		break;
346c678bc4fSBill Paul 	case IFM_100_TX:
347c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_100);
348c678bc4fSBill Paul 		break;
349c678bc4fSBill Paul 	case IFM_10_T:
350c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_10);
351c678bc4fSBill Paul 		break;
352c678bc4fSBill Paul 	default:
353c678bc4fSBill Paul 		/*
354c678bc4fSBill Paul 		 * Choose something, even if it's wrong. Clearing
355c678bc4fSBill Paul 		 * all the bits will hose autoneg on the internal
356c678bc4fSBill Paul 		 * PHY.
357c678bc4fSBill Paul 		 */
358c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000);
359c678bc4fSBill Paul 		break;
360c678bc4fSBill Paul 	}
361c678bc4fSBill Paul 
362c678bc4fSBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
363c678bc4fSBill Paul 		LGE_SETBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
364c678bc4fSBill Paul 	} else {
365c678bc4fSBill Paul 		LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX);
366c678bc4fSBill Paul 	}
367c678bc4fSBill Paul 
368c678bc4fSBill Paul 	return;
369c678bc4fSBill Paul }
370c678bc4fSBill Paul 
3719bee8811SAlfred Perlstein static void
3729bee8811SAlfred Perlstein lge_setmulti(sc)
373c678bc4fSBill Paul 	struct lge_softc	*sc;
374c678bc4fSBill Paul {
375c678bc4fSBill Paul 	struct ifnet		*ifp;
376c678bc4fSBill Paul 	struct ifmultiaddr	*ifma;
377c678bc4fSBill Paul 	u_int32_t		h = 0, hashes[2] = { 0, 0 };
378c678bc4fSBill Paul 
379fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
380b05223a3SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
381c678bc4fSBill Paul 
382c678bc4fSBill Paul 	/* Make sure multicast hash table is enabled. */
383c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_MCAST);
384c678bc4fSBill Paul 
385c678bc4fSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
386c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MAR0, 0xFFFFFFFF);
387c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MAR1, 0xFFFFFFFF);
388c678bc4fSBill Paul 		return;
389c678bc4fSBill Paul 	}
390c678bc4fSBill Paul 
391c678bc4fSBill Paul 	/* first, zot all the existing hash bits */
392c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR0, 0);
393c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR1, 0);
394c678bc4fSBill Paul 
395c678bc4fSBill Paul 	/* now program new ones */
396eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
397c678bc4fSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
398c678bc4fSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
399c678bc4fSBill Paul 			continue;
4000e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
4010e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
402c678bc4fSBill Paul 		if (h < 32)
403c678bc4fSBill Paul 			hashes[0] |= (1 << h);
404c678bc4fSBill Paul 		else
405c678bc4fSBill Paul 			hashes[1] |= (1 << (h - 32));
406c678bc4fSBill Paul 	}
407eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
408c678bc4fSBill Paul 
409c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR0, hashes[0]);
410c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MAR1, hashes[1]);
411c678bc4fSBill Paul 
412c678bc4fSBill Paul 	return;
413c678bc4fSBill Paul }
414c678bc4fSBill Paul 
4159bee8811SAlfred Perlstein static void
4169bee8811SAlfred Perlstein lge_reset(sc)
417c678bc4fSBill Paul 	struct lge_softc	*sc;
418c678bc4fSBill Paul {
419c678bc4fSBill Paul 	register int		i;
420c678bc4fSBill Paul 
421c678bc4fSBill Paul 	LGE_SETBIT(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_SOFTRST);
422c678bc4fSBill Paul 
423c678bc4fSBill Paul 	for (i = 0; i < LGE_TIMEOUT; i++) {
424c678bc4fSBill Paul 		if (!(CSR_READ_4(sc, LGE_MODE1) & LGE_MODE1_SOFTRST))
425c678bc4fSBill Paul 			break;
426c678bc4fSBill Paul 	}
427c678bc4fSBill Paul 
428c678bc4fSBill Paul 	if (i == LGE_TIMEOUT)
4296b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "reset never completed\n");
430c678bc4fSBill Paul 
431c678bc4fSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
432c678bc4fSBill Paul 	DELAY(1000);
433c678bc4fSBill Paul 
434c678bc4fSBill Paul         return;
435c678bc4fSBill Paul }
436c678bc4fSBill Paul 
437c678bc4fSBill Paul /*
438c678bc4fSBill Paul  * Probe for a Level 1 chip. Check the PCI vendor and device
439c678bc4fSBill Paul  * IDs against our list and return a device name if we find a match.
440c678bc4fSBill Paul  */
4419bee8811SAlfred Perlstein static int
4429bee8811SAlfred Perlstein lge_probe(dev)
443c678bc4fSBill Paul 	device_t		dev;
444c678bc4fSBill Paul {
445*6aa1145cSMarius Strobl 	const struct lge_type	*t;
446c678bc4fSBill Paul 
447c678bc4fSBill Paul 	t = lge_devs;
448c678bc4fSBill Paul 
449c678bc4fSBill Paul 	while(t->lge_name != NULL) {
450c678bc4fSBill Paul 		if ((pci_get_vendor(dev) == t->lge_vid) &&
451c678bc4fSBill Paul 		    (pci_get_device(dev) == t->lge_did)) {
452c678bc4fSBill Paul 			device_set_desc(dev, t->lge_name);
453b77e575eSWarner Losh 			return(BUS_PROBE_DEFAULT);
454c678bc4fSBill Paul 		}
455c678bc4fSBill Paul 		t++;
456c678bc4fSBill Paul 	}
457c678bc4fSBill Paul 
458c678bc4fSBill Paul 	return(ENXIO);
459c678bc4fSBill Paul }
460c678bc4fSBill Paul 
461c678bc4fSBill Paul /*
462c678bc4fSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
463c678bc4fSBill Paul  * setup and ethernet/BPF attach.
464c678bc4fSBill Paul  */
4659bee8811SAlfred Perlstein static int
4669bee8811SAlfred Perlstein lge_attach(dev)
467c678bc4fSBill Paul 	device_t		dev;
468c678bc4fSBill Paul {
469c678bc4fSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
470c678bc4fSBill Paul 	struct lge_softc	*sc;
47178de678eSJohn Baldwin 	struct ifnet		*ifp = NULL;
47278de678eSJohn Baldwin 	int			error = 0, rid;
473c678bc4fSBill Paul 
474c678bc4fSBill Paul 	sc = device_get_softc(dev);
4756b9f5c94SGleb Smirnoff 	sc->lge_dev = dev;
4766b9f5c94SGleb Smirnoff 
477b05223a3SJohn Baldwin 	mtx_init(&sc->lge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
478b05223a3SJohn Baldwin 	    MTX_DEF);
479b05223a3SJohn Baldwin 	callout_init_mtx(&sc->lge_stat_callout, &sc->lge_mtx, 0);
48078de678eSJohn Baldwin 
481c678bc4fSBill Paul 	/*
482c678bc4fSBill Paul 	 * Map control/status registers.
483c678bc4fSBill Paul 	 */
484c678bc4fSBill Paul 	pci_enable_busmaster(dev);
485c678bc4fSBill Paul 
486c678bc4fSBill Paul 	rid = LGE_RID;
4875f96beb9SNate Lawson 	sc->lge_res = bus_alloc_resource_any(dev, LGE_RES, &rid, RF_ACTIVE);
488c678bc4fSBill Paul 
489c678bc4fSBill Paul 	if (sc->lge_res == NULL) {
49078de678eSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
491c678bc4fSBill Paul 		error = ENXIO;
492c678bc4fSBill Paul 		goto fail;
493c678bc4fSBill Paul 	}
494c678bc4fSBill Paul 
495c678bc4fSBill Paul 	sc->lge_btag = rman_get_bustag(sc->lge_res);
496c678bc4fSBill Paul 	sc->lge_bhandle = rman_get_bushandle(sc->lge_res);
497c678bc4fSBill Paul 
498c678bc4fSBill Paul 	/* Allocate interrupt */
499c678bc4fSBill Paul 	rid = 0;
5005f96beb9SNate Lawson 	sc->lge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
501c678bc4fSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
502c678bc4fSBill Paul 
503c678bc4fSBill Paul 	if (sc->lge_irq == NULL) {
50478de678eSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
505c678bc4fSBill Paul 		error = ENXIO;
506c678bc4fSBill Paul 		goto fail;
507c678bc4fSBill Paul 	}
508c678bc4fSBill Paul 
509c678bc4fSBill Paul 	/* Reset the adapter. */
510c678bc4fSBill Paul 	lge_reset(sc);
511c678bc4fSBill Paul 
512c678bc4fSBill Paul 	/*
513c678bc4fSBill Paul 	 * Get station address from the EEPROM.
514c678bc4fSBill Paul 	 */
515c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[0], LGE_EE_NODEADDR_0, 1, 0);
516c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[2], LGE_EE_NODEADDR_1, 1, 0);
517c678bc4fSBill Paul 	lge_read_eeprom(sc, (caddr_t)&eaddr[4], LGE_EE_NODEADDR_2, 1, 0);
518c678bc4fSBill Paul 
519c678bc4fSBill Paul 	sc->lge_ldata = contigmalloc(sizeof(struct lge_list_data), M_DEVBUF,
520b05223a3SJohn Baldwin 	    M_NOWAIT | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
521c678bc4fSBill Paul 
522c678bc4fSBill Paul 	if (sc->lge_ldata == NULL) {
52378de678eSJohn Baldwin 		device_printf(dev, "no memory for list buffers!\n");
524c678bc4fSBill Paul 		error = ENXIO;
525c678bc4fSBill Paul 		goto fail;
526c678bc4fSBill Paul 	}
527c678bc4fSBill Paul 
528c678bc4fSBill Paul 	/* Try to allocate memory for jumbo buffers. */
529c678bc4fSBill Paul 	if (lge_alloc_jumbo_mem(sc)) {
53078de678eSJohn Baldwin 		device_printf(dev, "jumbo buffer allocation failed\n");
531c678bc4fSBill Paul 		error = ENXIO;
532c678bc4fSBill Paul 		goto fail;
533c678bc4fSBill Paul 	}
534c678bc4fSBill Paul 
535fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp = if_alloc(IFT_ETHER);
536fc74a9f9SBrooks Davis 	if (ifp == NULL) {
53778de678eSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
538fc74a9f9SBrooks Davis 		error = ENOSPC;
539fc74a9f9SBrooks Davis 		goto fail;
540fc74a9f9SBrooks Davis 	}
541c678bc4fSBill Paul 	ifp->if_softc = sc;
5429bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
543c678bc4fSBill Paul 	ifp->if_mtu = ETHERMTU;
544b05223a3SJohn Baldwin 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
545c678bc4fSBill Paul 	ifp->if_ioctl = lge_ioctl;
546c678bc4fSBill Paul 	ifp->if_start = lge_start;
547c678bc4fSBill Paul 	ifp->if_init = lge_init;
548c678bc4fSBill Paul 	ifp->if_snd.ifq_maxlen = LGE_TX_LIST_CNT - 1;
549ed63a3c7SJonathan Lemon 	ifp->if_capabilities = IFCAP_RXCSUM;
550ed63a3c7SJonathan Lemon 	ifp->if_capenable = ifp->if_capabilities;
551c678bc4fSBill Paul 
552c678bc4fSBill Paul 	if (CSR_READ_4(sc, LGE_GMIIMODE) & LGE_GMIIMODE_PCSENH)
553c678bc4fSBill Paul 		sc->lge_pcs = 1;
554c678bc4fSBill Paul 	else
555c678bc4fSBill Paul 		sc->lge_pcs = 0;
556c678bc4fSBill Paul 
557c678bc4fSBill Paul 	/*
558c678bc4fSBill Paul 	 * Do MII setup.
559c678bc4fSBill Paul 	 */
560d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->lge_miibus, ifp, lge_ifmedia_upd,
561d6c65d27SMarius Strobl 	    lge_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
562d6c65d27SMarius Strobl 	if (error != 0) {
563d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
564c678bc4fSBill Paul 		goto fail;
565c678bc4fSBill Paul 	}
566c678bc4fSBill Paul 
567c678bc4fSBill Paul 	/*
568c678bc4fSBill Paul 	 * Call MI attach routine.
569c678bc4fSBill Paul 	 */
570673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
571b05223a3SJohn Baldwin 
572b05223a3SJohn Baldwin 	error = bus_setup_intr(dev, sc->lge_irq, INTR_TYPE_NET | INTR_MPSAFE,
573ef544f63SPaolo Pisati 	    NULL, lge_intr, sc, &sc->lge_intrhand);
574b05223a3SJohn Baldwin 
575b05223a3SJohn Baldwin 	if (error) {
576b05223a3SJohn Baldwin 		ether_ifdetach(ifp);
577b05223a3SJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
578b05223a3SJohn Baldwin 		goto fail;
579b05223a3SJohn Baldwin 	}
58078de678eSJohn Baldwin 	return (0);
581c678bc4fSBill Paul 
582c678bc4fSBill Paul fail:
583eeeebe75SPyun YongHyeon 	lge_free_jumbo_mem(sc);
58478de678eSJohn Baldwin 	if (sc->lge_ldata)
58578de678eSJohn Baldwin 		contigfree(sc->lge_ldata,
58678de678eSJohn Baldwin 		    sizeof(struct lge_list_data), M_DEVBUF);
58778de678eSJohn Baldwin 	if (ifp)
58878de678eSJohn Baldwin 		if_free(ifp);
58978de678eSJohn Baldwin 	if (sc->lge_irq)
59078de678eSJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
59178de678eSJohn Baldwin 	if (sc->lge_res)
59278de678eSJohn Baldwin 		bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
593b05223a3SJohn Baldwin 	mtx_destroy(&sc->lge_mtx);
594c678bc4fSBill Paul 	return(error);
595c678bc4fSBill Paul }
596c678bc4fSBill Paul 
5979bee8811SAlfred Perlstein static int
5989bee8811SAlfred Perlstein lge_detach(dev)
599c678bc4fSBill Paul 	device_t		dev;
600c678bc4fSBill Paul {
601c678bc4fSBill Paul 	struct lge_softc	*sc;
602c678bc4fSBill Paul 	struct ifnet		*ifp;
603c678bc4fSBill Paul 
604c678bc4fSBill Paul 	sc = device_get_softc(dev);
605fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
606c678bc4fSBill Paul 
607b05223a3SJohn Baldwin 	LGE_LOCK(sc);
608c678bc4fSBill Paul 	lge_reset(sc);
609c678bc4fSBill Paul 	lge_stop(sc);
610b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
611b05223a3SJohn Baldwin 	callout_drain(&sc->lge_stat_callout);
612673d9191SSam Leffler 	ether_ifdetach(ifp);
613c678bc4fSBill Paul 
614c678bc4fSBill Paul 	bus_generic_detach(dev);
615c678bc4fSBill Paul 	device_delete_child(dev, sc->lge_miibus);
616c678bc4fSBill Paul 
617c678bc4fSBill Paul 	bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand);
618c678bc4fSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq);
619c678bc4fSBill Paul 	bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res);
620c678bc4fSBill Paul 
621c678bc4fSBill Paul 	contigfree(sc->lge_ldata, sizeof(struct lge_list_data), M_DEVBUF);
622ad4f426eSWarner Losh 	if_free(ifp);
623c678bc4fSBill Paul 	lge_free_jumbo_mem(sc);
624b05223a3SJohn Baldwin 	mtx_destroy(&sc->lge_mtx);
625c678bc4fSBill Paul 
626c678bc4fSBill Paul 	return(0);
627c678bc4fSBill Paul }
628c678bc4fSBill Paul 
629c678bc4fSBill Paul /*
630c678bc4fSBill Paul  * Initialize the transmit descriptors.
631c678bc4fSBill Paul  */
6329bee8811SAlfred Perlstein static int
6339bee8811SAlfred Perlstein lge_list_tx_init(sc)
634c678bc4fSBill Paul 	struct lge_softc	*sc;
635c678bc4fSBill Paul {
636c678bc4fSBill Paul 	struct lge_list_data	*ld;
637c678bc4fSBill Paul 	struct lge_ring_data	*cd;
638c678bc4fSBill Paul 	int			i;
639c678bc4fSBill Paul 
640c678bc4fSBill Paul 	cd = &sc->lge_cdata;
641c678bc4fSBill Paul 	ld = sc->lge_ldata;
642c678bc4fSBill Paul 	for (i = 0; i < LGE_TX_LIST_CNT; i++) {
643c678bc4fSBill Paul 		ld->lge_tx_list[i].lge_mbuf = NULL;
644c678bc4fSBill Paul 		ld->lge_tx_list[i].lge_ctl = 0;
645c678bc4fSBill Paul 	}
646c678bc4fSBill Paul 
647c678bc4fSBill Paul 	cd->lge_tx_prod = cd->lge_tx_cons = 0;
648c678bc4fSBill Paul 
649c678bc4fSBill Paul 	return(0);
650c678bc4fSBill Paul }
651c678bc4fSBill Paul 
652c678bc4fSBill Paul 
653c678bc4fSBill Paul /*
654c678bc4fSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
655c678bc4fSBill Paul  * we arralge the descriptors in a closed ring, so that the last descriptor
656c678bc4fSBill Paul  * points back to the first.
657c678bc4fSBill Paul  */
6589bee8811SAlfred Perlstein static int
6599bee8811SAlfred Perlstein lge_list_rx_init(sc)
660c678bc4fSBill Paul 	struct lge_softc	*sc;
661c678bc4fSBill Paul {
662c678bc4fSBill Paul 	struct lge_list_data	*ld;
663c678bc4fSBill Paul 	struct lge_ring_data	*cd;
664c678bc4fSBill Paul 	int			i;
665c678bc4fSBill Paul 
666c678bc4fSBill Paul 	ld = sc->lge_ldata;
667c678bc4fSBill Paul 	cd = &sc->lge_cdata;
668c678bc4fSBill Paul 
669c678bc4fSBill Paul 	cd->lge_rx_prod = cd->lge_rx_cons = 0;
670c678bc4fSBill Paul 
671c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
672c678bc4fSBill Paul 
673c678bc4fSBill Paul 	for (i = 0; i < LGE_RX_LIST_CNT; i++) {
674c678bc4fSBill Paul 		if (CSR_READ_1(sc, LGE_RXCMDFREE_8BIT) == 0)
675c678bc4fSBill Paul 			break;
676c678bc4fSBill Paul 		if (lge_newbuf(sc, &ld->lge_rx_list[i], NULL) == ENOBUFS)
677c678bc4fSBill Paul 			return(ENOBUFS);
678c678bc4fSBill Paul 	}
679c678bc4fSBill Paul 
680c678bc4fSBill Paul 	/* Clear possible 'rx command queue empty' interrupt. */
681c678bc4fSBill Paul 	CSR_READ_4(sc, LGE_ISR);
682c678bc4fSBill Paul 
683c678bc4fSBill Paul 	return(0);
684c678bc4fSBill Paul }
685c678bc4fSBill Paul 
686c678bc4fSBill Paul /*
687c678bc4fSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
688c678bc4fSBill Paul  */
6899bee8811SAlfred Perlstein static int
6909bee8811SAlfred Perlstein lge_newbuf(sc, c, m)
691c678bc4fSBill Paul 	struct lge_softc	*sc;
692c678bc4fSBill Paul 	struct lge_rx_desc	*c;
693c678bc4fSBill Paul 	struct mbuf		*m;
694c678bc4fSBill Paul {
695c678bc4fSBill Paul 	struct mbuf		*m_new = NULL;
696c678bc4fSBill Paul 	caddr_t			*buf = NULL;
697c678bc4fSBill Paul 
698c678bc4fSBill Paul 	if (m == NULL) {
699a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
700c678bc4fSBill Paul 		if (m_new == NULL) {
7016b9f5c94SGleb Smirnoff 			device_printf(sc->lge_dev, "no memory for rx list "
70278de678eSJohn Baldwin 			    "-- packet dropped!\n");
703c678bc4fSBill Paul 			return(ENOBUFS);
704c678bc4fSBill Paul 		}
705c678bc4fSBill Paul 
706c678bc4fSBill Paul 		/* Allocate the jumbo buffer */
707c678bc4fSBill Paul 		buf = lge_jalloc(sc);
708c678bc4fSBill Paul 		if (buf == NULL) {
709c678bc4fSBill Paul #ifdef LGE_VERBOSE
7106b9f5c94SGleb Smirnoff 			device_printf(sc->lge_dev, "jumbo allocation failed "
71178de678eSJohn Baldwin 			    "-- packet dropped!\n");
712c678bc4fSBill Paul #endif
713c678bc4fSBill Paul 			m_freem(m_new);
714c678bc4fSBill Paul 			return(ENOBUFS);
715c678bc4fSBill Paul 		}
716c678bc4fSBill Paul 		/* Attach the buffer to the mbuf */
717c678bc4fSBill Paul 		m_new->m_data = (void *)buf;
7187437599fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = LGE_JUMBO_FRAMELEN;
7197437599fSBill Paul 		MEXTADD(m_new, buf, LGE_JUMBO_FRAMELEN, lge_jfree,
720cf827063SPoul-Henning Kamp 		    buf, (struct lge_softc *)sc, 0, EXT_NET_DRV);
721c678bc4fSBill Paul 	} else {
722c678bc4fSBill Paul 		m_new = m;
7237437599fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = LGE_JUMBO_FRAMELEN;
724c678bc4fSBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
725c678bc4fSBill Paul 	}
726c678bc4fSBill Paul 
727c678bc4fSBill Paul 	/*
728c678bc4fSBill Paul 	 * Adjust alignment so packet payload begins on a
729c678bc4fSBill Paul 	 * longword boundary. Mandatory for Alpha, useful on
730c678bc4fSBill Paul 	 * x86 too.
731c678bc4fSBill Paul 	*/
732c678bc4fSBill Paul 	m_adj(m_new, ETHER_ALIGN);
733c678bc4fSBill Paul 
734c678bc4fSBill Paul 	c->lge_mbuf = m_new;
735c678bc4fSBill Paul 	c->lge_fragptr_hi = 0;
736c678bc4fSBill Paul 	c->lge_fragptr_lo = vtophys(mtod(m_new, caddr_t));
737c678bc4fSBill Paul 	c->lge_fraglen = m_new->m_len;
738c678bc4fSBill Paul 	c->lge_ctl = m_new->m_len | LGE_RXCTL_WANTINTR | LGE_FRAGCNT(1);
739c678bc4fSBill Paul 	c->lge_sts = 0;
740c678bc4fSBill Paul 
741c678bc4fSBill Paul 	/*
742c678bc4fSBill Paul 	 * Put this buffer in the RX command FIFO. To do this,
743c678bc4fSBill Paul 	 * we just write the physical address of the descriptor
744c678bc4fSBill Paul 	 * into the RX descriptor address registers. Note that
745c678bc4fSBill Paul 	 * there are two registers, one high DWORD and one low
746c678bc4fSBill Paul 	 * DWORD, which lets us specify a 64-bit address if
747c678bc4fSBill Paul 	 * desired. We only use a 32-bit address for now.
748c678bc4fSBill Paul 	 * Writing to the low DWORD register is what actually
749c678bc4fSBill Paul 	 * causes the command to be issued, so we do that
750c678bc4fSBill Paul 	 * last.
751c678bc4fSBill Paul 	 */
752c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_LO, vtophys(c));
753c678bc4fSBill Paul 	LGE_INC(sc->lge_cdata.lge_rx_prod, LGE_RX_LIST_CNT);
754c678bc4fSBill Paul 
755c678bc4fSBill Paul 	return(0);
756c678bc4fSBill Paul }
757c678bc4fSBill Paul 
7589bee8811SAlfred Perlstein static int
7599bee8811SAlfred Perlstein lge_alloc_jumbo_mem(sc)
760c678bc4fSBill Paul 	struct lge_softc	*sc;
761c678bc4fSBill Paul {
762c678bc4fSBill Paul 	caddr_t			ptr;
763c678bc4fSBill Paul 	register int		i;
764c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
765c678bc4fSBill Paul 
766c678bc4fSBill Paul 	/* Grab a big chunk o' storage. */
767c678bc4fSBill Paul 	sc->lge_cdata.lge_jumbo_buf = contigmalloc(LGE_JMEM, M_DEVBUF,
768c678bc4fSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
769c678bc4fSBill Paul 
770c678bc4fSBill Paul 	if (sc->lge_cdata.lge_jumbo_buf == NULL) {
7716b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "no memory for jumbo buffers!\n");
772c678bc4fSBill Paul 		return(ENOBUFS);
773c678bc4fSBill Paul 	}
774c678bc4fSBill Paul 
775c678bc4fSBill Paul 	SLIST_INIT(&sc->lge_jfree_listhead);
776c678bc4fSBill Paul 	SLIST_INIT(&sc->lge_jinuse_listhead);
777c678bc4fSBill Paul 
778c678bc4fSBill Paul 	/*
779c678bc4fSBill Paul 	 * Now divide it up into 9K pieces and save the addresses
780c678bc4fSBill Paul 	 * in an array.
781c678bc4fSBill Paul 	 */
782c678bc4fSBill Paul 	ptr = sc->lge_cdata.lge_jumbo_buf;
783c678bc4fSBill Paul 	for (i = 0; i < LGE_JSLOTS; i++) {
784c678bc4fSBill Paul 		sc->lge_cdata.lge_jslots[i] = ptr;
7857437599fSBill Paul 		ptr += LGE_JLEN;
786c678bc4fSBill Paul 		entry = malloc(sizeof(struct lge_jpool_entry),
787c678bc4fSBill Paul 		    M_DEVBUF, M_NOWAIT);
788c678bc4fSBill Paul 		if (entry == NULL) {
7896b9f5c94SGleb Smirnoff 			device_printf(sc->lge_dev, "no memory for jumbo "
79078de678eSJohn Baldwin 			    "buffer queue!\n");
791c678bc4fSBill Paul 			return(ENOBUFS);
792c678bc4fSBill Paul 		}
793c678bc4fSBill Paul 		entry->slot = i;
794c678bc4fSBill Paul 		SLIST_INSERT_HEAD(&sc->lge_jfree_listhead,
795c678bc4fSBill Paul 		    entry, jpool_entries);
796c678bc4fSBill Paul 	}
797c678bc4fSBill Paul 
798c678bc4fSBill Paul 	return(0);
799c678bc4fSBill Paul }
800c678bc4fSBill Paul 
8019bee8811SAlfred Perlstein static void
8029bee8811SAlfred Perlstein lge_free_jumbo_mem(sc)
803c678bc4fSBill Paul 	struct lge_softc	*sc;
804c678bc4fSBill Paul {
805c678bc4fSBill Paul 	struct lge_jpool_entry	*entry;
806c678bc4fSBill Paul 
807eeeebe75SPyun YongHyeon 	if (sc->lge_cdata.lge_jumbo_buf == NULL)
808eeeebe75SPyun YongHyeon 		return;
809eeeebe75SPyun YongHyeon 
810eeeebe75SPyun YongHyeon 	while ((entry = SLIST_FIRST(&sc->lge_jinuse_listhead))) {
811eeeebe75SPyun YongHyeon 		device_printf(sc->lge_dev,
812eeeebe75SPyun YongHyeon 		    "asked to free buffer that is in use!\n");
813eeeebe75SPyun YongHyeon 		SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead, jpool_entries);
814eeeebe75SPyun YongHyeon 		SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry,
815eeeebe75SPyun YongHyeon 		    jpool_entries);
816eeeebe75SPyun YongHyeon 	}
817eeeebe75SPyun YongHyeon 	while (!SLIST_EMPTY(&sc->lge_jfree_listhead)) {
818c678bc4fSBill Paul 		entry = SLIST_FIRST(&sc->lge_jfree_listhead);
819c678bc4fSBill Paul 		SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
8207437599fSBill Paul 		free(entry, M_DEVBUF);
821c678bc4fSBill Paul 	}
822c678bc4fSBill Paul 
823c678bc4fSBill Paul 	contigfree(sc->lge_cdata.lge_jumbo_buf, LGE_JMEM, M_DEVBUF);
824c678bc4fSBill Paul 
825c678bc4fSBill Paul 	return;
826c678bc4fSBill Paul }
827c678bc4fSBill Paul 
828c678bc4fSBill Paul /*
829c678bc4fSBill Paul  * Allocate a jumbo buffer.
830c678bc4fSBill Paul  */
8319bee8811SAlfred Perlstein static void *
8329bee8811SAlfred Perlstein lge_jalloc(sc)
833c678bc4fSBill Paul 	struct lge_softc	*sc;
834c678bc4fSBill Paul {
835c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
836c678bc4fSBill Paul 
837c678bc4fSBill Paul 	entry = SLIST_FIRST(&sc->lge_jfree_listhead);
838c678bc4fSBill Paul 
839c678bc4fSBill Paul 	if (entry == NULL) {
840c678bc4fSBill Paul #ifdef LGE_VERBOSE
8416b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "no free jumbo buffers\n");
842c678bc4fSBill Paul #endif
843c678bc4fSBill Paul 		return(NULL);
844c678bc4fSBill Paul 	}
845c678bc4fSBill Paul 
846c678bc4fSBill Paul 	SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries);
847c678bc4fSBill Paul 	SLIST_INSERT_HEAD(&sc->lge_jinuse_listhead, entry, jpool_entries);
848c678bc4fSBill Paul 	return(sc->lge_cdata.lge_jslots[entry->slot]);
849c678bc4fSBill Paul }
850c678bc4fSBill Paul 
851c678bc4fSBill Paul /*
852c678bc4fSBill Paul  * Release a jumbo buffer.
853c678bc4fSBill Paul  */
8549bee8811SAlfred Perlstein static void
8559bee8811SAlfred Perlstein lge_jfree(buf, args)
856914596abSAlfred Perlstein 	void			*buf;
857c678bc4fSBill Paul 	void			*args;
858c678bc4fSBill Paul {
859c678bc4fSBill Paul 	struct lge_softc	*sc;
860c678bc4fSBill Paul 	int		        i;
861c678bc4fSBill Paul 	struct lge_jpool_entry   *entry;
862c678bc4fSBill Paul 
863c678bc4fSBill Paul 	/* Extract the softc struct pointer. */
864c678bc4fSBill Paul 	sc = args;
865c678bc4fSBill Paul 
866c678bc4fSBill Paul 	if (sc == NULL)
867c678bc4fSBill Paul 		panic("lge_jfree: can't find softc pointer!");
868c678bc4fSBill Paul 
869c678bc4fSBill Paul 	/* calculate the slot this buffer belongs to */
870c678bc4fSBill Paul 	i = ((vm_offset_t)buf
871c678bc4fSBill Paul 	     - (vm_offset_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN;
872c678bc4fSBill Paul 
873c678bc4fSBill Paul 	if ((i < 0) || (i >= LGE_JSLOTS))
874c678bc4fSBill Paul 		panic("lge_jfree: asked to free buffer that we don't manage!");
875c678bc4fSBill Paul 
876c678bc4fSBill Paul 	entry = SLIST_FIRST(&sc->lge_jinuse_listhead);
877c678bc4fSBill Paul 	if (entry == NULL)
878c678bc4fSBill Paul 		panic("lge_jfree: buffer not in use!");
879c678bc4fSBill Paul 	entry->slot = i;
880c678bc4fSBill Paul 	SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead, jpool_entries);
881c678bc4fSBill Paul 	SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry, jpool_entries);
882c678bc4fSBill Paul 
883c678bc4fSBill Paul 	return;
884c678bc4fSBill Paul }
885c678bc4fSBill Paul 
886c678bc4fSBill Paul /*
887c678bc4fSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
888c678bc4fSBill Paul  * the higher level protocols.
889c678bc4fSBill Paul  */
8909bee8811SAlfred Perlstein static void
8919bee8811SAlfred Perlstein lge_rxeof(sc, cnt)
892c678bc4fSBill Paul 	struct lge_softc	*sc;
893c678bc4fSBill Paul 	int			cnt;
894c678bc4fSBill Paul {
895c678bc4fSBill Paul         struct mbuf		*m;
896c678bc4fSBill Paul         struct ifnet		*ifp;
897c678bc4fSBill Paul 	struct lge_rx_desc	*cur_rx;
898c678bc4fSBill Paul 	int			c, i, total_len = 0;
899c678bc4fSBill Paul 	u_int32_t		rxsts, rxctl;
900c678bc4fSBill Paul 
901fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
902c678bc4fSBill Paul 
903c678bc4fSBill Paul 	/* Find out how many frames were processed. */
904c678bc4fSBill Paul 	c = cnt;
905c678bc4fSBill Paul 	i = sc->lge_cdata.lge_rx_cons;
906c678bc4fSBill Paul 
907c678bc4fSBill Paul 	/* Suck them in. */
908c678bc4fSBill Paul 	while(c) {
909c678bc4fSBill Paul 		struct mbuf		*m0 = NULL;
910c678bc4fSBill Paul 
911c678bc4fSBill Paul 		cur_rx = &sc->lge_ldata->lge_rx_list[i];
912c678bc4fSBill Paul 		rxctl = cur_rx->lge_ctl;
913c678bc4fSBill Paul 		rxsts = cur_rx->lge_sts;
914c678bc4fSBill Paul 		m = cur_rx->lge_mbuf;
915c678bc4fSBill Paul 		cur_rx->lge_mbuf = NULL;
916c678bc4fSBill Paul 		total_len = LGE_RXBYTES(cur_rx);
917c678bc4fSBill Paul 		LGE_INC(i, LGE_RX_LIST_CNT);
918c678bc4fSBill Paul 		c--;
919c678bc4fSBill Paul 
920c678bc4fSBill Paul 		/*
921c678bc4fSBill Paul 		 * If an error occurs, update stats, clear the
922c678bc4fSBill Paul 		 * status word and leave the mbuf cluster in place:
923c678bc4fSBill Paul 		 * it should simply get re-used next time this descriptor
924c678bc4fSBill Paul 	 	 * comes up in the ring.
925c678bc4fSBill Paul 		 */
926c678bc4fSBill Paul 		if (rxctl & LGE_RXCTL_ERRMASK) {
927c678bc4fSBill Paul 			ifp->if_ierrors++;
928c678bc4fSBill Paul 			lge_newbuf(sc, &LGE_RXTAIL(sc), m);
929c678bc4fSBill Paul 			continue;
930c678bc4fSBill Paul 		}
931c678bc4fSBill Paul 
932c678bc4fSBill Paul 		if (lge_newbuf(sc, &LGE_RXTAIL(sc), NULL) == ENOBUFS) {
933f5eece3fSBosko Milekic 			m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN,
934f5eece3fSBosko Milekic 			    ifp, NULL);
935c678bc4fSBill Paul 			lge_newbuf(sc, &LGE_RXTAIL(sc), m);
936c678bc4fSBill Paul 			if (m0 == NULL) {
9376b9f5c94SGleb Smirnoff 				device_printf(sc->lge_dev, "no receive buffers "
93878de678eSJohn Baldwin 				    "available -- packet dropped!\n");
939c678bc4fSBill Paul 				ifp->if_ierrors++;
940c678bc4fSBill Paul 				continue;
941c678bc4fSBill Paul 			}
942c678bc4fSBill Paul 			m = m0;
943c678bc4fSBill Paul 		} else {
944c678bc4fSBill Paul 			m->m_pkthdr.rcvif = ifp;
945c678bc4fSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
946c678bc4fSBill Paul 		}
947c678bc4fSBill Paul 
948c678bc4fSBill Paul 		ifp->if_ipackets++;
949c678bc4fSBill Paul 
950c678bc4fSBill Paul 		/* Do IP checksum checking. */
951c678bc4fSBill Paul 		if (rxsts & LGE_RXSTS_ISIP)
952c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
953c678bc4fSBill Paul 		if (!(rxsts & LGE_RXSTS_IPCSUMERR))
954c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
955c678bc4fSBill Paul 		if ((rxsts & LGE_RXSTS_ISTCP &&
956c678bc4fSBill Paul 		    !(rxsts & LGE_RXSTS_TCPCSUMERR)) ||
957c678bc4fSBill Paul 		    (rxsts & LGE_RXSTS_ISUDP &&
958c678bc4fSBill Paul 		    !(rxsts & LGE_RXSTS_UDPCSUMERR))) {
959c678bc4fSBill Paul 			m->m_pkthdr.csum_flags |=
960c678bc4fSBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
961c9215605SBill Paul 			m->m_pkthdr.csum_data = 0xffff;
962c678bc4fSBill Paul 		}
963c9215605SBill Paul 
964b05223a3SJohn Baldwin 		LGE_UNLOCK(sc);
965673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
966b05223a3SJohn Baldwin 		LGE_LOCK(sc);
967c678bc4fSBill Paul 	}
968c678bc4fSBill Paul 
969c678bc4fSBill Paul 	sc->lge_cdata.lge_rx_cons = i;
970c678bc4fSBill Paul 
971c678bc4fSBill Paul 	return;
972c678bc4fSBill Paul }
973c678bc4fSBill Paul 
97437c84183SPoul-Henning Kamp static void
9759bee8811SAlfred Perlstein lge_rxeoc(sc)
976c678bc4fSBill Paul 	struct lge_softc	*sc;
977c678bc4fSBill Paul {
978c678bc4fSBill Paul 	struct ifnet		*ifp;
979c678bc4fSBill Paul 
980fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
98113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
982b05223a3SJohn Baldwin 	lge_init_locked(sc);
983c678bc4fSBill Paul 	return;
984c678bc4fSBill Paul }
985c678bc4fSBill Paul 
986c678bc4fSBill Paul /*
987c678bc4fSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
988c678bc4fSBill Paul  * the list buffers.
989c678bc4fSBill Paul  */
990c678bc4fSBill Paul 
9919bee8811SAlfred Perlstein static void
9929bee8811SAlfred Perlstein lge_txeof(sc)
993c678bc4fSBill Paul 	struct lge_softc	*sc;
994c678bc4fSBill Paul {
995c678bc4fSBill Paul 	struct lge_tx_desc	*cur_tx = NULL;
996c678bc4fSBill Paul 	struct ifnet		*ifp;
997c678bc4fSBill Paul 	u_int32_t		idx, txdone;
998c678bc4fSBill Paul 
999fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
1000c678bc4fSBill Paul 
1001c678bc4fSBill Paul 	/* Clear the timeout timer. */
1002e55bc015SJohn Baldwin 	sc->lge_timer = 0;
1003c678bc4fSBill Paul 
1004c678bc4fSBill Paul 	/*
1005c678bc4fSBill Paul 	 * Go through our tx list and free mbufs for those
1006c678bc4fSBill Paul 	 * frames that have been transmitted.
1007c678bc4fSBill Paul 	 */
1008c678bc4fSBill Paul 	idx = sc->lge_cdata.lge_tx_cons;
1009c678bc4fSBill Paul 	txdone = CSR_READ_1(sc, LGE_TXDMADONE_8BIT);
1010c678bc4fSBill Paul 
1011c678bc4fSBill Paul 	while (idx != sc->lge_cdata.lge_tx_prod && txdone) {
1012c678bc4fSBill Paul 		cur_tx = &sc->lge_ldata->lge_tx_list[idx];
1013c678bc4fSBill Paul 
1014c678bc4fSBill Paul 		ifp->if_opackets++;
1015c678bc4fSBill Paul 		if (cur_tx->lge_mbuf != NULL) {
1016c678bc4fSBill Paul 			m_freem(cur_tx->lge_mbuf);
1017c678bc4fSBill Paul 			cur_tx->lge_mbuf = NULL;
1018c678bc4fSBill Paul 		}
1019c678bc4fSBill Paul 		cur_tx->lge_ctl = 0;
1020c678bc4fSBill Paul 
1021c678bc4fSBill Paul 		txdone--;
1022c678bc4fSBill Paul 		LGE_INC(idx, LGE_TX_LIST_CNT);
1023e55bc015SJohn Baldwin 		sc->lge_timer = 0;
1024c678bc4fSBill Paul 	}
1025c678bc4fSBill Paul 
1026c678bc4fSBill Paul 	sc->lge_cdata.lge_tx_cons = idx;
1027c678bc4fSBill Paul 
1028c678bc4fSBill Paul 	if (cur_tx != NULL)
102913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1030c678bc4fSBill Paul 
1031c678bc4fSBill Paul 	return;
1032c678bc4fSBill Paul }
1033c678bc4fSBill Paul 
10349bee8811SAlfred Perlstein static void
10359bee8811SAlfred Perlstein lge_tick(xsc)
1036c678bc4fSBill Paul 	void			*xsc;
1037c678bc4fSBill Paul {
1038c678bc4fSBill Paul 	struct lge_softc	*sc;
1039c678bc4fSBill Paul 	struct mii_data		*mii;
1040c678bc4fSBill Paul 	struct ifnet		*ifp;
1041c678bc4fSBill Paul 
1042c678bc4fSBill Paul 	sc = xsc;
1043fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
1044b05223a3SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
1045c678bc4fSBill Paul 
1046c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_SINGLE_COLL_PKTS);
1047c678bc4fSBill Paul 	ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1048c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_MULTI_COLL_PKTS);
1049c678bc4fSBill Paul 	ifp->if_collisions += CSR_READ_4(sc, LGE_STATSVAL);
1050c678bc4fSBill Paul 
1051c678bc4fSBill Paul 	if (!sc->lge_link) {
1052c678bc4fSBill Paul 		mii = device_get_softc(sc->lge_miibus);
1053c678bc4fSBill Paul 		mii_tick(mii);
1054c678bc4fSBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1055c678bc4fSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1056c678bc4fSBill Paul 			sc->lge_link++;
10576d6b7a18SPoul-Henning Kamp 			if (bootverbose &&
10586d6b7a18SPoul-Henning Kamp 		  	    (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX||
10596d6b7a18SPoul-Henning Kamp 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T))
10606b9f5c94SGleb Smirnoff 				device_printf(sc->lge_dev, "gigabit link up\n");
1061c678bc4fSBill Paul 			if (ifp->if_snd.ifq_head != NULL)
1062b05223a3SJohn Baldwin 				lge_start_locked(ifp);
1063c678bc4fSBill Paul 		}
1064c678bc4fSBill Paul 	}
1065c678bc4fSBill Paul 
1066e55bc015SJohn Baldwin 	if (sc->lge_timer != 0 && --sc->lge_timer == 0)
1067e55bc015SJohn Baldwin 		lge_watchdog(sc);
1068b05223a3SJohn Baldwin 	callout_reset(&sc->lge_stat_callout, hz, lge_tick, sc);
1069c678bc4fSBill Paul 
1070c678bc4fSBill Paul 	return;
1071c678bc4fSBill Paul }
1072c678bc4fSBill Paul 
10739bee8811SAlfred Perlstein static void
10749bee8811SAlfred Perlstein lge_intr(arg)
1075c678bc4fSBill Paul 	void			*arg;
1076c678bc4fSBill Paul {
1077c678bc4fSBill Paul 	struct lge_softc	*sc;
1078c678bc4fSBill Paul 	struct ifnet		*ifp;
1079c678bc4fSBill Paul 	u_int32_t		status;
1080c678bc4fSBill Paul 
1081c678bc4fSBill Paul 	sc = arg;
1082fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
1083b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1084c678bc4fSBill Paul 
1085c678bc4fSBill Paul 	/* Supress unwanted interrupts */
1086c678bc4fSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1087c678bc4fSBill Paul 		lge_stop(sc);
1088b05223a3SJohn Baldwin 		LGE_UNLOCK(sc);
1089c678bc4fSBill Paul 		return;
1090c678bc4fSBill Paul 	}
1091c678bc4fSBill Paul 
1092c678bc4fSBill Paul 	for (;;) {
1093c678bc4fSBill Paul 		/*
1094c678bc4fSBill Paul 		 * Reading the ISR register clears all interrupts, and
1095c678bc4fSBill Paul 		 * clears the 'interrupts enabled' bit in the IMR
1096c678bc4fSBill Paul 		 * register.
1097c678bc4fSBill Paul 		 */
1098c678bc4fSBill Paul 		status = CSR_READ_4(sc, LGE_ISR);
1099c678bc4fSBill Paul 
1100c678bc4fSBill Paul 		if ((status & LGE_INTRS) == 0)
1101c678bc4fSBill Paul 			break;
1102c678bc4fSBill Paul 
1103c678bc4fSBill Paul 		if ((status & (LGE_ISR_TXCMDFIFO_EMPTY|LGE_ISR_TXDMA_DONE)))
1104c678bc4fSBill Paul 			lge_txeof(sc);
1105c678bc4fSBill Paul 
1106c678bc4fSBill Paul 		if (status & LGE_ISR_RXDMA_DONE)
1107c678bc4fSBill Paul 			lge_rxeof(sc, LGE_RX_DMACNT(status));
1108c678bc4fSBill Paul 
1109c678bc4fSBill Paul 		if (status & LGE_ISR_RXCMDFIFO_EMPTY)
1110c678bc4fSBill Paul 			lge_rxeoc(sc);
1111c678bc4fSBill Paul 
1112c678bc4fSBill Paul 		if (status & LGE_ISR_PHY_INTR) {
1113c678bc4fSBill Paul 			sc->lge_link = 0;
1114b05223a3SJohn Baldwin 			callout_stop(&sc->lge_stat_callout);
1115c678bc4fSBill Paul 			lge_tick(sc);
1116c678bc4fSBill Paul 		}
1117c678bc4fSBill Paul 	}
1118c678bc4fSBill Paul 
1119c678bc4fSBill Paul 	/* Re-enable interrupts. */
1120c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|LGE_IMR_INTR_ENB);
1121c678bc4fSBill Paul 
1122c678bc4fSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1123b05223a3SJohn Baldwin 		lge_start_locked(ifp);
1124c678bc4fSBill Paul 
1125b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1126c678bc4fSBill Paul 	return;
1127c678bc4fSBill Paul }
1128c678bc4fSBill Paul 
1129c678bc4fSBill Paul /*
1130c678bc4fSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1131c678bc4fSBill Paul  * pointers to the fragment pointers.
1132c678bc4fSBill Paul  */
11339bee8811SAlfred Perlstein static int
11349bee8811SAlfred Perlstein lge_encap(sc, m_head, txidx)
1135c678bc4fSBill Paul 	struct lge_softc	*sc;
1136c678bc4fSBill Paul 	struct mbuf		*m_head;
1137c678bc4fSBill Paul 	u_int32_t		*txidx;
1138c678bc4fSBill Paul {
1139c678bc4fSBill Paul 	struct lge_frag		*f = NULL;
1140c678bc4fSBill Paul 	struct lge_tx_desc	*cur_tx;
1141c678bc4fSBill Paul 	struct mbuf		*m;
1142c678bc4fSBill Paul 	int			frag = 0, tot_len = 0;
1143c678bc4fSBill Paul 
1144c678bc4fSBill Paul 	/*
1145c678bc4fSBill Paul  	 * Start packing the mbufs in this chain into
1146c678bc4fSBill Paul 	 * the fragment pointers. Stop when we run out
1147c678bc4fSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1148c678bc4fSBill Paul 	 */
1149c678bc4fSBill Paul 	m = m_head;
1150c678bc4fSBill Paul 	cur_tx = &sc->lge_ldata->lge_tx_list[*txidx];
1151c678bc4fSBill Paul 	frag = 0;
1152c678bc4fSBill Paul 
1153c678bc4fSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1154c678bc4fSBill Paul 		if (m->m_len != 0) {
1155c678bc4fSBill Paul 			tot_len += m->m_len;
1156c678bc4fSBill Paul 			f = &cur_tx->lge_frags[frag];
1157c678bc4fSBill Paul 			f->lge_fraglen = m->m_len;
1158c678bc4fSBill Paul 			f->lge_fragptr_lo = vtophys(mtod(m, vm_offset_t));
1159c678bc4fSBill Paul 			f->lge_fragptr_hi = 0;
1160c678bc4fSBill Paul 			frag++;
1161c678bc4fSBill Paul 		}
1162c678bc4fSBill Paul 	}
1163c678bc4fSBill Paul 
1164c678bc4fSBill Paul 	if (m != NULL)
1165c678bc4fSBill Paul 		return(ENOBUFS);
1166c678bc4fSBill Paul 
1167c678bc4fSBill Paul 	cur_tx->lge_mbuf = m_head;
1168c678bc4fSBill Paul 	cur_tx->lge_ctl = LGE_TXCTL_WANTINTR|LGE_FRAGCNT(frag)|tot_len;
11691c352ef7SBill Paul 	LGE_INC((*txidx), LGE_TX_LIST_CNT);
1170c678bc4fSBill Paul 
1171c678bc4fSBill Paul 	/* Queue for transmit */
1172c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_TXDESC_ADDR_LO, vtophys(cur_tx));
1173c678bc4fSBill Paul 
1174c678bc4fSBill Paul 	return(0);
1175c678bc4fSBill Paul }
1176c678bc4fSBill Paul 
1177c678bc4fSBill Paul /*
1178c678bc4fSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1179c678bc4fSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1180c678bc4fSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1181c678bc4fSBill Paul  * physical addresses.
1182c678bc4fSBill Paul  */
1183c678bc4fSBill Paul 
11849bee8811SAlfred Perlstein static void
11859bee8811SAlfred Perlstein lge_start(ifp)
1186c678bc4fSBill Paul 	struct ifnet		*ifp;
1187c678bc4fSBill Paul {
1188c678bc4fSBill Paul 	struct lge_softc	*sc;
1189b05223a3SJohn Baldwin 
1190b05223a3SJohn Baldwin 	sc = ifp->if_softc;
1191b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1192b05223a3SJohn Baldwin 	lge_start_locked(ifp);
1193b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1194b05223a3SJohn Baldwin }
1195b05223a3SJohn Baldwin 
1196b05223a3SJohn Baldwin static void
1197b05223a3SJohn Baldwin lge_start_locked(ifp)
1198b05223a3SJohn Baldwin 	struct ifnet		*ifp;
1199b05223a3SJohn Baldwin {
1200b05223a3SJohn Baldwin 	struct lge_softc	*sc;
1201c678bc4fSBill Paul 	struct mbuf		*m_head = NULL;
1202c678bc4fSBill Paul 	u_int32_t		idx;
1203c678bc4fSBill Paul 
1204c678bc4fSBill Paul 	sc = ifp->if_softc;
1205c678bc4fSBill Paul 
1206c678bc4fSBill Paul 	if (!sc->lge_link)
1207c678bc4fSBill Paul 		return;
1208c678bc4fSBill Paul 
1209c678bc4fSBill Paul 	idx = sc->lge_cdata.lge_tx_prod;
1210c678bc4fSBill Paul 
121113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
1212c678bc4fSBill Paul 		return;
1213c678bc4fSBill Paul 
1214c678bc4fSBill Paul 	while(sc->lge_ldata->lge_tx_list[idx].lge_mbuf == NULL) {
1215c678bc4fSBill Paul 		if (CSR_READ_1(sc, LGE_TXCMDFREE_8BIT) == 0)
1216c678bc4fSBill Paul 			break;
1217c678bc4fSBill Paul 
1218c678bc4fSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1219c678bc4fSBill Paul 		if (m_head == NULL)
1220c678bc4fSBill Paul 			break;
1221c678bc4fSBill Paul 
1222c678bc4fSBill Paul 		if (lge_encap(sc, m_head, &idx)) {
1223c678bc4fSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
122413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1225c678bc4fSBill Paul 			break;
1226c678bc4fSBill Paul 		}
1227c678bc4fSBill Paul 
1228c678bc4fSBill Paul 		/*
1229c678bc4fSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1230c678bc4fSBill Paul 		 * to him.
1231c678bc4fSBill Paul 		 */
1232673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
1233c678bc4fSBill Paul 	}
1234c678bc4fSBill Paul 
1235c678bc4fSBill Paul 	sc->lge_cdata.lge_tx_prod = idx;
1236c678bc4fSBill Paul 
1237c678bc4fSBill Paul 	/*
1238c678bc4fSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1239c678bc4fSBill Paul 	 */
1240e55bc015SJohn Baldwin 	sc->lge_timer = 5;
1241c678bc4fSBill Paul 
1242c678bc4fSBill Paul 	return;
1243c678bc4fSBill Paul }
1244c678bc4fSBill Paul 
12459bee8811SAlfred Perlstein static void
12469bee8811SAlfred Perlstein lge_init(xsc)
1247c678bc4fSBill Paul 	void			*xsc;
1248c678bc4fSBill Paul {
1249c678bc4fSBill Paul 	struct lge_softc	*sc = xsc;
1250b05223a3SJohn Baldwin 
1251b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1252b05223a3SJohn Baldwin 	lge_init_locked(sc);
1253b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1254b05223a3SJohn Baldwin }
1255b05223a3SJohn Baldwin 
1256b05223a3SJohn Baldwin static void
1257b05223a3SJohn Baldwin lge_init_locked(sc)
1258b05223a3SJohn Baldwin 	struct lge_softc	*sc;
1259b05223a3SJohn Baldwin {
1260fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->lge_ifp;
1261c678bc4fSBill Paul 
1262b05223a3SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
126313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1264c678bc4fSBill Paul 		return;
1265c678bc4fSBill Paul 
1266c678bc4fSBill Paul 	/*
1267c678bc4fSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1268c678bc4fSBill Paul 	 */
1269c678bc4fSBill Paul 	lge_stop(sc);
1270c678bc4fSBill Paul 	lge_reset(sc);
1271c678bc4fSBill Paul 
1272c678bc4fSBill Paul 	/* Set MAC address */
12734a0d6638SRuslan Ermilov 	CSR_WRITE_4(sc, LGE_PAR0, *(u_int32_t *)(&IF_LLADDR(sc->lge_ifp)[0]));
12744a0d6638SRuslan Ermilov 	CSR_WRITE_4(sc, LGE_PAR1, *(u_int32_t *)(&IF_LLADDR(sc->lge_ifp)[4]));
1275c678bc4fSBill Paul 
1276c678bc4fSBill Paul 	/* Init circular RX list. */
1277c678bc4fSBill Paul 	if (lge_list_rx_init(sc) == ENOBUFS) {
12786b9f5c94SGleb Smirnoff 		device_printf(sc->lge_dev, "initialization failed: no "
127978de678eSJohn Baldwin 		    "memory for rx buffers\n");
1280c678bc4fSBill Paul 		lge_stop(sc);
1281c678bc4fSBill Paul 		return;
1282c678bc4fSBill Paul 	}
1283c678bc4fSBill Paul 
1284c678bc4fSBill Paul 	/*
1285c678bc4fSBill Paul 	 * Init tx descriptors.
1286c678bc4fSBill Paul 	 */
1287c678bc4fSBill Paul 	lge_list_tx_init(sc);
1288c678bc4fSBill Paul 
1289c678bc4fSBill Paul 	/* Set initial value for MODE1 register. */
1290c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_UCAST|
1291c678bc4fSBill Paul 	    LGE_MODE1_TX_CRC|LGE_MODE1_TXPAD|
1292c678bc4fSBill Paul 	    LGE_MODE1_RX_FLOWCTL|LGE_MODE1_SETRST_CTL0|
1293c678bc4fSBill Paul 	    LGE_MODE1_SETRST_CTL1|LGE_MODE1_SETRST_CTL2);
1294c678bc4fSBill Paul 
1295c678bc4fSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1296c678bc4fSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1297c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1,
1298c678bc4fSBill Paul 		    LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_PROMISC);
1299c678bc4fSBill Paul 	} else {
1300c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_PROMISC);
1301c678bc4fSBill Paul 	}
1302c678bc4fSBill Paul 
1303c678bc4fSBill Paul 	/*
1304c678bc4fSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1305c678bc4fSBill Paul 	 */
1306c678bc4fSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1307c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1,
1308c678bc4fSBill Paul 		    LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_BCAST);
1309c678bc4fSBill Paul 	} else {
1310c678bc4fSBill Paul 		CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_BCAST);
1311c678bc4fSBill Paul 	}
1312c678bc4fSBill Paul 
1313c678bc4fSBill Paul 	/* Packet padding workaround? */
1314c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RMVPAD);
1315c678bc4fSBill Paul 
1316c678bc4fSBill Paul 	/* No error frames */
1317c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ERRPKTS);
1318c678bc4fSBill Paul 
1319c678bc4fSBill Paul 	/* Receive large frames */
1320c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_GIANTS);
1321c678bc4fSBill Paul 
1322c678bc4fSBill Paul 	/* Workaround: disable RX/TX flow control */
1323c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_TX_FLOWCTL);
1324c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_FLOWCTL);
1325c678bc4fSBill Paul 
1326c678bc4fSBill Paul 	/* Make sure to strip CRC from received frames */
1327c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_CRC);
1328c678bc4fSBill Paul 
1329c678bc4fSBill Paul 	/* Turn off magic packet mode */
1330c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_MPACK_ENB);
1331c678bc4fSBill Paul 
1332c678bc4fSBill Paul 	/* Turn off all VLAN stuff */
1333c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_VLAN_RX|LGE_MODE1_VLAN_TX|
1334c678bc4fSBill Paul 	    LGE_MODE1_VLAN_STRIP|LGE_MODE1_VLAN_INSERT);
1335c678bc4fSBill Paul 
1336c678bc4fSBill Paul 	/* Workarond: FIFO overflow */
1337c678bc4fSBill Paul 	CSR_WRITE_2(sc, LGE_RXFIFO_HIWAT, 0x3FFF);
1338c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL1|LGE_IMR_RXFIFO_WAT);
1339c678bc4fSBill Paul 
1340c678bc4fSBill Paul 	/*
1341c678bc4fSBill Paul 	 * Load the multicast filter.
1342c678bc4fSBill Paul 	 */
1343c678bc4fSBill Paul 	lge_setmulti(sc);
1344c678bc4fSBill Paul 
1345c678bc4fSBill Paul 	/*
1346c678bc4fSBill Paul 	 * Enable hardware checksum validation for all received IPv4
1347c678bc4fSBill Paul 	 * packets, do not reject packets with bad checksums.
1348c678bc4fSBill Paul 	 */
1349c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE2, LGE_MODE2_RX_IPCSUM|
1350c678bc4fSBill Paul 	    LGE_MODE2_RX_TCPCSUM|LGE_MODE2_RX_UDPCSUM|
1351c678bc4fSBill Paul 	    LGE_MODE2_RX_ERRCSUM);
1352c678bc4fSBill Paul 
1353c678bc4fSBill Paul 	/*
1354c678bc4fSBill Paul 	 * Enable the delivery of PHY interrupts based on
1355c678bc4fSBill Paul 	 * link/speed/duplex status chalges.
1356c678bc4fSBill Paul 	 */
1357c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_GMIIPOLL);
1358c678bc4fSBill Paul 
1359c678bc4fSBill Paul 	/* Enable receiver and transmitter. */
1360c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0);
1361c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_ENB);
1362c678bc4fSBill Paul 
1363c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_TXDESC_ADDR_HI, 0);
1364c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_TX_ENB);
1365c678bc4fSBill Paul 
1366c678bc4fSBill Paul 	/*
1367c678bc4fSBill Paul 	 * Enable interrupts.
1368c678bc4fSBill Paul 	 */
1369c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|
1370c678bc4fSBill Paul 	    LGE_IMR_SETRST_CTL1|LGE_IMR_INTR_ENB|LGE_INTRS);
1371c678bc4fSBill Paul 
1372b05223a3SJohn Baldwin 	lge_ifmedia_upd_locked(ifp);
1373c678bc4fSBill Paul 
137413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
137513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1376c678bc4fSBill Paul 
1377b05223a3SJohn Baldwin 	callout_reset(&sc->lge_stat_callout, hz, lge_tick, sc);
1378c678bc4fSBill Paul 
1379c678bc4fSBill Paul 	return;
1380c678bc4fSBill Paul }
1381c678bc4fSBill Paul 
1382c678bc4fSBill Paul /*
1383c678bc4fSBill Paul  * Set media options.
1384c678bc4fSBill Paul  */
13859bee8811SAlfred Perlstein static int
13869bee8811SAlfred Perlstein lge_ifmedia_upd(ifp)
1387c678bc4fSBill Paul 	struct ifnet		*ifp;
1388c678bc4fSBill Paul {
1389c678bc4fSBill Paul 	struct lge_softc	*sc;
1390b05223a3SJohn Baldwin 
1391b05223a3SJohn Baldwin 	sc = ifp->if_softc;
1392b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1393b05223a3SJohn Baldwin 	lge_ifmedia_upd_locked(ifp);
1394b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1395b05223a3SJohn Baldwin 
1396b05223a3SJohn Baldwin 	return(0);
1397b05223a3SJohn Baldwin }
1398b05223a3SJohn Baldwin 
1399b05223a3SJohn Baldwin static void
1400b05223a3SJohn Baldwin lge_ifmedia_upd_locked(ifp)
1401b05223a3SJohn Baldwin 	struct ifnet		*ifp;
1402b05223a3SJohn Baldwin {
1403b05223a3SJohn Baldwin 	struct lge_softc	*sc;
1404c678bc4fSBill Paul 	struct mii_data		*mii;
14053fcb7a53SMarius Strobl 	struct mii_softc	*miisc;
1406c678bc4fSBill Paul 
1407c678bc4fSBill Paul 	sc = ifp->if_softc;
1408c678bc4fSBill Paul 
1409b05223a3SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
1410c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
1411c678bc4fSBill Paul 	sc->lge_link = 0;
14123fcb7a53SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
14133fcb7a53SMarius Strobl 		PHY_RESET(miisc);
1414c678bc4fSBill Paul 	mii_mediachg(mii);
1415c678bc4fSBill Paul }
1416c678bc4fSBill Paul 
1417c678bc4fSBill Paul /*
1418c678bc4fSBill Paul  * Report current media status.
1419c678bc4fSBill Paul  */
14209bee8811SAlfred Perlstein static void
14219bee8811SAlfred Perlstein lge_ifmedia_sts(ifp, ifmr)
1422c678bc4fSBill Paul 	struct ifnet		*ifp;
1423c678bc4fSBill Paul 	struct ifmediareq	*ifmr;
1424c678bc4fSBill Paul {
1425c678bc4fSBill Paul 	struct lge_softc	*sc;
1426c678bc4fSBill Paul 	struct mii_data		*mii;
1427c678bc4fSBill Paul 
1428c678bc4fSBill Paul 	sc = ifp->if_softc;
1429c678bc4fSBill Paul 
1430b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1431c678bc4fSBill Paul 	mii = device_get_softc(sc->lge_miibus);
1432c678bc4fSBill Paul 	mii_pollstat(mii);
1433b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1434c678bc4fSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
1435c678bc4fSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
1436c678bc4fSBill Paul 
1437c678bc4fSBill Paul 	return;
1438c678bc4fSBill Paul }
1439c678bc4fSBill Paul 
14409bee8811SAlfred Perlstein static int
14419bee8811SAlfred Perlstein lge_ioctl(ifp, command, data)
1442c678bc4fSBill Paul 	struct ifnet		*ifp;
1443c678bc4fSBill Paul 	u_long			command;
1444c678bc4fSBill Paul 	caddr_t			data;
1445c678bc4fSBill Paul {
1446c678bc4fSBill Paul 	struct lge_softc	*sc = ifp->if_softc;
1447c678bc4fSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
1448c678bc4fSBill Paul 	struct mii_data		*mii;
1449b05223a3SJohn Baldwin 	int			error = 0;
1450c678bc4fSBill Paul 
1451c678bc4fSBill Paul 	switch(command) {
1452c678bc4fSBill Paul 	case SIOCSIFMTU:
1453b05223a3SJohn Baldwin 		LGE_LOCK(sc);
1454c678bc4fSBill Paul 		if (ifr->ifr_mtu > LGE_JUMBO_MTU)
1455c678bc4fSBill Paul 			error = EINVAL;
1456c678bc4fSBill Paul 		else
1457c678bc4fSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
1458b05223a3SJohn Baldwin 		LGE_UNLOCK(sc);
1459c678bc4fSBill Paul 		break;
1460c678bc4fSBill Paul 	case SIOCSIFFLAGS:
1461b05223a3SJohn Baldwin 		LGE_LOCK(sc);
1462c678bc4fSBill Paul 		if (ifp->if_flags & IFF_UP) {
146313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1464c678bc4fSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
1465c678bc4fSBill Paul 			    !(sc->lge_if_flags & IFF_PROMISC)) {
1466c678bc4fSBill Paul 				CSR_WRITE_4(sc, LGE_MODE1,
1467c678bc4fSBill Paul 				    LGE_MODE1_SETRST_CTL1|
1468c678bc4fSBill Paul 				    LGE_MODE1_RX_PROMISC);
146913f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1470c678bc4fSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
1471c678bc4fSBill Paul 			    sc->lge_if_flags & IFF_PROMISC) {
1472c678bc4fSBill Paul 				CSR_WRITE_4(sc, LGE_MODE1,
1473c678bc4fSBill Paul 				    LGE_MODE1_RX_PROMISC);
1474c678bc4fSBill Paul 			} else {
147513f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1476b05223a3SJohn Baldwin 				lge_init_locked(sc);
1477c678bc4fSBill Paul 			}
1478c678bc4fSBill Paul 		} else {
147913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1480c678bc4fSBill Paul 				lge_stop(sc);
1481c678bc4fSBill Paul 		}
1482c678bc4fSBill Paul 		sc->lge_if_flags = ifp->if_flags;
1483b05223a3SJohn Baldwin 		LGE_UNLOCK(sc);
1484c678bc4fSBill Paul 		error = 0;
1485c678bc4fSBill Paul 		break;
1486c678bc4fSBill Paul 	case SIOCADDMULTI:
1487c678bc4fSBill Paul 	case SIOCDELMULTI:
1488b05223a3SJohn Baldwin 		LGE_LOCK(sc);
1489c678bc4fSBill Paul 		lge_setmulti(sc);
1490b05223a3SJohn Baldwin 		LGE_UNLOCK(sc);
1491c678bc4fSBill Paul 		error = 0;
1492c678bc4fSBill Paul 		break;
1493c678bc4fSBill Paul 	case SIOCGIFMEDIA:
1494c678bc4fSBill Paul 	case SIOCSIFMEDIA:
1495c678bc4fSBill Paul 		mii = device_get_softc(sc->lge_miibus);
1496c678bc4fSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1497c678bc4fSBill Paul 		break;
1498c678bc4fSBill Paul 	default:
1499673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
1500c678bc4fSBill Paul 		break;
1501c678bc4fSBill Paul 	}
1502c678bc4fSBill Paul 
1503c678bc4fSBill Paul 	return(error);
1504c678bc4fSBill Paul }
1505c678bc4fSBill Paul 
15069bee8811SAlfred Perlstein static void
1507e55bc015SJohn Baldwin lge_watchdog(sc)
1508c678bc4fSBill Paul 	struct lge_softc	*sc;
1509e55bc015SJohn Baldwin {
1510e55bc015SJohn Baldwin 	struct ifnet		*ifp;
1511c678bc4fSBill Paul 
1512e55bc015SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
1513e55bc015SJohn Baldwin 	ifp = sc->lge_ifp;
1514c678bc4fSBill Paul 
1515c678bc4fSBill Paul 	ifp->if_oerrors++;
151678de678eSJohn Baldwin 	if_printf(ifp, "watchdog timeout\n");
1517c678bc4fSBill Paul 
1518c678bc4fSBill Paul 	lge_stop(sc);
1519c678bc4fSBill Paul 	lge_reset(sc);
152013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1521b05223a3SJohn Baldwin 	lge_init_locked(sc);
1522c678bc4fSBill Paul 
1523c678bc4fSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1524b05223a3SJohn Baldwin 		lge_start_locked(ifp);
1525c678bc4fSBill Paul }
1526c678bc4fSBill Paul 
1527c678bc4fSBill Paul /*
1528c678bc4fSBill Paul  * Stop the adapter and free any mbufs allocated to the
1529c678bc4fSBill Paul  * RX and TX lists.
1530c678bc4fSBill Paul  */
15319bee8811SAlfred Perlstein static void
15329bee8811SAlfred Perlstein lge_stop(sc)
1533c678bc4fSBill Paul 	struct lge_softc	*sc;
1534c678bc4fSBill Paul {
1535c678bc4fSBill Paul 	register int		i;
1536c678bc4fSBill Paul 	struct ifnet		*ifp;
1537c678bc4fSBill Paul 
1538b05223a3SJohn Baldwin 	LGE_LOCK_ASSERT(sc);
1539fc74a9f9SBrooks Davis 	ifp = sc->lge_ifp;
1540e55bc015SJohn Baldwin 	sc->lge_timer = 0;
1541b05223a3SJohn Baldwin 	callout_stop(&sc->lge_stat_callout);
1542c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_INTR_ENB);
1543c678bc4fSBill Paul 
1544c678bc4fSBill Paul 	/* Disable receiver and transmitter. */
1545c678bc4fSBill Paul 	CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ENB|LGE_MODE1_TX_ENB);
1546c678bc4fSBill Paul 	sc->lge_link = 0;
1547c678bc4fSBill Paul 
1548c678bc4fSBill Paul 	/*
1549c678bc4fSBill Paul 	 * Free data in the RX lists.
1550c678bc4fSBill Paul 	 */
1551c678bc4fSBill Paul 	for (i = 0; i < LGE_RX_LIST_CNT; i++) {
1552c678bc4fSBill Paul 		if (sc->lge_ldata->lge_rx_list[i].lge_mbuf != NULL) {
1553c678bc4fSBill Paul 			m_freem(sc->lge_ldata->lge_rx_list[i].lge_mbuf);
1554c678bc4fSBill Paul 			sc->lge_ldata->lge_rx_list[i].lge_mbuf = NULL;
1555c678bc4fSBill Paul 		}
1556c678bc4fSBill Paul 	}
1557c678bc4fSBill Paul 	bzero((char *)&sc->lge_ldata->lge_rx_list,
1558c678bc4fSBill Paul 		sizeof(sc->lge_ldata->lge_rx_list));
1559c678bc4fSBill Paul 
1560c678bc4fSBill Paul 	/*
1561c678bc4fSBill Paul 	 * Free the TX list buffers.
1562c678bc4fSBill Paul 	 */
1563c678bc4fSBill Paul 	for (i = 0; i < LGE_TX_LIST_CNT; i++) {
1564c678bc4fSBill Paul 		if (sc->lge_ldata->lge_tx_list[i].lge_mbuf != NULL) {
1565c678bc4fSBill Paul 			m_freem(sc->lge_ldata->lge_tx_list[i].lge_mbuf);
1566c678bc4fSBill Paul 			sc->lge_ldata->lge_tx_list[i].lge_mbuf = NULL;
1567c678bc4fSBill Paul 		}
1568c678bc4fSBill Paul 	}
1569c678bc4fSBill Paul 
1570c678bc4fSBill Paul 	bzero((char *)&sc->lge_ldata->lge_tx_list,
1571c678bc4fSBill Paul 		sizeof(sc->lge_ldata->lge_tx_list));
1572c678bc4fSBill Paul 
157313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1574c678bc4fSBill Paul 
1575c678bc4fSBill Paul 	return;
1576c678bc4fSBill Paul }
1577c678bc4fSBill Paul 
1578c678bc4fSBill Paul /*
1579c678bc4fSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
1580c678bc4fSBill Paul  * get confused by errant DMAs when rebooting.
1581c678bc4fSBill Paul  */
15826a087a87SPyun YongHyeon static int
15839bee8811SAlfred Perlstein lge_shutdown(dev)
1584c678bc4fSBill Paul 	device_t		dev;
1585c678bc4fSBill Paul {
1586c678bc4fSBill Paul 	struct lge_softc	*sc;
1587c678bc4fSBill Paul 
1588c678bc4fSBill Paul 	sc = device_get_softc(dev);
1589c678bc4fSBill Paul 
1590b05223a3SJohn Baldwin 	LGE_LOCK(sc);
1591c678bc4fSBill Paul 	lge_reset(sc);
1592c678bc4fSBill Paul 	lge_stop(sc);
1593b05223a3SJohn Baldwin 	LGE_UNLOCK(sc);
1594c678bc4fSBill Paul 
15956a087a87SPyun YongHyeon 	return (0);
1596c678bc4fSBill Paul }
1597